Fold layout transitions into subpass (Refs #155), refactoring of offscreen render pass

This commit is contained in:
saschawillems 2016-08-13 19:31:41 +02:00
parent 78736527ee
commit f60e5d25fd

View file

@ -127,18 +127,17 @@ public:
VkDeviceMemory mem; VkDeviceMemory mem;
VkImageView view; VkImageView view;
}; };
struct FrameBuffer { struct OffscreenPass {
int32_t width, height; int32_t width, height;
VkFramebuffer frameBuffer; VkFramebuffer frameBuffer;
FrameBufferAttachment color, depth; FrameBufferAttachment color, depth;
VkRenderPass renderPass; VkRenderPass renderPass;
VkSampler depthSampler; VkSampler depthSampler;
} offScreenFrameBuf; VkDescriptorImageInfo descriptor;
VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
VkCommandBuffer offScreenCmdBuffer = VK_NULL_HANDLE; // Semaphore used to synchronize between offscreen and final scene render pass
VkSemaphore semaphore = VK_NULL_HANDLE;
// Semaphore used to synchronize offscreen rendering before using it's texture target for sampling } offscreenPass;
VkSemaphore offscreenSemaphore = VK_NULL_HANDLE;
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
{ {
@ -155,21 +154,21 @@ public:
// Note : Inherited destructor cleans up resources stored in base class // Note : Inherited destructor cleans up resources stored in base class
// Frame buffer // Frame buffer
vkDestroySampler(device, offScreenFrameBuf.depthSampler, nullptr); vkDestroySampler(device, offscreenPass.depthSampler, nullptr);
// Color attachment // Color attachment
vkDestroyImageView(device, offScreenFrameBuf.color.view, nullptr); vkDestroyImageView(device, offscreenPass.color.view, nullptr);
vkDestroyImage(device, offScreenFrameBuf.color.image, nullptr); vkDestroyImage(device, offscreenPass.color.image, nullptr);
vkFreeMemory(device, offScreenFrameBuf.color.mem, nullptr); vkFreeMemory(device, offscreenPass.color.mem, nullptr);
// Depth attachment // Depth attachment
vkDestroyImageView(device, offScreenFrameBuf.depth.view, nullptr); vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
vkDestroyImage(device, offScreenFrameBuf.depth.image, nullptr); vkDestroyImage(device, offscreenPass.depth.image, nullptr);
vkFreeMemory(device, offScreenFrameBuf.depth.mem, nullptr); vkFreeMemory(device, offscreenPass.depth.mem, nullptr);
vkDestroyFramebuffer(device, offScreenFrameBuf.frameBuffer, nullptr); vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr);
vkDestroyRenderPass(device, offScreenFrameBuf.renderPass, nullptr); vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr);
vkDestroyPipeline(device, pipelines.quad, nullptr); vkDestroyPipeline(device, pipelines.quad, nullptr);
vkDestroyPipeline(device, pipelines.offscreen, nullptr); vkDestroyPipeline(device, pipelines.offscreen, nullptr);
@ -189,42 +188,34 @@ public:
vkTools::destroyUniformData(device, &uniformData.offscreen); vkTools::destroyUniformData(device, &uniformData.offscreen);
vkTools::destroyUniformData(device, &uniformData.scene); vkTools::destroyUniformData(device, &uniformData.scene);
vkFreeCommandBuffers(device, cmdPool, 1, &offScreenCmdBuffer); vkFreeCommandBuffers(device, cmdPool, 1, &offscreenPass.commandBuffer);
vkDestroySemaphore(device, offscreenSemaphore, nullptr); vkDestroySemaphore(device, offscreenPass.semaphore, nullptr);
} }
// Set up a separate render pass for the offscreen frame buffer // Set up a separate render pass for the offscreen frame buffer
// This is necessary as the offscreen frame buffer attachments // This is necessary as the offscreen frame buffer attachments use formats different to those from the example render pass
// use formats different to the ones from the visible frame buffer
// and at least the depth one may not be compatible
void prepareOffscreenRenderpass() void prepareOffscreenRenderpass()
{ {
// todo: no color attachment required
VkAttachmentDescription attDesc[2]; VkAttachmentDescription attDesc[2];
attDesc[0].format = FB_COLOR_FORMAT; attDesc[0].format = FB_COLOR_FORMAT;
attDesc[0].samples = VK_SAMPLE_COUNT_1_BIT; attDesc[0].samples = VK_SAMPLE_COUNT_1_BIT;
attDesc[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attDesc[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
// We only need depth information for shadow mapping attDesc[0].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; // We won't sample from color, so throw away
// So we don't need to store the color information
// after the render pass has finished
attDesc[0].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attDesc[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attDesc[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attDesc[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attDesc[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attDesc[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; attDesc[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attDesc[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; attDesc[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attDesc[0].flags = VK_FLAGS_NONE; attDesc[0].flags = VK_FLAGS_NONE;
attDesc[1].format = DEPTH_FORMAT; attDesc[1].format = DEPTH_FORMAT;
attDesc[1].samples = VK_SAMPLE_COUNT_1_BIT; attDesc[1].samples = VK_SAMPLE_COUNT_1_BIT;
attDesc[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attDesc[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
// Since we need to copy the depth attachment contents to our texture attDesc[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE; // We will read from depth, so it's important to store the depth attachment results
// used for shadow mapping we must use STORE_OP_STORE to make sure that
// the depth attachment contents are preserved after rendering to it
// has finished
attDesc[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attDesc[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attDesc[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attDesc[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attDesc[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attDesc[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; attDesc[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attDesc[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; attDesc[1].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
attDesc[1].flags = VK_FLAGS_NONE; attDesc[1].flags = VK_FLAGS_NONE;
VkAttachmentReference colorReference = {}; VkAttachmentReference colorReference = {};
@ -241,23 +232,42 @@ public:
subpass.pColorAttachments = &colorReference; subpass.pColorAttachments = &colorReference;
subpass.pDepthStencilAttachment = &depthReference; subpass.pDepthStencilAttachment = &depthReference;
// Use subpass dependencies for layout transitions
std::array<VkSubpassDependency, 2> dependencies;
dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
dependencies[0].dstSubpass = 0;
dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_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].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
dependencies[1].srcSubpass = 0;
dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_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].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
VkRenderPassCreateInfo renderPassCreateInfo = vkTools::initializers::renderPassCreateInfo(); VkRenderPassCreateInfo renderPassCreateInfo = vkTools::initializers::renderPassCreateInfo();
renderPassCreateInfo.attachmentCount = 2; renderPassCreateInfo.attachmentCount = 2;
renderPassCreateInfo.pAttachments = attDesc; renderPassCreateInfo.pAttachments = attDesc;
renderPassCreateInfo.subpassCount = 1; renderPassCreateInfo.subpassCount = 1;
renderPassCreateInfo.pSubpasses = &subpass; renderPassCreateInfo.pSubpasses = &subpass;
renderPassCreateInfo.dependencyCount = static_cast<uint32_t>(dependencies.size());
renderPassCreateInfo.pDependencies = dependencies.data();
VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCreateInfo, nullptr, &offScreenFrameBuf.renderPass)); VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCreateInfo, nullptr, &offscreenPass.renderPass));
} }
// Setup the offscreen framebuffer for rendering the scene from // Setup the offscreen framebuffer for rendering the scene from light's point-of-view to
// light's point-of-view to // The depth attachment of this framebuffer will then be used to sample from in the fragment shader of the shadowing pass
// The depth attachment of this framebuffer will then be used
// to sample frame in the fragment shader of the shadowing pass
void prepareOffscreenFramebuffer() void prepareOffscreenFramebuffer()
{ {
offScreenFrameBuf.width = SHADOWMAP_DIM; offscreenPass.width = SHADOWMAP_DIM;
offScreenFrameBuf.height = SHADOWMAP_DIM; offscreenPass.height = SHADOWMAP_DIM;
VkFormat fbColorFormat = FB_COLOR_FORMAT; VkFormat fbColorFormat = FB_COLOR_FORMAT;
@ -265,8 +275,8 @@ public:
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.format = fbColorFormat;
image.extent.width = offScreenFrameBuf.width; image.extent.width = offscreenPass.width;
image.extent.height = offScreenFrameBuf.height; image.extent.height = offscreenPass.height;
image.extent.depth = 1; image.extent.depth = 1;
image.mipLevels = 1; image.mipLevels = 1;
image.arrayLayers = 1; image.arrayLayers = 1;
@ -287,25 +297,16 @@ public:
colorImageView.subresourceRange.levelCount = 1; colorImageView.subresourceRange.levelCount = 1;
colorImageView.subresourceRange.baseArrayLayer = 0; colorImageView.subresourceRange.baseArrayLayer = 0;
colorImageView.subresourceRange.layerCount = 1; colorImageView.subresourceRange.layerCount = 1;
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offScreenFrameBuf.color.image)); VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offscreenPass.color.image));
vkGetImageMemoryRequirements(device, offScreenFrameBuf.color.image, &memReqs); 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, &offScreenFrameBuf.color.mem)); VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.color.mem));
VK_CHECK_RESULT(vkBindImageMemory(device, offScreenFrameBuf.color.image, offScreenFrameBuf.color.mem, 0)); VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.color.image, offscreenPass.color.mem, 0));
VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); colorImageView.image = offscreenPass.color.image;
VK_CHECK_RESULT(vkCreateImageView(device, &colorImageView, nullptr, &offscreenPass.color.view));
vkTools::setImageLayout(
layoutCmd,
offScreenFrameBuf.color.image,
VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
colorImageView.image = offScreenFrameBuf.color.image;
VK_CHECK_RESULT(vkCreateImageView(device, &colorImageView, nullptr, &offScreenFrameBuf.color.view));
// Depth stencil attachment // Depth stencil attachment
image.format = DEPTH_FORMAT; image.format = DEPTH_FORMAT;
@ -321,27 +322,16 @@ 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, &offScreenFrameBuf.depth.image)); VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offscreenPass.depth.image));
vkGetImageMemoryRequirements(device, offScreenFrameBuf.depth.image, &memReqs); vkGetImageMemoryRequirements(device, offscreenPass.depth.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, &offScreenFrameBuf.depth.mem)); VK_CHECK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &offscreenPass.depth.mem));
VK_CHECK_RESULT(vkBindImageMemory(device, offScreenFrameBuf.depth.image, offScreenFrameBuf.depth.mem, 0)); VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.depth.image, offscreenPass.depth.mem, 0));
// Set the initial layout to shader read instead of attachment depthStencilView.image = offscreenPass.depth.image;
// This is done as the render loop does the actualy image layout transitions VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &offscreenPass.depth.view));
vkTools::setImageLayout(
layoutCmd,
offScreenFrameBuf.depth.image,
VK_IMAGE_ASPECT_DEPTH_BIT,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
VulkanExampleBase::flushCommandBuffer(layoutCmd, queue, true);
depthStencilView.image = offScreenFrameBuf.depth.image;
VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &offScreenFrameBuf.depth.view));
// Create sampler to sample from to depth attachment // Create sampler to sample from to depth attachment
// Used to sample in the fragment shader for shadowed rendering // Used to sample in the fragment shader for shadowed rendering
@ -357,36 +347,38 @@ public:
sampler.minLod = 0.0f; sampler.minLod = 0.0f;
sampler.maxLod = 1.0f; sampler.maxLod = 1.0f;
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &offScreenFrameBuf.depthSampler)); VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &offscreenPass.depthSampler));
VkImageView attachments[2]; VkImageView attachments[2];
attachments[0] = offScreenFrameBuf.color.view; attachments[0] = offscreenPass.color.view;
attachments[1] = offScreenFrameBuf.depth.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 = offScreenFrameBuf.renderPass; fbufCreateInfo.renderPass = offscreenPass.renderPass;
fbufCreateInfo.attachmentCount = 2; fbufCreateInfo.attachmentCount = 2;
fbufCreateInfo.pAttachments = attachments; fbufCreateInfo.pAttachments = attachments;
fbufCreateInfo.width = offScreenFrameBuf.width; fbufCreateInfo.width = offscreenPass.width;
fbufCreateInfo.height = offScreenFrameBuf.height; fbufCreateInfo.height = offscreenPass.height;
fbufCreateInfo.layers = 1; fbufCreateInfo.layers = 1;
VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &offScreenFrameBuf.frameBuffer)); VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &offscreenPass.frameBuffer));
} }
void buildOffscreenCommandBuffer() void buildOffscreenCommandBuffer()
{ {
if (offScreenCmdBuffer == VK_NULL_HANDLE) if (offscreenPass.commandBuffer == VK_NULL_HANDLE)
{ {
offScreenCmdBuffer = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false); offscreenPass.commandBuffer = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false);
}
if (offscreenPass.semaphore == VK_NULL_HANDLE)
{
// Create a semaphore used to synchronize offscreen rendering and usage
VkSemaphoreCreateInfo semaphoreCreateInfo = vkTools::initializers::semaphoreCreateInfo();
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &offscreenPass.semaphore));
} }
// Create a semaphore used to synchronize offscreen rendering and usage
VkSemaphoreCreateInfo semaphoreCreateInfo = vkTools::initializers::semaphoreCreateInfo();
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &offscreenSemaphore));
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo(); VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
@ -395,60 +387,44 @@ public:
clearValues[1].depthStencil = { 1.0f, 0 }; clearValues[1].depthStencil = { 1.0f, 0 };
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo(); VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
renderPassBeginInfo.renderPass = offScreenFrameBuf.renderPass; renderPassBeginInfo.renderPass = offscreenPass.renderPass;
renderPassBeginInfo.framebuffer = offScreenFrameBuf.frameBuffer; renderPassBeginInfo.framebuffer = offscreenPass.frameBuffer;
renderPassBeginInfo.renderArea.offset.x = 0; renderPassBeginInfo.renderArea.offset.x = 0;
renderPassBeginInfo.renderArea.offset.y = 0; renderPassBeginInfo.renderArea.offset.y = 0;
renderPassBeginInfo.renderArea.extent.width = offScreenFrameBuf.width; renderPassBeginInfo.renderArea.extent.width = offscreenPass.width;
renderPassBeginInfo.renderArea.extent.height = offScreenFrameBuf.height; renderPassBeginInfo.renderArea.extent.height = offscreenPass.height;
renderPassBeginInfo.clearValueCount = 2; renderPassBeginInfo.clearValueCount = 2;
renderPassBeginInfo.pClearValues = clearValues; renderPassBeginInfo.pClearValues = clearValues;
VK_CHECK_RESULT(vkBeginCommandBuffer(offScreenCmdBuffer, &cmdBufInfo)); VK_CHECK_RESULT(vkBeginCommandBuffer(offscreenPass.commandBuffer, &cmdBufInfo));
// Change back layout of the depth attachment after sampling in the fragment shader VkViewport viewport = vkTools::initializers::viewport((float)offscreenPass.width, (float)offscreenPass.height, 0.0f, 1.0f);
vkTools::setImageLayout( vkCmdSetViewport(offscreenPass.commandBuffer, 0, 1, &viewport);
offScreenCmdBuffer,
offScreenFrameBuf.depth.image,
VK_IMAGE_ASPECT_DEPTH_BIT,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
VkViewport viewport = vkTools::initializers::viewport((float)offScreenFrameBuf.width, (float)offScreenFrameBuf.height, 0.0f, 1.0f); VkRect2D scissor = vkTools::initializers::rect2D(offscreenPass.width, offscreenPass.height, 0, 0);
vkCmdSetViewport(offScreenCmdBuffer, 0, 1, &viewport); vkCmdSetScissor(offscreenPass.commandBuffer, 0, 1, &scissor);
VkRect2D scissor = vkTools::initializers::rect2D(offScreenFrameBuf.width, offScreenFrameBuf.height, 0, 0);
vkCmdSetScissor(offScreenCmdBuffer, 0, 1, &scissor);
// Set depth bias (aka "Polygon offset") // Set depth bias (aka "Polygon offset")
// Required to avoid shadow mapping artefacts // Required to avoid shadow mapping artefacts
vkCmdSetDepthBias( vkCmdSetDepthBias(
offScreenCmdBuffer, offscreenPass.commandBuffer,
depthBiasConstant, depthBiasConstant,
0.0f, 0.0f,
depthBiasSlope); depthBiasSlope);
vkCmdBeginRenderPass(offScreenCmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBeginRenderPass(offscreenPass.commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen); vkCmdBindPipeline(offscreenPass.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.offscreen);
vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, &descriptorSets.offscreen, 0, NULL); vkCmdBindDescriptorSets(offscreenPass.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.offscreen, 0, 1, &descriptorSets.offscreen, 0, NULL);
VkDeviceSize offsets[1] = { 0 }; VkDeviceSize offsets[1] = { 0 };
vkCmdBindVertexBuffers(offScreenCmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.scene.vertices.buf, offsets); vkCmdBindVertexBuffers(offscreenPass.commandBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.scene.vertices.buf, offsets);
vkCmdBindIndexBuffer(offScreenCmdBuffer, meshes.scene.indices.buf, 0, VK_INDEX_TYPE_UINT32); vkCmdBindIndexBuffer(offscreenPass.commandBuffer, meshes.scene.indices.buf, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(offScreenCmdBuffer, meshes.scene.indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(offscreenPass.commandBuffer, meshes.scene.indexCount, 1, 0, 0, 0);
vkCmdEndRenderPass(offScreenCmdBuffer); vkCmdEndRenderPass(offscreenPass.commandBuffer);
// Change layout of the depth attachment for sampling in the fragment shader VK_CHECK_RESULT(vkEndCommandBuffer(offscreenPass.commandBuffer));
vkTools::setImageLayout(
offScreenCmdBuffer,
offScreenFrameBuf.depth.image,
VK_IMAGE_ASPECT_DEPTH_BIT,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
VK_CHECK_RESULT(vkEndCommandBuffer(offScreenCmdBuffer));
} }
void buildCommandBuffers() void buildCommandBuffers()
@ -669,8 +645,8 @@ public:
// Image descriptor for the shadow map attachment // Image descriptor for the shadow map attachment
VkDescriptorImageInfo texDescriptor = VkDescriptorImageInfo texDescriptor =
vkTools::initializers::descriptorImageInfo( vkTools::initializers::descriptorImageInfo(
offScreenFrameBuf.depthSampler, offscreenPass.depthSampler,
offScreenFrameBuf.depth.view, offscreenPass.depth.view,
VK_IMAGE_LAYOUT_GENERAL); VK_IMAGE_LAYOUT_GENERAL);
std::vector<VkWriteDescriptorSet> writeDescriptorSets = std::vector<VkWriteDescriptorSet> writeDescriptorSets =
@ -709,8 +685,8 @@ public:
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.scene)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.scene));
// Image descriptor for the shadow map attachment // Image descriptor for the shadow map attachment
texDescriptor.sampler = offScreenFrameBuf.depthSampler; texDescriptor.sampler = offscreenPass.depthSampler;
texDescriptor.imageView = offScreenFrameBuf.depth.view; texDescriptor.imageView = offscreenPass.depth.view;
std::vector<VkWriteDescriptorSet> sceneDescriptorSets = std::vector<VkWriteDescriptorSet> sceneDescriptorSets =
{ {
@ -828,7 +804,7 @@ public:
0); 0);
pipelineCreateInfo.layout = pipelineLayouts.offscreen; pipelineCreateInfo.layout = pipelineLayouts.offscreen;
pipelineCreateInfo.renderPass = offScreenFrameBuf.renderPass; pipelineCreateInfo.renderPass = offscreenPass.renderPass;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.offscreen)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.offscreen));
} }
@ -845,7 +821,7 @@ public:
&uniformDataVS.memory, &uniformDataVS.memory,
&uniformDataVS.descriptor); &uniformDataVS.descriptor);
// Offsvreen vertex shader uniform buffer block // Offscreen vertex shader uniform buffer block
createBuffer( createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
@ -936,27 +912,25 @@ public:
{ {
VulkanExampleBase::prepareFrame(); VulkanExampleBase::prepareFrame();
// The scene render command buffer has to wait for the offscreen // The scene render command buffer has to wait for the offscreen rendering (and transfer) to be finished before using the shadow map
// rendering (and transfer) to be finished before using // Therefore we synchronize using an additional semaphore
// the shadow map, so we need to synchronize
// We use an additional semaphore for this
// Offscreen rendering // Offscreen rendering
// Wait for swap chain presentation to finish // Wait for swap chain presentation to finish
submitInfo.pWaitSemaphores = &semaphores.presentComplete; submitInfo.pWaitSemaphores = &semaphores.presentComplete;
// Signal ready with offscreen semaphore // Signal ready with offscreen semaphore
submitInfo.pSignalSemaphores = &offscreenSemaphore; submitInfo.pSignalSemaphores = &offscreenPass.semaphore;
// Submit work // Submit work
submitInfo.commandBufferCount = 1; submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &offScreenCmdBuffer; submitInfo.pCommandBuffers = &offscreenPass.commandBuffer;
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE)); VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
// Scene rendering // Scene rendering
// Wait for offscreen semaphore // Wait for offscreen semaphore
submitInfo.pWaitSemaphores = &offscreenSemaphore; submitInfo.pWaitSemaphores = &offscreenPass.semaphore;;
// Signal ready with render complete semaphpre // Signal ready with render complete semaphpre
submitInfo.pSignalSemaphores = &semaphores.renderComplete; submitInfo.pSignalSemaphores = &semaphores.renderComplete;
@ -1043,63 +1017,4 @@ public:
}; };
VulkanExample *vulkanExample; VULKAN_EXAMPLE_MAIN()
#if defined(_WIN32)
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (vulkanExample != NULL)
{
vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam);
}
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}
#elif defined(__linux__) && !defined(__ANDROID__)
static void handleEvent(const xcb_generic_event_t *event)
{
if (vulkanExample != NULL)
{
vulkanExample->handleEvent(event);
}
}
#endif
// Main entry point
#if defined(_WIN32)
// Windows entry point
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
#elif defined(__ANDROID__)
// Android entry point
void android_main(android_app* state)
#elif defined(__linux__)
// Linux entry point
int main(const int argc, const char *argv[])
#endif
{
#if defined(__ANDROID__)
// Removing this may cause the compiler to omit the main entry point
// which would make the application crash at start
app_dummy();
#endif
vulkanExample = new VulkanExample();
#if defined(_WIN32)
vulkanExample->setupWindow(hInstance, WndProc);
#elif defined(__ANDROID__)
// Attach vulkan example to global android application state
state->userData = vulkanExample;
state->onAppCmd = VulkanExample::handleAppCommand;
state->onInputEvent = VulkanExample::handleAppInput;
vulkanExample->androidApp = state;
#elif defined(__linux__)
vulkanExample->setupWindow();
#endif
#if !defined(__ANDROID__)
vulkanExample->initSwapchain();
vulkanExample->prepare();
#endif
vulkanExample->renderLoop();
delete(vulkanExample);
#if !defined(__ANDROID__)
return 0;
#endif
}