Skip to content

Commit 798d43f

Browse files
committed
Add depth attachment support to rendering pipeline
- Integrate depth attachment format detection into pipeline setup. - Transition depth images to optimal layouts for depth/stencil usage. - Include depth attachment in rendering configuration with appropriate clear values.
1 parent e4a2b5b commit 798d43f

File tree

3 files changed

+138
-9
lines changed

3 files changed

+138
-9
lines changed

attachments/27_depth_buffering.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,12 @@ class HelloTriangleApplication {
516516

517517
pipelineLayout = vk::raii::PipelineLayout( device, pipelineLayoutInfo );
518518

519-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
519+
vk::Format depthFormat = findDepthFormat();
520+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
521+
.colorAttachmentCount = 1,
522+
.pColorAttachmentFormats = &swapChainImageFormat,
523+
.depthAttachmentFormat = depthFormat
524+
};
520525
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
521526
.stageCount = 2,
522527
.pStages = shaderStages,
@@ -889,19 +894,57 @@ class HelloTriangleApplication {
889894
vk::PipelineStageFlagBits2::eTopOfPipe, // srcStage
890895
vk::PipelineStageFlagBits2::eColorAttachmentOutput // dstStage
891896
);
897+
// Transition depth image to depth attachment optimal layout
898+
vk::ImageMemoryBarrier2 depthBarrier = {
899+
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
900+
.srcAccessMask = {},
901+
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
902+
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
903+
.oldLayout = vk::ImageLayout::eUndefined,
904+
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
905+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
906+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
907+
.image = depthImage,
908+
.subresourceRange = {
909+
.aspectMask = vk::ImageAspectFlagBits::eDepth,
910+
.baseMipLevel = 0,
911+
.levelCount = 1,
912+
.baseArrayLayer = 0,
913+
.layerCount = 1
914+
}
915+
};
916+
vk::DependencyInfo depthDependencyInfo = {
917+
.dependencyFlags = {},
918+
.imageMemoryBarrierCount = 1,
919+
.pImageMemoryBarriers = &depthBarrier
920+
};
921+
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);
922+
892923
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
893-
vk::RenderingAttachmentInfo attachmentInfo = {
924+
vk::ClearValue clearDepth = vk::ClearDepthStencilValue(1.0f, 0);
925+
926+
vk::RenderingAttachmentInfo colorAttachmentInfo = {
894927
.imageView = swapChainImageViews[imageIndex],
895928
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
896929
.loadOp = vk::AttachmentLoadOp::eClear,
897930
.storeOp = vk::AttachmentStoreOp::eStore,
898931
.clearValue = clearColor
899932
};
933+
934+
vk::RenderingAttachmentInfo depthAttachmentInfo = {
935+
.imageView = depthImageView,
936+
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
937+
.loadOp = vk::AttachmentLoadOp::eClear,
938+
.storeOp = vk::AttachmentStoreOp::eDontCare,
939+
.clearValue = clearDepth
940+
};
941+
900942
vk::RenderingInfo renderingInfo = {
901943
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
902944
.layerCount = 1,
903945
.colorAttachmentCount = 1,
904-
.pColorAttachments = &attachmentInfo
946+
.pColorAttachments = &colorAttachmentInfo,
947+
.pDepthAttachment = &depthAttachmentInfo
905948
};
906949
commandBuffers[currentFrame].beginRendering(renderingInfo);
907950
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);

attachments/28_model_loading.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,12 @@ class HelloTriangleApplication {
520520

521521
pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
522522

523-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
523+
vk::Format depthFormat = findDepthFormat();
524+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
525+
.colorAttachmentCount = 1,
526+
.pColorAttachmentFormats = &swapChainImageFormat,
527+
.depthAttachmentFormat = depthFormat
528+
};
524529
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
525530
.stageCount = 2,
526531
.pStages = shaderStages,
@@ -934,19 +939,57 @@ class HelloTriangleApplication {
934939
vk::PipelineStageFlagBits2::eTopOfPipe, // srcStage
935940
vk::PipelineStageFlagBits2::eColorAttachmentOutput // dstStage
936941
);
942+
// Transition depth image to depth attachment optimal layout
943+
vk::ImageMemoryBarrier2 depthBarrier = {
944+
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
945+
.srcAccessMask = {},
946+
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
947+
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
948+
.oldLayout = vk::ImageLayout::eUndefined,
949+
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
950+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
951+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
952+
.image = depthImage,
953+
.subresourceRange = {
954+
.aspectMask = vk::ImageAspectFlagBits::eDepth,
955+
.baseMipLevel = 0,
956+
.levelCount = 1,
957+
.baseArrayLayer = 0,
958+
.layerCount = 1
959+
}
960+
};
961+
vk::DependencyInfo depthDependencyInfo = {
962+
.dependencyFlags = {},
963+
.imageMemoryBarrierCount = 1,
964+
.pImageMemoryBarriers = &depthBarrier
965+
};
966+
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);
967+
937968
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
938-
vk::RenderingAttachmentInfo attachmentInfo = {
969+
vk::ClearValue clearDepth = vk::ClearDepthStencilValue(1.0f, 0);
970+
971+
vk::RenderingAttachmentInfo colorAttachmentInfo = {
939972
.imageView = swapChainImageViews[imageIndex],
940973
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
941974
.loadOp = vk::AttachmentLoadOp::eClear,
942975
.storeOp = vk::AttachmentStoreOp::eStore,
943976
.clearValue = clearColor
944977
};
978+
979+
vk::RenderingAttachmentInfo depthAttachmentInfo = {
980+
.imageView = depthImageView,
981+
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
982+
.loadOp = vk::AttachmentLoadOp::eClear,
983+
.storeOp = vk::AttachmentStoreOp::eDontCare,
984+
.clearValue = clearDepth
985+
};
986+
945987
vk::RenderingInfo renderingInfo = {
946988
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
947989
.layerCount = 1,
948990
.colorAttachmentCount = 1,
949-
.pColorAttachments = &attachmentInfo
991+
.pColorAttachments = &colorAttachmentInfo,
992+
.pDepthAttachment = &depthAttachmentInfo
950993
};
951994
commandBuffers[currentFrame].beginRendering(renderingInfo);
952995
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);

attachments/29_mipmapping.cpp

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,12 @@ class HelloTriangleApplication {
520520

521521
pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
522522

523-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
523+
vk::Format depthFormat = findDepthFormat();
524+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
525+
.colorAttachmentCount = 1,
526+
.pColorAttachmentFormats = &swapChainImageFormat,
527+
.depthAttachmentFormat = depthFormat
528+
};
524529
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
525530
.stageCount = 2,
526531
.pStages = shaderStages,
@@ -1000,19 +1005,57 @@ class HelloTriangleApplication {
10001005
vk::PipelineStageFlagBits2::eTopOfPipe, // srcStage
10011006
vk::PipelineStageFlagBits2::eColorAttachmentOutput // dstStage
10021007
);
1008+
// Transition depth image to depth attachment optimal layout
1009+
vk::ImageMemoryBarrier2 depthBarrier = {
1010+
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
1011+
.srcAccessMask = {},
1012+
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
1013+
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
1014+
.oldLayout = vk::ImageLayout::eUndefined,
1015+
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1016+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1017+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1018+
.image = depthImage,
1019+
.subresourceRange = {
1020+
.aspectMask = vk::ImageAspectFlagBits::eDepth,
1021+
.baseMipLevel = 0,
1022+
.levelCount = 1,
1023+
.baseArrayLayer = 0,
1024+
.layerCount = 1
1025+
}
1026+
};
1027+
vk::DependencyInfo depthDependencyInfo = {
1028+
.dependencyFlags = {},
1029+
.imageMemoryBarrierCount = 1,
1030+
.pImageMemoryBarriers = &depthBarrier
1031+
};
1032+
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);
1033+
10031034
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
1004-
vk::RenderingAttachmentInfo attachmentInfo = {
1035+
vk::ClearValue clearDepth = vk::ClearDepthStencilValue(1.0f, 0);
1036+
1037+
vk::RenderingAttachmentInfo colorAttachmentInfo = {
10051038
.imageView = swapChainImageViews[imageIndex],
10061039
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
10071040
.loadOp = vk::AttachmentLoadOp::eClear,
10081041
.storeOp = vk::AttachmentStoreOp::eStore,
10091042
.clearValue = clearColor
10101043
};
1044+
1045+
vk::RenderingAttachmentInfo depthAttachmentInfo = {
1046+
.imageView = depthImageView,
1047+
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
1048+
.loadOp = vk::AttachmentLoadOp::eClear,
1049+
.storeOp = vk::AttachmentStoreOp::eDontCare,
1050+
.clearValue = clearDepth
1051+
};
1052+
10111053
vk::RenderingInfo renderingInfo = {
10121054
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
10131055
.layerCount = 1,
10141056
.colorAttachmentCount = 1,
1015-
.pColorAttachments = &attachmentInfo
1057+
.pColorAttachments = &colorAttachmentInfo,
1058+
.pDepthAttachment = &depthAttachmentInfo
10161059
};
10171060
commandBuffers[currentFrame].beginRendering(renderingInfo);
10181061
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);

0 commit comments

Comments
 (0)