Depth only pass for shadow map generation (removed color attachment)
This commit is contained in:
parent
f60e5d25fd
commit
a84d5421fe
1 changed files with 31 additions and 83 deletions
|
|
@ -130,7 +130,7 @@ public:
|
||||||
struct OffscreenPass {
|
struct OffscreenPass {
|
||||||
int32_t width, height;
|
int32_t width, height;
|
||||||
VkFramebuffer frameBuffer;
|
VkFramebuffer frameBuffer;
|
||||||
FrameBufferAttachment color, depth;
|
FrameBufferAttachment depth;
|
||||||
VkRenderPass renderPass;
|
VkRenderPass renderPass;
|
||||||
VkSampler depthSampler;
|
VkSampler depthSampler;
|
||||||
VkDescriptorImageInfo descriptor;
|
VkDescriptorImageInfo descriptor;
|
||||||
|
|
@ -156,11 +156,6 @@ public:
|
||||||
// Frame buffer
|
// Frame buffer
|
||||||
vkDestroySampler(device, offscreenPass.depthSampler, nullptr);
|
vkDestroySampler(device, offscreenPass.depthSampler, nullptr);
|
||||||
|
|
||||||
// Color attachment
|
|
||||||
vkDestroyImageView(device, offscreenPass.color.view, nullptr);
|
|
||||||
vkDestroyImage(device, offscreenPass.color.image, nullptr);
|
|
||||||
vkFreeMemory(device, offscreenPass.color.mem, nullptr);
|
|
||||||
|
|
||||||
// Depth attachment
|
// Depth attachment
|
||||||
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
|
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
|
||||||
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
|
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
|
||||||
|
|
@ -197,40 +192,24 @@ public:
|
||||||
void prepareOffscreenRenderpass()
|
void prepareOffscreenRenderpass()
|
||||||
{
|
{
|
||||||
// todo: no color attachment required
|
// todo: no color attachment required
|
||||||
VkAttachmentDescription attDesc[2];
|
VkAttachmentDescription attchmentDescription{};
|
||||||
attDesc[0].format = FB_COLOR_FORMAT;
|
attchmentDescription.format = DEPTH_FORMAT;
|
||||||
attDesc[0].samples = VK_SAMPLE_COUNT_1_BIT;
|
attchmentDescription.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
attDesc[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
attchmentDescription.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; // Clear depth at beginning of the render pass
|
||||||
attDesc[0].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; // We won't sample from color, so throw away
|
attchmentDescription.storeOp = VK_ATTACHMENT_STORE_OP_STORE; // We will read from depth, so it's important to store the depth attachment results
|
||||||
attDesc[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
attchmentDescription.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||||
attDesc[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
attchmentDescription.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||||
attDesc[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
attchmentDescription.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; // We don't care about initial layout of the attachment
|
||||||
attDesc[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
attchmentDescription.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; // Attachment will be transitioned to shader read at render pass end
|
||||||
attDesc[0].flags = VK_FLAGS_NONE;
|
|
||||||
|
|
||||||
attDesc[1].format = DEPTH_FORMAT;
|
|
||||||
attDesc[1].samples = VK_SAMPLE_COUNT_1_BIT;
|
|
||||||
attDesc[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
|
||||||
attDesc[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE; // We will read from depth, so it's important to store the depth attachment results
|
|
||||||
attDesc[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
|
||||||
attDesc[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
||||||
attDesc[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
attDesc[1].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
||||||
attDesc[1].flags = VK_FLAGS_NONE;
|
|
||||||
|
|
||||||
VkAttachmentReference colorReference = {};
|
|
||||||
colorReference.attachment = 0;
|
|
||||||
colorReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
||||||
|
|
||||||
VkAttachmentReference depthReference = {};
|
VkAttachmentReference depthReference = {};
|
||||||
depthReference.attachment = 1;
|
depthReference.attachment = 0;
|
||||||
depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; // Attachment will be used as depth/stencil during render pass
|
||||||
|
|
||||||
VkSubpassDescription subpass = {};
|
VkSubpassDescription subpass = {};
|
||||||
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||||
subpass.colorAttachmentCount = 1;
|
subpass.colorAttachmentCount = 0; // No color attachments
|
||||||
subpass.pColorAttachments = &colorReference;
|
subpass.pDepthStencilAttachment = &depthReference; // Reference to our depth attachment
|
||||||
subpass.pDepthStencilAttachment = &depthReference;
|
|
||||||
|
|
||||||
// Use subpass dependencies for layout transitions
|
// Use subpass dependencies for layout transitions
|
||||||
std::array<VkSubpassDependency, 2> dependencies;
|
std::array<VkSubpassDependency, 2> dependencies;
|
||||||
|
|
@ -240,20 +219,20 @@ public:
|
||||||
dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
||||||
dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||||
dependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
dependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||||||
dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
dependencies[0].dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||||||
dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
||||||
|
|
||||||
dependencies[1].srcSubpass = 0;
|
dependencies[1].srcSubpass = 0;
|
||||||
dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
|
dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
|
||||||
dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||||
dependencies[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
dependencies[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
||||||
dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
dependencies[1].srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
||||||
dependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
dependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||||||
dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
||||||
|
|
||||||
VkRenderPassCreateInfo renderPassCreateInfo = vkTools::initializers::renderPassCreateInfo();
|
VkRenderPassCreateInfo renderPassCreateInfo = vkTools::initializers::renderPassCreateInfo();
|
||||||
renderPassCreateInfo.attachmentCount = 2;
|
renderPassCreateInfo.attachmentCount = 1;
|
||||||
renderPassCreateInfo.pAttachments = attDesc;
|
renderPassCreateInfo.pAttachments = &attchmentDescription;
|
||||||
renderPassCreateInfo.subpassCount = 1;
|
renderPassCreateInfo.subpassCount = 1;
|
||||||
renderPassCreateInfo.pSubpasses = &subpass;
|
renderPassCreateInfo.pSubpasses = &subpass;
|
||||||
renderPassCreateInfo.dependencyCount = static_cast<uint32_t>(dependencies.size());
|
renderPassCreateInfo.dependencyCount = static_cast<uint32_t>(dependencies.size());
|
||||||
|
|
@ -271,10 +250,9 @@ public:
|
||||||
|
|
||||||
VkFormat fbColorFormat = FB_COLOR_FORMAT;
|
VkFormat fbColorFormat = FB_COLOR_FORMAT;
|
||||||
|
|
||||||
// Color attachment
|
// For shadow mapping we only need a depth attachment
|
||||||
VkImageCreateInfo image = vkTools::initializers::imageCreateInfo();
|
VkImageCreateInfo image = vkTools::initializers::imageCreateInfo();
|
||||||
image.imageType = VK_IMAGE_TYPE_2D;
|
image.imageType = VK_IMAGE_TYPE_2D;
|
||||||
image.format = fbColorFormat;
|
|
||||||
image.extent.width = offscreenPass.width;
|
image.extent.width = offscreenPass.width;
|
||||||
image.extent.height = offscreenPass.height;
|
image.extent.height = offscreenPass.height;
|
||||||
image.extent.depth = 1;
|
image.extent.depth = 1;
|
||||||
|
|
@ -282,36 +260,17 @@ public:
|
||||||
image.arrayLayers = 1;
|
image.arrayLayers = 1;
|
||||||
image.samples = VK_SAMPLE_COUNT_1_BIT;
|
image.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||||
image.tiling = VK_IMAGE_TILING_OPTIMAL;
|
image.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||||
// Image of the framebuffer is blit source
|
image.format = DEPTH_FORMAT; // Depth stencil attachment
|
||||||
image.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
image.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; // We will sample directly from the depth attachment for the shadow mapping
|
||||||
|
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offscreenPass.depth.image));
|
||||||
|
|
||||||
VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
|
VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
|
||||||
VkMemoryRequirements memReqs;
|
VkMemoryRequirements memReqs;
|
||||||
|
vkGetImageMemoryRequirements(device, offscreenPass.depth.image, &memReqs);
|
||||||
VkImageViewCreateInfo colorImageView = vkTools::initializers::imageViewCreateInfo();
|
|
||||||
colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
||||||
colorImageView.format = fbColorFormat;
|
|
||||||
colorImageView.subresourceRange = {};
|
|
||||||
colorImageView.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
||||||
colorImageView.subresourceRange.baseMipLevel = 0;
|
|
||||||
colorImageView.subresourceRange.levelCount = 1;
|
|
||||||
colorImageView.subresourceRange.baseArrayLayer = 0;
|
|
||||||
colorImageView.subresourceRange.layerCount = 1;
|
|
||||||
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offscreenPass.color.image));
|
|
||||||
|
|
||||||
vkGetImageMemoryRequirements(device, offscreenPass.color.image, &memReqs);
|
|
||||||
memAlloc.allocationSize = memReqs.size;
|
memAlloc.allocationSize = memReqs.size;
|
||||||
memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.color.mem));
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.depth.mem));
|
||||||
VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.color.image, offscreenPass.color.mem, 0));
|
VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.depth.image, offscreenPass.depth.mem, 0));
|
||||||
|
|
||||||
colorImageView.image = offscreenPass.color.image;
|
|
||||||
VK_CHECK_RESULT(vkCreateImageView(device, &colorImageView, nullptr, &offscreenPass.color.view));
|
|
||||||
|
|
||||||
// Depth stencil attachment
|
|
||||||
image.format = DEPTH_FORMAT;
|
|
||||||
// We will sample directly from the depth attachment for the shadow mapping
|
|
||||||
image.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
||||||
|
|
||||||
VkImageViewCreateInfo depthStencilView = vkTools::initializers::imageViewCreateInfo();
|
VkImageViewCreateInfo depthStencilView = vkTools::initializers::imageViewCreateInfo();
|
||||||
depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||||
|
|
@ -322,14 +281,6 @@ public:
|
||||||
depthStencilView.subresourceRange.levelCount = 1;
|
depthStencilView.subresourceRange.levelCount = 1;
|
||||||
depthStencilView.subresourceRange.baseArrayLayer = 0;
|
depthStencilView.subresourceRange.baseArrayLayer = 0;
|
||||||
depthStencilView.subresourceRange.layerCount = 1;
|
depthStencilView.subresourceRange.layerCount = 1;
|
||||||
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offscreenPass.depth.image));
|
|
||||||
|
|
||||||
vkGetImageMemoryRequirements(device, offscreenPass.depth.image, &memReqs);
|
|
||||||
memAlloc.allocationSize = memReqs.size;
|
|
||||||
memAlloc.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
||||||
VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.depth.mem));
|
|
||||||
VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.depth.image, offscreenPass.depth.mem, 0));
|
|
||||||
|
|
||||||
depthStencilView.image = offscreenPass.depth.image;
|
depthStencilView.image = offscreenPass.depth.image;
|
||||||
VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &offscreenPass.depth.view));
|
VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &offscreenPass.depth.view));
|
||||||
|
|
||||||
|
|
@ -349,17 +300,13 @@ public:
|
||||||
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
||||||
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &offscreenPass.depthSampler));
|
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &offscreenPass.depthSampler));
|
||||||
|
|
||||||
VkImageView attachments[2];
|
|
||||||
attachments[0] = offscreenPass.color.view;
|
|
||||||
attachments[1] = offscreenPass.depth.view;
|
|
||||||
|
|
||||||
prepareOffscreenRenderpass();
|
prepareOffscreenRenderpass();
|
||||||
|
|
||||||
// Create frame buffer
|
// Create frame buffer
|
||||||
VkFramebufferCreateInfo fbufCreateInfo = vkTools::initializers::framebufferCreateInfo();
|
VkFramebufferCreateInfo fbufCreateInfo = vkTools::initializers::framebufferCreateInfo();
|
||||||
fbufCreateInfo.renderPass = offscreenPass.renderPass;
|
fbufCreateInfo.renderPass = offscreenPass.renderPass;
|
||||||
fbufCreateInfo.attachmentCount = 2;
|
fbufCreateInfo.attachmentCount = 1;
|
||||||
fbufCreateInfo.pAttachments = attachments;
|
fbufCreateInfo.pAttachments = &offscreenPass.depth.view;
|
||||||
fbufCreateInfo.width = offscreenPass.width;
|
fbufCreateInfo.width = offscreenPass.width;
|
||||||
fbufCreateInfo.height = offscreenPass.height;
|
fbufCreateInfo.height = offscreenPass.height;
|
||||||
fbufCreateInfo.layers = 1;
|
fbufCreateInfo.layers = 1;
|
||||||
|
|
@ -382,9 +329,8 @@ public:
|
||||||
|
|
||||||
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
||||||
|
|
||||||
VkClearValue clearValues[2];
|
VkClearValue clearValues[1];
|
||||||
clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } };
|
clearValues[0].depthStencil = { 1.0f, 0 };
|
||||||
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
||||||
|
|
||||||
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
|
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
|
||||||
renderPassBeginInfo.renderPass = offscreenPass.renderPass;
|
renderPassBeginInfo.renderPass = offscreenPass.renderPass;
|
||||||
|
|
@ -791,6 +737,8 @@ public:
|
||||||
// Offscreen pipeline
|
// Offscreen pipeline
|
||||||
shaderStages[0] = loadShader(getAssetPath() + "shaders/shadowmapping/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/shadowmapping/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
||||||
shaderStages[1] = loadShader(getAssetPath() + "shaders/shadowmapping/offscreen.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/shadowmapping/offscreen.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||||
|
// No blend attachment states (no color attachments used)
|
||||||
|
colorBlendState.attachmentCount = 0;
|
||||||
// Cull front faces
|
// Cull front faces
|
||||||
depthStencilState.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
|
depthStencilState.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
|
||||||
// Enable depth bias
|
// Enable depth bias
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue