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" |
| 11 | #include "GrVkGpu.h" |
| 12 | #include "GrVkImageView.h" |
| 13 | #include "GrVkUtil.h" |
| 14 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 15 | #include "SkMipmap.h" |
| 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 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 21 | template<typename ResourceType> |
| 22 | GrVkTextureRenderTarget* GrVkTextureRenderTarget::Create(GrVkGpu* gpu, |
| 23 | ResourceType resourceType, |
| 24 | const GrSurfaceDesc& desc, |
| 25 | VkFormat format, |
| 26 | const GrVkImage::Resource* imageResource) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | VkImage image = imageResource->fImage; |
| 28 | // Create the texture ImageView |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 29 | uint32_t mipLevels = 1; |
| 30 | //TODO: does a mipmapped textureRenderTarget make sense? |
| 31 | //if (desc.fIsMipMapped) { |
| 32 | // mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height()); |
| 33 | //} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 34 | const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, format, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 35 | GrVkImageView::kColor_Type, mipLevels); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 36 | if (!imageView) { |
| 37 | return nullptr; |
| 38 | } |
| 39 | |
| 40 | VkFormat pixelFormat; |
| 41 | GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat); |
| 42 | |
| 43 | VkImage colorImage; |
| 44 | |
| 45 | // create msaa surface if necessary |
| 46 | const GrVkImage::Resource* msaaImageResource = nullptr; |
| 47 | const GrVkImageView* resolveAttachmentView = nullptr; |
| 48 | if (desc.fSampleCnt) { |
| 49 | GrVkImage::ImageDesc msImageDesc; |
| 50 | msImageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 51 | msImageDesc.fFormat = pixelFormat; |
| 52 | msImageDesc.fWidth = desc.fWidth; |
| 53 | msImageDesc.fHeight = desc.fHeight; |
| 54 | msImageDesc.fLevels = 1; |
| 55 | msImageDesc.fSamples = desc.fSampleCnt; |
| 56 | msImageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 57 | msImageDesc.fUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 58 | msImageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 59 | |
| 60 | msaaImageResource = GrVkImage::CreateResource(gpu, msImageDesc); |
| 61 | |
| 62 | if (!msaaImageResource) { |
| 63 | imageView->unref(gpu); |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
| 67 | // Set color attachment image |
| 68 | colorImage = msaaImageResource->fImage; |
| 69 | |
| 70 | // Create resolve attachment view if necessary. |
| 71 | // If the format matches, this is the same as the texture imageView. |
| 72 | if (pixelFormat == format) { |
| 73 | resolveAttachmentView = imageView; |
| 74 | resolveAttachmentView->ref(); |
| 75 | } else { |
| 76 | resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelFormat, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 77 | GrVkImageView::kColor_Type, 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 78 | if (!resolveAttachmentView) { |
| 79 | msaaImageResource->unref(gpu); |
| 80 | imageView->unref(gpu); |
| 81 | return nullptr; |
| 82 | } |
| 83 | } |
| 84 | } else { |
| 85 | // Set color attachment image |
| 86 | colorImage = imageResource->fImage; |
| 87 | } |
| 88 | |
| 89 | const GrVkImageView* colorAttachmentView; |
| 90 | // Get color attachment view. |
| 91 | // If the format matches and there's no multisampling, |
| 92 | // this is the same as the texture imageView |
| 93 | if (pixelFormat == format && !resolveAttachmentView) { |
| 94 | colorAttachmentView = imageView; |
| 95 | colorAttachmentView->ref(); |
| 96 | } else { |
| 97 | colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 98 | GrVkImageView::kColor_Type, 1); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 99 | if (!colorAttachmentView) { |
| 100 | if (msaaImageResource) { |
| 101 | resolveAttachmentView->unref(gpu); |
| 102 | msaaImageResource->unref(gpu); |
| 103 | } |
| 104 | imageView->unref(gpu); |
| 105 | return nullptr; |
| 106 | } |
| 107 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 108 | GrVkTextureRenderTarget* texRT; |
| 109 | if (msaaImageResource) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 110 | texRT = new GrVkTextureRenderTarget(gpu, resourceType, desc, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 111 | imageResource, imageView, msaaImageResource, |
| 112 | colorAttachmentView, |
| 113 | resolveAttachmentView); |
| 114 | msaaImageResource->unref(gpu); |
| 115 | } else { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 116 | texRT = new GrVkTextureRenderTarget(gpu, resourceType, desc, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 117 | imageResource, imageView, |
| 118 | colorAttachmentView); |
| 119 | } |
| 120 | return texRT; |
| 121 | } |
| 122 | |
| 123 | GrVkTextureRenderTarget* |
| 124 | GrVkTextureRenderTarget::CreateNewTextureRenderTarget(GrVkGpu* gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 125 | SkBudgeted budgeted, |
| 126 | const GrSurfaceDesc& desc, |
| 127 | const GrVkImage::ImageDesc& imageDesc) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 128 | SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 129 | SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); |
| 130 | |
| 131 | const GrVkImage::Resource* imageRsrc = GrVkImage::CreateResource(gpu, imageDesc); |
| 132 | |
| 133 | if (!imageRsrc) { |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 137 | GrVkTextureRenderTarget* trt = Create(gpu, budgeted, desc, imageDesc.fFormat, |
| 138 | imageRsrc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 139 | // Create() will increment the refCount of the image resource if it succeeds |
| 140 | imageRsrc->unref(gpu); |
| 141 | |
| 142 | return trt; |
| 143 | } |
| 144 | |
| 145 | GrVkTextureRenderTarget* |
| 146 | GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(GrVkGpu* gpu, |
| 147 | const GrSurfaceDesc& desc, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 148 | GrWrapOwnership ownership, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 149 | VkFormat format, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 150 | const GrVkTextureInfo* info) { |
| 151 | SkASSERT(info); |
| 152 | // Wrapped textures require both image and allocation (because they can be mapped) |
| 153 | SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 154 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 155 | GrVkImage::Resource::Flags flags = (VK_IMAGE_TILING_LINEAR == info->fImageTiling) |
| 156 | ? Resource::kLinearTiling_Flag : Resource::kNo_Flags; |
| 157 | |
jvanverth | fe170d2 | 2016-03-22 13:15:44 -0700 | [diff] [blame] | 158 | const GrVkImage::Resource* imageResource; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 159 | if (kBorrow_GrWrapOwnership == ownership) { |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 160 | imageResource = new GrVkImage::BorrowedResource(info->fImage, |
| 161 | info->fAlloc, |
| 162 | flags, |
| 163 | info->fFormat); |
jvanverth | fe170d2 | 2016-03-22 13:15:44 -0700 | [diff] [blame] | 164 | } else { |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 165 | imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, flags, info->fFormat); |
jvanverth | fe170d2 | 2016-03-22 13:15:44 -0700 | [diff] [blame] | 166 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 167 | if (!imageResource) { |
| 168 | return nullptr; |
| 169 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 170 | GrVkTextureRenderTarget* trt = Create(gpu, kWrapped, desc, format, imageResource); |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 171 | if (trt) { |
| 172 | trt->fCurrentLayout = info->fImageLayout; |
| 173 | } |
| 174 | // Create() will increment the refCount of the image resource if it succeeds |
| 175 | imageResource->unref(gpu); |
| 176 | |
| 177 | return trt; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 178 | } |