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