Fold layout transitions into subpass (Refs #155), refactoring of offscreen render pass
This commit is contained in:
parent
fe090d85ba
commit
a28618b7e0
1 changed files with 161 additions and 216 deletions
|
|
@ -109,17 +109,18 @@ public:
|
|||
VkDeviceMemory mem;
|
||||
VkImageView view;
|
||||
};
|
||||
struct FrameBuffer {
|
||||
struct OffscreenPass {
|
||||
int32_t width, height;
|
||||
VkFramebuffer frameBuffer;
|
||||
FrameBufferAttachment color, depth;
|
||||
VkSampler colorSampler;
|
||||
} offScreenFrameBuf;
|
||||
VkRenderPass renderPass;
|
||||
VkSampler sampler;
|
||||
VkDescriptorImageInfo descriptor;
|
||||
VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
|
||||
// Semaphore used to synchronize between offscreen and final scene render pass
|
||||
VkSemaphore semaphore = VK_NULL_HANDLE;
|
||||
} offscreenPass;
|
||||
|
||||
VkCommandBuffer offScreenCmdBuffer = VK_NULL_HANDLE;
|
||||
|
||||
// Semaphore used to synchronize between offscreen and final scene rendering
|
||||
VkSemaphore offscreenSemaphore = VK_NULL_HANDLE;
|
||||
|
||||
glm::vec3 meshPos = glm::vec3(0.0f, -1.5f, 0.0f);
|
||||
glm::vec3 meshRot = glm::vec3(0.0f);
|
||||
|
|
@ -141,21 +142,22 @@ public:
|
|||
|
||||
// Textures
|
||||
textureLoader->destroyTexture(textures.colorMap);
|
||||
vkDestroySampler(device, offScreenFrameBuf.colorSampler, nullptr);
|
||||
|
||||
// Frame buffer
|
||||
|
||||
// Color attachment
|
||||
vkDestroyImageView(device, offScreenFrameBuf.color.view, nullptr);
|
||||
vkDestroyImage(device, offScreenFrameBuf.color.image, nullptr);
|
||||
vkFreeMemory(device, offScreenFrameBuf.color.mem, nullptr);
|
||||
vkDestroyImageView(device, offscreenPass.color.view, nullptr);
|
||||
vkDestroyImage(device, offscreenPass.color.image, nullptr);
|
||||
vkFreeMemory(device, offscreenPass.color.mem, nullptr);
|
||||
|
||||
// Depth attachment
|
||||
vkDestroyImageView(device, offScreenFrameBuf.depth.view, nullptr);
|
||||
vkDestroyImage(device, offScreenFrameBuf.depth.image, nullptr);
|
||||
vkFreeMemory(device, offScreenFrameBuf.depth.mem, nullptr);
|
||||
vkDestroyImageView(device, offscreenPass.depth.view, nullptr);
|
||||
vkDestroyImage(device, offscreenPass.depth.image, nullptr);
|
||||
vkFreeMemory(device, offscreenPass.depth.mem, nullptr);
|
||||
|
||||
vkDestroyFramebuffer(device, offScreenFrameBuf.frameBuffer, nullptr);
|
||||
vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr);
|
||||
vkDestroySampler(device, offscreenPass.sampler, nullptr);
|
||||
vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr);
|
||||
|
||||
vkDestroyPipeline(device, pipelines.debug, nullptr);
|
||||
vkDestroyPipeline(device, pipelines.shaded, nullptr);
|
||||
|
|
@ -179,19 +181,16 @@ public:
|
|||
vkTools::destroyUniformData(device, &uniformData.vsOffScreen);
|
||||
vkTools::destroyUniformData(device, &uniformData.vsDebugQuad);
|
||||
|
||||
vkFreeCommandBuffers(device, cmdPool, 1, &offScreenCmdBuffer);
|
||||
vkDestroySemaphore(device, offscreenSemaphore, nullptr);
|
||||
vkFreeCommandBuffers(device, cmdPool, 1, &offscreenPass.commandBuffer);
|
||||
vkDestroySemaphore(device, offscreenPass.semaphore, nullptr);
|
||||
}
|
||||
|
||||
// Setup the offscreen framebuffer for rendering the mirrored scene
|
||||
// The color attachment of this framebuffer will then be used
|
||||
// to sample frame in the fragment shader of the final pass
|
||||
void prepareOffscreenFramebuffer()
|
||||
// The color attachment of this framebuffer will then be used to sample from in the fragment shader of the final pass
|
||||
void prepareOffscreen()
|
||||
{
|
||||
offScreenFrameBuf.width = FB_DIM;
|
||||
offScreenFrameBuf.height = FB_DIM;
|
||||
|
||||
VkFormat fbColorFormat = FB_COLOR_FORMAT;
|
||||
offscreenPass.width = FB_DIM;
|
||||
offscreenPass.height = FB_DIM;
|
||||
|
||||
// Find a suitable depth format
|
||||
VkFormat fbDepthFormat;
|
||||
|
|
@ -201,9 +200,9 @@ public:
|
|||
// Color attachment
|
||||
VkImageCreateInfo image = vkTools::initializers::imageCreateInfo();
|
||||
image.imageType = VK_IMAGE_TYPE_2D;
|
||||
image.format = fbColorFormat;
|
||||
image.extent.width = offScreenFrameBuf.width;
|
||||
image.extent.height = offScreenFrameBuf.height;
|
||||
image.format = FB_COLOR_FORMAT;
|
||||
image.extent.width = offscreenPass.width;
|
||||
image.extent.height = offscreenPass.height;
|
||||
image.extent.depth = 1;
|
||||
image.mipLevels = 1;
|
||||
image.arrayLayers = 1;
|
||||
|
|
@ -215,58 +214,51 @@ public:
|
|||
VkMemoryAllocateInfo memAlloc = vkTools::initializers::memoryAllocateInfo();
|
||||
VkMemoryRequirements memReqs;
|
||||
|
||||
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offscreenPass.color.image));
|
||||
vkGetImageMemoryRequirements(device, offscreenPass.color.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.color.mem));
|
||||
VK_CHECK_RESULT(vkBindImageMemory(device, offscreenPass.color.image, offscreenPass.color.mem, 0));
|
||||
|
||||
VkImageViewCreateInfo colorImageView = vkTools::initializers::imageViewCreateInfo();
|
||||
colorImageView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
colorImageView.format = fbColorFormat;
|
||||
colorImageView.format = FB_COLOR_FORMAT;
|
||||
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;
|
||||
colorImageView.image = offscreenPass.color.image;
|
||||
VK_CHECK_RESULT(vkCreateImageView(device, &colorImageView, nullptr, &offscreenPass.color.view));
|
||||
|
||||
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offScreenFrameBuf.color.image));
|
||||
vkGetImageMemoryRequirements(device, offScreenFrameBuf.color.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, &offScreenFrameBuf.color.mem));
|
||||
VK_CHECK_RESULT(vkBindImageMemory(device, offScreenFrameBuf.color.image, offScreenFrameBuf.color.mem, 0));
|
||||
|
||||
// Get a primary command buffer for submitting image layout transitions for the framebuffer attachments
|
||||
VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||
|
||||
// Set the initial layout to shader read instead of attachment
|
||||
// This is done as the render loop does the actualy image layout transitions
|
||||
vkTools::setImageLayout(
|
||||
layoutCmd,
|
||||
offScreenFrameBuf.color.image,
|
||||
VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
VK_IMAGE_LAYOUT_UNDEFINED,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||
|
||||
colorImageView.image = offScreenFrameBuf.color.image;
|
||||
VK_CHECK_RESULT(vkCreateImageView(device, &colorImageView, nullptr, &offScreenFrameBuf.color.view));
|
||||
|
||||
// Create sampler to sample from to color attachment
|
||||
// Used to sample in the fragment shader for final rendering
|
||||
VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo();
|
||||
sampler.magFilter = VK_FILTER_LINEAR;
|
||||
sampler.minFilter = VK_FILTER_LINEAR;
|
||||
sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
||||
sampler.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||
sampler.addressModeV = sampler.addressModeU;
|
||||
sampler.addressModeW = sampler.addressModeU;
|
||||
sampler.mipLodBias = 0.0f;
|
||||
sampler.maxAnisotropy = 0;
|
||||
sampler.minLod = 0.0f;
|
||||
sampler.maxLod = 1.0f;
|
||||
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
||||
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &offScreenFrameBuf.colorSampler));
|
||||
// Create sampler to sample from the attachment in the fragment shader
|
||||
VkSamplerCreateInfo samplerInfo = vkTools::initializers::samplerCreateInfo();
|
||||
samplerInfo.magFilter = VK_FILTER_LINEAR;
|
||||
samplerInfo.minFilter = VK_FILTER_LINEAR;
|
||||
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
||||
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
||||
samplerInfo.addressModeV = samplerInfo.addressModeU;
|
||||
samplerInfo.addressModeW = samplerInfo.addressModeU;
|
||||
samplerInfo.mipLodBias = 0.0f;
|
||||
samplerInfo.maxAnisotropy = 0;
|
||||
samplerInfo.minLod = 0.0f;
|
||||
samplerInfo.maxLod = 1.0f;
|
||||
samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
||||
VK_CHECK_RESULT(vkCreateSampler(device, &samplerInfo, nullptr, &offscreenPass.sampler));
|
||||
|
||||
// Depth stencil attachment
|
||||
image.format = fbDepthFormat;
|
||||
image.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
|
||||
|
||||
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));
|
||||
|
||||
VkImageViewCreateInfo depthStencilView = vkTools::initializers::imageViewCreateInfo();
|
||||
depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
depthStencilView.format = fbDepthFormat;
|
||||
|
|
@ -277,53 +269,102 @@ public:
|
|||
depthStencilView.subresourceRange.levelCount = 1;
|
||||
depthStencilView.subresourceRange.baseArrayLayer = 0;
|
||||
depthStencilView.subresourceRange.layerCount = 1;
|
||||
depthStencilView.image = offscreenPass.depth.image;
|
||||
VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &offscreenPass.depth.view));
|
||||
|
||||
VK_CHECK_RESULT(vkCreateImage(device, &image, nullptr, &offScreenFrameBuf.depth.image));
|
||||
vkGetImageMemoryRequirements(device, offScreenFrameBuf.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, &offScreenFrameBuf.depth.mem));
|
||||
VK_CHECK_RESULT(vkBindImageMemory(device, offScreenFrameBuf.depth.image, offScreenFrameBuf.depth.mem, 0));
|
||||
// Create a separate render pass for the offscreen rendering as it may differ from the one used for scene rendering
|
||||
|
||||
vkTools::setImageLayout(
|
||||
layoutCmd,
|
||||
offScreenFrameBuf.depth.image,
|
||||
VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
|
||||
VK_IMAGE_LAYOUT_UNDEFINED,
|
||||
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
||||
std::array<VkAttachmentDescription, 2> attchmentDescriptions = {};
|
||||
// Color attachment
|
||||
attchmentDescriptions[0].format = FB_COLOR_FORMAT;
|
||||
attchmentDescriptions[0].samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
attchmentDescriptions[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||
attchmentDescriptions[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||
attchmentDescriptions[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||
attchmentDescriptions[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||
attchmentDescriptions[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
attchmentDescriptions[0].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
// Depth attachment
|
||||
attchmentDescriptions[1].format = fbDepthFormat;
|
||||
attchmentDescriptions[1].samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
attchmentDescriptions[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||
attchmentDescriptions[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||
attchmentDescriptions[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||
attchmentDescriptions[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||
attchmentDescriptions[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
attchmentDescriptions[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
||||
|
||||
// Submit the command buffer to apply the image memory barrier
|
||||
VulkanExampleBase::flushCommandBuffer(layoutCmd, queue, true);
|
||||
VkAttachmentReference colorReference = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL };
|
||||
VkAttachmentReference depthReference = { 1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL };
|
||||
|
||||
depthStencilView.image = offScreenFrameBuf.depth.image;
|
||||
VK_CHECK_RESULT(vkCreateImageView(device, &depthStencilView, nullptr, &offScreenFrameBuf.depth.view));
|
||||
VkSubpassDescription subpassDescription = {};
|
||||
subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
||||
subpassDescription.colorAttachmentCount = 1;
|
||||
subpassDescription.pColorAttachments = &colorReference;
|
||||
subpassDescription.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;
|
||||
|
||||
// Create the actual renderpass
|
||||
VkRenderPassCreateInfo renderPassInfo = {};
|
||||
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
||||
renderPassInfo.attachmentCount = static_cast<uint32_t>(attchmentDescriptions.size());
|
||||
renderPassInfo.pAttachments = attchmentDescriptions.data();
|
||||
renderPassInfo.subpassCount = 1;
|
||||
renderPassInfo.pSubpasses = &subpassDescription;
|
||||
renderPassInfo.dependencyCount = static_cast<uint32_t>(dependencies.size());
|
||||
renderPassInfo.pDependencies = dependencies.data();
|
||||
|
||||
VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassInfo, nullptr, &offscreenPass.renderPass));
|
||||
|
||||
VkImageView attachments[2];
|
||||
attachments[0] = offScreenFrameBuf.color.view;
|
||||
attachments[1] = offScreenFrameBuf.depth.view;
|
||||
attachments[0] = offscreenPass.color.view;
|
||||
attachments[1] = offscreenPass.depth.view;
|
||||
|
||||
VkFramebufferCreateInfo fbufCreateInfo = vkTools::initializers::framebufferCreateInfo();
|
||||
fbufCreateInfo.renderPass = renderPass;
|
||||
fbufCreateInfo.renderPass = offscreenPass.renderPass;
|
||||
fbufCreateInfo.attachmentCount = 2;
|
||||
fbufCreateInfo.pAttachments = attachments;
|
||||
fbufCreateInfo.width = offScreenFrameBuf.width;
|
||||
fbufCreateInfo.height = offScreenFrameBuf.height;
|
||||
fbufCreateInfo.width = offscreenPass.width;
|
||||
fbufCreateInfo.height = offscreenPass.height;
|
||||
fbufCreateInfo.layers = 1;
|
||||
|
||||
VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &offScreenFrameBuf.frameBuffer));
|
||||
VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &offscreenPass.frameBuffer));
|
||||
|
||||
// Fill a descriptor for later use in a descriptor set
|
||||
offscreenPass.descriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
offscreenPass.descriptor.imageView = offscreenPass.color.view;
|
||||
offscreenPass.descriptor.sampler = offscreenPass.sampler;
|
||||
}
|
||||
|
||||
// Sets up the command buffer that renders the scene to the offscreen frame buffer
|
||||
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);
|
||||
}
|
||||
|
||||
// Create a semaphore used to synchronize offscreen rendering and usage
|
||||
VkSemaphoreCreateInfo semaphoreCreateInfo = vkTools::initializers::semaphoreCreateInfo();
|
||||
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &offscreenSemaphore));
|
||||
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &offscreenPass.semaphore));
|
||||
|
||||
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
|
||||
|
||||
|
|
@ -332,51 +373,35 @@ public:
|
|||
clearValues[1].depthStencil = { 1.0f, 0 };
|
||||
|
||||
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
|
||||
renderPassBeginInfo.renderPass = renderPass;
|
||||
renderPassBeginInfo.framebuffer = offScreenFrameBuf.frameBuffer;
|
||||
renderPassBeginInfo.renderArea.extent.width = offScreenFrameBuf.width;
|
||||
renderPassBeginInfo.renderArea.extent.height = offScreenFrameBuf.height;
|
||||
renderPassBeginInfo.renderPass = offscreenPass.renderPass;
|
||||
renderPassBeginInfo.framebuffer = offscreenPass.frameBuffer;
|
||||
renderPassBeginInfo.renderArea.extent.width = offscreenPass.width;
|
||||
renderPassBeginInfo.renderArea.extent.height = offscreenPass.height;
|
||||
renderPassBeginInfo.clearValueCount = 2;
|
||||
renderPassBeginInfo.pClearValues = clearValues;
|
||||
|
||||
VK_CHECK_RESULT(vkBeginCommandBuffer(offScreenCmdBuffer, &cmdBufInfo));
|
||||
VK_CHECK_RESULT(vkBeginCommandBuffer(offscreenPass.commandBuffer, &cmdBufInfo));
|
||||
|
||||
// Change back layout of the color attachment after sampling in the fragment shader
|
||||
vkTools::setImageLayout(
|
||||
offScreenCmdBuffer,
|
||||
offScreenFrameBuf.color.image,
|
||||
VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
|
||||
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||
vkCmdBeginRenderPass(offscreenPass.commandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||
|
||||
vkCmdBeginRenderPass(offScreenCmdBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
||||
VkViewport viewport = vkTools::initializers::viewport((float)offscreenPass.width, (float)offscreenPass.height, 0.0f, 1.0f);
|
||||
vkCmdSetViewport(offscreenPass.commandBuffer, 0, 1, &viewport);
|
||||
|
||||
VkViewport viewport = vkTools::initializers::viewport((float)offScreenFrameBuf.width, (float)offScreenFrameBuf.height, 0.0f, 1.0f);
|
||||
vkCmdSetViewport(offScreenCmdBuffer, 0, 1, &viewport);
|
||||
|
||||
VkRect2D scissor = vkTools::initializers::rect2D(offScreenFrameBuf.width, offScreenFrameBuf.height, 0, 0);
|
||||
vkCmdSetScissor(offScreenCmdBuffer, 0, 1, &scissor);
|
||||
VkRect2D scissor = vkTools::initializers::rect2D(offscreenPass.width, offscreenPass.height, 0, 0);
|
||||
vkCmdSetScissor(offscreenPass.commandBuffer, 0, 1, &scissor);
|
||||
|
||||
VkDeviceSize offsets[1] = { 0 };
|
||||
|
||||
// Mirrored scene
|
||||
vkCmdBindDescriptorSets(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.shaded, 0, 1, &descriptorSets.offscreen, 0, NULL);
|
||||
vkCmdBindPipeline(offScreenCmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.shadedOffscreen);
|
||||
vkCmdBindVertexBuffers(offScreenCmdBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.example.vertices.buf, offsets);
|
||||
vkCmdBindIndexBuffer(offScreenCmdBuffer, meshes.example.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
||||
vkCmdDrawIndexed(offScreenCmdBuffer, meshes.example.indexCount, 1, 0, 0, 0);
|
||||
vkCmdBindDescriptorSets(offscreenPass.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.shaded, 0, 1, &descriptorSets.offscreen, 0, NULL);
|
||||
vkCmdBindPipeline(offscreenPass.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.shadedOffscreen);
|
||||
vkCmdBindVertexBuffers(offscreenPass.commandBuffer, VERTEX_BUFFER_BIND_ID, 1, &meshes.example.vertices.buf, offsets);
|
||||
vkCmdBindIndexBuffer(offscreenPass.commandBuffer, meshes.example.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
||||
vkCmdDrawIndexed(offscreenPass.commandBuffer, meshes.example.indexCount, 1, 0, 0, 0);
|
||||
|
||||
vkCmdEndRenderPass(offScreenCmdBuffer);
|
||||
vkCmdEndRenderPass(offscreenPass.commandBuffer);
|
||||
|
||||
// Change layout of the color attachment for sampling in the fragment shader
|
||||
vkTools::setImageLayout(
|
||||
offScreenCmdBuffer,
|
||||
offScreenFrameBuf.color.image,
|
||||
VK_IMAGE_ASPECT_COLOR_BIT,
|
||||
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
|
||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||
|
||||
VK_CHECK_RESULT(vkEndCommandBuffer(offScreenCmdBuffer));
|
||||
VK_CHECK_RESULT(vkEndCommandBuffer(offscreenPass.commandBuffer));
|
||||
}
|
||||
|
||||
void reBuildCommandBuffers()
|
||||
|
|
@ -457,18 +482,11 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
void loadMeshes()
|
||||
void loadAssets()
|
||||
{
|
||||
loadMesh(getAssetPath() + "models/plane.obj", &meshes.plane, vertexLayout, 0.4f);
|
||||
loadMesh(getAssetPath() + "models/plane.obj", &meshes.plane, vertexLayout, 0.5f);
|
||||
loadMesh(getAssetPath() + "models/chinesedragon.dae", &meshes.example, vertexLayout, 0.3f);
|
||||
}
|
||||
|
||||
void loadTextures()
|
||||
{
|
||||
textureLoader->loadTexture(
|
||||
getAssetPath() + "textures/darkmetal_bc3.ktx",
|
||||
VK_FORMAT_BC3_UNORM_BLOCK,
|
||||
&textures.colorMap);
|
||||
textureLoader->loadTexture(getAssetPath() + "textures/darkmetal_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.colorMap);
|
||||
}
|
||||
|
||||
void generateQuad()
|
||||
|
|
@ -623,20 +641,6 @@ public:
|
|||
|
||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.mirror));
|
||||
|
||||
// Image descriptor for the offscreen mirror color attachment image
|
||||
VkDescriptorImageInfo texDescriptorMirror =
|
||||
vkTools::initializers::descriptorImageInfo(
|
||||
offScreenFrameBuf.colorSampler,
|
||||
offScreenFrameBuf.color.view,
|
||||
VK_IMAGE_LAYOUT_GENERAL);
|
||||
|
||||
// Image descriptor for the color map
|
||||
VkDescriptorImageInfo texDescriptorColorMap =
|
||||
vkTools::initializers::descriptorImageInfo(
|
||||
textures.colorMap.sampler,
|
||||
textures.colorMap.view,
|
||||
VK_IMAGE_LAYOUT_GENERAL);
|
||||
|
||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets =
|
||||
{
|
||||
// Binding 0 : Vertex shader uniform buffer
|
||||
|
|
@ -650,13 +654,13 @@ public:
|
|||
descriptorSets.mirror,
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
1,
|
||||
&texDescriptorMirror),
|
||||
&offscreenPass.descriptor),
|
||||
// Binding 2 : Fragment shader texture sampler
|
||||
vkTools::initializers::writeDescriptorSet(
|
||||
descriptorSets.mirror,
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
2,
|
||||
&texDescriptorColorMap)
|
||||
&textures.colorMap.descriptor)
|
||||
};
|
||||
|
||||
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
|
||||
|
|
@ -677,7 +681,7 @@ public:
|
|||
descriptorSets.debugQuad,
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
||||
1,
|
||||
&texDescriptorMirror)
|
||||
&offscreenPass.descriptor)
|
||||
};
|
||||
vkUpdateDescriptorSets(device, debugQuadWriteDescriptorSets.size(), debugQuadWriteDescriptorSets.data(), 0, NULL);
|
||||
|
||||
|
|
@ -807,6 +811,7 @@ public:
|
|||
// Offscreen
|
||||
// Flip culling
|
||||
rasterizationState.cullMode = VK_CULL_MODE_FRONT_BIT;
|
||||
pipelineCreateInfo.renderPass = offscreenPass.renderPass;
|
||||
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.shadedOffscreen));
|
||||
|
||||
}
|
||||
|
|
@ -934,17 +939,17 @@ public:
|
|||
// Wait for swap chain presentation to finish
|
||||
submitInfo.pWaitSemaphores = &semaphores.presentComplete;
|
||||
// Signal ready with offscreen semaphore
|
||||
submitInfo.pSignalSemaphores = &offscreenSemaphore;
|
||||
submitInfo.pSignalSemaphores = &offscreenPass.semaphore;
|
||||
|
||||
// Submit work
|
||||
submitInfo.commandBufferCount = 1;
|
||||
submitInfo.pCommandBuffers = &offScreenCmdBuffer;
|
||||
submitInfo.pCommandBuffers = &offscreenPass.commandBuffer;
|
||||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||||
|
||||
// Scene rendering
|
||||
|
||||
// Wait for offscreen semaphore
|
||||
submitInfo.pWaitSemaphores = &offscreenSemaphore;
|
||||
submitInfo.pWaitSemaphores = &offscreenPass.semaphore;
|
||||
// Signal ready with render complete semaphpre
|
||||
submitInfo.pSignalSemaphores = &semaphores.renderComplete;
|
||||
|
||||
|
|
@ -958,10 +963,9 @@ public:
|
|||
void prepare()
|
||||
{
|
||||
VulkanExampleBase::prepare();
|
||||
loadTextures();
|
||||
loadAssets();
|
||||
generateQuad();
|
||||
loadMeshes();
|
||||
prepareOffscreenFramebuffer();
|
||||
prepareOffscreen();
|
||||
setupVertexDescriptions();
|
||||
prepareUniformBuffers();
|
||||
setupDescriptorSetLayout();
|
||||
|
|
@ -1019,63 +1023,4 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
VulkanExample *vulkanExample;
|
||||
|
||||
#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
|
||||
}
|
||||
VULKAN_EXAMPLE_MAIN()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue