Skip to content

Commit 8907b64

Browse files
authored
Merge pull request #86 from gpx1000/multisampling-issue-72
Enable multisampling and improve depth handling in rendering pipeline
2 parents 07d4d97 + 798d43f commit 8907b64

File tree

4 files changed

+230
-15
lines changed

4 files changed

+230
-15
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);

attachments/30_multisampling.cpp

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class HelloTriangleApplication {
181181
setupDebugMessenger();
182182
createSurface();
183183
pickPhysicalDevice();
184+
msaaSamples = getMaxUsableSampleCount();
184185
createLogicalDevice();
185186
createSwapChain();
186187
createImageViews();
@@ -528,7 +529,12 @@ class HelloTriangleApplication {
528529

529530
pipelineLayout = vk::raii::PipelineLayout(device, pipelineLayoutInfo);
530531

531-
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
532+
vk::Format depthFormat = findDepthFormat();
533+
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
534+
.colorAttachmentCount = 1,
535+
.pColorAttachmentFormats = &swapChainImageFormat,
536+
.depthAttachmentFormat = depthFormat
537+
};
532538
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
533539
.stageCount = 2,
534540
.pStages = shaderStages,
@@ -1019,7 +1025,8 @@ class HelloTriangleApplication {
10191025

10201026
void recordCommandBuffer(uint32_t imageIndex) {
10211027
commandBuffers[currentFrame].begin({});
1022-
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
1028+
// Before starting rendering, transition the images to the appropriate layouts
1029+
// Transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
10231030
transition_image_layout(
10241031
imageIndex,
10251032
vk::ImageLayout::eUndefined,
@@ -1029,19 +1036,60 @@ class HelloTriangleApplication {
10291036
vk::PipelineStageFlagBits2::eTopOfPipe, // srcStage
10301037
vk::PipelineStageFlagBits2::eColorAttachmentOutput // dstStage
10311038
);
1039+
1040+
// Transition the multisampled color image to COLOR_ATTACHMENT_OPTIMAL
1041+
transition_image_layout_custom(
1042+
colorImage,
1043+
vk::ImageLayout::eUndefined,
1044+
vk::ImageLayout::eColorAttachmentOptimal,
1045+
{},
1046+
vk::AccessFlagBits2::eColorAttachmentWrite,
1047+
vk::PipelineStageFlagBits2::eTopOfPipe,
1048+
vk::PipelineStageFlagBits2::eColorAttachmentOutput,
1049+
vk::ImageAspectFlagBits::eColor
1050+
);
1051+
1052+
// Transition the depth image to DEPTH_ATTACHMENT_OPTIMAL
1053+
transition_image_layout_custom(
1054+
depthImage,
1055+
vk::ImageLayout::eUndefined,
1056+
vk::ImageLayout::eDepthAttachmentOptimal,
1057+
{},
1058+
vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
1059+
vk::PipelineStageFlagBits2::eTopOfPipe,
1060+
vk::PipelineStageFlagBits2::eEarlyFragmentTests,
1061+
vk::ImageAspectFlagBits::eDepth
1062+
);
10321063
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
1033-
vk::RenderingAttachmentInfo attachmentInfo = {
1034-
.imageView = swapChainImageViews[imageIndex],
1064+
vk::ClearValue clearDepth = vk::ClearDepthStencilValue(1.0f, 0);
1065+
1066+
// Color attachment (multisampled) with resolve attachment
1067+
vk::RenderingAttachmentInfo colorAttachment = {
1068+
.imageView = colorImageView,
10351069
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
1070+
.resolveMode = vk::ResolveModeFlagBits::eAverage,
1071+
.resolveImageView = swapChainImageViews[imageIndex],
1072+
.resolveImageLayout = vk::ImageLayout::eColorAttachmentOptimal,
10361073
.loadOp = vk::AttachmentLoadOp::eClear,
10371074
.storeOp = vk::AttachmentStoreOp::eStore,
10381075
.clearValue = clearColor
10391076
};
1077+
1078+
// Depth attachment
1079+
vk::RenderingAttachmentInfo depthAttachment = {
1080+
.imageView = depthImageView,
1081+
.imageLayout = vk::ImageLayout::eDepthAttachmentOptimal,
1082+
.loadOp = vk::AttachmentLoadOp::eClear,
1083+
.storeOp = vk::AttachmentStoreOp::eDontCare,
1084+
.clearValue = clearDepth
1085+
};
1086+
10401087
vk::RenderingInfo renderingInfo = {
10411088
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
10421089
.layerCount = 1,
10431090
.colorAttachmentCount = 1,
1044-
.pColorAttachments = &attachmentInfo
1091+
.pColorAttachments = &colorAttachment,
1092+
.pDepthAttachment = &depthAttachment
10451093
};
10461094
commandBuffers[currentFrame].beginRendering(renderingInfo);
10471095
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
@@ -1052,7 +1100,9 @@ class HelloTriangleApplication {
10521100
commandBuffers[currentFrame].bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, *descriptorSets[currentFrame], nullptr);
10531101
commandBuffers[currentFrame].drawIndexed(indices.size(), 1, 0, 0, 0);
10541102
commandBuffers[currentFrame].endRendering();
1055-
// After rendering, transition the swapchain image to PRESENT_SRC
1103+
// After rendering, transition the images to appropriate layouts
1104+
1105+
// Transition the swapchain image to PRESENT_SRC
10561106
transition_image_layout(
10571107
imageIndex,
10581108
vk::ImageLayout::eColorAttachmentOptimal,
@@ -1100,6 +1150,42 @@ class HelloTriangleApplication {
11001150
commandBuffers[currentFrame].pipelineBarrier2(dependency_info);
11011151
}
11021152

1153+
void transition_image_layout_custom(
1154+
vk::raii::Image& image,
1155+
vk::ImageLayout old_layout,
1156+
vk::ImageLayout new_layout,
1157+
vk::AccessFlags2 src_access_mask,
1158+
vk::AccessFlags2 dst_access_mask,
1159+
vk::PipelineStageFlags2 src_stage_mask,
1160+
vk::PipelineStageFlags2 dst_stage_mask,
1161+
vk::ImageAspectFlags aspect_mask
1162+
) {
1163+
vk::ImageMemoryBarrier2 barrier = {
1164+
.srcStageMask = src_stage_mask,
1165+
.srcAccessMask = src_access_mask,
1166+
.dstStageMask = dst_stage_mask,
1167+
.dstAccessMask = dst_access_mask,
1168+
.oldLayout = old_layout,
1169+
.newLayout = new_layout,
1170+
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1171+
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1172+
.image = image,
1173+
.subresourceRange = {
1174+
.aspectMask = aspect_mask,
1175+
.baseMipLevel = 0,
1176+
.levelCount = 1,
1177+
.baseArrayLayer = 0,
1178+
.layerCount = 1
1179+
}
1180+
};
1181+
vk::DependencyInfo dependency_info = {
1182+
.dependencyFlags = {},
1183+
.imageMemoryBarrierCount = 1,
1184+
.pImageMemoryBarriers = &barrier
1185+
};
1186+
commandBuffers[currentFrame].pipelineBarrier2(dependency_info);
1187+
}
1188+
11031189
void createSyncObjects() {
11041190
presentCompleteSemaphore.clear();
11051191
renderFinishedSemaphore.clear();

0 commit comments

Comments
 (0)