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