diff --git a/imgui.ini b/imgui.ini index 0278abe..2001fb6 100644 --- a/imgui.ini +++ b/imgui.ini @@ -29,8 +29,8 @@ Size=249,166 Collapsed=0 [Window][Performance] -Pos=1617,2 -Size=302,1002 +Pos=1716,2 +Size=203,1002 Collapsed=0 DockId=0x00000001,0 @@ -41,5 +41,5 @@ Collapsed=0 DockId=0x00000001,1 [Docking][Data] -DockNode ID=0x00000001 Pos=1617,2 Size=302,1002 Selected=0x60B79D0E +DockNode ID=0x00000001 Pos=1716,2 Size=203,1002 Selected=0x60B79D0E diff --git a/include/application.hpp b/include/application.hpp index 49042f0..da7cb0d 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -16,8 +16,10 @@ #include #include #include "constraints.hpp" +#include "fixed_list.hpp" #include #include +#include using std::unique_ptr, std::make_unique; using std::vector; @@ -44,10 +46,12 @@ public: private: void imGuiWindows(); struct PerformanceInformation { - float totalUpdateDuration; + static const size_t saves = 144; + FixedList recentTotalUpdateDurations = FixedList(saves); float updateDuration; - float frameDuration; - } performanceInformation {}; + FixedList recentFrameDurations = FixedList(saves); + + } performanceInformation; void createSyncObjects(); diff --git a/include/fixed_list.hpp b/include/fixed_list.hpp new file mode 100644 index 0000000..59d151b --- /dev/null +++ b/include/fixed_list.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +class FixedList : private std::list { + size_t maxSize; +public: + explicit FixedList(size_t maxSize); + void push(float value); + float average() const; +}; \ No newline at end of file diff --git a/src/application.cpp b/src/application.cpp index 529ab95..178ae8a 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -20,6 +20,7 @@ #include "imgui.h" #include "imgui/backends/imgui_impl_vulkan.h" #include "imgui/backends/imgui_impl_glfw.h" +#include "fixed_list.hpp" using namespace std::chrono; @@ -151,7 +152,7 @@ void Application::mainLoop() { std::this_thread::sleep_for(requestedUpdateDuration - measuredUpdateDuration); performanceInformation.updateDuration = measuredUpdateDuration.count(); - performanceInformation.totalUpdateDuration = std::max(requestedUpdateDuration, measuredUpdateDuration).count(); + performanceInformation.recentTotalUpdateDurations.push(std::max(requestedUpdateDuration, measuredUpdateDuration).count()); } }); @@ -162,7 +163,7 @@ void Application::mainLoop() { auto t2 = system_clock::now(); float seconds = duration(t2 - t1).count(); - performanceInformation.frameDuration = seconds; + performanceInformation.recentFrameDurations.push(seconds); t1 = system_clock::now(); drawFrame(seconds); } @@ -178,8 +179,8 @@ void Application::createSyncObjects() { computeSemaphore = make_unique(); transferSemaphore = make_unique(); renderFence = make_unique(true); - computeFence = make_unique(true); - transferFence = make_unique(true); + computeFence = make_unique(false); + transferFence = make_unique(false); } void Application::createMeshBuffers() { @@ -188,14 +189,14 @@ void Application::createMeshBuffers() { auto body = std::make_unique(&sphere, 1.f / 60); - for (size_t i = 0; i < 2; i++){ + for (size_t i = 0; i < 10; i++){ auto copy = std::make_unique(*body.get()); copy->applyVertexOffset({i * 2, 0, 0}); softBodies.push_back(std::move(copy)); } body = std::make_unique(&bunny, 1.f / 10); - for (size_t i = 0; i < 2; i++){ + for (size_t i = 0; i < 10; i++){ auto copy = std::make_unique(*body.get()); copy->applyVertexOffset({i * 2, 0, 2}); softBodies.push_back(std::move(copy)); @@ -496,16 +497,6 @@ void Application::recordDrawCommands(VkCommandBuffer commandBuffer) { } void Application::update() { - vkWaitForFences(Instance::GetDevice(), 1, &computeFence->handle, VK_TRUE, UINT64_MAX); - vkResetFences(Instance::GetDevice(), 1, &computeFence->handle); - - currentDrawVertexBuffer = 1 - currentDrawVertexBuffer; - - vkWaitForFences(Instance::GetDevice(), 1, &transferFence->handle, VK_TRUE, UINT64_MAX); - vkResetFences(Instance::GetDevice(), 1, &transferFence->handle); - - currentDrawVertexBuffer = 1 - currentDrawVertexBuffer; - VkCommandBufferBeginInfo beginInfo {}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.flags = 0; @@ -561,6 +552,17 @@ void Application::update() { vkQueueSubmit(Instance::instance->computeAndTransferQueue, 1, &submit, transferFence->handle); submitMutex.unlock(); } + + vkWaitForFences(Instance::GetDevice(), 1, &computeFence->handle, VK_TRUE, UINT64_MAX); + vkResetFences(Instance::GetDevice(), 1, &computeFence->handle); + + currentDrawVertexBuffer = 1 - currentDrawVertexBuffer; + + vkWaitForFences(Instance::GetDevice(), 1, &transferFence->handle, VK_TRUE, UINT64_MAX); + vkResetFences(Instance::GetDevice(), 1, &transferFence->handle); + + currentDrawVertexBuffer = 1 - currentDrawVertexBuffer; + // descriptorPool->bindBuffer(*vertexBuffers[1 - currentDrawVertexBuffer], VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0); } uint32_t Application::GetGroupCount(uint32_t threads, uint32_t blockSize) { @@ -711,9 +713,14 @@ void Application::imGuiWindows() { 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); + + float updateMS = performanceInformation.updateDuration * 1000.f; + float updateHZ = 1 / performanceInformation.recentTotalUpdateDurations.average(); + ImGui::Text("Updates: %.0fms | %.1fHz", updateMS, updateHZ); + + float frameMS = performanceInformation.recentFrameDurations.average() * 1000.f; + float frameHZ = 1 / performanceInformation.recentFrameDurations.average(); + ImGui::Text("Frames: %.2fms | %.1fHz", frameMS, frameHZ); + ImGui::End(); } diff --git a/src/fixed_list.cpp b/src/fixed_list.cpp new file mode 100644 index 0000000..39f093d --- /dev/null +++ b/src/fixed_list.cpp @@ -0,0 +1,17 @@ +#include +#include "fixed_list.hpp" + +FixedList::FixedList(size_t maxSize) : std::list(), maxSize(maxSize) {} + +void FixedList::push(float value) { + push_back(value); + + if (size() > maxSize) + erase(begin()); +} + +float FixedList::average() const { + if (empty()) + return 0.0f; + return std::reduce(begin(), end(), 0.0f) / static_cast(size()); +}