Incorporating multi-pass rendering using subpasses in Vulkan involves breaking down the render process into distinct stages. For instance, in a simple rendering engine that initially renders a textured, lit cube, you can break this process into multiple subpasses to separate lighting from geometry rendering.
Below is a basic tutorial on how you can achieve this in C++ using Vulkan:
Step 1: Define the Structure of Render Pass with Subpasses
First, you need to define a render pass that contains multiple subpasses. These subpasses will separate different stages of the rendering, such as geometry rendering in one pass and lighting in another.
Adjust the Render Pass Creation
cpp Copy code
VkAttachmentDescription colorAttachment = {};
colorAttachment.format = swapChainImageFormat;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
// First subpass: Geometry pass
VkSubpassDescription geometrySubpass = {};
geometrySubpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
geometrySubpass.colorAttachmentCount = 1;
geometrySubpass.pColorAttachments = &colorAttachmentRef;
// Second subpass: Lighting pass
VkSubpassDescription lightingSubpass = {};
lightingSubpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
lightingSubpass.colorAttachmentCount = 1;
lightingSubpass.pColorAttachments = &colorAttachmentRef;
// Specify subpass dependencies
std::array<VkSubpassDependency, 2> dependencies;
dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
dependencies[0].dstSubpass = 0;
dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
dependencies[1].srcSubpass = 0;
dependencies[1].dstSubpass = 1;
dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
VkRenderPassCreateInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = 1;
renderPassInfo.pAttachments = &colorAttachment;
renderPassInfo.subpassCount = 2;
renderPassInfo.pSubpasses = (std::array<VkSubpassDescription, 2>){ geometrySubpass, lightingSubpass }.data();
renderPassInfo.dependencyCount = 2;
renderPassInfo.pDependencies = dependencies.data();
if (vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass) != VK_SUCCESS) {
throw std::runtime_error("failed to create render pass!");
}
Step 2: Modify Graphics Pipelines for Subpasses
Create two graphics pipelines: one for the geometry pass and another for the lighting pass.
Pipeline for Geometry Subpass
cpp Copy code
VkGraphicsPipelineCreateInfo pipelineInfo = {};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = 2; // Vertex and Fragment shaders
pipelineInfo.pStages = shaderStages;
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.layout = pipelineLayout;
pipelineInfo.renderPass = renderPass;
pipelineInfo.subpass = 0; // Geometry pass
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
throw std::runtime_error("failed to create graphics pipeline!");
}
Pipeline for Lighting Subpass
This will largely be the same but will use a different shader that performs the lighting calculations.
cpp Copy code
VkGraphicsPipelineCreateInfo lightingPipelineInfo = {};
lightingPipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
lightingPipelineInfo.stageCount = 2; // Vertex and Fragment shaders
lightingPipelineInfo.pStages = lightingShaderStages;
lightingPipelineInfo.pVertexInputState = &vertexInputInfo;
lightingPipelineInfo.pInputAssemblyState = &inputAssembly;
lightingPipelineInfo.pViewportState = &viewportState;
lightingPipelineInfo.pRasterizationState = &rasterizer;
lightingPipelineInfo.pMultisampleState = &multisampling;
lightingPipelineInfo.pColorBlendState = &colorBlending;
lightingPipelineInfo.layout = lightingPipelineLayout;
lightingPipelineInfo.renderPass = renderPass;
lightingPipelineInfo.subpass = 1; // Lighting pass
if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &lightingPipelineInfo, nullptr, &lightingPipeline) != VK_SUCCESS) {
throw std::runtime.error("failed to create lighting pipeline!");
}
Step 3: Record Command Buffers
In the command buffers, issue draw and dispatch commands to both subpasses.
cpp Copy code
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) {
throw std::runtime_error("failed to begin recording command buffer!");
}
VkRenderPassBeginInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = renderPass;
renderPassInfo.framebuffer = swapChainFramebuffers[i];
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = swapChainExtent;
// Clear values
std::array<VkClearValue, 1> clearColors = {};
clearColors[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } };
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearColors.size());
renderPassInfo.pClearValues = clearColors.data();
vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
// Geometry Subpass
vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
vkCmdDraw(commandBuffers[i], static_cast<uint32_t>(vertices.size()), 1, 0, 0);
// Transition to the next subpass
vkCmdNextSubpass(commandBuffers[i], VK_SUBPASS_CONTENTS_INLINE);
// Lighting Subpass
vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, lightingPipeline);
vkCmdDraw(commandBuffers[i], static_cast<uint32_t>(vertices.size()), 1, 0, 0);
vkCmdEndRenderPass(commandBuffers[i]);
if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) {
throw std::runtime_error("failed to record command buffer!");
}
Conclusion
With these steps, your renderer will now separate the geometry and lighting stages into distinct subpasses, improving readability and potentially performance. To further enhance this setup, consider optimizations like batching lights, and utilizing more complex shading techniques like deferred shading.