OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrVkTextureRenderTarget.h" | 8 #include "GrVkTextureRenderTarget.h" |
9 | 9 |
10 #include "GrRenderTargetPriv.h" | 10 #include "GrRenderTargetPriv.h" |
11 #include "GrVkGpu.h" | 11 #include "GrVkGpu.h" |
12 #include "GrVkImageView.h" | 12 #include "GrVkImageView.h" |
13 #include "GrVkUtil.h" | 13 #include "GrVkUtil.h" |
14 | 14 |
15 #include "SkMipMap.h" | 15 #include "SkMipMap.h" |
16 | 16 |
17 #include "vk/GrVkTypes.h" | 17 #include "vk/GrVkTypes.h" |
18 | 18 |
19 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) | 19 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
20 | 20 |
21 template<typename ResourceType> | |
22 GrVkTextureRenderTarget* GrVkTextureRenderTarget::Create(GrVkGpu* gpu, | 21 GrVkTextureRenderTarget* GrVkTextureRenderTarget::Create(GrVkGpu* gpu, |
23 ResourceType resourceTy
pe, | |
24 const GrSurfaceDesc& de
sc, | 22 const GrSurfaceDesc& de
sc, |
25 VkFormat format, | 23 const GrVkImageInfo& in
fo, |
26 const GrVkImage::Resour
ce* imageResource) { | 24 SkBudgeted budgeted, |
27 VkImage image = imageResource->fImage; | 25 GrVkImage::Wrapped wrap
ped) { |
| 26 VkImage image = info.fImage; |
28 // Create the texture ImageView | 27 // Create the texture ImageView |
29 uint32_t mipLevels = 1; | 28 uint32_t mipLevels = 1; |
30 //TODO: does a mipmapped textureRenderTarget make sense? | 29 //TODO: does a mipmapped textureRenderTarget make sense? |
31 //if (desc.fIsMipMapped) { | 30 //if (desc.fIsMipMapped) { |
32 // mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height())
+ 1; | 31 // mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height())
+ 1; |
33 //} | 32 //} |
34 const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, format, | 33 const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, info.fFor
mat, |
35 GrVkImageView::kColor
_Type, mipLevels); | 34 GrVkImageView::kColor
_Type, mipLevels); |
36 if (!imageView) { | 35 if (!imageView) { |
37 return nullptr; | 36 return nullptr; |
38 } | 37 } |
39 | 38 |
40 VkFormat pixelFormat; | 39 VkFormat pixelFormat; |
41 GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat); | 40 GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat); |
42 | 41 |
43 VkImage colorImage; | 42 VkImage colorImage; |
44 | 43 |
45 // create msaa surface if necessary | 44 // create msaa surface if necessary |
46 const GrVkImage::Resource* msaaImageResource = nullptr; | 45 GrVkImageInfo msInfo; |
47 const GrVkImageView* resolveAttachmentView = nullptr; | 46 const GrVkImageView* resolveAttachmentView = nullptr; |
48 if (desc.fSampleCnt) { | 47 if (desc.fSampleCnt) { |
49 GrVkImage::ImageDesc msImageDesc; | 48 GrVkImage::ImageDesc msImageDesc; |
50 msImageDesc.fImageType = VK_IMAGE_TYPE_2D; | 49 msImageDesc.fImageType = VK_IMAGE_TYPE_2D; |
51 msImageDesc.fFormat = pixelFormat; | 50 msImageDesc.fFormat = pixelFormat; |
52 msImageDesc.fWidth = desc.fWidth; | 51 msImageDesc.fWidth = desc.fWidth; |
53 msImageDesc.fHeight = desc.fHeight; | 52 msImageDesc.fHeight = desc.fHeight; |
54 msImageDesc.fLevels = 1; | 53 msImageDesc.fLevels = 1; |
55 msImageDesc.fSamples = desc.fSampleCnt; | 54 msImageDesc.fSamples = desc.fSampleCnt; |
56 msImageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; | 55 msImageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
57 msImageDesc.fUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; | 56 msImageDesc.fUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
58 msImageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; | 57 msImageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
59 | 58 |
60 msaaImageResource = GrVkImage::CreateResource(gpu, msImageDesc); | 59 if (!GrVkImage::InitImageInfo(gpu, msImageDesc, &msInfo)) { |
61 | |
62 if (!msaaImageResource) { | |
63 imageView->unref(gpu); | |
64 return nullptr; | 60 return nullptr; |
65 } | 61 } |
66 | 62 |
67 // Set color attachment image | 63 // Set color attachment image |
68 colorImage = msaaImageResource->fImage; | 64 colorImage = msInfo.fImage; |
69 | 65 |
70 // Create resolve attachment view if necessary. | 66 // Create resolve attachment view if necessary. |
71 // If the format matches, this is the same as the texture imageView. | 67 // If the format matches, this is the same as the texture imageView. |
72 if (pixelFormat == format) { | 68 if (pixelFormat == info.fFormat) { |
73 resolveAttachmentView = imageView; | 69 resolveAttachmentView = imageView; |
74 resolveAttachmentView->ref(); | 70 resolveAttachmentView->ref(); |
75 } else { | 71 } else { |
76 resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelForma
t, | 72 resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelForma
t, |
77 GrVkImageView::kColor_
Type, 1); | 73 GrVkImageView::kColor_
Type, 1); |
78 if (!resolveAttachmentView) { | 74 if (!resolveAttachmentView) { |
79 msaaImageResource->unref(gpu); | 75 GrVkImage::DestroyImageInfo(gpu, &msInfo); |
80 imageView->unref(gpu); | 76 imageView->unref(gpu); |
81 return nullptr; | 77 return nullptr; |
82 } | 78 } |
83 } | 79 } |
84 } else { | 80 } else { |
85 // Set color attachment image | 81 // Set color attachment image |
86 colorImage = imageResource->fImage; | 82 colorImage = info.fImage; |
87 } | 83 } |
88 | 84 |
89 const GrVkImageView* colorAttachmentView; | 85 const GrVkImageView* colorAttachmentView; |
90 // Get color attachment view. | 86 // Get color attachment view. |
91 // If the format matches and there's no multisampling, | 87 // If the format matches and there's no multisampling, |
92 // this is the same as the texture imageView | 88 // this is the same as the texture imageView |
93 if (pixelFormat == format && !resolveAttachmentView) { | 89 if (pixelFormat == info.fFormat && !resolveAttachmentView) { |
94 colorAttachmentView = imageView; | 90 colorAttachmentView = imageView; |
95 colorAttachmentView->ref(); | 91 colorAttachmentView->ref(); |
96 } else { | 92 } else { |
97 colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat
, | 93 colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat
, |
98 GrVkImageView::kColor_Type,
1); | 94 GrVkImageView::kColor_Type,
1); |
99 if (!colorAttachmentView) { | 95 if (!colorAttachmentView) { |
100 if (msaaImageResource) { | 96 if (desc.fSampleCnt) { |
101 resolveAttachmentView->unref(gpu); | 97 resolveAttachmentView->unref(gpu); |
102 msaaImageResource->unref(gpu); | 98 GrVkImage::DestroyImageInfo(gpu, &msInfo); |
103 } | 99 } |
104 imageView->unref(gpu); | 100 imageView->unref(gpu); |
105 return nullptr; | 101 return nullptr; |
106 } | 102 } |
107 } | 103 } |
108 GrVkTextureRenderTarget* texRT; | 104 GrVkTextureRenderTarget* texRT; |
109 if (msaaImageResource) { | 105 if (desc.fSampleCnt) { |
110 texRT = new GrVkTextureRenderTarget(gpu, resourceType, desc, | 106 if (GrVkImage::kNot_Wrapped == wrapped) { |
111 imageResource, imageView, msaaImageR
esource, | 107 texRT = new GrVkTextureRenderTarget(gpu, budgeted, desc, |
112 colorAttachmentView, | 108 info, imageView, msInfo, |
113 resolveAttachmentView); | 109 colorAttachmentView, |
114 msaaImageResource->unref(gpu); | 110 resolveAttachmentView); |
| 111 } else { |
| 112 texRT = new GrVkTextureRenderTarget(gpu, desc, |
| 113 info, imageView, msInfo, |
| 114 colorAttachmentView, |
| 115 resolveAttachmentView, wrapped); |
| 116 } |
115 } else { | 117 } else { |
116 texRT = new GrVkTextureRenderTarget(gpu, resourceType, desc, | 118 if (GrVkImage::kNot_Wrapped == wrapped) { |
117 imageResource, imageView, | 119 texRT = new GrVkTextureRenderTarget(gpu, budgeted, desc, |
118 colorAttachmentView); | 120 info, imageView, |
| 121 colorAttachmentView); |
| 122 } else { |
| 123 texRT = new GrVkTextureRenderTarget(gpu, desc, |
| 124 info, imageView, |
| 125 colorAttachmentView, wrapped); |
| 126 } |
119 } | 127 } |
120 return texRT; | 128 return texRT; |
121 } | 129 } |
122 | 130 |
123 GrVkTextureRenderTarget* | 131 GrVkTextureRenderTarget* |
124 GrVkTextureRenderTarget::CreateNewTextureRenderTarget(GrVkGpu* gpu, | 132 GrVkTextureRenderTarget::CreateNewTextureRenderTarget(GrVkGpu* gpu, |
125 SkBudgeted budgeted, | 133 SkBudgeted budgeted, |
126 const GrSurfaceDesc& desc, | 134 const GrSurfaceDesc& desc, |
127 const GrVkImage::ImageDesc
& imageDesc) { | 135 const GrVkImage::ImageDesc
& imageDesc) { |
128 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); | 136 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
129 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); | 137 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); |
130 | 138 |
131 const GrVkImage::Resource* imageRsrc = GrVkImage::CreateResource(gpu, imageD
esc); | 139 GrVkImageInfo info; |
132 | 140 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
133 if (!imageRsrc) { | |
134 return nullptr; | 141 return nullptr; |
135 } | 142 } |
136 | 143 |
137 GrVkTextureRenderTarget* trt = Create(gpu, budgeted, desc, imageDesc.fFormat
, | 144 GrVkTextureRenderTarget* trt = Create(gpu, desc, info, budgeted, GrVkImage::
kNot_Wrapped); |
138 imageRsrc); | 145 if (!trt) { |
139 // Create() will increment the refCount of the image resource if it succeeds | 146 GrVkImage::DestroyImageInfo(gpu, &info); |
140 imageRsrc->unref(gpu); | 147 } |
141 | 148 |
142 return trt; | 149 return trt; |
143 } | 150 } |
144 | 151 |
145 GrVkTextureRenderTarget* | 152 GrVkTextureRenderTarget* |
146 GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(GrVkGpu* gpu, | 153 GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(GrVkGpu* gpu, |
147 const GrSurfaceDesc& d
esc, | 154 const GrSurfaceDesc& d
esc, |
148 GrWrapOwnership owners
hip, | 155 GrWrapOwnership owners
hip, |
149 VkFormat format, | 156 const GrVkImageInfo* i
nfo) { |
150 const GrVkTextureInfo*
info) { | |
151 SkASSERT(info); | 157 SkASSERT(info); |
152 // Wrapped textures require both image and allocation (because they can be m
apped) | 158 // Wrapped textures require both image and allocation (because they can be m
apped) |
153 SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc); | 159 SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc); |
154 | 160 |
155 GrVkImage::Resource::Flags flags = (VK_IMAGE_TILING_LINEAR == info->fImageTi
ling) | 161 |
156 ? Resource::kLinearTiling_Flag : Resource::
kNo_Flags; | 162 GrVkImage::Wrapped wrapped = kBorrow_GrWrapOwnership == ownership ? GrVkImag
e::kBorrowed_Wrapped |
| 163 : GrVkImag
e::kAdopted_Wrapped; |
157 | 164 |
158 const GrVkImage::Resource* imageResource; | 165 GrVkTextureRenderTarget* trt = Create(gpu, desc, *info, SkBudgeted::kNo, wra
pped); |
159 if (kBorrow_GrWrapOwnership == ownership) { | |
160 imageResource = new GrVkImage::BorrowedResource(info->fImage, | |
161 info->fAlloc, | |
162 info->fFormat, | |
163 info->fLevelCount, | |
164 flags); | |
165 } else { | |
166 imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, info
->fFormat, | |
167 info->fLevelCount, flags); | |
168 } | |
169 if (!imageResource) { | |
170 return nullptr; | |
171 } | |
172 GrVkTextureRenderTarget* trt = Create(gpu, kWrapped, desc, format, imageReso
urce); | |
173 if (trt) { | |
174 trt->fCurrentLayout = info->fImageLayout; | |
175 } | |
176 // Create() will increment the refCount of the image resource if it succeeds | |
177 imageResource->unref(gpu); | |
178 | 166 |
179 return trt; | 167 return trt; |
180 } | 168 } |
OLD | NEW |