@@ -19,6 +19,7 @@ VulkanContext* context;
1919VkSurfaceKHR surface;
2020VulkanSwapchain swapchain;
2121VkRenderPass renderPass;
22+ std::vector<VulkanImage> depthBuffers;
2223std::vector<VkFramebuffer> framebuffers;
2324VulkanPipeline spritePipeline;
2425VulkanPipeline modelPipeline;
@@ -54,17 +55,27 @@ void recreateRenderPass() {
5455 for (uint32_t i = 0 ; i < framebuffers.size (); ++i) {
5556 VK (vkDestroyFramebuffer (context->device , framebuffers[i], 0 ));
5657 }
58+ for (uint32_t i = 0 ; i < depthBuffers.size (); ++i) {
59+ destroyImage (context, &depthBuffers[i]);
60+ }
5761 destroyRenderpass (context, renderPass);
5862 }
5963 framebuffers.clear ();
64+ depthBuffers.clear ();
6065
6166 renderPass = createRenderPass (context, swapchain.format );
6267 framebuffers.resize (swapchain.images .size ());
68+ depthBuffers.resize (swapchain.images .size ());
6369 for (uint32_t i = 0 ; i < swapchain.images .size (); ++i) {
70+ createImage (context, &depthBuffers.data ()[i], swapchain.width , swapchain.height , VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
71+ VkImageView attachments[] = {
72+ swapchain.imageViews [i],
73+ depthBuffers[i].view ,
74+ };
6475 VkFramebufferCreateInfo createInfo = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO };
6576 createInfo.renderPass = renderPass;
66- createInfo.attachmentCount = 1 ;
67- createInfo.pAttachments = &swapchain. imageViews [i] ;
77+ createInfo.attachmentCount = ARRAY_COUNT (attachments) ;
78+ createInfo.pAttachments = attachments ;
6879 createInfo.width = swapchain.width ;
6980 createInfo.height = swapchain.height ;
7081 createInfo.layers = 1 ;
@@ -313,18 +324,21 @@ void renderApplication() {
313324 VkCommandBuffer commandBuffer = commandBuffers[frameIndex];
314325 VKA (vkBeginCommandBuffer (commandBuffer, &beginInfo));
315326
316- VkViewport viewport = { 0 .0f , 0 .0f , (float )swapchain.width , (float )swapchain.height };
327+ VkViewport viewport = { 0 .0f , 0 .0f , (float )swapchain.width , (float )swapchain.height , 0 . 0f , 1 . 0f };
317328 VkRect2D scissor = { {0 , 0 }, {swapchain.width , swapchain.height } };
318329 vkCmdSetViewport (commandBuffer, 0 , 1 , &viewport);
319330 vkCmdSetScissor (commandBuffer, 0 , 1 , &scissor);
320331
321- VkClearValue clearValue = {0 .0f , greenChannel, 1 .0f , 1 .0f };
332+ VkClearValue clearValues[2 ] = {
333+ {0 .0f , greenChannel, 1 .0f , 1 .0f },
334+ {0 .0f , 0 .0f },
335+ };
322336 VkRenderPassBeginInfo beginInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO };
323337 beginInfo.renderPass = renderPass;
324338 beginInfo.framebuffer = framebuffers[imageIndex];
325339 beginInfo.renderArea = { {0 , 0 }, {swapchain.width , swapchain.height } };
326- beginInfo.clearValueCount = 1 ;
327- beginInfo.pClearValues = &clearValue ;
340+ beginInfo.clearValueCount = ARRAY_COUNT (clearValues) ;
341+ beginInfo.pClearValues = clearValues ;
328342 vkCmdBeginRenderPass (commandBuffer, &beginInfo, VK_SUBPASS_CONTENTS_INLINE);
329343
330344#if 0
@@ -415,6 +429,10 @@ void shutdownApplication() {
415429 VK (vkDestroyFramebuffer (context->device , framebuffers[i], 0 ));
416430 }
417431 framebuffers.clear ();
432+ for (uint32_t i = 0 ; i < depthBuffers.size (); ++i) {
433+ destroyImage (context, &depthBuffers[i]);
434+ }
435+ depthBuffers.clear ();
418436 destroyRenderpass (context, renderPass);
419437 destroySwapchain (context, &swapchain);
420438 VK (vkDestroySurfaceKHR (context->instance , surface, 0 ));
0 commit comments