Better structure again (whoohoo)!

This commit is contained in:
2026-02-22 17:42:21 +00:00
parent 864c0247bb
commit d435be97e2
8 changed files with 23 additions and 7 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -25,10 +25,11 @@ namespace cve {
if (cveSwapChain == nullptr) { if (cveSwapChain == nullptr) {
cveSwapChain = std::make_unique<CveSwapChain>(cveDevice, extent); cveSwapChain = std::make_unique<CveSwapChain>(cveDevice, extent);
} else { } else {
cveSwapChain = std::make_unique<CveSwapChain>(cveDevice, extent, std::move(cveSwapChain)); std::shared_ptr<CveSwapChain> oldSwapChain = std::move(cveSwapChain);
if (cveSwapChain->imageCount() != commandBuffers.size()) { cveSwapChain = std::make_unique<CveSwapChain>(cveDevice, extent, oldSwapChain);
freeCommandBuffers();
createCommandBuffers(); if (!oldSwapChain->compareSwapFormats(*cveSwapChain.get())) {
throw std::runtime_error("Swap chain image format/depth has changed.");
} }
} }
@@ -36,7 +37,7 @@ namespace cve {
} }
void CveRenderer::createCommandBuffers() { void CveRenderer::createCommandBuffers() {
commandBuffers.resize(cveSwapChain->imageCount()); commandBuffers.resize(CveSwapChain::MAX_FRAMES_IN_FLIGHT);
VkCommandBufferAllocateInfo allocInfo{}; VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
@@ -99,6 +100,8 @@ namespace cve {
} }
isFrameStarted = false; isFrameStarted = false;
currentFrameIndex = (currentFrameIndex + 1) % CveSwapChain::MAX_FRAMES_IN_FLIGHT;
} }
void CveRenderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer) { void CveRenderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer) {

View File

@@ -23,7 +23,12 @@ namespace cve {
VkCommandBuffer getCurrentCommandBuffer() const { VkCommandBuffer getCurrentCommandBuffer() const {
assert(isFrameStarted && "Cannot get command buffer when frame is not in progress"); assert(isFrameStarted && "Cannot get command buffer when frame is not in progress");
return commandBuffers[currentImageIndex]; return commandBuffers[currentFrameIndex];
}
int getFrameIndex() const {
assert(isFrameStarted && "Cannot get frame index when frame is not started");
return currentFrameIndex;
} }
VkCommandBuffer beginFrame(); VkCommandBuffer beginFrame();
@@ -41,7 +46,8 @@ namespace cve {
std::unique_ptr<CveSwapChain> cveSwapChain; std::unique_ptr<CveSwapChain> cveSwapChain;
std::vector<VkCommandBuffer> commandBuffers; std::vector<VkCommandBuffer> commandBuffers;
uint32_t currentImageIndex; uint32_t currentImageIndex = 0;
int currentFrameIndex = 0;
bool isFrameStarted = false; bool isFrameStarted = false;
}; };
} }

View File

@@ -301,6 +301,7 @@ void CveSwapChain::createFramebuffers() {
void CveSwapChain::createDepthResources() { void CveSwapChain::createDepthResources() {
VkFormat depthFormat = findDepthFormat(); VkFormat depthFormat = findDepthFormat();
swapChainDepthFormat = depthFormat;
VkExtent2D swapChainExtent = getSwapChainExtent(); VkExtent2D swapChainExtent = getSwapChainExtent();
depthImages.resize(imageCount()); depthImages.resize(imageCount());

View File

@@ -40,6 +40,11 @@ class CveSwapChain {
VkResult acquireNextImage(uint32_t *imageIndex); VkResult acquireNextImage(uint32_t *imageIndex);
VkResult submitCommandBuffers(const VkCommandBuffer *buffers, uint32_t *imageIndex); VkResult submitCommandBuffers(const VkCommandBuffer *buffers, uint32_t *imageIndex);
bool compareSwapFormats(const CveSwapChain& swapChain) const {
return swapChain.swapChainDepthFormat == swapChainDepthFormat &&
swapChain.swapChainImageFormat == swapChainImageFormat;
};
private: private:
void init(); void init();
void createSwapChain(); void createSwapChain();
@@ -57,6 +62,7 @@ class CveSwapChain {
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities); VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities);
VkFormat swapChainImageFormat; VkFormat swapChainImageFormat;
VkFormat swapChainDepthFormat;
VkExtent2D swapChainExtent; VkExtent2D swapChainExtent;
std::vector<VkFramebuffer> swapChainFramebuffers; std::vector<VkFramebuffer> swapChainFramebuffers;