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 case VK_FORMAT_X8_D24_UNORM_PACK32
:
158 return V_008F14_IMG_DATA_FORMAT_8_24
;
159 case VK_FORMAT_S8_UINT
:
160 return V_008F14_IMG_DATA_FORMAT_8
;
161 case VK_FORMAT_D32_SFLOAT
:
162 return V_008F14_IMG_DATA_FORMAT_32
;
163 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
164 return V_008F14_IMG_DATA_FORMAT_X24_8_32
;
169 case VK_FORMAT_COLORSPACE_YUV
:
170 goto out_unknown
; /* TODO */
172 case VK_FORMAT_COLORSPACE_SRGB
:
173 if (desc
->nr_channels
!= 4 && desc
->nr_channels
!= 1)
181 if (desc
->layout
== VK_FORMAT_LAYOUT_RGTC
) {
183 case VK_FORMAT_BC4_UNORM_BLOCK
:
184 case VK_FORMAT_BC4_SNORM_BLOCK
:
185 return V_008F14_IMG_DATA_FORMAT_BC4
;
186 case VK_FORMAT_BC5_UNORM_BLOCK
:
187 case VK_FORMAT_BC5_SNORM_BLOCK
:
188 return V_008F14_IMG_DATA_FORMAT_BC5
;
194 if (desc
->layout
== VK_FORMAT_LAYOUT_S3TC
) {
196 case VK_FORMAT_BC1_RGB_UNORM_BLOCK
:
197 case VK_FORMAT_BC1_RGB_SRGB_BLOCK
:
198 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK
:
199 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK
:
200 return V_008F14_IMG_DATA_FORMAT_BC1
;
201 case VK_FORMAT_BC2_UNORM_BLOCK
:
202 case VK_FORMAT_BC2_SRGB_BLOCK
:
203 return V_008F14_IMG_DATA_FORMAT_BC2
;
204 case VK_FORMAT_BC3_UNORM_BLOCK
:
205 case VK_FORMAT_BC3_SRGB_BLOCK
:
206 return V_008F14_IMG_DATA_FORMAT_BC3
;
212 if (desc
->layout
== VK_FORMAT_LAYOUT_BPTC
) {
214 case VK_FORMAT_BC6H_UFLOAT_BLOCK
:
215 case VK_FORMAT_BC6H_SFLOAT_BLOCK
:
216 return V_008F14_IMG_DATA_FORMAT_BC6
;
217 case VK_FORMAT_BC7_UNORM_BLOCK
:
218 case VK_FORMAT_BC7_SRGB_BLOCK
:
219 return V_008F14_IMG_DATA_FORMAT_BC7
;
225 if (format
== VK_FORMAT_E5B9G9R9_UFLOAT_PACK32
) {
226 return V_008F14_IMG_DATA_FORMAT_5_9_9_9
;
227 } else if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
) {
228 return V_008F14_IMG_DATA_FORMAT_10_11_11
;
231 /* R8G8Bx_SNORM - TODO CxV8U8 */
233 /* hw cannot support mixed formats (except depth/stencil, since only
235 if (desc
->is_mixed
&& desc
->colorspace
!= VK_FORMAT_COLORSPACE_ZS
)
238 /* See whether the components are of the same size. */
239 for (i
= 1; i
< desc
->nr_channels
; i
++) {
240 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
243 /* Non-uniform formats. */
245 switch(desc
->nr_channels
) {
247 if (desc
->channel
[0].size
== 5 &&
248 desc
->channel
[1].size
== 6 &&
249 desc
->channel
[2].size
== 5) {
250 return V_008F14_IMG_DATA_FORMAT_5_6_5
;
254 if (desc
->channel
[0].size
== 5 &&
255 desc
->channel
[1].size
== 5 &&
256 desc
->channel
[2].size
== 5 &&
257 desc
->channel
[3].size
== 1) {
258 return V_008F14_IMG_DATA_FORMAT_1_5_5_5
;
260 if (desc
->channel
[0].size
== 1 &&
261 desc
->channel
[1].size
== 5 &&
262 desc
->channel
[2].size
== 5 &&
263 desc
->channel
[3].size
== 5) {
264 return V_008F14_IMG_DATA_FORMAT_5_5_5_1
;
266 if (desc
->channel
[0].size
== 10 &&
267 desc
->channel
[1].size
== 10 &&
268 desc
->channel
[2].size
== 10 &&
269 desc
->channel
[3].size
== 2) {
270 /* Closed VK driver does this also no 2/10/10/10 snorm */
271 if (desc
->channel
[0].type
== VK_FORMAT_TYPE_SIGNED
&&
272 desc
->channel
[0].normalized
)
274 return V_008F14_IMG_DATA_FORMAT_2_10_10_10
;
281 if (first_non_void
< 0 || first_non_void
> 3)
284 /* uniform formats */
285 switch (desc
->channel
[first_non_void
].size
) {
287 switch (desc
->nr_channels
) {
288 #if 0 /* Not supported for render targets */
290 return V_008F14_IMG_DATA_FORMAT_4_4
;
293 return V_008F14_IMG_DATA_FORMAT_4_4_4_4
;
297 switch (desc
->nr_channels
) {
299 return V_008F14_IMG_DATA_FORMAT_8
;
301 return V_008F14_IMG_DATA_FORMAT_8_8
;
303 return V_008F14_IMG_DATA_FORMAT_8_8_8_8
;
307 switch (desc
->nr_channels
) {
309 return V_008F14_IMG_DATA_FORMAT_16
;
311 return V_008F14_IMG_DATA_FORMAT_16_16
;
313 return V_008F14_IMG_DATA_FORMAT_16_16_16_16
;
317 switch (desc
->nr_channels
) {
319 return V_008F14_IMG_DATA_FORMAT_32
;
321 return V_008F14_IMG_DATA_FORMAT_32_32
;
322 #if 0 /* Not supported for render targets */
324 return V_008F14_IMG_DATA_FORMAT_32_32_32
;
327 return V_008F14_IMG_DATA_FORMAT_32_32_32_32
;
332 /* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
336 uint32_t radv_translate_tex_numformat(VkFormat format
,
337 const struct vk_format_description
*desc
,
341 case VK_FORMAT_D24_UNORM_S8_UINT
:
342 return V_008F14_IMG_NUM_FORMAT_UNORM
;
344 if (first_non_void
< 0) {
345 if (vk_format_is_compressed(format
)) {
347 case VK_FORMAT_BC1_RGB_SRGB_BLOCK
:
348 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK
:
349 case VK_FORMAT_BC2_SRGB_BLOCK
:
350 case VK_FORMAT_BC3_SRGB_BLOCK
:
351 case VK_FORMAT_BC7_SRGB_BLOCK
:
352 return V_008F14_IMG_NUM_FORMAT_SRGB
;
353 case VK_FORMAT_BC4_SNORM_BLOCK
:
354 case VK_FORMAT_BC5_SNORM_BLOCK
:
355 case VK_FORMAT_BC6H_SFLOAT_BLOCK
:
356 return V_008F14_IMG_NUM_FORMAT_SNORM
;
358 return V_008F14_IMG_NUM_FORMAT_UNORM
;
360 } else if (desc
->layout
== VK_FORMAT_LAYOUT_SUBSAMPLED
) {
361 return V_008F14_IMG_NUM_FORMAT_UNORM
;
363 return V_008F14_IMG_NUM_FORMAT_FLOAT
;
365 } else if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
366 return V_008F14_IMG_NUM_FORMAT_SRGB
;
368 switch (desc
->channel
[first_non_void
].type
) {
369 case VK_FORMAT_TYPE_FLOAT
:
370 return V_008F14_IMG_NUM_FORMAT_FLOAT
;
371 case VK_FORMAT_TYPE_SIGNED
:
372 if (desc
->channel
[first_non_void
].normalized
)
373 return V_008F14_IMG_NUM_FORMAT_SNORM
;
374 else if (desc
->channel
[first_non_void
].pure_integer
)
375 return V_008F14_IMG_NUM_FORMAT_SINT
;
377 return V_008F14_IMG_NUM_FORMAT_SSCALED
;
378 case VK_FORMAT_TYPE_UNSIGNED
:
379 if (desc
->channel
[first_non_void
].normalized
)
380 return V_008F14_IMG_NUM_FORMAT_UNORM
;
381 else if (desc
->channel
[first_non_void
].pure_integer
)
382 return V_008F14_IMG_NUM_FORMAT_UINT
;
384 return V_008F14_IMG_NUM_FORMAT_USCALED
;
386 return V_008F14_IMG_NUM_FORMAT_UNORM
;
392 uint32_t radv_translate_color_numformat(VkFormat format
,
393 const struct vk_format_description
*desc
,
397 if (first_non_void
== 4 || desc
->channel
[first_non_void
].type
== VK_FORMAT_TYPE_FLOAT
)
398 ntype
= V_028C70_NUMBER_FLOAT
;
400 ntype
= V_028C70_NUMBER_UNORM
;
401 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
)
402 ntype
= V_028C70_NUMBER_SRGB
;
403 else if (desc
->channel
[first_non_void
].type
== VK_FORMAT_TYPE_SIGNED
) {
404 if (desc
->channel
[first_non_void
].pure_integer
) {
405 ntype
= V_028C70_NUMBER_SINT
;
406 } else if (desc
->channel
[first_non_void
].normalized
) {
407 ntype
= V_028C70_NUMBER_SNORM
;
410 } else if (desc
->channel
[first_non_void
].type
== VK_FORMAT_TYPE_UNSIGNED
) {
411 if (desc
->channel
[first_non_void
].pure_integer
) {
412 ntype
= V_028C70_NUMBER_UINT
;
413 } else if (desc
->channel
[first_non_void
].normalized
) {
414 ntype
= V_028C70_NUMBER_UNORM
;
422 static bool radv_is_sampler_format_supported(VkFormat format
, bool *linear_sampling
)
424 const struct vk_format_description
*desc
= vk_format_description(format
);
426 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
428 num_format
= radv_translate_tex_numformat(format
, desc
,
429 vk_format_get_first_non_void_channel(format
));
431 if (num_format
== V_008F14_IMG_NUM_FORMAT_USCALED
||
432 num_format
== V_008F14_IMG_NUM_FORMAT_SSCALED
)
435 if (num_format
== V_008F14_IMG_NUM_FORMAT_UNORM
||
436 num_format
== V_008F14_IMG_NUM_FORMAT_SNORM
||
437 num_format
== V_008F14_IMG_NUM_FORMAT_FLOAT
||
438 num_format
== V_008F14_IMG_NUM_FORMAT_SRGB
)
439 *linear_sampling
= true;
441 *linear_sampling
= false;
442 return radv_translate_tex_dataformat(format
, vk_format_description(format
),
443 vk_format_get_first_non_void_channel(format
)) != ~0U;
447 static bool radv_is_storage_image_format_supported(struct radv_physical_device
*physical_device
,
450 const struct vk_format_description
*desc
= vk_format_description(format
);
451 unsigned data_format
, num_format
;
452 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
455 data_format
= radv_translate_tex_dataformat(format
, desc
,
456 vk_format_get_first_non_void_channel(format
));
457 num_format
= radv_translate_tex_numformat(format
, desc
,
458 vk_format_get_first_non_void_channel(format
));
460 if(data_format
== ~0 || num_format
== ~0)
463 /* Extracted from the GCN3 ISA document. */
465 case V_008F14_IMG_NUM_FORMAT_UNORM
:
466 case V_008F14_IMG_NUM_FORMAT_SNORM
:
467 case V_008F14_IMG_NUM_FORMAT_UINT
:
468 case V_008F14_IMG_NUM_FORMAT_SINT
:
469 case V_008F14_IMG_NUM_FORMAT_FLOAT
:
475 switch(data_format
) {
476 case V_008F14_IMG_DATA_FORMAT_8
:
477 case V_008F14_IMG_DATA_FORMAT_16
:
478 case V_008F14_IMG_DATA_FORMAT_8_8
:
479 case V_008F14_IMG_DATA_FORMAT_32
:
480 case V_008F14_IMG_DATA_FORMAT_16_16
:
481 case V_008F14_IMG_DATA_FORMAT_10_11_11
:
482 case V_008F14_IMG_DATA_FORMAT_11_11_10
:
483 case V_008F14_IMG_DATA_FORMAT_10_10_10_2
:
484 case V_008F14_IMG_DATA_FORMAT_2_10_10_10
:
485 case V_008F14_IMG_DATA_FORMAT_8_8_8_8
:
486 case V_008F14_IMG_DATA_FORMAT_32_32
:
487 case V_008F14_IMG_DATA_FORMAT_16_16_16_16
:
488 case V_008F14_IMG_DATA_FORMAT_32_32_32_32
:
489 case V_008F14_IMG_DATA_FORMAT_5_6_5
:
490 case V_008F14_IMG_DATA_FORMAT_1_5_5_5
:
491 case V_008F14_IMG_DATA_FORMAT_5_5_5_1
:
492 case V_008F14_IMG_DATA_FORMAT_4_4_4_4
:
493 /* TODO: FMASK formats. */
500 static bool radv_is_buffer_format_supported(VkFormat format
)
502 const struct vk_format_description
*desc
= vk_format_description(format
);
503 unsigned data_format
, num_format
;
504 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
507 data_format
= radv_translate_buffer_dataformat(desc
,
508 vk_format_get_first_non_void_channel(format
));
509 num_format
= radv_translate_buffer_numformat(desc
,
510 vk_format_get_first_non_void_channel(format
));
512 return data_format
!= V_008F0C_BUF_DATA_FORMAT_INVALID
&&
516 bool radv_is_colorbuffer_format_supported(VkFormat format
, bool *blendable
)
518 const struct vk_format_description
*desc
= vk_format_description(format
);
519 uint32_t color_format
= radv_translate_colorformat(format
);
520 uint32_t color_swap
= radv_translate_colorswap(format
, false);
521 uint32_t color_num_format
= radv_translate_color_numformat(format
,
523 vk_format_get_first_non_void_channel(format
));
525 if (color_num_format
== V_028C70_NUMBER_UINT
|| color_num_format
== V_028C70_NUMBER_SINT
||
526 color_format
== V_028C70_COLOR_8_24
|| color_format
== V_028C70_COLOR_24_8
||
527 color_format
== V_028C70_COLOR_X24_8_32_FLOAT
) {
531 return color_format
!= V_028C70_COLOR_INVALID
&&
533 color_num_format
!= ~0;
536 static bool radv_is_zs_format_supported(VkFormat format
)
538 return radv_translate_dbformat(format
) != V_028040_Z_INVALID
;
542 radv_physical_device_get_format_properties(struct radv_physical_device
*physical_device
,
544 VkFormatProperties
*out_properties
)
546 VkFormatFeatureFlags linear
= 0, tiled
= 0, buffer
= 0;
547 const struct vk_format_description
*desc
= vk_format_description(format
);
550 out_properties
->linearTilingFeatures
= linear
;
551 out_properties
->optimalTilingFeatures
= tiled
;
552 out_properties
->bufferFeatures
= buffer
;
556 if (radv_is_storage_image_format_supported(physical_device
, format
)) {
557 tiled
|= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
;
558 linear
|= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
;
561 if (radv_is_buffer_format_supported(format
)) {
562 buffer
|= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT
|
563 VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT
|
564 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT
;
567 if (vk_format_is_depth_or_stencil(format
)) {
568 if (radv_is_zs_format_supported(format
))
569 tiled
|= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
;
570 tiled
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
;
571 tiled
|= VK_FORMAT_FEATURE_BLIT_SRC_BIT
|
572 VK_FORMAT_FEATURE_BLIT_DST_BIT
;
574 bool linear_sampling
;
575 if (radv_is_sampler_format_supported(format
, &linear_sampling
)) {
576 linear
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
|
577 VK_FORMAT_FEATURE_BLIT_SRC_BIT
;
578 tiled
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
|
579 VK_FORMAT_FEATURE_BLIT_SRC_BIT
;
580 if (linear_sampling
) {
581 linear
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT
;
582 tiled
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT
;
585 if (radv_is_colorbuffer_format_supported(format
, &blendable
)) {
586 linear
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
| VK_FORMAT_FEATURE_BLIT_DST_BIT
;
587 tiled
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
| VK_FORMAT_FEATURE_BLIT_DST_BIT
;
589 linear
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT
;
590 tiled
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT
;
595 if (format
== VK_FORMAT_R32_UINT
|| format
== VK_FORMAT_R32_SINT
) {
596 buffer
|= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT
;
597 linear
|= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT
;
598 tiled
|= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT
;
601 out_properties
->linearTilingFeatures
= linear
;
602 out_properties
->optimalTilingFeatures
= tiled
;
603 out_properties
->bufferFeatures
= buffer
;
606 uint32_t radv_translate_colorformat(VkFormat format
)
608 const struct vk_format_description
*desc
= vk_format_description(format
);
610 #define HAS_SIZE(x,y,z,w) \
611 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
612 desc->channel[2].size == (z) && desc->channel[3].size == (w))
614 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
) /* isn't plain */
615 return V_028C70_COLOR_10_11_11
;
617 if (desc
->layout
!= VK_FORMAT_LAYOUT_PLAIN
)
618 return V_028C70_COLOR_INVALID
;
620 /* hw cannot support mixed formats (except depth/stencil, since
621 * stencil is not written to). */
622 if (desc
->is_mixed
&& desc
->colorspace
!= VK_FORMAT_COLORSPACE_ZS
)
623 return V_028C70_COLOR_INVALID
;
625 switch (desc
->nr_channels
) {
627 switch (desc
->channel
[0].size
) {
629 return V_028C70_COLOR_8
;
631 return V_028C70_COLOR_16
;
633 return V_028C70_COLOR_32
;
637 if (desc
->channel
[0].size
== desc
->channel
[1].size
) {
638 switch (desc
->channel
[0].size
) {
640 return V_028C70_COLOR_8_8
;
642 return V_028C70_COLOR_16_16
;
644 return V_028C70_COLOR_32_32
;
646 } else if (HAS_SIZE(8,24,0,0)) {
647 return V_028C70_COLOR_24_8
;
648 } else if (HAS_SIZE(24,8,0,0)) {
649 return V_028C70_COLOR_8_24
;
653 if (HAS_SIZE(5,6,5,0)) {
654 return V_028C70_COLOR_5_6_5
;
655 } else if (HAS_SIZE(32,8,24,0)) {
656 return V_028C70_COLOR_X24_8_32_FLOAT
;
660 if (desc
->channel
[0].size
== desc
->channel
[1].size
&&
661 desc
->channel
[0].size
== desc
->channel
[2].size
&&
662 desc
->channel
[0].size
== desc
->channel
[3].size
) {
663 switch (desc
->channel
[0].size
) {
665 return V_028C70_COLOR_4_4_4_4
;
667 return V_028C70_COLOR_8_8_8_8
;
669 return V_028C70_COLOR_16_16_16_16
;
671 return V_028C70_COLOR_32_32_32_32
;
673 } else if (HAS_SIZE(5,5,5,1)) {
674 return V_028C70_COLOR_1_5_5_5
;
675 } else if (HAS_SIZE(1,5,5,5)) {
676 return V_028C70_COLOR_5_5_5_1
;
677 } else if (HAS_SIZE(10,10,10,2)) {
678 return V_028C70_COLOR_2_10_10_10
;
682 return V_028C70_COLOR_INVALID
;
685 uint32_t radv_colorformat_endian_swap(uint32_t colorformat
)
687 if (0/*SI_BIG_ENDIAN*/) {
688 switch(colorformat
) {
690 case V_028C70_COLOR_8
:
691 return V_028C70_ENDIAN_NONE
;
693 /* 16-bit buffers. */
694 case V_028C70_COLOR_5_6_5
:
695 case V_028C70_COLOR_1_5_5_5
:
696 case V_028C70_COLOR_4_4_4_4
:
697 case V_028C70_COLOR_16
:
698 case V_028C70_COLOR_8_8
:
699 return V_028C70_ENDIAN_8IN16
;
701 /* 32-bit buffers. */
702 case V_028C70_COLOR_8_8_8_8
:
703 case V_028C70_COLOR_2_10_10_10
:
704 case V_028C70_COLOR_8_24
:
705 case V_028C70_COLOR_24_8
:
706 case V_028C70_COLOR_16_16
:
707 return V_028C70_ENDIAN_8IN32
;
709 /* 64-bit buffers. */
710 case V_028C70_COLOR_16_16_16_16
:
711 return V_028C70_ENDIAN_8IN16
;
713 case V_028C70_COLOR_32_32
:
714 return V_028C70_ENDIAN_8IN32
;
716 /* 128-bit buffers. */
717 case V_028C70_COLOR_32_32_32_32
:
718 return V_028C70_ENDIAN_8IN32
;
720 return V_028C70_ENDIAN_NONE
; /* Unsupported. */
723 return V_028C70_ENDIAN_NONE
;
727 uint32_t radv_translate_dbformat(VkFormat format
)
730 case VK_FORMAT_D16_UNORM
:
731 case VK_FORMAT_D16_UNORM_S8_UINT
:
732 return V_028040_Z_16
;
733 case VK_FORMAT_X8_D24_UNORM_PACK32
:
734 case VK_FORMAT_D24_UNORM_S8_UINT
:
735 return V_028040_Z_24
; /* deprecated on SI */
736 case VK_FORMAT_D32_SFLOAT
:
737 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
738 return V_028040_Z_32_FLOAT
;
740 return V_028040_Z_INVALID
;
744 unsigned radv_translate_colorswap(VkFormat format
, bool do_endian_swap
)
746 const struct vk_format_description
*desc
= vk_format_description(format
);
748 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz)
750 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
)
751 return V_0280A0_SWAP_STD
;
753 if (desc
->layout
!= VK_FORMAT_LAYOUT_PLAIN
)
756 switch (desc
->nr_channels
) {
758 if (HAS_SWIZZLE(0,X
))
759 return V_0280A0_SWAP_STD
; /* X___ */
760 else if (HAS_SWIZZLE(3,X
))
761 return V_0280A0_SWAP_ALT_REV
; /* ___X */
764 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
765 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
766 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
767 return V_0280A0_SWAP_STD
; /* XY__ */
768 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
769 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
770 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
772 return (do_endian_swap
? V_0280A0_SWAP_STD
: V_0280A0_SWAP_STD_REV
);
773 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
774 return V_0280A0_SWAP_ALT
; /* X__Y */
775 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
776 return V_0280A0_SWAP_ALT_REV
; /* Y__X */
779 if (HAS_SWIZZLE(0,X
))
780 return (do_endian_swap
? V_0280A0_SWAP_STD_REV
: V_0280A0_SWAP_STD
);
781 else if (HAS_SWIZZLE(0,Z
))
782 return V_0280A0_SWAP_STD_REV
; /* ZYX */
785 /* check the middle channels, the 1st and 4th channel can be NONE */
786 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
787 return V_0280A0_SWAP_STD
; /* XYZW */
788 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
789 return V_0280A0_SWAP_STD_REV
; /* WZYX */
790 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
791 return V_0280A0_SWAP_ALT
; /* ZYXW */
792 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
795 return V_0280A0_SWAP_ALT_REV
;
797 return (do_endian_swap
? V_0280A0_SWAP_ALT
: V_0280A0_SWAP_ALT_REV
);
804 bool radv_format_pack_clear_color(VkFormat format
,
805 uint32_t clear_vals
[2],
806 VkClearColorValue
*value
)
808 uint8_t r
= 0, g
= 0, b
= 0, a
= 0;
809 const struct vk_format_description
*desc
= vk_format_description(format
);
811 if (vk_format_get_component_bits(format
, VK_FORMAT_COLORSPACE_RGB
, 0) <= 8) {
812 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_RGB
) {
813 r
= float_to_ubyte(value
->float32
[0]);
814 g
= float_to_ubyte(value
->float32
[1]);
815 b
= float_to_ubyte(value
->float32
[2]);
816 a
= float_to_ubyte(value
->float32
[3]);
817 } else if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
818 r
= util_format_linear_float_to_srgb_8unorm(value
->float32
[0]);
819 g
= util_format_linear_float_to_srgb_8unorm(value
->float32
[1]);
820 b
= util_format_linear_float_to_srgb_8unorm(value
->float32
[2]);
821 a
= float_to_ubyte(value
->float32
[3]);
825 case VK_FORMAT_R8_UNORM
:
826 case VK_FORMAT_R8_SRGB
:
830 case VK_FORMAT_R8G8_UNORM
:
831 case VK_FORMAT_R8G8_SRGB
:
832 clear_vals
[0] = r
| g
<< 8;
835 case VK_FORMAT_R8G8B8A8_SRGB
:
836 case VK_FORMAT_R8G8B8A8_UNORM
:
837 clear_vals
[0] = r
| g
<< 8 | b
<< 16 | a
<< 24;
840 case VK_FORMAT_B8G8R8A8_SRGB
:
841 case VK_FORMAT_B8G8R8A8_UNORM
:
842 clear_vals
[0] = b
| g
<< 8 | r
<< 16 | a
<< 24;
845 case VK_FORMAT_A8B8G8R8_UNORM_PACK32
:
846 case VK_FORMAT_A8B8G8R8_SRGB_PACK32
:
847 clear_vals
[0] = r
| g
<< 8 | b
<< 16 | a
<< 24;
850 case VK_FORMAT_R8_UINT
:
851 clear_vals
[0] = value
->uint32
[0] & 0xff;
854 case VK_FORMAT_R16_UINT
:
855 clear_vals
[0] = value
->uint32
[0] & 0xffff;
858 case VK_FORMAT_R8G8_UINT
:
859 clear_vals
[0] = value
->uint32
[0] & 0xff;
860 clear_vals
[0] |= (value
->uint32
[1] & 0xff) << 8;
863 case VK_FORMAT_R8G8B8A8_UINT
:
864 clear_vals
[0] = value
->uint32
[0] & 0xff;
865 clear_vals
[0] |= (value
->uint32
[1] & 0xff) << 8;
866 clear_vals
[0] |= (value
->uint32
[2] & 0xff) << 16;
867 clear_vals
[0] |= (value
->uint32
[3] & 0xff) << 24;
870 case VK_FORMAT_A8B8G8R8_UINT_PACK32
:
871 clear_vals
[0] = value
->uint32
[0] & 0xff;
872 clear_vals
[0] |= (value
->uint32
[1] & 0xff) << 8;
873 clear_vals
[0] |= (value
->uint32
[2] & 0xff) << 16;
874 clear_vals
[0] |= (value
->uint32
[3] & 0xff) << 24;
877 case VK_FORMAT_R16G16_UINT
:
878 clear_vals
[0] = value
->uint32
[0] & 0xffff;
879 clear_vals
[0] |= (value
->uint32
[1] & 0xffff) << 16;
882 case VK_FORMAT_R16G16B16A16_UINT
:
883 clear_vals
[0] = value
->uint32
[0] & 0xffff;
884 clear_vals
[0] |= (value
->uint32
[1] & 0xffff) << 16;
885 clear_vals
[1] = value
->uint32
[2] & 0xffff;
886 clear_vals
[1] |= (value
->uint32
[3] & 0xffff) << 16;
888 case VK_FORMAT_R32_UINT
:
889 clear_vals
[0] = value
->uint32
[0];
892 case VK_FORMAT_R32G32_UINT
:
893 clear_vals
[0] = value
->uint32
[0];
894 clear_vals
[1] = value
->uint32
[1];
896 case VK_FORMAT_R32_SINT
:
897 clear_vals
[0] = value
->int32
[0];
900 case VK_FORMAT_R16_SFLOAT
:
901 clear_vals
[0] = util_float_to_half(value
->float32
[0]);
904 case VK_FORMAT_R16G16_SFLOAT
:
905 clear_vals
[0] = util_float_to_half(value
->float32
[0]);
906 clear_vals
[0] |= (uint32_t)util_float_to_half(value
->float32
[1]) << 16;
909 case VK_FORMAT_R16G16B16A16_SFLOAT
:
910 clear_vals
[0] = util_float_to_half(value
->float32
[0]);
911 clear_vals
[0] |= (uint32_t)util_float_to_half(value
->float32
[1]) << 16;
912 clear_vals
[1] = util_float_to_half(value
->float32
[2]);
913 clear_vals
[1] |= (uint32_t)util_float_to_half(value
->float32
[3]) << 16;
915 case VK_FORMAT_R16_UNORM
:
916 clear_vals
[0] = ((uint16_t)util_iround(CLAMP(value
->float32
[0], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
919 case VK_FORMAT_R16G16_UNORM
:
920 clear_vals
[0] = ((uint16_t)util_iround(CLAMP(value
->float32
[0], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
921 clear_vals
[0] |= ((uint16_t)util_iround(CLAMP(value
->float32
[1], 0.0f
, 1.0f
) * 0xffff)) << 16;
924 case VK_FORMAT_R16G16B16A16_UNORM
:
925 clear_vals
[0] = ((uint16_t)util_iround(CLAMP(value
->float32
[0], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
926 clear_vals
[0] |= ((uint16_t)util_iround(CLAMP(value
->float32
[1], 0.0f
, 1.0f
) * 0xffff)) << 16;
927 clear_vals
[1] = ((uint16_t)util_iround(CLAMP(value
->float32
[2], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
928 clear_vals
[1] |= ((uint16_t)util_iround(CLAMP(value
->float32
[3], 0.0f
, 1.0f
) * 0xffff)) << 16;
930 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
933 case VK_FORMAT_R32G32_SFLOAT
:
934 clear_vals
[0] = fui(value
->float32
[0]);
935 clear_vals
[1] = fui(value
->float32
[1]);
937 case VK_FORMAT_R32_SFLOAT
:
939 clear_vals
[0] = fui(value
->float32
[0]);
942 fprintf(stderr
, "failed to fast clear %d\n", format
);
948 void radv_GetPhysicalDeviceFormatProperties(
949 VkPhysicalDevice physicalDevice
,
951 VkFormatProperties
* pFormatProperties
)
953 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
955 radv_physical_device_get_format_properties(physical_device
,
960 VkResult
radv_GetPhysicalDeviceImageFormatProperties(
961 VkPhysicalDevice physicalDevice
,
964 VkImageTiling tiling
,
965 VkImageUsageFlags usage
,
966 VkImageCreateFlags createFlags
,
967 VkImageFormatProperties
* pImageFormatProperties
)
969 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
970 VkFormatProperties format_props
;
971 VkFormatFeatureFlags format_feature_flags
;
972 VkExtent3D maxExtent
;
973 uint32_t maxMipLevels
;
974 uint32_t maxArraySize
;
975 VkSampleCountFlags sampleCounts
= VK_SAMPLE_COUNT_1_BIT
;
977 radv_physical_device_get_format_properties(physical_device
, format
,
979 if (tiling
== VK_IMAGE_TILING_LINEAR
) {
980 format_feature_flags
= format_props
.linearTilingFeatures
;
981 } else if (tiling
== VK_IMAGE_TILING_OPTIMAL
) {
982 format_feature_flags
= format_props
.optimalTilingFeatures
;
984 unreachable("bad VkImageTiling");
987 if (format_feature_flags
== 0)
992 unreachable("bad vkimage type\n");
993 case VK_IMAGE_TYPE_1D
:
994 maxExtent
.width
= 16384;
995 maxExtent
.height
= 1;
997 maxMipLevels
= 15; /* log2(maxWidth) + 1 */
1000 case VK_IMAGE_TYPE_2D
:
1001 maxExtent
.width
= 16384;
1002 maxExtent
.height
= 16384;
1003 maxExtent
.depth
= 1;
1004 maxMipLevels
= 15; /* log2(maxWidth) + 1 */
1005 maxArraySize
= 2048;
1007 case VK_IMAGE_TYPE_3D
:
1008 maxExtent
.width
= 2048;
1009 maxExtent
.height
= 2048;
1010 maxExtent
.depth
= 2048;
1011 maxMipLevels
= 12; /* log2(maxWidth) + 1 */
1016 if (tiling
== VK_IMAGE_TILING_OPTIMAL
&&
1017 type
== VK_IMAGE_TYPE_2D
&&
1018 (format_feature_flags
& (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
|
1019 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
)) &&
1020 !(createFlags
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
) &&
1021 !(usage
& VK_IMAGE_USAGE_STORAGE_BIT
)) {
1022 sampleCounts
|= VK_SAMPLE_COUNT_2_BIT
| VK_SAMPLE_COUNT_4_BIT
| VK_SAMPLE_COUNT_8_BIT
;
1025 if (usage
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
1026 if (!(format_feature_flags
& VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
)) {
1031 if (usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
1032 if (!(format_feature_flags
& VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
)) {
1037 if (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
1038 if (!(format_feature_flags
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
)) {
1043 if (usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
1044 if (!(format_feature_flags
& VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
1049 *pImageFormatProperties
= (VkImageFormatProperties
) {
1050 .maxExtent
= maxExtent
,
1051 .maxMipLevels
= maxMipLevels
,
1052 .maxArrayLayers
= maxArraySize
,
1053 .sampleCounts
= sampleCounts
,
1055 /* FINISHME: Accurately calculate
1056 * VkImageFormatProperties::maxResourceSize.
1058 .maxResourceSize
= UINT32_MAX
,
1063 *pImageFormatProperties
= (VkImageFormatProperties
) {
1064 .maxExtent
= { 0, 0, 0 },
1066 .maxArrayLayers
= 0,
1068 .maxResourceSize
= 0,
1071 return VK_ERROR_FORMAT_NOT_SUPPORTED
;
1074 void radv_GetPhysicalDeviceSparseImageFormatProperties(
1075 VkPhysicalDevice physicalDevice
,
1079 VkImageUsageFlags usage
,
1080 VkImageTiling tiling
,
1081 uint32_t* pNumProperties
,
1082 VkSparseImageFormatProperties
* pProperties
)
1084 /* Sparse images are not yet supported. */
1085 *pNumProperties
= 0;