Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrVkRenderPass.h" |
| 9 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 10 | #include "GrProcessor.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | #include "GrVkFramebuffer.h" |
| 12 | #include "GrVkGpu.h" |
| 13 | #include "GrVkRenderTarget.h" |
| 14 | #include "GrVkUtil.h" |
| 15 | |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 16 | typedef GrVkRenderPass::AttachmentsDescriptor::AttachmentDesc AttachmentDesc; |
| 17 | |
| 18 | void setup_vk_attachment_description(VkAttachmentDescription* attachment, |
| 19 | const AttachmentDesc& desc, |
| 20 | VkImageLayout layout) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 21 | attachment->flags = 0; |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 22 | attachment->format = desc.fFormat; |
| 23 | SkAssertResult(GrSampleCountToVkSampleCount(desc.fSamples, &attachment->samples)); |
| 24 | switch (layout) { |
| 25 | case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL: |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 26 | attachment->loadOp = desc.fLoadStoreOps.fLoadOp; |
| 27 | attachment->storeOp = desc.fLoadStoreOps.fStoreOp; |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 28 | attachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 29 | attachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 30 | break; |
| 31 | case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL: |
| 32 | attachment->loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 33 | attachment->storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 34 | attachment->stencilLoadOp = desc.fLoadStoreOps.fLoadOp; |
| 35 | attachment->stencilStoreOp = desc.fLoadStoreOps.fStoreOp; |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 36 | break; |
| 37 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 38 | SK_ABORT("Unexpected attachment layout"); |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 41 | attachment->initialLayout = layout; |
| 42 | attachment->finalLayout = layout; |
| 43 | } |
| 44 | |
| 45 | void GrVkRenderPass::initSimple(const GrVkGpu* gpu, const GrVkRenderTarget& target) { |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 46 | static const GrVkRenderPass::LoadStoreOps kBasicLoadStoreOps(VK_ATTACHMENT_LOAD_OP_LOAD, |
| 47 | VK_ATTACHMENT_STORE_OP_STORE); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 48 | |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 49 | this->init(gpu, target, kBasicLoadStoreOps, kBasicLoadStoreOps); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void GrVkRenderPass::init(const GrVkGpu* gpu, |
| 53 | const LoadStoreOps& colorOp, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 54 | const LoadStoreOps& stencilOp) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 55 | uint32_t numAttachments = fAttachmentsDescriptor.fAttachmentCount; |
| 56 | // Attachment descriptions to be set on the render pass |
| 57 | SkTArray<VkAttachmentDescription> attachments(numAttachments); |
| 58 | attachments.reset(numAttachments); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 59 | memset(attachments.begin(), 0, numAttachments * sizeof(VkAttachmentDescription)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 60 | |
| 61 | // Refs to attachments on the render pass (as described by teh VkAttachmentDescription above), |
| 62 | // that are used by the subpass. |
| 63 | VkAttachmentReference colorRef; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 64 | VkAttachmentReference stencilRef; |
| 65 | uint32_t currentAttachment = 0; |
| 66 | |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 67 | // Go through each of the attachment types (color, stencil) and set the necessary |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 68 | // on the various Vk structs. |
| 69 | VkSubpassDescription subpassDesc; |
| 70 | memset(&subpassDesc, 0, sizeof(VkSubpassDescription)); |
| 71 | subpassDesc.flags = 0; |
| 72 | subpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; |
| 73 | subpassDesc.inputAttachmentCount = 0; |
| 74 | subpassDesc.pInputAttachments = nullptr; |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 75 | subpassDesc.pResolveAttachments = nullptr; |
| 76 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 77 | if (fAttachmentFlags & kColor_AttachmentFlag) { |
| 78 | // set up color attachment |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 79 | fAttachmentsDescriptor.fColor.fLoadStoreOps = colorOp; |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 80 | setup_vk_attachment_description(&attachments[currentAttachment], |
| 81 | fAttachmentsDescriptor.fColor, |
| 82 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 83 | // setup subpass use of attachment |
| 84 | colorRef.attachment = currentAttachment++; |
| 85 | colorRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 86 | subpassDesc.colorAttachmentCount = 1; |
Greg Daniel | 77a86f8 | 2017-01-23 11:04:45 -0500 | [diff] [blame] | 87 | |
| 88 | if (VK_ATTACHMENT_LOAD_OP_CLEAR == colorOp.fLoadOp) { |
Greg Daniel | b68319a | 2018-02-23 16:08:28 -0500 | [diff] [blame] | 89 | fClearValueCount = colorRef.attachment + 1; |
Greg Daniel | 77a86f8 | 2017-01-23 11:04:45 -0500 | [diff] [blame] | 90 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 91 | } else { |
| 92 | // I don't think there should ever be a time where we don't have a color attachment |
| 93 | SkASSERT(false); |
| 94 | colorRef.attachment = VK_ATTACHMENT_UNUSED; |
| 95 | colorRef.layout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 96 | subpassDesc.colorAttachmentCount = 0; |
| 97 | } |
| 98 | subpassDesc.pColorAttachments = &colorRef; |
| 99 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 100 | if (fAttachmentFlags & kStencil_AttachmentFlag) { |
| 101 | // set up stencil attachment |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 102 | fAttachmentsDescriptor.fStencil.fLoadStoreOps = stencilOp; |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 103 | setup_vk_attachment_description(&attachments[currentAttachment], |
| 104 | fAttachmentsDescriptor.fStencil, |
| 105 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 106 | // setup subpass use of attachment |
| 107 | stencilRef.attachment = currentAttachment++; |
| 108 | stencilRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; |
Greg Daniel | 77a86f8 | 2017-01-23 11:04:45 -0500 | [diff] [blame] | 109 | if (VK_ATTACHMENT_LOAD_OP_CLEAR == stencilOp.fLoadOp) { |
Greg Daniel | b68319a | 2018-02-23 16:08:28 -0500 | [diff] [blame] | 110 | fClearValueCount = SkTMax(fClearValueCount, stencilRef.attachment + 1); |
Greg Daniel | 77a86f8 | 2017-01-23 11:04:45 -0500 | [diff] [blame] | 111 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 112 | } else { |
| 113 | stencilRef.attachment = VK_ATTACHMENT_UNUSED; |
| 114 | stencilRef.layout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 115 | } |
| 116 | subpassDesc.pDepthStencilAttachment = &stencilRef; |
| 117 | |
| 118 | subpassDesc.preserveAttachmentCount = 0; |
| 119 | subpassDesc.pPreserveAttachments = nullptr; |
| 120 | |
| 121 | SkASSERT(numAttachments == currentAttachment); |
| 122 | |
| 123 | // Create the VkRenderPass compatible with the attachment descriptions above |
| 124 | VkRenderPassCreateInfo createInfo; |
| 125 | memset(&createInfo, 0, sizeof(VkRenderPassCreateInfo)); |
| 126 | createInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
| 127 | createInfo.pNext = nullptr; |
| 128 | createInfo.flags = 0; |
| 129 | createInfo.attachmentCount = numAttachments; |
| 130 | createInfo.pAttachments = attachments.begin(); |
| 131 | createInfo.subpassCount = 1; |
| 132 | createInfo.pSubpasses = &subpassDesc; |
| 133 | createInfo.dependencyCount = 0; |
| 134 | createInfo.pDependencies = nullptr; |
| 135 | |
| 136 | GR_VK_CALL_ERRCHECK(gpu->vkInterface(), CreateRenderPass(gpu->device(), |
| 137 | &createInfo, |
| 138 | nullptr, |
| 139 | &fRenderPass)); |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 140 | |
| 141 | // Get granularity for this render pass |
| 142 | GR_VK_CALL(gpu->vkInterface(), GetRenderAreaGranularity(gpu->device(), |
| 143 | fRenderPass, |
| 144 | &fGranularity)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 145 | } |
| 146 | |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 147 | void GrVkRenderPass::init(const GrVkGpu* gpu, |
| 148 | const GrVkRenderPass& compatibleRenderPass, |
| 149 | const LoadStoreOps& colorOp, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 150 | const LoadStoreOps& stencilOp) { |
| 151 | fAttachmentFlags = compatibleRenderPass.fAttachmentFlags; |
| 152 | fAttachmentsDescriptor = compatibleRenderPass.fAttachmentsDescriptor; |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 153 | this->init(gpu, colorOp, stencilOp); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void GrVkRenderPass::init(const GrVkGpu* gpu, |
Greg Daniel | 77a86f8 | 2017-01-23 11:04:45 -0500 | [diff] [blame] | 157 | const GrVkRenderTarget& target, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 158 | const LoadStoreOps& colorOp, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 159 | const LoadStoreOps& stencilOp) { |
| 160 | // Get attachment information from render target. This includes which attachments the render |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 161 | // target has (color, stencil) and the attachments format and sample count. |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 162 | target.getAttachmentsDescriptor(&fAttachmentsDescriptor, &fAttachmentFlags); |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 163 | this->init(gpu, colorOp, stencilOp); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 166 | void GrVkRenderPass::freeGPUData(GrVkGpu* gpu) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 167 | if (!(fAttachmentFlags & kExternal_AttachmentFlag)) { |
| 168 | GR_VK_CALL(gpu->vkInterface(), DestroyRenderPass(gpu->device(), fRenderPass, nullptr)); |
| 169 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 172 | bool GrVkRenderPass::colorAttachmentIndex(uint32_t* index) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 173 | *index = fColorAttachmentIndex; |
| 174 | if ((fAttachmentFlags & kColor_AttachmentFlag) || |
| 175 | (fAttachmentFlags & kExternal_AttachmentFlag)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 181 | // Works under the assumption that stencil attachment will always be after the color and resolve |
| 182 | // attachment. |
| 183 | bool GrVkRenderPass::stencilAttachmentIndex(uint32_t* index) const { |
| 184 | *index = 0; |
| 185 | if (fAttachmentFlags & kColor_AttachmentFlag) { |
| 186 | ++(*index); |
| 187 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 188 | if (fAttachmentFlags & kStencil_AttachmentFlag) { |
| 189 | return true; |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 194 | bool GrVkRenderPass::isCompatible(const AttachmentsDescriptor& desc, |
| 195 | const AttachmentFlags& flags) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 196 | SkASSERT(!(fAttachmentFlags & kExternal_AttachmentFlag)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 197 | if (flags != fAttachmentFlags) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | if (fAttachmentFlags & kColor_AttachmentFlag) { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 202 | if (!fAttachmentsDescriptor.fColor.isCompatible(desc.fColor)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 206 | if (fAttachmentFlags & kStencil_AttachmentFlag) { |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 207 | if (!fAttachmentsDescriptor.fStencil.isCompatible(desc.fStencil)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 214 | |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 215 | bool GrVkRenderPass::isCompatible(const GrVkRenderTarget& target) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 216 | SkASSERT(!(fAttachmentFlags & kExternal_AttachmentFlag)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 217 | AttachmentsDescriptor desc; |
| 218 | AttachmentFlags flags; |
| 219 | target.getAttachmentsDescriptor(&desc, &flags); |
| 220 | |
| 221 | return this->isCompatible(desc, flags); |
| 222 | } |
| 223 | |
| 224 | bool GrVkRenderPass::isCompatible(const GrVkRenderPass& renderPass) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 225 | SkASSERT(!(fAttachmentFlags & kExternal_AttachmentFlag)); |
egdaniel | 9a6cf80 | 2016-06-08 08:22:05 -0700 | [diff] [blame] | 226 | return this->isCompatible(renderPass.fAttachmentsDescriptor, renderPass.fAttachmentFlags); |
| 227 | } |
| 228 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 229 | bool GrVkRenderPass::isCompatibleExternalRP(VkRenderPass renderPass) const { |
| 230 | SkASSERT(fAttachmentFlags & kExternal_AttachmentFlag); |
| 231 | return fRenderPass == renderPass; |
| 232 | } |
| 233 | |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 234 | bool GrVkRenderPass::equalLoadStoreOps(const LoadStoreOps& colorOps, |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 235 | const LoadStoreOps& stencilOps) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 236 | SkASSERT(!(fAttachmentFlags & kExternal_AttachmentFlag)); |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 237 | if (fAttachmentFlags & kColor_AttachmentFlag) { |
| 238 | if (fAttachmentsDescriptor.fColor.fLoadStoreOps != colorOps) { |
| 239 | return false; |
| 240 | } |
| 241 | } |
egdaniel | 2feb093 | 2016-06-08 06:48:09 -0700 | [diff] [blame] | 242 | if (fAttachmentFlags & kStencil_AttachmentFlag) { |
| 243 | if (fAttachmentsDescriptor.fStencil.fLoadStoreOps != stencilOps) { |
| 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | return true; |
| 248 | } |
| 249 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 250 | void GrVkRenderPass::genKey(GrProcessorKeyBuilder* b) const { |
| 251 | b->add32(fAttachmentFlags); |
| 252 | if (fAttachmentFlags & kColor_AttachmentFlag) { |
| 253 | b->add32(fAttachmentsDescriptor.fColor.fFormat); |
| 254 | b->add32(fAttachmentsDescriptor.fColor.fSamples); |
| 255 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 256 | if (fAttachmentFlags & kStencil_AttachmentFlag) { |
| 257 | b->add32(fAttachmentsDescriptor.fStencil.fFormat); |
| 258 | b->add32(fAttachmentsDescriptor.fStencil.fSamples); |
| 259 | } |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 260 | if (fAttachmentFlags & kExternal_AttachmentFlag) { |
| 261 | SkASSERT(!(fAttachmentFlags & ~kExternal_AttachmentFlag)); |
| 262 | uint64_t handle = (uint64_t)fRenderPass; |
Brian Osman | 09a66ac | 2019-02-05 09:59:50 -0500 | [diff] [blame] | 263 | b->add32((uint32_t)(handle & 0xFFFFFFFF)); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 264 | b->add32((uint32_t)(handle>>32)); |
| 265 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 266 | } |