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 "GrVkRenderTarget.h" |
| 9 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 10 | #include "GrBackendSurface.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | #include "GrRenderTargetPriv.h" |
| 12 | #include "GrVkCommandBuffer.h" |
| 13 | #include "GrVkFramebuffer.h" |
| 14 | #include "GrVkGpu.h" |
| 15 | #include "GrVkImageView.h" |
| 16 | #include "GrVkResourceProvider.h" |
| 17 | #include "GrVkUtil.h" |
| 18 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 19 | #include "vk/GrVkTypes.h" |
| 20 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 21 | #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
| 22 | |
| 23 | // We're virtually derived from GrSurface (via GrRenderTarget) so its |
| 24 | // constructor must be explicitly called. |
| 25 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
| 26 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 27 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 28 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 29 | const GrVkImageInfo& msaaInfo, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 30 | sk_sp<GrVkImageLayout> msaaLayout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 31 | const GrVkImageView* colorAttachmentView, |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 32 | const GrVkImageView* resolveAttachmentView) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 33 | : GrSurface(gpu, desc) |
| 34 | , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kBorrowed) |
| 35 | // for the moment we only support 1:1 color to stencil |
| 36 | , GrRenderTarget(gpu, desc) |
| 37 | , fColorAttachmentView(colorAttachmentView) |
| 38 | , fMSAAImage(new GrVkImage(msaaInfo, std::move(msaaLayout), |
| 39 | GrBackendObjectOwnership::kOwned)) |
| 40 | , fResolveAttachmentView(resolveAttachmentView) |
| 41 | , fFramebuffer(nullptr) |
| 42 | , fCachedSimpleRenderPass(nullptr) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 43 | SkASSERT(desc.fSampleCnt > 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 44 | this->createFramebuffer(gpu); |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 45 | this->registerWithCacheWrapped(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // We're virtually derived from GrSurface (via GrRenderTarget) so its |
| 49 | // constructor must be explicitly called. |
| 50 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
| 51 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 52 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 53 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 54 | const GrVkImageInfo& msaaInfo, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 55 | sk_sp<GrVkImageLayout> msaaLayout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 56 | const GrVkImageView* colorAttachmentView, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 57 | const GrVkImageView* resolveAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 58 | GrBackendObjectOwnership ownership) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 59 | : GrSurface(gpu, desc) |
| 60 | , GrVkImage(info, std::move(layout), ownership) |
| 61 | // for the moment we only support 1:1 color to stencil |
| 62 | , GrRenderTarget(gpu, desc) |
| 63 | , fColorAttachmentView(colorAttachmentView) |
| 64 | , fMSAAImage(new GrVkImage(msaaInfo, std::move(msaaLayout), |
| 65 | GrBackendObjectOwnership::kOwned)) |
| 66 | , fResolveAttachmentView(resolveAttachmentView) |
| 67 | , fFramebuffer(nullptr) |
| 68 | , fCachedSimpleRenderPass(nullptr) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 69 | SkASSERT(desc.fSampleCnt > 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 70 | this->createFramebuffer(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // We're virtually derived from GrSurface (via GrRenderTarget) so its |
| 74 | // constructor must be explicitly called. |
| 75 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
| 76 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 77 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 78 | sk_sp<GrVkImageLayout> layout, |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 79 | const GrVkImageView* colorAttachmentView) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 80 | : GrSurface(gpu, desc) |
| 81 | , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kBorrowed) |
| 82 | , GrRenderTarget(gpu, desc) |
| 83 | , fColorAttachmentView(colorAttachmentView) |
| 84 | , fMSAAImage(nullptr) |
| 85 | , fResolveAttachmentView(nullptr) |
| 86 | , fFramebuffer(nullptr) |
| 87 | , fCachedSimpleRenderPass(nullptr) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 88 | SkASSERT(1 == desc.fSampleCnt); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 89 | this->createFramebuffer(gpu); |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 90 | this->registerWithCacheWrapped(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // We're virtually derived from GrSurface (via GrRenderTarget) so its |
| 94 | // constructor must be explicitly called. |
| 95 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
| 96 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 97 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 98 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 99 | const GrVkImageView* colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 100 | GrBackendObjectOwnership ownership) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 101 | : GrSurface(gpu, desc) |
| 102 | , GrVkImage(info, std::move(layout), ownership) |
| 103 | , GrRenderTarget(gpu, desc) |
| 104 | , fColorAttachmentView(colorAttachmentView) |
| 105 | , fMSAAImage(nullptr) |
| 106 | , fResolveAttachmentView(nullptr) |
| 107 | , fFramebuffer(nullptr) |
| 108 | , fCachedSimpleRenderPass(nullptr) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 109 | SkASSERT(1 == desc.fSampleCnt); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 110 | this->createFramebuffer(gpu); |
| 111 | } |
| 112 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 113 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
| 114 | const GrSurfaceDesc& desc, |
| 115 | const GrVkImageInfo& info, |
| 116 | sk_sp<GrVkImageLayout> layout, |
| 117 | const GrVkRenderPass* renderPass, |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 118 | GrVkSecondaryCommandBuffer* secondaryCommandBuffer) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 119 | : GrSurface(gpu, desc) |
| 120 | , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kBorrowed, true) |
| 121 | , GrRenderTarget(gpu, desc) |
| 122 | , fColorAttachmentView(nullptr) |
| 123 | , fMSAAImage(nullptr) |
| 124 | , fResolveAttachmentView(nullptr) |
| 125 | , fFramebuffer(nullptr) |
| 126 | , fCachedSimpleRenderPass(renderPass) |
| 127 | , fSecondaryCommandBuffer(secondaryCommandBuffer) { |
| 128 | this->registerWithCacheWrapped(); |
| 129 | } |
| 130 | |
| 131 | sk_sp<GrVkRenderTarget> GrVkRenderTarget::MakeWrappedRenderTarget( |
| 132 | GrVkGpu* gpu, const GrSurfaceDesc& desc, const GrVkImageInfo& info, |
| 133 | sk_sp<GrVkImageLayout> layout) { |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 134 | SkASSERT(VK_NULL_HANDLE != info.fImage); |
| 135 | |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 136 | SkASSERT(1 == info.fLevelCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 137 | VkFormat pixelFormat; |
| 138 | GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 139 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 140 | VkImage colorImage; |
| 141 | |
| 142 | // create msaa surface if necessary |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 143 | GrVkImageInfo msInfo; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 144 | sk_sp<GrVkImageLayout> msLayout; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 145 | const GrVkImageView* resolveAttachmentView = nullptr; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 146 | if (desc.fSampleCnt > 1) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 147 | GrVkImage::ImageDesc msImageDesc; |
| 148 | msImageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 149 | msImageDesc.fFormat = pixelFormat; |
| 150 | msImageDesc.fWidth = desc.fWidth; |
| 151 | msImageDesc.fHeight = desc.fHeight; |
| 152 | msImageDesc.fLevels = 1; |
| 153 | msImageDesc.fSamples = desc.fSampleCnt; |
| 154 | msImageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 155 | msImageDesc.fUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 156 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 157 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 158 | msImageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 159 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 160 | if (!GrVkImage::InitImageInfo(gpu, msImageDesc, &msInfo)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 161 | return nullptr; |
| 162 | } |
| 163 | |
| 164 | // Set color attachment image |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 165 | colorImage = msInfo.fImage; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 166 | |
| 167 | // Create Resolve attachment view |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 168 | resolveAttachmentView = GrVkImageView::Create(gpu, info.fImage, pixelFormat, |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 169 | GrVkImageView::kColor_Type, 1, |
| 170 | GrVkYcbcrConversionInfo()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 171 | if (!resolveAttachmentView) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 172 | GrVkImage::DestroyImageInfo(gpu, &msInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 173 | return nullptr; |
| 174 | } |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 175 | msLayout.reset(new GrVkImageLayout(msInfo.fImageLayout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 176 | } else { |
| 177 | // Set color attachment image |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 178 | colorImage = info.fImage; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Get color attachment view |
| 182 | const GrVkImageView* colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat, |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 183 | GrVkImageView::kColor_Type, 1, |
| 184 | GrVkYcbcrConversionInfo()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 185 | if (!colorAttachmentView) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 186 | if (desc.fSampleCnt > 1) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 187 | resolveAttachmentView->unref(gpu); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 188 | GrVkImage::DestroyImageInfo(gpu, &msInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 189 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 190 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 193 | GrVkRenderTarget* vkRT; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 194 | if (desc.fSampleCnt > 1) { |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 195 | vkRT = new GrVkRenderTarget(gpu, desc, info, std::move(layout), msInfo, |
| 196 | std::move(msLayout), colorAttachmentView, |
| 197 | resolveAttachmentView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 198 | } else { |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 199 | vkRT = new GrVkRenderTarget(gpu, desc, info, std::move(layout), colorAttachmentView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 202 | return sk_sp<GrVkRenderTarget>(vkRT); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 205 | sk_sp<GrVkRenderTarget> GrVkRenderTarget::MakeSecondaryCBRenderTarget( |
| 206 | GrVkGpu* gpu, const GrSurfaceDesc& desc, const GrVkDrawableInfo& vkInfo) { |
| 207 | // We only set the few properties of the GrVkImageInfo that we know like layout and format. The |
| 208 | // others we keep at the default "null" values. |
| 209 | GrVkImageInfo info; |
| 210 | info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 211 | info.fFormat = vkInfo.fFormat; |
| 212 | |
| 213 | sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)); |
| 214 | |
| 215 | const GrVkRenderPass* rp = |
| 216 | gpu->resourceProvider().findCompatibleExternalRenderPass(vkInfo.fCompatibleRenderPass, |
| 217 | vkInfo.fColorAttachmentIndex); |
| 218 | if (!rp) { |
| 219 | return nullptr; |
| 220 | } |
| 221 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 222 | GrVkSecondaryCommandBuffer* scb = |
| 223 | GrVkSecondaryCommandBuffer::Create(vkInfo.fSecondaryCommandBuffer); |
| 224 | if (!scb) { |
| 225 | return nullptr; |
| 226 | } |
| 227 | |
| 228 | GrVkRenderTarget* vkRT = new GrVkRenderTarget(gpu, desc, info, std::move(layout), rp, scb); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 229 | |
| 230 | return sk_sp<GrVkRenderTarget>(vkRT); |
| 231 | } |
| 232 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 233 | bool GrVkRenderTarget::completeStencilAttachment() { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 234 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 235 | this->createFramebuffer(this->getVkGpu()); |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | void GrVkRenderTarget::createFramebuffer(GrVkGpu* gpu) { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 240 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 241 | if (fFramebuffer) { |
| 242 | fFramebuffer->unref(gpu); |
| 243 | } |
| 244 | if (fCachedSimpleRenderPass) { |
| 245 | fCachedSimpleRenderPass->unref(gpu); |
| 246 | } |
| 247 | |
| 248 | // Vulkan requires us to create a compatible renderpass before we can create our framebuffer, |
| 249 | // so we use this to get a (cached) basic renderpass, only for creation. |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 250 | fCachedSimpleRenderPass = |
| 251 | gpu->resourceProvider().findCompatibleRenderPass(*this, &fCompatibleRPHandle); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 252 | |
| 253 | // Stencil attachment view is stored in the base RT stencil attachment |
| 254 | const GrVkImageView* stencilView = this->stencilAttachmentView(); |
| 255 | fFramebuffer = GrVkFramebuffer::Create(gpu, this->width(), this->height(), |
| 256 | fCachedSimpleRenderPass, fColorAttachmentView, |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 257 | stencilView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 258 | SkASSERT(fFramebuffer); |
| 259 | } |
| 260 | |
| 261 | void GrVkRenderTarget::getAttachmentsDescriptor( |
| 262 | GrVkRenderPass::AttachmentsDescriptor* desc, |
| 263 | GrVkRenderPass::AttachmentFlags* attachmentFlags) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 264 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 265 | VkFormat colorFormat; |
| 266 | GrPixelConfigToVkFormat(this->config(), &colorFormat); |
| 267 | desc->fColor.fFormat = colorFormat; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 268 | desc->fColor.fSamples = this->numColorSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 269 | *attachmentFlags = GrVkRenderPass::kColor_AttachmentFlag; |
| 270 | uint32_t attachmentCount = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 271 | |
| 272 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 273 | if (stencil) { |
| 274 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 275 | desc->fStencil.fFormat = vkStencil->vkFormat(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 276 | desc->fStencil.fSamples = vkStencil->numSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 277 | // Currently in vulkan stencil and color attachments must all have same number of samples |
| 278 | SkASSERT(desc->fColor.fSamples == desc->fStencil.fSamples); |
| 279 | *attachmentFlags |= GrVkRenderPass::kStencil_AttachmentFlag; |
| 280 | ++attachmentCount; |
| 281 | } |
| 282 | desc->fAttachmentCount = attachmentCount; |
| 283 | } |
| 284 | |
| 285 | GrVkRenderTarget::~GrVkRenderTarget() { |
| 286 | // either release or abandon should have been called by the owner of this object. |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 287 | SkASSERT(!fMSAAImage); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 288 | SkASSERT(!fResolveAttachmentView); |
| 289 | SkASSERT(!fColorAttachmentView); |
| 290 | SkASSERT(!fFramebuffer); |
| 291 | SkASSERT(!fCachedSimpleRenderPass); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 292 | SkASSERT(!fSecondaryCommandBuffer); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void GrVkRenderTarget::addResources(GrVkCommandBuffer& commandBuffer) const { |
| 296 | commandBuffer.addResource(this->framebuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 297 | commandBuffer.addResource(this->colorAttachmentView()); |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 298 | commandBuffer.addResource(this->msaaImageResource() ? this->msaaImageResource() |
| 299 | : this->resource()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 300 | if (this->stencilImageResource()) { |
| 301 | commandBuffer.addResource(this->stencilImageResource()); |
| 302 | commandBuffer.addResource(this->stencilAttachmentView()); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void GrVkRenderTarget::releaseInternalObjects() { |
| 307 | GrVkGpu* gpu = this->getVkGpu(); |
| 308 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 309 | if (fMSAAImage) { |
| 310 | fMSAAImage->releaseImage(gpu); |
Greg Daniel | 2db3232 | 2017-04-03 10:29:43 -0400 | [diff] [blame] | 311 | fMSAAImage.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (fResolveAttachmentView) { |
| 315 | fResolveAttachmentView->unref(gpu); |
| 316 | fResolveAttachmentView = nullptr; |
| 317 | } |
| 318 | if (fColorAttachmentView) { |
| 319 | fColorAttachmentView->unref(gpu); |
| 320 | fColorAttachmentView = nullptr; |
| 321 | } |
| 322 | if (fFramebuffer) { |
| 323 | fFramebuffer->unref(gpu); |
| 324 | fFramebuffer = nullptr; |
| 325 | } |
| 326 | if (fCachedSimpleRenderPass) { |
| 327 | fCachedSimpleRenderPass->unref(gpu); |
| 328 | fCachedSimpleRenderPass = nullptr; |
| 329 | } |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 330 | if (fSecondaryCommandBuffer) { |
| 331 | fSecondaryCommandBuffer->unref(gpu); |
| 332 | fSecondaryCommandBuffer = nullptr; |
| 333 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void GrVkRenderTarget::abandonInternalObjects() { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 337 | if (fMSAAImage) { |
| 338 | fMSAAImage->abandonImage(); |
Greg Daniel | 2db3232 | 2017-04-03 10:29:43 -0400 | [diff] [blame] | 339 | fMSAAImage.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (fResolveAttachmentView) { |
| 343 | fResolveAttachmentView->unrefAndAbandon(); |
| 344 | fResolveAttachmentView = nullptr; |
| 345 | } |
| 346 | if (fColorAttachmentView) { |
| 347 | fColorAttachmentView->unrefAndAbandon(); |
| 348 | fColorAttachmentView = nullptr; |
| 349 | } |
| 350 | if (fFramebuffer) { |
| 351 | fFramebuffer->unrefAndAbandon(); |
| 352 | fFramebuffer = nullptr; |
| 353 | } |
| 354 | if (fCachedSimpleRenderPass) { |
| 355 | fCachedSimpleRenderPass->unrefAndAbandon(); |
| 356 | fCachedSimpleRenderPass = nullptr; |
| 357 | } |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 358 | if (fSecondaryCommandBuffer) { |
| 359 | fSecondaryCommandBuffer->unrefAndAbandon(); |
| 360 | fSecondaryCommandBuffer = nullptr; |
| 361 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void GrVkRenderTarget::onRelease() { |
| 365 | this->releaseInternalObjects(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 366 | this->releaseImage(this->getVkGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 367 | GrRenderTarget::onRelease(); |
| 368 | } |
| 369 | |
| 370 | void GrVkRenderTarget::onAbandon() { |
| 371 | this->abandonInternalObjects(); |
| 372 | this->abandonImage(); |
| 373 | GrRenderTarget::onAbandon(); |
| 374 | } |
| 375 | |
| 376 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 377 | GrBackendRenderTarget GrVkRenderTarget::getBackendRenderTarget() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 378 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 379 | return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(), |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 380 | fInfo, this->grVkImageLayout()); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 381 | } |
| 382 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 383 | const GrVkResource* GrVkRenderTarget::stencilImageResource() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 384 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 385 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 386 | if (stencil) { |
| 387 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 388 | return vkStencil->imageResource(); |
| 389 | } |
| 390 | |
| 391 | return nullptr; |
| 392 | } |
| 393 | |
| 394 | const GrVkImageView* GrVkRenderTarget::stencilAttachmentView() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 395 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 396 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 397 | if (stencil) { |
| 398 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 399 | return vkStencil->stencilView(); |
| 400 | } |
| 401 | |
| 402 | return nullptr; |
| 403 | } |
| 404 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 405 | GrVkGpu* GrVkRenderTarget::getVkGpu() const { |
| 406 | SkASSERT(!this->wasDestroyed()); |
| 407 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 408 | } |