Skip to content

Commit 7f535c1

Browse files
Merge pull request #152 from KhronosGroup/depth_fix
Fix non-working depth buffer for chapters 35 and 36
2 parents 805e16a + 00d9688 commit 7f535c1

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
@@ -675,7 +675,12 @@ class VulkanApplication {
675675

676676
pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
677677

678-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainSurfaceFormat.format };
678+
vk::Format depthFormat = findDepthFormat();
679+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
680+
.colorAttachmentCount = 1,
681+
.pColorAttachmentFormats = &swapChainSurfaceFormat.format,
682+
.depthAttachmentFormat = depthFormat
683+
};
679684
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
680685
.stageCount = 2,
681686
.pStages = shaderStages,
@@ -1185,6 +1190,31 @@ class VulkanApplication {
11851190
vk::PipelineStageFlagBits2::eTopOfPipe,
11861191
vk::PipelineStageFlagBits2::eColorAttachmentOutput
11871192
);
1193+
vk::ImageMemoryBarrier2 depthBarrier = {
1194+
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
1195+
.srcAccessMask = {},
1196+
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
1197+
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
1198+
.oldLayout = vk::ImageLayout::eUndefined,
1199+
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1200+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1201+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1202+
.image = *depthImage,
1203+
.subresourceRange = {
1204+
.aspectMask = vk::ImageAspectFlagBits::eDepth,
1205+
.baseMipLevel = 0,
1206+
.levelCount = 1,
1207+
.baseArrayLayer = 0,
1208+
.layerCount = 1
1209+
}
1210+
};
1211+
vk::DependencyInfo depthDependencyInfo = {
1212+
.dependencyFlags = {},
1213+
.imageMemoryBarrierCount = 1,
1214+
.pImageMemoryBarriers = &depthBarrier
1215+
};
1216+
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);
1217+
11881218
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
11891219
vk::RenderingAttachmentInfo attachmentInfo = {
11901220
.imageView = *swapChainImageViews[imageIndex],
@@ -1193,11 +1223,20 @@ class VulkanApplication {
11931223
.storeOp = vk::AttachmentStoreOp::eStore,
11941224
.clearValue = clearColor
11951225
};
1226+
vk::ClearValue clearDepth = vk::ClearDepthStencilValue{ 1.0f, 0 };
1227+
vk::RenderingAttachmentInfo depthAttachmentInfo{
1228+
.imageView = *depthImageView,
1229+
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1230+
.loadOp = vk::AttachmentLoadOp::eClear,
1231+
.storeOp = vk::AttachmentStoreOp::eDontCare,
1232+
.clearValue = clearDepth
1233+
};
11961234
vk::RenderingInfo renderingInfo = {
11971235
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
11981236
.layerCount = 1,
11991237
.colorAttachmentCount = 1,
1200-
.pColorAttachments = &attachmentInfo
1238+
.pColorAttachments = &attachmentInfo,
1239+
.pDepthAttachment = &depthAttachmentInfo
12011240
};
12021241
commandBuffers[currentFrame].beginRendering(renderingInfo);
12031242
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
@@ -702,8 +702,8 @@ class VulkanApplication {
702702
.depthClampEnable = vk::False,
703703
.rasterizerDiscardEnable = vk::False,
704704
.polygonMode = vk::PolygonMode::eFill,
705-
.cullMode = vk::CullModeFlagBits::eBack, // Re-enabled culling for better performance
706-
.frontFace = vk::FrontFace::eClockwise, // Keeping Clockwise for glTF
705+
.cullMode = vk::CullModeFlagBits::eBack,
706+
.frontFace = vk::FrontFace::eCounterClockwise,
707707
.depthBiasEnable = vk::False
708708
};
709709
rasterizer.lineWidth = 1.0f;
@@ -738,8 +738,13 @@ class VulkanApplication {
738738
vk::PipelineLayoutCreateInfo pipelineLayoutInfo{ .setLayoutCount = 1, .pSetLayouts = &*descriptorSetLayout, .pushConstantRangeCount = 0 };
739739

740740
pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
741-
742-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainSurfaceFormat.format };
741+
742+
vk::Format depthFormat = findDepthFormat();
743+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
744+
.colorAttachmentCount = 1,
745+
.pColorAttachmentFormats = &swapChainSurfaceFormat.format,
746+
.depthAttachmentFormat = depthFormat
747+
};
743748
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
744749
.stageCount = 2,
745750
.pStages = shaderStages,
@@ -1013,10 +1018,7 @@ class VulkanApplication {
10131018
Vertex vertex{};
10141019

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

10211023
if (hasTexCoords) {
10221024
const float* texCoord = reinterpret_cast<const float*>(&texCoordBuffer->data[texCoordBufferView->byteOffset + texCoordAccessor->byteOffset + i * 8]);
@@ -1099,17 +1101,17 @@ class VulkanApplication {
10991101
void setupGameObjects() {
11001102
// Object 1 - Center
11011103
gameObjects[0].position = {0.0f, 0.0f, 0.0f};
1102-
gameObjects[0].rotation = {0.0f, 0.0f, 0.0f};
1104+
gameObjects[0].rotation = {0.0f, glm::radians(-90.0f), 0.0f};
11031105
gameObjects[0].scale = {1.0f, 1.0f, 1.0f};
11041106

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

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

@@ -1277,6 +1279,31 @@ class VulkanApplication {
12771279
vk::PipelineStageFlagBits2::eTopOfPipe,
12781280
vk::PipelineStageFlagBits2::eColorAttachmentOutput
12791281
);
1282+
vk::ImageMemoryBarrier2 depthBarrier = {
1283+
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
1284+
.srcAccessMask = {},
1285+
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
1286+
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
1287+
.oldLayout = vk::ImageLayout::eUndefined,
1288+
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1289+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1290+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1291+
.image = *depthImage,
1292+
.subresourceRange = {
1293+
.aspectMask = vk::ImageAspectFlagBits::eDepth,
1294+
.baseMipLevel = 0,
1295+
.levelCount = 1,
1296+
.baseArrayLayer = 0,
1297+
.layerCount = 1
1298+
}
1299+
};
1300+
vk::DependencyInfo depthDependencyInfo = {
1301+
.dependencyFlags = {},
1302+
.imageMemoryBarrierCount = 1,
1303+
.pImageMemoryBarriers = &depthBarrier
1304+
};
1305+
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);
1306+
12801307
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
12811308
vk::RenderingAttachmentInfo attachmentInfo = {
12821309
.imageView = *swapChainImageViews[imageIndex],
@@ -1285,11 +1312,20 @@ class VulkanApplication {
12851312
.storeOp = vk::AttachmentStoreOp::eStore,
12861313
.clearValue = clearColor
12871314
};
1315+
vk::ClearValue clearDepth = vk::ClearDepthStencilValue{ 1.0f, 0 };
1316+
vk::RenderingAttachmentInfo depthAttachmentInfo{
1317+
.imageView = *depthImageView,
1318+
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1319+
.loadOp = vk::AttachmentLoadOp::eClear,
1320+
.storeOp = vk::AttachmentStoreOp::eDontCare,
1321+
.clearValue = clearDepth
1322+
};
12881323
vk::RenderingInfo renderingInfo = {
12891324
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
12901325
.layerCount = 1,
12911326
.colorAttachmentCount = 1,
1292-
.pColorAttachments = &attachmentInfo
1327+
.pColorAttachments = &attachmentInfo,
1328+
.pDepthAttachment = &depthAttachmentInfo
12931329
};
12941330
commandBuffers[currentFrame].beginRendering(renderingInfo);
12951331
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
@@ -1389,6 +1425,8 @@ class VulkanApplication {
13891425
// Camera and projection matrices (shared by all objects)
13901426
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));
13911427
glm::mat4 proj = glm::perspective(glm::radians(45.0f), static_cast<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height), 0.1f, 20.0f);
1428+
proj[1][1] *= -1;
1429+
13921430
// Update uniform buffers for each object
13931431
for (auto& gameObject : gameObjects) {
13941432
// Apply continuous rotation to the object based on frame time

0 commit comments

Comments
 (0)