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