Skip to content

Commit

Permalink
Fix incorrect synchronisation of command buffers, and associated reso…
Browse files Browse the repository at this point in the history
…urces, as semaphores/fences should be on the current frame index, not the swap buffer image index.
  • Loading branch information
GPSnoopy committed Dec 25, 2024
1 parent c8c79bc commit 92170b6
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/RayTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand All @@ -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 = {};
Expand Down
2 changes: 1 addition & 1 deletion src/RayTracer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 9 additions & 9 deletions src/Vulkan/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void Application::DeleteSwapChain()

void Application::DrawFrame()
{
const auto noTimeout = std::numeric_limits<uint64_t>::max();
constexpr auto noTimeout = std::numeric_limits<uint64_t>::max();

auto& inFlightFence = inFlightFences_[currentFrame_];
const auto imageAvailableSemaphore = imageAvailableSemaphores_[currentFrame_].Handle();
Expand All @@ -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;
Expand Down Expand Up @@ -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<VkClearValue, 2> clearValues = {};
clearValues[0].color = { {0.0f, 0.0f, 0.0f, 1.0f} };
Expand All @@ -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 };
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/Vulkan/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand All @@ -70,7 +70,7 @@ namespace Vulkan

private:

void UpdateUniformBuffer(uint32_t imageIndex);
void UpdateUniformBuffer();
void RecreateSwapChain();

const VkPresentModeKHR presentMode_;
Expand Down
10 changes: 5 additions & 5 deletions src/Vulkan/DescriptorSets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -86,15 +86,15 @@ VkWriteDescriptorSet DescriptorSets::Bind(uint32_t index, uint32_t binding, cons
return descriptorWrite;
}

void DescriptorSets::UpdateDescriptors(uint32_t index, const std::vector<VkWriteDescriptorSet>& descriptorWrites)
void DescriptorSets::UpdateDescriptors(const std::vector<VkWriteDescriptorSet>& descriptorWrites)
{
vkUpdateDescriptorSets(
descriptorPool_.Device().Handle(),
static_cast<uint32_t>(descriptorWrites.size()),
descriptorWrites.data(), 0, nullptr);
}

VkDescriptorType DescriptorSets::GetBindingType(uint32_t binding) const
VkDescriptorType DescriptorSets::GetBindingType(const uint32_t binding) const
{
const auto it = bindingTypes_.find(binding);
if (it == bindingTypes_.end())
Expand Down
10 changes: 5 additions & 5 deletions src/Vulkan/DescriptorSets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ namespace Vulkan

~DescriptorSets();

VkDescriptorSet Handle(uint32_t index) const { return descriptorSets_[index]; }
VkDescriptorSet Handle(size_t index) const { return descriptorSets_[index]; }

VkWriteDescriptorSet Bind(uint32_t index, uint32_t binding, const VkDescriptorBufferInfo& bufferInfo, uint32_t count = 1) const;
VkWriteDescriptorSet Bind(uint32_t index, uint32_t binding, const VkDescriptorImageInfo& imageInfo, uint32_t count = 1) const;
VkWriteDescriptorSet Bind(uint32_t index, uint32_t binding, const VkWriteDescriptorSetAccelerationStructureKHR& structureInfo, uint32_t count = 1) const;
VkWriteDescriptorSet Bind(size_t index, uint32_t binding, const VkDescriptorBufferInfo& bufferInfo, uint32_t count = 1) const;
VkWriteDescriptorSet Bind(size_t index, uint32_t binding, const VkDescriptorImageInfo& imageInfo, uint32_t count = 1) const;
VkWriteDescriptorSet Bind(size_t index, uint32_t binding, const VkWriteDescriptorSetAccelerationStructureKHR& structureInfo, uint32_t count = 1) const;

void UpdateDescriptors(uint32_t index, const std::vector<VkWriteDescriptorSet>& descriptorWrites);
void UpdateDescriptors(const std::vector<VkWriteDescriptorSet>& descriptorWrites);

private:

Expand Down
4 changes: 2 additions & 2 deletions src/Vulkan/GraphicsPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ GraphicsPipeline::GraphicsPipeline(
descriptorSets.Bind(i, 2, *imageInfos.data(), static_cast<uint32_t>(imageInfos.size()))
};

descriptorSets.UpdateDescriptors(i, descriptorWrites);
descriptorSets.UpdateDescriptors(descriptorWrites);
}

// Create pipeline layout and render pass.
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Vulkan/GraphicsPipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
Expand Down
4 changes: 2 additions & 2 deletions src/Vulkan/RayTracing/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Vulkan/RayTracing/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions src/Vulkan/RayTracing/RayTracingPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Vulkan/RayTracing/RayTracingPipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 92170b6

Please sign in to comment.