Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrVkRenderTarget.h" |
| 9 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 10 | #include "GrBackendSurface.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | #include "GrRenderTargetPriv.h" |
| 12 | #include "GrVkCommandBuffer.h" |
| 13 | #include "GrVkFramebuffer.h" |
| 14 | #include "GrVkGpu.h" |
| 15 | #include "GrVkImageView.h" |
| 16 | #include "GrVkResourceProvider.h" |
| 17 | #include "GrVkUtil.h" |
| 18 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 19 | #include "vk/GrVkTypes.h" |
| 20 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 21 | #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
| 22 | |
| 23 | // We're virtually derived from GrSurface (via GrRenderTarget) so its |
| 24 | // constructor must be explicitly called. |
| 25 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 26 | SkBudgeted budgeted, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 28 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 29 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 30 | const GrVkImageInfo& msaaInfo, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 31 | sk_sp<GrVkImageLayout> msaaLayout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 32 | const GrVkImageView* colorAttachmentView, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 33 | const GrVkImageView* resolveAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 34 | GrBackendObjectOwnership ownership) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 35 | : GrSurface(gpu, desc) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 36 | , GrVkImage(info, std::move(layout), ownership) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 37 | // for the moment we only support 1:1 color to stencil |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 38 | , GrRenderTarget(gpu, desc) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 39 | , fColorAttachmentView(colorAttachmentView) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 40 | , fMSAAImage(new GrVkImage(msaaInfo, std::move(msaaLayout), GrBackendObjectOwnership::kOwned)) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 41 | , fResolveAttachmentView(resolveAttachmentView) |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 42 | , fFramebuffer(nullptr) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 43 | , fCachedSimpleRenderPass(nullptr) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 44 | SkASSERT(desc.fSampleCnt > 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 45 | this->createFramebuffer(gpu); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 46 | this->registerWithCache(budgeted); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // We're virtually derived from GrSurface (via GrRenderTarget) so its |
| 50 | // constructor must be explicitly called. |
| 51 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
| 52 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 53 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 54 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 55 | const GrVkImageInfo& msaaInfo, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 56 | sk_sp<GrVkImageLayout> msaaLayout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 57 | const GrVkImageView* colorAttachmentView, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 58 | const GrVkImageView* resolveAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 59 | GrBackendObjectOwnership ownership) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 60 | : GrSurface(gpu, desc) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 61 | , GrVkImage(info, std::move(layout), ownership) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 62 | // for the moment we only support 1:1 color to stencil |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 63 | , GrRenderTarget(gpu, desc) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 64 | , fColorAttachmentView(colorAttachmentView) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 65 | , fMSAAImage(new GrVkImage(msaaInfo, std::move(msaaLayout), GrBackendObjectOwnership::kOwned)) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 66 | , fResolveAttachmentView(resolveAttachmentView) |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 67 | , fFramebuffer(nullptr) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 68 | , fCachedSimpleRenderPass(nullptr) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 69 | SkASSERT(desc.fSampleCnt > 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 70 | this->createFramebuffer(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // We're virtually derived from GrSurface (via GrRenderTarget) so its |
| 74 | // constructor must be explicitly called. |
| 75 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 76 | SkBudgeted budgeted, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 77 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 78 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 79 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 80 | const GrVkImageView* colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 81 | GrBackendObjectOwnership ownership) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 82 | : GrSurface(gpu, desc) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 83 | , GrVkImage(info, std::move(layout), ownership) |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 84 | , GrRenderTarget(gpu, desc) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 85 | , fColorAttachmentView(colorAttachmentView) |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 86 | , fMSAAImage(nullptr) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 87 | , fResolveAttachmentView(nullptr) |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 88 | , fFramebuffer(nullptr) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 89 | , fCachedSimpleRenderPass(nullptr) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 90 | SkASSERT(1 == desc.fSampleCnt); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 91 | this->createFramebuffer(gpu); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 92 | this->registerWithCache(budgeted); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // We're virtually derived from GrSurface (via GrRenderTarget) so its |
| 96 | // constructor must be explicitly called. |
| 97 | GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu, |
| 98 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 99 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 100 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 101 | const GrVkImageView* colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 102 | GrBackendObjectOwnership ownership) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 103 | : GrSurface(gpu, desc) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 104 | , GrVkImage(info, std::move(layout), ownership) |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 105 | , GrRenderTarget(gpu, desc) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 106 | , fColorAttachmentView(colorAttachmentView) |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 107 | , fMSAAImage(nullptr) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 108 | , fResolveAttachmentView(nullptr) |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 109 | , fFramebuffer(nullptr) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 110 | , fCachedSimpleRenderPass(nullptr) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 111 | SkASSERT(1 == desc.fSampleCnt); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 112 | this->createFramebuffer(gpu); |
| 113 | } |
| 114 | |
| 115 | GrVkRenderTarget* |
| 116 | GrVkRenderTarget::Create(GrVkGpu* gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 117 | SkBudgeted budgeted, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 118 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 119 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 120 | sk_sp<GrVkImageLayout> layout, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 121 | GrBackendObjectOwnership ownership) { |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 122 | SkASSERT(1 == info.fLevelCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 123 | VkFormat pixelFormat; |
| 124 | GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 125 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 126 | VkImage colorImage; |
| 127 | |
| 128 | // create msaa surface if necessary |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 129 | GrVkImageInfo msInfo; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 130 | sk_sp<GrVkImageLayout> msLayout; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 131 | const GrVkImageView* resolveAttachmentView = nullptr; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 132 | if (desc.fSampleCnt > 1) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | } |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 160 | msLayout.reset(new GrVkImageLayout(msInfo.fImageLayout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 161 | } else { |
| 162 | // Set color attachment image |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 163 | colorImage = info.fImage; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Get color attachment view |
| 167 | const GrVkImageView* colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 168 | GrVkImageView::kColor_Type, 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 169 | if (!colorAttachmentView) { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 170 | if (desc.fSampleCnt > 1) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 171 | resolveAttachmentView->unref(gpu); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 172 | GrVkImage::DestroyImageInfo(gpu, &msInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 173 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 174 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | GrVkRenderTarget* texRT; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 178 | if (desc.fSampleCnt > 1) { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 179 | texRT = new GrVkRenderTarget(gpu, budgeted, desc, info, std::move(layout), msInfo, |
| 180 | std::move(msLayout), colorAttachmentView, |
| 181 | resolveAttachmentView, ownership); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 182 | } else { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 183 | texRT = new GrVkRenderTarget(gpu, budgeted, desc, info, std::move(layout), |
| 184 | colorAttachmentView, ownership); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return texRT; |
| 188 | } |
| 189 | |
| 190 | GrVkRenderTarget* |
| 191 | GrVkRenderTarget::CreateNewRenderTarget(GrVkGpu* gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 192 | SkBudgeted budgeted, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 193 | const GrSurfaceDesc& desc, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 194 | const GrVkImage::ImageDesc& imageDesc) { |
| 195 | SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 196 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 197 | GrVkImageInfo info; |
| 198 | if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 199 | return nullptr; |
| 200 | } |
| 201 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 202 | sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout)); |
| 203 | GrVkRenderTarget* rt = GrVkRenderTarget::Create(gpu, budgeted, desc, info, std::move(layout), |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 204 | GrBackendObjectOwnership::kOwned); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 205 | if (!rt) { |
| 206 | GrVkImage::DestroyImageInfo(gpu, &info); |
| 207 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 208 | return rt; |
| 209 | } |
| 210 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 211 | sk_sp<GrVkRenderTarget> |
| 212 | GrVkRenderTarget::MakeWrappedRenderTarget(GrVkGpu* gpu, |
| 213 | const GrSurfaceDesc& desc, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 214 | const GrVkImageInfo& info, |
| 215 | sk_sp<GrVkImageLayout> layout) { |
| 216 | SkASSERT(VK_NULL_HANDLE != info.fImage); |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 217 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 218 | return sk_sp<GrVkRenderTarget>( |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame^] | 219 | GrVkRenderTarget::Create(gpu, SkBudgeted::kNo, desc, info, std::move(layout), |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 220 | GrBackendObjectOwnership::kBorrowed)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | bool GrVkRenderTarget::completeStencilAttachment() { |
| 224 | this->createFramebuffer(this->getVkGpu()); |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | void GrVkRenderTarget::createFramebuffer(GrVkGpu* gpu) { |
| 229 | if (fFramebuffer) { |
| 230 | fFramebuffer->unref(gpu); |
| 231 | } |
| 232 | if (fCachedSimpleRenderPass) { |
| 233 | fCachedSimpleRenderPass->unref(gpu); |
| 234 | } |
| 235 | |
| 236 | // Vulkan requires us to create a compatible renderpass before we can create our framebuffer, |
| 237 | // so we use this to get a (cached) basic renderpass, only for creation. |
egdaniel | d62e28b | 2016-06-07 08:43:30 -0700 | [diff] [blame] | 238 | fCachedSimpleRenderPass = |
| 239 | gpu->resourceProvider().findCompatibleRenderPass(*this, &fCompatibleRPHandle); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 240 | |
| 241 | // Stencil attachment view is stored in the base RT stencil attachment |
| 242 | const GrVkImageView* stencilView = this->stencilAttachmentView(); |
| 243 | fFramebuffer = GrVkFramebuffer::Create(gpu, this->width(), this->height(), |
| 244 | fCachedSimpleRenderPass, fColorAttachmentView, |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 245 | stencilView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 246 | SkASSERT(fFramebuffer); |
| 247 | } |
| 248 | |
| 249 | void GrVkRenderTarget::getAttachmentsDescriptor( |
| 250 | GrVkRenderPass::AttachmentsDescriptor* desc, |
| 251 | GrVkRenderPass::AttachmentFlags* attachmentFlags) const { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 252 | VkFormat colorFormat; |
| 253 | GrPixelConfigToVkFormat(this->config(), &colorFormat); |
| 254 | desc->fColor.fFormat = colorFormat; |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 255 | desc->fColor.fSamples = this->numColorSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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(); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 263 | desc->fStencil.fSamples = vkStencil->numSamples(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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); |
Greg Daniel | 2db3232 | 2017-04-03 10:29:43 -0400 | [diff] [blame] | 297 | fMSAAImage.reset(); |
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(); |
Greg Daniel | 2db3232 | 2017-04-03 10:29:43 -0400 | [diff] [blame] | 321 | fMSAAImage.reset(); |
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 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 362 | GrBackendRenderTarget GrVkRenderTarget::getBackendRenderTarget() const { |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 363 | int numStencilBits = 0; |
| 364 | if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) { |
| 365 | numStencilBits = stencil->bits(); |
| 366 | } |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 367 | return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(), |
| 368 | numStencilBits, fInfo); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 369 | } |
| 370 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 371 | const GrVkResource* GrVkRenderTarget::stencilImageResource() const { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 372 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 373 | if (stencil) { |
| 374 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 375 | return vkStencil->imageResource(); |
| 376 | } |
| 377 | |
| 378 | return nullptr; |
| 379 | } |
| 380 | |
| 381 | const GrVkImageView* GrVkRenderTarget::stencilAttachmentView() const { |
| 382 | const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 383 | if (stencil) { |
| 384 | const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil); |
| 385 | return vkStencil->stencilView(); |
| 386 | } |
| 387 | |
| 388 | return nullptr; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | GrVkGpu* GrVkRenderTarget::getVkGpu() const { |
| 393 | SkASSERT(!this->wasDestroyed()); |
| 394 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 395 | } |