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 "GrVkTextureRenderTarget.h" |
| 9 | |
| 10 | #include "GrRenderTargetPriv.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 11 | #include "GrTexturePriv.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 12 | #include "GrVkGpu.h" |
| 13 | #include "GrVkImageView.h" |
| 14 | #include "GrVkUtil.h" |
| 15 | |
egdaniel | dd97b85 | 2016-04-28 09:30:39 -0700 | [diff] [blame] | 16 | #include "SkMipMap.h" |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 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 | |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 22 | GrVkTextureRenderTarget::GrVkTextureRenderTarget(GrVkGpu* gpu, |
| 23 | SkBudgeted budgeted, |
| 24 | const GrSurfaceDesc& desc, |
| 25 | const GrVkImageInfo& info, |
| 26 | const GrVkImageView* texView, |
| 27 | const GrVkImageInfo& msaaInfo, |
| 28 | const GrVkImageView* colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 29 | const GrVkImageView* resolveAttachmentView, |
| 30 | GrBackendObjectOwnership ownership) |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 31 | : GrSurface(gpu, desc) |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 32 | , GrVkImage(info, ownership) |
| 33 | , GrVkTexture(gpu, desc, info, texView, ownership) |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 34 | , GrVkRenderTarget(gpu, desc, info, msaaInfo, colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 35 | resolveAttachmentView, GrBackendObjectOwnership::kOwned) { |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 36 | this->registerWithCache(budgeted); |
| 37 | } |
| 38 | |
| 39 | GrVkTextureRenderTarget::GrVkTextureRenderTarget(GrVkGpu* gpu, |
| 40 | SkBudgeted budgeted, |
| 41 | const GrSurfaceDesc& desc, |
| 42 | const GrVkImageInfo& info, |
| 43 | const GrVkImageView* texView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 44 | const GrVkImageView* colorAttachmentView, |
| 45 | GrBackendObjectOwnership ownership) |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 46 | : GrSurface(gpu, desc) |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 47 | , GrVkImage(info, ownership) |
| 48 | , GrVkTexture(gpu, desc, info, texView, ownership) |
| 49 | , GrVkRenderTarget(gpu, desc, info, colorAttachmentView, GrBackendObjectOwnership::kOwned) { |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 50 | this->registerWithCache(budgeted); |
| 51 | } |
| 52 | |
| 53 | GrVkTextureRenderTarget::GrVkTextureRenderTarget(GrVkGpu* gpu, |
| 54 | const GrSurfaceDesc& desc, |
| 55 | const GrVkImageInfo& info, |
| 56 | const GrVkImageView* texView, |
| 57 | const GrVkImageInfo& msaaInfo, |
| 58 | const GrVkImageView* colorAttachmentView, |
| 59 | const GrVkImageView* resolveAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 60 | GrBackendObjectOwnership ownership) |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 61 | : GrSurface(gpu, desc) |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 62 | , GrVkImage(info, ownership) |
| 63 | , GrVkTexture(gpu, desc, info, texView, ownership) |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 64 | , GrVkRenderTarget(gpu, desc, info, msaaInfo, colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 65 | resolveAttachmentView, ownership) { |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 66 | this->registerWithCacheWrapped(); |
| 67 | } |
| 68 | |
| 69 | GrVkTextureRenderTarget::GrVkTextureRenderTarget(GrVkGpu* gpu, |
| 70 | const GrSurfaceDesc& desc, |
| 71 | const GrVkImageInfo& info, |
| 72 | const GrVkImageView* texView, |
| 73 | const GrVkImageView* colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 74 | GrBackendObjectOwnership ownership) |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 75 | : GrSurface(gpu, desc) |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 76 | , GrVkImage(info, ownership) |
| 77 | , GrVkTexture(gpu, desc, info, texView, ownership) |
| 78 | , GrVkRenderTarget(gpu, desc, info, colorAttachmentView, ownership) { |
Greg Daniel | 6ecc911 | 2017-06-16 16:17:03 +0000 | [diff] [blame] | 79 | this->registerWithCacheWrapped(); |
| 80 | } |
| 81 | |
| 82 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 83 | sk_sp<GrVkTextureRenderTarget> GrVkTextureRenderTarget::Make(GrVkGpu* gpu, |
| 84 | const GrSurfaceDesc& desc, |
| 85 | const GrVkImageInfo& info, |
| 86 | SkBudgeted budgeted, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 87 | GrBackendObjectOwnership ownership, |
| 88 | bool isWrapped) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 89 | VkImage image = info.fImage; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 90 | // Create the texture ImageView |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 91 | const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, info.fFormat, |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 92 | GrVkImageView::kColor_Type, |
| 93 | info.fLevelCount); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 94 | if (!imageView) { |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | VkFormat pixelFormat; |
| 99 | GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat); |
| 100 | |
| 101 | VkImage colorImage; |
| 102 | |
| 103 | // create msaa surface if necessary |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 104 | GrVkImageInfo msInfo; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | const GrVkImageView* resolveAttachmentView = nullptr; |
| 106 | if (desc.fSampleCnt) { |
| 107 | GrVkImage::ImageDesc msImageDesc; |
| 108 | msImageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 109 | msImageDesc.fFormat = pixelFormat; |
| 110 | msImageDesc.fWidth = desc.fWidth; |
| 111 | msImageDesc.fHeight = desc.fHeight; |
| 112 | msImageDesc.fLevels = 1; |
| 113 | msImageDesc.fSamples = desc.fSampleCnt; |
| 114 | msImageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 115 | msImageDesc.fUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 116 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | |
| 117 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 118 | msImageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 119 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 120 | if (!GrVkImage::InitImageInfo(gpu, msImageDesc, &msInfo)) { |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 121 | imageView->unref(gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 122 | return nullptr; |
| 123 | } |
| 124 | |
| 125 | // Set color attachment image |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 126 | colorImage = msInfo.fImage; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 127 | |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 128 | // Create resolve attachment view. |
| 129 | resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelFormat, |
| 130 | GrVkImageView::kColor_Type, |
| 131 | info.fLevelCount); |
| 132 | if (!resolveAttachmentView) { |
| 133 | GrVkImage::DestroyImageInfo(gpu, &msInfo); |
| 134 | imageView->unref(gpu); |
| 135 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 136 | } |
| 137 | } else { |
| 138 | // Set color attachment image |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 139 | colorImage = info.fImage; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 140 | } |
| 141 | |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 142 | const GrVkImageView* colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat, |
| 143 | GrVkImageView::kColor_Type, 1); |
| 144 | if (!colorAttachmentView) { |
| 145 | if (desc.fSampleCnt) { |
| 146 | resolveAttachmentView->unref(gpu); |
| 147 | GrVkImage::DestroyImageInfo(gpu, &msInfo); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 148 | } |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 149 | imageView->unref(gpu); |
| 150 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 151 | } |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 152 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 153 | sk_sp<GrVkTextureRenderTarget> texRT; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 154 | if (desc.fSampleCnt) { |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 155 | if (!isWrapped) { |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 156 | texRT = sk_sp<GrVkTextureRenderTarget>(new GrVkTextureRenderTarget( |
| 157 | gpu, budgeted, desc, |
| 158 | info, imageView, msInfo, |
| 159 | colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 160 | resolveAttachmentView, ownership)); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 161 | } else { |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 162 | texRT = sk_sp<GrVkTextureRenderTarget>(new GrVkTextureRenderTarget( |
| 163 | gpu, desc, |
| 164 | info, imageView, msInfo, |
| 165 | colorAttachmentView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 166 | resolveAttachmentView, ownership)); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 167 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 168 | } else { |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 169 | if (!isWrapped) { |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 170 | texRT = sk_sp<GrVkTextureRenderTarget>(new GrVkTextureRenderTarget( |
| 171 | gpu, budgeted, desc, |
| 172 | info, imageView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 173 | colorAttachmentView, ownership)); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 174 | } else { |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 175 | texRT = sk_sp<GrVkTextureRenderTarget>(new GrVkTextureRenderTarget( |
| 176 | gpu, desc, |
| 177 | info, imageView, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 178 | colorAttachmentView, ownership)); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 179 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 180 | } |
| 181 | return texRT; |
| 182 | } |
| 183 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 184 | sk_sp<GrVkTextureRenderTarget> |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 185 | GrVkTextureRenderTarget::CreateNewTextureRenderTarget(GrVkGpu* gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 186 | SkBudgeted budgeted, |
| 187 | const GrSurfaceDesc& desc, |
| 188 | const GrVkImage::ImageDesc& imageDesc) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 189 | SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 190 | SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); |
| 191 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 192 | GrVkImageInfo info; |
| 193 | if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 194 | return nullptr; |
| 195 | } |
| 196 | |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 197 | sk_sp<GrVkTextureRenderTarget> trt = Make(gpu, desc, info, budgeted, |
| 198 | GrBackendObjectOwnership::kOwned, false); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 199 | if (!trt) { |
| 200 | GrVkImage::DestroyImageInfo(gpu, &info); |
| 201 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 202 | |
| 203 | return trt; |
| 204 | } |
| 205 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 206 | sk_sp<GrVkTextureRenderTarget> |
| 207 | GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(GrVkGpu* gpu, |
| 208 | const GrSurfaceDesc& desc, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 209 | GrWrapOwnership wrapOwnership, |
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); |
| 212 | // Wrapped textures require both image and allocation (because they can be mapped) |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 213 | SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc.fMemory); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 214 | |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 215 | GrBackendObjectOwnership ownership = kBorrow_GrWrapOwnership == wrapOwnership |
| 216 | ? GrBackendObjectOwnership::kBorrowed : GrBackendObjectOwnership::kOwned; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 217 | |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame^] | 218 | return Make(gpu, desc, *info, SkBudgeted::kNo, ownership, true); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 219 | } |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 220 | |
| 221 | bool GrVkTextureRenderTarget::updateForMipmap(GrVkGpu* gpu, const GrVkImageInfo& newInfo) { |
| 222 | VkFormat pixelFormat; |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 223 | GrPixelConfigToVkFormat(this->config(), &pixelFormat); |
| 224 | if (this->numStencilSamples()) { |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 225 | const GrVkImageView* resolveAttachmentView = |
| 226 | GrVkImageView::Create(gpu, |
| 227 | newInfo.fImage, |
| 228 | pixelFormat, |
| 229 | GrVkImageView::kColor_Type, |
| 230 | newInfo.fLevelCount); |
| 231 | if (!resolveAttachmentView) { |
| 232 | return false; |
| 233 | } |
| 234 | fResolveAttachmentView->unref(gpu); |
| 235 | fResolveAttachmentView = resolveAttachmentView; |
| 236 | } else { |
| 237 | const GrVkImageView* colorAttachmentView = GrVkImageView::Create(gpu, |
| 238 | newInfo.fImage, |
| 239 | pixelFormat, |
| 240 | GrVkImageView::kColor_Type, |
| 241 | 1); |
| 242 | if (!colorAttachmentView) { |
| 243 | return false; |
| 244 | } |
| 245 | fColorAttachmentView->unref(gpu); |
| 246 | fColorAttachmentView = colorAttachmentView; |
| 247 | } |
| 248 | |
| 249 | this->createFramebuffer(gpu); |
| 250 | return true; |
| 251 | } |
| 252 | |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 253 | size_t GrVkTextureRenderTarget::onGpuMemorySize() const { |
| 254 | // The plus 1 is to account for the resolve texture. |
| 255 | int numColorSamples = this->numColorSamples() + 1; |
| 256 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), |
| 257 | numColorSamples, // TODO: this still correct? |
| 258 | this->texturePriv().hasMipMaps()); |
| 259 | } |