I have previously had a conversation that I want to continue. Here is the previous conversation:

Please write a basic tutorial for incorporating multi pass rendering into the code of a 3d renderer. The multi pass tutorial should make use of the subpass functionality in Vulkan if possible. Assume that the renderer is written in C++ using Vulkan. The current state of the renderer at the beginning of the tutorial is that it is rendering a cube to a window with a texture, color and multisampling with simple lighting. The aim of the tutorial should be to split the lighting into a separate pass with deferred shading.

Here is the current code for the render pass:

void Renderer::CreateRenderPass()
{
vk::AttachmentDescription colorAttachment{};
colorAttachment.format = swapChainImageFormat;
colorAttachment.samples = msaaSamples;
colorAttachment.loadOp = vk::AttachmentLoadOp::eClear;
colorAttachment.storeOp = vk::AttachmentStoreOp::eStore;
colorAttachment.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
colorAttachment.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
colorAttachment.initialLayout = vk::ImageLayout::eUndefined;
colorAttachment.finalLayout = vk::ImageLayout::eColorAttachmentOptimal;

vk::AttachmentDescription colorAttachmentResolve{};
colorAttachmentResolve.format = swapChainImageFormat;
colorAttachmentResolve.samples = vk::SampleCountFlagBits::e1;
colorAttachmentResolve.loadOp = vk::AttachmentLoadOp::eClear;
colorAttachmentResolve.storeOp = vk::AttachmentStoreOp::eStore;
colorAttachmentResolve.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
colorAttachmentResolve.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
colorAttachmentResolve.initialLayout = vk::ImageLayout::eUndefined;
colorAttachmentResolve.finalLayout = vk::ImageLayout::ePresentSrcKHR;

vk::AttachmentDescription depthAttachment{};
depthAttachment.format = FindDepthFormat(*GetPhysicalDevice());
depthAttachment.samples = msaaSamples;
depthAttachment.loadOp = vk::AttachmentLoadOp::eClear;
depthAttachment.storeOp = vk::AttachmentStoreOp::eDontCare;
depthAttachment.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
depthAttachment.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
depthAttachment.initialLayout = vk::ImageLayout::eUndefined;
depthAttachment.finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;

vk::AttachmentReference colorAttachmentRef{};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = vk::ImageLayout::eColorAttachmentOptimal;

vk::AttachmentReference colorAttachmentResolveRef{};
colorAttachmentResolveRef.attachment = 2;
colorAttachmentResolveRef.layout = vk::ImageLayout::eColorAttachmentOptimal;

vk::AttachmentReference depthAttachmentRef{};
depthAttachmentRef.attachment = 1; // Index of the depth attachment in the attachments array
depthAttachmentRef.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;

vk::SubpassDescription subpass{};
subpass.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
subpass.pDepthStencilAttachment = &depthAttachmentRef; // Specify the depth attachment reference
subpass.pResolveAttachments = &colorAttachmentResolveRef;

vk::RenderPassCreateInfo renderPassInfo{};
std::array<vk::AttachmentDescription, 3> attachments = { colorAttachment, depthAttachment, colorAttachmentResolve };
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
renderPassInfo.pAttachments = attachments.data();
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;

vk::SubpassDependency dependency{};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
dependency.srcAccessMask = {};
dependency.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
dependency.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite;

renderPassInfo.dependencyCount = 1;
renderPassInfo.pDependencies = &dependency;

if (device.createRenderPass(&renderPassInfo, nullptr, &renderPass) != vk::Result::eSuccess)
{
throw std::runtime_error(“Failed to create render pass.”);
}
}

Here is the current vertex shader:

#version 450

layout(binding = 0) buffer ModelBufferObject {
mat4 models[];
} modelUBO;

layout(binding = 1) uniform ViewBufferObject {
mat4 view;
} viewUBO;

layout(binding = 2) uniform ProjBufferObject {
mat4 proj;
} projUBO;

layout(binding = 4) uniform ModelCountBuffer {
int modelCount; // Number of models
} modelCountUBO;

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;
layout(location = 3) in vec3 inNormal;

layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;
layout(location = 2) out vec3 fragNormal;
layout(location = 3) out vec3 fragPosition;

void main() {
mat4 model = modelUBO.models[gl_InstanceIndex]; // Use the model matrix for the current instance
gl_Position = projUBO.proj * viewUBO.view * model * vec4(inPosition, 1.0);
fragColor = inColor;
fragTexCoord = inTexCoord;
fragNormal = mat3(modelUBO.models[gl_InstanceIndex]) * inNormal;
fragPosition = vec3(modelUBO.models[gl_InstanceIndex] * vec4(inPosition, 1.0));
}

Here is the current fragment shader:

#version 450

layout(binding = 3) uniform sampler2D texSampler;

layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 2) in vec3 fragNormal;
layout(location = 3) in vec3 fragPosition;

layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 5) uniform Ambient_Data {
vec3 color;
float intensity;
} ambient;

layout(set = 0, binding = 6) uniform Directional_Data {
vec3 position;
vec3 color;
float intensity;
} directional;

void main() {
vec4 textureColor = texture(texSampler, fragTexCoord);
vec4 ambient_color = ambient.intensity * vec4(ambient.color,1.0);
vec3 light_direction = normalize(directional.position - fragPosition);
float directional_intensity = max(dot(fragNormal, light_direction), 0.0);
vec3 directional_color = directional_intensity * directional.color;
vec4 fragmentColor = vec4(fragColor, 1.0); // Convert fragColor from vec3 to vec4
outColor = textureColor * fragmentColor * (ambient_color + vec4(directional_color, 1.0)); // Blend the texture and fragment colors
}




In this tutorial, we’ll add deferred shading to your renderer using Vulkan’s subpass functionality. Deferred shading splits rendering into two passes: the first pass (geometry pass) fills the G-buffer, and the second pass (lighting pass) uses the G-buffer data to compute the final lighting.

Here’s a step-by-step explanation and code to help you transition from your current single-pass renderer to a deferred shading setup.

### Step 1: Create Additional Attachments for the G-buffer

Deferred shading requires multiple attachments to store information like world position, normals, albedo (color), and so forth.

void Renderer::CreateRenderPass()
{
    vk::AttachmentDescription positionAttachment{};
    positionAttachment.format = vk::Format::eR16G16B16A16Sfloat; // High precision format
    positionAttachment.samples = vk::SampleCountFlagBits::e1;
    positionAttachment.loadOp = vk::AttachmentLoadOp::eClear;
    positionAttachment.storeOp = vk::AttachmentStoreOp::eStore;
    positionAttachment.initialLayout = vk::ImageLayout::eUndefined;
    positionAttachment.finalLayout = vk::ImageLayout::eColorAttachmentOptimal;

    vk::AttachmentDescription normalAttachment = positionAttachment;
    normalAttachment.format = vk::Format::eR16G16B16A16Sfloat;

    vk::AttachmentDescription albedoAttachment = positionAttachment;
    albedoAttachment.format = swapChainImageFormat;

    vk::AttachmentDescription depthAttachment = positionAttachment;
    depthAttachment.format = FindDepthFormat(*GetPhysicalDevice());
    depthAttachment.samples = msaaSamples;
    depthAttachment.finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;

    vk::AttachmentDescription colorAttachmentResolve = albedoAttachment;

    std::array<vk::AttachmentDescription, 5> attachments = {
        positionAttachment, normalAttachment, albedoAttachment, depthAttachment, colorAttachmentResolve
    };

    vk::AttachmentReference positionAttachmentRef{};
    positionAttachmentRef.attachment = 0;
    positionAttachmentRef.layout = vk::ImageLayout::eColorAttachmentOptimal;

    vk::AttachmentReference normalAttachmentRef{};
    normalAttachmentRef.attachment = 1;
    normalAttachmentRef.layout = vk::ImageLayout::eColorAttachmentOptimal;

    vk::AttachmentReference albedoAttachmentRef{};
    albedoAttachmentRef.attachment = 2;
    albedoAttachmentRef.layout = vk::ImageLayout::eColorAttachmentOptimal;

    vk::AttachmentReference depthAttachmentRef{};
    depthAttachmentRef.attachment = 3;
    depthAttachmentRef.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;

    vk::AttachmentReference colorAttachmentResolveRef{};
    colorAttachmentResolveRef.attachment = 4;
    colorAttachmentResolveRef.layout = vk::ImageLayout::eColorAttachmentOptimal;

    std::array<vk::AttachmentReference, 3> colorAttachments = {
        positionAttachmentRef, normalAttachmentRef, albedoAttachmentRef
    };

    vk::SubpassDescription geometryPass{};
    geometryPass.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
    geometryPass.colorAttachmentCount = static_cast<uint32_t>(colorAttachments.size());
    geometryPass.pColorAttachments = colorAttachments.data();
    geometryPass.pDepthStencilAttachment = &depthAttachmentRef;

    vk::AttachmentReference depthAttachmentResolveRef{};
    depthAttachmentResolveRef.attachment = 3; // Index of the depth attachment in the attachments array
    depthAttachmentResolveRef.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;

    vk::AttachmentReference colorAttachmentFinalRef{};
    colorAttachmentFinalRef.attachment = 4;
    colorAttachmentFinalRef.layout = vk::ImageLayout::eColorAttachmentOptimal;

    vk::SubpassDescription lightingPass{};
    lightingPass.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
    lightingPass.colorAttachmentCount = 1;
    lightingPass.pColorAttachments = &colorAttachmentFinalRef;

    vk::SubpassDependency dependency{};
    dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
    dependency.dstSubpass = 0;
    dependency.srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
    dependency.srcAccessMask = {};
    dependency.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
    dependency.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite;

    std::array<vk::SubpassDescription, 2> subpasses = { geometryPass, lightingPass };

    vk::RenderPassCreateInfo renderPassInfo{};
    renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
    renderPassInfo.pAttachments = attachments.data();
    renderPassInfo.subpassCount = static_cast<uint32_t>(subpasses.size());
    renderPassInfo.pSubpasses = subpasses.data();
    renderPassInfo.dependencyCount = 1;
    renderPassInfo.pDependencies = &dependency;

    if (device.createRenderPass(&renderPassInfo, nullptr, &renderPass) != vk::Result::eSuccess)
    {
        throw std::runtime_error(“Failed to create render pass.”);
    }
}


### Step 2: Update Vertex Shader

Since we now need to output position, normal, and albedo, we update our vertex shader to account for additional outputs.

Vertex Shader:

#version 450

layout(binding = 0) buffer ModelBufferObject {
    mat4 models[];
} modelUBO;

layout(binding = 1) uniform ViewBufferObject {
    mat4 view;
} viewUBO;

layout(binding = 2) uniform ProjBufferObject {
    mat4 proj;
} projUBO;

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inColor;
layout(location = 2) in vec2 inTexCoord;
layout(location = 3) in vec3 inNormal;

layout(location = 0) out vec3 fragPosition;
layout(location = 1) out vec3 fragNormal;
layout(location = 2) out vec3 fragColor;
layout(location = 3) out vec2 fragTexCoord;

void main() {
    mat4 model = modelUBO.models[gl_InstanceIndex]; // Use the model matrix for the current instance
    gl_Position = projUBO.proj * viewUBO.view * model * vec4(inPosition, 1.0);
    fragPosition = vec3(model * vec4(inPosition, 1.0));
    fragNormal = normalize(mat3(transpose(inverse(model))) * inNormal);
    fragColor = inColor;
    fragTexCoord = inTexCoord;
}


### Step 3: Create Geometry Pass (Fragment Shader)

In the geometry pass, we store world position, normal, and albedo in the respective G-buffer attachments.

Geometry Fragment Shader:

#version 450

layout(location = 0) in vec3 fragPosition;
layout(location = 1) in vec3 fragNormal;
layout(location = 2) in vec3 fragColor;
layout(location = 3) in vec2 fragTexCoord;

layout(binding = 0) uniform sampler2D texSampler;

layout(location = 0) out vec4 gPosition;
layout(location = 1) out vec4 gNormal;
layout(location = 2) out vec4 gAlbedo;

void main() {
    gPosition = vec4(fragPosition, 1.0);
    gNormal = vec4(fragNormal, 1.0);
    gAlbedo = texture(texSampler, fragTexCoord) * vec4(fragColor, 1.0);
}


### Step 4: Create Lighting Pass (Fragment Shader)

The lighting pass computes the final image from the G-buffer data.

Lighting Fragment Shader:

#version 450

layout(location = 0) out vec4 outColor;

layout(binding = 0) uniform sampler2D gPosition;
layout(binding = 1) uniform sampler2D gNormal;
layout(binding = 2) uniform sampler2D gAlbedo;

layout(set = 0, binding = 5) uniform Ambient_Data {
    vec3 color;
    float intensity;
} ambient;

layout(set = 0, binding = 6) uniform Directional_Data {
    vec3 position;
    vec3 color;
    float intensity;
} directional;

void main() {
    vec3 fragPosition = texture(gPosition, fragTexCoord).xyz;
    vec3 fragNormal = normalize(texture(gNormal, fragTexCoord).xyz);
    vec3 fragAlbedo = texture(gAlbedo, fragTexCoord).rgb;

    vec3 ambientLight = ambient.intensity * ambient.color;

    vec3 lightDir = normalize(directional.position - fragPosition);
    float diff = max(dot(fragNormal, lightDir), 0.0);
    vec3 diffuse = diff * directional.color;

    outColor = vec4((ambientLight + diffuse) * fragAlbedo, 1.0);
}


### Step 5: Update Pipeline Configuration

Since we have defined two subpasses, we need to create two graphics pipelines: one for the geometry pass and one for the lighting pass. Use the appropriate shaders and configurations for each.

### Conclusion

This tutorial provided a high-level overview and key code components to convert your single-pass forward render to a deferred renderer with Vulkan subpasses. This setup enables more efficient multiple light rendering by separating the responsibilities into different passes.

Ensure that the pipeline and descriptor sets are properly configured, and you’ll need to update your rendering logic to handle the subpasses correctly.

By following these steps, you have implemented a deferred shading technique in Vulkan. This will give you a robust foundation to build upon for more advanced features like additional light types, shadow mapping, and more.

Can you explain how the pipeline and descriptor sets should be configured to handle this new rendering approach?

Certainly! Configuring the pipeline and descriptor sets to handle the new deferred rendering approach requires creating separate pipelines for the geometry and lighting passes and managing the descriptors appropriately. Here’s a step-by-step outline of the tasks needed to accommodate the deferred rendering system.

### Step 1: Create Descriptor Sets for the Geometry Pass

For the geometry pass, you’ll need descriptors to access your model, view, projection matrices, and texture sampler.

Descriptor Set Layout for Geometry Pass:

void CreateGeometryPassDescriptorSetLayout()
{
    std::array<vk::DescriptorSetLayoutBinding, 4> bindings = {};
    
    // Binding 0: Model matrix buffer
    bindings[0].binding = 0;
    bindings[0].descriptorType = vk::DescriptorType::eUniformBuffer;
    bindings[0].descriptorCount = 1;
    bindings[0].stageFlags = vk::ShaderStageFlagBits::eVertex;

    // Binding 1: View matrix buffer
    bindings[1].binding = 1;
    bindings[1].descriptorType = vk::DescriptorType::eUniformBuffer;
    bindings[1].descriptorCount = 1;
    bindings[1].stageFlags = vk::ShaderStageFlagBits::eVertex;

    // Binding 2: Projection matrix buffer
    bindings[2].binding = 2;
    bindings[2].descriptorType = vk::DescriptorType::eUniformBuffer;
    bindings[2].descriptorCount = 1;
    bindings[2].stageFlags = vk::ShaderStageFlagBits::eVertex;

    // Binding 3: Texture sampler
    bindings[3].binding = 3;
    bindings[3].descriptorType = vk::DescriptorType::eCombinedImageSampler;
    bindings[3].descriptorCount = 1;
    bindings[3].stageFlags = vk::ShaderStageFlagBits::eFragment;

    vk::DescriptorSetLayoutCreateInfo layoutInfo{};
    layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
    layoutInfo.pBindings = bindings.data();

    if (device.createDescriptorSetLayout(&layoutInfo, nullptr, &geometryPassDescriptorSetLayout) != vk::Result::eSuccess) {
        throw std::runtime_error(“Failed to create geometry pass descriptor set layout.”);
    }
}


### Step 2: Create Descriptor Sets for the Lighting Pass

The lighting pass descriptor sets will handle the G-buffer textures (position, normal, albedo) along with ambient and directional light data.

Descriptor Set Layout for Lighting Pass:

void CreateLightingPassDescriptorSetLayout()
{
    std::array<vk::DescriptorSetLayoutBinding, 5> bindings = {};
    
    // Binding 0: Position texture
    bindings[0].binding = 0;
    bindings[0].descriptorType = vk::DescriptorType::eCombinedImageSampler;
    bindings[0].descriptorCount = 1;
    bindings[0].stageFlags = vk::ShaderStageFlagBits::eFragment;

    // Binding 1: Normal texture
    bindings[1].binding = 1;
    bindings[1].descriptorType = vk::DescriptorType::eCombinedImageSampler;
    bindings[1].descriptorCount = 1;
    bindings[1].stageFlags = vk::ShaderStageFlagBits::eFragment;

    // Binding 2: Albedo texture
    bindings[2].binding = 2;
    bindings[2].descriptorType = vk::DescriptorType::eCombinedImageSampler;
    bindings[2].descriptorCount = 1;
    bindings[2].stageFlags = vk::ShaderStageFlagBits::eFragment;

    // Binding 3: Ambient light data
    bindings[3].binding = 3;
    bindings[3].descriptorType = vk::DescriptorType::eUniformBuffer;
    bindings[3].descriptorCount = 1;
    bindings[3].stageFlags = vk::ShaderStageFlagBits::eFragment;

    // Binding 4: Directional light data
    bindings[4].binding = 4;
    bindings[4].descriptorType = vk::DescriptorType::eUniformBuffer;
    bindings[4].descriptorCount = 1;
    bindings[4].stageFlags = vk::ShaderStageFlagBits::eFragment;

    vk::DescriptorSetLayoutCreateInfo layoutInfo{};
    layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
    layoutInfo.pBindings = bindings.data();

    if (device.createDescriptorSetLayout(&layoutInfo, nullptr, &lightingPassDescriptorSetLayout) != vk::Result::eSuccess) {
        throw std::runtime_error(“Failed to create lighting pass descriptor set layout.”);
    }
}


### Step 3: Create Pipeline Layouts

Each pipeline will need its own pipeline layout incorporating the descriptor set layouts created above.

Pipeline Layout for Geometry Pass:

void CreateGeometryPassPipelineLayout()
{
    vk::PipelineLayoutCreateInfo pipelineLayoutInfo{};
    pipelineLayoutInfo.setLayoutCount = 1;
    pipelineLayoutInfo.pSetLayouts = &geometryPassDescriptorSetLayout;

    if (device.createPipelineLayout(&pipelineLayoutInfo, nullptr, &geometryPipelineLayout) != vk::Result::eSuccess) {
        throw std::runtime_error(“Failed to create geometry pass pipeline layout.”);
    }
}


Pipeline Layout for Lighting Pass:

void CreateLightingPassPipelineLayout()
{
    vk::PipelineLayoutCreateInfo pipelineLayoutInfo{};
    pipelineLayoutInfo.setLayoutCount = 1;
    pipelineLayoutInfo.pSetLayouts = &lightingPassDescriptorSetLayout;

    if (device.createPipelineLayout(&pipelineLayoutInfo, nullptr, &lightingPipelineLayout) != vk::Result::eSuccess) {
        throw std::runtime_error(“Failed to create lighting pass pipeline layout.”);
    }
}


### Step 4: Create Graphics Pipelines

Next, you need to create two graphics pipelines: one for the geometry pass and one for the lighting pass.

Graphics Pipeline for Geometry Pass:

void CreateGeometryPassPipeline()
{
    // Load shaders and create shader stages
    vk::ShaderModule vertShaderModule = CreateShaderModule(loadShaderCode(“shaders/geometry_pass.vert.spv”));
    vk::ShaderModule fragShaderModule = CreateShaderModule(loadShaderCode(“shaders/geometry_pass.frag.spv”));

    vk::PipelineShaderStageCreateInfo vertShaderStageInfo{};
    vertShaderStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
    vertShaderStageInfo.module = vertShaderModule;
    vertShaderStageInfo.pName = “main”;

    vk::PipelineShaderStageCreateInfo fragShaderStageInfo{};
    fragShaderStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
    fragShaderStageInfo.module = fragShaderModule;
    fragShaderStageInfo.pName = “main”;

    vk::PipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };

    // Setup other pipeline state (e.g., input layout, viewport, rasterization, etc.)
    vk::PipelineVertexInputStateCreateInfo vertexInputInfo{};
    // … (setup input binding and attribute descriptions)

    vk::PipelineInputAssemblyStateCreateInfo inputAssembly{};
    inputAssembly.topology = vk::PrimitiveTopology::eTriangleList;

    vk::Viewport viewport{};
    vk::Rect2D scissor{};
    vk::PipelineViewportStateCreateInfo viewportState{};
    viewportState.viewportCount = 1;
    viewportState.pViewports = &viewport;
    viewportState.scissorCount = 1;
    viewportState.pScissors = &scissor;

    vk::PipelineRasterizationStateCreateInfo rasterizer{};
    rasterizer.polygonMode = vk::PolygonMode::eFill;
    rasterizer.lineWidth = 1.0f;

    vk::PipelineMultisampleStateCreateInfo multisampling{};
    multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1;

    vk::PipelineDepthStencilStateCreateInfo depthStencil{};
    depthStencil.depthTestEnable = VK_TRUE;
    depthStencil.depthWriteEnable = VK_TRUE;
    depthStencil.depthCompareOp = vk::CompareOp::eLess;

    // Add color attachments for position, normal, and albedo
    vk::PipelineColorBlendAttachmentState colorBlendAttachment{};
    colorBlendAttachment.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
                                          vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;

    std::array<vk::PipelineColorBlendAttachmentState, 3> colorBlendAttachments = {
        colorBlendAttachment, colorBlendAttachment, colorBlendAttachment
    };

    vk::PipelineColorBlendStateCreateInfo colorBlending{};
    colorBlending.attachmentCount = static_cast<uint32_t>(colorBlendAttachments.size());
    colorBlending.pAttachments = colorBlendAttachments.data();

    vk::GraphicsPipelineCreateInfo pipelineInfo{};
    pipelineInfo.stageCount = 2;
    pipelineInfo.pStages = shaderStages;
    pipelineInfo.pVertexInputState = &vertexInputInfo;
    pipelineInfo.pInputAssemblyState = &inputAssembly;
    pipelineInfo.pViewportState = &viewportState;
    pipelineInfo.pRasterizationState = &rasterizer;
    pipelineInfo.pMultisampleState = &multisampling;
    pipelineInfo.pDepthStencilState = &depthStencil;
    pipelineInfo.pColorBlendState = &colorBlending;
    pipelineInfo.layout = geometryPipelineLayout;
    pipelineInfo.renderPass = renderPass;
    pipelineInfo.subpass = 0;

    if (device.createGraphicsPipeline(VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &geometryPipeline) != vk::Result::eSuccess) {
        throw std::runtime_error(“Failed to create geometry pass pipeline.”);
    }

    device.destroyShaderModule(vertShaderModule, nullptr);
    device.destroyShaderModule(fragShaderModule, nullptr);
}


Graphics Pipeline for Lighting Pass:

void CreateLightingPassPipeline()
{
    // Load shaders and create shader stages
    vk::ShaderModule vertShaderModule = CreateShaderModule(loadShaderCode(“shaders/lighting_pass.vert.spv”));
    vk::ShaderModule fragShaderModule = CreateShaderModule(loadShaderCode(“shaders/lighting_pass.frag.spv”));

    vk::PipelineShaderStageCreateInfo vertShaderStageInfo{};
    vertShaderStageInfo.stage = vk::ShaderStageFlagBits::eVertex;
    vertShaderStageInfo.module = vertShaderModule;
    vertShaderStageInfo.pName = “main”;

    vk::PipelineShaderStageCreateInfo fragShaderStageInfo{};
    fragShaderStageInfo.stage = vk::ShaderStageFlagBits::eFragment;
    fragShaderStageInfo.module = fragShaderModule;
    fragShaderStageInfo.pName = “main”;

    vk::PipelineShaderStageCreateInfo shaderStages[] = { vertShaderStageInfo, fragShaderStageInfo };

    // Setup other pipeline state (e.g., input layout, viewport, rasterization, etc.)
    vk::PipelineVertexInputStateCreateInfo vertexInputInfo{};
    // No inputs for screen-space quad vertices

    vk::PipelineInputAssemblyStateCreateInfo inputAssembly{};
    inputAssembly.topology = vk::PrimitiveTopology::eTriangleList;

    vk::Viewport viewport{};
    vk::Rect2D scissor{};
    vk::PipelineViewportStateCreateInfo viewportState{};
    viewportState.viewportCount = 1;
    viewportState.pViewports = &viewport;
    viewportState.scissorCount = 1;
    viewportState.pScissors = &scissor;

    vk::PipelineRasterizationStateCreateInfo rasterizer{};
    rasterizer.polygonMode = vk::PolygonMode::eFill;
    rasterizer.lineWidth = 1.0f;

    vk::PipelineMultisampleStateCreateInfo multisampling{};
    multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1;

    vk::PipelineDepthStencilStateCreateInfo depthStencil{};
    depthStencil.depthTestEnable = VK_FALSE; // No need for depth test in lighting pass

    vk::PipelineColorBlendAttachmentState colorBlendAttachment{};
    colorBlendAttachment.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
                                          vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
    
    vk::PipelineColorBlendStateCreateInfo colorBlending{};
    colorBlending.attachmentCount = 1;
    colorBlending.pAttachments = &colorBlendAttachment;

    vk::GraphicsPipelineCreateInfo pipelineInfo{};
    pipelineInfo.stageCount = 2;
    pipelineInfo.pStages = shaderStages;
    pipelineInfo.pVertexInputState = &vertexInputInfo;
    pipelineInfo.pInputAssemblyState = &inputAssembly;
    pipelineInfo.pViewportState = &viewportState;
    pipelineInfo.pRasterizationState = &rasterizer;
    pipelineInfo.pMultisampleState = &multisampling;
    pipelineInfo.pDepthStencilState = &depthStencil;
    pipelineInfo.pColorBlendState = &colorBlending;
    pipelineInfo.layout = lightingPipelineLayout;
    pipelineInfo.renderPass = renderPass;
    pipelineInfo.subpass = 1;

    if (device.createGraphicsPipeline(VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &lightingPipeline) != vk::Result::eSuccess) {
        throw std::runtime_error(“Failed to create lighting pass pipeline.”);
    }

    device.destroyShaderModule(vertShaderModule, nullptr);
    device.destroyShaderModule(fragShaderModule, nullptr);
}


### Step 5: Descriptor Set Allocation and Update

After creating the descriptor set layouts, allocate and update the descriptor sets.

Allocate and Update Descriptor Sets for Geometry Pass:

void AllocateAndWriteGeometryDescriptorSet()
{
    vk::DescriptorSetAllocateInfo allocInfo{};
    allocInfo.descriptorPool = descriptorPool;
    allocInfo.descriptorSetCount = 1;
    allocInfo.pSetLayouts = &geometryPassDescriptorSetLayout;

    if (device.allocateDescriptorSets(&allocInfo, &geometryDescriptorSet) != vk::Result::eSuccess) {
        throw std::runtime_error(“Failed to allocate geometry descriptor set.”);
    }

    std::array<vk::WriteDescriptorSet, 4> descriptorWrites = {};

    // Binding 0: Model matrix buffer
    vk::DescriptorBufferInfo modelBufferInfo = {};
    modelBufferInfo.buffer = modelUniformBuffer;
    modelBufferInfo.offset = 0;
    modelBufferInfo.range = sizeof(ModelBufferObject);

    descriptorWrites[0].dstSet = geometryDescriptorSet;
    descriptorWrites[0].dstBinding = 0;
    descriptorWrites[0].descriptorType = vk::DescriptorType::eUniformBuffer;
    descriptorWrites[0].descriptorCount = 1;
    descriptorWrites[0].pBufferInfo = &modelBufferInfo;

    // Binding 1: View matrix buffer
    vk::DescriptorBufferInfo viewBufferInfo = {};
    viewBufferInfo.buffer = viewUniformBuffer;
    viewBufferInfo.offset = 0;
    viewBufferInfo.range = sizeof(ViewBufferObject);

    descriptorWrites[1].dstSet = geometryDescriptorSet;
    descriptorWrites[1].dstBinding = 1;
    descriptorWrites[1].descriptorType = vk::DescriptorType::eUniformBuffer;
    descriptorWrites[1].descriptorCount = 1;
    descriptorWrites[1].pBufferInfo = &viewBufferInfo;

    // Binding 2: Projection matrix buffer
    vk::DescriptorBufferInfo projBufferInfo = {};
    projBufferInfo.buffer = projUniformBuffer;
    projBufferInfo.offset = 0;
    projBufferInfo.range = sizeof(ProjBufferObject);

    descriptorWrites[2].dstSet = geometryDescriptorSet;
    descriptorWrites[2].dstBinding = 2;
    descriptorWrites[2].descriptorType = vk::DescriptorType::eUniformBuffer;
    descriptorWrites[2].descriptorCount = 1;
    descriptorWrites[2].pBufferInfo = &projBufferInfo;

    // Binding 3: Texture sampler
    vk::DescriptorImageInfo texSamplerInfo = {};
    texSamplerInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
    texSamplerInfo.imageView = textureImageView;
    texSamplerInfo.sampler = textureSampler;

    descriptorWrites[3].dstSet = geometryDescriptorSet;
    descriptorWrites[3].dstBinding = 3;
    descriptorWrites[3].descriptorType = vk::DescriptorType::eCombinedImageSampler;
    descriptorWrites[3].descriptorCount = 1;
    descriptorWrites[3].pImageInfo = &texSamplerInfo;

    device.updateDescriptorSets(descriptorWrites.size(), descriptorWrites.data(), 0, nullptr);
}


Allocate and Update Descriptor Sets for Lighting Pass:

void AllocateAndWriteLightingDescriptorSet()
{
    vk::DescriptorSetAllocateInfo allocInfo{};
    allocInfo.descriptorPool = descriptorPool;
    allocInfo.descriptorSetCount = 1;
    allocInfo.pSetLayouts = &lightingPassDescriptorSetLayout;

    if (device.allocateDescriptorSets(&allocInfo, &lightingDescriptorSet) != vk::Result::eSuccess) {
        throw std::runtime_error(“Failed to allocate lighting descriptor set.”);
    }

    std::array<vk::WriteDescriptorSet, 5> descriptorWrites = {};

    // Binding 0: Position texture
    vk::DescriptorImageInfo positionTextureInfo = {};
    positionTextureInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
    positionTextureInfo.imageView = positionAttachment.view;
    positionTextureInfo.sampler = positionSampler;

    descriptorWrites[0].dstSet = lightingDescriptorSet;
    descriptorWrites[0].dstBinding = 0;
    descriptorWrites[0].descriptorType = vk::DescriptorType::eCombinedImageSampler;
    descriptorWrites[0].descriptorCount = 1;
    descriptorWrites[0].pImageInfo = &positionTextureInfo;

    // Binding 1: Normal texture
    vk::DescriptorImageInfo normalTextureInfo = {};
    normalTextureInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
    normalTextureInfo.imageView = normalAttachment.view;
    normalTextureInfo.sampler = normalSampler;

    descriptorWrites[1].dstSet = lightingDescriptorSet;
    descriptorWrites[1].dstBinding = 1;
    descriptorWrites[1].descriptorType = vk::DescriptorType::eCombinedImageSampler;
    descriptorWrites[1].descriptorCount = 1;
    descriptorWrites[1].pImageInfo = &normalTextureInfo;

    // Binding 2: Albedo texture
    vk::DescriptorImageInfo albedoTextureInfo = {};
    albedoTextureInfo.imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
    albedoTextureInfo.imageView = albedoAttachment.view;
    albedoTextureInfo.sampler = albedoSampler;

    descriptorWrites[2].dstSet = lightingDescriptorSet;
    descriptorWrites[2].dstBinding = 2;
    descriptorWrites[2].descriptorType = vk::DescriptorType::eCombinedImageSampler;
    descriptorWrites[2].descriptorCount = 1;
    descriptorWrites[2].pImageInfo = &albedoTextureInfo;

    // Binding 3: Ambient light data
    vk::DescriptorBufferInfo ambientBufferInfo = {};
    ambientBufferInfo.buffer = ambientUniformBuffer;
    ambientBufferInfo.offset = 0;
    ambientBufferInfo.range = sizeof(AmbientData);

    descriptorWrites[3].dstSet = lightingDescriptorSet;
    descriptorWrites[3].dstBinding = 3;
    descriptorWrites[3].descriptorType = vk::DescriptorType::eUniformBuffer;
    descriptorWrites[3].descriptorCount = 1;
    descriptorWrites[3].pBufferInfo = &ambientBufferInfo;

    // Binding 4: Directional light data
    vk::DescriptorBufferInfo directionalBufferInfo = {};
    directionalBufferInfo.buffer = directionalUniformBuffer;
    directionalBufferInfo.offset = 0;
    directionalBufferInfo.range = sizeof(DirectionalData);

    descriptorWrites[4].dstSet = lightingDescriptorSet;
    descriptorWrites[4].dstBinding = 4;
    descriptorWrites[4].descriptorType = vk::DescriptorType::eUniformBuffer;
    descriptorWrites[4].descriptorCount = 1;
    descriptorWrites[4].pBufferInfo = &directionalBufferInfo;

    device.updateDescriptorSets(static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
}


### Step 6: Render the Scene with the New Pipelines

To render the scene with the new deferred shading system, you’ll use the new pipelines and descriptor sets within your render loop. Ensure you handle the render pass with subpasses correctly.

Render Loop:

void Render()
{
    // Begin the render pass
    vk::CommandBufferBeginInfo commandBufferBeginInfo{};
    commandBuffer.begin(&commandBufferBeginInfo);

    vk::RenderPassBeginInfo renderPassInfo{};
    renderPassInfo.renderPass = renderPass;
    renderPassInfo.framebuffer = framebuffer; // Handle needs to be created for FB including new attachments
    renderPassInfo.renderArea.offset = { 0, 0 };
    renderPassInfo.renderArea.extent = swapChainExtent;

    std::array<vk::ClearValue, 4> clearValues = {};
    clearValues[0].color = vk::ClearColorValue{ std::array<float, 4>{0.1f, 0.1f, 0.1f, 1.0f} };
    clearValues[1].color = vk::ClearColorValue{ std::array<float, 4>{0.1f, 0.1f, 0.1f, 1.0f} };
    clearValues[2].color = vk::ClearColorValue{ std::array<float, 4>{0.1f, 0.1f, 0.1f, 1.0f} };
    clearValues[3].depthStencil = vk::ClearDepthStencilValue{ 1.0f, 0 };
    renderPassInfo.clearValueCount = clearValues.size();
    renderPassInfo.pClearValues = clearValues.data();

    commandBuffer.beginRenderPass(&renderPassInfo, vk::SubpassContents::eInline);

    // Geometry Pass
    commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, geometryPipeline);
    commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, geometryPipelineLayout, 0, 1, &geometryDescriptorSet, 0, nullptr);

    vk::Buffer vertexBuffers[] = { vertexBuffer };
    vk::DeviceSize offsets[] = { 0 };
    commandBuffer.bindVertexBuffers(0, 1, vertexBuffers, offsets);
    commandBuffer.bindIndexBuffer(indexBuffer, 0, vk::IndexType::eUint32);

    commandBuffer.drawIndexed(static_cast<uint32_t>(indices.size()), 1, 0, 0, 0);

    // Transition to Lighting Pass
    commandBuffer.nextSubpass(vk::SubpassContents::eInline);

    // Lighting Pass
    commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, lightingPipeline);
    commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, lightingPipelineLayout, 0, 1, &lightingDescriptorSet, 0, nullptr);

    // Draw fullscreen quad
    commandBuffer.draw(3, 1, 0, 0);

    commandBuffer.endRenderPass();
    commandBuffer.end();

    // Submit command buffer, present, etc.
}


### Step 7: Update Framebuffers

Make sure the framebuffers are created to incorporate the new attachments for the deferred rendering system. Specifically, add the position, normal, and albedo attachments.

Update Framebuffer Creation:

void CreateFramebuffers()
{
    swapChainFramebuffers.resize(swapChainImageViews.size());

    for (size_t i = 0; i < swapChainImageViews.size(); i++) {
        std::array<vk::ImageView, 5> attachments = {
            positionImageView,
            normalImageView,
            albedoImageView,
            depthImageView,
            swapChainImageViews[i]
        };

        vk::FramebufferCreateInfo framebufferInfo{};
        framebufferInfo.renderPass = renderPass;
        framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
        framebufferInfo.pAttachments = attachments.data();
        framebufferInfo.width = swapChainExtent.width;
        framebufferInfo.height = swapChainExtent.height;
        framebufferInfo.layers = 1;

        if (device.createFramebuffer(&framebufferInfo, nullptr, &swapChainFramebuffers[i]) != vk::Result::eSuccess) {
            throw std::runtime_error(“Failed to create framebuffer.”);
        }
    }
}


### Step 8: Handle Attachment Creation

Ensure you create the images and image views for the position, normal, and albedo attachments.

Create G-buffer Attachments:

void CreateGBufferAttachments()
{
    vk::Format gBufferFormat = vk::Format::eR16G16B16A16Sfloat;
    CreateImage(swapChainExtent.width, swapChainExtent.height, gBufferFormat, vk::ImageTiling::eOptimal, 
                vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eSampled, 
                vk::MemoryPropertyFlagBits::eDeviceLocal, positionImage, positionImageMemory);
    positionImageView = CreateImageView(positionImage, gBufferFormat, vk::ImageAspectFlagBits::eColor);

    CreateImage(swapChainExtent.width, swapChainExtent.height, gBufferFormat, vk::ImageTiling::eOptimal, 
                vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eSampled, 
                vk::MemoryPropertyFlagBits::eDeviceLocal, normalImage, normalImageMemory);
    normalImageView = CreateImageView(normalImage, gBufferFormat, vk::ImageAspectFlagBits::eColor);

    CreateImage(swapChainExtent.width, swapChainExtent.height, swapChainImageFormat, vk::ImageTiling::eOptimal, 
                vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eSampled, 
                vk::MemoryPropertyFlagBits::eDeviceLocal, albedoImage, albedoImageMemory);
    albedoImageView = CreateImageView(albedoImage, swapChainImageFormat, vk::ImageAspectFlagBits::eColor);
}


### Conclusion

By following these steps, you’ve successfully configured descriptor sets, created new pipelines for the geometry and lighting passes, updated your framebuffer creation process, and ensured your renderer is now using a deferred shading approach in Vulkan.

Remember to test thoroughly to ensure proper integration and debugging during this process. This deferred shading technique allows for more complex and efficient lighting calculations, enabling more advanced graphics capabilities in your renderer.