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