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 =
678 vk_format_get_plane_count(format) + ac_modifier_has_dcc(mods[i]) + ac_modifier_has_dcc_retile(mods[i]);
679
680 vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out, out_props)
681 {
682 *out_props = (VkDrmFormatModifierPropertiesEXT){
683 .drmFormatModifier = mods[i],
684 .drmFormatModifierPlaneCount = planes,
685 .drmFormatModifierTilingFeatures = vk_format_features2_to_features(features),
686 };
687 };
688 }
689
690 free(mods);
691 }
692
693 static void
radv_list_drm_format_modifiers_2(struct radv_physical_device * pdev,VkFormat format,const VkFormatProperties3 * format_props,VkDrmFormatModifierPropertiesList2EXT * mod_list)694 radv_list_drm_format_modifiers_2(struct radv_physical_device *pdev, VkFormat format,
695 const VkFormatProperties3 *format_props,
696 VkDrmFormatModifierPropertiesList2EXT *mod_list)
697 {
698 unsigned mod_count;
699
700 if (!mod_list)
701 return;
702
703 if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format)) {
704 mod_list->drmFormatModifierCount = 0;
705 return;
706 }
707
708 VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierProperties2EXT, out, mod_list->pDrmFormatModifierProperties,
709 &mod_list->drmFormatModifierCount);
710
711 ac_get_supported_modifiers(&pdev->info, &radv_modifier_options, radv_format_to_pipe_format(format), &mod_count,
712 NULL);
713
714 uint64_t *mods = malloc(mod_count * sizeof(uint64_t));
715 if (!mods) {
716 /* We can't return an error here ... */
717 mod_list->drmFormatModifierCount = 0;
718 return;
719 }
720 ac_get_supported_modifiers(&pdev->info, &radv_modifier_options, radv_format_to_pipe_format(format), &mod_count,
721 mods);
722
723 for (unsigned i = 0; i < mod_count; ++i) {
724 VkFormatFeatureFlags2 features = radv_get_modifier_flags(pdev, format, mods[i], format_props);
725 if (!features)
726 continue;
727
728 unsigned planes =
729 vk_format_get_plane_count(format) + ac_modifier_has_dcc(mods[i]) + ac_modifier_has_dcc_retile(mods[i]);
730
731 vk_outarray_append_typed(VkDrmFormatModifierProperties2EXT, &out, out_props)
732 {
733 *out_props = (VkDrmFormatModifierProperties2EXT){
734 .drmFormatModifier = mods[i],
735 .drmFormatModifierPlaneCount = planes,
736 .drmFormatModifierTilingFeatures = features,
737 };
738 };
739 }
740
741 free(mods);
742 }
743
744 static VkResult
radv_check_modifier_support(struct radv_physical_device * pdev,const VkPhysicalDeviceImageFormatInfo2 * info,VkImageFormatProperties * props,VkFormat format,uint64_t modifier)745 radv_check_modifier_support(struct radv_physical_device *pdev, const VkPhysicalDeviceImageFormatInfo2 *info,
746 VkImageFormatProperties *props, VkFormat format, uint64_t modifier)
747 {
748 uint32_t max_width, max_height;
749
750 if (info->type != VK_IMAGE_TYPE_2D)
751 return VK_ERROR_FORMAT_NOT_SUPPORTED;
752
753 if (radv_is_format_emulated(pdev, format))
754 return VK_ERROR_FORMAT_NOT_SUPPORTED;
755
756 /* We did not add modifiers for sparse textures. */
757 if (info->flags &
758 (VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_SPARSE_ALIASED_BIT))
759 return VK_ERROR_FORMAT_NOT_SUPPORTED;
760
761 /*
762 * Need to check the modifier is supported in general:
763 * "If the drmFormatModifier is incompatible with the parameters specified
764 * in VkPhysicalDeviceImageFormatInfo2 and its pNext chain, then
765 * vkGetPhysicalDeviceImageFormatProperties2 returns VK_ERROR_FORMAT_NOT_SUPPORTED.
766 * The implementation must support the query of any drmFormatModifier,
767 * including unknown and invalid modifier values."
768 */
769 VkDrmFormatModifierPropertiesListEXT mod_list = {
770 .sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
771 };
772
773 VkFormatProperties2 format_props2 = {.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, .pNext = &mod_list};
774
775 radv_GetPhysicalDeviceFormatProperties2(radv_physical_device_to_handle(pdev), format, &format_props2);
776
777 if (!mod_list.drmFormatModifierCount)
778 return VK_ERROR_FORMAT_NOT_SUPPORTED;
779
780 mod_list.pDrmFormatModifierProperties =
781 calloc(mod_list.drmFormatModifierCount, sizeof(*mod_list.pDrmFormatModifierProperties));
782 if (!mod_list.pDrmFormatModifierProperties)
783 return VK_ERROR_OUT_OF_HOST_MEMORY;
784
785 radv_GetPhysicalDeviceFormatProperties2(radv_physical_device_to_handle(pdev), format, &format_props2);
786
787 bool found = false;
788 for (uint32_t i = 0; i < mod_list.drmFormatModifierCount && !found; ++i)
789 if (mod_list.pDrmFormatModifierProperties[i].drmFormatModifier == modifier)
790 found = true;
791
792 free(mod_list.pDrmFormatModifierProperties);
793
794 if (!found)
795 return VK_ERROR_FORMAT_NOT_SUPPORTED;
796
797 bool need_dcc_sign_reinterpret = false;
798 if (ac_modifier_has_dcc(modifier) &&
799 !radv_are_formats_dcc_compatible(pdev, info->pNext, format, info->flags, &need_dcc_sign_reinterpret) &&
800 !need_dcc_sign_reinterpret)
801 return VK_ERROR_FORMAT_NOT_SUPPORTED;
802
803 /* We can expand this as needed and implemented but there is not much demand
804 * for more. */
805 if (ac_modifier_has_dcc(modifier)) {
806 props->maxMipLevels = 1;
807 props->maxArrayLayers = 1;
808 }
809
810 ac_modifier_max_extent(&pdev->info, modifier, &max_width, &max_height);
811 props->maxExtent.width = MIN2(props->maxExtent.width, max_width);
812 props->maxExtent.height = MIN2(props->maxExtent.width, max_height);
813
814 /* We don't support MSAA for modifiers */
815 props->sampleCounts &= VK_SAMPLE_COUNT_1_BIT;
816 return VK_SUCCESS;
817 }
818
819 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties2 * pFormatProperties)820 radv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice, VkFormat format,
821 VkFormatProperties2 *pFormatProperties)
822 {
823 VK_FROM_HANDLE(radv_physical_device, pdev, physicalDevice);
824 VkFormatProperties3 format_props;
825
826 radv_physical_device_get_format_properties(pdev, format, &format_props);
827
828 pFormatProperties->formatProperties.linearTilingFeatures =
829 vk_format_features2_to_features(format_props.linearTilingFeatures);
830 pFormatProperties->formatProperties.optimalTilingFeatures =
831 vk_format_features2_to_features(format_props.optimalTilingFeatures);
832 pFormatProperties->formatProperties.bufferFeatures = vk_format_features2_to_features(format_props.bufferFeatures);
833
834 VkFormatProperties3 *format_props_extended = vk_find_struct(pFormatProperties, FORMAT_PROPERTIES_3);
835 if (format_props_extended) {
836 format_props_extended->linearTilingFeatures = format_props.linearTilingFeatures;
837 format_props_extended->optimalTilingFeatures = format_props.optimalTilingFeatures;
838 format_props_extended->bufferFeatures = format_props.bufferFeatures;
839 }
840
841 radv_list_drm_format_modifiers(pdev, format, &format_props,
842 vk_find_struct(pFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT));
843 radv_list_drm_format_modifiers_2(pdev, format, &format_props,
844 vk_find_struct(pFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_2_EXT));
845 }
846
847 static VkResult
radv_get_image_format_properties(struct radv_physical_device * pdev,const VkPhysicalDeviceImageFormatInfo2 * info,VkFormat format,VkImageFormatProperties * pImageFormatProperties)848 radv_get_image_format_properties(struct radv_physical_device *pdev, const VkPhysicalDeviceImageFormatInfo2 *info,
849 VkFormat format, VkImageFormatProperties *pImageFormatProperties)
850
851 {
852 VkFormatProperties3 format_props;
853 VkFormatFeatureFlags2 format_feature_flags;
854 VkExtent3D maxExtent;
855 uint32_t maxMipLevels;
856 uint32_t maxArraySize;
857 VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
858 const struct util_format_description *desc = vk_format_description(format);
859 enum amd_gfx_level gfx_level = pdev->info.gfx_level;
860 VkImageTiling tiling = info->tiling;
861 const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *mod_info =
862 vk_find_struct_const(info->pNext, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
863 VkResult result = VK_ERROR_FORMAT_NOT_SUPPORTED;
864
865 radv_physical_device_get_format_properties(pdev, format, &format_props);
866 if (tiling == VK_IMAGE_TILING_LINEAR) {
867 format_feature_flags = format_props.linearTilingFeatures;
868 } else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
869 format_feature_flags = format_props.optimalTilingFeatures;
870 } else if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
871 format_feature_flags = radv_get_modifier_flags(pdev, format, mod_info->drmFormatModifier, &format_props);
872 } else {
873 unreachable("bad VkImageTiling");
874 }
875
876 if (format_feature_flags == 0)
877 goto unsupported;
878
879 if (info->type == VK_IMAGE_TYPE_1D && radv_is_format_emulated(pdev, format))
880 goto unsupported;
881 if (info->type != VK_IMAGE_TYPE_2D && vk_format_is_depth_or_stencil(format))
882 goto unsupported;
883
884 switch (info->type) {
885 default:
886 unreachable("bad vkimage type\n");
887 case VK_IMAGE_TYPE_1D:
888 maxExtent.width = 16384;
889 maxExtent.height = 1;
890 maxExtent.depth = 1;
891 maxMipLevels = 15; /* log2(maxWidth) + 1 */
892 maxArraySize = gfx_level >= GFX10 ? 8192 : 2048;
893 break;
894 case VK_IMAGE_TYPE_2D:
895 maxExtent.width = 16384;
896 maxExtent.height = 16384;
897 maxExtent.depth = 1;
898 maxMipLevels = 15; /* log2(maxWidth) + 1 */
899 maxArraySize = gfx_level >= GFX10 ? 8192 : 2048;
900 break;
901 case VK_IMAGE_TYPE_3D:
902 if (gfx_level >= GFX10) {
903 maxExtent.width = 8192;
904 maxExtent.height = 8192;
905 maxExtent.depth = 8192;
906 } else {
907 maxExtent.width = 2048;
908 maxExtent.height = 2048;
909 maxExtent.depth = 2048;
910 }
911 maxMipLevels = util_logbase2(maxExtent.width) + 1;
912 maxArraySize = 1;
913 break;
914 }
915
916 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
917 /* Might be able to support but the entire format support is
918 * messy, so taking the lazy way out. */
919 maxArraySize = 1;
920 }
921
922 if (tiling == VK_IMAGE_TILING_OPTIMAL && info->type == VK_IMAGE_TYPE_2D &&
923 (format_feature_flags &
924 (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
925 !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
926 !(info->usage & VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR)) {
927 sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
928 }
929
930 if (tiling == VK_IMAGE_TILING_LINEAR && (format == VK_FORMAT_R32G32B32_SFLOAT ||
931 format == VK_FORMAT_R32G32B32_SINT || format == VK_FORMAT_R32G32B32_UINT)) {
932 /* R32G32B32 is a weird format and the driver currently only
933 * supports the barely minimum.
934 * TODO: Implement more if we really need to.
935 */
936 if (info->type == VK_IMAGE_TYPE_3D)
937 goto unsupported;
938 maxArraySize = 1;
939 maxMipLevels = 1;
940 }
941
942 /* We can't create 3d compressed 128bpp images that can be rendered to on GFX9 */
943 if (pdev->info.gfx_level >= GFX9 && info->type == VK_IMAGE_TYPE_3D && vk_format_get_blocksizebits(format) == 128 &&
944 vk_format_is_compressed(format) && (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
945 ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) || (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))) {
946 goto unsupported;
947 }
948
949 /* For some reasons, we can't create 1d block-compressed images that can be stored to with a
950 * different format on GFX6.
951 */
952 if (pdev->info.gfx_level == GFX6 && info->type == VK_IMAGE_TYPE_1D && vk_format_is_block_compressed(format) &&
953 (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
954 ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) || (info->usage & VK_IMAGE_USAGE_STORAGE_BIT))) {
955 goto unsupported;
956 }
957
958 /* From the Vulkan 1.3.206 spec:
959 *
960 * "VK_IMAGE_CREATE_EXTENDED_USAGE_BIT specifies that the image can be created with usage flags
961 * that are not supported for the format the image is created with but are supported for at least
962 * one format a VkImageView created from the image can have."
963 */
964 VkImageUsageFlags image_usage = info->usage;
965 if (info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT)
966 image_usage = 0;
967
968 if (image_usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
969 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT)) {
970 goto unsupported;
971 }
972 }
973
974 if (image_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
975 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT)) {
976 goto unsupported;
977 }
978 }
979
980 if (image_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
981 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT)) {
982 goto unsupported;
983 }
984 }
985
986 if (image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
987 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) {
988 goto unsupported;
989 }
990 }
991
992 if (image_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
993 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT)) {
994 goto unsupported;
995 }
996 }
997
998 if (image_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
999 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT)) {
1000 goto unsupported;
1001 }
1002 }
1003
1004 if (image_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
1005 if (!(format_feature_flags &
1006 (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT))) {
1007 goto unsupported;
1008 }
1009 }
1010
1011 if (image_usage & VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR) {
1012 if (!(format_feature_flags & VK_FORMAT_FEATURE_2_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR))
1013 goto unsupported;
1014 }
1015
1016 /* Sparse resources with multi-planar formats are unsupported. */
1017 if (info->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
1018 if (vk_format_get_plane_count(format) > 1)
1019 goto unsupported;
1020 }
1021
1022 if (info->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1023 /* Sparse textures are only supported on GFX8+. */
1024 if (pdev->info.gfx_level < GFX8)
1025 goto unsupported;
1026
1027 if (vk_format_get_plane_count(format) > 1 || info->type == VK_IMAGE_TYPE_1D ||
1028 info->tiling != VK_IMAGE_TILING_OPTIMAL || vk_format_is_depth_or_stencil(format))
1029 goto unsupported;
1030 }
1031
1032 if ((info->flags & (VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT)) &&
1033 radv_is_format_emulated(pdev, format)) {
1034 goto unsupported;
1035 }
1036
1037 *pImageFormatProperties = (VkImageFormatProperties){
1038 .maxExtent = maxExtent,
1039 .maxMipLevels = maxMipLevels,
1040 .maxArrayLayers = maxArraySize,
1041 .sampleCounts = sampleCounts,
1042
1043 /* FINISHME: Accurately calculate
1044 * VkImageFormatProperties::maxResourceSize.
1045 */
1046 .maxResourceSize = UINT32_MAX,
1047 };
1048
1049 if (mod_info) {
1050 result = radv_check_modifier_support(pdev, info, pImageFormatProperties, format, mod_info->drmFormatModifier);
1051 if (result != VK_SUCCESS)
1052 goto unsupported;
1053 }
1054
1055 return VK_SUCCESS;
1056 unsupported:
1057 *pImageFormatProperties = (VkImageFormatProperties){
1058 .maxExtent = {0, 0, 0},
1059 .maxMipLevels = 0,
1060 .maxArrayLayers = 0,
1061 .sampleCounts = 0,
1062 .maxResourceSize = 0,
1063 };
1064
1065 return result;
1066 }
1067
1068 static void
get_external_image_format_properties(struct radv_physical_device * pdev,const VkPhysicalDeviceImageFormatInfo2 * pImageFormatInfo,VkExternalMemoryHandleTypeFlagBits handleType,VkExternalMemoryProperties * external_properties,VkImageFormatProperties * format_properties)1069 get_external_image_format_properties(struct radv_physical_device *pdev,
1070 const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
1071 VkExternalMemoryHandleTypeFlagBits handleType,
1072 VkExternalMemoryProperties *external_properties,
1073 VkImageFormatProperties *format_properties)
1074 {
1075 VkExternalMemoryFeatureFlagBits flags = 0;
1076 VkExternalMemoryHandleTypeFlags export_flags = 0;
1077 VkExternalMemoryHandleTypeFlags compat_flags = 0;
1078
1079 if (radv_is_format_emulated(pdev, pImageFormatInfo->format))
1080 return;
1081
1082 if (pImageFormatInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)
1083 return;
1084
1085 switch (handleType) {
1086 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1087 if (pImageFormatInfo->tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
1088 break;
1089 FALLTHROUGH;
1090 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1091 if (pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
1092 break;
1093 flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1094 if (handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT &&
1095 pImageFormatInfo->tiling != VK_IMAGE_TILING_LINEAR)
1096 flags |= VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT;
1097
1098 compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
1099 if (pImageFormatInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1100 compat_flags |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1101 export_flags |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1102 }
1103 break;
1104 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
1105 if (!pdev->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer)
1106 break;
1107
1108 if (pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
1109 break;
1110
1111 format_properties->maxMipLevels = MIN2(1, format_properties->maxMipLevels);
1112 format_properties->maxArrayLayers = MIN2(1, format_properties->maxArrayLayers);
1113 format_properties->sampleCounts &= VK_SAMPLE_COUNT_1_BIT;
1114
1115 flags = VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1116
1117 /* advertise EXPORTABLE only when radv_create_ahb_memory supports the format */
1118 if (radv_android_gralloc_supports_format(pImageFormatInfo->format, pImageFormatInfo->usage))
1119 flags |= VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT;
1120
1121 compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
1122 break;
1123 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1124 flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1125 compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1126 break;
1127 default:
1128 break;
1129 }
1130
1131 *external_properties = (VkExternalMemoryProperties){
1132 .externalMemoryFeatures = flags,
1133 .exportFromImportedHandleTypes = export_flags,
1134 .compatibleHandleTypes = compat_flags,
1135 };
1136 }
1137
1138 VKAPI_ATTR VkResult VKAPI_CALL
radv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * base_info,VkImageFormatProperties2 * base_props)1139 radv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,
1140 const VkPhysicalDeviceImageFormatInfo2 *base_info,
1141 VkImageFormatProperties2 *base_props)
1142 {
1143 VK_FROM_HANDLE(radv_physical_device, pdev, physicalDevice);
1144 const struct radv_instance *instance = radv_physical_device_instance(pdev);
1145 const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1146 VkExternalImageFormatProperties *external_props = NULL;
1147 struct VkAndroidHardwareBufferUsageANDROID *android_usage = NULL;
1148 VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
1149 VkTextureLODGatherFormatPropertiesAMD *texture_lod_props = NULL;
1150 VkImageCompressionPropertiesEXT *image_compression_props = NULL;
1151 VkResult result;
1152 VkFormat format = radv_select_android_external_format(base_info->pNext, base_info->format);
1153
1154 result = radv_get_image_format_properties(pdev, base_info, format, &base_props->imageFormatProperties);
1155 if (result != VK_SUCCESS)
1156 return result;
1157
1158 /* Extract input structs */
1159 vk_foreach_struct_const (s, base_info->pNext) {
1160 switch (s->sType) {
1161 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1162 external_info = (const void *)s;
1163 break;
1164 default:
1165 break;
1166 }
1167 }
1168
1169 /* Extract output structs */
1170 vk_foreach_struct (s, base_props->pNext) {
1171 switch (s->sType) {
1172 case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1173 external_props = (void *)s;
1174 break;
1175 case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
1176 ycbcr_props = (void *)s;
1177 break;
1178 case VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID:
1179 android_usage = (void *)s;
1180 break;
1181 case VK_STRUCTURE_TYPE_TEXTURE_LOD_GATHER_FORMAT_PROPERTIES_AMD:
1182 texture_lod_props = (void *)s;
1183 break;
1184 case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT:
1185 image_compression_props = (void *)s;
1186 break;
1187 default:
1188 break;
1189 }
1190 }
1191
1192 bool ahb_supported = pdev->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer;
1193 if (android_usage && ahb_supported) {
1194 android_usage->androidHardwareBufferUsage = vk_image_usage_to_ahb_usage(base_info->flags, base_info->usage);
1195 }
1196
1197 /* From the Vulkan 1.0.97 spec:
1198 *
1199 * If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1200 * behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1201 * present and VkExternalImageFormatProperties will be ignored.
1202 */
1203 if (external_info && external_info->handleType != 0) {
1204 VkExternalImageFormatProperties fallback_external_props;
1205
1206 if (!external_props) {
1207 memset(&fallback_external_props, 0, sizeof(fallback_external_props));
1208 external_props = &fallback_external_props;
1209 }
1210
1211 get_external_image_format_properties(pdev, base_info, external_info->handleType,
1212 &external_props->externalMemoryProperties,
1213 &base_props->imageFormatProperties);
1214 if (!external_props->externalMemoryProperties.externalMemoryFeatures) {
1215 /* From the Vulkan 1.0.97 spec:
1216 *
1217 * If handleType is not compatible with the [parameters] specified
1218 * in VkPhysicalDeviceImageFormatInfo2, then
1219 * vkGetPhysicalDeviceImageFormatProperties2 returns
1220 * VK_ERROR_FORMAT_NOT_SUPPORTED.
1221 */
1222 result = vk_errorf(pdev, VK_ERROR_FORMAT_NOT_SUPPORTED, "unsupported VkExternalMemoryTypeFlagBitsKHR 0x%x",
1223 external_info->handleType);
1224 goto fail;
1225 }
1226 }
1227
1228 if (ycbcr_props) {
1229 ycbcr_props->combinedImageSamplerDescriptorCount = 1;
1230 }
1231
1232 if (texture_lod_props) {
1233 if (pdev->info.gfx_level >= GFX9) {
1234 texture_lod_props->supportsTextureGatherLODBiasAMD = true;
1235 } else {
1236 texture_lod_props->supportsTextureGatherLODBiasAMD = !vk_format_is_int(format);
1237 }
1238 }
1239
1240 if (image_compression_props) {
1241 image_compression_props->imageCompressionFixedRateFlags = VK_IMAGE_COMPRESSION_FIXED_RATE_NONE_EXT;
1242
1243 if (vk_format_is_depth_or_stencil(format)) {
1244 image_compression_props->imageCompressionFlags = (instance->debug_flags & RADV_DEBUG_NO_HIZ)
1245 ? VK_IMAGE_COMPRESSION_DISABLED_EXT
1246 : VK_IMAGE_COMPRESSION_DEFAULT_EXT;
1247 } else {
1248 image_compression_props->imageCompressionFlags =
1249 ((instance->debug_flags & RADV_DEBUG_NO_DCC) || pdev->info.gfx_level < GFX8)
1250 ? VK_IMAGE_COMPRESSION_DISABLED_EXT
1251 : VK_IMAGE_COMPRESSION_DEFAULT_EXT;
1252 }
1253 }
1254
1255 return VK_SUCCESS;
1256
1257 fail:
1258 if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1259 /* From the Vulkan 1.0.97 spec:
1260 *
1261 * If the combination of parameters to
1262 * vkGetPhysicalDeviceImageFormatProperties2 is not supported by
1263 * the implementation for use in vkCreateImage, then all members of
1264 * imageFormatProperties will be filled with zero.
1265 */
1266 base_props->imageFormatProperties = (VkImageFormatProperties){0};
1267 }
1268
1269 return result;
1270 }
1271
1272 static void
fill_sparse_image_format_properties(struct radv_physical_device * pdev,VkImageType type,VkFormat format,VkSparseImageFormatProperties * prop)1273 fill_sparse_image_format_properties(struct radv_physical_device *pdev, VkImageType type, VkFormat format,
1274 VkSparseImageFormatProperties *prop)
1275 {
1276 prop->aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1277 prop->flags = 0;
1278
1279 /* On GFX8 we first subdivide by level and then layer, leading to a single
1280 * miptail. On GFX9+ we first subdivide by layer and then level which results
1281 * in a miptail per layer. */
1282 if (pdev->info.gfx_level < GFX9)
1283 prop->flags |= VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT;
1284
1285 unsigned w, h;
1286 unsigned d = 1;
1287 if (type == VK_IMAGE_TYPE_3D) {
1288 if (pdev->info.gfx_level >= GFX9) {
1289 unsigned l2_size = 16 - util_logbase2(vk_format_get_blocksize(format));
1290 w = (1u << ((l2_size + 2) / 3)) * vk_format_get_blockwidth(format);
1291 h = (1u << ((l2_size + 1) / 3)) * vk_format_get_blockheight(format);
1292 d = (1u << ((l2_size + 0) / 3));
1293 } else {
1294 /* GFX7/GFX8 thick tiling modes */
1295 unsigned bs = vk_format_get_blocksize(format);
1296 unsigned l2_size = 16 - util_logbase2(bs) - (bs <= 4 ? 2 : 0);
1297 w = (1u << ((l2_size + 1) / 2)) * vk_format_get_blockwidth(format);
1298 h = (1u << (l2_size / 2)) * vk_format_get_blockheight(format);
1299 d = bs <= 4 ? 4 : 1;
1300 }
1301 } else {
1302 /* This assumes the sparse image tile size is always 64 KiB (1 << 16) */
1303 unsigned l2_size = 16 - util_logbase2(vk_format_get_blocksize(format));
1304 w = (1u << ((l2_size + 1) / 2)) * vk_format_get_blockwidth(format);
1305 h = (1u << (l2_size / 2)) * vk_format_get_blockheight(format);
1306 }
1307 prop->imageGranularity = (VkExtent3D){w, h, d};
1308 }
1309
1310 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)1311 radv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,
1312 const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
1313 uint32_t *pPropertyCount,
1314 VkSparseImageFormatProperties2 *pProperties)
1315 {
1316 VK_FROM_HANDLE(radv_physical_device, pdev, physicalDevice);
1317 VkResult result;
1318
1319 if (pFormatInfo->samples > VK_SAMPLE_COUNT_1_BIT) {
1320 *pPropertyCount = 0;
1321 return;
1322 }
1323
1324 const VkPhysicalDeviceImageFormatInfo2 fmt_info = {
1325 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1326 .format = pFormatInfo->format,
1327 .type = pFormatInfo->type,
1328 .tiling = pFormatInfo->tiling,
1329 .usage = pFormatInfo->usage,
1330 .flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT};
1331
1332 VkImageFormatProperties fmt_props;
1333 result = radv_get_image_format_properties(pdev, &fmt_info, pFormatInfo->format, &fmt_props);
1334 if (result != VK_SUCCESS) {
1335 *pPropertyCount = 0;
1336 return;
1337 }
1338
1339 VK_OUTARRAY_MAKE_TYPED(VkSparseImageFormatProperties2, out, pProperties, pPropertyCount);
1340
1341 vk_outarray_append_typed(VkSparseImageFormatProperties2, &out, prop)
1342 {
1343 fill_sparse_image_format_properties(pdev, pFormatInfo->type, pFormatInfo->format, &prop->properties);
1344 };
1345 }
1346
1347 VKAPI_ATTR void VKAPI_CALL
radv_GetImageSparseMemoryRequirements2(VkDevice _device,const VkImageSparseMemoryRequirementsInfo2 * pInfo,uint32_t * pSparseMemoryRequirementCount,VkSparseImageMemoryRequirements2 * pSparseMemoryRequirements)1348 radv_GetImageSparseMemoryRequirements2(VkDevice _device, const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1349 uint32_t *pSparseMemoryRequirementCount,
1350 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1351 {
1352 VK_FROM_HANDLE(radv_device, device, _device);
1353 VK_FROM_HANDLE(radv_image, image, pInfo->image);
1354 struct radv_physical_device *pdev = radv_device_physical(device);
1355
1356 if (!(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)) {
1357 *pSparseMemoryRequirementCount = 0;
1358 return;
1359 }
1360
1361 VK_OUTARRAY_MAKE_TYPED(VkSparseImageMemoryRequirements2, out, pSparseMemoryRequirements,
1362 pSparseMemoryRequirementCount);
1363
1364 vk_outarray_append_typed(VkSparseImageMemoryRequirements2, &out, req)
1365 {
1366 fill_sparse_image_format_properties(pdev, image->vk.image_type, image->vk.format,
1367 &req->memoryRequirements.formatProperties);
1368 req->memoryRequirements.imageMipTailFirstLod = image->planes[0].surface.first_mip_tail_level;
1369
1370 if (req->memoryRequirements.imageMipTailFirstLod < image->vk.mip_levels) {
1371 if (pdev->info.gfx_level >= GFX9) {
1372 /* The tail is always a single tile per layer. */
1373 req->memoryRequirements.imageMipTailSize = 65536;
1374 req->memoryRequirements.imageMipTailOffset =
1375 image->planes[0].surface.u.gfx9.prt_level_offset[req->memoryRequirements.imageMipTailFirstLod] & ~65535;
1376 req->memoryRequirements.imageMipTailStride = image->planes[0].surface.u.gfx9.surf_slice_size;
1377 } else {
1378 req->memoryRequirements.imageMipTailOffset =
1379 (uint64_t)image->planes[0]
1380 .surface.u.legacy.level[req->memoryRequirements.imageMipTailFirstLod]
1381 .offset_256B *
1382 256;
1383 req->memoryRequirements.imageMipTailSize = image->size - req->memoryRequirements.imageMipTailOffset;
1384 req->memoryRequirements.imageMipTailStride = 0;
1385 }
1386 } else {
1387 req->memoryRequirements.imageMipTailSize = 0;
1388 req->memoryRequirements.imageMipTailOffset = 0;
1389 req->memoryRequirements.imageMipTailStride = 0;
1390 }
1391 };
1392 }
1393
1394 VKAPI_ATTR void VKAPI_CALL
radv_GetDeviceImageSparseMemoryRequirements(VkDevice device,const VkDeviceImageMemoryRequirements * pInfo,uint32_t * pSparseMemoryRequirementCount,VkSparseImageMemoryRequirements2 * pSparseMemoryRequirements)1395 radv_GetDeviceImageSparseMemoryRequirements(VkDevice device, const VkDeviceImageMemoryRequirements *pInfo,
1396 uint32_t *pSparseMemoryRequirementCount,
1397 VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1398 {
1399 UNUSED VkResult result;
1400 VkImage image;
1401
1402 /* Determining the image size/alignment require to create a surface, which is complicated without
1403 * creating an image.
1404 * TODO: Avoid creating an image.
1405 */
1406 result =
1407 radv_image_create(device, &(struct radv_image_create_info){.vk_info = pInfo->pCreateInfo}, NULL, &image, true);
1408 assert(result == VK_SUCCESS);
1409
1410 VkImageSparseMemoryRequirementsInfo2 info2 = {
1411 .sType = VK_STRUCTURE_TYPE_IMAGE_SPARSE_MEMORY_REQUIREMENTS_INFO_2,
1412 .image = image,
1413 };
1414
1415 radv_GetImageSparseMemoryRequirements2(device, &info2, pSparseMemoryRequirementCount, pSparseMemoryRequirements);
1416
1417 radv_DestroyImage(device, image, NULL);
1418 }
1419
1420 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)1421 radv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,
1422 const VkPhysicalDeviceExternalBufferInfo *pExternalBufferInfo,
1423 VkExternalBufferProperties *pExternalBufferProperties)
1424 {
1425 VkExternalMemoryFeatureFlagBits flags = 0;
1426 VkExternalMemoryHandleTypeFlags export_flags = 0;
1427 VkExternalMemoryHandleTypeFlags compat_flags = 0;
1428 switch (pExternalBufferInfo->handleType) {
1429 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1430 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1431 flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1432 compat_flags = export_flags =
1433 VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1434 break;
1435 case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1436 flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1437 compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1438 break;
1439 default:
1440 break;
1441 }
1442 pExternalBufferProperties->externalMemoryProperties = (VkExternalMemoryProperties){
1443 .externalMemoryFeatures = flags,
1444 .exportFromImportedHandleTypes = export_flags,
1445 .compatibleHandleTypes = compat_flags,
1446 };
1447 }
1448
1449 /* DCC channel type categories within which formats can be reinterpreted
1450 * while keeping the same DCC encoding. The swizzle must also match. */
1451 enum dcc_channel_type {
1452 dcc_channel_float,
1453 dcc_channel_uint,
1454 dcc_channel_sint,
1455 dcc_channel_incompatible,
1456 };
1457
1458 /* Return the type of DCC encoding. */
1459 static void
radv_get_dcc_channel_type(const struct util_format_description * desc,enum dcc_channel_type * type,unsigned * size)1460 radv_get_dcc_channel_type(const struct util_format_description *desc, enum dcc_channel_type *type, unsigned *size)
1461 {
1462 int i = util_format_get_first_non_void_channel(desc->format);
1463 if (i == -1) {
1464 *type = dcc_channel_incompatible;
1465 return;
1466 }
1467
1468 switch (desc->channel[i].size) {
1469 case 32:
1470 case 16:
1471 case 10:
1472 case 8:
1473 *size = desc->channel[i].size;
1474 if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
1475 *type = dcc_channel_float;
1476 else if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
1477 *type = dcc_channel_uint;
1478 else
1479 *type = dcc_channel_sint;
1480 break;
1481 default:
1482 *type = dcc_channel_incompatible;
1483 break;
1484 }
1485 }
1486
1487 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1488 bool
radv_dcc_formats_compatible(enum amd_gfx_level gfx_level,VkFormat format1,VkFormat format2,bool * sign_reinterpret)1489 radv_dcc_formats_compatible(enum amd_gfx_level gfx_level, VkFormat format1, VkFormat format2, bool *sign_reinterpret)
1490 {
1491 const struct util_format_description *desc1, *desc2;
1492 enum dcc_channel_type type1, type2;
1493 unsigned size1 = 0, size2 = 0;
1494 int i;
1495
1496 /* All formats are compatible on GFX11. */
1497 if (gfx_level >= GFX11)
1498 return true;
1499
1500 if (format1 == format2)
1501 return true;
1502
1503 desc1 = vk_format_description(format1);
1504 desc2 = vk_format_description(format2);
1505
1506 if (desc1->nr_channels != desc2->nr_channels)
1507 return false;
1508
1509 /* Swizzles must be the same. */
1510 for (i = 0; i < desc1->nr_channels; i++)
1511 if (desc1->swizzle[i] <= PIPE_SWIZZLE_W && desc2->swizzle[i] <= PIPE_SWIZZLE_W &&
1512 desc1->swizzle[i] != desc2->swizzle[i])
1513 return false;
1514
1515 radv_get_dcc_channel_type(desc1, &type1, &size1);
1516 radv_get_dcc_channel_type(desc2, &type2, &size2);
1517
1518 if (type1 == dcc_channel_incompatible || type2 == dcc_channel_incompatible ||
1519 (type1 == dcc_channel_float) != (type2 == dcc_channel_float) || size1 != size2)
1520 return false;
1521
1522 if (type1 != type2)
1523 *sign_reinterpret = true;
1524
1525 return true;
1526 }
1527