radv_formats.c 63.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright © 2016 Red Hat.
 * Copyright © 2016 Bas Nieuwenhuizen
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "radv_private.h"
26
#include "radv_debug.h"
27
28
29
30

#include "vk_format.h"
#include "sid.h"

31
32
#include "vk_util.h"

33
#include "drm-uapi/drm_fourcc.h"
34
#include "util/half_float.h"
35
#include "util/format_srgb.h"
36
#include "util/format_r11g11b10f.h"
37
#include "util/format_rgb9e5.h"
38
#include "vulkan/util/vk_format.h"
39

40
uint32_t radv_translate_buffer_dataformat(const struct util_format_description *desc,
41
42
43
44
45
					  int first_non_void)
{
	unsigned type;
	int i;

46
	assert(util_format_get_num_planes(desc->format) == 1);
47

48
	if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
49
50
51
52
53
54
		return V_008F0C_BUF_DATA_FORMAT_10_11_11;

	if (first_non_void < 0)
		return V_008F0C_BUF_DATA_FORMAT_INVALID;
	type = desc->channel[first_non_void].type;

55
	if (type == UTIL_FORMAT_TYPE_FIXED)
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
		return V_008F0C_BUF_DATA_FORMAT_INVALID;
	if (desc->nr_channels == 4 &&
	    desc->channel[0].size == 10 &&
	    desc->channel[1].size == 10 &&
	    desc->channel[2].size == 10 &&
	    desc->channel[3].size == 2)
		return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;

	/* See whether the components are of the same size. */
	for (i = 0; i < desc->nr_channels; i++) {
		if (desc->channel[first_non_void].size != desc->channel[i].size)
			return V_008F0C_BUF_DATA_FORMAT_INVALID;
	}

	switch (desc->channel[first_non_void].size) {
	case 8:
		switch (desc->nr_channels) {
		case 1:
			return V_008F0C_BUF_DATA_FORMAT_8;
		case 2:
			return V_008F0C_BUF_DATA_FORMAT_8_8;
		case 4:
			return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
		}
		break;
	case 16:
		switch (desc->nr_channels) {
		case 1:
			return V_008F0C_BUF_DATA_FORMAT_16;
		case 2:
			return V_008F0C_BUF_DATA_FORMAT_16_16;
		case 4:
			return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
		}
		break;
	case 32:
		/* From the Southern Islands ISA documentation about MTBUF:
		 * 'Memory reads of data in memory that is 32 or 64 bits do not
		 * undergo any format conversion.'
		 */
96
		if (type != UTIL_FORMAT_TYPE_FLOAT &&
97
98
99
100
101
102
103
104
105
106
107
108
109
110
		    !desc->channel[first_non_void].pure_integer)
			return V_008F0C_BUF_DATA_FORMAT_INVALID;

		switch (desc->nr_channels) {
		case 1:
			return V_008F0C_BUF_DATA_FORMAT_32;
		case 2:
			return V_008F0C_BUF_DATA_FORMAT_32_32;
		case 3:
			return V_008F0C_BUF_DATA_FORMAT_32_32_32;
		case 4:
			return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
		}
		break;
111
112
113
	case 64:
		if (desc->nr_channels == 1)
			return V_008F0C_BUF_DATA_FORMAT_32_32;
114
115
116
117
118
	}

	return V_008F0C_BUF_DATA_FORMAT_INVALID;
}

119
uint32_t radv_translate_buffer_numformat(const struct util_format_description *desc,
120
121
					 int first_non_void)
{
122
	assert(util_format_get_num_planes(desc->format) == 1);
123

124
	if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
125
126
127
128
129
130
		return V_008F0C_BUF_NUM_FORMAT_FLOAT;

	if (first_non_void < 0)
		return ~0;

	switch (desc->channel[first_non_void].type) {
131
	case UTIL_FORMAT_TYPE_SIGNED:
132
133
134
135
136
137
138
		if (desc->channel[first_non_void].normalized)
			return V_008F0C_BUF_NUM_FORMAT_SNORM;
		else if (desc->channel[first_non_void].pure_integer)
			return V_008F0C_BUF_NUM_FORMAT_SINT;
		else
			return V_008F0C_BUF_NUM_FORMAT_SSCALED;
		break;
139
	case UTIL_FORMAT_TYPE_UNSIGNED:
140
141
142
143
144
145
146
		if (desc->channel[first_non_void].normalized)
			return V_008F0C_BUF_NUM_FORMAT_UNORM;
		else if (desc->channel[first_non_void].pure_integer)
			return V_008F0C_BUF_NUM_FORMAT_UINT;
		else
			return V_008F0C_BUF_NUM_FORMAT_USCALED;
		break;
147
	case UTIL_FORMAT_TYPE_FLOAT:
148
149
150
151
152
153
	default:
		return V_008F0C_BUF_NUM_FORMAT_FLOAT;
	}
}

uint32_t radv_translate_tex_dataformat(VkFormat format,
154
				       const struct util_format_description *desc,
155
156
157
158
159
				       int first_non_void)
{
	bool uniform = true;
	int i;

160
161
	assert(vk_format_get_plane_count(format) == 1);

162
163
164
165
166
	if (!desc)
		return ~0;
	/* Colorspace (return non-RGB formats directly). */
	switch (desc->colorspace) {
		/* Depth stencil formats */
167
	case UTIL_FORMAT_COLORSPACE_ZS:
168
169
170
171
		switch (format) {
		case VK_FORMAT_D16_UNORM:
			return V_008F14_IMG_DATA_FORMAT_16;
		case VK_FORMAT_D24_UNORM_S8_UINT:
172
		case VK_FORMAT_X8_D24_UNORM_PACK32:
173
174
175
176
177
178
179
180
181
182
183
			return V_008F14_IMG_DATA_FORMAT_8_24;
		case VK_FORMAT_S8_UINT:
			return V_008F14_IMG_DATA_FORMAT_8;
		case VK_FORMAT_D32_SFLOAT:
			return V_008F14_IMG_DATA_FORMAT_32;
		case VK_FORMAT_D32_SFLOAT_S8_UINT:
			return V_008F14_IMG_DATA_FORMAT_X24_8_32;
		default:
			goto out_unknown;
		}

184
	case UTIL_FORMAT_COLORSPACE_YUV:
185
186
		goto out_unknown; /* TODO */

187
	case UTIL_FORMAT_COLORSPACE_SRGB:
188
189
190
191
192
193
194
195
		if (desc->nr_channels != 4 && desc->nr_channels != 1)
			goto out_unknown;
		break;

	default:
		break;
	}

196
	if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
197
198
199
200
201
202
203
204
205
206
207
		switch(format) {
		/* Don't ask me why this looks inverted. PAL does the same. */
		case VK_FORMAT_G8B8G8R8_422_UNORM:
			return V_008F14_IMG_DATA_FORMAT_BG_RG;
		case VK_FORMAT_B8G8R8G8_422_UNORM:
			return V_008F14_IMG_DATA_FORMAT_GB_GR;
		default:
			goto out_unknown;
		}
	}

208
	if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
209
210
211
212
213
214
215
216
217
218
219
220
		switch(format) {
		case VK_FORMAT_BC4_UNORM_BLOCK:
		case VK_FORMAT_BC4_SNORM_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_BC4;
		case VK_FORMAT_BC5_UNORM_BLOCK:
		case VK_FORMAT_BC5_SNORM_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_BC5;
		default:
			break;
		}
	}

221
	if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
		switch(format) {
		case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
		case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
		case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
		case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_BC1;
		case VK_FORMAT_BC2_UNORM_BLOCK:
		case VK_FORMAT_BC2_SRGB_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_BC2;
		case VK_FORMAT_BC3_UNORM_BLOCK:
		case VK_FORMAT_BC3_SRGB_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_BC3;
		default:
			break;
		}
	}

239
	if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
240
241
242
243
244
245
246
247
248
249
250
251
		switch(format) {
		case VK_FORMAT_BC6H_UFLOAT_BLOCK:
		case VK_FORMAT_BC6H_SFLOAT_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_BC6;
		case VK_FORMAT_BC7_UNORM_BLOCK:
		case VK_FORMAT_BC7_SRGB_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_BC7;
		default:
			break;
		}
	}

252
	if (desc->layout == UTIL_FORMAT_LAYOUT_ETC) {
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
		switch (format) {
		case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
		case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
		case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
		case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
		case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
		case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
		case VK_FORMAT_EAC_R11_UNORM_BLOCK:
		case VK_FORMAT_EAC_R11_SNORM_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_ETC2_R;
		case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
		case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
			return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
		default:
			break;
		}
	}

274
275
276
277
278
279
280
281
282
283
	if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
		return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
	} else if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
		return V_008F14_IMG_DATA_FORMAT_10_11_11;
	}

	/* R8G8Bx_SNORM - TODO CxV8U8 */

	/* hw cannot support mixed formats (except depth/stencil, since only
	 * depth is read).*/
284
	if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
		goto out_unknown;

	/* See whether the components are of the same size. */
	for (i = 1; i < desc->nr_channels; i++) {
		uniform = uniform && desc->channel[0].size == desc->channel[i].size;
	}

	/* Non-uniform formats. */
	if (!uniform) {
		switch(desc->nr_channels) {
		case 3:
			if (desc->channel[0].size == 5 &&
			    desc->channel[1].size == 6 &&
			    desc->channel[2].size == 5) {
				return V_008F14_IMG_DATA_FORMAT_5_6_5;
			}
			goto out_unknown;
		case 4:
			if (desc->channel[0].size == 5 &&
			    desc->channel[1].size == 5 &&
			    desc->channel[2].size == 5 &&
			    desc->channel[3].size == 1) {
				return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
			}
			if (desc->channel[0].size == 1 &&
			    desc->channel[1].size == 5 &&
			    desc->channel[2].size == 5 &&
			    desc->channel[3].size == 5) {
				return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
			}
			if (desc->channel[0].size == 10 &&
			    desc->channel[1].size == 10 &&
			    desc->channel[2].size == 10 &&
			    desc->channel[3].size == 2) {
				/* Closed VK driver does this also no 2/10/10/10 snorm */
320
				if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED &&
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
				    desc->channel[0].normalized)
					goto out_unknown;
				return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
			}
			goto out_unknown;
		}
		goto out_unknown;
	}

	if (first_non_void < 0 || first_non_void > 3)
		goto out_unknown;

	/* uniform formats */
	switch (desc->channel[first_non_void].size) {
	case 4:
		switch (desc->nr_channels) {
#if 0 /* Not supported for render targets */
		case 2:
			return V_008F14_IMG_DATA_FORMAT_4_4;
#endif
		case 4:
			return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
		}
		break;
	case 8:
		switch (desc->nr_channels) {
		case 1:
			return V_008F14_IMG_DATA_FORMAT_8;
		case 2:
			return V_008F14_IMG_DATA_FORMAT_8_8;
		case 4:
			return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
		}
		break;
	case 16:
		switch (desc->nr_channels) {
		case 1:
			return V_008F14_IMG_DATA_FORMAT_16;
		case 2:
			return V_008F14_IMG_DATA_FORMAT_16_16;
		case 4:
			return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
		}
		break;
	case 32:
		switch (desc->nr_channels) {
		case 1:
			return V_008F14_IMG_DATA_FORMAT_32;
		case 2:
			return V_008F14_IMG_DATA_FORMAT_32_32;
		case 3:
			return V_008F14_IMG_DATA_FORMAT_32_32_32;
		case 4:
			return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
		}
376
377
378
379
380
		break;
	case 64:
		if (desc->nr_channels == 1)
			return V_008F14_IMG_DATA_FORMAT_32_32;
		break;
381
382
383
384
385
386
387
388
	}

out_unknown:
	/* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
	return ~0;
}

uint32_t radv_translate_tex_numformat(VkFormat format,
389
				      const struct util_format_description *desc,
390
391
				      int first_non_void)
{
392
393
	assert(vk_format_get_plane_count(format) == 1);

394
395
396
397
398
399
400
401
402
403
404
405
	switch (format) {
	case VK_FORMAT_D24_UNORM_S8_UINT:
		return V_008F14_IMG_NUM_FORMAT_UNORM;
	default:
		if (first_non_void < 0) {
			if (vk_format_is_compressed(format)) {
				switch (format) {
				case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
				case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
				case VK_FORMAT_BC2_SRGB_BLOCK:
				case VK_FORMAT_BC3_SRGB_BLOCK:
				case VK_FORMAT_BC7_SRGB_BLOCK:
406
407
408
				case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
				case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
				case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
409
410
411
412
					return V_008F14_IMG_NUM_FORMAT_SRGB;
				case VK_FORMAT_BC4_SNORM_BLOCK:
				case VK_FORMAT_BC5_SNORM_BLOCK:
			        case VK_FORMAT_BC6H_SFLOAT_BLOCK:
413
414
				case VK_FORMAT_EAC_R11_SNORM_BLOCK:
				case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
415
416
417
418
					return V_008F14_IMG_NUM_FORMAT_SNORM;
				default:
					return V_008F14_IMG_NUM_FORMAT_UNORM;
				}
419
			} else if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
420
421
422
423
				return V_008F14_IMG_NUM_FORMAT_UNORM;
			} else {
				return V_008F14_IMG_NUM_FORMAT_FLOAT;
			}
424
		} else if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
425
426
427
			return V_008F14_IMG_NUM_FORMAT_SRGB;
		} else {
			switch (desc->channel[first_non_void].type) {
428
			case UTIL_FORMAT_TYPE_FLOAT:
429
				return V_008F14_IMG_NUM_FORMAT_FLOAT;
430
			case UTIL_FORMAT_TYPE_SIGNED:
431
432
433
434
435
436
				if (desc->channel[first_non_void].normalized)
					return V_008F14_IMG_NUM_FORMAT_SNORM;
				else if (desc->channel[first_non_void].pure_integer)
					return V_008F14_IMG_NUM_FORMAT_SINT;
				else
					return V_008F14_IMG_NUM_FORMAT_SSCALED;
437
			case UTIL_FORMAT_TYPE_UNSIGNED:
438
439
440
441
442
443
444
445
446
447
448
449
450
451
				if (desc->channel[first_non_void].normalized)
					return V_008F14_IMG_NUM_FORMAT_UNORM;
				else if (desc->channel[first_non_void].pure_integer)
					return V_008F14_IMG_NUM_FORMAT_UINT;
				else
					return V_008F14_IMG_NUM_FORMAT_USCALED;
			default:
				return V_008F14_IMG_NUM_FORMAT_UNORM;
			}
		}
	}
}

uint32_t radv_translate_color_numformat(VkFormat format,
452
					const struct util_format_description *desc,
453
454
455
					int first_non_void)
{
	unsigned ntype;
456
457
458

	assert(vk_format_get_plane_count(format) == 1);

459
	if (first_non_void == -1 || desc->channel[first_non_void].type == UTIL_FORMAT_TYPE_FLOAT)
460
461
462
		ntype = V_028C70_NUMBER_FLOAT;
	else {
		ntype = V_028C70_NUMBER_UNORM;
463
		if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
464
			ntype = V_028C70_NUMBER_SRGB;
465
		else if (desc->channel[first_non_void].type == UTIL_FORMAT_TYPE_SIGNED) {
466
467
468
469
470
471
			if (desc->channel[first_non_void].pure_integer) {
				ntype = V_028C70_NUMBER_SINT;
			} else if (desc->channel[first_non_void].normalized) {
				ntype = V_028C70_NUMBER_SNORM;
			} else
				ntype = ~0u;
472
		} else if (desc->channel[first_non_void].type == UTIL_FORMAT_TYPE_UNSIGNED) {
473
474
475
476
477
478
479
480
481
482
483
484
485
			if (desc->channel[first_non_void].pure_integer) {
				ntype = V_028C70_NUMBER_UINT;
			} else if (desc->channel[first_non_void].normalized) {
				ntype = V_028C70_NUMBER_UNORM;
			} else
				ntype = ~0u;
		}
	}
	return ntype;
}

static bool radv_is_sampler_format_supported(VkFormat format, bool *linear_sampling)
{
486
	const struct util_format_description *desc = vk_format_description(format);
487
	uint32_t num_format;
488
	if (!desc || format == VK_FORMAT_UNDEFINED ||
489
490
	    format == VK_FORMAT_R64_UINT || format == VK_FORMAT_R64_SINT ||
	    format == VK_FORMAT_R64_SFLOAT)
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
		return false;
	num_format = radv_translate_tex_numformat(format, desc,
						  vk_format_get_first_non_void_channel(format));

	if (num_format == V_008F14_IMG_NUM_FORMAT_USCALED ||
	    num_format == V_008F14_IMG_NUM_FORMAT_SSCALED)
		return false;

	if (num_format == V_008F14_IMG_NUM_FORMAT_UNORM ||
	    num_format == V_008F14_IMG_NUM_FORMAT_SNORM ||
	    num_format == V_008F14_IMG_NUM_FORMAT_FLOAT ||
	    num_format == V_008F14_IMG_NUM_FORMAT_SRGB)
		*linear_sampling = true;
	else
		*linear_sampling = false;
	return radv_translate_tex_dataformat(format, vk_format_description(format),
					     vk_format_get_first_non_void_channel(format)) != ~0U;
}

510
511
512
513
514
515
516
517
518
bool
radv_is_atomic_format_supported(VkFormat format)
{
	return format == VK_FORMAT_R32_UINT ||
		format == VK_FORMAT_R32_SINT ||
		format == VK_FORMAT_R32_SFLOAT ||
		format == VK_FORMAT_R64_UINT ||
		format == VK_FORMAT_R64_SINT;
}
519
520
521
522

static bool radv_is_storage_image_format_supported(struct radv_physical_device *physical_device,
						   VkFormat format)
{
523
	const struct util_format_description *desc = vk_format_description(format);
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
	unsigned data_format, num_format;
	if (!desc || format == VK_FORMAT_UNDEFINED)
		return false;

	data_format = radv_translate_tex_dataformat(format, desc,
						    vk_format_get_first_non_void_channel(format));
	num_format = radv_translate_tex_numformat(format, desc,
						  vk_format_get_first_non_void_channel(format));

	if(data_format == ~0 || num_format == ~0)
		return false;

	/* Extracted from the GCN3 ISA document. */
	switch(num_format) {
	case V_008F14_IMG_NUM_FORMAT_UNORM:
	case V_008F14_IMG_NUM_FORMAT_SNORM:
	case V_008F14_IMG_NUM_FORMAT_UINT:
	case V_008F14_IMG_NUM_FORMAT_SINT:
	case V_008F14_IMG_NUM_FORMAT_FLOAT:
		break;
	default:
		return false;
	}

	switch(data_format) {
	case V_008F14_IMG_DATA_FORMAT_8:
	case V_008F14_IMG_DATA_FORMAT_16:
	case V_008F14_IMG_DATA_FORMAT_8_8:
	case V_008F14_IMG_DATA_FORMAT_32:
	case V_008F14_IMG_DATA_FORMAT_16_16:
	case V_008F14_IMG_DATA_FORMAT_10_11_11:
	case V_008F14_IMG_DATA_FORMAT_11_11_10:
	case V_008F14_IMG_DATA_FORMAT_10_10_10_2:
	case V_008F14_IMG_DATA_FORMAT_2_10_10_10:
	case V_008F14_IMG_DATA_FORMAT_8_8_8_8:
	case V_008F14_IMG_DATA_FORMAT_32_32:
	case V_008F14_IMG_DATA_FORMAT_16_16_16_16:
	case V_008F14_IMG_DATA_FORMAT_32_32_32_32:
	case V_008F14_IMG_DATA_FORMAT_5_6_5:
	case V_008F14_IMG_DATA_FORMAT_1_5_5_5:
	case V_008F14_IMG_DATA_FORMAT_5_5_5_1:
	case V_008F14_IMG_DATA_FORMAT_4_4_4_4:
		/* TODO: FMASK formats. */
		return true;
	default:
		return false;
	}
}

573
bool radv_is_buffer_format_supported(VkFormat format, bool *scaled)
574
{
575
	const struct util_format_description *desc = vk_format_description(format);
576
577
578
579
580
581
582
583
584
	unsigned data_format, num_format;
	if (!desc || format == VK_FORMAT_UNDEFINED)
		return false;

	data_format = radv_translate_buffer_dataformat(desc,
						       vk_format_get_first_non_void_channel(format));
	num_format = radv_translate_buffer_numformat(desc,
						     vk_format_get_first_non_void_channel(format));

585
586
	if (scaled)
		*scaled = (num_format == V_008F0C_BUF_NUM_FORMAT_SSCALED) || (num_format == V_008F0C_BUF_NUM_FORMAT_USCALED);
587
588
589
590
	return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID &&
		num_format != ~0;
}

591
592
bool radv_is_colorbuffer_format_supported(const struct radv_physical_device *pdevice,
                                          VkFormat format, bool *blendable)
593
{
594
	const struct util_format_description *desc = vk_format_description(format);
595
596
597
598
599
600
601
602
603
604
605
606
	uint32_t color_format = radv_translate_colorformat(format);
	uint32_t color_swap = radv_translate_colorswap(format, false);
	uint32_t color_num_format = radv_translate_color_numformat(format,
								   desc,
								   vk_format_get_first_non_void_channel(format));

	if (color_num_format == V_028C70_NUMBER_UINT || color_num_format == V_028C70_NUMBER_SINT ||
	    color_format == V_028C70_COLOR_8_24 || color_format == V_028C70_COLOR_24_8 ||
	    color_format == V_028C70_COLOR_X24_8_32_FLOAT) {
		*blendable = false;
	} else
		*blendable = true;
607
608
609
610

	if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32 && pdevice->rad_info.chip_class < GFX10_3)
		return false;

611
612
613
614
615
616
617
	return color_format != V_028C70_COLOR_INVALID &&
		color_swap != ~0U &&
		color_num_format != ~0;
}

static bool radv_is_zs_format_supported(VkFormat format)
{
618
	return radv_translate_dbformat(format) != V_028040_Z_INVALID || format == VK_FORMAT_S8_UINT;
619
620
}

621
622
623
624
625
static bool radv_is_filter_minmax_format_supported(VkFormat format)
{
	/* From the Vulkan spec 1.1.71:
	 *
	 * "The following formats must support the
626
	 *  VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT feature with
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
	 *  VK_IMAGE_TILING_OPTIMAL, if they support
	 *  VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT."
	 */
	/* TODO: enable more formats. */
	switch (format) {
	case VK_FORMAT_R8_UNORM:
	case VK_FORMAT_R8_SNORM:
	case VK_FORMAT_R16_UNORM:
	case VK_FORMAT_R16_SNORM:
	case VK_FORMAT_R16_SFLOAT:
	case VK_FORMAT_R32_SFLOAT:
	case VK_FORMAT_D16_UNORM:
	case VK_FORMAT_X8_D24_UNORM_PACK32:
	case VK_FORMAT_D32_SFLOAT:
	case VK_FORMAT_D16_UNORM_S8_UINT:
	case VK_FORMAT_D24_UNORM_S8_UINT:
	case VK_FORMAT_D32_SFLOAT_S8_UINT:
		return true;
	default:
		return false;
	}
}

650
651
652
653
654
bool
radv_device_supports_etc(struct radv_physical_device *physical_device)
{
	return physical_device->rad_info.family == CHIP_VEGA10 ||
	       physical_device->rad_info.family == CHIP_RAVEN ||
655
	       physical_device->rad_info.family == CHIP_RAVEN2 ||
656
657
658
	       physical_device->rad_info.family == CHIP_STONEY;
}

659
660
661
662
663
664
static void
radv_physical_device_get_format_properties(struct radv_physical_device *physical_device,
					   VkFormat format,
					   VkFormatProperties *out_properties)
{
	VkFormatFeatureFlags linear = 0, tiled = 0, buffer = 0;
665
	const struct util_format_description *desc = vk_format_description(format);
666
	bool blendable;
667
	bool scaled = false;
668
	/* TODO: implement some software emulation of SUBSAMPLED formats. */
669
	if (!desc || vk_format_to_pipe_format(format) == PIPE_FORMAT_NONE ||
670
	    desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
671
672
673
674
675
676
		out_properties->linearTilingFeatures = linear;
		out_properties->optimalTilingFeatures = tiled;
		out_properties->bufferFeatures = buffer;
		return;
	}

677
	if (desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
678
	    !radv_device_supports_etc(physical_device)) {
679
680
681
682
683
684
		out_properties->linearTilingFeatures = linear;
		out_properties->optimalTilingFeatures = tiled;
		out_properties->bufferFeatures = buffer;
		return;
	}

685
	if (vk_format_get_plane_count(format) > 1 ||
686
	    desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
687
688
689
		uint32_t tiling = VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
		                  VK_FORMAT_FEATURE_TRANSFER_DST_BIT |
		                  VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
690
		                  VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT |
691
692
693
		                  VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT;

		/* The subsampled formats have no support for linear filters. */
694
		if (desc->layout != UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
695
696
697
698
			tiling |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT;
		}

		/* Fails for unknown reasons with linear tiling & subsampled formats. */
699
		out_properties->linearTilingFeatures = desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED ? 0 : tiling;
700
701
702
703
704
		out_properties->optimalTilingFeatures = tiling;
		out_properties->bufferFeatures = 0;
		return;
	}

705
706
707
708
709
	if (radv_is_storage_image_format_supported(physical_device, format)) {
		tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
		linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
	}

710
	if (radv_is_buffer_format_supported(format, &scaled)) {
711
712
713
714
715
716
		if (format != VK_FORMAT_R64_UINT && format != VK_FORMAT_R64_SINT) {
			buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
			if (!scaled)
				buffer |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;
		}
		buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
717
718
719
	}

	if (vk_format_is_depth_or_stencil(format)) {
720
		if (radv_is_zs_format_supported(format)) {
721
			tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
722
723
724
			tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
			tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
			         VK_FORMAT_FEATURE_BLIT_DST_BIT;
725
726
			tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
			         VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
727

728
			if (radv_is_filter_minmax_format_supported(format))
729
				 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
730

731
			if (vk_format_has_depth(format))
732
733
				tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;

734
			/* Don't support blitting surfaces with depth/stencil. */
735
			if (vk_format_has_depth(format) && vk_format_has_stencil(format))
736
737
				tiled &= ~VK_FORMAT_FEATURE_BLIT_DST_BIT;

738
739
			/* Don't support linear depth surfaces */
			linear = 0;
740
		}
741
742
743
744
745
746
747
	} else {
		bool linear_sampling;
		if (radv_is_sampler_format_supported(format, &linear_sampling)) {
			linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
				VK_FORMAT_FEATURE_BLIT_SRC_BIT;
			tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
				VK_FORMAT_FEATURE_BLIT_SRC_BIT;
748
749

			if (radv_is_filter_minmax_format_supported(format))
750
				 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT;
751

752
753
754
755
			if (linear_sampling) {
				linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
				tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
			}
756
757
758
759
760
761
762

			/* Don't support blitting for R32G32B32 formats. */
			if (format == VK_FORMAT_R32G32B32_SFLOAT ||
			    format == VK_FORMAT_R32G32B32_UINT ||
			    format == VK_FORMAT_R32G32B32_SINT) {
				linear &= ~VK_FORMAT_FEATURE_BLIT_SRC_BIT;
			}
763
		}
764
		if (radv_is_colorbuffer_format_supported(physical_device, format, &blendable)) {
765
766
767
768
769
770
771
			linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
			tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
			if (blendable) {
				linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
				tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
			}
		}
772
		if (tiled && !scaled) {
773
774
			tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
			         VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
775
		}
776
777
778
779

		/* Tiled formatting does not support NPOT pixel sizes */
		if (!util_is_power_of_two_or_zero(vk_format_get_blocksize(format)))
			tiled = 0;
780
781
	}

782
	if (linear && !scaled) {
783
784
		linear |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
		          VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
785
786
	}

787
	if (radv_is_atomic_format_supported(format)) {
788
789
790
791
792
		buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
		linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
		tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
	}

793
794
795
796
797
798
799
	switch(format) {
	case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
	case VK_FORMAT_A2B10G10R10_SNORM_PACK32:
	case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
	case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:
	case VK_FORMAT_A2R10G10B10_SINT_PACK32:
	case VK_FORMAT_A2B10G10R10_SINT_PACK32:
800
801
802
803
		buffer &= ~(VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
			    VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT);
		linear = 0;
		tiled = 0;
804
805
806
807
808
		break;
	default:
		break;
	}

809
810
811
812
	/* addrlib does not support linear compressed textures. */
	if (vk_format_is_compressed(format))
		linear = 0;

813
814
815
816
817
818
819
	out_properties->linearTilingFeatures = linear;
	out_properties->optimalTilingFeatures = tiled;
	out_properties->bufferFeatures = buffer;
}

uint32_t radv_translate_colorformat(VkFormat format)
{
820
	const struct util_format_description *desc = vk_format_description(format);
821
822
823
824
825
826
827
828

#define HAS_SIZE(x,y,z,w)						\
	(desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
         desc->channel[2].size == (z) && desc->channel[3].size == (w))

	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) /* isn't plain */
		return V_028C70_COLOR_10_11_11;

829
830
831
	if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32)
		return V_028C70_COLOR_5_9_9_9;

832
	if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
833
834
835
836
		return V_028C70_COLOR_INVALID;

	/* hw cannot support mixed formats (except depth/stencil, since
	 * stencil is not written to). */
837
	if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
		return V_028C70_COLOR_INVALID;

	switch (desc->nr_channels) {
	case 1:
		switch (desc->channel[0].size) {
		case 8:
			return V_028C70_COLOR_8;
		case 16:
			return V_028C70_COLOR_16;
		case 32:
			return V_028C70_COLOR_32;
		}
		break;
	case 2:
		if (desc->channel[0].size == desc->channel[1].size) {
			switch (desc->channel[0].size) {
			case 8:
				return V_028C70_COLOR_8_8;
			case 16:
				return V_028C70_COLOR_16_16;
			case 32:
				return V_028C70_COLOR_32_32;
			}
		} else if (HAS_SIZE(8,24,0,0)) {
			return V_028C70_COLOR_24_8;
		} else if (HAS_SIZE(24,8,0,0)) {
			return V_028C70_COLOR_8_24;
		}
		break;
	case 3:
		if (HAS_SIZE(5,6,5,0)) {
			return V_028C70_COLOR_5_6_5;
		} else if (HAS_SIZE(32,8,24,0)) {
			return V_028C70_COLOR_X24_8_32_FLOAT;
		}
		break;
	case 4:
		if (desc->channel[0].size == desc->channel[1].size &&
		    desc->channel[0].size == desc->channel[2].size &&
		    desc->channel[0].size == desc->channel[3].size) {
			switch (desc->channel[0].size) {
			case 4:
				return V_028C70_COLOR_4_4_4_4;
			case 8:
				return V_028C70_COLOR_8_8_8_8;
			case 16:
				return V_028C70_COLOR_16_16_16_16;
			case 32:
				return V_028C70_COLOR_32_32_32_32;
			}
		} else if (HAS_SIZE(5,5,5,1)) {
			return V_028C70_COLOR_1_5_5_5;
		} else if (HAS_SIZE(1,5,5,5)) {
			return V_028C70_COLOR_5_5_5_1;
		} else if (HAS_SIZE(10,10,10,2)) {
			return V_028C70_COLOR_2_10_10_10;
		}
		break;
	}
	return V_028C70_COLOR_INVALID;
}

uint32_t radv_colorformat_endian_swap(uint32_t colorformat)
{
	if (0/*SI_BIG_ENDIAN*/) {
		switch(colorformat) {
			/* 8-bit buffers. */
		case V_028C70_COLOR_8:
			return V_028C70_ENDIAN_NONE;

			/* 16-bit buffers. */
		case V_028C70_COLOR_5_6_5:
		case V_028C70_COLOR_1_5_5_5:
		case V_028C70_COLOR_4_4_4_4:
		case V_028C70_COLOR_16:
		case V_028C70_COLOR_8_8:
			return V_028C70_ENDIAN_8IN16;

			/* 32-bit buffers. */
		case V_028C70_COLOR_8_8_8_8:
		case V_028C70_COLOR_2_10_10_10:
		case V_028C70_COLOR_8_24:
		case V_028C70_COLOR_24_8:
		case V_028C70_COLOR_16_16:
			return V_028C70_ENDIAN_8IN32;

			/* 64-bit buffers. */
		case V_028C70_COLOR_16_16_16_16:
			return V_028C70_ENDIAN_8IN16;

		case V_028C70_COLOR_32_32:
			return V_028C70_ENDIAN_8IN32;

			/* 128-bit buffers. */
		case V_028C70_COLOR_32_32_32_32:
			return V_028C70_ENDIAN_8IN32;
		default:
			return V_028C70_ENDIAN_NONE; /* Unsupported. */
		}
	} else {
		return V_028C70_ENDIAN_NONE;
	}
}

uint32_t radv_translate_dbformat(VkFormat format)
{
	switch (format) {
	case VK_FORMAT_D16_UNORM:
	case VK_FORMAT_D16_UNORM_S8_UINT:
		return V_028040_Z_16;
	case VK_FORMAT_D32_SFLOAT:
	case VK_FORMAT_D32_SFLOAT_S8_UINT:
		return V_028040_Z_32_FLOAT;
	default:
		return V_028040_Z_INVALID;
	}
}

unsigned radv_translate_colorswap(VkFormat format, bool do_endian_swap)
{
958
	const struct util_format_description *desc = vk_format_description(format);
959

Bas Nieuwenhuizen's avatar
Bas Nieuwenhuizen committed
960
#define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == PIPE_SWIZZLE_##swz)
961
962

	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
963
		return V_028C70_SWAP_STD;
964

965
966
967
	if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32)
		return V_028C70_SWAP_STD;

968
	if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
969
970
971
972
973
		return ~0U;

	switch (desc->nr_channels) {
	case 1:
		if (HAS_SWIZZLE(0,X))
974
			return V_028C70_SWAP_STD; /* X___ */
975
		else if (HAS_SWIZZLE(3,X))
976
			return V_028C70_SWAP_ALT_REV; /* ___X */
977
978
979
980
981
		break;
	case 2:
		if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
		    (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
		    (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
982
			return V_028C70_SWAP_STD; /* XY__ */
983
984
985
986
		else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
			 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
		         (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
			/* YX__ */
987
			return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
988
		else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
989
			return V_028C70_SWAP_ALT; /* X__Y */
990
		else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
991
			return V_028C70_SWAP_ALT_REV; /* Y__X */
992
993
994
		break;
	case 3:
		if (HAS_SWIZZLE(0,X))
995
			return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
996
		else if (HAS_SWIZZLE(0,Z))
997
			return V_028C70_SWAP_STD_REV; /* ZYX */
998
999
1000
1001
		break;
	case 4:
		/* check the middle channels, the 1st and 4th channel can be NONE */
		if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
1002
			return V_028C70_SWAP_STD; /* XYZW */
1003
		} else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
1004
			return V_028C70_SWAP_STD_REV; /* WZYX */
1005
		} else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
1006
			return V_028C70_SWAP_ALT; /* ZYXW */
1007
1008
1009
		} else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
			/* YZWX */
			if (desc->is_array)
1010
				return V_028C70_SWAP_ALT_REV;
1011
			else
1012
				return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
		}
		break;
	}
	return ~0U;
}

bool radv_format_pack_clear_color(VkFormat format,
				  uint32_t clear_vals[2],
				  VkClearColorValue *value)
{
1023
	const struct util_format_description *desc = vk_format_description(format);
1024

1025
	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
1026
1027
		clear_vals[0] = float3_to_r11g11b10f(value->float32);
		clear_vals[1] = 0;
1028
		return true;
1029
1030
1031
1032
	} else if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
		clear_vals[0] = float3_to_rgb9e5(value->float32);
		clear_vals[1] = 0;
		return true;
1033
1034
	}

1035
	if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
		fprintf(stderr, "failed to fast clear for non-plain format %d\n", format);
		return false;
	}

	if (!util_is_power_of_two_or_zero(desc->block.bits)) {
		fprintf(stderr, "failed to fast clear for NPOT format %d\n", format);
		return false;
	}

	if (desc->block.bits > 64) {
		/*
		 * We have a 128 bits format, check if the first 3 components are the same.
		 * Every elements has to be 32 bits since we don't support 64-bit formats,
		 * and we can skip swizzling checks as alpha always comes last for these and
		 * we do not care about the rest as they have to be the same.
		 */
1052
		if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
1053
1054
1055
1056
1057
1058
1059
1060
			if (value->float32[0] != value->float32[1] ||
			    value->float32[0] != value->float32[2])
				return false;
		} else {
			if (value->uint32[0] != value->uint32[1] ||
			    value->uint32[0] != value->uint32[2])
				return false;
		}
1061
1062
		clear_vals[0] = value->uint32[0];
		clear_vals[1] = value->uint32[3];
1063
1064
1065
1066
1067
		return true;
	}
	uint64_t clear_val = 0;

	for (unsigned c = 0; c < 4; ++c) {
1068
		if (desc->swizzle[c] >= 4)
1069
1070
			continue;

1071
		const struct util_format_channel_description *channel = &desc->channel[desc->swizzle[c]];
1072
1073
1074
1075
1076
1077
		assert(channel->size);

		uint64_t v = 0;
		if (channel->pure_integer) {
			v = value->uint32[c]  & ((1ULL << channel->size) - 1);
		} else if (channel->normalized) {
1078
			if (channel->type == UTIL_FORMAT_TYPE_UNSIGNED &&
1079
			    desc->swizzle[c] < 3 &&
1080
			    desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
1081
1082
1083
				assert(channel->size == 8);

				v = util_format_linear_float_to_srgb_8unorm(value->float32[c]);
1084
1085
1086
			} else {
				float f = MIN2(value->float32[c], 1.0f);

1087
				if (channel->type == UTIL_FORMAT_TYPE_UNSIGNED) {
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
					f = MAX2(f, 0.0f) * ((1ULL << channel->size) - 1);
				} else {
					f = MAX2(f, -1.0f) * ((1ULL << (channel->size - 1)) - 1);
				}

				/* The hardware rounds before conversion. */
				if (f > 0)
					f += 0.5f;
				else
					f -= 0.5f;

				v = (uint64_t)f;
1100
			}
1101
		} else if (channel->type == UTIL_FORMAT_TYPE_FLOAT) {
1102
1103
1104
			if (channel->size == 32) {
				memcpy(&v, &value->float32[c], 4);
			} else if(channel->size == 16) {
1105
				v = _mesa_float_to_float16_rtz(value->float32[c]);
1106
1107
1108
1109
1110
1111
			} else {
				fprintf(stderr, "failed to fast clear for unhandled float size in format %d\n", format);
				return false;
			}
		} else {
			fprintf(stderr, "failed to fast clear for unhandled component type in format %d\n", format);
1112
			return false;
1113
1114
		}
		clear_val |= (v & ((1ULL << channel->size) - 1)) << channel->shift;
1115
	}
1116
1117
1118
1119

	clear_vals[0] = clear_val;
	clear_vals[1] = clear_val >> 32;

1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
	return true;
}

void radv_GetPhysicalDeviceFormatProperties(
	VkPhysicalDevice                            physicalDevice,
	VkFormat                                    format,
	VkFormatProperties*                         pFormatProperties)
{
	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);

	radv_physical_device_get_format_properties(physical_device,
						   format,
						   pFormatProperties);
}

1135
static const struct ac_modifier_options radv_modifier_options = {
1136
1137
	.dcc = true,
	.dcc_retile = true,
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
};

static VkFormatFeatureFlags
radv_get_modifier_flags(struct radv_physical_device *dev,
                        VkFormat format, uint64_t modifier,
                        const VkFormatProperties *props)
{
	VkFormatFeatureFlags features;

	if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format))
		return 0;

	if (modifier == DRM_FORMAT_MOD_LINEAR)
		features = props->linearTilingFeatures;
	else
		features = props->optimalTilingFeatures;

	if (modifier != DRM_FORMAT_MOD_LINEAR && vk_format_get_plane_count(format) > 1)
		return 0;

	if (ac_modifier_has_dcc(modifier)) {
1159
		features &= ~VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
1160

1161
		if (dev->instance->debug_flags & (RADV_DEBUG_NO_DCC | RADV_DEBUG_NO_DISPLAY_DCC))
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
			return 0;
	}

	return features;
}

static void radv_list_drm_format_modifiers(struct radv_physical_device *dev,
                                           VkFormat format,
                                           VkFormatProperties2 *pFormatProperties)
{
	VkDrmFormatModifierPropertiesListEXT *mod_list =
		vk_find_struct(pFormatProperties, DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT);
	unsigned mod_count;

	if (!mod_list)
		return;

	if (vk_format_is_compressed(format) || vk_format_is_depth_or_stencil(format)) {
		mod_list->drmFormatModifierCount = 0;
		return;
	}

	ac_get_supported_modifiers(&dev->rad_info, &radv_modifier_options,
	                           vk_format_to_pipe_format(format), &mod_count, NULL);
	if (!mod_list->pDrmFormatModifierProperties) {
		mod_list->drmFormatModifierCount = mod_count;
		return;
	}

	mod_count = MIN2(mod_count, mod_list->drmFormatModifierCount);

	uint64_t *mods = malloc(mod_count * sizeof(uint64_t));
	if (!mods) {
		/* We can't return an error here ... */
		mod_list->drmFormatModifierCount = 0;
		return;
	}
	ac_get_supported_modifiers(&dev->rad_info, &radv_modifier_options,
	                           vk_format_to_pipe_format(format), &mod_count, mods);

	mod_list->drmFormatModifierCount = 0;
	for (unsigned i = 0; i < mod_count; ++i) {
		VkFormatFeatureFlags features =
			radv_get_modifier_flags(dev, format, mods[i], &pFormatProperties->formatProperties);
		unsigned planes = vk_format_get_plane_count(format);
		if (planes == 1) {
			if (ac_modifier_has_dcc_retile(mods[i]))
				planes = 3;
			else if (ac_modifier_has_dcc(mods[i]))
				planes = 2;
		}

		if (!features)
			continue;

		mod_list->pDrmFormatModifierProperties[mod_list->drmFormatModifierCount].drmFormatModifier = mods[i];
		mod_list->pDrmFormatModifierProperties[mod_list->drmFormatModifierCount].drmFormatModifierPlaneCount = planes;
		mod_list->pDrmFormatModifierProperties[mod_list->drmFormatModifierCount].drmFormatModifierTilingFeatures = features;

		++mod_list->drmFormatModifierCount;
	}

	free(mods);
}


static VkResult radv_check_modifier_support(struct radv_physical_device *dev,
                                       const VkPhysicalDeviceImageFormatInfo2 *info,
                                       VkImageFormatProperties *props,
                                       VkFormat format,
                                       uint64_t modifier)
{
	if (info->type != VK_IMAGE_TYPE_2D)
		return VK_ERROR_FORMAT_NOT_SUPPORTED;

	/* We did not add modifiers for sparse textures. */
	if (info->flags & (VK_IMAGE_CREATE_SPARSE_BINDING_BIT |
	                   VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT |
	                   VK_IMAGE_CREATE_SPARSE_ALIASED_BIT))
		return VK_ERROR_FORMAT_NOT_SUPPORTED;

	/*
	 * Need to check the modifier is supported in general:
	 * "If the drmFormatModifier is incompatible with the parameters specified
	 * in VkPhysicalDeviceImageFormatInfo2 and its pNext chain, then
	 * vkGetPhysicalDeviceImageFormatProperties2 returns VK_ERROR_FORMAT_NOT_SUPPORTED.
	 * The implementation must support the query of any drmFormatModifier,
	 * including unknown and invalid modifier values."
	 */
	VkDrmFormatModifierPropertiesListEXT mod_list = {
		.sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT,
	};

	VkFormatProperties2 format_props2 = {
		.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
		.pNext = &mod_list
	};

	radv_GetPhysicalDeviceFormatProperties2(radv_physical_device_to_handle(dev), format,  &format_props2);

	if (!mod_list.drmFormatModifierCount)
		return VK_ERROR_FORMAT_NOT_SUPPORTED;

	mod_list.pDrmFormatModifierProperties = calloc(mod_list.drmFormatModifierCount, sizeof(*mod_list.pDrmFormatModifierProperties));
	if (!mod_list.pDrmFormatModifierProperties)
		return VK_ERROR_OUT_OF_HOST_MEMORY;

	radv_GetPhysicalDeviceFormatProperties2(radv_physical_device_to_handle(dev), format,  &format_props2);

	bool found = false;
	for (uint32_t i = 0; i < mod_list.drmFormatModifierCount && !found; ++i)
		if (mod_list.pDrmFormatModifierProperties[i].drmFormatModifier == modifier)
			found = true;

	free(mod_list.pDrmFormatModifierProperties);

	if (!found)
		return VK_ERROR_FORMAT_NOT_SUPPORTED;

	if (ac_modifier_has_dcc(modifier) &&
	    !radv_are_formats_dcc_compatible(dev, info->pNext, format, info->flags))
		return VK_ERROR_FORMAT_NOT_SUPPORTED;

	/* We can expand this as needed and implemented but there is not much demand
	 * for more. */
	if (ac_modifier_has_dcc(modifier)) {
		props->maxMipLevels = 1;
		props->maxArrayLayers = 1;
	}
	/* We don't support MSAA for modifiers */
	props->sampleCounts &= VK_SAMPLE_COUNT_1_BIT;
	return VK_SUCCESS;
}

1296
void radv_GetPhysicalDeviceFormatProperties2(
1297
1298
	VkPhysicalDevice                            physicalDevice,
	VkFormat                                    format,
1299
	VkFormatProperties2*                        pFormatProperties)
1300
1301
1302
1303
1304
1305
{
	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);

	radv_physical_device_get_format_properties(physical_device,
						   format,
						   &pFormatProperties->formatProperties);
1306
1307

	radv_list_drm_format_modifiers(physical_device, format, pFormatProperties);
1308
1309
}

1310
static VkResult radv_get_image_format_properties(struct radv_physical_device *physical_device,
1311
						 const VkPhysicalDeviceImageFormatInfo2 *info,
1312
						 VkFormat format,
1313
1314
						 VkImageFormatProperties *pImageFormatProperties)

1315
1316
1317
1318
1319
1320
1321
{
	VkFormatProperties format_props;
	VkFormatFeatureFlags format_feature_flags;
	VkExtent3D maxExtent;
	uint32_t maxMipLevels;
	uint32_t maxArraySize;
	VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1322
	const struct util_format_description *desc = vk_format_description(format);
1323
	enum chip_class chip_class = physical_device->rad_info.chip_class;
1324
1325
1326
1327
1328
	VkImageTiling tiling = info->tiling;
	const VkPhysicalDeviceImageDrmFormatModifierInfoEXT *mod_info =
		vk_find_struct_const(info->pNext, PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT);
	VkResult result = VK_ERROR_FORMAT_NOT_SUPPORTED;

1329
	radv_physical_device_get_format_properties(physical_device, format,
1330
						   &format_props);
1331
	if (tiling == VK_IMAGE_TILING_LINEAR) {
1332
		format_feature_flags = format_props.linearTilingFeatures;
1333
	} else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
1334
		format_feature_flags = format_props.optimalTilingFeatures;
1335
1336
1337
1338
	} else if (tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT) {
		format_feature_flags = radv_get_modifier_flags(physical_device, format,
		                                               mod_info->drmFormatModifier,
		                                               &format_props);
1339
1340
1341
1342
1343
1344
1345
	} else {
		unreachable("bad VkImageTiling");
	}

	if (format_feature_flags == 0)
		goto unsupported;

1346
	if (info->type != VK_IMAGE_TYPE_2D && vk_format_is_depth_or_stencil(format))
1347
1348
		goto unsupported;

1349
	switch (info->type) {
1350
1351
1352
1353
1354
1355
1356
	default:
		unreachable("bad vkimage type\n");
	case VK_IMAGE_TYPE_1D:
		maxExtent.width = 16384;
		maxExtent.height = 1;
		maxExtent.depth = 1;
		maxMipLevels = 15; /* log2(maxWidth) + 1 */
1357
		maxArraySize = chip_class >= GFX10 ? 8192 : 2048;
1358
1359
1360
1361
1362
1363
		break;
	case VK_IMAGE_TYPE_2D:
		maxExtent.width = 16384;
		maxExtent.height = 16384;
		maxExtent.depth = 1;
		maxMipLevels = 15; /* log2(maxWidth) + 1 */
1364
		maxArraySize = chip_class >= GFX10 ? 8192 : 2048;
1365
1366
		break;
	case VK_IMAGE_TYPE_3D:
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
		if (chip_class >= GFX10) {
			maxExtent.width = 8192;
			maxExtent.height = 8192;
			maxExtent.depth = 8192;
		} else {
			maxExtent.width = 2048;
			maxExtent.height = 2048;
			maxExtent.depth = 2048;
		}
		maxMipLevels = util_logbase2(maxExtent.width) + 1;
1377
1378
1379
1380
		maxArraySize = 1;
		break;
	}

1381
	if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
1382
1383
1384
1385
1386
		/* Might be able to support but the entire format support is
		 * messy, so taking the lazy way out. */
		maxArraySize = 1;
	}

1387
	if (tiling == VK_IMAGE_TILING_OPTIMAL &&
1388
	    info->type == VK_IMAGE_TYPE_2D &&
1389
1390
	    (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
				     VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1391
	    !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)) {
1392
1393
1394
		sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
	}

1395
	if (tiling == VK_IMAGE_TILING_LINEAR &&
1396
1397
1398
	    (format == VK_FORMAT_R32G32B32_SFLOAT ||
	     format == VK_FORMAT_R32G32B32_SINT ||
	     format == VK_FORMAT_R32G32B32_UINT)) {
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
		/* R32G32B32 is a weird format and the driver currently only
		 * supports the barely minimum.
		 * TODO: Implement more if we really need to.
		 */
		if (info->type == VK_IMAGE_TYPE_3D)
			goto unsupported;
		maxArraySize = 1;
		maxMipLevels = 1;
	}

1409
1410
1411
1412

	/* We can't create 3d compressed 128bpp images that can be rendered to on GFX9 */
	if (physical_device->rad_info.chip_class >= GFX9 &&
	    info->type == VK_IMAGE_TYPE_3D &&
1413
1414
	    vk_format_get_blocksizebits(format) == 128 &&
	    vk_format_is_compressed(format) &&
1415
1416
1417
1418
1419
1420
	    (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
	    ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) ||
	     (info->usage & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))) {
		goto unsupported;
	}

1421
	if (info->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
1422
1423
1424
1425
1426
		if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
			goto unsupported;
		}
	}

1427
	if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1428
1429
1430
1431
1432
		if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
			goto unsupported;
		}
	}

1433
	if (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
1434
1435
1436
1437
1438
		if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
			goto unsupported;
		}
	}

1439
	if (info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1440
1441
1442
1443
1444
		if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
			goto unsupported;
		}
	}

1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
	if (info->usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
		if (!(format_feature_flags & VK_FORMAT_FEATURE_TRANSFER_SRC_BIT)) {
			goto unsupported;
		}
	}

	if (info->usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
		if (!(format_feature_flags & VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
			goto unsupported;
		}
	}

	if (info->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
		if (!(format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
		                              VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))) {
			goto unsupported;
		}
	}

1464
1465
	/* Sparse resources with multi-planar formats are unsupported. */
	if (info->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
1466
		if (vk_format_get_plane_count(format) > 1)
1467
1468
1469
			goto unsupported;
	}

1470
	if (info->flags & VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT) {
1471
1472
1473
1474
		/* Sparse textures are only supported on GFX8+. */
		if (physical_device->rad_info.chip_class < GFX8)
			goto unsupported;

1475
		if (vk_format_get_plane_count(format) > 1 || info->type != VK_IMAGE_TYPE_2D ||
1476
1477
1478
1479
1480
		    info->tiling != VK_IMAGE_TILING_OPTIMAL ||
		    vk_format_is_depth_or_stencil(format))
			goto unsupported;
	}

1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
	*pImageFormatProperties = (VkImageFormatProperties) {
		.maxExtent = maxExtent,
		.maxMipLevels = maxMipLevels,
		.maxArrayLayers = maxArraySize,
		.sampleCounts = sampleCounts,

		/* FINISHME: Accurately calculate
		 * VkImageFormatProperties::maxResourceSize.
		 */
		.maxResourceSize = UINT32_MAX,
	};

1493
1494
1495
1496
1497
1498
1499
1500
	if (mod_info) {
		result = radv_check_modifier_support(physical_device, info,
		                                     pImageFormatProperties,
		                                     format, mod_info->drmFormatModifier);
		if (result != VK_SUCCESS)
			goto unsupported;
	}

1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
	return VK_SUCCESS;
unsupported:
	*pImageFormatProperties = (VkImageFormatProperties) {
		.maxExtent = { 0, 0, 0 },
		.maxMipLevels = 0,
		.maxArrayLayers = 0,
		.sampleCounts = 0,
		.maxResourceSize = 0,
	};