ac: add ac_build_{struct,raw}_tbuffer_load() helpers
[mesa.git] / src / amd / vulkan / radv_formats.c
1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include "radv_private.h"
26
27 #include "vk_format.h"
28 #include "sid.h"
29
30 #include "vk_util.h"
31
32 #include "util/u_half.h"
33 #include "util/format_srgb.h"
34 #include "util/format_r11g11b10f.h"
35
36 uint32_t radv_translate_buffer_dataformat(const struct vk_format_description *desc,
37 int first_non_void)
38 {
39 unsigned type;
40 int i;
41
42 if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
43 return V_008F0C_BUF_DATA_FORMAT_10_11_11;
44
45 if (first_non_void < 0)
46 return V_008F0C_BUF_DATA_FORMAT_INVALID;
47 type = desc->channel[first_non_void].type;
48
49 if (type == VK_FORMAT_TYPE_FIXED)
50 return V_008F0C_BUF_DATA_FORMAT_INVALID;
51 if (desc->nr_channels == 4 &&
52 desc->channel[0].size == 10 &&
53 desc->channel[1].size == 10 &&
54 desc->channel[2].size == 10 &&
55 desc->channel[3].size == 2)
56 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
57
58 /* See whether the components are of the same size. */
59 for (i = 0; i < desc->nr_channels; i++) {
60 if (desc->channel[first_non_void].size != desc->channel[i].size)
61 return V_008F0C_BUF_DATA_FORMAT_INVALID;
62 }
63
64 switch (desc->channel[first_non_void].size) {
65 case 8:
66 switch (desc->nr_channels) {
67 case 1:
68 return V_008F0C_BUF_DATA_FORMAT_8;
69 case 2:
70 return V_008F0C_BUF_DATA_FORMAT_8_8;
71 case 4:
72 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
73 }
74 break;
75 case 16:
76 switch (desc->nr_channels) {
77 case 1:
78 return V_008F0C_BUF_DATA_FORMAT_16;
79 case 2:
80 return V_008F0C_BUF_DATA_FORMAT_16_16;
81 case 4:
82 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
83 }
84 break;
85 case 32:
86 /* From the Southern Islands ISA documentation about MTBUF:
87 * 'Memory reads of data in memory that is 32 or 64 bits do not
88 * undergo any format conversion.'
89 */
90 if (type != VK_FORMAT_TYPE_FLOAT &&
91 !desc->channel[first_non_void].pure_integer)
92 return V_008F0C_BUF_DATA_FORMAT_INVALID;
93
94 switch (desc->nr_channels) {
95 case 1:
96 return V_008F0C_BUF_DATA_FORMAT_32;
97 case 2:
98 return V_008F0C_BUF_DATA_FORMAT_32_32;
99 case 3:
100 return V_008F0C_BUF_DATA_FORMAT_32_32_32;
101 case 4:
102 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
103 }
104 break;
105 }
106
107 return V_008F0C_BUF_DATA_FORMAT_INVALID;
108 }
109
110 uint32_t radv_translate_buffer_numformat(const struct vk_format_description *desc,
111 int first_non_void)
112 {
113 if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
114 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
115
116 if (first_non_void < 0)
117 return ~0;
118
119 switch (desc->channel[first_non_void].type) {
120 case VK_FORMAT_TYPE_SIGNED:
121 if (desc->channel[first_non_void].normalized)
122 return V_008F0C_BUF_NUM_FORMAT_SNORM;
123 else if (desc->channel[first_non_void].pure_integer)
124 return V_008F0C_BUF_NUM_FORMAT_SINT;
125 else
126 return V_008F0C_BUF_NUM_FORMAT_SSCALED;
127 break;
128 case VK_FORMAT_TYPE_UNSIGNED:
129 if (desc->channel[first_non_void].normalized)
130 return V_008F0C_BUF_NUM_FORMAT_UNORM;
131 else if (desc->channel[first_non_void].pure_integer)
132 return V_008F0C_BUF_NUM_FORMAT_UINT;
133 else
134 return V_008F0C_BUF_NUM_FORMAT_USCALED;
135 break;
136 case VK_FORMAT_TYPE_FLOAT:
137 default:
138 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
139 }
140 }
141
142 uint32_t radv_translate_tex_dataformat(VkFormat format,
143 const struct vk_format_description *desc,
144 int first_non_void)
145 {
146 bool uniform = true;
147 int i;
148
149 if (!desc)
150 return ~0;
151 /* Colorspace (return non-RGB formats directly). */
152 switch (desc->colorspace) {
153 /* Depth stencil formats */
154 case VK_FORMAT_COLORSPACE_ZS:
155 switch (format) {
156 case VK_FORMAT_D16_UNORM:
157 return V_008F14_IMG_DATA_FORMAT_16;
158 case VK_FORMAT_D24_UNORM_S8_UINT:
159 case VK_FORMAT_X8_D24_UNORM_PACK32:
160 return V_008F14_IMG_DATA_FORMAT_8_24;
161 case VK_FORMAT_S8_UINT:
162 return V_008F14_IMG_DATA_FORMAT_8;
163 case VK_FORMAT_D32_SFLOAT:
164 return V_008F14_IMG_DATA_FORMAT_32;
165 case VK_FORMAT_D32_SFLOAT_S8_UINT:
166 return V_008F14_IMG_DATA_FORMAT_X24_8_32;
167 default:
168 goto out_unknown;
169 }
170
171 case VK_FORMAT_COLORSPACE_YUV:
172 goto out_unknown; /* TODO */
173
174 case VK_FORMAT_COLORSPACE_SRGB:
175 if (desc->nr_channels != 4 && desc->nr_channels != 1)
176 goto out_unknown;
177 break;
178
179 default:
180 break;
181 }
182
183 if (desc->layout == VK_FORMAT_LAYOUT_RGTC) {
184 switch(format) {
185 case VK_FORMAT_BC4_UNORM_BLOCK:
186 case VK_FORMAT_BC4_SNORM_BLOCK:
187 return V_008F14_IMG_DATA_FORMAT_BC4;
188 case VK_FORMAT_BC5_UNORM_BLOCK:
189 case VK_FORMAT_BC5_SNORM_BLOCK:
190 return V_008F14_IMG_DATA_FORMAT_BC5;
191 default:
192 break;
193 }
194 }
195
196 if (desc->layout == VK_FORMAT_LAYOUT_S3TC) {
197 switch(format) {
198 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
199 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
200 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
201 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
202 return V_008F14_IMG_DATA_FORMAT_BC1;
203 case VK_FORMAT_BC2_UNORM_BLOCK:
204 case VK_FORMAT_BC2_SRGB_BLOCK:
205 return V_008F14_IMG_DATA_FORMAT_BC2;
206 case VK_FORMAT_BC3_UNORM_BLOCK:
207 case VK_FORMAT_BC3_SRGB_BLOCK:
208 return V_008F14_IMG_DATA_FORMAT_BC3;
209 default:
210 break;
211 }
212 }
213
214 if (desc->layout == VK_FORMAT_LAYOUT_BPTC) {
215 switch(format) {
216 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
217 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
218 return V_008F14_IMG_DATA_FORMAT_BC6;
219 case VK_FORMAT_BC7_UNORM_BLOCK:
220 case VK_FORMAT_BC7_SRGB_BLOCK:
221 return V_008F14_IMG_DATA_FORMAT_BC7;
222 default:
223 break;
224 }
225 }
226
227 if (desc->layout == VK_FORMAT_LAYOUT_ETC) {
228 switch (format) {
229 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
230 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
231 return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
232 case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
233 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
234 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
235 case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
236 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
237 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
238 case VK_FORMAT_EAC_R11_UNORM_BLOCK:
239 case VK_FORMAT_EAC_R11_SNORM_BLOCK:
240 return V_008F14_IMG_DATA_FORMAT_ETC2_R;
241 case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
242 case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
243 return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
244 default:
245 break;
246 }
247 }
248
249 if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
250 return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
251 } else if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
252 return V_008F14_IMG_DATA_FORMAT_10_11_11;
253 }
254
255 /* R8G8Bx_SNORM - TODO CxV8U8 */
256
257 /* hw cannot support mixed formats (except depth/stencil, since only
258 * depth is read).*/
259 if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
260 goto out_unknown;
261
262 /* See whether the components are of the same size. */
263 for (i = 1; i < desc->nr_channels; i++) {
264 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
265 }
266
267 /* Non-uniform formats. */
268 if (!uniform) {
269 switch(desc->nr_channels) {
270 case 3:
271 if (desc->channel[0].size == 5 &&
272 desc->channel[1].size == 6 &&
273 desc->channel[2].size == 5) {
274 return V_008F14_IMG_DATA_FORMAT_5_6_5;
275 }
276 goto out_unknown;
277 case 4:
278 if (desc->channel[0].size == 5 &&
279 desc->channel[1].size == 5 &&
280 desc->channel[2].size == 5 &&
281 desc->channel[3].size == 1) {
282 return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
283 }
284 if (desc->channel[0].size == 1 &&
285 desc->channel[1].size == 5 &&
286 desc->channel[2].size == 5 &&
287 desc->channel[3].size == 5) {
288 return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
289 }
290 if (desc->channel[0].size == 10 &&
291 desc->channel[1].size == 10 &&
292 desc->channel[2].size == 10 &&
293 desc->channel[3].size == 2) {
294 /* Closed VK driver does this also no 2/10/10/10 snorm */
295 if (desc->channel[0].type == VK_FORMAT_TYPE_SIGNED &&
296 desc->channel[0].normalized)
297 goto out_unknown;
298 return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
299 }
300 goto out_unknown;
301 }
302 goto out_unknown;
303 }
304
305 if (first_non_void < 0 || first_non_void > 3)
306 goto out_unknown;
307
308 /* uniform formats */
309 switch (desc->channel[first_non_void].size) {
310 case 4:
311 switch (desc->nr_channels) {
312 #if 0 /* Not supported for render targets */
313 case 2:
314 return V_008F14_IMG_DATA_FORMAT_4_4;
315 #endif
316 case 4:
317 return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
318 }
319 break;
320 case 8:
321 switch (desc->nr_channels) {
322 case 1:
323 return V_008F14_IMG_DATA_FORMAT_8;
324 case 2:
325 return V_008F14_IMG_DATA_FORMAT_8_8;
326 case 4:
327 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
328 }
329 break;
330 case 16:
331 switch (desc->nr_channels) {
332 case 1:
333 return V_008F14_IMG_DATA_FORMAT_16;
334 case 2:
335 return V_008F14_IMG_DATA_FORMAT_16_16;
336 case 4:
337 return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
338 }
339 break;
340 case 32:
341 switch (desc->nr_channels) {
342 case 1:
343 return V_008F14_IMG_DATA_FORMAT_32;
344 case 2:
345 return V_008F14_IMG_DATA_FORMAT_32_32;
346 case 3:
347 return V_008F14_IMG_DATA_FORMAT_32_32_32;
348 case 4:
349 return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
350 }
351 }
352
353 out_unknown:
354 /* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
355 return ~0;
356 }
357
358 uint32_t radv_translate_tex_numformat(VkFormat format,
359 const struct vk_format_description *desc,
360 int first_non_void)
361 {
362 switch (format) {
363 case VK_FORMAT_D24_UNORM_S8_UINT:
364 return V_008F14_IMG_NUM_FORMAT_UNORM;
365 default:
366 if (first_non_void < 0) {
367 if (vk_format_is_compressed(format)) {
368 switch (format) {
369 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
370 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
371 case VK_FORMAT_BC2_SRGB_BLOCK:
372 case VK_FORMAT_BC3_SRGB_BLOCK:
373 case VK_FORMAT_BC7_SRGB_BLOCK:
374 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
375 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
376 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
377 return V_008F14_IMG_NUM_FORMAT_SRGB;
378 case VK_FORMAT_BC4_SNORM_BLOCK:
379 case VK_FORMAT_BC5_SNORM_BLOCK:
380 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
381 case VK_FORMAT_EAC_R11_SNORM_BLOCK:
382 case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
383 return V_008F14_IMG_NUM_FORMAT_SNORM;
384 default:
385 return V_008F14_IMG_NUM_FORMAT_UNORM;
386 }
387 } else if (desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
388 return V_008F14_IMG_NUM_FORMAT_UNORM;
389 } else {
390 return V_008F14_IMG_NUM_FORMAT_FLOAT;
391 }
392 } else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
393 return V_008F14_IMG_NUM_FORMAT_SRGB;
394 } else {
395 switch (desc->channel[first_non_void].type) {
396 case VK_FORMAT_TYPE_FLOAT:
397 return V_008F14_IMG_NUM_FORMAT_FLOAT;
398 case VK_FORMAT_TYPE_SIGNED:
399 if (desc->channel[first_non_void].normalized)
400 return V_008F14_IMG_NUM_FORMAT_SNORM;
401 else if (desc->channel[first_non_void].pure_integer)
402 return V_008F14_IMG_NUM_FORMAT_SINT;
403 else
404 return V_008F14_IMG_NUM_FORMAT_SSCALED;
405 case VK_FORMAT_TYPE_UNSIGNED:
406 if (desc->channel[first_non_void].normalized)
407 return V_008F14_IMG_NUM_FORMAT_UNORM;
408 else if (desc->channel[first_non_void].pure_integer)
409 return V_008F14_IMG_NUM_FORMAT_UINT;
410 else
411 return V_008F14_IMG_NUM_FORMAT_USCALED;
412 default:
413 return V_008F14_IMG_NUM_FORMAT_UNORM;
414 }
415 }
416 }
417 }
418
419 uint32_t radv_translate_color_numformat(VkFormat format,
420 const struct vk_format_description *desc,
421 int first_non_void)
422 {
423 unsigned ntype;
424 if (first_non_void == -1 || desc->channel[first_non_void].type == VK_FORMAT_TYPE_FLOAT)
425 ntype = V_028C70_NUMBER_FLOAT;
426 else {
427 ntype = V_028C70_NUMBER_UNORM;
428 if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB)
429 ntype = V_028C70_NUMBER_SRGB;
430 else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_SIGNED) {
431 if (desc->channel[first_non_void].pure_integer) {
432 ntype = V_028C70_NUMBER_SINT;
433 } else if (desc->channel[first_non_void].normalized) {
434 ntype = V_028C70_NUMBER_SNORM;
435 } else
436 ntype = ~0u;
437 } else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_UNSIGNED) {
438 if (desc->channel[first_non_void].pure_integer) {
439 ntype = V_028C70_NUMBER_UINT;
440 } else if (desc->channel[first_non_void].normalized) {
441 ntype = V_028C70_NUMBER_UNORM;
442 } else
443 ntype = ~0u;
444 }
445 }
446 return ntype;
447 }
448
449 static bool radv_is_sampler_format_supported(VkFormat format, bool *linear_sampling)
450 {
451 const struct vk_format_description *desc = vk_format_description(format);
452 uint32_t num_format;
453 if (!desc || format == VK_FORMAT_UNDEFINED)
454 return false;
455 num_format = radv_translate_tex_numformat(format, desc,
456 vk_format_get_first_non_void_channel(format));
457
458 if (num_format == V_008F14_IMG_NUM_FORMAT_USCALED ||
459 num_format == V_008F14_IMG_NUM_FORMAT_SSCALED)
460 return false;
461
462 if (num_format == V_008F14_IMG_NUM_FORMAT_UNORM ||
463 num_format == V_008F14_IMG_NUM_FORMAT_SNORM ||
464 num_format == V_008F14_IMG_NUM_FORMAT_FLOAT ||
465 num_format == V_008F14_IMG_NUM_FORMAT_SRGB)
466 *linear_sampling = true;
467 else
468 *linear_sampling = false;
469 return radv_translate_tex_dataformat(format, vk_format_description(format),
470 vk_format_get_first_non_void_channel(format)) != ~0U;
471 }
472
473
474 static bool radv_is_storage_image_format_supported(struct radv_physical_device *physical_device,
475 VkFormat format)
476 {
477 const struct vk_format_description *desc = vk_format_description(format);
478 unsigned data_format, num_format;
479 if (!desc || format == VK_FORMAT_UNDEFINED)
480 return false;
481
482 data_format = radv_translate_tex_dataformat(format, desc,
483 vk_format_get_first_non_void_channel(format));
484 num_format = radv_translate_tex_numformat(format, desc,
485 vk_format_get_first_non_void_channel(format));
486
487 if(data_format == ~0 || num_format == ~0)
488 return false;
489
490 /* Extracted from the GCN3 ISA document. */
491 switch(num_format) {
492 case V_008F14_IMG_NUM_FORMAT_UNORM:
493 case V_008F14_IMG_NUM_FORMAT_SNORM:
494 case V_008F14_IMG_NUM_FORMAT_UINT:
495 case V_008F14_IMG_NUM_FORMAT_SINT:
496 case V_008F14_IMG_NUM_FORMAT_FLOAT:
497 break;
498 default:
499 return false;
500 }
501
502 switch(data_format) {
503 case V_008F14_IMG_DATA_FORMAT_8:
504 case V_008F14_IMG_DATA_FORMAT_16:
505 case V_008F14_IMG_DATA_FORMAT_8_8:
506 case V_008F14_IMG_DATA_FORMAT_32:
507 case V_008F14_IMG_DATA_FORMAT_16_16:
508 case V_008F14_IMG_DATA_FORMAT_10_11_11:
509 case V_008F14_IMG_DATA_FORMAT_11_11_10:
510 case V_008F14_IMG_DATA_FORMAT_10_10_10_2:
511 case V_008F14_IMG_DATA_FORMAT_2_10_10_10:
512 case V_008F14_IMG_DATA_FORMAT_8_8_8_8:
513 case V_008F14_IMG_DATA_FORMAT_32_32:
514 case V_008F14_IMG_DATA_FORMAT_16_16_16_16:
515 case V_008F14_IMG_DATA_FORMAT_32_32_32_32:
516 case V_008F14_IMG_DATA_FORMAT_5_6_5:
517 case V_008F14_IMG_DATA_FORMAT_1_5_5_5:
518 case V_008F14_IMG_DATA_FORMAT_5_5_5_1:
519 case V_008F14_IMG_DATA_FORMAT_4_4_4_4:
520 /* TODO: FMASK formats. */
521 return true;
522 default:
523 return false;
524 }
525 }
526
527 static bool radv_is_buffer_format_supported(VkFormat format, bool *scaled)
528 {
529 const struct vk_format_description *desc = vk_format_description(format);
530 unsigned data_format, num_format;
531 if (!desc || format == VK_FORMAT_UNDEFINED)
532 return false;
533
534 data_format = radv_translate_buffer_dataformat(desc,
535 vk_format_get_first_non_void_channel(format));
536 num_format = radv_translate_buffer_numformat(desc,
537 vk_format_get_first_non_void_channel(format));
538
539 *scaled = (num_format == V_008F0C_BUF_NUM_FORMAT_SSCALED) || (num_format == V_008F0C_BUF_NUM_FORMAT_USCALED);
540 return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID &&
541 num_format != ~0;
542 }
543
544 bool radv_is_colorbuffer_format_supported(VkFormat format, bool *blendable)
545 {
546 const struct vk_format_description *desc = vk_format_description(format);
547 uint32_t color_format = radv_translate_colorformat(format);
548 uint32_t color_swap = radv_translate_colorswap(format, false);
549 uint32_t color_num_format = radv_translate_color_numformat(format,
550 desc,
551 vk_format_get_first_non_void_channel(format));
552
553 if (color_num_format == V_028C70_NUMBER_UINT || color_num_format == V_028C70_NUMBER_SINT ||
554 color_format == V_028C70_COLOR_8_24 || color_format == V_028C70_COLOR_24_8 ||
555 color_format == V_028C70_COLOR_X24_8_32_FLOAT) {
556 *blendable = false;
557 } else
558 *blendable = true;
559 return color_format != V_028C70_COLOR_INVALID &&
560 color_swap != ~0U &&
561 color_num_format != ~0;
562 }
563
564 static bool radv_is_zs_format_supported(VkFormat format)
565 {
566 return radv_translate_dbformat(format) != V_028040_Z_INVALID || format == VK_FORMAT_S8_UINT;
567 }
568
569 static bool radv_is_filter_minmax_format_supported(VkFormat format)
570 {
571 /* From the Vulkan spec 1.1.71:
572 *
573 * "The following formats must support the
574 * VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT feature with
575 * VK_IMAGE_TILING_OPTIMAL, if they support
576 * VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT."
577 */
578 /* TODO: enable more formats. */
579 switch (format) {
580 case VK_FORMAT_R8_UNORM:
581 case VK_FORMAT_R8_SNORM:
582 case VK_FORMAT_R16_UNORM:
583 case VK_FORMAT_R16_SNORM:
584 case VK_FORMAT_R16_SFLOAT:
585 case VK_FORMAT_R32_SFLOAT:
586 case VK_FORMAT_D16_UNORM:
587 case VK_FORMAT_X8_D24_UNORM_PACK32:
588 case VK_FORMAT_D32_SFLOAT:
589 case VK_FORMAT_D16_UNORM_S8_UINT:
590 case VK_FORMAT_D24_UNORM_S8_UINT:
591 case VK_FORMAT_D32_SFLOAT_S8_UINT:
592 return true;
593 default:
594 return false;
595 }
596 }
597
598 bool
599 radv_device_supports_etc(struct radv_physical_device *physical_device)
600 {
601 return physical_device->rad_info.family == CHIP_VEGA10 ||
602 physical_device->rad_info.family == CHIP_RAVEN ||
603 physical_device->rad_info.family == CHIP_STONEY;
604 }
605
606 static void
607 radv_physical_device_get_format_properties(struct radv_physical_device *physical_device,
608 VkFormat format,
609 VkFormatProperties *out_properties)
610 {
611 VkFormatFeatureFlags linear = 0, tiled = 0, buffer = 0;
612 const struct vk_format_description *desc = vk_format_description(format);
613 bool blendable;
614 bool scaled = false;
615 if (!desc) {
616 out_properties->linearTilingFeatures = linear;
617 out_properties->optimalTilingFeatures = tiled;
618 out_properties->bufferFeatures = buffer;
619 return;
620 }
621
622 if (desc->layout == VK_FORMAT_LAYOUT_ETC &&
623 !radv_device_supports_etc(physical_device)) {
624 out_properties->linearTilingFeatures = linear;
625 out_properties->optimalTilingFeatures = tiled;
626 out_properties->bufferFeatures = buffer;
627 return;
628 }
629
630 if (radv_is_storage_image_format_supported(physical_device, format)) {
631 tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
632 linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
633 }
634
635 if (radv_is_buffer_format_supported(format, &scaled)) {
636 buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
637 if (!scaled)
638 buffer |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
639 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
640 }
641
642 if (vk_format_is_depth_or_stencil(format)) {
643 if (radv_is_zs_format_supported(format)) {
644 tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
645 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
646 tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
647 VK_FORMAT_FEATURE_BLIT_DST_BIT;
648 tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
649 VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
650
651 if (radv_is_filter_minmax_format_supported(format))
652 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT;
653
654 /* Don't support blitting surfaces with depth/stencil. */
655 if (vk_format_is_depth(format) && vk_format_is_stencil(format))
656 tiled &= ~VK_FORMAT_FEATURE_BLIT_DST_BIT;
657
658 /* Don't support linear depth surfaces */
659 linear = 0;
660 }
661 } else {
662 bool linear_sampling;
663 if (radv_is_sampler_format_supported(format, &linear_sampling)) {
664 linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
665 VK_FORMAT_FEATURE_BLIT_SRC_BIT;
666 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
667 VK_FORMAT_FEATURE_BLIT_SRC_BIT;
668
669 if (radv_is_filter_minmax_format_supported(format))
670 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT;
671
672 if (linear_sampling) {
673 linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
674 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
675 }
676
677 /* Don't support blitting for R32G32B32 formats. */
678 if (format == VK_FORMAT_R32G32B32_SFLOAT ||
679 format == VK_FORMAT_R32G32B32_UINT ||
680 format == VK_FORMAT_R32G32B32_SINT) {
681 linear &= ~VK_FORMAT_FEATURE_BLIT_SRC_BIT;
682 }
683 }
684 if (radv_is_colorbuffer_format_supported(format, &blendable)) {
685 linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
686 tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
687 if (blendable) {
688 linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
689 tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
690 }
691 }
692 if (tiled && !scaled) {
693 tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
694 VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
695 }
696
697 /* Tiled formatting does not support NPOT pixel sizes */
698 if (!util_is_power_of_two_or_zero(vk_format_get_blocksize(format)))
699 tiled = 0;
700 }
701
702 if (linear && !scaled) {
703 linear |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
704 VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
705 }
706
707 if (format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT) {
708 buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
709 linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
710 tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
711 }
712
713 switch(format) {
714 case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
715 case VK_FORMAT_A2B10G10R10_SNORM_PACK32:
716 case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
717 case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:
718 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
719 case VK_FORMAT_A2B10G10R10_SINT_PACK32:
720 if (physical_device->rad_info.chip_class <= VI &&
721 physical_device->rad_info.family != CHIP_STONEY) {
722 buffer &= ~(VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
723 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT);
724 linear = 0;
725 tiled = 0;
726 }
727 break;
728 default:
729 break;
730 }
731
732 out_properties->linearTilingFeatures = linear;
733 out_properties->optimalTilingFeatures = tiled;
734 out_properties->bufferFeatures = buffer;
735 }
736
737 uint32_t radv_translate_colorformat(VkFormat format)
738 {
739 const struct vk_format_description *desc = vk_format_description(format);
740
741 #define HAS_SIZE(x,y,z,w) \
742 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
743 desc->channel[2].size == (z) && desc->channel[3].size == (w))
744
745 if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) /* isn't plain */
746 return V_028C70_COLOR_10_11_11;
747
748 if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
749 return V_028C70_COLOR_INVALID;
750
751 /* hw cannot support mixed formats (except depth/stencil, since
752 * stencil is not written to). */
753 if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
754 return V_028C70_COLOR_INVALID;
755
756 switch (desc->nr_channels) {
757 case 1:
758 switch (desc->channel[0].size) {
759 case 8:
760 return V_028C70_COLOR_8;
761 case 16:
762 return V_028C70_COLOR_16;
763 case 32:
764 return V_028C70_COLOR_32;
765 }
766 break;
767 case 2:
768 if (desc->channel[0].size == desc->channel[1].size) {
769 switch (desc->channel[0].size) {
770 case 8:
771 return V_028C70_COLOR_8_8;
772 case 16:
773 return V_028C70_COLOR_16_16;
774 case 32:
775 return V_028C70_COLOR_32_32;
776 }
777 } else if (HAS_SIZE(8,24,0,0)) {
778 return V_028C70_COLOR_24_8;
779 } else if (HAS_SIZE(24,8,0,0)) {
780 return V_028C70_COLOR_8_24;
781 }
782 break;
783 case 3:
784 if (HAS_SIZE(5,6,5,0)) {
785 return V_028C70_COLOR_5_6_5;
786 } else if (HAS_SIZE(32,8,24,0)) {
787 return V_028C70_COLOR_X24_8_32_FLOAT;
788 }
789 break;
790 case 4:
791 if (desc->channel[0].size == desc->channel[1].size &&
792 desc->channel[0].size == desc->channel[2].size &&
793 desc->channel[0].size == desc->channel[3].size) {
794 switch (desc->channel[0].size) {
795 case 4:
796 return V_028C70_COLOR_4_4_4_4;
797 case 8:
798 return V_028C70_COLOR_8_8_8_8;
799 case 16:
800 return V_028C70_COLOR_16_16_16_16;
801 case 32:
802 return V_028C70_COLOR_32_32_32_32;
803 }
804 } else if (HAS_SIZE(5,5,5,1)) {
805 return V_028C70_COLOR_1_5_5_5;
806 } else if (HAS_SIZE(1,5,5,5)) {
807 return V_028C70_COLOR_5_5_5_1;
808 } else if (HAS_SIZE(10,10,10,2)) {
809 return V_028C70_COLOR_2_10_10_10;
810 }
811 break;
812 }
813 return V_028C70_COLOR_INVALID;
814 }
815
816 uint32_t radv_colorformat_endian_swap(uint32_t colorformat)
817 {
818 if (0/*SI_BIG_ENDIAN*/) {
819 switch(colorformat) {
820 /* 8-bit buffers. */
821 case V_028C70_COLOR_8:
822 return V_028C70_ENDIAN_NONE;
823
824 /* 16-bit buffers. */
825 case V_028C70_COLOR_5_6_5:
826 case V_028C70_COLOR_1_5_5_5:
827 case V_028C70_COLOR_4_4_4_4:
828 case V_028C70_COLOR_16:
829 case V_028C70_COLOR_8_8:
830 return V_028C70_ENDIAN_8IN16;
831
832 /* 32-bit buffers. */
833 case V_028C70_COLOR_8_8_8_8:
834 case V_028C70_COLOR_2_10_10_10:
835 case V_028C70_COLOR_8_24:
836 case V_028C70_COLOR_24_8:
837 case V_028C70_COLOR_16_16:
838 return V_028C70_ENDIAN_8IN32;
839
840 /* 64-bit buffers. */
841 case V_028C70_COLOR_16_16_16_16:
842 return V_028C70_ENDIAN_8IN16;
843
844 case V_028C70_COLOR_32_32:
845 return V_028C70_ENDIAN_8IN32;
846
847 /* 128-bit buffers. */
848 case V_028C70_COLOR_32_32_32_32:
849 return V_028C70_ENDIAN_8IN32;
850 default:
851 return V_028C70_ENDIAN_NONE; /* Unsupported. */
852 }
853 } else {
854 return V_028C70_ENDIAN_NONE;
855 }
856 }
857
858 uint32_t radv_translate_dbformat(VkFormat format)
859 {
860 switch (format) {
861 case VK_FORMAT_D16_UNORM:
862 case VK_FORMAT_D16_UNORM_S8_UINT:
863 return V_028040_Z_16;
864 case VK_FORMAT_D32_SFLOAT:
865 case VK_FORMAT_D32_SFLOAT_S8_UINT:
866 return V_028040_Z_32_FLOAT;
867 default:
868 return V_028040_Z_INVALID;
869 }
870 }
871
872 unsigned radv_translate_colorswap(VkFormat format, bool do_endian_swap)
873 {
874 const struct vk_format_description *desc = vk_format_description(format);
875
876 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz)
877
878 if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
879 return V_028C70_SWAP_STD;
880
881 if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
882 return ~0U;
883
884 switch (desc->nr_channels) {
885 case 1:
886 if (HAS_SWIZZLE(0,X))
887 return V_028C70_SWAP_STD; /* X___ */
888 else if (HAS_SWIZZLE(3,X))
889 return V_028C70_SWAP_ALT_REV; /* ___X */
890 break;
891 case 2:
892 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
893 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
894 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
895 return V_028C70_SWAP_STD; /* XY__ */
896 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
897 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
898 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
899 /* YX__ */
900 return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
901 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
902 return V_028C70_SWAP_ALT; /* X__Y */
903 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
904 return V_028C70_SWAP_ALT_REV; /* Y__X */
905 break;
906 case 3:
907 if (HAS_SWIZZLE(0,X))
908 return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
909 else if (HAS_SWIZZLE(0,Z))
910 return V_028C70_SWAP_STD_REV; /* ZYX */
911 break;
912 case 4:
913 /* check the middle channels, the 1st and 4th channel can be NONE */
914 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
915 return V_028C70_SWAP_STD; /* XYZW */
916 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
917 return V_028C70_SWAP_STD_REV; /* WZYX */
918 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
919 return V_028C70_SWAP_ALT; /* ZYXW */
920 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
921 /* YZWX */
922 if (desc->is_array)
923 return V_028C70_SWAP_ALT_REV;
924 else
925 return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
926 }
927 break;
928 }
929 return ~0U;
930 }
931
932 bool radv_format_pack_clear_color(VkFormat format,
933 uint32_t clear_vals[2],
934 VkClearColorValue *value)
935 {
936 const struct vk_format_description *desc = vk_format_description(format);
937
938 if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
939 clear_vals[0] = float3_to_r11g11b10f(value->float32);
940 clear_vals[1] = 0;
941 return true;
942 }
943
944 if (desc->layout != VK_FORMAT_LAYOUT_PLAIN) {
945 fprintf(stderr, "failed to fast clear for non-plain format %d\n", format);
946 return false;
947 }
948
949 if (!util_is_power_of_two_or_zero(desc->block.bits)) {
950 fprintf(stderr, "failed to fast clear for NPOT format %d\n", format);
951 return false;
952 }
953
954 if (desc->block.bits > 64) {
955 /*
956 * We have a 128 bits format, check if the first 3 components are the same.
957 * Every elements has to be 32 bits since we don't support 64-bit formats,
958 * and we can skip swizzling checks as alpha always comes last for these and
959 * we do not care about the rest as they have to be the same.
960 */
961 if (desc->channel[0].type == VK_FORMAT_TYPE_FLOAT) {
962 if (value->float32[0] != value->float32[1] ||
963 value->float32[0] != value->float32[2])
964 return false;
965 } else {
966 if (value->uint32[0] != value->uint32[1] ||
967 value->uint32[0] != value->uint32[2])
968 return false;
969 }
970 clear_vals[0] = value->uint32[0];
971 clear_vals[1] = value->uint32[3];
972 return true;
973 }
974 uint64_t clear_val = 0;
975
976 for (unsigned c = 0; c < 4; ++c) {
977 if (desc->swizzle[c] >= 4)
978 continue;
979
980 const struct vk_format_channel_description *channel = &desc->channel[desc->swizzle[c]];
981 assert(channel->size);
982
983 uint64_t v = 0;
984 if (channel->pure_integer) {
985 v = value->uint32[c] & ((1ULL << channel->size) - 1);
986 } else if (channel->normalized) {
987 if (channel->type == VK_FORMAT_TYPE_UNSIGNED &&
988 desc->swizzle[c] < 3 &&
989 desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
990 assert(channel->size == 8);
991
992 v = util_format_linear_float_to_srgb_8unorm(value->float32[c]);
993 } else if (channel->type == VK_FORMAT_TYPE_UNSIGNED) {
994 v = MAX2(MIN2(value->float32[c], 1.0f), 0.0f) * ((1ULL << channel->size) - 1);
995 } else {
996 v = MAX2(MIN2(value->float32[c], 1.0f), -1.0f) * ((1ULL << (channel->size - 1)) - 1);
997 }
998 } else if (channel->type == VK_FORMAT_TYPE_FLOAT) {
999 if (channel->size == 32) {
1000 memcpy(&v, &value->float32[c], 4);
1001 } else if(channel->size == 16) {
1002 v = util_float_to_half(value->float32[c]);
1003 } else {
1004 fprintf(stderr, "failed to fast clear for unhandled float size in format %d\n", format);
1005 return false;
1006 }
1007 } else {
1008 fprintf(stderr, "failed to fast clear for unhandled component type in format %d\n", format);
1009 return false;
1010 }
1011 clear_val |= (v & ((1ULL << channel->size) - 1)) << channel->shift;
1012 }
1013
1014 clear_vals[0] = clear_val;
1015 clear_vals[1] = clear_val >> 32;
1016
1017 return true;
1018 }
1019
1020 void radv_GetPhysicalDeviceFormatProperties(
1021 VkPhysicalDevice physicalDevice,
1022 VkFormat format,
1023 VkFormatProperties* pFormatProperties)
1024 {
1025 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1026
1027 radv_physical_device_get_format_properties(physical_device,
1028 format,
1029 pFormatProperties);
1030 }
1031
1032 void radv_GetPhysicalDeviceFormatProperties2(
1033 VkPhysicalDevice physicalDevice,
1034 VkFormat format,
1035 VkFormatProperties2* pFormatProperties)
1036 {
1037 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1038
1039 radv_physical_device_get_format_properties(physical_device,
1040 format,
1041 &pFormatProperties->formatProperties);
1042 }
1043
1044 static VkResult radv_get_image_format_properties(struct radv_physical_device *physical_device,
1045 const VkPhysicalDeviceImageFormatInfo2 *info,
1046 VkImageFormatProperties *pImageFormatProperties)
1047
1048 {
1049 VkFormatProperties format_props;
1050 VkFormatFeatureFlags format_feature_flags;
1051 VkExtent3D maxExtent;
1052 uint32_t maxMipLevels;
1053 uint32_t maxArraySize;
1054 VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1055
1056 radv_physical_device_get_format_properties(physical_device, info->format,
1057 &format_props);
1058 if (info->tiling == VK_IMAGE_TILING_LINEAR) {
1059 format_feature_flags = format_props.linearTilingFeatures;
1060 } else if (info->tiling == VK_IMAGE_TILING_OPTIMAL) {
1061 format_feature_flags = format_props.optimalTilingFeatures;
1062 } else {
1063 unreachable("bad VkImageTiling");
1064 }
1065
1066 if (format_feature_flags == 0)
1067 goto unsupported;
1068
1069 if (info->type != VK_IMAGE_TYPE_2D && vk_format_is_depth_or_stencil(info->format))
1070 goto unsupported;
1071
1072 switch (info->type) {
1073 default:
1074 unreachable("bad vkimage type\n");
1075 case VK_IMAGE_TYPE_1D:
1076 maxExtent.width = 16384;
1077 maxExtent.height = 1;
1078 maxExtent.depth = 1;
1079 maxMipLevels = 15; /* log2(maxWidth) + 1 */
1080 maxArraySize = 2048;
1081 break;
1082 case VK_IMAGE_TYPE_2D:
1083 maxExtent.width = 16384;
1084 maxExtent.height = 16384;
1085 maxExtent.depth = 1;
1086 maxMipLevels = 15; /* log2(maxWidth) + 1 */
1087 maxArraySize = 2048;
1088 break;
1089 case VK_IMAGE_TYPE_3D:
1090 maxExtent.width = 2048;
1091 maxExtent.height = 2048;
1092 maxExtent.depth = 2048;
1093 maxMipLevels = 12; /* log2(maxWidth) + 1 */
1094 maxArraySize = 1;
1095 break;
1096 }
1097
1098 if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
1099 info->type == VK_IMAGE_TYPE_2D &&
1100 (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
1101 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1102 !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)) {
1103 sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
1104 }
1105
1106 if (info->tiling == VK_IMAGE_TILING_LINEAR &&
1107 (info->format == VK_FORMAT_R32G32B32_SFLOAT ||
1108 info->format == VK_FORMAT_R32G32B32_SINT ||
1109 info->format == VK_FORMAT_R32G32B32_UINT)) {
1110 /* R32G32B32 is a weird format and the driver currently only
1111 * supports the barely minimum.
1112 * TODO: Implement more if we really need to.
1113 */
1114 if (info->type == VK_IMAGE_TYPE_3D)
1115 goto unsupported;
1116 maxArraySize = 1;
1117 maxMipLevels = 1;
1118 }
1119
1120
1121 /* We can't create 3d compressed 128bpp images that can be rendered to on GFX9 */
1122 if (physical_device->rad_info.chip_class >= GFX9 &&
1123 info->type == VK_IMAGE_TYPE_3D &&
1124 vk_format_get_blocksizebits(info->format) == 128 &&
1125 vk_format_is_compressed(info->format) &&
1126 (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
1127 ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) ||
1128 (info->usage & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))) {
1129 goto unsupported;
1130 }
1131
1132 if (info->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
1133 if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
1134 goto unsupported;
1135 }
1136 }
1137
1138 if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1139 if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
1140 goto unsupported;
1141 }
1142 }
1143
1144 if (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
1145 if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
1146 goto unsupported;
1147 }
1148 }
1149
1150 if (info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1151 if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1152 goto unsupported;
1153 }
1154 }
1155
1156 if (info->usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
1157 if (!(format_feature_flags & VK_FORMAT_FEATURE_TRANSFER_SRC_BIT)) {
1158 goto unsupported;
1159 }
1160 }
1161
1162 if (info->usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
1163 if (!(format_feature_flags & VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
1164 goto unsupported;
1165 }
1166 }
1167
1168 if (info->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
1169 if (!(format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
1170 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))) {
1171 goto unsupported;
1172 }
1173 }
1174
1175 *pImageFormatProperties = (VkImageFormatProperties) {
1176 .maxExtent = maxExtent,
1177 .maxMipLevels = maxMipLevels,
1178 .maxArrayLayers = maxArraySize,
1179 .sampleCounts = sampleCounts,
1180
1181 /* FINISHME: Accurately calculate
1182 * VkImageFormatProperties::maxResourceSize.
1183 */
1184 .maxResourceSize = UINT32_MAX,
1185 };
1186
1187 return VK_SUCCESS;
1188 unsupported:
1189 *pImageFormatProperties = (VkImageFormatProperties) {
1190 .maxExtent = { 0, 0, 0 },
1191 .maxMipLevels = 0,
1192 .maxArrayLayers = 0,
1193 .sampleCounts = 0,
1194 .maxResourceSize = 0,
1195 };
1196
1197 return VK_ERROR_FORMAT_NOT_SUPPORTED;
1198 }
1199
1200 VkResult radv_GetPhysicalDeviceImageFormatProperties(
1201 VkPhysicalDevice physicalDevice,
1202 VkFormat format,
1203 VkImageType type,
1204 VkImageTiling tiling,
1205 VkImageUsageFlags usage,
1206 VkImageCreateFlags createFlags,
1207 VkImageFormatProperties* pImageFormatProperties)
1208 {
1209 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1210
1211 const VkPhysicalDeviceImageFormatInfo2 info = {
1212 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1213 .pNext = NULL,
1214 .format = format,
1215 .type = type,
1216 .tiling = tiling,
1217 .usage = usage,
1218 .flags = createFlags,
1219 };
1220
1221 return radv_get_image_format_properties(physical_device, &info,
1222 pImageFormatProperties);
1223 }
1224
1225 static void
1226 get_external_image_format_properties(const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
1227 VkExternalMemoryHandleTypeFlagBits handleType,
1228 VkExternalMemoryProperties *external_properties)
1229 {
1230 VkExternalMemoryFeatureFlagBits flags = 0;
1231 VkExternalMemoryHandleTypeFlags export_flags = 0;
1232 VkExternalMemoryHandleTypeFlags compat_flags = 0;
1233 switch (handleType) {
1234 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1235 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1236 switch (pImageFormatInfo->type) {
1237 case VK_IMAGE_TYPE_2D:
1238 flags = VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT|VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT|VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1239 compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1240 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1241 break;
1242 default:
1243 break;
1244 }
1245 break;
1246 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1247 flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1248 compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1249 break;
1250 default:
1251 break;
1252 }
1253
1254 *external_properties = (VkExternalMemoryProperties) {
1255 .externalMemoryFeatures = flags,
1256 .exportFromImportedHandleTypes = export_flags,
1257 .compatibleHandleTypes = compat_flags,
1258 };
1259 }
1260
1261 VkResult radv_GetPhysicalDeviceImageFormatProperties2(
1262 VkPhysicalDevice physicalDevice,
1263 const VkPhysicalDeviceImageFormatInfo2 *base_info,
1264 VkImageFormatProperties2 *base_props)
1265 {
1266 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1267 const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1268 VkExternalImageFormatProperties *external_props = NULL;
1269 VkResult result;
1270
1271 result = radv_get_image_format_properties(physical_device, base_info,
1272 &base_props->imageFormatProperties);
1273 if (result != VK_SUCCESS)
1274 return result;
1275
1276 /* Extract input structs */
1277 vk_foreach_struct_const(s, base_info->pNext) {
1278 switch (s->sType) {
1279 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1280 external_info = (const void *) s;
1281 break;
1282 default:
1283 break;
1284 }
1285 }
1286
1287 /* Extract output structs */
1288 vk_foreach_struct(s, base_props->pNext) {
1289 switch (s->sType) {
1290 case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1291 external_props = (void *) s;
1292 break;
1293 default:
1294 break;
1295 }
1296 }
1297
1298 /* From the Vulkan 1.0.97 spec:
1299 *
1300 * If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1301 * behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1302 * present and VkExternalImageFormatProperties will be ignored.
1303 */
1304 if (external_info && external_info->handleType != 0) {
1305 switch (external_info->handleType) {
1306 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1307 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1308 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1309 get_external_image_format_properties(base_info, external_info->handleType,
1310 &external_props->externalMemoryProperties);
1311 break;
1312 default:
1313 /* From the Vulkan 1.0.97 spec:
1314 *
1315 * If handleType is not compatible with the [parameters] specified
1316 * in VkPhysicalDeviceImageFormatInfo2, then
1317 * vkGetPhysicalDeviceImageFormatProperties2 returns
1318 * VK_ERROR_FORMAT_NOT_SUPPORTED.
1319 */
1320 result = vk_errorf(physical_device->instance, VK_ERROR_FORMAT_NOT_SUPPORTED,
1321 "unsupported VkExternalMemoryTypeFlagBitsKHR 0x%x",
1322 external_info->handleType);
1323 goto fail;
1324 }
1325 }
1326
1327 return VK_SUCCESS;
1328
1329 fail:
1330 if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1331 /* From the Vulkan 1.0.97 spec:
1332 *
1333 * If the combination of parameters to
1334 * vkGetPhysicalDeviceImageFormatProperties2 is not supported by
1335 * the implementation for use in vkCreateImage, then all members of
1336 * imageFormatProperties will be filled with zero.
1337 */
1338 base_props->imageFormatProperties = (VkImageFormatProperties) {0};
1339 }
1340
1341 return result;
1342 }
1343
1344 void radv_GetPhysicalDeviceSparseImageFormatProperties(
1345 VkPhysicalDevice physicalDevice,
1346 VkFormat format,
1347 VkImageType type,
1348 uint32_t samples,
1349 VkImageUsageFlags usage,
1350 VkImageTiling tiling,
1351 uint32_t* pNumProperties,
1352 VkSparseImageFormatProperties* pProperties)
1353 {
1354 /* Sparse images are not yet supported. */
1355 *pNumProperties = 0;
1356 }
1357
1358 void radv_GetPhysicalDeviceSparseImageFormatProperties2(
1359 VkPhysicalDevice physicalDevice,
1360 const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
1361 uint32_t *pPropertyCount,
1362 VkSparseImageFormatProperties2 *pProperties)
1363 {
1364 /* Sparse images are not yet supported. */
1365 *pPropertyCount = 0;
1366 }
1367
1368 void radv_GetPhysicalDeviceExternalBufferProperties(
1369 VkPhysicalDevice physicalDevice,
1370 const VkPhysicalDeviceExternalBufferInfo *pExternalBufferInfo,
1371 VkExternalBufferProperties *pExternalBufferProperties)
1372 {
1373 VkExternalMemoryFeatureFlagBits flags = 0;
1374 VkExternalMemoryHandleTypeFlags export_flags = 0;
1375 VkExternalMemoryHandleTypeFlags compat_flags = 0;
1376 switch(pExternalBufferInfo->handleType) {
1377 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1378 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1379 flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1380 VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1381 compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1382 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1383 break;
1384 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1385 flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1386 compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1387 break;
1388 default:
1389 break;
1390 }
1391 pExternalBufferProperties->externalMemoryProperties = (VkExternalMemoryProperties) {
1392 .externalMemoryFeatures = flags,
1393 .exportFromImportedHandleTypes = export_flags,
1394 .compatibleHandleTypes = compat_flags,
1395 };
1396 }
1397
1398 /* DCC channel type categories within which formats can be reinterpreted
1399 * while keeping the same DCC encoding. The swizzle must also match. */
1400 enum dcc_channel_type {
1401 dcc_channel_float32,
1402 dcc_channel_uint32,
1403 dcc_channel_sint32,
1404 dcc_channel_float16,
1405 dcc_channel_uint16,
1406 dcc_channel_sint16,
1407 dcc_channel_uint_10_10_10_2,
1408 dcc_channel_uint8,
1409 dcc_channel_sint8,
1410 dcc_channel_incompatible,
1411 };
1412
1413 /* Return the type of DCC encoding. */
1414 static enum dcc_channel_type
1415 radv_get_dcc_channel_type(const struct vk_format_description *desc)
1416 {
1417 int i;
1418
1419 /* Find the first non-void channel. */
1420 for (i = 0; i < desc->nr_channels; i++)
1421 if (desc->channel[i].type != VK_FORMAT_TYPE_VOID)
1422 break;
1423 if (i == desc->nr_channels)
1424 return dcc_channel_incompatible;
1425
1426 switch (desc->channel[i].size) {
1427 case 32:
1428 if (desc->channel[i].type == VK_FORMAT_TYPE_FLOAT)
1429 return dcc_channel_float32;
1430 if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1431 return dcc_channel_uint32;
1432 return dcc_channel_sint32;
1433 case 16:
1434 if (desc->channel[i].type == VK_FORMAT_TYPE_FLOAT)
1435 return dcc_channel_float16;
1436 if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1437 return dcc_channel_uint16;
1438 return dcc_channel_sint16;
1439 case 10:
1440 return dcc_channel_uint_10_10_10_2;
1441 case 8:
1442 if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1443 return dcc_channel_uint8;
1444 return dcc_channel_sint8;
1445 default:
1446 return dcc_channel_incompatible;
1447 }
1448 }
1449
1450 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1451 bool radv_dcc_formats_compatible(VkFormat format1,
1452 VkFormat format2)
1453 {
1454 const struct vk_format_description *desc1, *desc2;
1455 enum dcc_channel_type type1, type2;
1456 int i;
1457
1458 if (format1 == format2)
1459 return true;
1460
1461 desc1 = vk_format_description(format1);
1462 desc2 = vk_format_description(format2);
1463
1464 if (desc1->nr_channels != desc2->nr_channels)
1465 return false;
1466
1467 /* Swizzles must be the same. */
1468 for (i = 0; i < desc1->nr_channels; i++)
1469 if (desc1->swizzle[i] <= VK_SWIZZLE_W &&
1470 desc2->swizzle[i] <= VK_SWIZZLE_W &&
1471 desc1->swizzle[i] != desc2->swizzle[i])
1472 return false;
1473
1474 type1 = radv_get_dcc_channel_type(desc1);
1475 type2 = radv_get_dcc_channel_type(desc2);
1476
1477 return type1 != dcc_channel_incompatible &&
1478 type2 != dcc_channel_incompatible &&
1479 type1 == type2;
1480 }
1481