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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkRenderTarget.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrBackendSurface.h" |
| 11 | #include "src/gpu/GrRenderTargetPriv.h" |
| 12 | #include "src/gpu/vk/GrVkCommandBuffer.h" |
| 13 | #include "src/gpu/vk/GrVkFramebuffer.h" |
| 14 | #include "src/gpu/vk/GrVkGpu.h" |
| 15 | #include "src/gpu/vk/GrVkImageView.h" |
| 16 | #include "src/gpu/vk/GrVkResourceProvider.h" |
| 17 | #include "src/gpu/vk/GrVkUtil.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/gpu/vk/GrVkTypes.h" |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 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); |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 45 | this->registerWithCacheWrapped(GrWrapCacheable::kNo); |
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); |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 90 | this->registerWithCacheWrapped(GrWrapCacheable::kNo); |
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) { |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 128 | this->registerWithCacheWrapped(GrWrapCacheable::kNo); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 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; |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 159 | msImageDesc.fIsProtected = desc.fIsProtected; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 160 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 161 | if (!GrVkImage::InitImageInfo(gpu, msImageDesc, &msInfo)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 162 | return nullptr; |
| 163 | } |
| 164 | |
| 165 | // Set color attachment image |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 166 | colorImage = msInfo.fImage; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 167 | |
| 168 | // Create Resolve attachment view |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 169 | resolveAttachmentView = GrVkImageView::Create(gpu, info.fImage, pixelFormat, |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 170 | GrVkImageView::kColor_Type, 1, |
| 171 | GrVkYcbcrConversionInfo()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 172 | if (!resolveAttachmentView) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 173 | GrVkImage::DestroyImageInfo(gpu, &msInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 174 | return nullptr; |
| 175 | } |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 176 | msLayout.reset(new GrVkImageLayout(msInfo.fImageLayout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 177 | } else { |
| 178 | // Set color attachment image |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 179 | colorImage = info.fImage; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Get color attachment view |
| 183 | const GrVkImageView* colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat, |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 184 | GrVkImageView::kColor_Type, 1, |
| 185 | GrVkYcbcrConversionInfo()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 186 | if (!colorAttachmentView) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 187 | if (desc.fSampleCnt > 1) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 188 | resolveAttachmentView->unref(gpu); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 189 | GrVkImage::DestroyImageInfo(gpu, &msInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 190 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 191 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 194 | GrVkRenderTarget* vkRT; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 195 | if (desc.fSampleCnt > 1) { |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 196 | vkRT = new GrVkRenderTarget(gpu, desc, info, std::move(layout), msInfo, |
| 197 | std::move(msLayout), colorAttachmentView, |
| 198 | resolveAttachmentView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 199 | } else { |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 200 | vkRT = new GrVkRenderTarget(gpu, desc, info, std::move(layout), colorAttachmentView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 203 | return sk_sp<GrVkRenderTarget>(vkRT); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 206 | sk_sp<GrVkRenderTarget> GrVkRenderTarget::MakeSecondaryCBRenderTarget( |
| 207 | GrVkGpu* gpu, const GrSurfaceDesc& desc, const GrVkDrawableInfo& vkInfo) { |
| 208 | // We only set the few properties of the GrVkImageInfo that we know like layout and format. The |
| 209 | // others we keep at the default "null" values. |
| 210 | GrVkImageInfo info; |
| 211 | info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
| 212 | info.fFormat = vkInfo.fFormat; |
| 213 | |
| 214 | sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)); |
| 215 | |
| 216 | const GrVkRenderPass* rp = |
| 217 | gpu->resourceProvider().findCompatibleExternalRenderPass(vkInfo.fCompatibleRenderPass, |
| 218 | vkInfo.fColorAttachmentIndex); |
| 219 | if (!rp) { |
| 220 | return nullptr; |
| 221 | } |
| 222 | |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 223 | GrVkSecondaryCommandBuffer* scb = |
| 224 | GrVkSecondaryCommandBuffer::Create(vkInfo.fSecondaryCommandBuffer); |
| 225 | if (!scb) { |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
| 229 | GrVkRenderTarget* vkRT = new GrVkRenderTarget(gpu, desc, info, std::move(layout), rp, scb); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 230 | |
| 231 | return sk_sp<GrVkRenderTarget>(vkRT); |
| 232 | } |
| 233 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 234 | bool GrVkRenderTarget::completeStencilAttachment() { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 235 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 236 | this->createFramebuffer(this->getVkGpu()); |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | void GrVkRenderTarget::createFramebuffer(GrVkGpu* gpu) { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 241 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 242 | if (fFramebuffer) { |
| 243 | fFramebuffer->unref(gpu); |
| 244 | } |
| 245 | if (fCachedSimpleRenderPass) { |
| 246 | fCachedSimpleRenderPass->unref(gpu); |
| 247 | } |
| 248 | |
| 249 | // Vulkan requires us to create a compatible renderpass before we can create our framebuffer, |
| 250 | // so we use this to get a (cached) basic renderpass, only for creation. |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 251 | fCachedSimpleRenderPass = |
| 252 | gpu->resourceProvider().findCompatibleRenderPass(*this, &fCompatibleRPHandle); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 253 | |
| 254 | // Stencil attachment view is stored in the base RT stencil attachment |
| 255 | const GrVkImageView* stencilView = this->stencilAttachmentView(); |
| 256 | fFramebuffer = GrVkFramebuffer::Create(gpu, this->width(), this->height(), |
| 257 | fCachedSimpleRenderPass, fColorAttachmentView, |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 258 | stencilView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 259 | SkASSERT(fFramebuffer); |
| 260 | } |
| 261 | |
| 262 | void GrVkRenderTarget::getAttachmentsDescriptor( |
| 263 | GrVkRenderPass::AttachmentsDescriptor* desc, |
| 264 | GrVkRenderPass::AttachmentFlags* attachmentFlags) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 265 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 266 | VkFormat colorFormat; |
| 267 | GrPixelConfigToVkFormat(this->config(), &colorFormat); |
| 268 | desc->fColor.fFormat = colorFormat; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 269 | desc->fColor.fSamples = this->numSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 270 | *attachmentFlags = GrVkRenderPass::kColor_AttachmentFlag; |
| 271 | uint32_t attachmentCount = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 272 | |
| 273 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 274 | if (stencil) { |
| 275 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 276 | desc->fStencil.fFormat = vkStencil->vkFormat(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 277 | desc->fStencil.fSamples = vkStencil->numSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 278 | // Currently in vulkan stencil and color attachments must all have same number of samples |
| 279 | SkASSERT(desc->fColor.fSamples == desc->fStencil.fSamples); |
| 280 | *attachmentFlags |= GrVkRenderPass::kStencil_AttachmentFlag; |
| 281 | ++attachmentCount; |
| 282 | } |
| 283 | desc->fAttachmentCount = attachmentCount; |
| 284 | } |
| 285 | |
| 286 | GrVkRenderTarget::~GrVkRenderTarget() { |
| 287 | // 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] | 288 | SkASSERT(!fMSAAImage); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 289 | SkASSERT(!fResolveAttachmentView); |
| 290 | SkASSERT(!fColorAttachmentView); |
| 291 | SkASSERT(!fFramebuffer); |
| 292 | SkASSERT(!fCachedSimpleRenderPass); |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 293 | SkASSERT(!fSecondaryCommandBuffer); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void GrVkRenderTarget::addResources(GrVkCommandBuffer& commandBuffer) const { |
| 297 | commandBuffer.addResource(this->framebuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 298 | commandBuffer.addResource(this->colorAttachmentView()); |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 299 | commandBuffer.addResource(this->msaaImageResource() ? this->msaaImageResource() |
| 300 | : this->resource()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 301 | if (this->stencilImageResource()) { |
| 302 | commandBuffer.addResource(this->stencilImageResource()); |
| 303 | commandBuffer.addResource(this->stencilAttachmentView()); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void GrVkRenderTarget::releaseInternalObjects() { |
| 308 | GrVkGpu* gpu = this->getVkGpu(); |
| 309 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 310 | if (fMSAAImage) { |
| 311 | fMSAAImage->releaseImage(gpu); |
Greg Daniel | 2db3232 | 2017-04-03 10:29:43 -0400 | [diff] [blame] | 312 | fMSAAImage.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if (fResolveAttachmentView) { |
| 316 | fResolveAttachmentView->unref(gpu); |
| 317 | fResolveAttachmentView = nullptr; |
| 318 | } |
| 319 | if (fColorAttachmentView) { |
| 320 | fColorAttachmentView->unref(gpu); |
| 321 | fColorAttachmentView = nullptr; |
| 322 | } |
| 323 | if (fFramebuffer) { |
| 324 | fFramebuffer->unref(gpu); |
| 325 | fFramebuffer = nullptr; |
| 326 | } |
| 327 | if (fCachedSimpleRenderPass) { |
| 328 | fCachedSimpleRenderPass->unref(gpu); |
| 329 | fCachedSimpleRenderPass = nullptr; |
| 330 | } |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 331 | if (fSecondaryCommandBuffer) { |
| 332 | fSecondaryCommandBuffer->unref(gpu); |
| 333 | fSecondaryCommandBuffer = nullptr; |
| 334 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void GrVkRenderTarget::abandonInternalObjects() { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 338 | if (fMSAAImage) { |
| 339 | fMSAAImage->abandonImage(); |
Greg Daniel | 2db3232 | 2017-04-03 10:29:43 -0400 | [diff] [blame] | 340 | fMSAAImage.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | if (fResolveAttachmentView) { |
| 344 | fResolveAttachmentView->unrefAndAbandon(); |
| 345 | fResolveAttachmentView = nullptr; |
| 346 | } |
| 347 | if (fColorAttachmentView) { |
| 348 | fColorAttachmentView->unrefAndAbandon(); |
| 349 | fColorAttachmentView = nullptr; |
| 350 | } |
| 351 | if (fFramebuffer) { |
| 352 | fFramebuffer->unrefAndAbandon(); |
| 353 | fFramebuffer = nullptr; |
| 354 | } |
| 355 | if (fCachedSimpleRenderPass) { |
| 356 | fCachedSimpleRenderPass->unrefAndAbandon(); |
| 357 | fCachedSimpleRenderPass = nullptr; |
| 358 | } |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 359 | if (fSecondaryCommandBuffer) { |
| 360 | fSecondaryCommandBuffer->unrefAndAbandon(); |
| 361 | fSecondaryCommandBuffer = nullptr; |
| 362 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void GrVkRenderTarget::onRelease() { |
| 366 | this->releaseInternalObjects(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 367 | this->releaseImage(this->getVkGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 368 | GrRenderTarget::onRelease(); |
| 369 | } |
| 370 | |
| 371 | void GrVkRenderTarget::onAbandon() { |
| 372 | this->abandonInternalObjects(); |
| 373 | this->abandonImage(); |
| 374 | GrRenderTarget::onAbandon(); |
| 375 | } |
| 376 | |
| 377 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 378 | GrBackendRenderTarget GrVkRenderTarget::getBackendRenderTarget() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 379 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Brian Salomon | 4456a0d | 2019-07-18 15:05:11 -0400 | [diff] [blame^] | 380 | return GrBackendRenderTarget(this->width(), this->height(), this->numSamples(), fInfo, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 381 | this->grVkImageLayout()); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 382 | } |
| 383 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 384 | const GrVkResource* GrVkRenderTarget::stencilImageResource() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 385 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 386 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 387 | if (stencil) { |
| 388 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 389 | return vkStencil->imageResource(); |
| 390 | } |
| 391 | |
| 392 | return nullptr; |
| 393 | } |
| 394 | |
| 395 | const GrVkImageView* GrVkRenderTarget::stencilAttachmentView() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 396 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 397 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 398 | if (stencil) { |
| 399 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 400 | return vkStencil->stencilView(); |
| 401 | } |
| 402 | |
| 403 | return nullptr; |
| 404 | } |
| 405 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 406 | GrVkGpu* GrVkRenderTarget::getVkGpu() const { |
| 407 | SkASSERT(!this->wasDestroyed()); |
| 408 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 409 | } |