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