2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
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:
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
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
25 #include "radv_private.h"
27 #include "vk_format.h"
29 #include "r600d_common.h"
31 #include "util/u_half.h"
32 #include "util/format_srgb.h"
34 uint32_t radv_translate_buffer_dataformat(const struct vk_format_description
*desc
,
40 if (desc
->format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
)
41 return V_008F0C_BUF_DATA_FORMAT_10_11_11
;
43 if (first_non_void
< 0)
44 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
45 type
= desc
->channel
[first_non_void
].type
;
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
;
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
;
62 switch (desc
->channel
[first_non_void
].size
) {
64 switch (desc
->nr_channels
) {
66 return V_008F0C_BUF_DATA_FORMAT_8
;
68 return V_008F0C_BUF_DATA_FORMAT_8_8
;
70 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8
;
74 switch (desc
->nr_channels
) {
76 return V_008F0C_BUF_DATA_FORMAT_16
;
78 return V_008F0C_BUF_DATA_FORMAT_16_16
;
80 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16
;
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.'
88 if (type
!= VK_FORMAT_TYPE_FLOAT
&&
89 !desc
->channel
[first_non_void
].pure_integer
)
90 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
92 switch (desc
->nr_channels
) {
94 return V_008F0C_BUF_DATA_FORMAT_32
;
96 return V_008F0C_BUF_DATA_FORMAT_32_32
;
98 return V_008F0C_BUF_DATA_FORMAT_32_32_32
;
100 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32
;
105 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
108 uint32_t radv_translate_buffer_numformat(const struct vk_format_description
*desc
,
111 if (desc
->format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
)
112 return V_008F0C_BUF_NUM_FORMAT_FLOAT
;
114 if (first_non_void
< 0)
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
;
124 return V_008F0C_BUF_NUM_FORMAT_SSCALED
;
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
;
132 return V_008F0C_BUF_NUM_FORMAT_USCALED
;
134 case VK_FORMAT_TYPE_FLOAT
:
136 return V_008F0C_BUF_NUM_FORMAT_FLOAT
;
140 uint32_t radv_translate_tex_dataformat(VkFormat format
,
141 const struct vk_format_description
*desc
,
149 /* Colorspace (return non-RGB formats directly). */
150 switch (desc
->colorspace
) {
151 /* Depth stencil formats */
152 case VK_FORMAT_COLORSPACE_ZS
:
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
;
168 case VK_FORMAT_COLORSPACE_YUV
:
169 goto out_unknown
; /* TODO */
171 case VK_FORMAT_COLORSPACE_SRGB
:
172 if (desc
->nr_channels
!= 4 && desc
->nr_channels
!= 1)
180 if (desc
->layout
== VK_FORMAT_LAYOUT_RGTC
) {
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
;
193 if (desc
->layout
== VK_FORMAT_LAYOUT_S3TC
) {
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
;
211 if (desc
->layout
== VK_FORMAT_LAYOUT_BPTC
) {
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
;
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
;
230 /* R8G8Bx_SNORM - TODO CxV8U8 */
232 /* hw cannot support mixed formats (except depth/stencil, since only
234 if (desc
->is_mixed
&& desc
->colorspace
!= VK_FORMAT_COLORSPACE_ZS
)
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
;
242 /* Non-uniform formats. */
244 switch(desc
->nr_channels
) {
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
;
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
;
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
;
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
)
273 return V_008F14_IMG_DATA_FORMAT_2_10_10_10
;
280 if (first_non_void
< 0 || first_non_void
> 3)
283 /* uniform formats */
284 switch (desc
->channel
[first_non_void
].size
) {
286 switch (desc
->nr_channels
) {
287 #if 0 /* Not supported for render targets */
289 return V_008F14_IMG_DATA_FORMAT_4_4
;
292 return V_008F14_IMG_DATA_FORMAT_4_4_4_4
;
296 switch (desc
->nr_channels
) {
298 return V_008F14_IMG_DATA_FORMAT_8
;
300 return V_008F14_IMG_DATA_FORMAT_8_8
;
302 return V_008F14_IMG_DATA_FORMAT_8_8_8_8
;
306 switch (desc
->nr_channels
) {
308 return V_008F14_IMG_DATA_FORMAT_16
;
310 return V_008F14_IMG_DATA_FORMAT_16_16
;
312 return V_008F14_IMG_DATA_FORMAT_16_16_16_16
;
316 switch (desc
->nr_channels
) {
318 return V_008F14_IMG_DATA_FORMAT_32
;
320 return V_008F14_IMG_DATA_FORMAT_32_32
;
321 #if 0 /* Not supported for render targets */
323 return V_008F14_IMG_DATA_FORMAT_32_32_32
;
326 return V_008F14_IMG_DATA_FORMAT_32_32_32_32
;
331 /* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
335 uint32_t radv_translate_tex_numformat(VkFormat format
,
336 const struct vk_format_description
*desc
,
340 case VK_FORMAT_D24_UNORM_S8_UINT
:
341 return V_008F14_IMG_NUM_FORMAT_UNORM
;
343 if (first_non_void
< 0) {
344 if (vk_format_is_compressed(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
;
357 return V_008F14_IMG_NUM_FORMAT_UNORM
;
359 } else if (desc
->layout
== VK_FORMAT_LAYOUT_SUBSAMPLED
) {
360 return V_008F14_IMG_NUM_FORMAT_UNORM
;
362 return V_008F14_IMG_NUM_FORMAT_FLOAT
;
364 } else if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
365 return V_008F14_IMG_NUM_FORMAT_SRGB
;
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
;
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
;
383 return V_008F14_IMG_NUM_FORMAT_USCALED
;
385 return V_008F14_IMG_NUM_FORMAT_UNORM
;
391 uint32_t radv_translate_color_numformat(VkFormat format
,
392 const struct vk_format_description
*desc
,
396 if (first_non_void
== 4 || desc
->channel
[first_non_void
].type
== VK_FORMAT_TYPE_FLOAT
)
397 ntype
= V_028C70_NUMBER_FLOAT
;
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
;
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
;
421 static bool radv_is_sampler_format_supported(VkFormat format
, bool *linear_sampling
)
423 const struct vk_format_description
*desc
= vk_format_description(format
);
425 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
427 num_format
= radv_translate_tex_numformat(format
, desc
,
428 vk_format_get_first_non_void_channel(format
));
430 if (num_format
== V_008F14_IMG_NUM_FORMAT_USCALED
||
431 num_format
== V_008F14_IMG_NUM_FORMAT_SSCALED
)
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;
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;
446 static bool radv_is_storage_image_format_supported(struct radv_physical_device
*physical_device
,
449 const struct vk_format_description
*desc
= vk_format_description(format
);
450 unsigned data_format
, num_format
;
451 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
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
));
459 if(data_format
== ~0 || num_format
== ~0)
462 /* Extracted from the GCN3 ISA document. */
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
:
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. */
499 static bool radv_is_buffer_format_supported(VkFormat format
)
501 const struct vk_format_description
*desc
= vk_format_description(format
);
502 unsigned data_format
, num_format
;
503 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
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
));
511 return data_format
!= V_008F0C_BUF_DATA_FORMAT_INVALID
&&
515 bool radv_is_colorbuffer_format_supported(VkFormat format
, bool *blendable
)
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
,
522 vk_format_get_first_non_void_channel(format
));
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
) {
530 return color_format
!= V_028C70_COLOR_INVALID
&&
532 color_num_format
!= ~0;
535 static bool radv_is_zs_format_supported(VkFormat format
)
537 return radv_translate_dbformat(format
) != V_028040_Z_INVALID
;
541 radv_physical_device_get_format_properties(struct radv_physical_device
*physical_device
,
543 VkFormatProperties
*out_properties
)
545 VkFormatFeatureFlags linear
= 0, tiled
= 0, buffer
= 0;
546 const struct vk_format_description
*desc
= vk_format_description(format
);
549 out_properties
->linearTilingFeatures
= linear
;
550 out_properties
->optimalTilingFeatures
= tiled
;
551 out_properties
->bufferFeatures
= buffer
;
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
;
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
;
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
;
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
;
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
;
588 linear
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT
;
589 tiled
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT
;
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
;
600 out_properties
->linearTilingFeatures
= linear
;
601 out_properties
->optimalTilingFeatures
= tiled
;
602 out_properties
->bufferFeatures
= buffer
;
605 uint32_t radv_translate_colorformat(VkFormat format
)
607 const struct vk_format_description
*desc
= vk_format_description(format
);
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))
613 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
) /* isn't plain */
614 return V_028C70_COLOR_10_11_11
;
616 if (desc
->layout
!= VK_FORMAT_LAYOUT_PLAIN
)
617 return V_028C70_COLOR_INVALID
;
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
;
624 switch (desc
->nr_channels
) {
626 switch (desc
->channel
[0].size
) {
628 return V_028C70_COLOR_8
;
630 return V_028C70_COLOR_16
;
632 return V_028C70_COLOR_32
;
636 if (desc
->channel
[0].size
== desc
->channel
[1].size
) {
637 switch (desc
->channel
[0].size
) {
639 return V_028C70_COLOR_8_8
;
641 return V_028C70_COLOR_16_16
;
643 return V_028C70_COLOR_32_32
;
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
;
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
;
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
) {
664 return V_028C70_COLOR_4_4_4_4
;
666 return V_028C70_COLOR_8_8_8_8
;
668 return V_028C70_COLOR_16_16_16_16
;
670 return V_028C70_COLOR_32_32_32_32
;
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
;
681 return V_028C70_COLOR_INVALID
;
684 uint32_t radv_colorformat_endian_swap(uint32_t colorformat
)
686 if (0/*SI_BIG_ENDIAN*/) {
687 switch(colorformat
) {
689 case V_028C70_COLOR_8
:
690 return V_028C70_ENDIAN_NONE
;
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
;
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
;
708 /* 64-bit buffers. */
709 case V_028C70_COLOR_16_16_16_16
:
710 return V_028C70_ENDIAN_8IN16
;
712 case V_028C70_COLOR_32_32
:
713 return V_028C70_ENDIAN_8IN32
;
715 /* 128-bit buffers. */
716 case V_028C70_COLOR_32_32_32_32
:
717 return V_028C70_ENDIAN_8IN32
;
719 return V_028C70_ENDIAN_NONE
; /* Unsupported. */
722 return V_028C70_ENDIAN_NONE
;
726 uint32_t radv_translate_dbformat(VkFormat 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
;
739 return V_028040_Z_INVALID
;
743 unsigned radv_translate_colorswap(VkFormat format
, bool do_endian_swap
)
745 const struct vk_format_description
*desc
= vk_format_description(format
);
747 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz)
749 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
)
750 return V_0280A0_SWAP_STD
;
752 if (desc
->layout
!= VK_FORMAT_LAYOUT_PLAIN
)
755 switch (desc
->nr_channels
) {
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 */
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
)))
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 */
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 */
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
)) {
794 return V_0280A0_SWAP_ALT_REV
;
796 return (do_endian_swap
? V_0280A0_SWAP_ALT
: V_0280A0_SWAP_ALT_REV
);
803 bool radv_format_pack_clear_color(VkFormat format
,
804 uint32_t clear_vals
[2],
805 VkClearColorValue
*value
)
807 uint8_t r
= 0, g
= 0, b
= 0, a
= 0;
808 const struct vk_format_description
*desc
= vk_format_description(format
);
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]);
824 case VK_FORMAT_R8_UNORM
:
825 case VK_FORMAT_R8_SRGB
:
829 case VK_FORMAT_R8G8_UNORM
:
830 case VK_FORMAT_R8G8_SRGB
:
831 clear_vals
[0] = r
| g
<< 8;
834 case VK_FORMAT_R8G8B8A8_SRGB
:
835 case VK_FORMAT_R8G8B8A8_UNORM
:
836 clear_vals
[0] = r
| g
<< 8 | b
<< 16 | a
<< 24;
839 case VK_FORMAT_B8G8R8A8_SRGB
:
840 case VK_FORMAT_B8G8R8A8_UNORM
:
841 clear_vals
[0] = b
| g
<< 8 | r
<< 16 | a
<< 24;
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;
849 case VK_FORMAT_R8_UINT
:
850 clear_vals
[0] = value
->uint32
[0] & 0xff;
853 case VK_FORMAT_R16_UINT
:
854 clear_vals
[0] = value
->uint32
[0] & 0xffff;
857 case VK_FORMAT_R8G8_UINT
:
858 clear_vals
[0] = value
->uint32
[0] & 0xff;
859 clear_vals
[0] |= (value
->uint32
[1] & 0xff) << 8;
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;
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;
876 case VK_FORMAT_R16G16_UINT
:
877 clear_vals
[0] = value
->uint32
[0] & 0xffff;
878 clear_vals
[0] |= (value
->uint32
[1] & 0xffff) << 16;
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;
887 case VK_FORMAT_R32_UINT
:
888 clear_vals
[0] = value
->uint32
[0];
891 case VK_FORMAT_R32G32_UINT
:
892 clear_vals
[0] = value
->uint32
[0];
893 clear_vals
[1] = value
->uint32
[1];
895 case VK_FORMAT_R32_SINT
:
896 clear_vals
[0] = value
->int32
[0];
899 case VK_FORMAT_R16_SFLOAT
:
900 clear_vals
[0] = util_float_to_half(value
->float32
[0]);
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;
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;
914 case VK_FORMAT_R16_UNORM
:
915 clear_vals
[0] = ((uint16_t)util_iround(CLAMP(value
->float32
[0], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
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;
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;
929 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
932 case VK_FORMAT_R32G32_SFLOAT
:
933 clear_vals
[0] = fui(value
->float32
[0]);
934 clear_vals
[1] = fui(value
->float32
[1]);
936 case VK_FORMAT_R32_SFLOAT
:
938 clear_vals
[0] = fui(value
->float32
[0]);
941 fprintf(stderr
, "failed to fast clear %d\n", format
);
947 void radv_GetPhysicalDeviceFormatProperties(
948 VkPhysicalDevice physicalDevice
,
950 VkFormatProperties
* pFormatProperties
)
952 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
954 radv_physical_device_get_format_properties(physical_device
,
959 VkResult
radv_GetPhysicalDeviceImageFormatProperties(
960 VkPhysicalDevice physicalDevice
,
963 VkImageTiling tiling
,
964 VkImageUsageFlags usage
,
965 VkImageCreateFlags createFlags
,
966 VkImageFormatProperties
* pImageFormatProperties
)
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
;
976 radv_physical_device_get_format_properties(physical_device
, format
,
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
;
983 unreachable("bad VkImageTiling");
986 if (format_feature_flags
== 0)
991 unreachable("bad vkimage type\n");
992 case VK_IMAGE_TYPE_1D
:
993 maxExtent
.width
= 16384;
994 maxExtent
.height
= 1;
996 maxMipLevels
= 15; /* log2(maxWidth) + 1 */
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;
1006 case VK_IMAGE_TYPE_3D
:
1007 maxExtent
.width
= 2048;
1008 maxExtent
.height
= 2048;
1009 maxExtent
.depth
= 2048;
1010 maxMipLevels
= 12; /* log2(maxWidth) + 1 */
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
;
1024 if (usage
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
1025 if (!(format_feature_flags
& VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
)) {
1030 if (usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
1031 if (!(format_feature_flags
& VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
)) {
1036 if (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
1037 if (!(format_feature_flags
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
)) {
1042 if (usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
1043 if (!(format_feature_flags
& VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
1048 *pImageFormatProperties
= (VkImageFormatProperties
) {
1049 .maxExtent
= maxExtent
,
1050 .maxMipLevels
= maxMipLevels
,
1051 .maxArrayLayers
= maxArraySize
,
1052 .sampleCounts
= sampleCounts
,
1054 /* FINISHME: Accurately calculate
1055 * VkImageFormatProperties::maxResourceSize.
1057 .maxResourceSize
= UINT32_MAX
,
1062 *pImageFormatProperties
= (VkImageFormatProperties
) {
1063 .maxExtent
= { 0, 0, 0 },
1065 .maxArrayLayers
= 0,
1067 .maxResourceSize
= 0,
1070 return VK_ERROR_FORMAT_NOT_SUPPORTED
;
1073 void radv_GetPhysicalDeviceSparseImageFormatProperties(
1074 VkPhysicalDevice physicalDevice
,
1078 VkImageUsageFlags usage
,
1079 VkImageTiling tiling
,
1080 uint32_t* pNumProperties
,
1081 VkSparseImageFormatProperties
* pProperties
)
1083 /* Sparse images are not yet supported. */
1084 *pNumProperties
= 0;