Skip to content

Commit bb8323d

Browse files
committed
Fixed swapchain recreation crash issues with commandbuffer
1 parent 6f8fff3 commit bb8323d

File tree

14 files changed

+43
-50
lines changed

14 files changed

+43
-50
lines changed

engine/render/renderer/Renderer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ void Renderer::EndFrame()
122122

123123
commandBuffers.End();
124124

125-
auto result = swapchain.SubmitCommandBuffers(commandBuffers, &currentImageIndex);
125+
commandBuffers.SetActiveBufferIndex(currentFrameIndex);
126+
127+
auto result = swapchain.SubmitCommandBuffers(commandBuffers, currentImageIndex);
126128

127129
if (result == Vulkan::Utils::ERROR_RESIZED || window.WasResized())
128130
{

engine/render/renderer/platform/vulkan/CommandBuffer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ void CommandBuffer::Swap(CommandBuffer& other)
5454
{
5555
auto tmpCmdBuffers = std::move(commandBuffers);
5656
auto tmpActiveCmdBuffer = activeCommandBuffer;
57+
auto tmpCurrentActiveBufferIndex = currentActiveBufferIndex;
5758

5859
commandBuffers = std::move(other.commandBuffers);
5960
activeCommandBuffer = other.activeCommandBuffer;
61+
currentActiveBufferIndex = other.currentActiveBufferIndex;
6062

6163
other.commandBuffers = std::move(tmpCmdBuffers);
6264
other.activeCommandBuffer = tmpActiveCmdBuffer;
65+
other.currentActiveBufferIndex = tmpCurrentActiveBufferIndex;
6366
}
6467
} // namespace Siege::Vulkan

engine/render/renderer/platform/vulkan/CommandBuffer.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#ifndef SIEGE_ENGINE_VULKAN_COMMANDBUFFER_H
1010
#define SIEGE_ENGINE_VULKAN_COMMANDBUFFER_H
1111

12-
#include "utils/Types.h"
13-
1412
#include <utils/collections/HeapArray.h>
1513

14+
#include "utils/Types.h"
15+
1616
namespace Siege::Vulkan
1717
{
1818
/**
@@ -73,13 +73,24 @@ class CommandBuffer
7373
/**
7474
* Returns the raw Vulkan CommandBuffer pointer
7575
* @param index the index of the CommandBuffer to be accessed
76-
* @return
76+
* @warn this function does not bounds check - please ensure that the provided index is in
77+
* bounds, otherwise undefined behaviour may occur
78+
* @return a raw Vulkan Command Buffer
7779
*/
78-
VkCommandBuffer Get(const size_t index = 0)
80+
VkCommandBuffer Get(const size_t index) const
7981
{
8082
return commandBuffers[index];
8183
}
8284

85+
/**
86+
* Returns the raw Vulkan CommandBuffer pointer of the current active Command Buffer
87+
* @return the current active command buffer
88+
*/
89+
VkCommandBuffer Get() const
90+
{
91+
return commandBuffers[currentActiveBufferIndex];
92+
}
93+
8394
/**
8495
* Returns the active command buffer (the one being recorded)
8596
* @return the vulkan command buffer pointer
@@ -120,6 +131,11 @@ class CommandBuffer
120131
commandBuffer.Submit();
121132
}
122133

134+
inline void SetActiveBufferIndex(uint32_t index)
135+
{
136+
currentActiveBufferIndex = index;
137+
}
138+
123139
private:
124140

125141
/**
@@ -130,6 +146,7 @@ class CommandBuffer
130146

131147
::Siege::Utils::MHArray<VkCommandBuffer> commandBuffers;
132148
VkCommandBuffer activeCommandBuffer {nullptr};
149+
uint32_t currentActiveBufferIndex {0};
133150
};
134151
} // namespace Siege::Vulkan
135152

engine/render/renderer/platform/vulkan/Fence.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Fence::~Fence()
3737
{
3838
if (fences == nullptr) return;
3939

40-
CC_LOG_INFO("[FENCE] Destroying Fence")
4140
auto device = Context::GetVkLogicalDevice();
4241

4342
size = (initState == FENCE_EMPTY || initState == FENCE_EMPTY_SIGNALED) * size;

engine/render/renderer/platform/vulkan/Renderer.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

engine/render/renderer/platform/vulkan/Renderer.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

engine/render/renderer/platform/vulkan/Swapchain.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,35 +147,34 @@ void Swapchain::EndRenderPass(Vulkan::CommandBuffer& commandBuffer)
147147
vkCmdEndRenderPass(OUT commandBuffer.GetActiveCommandBuffer());
148148
}
149149

150-
Utils::Result Swapchain::SubmitCommandBuffers(CommandBuffer& buffers, uint32_t* imageIndex)
150+
Utils::Result Swapchain::SubmitCommandBuffers(const CommandBuffer& buffers, uint32_t imageIndex)
151151
{
152-
uint32_t index = *imageIndex;
153-
uint32_t frameIdx = currentFrame;
154-
155152
// If the image being asked for is being used, we wait for it to become available
156-
imagesInFlight.Wait(index);
153+
imagesInFlight.Wait(imageIndex);
157154

158155
// Get the frame's image and move it to our images in flight
159-
imagesInFlight.Set(index, inFlightFences[currentFrame]);
156+
imagesInFlight.Set(imageIndex, inFlightFences[currentFrame]);
160157

161158
inFlightFences.Reset(currentFrame);
162159

163160
Utils::SubmitGraphicsCommand()
164161
.ToQueue(Vulkan::Context::GetCurrentDevice()->GetGraphicsQueue())
165162
.WaitOnSemaphores({imageAvailableSemaphores[currentFrame]})
166163
.WithPipelineStages({VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT})
167-
.WithCommandBuffers({buffers[currentFrame]})
164+
.WithCommandBuffers({buffers.Get()})
168165
.SignalSemaphores({renderFinishedSemaphores[currentFrame]})
169166
.Submit(inFlightFences[currentFrame]);
170167

168+
uint32_t previousFrame = currentFrame;
169+
171170
currentFrame = ((currentFrame + 1) < MAX_FRAMES_IN_FLIGHT) * (currentFrame + 1);
172171

173172
// Return the result of the rendering process
174173
return Utils::SubmitPresentCommand()
175174
.ToQueue(Vulkan::Context::GetCurrentDevice()->GetPresentQueue())
176-
.SignalSemaphores({renderFinishedSemaphores[frameIdx]})
175+
.SignalSemaphores({renderFinishedSemaphores[previousFrame]})
177176
.ForSwapchains({swapchain})
178-
.WithImageIndices({index})
177+
.WithImageIndices({imageIndex})
179178
.Submit();
180179
}
181180

engine/render/renderer/platform/vulkan/Swapchain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Swapchain
9797
* @param imageIndex - the index of the image being drawn
9898
* @return VkResult - the result of submitting the buffer
9999
*/
100-
Utils::Result SubmitCommandBuffers(CommandBuffer& commandBuffer, uint32_t* imageIndex);
100+
Utils::Result SubmitCommandBuffers(const CommandBuffer& commandBuffer, uint32_t imageIndex);
101101

102102
/**
103103
* Compares the inputted image and depth formats to the ones used in the Swapchain

engine/render/renderer/platform/vulkan/utils/CommandBuffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ VkCommandBuffer CommandBuffer::AllocateCommandBuffer(VkDevice device, VkCommandP
2929
}
3030

3131
::Siege::Utils::MHArray<VkCommandBuffer> CommandBuffer::AllocateCommandBuffers(VkDevice device,
32-
VkCommandPool pool,
33-
uint32_t count)
32+
VkCommandPool pool,
33+
uint32_t count)
3434
{
3535
using ::Siege::Utils::MHArray;
3636
MHArray<VkCommandBuffer> buffers(count);

engine/render/renderer/platform/vulkan/utils/CommandBuffer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#ifndef SIEGE_ENGINE_COMMANDBUFFER_H
1010
#define SIEGE_ENGINE_COMMANDBUFFER_H
1111

12-
#include <volk/volk.h>
13-
1412
#include <utils/collections/HeapArray.h>
13+
#include <volk/volk.h>
1514

1615
namespace Siege::Vulkan::Utils
1716
{
@@ -20,7 +19,9 @@ class CommandBuffer
2019
public:
2120

2221
static VkCommandBuffer AllocateCommandBuffer(VkDevice device, VkCommandPool pool);
23-
static ::Siege::Utils::MHArray<VkCommandBuffer> AllocateCommandBuffers(VkDevice, VkCommandPool, uint32_t);
22+
static ::Siege::Utils::MHArray<VkCommandBuffer> AllocateCommandBuffers(VkDevice,
23+
VkCommandPool,
24+
uint32_t);
2425
static void BeginSingleTimeCommand(VkCommandBuffer buffer);
2526
static void EndCommandBuffer(VkCommandBuffer commandBuffer);
2627
};

0 commit comments

Comments
 (0)