From 92170b6ca9463557697378987491b8b2691afb34 Mon Sep 17 00:00:00 2001 From: Tanguy Fautre Date: Wed, 25 Dec 2024 20:11:00 +0000 Subject: [PATCH] Fix incorrect synchronisation of command buffers, and associated resources, as semaphores/fences should be on the current frame index, not the swap buffer image index. --- src/RayTracer.cpp | 6 +++--- src/RayTracer.hpp | 2 +- src/Vulkan/Application.cpp | 18 +++++++++--------- src/Vulkan/Application.hpp | 4 ++-- src/Vulkan/DescriptorSets.cpp | 10 +++++----- src/Vulkan/DescriptorSets.hpp | 10 +++++----- src/Vulkan/GraphicsPipeline.cpp | 4 ++-- src/Vulkan/GraphicsPipeline.hpp | 2 +- src/Vulkan/RayTracing/Application.cpp | 4 ++-- src/Vulkan/RayTracing/Application.hpp | 2 +- src/Vulkan/RayTracing/RayTracingPipeline.cpp | 4 ++-- src/Vulkan/RayTracing/RayTracingPipeline.hpp | 2 +- 12 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/RayTracer.cpp b/src/RayTracer.cpp index d1b760b5..3181081b 100644 --- a/src/RayTracer.cpp +++ b/src/RayTracer.cpp @@ -141,7 +141,7 @@ void RayTracer::DrawFrame() Application::DrawFrame(); } -void RayTracer::Render(VkCommandBuffer commandBuffer, const uint32_t imageIndex) +void RayTracer::Render(VkCommandBuffer commandBuffer, const size_t currentFrame, const uint32_t imageIndex) { // Record delta time between calls to Render. const auto prevTime = time_; @@ -156,8 +156,8 @@ void RayTracer::Render(VkCommandBuffer commandBuffer, const uint32_t imageIndex) // Render the scene userSettings_.IsRayTraced - ? Vulkan::RayTracing::Application::Render(commandBuffer, imageIndex) - : Vulkan::Application::Render(commandBuffer, imageIndex); + ? Vulkan::RayTracing::Application::Render(commandBuffer, currentFrame, imageIndex) + : Vulkan::Application::Render(commandBuffer, currentFrame, imageIndex); // Render the UI Statistics stats = {}; diff --git a/src/RayTracer.hpp b/src/RayTracer.hpp index 49a366b1..fbeb5937 100644 --- a/src/RayTracer.hpp +++ b/src/RayTracer.hpp @@ -29,7 +29,7 @@ class RayTracer final : public Vulkan::RayTracing::Application void CreateSwapChain() override; void DeleteSwapChain() override; void DrawFrame() override; - void Render(VkCommandBuffer commandBuffer, uint32_t imageIndex) override; + void Render(VkCommandBuffer commandBuffer, size_t currentFrame, uint32_t imageIndex) override; void OnKey(int key, int scancode, int action, int mods) override; void OnCursorPosition(double xpos, double ypos) override; diff --git a/src/Vulkan/Application.cpp b/src/Vulkan/Application.cpp index d331b06f..6a93b3cb 100644 --- a/src/Vulkan/Application.cpp +++ b/src/Vulkan/Application.cpp @@ -161,7 +161,7 @@ void Application::DeleteSwapChain() void Application::DrawFrame() { - const auto noTimeout = std::numeric_limits::max(); + constexpr auto noTimeout = std::numeric_limits::max(); auto& inFlightFence = inFlightFences_[currentFrame_]; const auto imageAvailableSemaphore = imageAvailableSemaphores_[currentFrame_].Handle(); @@ -183,11 +183,11 @@ void Application::DrawFrame() Throw(std::runtime_error(std::string("failed to acquire next image (") + ToString(result) + ")")); } - const auto commandBuffer = commandBuffers_->Begin(imageIndex); - Render(commandBuffer, imageIndex); - commandBuffers_->End(imageIndex); + const auto commandBuffer = commandBuffers_->Begin(currentFrame_); + Render(commandBuffer, currentFrame_, imageIndex); + commandBuffers_->End(currentFrame_); - UpdateUniformBuffer(imageIndex); + UpdateUniformBuffer(); VkSubmitInfo submitInfo = {}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; @@ -236,7 +236,7 @@ void Application::DrawFrame() currentFrame_ = (currentFrame_ + 1) % inFlightFences_.size(); } -void Application::Render(VkCommandBuffer commandBuffer, const uint32_t imageIndex) +void Application::Render(VkCommandBuffer commandBuffer, const size_t currentFrame, const uint32_t imageIndex) { std::array clearValues = {}; clearValues[0].color = { {0.0f, 0.0f, 0.0f, 1.0f} }; @@ -255,7 +255,7 @@ void Application::Render(VkCommandBuffer commandBuffer, const uint32_t imageInde { const auto& scene = GetScene(); - VkDescriptorSet descriptorSets[] = { graphicsPipeline_->DescriptorSet(imageIndex) }; + VkDescriptorSet descriptorSets[] = { graphicsPipeline_->DescriptorSet(currentFrame) }; VkBuffer vertexBuffers[] = { scene.VertexBuffer().Handle() }; const VkBuffer indexBuffer = scene.IndexBuffer().Handle(); VkDeviceSize offsets[] = { 0 }; @@ -282,9 +282,9 @@ void Application::Render(VkCommandBuffer commandBuffer, const uint32_t imageInde vkCmdEndRenderPass(commandBuffer); } -void Application::UpdateUniformBuffer(const uint32_t imageIndex) +void Application::UpdateUniformBuffer() { - uniformBuffers_[imageIndex].SetValue(GetUniformBufferObject(swapChain_->Extent())); + uniformBuffers_[currentFrame_].SetValue(GetUniformBufferObject(swapChain_->Extent())); } void Application::RecreateSwapChain() diff --git a/src/Vulkan/Application.hpp b/src/Vulkan/Application.hpp index 5dff0821..87ceb899 100644 --- a/src/Vulkan/Application.hpp +++ b/src/Vulkan/Application.hpp @@ -59,7 +59,7 @@ namespace Vulkan virtual void CreateSwapChain(); virtual void DeleteSwapChain(); virtual void DrawFrame(); - virtual void Render(VkCommandBuffer commandBuffer, uint32_t imageIndex); + virtual void Render(VkCommandBuffer commandBuffer, size_t currentFrame, uint32_t imageIndex); virtual void OnKey(int key, int scancode, int action, int mods) { } virtual void OnCursorPosition(double xpos, double ypos) { } @@ -70,7 +70,7 @@ namespace Vulkan private: - void UpdateUniformBuffer(uint32_t imageIndex); + void UpdateUniformBuffer(); void RecreateSwapChain(); const VkPresentModeKHR presentMode_; diff --git a/src/Vulkan/DescriptorSets.cpp b/src/Vulkan/DescriptorSets.cpp index 35d6f330..3ebd7989 100644 --- a/src/Vulkan/DescriptorSets.cpp +++ b/src/Vulkan/DescriptorSets.cpp @@ -44,7 +44,7 @@ DescriptorSets::~DescriptorSets() //} } -VkWriteDescriptorSet DescriptorSets::Bind(const uint32_t index, const uint32_t binding, const VkDescriptorBufferInfo& bufferInfo, const uint32_t count) const +VkWriteDescriptorSet DescriptorSets::Bind(const size_t index, const uint32_t binding, const VkDescriptorBufferInfo& bufferInfo, const uint32_t count) const { VkWriteDescriptorSet descriptorWrite = {}; descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; @@ -58,7 +58,7 @@ VkWriteDescriptorSet DescriptorSets::Bind(const uint32_t index, const uint32_t b return descriptorWrite; } -VkWriteDescriptorSet DescriptorSets::Bind(const uint32_t index, const uint32_t binding, const VkDescriptorImageInfo& imageInfo, const uint32_t count) const +VkWriteDescriptorSet DescriptorSets::Bind(const size_t index, const uint32_t binding, const VkDescriptorImageInfo& imageInfo, const uint32_t count) const { VkWriteDescriptorSet descriptorWrite = {}; descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; @@ -72,7 +72,7 @@ VkWriteDescriptorSet DescriptorSets::Bind(const uint32_t index, const uint32_t b return descriptorWrite; } -VkWriteDescriptorSet DescriptorSets::Bind(uint32_t index, uint32_t binding, const VkWriteDescriptorSetAccelerationStructureKHR& structureInfo, const uint32_t count) const +VkWriteDescriptorSet DescriptorSets::Bind(const size_t index, const uint32_t binding, const VkWriteDescriptorSetAccelerationStructureKHR& structureInfo, const uint32_t count) const { VkWriteDescriptorSet descriptorWrite = {}; descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; @@ -86,7 +86,7 @@ VkWriteDescriptorSet DescriptorSets::Bind(uint32_t index, uint32_t binding, cons return descriptorWrite; } -void DescriptorSets::UpdateDescriptors(uint32_t index, const std::vector& descriptorWrites) +void DescriptorSets::UpdateDescriptors(const std::vector& descriptorWrites) { vkUpdateDescriptorSets( descriptorPool_.Device().Handle(), @@ -94,7 +94,7 @@ void DescriptorSets::UpdateDescriptors(uint32_t index, const std::vector& descriptorWrites); + void UpdateDescriptors(const std::vector& descriptorWrites); private: diff --git a/src/Vulkan/GraphicsPipeline.cpp b/src/Vulkan/GraphicsPipeline.cpp index 8e4ba509..69e00884 100644 --- a/src/Vulkan/GraphicsPipeline.cpp +++ b/src/Vulkan/GraphicsPipeline.cpp @@ -155,7 +155,7 @@ GraphicsPipeline::GraphicsPipeline( descriptorSets.Bind(i, 2, *imageInfos.data(), static_cast(imageInfos.size())) }; - descriptorSets.UpdateDescriptors(i, descriptorWrites); + descriptorSets.UpdateDescriptors(descriptorWrites); } // Create pipeline layout and render pass. @@ -208,7 +208,7 @@ GraphicsPipeline::~GraphicsPipeline() descriptorSetManager_.reset(); } -VkDescriptorSet GraphicsPipeline::DescriptorSet(const uint32_t index) const +VkDescriptorSet GraphicsPipeline::DescriptorSet(const size_t index) const { return descriptorSetManager_->DescriptorSets().Handle(index); } diff --git a/src/Vulkan/GraphicsPipeline.hpp b/src/Vulkan/GraphicsPipeline.hpp index 8afd073d..26c9f821 100644 --- a/src/Vulkan/GraphicsPipeline.hpp +++ b/src/Vulkan/GraphicsPipeline.hpp @@ -31,7 +31,7 @@ namespace Vulkan bool isWireFrame); ~GraphicsPipeline(); - VkDescriptorSet DescriptorSet(uint32_t index) const; + VkDescriptorSet DescriptorSet(size_t index) const; bool IsWireFrame() const { return isWireFrame_; } const class PipelineLayout& PipelineLayout() const { return *pipelineLayout_; } const class RenderPass& RenderPass() const { return *renderPass_; } diff --git a/src/Vulkan/RayTracing/Application.cpp b/src/Vulkan/RayTracing/Application.cpp index 6544479f..45dd0ede 100644 --- a/src/Vulkan/RayTracing/Application.cpp +++ b/src/Vulkan/RayTracing/Application.cpp @@ -166,11 +166,11 @@ void Application::DeleteSwapChain() Vulkan::Application::DeleteSwapChain(); } -void Application::Render(VkCommandBuffer commandBuffer, const uint32_t imageIndex) +void Application::Render(VkCommandBuffer commandBuffer, const size_t currentFrame, const uint32_t imageIndex) { const auto extent = SwapChain().Extent(); - VkDescriptorSet descriptorSets[] = { rayTracingPipeline_->DescriptorSet(imageIndex) }; + VkDescriptorSet descriptorSets[] = { rayTracingPipeline_->DescriptorSet(currentFrame) }; VkImageSubresourceRange subresourceRange = {}; subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; diff --git a/src/Vulkan/RayTracing/Application.hpp b/src/Vulkan/RayTracing/Application.hpp index 79df31e6..482ddab7 100644 --- a/src/Vulkan/RayTracing/Application.hpp +++ b/src/Vulkan/RayTracing/Application.hpp @@ -35,7 +35,7 @@ namespace Vulkan::RayTracing void DeleteAccelerationStructures(); void CreateSwapChain() override; void DeleteSwapChain() override; - void Render(VkCommandBuffer commandBuffer, uint32_t imageIndex) override; + void Render(VkCommandBuffer commandBuffer, size_t currentFrame, uint32_t imageIndex) override; private: diff --git a/src/Vulkan/RayTracing/RayTracingPipeline.cpp b/src/Vulkan/RayTracing/RayTracingPipeline.cpp index abef4b7d..a8a374f9 100644 --- a/src/Vulkan/RayTracing/RayTracingPipeline.cpp +++ b/src/Vulkan/RayTracing/RayTracingPipeline.cpp @@ -137,7 +137,7 @@ RayTracingPipeline::RayTracingPipeline( descriptorWrites.push_back(descriptorSets.Bind(i, 9, proceduralBufferInfo)); } - descriptorSets.UpdateDescriptors(i, descriptorWrites); + descriptorSets.UpdateDescriptors(descriptorWrites); } pipelineLayout_.reset(new class PipelineLayout(device, descriptorSetManager_->DescriptorSetLayout())); @@ -237,7 +237,7 @@ RayTracingPipeline::~RayTracingPipeline() descriptorSetManager_.reset(); } -VkDescriptorSet RayTracingPipeline::DescriptorSet(const uint32_t index) const +VkDescriptorSet RayTracingPipeline::DescriptorSet(const size_t index) const { return descriptorSetManager_->DescriptorSets().Handle(index); } diff --git a/src/Vulkan/RayTracing/RayTracingPipeline.hpp b/src/Vulkan/RayTracing/RayTracingPipeline.hpp index d97a070a..45432845 100644 --- a/src/Vulkan/RayTracing/RayTracingPipeline.hpp +++ b/src/Vulkan/RayTracing/RayTracingPipeline.hpp @@ -44,7 +44,7 @@ namespace Vulkan::RayTracing uint32_t TriangleHitGroupIndex() const { return triangleHitGroupIndex_; } uint32_t ProceduralHitGroupIndex() const { return proceduralHitGroupIndex_; } - VkDescriptorSet DescriptorSet(uint32_t index) const; + VkDescriptorSet DescriptorSet(size_t index) const; const class PipelineLayout& PipelineLayout() const { return *pipelineLayout_; } private: