radv: add initial non-conformant radv vulkan driver
[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 #include "r600d_common.h"
30
31 #include "util/u_half.h"
32 #include "util/format_srgb.h"
33
34 uint32_t radv_translate_buffer_dataformat(const struct vk_format_description *desc,
35 int first_non_void)
36 {
37 unsigned type;
38 int i;
39
40 if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
41 return V_008F0C_BUF_DATA_FORMAT_10_11_11;
42
43 if (first_non_void < 0)
44 return V_008F0C_BUF_DATA_FORMAT_INVALID;
45 type = desc->channel[first_non_void].type;
46
47 if (type == VK_FORMAT_TYPE_FIXED)
48 return V_008F0C_BUF_DATA_FORMAT_INVALID;
49 if (desc->nr_channels == 4 &&
50 desc->channel[0].size == 10 &&
51 desc->channel[1].size == 10 &&
52 desc->channel[2].size == 10 &&
53 desc->channel[3].size == 2)
54 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
55
56 /* See whether the components are of the same size. */
57 for (i = 0; i < desc->nr_channels; i++) {
58 if (desc->channel[first_non_void].size != desc->channel[i].size)
59 return V_008F0C_BUF_DATA_FORMAT_INVALID;
60 }
61
62 switch (desc->channel[first_non_void].size) {
63 case 8:
64 switch (desc->nr_channels) {
65 case 1:
66 return V_008F0C_BUF_DATA_FORMAT_8;
67 case 2:
68 return V_008F0C_BUF_DATA_FORMAT_8_8;
69 case 4:
70 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
71 }
72 break;
73 case 16:
74 switch (desc->nr_channels) {
75 case 1:
76 return V_008F0C_BUF_DATA_FORMAT_16;
77 case 2:
78 return V_008F0C_BUF_DATA_FORMAT_16_16;
79 case 4:
80 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
81 }
82 break;
83 case 32:
84 /* From the Southern Islands ISA documentation about MTBUF:
85 * 'Memory reads of data in memory that is 32 or 64 bits do not
86 * undergo any format conversion.'
87 */
88 if (type != VK_FORMAT_TYPE_FLOAT &&
89 !desc->channel[first_non_void].pure_integer)
90 return V_008F0C_BUF_DATA_FORMAT_INVALID;
91
92 switch (desc->nr_channels) {
93 case 1:
94 return V_008F0C_BUF_DATA_FORMAT_32;
95 case 2:
96 return V_008F0C_BUF_DATA_FORMAT_32_32;
97 case 3:
98 return V_008F0C_BUF_DATA_FORMAT_32_32_32;
99 case 4:
100 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
101 }
102 break;
103 }
104
105 return V_008F0C_BUF_DATA_FORMAT_INVALID;
106 }
107
108 uint32_t radv_translate_buffer_numformat(const struct vk_format_description *desc,
109 int first_non_void)
110 {
111 if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
112 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
113
114 if (first_non_void < 0)
115 return ~0;
116
117 switch (desc->channel[first_non_void].type) {
118 case VK_FORMAT_TYPE_SIGNED:
119 if (desc->channel[first_non_void].normalized)
120 return V_008F0C_BUF_NUM_FORMAT_SNORM;
121 else if (desc->channel[first_non_void].pure_integer)
122 return V_008F0C_BUF_NUM_FORMAT_SINT;
123 else
124 return V_008F0C_BUF_NUM_FORMAT_SSCALED;
125 break;
126 case VK_FORMAT_TYPE_UNSIGNED:
127 if (desc->channel[first_non_void].normalized)
128 return V_008F0C_BUF_NUM_FORMAT_UNORM;
129 else if (desc->channel[first_non_void].pure_integer)
130 return V_008F0C_BUF_NUM_FORMAT_UINT;
131 else
132 return V_008F0C_BUF_NUM_FORMAT_USCALED;
133 break;
134 case VK_FORMAT_TYPE_FLOAT:
135 default:
136 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
137 }
138 }
139
140 uint32_t radv_translate_tex_dataformat(VkFormat format,
141 const struct vk_format_description *desc,
142 int first_non_void)
143 {
144 bool uniform = true;
145 int i;
146
147 if (!desc)
148 return ~0;
149 /* Colorspace (return non-RGB formats directly). */
150 switch (desc->colorspace) {
151 /* Depth stencil formats */
152 case VK_FORMAT_COLORSPACE_ZS:
153 switch (format) {
154 case VK_FORMAT_D16_UNORM:
155 return V_008F14_IMG_DATA_FORMAT_16;
156 case VK_FORMAT_D24_UNORM_S8_UINT:
157 return V_008F14_IMG_DATA_FORMAT_8_24;
158 case VK_FORMAT_S8_UINT:
159 return V_008F14_IMG_DATA_FORMAT_8;
160 case VK_FORMAT_D32_SFLOAT:
161 return V_008F14_IMG_DATA_FORMAT_32;
162 case VK_FORMAT_D32_SFLOAT_S8_UINT:
163 return V_008F14_IMG_DATA_FORMAT_X24_8_32;
164 default:
165 goto out_unknown;
166 }
167
168 case VK_FORMAT_COLORSPACE_YUV:
169 goto out_unknown; /* TODO */
170
171 case VK_FORMAT_COLORSPACE_SRGB:
172 if (desc->nr_channels != 4 && desc->nr_channels != 1)
173 goto out_unknown;
174 break;
175
176 default:
177 break;
178 }
179
180 if (desc->layout == VK_FORMAT_LAYOUT_RGTC) {
181 switch(format) {
182 case VK_FORMAT_BC4_UNORM_BLOCK:
183 case VK_FORMAT_BC4_SNORM_BLOCK:
184 return V_008F14_IMG_DATA_FORMAT_BC4;
185 case VK_FORMAT_BC5_UNORM_BLOCK:
186 case VK_FORMAT_BC5_SNORM_BLOCK:
187 return V_008F14_IMG_DATA_FORMAT_BC5;
188 default:
189 break;
190 }
191 }
192
193 if (desc->layout == VK_FORMAT_LAYOUT_S3TC) {
194 switch(format) {
195 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
196 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
197 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
198 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
199 return V_008F14_IMG_DATA_FORMAT_BC1;
200 case VK_FORMAT_BC2_UNORM_BLOCK:
201 case VK_FORMAT_BC2_SRGB_BLOCK:
202 return V_008F14_IMG_DATA_FORMAT_BC2;
203 case VK_FORMAT_BC3_UNORM_BLOCK:
204 case VK_FORMAT_BC3_SRGB_BLOCK:
205 return V_008F14_IMG_DATA_FORMAT_BC3;
206 default:
207 break;
208 }
209 }
210
211 if (desc->layout == VK_FORMAT_LAYOUT_BPTC) {
212 switch(format) {
213 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
214 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
215 return V_008F14_IMG_DATA_FORMAT_BC6;
216 case VK_FORMAT_BC7_UNORM_BLOCK:
217 case VK_FORMAT_BC7_SRGB_BLOCK:
218 return V_008F14_IMG_DATA_FORMAT_BC7;
219 default:
220 break;
221 }
222 }
223
224 if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
225 return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
226 } else if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
227 return V_008F14_IMG_DATA_FORMAT_10_11_11;
228 }
229
230 /* R8G8Bx_SNORM - TODO CxV8U8 */
231
232 /* hw cannot support mixed formats (except depth/stencil, since only
233 * depth is read).*/
234 if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
235 goto out_unknown;
236
237 /* See whether the components are of the same size. */
238 for (i = 1; i < desc->nr_channels; i++) {
239 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
240 }
241
242 /* Non-uniform formats. */
243 if (!uniform) {
244 switch(desc->nr_channels) {
245 case 3:
246 if (desc->channel[0].size == 5 &&
247 desc->channel[1].size == 6 &&
248 desc->channel[2].size == 5) {
249 return V_008F14_IMG_DATA_FORMAT_5_6_5;
250 }
251 goto out_unknown;
252 case 4:
253 if (desc->channel[0].size == 5 &&
254 desc->channel[1].size == 5 &&
255 desc->channel[2].size == 5 &&
256 desc->channel[3].size == 1) {
257 return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
258 }
259 if (desc->channel[0].size == 1 &&
260 desc->channel[1].size == 5 &&
261 desc->channel[2].size == 5 &&
262 desc->channel[3].size == 5) {
263 return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
264 }
265 if (desc->channel[0].size == 10 &&
266 desc->channel[1].size == 10 &&
267 desc->channel[2].size == 10 &&
268 desc->channel[3].size == 2) {
269 /* Closed VK driver does this also no 2/10/10/10 snorm */
270 if (desc->channel[0].type == VK_FORMAT_TYPE_SIGNED &&
271 desc->channel[0].normalized)
272 goto out_unknown;
273 return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
274 }
275 goto out_unknown;
276 }
277 goto out_unknown;
278 }
279
280 if (first_non_void < 0 || first_non_void > 3)
281 goto out_unknown;
282
283 /* uniform formats */
284 switch (desc->channel[first_non_void].size) {
285 case 4:
286 switch (desc->nr_channels) {
287 #if 0 /* Not supported for render targets */
288 case 2:
289 return V_008F14_IMG_DATA_FORMAT_4_4;
290 #endif
291 case 4:
292 return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
293 }
294 break;
295 case 8:
296 switch (desc->nr_channels) {
297 case 1:
298 return V_008F14_IMG_DATA_FORMAT_8;
299 case 2:
300 return V_008F14_IMG_DATA_FORMAT_8_8;
301 case 4:
302 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
303 }
304 break;
305 case 16:
306 switch (desc->nr_channels) {
307 case 1:
308 return V_008F14_IMG_DATA_FORMAT_16;
309 case 2:
310 return V_008F14_IMG_DATA_FORMAT_16_16;
311 case 4:
312 return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
313 }
314 break;
315 case 32:
316 switch (desc->nr_channels) {
317 case 1:
318 return V_008F14_IMG_DATA_FORMAT_32;
319 case 2:
320 return V_008F14_IMG_DATA_FORMAT_32_32;
321 #if 0 /* Not supported for render targets */
322 case 3:
323 return V_008F14_IMG_DATA_FORMAT_32_32_32;
324 #endif
325 case 4:
326 return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
327 }
328 }
329
330 out_unknown:
331 /* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
332 return ~0;
333 }
334
335 uint32_t radv_translate_tex_numformat(VkFormat format,
336 const struct vk_format_description *desc,
337 int first_non_void)
338 {
339 switch (format) {
340 case VK_FORMAT_D24_UNORM_S8_UINT:
341 return V_008F14_IMG_NUM_FORMAT_UNORM;
342 default:
343 if (first_non_void < 0) {
344 if (vk_format_is_compressed(format)) {
345 switch (format) {
346 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
347 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
348 case VK_FORMAT_BC2_SRGB_BLOCK:
349 case VK_FORMAT_BC3_SRGB_BLOCK:
350 case VK_FORMAT_BC7_SRGB_BLOCK:
351 return V_008F14_IMG_NUM_FORMAT_SRGB;
352 case VK_FORMAT_BC4_SNORM_BLOCK:
353 case VK_FORMAT_BC5_SNORM_BLOCK:
354 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
355 return V_008F14_IMG_NUM_FORMAT_SNORM;
356 default:
357 return V_008F14_IMG_NUM_FORMAT_UNORM;
358 }
359 } else if (desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
360 return V_008F14_IMG_NUM_FORMAT_UNORM;
361 } else {
362 return V_008F14_IMG_NUM_FORMAT_FLOAT;
363 }
364 } else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
365 return V_008F14_IMG_NUM_FORMAT_SRGB;
366 } else {
367 switch (desc->channel[first_non_void].type) {
368 case VK_FORMAT_TYPE_FLOAT:
369 return V_008F14_IMG_NUM_FORMAT_FLOAT;
370 case VK_FORMAT_TYPE_SIGNED:
371 if (desc->channel[first_non_void].normalized)
372 return V_008F14_IMG_NUM_FORMAT_SNORM;
373 else if (desc->channel[first_non_void].pure_integer)
374 return V_008F14_IMG_NUM_FORMAT_SINT;
375 else
376 return V_008F14_IMG_NUM_FORMAT_SSCALED;
377 case VK_FORMAT_TYPE_UNSIGNED:
378 if (desc->channel[first_non_void].normalized)
379 return V_008F14_IMG_NUM_FORMAT_UNORM;
380 else if (desc->channel[first_non_void].pure_integer)
381 return V_008F14_IMG_NUM_FORMAT_UINT;
382 else
383 return V_008F14_IMG_NUM_FORMAT_USCALED;
384 default:
385 return V_008F14_IMG_NUM_FORMAT_UNORM;
386 }
387 }
388 }
389 }
390
391 uint32_t radv_translate_color_numformat(VkFormat format,
392 const struct vk_format_description *desc,
393 int first_non_void)
394 {
395 unsigned ntype;
396 if (first_non_void == 4 || desc->channel[first_non_void].type == VK_FORMAT_TYPE_FLOAT)
397 ntype = V_028C70_NUMBER_FLOAT;
398 else {
399 ntype = V_028C70_NUMBER_UNORM;
400 if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB)
401 ntype = V_028C70_NUMBER_SRGB;
402 else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_SIGNED) {
403 if (desc->channel[first_non_void].pure_integer) {
404 ntype = V_028C70_NUMBER_SINT;
405 } else if (desc->channel[first_non_void].normalized) {
406 ntype = V_028C70_NUMBER_SNORM;
407 } else
408 ntype = ~0u;
409 } else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_UNSIGNED) {
410 if (desc->channel[first_non_void].pure_integer) {
411 ntype = V_028C70_NUMBER_UINT;
412 } else if (desc->channel[first_non_void].normalized) {
413 ntype = V_028C70_NUMBER_UNORM;
414 } else
415 ntype = ~0u;
416 }
417 }
418 return ntype;
419 }
420
421 static bool radv_is_sampler_format_supported(VkFormat format, bool *linear_sampling)
422 {
423 const struct vk_format_description *desc = vk_format_description(format);
424 uint32_t num_format;
425 if (!desc || format == VK_FORMAT_UNDEFINED)
426 return false;
427 num_format = radv_translate_tex_numformat(format, desc,
428 vk_format_get_first_non_void_channel(format));
429
430 if (num_format == V_008F14_IMG_NUM_FORMAT_USCALED ||
431 num_format == V_008F14_IMG_NUM_FORMAT_SSCALED)
432 return false;
433
434 if (num_format == V_008F14_IMG_NUM_FORMAT_UNORM ||
435 num_format == V_008F14_IMG_NUM_FORMAT_SNORM ||
436 num_format == V_008F14_IMG_NUM_FORMAT_FLOAT ||
437 num_format == V_008F14_IMG_NUM_FORMAT_SRGB)
438 *linear_sampling = true;
439 else
440 *linear_sampling = false;
441 return radv_translate_tex_dataformat(format, vk_format_description(format),
442 vk_format_get_first_non_void_channel(format)) != ~0U;
443 }
444
445
446 static bool radv_is_storage_image_format_supported(struct radv_physical_device *physical_device,
447 VkFormat format)
448 {
449 const struct vk_format_description *desc = vk_format_description(format);
450 unsigned data_format, num_format;
451 if (!desc || format == VK_FORMAT_UNDEFINED)
452 return false;
453
454 data_format = radv_translate_tex_dataformat(format, desc,
455 vk_format_get_first_non_void_channel(format));
456 num_format = radv_translate_tex_numformat(format, desc,
457 vk_format_get_first_non_void_channel(format));
458
459 if(data_format == ~0 || num_format == ~0)
460 return false;
461
462 /* Extracted from the GCN3 ISA document. */
463 switch(num_format) {
464 case V_008F14_IMG_NUM_FORMAT_UNORM:
465 case V_008F14_IMG_NUM_FORMAT_SNORM:
466 case V_008F14_IMG_NUM_FORMAT_UINT:
467 case V_008F14_IMG_NUM_FORMAT_SINT:
468 case V_008F14_IMG_NUM_FORMAT_FLOAT:
469 break;
470 default:
471 return false;
472 }
473
474 switch(data_format) {
475 case V_008F14_IMG_DATA_FORMAT_8:
476 case V_008F14_IMG_DATA_FORMAT_16:
477 case V_008F14_IMG_DATA_FORMAT_8_8:
478 case V_008F14_IMG_DATA_FORMAT_32:
479 case V_008F14_IMG_DATA_FORMAT_16_16:
480 case V_008F14_IMG_DATA_FORMAT_10_11_11:
481 case V_008F14_IMG_DATA_FORMAT_11_11_10:
482 case V_008F14_IMG_DATA_FORMAT_10_10_10_2:
483 case V_008F14_IMG_DATA_FORMAT_2_10_10_10:
484 case V_008F14_IMG_DATA_FORMAT_8_8_8_8:
485 case V_008F14_IMG_DATA_FORMAT_32_32:
486 case V_008F14_IMG_DATA_FORMAT_16_16_16_16:
487 case V_008F14_IMG_DATA_FORMAT_32_32_32_32:
488 case V_008F14_IMG_DATA_FORMAT_5_6_5:
489 case V_008F14_IMG_DATA_FORMAT_1_5_5_5:
490 case V_008F14_IMG_DATA_FORMAT_5_5_5_1:
491 case V_008F14_IMG_DATA_FORMAT_4_4_4_4:
492 /* TODO: FMASK formats. */
493 return true;
494 default:
495 return false;
496 }
497 }
498
499 static bool radv_is_buffer_format_supported(VkFormat format)
500 {
501 const struct vk_format_description *desc = vk_format_description(format);
502 unsigned data_format, num_format;
503 if (!desc || format == VK_FORMAT_UNDEFINED)
504 return false;
505
506 data_format = radv_translate_buffer_dataformat(desc,
507 vk_format_get_first_non_void_channel(format));
508 num_format = radv_translate_buffer_numformat(desc,
509 vk_format_get_first_non_void_channel(format));
510
511 return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID &&
512 num_format != ~0;
513 }
514
515 bool radv_is_colorbuffer_format_supported(VkFormat format, bool *blendable)
516 {
517 const struct vk_format_description *desc = vk_format_description(format);
518 uint32_t color_format = radv_translate_colorformat(format);
519 uint32_t color_swap = radv_translate_colorswap(format, false);
520 uint32_t color_num_format = radv_translate_color_numformat(format,
521 desc,
522 vk_format_get_first_non_void_channel(format));
523
524 if (color_num_format == V_028C70_NUMBER_UINT || color_num_format == V_028C70_NUMBER_SINT ||
525 color_format == V_028C70_COLOR_8_24 || color_format == V_028C70_COLOR_24_8 ||
526 color_format == V_028C70_COLOR_X24_8_32_FLOAT) {
527 *blendable = false;
528 } else
529 *blendable = true;
530 return color_format != V_028C70_COLOR_INVALID &&
531 color_swap != ~0U &&
532 color_num_format != ~0;
533 }
534
535 static bool radv_is_zs_format_supported(VkFormat format)
536 {
537 return radv_translate_dbformat(format) != V_028040_Z_INVALID;
538 }
539
540 static void
541 radv_physical_device_get_format_properties(struct radv_physical_device *physical_device,
542 VkFormat format,
543 VkFormatProperties *out_properties)
544 {
545 VkFormatFeatureFlags linear = 0, tiled = 0, buffer = 0;
546 const struct vk_format_description *desc = vk_format_description(format);
547 bool blendable;
548 if (!desc) {
549 out_properties->linearTilingFeatures = linear;
550 out_properties->optimalTilingFeatures = tiled;
551 out_properties->bufferFeatures = buffer;
552 return;
553 }
554
555 if (radv_is_storage_image_format_supported(physical_device, format)) {
556 tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
557 linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
558 }
559
560 if (radv_is_buffer_format_supported(format)) {
561 buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT |
562 VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
563 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
564 }
565
566 if (vk_format_is_depth_or_stencil(format)) {
567 if (radv_is_zs_format_supported(format))
568 tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
569 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
570 tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
571 VK_FORMAT_FEATURE_BLIT_DST_BIT;
572 } else {
573 bool linear_sampling;
574 if (radv_is_sampler_format_supported(format, &linear_sampling)) {
575 linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
576 VK_FORMAT_FEATURE_BLIT_SRC_BIT;
577 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
578 VK_FORMAT_FEATURE_BLIT_SRC_BIT;
579 if (linear_sampling) {
580 linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
581 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
582 }
583 }
584 if (radv_is_colorbuffer_format_supported(format, &blendable)) {
585 linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
586 tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
587 if (blendable) {
588 linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
589 tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
590 }
591 }
592 }
593
594 if (format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT) {
595 buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
596 linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
597 tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
598 }
599
600 out_properties->linearTilingFeatures = linear;
601 out_properties->optimalTilingFeatures = tiled;
602 out_properties->bufferFeatures = buffer;
603 }
604
605 uint32_t radv_translate_colorformat(VkFormat format)
606 {
607 const struct vk_format_description *desc = vk_format_description(format);
608
609 #define HAS_SIZE(x,y,z,w) \
610 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
611 desc->channel[2].size == (z) && desc->channel[3].size == (w))
612
613 if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) /* isn't plain */
614 return V_028C70_COLOR_10_11_11;
615
616 if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
617 return V_028C70_COLOR_INVALID;
618
619 /* hw cannot support mixed formats (except depth/stencil, since
620 * stencil is not written to). */
621 if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
622 return V_028C70_COLOR_INVALID;
623
624 switch (desc->nr_channels) {
625 case 1:
626 switch (desc->channel[0].size) {
627 case 8:
628 return V_028C70_COLOR_8;
629 case 16:
630 return V_028C70_COLOR_16;
631 case 32:
632 return V_028C70_COLOR_32;
633 }
634 break;
635 case 2:
636 if (desc->channel[0].size == desc->channel[1].size) {
637 switch (desc->channel[0].size) {
638 case 8:
639 return V_028C70_COLOR_8_8;
640 case 16:
641 return V_028C70_COLOR_16_16;
642 case 32:
643 return V_028C70_COLOR_32_32;
644 }
645 } else if (HAS_SIZE(8,24,0,0)) {
646 return V_028C70_COLOR_24_8;
647 } else if (HAS_SIZE(24,8,0,0)) {
648 return V_028C70_COLOR_8_24;
649 }
650 break;
651 case 3:
652 if (HAS_SIZE(5,6,5,0)) {
653 return V_028C70_COLOR_5_6_5;
654 } else if (HAS_SIZE(32,8,24,0)) {
655 return V_028C70_COLOR_X24_8_32_FLOAT;
656 }
657 break;
658 case 4:
659 if (desc->channel[0].size == desc->channel[1].size &&
660 desc->channel[0].size == desc->channel[2].size &&
661 desc->channel[0].size == desc->channel[3].size) {
662 switch (desc->channel[0].size) {
663 case 4:
664 return V_028C70_COLOR_4_4_4_4;
665 case 8:
666 return V_028C70_COLOR_8_8_8_8;
667 case 16:
668 return V_028C70_COLOR_16_16_16_16;
669 case 32:
670 return V_028C70_COLOR_32_32_32_32;
671 }
672 } else if (HAS_SIZE(5,5,5,1)) {
673 return V_028C70_COLOR_1_5_5_5;
674 } else if (HAS_SIZE(1,5,5,5)) {
675 return V_028C70_COLOR_5_5_5_1;
676 } else if (HAS_SIZE(10,10,10,2)) {
677 return V_028C70_COLOR_2_10_10_10;
678 }
679 break;
680 }
681 return V_028C70_COLOR_INVALID;
682 }
683
684 uint32_t radv_colorformat_endian_swap(uint32_t colorformat)
685 {
686 if (0/*SI_BIG_ENDIAN*/) {
687 switch(colorformat) {
688 /* 8-bit buffers. */
689 case V_028C70_COLOR_8:
690 return V_028C70_ENDIAN_NONE;
691
692 /* 16-bit buffers. */
693 case V_028C70_COLOR_5_6_5:
694 case V_028C70_COLOR_1_5_5_5:
695 case V_028C70_COLOR_4_4_4_4:
696 case V_028C70_COLOR_16:
697 case V_028C70_COLOR_8_8:
698 return V_028C70_ENDIAN_8IN16;
699
700 /* 32-bit buffers. */
701 case V_028C70_COLOR_8_8_8_8:
702 case V_028C70_COLOR_2_10_10_10:
703 case V_028C70_COLOR_8_24:
704 case V_028C70_COLOR_24_8:
705 case V_028C70_COLOR_16_16:
706 return V_028C70_ENDIAN_8IN32;
707
708 /* 64-bit buffers. */
709 case V_028C70_COLOR_16_16_16_16:
710 return V_028C70_ENDIAN_8IN16;
711
712 case V_028C70_COLOR_32_32:
713 return V_028C70_ENDIAN_8IN32;
714
715 /* 128-bit buffers. */
716 case V_028C70_COLOR_32_32_32_32:
717 return V_028C70_ENDIAN_8IN32;
718 default:
719 return V_028C70_ENDIAN_NONE; /* Unsupported. */
720 }
721 } else {
722 return V_028C70_ENDIAN_NONE;
723 }
724 }
725
726 uint32_t radv_translate_dbformat(VkFormat format)
727 {
728 switch (format) {
729 case VK_FORMAT_D16_UNORM:
730 case VK_FORMAT_D16_UNORM_S8_UINT:
731 return V_028040_Z_16;
732 case VK_FORMAT_X8_D24_UNORM_PACK32:
733 case VK_FORMAT_D24_UNORM_S8_UINT:
734 return V_028040_Z_24; /* deprecated on SI */
735 case VK_FORMAT_D32_SFLOAT:
736 case VK_FORMAT_D32_SFLOAT_S8_UINT:
737 return V_028040_Z_32_FLOAT;
738 default:
739 return V_028040_Z_INVALID;
740 }
741 }
742
743 unsigned radv_translate_colorswap(VkFormat format, bool do_endian_swap)
744 {
745 const struct vk_format_description *desc = vk_format_description(format);
746
747 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz)
748
749 if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
750 return V_0280A0_SWAP_STD;
751
752 if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
753 return ~0U;
754
755 switch (desc->nr_channels) {
756 case 1:
757 if (HAS_SWIZZLE(0,X))
758 return V_0280A0_SWAP_STD; /* X___ */
759 else if (HAS_SWIZZLE(3,X))
760 return V_0280A0_SWAP_ALT_REV; /* ___X */
761 break;
762 case 2:
763 if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
764 (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
765 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
766 return V_0280A0_SWAP_STD; /* XY__ */
767 else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
768 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
769 (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
770 /* YX__ */
771 return (do_endian_swap ? V_0280A0_SWAP_STD : V_0280A0_SWAP_STD_REV);
772 else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
773 return V_0280A0_SWAP_ALT; /* X__Y */
774 else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
775 return V_0280A0_SWAP_ALT_REV; /* Y__X */
776 break;
777 case 3:
778 if (HAS_SWIZZLE(0,X))
779 return (do_endian_swap ? V_0280A0_SWAP_STD_REV : V_0280A0_SWAP_STD);
780 else if (HAS_SWIZZLE(0,Z))
781 return V_0280A0_SWAP_STD_REV; /* ZYX */
782 break;
783 case 4:
784 /* check the middle channels, the 1st and 4th channel can be NONE */
785 if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
786 return V_0280A0_SWAP_STD; /* XYZW */
787 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
788 return V_0280A0_SWAP_STD_REV; /* WZYX */
789 } else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
790 return V_0280A0_SWAP_ALT; /* ZYXW */
791 } else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
792 /* YZWX */
793 if (desc->is_array)
794 return V_0280A0_SWAP_ALT_REV;
795 else
796 return (do_endian_swap ? V_0280A0_SWAP_ALT : V_0280A0_SWAP_ALT_REV);
797 }
798 break;
799 }
800 return ~0U;
801 }
802
803 bool radv_format_pack_clear_color(VkFormat format,
804 uint32_t clear_vals[2],
805 VkClearColorValue *value)
806 {
807 uint8_t r, g, b, a;
808 const struct vk_format_description *desc = vk_format_description(format);
809
810 if (vk_format_get_component_bits(format, VK_FORMAT_COLORSPACE_RGB, 0) <= 8) {
811 if (desc->colorspace == VK_FORMAT_COLORSPACE_RGB) {
812 r = float_to_ubyte(value->float32[0]);
813 g = float_to_ubyte(value->float32[1]);
814 b = float_to_ubyte(value->float32[2]);
815 a = float_to_ubyte(value->float32[3]);
816 } else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
817 r = util_format_linear_float_to_srgb_8unorm(value->float32[0]);
818 g = util_format_linear_float_to_srgb_8unorm(value->float32[1]);
819 b = util_format_linear_float_to_srgb_8unorm(value->float32[2]);
820 a = float_to_ubyte(value->float32[3]);
821 }
822 }
823 switch (format) {
824 case VK_FORMAT_R8_UNORM:
825 case VK_FORMAT_R8_SRGB:
826 clear_vals[0] = r;
827 clear_vals[1] = 0;
828 break;
829 case VK_FORMAT_R8G8_UNORM:
830 case VK_FORMAT_R8G8_SRGB:
831 clear_vals[0] = r | g << 8;
832 clear_vals[1] = 0;
833 break;
834 case VK_FORMAT_R8G8B8A8_SRGB:
835 case VK_FORMAT_R8G8B8A8_UNORM:
836 clear_vals[0] = r | g << 8 | b << 16 | a << 24;
837 clear_vals[1] = 0;
838 break;
839 case VK_FORMAT_B8G8R8A8_SRGB:
840 case VK_FORMAT_B8G8R8A8_UNORM:
841 clear_vals[0] = b | g << 8 | r << 16 | a << 24;
842 clear_vals[1] = 0;
843 break;
844 case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
845 case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
846 clear_vals[0] = r | g << 8 | b << 16 | a << 24;
847 clear_vals[1] = 0;
848 break;
849 case VK_FORMAT_R8_UINT:
850 clear_vals[0] = value->uint32[0] & 0xff;
851 clear_vals[1] = 0;
852 break;
853 case VK_FORMAT_R16_UINT:
854 clear_vals[0] = value->uint32[0] & 0xffff;
855 clear_vals[1] = 0;
856 break;
857 case VK_FORMAT_R8G8_UINT:
858 clear_vals[0] = value->uint32[0] & 0xff;
859 clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
860 clear_vals[1] = 0;
861 break;
862 case VK_FORMAT_R8G8B8A8_UINT:
863 clear_vals[0] = value->uint32[0] & 0xff;
864 clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
865 clear_vals[0] |= (value->uint32[2] & 0xff) << 16;
866 clear_vals[0] |= (value->uint32[3] & 0xff) << 24;
867 clear_vals[1] = 0;
868 break;
869 case VK_FORMAT_A8B8G8R8_UINT_PACK32:
870 clear_vals[0] = value->uint32[0] & 0xff;
871 clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
872 clear_vals[0] |= (value->uint32[2] & 0xff) << 16;
873 clear_vals[0] |= (value->uint32[3] & 0xff) << 24;
874 clear_vals[1] = 0;
875 break;
876 case VK_FORMAT_R16G16_UINT:
877 clear_vals[0] = value->uint32[0] & 0xffff;
878 clear_vals[0] |= (value->uint32[1] & 0xffff) << 16;
879 clear_vals[1] = 0;
880 break;
881 case VK_FORMAT_R16G16B16A16_UINT:
882 clear_vals[0] = value->uint32[0] & 0xffff;
883 clear_vals[0] |= (value->uint32[1] & 0xffff) << 16;
884 clear_vals[1] = value->uint32[2] & 0xffff;
885 clear_vals[1] |= (value->uint32[3] & 0xffff) << 16;
886 break;
887 case VK_FORMAT_R32_UINT:
888 clear_vals[0] = value->uint32[0];
889 clear_vals[1] = 0;
890 break;
891 case VK_FORMAT_R32G32_UINT:
892 clear_vals[0] = value->uint32[0];
893 clear_vals[1] = value->uint32[1];
894 break;
895 case VK_FORMAT_R32_SINT:
896 clear_vals[0] = value->int32[0];
897 clear_vals[1] = 0;
898 break;
899 case VK_FORMAT_R16_SFLOAT:
900 clear_vals[0] = util_float_to_half(value->float32[0]);
901 clear_vals[1] = 0;
902 break;
903 case VK_FORMAT_R16G16_SFLOAT:
904 clear_vals[0] = util_float_to_half(value->float32[0]);
905 clear_vals[0] |= (uint32_t)util_float_to_half(value->float32[1]) << 16;
906 clear_vals[1] = 0;
907 break;
908 case VK_FORMAT_R16G16B16A16_SFLOAT:
909 clear_vals[0] = util_float_to_half(value->float32[0]);
910 clear_vals[0] |= (uint32_t)util_float_to_half(value->float32[1]) << 16;
911 clear_vals[1] = util_float_to_half(value->float32[2]);
912 clear_vals[1] |= (uint32_t)util_float_to_half(value->float32[3]) << 16;
913 break;
914 case VK_FORMAT_R16_UNORM:
915 clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
916 clear_vals[1] = 0;
917 break;
918 case VK_FORMAT_R16G16_UNORM:
919 clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
920 clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0xffff)) << 16;
921 clear_vals[1] = 0;
922 break;
923 case VK_FORMAT_R16G16B16A16_UNORM:
924 clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
925 clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0xffff)) << 16;
926 clear_vals[1] = ((uint16_t)util_iround(CLAMP(value->float32[2], 0.0f, 1.0f) * 0xffff)) & 0xffff;
927 clear_vals[1] |= ((uint16_t)util_iround(CLAMP(value->float32[3], 0.0f, 1.0f) * 0xffff)) << 16;
928 break;
929 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
930 /* TODO */
931 return false;
932 case VK_FORMAT_R32G32_SFLOAT:
933 clear_vals[0] = fui(value->float32[0]);
934 clear_vals[1] = fui(value->float32[1]);
935 break;
936 case VK_FORMAT_R32_SFLOAT:
937 clear_vals[1] = 0;
938 clear_vals[0] = fui(value->float32[0]);
939 break;
940 default:
941 fprintf(stderr, "failed to fast clear %d\n", format);
942 return false;
943 }
944 return true;
945 }
946
947 void radv_GetPhysicalDeviceFormatProperties(
948 VkPhysicalDevice physicalDevice,
949 VkFormat format,
950 VkFormatProperties* pFormatProperties)
951 {
952 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
953
954 radv_physical_device_get_format_properties(physical_device,
955 format,
956 pFormatProperties);
957 }
958
959 VkResult radv_GetPhysicalDeviceImageFormatProperties(
960 VkPhysicalDevice physicalDevice,
961 VkFormat format,
962 VkImageType type,
963 VkImageTiling tiling,
964 VkImageUsageFlags usage,
965 VkImageCreateFlags createFlags,
966 VkImageFormatProperties* pImageFormatProperties)
967 {
968 RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
969 VkFormatProperties format_props;
970 VkFormatFeatureFlags format_feature_flags;
971 VkExtent3D maxExtent;
972 uint32_t maxMipLevels;
973 uint32_t maxArraySize;
974 VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
975
976 radv_physical_device_get_format_properties(physical_device, format,
977 &format_props);
978 if (tiling == VK_IMAGE_TILING_LINEAR) {
979 format_feature_flags = format_props.linearTilingFeatures;
980 } else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
981 format_feature_flags = format_props.optimalTilingFeatures;
982 } else {
983 unreachable("bad VkImageTiling");
984 }
985
986 if (format_feature_flags == 0)
987 goto unsupported;
988
989 switch (type) {
990 default:
991 unreachable("bad vkimage type\n");
992 case VK_IMAGE_TYPE_1D:
993 maxExtent.width = 16384;
994 maxExtent.height = 1;
995 maxExtent.depth = 1;
996 maxMipLevels = 15; /* log2(maxWidth) + 1 */
997 maxArraySize = 2048;
998 break;
999 case VK_IMAGE_TYPE_2D:
1000 maxExtent.width = 16384;
1001 maxExtent.height = 16384;
1002 maxExtent.depth = 1;
1003 maxMipLevels = 15; /* log2(maxWidth) + 1 */
1004 maxArraySize = 2048;
1005 break;
1006 case VK_IMAGE_TYPE_3D:
1007 maxExtent.width = 2048;
1008 maxExtent.height = 2048;
1009 maxExtent.depth = 2048;
1010 maxMipLevels = 12; /* log2(maxWidth) + 1 */
1011 maxArraySize = 1;
1012 break;
1013 }
1014
1015 if (tiling == VK_IMAGE_TILING_OPTIMAL &&
1016 type == VK_IMAGE_TYPE_2D &&
1017 (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
1018 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1019 !(createFlags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
1020 !(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1021 sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
1022 }
1023
1024 if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
1025 if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
1026 goto unsupported;
1027 }
1028 }
1029
1030 if (usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1031 if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
1032 goto unsupported;
1033 }
1034 }
1035
1036 if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
1037 if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
1038 goto unsupported;
1039 }
1040 }
1041
1042 if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1043 if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1044 goto unsupported;
1045 }
1046 }
1047
1048 *pImageFormatProperties = (VkImageFormatProperties) {
1049 .maxExtent = maxExtent,
1050 .maxMipLevels = maxMipLevels,
1051 .maxArrayLayers = maxArraySize,
1052 .sampleCounts = sampleCounts,
1053
1054 /* FINISHME: Accurately calculate
1055 * VkImageFormatProperties::maxResourceSize.
1056 */
1057 .maxResourceSize = UINT32_MAX,
1058 };
1059
1060 return VK_SUCCESS;
1061 unsupported:
1062 *pImageFormatProperties = (VkImageFormatProperties) {
1063 .maxExtent = { 0, 0, 0 },
1064 .maxMipLevels = 0,
1065 .maxArrayLayers = 0,
1066 .sampleCounts = 0,
1067 .maxResourceSize = 0,
1068 };
1069
1070 return VK_ERROR_FORMAT_NOT_SUPPORTED;
1071 }
1072
1073 void radv_GetPhysicalDeviceSparseImageFormatProperties(
1074 VkPhysicalDevice physicalDevice,
1075 VkFormat format,
1076 VkImageType type,
1077 uint32_t samples,
1078 VkImageUsageFlags usage,
1079 VkImageTiling tiling,
1080 uint32_t* pNumProperties,
1081 VkSparseImageFormatProperties* pProperties)
1082 {
1083 /* Sparse images are not yet supported. */
1084 *pNumProperties = 0;
1085 }