~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/amd/vulkan/radv_formats.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

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