Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions attachments/27_depth_buffering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,12 @@ class HelloTriangleApplication {

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

vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
vk::Format depthFormat = findDepthFormat();
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &swapChainImageFormat,
.depthAttachmentFormat = depthFormat
};
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
.stageCount = 2,
.pStages = shaderStages,
Expand Down Expand Up @@ -889,19 +894,57 @@ class HelloTriangleApplication {
vk::PipelineStageFlagBits2::eTopOfPipe, // srcStage
vk::PipelineStageFlagBits2::eColorAttachmentOutput // dstStage
);
// Transition depth image to depth attachment optimal layout
vk::ImageMemoryBarrier2 depthBarrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
.srcAccessMask = {},
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = depthImage,
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eDepth,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1
}
};
vk::DependencyInfo depthDependencyInfo = {
.dependencyFlags = {},
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &depthBarrier
};
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);

vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
vk::RenderingAttachmentInfo attachmentInfo = {
vk::ClearValue clearDepth = vk::ClearDepthStencilValue(1.0f, 0);

vk::RenderingAttachmentInfo colorAttachmentInfo = {
.imageView = swapChainImageViews[imageIndex],
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eStore,
.clearValue = clearColor
};

vk::RenderingAttachmentInfo depthAttachmentInfo = {
.imageView = depthImageView,
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eDontCare,
.clearValue = clearDepth
};

vk::RenderingInfo renderingInfo = {
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
.layerCount = 1,
.colorAttachmentCount = 1,
.pColorAttachments = &attachmentInfo
.pColorAttachments = &colorAttachmentInfo,
.pDepthAttachment = &depthAttachmentInfo
};
commandBuffers[currentFrame].beginRendering(renderingInfo);
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
Expand Down
49 changes: 46 additions & 3 deletions attachments/28_model_loading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,12 @@ class HelloTriangleApplication {

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

vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
vk::Format depthFormat = findDepthFormat();
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &swapChainImageFormat,
.depthAttachmentFormat = depthFormat
};
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
.stageCount = 2,
.pStages = shaderStages,
Expand Down Expand Up @@ -934,19 +939,57 @@ class HelloTriangleApplication {
vk::PipelineStageFlagBits2::eTopOfPipe, // srcStage
vk::PipelineStageFlagBits2::eColorAttachmentOutput // dstStage
);
// Transition depth image to depth attachment optimal layout
vk::ImageMemoryBarrier2 depthBarrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
.srcAccessMask = {},
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = depthImage,
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eDepth,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1
}
};
vk::DependencyInfo depthDependencyInfo = {
.dependencyFlags = {},
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &depthBarrier
};
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);

vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
vk::RenderingAttachmentInfo attachmentInfo = {
vk::ClearValue clearDepth = vk::ClearDepthStencilValue(1.0f, 0);

vk::RenderingAttachmentInfo colorAttachmentInfo = {
.imageView = swapChainImageViews[imageIndex],
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eStore,
.clearValue = clearColor
};

vk::RenderingAttachmentInfo depthAttachmentInfo = {
.imageView = depthImageView,
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eDontCare,
.clearValue = clearDepth
};

vk::RenderingInfo renderingInfo = {
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
.layerCount = 1,
.colorAttachmentCount = 1,
.pColorAttachments = &attachmentInfo
.pColorAttachments = &colorAttachmentInfo,
.pDepthAttachment = &depthAttachmentInfo
};
commandBuffers[currentFrame].beginRendering(renderingInfo);
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
Expand Down
49 changes: 46 additions & 3 deletions attachments/29_mipmapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,12 @@ class HelloTriangleApplication {

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

vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
vk::Format depthFormat = findDepthFormat();
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &swapChainImageFormat,
.depthAttachmentFormat = depthFormat
};
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
.stageCount = 2,
.pStages = shaderStages,
Expand Down Expand Up @@ -1000,19 +1005,57 @@ class HelloTriangleApplication {
vk::PipelineStageFlagBits2::eTopOfPipe, // srcStage
vk::PipelineStageFlagBits2::eColorAttachmentOutput // dstStage
);
// Transition depth image to depth attachment optimal layout
vk::ImageMemoryBarrier2 depthBarrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
.srcAccessMask = {},
.dstStageMask = vk::PipelineStageFlagBits2::eEarlyFragmentTests | vk::PipelineStageFlagBits2::eLateFragmentTests,
.dstAccessMask = vk::AccessFlagBits2::eDepthStencilAttachmentRead | vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = depthImage,
.subresourceRange = {
.aspectMask = vk::ImageAspectFlagBits::eDepth,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1
}
};
vk::DependencyInfo depthDependencyInfo = {
.dependencyFlags = {},
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &depthBarrier
};
commandBuffers[currentFrame].pipelineBarrier2(depthDependencyInfo);

vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
vk::RenderingAttachmentInfo attachmentInfo = {
vk::ClearValue clearDepth = vk::ClearDepthStencilValue(1.0f, 0);

vk::RenderingAttachmentInfo colorAttachmentInfo = {
.imageView = swapChainImageViews[imageIndex],
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eStore,
.clearValue = clearColor
};

vk::RenderingAttachmentInfo depthAttachmentInfo = {
.imageView = depthImageView,
.imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eDontCare,
.clearValue = clearDepth
};

vk::RenderingInfo renderingInfo = {
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
.layerCount = 1,
.colorAttachmentCount = 1,
.pColorAttachments = &attachmentInfo
.pColorAttachments = &colorAttachmentInfo,
.pDepthAttachment = &depthAttachmentInfo
};
commandBuffers[currentFrame].beginRendering(renderingInfo);
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
Expand Down
98 changes: 92 additions & 6 deletions attachments/30_multisampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class HelloTriangleApplication {
setupDebugMessenger();
createSurface();
pickPhysicalDevice();
msaaSamples = getMaxUsableSampleCount();
createLogicalDevice();
createSwapChain();
createImageViews();
Expand Down Expand Up @@ -528,7 +529,12 @@ class HelloTriangleApplication {

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

vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{ .colorAttachmentCount = 1, .pColorAttachmentFormats = &swapChainImageFormat };
vk::Format depthFormat = findDepthFormat();
vk::PipelineRenderingCreateInfo pipelineRenderingCreateInfo{
.colorAttachmentCount = 1,
.pColorAttachmentFormats = &swapChainImageFormat,
.depthAttachmentFormat = depthFormat
};
vk::GraphicsPipelineCreateInfo pipelineInfo{ .pNext = &pipelineRenderingCreateInfo,
.stageCount = 2,
.pStages = shaderStages,
Expand Down Expand Up @@ -1019,7 +1025,8 @@ class HelloTriangleApplication {

void recordCommandBuffer(uint32_t imageIndex) {
commandBuffers[currentFrame].begin({});
// Before starting rendering, transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
// Before starting rendering, transition the images to the appropriate layouts
// Transition the swapchain image to COLOR_ATTACHMENT_OPTIMAL
transition_image_layout(
imageIndex,
vk::ImageLayout::eUndefined,
Expand All @@ -1029,19 +1036,60 @@ class HelloTriangleApplication {
vk::PipelineStageFlagBits2::eTopOfPipe, // srcStage
vk::PipelineStageFlagBits2::eColorAttachmentOutput // dstStage
);

// Transition the multisampled color image to COLOR_ATTACHMENT_OPTIMAL
transition_image_layout_custom(
colorImage,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eColorAttachmentOptimal,
{},
vk::AccessFlagBits2::eColorAttachmentWrite,
vk::PipelineStageFlagBits2::eTopOfPipe,
vk::PipelineStageFlagBits2::eColorAttachmentOutput,
vk::ImageAspectFlagBits::eColor
);

// Transition the depth image to DEPTH_ATTACHMENT_OPTIMAL
transition_image_layout_custom(
depthImage,
vk::ImageLayout::eUndefined,
vk::ImageLayout::eDepthAttachmentOptimal,
{},
vk::AccessFlagBits2::eDepthStencilAttachmentWrite,
vk::PipelineStageFlagBits2::eTopOfPipe,
vk::PipelineStageFlagBits2::eEarlyFragmentTests,
vk::ImageAspectFlagBits::eDepth
);
vk::ClearValue clearColor = vk::ClearColorValue(0.0f, 0.0f, 0.0f, 1.0f);
vk::RenderingAttachmentInfo attachmentInfo = {
.imageView = swapChainImageViews[imageIndex],
vk::ClearValue clearDepth = vk::ClearDepthStencilValue(1.0f, 0);

// Color attachment (multisampled) with resolve attachment
vk::RenderingAttachmentInfo colorAttachment = {
.imageView = colorImageView,
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
.resolveMode = vk::ResolveModeFlagBits::eAverage,
.resolveImageView = swapChainImageViews[imageIndex],
.resolveImageLayout = vk::ImageLayout::eColorAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eStore,
.clearValue = clearColor
};

// Depth attachment
vk::RenderingAttachmentInfo depthAttachment = {
.imageView = depthImageView,
.imageLayout = vk::ImageLayout::eDepthAttachmentOptimal,
.loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eDontCare,
.clearValue = clearDepth
};

vk::RenderingInfo renderingInfo = {
.renderArea = { .offset = { 0, 0 }, .extent = swapChainExtent },
.layerCount = 1,
.colorAttachmentCount = 1,
.pColorAttachments = &attachmentInfo
.pColorAttachments = &colorAttachment,
.pDepthAttachment = &depthAttachment
};
commandBuffers[currentFrame].beginRendering(renderingInfo);
commandBuffers[currentFrame].bindPipeline(vk::PipelineBindPoint::eGraphics, *graphicsPipeline);
Expand All @@ -1052,7 +1100,9 @@ class HelloTriangleApplication {
commandBuffers[currentFrame].bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, *descriptorSets[currentFrame], nullptr);
commandBuffers[currentFrame].drawIndexed(indices.size(), 1, 0, 0, 0);
commandBuffers[currentFrame].endRendering();
// After rendering, transition the swapchain image to PRESENT_SRC
// After rendering, transition the images to appropriate layouts

// Transition the swapchain image to PRESENT_SRC
transition_image_layout(
imageIndex,
vk::ImageLayout::eColorAttachmentOptimal,
Expand Down Expand Up @@ -1100,6 +1150,42 @@ class HelloTriangleApplication {
commandBuffers[currentFrame].pipelineBarrier2(dependency_info);
}

void transition_image_layout_custom(
vk::raii::Image& image,
vk::ImageLayout old_layout,
vk::ImageLayout new_layout,
vk::AccessFlags2 src_access_mask,
vk::AccessFlags2 dst_access_mask,
vk::PipelineStageFlags2 src_stage_mask,
vk::PipelineStageFlags2 dst_stage_mask,
vk::ImageAspectFlags aspect_mask
) {
vk::ImageMemoryBarrier2 barrier = {
.srcStageMask = src_stage_mask,
.srcAccessMask = src_access_mask,
.dstStageMask = dst_stage_mask,
.dstAccessMask = dst_access_mask,
.oldLayout = old_layout,
.newLayout = new_layout,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = image,
.subresourceRange = {
.aspectMask = aspect_mask,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1
}
};
vk::DependencyInfo dependency_info = {
.dependencyFlags = {},
.imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &barrier
};
commandBuffers[currentFrame].pipelineBarrier2(dependency_info);
}

void createSyncObjects() {
presentCompleteSemaphore.clear();
renderFinishedSemaphore.clear();
Expand Down
Loading