Add performance metrics plot
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
|
||||
|
||||
#include "imgui_impl.h"
|
||||
#include "implot.h"
|
||||
|
||||
#include "application.h"
|
||||
#include "asset.h"
|
||||
@@ -224,6 +225,7 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii:
|
||||
scale_(size.x, size.y),
|
||||
swapchain(swapchain),
|
||||
context(ImGui::CreateContext()),
|
||||
plot_context(ImPlot::CreateContext()),
|
||||
io((ImGui::SetCurrentContext(context), ImGui::GetIO())),
|
||||
world(world)
|
||||
{
|
||||
@@ -260,6 +262,7 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii:
|
||||
};
|
||||
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
ImGui_ImplVulkan_Init(&init_info, *renderpass);
|
||||
|
||||
// Load Fonts
|
||||
@@ -299,6 +302,7 @@ imgui_context::imgui_context(vk::raii::PhysicalDevice physical_device, vk::raii:
|
||||
void imgui_context::new_frame(XrTime display_time)
|
||||
{
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
|
||||
if (last_display_time)
|
||||
io.DeltaTime = std::min((display_time - last_display_time) * 1e-9f, 0.1f);
|
||||
@@ -447,18 +451,20 @@ void imgui_context::new_frame(XrTime display_time)
|
||||
draw_list->AddCircleFilled(*position, radius, button_pressed ? color_pressed : color_unpressed);
|
||||
draw_list->AddCircle(*position, radius*1.2, ImGui::GetColorU32(ImVec4(0, 0, 0, alpha)), 0, radius * 0.4);
|
||||
}
|
||||
|
||||
image_index = swapchain.acquire();
|
||||
swapchain.wait();
|
||||
}
|
||||
|
||||
void imgui_context::render(vk::Image destination)
|
||||
XrCompositionLayerQuad imgui_context::end_frame()
|
||||
{
|
||||
vk::Image destination = swapchain.images()[image_index].image;
|
||||
swapchain.release();
|
||||
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
|
||||
ImGui::Render();
|
||||
ImDrawData* draw_data = ImGui::GetDrawData();
|
||||
const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f);
|
||||
|
||||
if (is_minimized)
|
||||
return;
|
||||
|
||||
current_command_buffer = (current_command_buffer + 1) % command_buffers.size();
|
||||
|
||||
@@ -483,7 +489,7 @@ void imgui_context::render(vk::Image destination)
|
||||
.pClearValues = &clear
|
||||
}, vk::SubpassContents::eInline);
|
||||
|
||||
ImGui_ImplVulkan_RenderDrawData(draw_data, *cb);
|
||||
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), *cb);
|
||||
|
||||
cb.endRenderPass();
|
||||
|
||||
@@ -494,13 +500,26 @@ void imgui_context::render(vk::Image destination)
|
||||
.pCommandBuffers = &*cb,
|
||||
}, *fence);
|
||||
|
||||
// Use the aliasing constructor
|
||||
// return std::shared_ptr<vk::raii::ImageView>{viewport, &vp.frames[vp.frameindex].image_view_texture};
|
||||
return XrCompositionLayerQuad{
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_QUAD,
|
||||
.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
|
||||
.space = world,
|
||||
.eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH,
|
||||
.subImage = {
|
||||
.swapchain = swapchain,
|
||||
.imageRect = {
|
||||
.offset = {0, 0},
|
||||
.extent = {(int32_t)size.width, (int32_t)size.height}},
|
||||
},
|
||||
.pose = pose(),
|
||||
.size = scale(),
|
||||
};
|
||||
}
|
||||
|
||||
imgui_context::~imgui_context()
|
||||
{
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
|
||||
std::vector<vk::Fence> fences;
|
||||
|
||||
@@ -515,6 +534,7 @@ imgui_context::~imgui_context()
|
||||
device.waitForFences(fences, true, 1'000'000'000);
|
||||
|
||||
ImGui_ImplVulkan_Shutdown();
|
||||
ImPlot::DestroyContext();
|
||||
ImGui::DestroyContext(context);
|
||||
}
|
||||
|
||||
@@ -546,12 +566,13 @@ ImTextureID imgui_context::load_texture(const std::string& filename, vk::raii::S
|
||||
|
||||
device.updateDescriptorSets(ds_write, nullptr);
|
||||
|
||||
ImTextureID id = ds.release();
|
||||
ImTextureID id = *ds;
|
||||
|
||||
textures.emplace(id, texture_data{
|
||||
.sampler = std::move(sampler),
|
||||
.image = std::move(loader.image),
|
||||
.image_view = std::move(loader.image_view)
|
||||
.image_view = std::move(loader.image_view),
|
||||
.descriptor_set = std::move(ds),
|
||||
});
|
||||
|
||||
return id;
|
||||
@@ -570,5 +591,10 @@ ImTextureID imgui_context::load_texture(const std::string& filename)
|
||||
void imgui_context::free_texture(ImTextureID texture)
|
||||
{
|
||||
textures.erase(texture);
|
||||
// TODO free descriptor set
|
||||
}
|
||||
|
||||
void imgui_context::set_current()
|
||||
{
|
||||
ImGui::SetCurrentContext(context);
|
||||
ImPlot::SetCurrentContext(plot_context);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include "xr/swapchain.h"
|
||||
#include <imgui.h>
|
||||
#include <implot.h>
|
||||
#include <vulkan/vulkan_raii.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
@@ -43,6 +44,7 @@ class imgui_context
|
||||
vk::raii::Sampler sampler;
|
||||
std::shared_ptr<vk::Image> image;
|
||||
vk::raii::ImageView image_view;
|
||||
vk::raii::DescriptorSet descriptor_set;
|
||||
};
|
||||
|
||||
public:
|
||||
@@ -111,8 +113,10 @@ private:
|
||||
glm::vec2 scale_;
|
||||
|
||||
xr::swapchain& swapchain;
|
||||
int image_index;
|
||||
|
||||
ImGuiContext * context;
|
||||
ImPlotContext * plot_context;
|
||||
ImGuiIO& io;
|
||||
|
||||
std::vector<std::pair<controller, controller_state>> controllers;
|
||||
@@ -134,24 +138,6 @@ public:
|
||||
orientation_ = orientation;
|
||||
}
|
||||
|
||||
XrCompositionLayerQuad composition_layer(XrSwapchain swapchain) const
|
||||
{
|
||||
return XrCompositionLayerQuad{
|
||||
.type = XR_TYPE_COMPOSITION_LAYER_QUAD,
|
||||
.layerFlags = XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
|
||||
.space = world,
|
||||
.eyeVisibility = XrEyeVisibility::XR_EYE_VISIBILITY_BOTH,
|
||||
.subImage = {
|
||||
.swapchain = swapchain,
|
||||
.imageRect = {
|
||||
.offset = {0, 0},
|
||||
.extent = {(int32_t)size.width, (int32_t)size.height}},
|
||||
},
|
||||
.pose = pose(),
|
||||
.size = scale(),
|
||||
};
|
||||
}
|
||||
|
||||
XrPosef pose() const
|
||||
{
|
||||
return XrPosef{
|
||||
@@ -195,8 +181,7 @@ public:
|
||||
}
|
||||
|
||||
void new_frame(XrTime display_time);
|
||||
|
||||
void render(vk::Image destination);
|
||||
XrCompositionLayerQuad end_frame();
|
||||
|
||||
ImFont * large_font;
|
||||
size_t get_focused_controller() const
|
||||
@@ -207,4 +192,5 @@ public:
|
||||
ImTextureID load_texture(const std::string& filename, vk::raii::Sampler&& sampler);
|
||||
ImTextureID load_texture(const std::string& filename);
|
||||
void free_texture(ImTextureID);
|
||||
void set_current();
|
||||
};
|
||||
|
||||
@@ -171,11 +171,6 @@ scene_renderer::scene_renderer(vk::raii::Device & device, vk::raii::PhysicalDevi
|
||||
{
|
||||
res.fence = device.createFence({.flags = vk::FenceCreateFlagBits::eSignaled});
|
||||
res.cb = std::move(cb);
|
||||
|
||||
res.query_pool = vk::raii::QueryPool(device, vk::QueryPoolCreateInfo{
|
||||
.queryType = vk::QueryType::eTimestamp,
|
||||
.queryCount = 2,
|
||||
});
|
||||
}
|
||||
|
||||
renderpass = create_renderpass();
|
||||
@@ -544,17 +539,12 @@ void scene_renderer::start_frame()
|
||||
current_frame_index = (current_frame_index + 1) % frame_resources.size();
|
||||
|
||||
auto& f = current_frame();
|
||||
f.cpu_time_start = std::chrono::steady_clock::now();
|
||||
|
||||
device.waitForFences(*f.fence, true, 1'000'000'000);
|
||||
device.resetFences(*f.fence);
|
||||
|
||||
f.resources.clear();
|
||||
f.cb.begin({.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
|
||||
|
||||
f.cb.resetQueryPool(*f.query_pool, 0, 2);
|
||||
f.cb.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, *f.query_pool, 0);
|
||||
|
||||
// TODO: replace with vector<byte> and write to GPU in end_frame()
|
||||
f.staging_buffer_offset = 0;
|
||||
if (!f.staging_buffer)
|
||||
@@ -574,20 +564,12 @@ void scene_renderer::end_frame()
|
||||
{
|
||||
auto& f = current_frame();
|
||||
|
||||
f.cb.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, *f.query_pool, 1);
|
||||
f.cb.end();
|
||||
|
||||
queue.submit(vk::SubmitInfo{
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &*f.cb,
|
||||
}, *f.fence);
|
||||
|
||||
auto cpu_time_stop = std::chrono::steady_clock::now();
|
||||
spdlog::info("scene_renderer::render: CPU time: {} us", std::chrono::duration_cast<std::chrono::microseconds>(cpu_time_stop - f.cpu_time_start).count());
|
||||
|
||||
auto [res, timestamps] = f.query_pool.getResults<uint64_t>(0, 2, 2 * sizeof(uint64_t), sizeof(uint64_t), vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait);
|
||||
if (res == vk::Result::eSuccess)
|
||||
spdlog::info("scene_renderer::render: GPU time: {} us", (int)((timestamps[1] - timestamps[0]) * physical_device_properties.limits.timestampPeriod * 1.0e-3));
|
||||
}
|
||||
|
||||
scene_renderer::per_frame_resources & scene_renderer::current_frame()
|
||||
|
||||
@@ -151,7 +151,6 @@ public:
|
||||
{
|
||||
vk::raii::Fence fence = nullptr;
|
||||
vk::raii::CommandBuffer cb = nullptr;
|
||||
vk::raii::QueryPool query_pool = nullptr;
|
||||
std::vector<std::shared_ptr<void>> resources;
|
||||
|
||||
// Uniform buffer for per-view and per-instance data
|
||||
@@ -161,8 +160,6 @@ public:
|
||||
|
||||
// device local
|
||||
// buffer_allocation uniform_buffer;
|
||||
|
||||
std::chrono::steady_clock::time_point cpu_time_start;
|
||||
};
|
||||
|
||||
std::vector<per_frame_resources> frame_resources;
|
||||
|
||||
Reference in New Issue
Block a user