| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrVkTextureRenderTarget.h" | 8 #include "GrVkTextureRenderTarget.h" |
| 9 | 9 |
| 10 #include "GrRenderTargetPriv.h" | 10 #include "GrRenderTargetPriv.h" |
| 11 #include "GrVkGpu.h" | 11 #include "GrVkGpu.h" |
| 12 #include "GrVkImageView.h" | 12 #include "GrVkImageView.h" |
| 13 #include "GrVkUtil.h" | 13 #include "GrVkUtil.h" |
| 14 | 14 |
| 15 #include "SkMipMap.h" | 15 #include "SkMipMap.h" |
| 16 | 16 |
| 17 #include "vk/GrVkTypes.h" | 17 #include "vk/GrVkTypes.h" |
| 18 | 18 |
| 19 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) | 19 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
| 20 | 20 |
| 21 template<typename ResourceType> | |
| 22 GrVkTextureRenderTarget* GrVkTextureRenderTarget::Create(GrVkGpu* gpu, | 21 GrVkTextureRenderTarget* GrVkTextureRenderTarget::Create(GrVkGpu* gpu, |
| 23 ResourceType resourceTy
pe, | |
| 24 const GrSurfaceDesc& de
sc, | 22 const GrSurfaceDesc& de
sc, |
| 25 VkFormat format, | 23 const GrVkImageInfo& in
fo, |
| 26 const GrVkImage::Resour
ce* imageResource) { | 24 SkBudgeted budgeted, |
| 27 VkImage image = imageResource->fImage; | 25 bool isWrapped, |
| 26 bool isBorrowed) { |
| 27 VkImage image = info.fImage; |
| 28 // Create the texture ImageView | 28 // Create the texture ImageView |
| 29 uint32_t mipLevels = 1; | 29 uint32_t mipLevels = 1; |
| 30 //TODO: does a mipmapped textureRenderTarget make sense? | 30 //TODO: does a mipmapped textureRenderTarget make sense? |
| 31 //if (desc.fIsMipMapped) { | 31 //if (desc.fIsMipMapped) { |
| 32 // mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height())
+ 1; | 32 // mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height())
+ 1; |
| 33 //} | 33 //} |
| 34 const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, format, | 34 const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, info.fFor
mat, |
| 35 GrVkImageView::kColor
_Type, mipLevels); | 35 GrVkImageView::kColor
_Type, mipLevels); |
| 36 if (!imageView) { | 36 if (!imageView) { |
| 37 return nullptr; | 37 return nullptr; |
| 38 } | 38 } |
| 39 | 39 |
| 40 VkFormat pixelFormat; | 40 VkFormat pixelFormat; |
| 41 GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat); | 41 GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat); |
| 42 | 42 |
| 43 VkImage colorImage; | 43 VkImage colorImage; |
| 44 | 44 |
| 45 // create msaa surface if necessary | 45 // create msaa surface if necessary |
| 46 const GrVkImage::Resource* msaaImageResource = nullptr; | 46 GrVkImageInfo msInfo; |
| 47 const GrVkImageView* resolveAttachmentView = nullptr; | 47 const GrVkImageView* resolveAttachmentView = nullptr; |
| 48 if (desc.fSampleCnt) { | 48 if (desc.fSampleCnt) { |
| 49 GrVkImage::ImageDesc msImageDesc; | 49 GrVkImage::ImageDesc msImageDesc; |
| 50 msImageDesc.fImageType = VK_IMAGE_TYPE_2D; | 50 msImageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 51 msImageDesc.fFormat = pixelFormat; | 51 msImageDesc.fFormat = pixelFormat; |
| 52 msImageDesc.fWidth = desc.fWidth; | 52 msImageDesc.fWidth = desc.fWidth; |
| 53 msImageDesc.fHeight = desc.fHeight; | 53 msImageDesc.fHeight = desc.fHeight; |
| 54 msImageDesc.fLevels = 1; | 54 msImageDesc.fLevels = 1; |
| 55 msImageDesc.fSamples = desc.fSampleCnt; | 55 msImageDesc.fSamples = desc.fSampleCnt; |
| 56 msImageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; | 56 msImageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 57 msImageDesc.fUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; | 57 msImageDesc.fUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 58 msImageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; | 58 msImageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 59 | 59 |
| 60 msaaImageResource = GrVkImage::CreateResource(gpu, msImageDesc); | 60 if (!GrVkImage::GetImageInfo(gpu, msImageDesc, &msInfo)) { |
| 61 | |
| 62 if (!msaaImageResource) { | |
| 63 imageView->unref(gpu); | |
| 64 return nullptr; | 61 return nullptr; |
| 65 } | 62 } |
| 66 | 63 |
| 67 // Set color attachment image | 64 // Set color attachment image |
| 68 colorImage = msaaImageResource->fImage; | 65 colorImage = msInfo.fImage; |
| 69 | 66 |
| 70 // Create resolve attachment view if necessary. | 67 // Create resolve attachment view if necessary. |
| 71 // If the format matches, this is the same as the texture imageView. | 68 // If the format matches, this is the same as the texture imageView. |
| 72 if (pixelFormat == format) { | 69 if (pixelFormat == info.fFormat) { |
| 73 resolveAttachmentView = imageView; | 70 resolveAttachmentView = imageView; |
| 74 resolveAttachmentView->ref(); | 71 resolveAttachmentView->ref(); |
| 75 } else { | 72 } else { |
| 76 resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelForma
t, | 73 resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelForma
t, |
| 77 GrVkImageView::kColor_
Type, 1); | 74 GrVkImageView::kColor_
Type, 1); |
| 78 if (!resolveAttachmentView) { | 75 if (!resolveAttachmentView) { |
| 79 msaaImageResource->unref(gpu); | 76 VK_CALL(gpu, DestroyImage(gpu->device(), msInfo.fImage, nullptr)
); |
| 77 VK_CALL(gpu, FreeMemory(gpu->device(), msInfo.fAlloc, nullptr)); |
| 80 imageView->unref(gpu); | 78 imageView->unref(gpu); |
| 81 return nullptr; | 79 return nullptr; |
| 82 } | 80 } |
| 83 } | 81 } |
| 84 } else { | 82 } else { |
| 85 // Set color attachment image | 83 // Set color attachment image |
| 86 colorImage = imageResource->fImage; | 84 colorImage = info.fImage; |
| 87 } | 85 } |
| 88 | 86 |
| 89 const GrVkImageView* colorAttachmentView; | 87 const GrVkImageView* colorAttachmentView; |
| 90 // Get color attachment view. | 88 // Get color attachment view. |
| 91 // If the format matches and there's no multisampling, | 89 // If the format matches and there's no multisampling, |
| 92 // this is the same as the texture imageView | 90 // this is the same as the texture imageView |
| 93 if (pixelFormat == format && !resolveAttachmentView) { | 91 if (pixelFormat == info.fFormat && !resolveAttachmentView) { |
| 94 colorAttachmentView = imageView; | 92 colorAttachmentView = imageView; |
| 95 colorAttachmentView->ref(); | 93 colorAttachmentView->ref(); |
| 96 } else { | 94 } else { |
| 97 colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat
, | 95 colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat
, |
| 98 GrVkImageView::kColor_Type,
1); | 96 GrVkImageView::kColor_Type,
1); |
| 99 if (!colorAttachmentView) { | 97 if (!colorAttachmentView) { |
| 100 if (msaaImageResource) { | 98 if (desc.fSampleCnt) { |
| 101 resolveAttachmentView->unref(gpu); | 99 resolveAttachmentView->unref(gpu); |
| 102 msaaImageResource->unref(gpu); | 100 VK_CALL(gpu, DestroyImage(gpu->device(), msInfo.fImage, nullptr)
); |
| 101 VK_CALL(gpu, FreeMemory(gpu->device(), msInfo.fAlloc, nullptr)); |
| 103 } | 102 } |
| 104 imageView->unref(gpu); | 103 imageView->unref(gpu); |
| 105 return nullptr; | 104 return nullptr; |
| 106 } | 105 } |
| 107 } | 106 } |
| 108 GrVkTextureRenderTarget* texRT; | 107 GrVkTextureRenderTarget* texRT; |
| 109 if (msaaImageResource) { | 108 if (desc.fSampleCnt) { |
| 110 texRT = new GrVkTextureRenderTarget(gpu, resourceType, desc, | 109 if (isWrapped) { |
| 111 imageResource, imageView, msaaImageR
esource, | 110 texRT = new GrVkTextureRenderTarget(gpu, desc, |
| 112 colorAttachmentView, | 111 info, imageView, msInfo, |
| 113 resolveAttachmentView); | 112 colorAttachmentView, |
| 114 msaaImageResource->unref(gpu); | 113 resolveAttachmentView, isBorrowe
d); |
| 114 } else { |
| 115 texRT = new GrVkTextureRenderTarget(gpu, budgeted, desc, |
| 116 info, imageView, msInfo, |
| 117 colorAttachmentView, |
| 118 resolveAttachmentView); |
| 119 } |
| 115 } else { | 120 } else { |
| 116 texRT = new GrVkTextureRenderTarget(gpu, resourceType, desc, | 121 if (isWrapped) { |
| 117 imageResource, imageView, | 122 texRT = new GrVkTextureRenderTarget(gpu, desc, |
| 118 colorAttachmentView); | 123 info, imageView, |
| 124 colorAttachmentView, isBorrowed)
; |
| 125 } else { |
| 126 texRT = new GrVkTextureRenderTarget(gpu, budgeted, desc, |
| 127 info, imageView, |
| 128 colorAttachmentView); |
| 129 } |
| 119 } | 130 } |
| 120 return texRT; | 131 return texRT; |
| 121 } | 132 } |
| 122 | 133 |
| 123 GrVkTextureRenderTarget* | 134 GrVkTextureRenderTarget* |
| 124 GrVkTextureRenderTarget::CreateNewTextureRenderTarget(GrVkGpu* gpu, | 135 GrVkTextureRenderTarget::CreateNewTextureRenderTarget(GrVkGpu* gpu, |
| 125 SkBudgeted budgeted, | 136 SkBudgeted budgeted, |
| 126 const GrSurfaceDesc& desc, | 137 const GrSurfaceDesc& desc, |
| 127 const GrVkImage::ImageDesc
& imageDesc) { | 138 const GrVkImage::ImageDesc
& imageDesc) { |
| 128 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); | 139 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 129 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); | 140 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); |
| 130 | 141 |
| 131 const GrVkImage::Resource* imageRsrc = GrVkImage::CreateResource(gpu, imageD
esc); | 142 GrVkImageInfo info; |
| 132 | 143 if (!GrVkImage::GetImageInfo(gpu, imageDesc, &info)) { |
| 133 if (!imageRsrc) { | |
| 134 return nullptr; | 144 return nullptr; |
| 135 } | 145 } |
| 136 | 146 |
| 137 GrVkTextureRenderTarget* trt = Create(gpu, budgeted, desc, imageDesc.fFormat
, | 147 GrVkTextureRenderTarget* trt = Create(gpu, desc, info, budgeted, false, fals
e); |
| 138 imageRsrc); | 148 if (!trt) { |
| 139 // Create() will increment the refCount of the image resource if it succeeds | 149 VK_CALL(gpu, DestroyImage(gpu->device(), info.fImage, nullptr)); |
| 140 imageRsrc->unref(gpu); | 150 VK_CALL(gpu, FreeMemory(gpu->device(), info.fAlloc, nullptr)); |
| 151 } |
| 141 | 152 |
| 142 return trt; | 153 return trt; |
| 143 } | 154 } |
| 144 | 155 |
| 145 GrVkTextureRenderTarget* | 156 GrVkTextureRenderTarget* |
| 146 GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(GrVkGpu* gpu, | 157 GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(GrVkGpu* gpu, |
| 147 const GrSurfaceDesc& d
esc, | 158 const GrSurfaceDesc& d
esc, |
| 148 GrWrapOwnership owners
hip, | 159 GrWrapOwnership owners
hip, |
| 149 VkFormat format, | 160 const GrVkImageInfo* i
nfo) { |
| 150 const GrVkTextureInfo*
info) { | |
| 151 SkASSERT(info); | 161 SkASSERT(info); |
| 152 // Wrapped textures require both image and allocation (because they can be m
apped) | 162 // Wrapped textures require both image and allocation (because they can be m
apped) |
| 153 SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc); | 163 SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc); |
| 154 | 164 |
| 155 GrVkImage::Resource::Flags flags = (VK_IMAGE_TILING_LINEAR == info->fImageTi
ling) | 165 GrVkTextureRenderTarget* trt = Create(gpu, desc, *info, SkBudgeted::kNo, tru
e, |
| 156 ? Resource::kLinearTiling_Flag : Resource::
kNo_Flags; | 166 kBorrow_GrWrapOwnership == ownership); |
| 157 | |
| 158 const GrVkImage::Resource* imageResource; | |
| 159 if (kBorrow_GrWrapOwnership == ownership) { | |
| 160 imageResource = new GrVkImage::BorrowedResource(info->fImage, | |
| 161 info->fAlloc, | |
| 162 info->fFormat, | |
| 163 info->fLevelCount, | |
| 164 flags); | |
| 165 } else { | |
| 166 imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, info
->fFormat, | |
| 167 info->fLevelCount, flags); | |
| 168 } | |
| 169 if (!imageResource) { | |
| 170 return nullptr; | |
| 171 } | |
| 172 GrVkTextureRenderTarget* trt = Create(gpu, kWrapped, desc, format, imageReso
urce); | |
| 173 if (trt) { | |
| 174 trt->fCurrentLayout = info->fImageLayout; | |
| 175 } | |
| 176 // Create() will increment the refCount of the image resource if it succeeds | |
| 177 imageResource->unref(gpu); | |
| 178 | 167 |
| 179 return trt; | 168 return trt; |
| 180 } | 169 } |
| OLD | NEW |