fix: Context memory leak.

This commit is contained in:
Anish Bhobe 2025-05-02 20:32:15 +02:00
parent d683de3181
commit 2facb3e6c1
2 changed files with 42 additions and 21 deletions

View File

@ -295,11 +295,8 @@ class GraphicsContext : public Context
void Draw(u32 vertexCount); void Draw(u32 vertexCount);
void DrawIndexed(u32 indexCount); void DrawIndexed(u32 indexCount);
DEPRECATE_RAW_CALLS DEPRECATE_RAW_CALLS void Dependency(const vk::DependencyInfo &dependencyInfo);
void Dependency(const vk::DependencyInfo &dependencyInfo); DEPRECATE_RAW_CALLS void BeginRendering(const vk::RenderingInfo &renderingInfo);
DEPRECATE_RAW_CALLS
void BeginRendering(const vk::RenderingInfo &renderingInfo);
void EndRendering(); void EndRendering();
}; };
@ -318,8 +315,12 @@ struct Frame
u32 m_ImageIdx; u32 m_ImageIdx;
vk::Image m_SwapchainImage; vk::Image m_SwapchainImage;
vk::ImageView m_SwapchainImageView; vk::ImageView m_SwapchainImageView;
u32 m_ImageIdx;
u32 m_CommandBuffersAllocated;
void Reset(u32 imageIdx, vk::Image swapchainImage, vk::ImageView swapchainImageView);
GraphicsContext CreateGraphicsContext(); GraphicsContext CreateGraphicsContext();
void WaitUntilReady();
}; };
class Device final class Device final

View File

@ -536,8 +536,7 @@ systems::Device::GetNextFrame()
m_CurrentFrameIdx = (m_CurrentFrameIdx + 1) % MAX_FRAMES_IN_FLIGHT; m_CurrentFrameIdx = (m_CurrentFrameIdx + 1) % MAX_FRAMES_IN_FLIGHT;
AbortIfFailedMV(m_Device.m_Device.waitForFences(1, &currentFrame.m_FrameAvailableFence, true, MaxValue<u64>), currentFrame.WaitUntilReady();
"Waiting for fence {} failed.", frameIndex);
u32 imageIndex = 0; u32 imageIndex = 0;
bool imageAcquired = false; bool imageAcquired = false;
@ -559,17 +558,9 @@ systems::Device::GetNextFrame()
} }
} }
// Reset fences here. In case swapchain was out of date, we leave the fences signalled. // Reset frame here.
AbortIfFailedMV(m_Device.m_Device.resetFences(1, &currentFrame.m_FrameAvailableFence), "Fence {} reset failed.", // In case swapchain was out of date, we will reset the frame next time.
frameIndex); currentFrame.Reset(imageIndex, m_Swapchain.m_Images[imageIndex], m_Swapchain.m_ImageViews[imageIndex]);
AbortIfFailedMV(m_Device.m_Device.resetCommandPool(currentFrame.m_Pool, {}), "Command pool {} reset failed.",
frameIndex);
currentFrame.m_ImageIdx = imageIndex;
currentFrame.m_SwapchainImage = m_Swapchain.m_Images[imageIndex];
currentFrame.m_SwapchainImageView = m_Swapchain.m_ImageViews[imageIndex];
return currentFrame; return currentFrame;
} }
@ -621,22 +612,51 @@ systems::Device::Present(Frame &frame)
} }
} }
void
systems::Frame::Reset(u32 imageIdx, vk::Image swapchainImage, vk::ImageView swapchainImageView)
{
AbortIfFailedMV(m_Device->m_Device.resetFences(1, &m_FrameAvailableFence), "Fence {} reset failed.",
m_FrameIdx);
AbortIfFailedMV(m_Device->m_Device.resetCommandPool(m_Pool, {}), "Command pool {} reset failed.",
m_FrameIdx);
m_CommandBuffersAllocated = 0;
m_ImageIdx = imageIdx;
m_SwapchainImage = swapchainImage;
m_SwapchainImageView = swapchainImageView;
}
systems::GraphicsContext systems::GraphicsContext
systems::Frame::CreateGraphicsContext() systems::Frame::CreateGraphicsContext()
{ {
vk::CommandBuffer cmd; vk::CommandBuffer cmd;
if (m_CommandBuffers.size() > m_CommandBuffersAllocated)
{
cmd = m_CommandBuffers[m_CommandBuffersAllocated++];
}
else
{
const vk::CommandBufferAllocateInfo allocateInfo{ const vk::CommandBufferAllocateInfo allocateInfo{
.commandPool = m_Pool, .commandPool = m_Pool,
.level = vk::CommandBufferLevel::ePrimary, .level = vk::CommandBufferLevel::ePrimary,
.commandBufferCount = 1, .commandBufferCount = 1,
}; };
AbortIfFailedMV(m_Device->m_Device.allocateCommandBuffers(&allocateInfo, &cmd), "Command buffer {} alloc failed.", AbortIfFailedMV(m_Device->m_Device.allocateCommandBuffers(&allocateInfo, &cmd),
m_FrameIdx); "Command buffer {} alloc failed.", m_FrameIdx);
m_CommandBuffers.push_back(cmd);
}
return GraphicsContext{cmd}; return GraphicsContext{cmd};
} }
void
systems::Frame::WaitUntilReady()
{
AbortIfFailedMV(m_Device->m_Device.waitForFences(1, &m_FrameAvailableFence, true, MaxValue<u64>),
"Waiting for fence {} failed.", m_FrameIdx);
}
#pragma endregion #pragma endregion
// ==================================================================================================== // ====================================================================================================