Create shadow render pass

This commit is contained in:
Camden Dixie O'Brien committed 2025-10-04 14:49:40 +01:00
1 parent aa2fc4a41a
commit cffe42e72b
2 files changed
+50 -17

No files matched your search

+49 -17
View File
@@ -393,9 +393,8 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name)
} }
// Set up depth image // Set up depth image
const VkFormat depth_img_fmt = VK_FORMAT_D32_SFLOAT;
create_img( create_img(
ctx, depth_img_fmt, ctx->swapchain_extent.width, ctx, VK_FORMAT_D32_SFLOAT, ctx->swapchain_extent.width,
ctx->swapchain_extent.height, ctx->swapchain_extent.height,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
VK_IMAGE_ASPECT_DEPTH_BIT, &ctx->depth_img, &ctx->depth_mem, VK_IMAGE_ASPECT_DEPTH_BIT, &ctx->depth_img, &ctx->depth_mem,
@@ -417,7 +416,7 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name)
.pScissors = &scissor, .pScissors = &scissor,
}; };
// Set up colour attachment // Attachment configs
const VkAttachmentDescription colour_attachment = { const VkAttachmentDescription colour_attachment = {
.format = surface_fmt.format, .format = surface_fmt.format,
.samples = VK_SAMPLE_COUNT_1_BIT, .samples = VK_SAMPLE_COUNT_1_BIT,
@@ -428,14 +427,18 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name)
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
}; };
const VkAttachmentReference colour_attachment_ref = { const VkAttachmentDescription shadow_depth_attachment = {
.attachment = 0, .format = VK_FORMAT_D32_SFLOAT,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, .samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
}; };
const VkAttachmentDescription render_depth_attachment = {
// Set up depth attatchment .format = VK_FORMAT_D32_SFLOAT,
const VkAttachmentDescription depth_attatchment = {
.format = depth_img_fmt,
.samples = VK_SAMPLE_COUNT_1_BIT, .samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
@@ -444,15 +447,37 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name)
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
}; };
VkAttachmentReference depth_attachment_ref = {
.attachment = 1, // Create shadow pass
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, const VkRenderPassCreateInfo shadow_pass_config = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.attachmentCount = 1,
.pAttachments = &shadow_depth_attachment,
.subpassCount = 1,
.pSubpasses = &(VkSubpassDescription){
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.pDepthStencilAttachment = &(VkAttachmentReference) {
.attachment = 0,
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
},
},
.dependencyCount = 1,
.pDependencies = &(VkSubpassDependency) {
.srcSubpass = VK_SUBPASS_EXTERNAL,
.srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
.srcAccessMask = VK_ACCESS_SHADER_READ_BIT,
.dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
},
}; };
result = vkCreateRenderPass(
ctx->dev, &shadow_pass_config, nullptr, &ctx->shadow_pass);
assert(result == VK_SUCCESS);
// Create render pass // Create render pass
const VkAttachmentDescription attachment_descs[] = { const VkAttachmentDescription attachment_descs[] = {
colour_attachment, colour_attachment,
depth_attatchment, render_depth_attachment,
}; };
const VkSubpassDependency dependency = { const VkSubpassDependency dependency = {
.srcSubpass = VK_SUBPASS_EXTERNAL, .srcSubpass = VK_SUBPASS_EXTERNAL,
@@ -466,14 +491,20 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name)
}; };
const VkRenderPassCreateInfo render_pass_config = { const VkRenderPassCreateInfo render_pass_config = {
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.attachmentCount = 2, .attachmentCount = NELEMS(attachment_descs),
.pAttachments = attachment_descs, .pAttachments = attachment_descs,
.subpassCount = 1, .subpassCount = 1,
.pSubpasses = &(VkSubpassDescription){ .pSubpasses = &(VkSubpassDescription){
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.colorAttachmentCount = 1, .colorAttachmentCount = 1,
.pColorAttachments = &colour_attachment_ref, .pColorAttachments = &(VkAttachmentReference) {
.pDepthStencilAttachment = &depth_attachment_ref, .attachment = 0,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
},
.pDepthStencilAttachment = &(VkAttachmentReference) {
.attachment = 1,
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
},
}, },
.dependencyCount = 1, .dependencyCount = 1,
.pDependencies = &dependency, .pDependencies = &dependency,
@@ -833,6 +864,7 @@ void gpu_cleanup(const gpu_ctx_t *ctx)
vkDestroyShaderModule(ctx->dev, ctx->model_vert_shader, nullptr); vkDestroyShaderModule(ctx->dev, ctx->model_vert_shader, nullptr);
vkDestroyShaderModule(ctx->dev, ctx->frag_shader, nullptr); vkDestroyShaderModule(ctx->dev, ctx->frag_shader, nullptr);
vkDestroyRenderPass(ctx->dev, ctx->shadow_pass, nullptr);
vkDestroyRenderPass(ctx->dev, ctx->render_pass, nullptr); vkDestroyRenderPass(ctx->dev, ctx->render_pass, nullptr);
vkDestroyImageView(ctx->dev, ctx->depth_view, nullptr); vkDestroyImageView(ctx->dev, ctx->depth_view, nullptr);
+1
View File
@@ -28,6 +28,7 @@ typedef struct {
VkShaderModule frag_shader; VkShaderModule frag_shader;
VkDescriptorSetLayout desc_set_layout; VkDescriptorSetLayout desc_set_layout;
VkDescriptorPool desc_pool; VkDescriptorPool desc_pool;
VkRenderPass shadow_pass;
VkRenderPass render_pass; VkRenderPass render_pass;
VkPipelineLayout floor_pipeline_layout; VkPipelineLayout floor_pipeline_layout;
VkPipeline floor_pipeline; VkPipeline floor_pipeline;