From 2f8c9477f24dca472f35f2c08a81893f1168524f Mon Sep 17 00:00:00 2001 From: saschawillems Date: Fri, 12 Aug 2016 20:36:49 +0200 Subject: [PATCH] Fold attachment layout transitions into subpass (Refs #155) --- textoverlay/textoverlay.cpp | 59 +++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/textoverlay/textoverlay.cpp b/textoverlay/textoverlay.cpp index 7433a75c..ddc2d5fb 100644 --- a/textoverlay/textoverlay.cpp +++ b/textoverlay/textoverlay.cpp @@ -477,8 +477,8 @@ public: attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - attachments[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; // Depth attachment attachments[1].format = depthFormat; @@ -487,7 +487,7 @@ public: attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE; attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; + attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; VkAttachmentReference colorReference = {}; @@ -498,17 +498,38 @@ public: depthReference.attachment = 1; depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - VkSubpassDescription subpass = {}; - subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; - subpass.flags = 0; - subpass.inputAttachmentCount = 0; - subpass.pInputAttachments = NULL; - subpass.colorAttachmentCount = 1; - subpass.pColorAttachments = &colorReference; - subpass.pResolveAttachments = NULL; - subpass.pDepthStencilAttachment = &depthReference; - subpass.preserveAttachmentCount = 0; - subpass.pPreserveAttachments = NULL; + // Use subpass dependencies for image layout transitions + VkSubpassDependency subpassDependencies[2] = {}; + + // Transition from final to initial (VK_SUBPASS_EXTERNAL refers to all commmands executed outside of the actual renderpass) + subpassDependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; + subpassDependencies[0].dstSubpass = 0; + subpassDependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; + subpassDependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + subpassDependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT; + subpassDependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + subpassDependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + + // Transition from initial to final + subpassDependencies[1].srcSubpass = 0; + subpassDependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL; + subpassDependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + subpassDependencies[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; + subpassDependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; + subpassDependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; + subpassDependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + + VkSubpassDescription subpassDescription = {}; + subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; + subpassDescription.flags = 0; + subpassDescription.inputAttachmentCount = 0; + subpassDescription.pInputAttachments = NULL; + subpassDescription.colorAttachmentCount = 1; + subpassDescription.pColorAttachments = &colorReference; + subpassDescription.pResolveAttachments = NULL; + subpassDescription.pDepthStencilAttachment = &depthReference; + subpassDescription.preserveAttachmentCount = 0; + subpassDescription.pPreserveAttachments = NULL; VkRenderPassCreateInfo renderPassInfo = {}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; @@ -516,9 +537,9 @@ public: renderPassInfo.attachmentCount = 2; renderPassInfo.pAttachments = attachments; renderPassInfo.subpassCount = 1; - renderPassInfo.pSubpasses = &subpass; - renderPassInfo.dependencyCount = 0; - renderPassInfo.pDependencies = NULL; + renderPassInfo.pSubpasses = &subpassDescription; + renderPassInfo.dependencyCount = 2; + renderPassInfo.pDependencies = subpassDependencies; VK_CHECK_RESULT(vkCreateRenderPass(vulkanDevice->logicalDevice, &renderPassInfo, nullptr, &renderPass)); } @@ -806,7 +827,7 @@ public: textOverlay->addText(deviceProperties.deviceName, 5.0f, 45.0f, TextOverlay::alignLeft); - textOverlay->addText("Press \"space\" to toggle text overlay", 5.0f, height - 20.0f, TextOverlay::alignLeft); + textOverlay->addText("Press \"space\" to toggle text overlay", 5.0f, 65.0f, TextOverlay::alignLeft); // Display projected cube vertices for (int32_t x = -1; x <= 1; x += 2) @@ -840,7 +861,7 @@ public: #if defined(__ANDROID__) // toto #else - textOverlay->addText("Hold middle mouse button and drag to move", 5.0f, height - 40.0f, TextOverlay::alignLeft); + textOverlay->addText("Hold middle mouse button and drag to move", 5.0f, 85.0f, TextOverlay::alignLeft); #endif textOverlay->endTextUpdate(); }