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"
33 #include "util/format_r11g11b10f.h"
35 uint32_t radv_translate_buffer_dataformat(const struct vk_format_description
*desc
,
41 if (desc
->format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
)
42 return V_008F0C_BUF_DATA_FORMAT_10_11_11
;
44 if (first_non_void
< 0)
45 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
46 type
= desc
->channel
[first_non_void
].type
;
48 if (type
== VK_FORMAT_TYPE_FIXED
)
49 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
50 if (desc
->nr_channels
== 4 &&
51 desc
->channel
[0].size
== 10 &&
52 desc
->channel
[1].size
== 10 &&
53 desc
->channel
[2].size
== 10 &&
54 desc
->channel
[3].size
== 2)
55 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10
;
57 /* See whether the components are of the same size. */
58 for (i
= 0; i
< desc
->nr_channels
; i
++) {
59 if (desc
->channel
[first_non_void
].size
!= desc
->channel
[i
].size
)
60 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
63 switch (desc
->channel
[first_non_void
].size
) {
65 switch (desc
->nr_channels
) {
67 return V_008F0C_BUF_DATA_FORMAT_8
;
69 return V_008F0C_BUF_DATA_FORMAT_8_8
;
71 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8
;
75 switch (desc
->nr_channels
) {
77 return V_008F0C_BUF_DATA_FORMAT_16
;
79 return V_008F0C_BUF_DATA_FORMAT_16_16
;
81 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16
;
85 /* From the Southern Islands ISA documentation about MTBUF:
86 * 'Memory reads of data in memory that is 32 or 64 bits do not
87 * undergo any format conversion.'
89 if (type
!= VK_FORMAT_TYPE_FLOAT
&&
90 !desc
->channel
[first_non_void
].pure_integer
)
91 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
93 switch (desc
->nr_channels
) {
95 return V_008F0C_BUF_DATA_FORMAT_32
;
97 return V_008F0C_BUF_DATA_FORMAT_32_32
;
99 return V_008F0C_BUF_DATA_FORMAT_32_32_32
;
101 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32
;
106 return V_008F0C_BUF_DATA_FORMAT_INVALID
;
109 uint32_t radv_translate_buffer_numformat(const struct vk_format_description
*desc
,
112 if (desc
->format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
)
113 return V_008F0C_BUF_NUM_FORMAT_FLOAT
;
115 if (first_non_void
< 0)
118 switch (desc
->channel
[first_non_void
].type
) {
119 case VK_FORMAT_TYPE_SIGNED
:
120 if (desc
->channel
[first_non_void
].normalized
)
121 return V_008F0C_BUF_NUM_FORMAT_SNORM
;
122 else if (desc
->channel
[first_non_void
].pure_integer
)
123 return V_008F0C_BUF_NUM_FORMAT_SINT
;
125 return V_008F0C_BUF_NUM_FORMAT_SSCALED
;
127 case VK_FORMAT_TYPE_UNSIGNED
:
128 if (desc
->channel
[first_non_void
].normalized
)
129 return V_008F0C_BUF_NUM_FORMAT_UNORM
;
130 else if (desc
->channel
[first_non_void
].pure_integer
)
131 return V_008F0C_BUF_NUM_FORMAT_UINT
;
133 return V_008F0C_BUF_NUM_FORMAT_USCALED
;
135 case VK_FORMAT_TYPE_FLOAT
:
137 return V_008F0C_BUF_NUM_FORMAT_FLOAT
;
141 uint32_t radv_translate_tex_dataformat(VkFormat format
,
142 const struct vk_format_description
*desc
,
150 /* Colorspace (return non-RGB formats directly). */
151 switch (desc
->colorspace
) {
152 /* Depth stencil formats */
153 case VK_FORMAT_COLORSPACE_ZS
:
155 case VK_FORMAT_D16_UNORM
:
156 return V_008F14_IMG_DATA_FORMAT_16
;
157 case VK_FORMAT_D24_UNORM_S8_UINT
:
158 case VK_FORMAT_X8_D24_UNORM_PACK32
:
159 return V_008F14_IMG_DATA_FORMAT_8_24
;
160 case VK_FORMAT_S8_UINT
:
161 return V_008F14_IMG_DATA_FORMAT_8
;
162 case VK_FORMAT_D32_SFLOAT
:
163 return V_008F14_IMG_DATA_FORMAT_32
;
164 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
165 return V_008F14_IMG_DATA_FORMAT_X24_8_32
;
170 case VK_FORMAT_COLORSPACE_YUV
:
171 goto out_unknown
; /* TODO */
173 case VK_FORMAT_COLORSPACE_SRGB
:
174 if (desc
->nr_channels
!= 4 && desc
->nr_channels
!= 1)
182 if (desc
->layout
== VK_FORMAT_LAYOUT_RGTC
) {
184 case VK_FORMAT_BC4_UNORM_BLOCK
:
185 case VK_FORMAT_BC4_SNORM_BLOCK
:
186 return V_008F14_IMG_DATA_FORMAT_BC4
;
187 case VK_FORMAT_BC5_UNORM_BLOCK
:
188 case VK_FORMAT_BC5_SNORM_BLOCK
:
189 return V_008F14_IMG_DATA_FORMAT_BC5
;
195 if (desc
->layout
== VK_FORMAT_LAYOUT_S3TC
) {
197 case VK_FORMAT_BC1_RGB_UNORM_BLOCK
:
198 case VK_FORMAT_BC1_RGB_SRGB_BLOCK
:
199 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK
:
200 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK
:
201 return V_008F14_IMG_DATA_FORMAT_BC1
;
202 case VK_FORMAT_BC2_UNORM_BLOCK
:
203 case VK_FORMAT_BC2_SRGB_BLOCK
:
204 return V_008F14_IMG_DATA_FORMAT_BC2
;
205 case VK_FORMAT_BC3_UNORM_BLOCK
:
206 case VK_FORMAT_BC3_SRGB_BLOCK
:
207 return V_008F14_IMG_DATA_FORMAT_BC3
;
213 if (desc
->layout
== VK_FORMAT_LAYOUT_BPTC
) {
215 case VK_FORMAT_BC6H_UFLOAT_BLOCK
:
216 case VK_FORMAT_BC6H_SFLOAT_BLOCK
:
217 return V_008F14_IMG_DATA_FORMAT_BC6
;
218 case VK_FORMAT_BC7_UNORM_BLOCK
:
219 case VK_FORMAT_BC7_SRGB_BLOCK
:
220 return V_008F14_IMG_DATA_FORMAT_BC7
;
226 if (format
== VK_FORMAT_E5B9G9R9_UFLOAT_PACK32
) {
227 return V_008F14_IMG_DATA_FORMAT_5_9_9_9
;
228 } else if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
) {
229 return V_008F14_IMG_DATA_FORMAT_10_11_11
;
232 /* R8G8Bx_SNORM - TODO CxV8U8 */
234 /* hw cannot support mixed formats (except depth/stencil, since only
236 if (desc
->is_mixed
&& desc
->colorspace
!= VK_FORMAT_COLORSPACE_ZS
)
239 /* See whether the components are of the same size. */
240 for (i
= 1; i
< desc
->nr_channels
; i
++) {
241 uniform
= uniform
&& desc
->channel
[0].size
== desc
->channel
[i
].size
;
244 /* Non-uniform formats. */
246 switch(desc
->nr_channels
) {
248 if (desc
->channel
[0].size
== 5 &&
249 desc
->channel
[1].size
== 6 &&
250 desc
->channel
[2].size
== 5) {
251 return V_008F14_IMG_DATA_FORMAT_5_6_5
;
255 if (desc
->channel
[0].size
== 5 &&
256 desc
->channel
[1].size
== 5 &&
257 desc
->channel
[2].size
== 5 &&
258 desc
->channel
[3].size
== 1) {
259 return V_008F14_IMG_DATA_FORMAT_1_5_5_5
;
261 if (desc
->channel
[0].size
== 1 &&
262 desc
->channel
[1].size
== 5 &&
263 desc
->channel
[2].size
== 5 &&
264 desc
->channel
[3].size
== 5) {
265 return V_008F14_IMG_DATA_FORMAT_5_5_5_1
;
267 if (desc
->channel
[0].size
== 10 &&
268 desc
->channel
[1].size
== 10 &&
269 desc
->channel
[2].size
== 10 &&
270 desc
->channel
[3].size
== 2) {
271 /* Closed VK driver does this also no 2/10/10/10 snorm */
272 if (desc
->channel
[0].type
== VK_FORMAT_TYPE_SIGNED
&&
273 desc
->channel
[0].normalized
)
275 return V_008F14_IMG_DATA_FORMAT_2_10_10_10
;
282 if (first_non_void
< 0 || first_non_void
> 3)
285 /* uniform formats */
286 switch (desc
->channel
[first_non_void
].size
) {
288 switch (desc
->nr_channels
) {
289 #if 0 /* Not supported for render targets */
291 return V_008F14_IMG_DATA_FORMAT_4_4
;
294 return V_008F14_IMG_DATA_FORMAT_4_4_4_4
;
298 switch (desc
->nr_channels
) {
300 return V_008F14_IMG_DATA_FORMAT_8
;
302 return V_008F14_IMG_DATA_FORMAT_8_8
;
304 return V_008F14_IMG_DATA_FORMAT_8_8_8_8
;
308 switch (desc
->nr_channels
) {
310 return V_008F14_IMG_DATA_FORMAT_16
;
312 return V_008F14_IMG_DATA_FORMAT_16_16
;
314 return V_008F14_IMG_DATA_FORMAT_16_16_16_16
;
318 switch (desc
->nr_channels
) {
320 return V_008F14_IMG_DATA_FORMAT_32
;
322 return V_008F14_IMG_DATA_FORMAT_32_32
;
323 #if 0 /* Not supported for render targets */
325 return V_008F14_IMG_DATA_FORMAT_32_32_32
;
328 return V_008F14_IMG_DATA_FORMAT_32_32_32_32
;
333 /* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
337 uint32_t radv_translate_tex_numformat(VkFormat format
,
338 const struct vk_format_description
*desc
,
342 case VK_FORMAT_D24_UNORM_S8_UINT
:
343 return V_008F14_IMG_NUM_FORMAT_UNORM
;
345 if (first_non_void
< 0) {
346 if (vk_format_is_compressed(format
)) {
348 case VK_FORMAT_BC1_RGB_SRGB_BLOCK
:
349 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK
:
350 case VK_FORMAT_BC2_SRGB_BLOCK
:
351 case VK_FORMAT_BC3_SRGB_BLOCK
:
352 case VK_FORMAT_BC7_SRGB_BLOCK
:
353 return V_008F14_IMG_NUM_FORMAT_SRGB
;
354 case VK_FORMAT_BC4_SNORM_BLOCK
:
355 case VK_FORMAT_BC5_SNORM_BLOCK
:
356 case VK_FORMAT_BC6H_SFLOAT_BLOCK
:
357 return V_008F14_IMG_NUM_FORMAT_SNORM
;
359 return V_008F14_IMG_NUM_FORMAT_UNORM
;
361 } else if (desc
->layout
== VK_FORMAT_LAYOUT_SUBSAMPLED
) {
362 return V_008F14_IMG_NUM_FORMAT_UNORM
;
364 return V_008F14_IMG_NUM_FORMAT_FLOAT
;
366 } else if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
367 return V_008F14_IMG_NUM_FORMAT_SRGB
;
369 switch (desc
->channel
[first_non_void
].type
) {
370 case VK_FORMAT_TYPE_FLOAT
:
371 return V_008F14_IMG_NUM_FORMAT_FLOAT
;
372 case VK_FORMAT_TYPE_SIGNED
:
373 if (desc
->channel
[first_non_void
].normalized
)
374 return V_008F14_IMG_NUM_FORMAT_SNORM
;
375 else if (desc
->channel
[first_non_void
].pure_integer
)
376 return V_008F14_IMG_NUM_FORMAT_SINT
;
378 return V_008F14_IMG_NUM_FORMAT_SSCALED
;
379 case VK_FORMAT_TYPE_UNSIGNED
:
380 if (desc
->channel
[first_non_void
].normalized
)
381 return V_008F14_IMG_NUM_FORMAT_UNORM
;
382 else if (desc
->channel
[first_non_void
].pure_integer
)
383 return V_008F14_IMG_NUM_FORMAT_UINT
;
385 return V_008F14_IMG_NUM_FORMAT_USCALED
;
387 return V_008F14_IMG_NUM_FORMAT_UNORM
;
393 uint32_t radv_translate_color_numformat(VkFormat format
,
394 const struct vk_format_description
*desc
,
398 if (first_non_void
== -1 || desc
->channel
[first_non_void
].type
== VK_FORMAT_TYPE_FLOAT
)
399 ntype
= V_028C70_NUMBER_FLOAT
;
401 ntype
= V_028C70_NUMBER_UNORM
;
402 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
)
403 ntype
= V_028C70_NUMBER_SRGB
;
404 else if (desc
->channel
[first_non_void
].type
== VK_FORMAT_TYPE_SIGNED
) {
405 if (desc
->channel
[first_non_void
].pure_integer
) {
406 ntype
= V_028C70_NUMBER_SINT
;
407 } else if (desc
->channel
[first_non_void
].normalized
) {
408 ntype
= V_028C70_NUMBER_SNORM
;
411 } else if (desc
->channel
[first_non_void
].type
== VK_FORMAT_TYPE_UNSIGNED
) {
412 if (desc
->channel
[first_non_void
].pure_integer
) {
413 ntype
= V_028C70_NUMBER_UINT
;
414 } else if (desc
->channel
[first_non_void
].normalized
) {
415 ntype
= V_028C70_NUMBER_UNORM
;
423 static bool radv_is_sampler_format_supported(VkFormat format
, bool *linear_sampling
)
425 const struct vk_format_description
*desc
= vk_format_description(format
);
427 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
429 num_format
= radv_translate_tex_numformat(format
, desc
,
430 vk_format_get_first_non_void_channel(format
));
432 if (num_format
== V_008F14_IMG_NUM_FORMAT_USCALED
||
433 num_format
== V_008F14_IMG_NUM_FORMAT_SSCALED
)
436 if (num_format
== V_008F14_IMG_NUM_FORMAT_UNORM
||
437 num_format
== V_008F14_IMG_NUM_FORMAT_SNORM
||
438 num_format
== V_008F14_IMG_NUM_FORMAT_FLOAT
||
439 num_format
== V_008F14_IMG_NUM_FORMAT_SRGB
)
440 *linear_sampling
= true;
442 *linear_sampling
= false;
443 return radv_translate_tex_dataformat(format
, vk_format_description(format
),
444 vk_format_get_first_non_void_channel(format
)) != ~0U;
448 static bool radv_is_storage_image_format_supported(struct radv_physical_device
*physical_device
,
451 const struct vk_format_description
*desc
= vk_format_description(format
);
452 unsigned data_format
, num_format
;
453 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
456 data_format
= radv_translate_tex_dataformat(format
, desc
,
457 vk_format_get_first_non_void_channel(format
));
458 num_format
= radv_translate_tex_numformat(format
, desc
,
459 vk_format_get_first_non_void_channel(format
));
461 if(data_format
== ~0 || num_format
== ~0)
464 /* Extracted from the GCN3 ISA document. */
466 case V_008F14_IMG_NUM_FORMAT_UNORM
:
467 case V_008F14_IMG_NUM_FORMAT_SNORM
:
468 case V_008F14_IMG_NUM_FORMAT_UINT
:
469 case V_008F14_IMG_NUM_FORMAT_SINT
:
470 case V_008F14_IMG_NUM_FORMAT_FLOAT
:
476 switch(data_format
) {
477 case V_008F14_IMG_DATA_FORMAT_8
:
478 case V_008F14_IMG_DATA_FORMAT_16
:
479 case V_008F14_IMG_DATA_FORMAT_8_8
:
480 case V_008F14_IMG_DATA_FORMAT_32
:
481 case V_008F14_IMG_DATA_FORMAT_16_16
:
482 case V_008F14_IMG_DATA_FORMAT_10_11_11
:
483 case V_008F14_IMG_DATA_FORMAT_11_11_10
:
484 case V_008F14_IMG_DATA_FORMAT_10_10_10_2
:
485 case V_008F14_IMG_DATA_FORMAT_2_10_10_10
:
486 case V_008F14_IMG_DATA_FORMAT_8_8_8_8
:
487 case V_008F14_IMG_DATA_FORMAT_32_32
:
488 case V_008F14_IMG_DATA_FORMAT_16_16_16_16
:
489 case V_008F14_IMG_DATA_FORMAT_32_32_32_32
:
490 case V_008F14_IMG_DATA_FORMAT_5_6_5
:
491 case V_008F14_IMG_DATA_FORMAT_1_5_5_5
:
492 case V_008F14_IMG_DATA_FORMAT_5_5_5_1
:
493 case V_008F14_IMG_DATA_FORMAT_4_4_4_4
:
494 /* TODO: FMASK formats. */
501 static bool radv_is_buffer_format_supported(VkFormat format
)
503 const struct vk_format_description
*desc
= vk_format_description(format
);
504 unsigned data_format
, num_format
;
505 if (!desc
|| format
== VK_FORMAT_UNDEFINED
)
508 data_format
= radv_translate_buffer_dataformat(desc
,
509 vk_format_get_first_non_void_channel(format
));
510 num_format
= radv_translate_buffer_numformat(desc
,
511 vk_format_get_first_non_void_channel(format
));
513 return data_format
!= V_008F0C_BUF_DATA_FORMAT_INVALID
&&
517 bool radv_is_colorbuffer_format_supported(VkFormat format
, bool *blendable
)
519 const struct vk_format_description
*desc
= vk_format_description(format
);
520 uint32_t color_format
= radv_translate_colorformat(format
);
521 uint32_t color_swap
= radv_translate_colorswap(format
, false);
522 uint32_t color_num_format
= radv_translate_color_numformat(format
,
524 vk_format_get_first_non_void_channel(format
));
526 if (color_num_format
== V_028C70_NUMBER_UINT
|| color_num_format
== V_028C70_NUMBER_SINT
||
527 color_format
== V_028C70_COLOR_8_24
|| color_format
== V_028C70_COLOR_24_8
||
528 color_format
== V_028C70_COLOR_X24_8_32_FLOAT
) {
532 return color_format
!= V_028C70_COLOR_INVALID
&&
534 color_num_format
!= ~0;
537 static bool radv_is_zs_format_supported(VkFormat format
)
539 return radv_translate_dbformat(format
) != V_028040_Z_INVALID
;
543 radv_physical_device_get_format_properties(struct radv_physical_device
*physical_device
,
545 VkFormatProperties
*out_properties
)
547 VkFormatFeatureFlags linear
= 0, tiled
= 0, buffer
= 0;
548 const struct vk_format_description
*desc
= vk_format_description(format
);
551 out_properties
->linearTilingFeatures
= linear
;
552 out_properties
->optimalTilingFeatures
= tiled
;
553 out_properties
->bufferFeatures
= buffer
;
557 if (radv_is_storage_image_format_supported(physical_device
, format
)) {
558 tiled
|= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
;
559 linear
|= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
;
562 if (radv_is_buffer_format_supported(format
)) {
563 buffer
|= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT
|
564 VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT
|
565 VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT
;
568 if (vk_format_is_depth_or_stencil(format
)) {
569 if (radv_is_zs_format_supported(format
)) {
570 tiled
|= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
;
571 tiled
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
;
572 tiled
|= VK_FORMAT_FEATURE_BLIT_SRC_BIT
|
573 VK_FORMAT_FEATURE_BLIT_DST_BIT
;
574 tiled
|= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR
|
575 VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR
;
578 bool linear_sampling
;
579 if (radv_is_sampler_format_supported(format
, &linear_sampling
)) {
580 linear
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
|
581 VK_FORMAT_FEATURE_BLIT_SRC_BIT
;
582 tiled
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
|
583 VK_FORMAT_FEATURE_BLIT_SRC_BIT
;
584 if (linear_sampling
) {
585 linear
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT
;
586 tiled
|= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT
;
589 if (radv_is_colorbuffer_format_supported(format
, &blendable
)) {
590 linear
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
| VK_FORMAT_FEATURE_BLIT_DST_BIT
;
591 tiled
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
| VK_FORMAT_FEATURE_BLIT_DST_BIT
;
593 linear
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT
;
594 tiled
|= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT
;
597 if (util_is_power_of_two(vk_format_get_blocksize(format
))) {
598 tiled
|= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR
|
599 VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR
;
603 if (util_is_power_of_two(vk_format_get_blocksize(format
))) {
604 linear
|= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR
|
605 VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR
;
608 if (format
== VK_FORMAT_R32_UINT
|| format
== VK_FORMAT_R32_SINT
) {
609 buffer
|= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT
;
610 linear
|= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT
;
611 tiled
|= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT
;
614 out_properties
->linearTilingFeatures
= linear
;
615 out_properties
->optimalTilingFeatures
= tiled
;
616 out_properties
->bufferFeatures
= buffer
;
619 uint32_t radv_translate_colorformat(VkFormat format
)
621 const struct vk_format_description
*desc
= vk_format_description(format
);
623 #define HAS_SIZE(x,y,z,w) \
624 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
625 desc->channel[2].size == (z) && desc->channel[3].size == (w))
627 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
) /* isn't plain */
628 return V_028C70_COLOR_10_11_11
;
630 if (desc
->layout
!= VK_FORMAT_LAYOUT_PLAIN
)
631 return V_028C70_COLOR_INVALID
;
633 /* hw cannot support mixed formats (except depth/stencil, since
634 * stencil is not written to). */
635 if (desc
->is_mixed
&& desc
->colorspace
!= VK_FORMAT_COLORSPACE_ZS
)
636 return V_028C70_COLOR_INVALID
;
638 switch (desc
->nr_channels
) {
640 switch (desc
->channel
[0].size
) {
642 return V_028C70_COLOR_8
;
644 return V_028C70_COLOR_16
;
646 return V_028C70_COLOR_32
;
650 if (desc
->channel
[0].size
== desc
->channel
[1].size
) {
651 switch (desc
->channel
[0].size
) {
653 return V_028C70_COLOR_8_8
;
655 return V_028C70_COLOR_16_16
;
657 return V_028C70_COLOR_32_32
;
659 } else if (HAS_SIZE(8,24,0,0)) {
660 return V_028C70_COLOR_24_8
;
661 } else if (HAS_SIZE(24,8,0,0)) {
662 return V_028C70_COLOR_8_24
;
666 if (HAS_SIZE(5,6,5,0)) {
667 return V_028C70_COLOR_5_6_5
;
668 } else if (HAS_SIZE(32,8,24,0)) {
669 return V_028C70_COLOR_X24_8_32_FLOAT
;
673 if (desc
->channel
[0].size
== desc
->channel
[1].size
&&
674 desc
->channel
[0].size
== desc
->channel
[2].size
&&
675 desc
->channel
[0].size
== desc
->channel
[3].size
) {
676 switch (desc
->channel
[0].size
) {
678 return V_028C70_COLOR_4_4_4_4
;
680 return V_028C70_COLOR_8_8_8_8
;
682 return V_028C70_COLOR_16_16_16_16
;
684 return V_028C70_COLOR_32_32_32_32
;
686 } else if (HAS_SIZE(5,5,5,1)) {
687 return V_028C70_COLOR_1_5_5_5
;
688 } else if (HAS_SIZE(1,5,5,5)) {
689 return V_028C70_COLOR_5_5_5_1
;
690 } else if (HAS_SIZE(10,10,10,2)) {
691 return V_028C70_COLOR_2_10_10_10
;
695 return V_028C70_COLOR_INVALID
;
698 uint32_t radv_colorformat_endian_swap(uint32_t colorformat
)
700 if (0/*SI_BIG_ENDIAN*/) {
701 switch(colorformat
) {
703 case V_028C70_COLOR_8
:
704 return V_028C70_ENDIAN_NONE
;
706 /* 16-bit buffers. */
707 case V_028C70_COLOR_5_6_5
:
708 case V_028C70_COLOR_1_5_5_5
:
709 case V_028C70_COLOR_4_4_4_4
:
710 case V_028C70_COLOR_16
:
711 case V_028C70_COLOR_8_8
:
712 return V_028C70_ENDIAN_8IN16
;
714 /* 32-bit buffers. */
715 case V_028C70_COLOR_8_8_8_8
:
716 case V_028C70_COLOR_2_10_10_10
:
717 case V_028C70_COLOR_8_24
:
718 case V_028C70_COLOR_24_8
:
719 case V_028C70_COLOR_16_16
:
720 return V_028C70_ENDIAN_8IN32
;
722 /* 64-bit buffers. */
723 case V_028C70_COLOR_16_16_16_16
:
724 return V_028C70_ENDIAN_8IN16
;
726 case V_028C70_COLOR_32_32
:
727 return V_028C70_ENDIAN_8IN32
;
729 /* 128-bit buffers. */
730 case V_028C70_COLOR_32_32_32_32
:
731 return V_028C70_ENDIAN_8IN32
;
733 return V_028C70_ENDIAN_NONE
; /* Unsupported. */
736 return V_028C70_ENDIAN_NONE
;
740 uint32_t radv_translate_dbformat(VkFormat format
)
743 case VK_FORMAT_D16_UNORM
:
744 case VK_FORMAT_D16_UNORM_S8_UINT
:
745 return V_028040_Z_16
;
746 case VK_FORMAT_D32_SFLOAT
:
747 case VK_FORMAT_D32_SFLOAT_S8_UINT
:
748 return V_028040_Z_32_FLOAT
;
750 return V_028040_Z_INVALID
;
754 unsigned radv_translate_colorswap(VkFormat format
, bool do_endian_swap
)
756 const struct vk_format_description
*desc
= vk_format_description(format
);
758 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz)
760 if (format
== VK_FORMAT_B10G11R11_UFLOAT_PACK32
)
761 return V_0280A0_SWAP_STD
;
763 if (desc
->layout
!= VK_FORMAT_LAYOUT_PLAIN
)
766 switch (desc
->nr_channels
) {
768 if (HAS_SWIZZLE(0,X
))
769 return V_0280A0_SWAP_STD
; /* X___ */
770 else if (HAS_SWIZZLE(3,X
))
771 return V_0280A0_SWAP_ALT_REV
; /* ___X */
774 if ((HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,Y
)) ||
775 (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(1,NONE
)) ||
776 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,Y
)))
777 return V_0280A0_SWAP_STD
; /* XY__ */
778 else if ((HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,X
)) ||
779 (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(1,NONE
)) ||
780 (HAS_SWIZZLE(0,NONE
) && HAS_SWIZZLE(1,X
)))
782 return (do_endian_swap
? V_0280A0_SWAP_STD
: V_0280A0_SWAP_STD_REV
);
783 else if (HAS_SWIZZLE(0,X
) && HAS_SWIZZLE(3,Y
))
784 return V_0280A0_SWAP_ALT
; /* X__Y */
785 else if (HAS_SWIZZLE(0,Y
) && HAS_SWIZZLE(3,X
))
786 return V_0280A0_SWAP_ALT_REV
; /* Y__X */
789 if (HAS_SWIZZLE(0,X
))
790 return (do_endian_swap
? V_0280A0_SWAP_STD_REV
: V_0280A0_SWAP_STD
);
791 else if (HAS_SWIZZLE(0,Z
))
792 return V_0280A0_SWAP_STD_REV
; /* ZYX */
795 /* check the middle channels, the 1st and 4th channel can be NONE */
796 if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,Z
)) {
797 return V_0280A0_SWAP_STD
; /* XYZW */
798 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,Y
)) {
799 return V_0280A0_SWAP_STD_REV
; /* WZYX */
800 } else if (HAS_SWIZZLE(1,Y
) && HAS_SWIZZLE(2,X
)) {
801 return V_0280A0_SWAP_ALT
; /* ZYXW */
802 } else if (HAS_SWIZZLE(1,Z
) && HAS_SWIZZLE(2,W
)) {
805 return V_0280A0_SWAP_ALT_REV
;
807 return (do_endian_swap
? V_0280A0_SWAP_ALT
: V_0280A0_SWAP_ALT_REV
);
814 bool radv_format_pack_clear_color(VkFormat format
,
815 uint32_t clear_vals
[2],
816 VkClearColorValue
*value
)
818 uint8_t r
= 0, g
= 0, b
= 0, a
= 0;
819 const struct vk_format_description
*desc
= vk_format_description(format
);
821 if (vk_format_get_component_bits(format
, VK_FORMAT_COLORSPACE_RGB
, 0) <= 8) {
822 if (desc
->colorspace
== VK_FORMAT_COLORSPACE_RGB
) {
823 r
= float_to_ubyte(value
->float32
[0]);
824 g
= float_to_ubyte(value
->float32
[1]);
825 b
= float_to_ubyte(value
->float32
[2]);
826 a
= float_to_ubyte(value
->float32
[3]);
827 } else if (desc
->colorspace
== VK_FORMAT_COLORSPACE_SRGB
) {
828 r
= util_format_linear_float_to_srgb_8unorm(value
->float32
[0]);
829 g
= util_format_linear_float_to_srgb_8unorm(value
->float32
[1]);
830 b
= util_format_linear_float_to_srgb_8unorm(value
->float32
[2]);
831 a
= float_to_ubyte(value
->float32
[3]);
835 case VK_FORMAT_R8_UNORM
:
836 case VK_FORMAT_R8_SRGB
:
840 case VK_FORMAT_R8G8_UNORM
:
841 case VK_FORMAT_R8G8_SRGB
:
842 clear_vals
[0] = r
| g
<< 8;
845 case VK_FORMAT_R8G8B8A8_SRGB
:
846 case VK_FORMAT_R8G8B8A8_UNORM
:
847 clear_vals
[0] = r
| g
<< 8 | b
<< 16 | a
<< 24;
850 case VK_FORMAT_B8G8R8A8_SRGB
:
851 case VK_FORMAT_B8G8R8A8_UNORM
:
852 clear_vals
[0] = b
| g
<< 8 | r
<< 16 | a
<< 24;
855 case VK_FORMAT_A8B8G8R8_UNORM_PACK32
:
856 case VK_FORMAT_A8B8G8R8_SRGB_PACK32
:
857 clear_vals
[0] = r
| g
<< 8 | b
<< 16 | a
<< 24;
860 case VK_FORMAT_R8_UINT
:
861 clear_vals
[0] = value
->uint32
[0] & 0xff;
864 case VK_FORMAT_R8_SINT
:
865 clear_vals
[0] = value
->int32
[0] & 0xff;
868 case VK_FORMAT_R16_UINT
:
869 clear_vals
[0] = value
->uint32
[0] & 0xffff;
872 case VK_FORMAT_R8G8_UINT
:
873 clear_vals
[0] = value
->uint32
[0] & 0xff;
874 clear_vals
[0] |= (value
->uint32
[1] & 0xff) << 8;
877 case VK_FORMAT_R8G8_SINT
:
878 clear_vals
[0] = value
->int32
[0] & 0xff;
879 clear_vals
[0] |= (value
->int32
[1] & 0xff) << 8;
882 case VK_FORMAT_R8G8B8A8_UINT
:
883 clear_vals
[0] = value
->uint32
[0] & 0xff;
884 clear_vals
[0] |= (value
->uint32
[1] & 0xff) << 8;
885 clear_vals
[0] |= (value
->uint32
[2] & 0xff) << 16;
886 clear_vals
[0] |= (value
->uint32
[3] & 0xff) << 24;
889 case VK_FORMAT_R8G8B8A8_SINT
:
890 clear_vals
[0] = value
->int32
[0] & 0xff;
891 clear_vals
[0] |= (value
->int32
[1] & 0xff) << 8;
892 clear_vals
[0] |= (value
->int32
[2] & 0xff) << 16;
893 clear_vals
[0] |= (value
->int32
[3] & 0xff) << 24;
896 case VK_FORMAT_A8B8G8R8_UINT_PACK32
:
897 clear_vals
[0] = value
->uint32
[0] & 0xff;
898 clear_vals
[0] |= (value
->uint32
[1] & 0xff) << 8;
899 clear_vals
[0] |= (value
->uint32
[2] & 0xff) << 16;
900 clear_vals
[0] |= (value
->uint32
[3] & 0xff) << 24;
903 case VK_FORMAT_R16G16_UINT
:
904 clear_vals
[0] = value
->uint32
[0] & 0xffff;
905 clear_vals
[0] |= (value
->uint32
[1] & 0xffff) << 16;
908 case VK_FORMAT_R16G16B16A16_UINT
:
909 clear_vals
[0] = value
->uint32
[0] & 0xffff;
910 clear_vals
[0] |= (value
->uint32
[1] & 0xffff) << 16;
911 clear_vals
[1] = value
->uint32
[2] & 0xffff;
912 clear_vals
[1] |= (value
->uint32
[3] & 0xffff) << 16;
914 case VK_FORMAT_R32_UINT
:
915 clear_vals
[0] = value
->uint32
[0];
918 case VK_FORMAT_R32G32_UINT
:
919 clear_vals
[0] = value
->uint32
[0];
920 clear_vals
[1] = value
->uint32
[1];
922 case VK_FORMAT_R32_SINT
:
923 clear_vals
[0] = value
->int32
[0];
926 case VK_FORMAT_R16_SFLOAT
:
927 clear_vals
[0] = util_float_to_half(value
->float32
[0]);
930 case VK_FORMAT_R16G16_SFLOAT
:
931 clear_vals
[0] = util_float_to_half(value
->float32
[0]);
932 clear_vals
[0] |= (uint32_t)util_float_to_half(value
->float32
[1]) << 16;
935 case VK_FORMAT_R16G16B16A16_SFLOAT
:
936 clear_vals
[0] = util_float_to_half(value
->float32
[0]);
937 clear_vals
[0] |= (uint32_t)util_float_to_half(value
->float32
[1]) << 16;
938 clear_vals
[1] = util_float_to_half(value
->float32
[2]);
939 clear_vals
[1] |= (uint32_t)util_float_to_half(value
->float32
[3]) << 16;
941 case VK_FORMAT_R16_UNORM
:
942 clear_vals
[0] = ((uint16_t)util_iround(CLAMP(value
->float32
[0], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
945 case VK_FORMAT_R16G16_UNORM
:
946 clear_vals
[0] = ((uint16_t)util_iround(CLAMP(value
->float32
[0], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
947 clear_vals
[0] |= ((uint16_t)util_iround(CLAMP(value
->float32
[1], 0.0f
, 1.0f
) * 0xffff)) << 16;
950 case VK_FORMAT_R16G16B16A16_UNORM
:
951 clear_vals
[0] = ((uint16_t)util_iround(CLAMP(value
->float32
[0], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
952 clear_vals
[0] |= ((uint16_t)util_iround(CLAMP(value
->float32
[1], 0.0f
, 1.0f
) * 0xffff)) << 16;
953 clear_vals
[1] = ((uint16_t)util_iround(CLAMP(value
->float32
[2], 0.0f
, 1.0f
) * 0xffff)) & 0xffff;
954 clear_vals
[1] |= ((uint16_t)util_iround(CLAMP(value
->float32
[3], 0.0f
, 1.0f
) * 0xffff)) << 16;
956 case VK_FORMAT_A2B10G10R10_UNORM_PACK32
:
957 clear_vals
[0] = ((uint16_t)util_iround(CLAMP(value
->float32
[0], 0.0f
, 1.0f
) * 0x3ff)) & 0x3ff;
958 clear_vals
[0] |= (((uint16_t)util_iround(CLAMP(value
->float32
[1], 0.0f
, 1.0f
) * 0x3ff)) & 0x3ff) << 10;
959 clear_vals
[0] |= (((uint16_t)util_iround(CLAMP(value
->float32
[2], 0.0f
, 1.0f
) * 0x3ff)) & 0x3ff) << 20;
960 clear_vals
[0] |= (((uint16_t)util_iround(CLAMP(value
->float32
[3], 0.0f
, 1.0f
) * 0x3)) & 0x3) << 30;
963 case VK_FORMAT_R32G32_SFLOAT
:
964 clear_vals
[0] = fui(value
->float32
[0]);
965 clear_vals
[1] = fui(value
->float32
[1]);
967 case VK_FORMAT_R32_SFLOAT
:
969 clear_vals
[0] = fui(value
->float32
[0]);
971 case VK_FORMAT_B10G11R11_UFLOAT_PACK32
:
972 clear_vals
[0] = float3_to_r11g11b10f(value
->float32
);
976 fprintf(stderr
, "failed to fast clear %d\n", format
);
982 void radv_GetPhysicalDeviceFormatProperties(
983 VkPhysicalDevice physicalDevice
,
985 VkFormatProperties
* pFormatProperties
)
987 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
989 radv_physical_device_get_format_properties(physical_device
,
994 void radv_GetPhysicalDeviceFormatProperties2KHR(
995 VkPhysicalDevice physicalDevice
,
997 VkFormatProperties2KHR
* pFormatProperties
)
999 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
1001 radv_physical_device_get_format_properties(physical_device
,
1003 &pFormatProperties
->formatProperties
);
1006 VkResult
radv_GetPhysicalDeviceImageFormatProperties(
1007 VkPhysicalDevice physicalDevice
,
1010 VkImageTiling tiling
,
1011 VkImageUsageFlags usage
,
1012 VkImageCreateFlags createFlags
,
1013 VkImageFormatProperties
* pImageFormatProperties
)
1015 RADV_FROM_HANDLE(radv_physical_device
, physical_device
, physicalDevice
);
1016 VkFormatProperties format_props
;
1017 VkFormatFeatureFlags format_feature_flags
;
1018 VkExtent3D maxExtent
;
1019 uint32_t maxMipLevels
;
1020 uint32_t maxArraySize
;
1021 VkSampleCountFlags sampleCounts
= VK_SAMPLE_COUNT_1_BIT
;
1023 radv_physical_device_get_format_properties(physical_device
, format
,
1025 if (tiling
== VK_IMAGE_TILING_LINEAR
) {
1026 format_feature_flags
= format_props
.linearTilingFeatures
;
1027 } else if (tiling
== VK_IMAGE_TILING_OPTIMAL
) {
1028 format_feature_flags
= format_props
.optimalTilingFeatures
;
1030 unreachable("bad VkImageTiling");
1033 if (format_feature_flags
== 0)
1038 unreachable("bad vkimage type\n");
1039 case VK_IMAGE_TYPE_1D
:
1040 maxExtent
.width
= 16384;
1041 maxExtent
.height
= 1;
1042 maxExtent
.depth
= 1;
1043 maxMipLevels
= 15; /* log2(maxWidth) + 1 */
1044 maxArraySize
= 2048;
1046 case VK_IMAGE_TYPE_2D
:
1047 maxExtent
.width
= 16384;
1048 maxExtent
.height
= 16384;
1049 maxExtent
.depth
= 1;
1050 maxMipLevels
= 15; /* log2(maxWidth) + 1 */
1051 maxArraySize
= 2048;
1053 case VK_IMAGE_TYPE_3D
:
1054 maxExtent
.width
= 2048;
1055 maxExtent
.height
= 2048;
1056 maxExtent
.depth
= 2048;
1057 maxMipLevels
= 12; /* log2(maxWidth) + 1 */
1062 if (tiling
== VK_IMAGE_TILING_OPTIMAL
&&
1063 type
== VK_IMAGE_TYPE_2D
&&
1064 (format_feature_flags
& (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
|
1065 VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
)) &&
1066 !(createFlags
& VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT
) &&
1067 !(usage
& VK_IMAGE_USAGE_STORAGE_BIT
)) {
1068 sampleCounts
|= VK_SAMPLE_COUNT_2_BIT
| VK_SAMPLE_COUNT_4_BIT
| VK_SAMPLE_COUNT_8_BIT
;
1071 if (usage
& VK_IMAGE_USAGE_SAMPLED_BIT
) {
1072 if (!(format_feature_flags
& VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT
)) {
1077 if (usage
& VK_IMAGE_USAGE_STORAGE_BIT
) {
1078 if (!(format_feature_flags
& VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT
)) {
1083 if (usage
& VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
) {
1084 if (!(format_feature_flags
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
)) {
1089 if (usage
& VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
) {
1090 if (!(format_feature_flags
& VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
)) {
1095 *pImageFormatProperties
= (VkImageFormatProperties
) {
1096 .maxExtent
= maxExtent
,
1097 .maxMipLevels
= maxMipLevels
,
1098 .maxArrayLayers
= maxArraySize
,
1099 .sampleCounts
= sampleCounts
,
1101 /* FINISHME: Accurately calculate
1102 * VkImageFormatProperties::maxResourceSize.
1104 .maxResourceSize
= UINT32_MAX
,
1109 *pImageFormatProperties
= (VkImageFormatProperties
) {
1110 .maxExtent
= { 0, 0, 0 },
1112 .maxArrayLayers
= 0,
1114 .maxResourceSize
= 0,
1117 return VK_ERROR_FORMAT_NOT_SUPPORTED
;
1120 VkResult
radv_GetPhysicalDeviceImageFormatProperties2KHR(
1121 VkPhysicalDevice physicalDevice
,
1122 const VkPhysicalDeviceImageFormatInfo2KHR
* pImageFormatInfo
,
1123 VkImageFormatProperties2KHR
*pImageFormatProperties
)
1125 return radv_GetPhysicalDeviceImageFormatProperties(physicalDevice
,
1126 pImageFormatInfo
->format
,
1127 pImageFormatInfo
->type
,
1128 pImageFormatInfo
->tiling
,
1129 pImageFormatInfo
->usage
,
1130 pImageFormatInfo
->flags
,
1131 &pImageFormatProperties
->imageFormatProperties
);
1134 void radv_GetPhysicalDeviceSparseImageFormatProperties(
1135 VkPhysicalDevice physicalDevice
,
1139 VkImageUsageFlags usage
,
1140 VkImageTiling tiling
,
1141 uint32_t* pNumProperties
,
1142 VkSparseImageFormatProperties
* pProperties
)
1144 /* Sparse images are not yet supported. */
1145 *pNumProperties
= 0;
1148 void radv_GetPhysicalDeviceSparseImageFormatProperties2KHR(
1149 VkPhysicalDevice physicalDevice
,
1150 const VkPhysicalDeviceSparseImageFormatInfo2KHR
* pFormatInfo
,
1151 uint32_t *pPropertyCount
,
1152 VkSparseImageFormatProperties2KHR
* pProperties
)
1154 /* Sparse images are not yet supported. */
1155 *pPropertyCount
= 0;