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 | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 34 | : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, 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 | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 37 | , GrRenderTarget(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, 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 | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 62 | : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, 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 | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 65 | , GrRenderTarget(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCnt, info.fProtected) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 66 | , fColorAttachmentView(colorAttachmentView) |
Brian Salomon | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 67 | , fMSAAImage( |
| 68 | new GrVkImage(msaaInfo, std::move(msaaLayout), GrBackendObjectOwnership::kOwned)) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 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 | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 84 | : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, info.fProtected) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 85 | , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kBorrowed) |
Brian Salomon | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 86 | , GrRenderTarget(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, 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 | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 104 | : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, info.fProtected) |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 105 | , GrVkImage(info, std::move(layout), ownership) |
Brian Salomon | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 106 | , GrRenderTarget(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, 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 | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 120 | VkCommandBuffer secondaryCommandBuffer) |
Brian Salomon | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 121 | : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, 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 | a9c2257 | 2019-08-05 12:57:09 -0400 | [diff] [blame] | 123 | , GrRenderTarget(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, 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) { |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 130 | SkASSERT(fSecondaryCommandBuffer != VK_NULL_HANDLE); |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 131 | this->registerWithCacheWrapped(GrWrapCacheable::kNo); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 134 | sk_sp<GrVkRenderTarget> GrVkRenderTarget::MakeWrappedRenderTarget(GrVkGpu* gpu, |
| 135 | const GrSurfaceDesc& desc, |
| 136 | int sampleCnt, |
| 137 | const GrVkImageInfo& info, |
| 138 | sk_sp<GrVkImageLayout> layout) { |
Greg Daniel | 001c67f | 2018-06-26 13:51:57 -0400 | [diff] [blame] | 139 | SkASSERT(VK_NULL_HANDLE != info.fImage); |
| 140 | |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 141 | SkASSERT(1 == info.fLevelCount); |
Greg Daniel | 0fac869 | 2019-08-16 13:13:17 -0400 | [diff] [blame] | 142 | VkFormat pixelFormat = info.fFormat; |
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 | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 227 | if (vkInfo.fSecondaryCommandBuffer == VK_NULL_HANDLE) { |
Greg Daniel | 070cbaf | 2019-01-03 17:35:54 -0500 | [diff] [blame] | 228 | return nullptr; |
| 229 | } |
| 230 | |
Greg Daniel | 8daf3b7 | 2019-07-30 09:57:26 -0400 | [diff] [blame] | 231 | GrVkRenderTarget* vkRT = new GrVkRenderTarget(gpu, desc, info, std::move(layout), rp, |
| 232 | vkInfo.fSecondaryCommandBuffer); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 233 | |
| 234 | return sk_sp<GrVkRenderTarget>(vkRT); |
| 235 | } |
| 236 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 237 | bool GrVkRenderTarget::completeStencilAttachment() { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 238 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 239 | this->createFramebuffer(this->getVkGpu()); |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | void GrVkRenderTarget::createFramebuffer(GrVkGpu* gpu) { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 244 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 245 | if (fFramebuffer) { |
| 246 | fFramebuffer->unref(gpu); |
| 247 | } |
| 248 | if (fCachedSimpleRenderPass) { |
| 249 | fCachedSimpleRenderPass->unref(gpu); |
| 250 | } |
| 251 | |
| 252 | // Vulkan requires us to create a compatible renderpass before we can create our framebuffer, |
| 253 | // so we use this to get a (cached) basic renderpass, only for creation. |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 254 | fCachedSimpleRenderPass = |
| 255 | gpu->resourceProvider().findCompatibleRenderPass(*this, &fCompatibleRPHandle); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 256 | |
| 257 | // Stencil attachment view is stored in the base RT stencil attachment |
| 258 | const GrVkImageView* stencilView = this->stencilAttachmentView(); |
| 259 | fFramebuffer = GrVkFramebuffer::Create(gpu, this->width(), this->height(), |
| 260 | fCachedSimpleRenderPass, fColorAttachmentView, |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 261 | stencilView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 262 | SkASSERT(fFramebuffer); |
| 263 | } |
| 264 | |
| 265 | void GrVkRenderTarget::getAttachmentsDescriptor( |
| 266 | GrVkRenderPass::AttachmentsDescriptor* desc, |
| 267 | GrVkRenderPass::AttachmentFlags* attachmentFlags) const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 268 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Brian Salomon | 1c53a9f | 2019-08-12 14:10:12 -0400 | [diff] [blame] | 269 | desc->fColor.fFormat = this->imageFormat(); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 270 | desc->fColor.fSamples = this->numSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 271 | *attachmentFlags = GrVkRenderPass::kColor_AttachmentFlag; |
| 272 | uint32_t attachmentCount = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 273 | |
| 274 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 275 | if (stencil) { |
| 276 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 277 | desc->fStencil.fFormat = vkStencil->vkFormat(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 278 | desc->fStencil.fSamples = vkStencil->numSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 279 | // Currently in vulkan stencil and color attachments must all have same number of samples |
| 280 | SkASSERT(desc->fColor.fSamples == desc->fStencil.fSamples); |
| 281 | *attachmentFlags |= GrVkRenderPass::kStencil_AttachmentFlag; |
| 282 | ++attachmentCount; |
| 283 | } |
| 284 | desc->fAttachmentCount = attachmentCount; |
| 285 | } |
| 286 | |
| 287 | GrVkRenderTarget::~GrVkRenderTarget() { |
| 288 | // 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] | 289 | SkASSERT(!fMSAAImage); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 290 | SkASSERT(!fResolveAttachmentView); |
| 291 | SkASSERT(!fColorAttachmentView); |
| 292 | SkASSERT(!fFramebuffer); |
| 293 | SkASSERT(!fCachedSimpleRenderPass); |
| 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 | } |
| 331 | } |
| 332 | |
| 333 | void GrVkRenderTarget::abandonInternalObjects() { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 334 | if (fMSAAImage) { |
| 335 | fMSAAImage->abandonImage(); |
Greg Daniel | 2db3232 | 2017-04-03 10:29:43 -0400 | [diff] [blame] | 336 | fMSAAImage.reset(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (fResolveAttachmentView) { |
| 340 | fResolveAttachmentView->unrefAndAbandon(); |
| 341 | fResolveAttachmentView = nullptr; |
| 342 | } |
| 343 | if (fColorAttachmentView) { |
| 344 | fColorAttachmentView->unrefAndAbandon(); |
| 345 | fColorAttachmentView = nullptr; |
| 346 | } |
| 347 | if (fFramebuffer) { |
| 348 | fFramebuffer->unrefAndAbandon(); |
| 349 | fFramebuffer = nullptr; |
| 350 | } |
| 351 | if (fCachedSimpleRenderPass) { |
| 352 | fCachedSimpleRenderPass->unrefAndAbandon(); |
| 353 | fCachedSimpleRenderPass = nullptr; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void GrVkRenderTarget::onRelease() { |
| 358 | this->releaseInternalObjects(); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 359 | this->releaseImage(this->getVkGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 360 | GrRenderTarget::onRelease(); |
| 361 | } |
| 362 | |
| 363 | void GrVkRenderTarget::onAbandon() { |
| 364 | this->abandonInternalObjects(); |
| 365 | this->abandonImage(); |
| 366 | GrRenderTarget::onAbandon(); |
| 367 | } |
| 368 | |
| 369 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 370 | GrBackendRenderTarget GrVkRenderTarget::getBackendRenderTarget() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 371 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Brian Salomon | 4456a0d | 2019-07-18 15:05:11 -0400 | [diff] [blame] | 372 | return GrBackendRenderTarget(this->width(), this->height(), this->numSamples(), fInfo, |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 373 | this->grVkImageLayout()); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 374 | } |
| 375 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 376 | const GrVkResource* GrVkRenderTarget::stencilImageResource() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 377 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 378 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 379 | if (stencil) { |
| 380 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 381 | return vkStencil->imageResource(); |
| 382 | } |
| 383 | |
| 384 | return nullptr; |
| 385 | } |
| 386 | |
| 387 | const GrVkImageView* GrVkRenderTarget::stencilAttachmentView() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 388 | SkASSERT(!this->wrapsSecondaryCommandBuffer()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 389 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 390 | if (stencil) { |
| 391 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 392 | return vkStencil->stencilView(); |
| 393 | } |
| 394 | |
| 395 | return nullptr; |
| 396 | } |
| 397 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 398 | GrVkGpu* GrVkRenderTarget::getVkGpu() const { |
| 399 | SkASSERT(!this->wasDestroyed()); |
| 400 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 401 | } |