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 "GrVkTexture.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 9 | |
| 10 | #include "GrTexturePriv.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | #include "GrVkGpu.h" |
| 12 | #include "GrVkImageView.h" |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 13 | #include "GrVkTextureRenderTarget.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | #include "GrVkUtil.h" |
| 15 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 16 | #include "vk/GrVkTypes.h" |
| 17 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
| 19 | |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 20 | // This method parallels GrTextureProxy::highestFilterMode |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 21 | static inline GrSamplerState::Filter highest_filter_mode(GrPixelConfig config) { |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 22 | if (GrPixelConfigIsSint(config)) { |
| 23 | // We only ever want to nearest-neighbor sample signed int textures. |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 24 | return GrSamplerState::Filter::kNearest; |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 25 | } |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 26 | return GrSamplerState::Filter::kMipMap; |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 27 | } |
| 28 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 29 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
| 30 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 31 | SkBudgeted budgeted, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 32 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 33 | const GrVkImageInfo& info, |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame^] | 34 | const GrVkImageView* view, |
| 35 | bool wasFullMipMapDataProvided) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 36 | : GrSurface(gpu, desc) |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 37 | , GrVkImage(info, GrBackendObjectOwnership::kOwned) |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 38 | , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig), |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame^] | 39 | info.fLevelCount > 1, wasFullMipMapDataProvided) |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 40 | , fTextureView(view) |
| 41 | , fLinearTextureView(nullptr) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 42 | this->registerWithCache(budgeted); |
| 43 | } |
| 44 | |
| 45 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 46 | Wrapped, |
| 47 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 48 | const GrVkImageInfo& info, |
| 49 | const GrVkImageView* view, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 50 | GrBackendObjectOwnership ownership) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 51 | : GrSurface(gpu, desc) |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 52 | , GrVkImage(info, ownership) |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 53 | , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig), |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame^] | 54 | info.fLevelCount > 1, false) |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 55 | , fTextureView(view) |
| 56 | , fLinearTextureView(nullptr) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 57 | this->registerWithCacheWrapped(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
| 61 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 62 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 63 | const GrVkImageInfo& info, |
| 64 | const GrVkImageView* view, |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame^] | 65 | GrBackendObjectOwnership ownership, |
| 66 | bool wasFullMipMapDataProvided) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 67 | : GrSurface(gpu, desc) |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 68 | , GrVkImage(info, ownership) |
Robert Phillips | 49081d1 | 2017-05-08 13:41:35 -0400 | [diff] [blame] | 69 | , INHERITED(gpu, desc, kTexture2DSampler_GrSLType, highest_filter_mode(desc.fConfig), |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame^] | 70 | info.fLevelCount > 1, wasFullMipMapDataProvided) |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 71 | , fTextureView(view) |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 72 | , fLinearTextureView(nullptr) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 75 | sk_sp<GrVkTexture> GrVkTexture::CreateNewTexture(GrVkGpu* gpu, SkBudgeted budgeted, |
| 76 | const GrSurfaceDesc& desc, |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame^] | 77 | const GrVkImage::ImageDesc& imageDesc, |
| 78 | bool fullMipMapDataProvided) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 79 | SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); |
| 80 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 81 | GrVkImageInfo info; |
| 82 | if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 83 | return nullptr; |
| 84 | } |
| 85 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 86 | const GrVkImageView* imageView = GrVkImageView::Create(gpu, info.fImage, info.fFormat, |
| 87 | GrVkImageView::kColor_Type, |
| 88 | info.fLevelCount); |
| 89 | if (!imageView) { |
| 90 | GrVkImage::DestroyImageInfo(gpu, &info); |
| 91 | return nullptr; |
| 92 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 93 | |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame^] | 94 | return sk_sp<GrVkTexture>(new GrVkTexture(gpu, budgeted, desc, info, imageView, |
| 95 | fullMipMapDataProvided)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 96 | } |
| 97 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 98 | sk_sp<GrVkTexture> GrVkTexture::MakeWrappedTexture(GrVkGpu* gpu, |
| 99 | const GrSurfaceDesc& desc, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 100 | GrWrapOwnership wrapOwnership, |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 101 | const GrVkImageInfo* info) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 102 | SkASSERT(info); |
| 103 | // Wrapped textures require both image and allocation (because they can be mapped) |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 104 | SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc.fMemory); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 106 | const GrVkImageView* imageView = GrVkImageView::Create(gpu, info->fImage, info->fFormat, |
| 107 | GrVkImageView::kColor_Type, |
| 108 | info->fLevelCount); |
| 109 | if (!imageView) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 110 | return nullptr; |
| 111 | } |
| 112 | |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 113 | GrBackendObjectOwnership ownership = kBorrow_GrWrapOwnership == wrapOwnership |
| 114 | ? GrBackendObjectOwnership::kBorrowed : GrBackendObjectOwnership::kOwned; |
| 115 | return sk_sp<GrVkTexture>(new GrVkTexture(gpu, kWrapped, desc, *info, imageView, ownership)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | GrVkTexture::~GrVkTexture() { |
| 119 | // either release or abandon should have been called by the owner of this object. |
| 120 | SkASSERT(!fTextureView); |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 121 | SkASSERT(!fLinearTextureView); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void GrVkTexture::onRelease() { |
| 125 | // we create this and don't hand it off, so we should always destroy it |
| 126 | if (fTextureView) { |
| 127 | fTextureView->unref(this->getVkGpu()); |
| 128 | fTextureView = nullptr; |
| 129 | } |
| 130 | |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 131 | if (fLinearTextureView) { |
| 132 | fLinearTextureView->unref(this->getVkGpu()); |
| 133 | fLinearTextureView = nullptr; |
| 134 | } |
| 135 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 136 | this->releaseImage(this->getVkGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 137 | |
| 138 | INHERITED::onRelease(); |
| 139 | } |
| 140 | |
| 141 | void GrVkTexture::onAbandon() { |
| 142 | if (fTextureView) { |
| 143 | fTextureView->unrefAndAbandon(); |
| 144 | fTextureView = nullptr; |
| 145 | } |
| 146 | |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 147 | if (fLinearTextureView) { |
| 148 | fLinearTextureView->unrefAndAbandon(); |
| 149 | fLinearTextureView = nullptr; |
| 150 | } |
| 151 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 152 | this->abandonImage(); |
| 153 | INHERITED::onAbandon(); |
| 154 | } |
| 155 | |
| 156 | GrBackendObject GrVkTexture::getTextureHandle() const { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 157 | return (GrBackendObject)&fInfo; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | GrVkGpu* GrVkTexture::getVkGpu() const { |
| 161 | SkASSERT(!this->wasDestroyed()); |
| 162 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 163 | } |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 164 | |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 165 | const GrVkImageView* GrVkTexture::textureView(bool allowSRGB) { |
| 166 | VkFormat linearFormat; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 167 | if (allowSRGB || !GrVkFormatIsSRGB(fInfo.fFormat, &linearFormat)) { |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 168 | return fTextureView; |
| 169 | } |
| 170 | |
| 171 | if (!fLinearTextureView) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 172 | fLinearTextureView = GrVkImageView::Create(this->getVkGpu(), fInfo.fImage, |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 173 | linearFormat, GrVkImageView::kColor_Type, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 174 | fInfo.fLevelCount); |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 175 | SkASSERT(fLinearTextureView); |
| 176 | } |
| 177 | |
| 178 | return fLinearTextureView; |
| 179 | } |
| 180 | |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 181 | bool GrVkTexture::reallocForMipmap(GrVkGpu* gpu, uint32_t mipLevels) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 182 | if (mipLevels == 1) { |
| 183 | // don't need to do anything for a 1x1 texture |
| 184 | return false; |
| 185 | } |
| 186 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 187 | const GrVkResource* oldResource = this->resource(); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 188 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 189 | // We shouldn't realloc something that doesn't belong to us |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 190 | if (fIsBorrowed) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 194 | bool renderTarget = SkToBool(this->asRenderTarget()); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 195 | |
| 196 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 197 | if (renderTarget) { |
| 198 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 199 | } |
| 200 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 201 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 202 | GrVkImage::ImageDesc imageDesc; |
| 203 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 204 | imageDesc.fFormat = fInfo.fFormat; |
Brian Salomon | d34edf3 | 2017-05-19 15:45:48 -0400 | [diff] [blame] | 205 | imageDesc.fWidth = this->width(); |
| 206 | imageDesc.fHeight = this->height(); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 207 | imageDesc.fLevels = mipLevels; |
| 208 | imageDesc.fSamples = 1; |
| 209 | imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 210 | imageDesc.fUsageFlags = usageFlags; |
| 211 | imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 212 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 213 | GrVkImageInfo info; |
| 214 | if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
bsalomon | c902164 | 2016-05-14 17:55:32 -0700 | [diff] [blame] | 215 | return false; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // have to create a new image view for new resource |
| 219 | const GrVkImageView* oldView = fTextureView; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 220 | VkImage image = info.fImage; |
| 221 | const GrVkImageView* textureView = GrVkImageView::Create(gpu, image, info.fFormat, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 222 | GrVkImageView::kColor_Type, mipLevels); |
| 223 | if (!textureView) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 224 | GrVkImage::DestroyImageInfo(gpu, &info); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 228 | if (renderTarget) { |
| 229 | GrVkTextureRenderTarget* texRT = static_cast<GrVkTextureRenderTarget*>(this); |
| 230 | if (!texRT->updateForMipmap(gpu, info)) { |
| 231 | GrVkImage::DestroyImageInfo(gpu, &info); |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 236 | oldResource->unref(gpu); |
| 237 | oldView->unref(gpu); |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 238 | if (fLinearTextureView) { |
| 239 | fLinearTextureView->unref(gpu); |
| 240 | fLinearTextureView = nullptr; |
| 241 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 242 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 243 | this->setNewResource(info.fImage, info.fAlloc, info.fImageTiling); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 244 | fTextureView = textureView; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 245 | fInfo = info; |
Greg Daniel | b280d4e | 2017-09-01 09:40:30 -0400 | [diff] [blame] | 246 | // SetMaxMipMapLevel stores the max level not the number of levels |
| 247 | this->texturePriv().setMaxMipMapLevel(mipLevels-1); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 248 | |
| 249 | return true; |
| 250 | } |