1 /*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "radv_formats.h"
9 #include "radv_android.h"
10 #include "radv_debug.h"
11 #include "radv_entrypoints.h"
12 #include "radv_image.h"
13
14 #include "sid.h"
15
16 #include "vk_android.h"
17 #include "vk_enum_defines.h"
18 #include "vk_format.h"
19 #include "vk_log.h"
20 #include "vk_util.h"
21
22 #include "util/format_r11g11b10f.h"
23 #include "util/format_rgb9e5.h"
24 #include "util/format_srgb.h"
25 #include "util/half_float.h"
26 #include "ac_drm_fourcc.h"
27 #include "ac_formats.h"
28
29 uint32_t
radv_translate_buffer_numformat(const struct util_format_description * desc,int first_non_void)30 radv_translate_buffer_numformat(const struct util_format_description *desc, int first_non_void)
31 {
32 assert(util_format_get_num_planes(desc->format) == 1);
33
34 return ac_translate_buffer_numformat(desc, first_non_void);
35 }
36
37 static bool
radv_is_vertex_buffer_format_supported(VkFormat format)38 radv_is_vertex_buffer_format_supported(VkFormat format)
39 {
40 if (format == VK_FORMAT_UNDEFINED)
41 return false;
42
43 if (vk_format_is_srgb(format))
44 return false;
45
46 const int first_non_void = vk_format_get_first_non_void_channel(format);
47 if (first_non_void < 0)
48 return false;
49
50 const struct util_format_description *desc = vk_format_description(format);
51 return ac_translate_buffer_dataformat(desc, first_non_void) != V_008F0C_BUF_DATA_FORMAT_INVALID;
52 }
53
54 uint32_t
radv_translate_tex_dataformat(const struct radv_physical_device * pdev,const struct util_format_description * desc,int first_non_void)55 radv_translate_tex_dataformat(const struct radv_physical_device *pdev, const struct util_format_description *desc,
56 int first_non_void)
57 {
58 assert(util_format_get_num_planes(desc->format) == 1);
59
60 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED &&
61 (desc->format == PIPE_FORMAT_R8G8_B8G8_UNORM || desc->format == PIPE_FORMAT_G8R8_B8R8_UNORM ||
62 desc->format == PIPE_FORMAT_G8R8_G8B8_UNORM || desc->format == PIPE_FORMAT_R8G8_R8B8_UNORM))
63 return ~0U;
64
65 /* Closed VK driver does this also no 2/10/10/10 snorm */
66 if (desc->format == PIPE_FORMAT_R10G10B10A2_SNORM)
67 return ~0u;
68
69 return ac_translate_tex_dataformat(&pdev->info, desc, first_non_void);
70 }
71
72 uint32_t
radv_translate_tex_numformat(const struct util_format_description * desc,int first_non_void)73 radv_translate_tex_numformat(const struct util_format_description *desc, int first_non_void)
74 {
75 assert(util_format_get_num_planes(desc->format) == 1);
76
77 return ac_translate_tex_numformat(desc, first_non_void);
78 }
79
80 static bool
radv_is_sampler_format_supported(const struct radv_physical_device * pdev,VkFormat format,bool * linear_sampling)81 radv_is_sampler_format_supported(const struct radv_physical_device *pdev, VkFormat format, bool *linear_sampling)
82 {
83 const struct util_format_description *desc = vk_format_description(format);
84 uint32_t num_format;
85 if (format == VK_FORMAT_UNDEFINED || format == VK_FORMAT_R64_UINT || format == VK_FORMAT_R64_SINT)
86 return false;
87 num_format = radv_translate_tex_numformat(desc, vk_format_get_first_non_void_channel(format));
88
89 if (num_format == V_008F14_IMG_NUM_FORMAT_USCALED || num_format == V_008F14_IMG_NUM_FORMAT_SSCALED)
90 return false;
91
92 if (num_format == V_008F14_IMG_NUM_FORMAT_UNORM || num_format == V_008F14_IMG_NUM_FORMAT_SNORM ||
93 num_format == V_008F14_IMG_NUM_FORMAT_FLOAT || num_format == V_008F14_IMG_NUM_FORMAT_SRGB)
94 *linear_sampling = true;
95 else
96 *linear_sampling = false;
97 return radv_translate_tex_dataformat(pdev, vk_format_description(format),
98 vk_format_get_first_non_void_channel(format)) != ~0U;
99 }
100
101 bool
radv_is_atomic_format_supported(VkFormat format)102 radv_is_atomic_format_supported(VkFormat format)
103 {
104 return format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT || format == VK_FORMAT_R32_SFLOAT ||
105 format == VK_FORMAT_R64_UINT || format == VK_FORMAT_R64_SINT;
106 }
107
108 bool
radv_is_storage_image_format_supported(const struct radv_physical_device * pdev,VkFormat format)109 radv_is_storage_image_format_supported(const struct radv_physical_device *pdev, VkFormat format)
110 {
111 const struct radv_instance *instance = radv_physical_device_instance(pdev);
112 const struct util_format_description *desc = vk_format_description(format);
113 unsigned data_format, num_format;
114 if (format == VK_FORMAT_UNDEFINED)
115 return false;
116
117 if (vk_format_has_stencil(format))
118 return false;
119
120 if (instance->drirc.disable_depth_storage && vk_format_has_depth(format))
121 return false;
122
123 data_format = radv_translate_tex_dataformat(pdev, desc, vk_format_get_first_non_void_channel(format));
124 num_format = radv_translate_tex_numformat(desc, vk_format_get_first_non_void_channel(format));
125
126 if (data_format == ~0)
127 return false;
128
129 /* Extracted from the GCN3 ISA document. */
130 switch (num_format) {
131 case V_008F14_IMG_NUM_FORMAT_UNORM:
132 case V_008F14_IMG_NUM_FORMAT_SNORM:
133 case V_008F14_IMG_NUM_FORMAT_UINT:
134 case V_008F14_IMG_NUM_FORMAT_SINT:
135 case V_008F14_IMG_NUM_FORMAT_FLOAT:
136 break;
137 default:
138 return false;
139 }
140
141 switch (data_format) {
142 case V_008F14_IMG_DATA_FORMAT_8:
143 case V_008F14_IMG_DATA_FORMAT_16:
144 case V_008F14_IMG_DATA_FORMAT_8_8:
145 case V_008F14_IMG_DATA_FORMAT_32:
146 case V_008F14_IMG_DATA_FORMAT_16_16:
147 case V_008F14_IMG_DATA_FORMAT_10_11_11:
148 case V_008F14_IMG_DATA_FORMAT_11_11_10:
149 case V_008F14_IMG_DATA_FORMAT_10_10_10_2:
150 case V_008F14_IMG_DATA_FORMAT_2_10_10_10:
151 case V_008F14_IMG_DATA_FORMAT_8_8_8_8:
152 case V_008F14_IMG_DATA_FORMAT_32_32:
153 case V_008F14_IMG_DATA_FORMAT_16_16_16_16:
154 case V_008F14_IMG_DATA_FORMAT_32_32_32_32:
155 case V_008F14_IMG_DATA_FORMAT_5_6_5:
156 case V_008F14_IMG_DATA_FORMAT_1_5_5_5:
157 case V_008F14_IMG_DATA_FORMAT_5_5_5_1:
158 case V_008F14_IMG_DATA_FORMAT_4_4_4_4:
159 /* TODO: FMASK formats. */
160 return true;
161 case V_008F14_IMG_DATA_FORMAT_5_9_9_9:
162 return pdev->info.gfx_level >= GFX10_3;
163 default:
164 return false;
165 }
166 }
167
168 static bool
radv_is_buffer_dataformat_supported(const struct util_format_description * desc,int first_non_void)169 radv_is_buffer_dataformat_supported(const struct util_format_description *desc, int first_non_void)
170 {
171 uint32_t data_format, type;
172
173 data_format = ac_translate_buffer_dataformat(desc, first_non_void);
174 if (data_format == V_008F0C_BUF_DATA_FORMAT_INVALID)
175 return false;
176
177 assert(first_non_void >= 0);
178
179 type = desc->channel[first_non_void].type;
180
181 if (desc->channel[first_non_void].size <= 16 && desc->nr_channels == 3 &&
182 desc->format != PIPE_FORMAT_R11G11B10_FLOAT)
183 return false;
184
185 /* From the Southern Islands ISA documentation about MTBUF:
186 * 'Memory reads of data in memory that is 32 or 64 bits do not
187 * undergo any format conversion.'
188 */
189 if (desc->channel[first_non_void].size == 32 && type != UTIL_FORMAT_TYPE_FLOAT &&
190 !desc->channel[first_non_void].pure_integer)
191 return false;
192
193 if (desc->channel[first_non_void].size == 64 && (type == UTIL_FORMAT_TYPE_FLOAT || desc->nr_channels != 1))
194 return false;
195
196 return true;
197 }
198
199 bool
radv_is_buffer_format_supported(VkFormat format,bool * scaled)200 radv_is_buffer_format_supported(VkFormat format, bool *scaled)
201 {
202 const struct util_format_description *desc = vk_format_description(format);
203 unsigned num_format;
204
205 if (format == VK_FORMAT_UNDEFINED)
206 return false;
207
208 const int first_non_void = vk_format_get_first_non_void_channel(format);
209 if (first_non_void < 0)
210 return false;
211
212 if (!radv_is_buffer_dataformat_supported(desc, first_non_void))
213 return false;
214
215 num_format = radv_translate_buffer_numformat(desc, first_non_void);
216 if (scaled)
217 *scaled = (num_format == V_008F0C_BUF_NUM_FORMAT_SSCALED) || (num_format == V_008F0C_BUF_NUM_FORMAT_USCALED);
218
219 return true;
220 }
221
222 static bool
radv_is_colorbuffer_format_blendable(const struct radv_physical_device * pdev,VkFormat format)223 radv_is_colorbuffer_format_blendable(const struct radv_physical_device *pdev, VkFormat format)
224 {
225 const struct util_format_description *desc = vk_format_description(format);
226 const uint32_t color_format = ac_get_cb_format(pdev->info.gfx_level, desc->format);
227 const uint32_t color_num_format = ac_get_cb_number_type(desc->format);
228
229 assert(color_format != V_028C70_COLOR_INVALID);
230 if (color_num_format == V_028C70_NUMBER_UINT || color_num_format == V_028C70_NUMBER_SINT ||
231 color_format == V_028C70_COLOR_8_24 || color_format == V_028C70_COLOR_24_8 ||
232 color_format == V_028C70_COLOR_X24_8_32_FLOAT)
233 return false;
234
235 return true;
236 }
237
238 bool
radv_is_colorbuffer_format_supported(const struct radv_physical_device * pdev,VkFormat format)239 radv_is_colorbuffer_format_supported(const struct radv_physical_device *pdev, VkFormat format)
240 {
241 const struct util_format_description *desc = vk_format_description(format);
242 return ac_is_colorbuffer_format_supported(pdev->info.gfx_level, desc->format);
243 }
244
245 static bool
radv_is_zs_format_supported(VkFormat format)246 radv_is_zs_format_supported(VkFormat format)
247 {
248 if (format == VK_FORMAT_D24_UNORM_S8_UINT || format == VK_FORMAT_X8_D24_UNORM_PACK32)
249 return false;
250
251 return ac_is_zs_format_supported(radv_format_to_pipe_format(format)) || format == VK_FORMAT_S8_UINT;
252 }
253
254 static bool
radv_is_filter_minmax_format_supported(const struct radv_physical_device * pdev,VkFormat format)255 radv_is_filter_minmax_format_supported(const struct radv_physical_device *pdev, VkFormat format)
256 {
257 return ac_is_reduction_mode_supported(&pdev->info, radv_format_to_pipe_format(format), false);
258 }
259
260 bool
radv_is_format_emulated(const struct radv_physical_device * pdev,VkFormat format)261 radv_is_format_emulated(const struct radv_physical_device *pdev, VkFormat format)
262 {
263 if (pdev->emulate_etc2 && vk_texcompress_etc2_emulation_format(format) != VK_FORMAT_UNDEFINED)
264 return true;
265
266 if (pdev->emulate_astc && vk_texcompress_astc_emulation_format(format) != VK_FORMAT_UNDEFINED)
267 return true;
268
269 return false;
270 }
271
272 static void
radv_physical_device_get_format_properties(struct radv_physical_device * pdev,VkFormat format,VkFormatProperties3 * out_properties)273 radv_physical_device_get_format_properties(struct radv_physical_device *pdev, VkFormat format,
274 VkFormatProperties3 *out_properties)
275 {
276 VkFormatFeatureFlags2 linear = 0, tiled = 0, buffer = 0;
277 const struct util_format_description *desc = vk_format_description(format);
278 bool scaled = false;
279 /* TODO: implement some software emulation of SUBSAMPLED formats. */
280 if (desc->format == PIPE_FORMAT_NONE || desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
281 out_properties->linearTilingFeatures = linear;
282 out_properties->optimalTilingFeatures = tiled;
283 out_properties->bufferFeatures = buffer;
284 return;
285 }
286
287 if ((desc->layout == UTIL_FORMAT_LAYOUT_ETC && !pdev->info.has_etc_support) ||
288 desc->layout == UTIL_FORMAT_LAYOUT_ASTC) {
289 if (radv_is_format_emulated(pdev, format)) {
290 /* required features for compressed formats */
291 tiled = VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
292 VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
293 VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
294
295 VkFormat emulation_format = vk_texcompress_etc2_emulation_format(format);
296 if (emulation_format == VK_FORMAT_UNDEFINED)
297 emulation_format = vk_texcompress_astc_emulation_format(format);
298
299 if (radv_is_filter_minmax_format_supported(pdev, emulation_format))
300 tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
301 }
302 out_properties->linearTilingFeatures = linear;
303 out_properties->optimalTilingFeatures = tiled;
304 out_properties->bufferFeatures = buffer;
305 return;
306 }
307
308 const bool multiplanar = vk_format_get_plane_count(format) > 1;
309 if (multiplanar || desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
310 uint64_t tiling = VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
311 VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
312
313 if (vk_format_get_ycbcr_info(format)) {
314 tiling |= VK_FORMAT_FEATURE_2_COSITED_CHROMA_SAMPLES_BIT | VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
315
316 /* The subsampled formats have no support for linear filters. */
317 if (desc->layout != UTIL_FORMAT_LAYOUT_SUBSAMPLED)
318 tiling |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT;
319 }
320
321 if (pdev->video_decode_enabled) {
322 if (format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM ||
323 format == VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16 ||
324 format == VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16)
325 tiling |= VK_FORMAT_FEATURE_2_VIDEO_DECODE_OUTPUT_BIT_KHR | VK_FORMAT_FEATURE_2_VIDEO_DECODE_DPB_BIT_KHR;
326 }
327
328 if (pdev->video_encode_enabled) {
329 if (format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM ||
330 format == VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16 ||
331 format == VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16)
332 tiling |= VK_FORMAT_FEATURE_2_VIDEO_ENCODE_INPUT_BIT_KHR | VK_FORMAT_FEATURE_2_VIDEO_ENCODE_DPB_BIT_KHR;
333 }
334
335 if (multiplanar)
336 tiling |= VK_FORMAT_FEATURE_2_DISJOINT_BIT;
337
338 /* Fails for unknown reasons with linear tiling & subsampled formats. */
339 out_properties->linearTilingFeatures = desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED ? 0 : tiling;
340 out_properties->optimalTilingFeatures = tiling;
341 out_properties->bufferFeatures = 0;
342 return;
343 }
344
345 if (radv_is_storage_image_format_supported(pdev, format)) {
346 tiled |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT | VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
347 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
348 linear |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT | VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
349 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
350 }
351
352 if (radv_is_vertex_buffer_format_supported(format))
353 buffer |= VK_FORMAT_FEATURE_2_VERTEX_BUFFER_BIT;
354
355 if (radv_is_buffer_format_supported(format, &scaled)) {
356 if (format != VK_FORMAT_R64_UINT && format != VK_FORMAT_R64_SINT && !scaled && !vk_format_is_srgb(format))
357 buffer |= VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT;
358 buffer |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT | VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
359 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
360 }
361
362 if (vk_format_is_depth_or_stencil(format)) {
363 if (radv_is_zs_format_supported(format)) {
364 tiled |= VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT;
365 tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT;
366 tiled |= VK_FORMAT_FEATURE_2_BLIT_SRC_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
367 tiled |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
368
369 if (radv_is_filter_minmax_format_supported(pdev, format))
370 tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
371
372 if (vk_format_has_depth(format)) {
373 tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
374 VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_DEPTH_COMPARISON_BIT;
375 }
376
377 /* Don't support blitting surfaces with depth/stencil. */
378 if (vk_format_has_depth(format) && vk_format_has_stencil(format))
379 tiled &= ~VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
380
381 /* Don't support linear depth surfaces */
382 linear = 0;
383 }
384 } else {
385 bool linear_sampling;
386 if (radv_is_sampler_format_supported(pdev, format, &linear_sampling)) {
387 linear |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
388 tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
389
390 if (radv_is_filter_minmax_format_supported(pdev, format)) {
391 tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
392 linear |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
393 }
394
395 if (linear_sampling) {
396 linear |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
397 tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
398 }
399
400 /* Don't support blitting/minmax for R32G32B32 formats. */
401 if (format == VK_FORMAT_R32G32B32_SFLOAT || format == VK_FORMAT_R32G32B32_UINT ||
402 format == VK_FORMAT_R32G32B32_SINT) {
403 linear &= ~VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
404 linear &= ~VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
405 }
406 }
407 if (radv_is_colorbuffer_format_supported(pdev, format) && desc->channel[0].size != 64) {
408 linear |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
409 tiled |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
410 if (radv_is_colorbuffer_format_blendable(pdev, format)) {
411 linear |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT;
412 tiled |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT;
413 }
414 }
415 if (tiled && !scaled) {
416 tiled |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
417 }
418
419 /* Tiled formatting does not support NPOT pixel sizes */
420 if (!util_is_power_of_two_or_zero(vk_format_get_blocksize(format)))
421 tiled = 0;
422 }
423
424 if (linear && !scaled) {
425 linear |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
426 }
427
428 if (radv_is_atomic_format_supported(format)) {
429 buffer |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
430 linear |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
431 tiled |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
432 }
433
434 switch (format) {
435 case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
436 case VK_FORMAT_A2B10G10R10_SNORM_PACK32:
437 case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
438 case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:
439 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
440 case VK_FORMAT_A2B10G10R10_SINT_PACK32:
441 buffer &= ~(VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT | VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT);
442 linear = 0;
443 tiled = 0;
444 break;
445 case VK_FORMAT_R64_UINT:
446 case VK_FORMAT_R64_SINT:
447 case VK_FORMAT_R64_SFLOAT:
448 tiled |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
449 linear |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
450 break;
451 default:
452 break;
453 }
454
455 switch (format) {
456 case VK_FORMAT_R32G32_SFLOAT:
457 case VK_FORMAT_R32G32B32_SFLOAT:
458 case VK_FORMAT_R32G32B32A32_SFLOAT:
459 case VK_FORMAT_R16G16_SFLOAT:
460 case VK_FORMAT_R16G16B16_SFLOAT:
461 case VK_FORMAT_R16G16B16A16_SFLOAT:
462 case VK_FORMAT_R16G16_SNORM:
463 case VK_FORMAT_R16G16_UNORM:
464 case VK_FORMAT_R16G16B16A16_SNORM:
465 case VK_FORMAT_R16G16B16A16_UNORM:
466 case VK_FORMAT_R8G8_SNORM:
467 case VK_FORMAT_R8G8_UNORM:
468 case VK_FORMAT_R8G8B8A8_SNORM:
469 case VK_FORMAT_R8G8B8A8_UNORM:
470 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
471 buffer |= VK_FORMAT_FEATURE_2_ACCELERATION_STRUCTURE_VERTEX_BUFFER_BIT_KHR;
472 break;
473 default:
474 break;
475 }
476 /* addrlib does not support linear compressed textures. */
477 if (vk_format_is_compressed(format))
478 linear = 0;
479
480 /* From the Vulkan spec 1.2.163:
481 *
482 * "VK_FORMAT_FEATURE_2_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT must be supported for the
483 * following formats if the attachmentFragmentShadingRate feature is supported:"
484 *
485 * - VK_FORMAT_R8_UINT
486 */
487 if (format == VK_FORMAT_R8_UINT) {
488 tiled |= VK_FORMAT_FEATURE_2_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR;
489 }
490
491 /* It's invalid to expose buffer features with depth/stencil formats. */
492 if (vk_format_is_depth_or_stencil(format)) {
493 buffer = 0;
494 }
495
496 out_properties->linearTilingFeatures = linear;
497 out_properties->optimalTilingFeatures = tiled;
498 out_properties->bufferFeatures = buffer;
499 }
500
501 bool
radv_format_pack_clear_color(VkFormat format,uint32_t clear_vals[2],VkClearColorValue * value)502 radv_format_pack_clear_color(VkFormat format, uint32_t clear_vals[2], VkClearColorValue *value)
503 {
504 const struct util_format_description *desc = vk_format_description(format);
505
506 if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
507 clear_vals[0] = float3_to_r11g11b10f(value->float32);
508 clear_vals[1] = 0;
509 return true;
510 } else if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
511 clear_vals[0] = float3_to_rgb9e5(value->float32);
512 clear_vals[1] = 0;
513 return true;
514 }
515
516 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
517 fprintf(stderr, "failed to fast clear for non-plain format %d\n", format);
518 return false;
519 }
520
521 if (!util_is_power_of_two_or_zero(desc->block.bits)) {
522 fprintf(stderr, "failed to fast clear for NPOT format %d\n", format);
523 return false;
524 }
525
526 if (desc->block.bits > 64) {
527 /*
528 * We have a 128 bits format, check if the first 3 components are the same.
529 * Every elements has to be 32 bits since we don't support 64-bit formats,
530 * and we can skip swizzling checks as alpha always comes last for these and
531 * we do not care about the rest as they have to be the same.
532 */
533 if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
534 if (value->float32[0] != value->float32[1] || value->float32[0] != value->float32[2])
535 return false;
536 } else {
537 if (value->uint32[0] != value->uint32[1] || value->uint32[0] != value->uint32[2])
538 return false;
539 }
540 clear_vals[0] = value->uint32[0];
541 clear_vals[1] = value->uint32[3];
542 return true;
543 }
544 uint64_t clear_val = 0;
545
546 for (unsigned c = 0; c < 4; ++c) {
547 if (desc->swizzle[c] >= 4)
548 continue;
549
550 const struct util_format_channel_description *channel = &desc->channel[desc->swizzle[c]];
551 assert(channel->size);
552
553 uint64_t v = 0;
554 if (channel->pure_integer) {
555 v = value->uint32[c] & ((1ULL << channel->size) - 1);
556 } else if (channel->normalized) {
557 if (channel->type == UTIL_FORMAT_TYPE_UNSIGNED && desc->swizzle[c] < 3 &&
558 desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
559 assert(channel->size == 8);
560
561 v = util_format_linear_float_to_srgb_8unorm(value->float32[c]);
562 } else {
563 float f = MIN2(value->float32[c], 1.0f);
564
565 if (channel->type == UTIL_FORMAT_TYPE_UNSIGNED) {
566 f = MAX2(f, 0.0f) * ((1ULL << channel->size) - 1);
567 } else {
568 f = MAX2(f, -1.0f) * ((1ULL << (channel->size - 1)) - 1);
569 }
570
571 /* The hardware rounds before conversion. */
572 if (f > 0)
573 f += 0.5f;
574 else
575 f -= 0.5f;
576
577 v = (uint64_t)f;
578 }
579 } else if (channel->type == UTIL_FORMAT_TYPE_FLOAT) {
580 if (channel->size == 32) {
581 memcpy(&v, &value->float32[c], 4);
582 } else if (channel->size == 16) {
583 v = _mesa_float_to_float16_rtz(value->float32[c]);
584 } else {
585 fprintf(stderr, "failed to fast clear for unhandled float size in format %d\n", format);
586 return false;
587 }
588 } else {
589 fprintf(stderr, "failed to fast clear for unhandled component type in format %d\n", format);
590 return false;
591 }
592 clear_val |= (v & ((1ULL << channel->size) - 1)) << channel->shift;
593 }
594
595 clear_vals[0] = clear_val;
596 clear_vals[1] = clear_val >> 32;
597
598 return true;
599 }
600
601 static const struct ac_modifier_options radv_modifier_options = {
602 .dcc = true,
603 .dcc_retile = true,
604 };
605
606 static VkFormatFeatureFlags2
radv_get_modifier_flags(struct radv_physical_device * pdev,VkFormat format,uint64_t modifier,const VkFormatProperties3 * props)607 radv_get_modifier_flags(struct radv_physical_device *pdev, VkFormat format, uint64_t modifier,
608 const VkFormatProperties3 *props)
609 {
610 const struct radv_instance *instance = radv_physical_device_instance(pdev);
611 VkFormatFeatureFlags2 features;
612
613 if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format))
614 return 0;
615
616 if (modifier == DRM_FORMAT_MOD_LINEAR)
617 features = props->linearTilingFeatures;
618 else
619 features = props->optimalTilingFeatures;
620
621 /* Unconditionally disable DISJOINT support for modifiers for now */
622 features &= ~VK_FORMAT_FEATURE_2_DISJOINT_BIT;
623
624 if (ac_modifier_has_dcc(modifier)) {
625 /* We don't enable DCC for multi-planar formats */
626 if (vk_format_get_plane_count(format) > 1)
627 return 0;
628
629 /* Only disable support for STORAGE_IMAGE on modifiers that
630 * do not support DCC image stores or when explicitly disabled.
631 */
632 if (!ac_modifier_supports_dcc_image_stores(pdev->info.gfx_level, modifier) ||
633 radv_is_atomic_format_supported(format) || instance->drirc.disable_dcc_stores)
634 features &= ~VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
635
636 if (instance->debug_flags & (RADV_DEBUG_NO_DCC | RADV_DEBUG_NO_DISPLAY_DCC))
637 return 0;
638 }
639
640 return features;
641 }
642
643 static void
radv_list_drm_format_modifiers(struct radv_physical_device * pdev,VkFormat format,const VkFormatProperties3 * format_props,VkDrmFormatModifierPropertiesListEXT * mod_list)644 radv_list_drm_format_modifiers(struct radv_physical_device *pdev, VkFormat format,
645 const VkFormatProperties3 *format_props, VkDrmFormatModifierPropertiesListEXT *mod_list)
646 {
647 unsigned mod_count;
648
649 if (!mod_list)
650 return;
651
652 if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format)) {
653 mod_list->drmFormatModifierCount = 0;
654 return;
655 }
656
657 VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierPropertiesEXT, out, mod_list->pDrmFormatModifierProperties,
658 &mod_list->drmFormatModifierCount);
659
660 ac_get_supported_modifiers(&pdev->info, &radv_modifier_options, radv_format_to_pipe_format(format), &mod_count,
661 NULL);
662
663 uint64_t *mods = malloc(mod_count * sizeof(uint64_t));
664 if (!mods) {
665 /* We can't return an error here ... */
666 mod_list->drmFormatModifierCount = 0;
667 return;
668 }
669 ac_get_supported_modifiers(&pdev->info, &radv_modifier_options, radv_format_to_pipe_format(format), &mod_count,
670 mods);
671
672 for (unsigned i = 0; i < mod_count; ++i) {
673 VkFormatFeatureFlags2 features = radv_get_modifier_flags(pdev, format, mods[i], format_props);
674 if (!features)
675 continue;
676
677 unsigned planes = vk_format_get_plane_count(format);
678
679 if (pdev->info.gfx_level < GFX12) {
680 /* DCC is transparent to the userspace driver on GFX12 so it doesn't
681 * need additional planes.
682 */
683 planes += ac_modifier_has_dcc(mods[i]) + ac_modifier_has_dcc_retile(mods[i]);
684 }
685
686 vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out, out_props)
687 {
688 *out_props = (VkDrmFormatModifierPropertiesEXT){
689 .drmFormatModifier = mods[i],
690 .drmFormatModifierPlaneCount = planes,
691 .drmFormatModifierTilingFeatures = vk_format_features2_to_features(features),
692 };
693 };
694 }
695
696 free(mods);
697 }
698
699 static void
radv_list_drm_format_modifiers_2(struct radv_physical_device * pdev,VkFormat format,const VkFormatProperties3 * format_props,VkDrmFormatModifierPropertiesList2EXT * mod_list)700 radv_list_drm_format_modifiers_2(struct radv_physical_device *pdev, VkFormat format,
701 const VkFormatProperties3 *format_props,
702 VkDrmFormatModifierPropertiesList2EXT *mod_list)
703 {
704 unsigned mod_count;
705
706 if (!mod_list)
707 return;
708
709 if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format)) {
710 mod_list->drmFormatModifierCount = 0;
711 return;
712 }
713
714 VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierProperties2EXT, out, mod_list->pDrmFormatModifierProperties,
715 &mod_list->drmFormatModifierCount);
716
717 ac_get_supported_modifiers(&pdev->info, &radv_modifier_options, radv_format_to_pipe_format(format), &mod_count,
718 NULL);
719
720 uint64_t *mods = malloc(mod_count * sizeof(uint64_t));
721 if (!mods) {
722 /* We can't return an error here ... */
723 mod_list->drmFormatModifierCount = 0;
724 return;
725 }
726 ac_get_supported_modifiers(&pdev->info, &radv_modifier_options, radv_format_to_pipe_format(format), &mod_count,
727 mods);
728
729 for (unsigned i = 0; i < mod_count; ++i) {
730 VkFormatFeatureFlags2 features = radv_get_modifier_flags(pdev, format, mods[i], format_props);
731 if (!features)
732 continue;
733
734 unsigned planes = vk_format_get_plane_count(format);
735
736 if (pdev->info.gfx_level < GFX12) {
737 /* DCC is transparent to the userspace driver on GFX12 so it doesn't
738 * need additional planes.
739 */
740 planes += ac_modifier_has_dcc(mods[i]) + ac_modifier_has_dcc_retile(mods[i]);
741 }
742
743 vk_outarray_append_typed(VkDrmFormatModifierProperties2EXT, &out, out_props)
744 {
745 *out_props = (VkDrmFormatModifierProperties2EXT){
746 .drmFormatModifier = mods[i],
747 .drmFormatModifierPlaneCount = planes,
748 .drmFormatModifierTilingFeatures = features,
749 };
750 };
751 }
752
753 free(mods);
754 }
755
756 static VkResult
radv_check_modifier_support(struct radv_physical_device * pdev,const VkPhysicalDeviceImageFormatInfo2 * info,VkImageFormatProperties * props,VkFormat format,uint64_t modifier)757 radv_check_modifier_support(struct radv_physical_device *pdev, const VkPhysicalDeviceImageFormatInfo2 *info,
758 VkImageFormatProperties *props, VkFormat format, uint64_t modifier)
759 {
760 uint32_t max_width, max_height;
761
762 if (info->type != VK_IMAGE_TYPE_2D)
763 return VK_ERROR_FORMAT_NOT_SUPPORTED;
764
765 if (radv_is_format_emulated(pdev, format))
766 return VK_ERROR_FORMAT_NOT_SUPPORTED;
767
768 /* We did not add modifiers for sparse textures. */
769 if (info->flags &
770 (VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_SPARSE_ALIASED_BIT))
771 return VK_ERROR_FORMAT_NOT_SUPPORTED;
772
773 /*
774 * Need to check the modifier is supported in general:
775 * "If the drmFormatModifier is incompatible with the parameters specified
776 * in VkPhysicalDeviceImageFormatInfo2 and its pNext chain, then
777 * vkGetPhysicalDeviceImageFormatProperties2 returns VK_ERROR_FORMAT_NOT_SUPPORTED.
778 * The implementation must support the query of any drmFormatModifier,
779 * including unknown and invalid modifier values."
780 */
781 VkDrmFormatModifierPropertiesListEXT mod_list = {
782 .sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
783 };
784
785 VkFormatProperties2 format_props2 = {.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, .pNext = &mod_list};
786
787 radv_GetPhysicalDeviceFormatProperties2(radv_physical_device_to_handle(pdev), format, &format_props2);
788
789 if (!mod_list.drmFormatModifierCount)
790 return VK_ERROR_FORMAT_NOT_SUPPORTED;
791
792 mod_list.pDrmFormatModifierProperties =
793 calloc(mod_list.drmFormatModifierCount, sizeof(*mod_list.pDrmFormatModifierProperties));
794 if (!mod_list.pDrmFormatModifierProperties)
795 return VK_ERROR_OUT_OF_HOST_MEMORY;
796
797 radv_GetPhysicalDeviceFormatProperties2(radv_physical_device_to_handle(pdev), format, &format_props2);
798
799 bool found = false;
800 for (uint32_t i = 0; i < mod_list.drmFormatModifierCount && !found; ++i)
801 if (mod_list.pDrmFormatModifierProperties[i].drmFormatModifier == modifier)
802 found = true;
803
804 free(mod_list.pDrmFormatModifierProperties);
805
806 if (!found)
807 return VK_ERROR_FORMAT_NOT_SUPPORTED;
808
809 bool need_dcc_sign_reinterpret = false;
810 if (ac_modifier_has_dcc(modifier) &&
811 !radv_are_formats_dcc_compatible(pdev, info->pNext, format, info->flags, &need_dcc_sign_reinterpret) &&
812 !need_dcc_sign_reinterpret)
813 return VK_ERROR_FORMAT_NOT_SUPPORTED;
814
815 /* We can expand this as needed and implemented but there is not much demand
816 * for more. */
817 if (ac_modifier_has_dcc(modifier)) {
818 props->maxMipLevels = 1;
819 props->maxArrayLayers = 1;
820 }
821
822 ac_modifier_max_extent(&pdev->info, modifier, &max_width, &max_height);
823 props->maxExtent.width = MIN2(props->maxExtent.width, max_width);
824 props->maxExtent.height = MIN2(props->maxExtent.width, max_height);
825
826 /* We don't support MSAA for modifiers */
827 props->sampleCounts &= VK_SAMPLE_COUNT_1_BIT;
828 return VK_SUCCESS;
829 }
830
831 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties2 * pFormatProperties)832 radv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice, VkFormat format,
833 VkFormatProperties2 *pFormatProperties)
834 {
835 VK_FROM_HANDLE(radv_physical_device, pdev, physicalDevice);
836 VkFormatProperties3 format_props;
837
838 radv_physical_device_get_format_properties(pdev, format, &format_props);
839
840 pFormatProperties->formatProperties.linearTilingFeatures =
841 vk_format_features2_to_features(format_props.linearTilingFeatures);
842 pFormatProperties->formatProperties.optimalTilingFeatures =
843 vk_format_features2_to_features(format_props.optimalTilingFeatures);
844 pFormatProperties->formatProperties.bufferFeatures = vk_format_features2_to_features(format_props.bufferFeatures);
845
846 VkFormatProperties3 *format_props_extended = vk_find_struct(pFormatProperties, FORMAT_PROPERTIES_3);
847 if (format_props_extended) {
848 format_props_extended->linearTilingFeatures = format_props.linearTilingFeatures;
849 format_props_extended->optimalTilingFeatures = format_props.optimalTilingFeatures;
850 format_props_extended->bufferFeatures = format_props.bufferFeatures;
851 }
852
853 radv_list_drm_format_modifiers(pdev, format, &format_props,
854 vk_find_struct(pFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT));
855 radv_list_drm_format_modifiers_2(pdev, format, &format_props,
856 vk_find_struct(pFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_2_EXT));
857 }
858
859 static VkResult
radv_get_image_format_properties(struct radv_physical_device * pdev,const VkPhysicalDeviceImageFormatInfo2 * info,VkFormat format,VkImageFormatProperties * pImageFormatProperties)860 radv_get_image_format_properties(struct radv_physical_device *pdev, const VkPhysicalDeviceImageFormatInfo2 *info,
861 VkFormat format, VkImageFormatProperties *pImageFormatProperties)
862
863 {
864 VkFormatProperties3 format_props;
865 VkFormatFeatureFlags2 format_feature_flags;
866 VkExtent3D maxExtent;
867 uint32_t maxMipLevels;
868 uint32_t maxArraySize;
869 VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
870 const struct util_format_description *desc = vk_format_description(format);
871 enum amd_gfx_level gfx_level = pdev->info.gfx_level;
872 VkImageTiling tiling = info->tiling;
873 const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *mod_info =
874 vk_find_struct_const(info->pNext, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
875 VkResult result = VK_ERROR_FORMAT_NOT_SUPPORTED;
876
877 radv_physical_device_get_format_properties(pdev, format, &format_props);
878 if (tiling == VK_IMAGE_TILING_LINEAR) {
879 format_feature_flags = format_props.linearTilingFeatures;
880 } else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
881 format_feature_flags = format_props.optimalTilingFeatures;
882 } else if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
883 format_feature_flags = radv_get_modifier_flags(pdev, format, mod_info->drmFormatModifier, &format_props);
884 } else {
885 unreachable("bad VkImageTiling");
886 }
887
888 if (format_feature_flags == 0)
889 goto unsupported;
890
891 if (info->type == VK_IMAGE_TYPE_1D && radv_is_format_emulated(pdev, format))
892 goto unsupported;
893 if (info->type != VK_IMAGE_TYPE_2D && vk_format_is_depth_or_stencil(format))
894 goto unsupported;
895
896 switch (info->type) {
897 default:
898 unreachable("bad vkimage type\n");
899 case VK_IMAGE_TYPE_1D:
900 maxExtent.width = 16384;
901 maxExtent.height = 1;
902 maxExtent.depth = 1;
903 maxMipLevels = 15; /* log2(maxWidth) + 1 */
904 maxArraySize = gfx_level >= GFX10 ? 8192 : 2048;
905 break;
906 case VK_IMAGE_TYPE_2D:
907 maxExtent.width = 16384;
908 maxExtent.height = 16384;
909 maxExtent.depth = 1;
910 maxMipLevels = 15; /* log2(maxWidth) + 1 */
911 maxArraySize = gfx_level >= GFX10 ? 8192 : 2048;
912 break;
913 case VK_IMAGE_TYPE_3D:
914 if (gfx_level >= GFX10) {
915 maxExtent.width = 8192;
916 maxExtent.height = 8192;
917 maxExtent.depth = 8192;
918 } else {
919 maxExtent.width = 2048;
920 maxExtent.height = 2048;
921 maxExtent.depth = 2048;
922 }
923 maxMipLevels = util_logbase2(maxExtent.width) + 1;
924 maxArraySize = 1;
925 break;
926 }
927
928 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
929 /* Might be able to support but the entire format support is
930 * messy, so taking the lazy way out. */
931 maxArraySize = 1;
932 }
933
934 if (tiling == VK_IMAGE_TILING_OPTIMAL && info->type == VK_IMAGE_TYPE_2D &&
935 (format_feature_flags &
936 (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
937 !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
938 !(info->usage & VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR)) {
939 sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
940 }
941
942 if (tiling == VK_IMAGE_TILING_LINEAR && (format == VK_FORMAT_R32G32B32_SFLOAT ||
943 format == VK_FORMAT_R32G32B32_SINT || format == VK_FORMAT_R32G32B32_UINT)) {
944 /* R32G32B32 is a weird format and the driver currently only
945 * supports the barely minimum.
946 * TODO: Implement more if we really need to.
947 */
948 if (info->type == VK_IMAGE_TYPE_3D)
949 goto unsupported;
950 maxArraySize = 1;
951 maxMipLevels = 1;
952 }
953
954 /* We can't create 3d compressed 128bpp images that can be rendered to on GFX9 */
955 if (pdev->info.gfx_level >= GFX9 && info->type == VK_IMAGE_TYPE_3D && vk_format_get_blocksizebits(format) == 128 &&
956 vk_format_is_compressed(format) && (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
957 ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) || (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))) {
958 goto unsupported;
959 }
960
961 /* For some reasons, we can't create 1d block-compressed images that can be stored to with a
962 * different format on GFX6.
963 */
964 if (pdev->info.gfx_level == GFX6 && info->type == VK_IMAGE_TYPE_1D && vk_format_is_block_compressed(format) &&
965 (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
966 ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) || (info->usage & VK_IMAGE_USAGE_STORAGE_BIT))) {
967 goto unsupported;
968 }
969
970 /* From the Vulkan 1.3.206 spec:
971 *
972 * "VK_IMAGE_CREATE_EXTENDED_USAGE_BIT specifies that the image can be created with usage flags
973 * that are not supported for the format the image is created with but are supported for at least
974 * one format a VkImageView created from the image can have."
975 */
976 VkImageUsageFlags image_usage = info->usage;
977 if (info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT)
978 image_usage = 0;
979
980 if (image_usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
981 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT)) {
982 goto unsupported;
983 }
984 }
985
986 if (image_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
987 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT)) {
988 goto unsupported;
989 }
990 }
991
992 if (image_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
993 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT)) {
994 goto unsupported;
995 }
996 }
997
998 if (image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
999 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1000 goto unsupported;
1001 }
1002 }
1003
1004 if (image_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
1005 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT)) {
1006 goto unsupported;
1007 }
1008 }
1009
1010 if (image_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
1011 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT)) {
1012 goto unsupported;
1013 }
1014 }
1015
1016 if (image_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
1017 if (!(format_feature_flags &
1018 (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT))) {
1019 goto unsupported;
1020 }
1021 }
1022
1023 if (image_usage & VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR) {
1024 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR))
1025 goto unsupported;
1026 }
1027
1028 /* Sparse resources with multi-planar formats are unsupported. */
1029 if (info->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
1030 if (vk_format_get_plane_count(format) > 1)
1031 goto unsupported;
1032 }
1033
1034 if (info->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1035 /* Sparse textures are only supported on GFX8+. */
1036 if (pdev->info.gfx_level < GFX8)
1037 goto unsupported;
1038
1039 if (vk_format_get_plane_count(format) > 1 || info->type == VK_IMAGE_TYPE_1D ||
1040 info->tiling != VK_IMAGE_TILING_OPTIMAL || vk_format_is_depth_or_stencil(format))
1041 goto unsupported;
1042 }
1043
1044 if ((info->flags & (VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT)) &&
1045 radv_is_format_emulated(pdev, format)) {
1046 goto unsupported;
1047 }
1048
1049 *pImageFormatProperties = (VkImageFormatProperties){
1050 .maxExtent = maxExtent,
1051 .maxMipLevels = maxMipLevels,
1052 .maxArrayLayers = maxArraySize,
1053 .sampleCounts = sampleCounts,
1054
1055 /* FINISHME: Accurately calculate
1056 * VkImageFormatProperties::maxResourceSize.
1057 */
1058 .maxResourceSize = UINT32_MAX,
1059 };
1060
1061 if (mod_info) {
1062 result = radv_check_modifier_support(pdev, info, pImageFormatProperties, format, mod_info->drmFormatModifier);
1063 if (result != VK_SUCCESS)
1064 goto unsupported;
1065 }
1066
1067 return VK_SUCCESS;
1068 unsupported:
1069 *pImageFormatProperties = (VkImageFormatProperties){
1070 .maxExtent = {0, 0, 0},
1071 .maxMipLevels = 0,
1072 .maxArrayLayers = 0,
1073 .sampleCounts = 0,
1074 .maxResourceSize = 0,
1075 };
1076
1077 return result;
1078 }
1079
1080 static void
get_external_image_format_properties(struct radv_physical_device * pdev,const VkPhysicalDeviceImageFormatInfo2 * pImageFormatInfo,VkExternalMemoryHandleTypeFlagBits handleType,VkExternalMemoryProperties * external_properties,VkImageFormatProperties * format_properties)1081 get_external_image_format_properties(struct radv_physical_device *pdev,
1082 const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
1083 VkExternalMemoryHandleTypeFlagBits handleType,
1084 VkExternalMemoryProperties *external_properties,
1085 VkImageFormatProperties *format_properties)
1086 {
1087 VkExternalMemoryFeatureFlagBits flags = 0;
1088 VkExternalMemoryHandleTypeFlags export_flags = 0;
1089 VkExternalMemoryHandleTypeFlags compat_flags = 0;
1090
1091 if (radv_is_format_emulated(pdev, pImageFormatInfo->format))
1092 return;
1093
1094 if (pImageFormatInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)
1095 return;
1096
1097 switch (handleType) {
1098 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1099 if (pImageFormatInfo->tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
1100 break;
1101 FALLTHROUGH;
1102 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1103 if (pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
1104 break;
1105 flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1106 if (handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT &&
1107 pImageFormatInfo->tiling != VK_IMAGE_TILING_LINEAR)
1108 flags |= VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT;
1109
1110 compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
1111 if (pImageFormatInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1112 compat_flags |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1113 export_flags |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1114 }
1115 break;
1116 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
1117 if (!pdev->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer)
1118 break;
1119
1120 if (pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
1121 break;
1122
1123 format_properties->maxMipLevels = MIN2(1, format_properties->maxMipLevels);
1124 format_properties->maxArrayLayers = MIN2(1, format_properties->maxArrayLayers);
1125 format_properties->sampleCounts &= VK_SAMPLE_COUNT_1_BIT;
1126
1127 flags = VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1128
1129 /* advertise EXPORTABLE only when radv_create_ahb_memory supports the format */
1130 if (radv_android_gralloc_supports_format(pImageFormatInfo->format, pImageFormatInfo->usage))
1131 flags |= VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT;
1132
1133 compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
1134 break;
1135 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1136 flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1137 compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1138 break;
1139 default:
1140 break;
1141 }
1142
1143 *external_properties = (VkExternalMemoryProperties){
1144 .externalMemoryFeatures = flags,
1145 .exportFromImportedHandleTypes = export_flags,
1146 .compatibleHandleTypes = compat_flags,
1147 };
1148 }
1149
1150 VKAPI_ATTR VkResult VKAPI_CALL
radv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * base_info,VkImageFormatProperties2 * base_props)1151 radv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,
1152 const VkPhysicalDeviceImageFormatInfo2 *base_info,
1153 VkImageFormatProperties2 *base_props)
1154 {
1155 VK_FROM_HANDLE(radv_physical_device, pdev, physicalDevice);
1156 const struct radv_instance *instance = radv_physical_device_instance(pdev);
1157 const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1158 VkExternalImageFormatProperties *external_props = NULL;
1159 struct VkAndroidHardwareBufferUsageANDROID *android_usage = NULL;
1160 VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
1161 VkTextureLODGatherFormatPropertiesAMD *texture_lod_props = NULL;
1162 VkImageCompressionPropertiesEXT *image_compression_props = NULL;
1163 VkResult result;
1164 VkFormat format = radv_select_android_external_format(base_info->pNext, base_info->format);
1165
1166 result = radv_get_image_format_properties(pdev, base_info, format, &base_props->imageFormatProperties);
1167 if (result != VK_SUCCESS)
1168 return result;
1169
1170 /* Extract input structs */
1171 vk_foreach_struct_const (s, base_info->pNext) {
1172 switch (s->sType) {
1173 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1174 external_info = (const void *)s;
1175 break;
1176 default:
1177 break;
1178 }
1179 }
1180
1181 /* Extract output structs */
1182 vk_foreach_struct (s, base_props->pNext) {
1183 switch (s->sType) {
1184 case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1185 external_props = (void *)s;
1186 break;
1187 case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
1188 ycbcr_props = (void *)s;
1189 break;
1190 case VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID:
1191 android_usage = (void *)s;
1192 break;
1193 case VK_STRUCTURE_TYPE_TEXTURE_LOD_GATHER_FORMAT_PROPERTIES_AMD:
1194 texture_lod_props = (void *)s;
1195 break;
1196 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT:
1197 image_compression_props = (void *)s;
1198 break;
1199 default:
1200 break;
1201 }
1202 }
1203
1204 bool ahb_supported = pdev->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer;
1205 if (android_usage && ahb_supported) {
1206 android_usage->androidHardwareBufferUsage = vk_image_usage_to_ahb_usage(base_info->flags, base_info->usage);
1207 }
1208
1209 /* From the Vulkan 1.0.97 spec:
1210 *
1211 * If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1212 * behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1213 * present and VkExternalImageFormatProperties will be ignored.
1214 */
1215 if (external_info && external_info->handleType != 0) {
1216 VkExternalImageFormatProperties fallback_external_props;
1217
1218 if (!external_props) {
1219 memset(&fallback_external_props, 0, sizeof(fallback_external_props));
1220 external_props = &fallback_external_props;
1221 }
1222
1223 get_external_image_format_properties(pdev, base_info, external_info->handleType,
1224 &external_props->externalMemoryProperties,
1225 &base_props->imageFormatProperties);
1226 if (!external_props->externalMemoryProperties.externalMemoryFeatures) {
1227 /* From the Vulkan 1.0.97 spec:
1228 *
1229 * If handleType is not compatible with the [parameters] specified
1230 * in VkPhysicalDeviceImageFormatInfo2, then
1231 * vkGetPhysicalDeviceImageFormatProperties2 returns
1232 * VK_ERROR_FORMAT_NOT_SUPPORTED.
1233 */
1234 result = vk_errorf(pdev, VK_ERROR_FORMAT_NOT_SUPPORTED, "unsupported VkExternalMemoryHandleTypeFlagBits 0x%x",
1235 external_info->handleType);
1236 goto fail;
1237 }
1238 }
1239
1240 if (ycbcr_props) {
1241 ycbcr_props->combinedImageSamplerDescriptorCount = 1;
1242 }
1243
1244 if (texture_lod_props) {
1245 if (pdev->info.gfx_level >= GFX9) {
1246 texture_lod_props->supportsTextureGatherLODBiasAMD = true;
1247 } else {
1248 texture_lod_props->supportsTextureGatherLODBiasAMD = !vk_format_is_int(format);
1249 }
1250 }
1251
1252 if (image_compression_props) {
1253 image_compression_props->imageCompressionFixedRateFlags = VK_IMAGE_COMPRESSION_FIXED_RATE_NONE_EXT;
1254
1255 if (vk_format_is_depth_or_stencil(format)) {
1256 image_compression_props->imageCompressionFlags = (instance->debug_flags & RADV_DEBUG_NO_HIZ)
1257 ? VK_IMAGE_COMPRESSION_DISABLED_EXT
1258 : VK_IMAGE_COMPRESSION_DEFAULT_EXT;
1259 } else {
1260 image_compression_props->imageCompressionFlags =
1261 ((instance->debug_flags & RADV_DEBUG_NO_DCC) || pdev->info.gfx_level < GFX8)
1262 ? VK_IMAGE_COMPRESSION_DISABLED_EXT
1263 : VK_IMAGE_COMPRESSION_DEFAULT_EXT;
1264 }
1265 }
1266
1267 return VK_SUCCESS;
1268
1269 fail:
1270 if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1271 /* From the Vulkan 1.0.97 spec:
1272 *
1273 * If the combination of parameters to
1274 * vkGetPhysicalDeviceImageFormatProperties2 is not supported by
1275 * the implementation for use in vkCreateImage, then all members of
1276 * imageFormatProperties will be filled with zero.
1277 */
1278 base_props->imageFormatProperties = (VkImageFormatProperties){0};
1279 }
1280
1281 return result;
1282 }
1283
1284 static void
fill_sparse_image_format_properties(struct radv_physical_device * pdev,VkImageType type,VkFormat format,VkSparseImageFormatProperties * prop)1285 fill_sparse_image_format_properties(struct radv_physical_device *pdev, VkImageType type, VkFormat format,
1286 VkSparseImageFormatProperties *prop)
1287 {
1288 prop->aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1289 prop->flags = 0;
1290
1291 /* On GFX8 we first subdivide by level and then layer, leading to a single
1292 * miptail. On GFX9+ we first subdivide by layer and then level which results
1293 * in a miptail per layer. */
1294 if (pdev->info.gfx_level < GFX9)
1295 prop->flags |= VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT;
1296
1297 unsigned w, h;
1298 unsigned d = 1;
1299 if (type == VK_IMAGE_TYPE_3D) {
1300 if (pdev->info.gfx_level >= GFX9) {
1301 unsigned l2_size = 16 - util_logbase2(vk_format_get_blocksize(format));
1302 w = (1u << ((l2_size + 2) / 3)) * vk_format_get_blockwidth(format);
1303 h = (1u << ((l2_size + 1) / 3)) * vk_format_get_blockheight(format);
1304 d = (1u << ((l2_size + 0) / 3));
1305 } else {
1306 /* GFX7/GFX8 thick tiling modes */
1307 unsigned bs = vk_format_get_blocksize(format);
1308 unsigned l2_size = 16 - util_logbase2(bs) - (bs <= 4 ? 2 : 0);
1309 w = (1u << ((l2_size + 1) / 2)) * vk_format_get_blockwidth(format);
1310 h = (1u << (l2_size / 2)) * vk_format_get_blockheight(format);
1311 d = bs <= 4 ? 4 : 1;
1312 }
1313 } else {
1314 /* This assumes the sparse image tile size is always 64 KiB (1 << 16) */
1315 unsigned l2_size = 16 - util_logbase2(vk_format_get_blocksize(format));
1316 w = (1u << ((l2_size + 1) / 2)) * vk_format_get_blockwidth(format);
1317 h = (1u << (l2_size / 2)) * vk_format_get_blockheight(format);
1318 }
1319 prop->imageGranularity = (VkExtent3D){w, h, d};
1320 }
1321
1322 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)1323 radv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,
1324 const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
1325 uint32_t *pPropertyCount,
1326 VkSparseImageFormatProperties2 *pProperties)
1327 {
1328 VK_FROM_HANDLE(radv_physical_device, pdev, physicalDevice);
1329 VkResult result;
1330
1331 if (pFormatInfo->samples > VK_SAMPLE_COUNT_1_BIT) {
1332 *pPropertyCount = 0;
1333 return;
1334 }
1335
1336 const VkPhysicalDeviceImageFormatInfo2 fmt_info = {
1337 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1338 .format = pFormatInfo->format,
1339 .type = pFormatInfo->type,
1340 .tiling = pFormatInfo->tiling,
1341 .usage = pFormatInfo->usage,
1342 .flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT};
1343
1344 VkImageFormatProperties fmt_props;
1345 result = radv_get_image_format_properties(pdev, &fmt_info, pFormatInfo->format, &fmt_props);
1346 if (result != VK_SUCCESS) {
1347 *pPropertyCount = 0;
1348 return;
1349 }
1350
1351 VK_OUTARRAY_MAKE_TYPED(VkSparseImageFormatProperties2, out, pProperties, pPropertyCount);
1352
1353 vk_outarray_append_typed(VkSparseImageFormatProperties2, &out, prop)
1354 {
1355 fill_sparse_image_format_properties(pdev, pFormatInfo->type, pFormatInfo->format, &prop->properties);
1356 };
1357 }
1358
1359 VKAPI_ATTR void VKAPI_CALL
radv_GetImageSparseMemoryRequirements2(VkDevice _device,const VkImageSparseMemoryRequirementsInfo2 * pInfo,uint32_t * pSparseMemoryRequirementCount,VkSparseImageMemoryRequirements2 * pSparseMemoryRequirements)1360 radv_GetImageSparseMemoryRequirements2(VkDevice _device, const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1361 uint32_t *pSparseMemoryRequirementCount,
1362 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1363 {
1364 VK_FROM_HANDLE(radv_device, device, _device);
1365 VK_FROM_HANDLE(radv_image, image, pInfo->image);
1366 struct radv_physical_device *pdev = radv_device_physical(device);
1367
1368 if (!(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)) {
1369 *pSparseMemoryRequirementCount = 0;
1370 return;
1371 }
1372
1373 VK_OUTARRAY_MAKE_TYPED(VkSparseImageMemoryRequirements2, out, pSparseMemoryRequirements,
1374 pSparseMemoryRequirementCount);
1375
1376 vk_outarray_append_typed(VkSparseImageMemoryRequirements2, &out, req)
1377 {
1378 fill_sparse_image_format_properties(pdev, image->vk.image_type, image->vk.format,
1379 &req->memoryRequirements.formatProperties);
1380 req->memoryRequirements.imageMipTailFirstLod = image->planes[0].surface.first_mip_tail_level;
1381
1382 if (req->memoryRequirements.imageMipTailFirstLod < image->vk.mip_levels) {
1383 if (pdev->info.gfx_level >= GFX9) {
1384 /* The tail is always a single tile per layer. */
1385 req->memoryRequirements.imageMipTailSize = 65536;
1386 req->memoryRequirements.imageMipTailOffset =
1387 image->planes[0].surface.u.gfx9.prt_level_offset[req->memoryRequirements.imageMipTailFirstLod] & ~65535;
1388 req->memoryRequirements.imageMipTailStride = image->planes[0].surface.u.gfx9.surf_slice_size;
1389 } else {
1390 req->memoryRequirements.imageMipTailOffset =
1391 (uint64_t)image->planes[0]
1392 .surface.u.legacy.level[req->memoryRequirements.imageMipTailFirstLod]
1393 .offset_256B *
1394 256;
1395 req->memoryRequirements.imageMipTailSize = image->size - req->memoryRequirements.imageMipTailOffset;
1396 req->memoryRequirements.imageMipTailStride = 0;
1397 }
1398 } else {
1399 req->memoryRequirements.imageMipTailSize = 0;
1400 req->memoryRequirements.imageMipTailOffset = 0;
1401 req->memoryRequirements.imageMipTailStride = 0;
1402 }
1403 };
1404 }
1405
1406 VKAPI_ATTR void VKAPI_CALL
radv_GetDeviceImageSparseMemoryRequirements(VkDevice device,const VkDeviceImageMemoryRequirements * pInfo,uint32_t * pSparseMemoryRequirementCount,VkSparseImageMemoryRequirements2 * pSparseMemoryRequirements)1407 radv_GetDeviceImageSparseMemoryRequirements(VkDevice device, const VkDeviceImageMemoryRequirements *pInfo,
1408 uint32_t *pSparseMemoryRequirementCount,
1409 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1410 {
1411 UNUSED VkResult result;
1412 VkImage image;
1413
1414 /* Determining the image size/alignment require to create a surface, which is complicated without
1415 * creating an image.
1416 * TODO: Avoid creating an image.
1417 */
1418 result =
1419 radv_image_create(device, &(struct radv_image_create_info){.vk_info = pInfo->pCreateInfo}, NULL, &image, true);
1420 assert(result == VK_SUCCESS);
1421
1422 VkImageSparseMemoryRequirementsInfo2 info2 = {
1423 .sType = VK_STRUCTURE_TYPE_IMAGE_SPARSE_MEMORY_REQUIREMENTS_INFO_2,
1424 .image = image,
1425 };
1426
1427 radv_GetImageSparseMemoryRequirements2(device, &info2, pSparseMemoryRequirementCount, pSparseMemoryRequirements);
1428
1429 radv_DestroyImage(device, image, NULL);
1430 }
1431
1432 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)1433 radv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,
1434 const VkPhysicalDeviceExternalBufferInfo *pExternalBufferInfo,
1435 VkExternalBufferProperties *pExternalBufferProperties)
1436 {
1437 VkExternalMemoryFeatureFlagBits flags = 0;
1438 VkExternalMemoryHandleTypeFlags export_flags = 0;
1439 VkExternalMemoryHandleTypeFlags compat_flags = 0;
1440 switch (pExternalBufferInfo->handleType) {
1441 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1442 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1443 flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1444 compat_flags = export_flags =
1445 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1446 break;
1447 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1448 flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1449 compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1450 break;
1451 default:
1452 break;
1453 }
1454 pExternalBufferProperties->externalMemoryProperties = (VkExternalMemoryProperties){
1455 .externalMemoryFeatures = flags,
1456 .exportFromImportedHandleTypes = export_flags,
1457 .compatibleHandleTypes = compat_flags,
1458 };
1459 }
1460
1461 /* DCC channel type categories within which formats can be reinterpreted
1462 * while keeping the same DCC encoding. The swizzle must also match. */
1463 enum dcc_channel_type {
1464 dcc_channel_float,
1465 dcc_channel_uint,
1466 dcc_channel_sint,
1467 dcc_channel_incompatible,
1468 };
1469
1470 /* Return the type of DCC encoding. */
1471 static void
radv_get_dcc_channel_type(const struct util_format_description * desc,enum dcc_channel_type * type,unsigned * size)1472 radv_get_dcc_channel_type(const struct util_format_description *desc, enum dcc_channel_type *type, unsigned *size)
1473 {
1474 int i = util_format_get_first_non_void_channel(desc->format);
1475 if (i == -1) {
1476 *type = dcc_channel_incompatible;
1477 return;
1478 }
1479
1480 switch (desc->channel[i].size) {
1481 case 32:
1482 case 16:
1483 case 10:
1484 case 8:
1485 *size = desc->channel[i].size;
1486 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
1487 *type = dcc_channel_float;
1488 else if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
1489 *type = dcc_channel_uint;
1490 else
1491 *type = dcc_channel_sint;
1492 break;
1493 default:
1494 *type = dcc_channel_incompatible;
1495 break;
1496 }
1497 }
1498
1499 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1500 bool
radv_dcc_formats_compatible(enum amd_gfx_level gfx_level,VkFormat format1,VkFormat format2,bool * sign_reinterpret)1501 radv_dcc_formats_compatible(enum amd_gfx_level gfx_level, VkFormat format1, VkFormat format2, bool *sign_reinterpret)
1502 {
1503 const struct util_format_description *desc1, *desc2;
1504 enum dcc_channel_type type1, type2;
1505 unsigned size1 = 0, size2 = 0;
1506 int i;
1507
1508 /* All formats are compatible on GFX11. */
1509 if (gfx_level >= GFX11)
1510 return true;
1511
1512 if (format1 == format2)
1513 return true;
1514
1515 desc1 = vk_format_description(format1);
1516 desc2 = vk_format_description(format2);
1517
1518 if (desc1->nr_channels != desc2->nr_channels)
1519 return false;
1520
1521 /* Swizzles must be the same. */
1522 for (i = 0; i < desc1->nr_channels; i++)
1523 if (desc1->swizzle[i] <= PIPE_SWIZZLE_W && desc2->swizzle[i] <= PIPE_SWIZZLE_W &&
1524 desc1->swizzle[i] != desc2->swizzle[i])
1525 return false;
1526
1527 radv_get_dcc_channel_type(desc1, &type1, &size1);
1528 radv_get_dcc_channel_type(desc2, &type2, &size2);
1529
1530 if (type1 == dcc_channel_incompatible || type2 == dcc_channel_incompatible ||
1531 (type1 == dcc_channel_float) != (type2 == dcc_channel_float) || size1 != size2)
1532 return false;
1533
1534 if (type1 != type2)
1535 *sign_reinterpret = true;
1536
1537 return true;
1538 }
1539