狸村 Mystic Web · 狸村
目录

目标:确保基础项目使用了*动态渲染*,并了解如何使用 RenderDoc 验证它。

在动态渲染中,我们不再创建 VkRenderPass 或 VkFrameBuffer;而是通过 vkCmdBeginRenderingKHR 开始渲染,并即时指定附件。这使得我们的代码更加灵活(无需预先声明子通道),并且现在是 Vulkan 中“现代”的渲染方式。

任务 1:检查动态渲染的设置

在提供的代码库中,找到图形管线的初始化代码:

/* TASK01:检查动态渲染的设置
 *
 * 这个新结构体取代了之前在管线创建中的渲染通道。
 * 请注意,此结构体现在通过下面的 .pNext 链接,而 .renderPass 不再使用。
 */
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
    .colorAttachmentCount = 1,
    .pColorAttachmentFormats = &swapChainImageFormat,
    .depthAttachmentFormat = depthFormat
};

vk::GraphicsPipelineCreateInfo pipelineInfo{
    .pNext = &pipelineRenderingCreateInfo,
    .stageCount = 2,
    .pStages = shaderStages,
    .pVertexInputState = &vertexInputInfo,
    .pInputAssemblyState = &inputAssembly,
    .pViewportState = &viewportState,
    .pRasterizationState = &rasterizer,
    .pMultisampleState = &multisampling,
    .pDepthStencilState = &depthStencil,
    .pColorBlendState = &colorBlending,
    .pDynamicState = &dynamicState,
    .layout = pipelineLayout,
    .renderPass = nullptr
};

graphicsPipeline = vk::raii::Pipeline(device, nullptr, pipelineInfo);

然后在命令缓冲录制中,我们开始渲染:

/* TASK01:检查动态渲染的设置
 *
 * 使用动态渲染,我们直接在 vk::RenderingAttachmentInfo 结构体中指定图像视图和加载/存储操作。
 * 这种方法消除了对显式渲染通道和帧缓冲对象的需求,简化了代码,并提供了在运行时更改附件的灵活性。
 */

vk::RenderingAttachmentInfo colorAttachmentInfo = {
    .imageView = swapChainImageViews[imageIndex],
    .imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
    .loadOp = vk::AttachmentLoadOp::eClear,
    .storeOp = vk::AttachmentStoreOp::eStore,
    .clearValue = clearColor
};

vk::RenderingAttachmentInfo depthAttachmentInfo = {
    .imageView = depthImageView,
    .imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
    .loadOp = vk::AttachmentLoadOp::eClear,
    .storeOp = vk::AttachmentStoreOp::eDontCare,
    .clearValue = clearDepth
};

// vk::RenderingInfo 结构体将这些附件与其他渲染参数组合在一起。
vk::RenderingInfo renderingInfo = {
    .renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
    .layerCount = 1,
    .colorAttachmentCount = 1,
    .pColorAttachments = &colorAttachmentInfo,
    .pDepthAttachment = &depthAttachmentInfo
};

// 注意:.beginRendering 取代了之前的 .beginRenderPass 调用。
commandBuffers[frameIndex].beginRendering(renderingInfo);

有关更多上下文,请参考之前的教程 章节

使用 RenderDoc 验证动态渲染

使用 RenderDoc 启动应用程序并捕获一帧:

  1. 指定可执行文件路径:Vulkan-Tutorial\attachments\build\38_ray_tracing\Debug\38_ray_tracing.exe

  2. 指定工作目录:Vulkan-Tutorial\attachments\build\38_ray_tracing

  3. 启动应用程序。

38 TASK01 renderdoc launch

在事件浏览器中,你应该看到以下调用,确认动态渲染已正确设置:

  1. vkCmdBeginRenderingKHRvkCmdEndRenderingKHR

  2. VkRenderingInfoKHR 取代了旧的渲染通道/帧缓冲概念。

  3. 通过 VkRenderingAttachmentInfo 设置颜色(和深度)附件。

38 TASK01 renderdoc events

在 RenderDoc 的纹理查看器中,你可以在不同点检查颜色和深度附件:

38 TASK01 renderdoc color

注意:动态渲染降低了 CPU 开销,并且结合 VK_KHR_dynamic_rendering_local_read 扩展,你可以在无需完整渲染通道的情况下进行类似子通道的 tile 本地读取。这对于在基于 tile 的渲染器上实现延迟着色等技术非常有用,因为可以在 tile 上直接读取上一个通道的附件,无需额外的内存带宽。虽然我们在这里不会实现延迟渲染器,但请注意这对移动端的益处。

完成此步骤后,你应该确信动态渲染已正确设置。现在我们可以继续学习光线追踪特性了。

导航

Vulkan 是 Khronos Group Inc. 的注册商标

教程内容版权归原作者,遵循 CC BY-SA 4.0;本站独立代码及设计除外。