Skip to content

Commit 1efe05b

Browse files
committed
Depth Buffer
1 parent 5fb65d9 commit 1efe05b

6 files changed

Lines changed: 59 additions & 17 deletions

File tree

shaders/model_vert.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ layout(location = 0) out vec3 out_normal;
1111

1212
void main() {
1313
mat4 modelViewProj = u_pushConstants.modelViewProj;
14-
gl_Position = modelViewProj * vec4(in_position.x, in_position.y, in_position.z, 1.0);
14+
gl_Position = modelViewProj * vec4(in_position, 1.0);
1515
out_normal = in_normal;
1616
}

shaders/model_vert.spv

-80 Bytes
Binary file not shown.

src/main.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ VulkanContext* context;
1919
VkSurfaceKHR surface;
2020
VulkanSwapchain swapchain;
2121
VkRenderPass renderPass;
22+
std::vector<VulkanImage> depthBuffers;
2223
std::vector<VkFramebuffer> framebuffers;
2324
VulkanPipeline spritePipeline;
2425
VulkanPipeline modelPipeline;
@@ -54,17 +55,27 @@ void recreateRenderPass() {
5455
for (uint32_t i = 0; i < framebuffers.size(); ++i) {
5556
VK(vkDestroyFramebuffer(context->device, framebuffers[i], 0));
5657
}
58+
for(uint32_t i = 0; i < depthBuffers.size(); ++i) {
59+
destroyImage(context, &depthBuffers[i]);
60+
}
5761
destroyRenderpass(context, renderPass);
5862
}
5963
framebuffers.clear();
64+
depthBuffers.clear();
6065

6166
renderPass = createRenderPass(context, swapchain.format);
6267
framebuffers.resize(swapchain.images.size());
68+
depthBuffers.resize(swapchain.images.size());
6369
for (uint32_t i = 0; i < swapchain.images.size(); ++i) {
70+
createImage(context, &depthBuffers.data()[i], swapchain.width, swapchain.height, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
71+
VkImageView attachments[] = {
72+
swapchain.imageViews[i],
73+
depthBuffers[i].view,
74+
};
6475
VkFramebufferCreateInfo createInfo = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO };
6576
createInfo.renderPass = renderPass;
66-
createInfo.attachmentCount = 1;
67-
createInfo.pAttachments = &swapchain.imageViews[i];
77+
createInfo.attachmentCount = ARRAY_COUNT(attachments);
78+
createInfo.pAttachments = attachments;
6879
createInfo.width = swapchain.width;
6980
createInfo.height = swapchain.height;
7081
createInfo.layers = 1;
@@ -313,18 +324,21 @@ void renderApplication() {
313324
VkCommandBuffer commandBuffer = commandBuffers[frameIndex];
314325
VKA(vkBeginCommandBuffer(commandBuffer, &beginInfo));
315326

316-
VkViewport viewport = { 0.0f, 0.0f, (float)swapchain.width, (float)swapchain.height };
327+
VkViewport viewport = { 0.0f, 0.0f, (float)swapchain.width, (float)swapchain.height, 0.0f, 1.0f};
317328
VkRect2D scissor = { {0, 0}, {swapchain.width, swapchain.height} };
318329
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
319330
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
320331

321-
VkClearValue clearValue = {0.0f, greenChannel, 1.0f, 1.0f};
332+
VkClearValue clearValues[2] = {
333+
{0.0f, greenChannel, 1.0f, 1.0f},
334+
{0.0f, 0.0f},
335+
};
322336
VkRenderPassBeginInfo beginInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO };
323337
beginInfo.renderPass = renderPass;
324338
beginInfo.framebuffer = framebuffers[imageIndex];
325339
beginInfo.renderArea = { {0, 0}, {swapchain.width, swapchain.height} };
326-
beginInfo.clearValueCount = 1;
327-
beginInfo.pClearValues = &clearValue;
340+
beginInfo.clearValueCount = ARRAY_COUNT(clearValues);
341+
beginInfo.pClearValues = clearValues;
328342
vkCmdBeginRenderPass(commandBuffer, &beginInfo, VK_SUBPASS_CONTENTS_INLINE);
329343

330344
#if 0
@@ -415,6 +429,10 @@ void shutdownApplication() {
415429
VK(vkDestroyFramebuffer(context->device, framebuffers[i], 0));
416430
}
417431
framebuffers.clear();
432+
for(uint32_t i = 0; i < depthBuffers.size(); ++i) {
433+
destroyImage(context, &depthBuffers[i]);
434+
}
435+
depthBuffers.clear();
418436
destroyRenderpass(context, renderPass);
419437
destroySwapchain(context, &swapchain);
420438
VK(vkDestroySurfaceKHR(context->instance, surface, 0));

src/vulkan_base/vulkan_pipeline.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFi
6565
VkPipelineMultisampleStateCreateInfo multisampleState = { VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO };
6666
multisampleState.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
6767

68+
VkPipelineDepthStencilStateCreateInfo depthStencilState = {VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO};
69+
depthStencilState.depthTestEnable = VK_TRUE;
70+
depthStencilState.depthWriteEnable = VK_TRUE;
71+
depthStencilState.depthCompareOp = VK_COMPARE_OP_GREATER;
72+
depthStencilState.minDepthBounds = 0.0f;
73+
depthStencilState.maxDepthBounds = 1.0f;
74+
6875
VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
6976
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
7077
colorBlendAttachment.blendEnable = VK_TRUE;
@@ -103,6 +110,7 @@ VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFi
103110
createInfo.pViewportState = &viewportState;
104111
createInfo.pRasterizationState = &rasterizationState;
105112
createInfo.pMultisampleState = &multisampleState;
113+
createInfo.pDepthStencilState = &depthStencilState;
106114
createInfo.pColorBlendState = &colorBlendState;
107115
createInfo.pDynamicState = &dynamicState;
108116
createInfo.layout = pipelineLayout;

src/vulkan_base/vulkan_renderpass.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@
33
VkRenderPass createRenderPass(VulkanContext* context, VkFormat format) {
44
VkRenderPass renderPass;
55

6-
VkAttachmentDescription attachmentDescription = {};
7-
attachmentDescription.format = format;
8-
attachmentDescription.samples = VK_SAMPLE_COUNT_1_BIT;
9-
attachmentDescription.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
10-
attachmentDescription.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
11-
attachmentDescription.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
12-
attachmentDescription.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
6+
VkAttachmentDescription attachmentDescriptions[2];
7+
attachmentDescriptions[0] = {};
8+
attachmentDescriptions[0].format = format;
9+
attachmentDescriptions[0].samples = VK_SAMPLE_COUNT_1_BIT;
10+
attachmentDescriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
11+
attachmentDescriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
12+
attachmentDescriptions[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
13+
attachmentDescriptions[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
14+
attachmentDescriptions[1] = {};
15+
attachmentDescriptions[1].format = VK_FORMAT_D32_SFLOAT;
16+
attachmentDescriptions[1].samples = VK_SAMPLE_COUNT_1_BIT;
17+
attachmentDescriptions[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
18+
attachmentDescriptions[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
19+
attachmentDescriptions[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
20+
attachmentDescriptions[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
21+
1322
VkAttachmentReference attachmentReference = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL };
23+
VkAttachmentReference depthStencilReference = {1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL};
1424

1525
VkSubpassDescription subpass = {};
1626
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
1727
subpass.colorAttachmentCount = 1;
1828
subpass.pColorAttachments = &attachmentReference;
29+
subpass.pDepthStencilAttachment = &depthStencilReference;
1930

2031
VkRenderPassCreateInfo createInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO };
21-
createInfo.attachmentCount = 1;
22-
createInfo.pAttachments = &attachmentDescription;
32+
createInfo.attachmentCount = ARRAY_COUNT(attachmentDescriptions);
33+
createInfo.pAttachments = attachmentDescriptions;
2334
createInfo.subpassCount = 1;
2435
createInfo.pSubpasses = &subpass;
2536
VKA(vkCreateRenderPass(context->device, &createInfo, 0, &renderPass));

src/vulkan_base/vulkan_utils.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,17 @@ void createImage(VulkanContext* context, VulkanImage* image, uint32_t width, uin
125125
VKA(vkAllocateMemory(context->device, &allocateInfo, 0, &image->memory));
126126
VKA(vkBindImageMemory(context->device, image->image, image->memory, 0));
127127

128+
VkImageAspectFlags aspect = VK_IMAGE_ASPECT_COLOR_BIT;
129+
if(format == VK_FORMAT_D32_SFLOAT) {
130+
aspect = VK_IMAGE_ASPECT_DEPTH_BIT;
131+
}
132+
128133
{
129134
VkImageViewCreateInfo createInfo = {VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
130135
createInfo.image = image->image;
131136
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
132137
createInfo.format = format;
133-
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
138+
createInfo.subresourceRange.aspectMask = aspect;
134139
createInfo.subresourceRange.levelCount = 1;
135140
createInfo.subresourceRange.layerCount = 1;
136141
VKA(vkCreateImageView(context->device, &createInfo, 0, &image->view));

0 commit comments

Comments
 (0)