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