From cffe42e72b309a091d648d367a087bd2b9b4f6a3 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 4 Oct 2025 14:49:40 +0100 Subject: [PATCH] Create shadow render pass --- gpu.c | 66 ++++++++++++++++++++++++++++++++++++++++++++--------------- gpu.h | 1 + 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/gpu.c b/gpu.c index 911a83b..65aa425 100644 --- a/gpu.c +++ b/gpu.c @@ -393,9 +393,8 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name) } // Set up depth image - const VkFormat depth_img_fmt = VK_FORMAT_D32_SFLOAT; create_img( - ctx, depth_img_fmt, ctx->swapchain_extent.width, + ctx, VK_FORMAT_D32_SFLOAT, ctx->swapchain_extent.width, ctx->swapchain_extent.height, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, 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, }; - // Set up colour attachment + // Attachment configs const VkAttachmentDescription colour_attachment = { .format = surface_fmt.format, .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, .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, }; - const VkAttachmentReference colour_attachment_ref = { - .attachment = 0, - .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + const VkAttachmentDescription shadow_depth_attachment = { + .format = VK_FORMAT_D32_SFLOAT, + .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, }; - - // Set up depth attatchment - const VkAttachmentDescription depth_attatchment = { - .format = depth_img_fmt, + const VkAttachmentDescription render_depth_attachment = { + .format = VK_FORMAT_D32_SFLOAT, .samples = VK_SAMPLE_COUNT_1_BIT, .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, .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, .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, }; - VkAttachmentReference depth_attachment_ref = { - .attachment = 1, - .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + + // Create shadow pass + 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 const VkAttachmentDescription attachment_descs[] = { colour_attachment, - depth_attatchment, + render_depth_attachment, }; const VkSubpassDependency dependency = { .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 = { .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, - .attachmentCount = 2, + .attachmentCount = NELEMS(attachment_descs), .pAttachments = attachment_descs, .subpassCount = 1, .pSubpasses = &(VkSubpassDescription){ .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, .colorAttachmentCount = 1, - .pColorAttachments = &colour_attachment_ref, - .pDepthStencilAttachment = &depth_attachment_ref, + .pColorAttachments = &(VkAttachmentReference) { + .attachment = 0, + .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + }, + .pDepthStencilAttachment = &(VkAttachmentReference) { + .attachment = 1, + .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + }, }, .dependencyCount = 1, .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->frag_shader, nullptr); + vkDestroyRenderPass(ctx->dev, ctx->shadow_pass, nullptr); vkDestroyRenderPass(ctx->dev, ctx->render_pass, nullptr); vkDestroyImageView(ctx->dev, ctx->depth_view, nullptr); diff --git a/gpu.h b/gpu.h index c67a68b..ec1c36c 100644 --- a/gpu.h +++ b/gpu.h @@ -28,6 +28,7 @@ typedef struct { VkShaderModule frag_shader; VkDescriptorSetLayout desc_set_layout; VkDescriptorPool desc_pool; + VkRenderPass shadow_pass; VkRenderPass render_pass; VkPipelineLayout floor_pipeline_layout; VkPipeline floor_pipeline;