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