From 1695940c6af0bb2d253f088655c695ec30ef337c Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Fri, 18 Oct 2024 23:16:19 +0200 Subject: [PATCH] imgui performance window --- imgui.ini | 10 ++++++++++ include/application.hpp | 8 +++++++- src/application.cpp | 40 +++++++++++++++++++++++----------------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/imgui.ini b/imgui.ini index e1d13d7..321faee 100644 --- a/imgui.ini +++ b/imgui.ini @@ -23,5 +23,15 @@ Pos=153,27 Size=353,999 Collapsed=0 +[Window][Test] +Pos=1246,20 +Size=249,166 +Collapsed=0 + +[Window][Performance] +Pos=1617,2 +Size=302,1002 +Collapsed=0 + [Docking][Data] diff --git a/include/application.hpp b/include/application.hpp index 7a00855..f743d8b 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -42,7 +42,13 @@ public: void mainLoop(); ~Application(); private: - void initIMGUI(); + void imGuiWindows(); + struct PerformanceInformation { + float totalUpdateDuration; + float updateDuration; + float frameDuration; + } performanceInformation {}; + void createSyncObjects(); unique_ptr imageAvailable; diff --git a/src/application.cpp b/src/application.cpp index a70adbe..33ab94c 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -141,8 +141,6 @@ Application::Application() { void Application::mainLoop() { vkDeviceWaitIdle(Instance::GetDevice()); std::future compute = std::async(std::launch::async, [this](){ - setbuf(stdout, NULL); - descriptorPool->bindBuffer(*vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0); while (!glfwWindowShouldClose(Instance::instance->window)){ auto t1 = system_clock::now(); update(); @@ -152,27 +150,19 @@ void Application::mainLoop() { auto requestedUpdateDuration = duration(simulationPropertiesBuffer->access().dt); std::this_thread::sleep_for(requestedUpdateDuration - measuredUpdateDuration); - auto totalUpdateDuration = std::max(requestedUpdateDuration, measuredUpdateDuration); - printf("\r%f", totalUpdateDuration.count()); + performanceInformation.updateDuration = measuredUpdateDuration.count(); + performanceInformation.totalUpdateDuration = std::max(requestedUpdateDuration, measuredUpdateDuration).count(); } }); - bool show_demo_window = true; - auto t1 = system_clock::now(); while (!glfwWindowShouldClose(Instance::instance->window)){ glfwPollEvents(); - - ImGui_ImplVulkan_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); - - ImGui::ShowDemoWindow(&show_demo_window); - - ImGui::Render(); + imGuiWindows(); auto t2 = system_clock::now(); float seconds = duration(t2 - t1).count(); + performanceInformation.frameDuration = seconds; t1 = system_clock::now(); drawFrame(seconds); } @@ -180,9 +170,7 @@ void Application::mainLoop() { vkDeviceWaitIdle(Instance::GetDevice()); } -Application::~Application() { - -} +Application::~Application() = default; void Application::createSyncObjects() { imageAvailable = make_unique(); @@ -377,7 +365,9 @@ void Application::drawFrame(float dt) { uint32_t imageIndex; VkResult result = vkAcquireNextImageKHR(Instance::GetDevice(), swapchain->handle, UINT64_MAX, imageAvailable->handle, VK_NULL_HANDLE, &imageIndex); if (result == VK_ERROR_OUT_OF_DATE_KHR){ + submitMutex.lock(); swapchain->recreateSwapchain(); + submitMutex.unlock(); return; } @@ -443,6 +433,7 @@ void Application::drawFrame(float dt) { recordDrawCommands(cmdBuffer); + ImGui::Render(); ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmdBuffer); vkCmdEndRenderPass(cmdBuffer); @@ -484,7 +475,9 @@ void Application::drawFrame(float dt) { if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || Instance::instance->windowResized){ Instance::instance->windowResized = false; + submitMutex.lock(); swapchain->recreateSwapchain(); + submitMutex.unlock(); } } @@ -710,3 +703,16 @@ void Application::recordNormalCommands(VkCommandBuffer cmdBuffer) { barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; vkCmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 1, &barrier, 0, nullptr, 0, nullptr); } + +void Application::imGuiWindows() { + ImGui_ImplVulkan_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + ImGui::Begin("Performance"); + ImGui::Text("Update time: %f", performanceInformation.updateDuration); + ImGui::Text("Updates per second: %f", 1 / performanceInformation.totalUpdateDuration); + ImGui::Text("Frame time: %f", performanceInformation.frameDuration); + ImGui::Text("Frames per second: %f", 1 / performanceInformation.frameDuration); + ImGui::End(); +}