fix: Context memory leak.
This commit is contained in:
parent
d683de3181
commit
2facb3e6c1
|
|
@ -295,11 +295,8 @@ class GraphicsContext : public Context
|
|||
void Draw(u32 vertexCount);
|
||||
void DrawIndexed(u32 indexCount);
|
||||
|
||||
DEPRECATE_RAW_CALLS
|
||||
void Dependency(const vk::DependencyInfo &dependencyInfo);
|
||||
|
||||
DEPRECATE_RAW_CALLS
|
||||
void BeginRendering(const vk::RenderingInfo &renderingInfo);
|
||||
DEPRECATE_RAW_CALLS void Dependency(const vk::DependencyInfo &dependencyInfo);
|
||||
DEPRECATE_RAW_CALLS void BeginRendering(const vk::RenderingInfo &renderingInfo);
|
||||
void EndRendering();
|
||||
};
|
||||
|
||||
|
|
@ -318,8 +315,12 @@ struct Frame
|
|||
u32 m_ImageIdx;
|
||||
vk::Image m_SwapchainImage;
|
||||
vk::ImageView m_SwapchainImageView;
|
||||
u32 m_ImageIdx;
|
||||
u32 m_CommandBuffersAllocated;
|
||||
|
||||
void Reset(u32 imageIdx, vk::Image swapchainImage, vk::ImageView swapchainImageView);
|
||||
GraphicsContext CreateGraphicsContext();
|
||||
void WaitUntilReady();
|
||||
};
|
||||
|
||||
class Device final
|
||||
|
|
|
|||
|
|
@ -536,8 +536,7 @@ systems::Device::GetNextFrame()
|
|||
|
||||
m_CurrentFrameIdx = (m_CurrentFrameIdx + 1) % MAX_FRAMES_IN_FLIGHT;
|
||||
|
||||
AbortIfFailedMV(m_Device.m_Device.waitForFences(1, ¤tFrame.m_FrameAvailableFence, true, MaxValue<u64>),
|
||||
"Waiting for fence {} failed.", frameIndex);
|
||||
currentFrame.WaitUntilReady();
|
||||
|
||||
u32 imageIndex = 0;
|
||||
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.
|
||||
AbortIfFailedMV(m_Device.m_Device.resetFences(1, ¤tFrame.m_FrameAvailableFence), "Fence {} reset failed.",
|
||||
frameIndex);
|
||||
|
||||
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];
|
||||
// Reset frame here.
|
||||
// In case swapchain was out of date, we will reset the frame next time.
|
||||
currentFrame.Reset(imageIndex, m_Swapchain.m_Images[imageIndex], m_Swapchain.m_ImageViews[imageIndex]);
|
||||
|
||||
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::Frame::CreateGraphicsContext()
|
||||
{
|
||||
vk::CommandBuffer cmd;
|
||||
|
||||
if (m_CommandBuffers.size() > m_CommandBuffersAllocated)
|
||||
{
|
||||
cmd = m_CommandBuffers[m_CommandBuffersAllocated++];
|
||||
}
|
||||
else
|
||||
{
|
||||
const vk::CommandBufferAllocateInfo allocateInfo{
|
||||
.commandPool = m_Pool,
|
||||
.level = vk::CommandBufferLevel::ePrimary,
|
||||
.commandBufferCount = 1,
|
||||
};
|
||||
AbortIfFailedMV(m_Device->m_Device.allocateCommandBuffers(&allocateInfo, &cmd), "Command buffer {} alloc failed.",
|
||||
m_FrameIdx);
|
||||
AbortIfFailedMV(m_Device->m_Device.allocateCommandBuffers(&allocateInfo, &cmd),
|
||||
"Command buffer {} alloc failed.", m_FrameIdx);
|
||||
m_CommandBuffers.push_back(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
|
||||
|
||||
// ====================================================================================================
|
||||
|
|
|
|||
Loading…
Reference in New Issue