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