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