• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "radv_debug.h"
26 #include "radv_private.h"
27 
28 #include "sid.h"
29 #include "vk_format.h"
30 
31 #include "vk_android.h"
32 #include "vk_util.h"
33 
34 #include "util/format_r11g11b10f.h"
35 #include "util/format_rgb9e5.h"
36 #include "util/format_srgb.h"
37 #include "util/half_float.h"
38 #include "vulkan/util/vk_enum_defines.h"
39 #include "vulkan/util/vk_format.h"
40 #include "ac_drm_fourcc.h"
41 
42 uint32_t
radv_translate_buffer_dataformat(const struct util_format_description * desc,int first_non_void)43 radv_translate_buffer_dataformat(const struct util_format_description *desc, int first_non_void)
44 {
45    unsigned type;
46    int i;
47 
48    assert(util_format_get_num_planes(desc->format) == 1);
49 
50    if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
51       return V_008F0C_BUF_DATA_FORMAT_10_11_11;
52 
53    if (first_non_void < 0)
54       return V_008F0C_BUF_DATA_FORMAT_INVALID;
55    type = desc->channel[first_non_void].type;
56 
57    if (type == UTIL_FORMAT_TYPE_FIXED)
58       return V_008F0C_BUF_DATA_FORMAT_INVALID;
59    if (desc->nr_channels == 4 && desc->channel[0].size == 10 && desc->channel[1].size == 10 &&
60        desc->channel[2].size == 10 && desc->channel[3].size == 2)
61       return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
62 
63    /* See whether the components are of the same size. */
64    for (i = 0; i < desc->nr_channels; i++) {
65       if (desc->channel[first_non_void].size != desc->channel[i].size)
66          return V_008F0C_BUF_DATA_FORMAT_INVALID;
67    }
68 
69    switch (desc->channel[first_non_void].size) {
70    case 8:
71       switch (desc->nr_channels) {
72       case 1:
73          return V_008F0C_BUF_DATA_FORMAT_8;
74       case 2:
75          return V_008F0C_BUF_DATA_FORMAT_8_8;
76       case 4:
77          return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
78       }
79       break;
80    case 16:
81       switch (desc->nr_channels) {
82       case 1:
83          return V_008F0C_BUF_DATA_FORMAT_16;
84       case 2:
85          return V_008F0C_BUF_DATA_FORMAT_16_16;
86       case 4:
87          return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
88       }
89       break;
90    case 32:
91       /* From the Southern Islands ISA documentation about MTBUF:
92        * 'Memory reads of data in memory that is 32 or 64 bits do not
93        * undergo any format conversion.'
94        */
95       if (type != UTIL_FORMAT_TYPE_FLOAT && !desc->channel[first_non_void].pure_integer)
96          return V_008F0C_BUF_DATA_FORMAT_INVALID;
97 
98       switch (desc->nr_channels) {
99       case 1:
100          return V_008F0C_BUF_DATA_FORMAT_32;
101       case 2:
102          return V_008F0C_BUF_DATA_FORMAT_32_32;
103       case 3:
104          return V_008F0C_BUF_DATA_FORMAT_32_32_32;
105       case 4:
106          return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
107       }
108       break;
109    case 64:
110       if (type != UTIL_FORMAT_TYPE_FLOAT && desc->nr_channels == 1)
111          return V_008F0C_BUF_DATA_FORMAT_32_32;
112    }
113 
114    return V_008F0C_BUF_DATA_FORMAT_INVALID;
115 }
116 
117 uint32_t
radv_translate_buffer_numformat(const struct util_format_description * desc,int first_non_void)118 radv_translate_buffer_numformat(const struct util_format_description *desc, int first_non_void)
119 {
120    assert(util_format_get_num_planes(desc->format) == 1);
121 
122    if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
123       return V_008F0C_BUF_NUM_FORMAT_FLOAT;
124 
125    if (first_non_void < 0)
126       return ~0;
127 
128    switch (desc->channel[first_non_void].type) {
129    case UTIL_FORMAT_TYPE_SIGNED:
130       if (desc->channel[first_non_void].normalized)
131          return V_008F0C_BUF_NUM_FORMAT_SNORM;
132       else if (desc->channel[first_non_void].pure_integer)
133          return V_008F0C_BUF_NUM_FORMAT_SINT;
134       else
135          return V_008F0C_BUF_NUM_FORMAT_SSCALED;
136       break;
137    case UTIL_FORMAT_TYPE_UNSIGNED:
138       if (desc->channel[first_non_void].normalized)
139          return V_008F0C_BUF_NUM_FORMAT_UNORM;
140       else if (desc->channel[first_non_void].pure_integer)
141          return V_008F0C_BUF_NUM_FORMAT_UINT;
142       else
143          return V_008F0C_BUF_NUM_FORMAT_USCALED;
144       break;
145    case UTIL_FORMAT_TYPE_FLOAT:
146    default:
147       return V_008F0C_BUF_NUM_FORMAT_FLOAT;
148    }
149 }
150 
151 static bool
radv_is_vertex_buffer_format_supported(VkFormat format)152 radv_is_vertex_buffer_format_supported(VkFormat format)
153 {
154    if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
155       return true;
156    if (format == VK_FORMAT_UNDEFINED || vk_format_is_srgb(format))
157       return false;
158 
159    int first_non_void = vk_format_get_first_non_void_channel(format);
160    if (first_non_void < 0)
161       return false;
162 
163    const struct util_format_description *desc = vk_format_description(format);
164    unsigned type = desc->channel[first_non_void].type;
165    if (type == UTIL_FORMAT_TYPE_FIXED)
166       return false;
167 
168    if (desc->nr_channels == 4 && desc->channel[0].size == 10 && desc->channel[1].size == 10 &&
169        desc->channel[2].size == 10 && desc->channel[3].size == 2)
170       return true;
171 
172    switch (desc->channel[first_non_void].size) {
173    case 8:
174    case 16:
175    case 32:
176    case 64:
177       return true;
178    default:
179       return false;
180    }
181 }
182 
183 uint32_t
radv_translate_tex_dataformat(VkFormat format,const struct util_format_description * desc,int first_non_void)184 radv_translate_tex_dataformat(VkFormat format, const struct util_format_description *desc, int first_non_void)
185 {
186    bool uniform = true;
187    int i;
188 
189    assert(vk_format_get_plane_count(format) == 1);
190 
191    /* Colorspace (return non-RGB formats directly). */
192    switch (desc->colorspace) {
193       /* Depth stencil formats */
194    case UTIL_FORMAT_COLORSPACE_ZS:
195       switch (format) {
196       case VK_FORMAT_D16_UNORM:
197          return V_008F14_IMG_DATA_FORMAT_16;
198       case VK_FORMAT_D24_UNORM_S8_UINT:
199       case VK_FORMAT_X8_D24_UNORM_PACK32:
200          return V_008F14_IMG_DATA_FORMAT_8_24;
201       case VK_FORMAT_S8_UINT:
202          return V_008F14_IMG_DATA_FORMAT_8;
203       case VK_FORMAT_D32_SFLOAT:
204          return V_008F14_IMG_DATA_FORMAT_32;
205       case VK_FORMAT_D32_SFLOAT_S8_UINT:
206          return V_008F14_IMG_DATA_FORMAT_X24_8_32;
207       default:
208          goto out_unknown;
209       }
210 
211    case UTIL_FORMAT_COLORSPACE_YUV:
212       goto out_unknown; /* TODO */
213 
214    default:
215       break;
216    }
217 
218    if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
219       switch (format) {
220       /* Don't ask me why this looks inverted. PAL does the same. */
221       case VK_FORMAT_G8B8G8R8_422_UNORM:
222          return V_008F14_IMG_DATA_FORMAT_BG_RG;
223       case VK_FORMAT_B8G8R8G8_422_UNORM:
224          return V_008F14_IMG_DATA_FORMAT_GB_GR;
225       default:
226          goto out_unknown;
227       }
228    }
229 
230    if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
231       switch (format) {
232       case VK_FORMAT_BC4_UNORM_BLOCK:
233       case VK_FORMAT_BC4_SNORM_BLOCK:
234          return V_008F14_IMG_DATA_FORMAT_BC4;
235       case VK_FORMAT_BC5_UNORM_BLOCK:
236       case VK_FORMAT_BC5_SNORM_BLOCK:
237          return V_008F14_IMG_DATA_FORMAT_BC5;
238       default:
239          break;
240       }
241    }
242 
243    if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
244       switch (format) {
245       case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
246       case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
247       case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
248       case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
249          return V_008F14_IMG_DATA_FORMAT_BC1;
250       case VK_FORMAT_BC2_UNORM_BLOCK:
251       case VK_FORMAT_BC2_SRGB_BLOCK:
252          return V_008F14_IMG_DATA_FORMAT_BC2;
253       case VK_FORMAT_BC3_UNORM_BLOCK:
254       case VK_FORMAT_BC3_SRGB_BLOCK:
255          return V_008F14_IMG_DATA_FORMAT_BC3;
256       default:
257          break;
258       }
259    }
260 
261    if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
262       switch (format) {
263       case VK_FORMAT_BC6H_UFLOAT_BLOCK:
264       case VK_FORMAT_BC6H_SFLOAT_BLOCK:
265          return V_008F14_IMG_DATA_FORMAT_BC6;
266       case VK_FORMAT_BC7_UNORM_BLOCK:
267       case VK_FORMAT_BC7_SRGB_BLOCK:
268          return V_008F14_IMG_DATA_FORMAT_BC7;
269       default:
270          break;
271       }
272    }
273 
274    if (desc->layout == UTIL_FORMAT_LAYOUT_ETC) {
275       switch (format) {
276       case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
277       case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
278          return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
279       case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
280       case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
281          return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
282       case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
283       case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
284          return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
285       case VK_FORMAT_EAC_R11_UNORM_BLOCK:
286       case VK_FORMAT_EAC_R11_SNORM_BLOCK:
287          return V_008F14_IMG_DATA_FORMAT_ETC2_R;
288       case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
289       case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
290          return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
291       default:
292          break;
293       }
294    }
295 
296    if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
297       return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
298    } else if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
299       return V_008F14_IMG_DATA_FORMAT_10_11_11;
300    }
301 
302    /* R8G8Bx_SNORM - TODO CxV8U8 */
303 
304    /* hw cannot support mixed formats (except depth/stencil, since only
305     * depth is read).*/
306    if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
307       goto out_unknown;
308 
309    /* See whether the components are of the same size. */
310    for (i = 1; i < desc->nr_channels; i++) {
311       uniform = uniform && desc->channel[0].size == desc->channel[i].size;
312    }
313 
314    /* Non-uniform formats. */
315    if (!uniform) {
316       switch (desc->nr_channels) {
317       case 3:
318          if (desc->channel[0].size == 5 && desc->channel[1].size == 6 && desc->channel[2].size == 5) {
319             return V_008F14_IMG_DATA_FORMAT_5_6_5;
320          }
321          goto out_unknown;
322       case 4:
323          if (desc->channel[0].size == 5 && desc->channel[1].size == 5 && desc->channel[2].size == 5 &&
324              desc->channel[3].size == 1) {
325             return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
326          }
327          if (desc->channel[0].size == 1 && desc->channel[1].size == 5 && desc->channel[2].size == 5 &&
328              desc->channel[3].size == 5) {
329             return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
330          }
331          if (desc->channel[0].size == 10 && desc->channel[1].size == 10 && desc->channel[2].size == 10 &&
332              desc->channel[3].size == 2) {
333             /* Closed VK driver does this also no 2/10/10/10 snorm */
334             if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED && desc->channel[0].normalized)
335                goto out_unknown;
336             return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
337          }
338          goto out_unknown;
339       }
340       goto out_unknown;
341    }
342 
343    if (first_non_void < 0 || first_non_void > 3)
344       goto out_unknown;
345 
346    /* uniform formats */
347    switch (desc->channel[first_non_void].size) {
348    case 4:
349       switch (desc->nr_channels) {
350 #if 0 /* Not supported for render targets */
351 		case 2:
352 			return V_008F14_IMG_DATA_FORMAT_4_4;
353 #endif
354       case 4:
355          return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
356       }
357       break;
358    case 8:
359       switch (desc->nr_channels) {
360       case 1:
361          return V_008F14_IMG_DATA_FORMAT_8;
362       case 2:
363          return V_008F14_IMG_DATA_FORMAT_8_8;
364       case 4:
365          return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
366       }
367       break;
368    case 16:
369       switch (desc->nr_channels) {
370       case 1:
371          return V_008F14_IMG_DATA_FORMAT_16;
372       case 2:
373          return V_008F14_IMG_DATA_FORMAT_16_16;
374       case 4:
375          return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
376       }
377       break;
378    case 32:
379       switch (desc->nr_channels) {
380       case 1:
381          return V_008F14_IMG_DATA_FORMAT_32;
382       case 2:
383          return V_008F14_IMG_DATA_FORMAT_32_32;
384       case 3:
385          return V_008F14_IMG_DATA_FORMAT_32_32_32;
386       case 4:
387          return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
388       }
389       break;
390    case 64:
391       if (desc->channel[0].type != UTIL_FORMAT_TYPE_FLOAT && desc->nr_channels == 1)
392          return V_008F14_IMG_DATA_FORMAT_32_32;
393       break;
394    }
395 
396 out_unknown:
397    /* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
398    return ~0;
399 }
400 
401 uint32_t
radv_translate_tex_numformat(VkFormat format,const struct util_format_description * desc,int first_non_void)402 radv_translate_tex_numformat(VkFormat format, const struct util_format_description *desc, int first_non_void)
403 {
404    assert(vk_format_get_plane_count(format) == 1);
405 
406    switch (format) {
407    case VK_FORMAT_D24_UNORM_S8_UINT:
408       return V_008F14_IMG_NUM_FORMAT_UNORM;
409    default:
410       if (first_non_void < 0) {
411          if (vk_format_is_compressed(format)) {
412             switch (format) {
413             case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
414             case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
415             case VK_FORMAT_BC2_SRGB_BLOCK:
416             case VK_FORMAT_BC3_SRGB_BLOCK:
417             case VK_FORMAT_BC7_SRGB_BLOCK:
418             case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
419             case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
420             case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
421                return V_008F14_IMG_NUM_FORMAT_SRGB;
422             case VK_FORMAT_BC4_SNORM_BLOCK:
423             case VK_FORMAT_BC5_SNORM_BLOCK:
424             case VK_FORMAT_BC6H_SFLOAT_BLOCK:
425             case VK_FORMAT_EAC_R11_SNORM_BLOCK:
426             case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
427                return V_008F14_IMG_NUM_FORMAT_SNORM;
428             default:
429                return V_008F14_IMG_NUM_FORMAT_UNORM;
430             }
431          } else if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
432             return V_008F14_IMG_NUM_FORMAT_UNORM;
433          } else {
434             return V_008F14_IMG_NUM_FORMAT_FLOAT;
435          }
436       } else if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
437          return V_008F14_IMG_NUM_FORMAT_SRGB;
438       } else {
439          switch (desc->channel[first_non_void].type) {
440          case UTIL_FORMAT_TYPE_FLOAT:
441             return V_008F14_IMG_NUM_FORMAT_FLOAT;
442          case UTIL_FORMAT_TYPE_SIGNED:
443             if (desc->channel[first_non_void].normalized)
444                return V_008F14_IMG_NUM_FORMAT_SNORM;
445             else if (desc->channel[first_non_void].pure_integer)
446                return V_008F14_IMG_NUM_FORMAT_SINT;
447             else
448                return V_008F14_IMG_NUM_FORMAT_SSCALED;
449          case UTIL_FORMAT_TYPE_UNSIGNED:
450             if (desc->channel[first_non_void].normalized)
451                return V_008F14_IMG_NUM_FORMAT_UNORM;
452             else if (desc->channel[first_non_void].pure_integer)
453                return V_008F14_IMG_NUM_FORMAT_UINT;
454             else
455                return V_008F14_IMG_NUM_FORMAT_USCALED;
456          default:
457             return V_008F14_IMG_NUM_FORMAT_UNORM;
458          }
459       }
460    }
461 }
462 
463 static bool
radv_is_sampler_format_supported(VkFormat format,bool * linear_sampling)464 radv_is_sampler_format_supported(VkFormat format, bool *linear_sampling)
465 {
466    const struct util_format_description *desc = vk_format_description(format);
467    uint32_t num_format;
468    if (format == VK_FORMAT_UNDEFINED || format == VK_FORMAT_R64_UINT || format == VK_FORMAT_R64_SINT)
469       return false;
470    num_format = radv_translate_tex_numformat(format, desc, vk_format_get_first_non_void_channel(format));
471 
472    if (num_format == V_008F14_IMG_NUM_FORMAT_USCALED || num_format == V_008F14_IMG_NUM_FORMAT_SSCALED)
473       return false;
474 
475    if (num_format == V_008F14_IMG_NUM_FORMAT_UNORM || num_format == V_008F14_IMG_NUM_FORMAT_SNORM ||
476        num_format == V_008F14_IMG_NUM_FORMAT_FLOAT || num_format == V_008F14_IMG_NUM_FORMAT_SRGB)
477       *linear_sampling = true;
478    else
479       *linear_sampling = false;
480    return radv_translate_tex_dataformat(format, vk_format_description(format),
481                                         vk_format_get_first_non_void_channel(format)) != ~0U;
482 }
483 
484 bool
radv_is_atomic_format_supported(VkFormat format)485 radv_is_atomic_format_supported(VkFormat format)
486 {
487    return format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT || format == VK_FORMAT_R32_SFLOAT ||
488           format == VK_FORMAT_R64_UINT || format == VK_FORMAT_R64_SINT;
489 }
490 
491 bool
radv_is_storage_image_format_supported(const struct radv_physical_device * physical_device,VkFormat format)492 radv_is_storage_image_format_supported(const struct radv_physical_device *physical_device, VkFormat format)
493 {
494    const struct util_format_description *desc = vk_format_description(format);
495    unsigned data_format, num_format;
496    if (format == VK_FORMAT_UNDEFINED)
497       return false;
498 
499    if (vk_format_is_depth_or_stencil(format))
500       return false;
501 
502    data_format = radv_translate_tex_dataformat(format, desc, vk_format_get_first_non_void_channel(format));
503    num_format = radv_translate_tex_numformat(format, desc, vk_format_get_first_non_void_channel(format));
504 
505    if (data_format == ~0 || num_format == ~0)
506       return false;
507 
508    /* Extracted from the GCN3 ISA document. */
509    switch (num_format) {
510    case V_008F14_IMG_NUM_FORMAT_UNORM:
511    case V_008F14_IMG_NUM_FORMAT_SNORM:
512    case V_008F14_IMG_NUM_FORMAT_UINT:
513    case V_008F14_IMG_NUM_FORMAT_SINT:
514    case V_008F14_IMG_NUM_FORMAT_FLOAT:
515       break;
516    default:
517       return false;
518    }
519 
520    switch (data_format) {
521    case V_008F14_IMG_DATA_FORMAT_8:
522    case V_008F14_IMG_DATA_FORMAT_16:
523    case V_008F14_IMG_DATA_FORMAT_8_8:
524    case V_008F14_IMG_DATA_FORMAT_32:
525    case V_008F14_IMG_DATA_FORMAT_16_16:
526    case V_008F14_IMG_DATA_FORMAT_10_11_11:
527    case V_008F14_IMG_DATA_FORMAT_11_11_10:
528    case V_008F14_IMG_DATA_FORMAT_10_10_10_2:
529    case V_008F14_IMG_DATA_FORMAT_2_10_10_10:
530    case V_008F14_IMG_DATA_FORMAT_8_8_8_8:
531    case V_008F14_IMG_DATA_FORMAT_32_32:
532    case V_008F14_IMG_DATA_FORMAT_16_16_16_16:
533    case V_008F14_IMG_DATA_FORMAT_32_32_32_32:
534    case V_008F14_IMG_DATA_FORMAT_5_6_5:
535    case V_008F14_IMG_DATA_FORMAT_1_5_5_5:
536    case V_008F14_IMG_DATA_FORMAT_5_5_5_1:
537    case V_008F14_IMG_DATA_FORMAT_4_4_4_4:
538       /* TODO: FMASK formats. */
539       return true;
540    case V_008F14_IMG_DATA_FORMAT_5_9_9_9:
541       return physical_device->rad_info.gfx_level >= GFX10_3;
542    default:
543       return false;
544    }
545 }
546 
547 bool
radv_is_buffer_format_supported(VkFormat format,bool * scaled)548 radv_is_buffer_format_supported(VkFormat format, bool *scaled)
549 {
550    const struct util_format_description *desc = vk_format_description(format);
551    unsigned data_format, num_format;
552    if (format == VK_FORMAT_UNDEFINED)
553       return false;
554 
555    data_format = radv_translate_buffer_dataformat(desc, vk_format_get_first_non_void_channel(format));
556    num_format = radv_translate_buffer_numformat(desc, vk_format_get_first_non_void_channel(format));
557 
558    if (scaled)
559       *scaled = (num_format == V_008F0C_BUF_NUM_FORMAT_SSCALED) || (num_format == V_008F0C_BUF_NUM_FORMAT_USCALED);
560    return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID && num_format != ~0;
561 }
562 
563 bool
radv_is_colorbuffer_format_supported(const struct radv_physical_device * pdevice,VkFormat format,bool * blendable)564 radv_is_colorbuffer_format_supported(const struct radv_physical_device *pdevice, VkFormat format, bool *blendable)
565 {
566    const struct util_format_description *desc = vk_format_description(format);
567    uint32_t color_format = ac_get_cb_format(pdevice->rad_info.gfx_level, desc->format);
568    uint32_t color_swap = radv_translate_colorswap(format, false);
569    uint32_t color_num_format = ac_get_cb_number_type(desc->format);
570 
571    if (color_num_format == V_028C70_NUMBER_UINT || color_num_format == V_028C70_NUMBER_SINT ||
572        color_format == V_028C70_COLOR_8_24 || color_format == V_028C70_COLOR_24_8 ||
573        color_format == V_028C70_COLOR_X24_8_32_FLOAT) {
574       *blendable = false;
575    } else
576       *blendable = true;
577 
578    if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 && pdevice->rad_info.gfx_level < GFX10_3)
579       return false;
580 
581    return color_format != V_028C70_COLOR_INVALID && color_swap != ~0U && color_num_format != ~0;
582 }
583 
584 static bool
radv_is_zs_format_supported(VkFormat format)585 radv_is_zs_format_supported(VkFormat format)
586 {
587    return radv_translate_dbformat(format) != V_028040_Z_INVALID || format == VK_FORMAT_S8_UINT;
588 }
589 
590 static bool
radv_is_filter_minmax_format_supported(const struct radv_physical_device * pdevice,VkFormat format)591 radv_is_filter_minmax_format_supported(const struct radv_physical_device *pdevice, VkFormat format)
592 {
593    enum amd_gfx_level gfx_level = pdevice->rad_info.gfx_level;
594 
595    switch (format) {
596    case VK_FORMAT_R4G4_UNORM_PACK8:
597    case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
598    case VK_FORMAT_A4R4G4B4_UNORM_PACK16_EXT:
599    case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
600    case VK_FORMAT_A4B4G4R4_UNORM_PACK16_EXT:
601    case VK_FORMAT_R5G6B5_UNORM_PACK16:
602    case VK_FORMAT_B5G6R5_UNORM_PACK16:
603    case VK_FORMAT_R5G5B5A1_UNORM_PACK16:
604    case VK_FORMAT_B5G5R5A1_UNORM_PACK16:
605    case VK_FORMAT_A1R5G5B5_UNORM_PACK16:
606    case VK_FORMAT_A1B5G5R5_UNORM_PACK16_KHR:
607    case VK_FORMAT_R8_UNORM:
608    case VK_FORMAT_A8_UNORM_KHR:
609    case VK_FORMAT_R8_SNORM:
610    case VK_FORMAT_R8_SRGB:
611    case VK_FORMAT_R8G8_UNORM:
612    case VK_FORMAT_R8G8_SNORM:
613    case VK_FORMAT_R8G8_SRGB:
614    case VK_FORMAT_R8G8B8A8_UNORM:
615    case VK_FORMAT_R8G8B8A8_SNORM:
616    case VK_FORMAT_R8G8B8A8_SRGB:
617    case VK_FORMAT_B8G8R8A8_UNORM:
618    case VK_FORMAT_B8G8R8A8_SNORM:
619    case VK_FORMAT_B8G8R8A8_SRGB:
620    case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
621    case VK_FORMAT_A8B8G8R8_SNORM_PACK32:
622    case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
623    case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
624    case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
625    case VK_FORMAT_R16_UNORM:
626    case VK_FORMAT_R16_SNORM:
627    case VK_FORMAT_R16_SFLOAT:
628    case VK_FORMAT_R16G16_UNORM:
629    case VK_FORMAT_R16G16_SNORM:
630    case VK_FORMAT_R16G16_SFLOAT:
631    case VK_FORMAT_R16G16B16A16_UNORM:
632    case VK_FORMAT_R16G16B16A16_SNORM:
633    case VK_FORMAT_R16G16B16A16_SFLOAT:
634    case VK_FORMAT_R32_SFLOAT:
635    case VK_FORMAT_R32G32_SFLOAT:
636    case VK_FORMAT_R32G32B32_SFLOAT:
637    case VK_FORMAT_R32G32B32A32_SFLOAT:
638    case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
639    case VK_FORMAT_D16_UNORM:
640    case VK_FORMAT_X8_D24_UNORM_PACK32:
641    case VK_FORMAT_D32_SFLOAT:
642    case VK_FORMAT_D16_UNORM_S8_UINT:
643    case VK_FORMAT_D24_UNORM_S8_UINT:
644    case VK_FORMAT_D32_SFLOAT_S8_UINT:
645       return true;
646    case VK_FORMAT_R8_UINT:
647    case VK_FORMAT_R8_SINT:
648    case VK_FORMAT_R8G8_UINT:
649    case VK_FORMAT_R8G8_SINT:
650    case VK_FORMAT_R8G8B8A8_UINT:
651    case VK_FORMAT_R8G8B8A8_SINT:
652    case VK_FORMAT_B8G8R8A8_UINT:
653    case VK_FORMAT_B8G8R8A8_SINT:
654    case VK_FORMAT_A8B8G8R8_UINT_PACK32:
655    case VK_FORMAT_A8B8G8R8_SINT_PACK32:
656    case VK_FORMAT_A2R10G10B10_UINT_PACK32:
657    case VK_FORMAT_A2B10G10R10_UINT_PACK32:
658    case VK_FORMAT_R16_UINT:
659    case VK_FORMAT_R16_SINT:
660    case VK_FORMAT_R16G16_UINT:
661    case VK_FORMAT_R16G16_SINT:
662    case VK_FORMAT_R16G16B16A16_UINT:
663    case VK_FORMAT_R16G16B16A16_SINT:
664    case VK_FORMAT_R32_UINT:
665    case VK_FORMAT_R32_SINT:
666    case VK_FORMAT_R32G32_UINT:
667    case VK_FORMAT_R32G32_SINT:
668    case VK_FORMAT_R32G32B32_UINT:
669    case VK_FORMAT_R32G32B32_SINT:
670    case VK_FORMAT_R32G32B32A32_UINT:
671    case VK_FORMAT_R32G32B32A32_SINT:
672    case VK_FORMAT_S8_UINT:
673       return gfx_level >= GFX9;
674    case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32:
675       return gfx_level >= GFX7;
676    default:
677       return false;
678    }
679 }
680 
681 bool
radv_is_format_emulated(const struct radv_physical_device * physical_device,VkFormat format)682 radv_is_format_emulated(const struct radv_physical_device *physical_device, VkFormat format)
683 {
684    if (physical_device->emulate_etc2 && vk_texcompress_etc2_emulation_format(format) != VK_FORMAT_UNDEFINED)
685       return true;
686 
687    if (physical_device->emulate_astc && vk_texcompress_astc_emulation_format(format) != VK_FORMAT_UNDEFINED)
688       return true;
689 
690    return false;
691 }
692 
693 bool
radv_device_supports_etc(const struct radv_physical_device * physical_device)694 radv_device_supports_etc(const struct radv_physical_device *physical_device)
695 {
696    return physical_device->rad_info.family == CHIP_VEGA10 || physical_device->rad_info.family == CHIP_RAVEN ||
697           physical_device->rad_info.family == CHIP_RAVEN2 || physical_device->rad_info.family == CHIP_STONEY;
698 }
699 
700 static void
radv_physical_device_get_format_properties(struct radv_physical_device * physical_device,VkFormat format,VkFormatProperties3 * out_properties)701 radv_physical_device_get_format_properties(struct radv_physical_device *physical_device, VkFormat format,
702                                            VkFormatProperties3 *out_properties)
703 {
704    VkFormatFeatureFlags2 linear = 0, tiled = 0, buffer = 0;
705    const struct util_format_description *desc = vk_format_description(format);
706    bool blendable;
707    bool scaled = false;
708    /* TODO: implement some software emulation of SUBSAMPLED formats. */
709    if (desc->format == PIPE_FORMAT_NONE || desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
710       out_properties->linearTilingFeatures = linear;
711       out_properties->optimalTilingFeatures = tiled;
712       out_properties->bufferFeatures = buffer;
713       return;
714    }
715 
716    if ((desc->layout == UTIL_FORMAT_LAYOUT_ETC && !radv_device_supports_etc(physical_device)) ||
717        desc->layout == UTIL_FORMAT_LAYOUT_ASTC) {
718       if (radv_is_format_emulated(physical_device, format)) {
719          /* required features for compressed formats */
720          tiled = VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
721                  VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
722                  VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
723 
724          VkFormat emulation_format = vk_texcompress_etc2_emulation_format(format);
725          if (emulation_format == VK_FORMAT_UNDEFINED)
726             emulation_format = vk_texcompress_astc_emulation_format(format);
727 
728          if (radv_is_filter_minmax_format_supported(physical_device, emulation_format))
729             tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
730       }
731       out_properties->linearTilingFeatures = linear;
732       out_properties->optimalTilingFeatures = tiled;
733       out_properties->bufferFeatures = buffer;
734       return;
735    }
736 
737    const bool multiplanar = vk_format_get_plane_count(format) > 1;
738    if (multiplanar || desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
739       uint64_t tiling = VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT |
740                         VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
741 
742       if (vk_format_get_ycbcr_info(format)) {
743          tiling |= VK_FORMAT_FEATURE_2_COSITED_CHROMA_SAMPLES_BIT | VK_FORMAT_FEATURE_2_MIDPOINT_CHROMA_SAMPLES_BIT;
744 
745          /* The subsampled formats have no support for linear filters. */
746          if (desc->layout != UTIL_FORMAT_LAYOUT_SUBSAMPLED)
747             tiling |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT;
748       }
749 
750       if (physical_device->instance->perftest_flags & RADV_PERFTEST_VIDEO_DECODE) {
751          if (format == VK_FORMAT_G8_B8R8_2PLANE_420_UNORM ||
752              format == VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16)
753             tiling |= VK_FORMAT_FEATURE_2_VIDEO_DECODE_OUTPUT_BIT_KHR | VK_FORMAT_FEATURE_2_VIDEO_DECODE_DPB_BIT_KHR;
754       }
755 
756       if (multiplanar)
757          tiling |= VK_FORMAT_FEATURE_2_DISJOINT_BIT;
758 
759       /* Fails for unknown reasons with linear tiling & subsampled formats. */
760       out_properties->linearTilingFeatures = desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED ? 0 : tiling;
761       out_properties->optimalTilingFeatures = tiling;
762       out_properties->bufferFeatures = 0;
763       return;
764    }
765 
766    if (radv_is_storage_image_format_supported(physical_device, format)) {
767       tiled |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT | VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
768                VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
769       linear |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT | VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
770                 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
771    }
772 
773    if (radv_is_vertex_buffer_format_supported(format))
774       buffer |= VK_FORMAT_FEATURE_2_VERTEX_BUFFER_BIT;
775 
776    if (radv_is_buffer_format_supported(format, &scaled)) {
777       if (format != VK_FORMAT_R64_UINT && format != VK_FORMAT_R64_SINT && !scaled && !vk_format_is_srgb(format))
778          buffer |= VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT;
779       buffer |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT | VK_FORMAT_FEATURE_2_STORAGE_READ_WITHOUT_FORMAT_BIT |
780                 VK_FORMAT_FEATURE_2_STORAGE_WRITE_WITHOUT_FORMAT_BIT;
781    }
782 
783    if (vk_format_is_depth_or_stencil(format)) {
784       if (radv_is_zs_format_supported(format)) {
785          tiled |= VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT;
786          tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT;
787          tiled |= VK_FORMAT_FEATURE_2_BLIT_SRC_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
788          tiled |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
789 
790          if (radv_is_filter_minmax_format_supported(physical_device, format))
791             tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
792 
793          if (vk_format_has_depth(format)) {
794             tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT |
795                      VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_DEPTH_COMPARISON_BIT;
796          }
797 
798          /* Don't support blitting surfaces with depth/stencil. */
799          if (vk_format_has_depth(format) && vk_format_has_stencil(format))
800             tiled &= ~VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
801 
802          /* Don't support linear depth surfaces */
803          linear = 0;
804       }
805    } else {
806       bool linear_sampling;
807       if (radv_is_sampler_format_supported(format, &linear_sampling)) {
808          linear |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
809          tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
810 
811          if (radv_is_filter_minmax_format_supported(physical_device, format)) {
812             tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
813             linear |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
814          }
815 
816          if (linear_sampling) {
817             linear |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
818             tiled |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
819          }
820 
821          /* Don't support blitting/minmax for R32G32B32 formats. */
822          if (format == VK_FORMAT_R32G32B32_SFLOAT || format == VK_FORMAT_R32G32B32_UINT ||
823              format == VK_FORMAT_R32G32B32_SINT) {
824             linear &= ~VK_FORMAT_FEATURE_2_BLIT_SRC_BIT;
825             linear &= ~VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
826          }
827       }
828       if (radv_is_colorbuffer_format_supported(physical_device, format, &blendable) && desc->channel[0].size != 64) {
829          linear |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
830          tiled |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT;
831          if (blendable) {
832             linear |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT;
833             tiled |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT;
834          }
835       }
836       if (tiled && !scaled) {
837          tiled |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
838       }
839 
840       /* Tiled formatting does not support NPOT pixel sizes */
841       if (!util_is_power_of_two_or_zero(vk_format_get_blocksize(format)))
842          tiled = 0;
843    }
844 
845    if (linear && !scaled) {
846       linear |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
847    }
848 
849    if (radv_is_atomic_format_supported(format)) {
850       buffer |= VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
851       linear |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
852       tiled |= VK_FORMAT_FEATURE_2_STORAGE_IMAGE_ATOMIC_BIT;
853    }
854 
855    switch (format) {
856    case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
857    case VK_FORMAT_A2B10G10R10_SNORM_PACK32:
858    case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
859    case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:
860    case VK_FORMAT_A2R10G10B10_SINT_PACK32:
861    case VK_FORMAT_A2B10G10R10_SINT_PACK32:
862       buffer &= ~(VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT | VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT);
863       linear = 0;
864       tiled = 0;
865       break;
866    case VK_FORMAT_R64_UINT:
867    case VK_FORMAT_R64_SINT:
868    case VK_FORMAT_R64_SFLOAT:
869       tiled |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
870       linear |= VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT;
871       break;
872    default:
873       break;
874    }
875 
876    switch (format) {
877    case VK_FORMAT_R32G32_SFLOAT:
878    case VK_FORMAT_R32G32B32_SFLOAT:
879    case VK_FORMAT_R32G32B32A32_SFLOAT:
880    case VK_FORMAT_R16G16_SFLOAT:
881    case VK_FORMAT_R16G16B16_SFLOAT:
882    case VK_FORMAT_R16G16B16A16_SFLOAT:
883    case VK_FORMAT_R16G16_SNORM:
884    case VK_FORMAT_R16G16_UNORM:
885    case VK_FORMAT_R16G16B16A16_SNORM:
886    case VK_FORMAT_R16G16B16A16_UNORM:
887    case VK_FORMAT_R8G8_SNORM:
888    case VK_FORMAT_R8G8_UNORM:
889    case VK_FORMAT_R8G8B8A8_SNORM:
890    case VK_FORMAT_R8G8B8A8_UNORM:
891    case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
892       buffer |= VK_FORMAT_FEATURE_2_ACCELERATION_STRUCTURE_VERTEX_BUFFER_BIT_KHR;
893       break;
894    default:
895       break;
896    }
897    /* addrlib does not support linear compressed textures. */
898    if (vk_format_is_compressed(format))
899       linear = 0;
900 
901    /* From the Vulkan spec 1.2.163:
902     *
903     * "VK_FORMAT_FEATURE_2_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT must be supported for the
904     *  following formats if the attachmentFragmentShadingRate feature is supported:"
905     *
906     * - VK_FORMAT_R8_UINT
907     */
908    if (format == VK_FORMAT_R8_UINT) {
909       tiled |= VK_FORMAT_FEATURE_2_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR;
910    }
911 
912    /* It's invalid to expose buffer features with depth/stencil formats. */
913    if (vk_format_is_depth_or_stencil(format)) {
914       buffer = 0;
915    }
916 
917    out_properties->linearTilingFeatures = linear;
918    out_properties->optimalTilingFeatures = tiled;
919    out_properties->bufferFeatures = buffer;
920 }
921 
922 uint32_t
radv_colorformat_endian_swap(uint32_t colorformat)923 radv_colorformat_endian_swap(uint32_t colorformat)
924 {
925    if (0 /*UTIL_ARCH_BIG_ENDIAN*/) {
926       switch (colorformat) {
927          /* 8-bit buffers. */
928       case V_028C70_COLOR_8:
929          return V_028C70_ENDIAN_NONE;
930 
931          /* 16-bit buffers. */
932       case V_028C70_COLOR_5_6_5:
933       case V_028C70_COLOR_1_5_5_5:
934       case V_028C70_COLOR_4_4_4_4:
935       case V_028C70_COLOR_16:
936       case V_028C70_COLOR_8_8:
937          return V_028C70_ENDIAN_8IN16;
938 
939          /* 32-bit buffers. */
940       case V_028C70_COLOR_8_8_8_8:
941       case V_028C70_COLOR_2_10_10_10:
942       case V_028C70_COLOR_8_24:
943       case V_028C70_COLOR_24_8:
944       case V_028C70_COLOR_16_16:
945          return V_028C70_ENDIAN_8IN32;
946 
947          /* 64-bit buffers. */
948       case V_028C70_COLOR_16_16_16_16:
949          return V_028C70_ENDIAN_8IN16;
950 
951       case V_028C70_COLOR_32_32:
952          return V_028C70_ENDIAN_8IN32;
953 
954          /* 128-bit buffers. */
955       case V_028C70_COLOR_32_32_32_32:
956          return V_028C70_ENDIAN_8IN32;
957       default:
958          return V_028C70_ENDIAN_NONE; /* Unsupported. */
959       }
960    } else {
961       return V_028C70_ENDIAN_NONE;
962    }
963 }
964 
965 uint32_t
radv_translate_dbformat(VkFormat format)966 radv_translate_dbformat(VkFormat format)
967 {
968    switch (format) {
969    case VK_FORMAT_D16_UNORM:
970    case VK_FORMAT_D16_UNORM_S8_UINT:
971       return V_028040_Z_16;
972    case VK_FORMAT_D32_SFLOAT:
973    case VK_FORMAT_D32_SFLOAT_S8_UINT:
974       return V_028040_Z_32_FLOAT;
975    default:
976       return V_028040_Z_INVALID;
977    }
978 }
979 
980 unsigned
radv_translate_colorswap(VkFormat format,bool do_endian_swap)981 radv_translate_colorswap(VkFormat format, bool do_endian_swap)
982 {
983    const struct util_format_description *desc = vk_format_description(format);
984 
985 #define HAS_SWIZZLE(chan, swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
986 
987    if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
988       return V_028C70_SWAP_STD;
989 
990    if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32)
991       return V_028C70_SWAP_STD;
992 
993    if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
994       return ~0U;
995 
996    switch (desc->nr_channels) {
997    case 1:
998       if (HAS_SWIZZLE(0, X))
999          return V_028C70_SWAP_STD; /* X___ */
1000       else if (HAS_SWIZZLE(3, X))
1001          return V_028C70_SWAP_ALT_REV; /* ___X */
1002       break;
1003    case 2:
1004       if ((HAS_SWIZZLE(0, X) && HAS_SWIZZLE(1, Y)) || (HAS_SWIZZLE(0, X) && HAS_SWIZZLE(1, NONE)) ||
1005           (HAS_SWIZZLE(0, NONE) && HAS_SWIZZLE(1, Y)))
1006          return V_028C70_SWAP_STD; /* XY__ */
1007       else if ((HAS_SWIZZLE(0, Y) && HAS_SWIZZLE(1, X)) || (HAS_SWIZZLE(0, Y) && HAS_SWIZZLE(1, NONE)) ||
1008                (HAS_SWIZZLE(0, NONE) && HAS_SWIZZLE(1, X)))
1009          /* YX__ */
1010          return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
1011       else if (HAS_SWIZZLE(0, X) && HAS_SWIZZLE(3, Y))
1012          return V_028C70_SWAP_ALT; /* X__Y */
1013       else if (HAS_SWIZZLE(0, Y) && HAS_SWIZZLE(3, X))
1014          return V_028C70_SWAP_ALT_REV; /* Y__X */
1015       break;
1016    case 3:
1017       if (HAS_SWIZZLE(0, X))
1018          return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
1019       else if (HAS_SWIZZLE(0, Z))
1020          return V_028C70_SWAP_STD_REV; /* ZYX */
1021       break;
1022    case 4:
1023       /* check the middle channels, the 1st and 4th channel can be NONE */
1024       if (HAS_SWIZZLE(1, Y) && HAS_SWIZZLE(2, Z)) {
1025          return V_028C70_SWAP_STD; /* XYZW */
1026       } else if (HAS_SWIZZLE(1, Z) && HAS_SWIZZLE(2, Y)) {
1027          return V_028C70_SWAP_STD_REV; /* WZYX */
1028       } else if (HAS_SWIZZLE(1, Y) && HAS_SWIZZLE(2, X)) {
1029          return V_028C70_SWAP_ALT; /* ZYXW */
1030       } else if (HAS_SWIZZLE(1, Z) && HAS_SWIZZLE(2, W)) {
1031          /* YZWX */
1032          if (desc->is_array)
1033             return V_028C70_SWAP_ALT_REV;
1034          else
1035             return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
1036       }
1037       break;
1038    }
1039    return ~0U;
1040 }
1041 
1042 bool
radv_format_pack_clear_color(VkFormat format,uint32_t clear_vals[2],VkClearColorValue * value)1043 radv_format_pack_clear_color(VkFormat format, uint32_t clear_vals[2], VkClearColorValue *value)
1044 {
1045    const struct util_format_description *desc = vk_format_description(format);
1046 
1047    if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
1048       clear_vals[0] = float3_to_r11g11b10f(value->float32);
1049       clear_vals[1] = 0;
1050       return true;
1051    } else if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
1052       clear_vals[0] = float3_to_rgb9e5(value->float32);
1053       clear_vals[1] = 0;
1054       return true;
1055    }
1056 
1057    if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
1058       fprintf(stderr, "failed to fast clear for non-plain format %d\n", format);
1059       return false;
1060    }
1061 
1062    if (!util_is_power_of_two_or_zero(desc->block.bits)) {
1063       fprintf(stderr, "failed to fast clear for NPOT format %d\n", format);
1064       return false;
1065    }
1066 
1067    if (desc->block.bits > 64) {
1068       /*
1069        * We have a 128 bits format, check if the first 3 components are the same.
1070        * Every elements has to be 32 bits since we don't support 64-bit formats,
1071        * and we can skip swizzling checks as alpha always comes last for these and
1072        * we do not care about the rest as they have to be the same.
1073        */
1074       if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
1075          if (value->float32[0] != value->float32[1] || value->float32[0] != value->float32[2])
1076             return false;
1077       } else {
1078          if (value->uint32[0] != value->uint32[1] || value->uint32[0] != value->uint32[2])
1079             return false;
1080       }
1081       clear_vals[0] = value->uint32[0];
1082       clear_vals[1] = value->uint32[3];
1083       return true;
1084    }
1085    uint64_t clear_val = 0;
1086 
1087    for (unsigned c = 0; c < 4; ++c) {
1088       if (desc->swizzle[c] >= 4)
1089          continue;
1090 
1091       const struct util_format_channel_description *channel = &desc->channel[desc->swizzle[c]];
1092       assert(channel->size);
1093 
1094       uint64_t v = 0;
1095       if (channel->pure_integer) {
1096          v = value->uint32[c] & ((1ULL << channel->size) - 1);
1097       } else if (channel->normalized) {
1098          if (channel->type == UTIL_FORMAT_TYPE_UNSIGNED && desc->swizzle[c] < 3 &&
1099              desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
1100             assert(channel->size == 8);
1101 
1102             v = util_format_linear_float_to_srgb_8unorm(value->float32[c]);
1103          } else {
1104             float f = MIN2(value->float32[c], 1.0f);
1105 
1106             if (channel->type == UTIL_FORMAT_TYPE_UNSIGNED) {
1107                f = MAX2(f, 0.0f) * ((1ULL << channel->size) - 1);
1108             } else {
1109                f = MAX2(f, -1.0f) * ((1ULL << (channel->size - 1)) - 1);
1110             }
1111 
1112             /* The hardware rounds before conversion. */
1113             if (f > 0)
1114                f += 0.5f;
1115             else
1116                f -= 0.5f;
1117 
1118             v = (uint64_t)f;
1119          }
1120       } else if (channel->type == UTIL_FORMAT_TYPE_FLOAT) {
1121          if (channel->size == 32) {
1122             memcpy(&v, &value->float32[c], 4);
1123          } else if (channel->size == 16) {
1124             v = _mesa_float_to_float16_rtz(value->float32[c]);
1125          } else {
1126             fprintf(stderr, "failed to fast clear for unhandled float size in format %d\n", format);
1127             return false;
1128          }
1129       } else {
1130          fprintf(stderr, "failed to fast clear for unhandled component type in format %d\n", format);
1131          return false;
1132       }
1133       clear_val |= (v & ((1ULL << channel->size) - 1)) << channel->shift;
1134    }
1135 
1136    clear_vals[0] = clear_val;
1137    clear_vals[1] = clear_val >> 32;
1138 
1139    return true;
1140 }
1141 
1142 static const struct ac_modifier_options radv_modifier_options = {
1143    .dcc = true,
1144    .dcc_retile = true,
1145 };
1146 
1147 static VkFormatFeatureFlags2
radv_get_modifier_flags(struct radv_physical_device * dev,VkFormat format,uint64_t modifier,const VkFormatProperties3 * props)1148 radv_get_modifier_flags(struct radv_physical_device *dev, VkFormat format, uint64_t modifier,
1149                         const VkFormatProperties3 *props)
1150 {
1151    VkFormatFeatureFlags2 features;
1152 
1153    if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format))
1154       return 0;
1155 
1156    if (modifier == DRM_FORMAT_MOD_LINEAR)
1157       features = props->linearTilingFeatures;
1158    else
1159       features = props->optimalTilingFeatures;
1160 
1161    /* Unconditionally disable DISJOINT support for modifiers for now */
1162    features &= ~VK_FORMAT_FEATURE_2_DISJOINT_BIT;
1163 
1164    if (ac_modifier_has_dcc(modifier)) {
1165       /* We don't enable DCC for multi-planar formats */
1166       if (vk_format_get_plane_count(format) > 1)
1167          return 0;
1168 
1169       /* Only disable support for STORAGE_IMAGE on modifiers that
1170        * do not support DCC image stores.
1171        */
1172       if (!ac_modifier_supports_dcc_image_stores(dev->rad_info.gfx_level, modifier) ||
1173           radv_is_atomic_format_supported(format))
1174          features &= ~VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT;
1175 
1176       if (dev->instance->debug_flags & (RADV_DEBUG_NO_DCC | RADV_DEBUG_NO_DISPLAY_DCC))
1177          return 0;
1178    }
1179 
1180    return features;
1181 }
1182 
1183 static void
radv_list_drm_format_modifiers(struct radv_physical_device * dev,VkFormat format,const VkFormatProperties3 * format_props,VkDrmFormatModifierPropertiesListEXT * mod_list)1184 radv_list_drm_format_modifiers(struct radv_physical_device *dev, VkFormat format,
1185                                const VkFormatProperties3 *format_props, VkDrmFormatModifierPropertiesListEXT *mod_list)
1186 {
1187    unsigned mod_count;
1188 
1189    if (!mod_list)
1190       return;
1191 
1192    if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format)) {
1193       mod_list->drmFormatModifierCount = 0;
1194       return;
1195    }
1196 
1197    VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierPropertiesEXT, out, mod_list->pDrmFormatModifierProperties,
1198                           &mod_list->drmFormatModifierCount);
1199 
1200    ac_get_supported_modifiers(&dev->rad_info, &radv_modifier_options, vk_format_to_pipe_format(format), &mod_count,
1201                               NULL);
1202 
1203    uint64_t *mods = malloc(mod_count * sizeof(uint64_t));
1204    if (!mods) {
1205       /* We can't return an error here ... */
1206       mod_list->drmFormatModifierCount = 0;
1207       return;
1208    }
1209    ac_get_supported_modifiers(&dev->rad_info, &radv_modifier_options, vk_format_to_pipe_format(format), &mod_count,
1210                               mods);
1211 
1212    for (unsigned i = 0; i < mod_count; ++i) {
1213       VkFormatFeatureFlags2 features = radv_get_modifier_flags(dev, format, mods[i], format_props);
1214       if (!features)
1215          continue;
1216 
1217       unsigned planes =
1218          vk_format_get_plane_count(format) + ac_modifier_has_dcc(mods[i]) + ac_modifier_has_dcc_retile(mods[i]);
1219 
1220       vk_outarray_append_typed(VkDrmFormatModifierPropertiesEXT, &out, out_props)
1221       {
1222          *out_props = (VkDrmFormatModifierPropertiesEXT){
1223             .drmFormatModifier = mods[i],
1224             .drmFormatModifierPlaneCount = planes,
1225             .drmFormatModifierTilingFeatures = vk_format_features2_to_features(features),
1226          };
1227       };
1228    }
1229 
1230    free(mods);
1231 }
1232 
1233 static void
radv_list_drm_format_modifiers_2(struct radv_physical_device * dev,VkFormat format,const VkFormatProperties3 * format_props,VkDrmFormatModifierPropertiesList2EXT * mod_list)1234 radv_list_drm_format_modifiers_2(struct radv_physical_device *dev, VkFormat format,
1235                                  const VkFormatProperties3 *format_props,
1236                                  VkDrmFormatModifierPropertiesList2EXT *mod_list)
1237 {
1238    unsigned mod_count;
1239 
1240    if (!mod_list)
1241       return;
1242 
1243    if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format)) {
1244       mod_list->drmFormatModifierCount = 0;
1245       return;
1246    }
1247 
1248    VK_OUTARRAY_MAKE_TYPED(VkDrmFormatModifierProperties2EXT, out, mod_list->pDrmFormatModifierProperties,
1249                           &mod_list->drmFormatModifierCount);
1250 
1251    ac_get_supported_modifiers(&dev->rad_info, &radv_modifier_options, vk_format_to_pipe_format(format), &mod_count,
1252                               NULL);
1253 
1254    uint64_t *mods = malloc(mod_count * sizeof(uint64_t));
1255    if (!mods) {
1256       /* We can't return an error here ... */
1257       mod_list->drmFormatModifierCount = 0;
1258       return;
1259    }
1260    ac_get_supported_modifiers(&dev->rad_info, &radv_modifier_options, vk_format_to_pipe_format(format), &mod_count,
1261                               mods);
1262 
1263    for (unsigned i = 0; i < mod_count; ++i) {
1264       VkFormatFeatureFlags2 features = radv_get_modifier_flags(dev, format, mods[i], format_props);
1265       if (!features)
1266          continue;
1267 
1268       unsigned planes =
1269          vk_format_get_plane_count(format) + ac_modifier_has_dcc(mods[i]) + ac_modifier_has_dcc_retile(mods[i]);
1270 
1271       vk_outarray_append_typed(VkDrmFormatModifierProperties2EXT, &out, out_props)
1272       {
1273          *out_props = (VkDrmFormatModifierProperties2EXT){
1274             .drmFormatModifier = mods[i],
1275             .drmFormatModifierPlaneCount = planes,
1276             .drmFormatModifierTilingFeatures = features,
1277          };
1278       };
1279    }
1280 
1281    free(mods);
1282 }
1283 
1284 static VkResult
radv_check_modifier_support(struct radv_physical_device * dev,const VkPhysicalDeviceImageFormatInfo2 * info,VkImageFormatProperties * props,VkFormat format,uint64_t modifier)1285 radv_check_modifier_support(struct radv_physical_device *dev, const VkPhysicalDeviceImageFormatInfo2 *info,
1286                             VkImageFormatProperties *props, VkFormat format, uint64_t modifier)
1287 {
1288    uint32_t max_width, max_height;
1289 
1290    if (info->type != VK_IMAGE_TYPE_2D)
1291       return VK_ERROR_FORMAT_NOT_SUPPORTED;
1292 
1293    if (radv_is_format_emulated(dev, format))
1294       return VK_ERROR_FORMAT_NOT_SUPPORTED;
1295 
1296    /* We did not add modifiers for sparse textures. */
1297    if (info->flags &
1298        (VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_SPARSE_ALIASED_BIT))
1299       return VK_ERROR_FORMAT_NOT_SUPPORTED;
1300 
1301    /*
1302     * Need to check the modifier is supported in general:
1303     * "If the drmFormatModifier is incompatible with the parameters specified
1304     * in VkPhysicalDeviceImageFormatInfo2 and its pNext chain, then
1305     * vkGetPhysicalDeviceImageFormatProperties2 returns VK_ERROR_FORMAT_NOT_SUPPORTED.
1306     * The implementation must support the query of any drmFormatModifier,
1307     * including unknown and invalid modifier values."
1308     */
1309    VkDrmFormatModifierPropertiesListEXT mod_list = {
1310       .sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
1311    };
1312 
1313    VkFormatProperties2 format_props2 = {.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2, .pNext = &mod_list};
1314 
1315    radv_GetPhysicalDeviceFormatProperties2(radv_physical_device_to_handle(dev), format, &format_props2);
1316 
1317    if (!mod_list.drmFormatModifierCount)
1318       return VK_ERROR_FORMAT_NOT_SUPPORTED;
1319 
1320    mod_list.pDrmFormatModifierProperties =
1321       calloc(mod_list.drmFormatModifierCount, sizeof(*mod_list.pDrmFormatModifierProperties));
1322    if (!mod_list.pDrmFormatModifierProperties)
1323       return VK_ERROR_OUT_OF_HOST_MEMORY;
1324 
1325    radv_GetPhysicalDeviceFormatProperties2(radv_physical_device_to_handle(dev), format, &format_props2);
1326 
1327    bool found = false;
1328    for (uint32_t i = 0; i < mod_list.drmFormatModifierCount && !found; ++i)
1329       if (mod_list.pDrmFormatModifierProperties[i].drmFormatModifier == modifier)
1330          found = true;
1331 
1332    free(mod_list.pDrmFormatModifierProperties);
1333 
1334    if (!found)
1335       return VK_ERROR_FORMAT_NOT_SUPPORTED;
1336 
1337    bool need_dcc_sign_reinterpret = false;
1338    if (ac_modifier_has_dcc(modifier) &&
1339        !radv_are_formats_dcc_compatible(dev, info->pNext, format, info->flags, &need_dcc_sign_reinterpret) &&
1340        !need_dcc_sign_reinterpret)
1341       return VK_ERROR_FORMAT_NOT_SUPPORTED;
1342 
1343    /* We can expand this as needed and implemented but there is not much demand
1344     * for more. */
1345    if (ac_modifier_has_dcc(modifier)) {
1346       props->maxMipLevels = 1;
1347       props->maxArrayLayers = 1;
1348    }
1349 
1350    ac_modifier_max_extent(&dev->rad_info, modifier, &max_width, &max_height);
1351    props->maxExtent.width = MIN2(props->maxExtent.width, max_width);
1352    props->maxExtent.height = MIN2(props->maxExtent.width, max_height);
1353 
1354    /* We don't support MSAA for modifiers */
1355    props->sampleCounts &= VK_SAMPLE_COUNT_1_BIT;
1356    return VK_SUCCESS;
1357 }
1358 
1359 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties2 * pFormatProperties)1360 radv_GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice, VkFormat format,
1361                                         VkFormatProperties2 *pFormatProperties)
1362 {
1363    RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1364    VkFormatProperties3 format_props;
1365 
1366    radv_physical_device_get_format_properties(physical_device, format, &format_props);
1367 
1368    pFormatProperties->formatProperties.linearTilingFeatures =
1369       vk_format_features2_to_features(format_props.linearTilingFeatures);
1370    pFormatProperties->formatProperties.optimalTilingFeatures =
1371       vk_format_features2_to_features(format_props.optimalTilingFeatures);
1372    pFormatProperties->formatProperties.bufferFeatures = vk_format_features2_to_features(format_props.bufferFeatures);
1373 
1374    VkFormatProperties3 *format_props_extended = vk_find_struct(pFormatProperties, FORMAT_PROPERTIES_3);
1375    if (format_props_extended) {
1376       format_props_extended->linearTilingFeatures = format_props.linearTilingFeatures;
1377       format_props_extended->optimalTilingFeatures = format_props.optimalTilingFeatures;
1378       format_props_extended->bufferFeatures = format_props.bufferFeatures;
1379    }
1380 
1381    radv_list_drm_format_modifiers(physical_device, format, &format_props,
1382                                   vk_find_struct(pFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT));
1383    radv_list_drm_format_modifiers_2(physical_device, format, &format_props,
1384                                     vk_find_struct(pFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_2_EXT));
1385 }
1386 
1387 static VkResult
radv_get_image_format_properties(struct radv_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * info,VkFormat format,VkImageFormatProperties * pImageFormatProperties)1388 radv_get_image_format_properties(struct radv_physical_device *physical_device,
1389                                  const VkPhysicalDeviceImageFormatInfo2 *info, VkFormat format,
1390                                  VkImageFormatProperties *pImageFormatProperties)
1391 
1392 {
1393    VkFormatProperties3 format_props;
1394    VkFormatFeatureFlags2 format_feature_flags;
1395    VkExtent3D maxExtent;
1396    uint32_t maxMipLevels;
1397    uint32_t maxArraySize;
1398    VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1399    const struct util_format_description *desc = vk_format_description(format);
1400    enum amd_gfx_level gfx_level = physical_device->rad_info.gfx_level;
1401    VkImageTiling tiling = info->tiling;
1402    const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *mod_info =
1403       vk_find_struct_const(info->pNext, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
1404    VkResult result = VK_ERROR_FORMAT_NOT_SUPPORTED;
1405 
1406    radv_physical_device_get_format_properties(physical_device, format, &format_props);
1407    if (tiling == VK_IMAGE_TILING_LINEAR) {
1408       format_feature_flags = format_props.linearTilingFeatures;
1409    } else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
1410       format_feature_flags = format_props.optimalTilingFeatures;
1411    } else if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1412       format_feature_flags =
1413          radv_get_modifier_flags(physical_device, format, mod_info->drmFormatModifier, &format_props);
1414    } else {
1415       unreachable("bad VkImageTiling");
1416    }
1417 
1418    if (format_feature_flags == 0)
1419       goto unsupported;
1420 
1421    if (info->type == VK_IMAGE_TYPE_1D && radv_is_format_emulated(physical_device, format))
1422       goto unsupported;
1423    if (info->type != VK_IMAGE_TYPE_2D && vk_format_is_depth_or_stencil(format))
1424       goto unsupported;
1425 
1426    switch (info->type) {
1427    default:
1428       unreachable("bad vkimage type\n");
1429    case VK_IMAGE_TYPE_1D:
1430       maxExtent.width = 16384;
1431       maxExtent.height = 1;
1432       maxExtent.depth = 1;
1433       maxMipLevels = 15; /* log2(maxWidth) + 1 */
1434       maxArraySize = gfx_level >= GFX10 ? 8192 : 2048;
1435       break;
1436    case VK_IMAGE_TYPE_2D:
1437       maxExtent.width = 16384;
1438       maxExtent.height = 16384;
1439       maxExtent.depth = 1;
1440       maxMipLevels = 15; /* log2(maxWidth) + 1 */
1441       maxArraySize = gfx_level >= GFX10 ? 8192 : 2048;
1442       break;
1443    case VK_IMAGE_TYPE_3D:
1444       if (gfx_level >= GFX10) {
1445          maxExtent.width = 8192;
1446          maxExtent.height = 8192;
1447          maxExtent.depth = 8192;
1448       } else {
1449          maxExtent.width = 2048;
1450          maxExtent.height = 2048;
1451          maxExtent.depth = 2048;
1452       }
1453       maxMipLevels = util_logbase2(maxExtent.width) + 1;
1454       maxArraySize = 1;
1455       break;
1456    }
1457 
1458    if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
1459       /* Might be able to support but the entire format support is
1460        * messy, so taking the lazy way out. */
1461       maxArraySize = 1;
1462    }
1463 
1464    if (tiling == VK_IMAGE_TILING_OPTIMAL && info->type == VK_IMAGE_TYPE_2D &&
1465        (format_feature_flags &
1466         (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1467        !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
1468        !(info->usage & VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR)) {
1469       sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
1470    }
1471 
1472    if (tiling == VK_IMAGE_TILING_LINEAR && (format == VK_FORMAT_R32G32B32_SFLOAT ||
1473                                             format == VK_FORMAT_R32G32B32_SINT || format == VK_FORMAT_R32G32B32_UINT)) {
1474       /* R32G32B32 is a weird format and the driver currently only
1475        * supports the barely minimum.
1476        * TODO: Implement more if we really need to.
1477        */
1478       if (info->type == VK_IMAGE_TYPE_3D)
1479          goto unsupported;
1480       maxArraySize = 1;
1481       maxMipLevels = 1;
1482    }
1483 
1484    /* We can't create 3d compressed 128bpp images that can be rendered to on GFX9 */
1485    if (physical_device->rad_info.gfx_level >= GFX9 && info->type == VK_IMAGE_TYPE_3D &&
1486        vk_format_get_blocksizebits(format) == 128 && vk_format_is_compressed(format) &&
1487        (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
1488        ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) || (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))) {
1489       goto unsupported;
1490    }
1491 
1492    /* For some reasons, we can't create 1d block-compressed images that can be stored to with a
1493     * different format on GFX6.
1494     */
1495    if (physical_device->rad_info.gfx_level == GFX6 && info->type == VK_IMAGE_TYPE_1D &&
1496        vk_format_is_block_compressed(format) && (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
1497        ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) || (info->usage & VK_IMAGE_USAGE_STORAGE_BIT))) {
1498       goto unsupported;
1499    }
1500 
1501    /* From the Vulkan 1.3.206 spec:
1502     *
1503     * "VK_IMAGE_CREATE_EXTENDED_USAGE_BIT specifies that the image can be created with usage flags
1504     * that are not supported for the format the image is created with but are supported for at least
1505     * one format a VkImageView created from the image can have."
1506     */
1507    VkImageUsageFlags image_usage = info->usage;
1508    if (info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT)
1509       image_usage = 0;
1510 
1511    if (image_usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
1512       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT)) {
1513          goto unsupported;
1514       }
1515    }
1516 
1517    if (image_usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1518       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT)) {
1519          goto unsupported;
1520       }
1521    }
1522 
1523    if (image_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
1524       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT)) {
1525          goto unsupported;
1526       }
1527    }
1528 
1529    if (image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1530       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1531          goto unsupported;
1532       }
1533    }
1534 
1535    if (image_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
1536       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_TRANSFER_SRC_BIT)) {
1537          goto unsupported;
1538       }
1539    }
1540 
1541    if (image_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
1542       if (!(format_feature_flags & VK_FORMAT_FEATURE_2_TRANSFER_DST_BIT)) {
1543          goto unsupported;
1544       }
1545    }
1546 
1547    if (image_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
1548       if (!(format_feature_flags &
1549             (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT))) {
1550          goto unsupported;
1551       }
1552    }
1553 
1554    /* Sparse resources with multi-planar formats are unsupported. */
1555    if (info->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
1556       if (vk_format_get_plane_count(format) > 1)
1557          goto unsupported;
1558    }
1559 
1560    if (info->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1561       /* Sparse textures are only supported on GFX8+. */
1562       if (physical_device->rad_info.gfx_level < GFX8)
1563          goto unsupported;
1564 
1565       if (vk_format_get_plane_count(format) > 1 || info->type == VK_IMAGE_TYPE_1D ||
1566           info->tiling != VK_IMAGE_TILING_OPTIMAL || vk_format_is_depth_or_stencil(format))
1567          goto unsupported;
1568    }
1569 
1570    if ((info->flags & (VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT)) &&
1571        radv_is_format_emulated(physical_device, format)) {
1572       goto unsupported;
1573    }
1574 
1575    *pImageFormatProperties = (VkImageFormatProperties){
1576       .maxExtent = maxExtent,
1577       .maxMipLevels = maxMipLevels,
1578       .maxArrayLayers = maxArraySize,
1579       .sampleCounts = sampleCounts,
1580 
1581       /* FINISHME: Accurately calculate
1582        * VkImageFormatProperties::maxResourceSize.
1583        */
1584       .maxResourceSize = UINT32_MAX,
1585    };
1586 
1587    if (mod_info) {
1588       result = radv_check_modifier_support(physical_device, info, pImageFormatProperties, format,
1589                                            mod_info->drmFormatModifier);
1590       if (result != VK_SUCCESS)
1591          goto unsupported;
1592    }
1593 
1594    return VK_SUCCESS;
1595 unsupported:
1596    *pImageFormatProperties = (VkImageFormatProperties){
1597       .maxExtent = {0, 0, 0},
1598       .maxMipLevels = 0,
1599       .maxArrayLayers = 0,
1600       .sampleCounts = 0,
1601       .maxResourceSize = 0,
1602    };
1603 
1604    return result;
1605 }
1606 
1607 static void
get_external_image_format_properties(struct radv_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2 * pImageFormatInfo,VkExternalMemoryHandleTypeFlagBits handleType,VkExternalMemoryProperties * external_properties,VkImageFormatProperties * format_properties)1608 get_external_image_format_properties(struct radv_physical_device *physical_device,
1609                                      const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
1610                                      VkExternalMemoryHandleTypeFlagBits handleType,
1611                                      VkExternalMemoryProperties *external_properties,
1612                                      VkImageFormatProperties *format_properties)
1613 {
1614    VkExternalMemoryFeatureFlagBits flags = 0;
1615    VkExternalMemoryHandleTypeFlags export_flags = 0;
1616    VkExternalMemoryHandleTypeFlags compat_flags = 0;
1617 
1618    if (radv_is_format_emulated(physical_device, pImageFormatInfo->format))
1619       return;
1620 
1621    if (pImageFormatInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)
1622       return;
1623 
1624    switch (handleType) {
1625    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1626       if (pImageFormatInfo->tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
1627          break;
1628       FALLTHROUGH;
1629    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1630       if (pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
1631          break;
1632       flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1633       if (handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT &&
1634           pImageFormatInfo->tiling != VK_IMAGE_TILING_LINEAR)
1635          flags |= VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT;
1636 
1637       compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
1638       if (pImageFormatInfo->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
1639          compat_flags |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1640          export_flags |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1641       }
1642       break;
1643    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID:
1644       if (!physical_device->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer)
1645          break;
1646 
1647       if (pImageFormatInfo->type != VK_IMAGE_TYPE_2D)
1648          break;
1649 
1650       format_properties->maxMipLevels = MIN2(1, format_properties->maxMipLevels);
1651       format_properties->maxArrayLayers = MIN2(1, format_properties->maxArrayLayers);
1652       format_properties->sampleCounts &= VK_SAMPLE_COUNT_1_BIT;
1653 
1654       flags = VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1655 
1656       /* advertise EXPORTABLE only when radv_create_ahb_memory supports the format */
1657       if (radv_android_gralloc_supports_format(pImageFormatInfo->format, pImageFormatInfo->usage))
1658          flags |= VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT;
1659 
1660       compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID;
1661       break;
1662    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1663       flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1664       compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1665       break;
1666    default:
1667       break;
1668    }
1669 
1670    *external_properties = (VkExternalMemoryProperties){
1671       .externalMemoryFeatures = flags,
1672       .exportFromImportedHandleTypes = export_flags,
1673       .compatibleHandleTypes = compat_flags,
1674    };
1675 }
1676 
1677 VKAPI_ATTR VkResult VKAPI_CALL
radv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * base_info,VkImageFormatProperties2 * base_props)1678 radv_GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,
1679                                              const VkPhysicalDeviceImageFormatInfo2 *base_info,
1680                                              VkImageFormatProperties2 *base_props)
1681 {
1682    RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1683    const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1684    VkExternalImageFormatProperties *external_props = NULL;
1685    struct VkAndroidHardwareBufferUsageANDROID *android_usage = NULL;
1686    VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
1687    VkTextureLODGatherFormatPropertiesAMD *texture_lod_props = NULL;
1688    VkImageCompressionPropertiesEXT *image_compression_props = NULL;
1689    VkResult result;
1690    VkFormat format = radv_select_android_external_format(base_info->pNext, base_info->format);
1691 
1692    result = radv_get_image_format_properties(physical_device, base_info, format, &base_props->imageFormatProperties);
1693    if (result != VK_SUCCESS)
1694       return result;
1695 
1696    /* Extract input structs */
1697    vk_foreach_struct_const (s, base_info->pNext) {
1698       switch (s->sType) {
1699       case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1700          external_info = (const void *)s;
1701          break;
1702       default:
1703          break;
1704       }
1705    }
1706 
1707    /* Extract output structs */
1708    vk_foreach_struct (s, base_props->pNext) {
1709       switch (s->sType) {
1710       case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1711          external_props = (void *)s;
1712          break;
1713       case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
1714          ycbcr_props = (void *)s;
1715          break;
1716       case VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_USAGE_ANDROID:
1717          android_usage = (void *)s;
1718          break;
1719       case VK_STRUCTURE_TYPE_TEXTURE_LOD_GATHER_FORMAT_PROPERTIES_AMD:
1720          texture_lod_props = (void *)s;
1721          break;
1722       case VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT:
1723          image_compression_props = (void *)s;
1724          break;
1725       default:
1726          break;
1727       }
1728    }
1729 
1730    bool ahb_supported = physical_device->vk.supported_extensions.ANDROID_external_memory_android_hardware_buffer;
1731    if (android_usage && ahb_supported) {
1732       android_usage->androidHardwareBufferUsage = vk_image_usage_to_ahb_usage(base_info->flags, base_info->usage);
1733    }
1734 
1735    /* From the Vulkan 1.0.97 spec:
1736     *
1737     *    If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1738     *    behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1739     *    present and VkExternalImageFormatProperties will be ignored.
1740     */
1741    if (external_info && external_info->handleType != 0) {
1742       VkExternalImageFormatProperties fallback_external_props;
1743 
1744       if (!external_props) {
1745          memset(&fallback_external_props, 0, sizeof(fallback_external_props));
1746          external_props = &fallback_external_props;
1747       }
1748 
1749       get_external_image_format_properties(physical_device, base_info, external_info->handleType,
1750                                            &external_props->externalMemoryProperties,
1751                                            &base_props->imageFormatProperties);
1752       if (!external_props->externalMemoryProperties.externalMemoryFeatures) {
1753          /* From the Vulkan 1.0.97 spec:
1754           *
1755           *    If handleType is not compatible with the [parameters] specified
1756           *    in VkPhysicalDeviceImageFormatInfo2, then
1757           *    vkGetPhysicalDeviceImageFormatProperties2 returns
1758           *    VK_ERROR_FORMAT_NOT_SUPPORTED.
1759           */
1760          result = vk_errorf(physical_device, VK_ERROR_FORMAT_NOT_SUPPORTED,
1761                             "unsupported VkExternalMemoryTypeFlagBitsKHR 0x%x", external_info->handleType);
1762          goto fail;
1763       }
1764    }
1765 
1766    if (ycbcr_props) {
1767       ycbcr_props->combinedImageSamplerDescriptorCount = 1;
1768    }
1769 
1770    if (texture_lod_props) {
1771       if (physical_device->rad_info.gfx_level >= GFX9) {
1772          texture_lod_props->supportsTextureGatherLODBiasAMD = true;
1773       } else {
1774          texture_lod_props->supportsTextureGatherLODBiasAMD = !vk_format_is_int(format);
1775       }
1776    }
1777 
1778    if (image_compression_props) {
1779       image_compression_props->imageCompressionFixedRateFlags = VK_IMAGE_COMPRESSION_FIXED_RATE_NONE_EXT;
1780 
1781       if (vk_format_is_depth_or_stencil(format)) {
1782          image_compression_props->imageCompressionFlags = (physical_device->instance->debug_flags & RADV_DEBUG_NO_HIZ)
1783                                                              ? VK_IMAGE_COMPRESSION_DISABLED_EXT
1784                                                              : VK_IMAGE_COMPRESSION_DEFAULT_EXT;
1785       } else {
1786          image_compression_props->imageCompressionFlags =
1787             ((physical_device->instance->debug_flags & RADV_DEBUG_NO_DCC) || physical_device->rad_info.gfx_level < GFX8)
1788                ? VK_IMAGE_COMPRESSION_DISABLED_EXT
1789                : VK_IMAGE_COMPRESSION_DEFAULT_EXT;
1790       }
1791    }
1792 
1793    return VK_SUCCESS;
1794 
1795 fail:
1796    if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1797       /* From the Vulkan 1.0.97 spec:
1798        *
1799        *    If the combination of parameters to
1800        *    vkGetPhysicalDeviceImageFormatProperties2 is not supported by
1801        *    the implementation for use in vkCreateImage, then all members of
1802        *    imageFormatProperties will be filled with zero.
1803        */
1804       base_props->imageFormatProperties = (VkImageFormatProperties){0};
1805    }
1806 
1807    return result;
1808 }
1809 
1810 static void
fill_sparse_image_format_properties(struct radv_physical_device * pdev,VkImageType type,VkFormat format,VkSparseImageFormatProperties * prop)1811 fill_sparse_image_format_properties(struct radv_physical_device *pdev, VkImageType type, VkFormat format,
1812                                     VkSparseImageFormatProperties *prop)
1813 {
1814    prop->aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1815    prop->flags = 0;
1816 
1817    /* On GFX8 we first subdivide by level and then layer, leading to a single
1818     * miptail. On GFX9+ we first subdivide by layer and then level which results
1819     * in a miptail per layer. */
1820    if (pdev->rad_info.gfx_level < GFX9)
1821       prop->flags |= VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT;
1822 
1823    unsigned w, h;
1824    unsigned d = 1;
1825    if (type == VK_IMAGE_TYPE_3D) {
1826       if (pdev->rad_info.gfx_level >= GFX9) {
1827          unsigned l2_size = 16 - util_logbase2(vk_format_get_blocksize(format));
1828          w = (1u << ((l2_size + 2) / 3)) * vk_format_get_blockwidth(format);
1829          h = (1u << ((l2_size + 1) / 3)) * vk_format_get_blockheight(format);
1830          d = (1u << ((l2_size + 0) / 3));
1831       } else {
1832          /* GFX7/GFX8 thick tiling modes */
1833          unsigned bs = vk_format_get_blocksize(format);
1834          unsigned l2_size = 16 - util_logbase2(bs) - (bs <= 4 ? 2 : 0);
1835          w = (1u << ((l2_size + 1) / 2)) * vk_format_get_blockwidth(format);
1836          h = (1u << (l2_size / 2)) * vk_format_get_blockheight(format);
1837          d = bs <= 4 ? 4 : 1;
1838       }
1839    } else {
1840       /* This assumes the sparse image tile size is always 64 KiB (1 << 16) */
1841       unsigned l2_size = 16 - util_logbase2(vk_format_get_blocksize(format));
1842       w = (1u << ((l2_size + 1) / 2)) * vk_format_get_blockwidth(format);
1843       h = (1u << (l2_size / 2)) * vk_format_get_blockheight(format);
1844    }
1845    prop->imageGranularity = (VkExtent3D){w, h, d};
1846 }
1847 
1848 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)1849 radv_GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,
1850                                                    const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
1851                                                    uint32_t *pPropertyCount,
1852                                                    VkSparseImageFormatProperties2 *pProperties)
1853 {
1854    RADV_FROM_HANDLE(radv_physical_device, pdev, physicalDevice);
1855    VkResult result;
1856 
1857    if (pFormatInfo->samples > VK_SAMPLE_COUNT_1_BIT) {
1858       *pPropertyCount = 0;
1859       return;
1860    }
1861 
1862    const VkPhysicalDeviceImageFormatInfo2 fmt_info = {
1863       .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1864       .format = pFormatInfo->format,
1865       .type = pFormatInfo->type,
1866       .tiling = pFormatInfo->tiling,
1867       .usage = pFormatInfo->usage,
1868       .flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT};
1869 
1870    VkImageFormatProperties fmt_props;
1871    result = radv_get_image_format_properties(pdev, &fmt_info, pFormatInfo->format, &fmt_props);
1872    if (result != VK_SUCCESS) {
1873       *pPropertyCount = 0;
1874       return;
1875    }
1876 
1877    VK_OUTARRAY_MAKE_TYPED(VkSparseImageFormatProperties2, out, pProperties, pPropertyCount);
1878 
1879    vk_outarray_append_typed(VkSparseImageFormatProperties2, &out, prop)
1880    {
1881       fill_sparse_image_format_properties(pdev, pFormatInfo->type, pFormatInfo->format, &prop->properties);
1882    };
1883 }
1884 
1885 VKAPI_ATTR void VKAPI_CALL
radv_GetImageSparseMemoryRequirements2(VkDevice _device,const VkImageSparseMemoryRequirementsInfo2 * pInfo,uint32_t * pSparseMemoryRequirementCount,VkSparseImageMemoryRequirements2 * pSparseMemoryRequirements)1886 radv_GetImageSparseMemoryRequirements2(VkDevice _device, const VkImageSparseMemoryRequirementsInfo2 *pInfo,
1887                                        uint32_t *pSparseMemoryRequirementCount,
1888                                        VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1889 {
1890    RADV_FROM_HANDLE(radv_device, device, _device);
1891    RADV_FROM_HANDLE(radv_image, image, pInfo->image);
1892 
1893    if (!(image->vk.create_flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)) {
1894       *pSparseMemoryRequirementCount = 0;
1895       return;
1896    }
1897 
1898    VK_OUTARRAY_MAKE_TYPED(VkSparseImageMemoryRequirements2, out, pSparseMemoryRequirements,
1899                           pSparseMemoryRequirementCount);
1900 
1901    vk_outarray_append_typed(VkSparseImageMemoryRequirements2, &out, req)
1902    {
1903       fill_sparse_image_format_properties(device->physical_device, image->vk.image_type, image->vk.format,
1904                                           &req->memoryRequirements.formatProperties);
1905       req->memoryRequirements.imageMipTailFirstLod = image->planes[0].surface.first_mip_tail_level;
1906 
1907       if (req->memoryRequirements.imageMipTailFirstLod < image->vk.mip_levels) {
1908          if (device->physical_device->rad_info.gfx_level >= GFX9) {
1909             /* The tail is always a single tile per layer. */
1910             req->memoryRequirements.imageMipTailSize = 65536;
1911             req->memoryRequirements.imageMipTailOffset =
1912                image->planes[0].surface.u.gfx9.prt_level_offset[req->memoryRequirements.imageMipTailFirstLod] & ~65535;
1913             req->memoryRequirements.imageMipTailStride = image->planes[0].surface.u.gfx9.surf_slice_size;
1914          } else {
1915             req->memoryRequirements.imageMipTailOffset =
1916                (uint64_t)image->planes[0]
1917                   .surface.u.legacy.level[req->memoryRequirements.imageMipTailFirstLod]
1918                   .offset_256B *
1919                256;
1920             req->memoryRequirements.imageMipTailSize = image->size - req->memoryRequirements.imageMipTailOffset;
1921             req->memoryRequirements.imageMipTailStride = 0;
1922          }
1923       } else {
1924          req->memoryRequirements.imageMipTailSize = 0;
1925          req->memoryRequirements.imageMipTailOffset = 0;
1926          req->memoryRequirements.imageMipTailStride = 0;
1927       }
1928    };
1929 }
1930 
1931 VKAPI_ATTR void VKAPI_CALL
radv_GetDeviceImageSparseMemoryRequirements(VkDevice device,const VkDeviceImageMemoryRequirements * pInfo,uint32_t * pSparseMemoryRequirementCount,VkSparseImageMemoryRequirements2 * pSparseMemoryRequirements)1932 radv_GetDeviceImageSparseMemoryRequirements(VkDevice device, const VkDeviceImageMemoryRequirements *pInfo,
1933                                             uint32_t *pSparseMemoryRequirementCount,
1934                                             VkSparseImageMemoryRequirements2 *pSparseMemoryRequirements)
1935 {
1936    UNUSED VkResult result;
1937    VkImage image;
1938 
1939    /* Determining the image size/alignment require to create a surface, which is complicated without
1940     * creating an image.
1941     * TODO: Avoid creating an image.
1942     */
1943    result =
1944       radv_image_create(device, &(struct radv_image_create_info){.vk_info = pInfo->pCreateInfo}, NULL, &image, true);
1945    assert(result == VK_SUCCESS);
1946 
1947    VkImageSparseMemoryRequirementsInfo2 info2 = {
1948       .sType = VK_STRUCTURE_TYPE_IMAGE_SPARSE_MEMORY_REQUIREMENTS_INFO_2,
1949       .image = image,
1950    };
1951 
1952    radv_GetImageSparseMemoryRequirements2(device, &info2, pSparseMemoryRequirementCount, pSparseMemoryRequirements);
1953 
1954    radv_DestroyImage(device, image, NULL);
1955 }
1956 
1957 VKAPI_ATTR void VKAPI_CALL
radv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)1958 radv_GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,
1959                                                const VkPhysicalDeviceExternalBufferInfo *pExternalBufferInfo,
1960                                                VkExternalBufferProperties *pExternalBufferProperties)
1961 {
1962    VkExternalMemoryFeatureFlagBits flags = 0;
1963    VkExternalMemoryHandleTypeFlags export_flags = 0;
1964    VkExternalMemoryHandleTypeFlags compat_flags = 0;
1965    switch (pExternalBufferInfo->handleType) {
1966    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1967    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1968       flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT | VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1969       compat_flags = export_flags =
1970          VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT | VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1971       break;
1972    case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1973       flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1974       compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1975       break;
1976    default:
1977       break;
1978    }
1979    pExternalBufferProperties->externalMemoryProperties = (VkExternalMemoryProperties){
1980       .externalMemoryFeatures = flags,
1981       .exportFromImportedHandleTypes = export_flags,
1982       .compatibleHandleTypes = compat_flags,
1983    };
1984 }
1985 
1986 /* DCC channel type categories within which formats can be reinterpreted
1987  * while keeping the same DCC encoding. The swizzle must also match. */
1988 enum dcc_channel_type {
1989    dcc_channel_float,
1990    dcc_channel_uint,
1991    dcc_channel_sint,
1992    dcc_channel_incompatible,
1993 };
1994 
1995 /* Return the type of DCC encoding. */
1996 static void
radv_get_dcc_channel_type(const struct util_format_description * desc,enum dcc_channel_type * type,unsigned * size)1997 radv_get_dcc_channel_type(const struct util_format_description *desc, enum dcc_channel_type *type, unsigned *size)
1998 {
1999    int i = util_format_get_first_non_void_channel(desc->format);
2000    if (i == -1) {
2001       *type = dcc_channel_incompatible;
2002       return;
2003    }
2004 
2005    switch (desc->channel[i].size) {
2006    case 32:
2007    case 16:
2008    case 10:
2009    case 8:
2010       *size = desc->channel[i].size;
2011       if (desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT)
2012          *type = dcc_channel_float;
2013       else if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED)
2014          *type = dcc_channel_uint;
2015       else
2016          *type = dcc_channel_sint;
2017       break;
2018    default:
2019       *type = dcc_channel_incompatible;
2020       break;
2021    }
2022 }
2023 
2024 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
2025 bool
radv_dcc_formats_compatible(enum amd_gfx_level gfx_level,VkFormat format1,VkFormat format2,bool * sign_reinterpret)2026 radv_dcc_formats_compatible(enum amd_gfx_level gfx_level, VkFormat format1, VkFormat format2, bool *sign_reinterpret)
2027 {
2028    const struct util_format_description *desc1, *desc2;
2029    enum dcc_channel_type type1, type2;
2030    unsigned size1, size2;
2031    int i;
2032 
2033    /* All formats are compatible on GFX11. */
2034    if (gfx_level >= GFX11)
2035       return true;
2036 
2037    if (format1 == format2)
2038       return true;
2039 
2040    desc1 = vk_format_description(format1);
2041    desc2 = vk_format_description(format2);
2042 
2043    if (desc1->nr_channels != desc2->nr_channels)
2044       return false;
2045 
2046    /* Swizzles must be the same. */
2047    for (i = 0; i < desc1->nr_channels; i++)
2048       if (desc1->swizzle[i] <= PIPE_SWIZZLE_W && desc2->swizzle[i] <= PIPE_SWIZZLE_W &&
2049           desc1->swizzle[i] != desc2->swizzle[i])
2050          return false;
2051 
2052    radv_get_dcc_channel_type(desc1, &type1, &size1);
2053    radv_get_dcc_channel_type(desc2, &type2, &size2);
2054 
2055    if (type1 == dcc_channel_incompatible || type2 == dcc_channel_incompatible ||
2056        (type1 == dcc_channel_float) != (type2 == dcc_channel_float) || size1 != size2)
2057       return false;
2058 
2059    if (type1 != type2)
2060       *sign_reinterpret = true;
2061 
2062    return true;
2063 }
2064