Add performance metrics plot

This commit is contained in:
Guillaume Meunier
2024-02-18 09:03:46 +01:00
parent e51a195f2c
commit 2e53f91f93
19 changed files with 518 additions and 156 deletions
+17 -12
View File
@@ -207,11 +207,16 @@ scenes::lobby::lobby()
};
servers.emplace(data.service.txt["cookie"], data);
}
auto json_show_plots = root["show_performance_metrics"];
if (json_show_plots.is_bool())
show_performance_metrics = json_show_plots.get_bool();
}
catch(std::exception& e)
{
spdlog::warn("Cannot read configuration: {}", e.what());
servers.clear();
show_performance_metrics = false;
}
spdlog::info("{} known server(s):", servers.size());
@@ -270,7 +275,9 @@ void scenes::lobby::save_config()
std::ofstream json(application::get_config_path() / "client.json");
json << "{\"servers\":[" << servers_str << "]}";
json << "{\"servers\":[" << servers_str << "],"
<< "\"show_performance_metrics\":" << std::boolalpha << show_performance_metrics
<< "}";
}
static std::string ip_address_to_string(const in_addr& addr)
@@ -478,7 +485,7 @@ static std::vector<XrCompositionLayerProjectionView> render_layer(std::vector<Xr
return layer_views;
}
void scenes::lobby::render()
void scenes::lobby::render(XrTime predicted_display_time, bool should_render)
{
if (async_session.valid() && async_session.poll() == utils::future_status::ready)
{
@@ -486,7 +493,7 @@ void scenes::lobby::render()
{
auto session = async_session.get();
if (session)
next_scene = stream::create(std::move(session));
next_scene = stream::create(std::move(session), show_performance_metrics);
async_session.reset();
}
@@ -508,6 +515,7 @@ void scenes::lobby::render()
update_server_list();
imgui_ctx->set_current();
if (!async_session.valid() && !next_scene && !ImGui::IsPopupOpen("", ImGuiPopupFlags_AnyPopup))
{
for(auto&& [cookie, data]: servers)
@@ -519,24 +527,21 @@ void scenes::lobby::render()
}
}
XrFrameState framestate = session.wait_frame();
if (!framestate.shouldRender)
if (!should_render)
{
spdlog::debug("framestate.shouldRender is false");
session.begin_frame();
session.end_frame(framestate.predictedDisplayTime, {});
session.end_frame(predicted_display_time, {});
return;
}
session.begin_frame();
auto [flags, views] = session.locate_views(viewconfig, framestate.predictedDisplayTime, world_space);
auto [flags, views] = session.locate_views(viewconfig, predicted_display_time, world_space);
assert(views.size() == swapchains_lobby.size());
input->apply(world_space, framestate.predictedDisplayTime);
input->apply(world_space, predicted_display_time);
XrCompositionLayerQuad imgui_layer = draw_gui(framestate.predictedDisplayTime);
XrCompositionLayerQuad imgui_layer = draw_gui(predicted_display_time);
assert(renderer);
renderer->start_frame();
@@ -572,7 +577,7 @@ void scenes::lobby::render()
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&imgui_layer));
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&controllers_layer));
session.end_frame(framestate.predictedDisplayTime, layers_base);
session.end_frame(predicted_display_time, layers_base);
}
void scenes::lobby::on_focused()
+6 -2
View File
@@ -81,7 +81,7 @@ class lobby : public scene_impl<lobby>
std::array<XrAction, 2> haptic_output;
std::string selected_item;
std::string hovered_item;
ImGuiID hovered_item;
std::vector<xr::swapchain> swapchains_lobby;
std::vector<xr::swapchain> swapchains_controllers;
@@ -110,6 +110,8 @@ class lobby : public scene_impl<lobby>
tab last_current_tab = tab::server_list;
ImTextureID about_picture;
bool show_performance_metrics = false;
void gui_connecting();
void gui_server_list();
void gui_add_server();
@@ -117,13 +119,15 @@ class lobby : public scene_impl<lobby>
void gui_about();
void gui_keyboard(ImVec2 size);
void vibrate_on_hover();
void connect(server_data& data);
public:
virtual ~lobby();
lobby();
void render() override;
void render(XrTime predicted_display_time, bool should_render) override;
void render_view(XrViewStateFlags flags, XrTime display_time, XrView & view, int swapchain_index, int image_index);
void on_unfocused() override;
+95 -38
View File
@@ -19,13 +19,14 @@
#include "application.h"
#include "imgui.h"
#include "implot.h"
#include "imgui_internal.h"
#include "lobby.h"
#include "version.h"
#include <spdlog/fmt/fmt.h>
#include <utils/strings.h>
#include "../external/IconsFontAwesome6.h"
#include "IconsFontAwesome6.h"
static void CenterTextH(const std::string& text)
{
@@ -58,6 +59,12 @@ static void CenterTextHV(const std::string& text)
ImGui::PopStyleVar();
}
void scenes::lobby::vibrate_on_hover()
{
if (ImGui::IsItemHovered())
hovered_item = ImGui::GetItemID();
}
void scenes::lobby::gui_connecting()
{
const ImVec2 button_size(220, 80);
@@ -97,13 +104,11 @@ void scenes::lobby::gui_connecting()
ImGui::CloseCurrentPopup();
}
if (ImGui::IsItemHovered())
hovered_item = "close";
vibrate_on_hover();
}
void scenes::lobby::gui_add_server()
{
const ImVec2 button_size(220, 80);
// TODO column widths
@@ -116,8 +121,7 @@ void scenes::lobby::gui_add_server()
static char buf[100];
ImGui::TableNextColumn();
ImGui::InputText("##Name", buf, sizeof(buf));
if (ImGui::IsItemHovered())
hovered_item = "name";
vibrate_on_hover();
ImGui::TableNextRow();
ImGui::TableNextColumn();
@@ -126,8 +130,7 @@ void scenes::lobby::gui_add_server()
static char buf2[100];
ImGui::TableNextColumn();
ImGui::InputText("##Hostname", buf2, sizeof(buf2));
if (ImGui::IsItemHovered())
hovered_item = "hostname";
vibrate_on_hover();
ImGui::TableNextRow();
ImGui::TableNextColumn();
@@ -136,8 +139,7 @@ void scenes::lobby::gui_add_server()
static int port;
ImGui::TableNextColumn();
ImGui::InputInt("##Port", &port);
if (ImGui::IsItemHovered())
hovered_item = "port";
vibrate_on_hover();
ImGui::EndTable();
@@ -154,8 +156,7 @@ void scenes::lobby::gui_add_server()
ImGui::SetCursorPosX(top_left.x);
if (ImGui::Button("Cancel", button_size))
current_tab = tab::server_list;
if (ImGui::IsItemHovered())
hovered_item = "cancel";
vibrate_on_hover();
ImGui::SameLine(bottom_right.x - button_size.x);
@@ -176,8 +177,7 @@ void scenes::lobby::gui_add_server()
save_config();
}
if (ImGui::IsItemHovered())
hovered_item = "save";
vibrate_on_hover();
}
void scenes::lobby::gui_server_list()
@@ -230,8 +230,7 @@ void scenes::lobby::gui_server_list()
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0,1,0,1));
if (ImGui::Checkbox(("Autoconnect##" + cookie).c_str(), &data.autoconnect))
save_config();
if (ImGui::IsItemHovered())
hovered_item = "autoconnect " + cookie;
vibrate_on_hover();
ImGui::PopStyleColor();
}
@@ -265,9 +264,7 @@ void scenes::lobby::gui_server_list()
connect(data);
ImGui::OpenPopup("connecting");
}
if (ImGui::IsItemHovered())
hovered_item = "connect " + cookie;
vibrate_on_hover();
ImGui::PopStyleColor(3);
ImGui::EndDisabled();
@@ -282,9 +279,7 @@ void scenes::lobby::gui_server_list()
if (ImGui::Button(("Remove##" + cookie).c_str(), button_size))
cookie_to_remove = cookie;
if (ImGui::IsItemHovered())
hovered_item = "remove " + cookie;
vibrate_on_hover();
ImGui::PopStyleColor(3);
}
@@ -317,6 +312,72 @@ void scenes::lobby::gui_server_list()
ImGui::PopStyleVar(3);
}
void scenes::lobby::gui_settings()
{
ImGuiStyle & style = ImGui::GetStyle();
if (ImGui::Checkbox("Show performance metrics", &show_performance_metrics))
save_config();
vibrate_on_hover();
if (show_performance_metrics)
{
float win_width = ImGui::GetWindowSize().x;
float win_height = ImGui::GetWindowSize().y;
ImVec2 plot_size{
win_width / 2 - style.ItemSpacing.x / 2,
win_height / 2
};
static std::array<float, 300> cpu_time;
static std::array<float, 300> gpu_time;
static int offset = 0;
float min_v = 0;
float max_v = 20;
cpu_time[offset] = application::get_cpu_time().count() * 1.0e-6;
gpu_time[offset] = application::get_gpu_time().count() * 1.0e-6;
offset = (offset + 1) % cpu_time.size();
ImPlot::PushStyleColor(ImPlotCol_PlotBg, IM_COL32(32, 32, 32, 64));
ImPlot::PushStyleColor(ImPlotCol_FrameBg, IM_COL32(0, 0, 0, 0));
ImPlot::PushStyleColor(ImPlotCol_AxisBg, IM_COL32(0, 0, 0, 0));
ImPlot::PushStyleColor(ImPlotCol_AxisBgActive, IM_COL32(0, 0, 0, 0));
ImPlot::PushStyleColor(ImPlotCol_AxisBgHovered, IM_COL32(0, 0, 0, 0));
if (ImPlot::BeginPlot("CPU time", plot_size, ImPlotFlags_CanvasOnly|ImPlotFlags_NoChild))
{
auto col = ImPlot::GetColormapColor(0);
ImPlot::SetupAxes(nullptr, "CPU time", ImPlotAxisFlags_NoDecorations, 0);
ImPlot::SetupAxesLimits(0, cpu_time.size() - 1, min_v, max_v, ImGuiCond_Always);
ImPlot::SetNextLineStyle(col);
ImPlot::SetNextFillStyle(col, 0.25);
ImPlot::PlotLine("CPU time", cpu_time.data(), cpu_time.size(), 1, 0, ImPlotLineFlags_Shaded, offset);
ImPlot::EndPlot();
}
ImGui::SameLine();
if (ImPlot::BeginPlot("GPU time", plot_size, ImPlotFlags_CanvasOnly|ImPlotFlags_NoChild))
{
auto col = ImPlot::GetColormapColor(1);
ImPlot::SetupAxes(nullptr,"GPU time [ms]", ImPlotAxisFlags_NoDecorations, 0);
ImPlot::SetupAxesLimits(0, gpu_time.size() - 1, min_v, max_v, ImGuiCond_Always);
ImPlot::SetNextLineStyle(col);
ImPlot::SetNextFillStyle(col, 0.25);
ImPlot::PlotLine("GPU time", gpu_time.data(), gpu_time.size(), 1, 0, ImPlotLineFlags_Shaded, offset);
ImPlot::EndPlot();
}
ImPlot::PopStyleColor(5);
}
}
void scenes::lobby::gui_about()
{
ImGui::PushFont(imgui_ctx->large_font);
@@ -408,9 +469,6 @@ XrCompositionLayerQuad scenes::lobby::draw_gui(XrTime predicted_display_time)
const float MinTabWidth = 300;
const float MaxTabWidth = 300;
int image_index = swapchain_imgui.acquire();
swapchain_imgui.wait();
std::optional<std::pair<glm::vec3, glm::quat>> head_position = application::locate_controller(application::view(), world_space, predicted_display_time);
if (head_position)
@@ -419,7 +477,7 @@ XrCompositionLayerQuad scenes::lobby::draw_gui(XrTime predicted_display_time)
}
auto last_hovered = hovered_item;
hovered_item = "";
hovered_item = 0;
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, {0, 0});
ImGui::PushStyleVar(ImGuiStyleVar_ScrollbarSize, 30);
@@ -452,6 +510,7 @@ XrCompositionLayerQuad scenes::lobby::draw_gui(XrTime predicted_display_time)
break;
case tab::settings:
gui_settings();
break;
case tab::about:
@@ -486,16 +545,18 @@ XrCompositionLayerQuad scenes::lobby::draw_gui(XrTime predicted_display_time)
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10,10));
RadioButtonWithoutCheckBox(ICON_FA_COMPUTER " Server list", &current_tab, tab::server_list, {TabWidth, 0});
if (ImGui::IsItemHovered())
hovered_item = "Server list";
vibrate_on_hover();
RadioButtonWithoutCheckBox(ICON_FA_GEARS " Settings", &current_tab, tab::settings, {TabWidth, 0});
vibrate_on_hover();
ImGui::SetCursorPosY(ImGui::GetContentRegionMax().y - 2*ImGui::GetCurrentContext()->FontSize - 4*style.FramePadding.y - style.ItemSpacing.y - style.WindowPadding.y);
RadioButtonWithoutCheckBox(ICON_FA_CIRCLE_INFO " About", &current_tab, tab::about, {TabWidth, 0});
if (ImGui::IsItemHovered())
hovered_item = "about";
vibrate_on_hover();
RadioButtonWithoutCheckBox(ICON_FA_DOOR_OPEN " Exit", &current_tab, tab::exit, {TabWidth, 0});
if (ImGui::IsItemHovered())
hovered_item = "exit";
vibrate_on_hover();
if (!ImGui::IsMouseDown(ImGuiMouseButton_Left))
{
@@ -513,16 +574,12 @@ XrCompositionLayerQuad scenes::lobby::draw_gui(XrTime predicted_display_time)
ImGui::PopStyleColor(); // ImGuiCol_WindowBg
ImGui::PopStyleVar(2); // ImGuiStyleVar_WindowPadding, ImGuiStyleVar_ScrollbarSize
if (hovered_item != last_hovered && hovered_item != "")
if (hovered_item != last_hovered && hovered_item != 0)
{
size_t controller = imgui_ctx->get_focused_controller();
if (controller < haptic_output.size())
application::haptic_start(haptic_output[controller], XR_NULL_PATH, 10'000'000, 1000, 1);
}
imgui_ctx->render(swapchain_imgui.images()[image_index].image);
swapchain_imgui.release();
return imgui_ctx->composition_layer(swapchain_imgui);
return imgui_ctx->end_frame();
}
+225 -41
View File
@@ -17,8 +17,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <vulkan/vulkan.hpp>
#include <vulkan/vulkan_enums.hpp>
#include <vulkan/vulkan_raii.hpp>
#define GLM_FORCE_RADIANS
@@ -38,6 +36,7 @@
#include <vulkan/vulkan_core.h>
#include "audio/audio.h"
#include "hardware.h"
#include "implot.h"
using namespace xrt::drivers::wivrn;
@@ -70,13 +69,19 @@ static const std::unordered_map<std::string, device_id> device_ids = {
};
// clang-format on
std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_session> network_session)
static const std::array supported_formats =
{
vk::Format::eR8G8B8A8Srgb,
vk::Format::eB8G8R8A8Srgb
};
std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_session> network_session, bool show_performance_metrics)
{
std::shared_ptr<stream> self{new stream};
spdlog::info("decoder_mutex.native_handle() = {}", (void*)self->decoder_mutex.native_handle());
self->network_session = std::move(network_session);
self->show_performance_metrics = show_performance_metrics;
from_headset::headset_info_packet info{};
@@ -137,9 +142,59 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
self->input_actions.emplace_back(it->second, action, action_type);
}
self->swapchain_format = vk::Format::eUndefined;
spdlog::info("Supported swapchain formats:");
for (auto format: self->session.get_swapchain_formats())
{
spdlog::info(" {}", vk::to_string(format));
}
for (auto format: self->session.get_swapchain_formats())
{
if (std::find(supported_formats.begin(), supported_formats.end(), format) != supported_formats.end())
{
self->swapchain_format = format;
break;
}
}
if (self->swapchain_format == vk::Format::eUndefined)
throw std::runtime_error("No supported swapchain format");
spdlog::info("Using format {}", vk::to_string(self->swapchain_format));
return self;
}
void scenes::stream::on_focused()
{
if (show_performance_metrics)
{
swapchain_imgui = xr::swapchain(
session,
device,
swapchain_format,
1500, 1000);
imgui_ctx.emplace(physical_device,
device,
queue_family_index,
queue,
view_space,
std::span<imgui_context::controller>{},
swapchain_imgui,
glm::vec2{1.0, 0.6666});
imgui_ctx->set_position({0,0,-1}, {1,0,0,0});
}
}
void scenes::stream::on_unfocused()
{
imgui_ctx.reset();
swapchain_imgui.reset();
}
scenes::stream::~stream()
{
exit();
@@ -234,21 +289,169 @@ std::shared_ptr<shard_accumulator::blit_handle> scenes::stream::accumulator_imag
return nullptr;
}
void scenes::stream::render()
scenes::stream::metric scenes::stream::get_metrics(XrTime predicted_display_time)
{
uint64_t rx = network_session->bytes_received();
uint64_t tx = network_session->bytes_sent();
float dt = (predicted_display_time - last_metric_time) * 1e-9f;
float bandwidth_rx = float(rx - bytes_received) / dt;
float bandwidth_tx = float(tx - bytes_sent ) /dt;
last_metric_time = predicted_display_time;
bytes_received = rx;
bytes_sent = tx;
return metric{
.cpu_time = application::get_cpu_time().count() * 1e-9f,
.gpu_time = application::get_gpu_time().count() * 1e-9f,
.bandwidth_rx = bandwidth_rx * 8,
.bandwidth_tx = bandwidth_tx * 8,
};
}
static float compute_plot_max_value(float * data, int count, ptrdiff_t stride)
{
float max = 0;
uintptr_t ptr = (uintptr_t)data;
for(int i = 0; i < count; i++)
{
max = std::max(max, *(float*)(ptr + i * stride));
}
// First power of 10 less than the max
float x = pow(10, floor(log10(max)));
return ceil(max / x) * x;
}
static std::pair<float, std::string> compute_plot_unit(float max_value)
{
if (max_value > 1e9)
return { 1e-9, "G" };
if (max_value > 1e6)
return { 1e-6, "M" };
if (max_value > 1e3)
return { 1e-3, "k" };
if (max_value > 1)
return { 1, "" };
if (max_value > 1e-3)
return { 1e3, "m" };
if (max_value > 1e-6)
return { 1e6, "u" };
return { 1e9, "n" };
}
struct getter_data
{
uintptr_t data;
int stride;
int offset;
int count;
float multiplier;
};
static ImPlotPoint getter(int index, void * data_)
{
getter_data& data = *(getter_data*)data_;
int offset_index = (index + data.offset) % data.count;
return ImPlotPoint(index, *(float*)(data.data + offset_index * data.stride) * data.multiplier);
}
XrCompositionLayerQuad scenes::stream::plot_performance_metrics(XrTime predicted_display_time)
{
imgui_ctx->new_frame(predicted_display_time);
const ImGuiStyle & style = ImGui::GetStyle();
ImGui::SetNextWindowPos({0, 0});
ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size);
ImGui::Begin("Performance metrics", nullptr,
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove);
ImVec2 window_size = ImGui::GetWindowSize() - ImVec2(2,2) * style.WindowPadding;
metrics[metrics_offset] = get_metrics(predicted_display_time);
metrics_offset = (metrics_offset + 1) % metrics.size();
static const std::array plots = {
std::make_tuple(ImPlot::GetColormapColor(0), "CPU time", "s", &metric::cpu_time),
std::make_tuple(ImPlot::GetColormapColor(1), "GPU time", "s", &metric::gpu_time),
std::make_tuple(ImPlot::GetColormapColor(2), "Download", "bit/s", &metric::bandwidth_rx),
std::make_tuple(ImPlot::GetColormapColor(3), "Upload", "bit/s", &metric::bandwidth_tx),
};
axis_scale.resize(plots.size());
int n_cols = 2;
int n_rows = ceil((float)plots.size() / n_cols);
ImVec2 plot_size = ImVec2(
window_size.x / n_cols - style.ItemSpacing.x * (n_cols-1) / n_cols,
window_size.y / n_rows - style.ItemSpacing.y * (n_rows-1) / n_rows);
ImPlot::PushStyleColor(ImPlotCol_PlotBg, IM_COL32(32, 32, 32, 64));
ImPlot::PushStyleColor(ImPlotCol_FrameBg, IM_COL32(0, 0, 0, 0));
ImPlot::PushStyleColor(ImPlotCol_AxisBg, IM_COL32(0, 0, 0, 0));
ImPlot::PushStyleColor(ImPlotCol_AxisBgActive, IM_COL32(0, 0, 0, 0));
ImPlot::PushStyleColor(ImPlotCol_AxisBgHovered, IM_COL32(0, 0, 0, 0));
int n = 0;
for(auto [color, title, unit, data]: plots)
{
if (ImPlot::BeginPlot(title, plot_size, ImPlotFlags_CanvasOnly|ImPlotFlags_NoChild))
{
float min_v = 0;
float max_v = compute_plot_max_value(&(metrics.data()->*data), metrics.size(), sizeof(metric));
auto [ multiplier, prefix ] = compute_plot_unit(max_v);
if (axis_scale[n] == 0)
axis_scale[n] = max_v;
else
axis_scale[n] = 0.99 * axis_scale[n] + 0.01 * max_v;
getter_data gdata{
.data = (uintptr_t)&(metrics.data()->*data),
.stride = sizeof(metric),
.offset = metrics_offset,
.count = (int)metrics.size(),
.multiplier = multiplier
};
std::string title_with_units = std::string(title) + " [" + prefix + unit + "]";
ImPlot::SetupAxes(nullptr, title_with_units.c_str(), ImPlotAxisFlags_NoDecorations, 0);
ImPlot::SetupAxesLimits(0, metrics.size() - 1, min_v * multiplier, axis_scale[n] * multiplier, ImGuiCond_Always);
ImPlot::SetNextLineStyle(color);
ImPlot::SetNextFillStyle(color, 0.25);
ImPlot::PlotLineG(title, getter, &gdata, metrics.size(), ImPlotLineFlags_Shaded);
// ImPlot::PlotLine(title, &(metrics.data()->*data), metrics.size(), 1, 0, ImPlotLineFlags_Shaded, metrics_offset, sizeof(metric));
ImPlot::EndPlot();
}
if (++n % 2 != 0)
ImGui::SameLine();
}
ImPlot::PopStyleColor(5);
ImGui::End();
return imgui_ctx->end_frame();
}
void scenes::stream::render(XrTime predicted_display_time, bool should_render)
{
if (exiting)
application::pop_scene();
XrFrameState framestate = session.wait_frame();
if (decoders.empty())
framestate.shouldRender = false;
should_render = false;
if (!framestate.shouldRender)
if (!should_render)
{
// TODO: stop/restart video stream
session.begin_frame();
session.end_frame(framestate.predictedDisplayTime, {});
session.end_frame(predicted_display_time, {});
std::unique_lock lock(decoder_mutex);
for (auto & i: decoders)
@@ -262,7 +465,7 @@ void scenes::stream::render()
session.begin_frame();
auto [flags, views] = session.locate_views(viewconfig, framestate.predictedDisplayTime, world_space);
auto [flags, views] = session.locate_views(viewconfig, predicted_display_time, world_space);
assert(views.size() == swapchains.size());
std::array<int, view_count> image_indices;
@@ -326,7 +529,7 @@ void scenes::stream::render()
current_blit_handles.push_back(blit_handle);
blit_handle->feedback.blitted = application::now();
blit_handle->feedback.displayed = framestate.predictedDisplayTime;
blit_handle->feedback.displayed = predicted_display_time;
blit_handle->feedback.real_pose[0] = views[0].pose;
blit_handle->feedback.real_pose[1] = views[1].pose;
@@ -392,7 +595,7 @@ void scenes::stream::render()
layer_view[swapchain_index].subImage.imageRect.extent.height = swapchains[swapchain_index].height();
}
float brightness = std::clamp<float>(dbrightness * (framestate.predictedDisplayTime - first_frame_time) / 1.e9, 0, 1);
float brightness = std::clamp<float>(dbrightness * (predicted_display_time - first_frame_time) / 1.e9, 0, 1);
XrCompositionLayerColorScaleBiasKHR color_scale_bias{
.type = XR_TYPE_COMPOSITION_LAYER_COLOR_SCALE_BIAS_KHR,
@@ -410,8 +613,16 @@ void scenes::stream::render()
if (instance.has_extension(XR_KHR_COMPOSITION_LAYER_COLOR_SCALE_BIAS_EXTENSION_NAME))
layer.next = &color_scale_bias;
XrCompositionLayerQuad imgui_layer;
if (imgui_ctx)
imgui_layer = plot_performance_metrics(predicted_display_time);
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&layer));
session.end_frame(/*timestamp*/ framestate.predictedDisplayTime, layers_base);
if (imgui_ctx)
layers_base.push_back(reinterpret_cast<XrCompositionLayerBaseHeader *>(&imgui_layer));
session.end_frame(/*timestamp*/ predicted_display_time, layers_base);
// Network operations may be blocking, do them once everything was submitted
for (const auto& handle: current_blit_handles)
@@ -433,12 +644,6 @@ void scenes::stream::exit()
shard_queue.close();
}
static const std::array supported_formats =
{
vk::Format::eR8G8B8A8Srgb,
vk::Format::eB8G8R8A8Srgb
};
void scenes::stream::setup(const to_headset::video_stream_description & description)
{
std::unique_lock lock(decoder_mutex);
@@ -459,27 +664,6 @@ void scenes::stream::setup(const to_headset::video_stream_description & descript
const uint32_t swapchain_width = video_width / description.foveation[0].x.scale;
const uint32_t swapchain_height = video_height / description.foveation[0].y.scale;
swapchain_format = vk::Format::eUndefined;
spdlog::info("Supported swapchain formats:");
for (auto format: session.get_swapchain_formats())
{
spdlog::info(" {}", vk::to_string(format));
}
for (auto format: session.get_swapchain_formats())
{
if (std::find(supported_formats.begin(), supported_formats.end(), format) != supported_formats.end())
{
swapchain_format = format;
break;
}
}
if (swapchain_format == vk::Format::eUndefined)
throw std::runtime_error("No supported swapchain format");
spdlog::info("Using format {}", vk::to_string(swapchain_format));
auto views = system.view_configuration_views(viewconfig);
swapchains.reserve(views.size());
+28 -2
View File
@@ -29,6 +29,7 @@
#include <thread>
#include <vulkan/vulkan_core.h>
#include "audio/audio.h"
#include "render/imgui_impl.h"
namespace scenes
{
@@ -82,20 +83,26 @@ class stream : public scene_impl<stream>, public std::enable_shared_from_this<st
bool video_started_ = false;
XrTime first_frame_time{};
const float dbrightness = 2;
bool show_performance_metrics = false;
std::vector<xr::swapchain> swapchains;
xr::swapchain swapchain_imgui;
vk::Format swapchain_format;
std::optional<audio> audio_handle;
std::optional<imgui_context> imgui_ctx;
stream() = default;
public:
~stream();
static std::shared_ptr<stream> create(std::unique_ptr<wivrn_session> session);
static std::shared_ptr<stream> create(std::unique_ptr<wivrn_session> session, bool show_performance_metrics);
void render() override;
void render(XrTime predicted_display_time, bool should_render) override;
void on_focused() override;
void on_unfocused() override;
void operator()(to_headset::handshake&&) {};
void operator()(to_headset::video_stream_data_shard &&);
@@ -129,5 +136,24 @@ private:
void setup(const to_headset::video_stream_description &);
void exit();
uint64_t bytes_sent = 0;
uint64_t bytes_received = 0;
struct metric
{
float cpu_time = 0;
float gpu_time = 0;
float bandwidth_rx = 0;
float bandwidth_tx = 0;
};
std::vector<metric> metrics{300};
std::vector<float> axis_scale;
XrTime last_metric_time = 0;
int metrics_offset = 0;
metric get_metrics(XrTime predicted_display_time);
XrCompositionLayerQuad plot_performance_metrics(XrTime predicted_display_time);
};
} // namespace scenes