blob: df2ad69ce0f0f260b80b1aa05a5c4178e0135fbe [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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
egdanieldd97b852016-04-28 09:30:39 -070015#include "SkMipMap.h"
jvanverth62340062016-04-26 08:01:44 -070016
jvanverthfd359ca2016-03-18 11:57:24 -070017#include "vk/GrVkTypes.h"
18
Greg Daniel164a9f02016-02-22 09:56:40 -050019#define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X)
20
kkinnunen2e6055b2016-04-22 01:48:29 -070021GrVkTextureRenderTarget* GrVkTextureRenderTarget::Create(GrVkGpu* gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -070022 const GrSurfaceDesc& desc,
egdanielb2df0c22016-05-13 11:30:37 -070023 const GrVkImageInfo& info,
24 SkBudgeted budgeted,
25 GrVkImage::Wrapped wrapped) {
26 VkImage image = info.fImage;
Greg Daniel164a9f02016-02-22 09:56:40 -050027 // Create the texture ImageView
jvanverth62340062016-04-26 08:01:44 -070028 uint32_t mipLevels = 1;
29 //TODO: does a mipmapped textureRenderTarget make sense?
30 //if (desc.fIsMipMapped) {
jvanverth82c05582016-05-03 11:19:01 -070031 // mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height()) + 1;
jvanverth62340062016-04-26 08:01:44 -070032 //}
egdanielb2df0c22016-05-13 11:30:37 -070033 const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, info.fFormat,
jvanverth62340062016-04-26 08:01:44 -070034 GrVkImageView::kColor_Type, mipLevels);
Greg Daniel164a9f02016-02-22 09:56:40 -050035 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
egdanielb2df0c22016-05-13 11:30:37 -070045 GrVkImageInfo msInfo;
Greg Daniel164a9f02016-02-22 09:56:40 -050046 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
egdanielb2df0c22016-05-13 11:30:37 -070059 if (!GrVkImage::InitImageInfo(gpu, msImageDesc, &msInfo)) {
Greg Daniel164a9f02016-02-22 09:56:40 -050060 return nullptr;
61 }
62
63 // Set color attachment image
egdanielb2df0c22016-05-13 11:30:37 -070064 colorImage = msInfo.fImage;
Greg Daniel164a9f02016-02-22 09:56:40 -050065
66 // Create resolve attachment view if necessary.
67 // If the format matches, this is the same as the texture imageView.
egdanielb2df0c22016-05-13 11:30:37 -070068 if (pixelFormat == info.fFormat) {
Greg Daniel164a9f02016-02-22 09:56:40 -050069 resolveAttachmentView = imageView;
70 resolveAttachmentView->ref();
71 } else {
72 resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelFormat,
jvanverth62340062016-04-26 08:01:44 -070073 GrVkImageView::kColor_Type, 1);
Greg Daniel164a9f02016-02-22 09:56:40 -050074 if (!resolveAttachmentView) {
egdanielb2df0c22016-05-13 11:30:37 -070075 GrVkImage::DestroyImageInfo(gpu, &msInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -050076 imageView->unref(gpu);
77 return nullptr;
78 }
79 }
80 } else {
81 // Set color attachment image
egdanielb2df0c22016-05-13 11:30:37 -070082 colorImage = info.fImage;
Greg Daniel164a9f02016-02-22 09:56:40 -050083 }
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
egdanielb2df0c22016-05-13 11:30:37 -070089 if (pixelFormat == info.fFormat && !resolveAttachmentView) {
Greg Daniel164a9f02016-02-22 09:56:40 -050090 colorAttachmentView = imageView;
91 colorAttachmentView->ref();
92 } else {
93 colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat,
jvanverth62340062016-04-26 08:01:44 -070094 GrVkImageView::kColor_Type, 1);
Greg Daniel164a9f02016-02-22 09:56:40 -050095 if (!colorAttachmentView) {
egdanielb2df0c22016-05-13 11:30:37 -070096 if (desc.fSampleCnt) {
Greg Daniel164a9f02016-02-22 09:56:40 -050097 resolveAttachmentView->unref(gpu);
egdanielb2df0c22016-05-13 11:30:37 -070098 GrVkImage::DestroyImageInfo(gpu, &msInfo);
Greg Daniel164a9f02016-02-22 09:56:40 -050099 }
100 imageView->unref(gpu);
101 return nullptr;
102 }
103 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500104 GrVkTextureRenderTarget* texRT;
egdanielb2df0c22016-05-13 11:30:37 -0700105 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 Daniel164a9f02016-02-22 09:56:40 -0500117 } else {
egdanielb2df0c22016-05-13 11:30:37 -0700118 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 Daniel164a9f02016-02-22 09:56:40 -0500127 }
128 return texRT;
129}
130
131GrVkTextureRenderTarget*
132GrVkTextureRenderTarget::CreateNewTextureRenderTarget(GrVkGpu* gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -0700133 SkBudgeted budgeted,
134 const GrSurfaceDesc& desc,
135 const GrVkImage::ImageDesc& imageDesc) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500136 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
137 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT);
138
egdanielb2df0c22016-05-13 11:30:37 -0700139 GrVkImageInfo info;
140 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500141 return nullptr;
142 }
143
egdanielb2df0c22016-05-13 11:30:37 -0700144 GrVkTextureRenderTarget* trt = Create(gpu, desc, info, budgeted, GrVkImage::kNot_Wrapped);
145 if (!trt) {
146 GrVkImage::DestroyImageInfo(gpu, &info);
147 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500148
149 return trt;
150}
151
152GrVkTextureRenderTarget*
153GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(GrVkGpu* gpu,
154 const GrSurfaceDesc& desc,
kkinnunen2e6055b2016-04-22 01:48:29 -0700155 GrWrapOwnership ownership,
egdanielb2df0c22016-05-13 11:30:37 -0700156 const GrVkImageInfo* info) {
jvanverthfd359ca2016-03-18 11:57:24 -0700157 SkASSERT(info);
158 // Wrapped textures require both image and allocation (because they can be mapped)
jvanverth1e305ba2016-06-01 09:39:15 -0700159 SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc.fMemory);
Greg Daniel164a9f02016-02-22 09:56:40 -0500160
egdanielb2df0c22016-05-13 11:30:37 -0700161 GrVkImage::Wrapped wrapped = kBorrow_GrWrapOwnership == ownership ? GrVkImage::kBorrowed_Wrapped
162 : GrVkImage::kAdopted_Wrapped;
jvanverthfd359ca2016-03-18 11:57:24 -0700163
egdanielb2df0c22016-05-13 11:30:37 -0700164 GrVkTextureRenderTarget* trt = Create(gpu, desc, *info, SkBudgeted::kNo, wrapped);
jvanverthfd359ca2016-03-18 11:57:24 -0700165
166 return trt;
Greg Daniel164a9f02016-02-22 09:56:40 -0500167}