|
| 1 | +== Dynamic Rendering |
| 2 | + |
| 3 | +*Objective*: Ensure the base project uses *dynamic rendering* and understand how to verify it using RenderDoc. |
| 4 | + |
| 5 | +In dynamic rendering, we no longer create a VkRenderPass or VkFrameBuffer; instead we begin rendering with `vkCmdBeginRenderingKHR`, specifying attachments on-the-fly. This makes our code more flexible (no need to predeclare subpasses) and is now the "modern" way to render in Vulkan. |
| 6 | + |
| 7 | +=== Task 1: Check the setup for dynamic rendering |
| 8 | + |
| 9 | +In the provided code base, locate the initialization of the graphics pipeline: |
| 10 | + |
| 11 | +[,c{pp}] |
| 12 | +---- |
| 13 | +/* TASK01: Check the setup for dynamic rendering |
| 14 | + * |
| 15 | + * This new struct replaces what previously was the render pass in the pipeline creation. |
| 16 | + * Note how this structure is now linked in .pNext below, and .renderPass is not used. |
| 17 | + */ |
| 18 | +vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ |
| 19 | + .colorAttachmentCount = 1, |
| 20 | + .pColorAttachmentFormats = &swapChainImageFormat, |
| 21 | + .depthAttachmentFormat = depthFormat |
| 22 | +}; |
| 23 | +
|
| 24 | +vk::GraphicsPipelineCreateInfo pipelineInfo{ |
| 25 | + .pNext = &pipelineRenderingCreateInfo, |
| 26 | + .stageCount = 2, |
| 27 | + .pStages = shaderStages, |
| 28 | + .pVertexInputState = &vertexInputInfo, |
| 29 | + .pInputAssemblyState = &inputAssembly, |
| 30 | + .pViewportState = &viewportState, |
| 31 | + .pRasterizationState = &rasterizer, |
| 32 | + .pMultisampleState = &multisampling, |
| 33 | + .pDepthStencilState = &depthStencil, |
| 34 | + .pColorBlendState = &colorBlending, |
| 35 | + .pDynamicState = &dynamicState, |
| 36 | + .layout = pipelineLayout, |
| 37 | + .renderPass = nullptr |
| 38 | +}; |
| 39 | +
|
| 40 | +graphicsPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo); |
| 41 | +---- |
| 42 | + |
| 43 | +And later on, the command buffer recording where we begin rendering: |
| 44 | + |
| 45 | +[,c{pp}] |
| 46 | +---- |
| 47 | +/* TASK01: Check the setup for dynamic rendering |
| 48 | + * |
| 49 | + * With dynamic rendering, we specify the image view and load/store operations directly |
| 50 | + * in the vk::RenderingAttachmentInfo structure. |
| 51 | + * This approach eliminates the need for explicit render pass and framebuffer objects, |
| 52 | + * simplifying the code and providing flexibility to change attachments at runtime. |
| 53 | + */ |
| 54 | +
|
| 55 | +vk::RenderingAttachmentInfo colorAttachmentInfo = { |
| 56 | + .imageView = swapChainImageViews[imageIndex], |
| 57 | + .imageLayout = vk::ImageLayout::eColorAttachmentOptimal, |
| 58 | + .loadOp = vk::AttachmentLoadOp::eClear, |
| 59 | + .storeOp = vk::AttachmentStoreOp::eStore, |
| 60 | + .clearValue = clearColor |
| 61 | +}; |
| 62 | +
|
| 63 | +vk::RenderingAttachmentInfo depthAttachmentInfo = { |
| 64 | + .imageView = depthImageView, |
| 65 | + .imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, |
| 66 | + .loadOp = vk::AttachmentLoadOp::eClear, |
| 67 | + .storeOp = vk::AttachmentStoreOp::eDontCare, |
| 68 | + .clearValue = clearDepth |
| 69 | +}; |
| 70 | +
|
| 71 | +// The vk::RenderingInfo structure combines these attachments with other rendering parameters. |
| 72 | +vk::RenderingInfo renderingInfo = { |
| 73 | + .renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent }, |
| 74 | + .layerCount = 1, |
| 75 | + .colorAttachmentCount = 1, |
| 76 | + .pColorAttachments = &colorAttachmentInfo, |
| 77 | + .pDepthAttachment = &depthAttachmentInfo |
| 78 | +}; |
| 79 | +
|
| 80 | +// Note: .beginRendering replaces the previous .beginRenderPass call. |
| 81 | +commandBuffers[currentFrame].beginRendering(renderingInfo); |
| 82 | +---- |
| 83 | + |
| 84 | +For more context, refer to the previous tutorial link:../../03_Drawing_a_triangle/02_Graphics_pipeline_basics/03_Render_passes.adoc[chapter]. |
| 85 | + |
| 86 | +==== Dynamic rendering with RenderDoc |
| 87 | + |
| 88 | +Use RenderDoc to launch the application and capture a frame: |
| 89 | + |
| 90 | +. Specify executable path: `Vulkan-Tutorial\attachments\build\38_ray_tracing\Debug\38_ray_tracing.exe`. |
| 91 | +. Specify working directory: `Vulkan-Tutorial\attachments\build\38_ray_tracing`. |
| 92 | +. Launch the application. |
| 93 | + |
| 94 | +image::../../../images/38_TASK01_renderdoc_launch.png[] |
| 95 | + |
| 96 | +In the Event Browser, you should see the calls that confirm that dynamic rendering is set up correctly: |
| 97 | + |
| 98 | +. `vkCmdBeginRenderingKHR` and `vkCmdEndRenderingKHR`. |
| 99 | +. `VkRenderingInfoKHR` replacing the old render pass/framebuffer concept. |
| 100 | +. Color (and depth) attachments set via `VkRenderingAttachmentInfo`. |
| 101 | + |
| 102 | +image::../../../images/38_TASK01_renderdoc_events.png[] |
| 103 | + |
| 104 | +In RenderDoc's Texture Viewer, you can inspect the color and depth attachments at various points: |
| 105 | + |
| 106 | +image::../../../images/38_TASK01_renderdoc_color.gif[] |
| 107 | + |
| 108 | +NOTE: Dynamic rendering reduces CPU overhead and, with the `VK_KHR_dynamic_rendering_local_read` extension, lets you do subpass-style tile-local reads without full render passes. This is great for techniques like deferred shading on tilers, where reading from a previous pass's attachment can be done on-tile without extra memory bandwidth. While we won't implement a deferred renderer here, be aware of this benefit for mobile. |
| 109 | + |
| 110 | +After this step, you should be comfortable that dynamic rendering is set up correctly. We can now move on to ray tracing features. |
| 111 | + |
| 112 | +=== Navigation |
| 113 | +- Previous: link:00_Overview.adoc[Overview] |
| 114 | +- Next: link:02_Acceleration_structures.adoc[Acceleration structures] |
0 commit comments