diff --git a/gpu.c b/gpu.c index 33da806..97adc48 100644 --- a/gpu.c +++ b/gpu.c @@ -418,7 +418,7 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name) .maxDepth = 1.0, }; const VkRect2D scissor = { .extent = ctx->swapchain_extent }; - VkPipelineViewportStateCreateInfo viewport_config = { + const VkPipelineViewportStateCreateInfo viewport_config = { .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, .viewportCount = 1, .pViewports = &viewport, @@ -712,6 +712,73 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name) .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, }; + // Create model pipeline layout + const VkPipelineLayoutCreateInfo model_pipeline_layout_config = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, + .pSetLayouts = &ctx->desc_set_layout, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &push_constant_range, + }; + result = vkCreatePipelineLayout( + ctx->dev, &model_pipeline_layout_config, nullptr, + &ctx->model_pipeline_layout); + assert(result == VK_SUCCESS); + + // Create shadow pipeline + const VkPipelineViewportStateCreateInfo shadow_viewport_config = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, + .pViewports = &(VkViewport) { + .width = SHADOW_IMG_SIZE, + .height = SHADOW_IMG_SIZE, + .minDepth = 0.0, + .maxDepth = 1.0, + }, + .scissorCount = 1, + .pScissors = &(VkRect2D) { + .extent = { + .width = SHADOW_IMG_SIZE, + .height = SHADOW_IMG_SIZE, + }, + }, + }; + const VkPipelineShaderStageCreateInfo shadow_shader_stages[] = { + { + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_VERTEX_BIT, + .module = ctx->model_vert_shader, + .pName = "main", + }, + }; + const VkPipelineRasterizationStateCreateInfo shadow_rasterizer_config = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, + .depthClampEnable = VK_FALSE, + .rasterizerDiscardEnable = VK_FALSE, + .polygonMode = VK_POLYGON_MODE_FILL, + .lineWidth = 1.0f, + .cullMode = VK_CULL_MODE_NONE, + .frontFace = VK_FRONT_FACE_CLOCKWISE, + }; + const VkGraphicsPipelineCreateInfo shadow_pipeline_config = { + .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, + .stageCount = NELEMS(shadow_shader_stages), + .pStages = shadow_shader_stages, + .pVertexInputState = &vertex_input_config, + .pInputAssemblyState = &input_assembly_config, + .pViewportState = &shadow_viewport_config, + .pRasterizationState = &shadow_rasterizer_config, + .pMultisampleState = &multisampling_config, + .layout = ctx->model_pipeline_layout, + .renderPass = ctx->shadow_pass, + .subpass = 0, + .pDepthStencilState = &depth_stencil_config, + }; + result = vkCreateGraphicsPipelines( + ctx->dev, VK_NULL_HANDLE, 1, &shadow_pipeline_config, nullptr, + &ctx->shadow_pipeline); + assert(result == VK_SUCCESS); + // Create model pipeline const VkPipelineShaderStageCreateInfo model_shader_stages[] = { { @@ -736,20 +803,9 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name) .cullMode = VK_CULL_MODE_BACK_BIT, .frontFace = VK_FRONT_FACE_CLOCKWISE, }; - const VkPipelineLayoutCreateInfo model_pipeline_layout_config = { - .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, - .setLayoutCount = 1, - .pSetLayouts = &ctx->desc_set_layout, - .pushConstantRangeCount = 1, - .pPushConstantRanges = &push_constant_range, - }; - result = vkCreatePipelineLayout( - ctx->dev, &model_pipeline_layout_config, nullptr, - &ctx->model_pipeline_layout); - assert(result == VK_SUCCESS); const VkGraphicsPipelineCreateInfo model_pipeline_config = { .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, - .stageCount = 2, + .stageCount = NELEMS(model_shader_stages), .pStages = model_shader_stages, .pVertexInputState = &vertex_input_config, .pInputAssemblyState = &input_assembly_config, @@ -778,8 +834,8 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name) .layers = 1, }; result = vkCreateFramebuffer( - ctx->dev, &shadow_framebuffer_config, nullptr, - &ctx->shadow_framebuf); + ctx->dev, &shadow_framebuffer_config, nullptr, + &ctx->shadow_framebuf); assert(result == VK_SUCCESS); for (unsigned i = 0; i < ctx->swapchain_img_count; ++i) { VkImageView attachments[] = { @@ -878,6 +934,7 @@ void gpu_cleanup(const gpu_ctx_t *ctx) vkDestroyPipeline(ctx->dev, ctx->floor_pipeline, nullptr); vkDestroyPipelineLayout(ctx->dev, ctx->floor_pipeline_layout, nullptr); + vkDestroyPipeline(ctx->dev, ctx->shadow_pipeline, nullptr); vkDestroyPipeline(ctx->dev, ctx->model_pipeline, nullptr); vkDestroyPipelineLayout(ctx->dev, ctx->model_pipeline_layout, nullptr); vkDestroyDescriptorPool(ctx->dev, ctx->desc_pool, nullptr); diff --git a/gpu.h b/gpu.h index a8f9ccd..f738e49 100644 --- a/gpu.h +++ b/gpu.h @@ -37,6 +37,7 @@ typedef struct { VkPipelineLayout floor_pipeline_layout; VkPipeline floor_pipeline; VkPipelineLayout model_pipeline_layout; + VkPipeline shadow_pipeline; VkPipeline model_pipeline; VkCommandPool cmd_pool; VkCommandBuffer cmd_buf;