Skip to content

Commit c8b83b6

Browse files
committed
Fix non-working depth buffer
1 parent afa478c commit c8b83b6

File tree

2 files changed

+91
-14
lines changed

2 files changed

+91
-14
lines changed

attachments/35_gltf_ktx.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,12 @@ class VulkanApplication {
673673

674674
pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
675675

676-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainSurfaceFormat.format };
676+
vk::Format depthFormat = findDepthFormat();
677+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
678+
.colorAttachmentCount = 1,
679+
.pColorAttachmentFormats = &swapChainSurfaceFormat.format,
680+
.depthAttachmentFormat = depthFormat
681+
};
677682
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
678683
.stageCount = 2,
679684
.pStages = shaderStages,
@@ -1183,6 +1188,31 @@ class VulkanApplication {
11831188
vk::PipelineStageFlagBits2::eTopOfPipe,
11841189
vk::PipelineStageFlagBits2::eColorAttachmentOutput
11851190
);
1191+
vk::ImageMemoryBarrier2 depthBarrier = {
1192+
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
1193+
.srcAccessMask = {},
1194+
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
1195+
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
1196+
.oldLayout = vk::ImageLayout::eUndefined,
1197+
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1198+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1199+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1200+
.image = depthImage,
1201+
.subresourceRange = {
1202+
.aspectMask = vk::ImageAspectFlagBits::eDepth,
1203+
.baseMipLevel = 0,
1204+
.levelCount = 1,
1205+
.baseArrayLayer = 0,
1206+
.layerCount = 1
1207+
}
1208+
};
1209+
vk::DependencyInfo depthDependencyInfo = {
1210+
.dependencyFlags = {},
1211+
.imageMemoryBarrierCount = 1,
1212+
.pImageMemoryBarriers = &depthBarrier
1213+
};
1214+
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);
1215+
11861216
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
11871217
vk::RenderingAttachmentInfo attachmentInfo = {
11881218
.imageView = *swapChainImageViews[imageIndex],
@@ -1191,11 +1221,20 @@ class VulkanApplication {
11911221
.storeOp = vk::AttachmentStoreOp::eStore,
11921222
.clearValue = clearColor
11931223
};
1224+
vk::ClearValue clearDepth = vk::ClearDepthStencilValue{ 1.0f, 0 };
1225+
vk::RenderingAttachmentInfo depthAttachmentInfo{
1226+
.imageView = *depthImageView,
1227+
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1228+
.loadOp = vk::AttachmentLoadOp::eClear,
1229+
.storeOp = vk::AttachmentStoreOp::eDontCare,
1230+
.clearValue = clearDepth
1231+
};
11941232
vk::RenderingInfo renderingInfo = {
11951233
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
11961234
.layerCount = 1,
11971235
.colorAttachmentCount = 1,
1198-
.pColorAttachments = &attachmentInfo
1236+
.pColorAttachments = &attachmentInfo,
1237+
.pDepthAttachment = &depthAttachmentInfo
11991238
};
12001239
commandBuffers[currentFrame].beginRendering(renderingInfo);
12011240
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);

attachments/36_multiple_objects.cpp

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,8 @@ class VulkanApplication {
700700
.depthClampEnable = vk::False,
701701
.rasterizerDiscardEnable = vk::False,
702702
.polygonMode = vk::PolygonMode::eFill,
703-
.cullMode = vk::CullModeFlagBits::eBack, // Re-enabled culling for better performance
704-
.frontFace = vk::FrontFace::eClockwise, // Keeping Clockwise for glTF
703+
.cullMode = vk::CullModeFlagBits::eBack,
704+
.frontFace = vk::FrontFace::eCounterClockwise,
705705
.depthBiasEnable = vk::False
706706
};
707707
rasterizer.lineWidth = 1.0f;
@@ -736,8 +736,13 @@ class VulkanApplication {
736736
vk::PipelineLayoutCreateInfo pipelineLayoutInfo{ .setLayoutCount = 1, .pSetLayouts = &*descriptorSetLayout, .pushConstantRangeCount = 0 };
737737

738738
pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
739-
740-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainSurfaceFormat.format };
739+
740+
vk::Format depthFormat = findDepthFormat();
741+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
742+
.colorAttachmentCount = 1,
743+
.pColorAttachmentFormats = &swapChainSurfaceFormat.format,
744+
.depthAttachmentFormat = depthFormat
745+
};
741746
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
742747
.stageCount = 2,
743748
.pStages = shaderStages,
@@ -1011,10 +1016,7 @@ class VulkanApplication {
10111016
Vertex vertex{};
10121017

10131018
const float* pos = reinterpret_cast<const float*>(&posBuffer.data[posBufferView.byteOffset + posAccessor.byteOffset + i * 12]);
1014-
// glTF uses a right-handed coordinate system with Y-up
1015-
// Vulkan uses a right-handed coordinate system with Y-down
1016-
// We need to flip the Y coordinate
1017-
vertex.pos = {pos[0], -pos[1], pos[2]};
1019+
vertex.pos = {pos[0], pos[1], pos[2]};
10181020

10191021
if (hasTexCoords) {
10201022
const float* texCoord = reinterpret_cast<const float*>(&texCoordBuffer->data[texCoordBufferView->byteOffset + texCoordAccessor->byteOffset + i * 8]);
@@ -1097,17 +1099,17 @@ class VulkanApplication {
10971099
void setupGameObjects() {
10981100
// Object 1 - Center
10991101
gameObjects[0].position = {0.0f, 0.0f, 0.0f};
1100-
gameObjects[0].rotation = {0.0f, 0.0f, 0.0f};
1102+
gameObjects[0].rotation = {0.0f, glm::radians(-90.0f), 0.0f};
11011103
gameObjects[0].scale = {1.0f, 1.0f, 1.0f};
11021104

11031105
// Object 2 - Left
11041106
gameObjects[1].position = {-2.0f, 0.0f, -1.0f};
1105-
gameObjects[1].rotation = {0.0f, glm::radians(45.0f), 0.0f};
1107+
gameObjects[1].rotation = {0.0f, glm::radians(-45.0f), 0.0f};
11061108
gameObjects[1].scale = {0.75f, 0.75f, 0.75f};
11071109

11081110
// Object 3 - Right
11091111
gameObjects[2].position = {2.0f, 0.0f, -1.0f};
1110-
gameObjects[2].rotation = {0.0f, glm::radians(-45.0f), 0.0f};
1112+
gameObjects[2].rotation = {0.0f, glm::radians(45.0f), 0.0f};
11111113
gameObjects[2].scale = {0.75f, 0.75f, 0.75f};
11121114
}
11131115

@@ -1275,6 +1277,31 @@ class VulkanApplication {
12751277
vk::PipelineStageFlagBits2::eTopOfPipe,
12761278
vk::PipelineStageFlagBits2::eColorAttachmentOutput
12771279
);
1280+
vk::ImageMemoryBarrier2 depthBarrier = {
1281+
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
1282+
.srcAccessMask = {},
1283+
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
1284+
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
1285+
.oldLayout = vk::ImageLayout::eUndefined,
1286+
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1287+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1288+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1289+
.image = depthImage,
1290+
.subresourceRange = {
1291+
.aspectMask = vk::ImageAspectFlagBits::eDepth,
1292+
.baseMipLevel = 0,
1293+
.levelCount = 1,
1294+
.baseArrayLayer = 0,
1295+
.layerCount = 1
1296+
}
1297+
};
1298+
vk::DependencyInfo depthDependencyInfo = {
1299+
.dependencyFlags = {},
1300+
.imageMemoryBarrierCount = 1,
1301+
.pImageMemoryBarriers = &depthBarrier
1302+
};
1303+
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);
1304+
12781305
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
12791306
vk::RenderingAttachmentInfo attachmentInfo = {
12801307
.imageView = *swapChainImageViews[imageIndex],
@@ -1283,11 +1310,20 @@ class VulkanApplication {
12831310
.storeOp = vk::AttachmentStoreOp::eStore,
12841311
.clearValue = clearColor
12851312
};
1313+
vk::ClearValue clearDepth = vk::ClearDepthStencilValue{ 1.0f, 0 };
1314+
vk::RenderingAttachmentInfo depthAttachmentInfo{
1315+
.imageView = *depthImageView,
1316+
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1317+
.loadOp = vk::AttachmentLoadOp::eClear,
1318+
.storeOp = vk::AttachmentStoreOp::eDontCare,
1319+
.clearValue = clearDepth
1320+
};
12861321
vk::RenderingInfo renderingInfo = {
12871322
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
12881323
.layerCount = 1,
12891324
.colorAttachmentCount = 1,
1290-
.pColorAttachments = &attachmentInfo
1325+
.pColorAttachments = &attachmentInfo,
1326+
.pDepthAttachment = &depthAttachmentInfo
12911327
};
12921328
commandBuffers[currentFrame].beginRendering(renderingInfo);
12931329
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
@@ -1387,6 +1423,8 @@ class VulkanApplication {
13871423
// Camera and projection matrices (shared by all objects)
13881424
glm::mat4 view = glm::lookAt(glm::vec3(2.0f, 2.0f, 6.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
13891425
glm::mat4 proj = glm::perspective(glm::radians(45.0f), static_cast<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height), 0.1f, 20.0f);
1426+
proj[1][1] *= -1;
1427+
13901428
// Update uniform buffers for each object
13911429
for (auto& gameObject : gameObjects) {
13921430
// Apply continuous rotation to the object based on frame time

0 commit comments

Comments
 (0)