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