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