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