1 // Copyright 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15
16 #include <vulkan/vulkan_core.h>
17
18 #include <vector>
19
20 #include "host-common/logging.h"
21 #include "vulkan/vk_enum_string_helper.h"
22
23 namespace gfxstream {
24 namespace vk {
25
26 // Header library that captures common patterns when working with
27 // Vulkan formats:
28 // - Macros to iterate over categories of formats
29 // - Add often-used parameters like the bytes per pixel and ASTC block size
30
31 #define LIST_VK_FORMATS_LINEAR(f) \
32 f(VK_FORMAT_R4G4_UNORM_PACK8, 1) \
33 f(VK_FORMAT_R4G4B4A4_UNORM_PACK16, 2) \
34 f(VK_FORMAT_B4G4R4A4_UNORM_PACK16, 2) \
35 f(VK_FORMAT_R5G6B5_UNORM_PACK16, 2) \
36 f(VK_FORMAT_B5G6R5_UNORM_PACK16, 2) \
37 f(VK_FORMAT_R5G5B5A1_UNORM_PACK16, 2) \
38 f(VK_FORMAT_B5G5R5A1_UNORM_PACK16, 2) \
39 f(VK_FORMAT_A1R5G5B5_UNORM_PACK16, 2) \
40 f(VK_FORMAT_R8_UNORM, 1) \
41 f(VK_FORMAT_R8_SNORM, 1) \
42 f(VK_FORMAT_R8_USCALED, 1) \
43 f(VK_FORMAT_R8_SSCALED, 1) \
44 f(VK_FORMAT_R8_UINT, 1) \
45 f(VK_FORMAT_R8_SINT, 1) \
46 f(VK_FORMAT_R8_SRGB, 1) \
47 f(VK_FORMAT_R8G8_UNORM, 2) \
48 f(VK_FORMAT_R8G8_SNORM, 2) \
49 f(VK_FORMAT_R8G8_USCALED, 2) \
50 f(VK_FORMAT_R8G8_SSCALED, 2) \
51 f(VK_FORMAT_R8G8_UINT, 2) \
52 f(VK_FORMAT_R8G8_SINT, 2) \
53 f(VK_FORMAT_R8G8_SRGB, 2) \
54 f(VK_FORMAT_R8G8B8_UNORM, 3) \
55 f(VK_FORMAT_R8G8B8_SNORM, 3) \
56 f(VK_FORMAT_R8G8B8_USCALED, 3) \
57 f(VK_FORMAT_R8G8B8_SSCALED, 3) \
58 f(VK_FORMAT_R8G8B8_UINT, 3) \
59 f(VK_FORMAT_R8G8B8_SINT, 3) \
60 f(VK_FORMAT_R8G8B8_SRGB, 3) \
61 f(VK_FORMAT_B8G8R8_UNORM, 3) \
62 f(VK_FORMAT_B8G8R8_SNORM, 3) \
63 f(VK_FORMAT_B8G8R8_USCALED, 3) \
64 f(VK_FORMAT_B8G8R8_SSCALED, 3) \
65 f(VK_FORMAT_B8G8R8_UINT, 3) \
66 f(VK_FORMAT_B8G8R8_SINT, 3) \
67 f(VK_FORMAT_B8G8R8_SRGB, 3) \
68 f(VK_FORMAT_R8G8B8A8_UNORM, 4) \
69 f(VK_FORMAT_R8G8B8A8_SNORM, 4) \
70 f(VK_FORMAT_R8G8B8A8_USCALED, 4) \
71 f(VK_FORMAT_R8G8B8A8_SSCALED, 4) \
72 f(VK_FORMAT_R8G8B8A8_UINT, 4) \
73 f(VK_FORMAT_R8G8B8A8_SINT, 4) \
74 f(VK_FORMAT_R8G8B8A8_SRGB, 4) \
75 f(VK_FORMAT_B8G8R8A8_UNORM, 4) \
76 f(VK_FORMAT_B8G8R8A8_SNORM, 4) \
77 f(VK_FORMAT_B8G8R8A8_USCALED, 4) \
78 f(VK_FORMAT_B8G8R8A8_SSCALED, 4) \
79 f(VK_FORMAT_B8G8R8A8_UINT, 4) \
80 f(VK_FORMAT_B8G8R8A8_SINT, 4) \
81 f(VK_FORMAT_B8G8R8A8_SRGB, 4) \
82 f(VK_FORMAT_A8B8G8R8_UNORM_PACK32, 4) \
83 f(VK_FORMAT_A8B8G8R8_SNORM_PACK32, 4) \
84 f(VK_FORMAT_A8B8G8R8_USCALED_PACK32, 4) \
85 f(VK_FORMAT_A8B8G8R8_SSCALED_PACK32, 4) \
86 f(VK_FORMAT_A8B8G8R8_UINT_PACK32, 4) \
87 f(VK_FORMAT_A8B8G8R8_SINT_PACK32, 4) \
88 f(VK_FORMAT_A8B8G8R8_SRGB_PACK32, 4) \
89 f(VK_FORMAT_A2R10G10B10_UNORM_PACK32, 4) \
90 f(VK_FORMAT_A2R10G10B10_SNORM_PACK32, 4) \
91 f(VK_FORMAT_A2R10G10B10_USCALED_PACK32, 4) \
92 f(VK_FORMAT_A2R10G10B10_SSCALED_PACK32, 4) \
93 f(VK_FORMAT_A2R10G10B10_UINT_PACK32, 4) \
94 f(VK_FORMAT_A2R10G10B10_SINT_PACK32, 4) \
95 f(VK_FORMAT_A2B10G10R10_UNORM_PACK32, 4) \
96 f(VK_FORMAT_A2B10G10R10_SNORM_PACK32, 4) \
97 f(VK_FORMAT_A2B10G10R10_USCALED_PACK32, 4) \
98 f(VK_FORMAT_A2B10G10R10_SSCALED_PACK32, 4) \
99 f(VK_FORMAT_A2B10G10R10_UINT_PACK32, 4) \
100 f(VK_FORMAT_A2B10G10R10_SINT_PACK32, 4) \
101 f(VK_FORMAT_R16_UNORM, 2) \
102 f(VK_FORMAT_R16_SNORM, 2) \
103 f(VK_FORMAT_R16_USCALED, 2) \
104 f(VK_FORMAT_R16_SSCALED, 2) \
105 f(VK_FORMAT_R16_UINT, 2) \
106 f(VK_FORMAT_R16_SINT, 2) \
107 f(VK_FORMAT_R16_SFLOAT, 2) \
108 f(VK_FORMAT_R16G16_UNORM, 4) \
109 f(VK_FORMAT_R16G16_SNORM, 4) \
110 f(VK_FORMAT_R16G16_USCALED, 4) \
111 f(VK_FORMAT_R16G16_SSCALED, 4) \
112 f(VK_FORMAT_R16G16_UINT, 4) \
113 f(VK_FORMAT_R16G16_SINT, 4) \
114 f(VK_FORMAT_R16G16_SFLOAT, 4) \
115 f(VK_FORMAT_R16G16B16_UNORM, 6) \
116 f(VK_FORMAT_R16G16B16_SNORM, 6) \
117 f(VK_FORMAT_R16G16B16_USCALED, 6) \
118 f(VK_FORMAT_R16G16B16_SSCALED, 6) \
119 f(VK_FORMAT_R16G16B16_UINT, 6) \
120 f(VK_FORMAT_R16G16B16_SINT, 6) \
121 f(VK_FORMAT_R16G16B16_SFLOAT, 6) \
122 f(VK_FORMAT_R16G16B16A16_UNORM, 8) \
123 f(VK_FORMAT_R16G16B16A16_SNORM, 8) \
124 f(VK_FORMAT_R16G16B16A16_USCALED, 8) \
125 f(VK_FORMAT_R16G16B16A16_SSCALED, 8) \
126 f(VK_FORMAT_R16G16B16A16_UINT, 8) \
127 f(VK_FORMAT_R16G16B16A16_SINT, 8) \
128 f(VK_FORMAT_R16G16B16A16_SFLOAT, 8) \
129 f(VK_FORMAT_R32_UINT, 4) \
130 f(VK_FORMAT_R32_SINT, 4) \
131 f(VK_FORMAT_R32_SFLOAT, 4) \
132 f(VK_FORMAT_R32G32_UINT, 8) \
133 f(VK_FORMAT_R32G32_SINT, 8) \
134 f(VK_FORMAT_R32G32_SFLOAT, 8) \
135 f(VK_FORMAT_R32G32B32_UINT, 8) \
136 f(VK_FORMAT_R32G32B32_SINT, 8) \
137 f(VK_FORMAT_R32G32B32_SFLOAT, 8) \
138 f(VK_FORMAT_R32G32B32A32_UINT, 16) \
139 f(VK_FORMAT_R32G32B32A32_SINT, 16) \
140 f(VK_FORMAT_R32G32B32A32_SFLOAT, 16) \
141 f(VK_FORMAT_R64_UINT, 8) \
142 f(VK_FORMAT_R64_SINT, 8) \
143 f(VK_FORMAT_R64_SFLOAT, 8) \
144 f(VK_FORMAT_R64G64_UINT, 16) \
145 f(VK_FORMAT_R64G64_SINT, 16) \
146 f(VK_FORMAT_R64G64_SFLOAT, 16) \
147 f(VK_FORMAT_R64G64B64_UINT, 24) \
148 f(VK_FORMAT_R64G64B64_SINT, 24) \
149 f(VK_FORMAT_R64G64B64_SFLOAT, 24) \
150 f(VK_FORMAT_R64G64B64A64_UINT, 32) \
151 f(VK_FORMAT_R64G64B64A64_SINT, 32) \
152 f(VK_FORMAT_R64G64B64A64_SFLOAT, 32) \
153 f(VK_FORMAT_B10G11R11_UFLOAT_PACK32, 4) \
154 f(VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, 4) \
155 f(VK_FORMAT_D16_UNORM, 2) \
156 f(VK_FORMAT_X8_D24_UNORM_PACK32, 4) \
157 f(VK_FORMAT_D32_SFLOAT, 4) \
158 f(VK_FORMAT_S8_UINT, 1) \
159 f(VK_FORMAT_D16_UNORM_S8_UINT, 3) \
160 f(VK_FORMAT_D24_UNORM_S8_UINT, 4) \
161 f(VK_FORMAT_D32_SFLOAT_S8_UINT, 5) \
162 f(VK_FORMAT_R10X6_UNORM_PACK16, 2) \
163 f(VK_FORMAT_R10X6G10X6_UNORM_2PACK16, 4) \
164 f(VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16, 8) \
165 f(VK_FORMAT_R12X4_UNORM_PACK16, 2) \
166 f(VK_FORMAT_R12X4G12X4_UNORM_2PACK16, 4) \
167 f(VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16, 8)
168
isEtc2(VkFormat format)169 constexpr bool isEtc2(VkFormat format) {
170 switch (format) {
171 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
172 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
173 case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
174 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
175 case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
176 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
177 case VK_FORMAT_EAC_R11_UNORM_BLOCK:
178 case VK_FORMAT_EAC_R11_SNORM_BLOCK:
179 case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
180 case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
181 return true;
182 default:
183 return false;
184 }
185 }
186
187 // Note: this only returns true for ASTC LDR, not HDR.
188 // TODO(gregschlom): Revisit our ASTC-related names if we decide to support HDR to make sure we
189 // clearly distinguish between the two modes.
isAstc(VkFormat format)190 constexpr bool isAstc(VkFormat format) {
191 switch (format) {
192 case VK_FORMAT_ASTC_4x4_UNORM_BLOCK:
193 case VK_FORMAT_ASTC_4x4_SRGB_BLOCK:
194 case VK_FORMAT_ASTC_5x4_UNORM_BLOCK:
195 case VK_FORMAT_ASTC_5x4_SRGB_BLOCK:
196 case VK_FORMAT_ASTC_5x5_UNORM_BLOCK:
197 case VK_FORMAT_ASTC_5x5_SRGB_BLOCK:
198 case VK_FORMAT_ASTC_6x5_UNORM_BLOCK:
199 case VK_FORMAT_ASTC_6x5_SRGB_BLOCK:
200 case VK_FORMAT_ASTC_6x6_UNORM_BLOCK:
201 case VK_FORMAT_ASTC_6x6_SRGB_BLOCK:
202 case VK_FORMAT_ASTC_8x5_UNORM_BLOCK:
203 case VK_FORMAT_ASTC_8x5_SRGB_BLOCK:
204 case VK_FORMAT_ASTC_8x6_UNORM_BLOCK:
205 case VK_FORMAT_ASTC_8x6_SRGB_BLOCK:
206 case VK_FORMAT_ASTC_8x8_UNORM_BLOCK:
207 case VK_FORMAT_ASTC_8x8_SRGB_BLOCK:
208 case VK_FORMAT_ASTC_10x5_UNORM_BLOCK:
209 case VK_FORMAT_ASTC_10x5_SRGB_BLOCK:
210 case VK_FORMAT_ASTC_10x6_UNORM_BLOCK:
211 case VK_FORMAT_ASTC_10x6_SRGB_BLOCK:
212 case VK_FORMAT_ASTC_10x8_UNORM_BLOCK:
213 case VK_FORMAT_ASTC_10x8_SRGB_BLOCK:
214 case VK_FORMAT_ASTC_10x10_UNORM_BLOCK:
215 case VK_FORMAT_ASTC_10x10_SRGB_BLOCK:
216 case VK_FORMAT_ASTC_12x10_UNORM_BLOCK:
217 case VK_FORMAT_ASTC_12x10_SRGB_BLOCK:
218 case VK_FORMAT_ASTC_12x12_UNORM_BLOCK:
219 case VK_FORMAT_ASTC_12x12_SRGB_BLOCK:
220 return true;
221 default:
222 return false;
223 }
224 }
225
isBc(VkFormat format)226 constexpr bool isBc(VkFormat format) {
227 switch (format) {
228 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
229 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
230 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
231 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
232 case VK_FORMAT_BC2_UNORM_BLOCK:
233 case VK_FORMAT_BC2_SRGB_BLOCK:
234 case VK_FORMAT_BC3_UNORM_BLOCK:
235 case VK_FORMAT_BC3_SRGB_BLOCK:
236 case VK_FORMAT_BC4_UNORM_BLOCK:
237 case VK_FORMAT_BC4_SNORM_BLOCK:
238 case VK_FORMAT_BC5_UNORM_BLOCK:
239 case VK_FORMAT_BC5_SNORM_BLOCK:
240 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
241 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
242 case VK_FORMAT_BC7_UNORM_BLOCK:
243 case VK_FORMAT_BC7_SRGB_BLOCK:
244 return true;
245 default:
246 return false;
247 }
248 }
249
formatIsUInt(VkFormat format)250 constexpr bool formatIsUInt(VkFormat format) {
251 switch (format) {
252 case VK_FORMAT_R8_UINT:
253 case VK_FORMAT_S8_UINT:
254 case VK_FORMAT_R8G8_UINT:
255 case VK_FORMAT_R8G8B8_UINT:
256 case VK_FORMAT_R8G8B8A8_UINT:
257 case VK_FORMAT_A8B8G8R8_UINT_PACK32:
258 case VK_FORMAT_A2B10G10R10_UINT_PACK32:
259 case VK_FORMAT_R16_UINT:
260 case VK_FORMAT_R16G16_UINT:
261 case VK_FORMAT_R16G16B16_UINT:
262 case VK_FORMAT_R16G16B16A16_UINT:
263 case VK_FORMAT_R32_UINT:
264 case VK_FORMAT_R32G32_UINT:
265 case VK_FORMAT_R32G32B32_UINT:
266 case VK_FORMAT_R32G32B32A32_UINT:
267 case VK_FORMAT_R64_UINT:
268 case VK_FORMAT_R64G64_UINT:
269 case VK_FORMAT_R64G64B64_UINT:
270 case VK_FORMAT_R64G64B64A64_UINT:
271 case VK_FORMAT_B8G8R8_UINT:
272 case VK_FORMAT_B8G8R8A8_UINT:
273 case VK_FORMAT_A2R10G10B10_UINT_PACK32:
274 return true;
275 default:
276 return false;
277 }
278 }
279
formatIsSInt(VkFormat format)280 constexpr bool formatIsSInt(VkFormat format) {
281 switch (format) {
282 case VK_FORMAT_R8_SINT:
283 case VK_FORMAT_R8G8_SINT:
284 case VK_FORMAT_R8G8B8_SINT:
285 case VK_FORMAT_R8G8B8A8_SINT:
286 case VK_FORMAT_A8B8G8R8_SINT_PACK32:
287 case VK_FORMAT_A2B10G10R10_SINT_PACK32:
288 case VK_FORMAT_R16_SINT:
289 case VK_FORMAT_R16G16_SINT:
290 case VK_FORMAT_R16G16B16_SINT:
291 case VK_FORMAT_R16G16B16A16_SINT:
292 case VK_FORMAT_R32_SINT:
293 case VK_FORMAT_R32G32_SINT:
294 case VK_FORMAT_R32G32B32_SINT:
295 case VK_FORMAT_R32G32B32A32_SINT:
296 case VK_FORMAT_R64_SINT:
297 case VK_FORMAT_R64G64_SINT:
298 case VK_FORMAT_R64G64B64_SINT:
299 case VK_FORMAT_R64G64B64A64_SINT:
300 case VK_FORMAT_B8G8R8_SINT:
301 case VK_FORMAT_B8G8R8A8_SINT:
302 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
303 return true;
304 default:
305 return false;
306 }
307 }
308
formatIsDepthOrStencil(VkFormat format)309 constexpr bool formatIsDepthOrStencil(VkFormat format) {
310 switch (format) {
311 case VK_FORMAT_D16_UNORM:
312 case VK_FORMAT_D16_UNORM_S8_UINT:
313 case VK_FORMAT_D24_UNORM_S8_UINT:
314 case VK_FORMAT_D32_SFLOAT:
315 case VK_FORMAT_D32_SFLOAT_S8_UINT:
316 case VK_FORMAT_S8_UINT:
317 case VK_FORMAT_X8_D24_UNORM_PACK32:
318 return true;
319 default:
320 return false;
321 }
322 }
323
formatRequiresSamplerYcbcrConversion(VkFormat format)324 constexpr bool formatRequiresSamplerYcbcrConversion(VkFormat format) {
325 switch (format) {
326 case VK_FORMAT_G8B8G8R8_422_UNORM:
327 case VK_FORMAT_B8G8R8G8_422_UNORM:
328 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
329 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
330 case VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM:
331 case VK_FORMAT_G8_B8R8_2PLANE_422_UNORM:
332 case VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM:
333 case VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:
334 case VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16:
335 case VK_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16:
336 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
337 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
338 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
339 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
340 case VK_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
341 case VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16:
342 case VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16:
343 case VK_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16:
344 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
345 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
346 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
347 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
348 case VK_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
349 case VK_FORMAT_G16B16G16R16_422_UNORM:
350 case VK_FORMAT_B16G16R16G16_422_UNORM:
351 case VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM:
352 case VK_FORMAT_G16_B16R16_2PLANE_420_UNORM:
353 case VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM:
354 case VK_FORMAT_G16_B16R16_2PLANE_422_UNORM:
355 case VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM:
356 #ifdef VK_EXT_YCBCR_2PLANE_444_FORMATS_SPEC_VERSION
357 case VK_FORMAT_G8_B8R8_2PLANE_444_UNORM_EXT:
358 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16_EXT:
359 case VK_FORMAT_G12X4_B12X4R12X4_2PLANE_444_UNORM_3PACK16_EXT:
360 case VK_FORMAT_G16_B16R16_2PLANE_444_UNORM_EXT:
361 #endif
362 return true;
363 default:
364 return false;
365 }
366 }
367
368 // Returns the size in bytes needed to copy an image with the given format,
369 // width, and height to a staging buffer and the VkBufferImageCopy-s needed
370 // to copy from a staging buffer to destination VkImage.
371 bool getFormatTransferInfo(VkFormat format, uint32_t width, uint32_t height,
372 VkDeviceSize* outStagingBufferCopySize,
373 std::vector<VkBufferImageCopy>* outBufferImageCopies);
374
375 } // namespace vk
376 } // namespace gfxstream
377
378