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