|
| 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 | +[,c++] |
| 10 | +---- |
| 11 | +/* TASK01: Check the setup for dynamic rendering */ |
| 12 | +// See pipeline creation: vk::PipelineRenderingCreateInfo with .pNext chain |
| 13 | +---- |
| 14 | + |
| 15 | +In RenderDoc, verify you see the calls that confirm that dynamic rendering is set up correctly: |
| 16 | + |
| 17 | +. vkCmdBeginRenderingKHR and vkCmdEndRenderingKHR. |
| 18 | +. VkRenderingInfoKHR which replaces the fixed framebuffer object. |
| 19 | +. Color attachment. |
| 20 | + |
| 21 | +image::../../../images/38_TASK01_renderdoc_events.png[] |
| 22 | +image::../../../images/38_TASK01_renderdoc_color.gif[] |
| 23 | + |
| 24 | +NOTE: Dynamic rendering reduces CPU overhead and, with VK_KHR_dynamic_rendering_local_read, lets you do subpass-style tile-local reads without full render passes. This is great for techniques like deferred shading on tilers. |
| 25 | + |
| 26 | + |
| 27 | +==== Dynamic rendering with RenderDoc |
| 28 | + |
| 29 | +Let’s make sure you can actually see dynamic rendering at work in a capture, and also look at the key places in code. |
| 30 | + |
| 31 | +How to capture with RenderDoc (Windows): |
| 32 | + |
| 33 | +. Set Executable to your built sample, e.g. C:\\Users\\you\\Vulkan-Tutorial\\attachments\\build\\38_ray_tracing\\Debug\\38_ray_tracing.exe |
| 34 | +. Set Working Directory to the folder containing the executable, e.g. ...\\attachments\\build\\38_ray_tracing |
| 35 | +. Launch and capture a frame. |
| 36 | + |
| 37 | +image::../../../images/38_TASK01_renderdoc_launch.png[] |
| 38 | + |
| 39 | +In the Event Browser and API Inspector you should see: |
| 40 | + |
| 41 | +- vkCmdBeginRenderingKHR / vkCmdEndRenderingKHR |
| 42 | +- VkRenderingInfoKHR replacing the old render pass/framebuffer concept |
| 43 | +- Color (and depth) attachments set via RenderingAttachmentInfo |
| 44 | + |
| 45 | +The relevant code locations you can skim for orientation: |
| 46 | + |
| 47 | +[,c++] |
| 48 | +---- |
| 49 | +/* TASK01 (excerpt): dynamic rendering is enabled by linking |
| 50 | + * vk::PipelineRenderingCreateInfo in the pNext chain when creating the graphics pipeline. |
| 51 | + */ |
| 52 | +vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ |
| 53 | + .colorAttachmentCount = 1, |
| 54 | + .pColorAttachmentFormats = &swapChainImageFormat, |
| 55 | + .depthAttachmentFormat = depthFormat |
| 56 | +}; |
| 57 | +
|
| 58 | +vk::GraphicsPipelineCreateInfo pipelineInfo{ |
| 59 | + .pNext = &pipelineRenderingCreateInfo, |
| 60 | + // ... standard pipeline state setup omitted for brevity ... |
| 61 | + .layout = pipelineLayout, |
| 62 | + .renderPass = nullptr |
| 63 | +}; |
| 64 | +---- |
| 65 | + |
| 66 | +And when recording the command buffer for a frame: |
| 67 | + |
| 68 | +[,c++] |
| 69 | +---- |
| 70 | +// These attachment infos replace subpass attachments in a legacy render pass |
| 71 | +vk::RenderingAttachmentInfo colorAttachmentInfo{ |
| 72 | + .imageView = swapChainImageViews[imageIndex], |
| 73 | + .imageLayout = vk::ImageLayout::eColorAttachmentOptimal, |
| 74 | + .loadOp = vk::AttachmentLoadOp::eClear, |
| 75 | + .storeOp = vk::AttachmentStoreOp::eStore, |
| 76 | + .clearValue = clearColor |
| 77 | +}; |
| 78 | +
|
| 79 | +vk::RenderingAttachmentInfo depthAttachmentInfo{ |
| 80 | + .imageView = depthImageView, |
| 81 | + .imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, |
| 82 | + .loadOp = vk::AttachmentLoadOp::eClear, |
| 83 | + .storeOp = vk::AttachmentStoreOp::eDontCare, |
| 84 | + .clearValue = clearDepth |
| 85 | +}; |
| 86 | +
|
| 87 | +vk::RenderingInfo renderingInfo{ |
| 88 | + .renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent }, |
| 89 | + .layerCount = 1, |
| 90 | + .colorAttachmentCount = 1, |
| 91 | + .pColorAttachments = &colorAttachmentInfo, |
| 92 | + .pDepthAttachment = &depthAttachmentInfo |
| 93 | +}; |
| 94 | +
|
| 95 | +// vkCmdBeginRenderingKHR replaces vkCmdBeginRenderPass |
| 96 | +commandBuffer.beginRendering(renderingInfo); |
| 97 | +// ... draw calls ... |
| 98 | +commandBuffer.endRendering(); |
| 99 | +---- |
| 100 | + |
| 101 | +That’s all there is to it. Dynamic rendering cuts out a lot of boilerplate and is the recommended path in modern Vulkan. It also pairs nicely with ray queries because you keep everything in a single raster pipeline. |
| 102 | + |
| 103 | + |
| 104 | +=== Navigation |
| 105 | +- Previous: link:00_Overview.adoc[Overview] |
| 106 | +- Next: link:02_Acceleration_structures.adoc[Acceleration structures] |
0 commit comments