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