diff --git a/client/application.h b/client/application.h index 09deb367..d81c0391 100644 --- a/client/application.h +++ b/client/application.h @@ -134,7 +134,6 @@ class application : public singleton std::vector> scene_stack; std::weak_ptr last_scene; std::chrono::nanoseconds last_scene_cpu_time; - std::chrono::nanoseconds last_scene_gpu_time; void loop(); @@ -389,11 +388,6 @@ public: return instance().last_scene_cpu_time; } - static std::chrono::nanoseconds get_gpu_time() - { - return instance().last_scene_gpu_time; - } - static bool get_hand_tracking_supported() { return instance().hand_tracking_supported; diff --git a/client/render/scene_renderer.cpp b/client/render/scene_renderer.cpp index 7921a2e9..1e38b362 100644 --- a/client/render/scene_renderer.cpp +++ b/client/render/scene_renderer.cpp @@ -190,6 +190,12 @@ scene_renderer::scene_renderer(vk::raii::Device & device, vk::raii::PhysicalDevi res.fence = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled}); res.cb = std::move(cb); } + query_pool = vk::raii::QueryPool( + device, + vk::QueryPoolCreateInfo{ + .queryType = vk::QueryType::eTimestamp, + .queryCount = uint32_t(2 * frames_in_flight), + }); renderpass = create_renderpass(); @@ -531,8 +537,25 @@ void scene_renderer::start_frame() throw std::runtime_error("vkWaitForfences: " + vk::to_string(result)); device.resetFences(*f.fence); + if (f.query_pool_filled) + { + auto [res, timestamps] = query_pool.getResults( + current_frame_index * 2, + 2, + 2 * sizeof(uint64_t), + sizeof(uint64_t), + vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait); + + if (res == vk::Result::eSuccess) + { + gpu_time_s = (timestamps[1] - timestamps[0]) * application::get_physical_device_properties().limits.timestampPeriod / 1e9; + } + } + f.resources.clear(); f.cb.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit}); + f.cb.resetQueryPool(*query_pool, current_frame_index * 2, 2); + f.cb.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, current_frame_index * 2); // TODO: replace with vector and write to GPU in end_frame() f.staging_buffer_offset = 0; @@ -553,12 +576,14 @@ void scene_renderer::end_frame() { auto& f = current_frame(); + f.cb.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *query_pool, current_frame_index * 2 + 1); f.cb.end(); queue.submit(vk::SubmitInfo{ .commandBufferCount = 1, .pCommandBuffers = &*f.cb, }, *f.fence); + f.query_pool_filled = true; } scene_renderer::per_frame_resources & scene_renderer::current_frame() diff --git a/client/render/scene_renderer.h b/client/render/scene_renderer.h index 030416ae..6c9ad918 100644 --- a/client/render/scene_renderer.h +++ b/client/render/scene_renderer.h @@ -150,18 +150,21 @@ public: vk::raii::Fence fence = nullptr; vk::raii::CommandBuffer cb = nullptr; std::vector> resources; + bool query_pool_filled = false; - // Uniform buffer for per-view and per-instance data - // host visible, host coherent + // Uniform buffer for per-view and per-instance data + // host visible, host coherent size_t staging_buffer_offset; - buffer_allocation staging_buffer; + buffer_allocation staging_buffer; - // device local - // buffer_allocation uniform_buffer; + // device local + // buffer_allocation uniform_buffer; }; std::vector frame_resources; int current_frame_index; + vk::raii::QueryPool query_pool = nullptr; + double gpu_time_s = 0; per_frame_resources& current_frame(); @@ -183,6 +186,11 @@ public: void render(scene_data & scene, const std::array& clear_color, std::span info); void end_frame(); + double get_gpu_time() const + { + return gpu_time_s; + } + std::shared_ptr get_default_material() { return default_material; diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index e3db34e9..8099cb87 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -31,7 +31,6 @@ #include "utils/contains.h" #include "wivrn_client.h" #include "xr/passthrough.h" -#include #include #include "wivrn_discover.h" @@ -52,8 +51,6 @@ #include #include -using namespace std::chrono_literals; - static const std::string discover_service = "_wivrn._tcp.local."; static bool force_autoconnect = false; diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 9300a467..7b56d908 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -28,7 +28,6 @@ #include #include #include -#include #include "render/scene_renderer.h" #include "render/imgui_impl.h" diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index b76af058..4251fcb8 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -419,7 +419,7 @@ void scenes::lobby::gui_settings() cpu_time[offset] = application::get_cpu_time().count() * 1.0e-6; - gpu_time[offset] = application::get_gpu_time().count() * 1.0e-6; + gpu_time[offset] = renderer->get_gpu_time() * 1'000; offset = (offset + 1) % cpu_time.size(); ImPlot::PushStyleColor(ImPlotCol_PlotBg, IM_COL32(32, 32, 32, 64));