Add depth buffering

This commit is contained in:
Piotr Krygier committed 2023-10-03 13:22:24 +02:00
1 parent 548ed57969
commit d328e38761
9 files changed
+323 -39

No files matched your search

+68 -25
View File
@@ -220,10 +220,26 @@ uint8_t create_swapchain_image_views()
*/
uint8_t create_render_pass()
{
VkAttachmentDescription attachments[2] = {};
VkAttachmentDescription color_attachment = {};
VkAttachmentReference color_attachment_ref = {};
VkSubpassDescription subpass = {};
VkRenderPassCreateInfo renderpass_info = {};
VkAttachmentDescription depth_attachment = {};
VkAttachmentReference depth_attachment_ref = {};
VkSubpassDependency dependency = {};
depth_attachment.format = find_depth_format();
depth_attachment.samples = VK_SAMPLE_COUNT_1_BIT;
depth_attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depth_attachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depth_attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depth_attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depth_attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depth_attachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
depth_attachment_ref.attachment = 1;
depth_attachment_ref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
color_attachment.flags = 0U;
color_attachment.format = IMAGE_FORMAT;
@@ -241,12 +257,26 @@ uint8_t create_render_pass()
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &color_attachment_ref;
subpass.pDepthStencilAttachment = &depth_attachment_ref;
attachments[0] = color_attachment;
attachments[1] = depth_attachment;
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
renderpass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderpass_info.attachmentCount = 1;
renderpass_info.pAttachments = &color_attachment;
renderpass_info.attachmentCount = 2;
renderpass_info.pAttachments = attachments;
renderpass_info.subpassCount = 1;
renderpass_info.pSubpasses = &subpass;
renderpass_info.dependencyCount = 1;
renderpass_info.pDependencies = &dependency;
if (VK_SUCCESS != vkCreateRenderPass(g_vulkan_state.device, &renderpass_info, NULL, &g_vulkan_state.render_pass)) {
LOGF(vulkan_error_messages[VULKAN_RENDERPASS_CREATION_FAILED]);
@@ -340,6 +370,7 @@ uint8_t create_graphics_pipeline()
VkPipelineMultisampleStateCreateInfo multisampling = {};
VkPipelineColorBlendStateCreateInfo color_blending = {};
VkPipelineDynamicStateCreateInfo dynamic_state = {};
VkPipelineDepthStencilStateCreateInfo depth_stencil = {};
STATUS_CHECK(fill_shader_stages(shader_stages));
fill_vertex_input_info(&vertex_input_info);
@@ -350,29 +381,41 @@ uint8_t create_graphics_pipeline()
fill_color_blend_state_info(&color_blending);
fill_dynamic_pipeline_state_info(&dynamic_state);
VkGraphicsPipelineCreateInfo pipelineInfo = {};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.pNext = NULL;
pipelineInfo.flags = 0U;
pipelineInfo.stageCount = SHADER_MODULES_COUNT;
pipelineInfo.pStages = shader_stages;
pipelineInfo.pVertexInputState = &vertex_input_info;
pipelineInfo.pInputAssemblyState = &input_assembly;
pipelineInfo.pTessellationState = NULL;
pipelineInfo.pViewportState = &viewport_state;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
pipelineInfo.pDepthStencilState = NULL;
pipelineInfo.pColorBlendState = &color_blending;
pipelineInfo.pDynamicState = &dynamic_state;
pipelineInfo.layout = g_vulkan_state.pipeline_layout;
pipelineInfo.renderPass = g_vulkan_state.render_pass;
pipelineInfo.subpass = 0;
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
pipelineInfo.basePipelineIndex = -1;
//TODO: Move to another function
depth_stencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
depth_stencil.depthTestEnable = VK_TRUE;
depth_stencil.depthWriteEnable = VK_TRUE;
depth_stencil.depthCompareOp = VK_COMPARE_OP_LESS;
depth_stencil.depthBoundsTestEnable = VK_FALSE;
depth_stencil.minDepthBounds = 0.0f; // Optional
depth_stencil.maxDepthBounds = 1.0f; // Optional
depth_stencil.stencilTestEnable = VK_FALSE;
// depth_stencil.front = {0}; // Optional
// depth_stencil.back = {0}; // Optional
VkGraphicsPipelineCreateInfo pipeline_info = {};
pipeline_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipeline_info.pNext = NULL;
pipeline_info.flags = 0U;
pipeline_info.stageCount = SHADER_MODULES_COUNT;
pipeline_info.pStages = shader_stages;
pipeline_info.pVertexInputState = &vertex_input_info;
pipeline_info.pInputAssemblyState = &input_assembly;
pipeline_info.pTessellationState = NULL;
pipeline_info.pViewportState = &viewport_state;
pipeline_info.pRasterizationState = &rasterizer;
pipeline_info.pMultisampleState = &multisampling;
pipeline_info.pDepthStencilState = &depth_stencil;
pipeline_info.pColorBlendState = &color_blending;
pipeline_info.pDynamicState = &dynamic_state;
pipeline_info.layout = g_vulkan_state.pipeline_layout;
pipeline_info.renderPass = g_vulkan_state.render_pass;
pipeline_info.subpass = 0;
pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
pipeline_info.basePipelineIndex = -1;
if (VK_SUCCESS !=
vkCreateGraphicsPipelines(g_vulkan_state.device, VK_NULL_HANDLE, 1, &pipelineInfo, NULL, &g_vulkan_state.graphics_pipeline)) {
vkCreateGraphicsPipelines(g_vulkan_state.device, VK_NULL_HANDLE, 1, &pipeline_info, NULL, &g_vulkan_state.graphics_pipeline)) {
LOGF(vulkan_error_messages[VULKAN_GRAPHICSPIPELINE_CREATION_FAILED]);
status = VULKAN_ERROR_GRAPHICSPIPELINE_CREATION_FAILED;
}
@@ -395,12 +438,12 @@ uint8_t create_framebuffers()
g_vulkan_state.swapchain_framebuffers = malloc(sizeof(VkFramebuffer) * g_swapchain_images_count);
for (size_t i = 0; i < g_swapchain_images_count; i++) {
VkImageView attachments[] = {g_vulkan_state.swapchain_image_views[i]};
VkImageView attachments[] = {g_vulkan_state.swapchain_image_views[i], rse_get_depth_image_view()};
VkFramebufferCreateInfo framebufferInfo = {};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = g_vulkan_state.render_pass;
framebufferInfo.attachmentCount = 1;
framebufferInfo.attachmentCount = 2;
framebufferInfo.pAttachments = attachments;
framebufferInfo.width = g_vulkan_state.swapchain_extent.width;
framebufferInfo.height = g_vulkan_state.swapchain_extent.height;