summaryrefslogtreecommitdiffstats
path: root/src/amd/vulkan/radv_formats.c
blob: 1188ee57b990848029b8003fbc06ff8f6f906300 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
320
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
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
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
/*
 * 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"

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

#include "util/u_half.h"
#include "util/format_srgb.h"

uint32_t radv_translate_buffer_dataformat(const struct vk_format_description *desc,
					  int first_non_void)
{
	unsigned type;
	int i;

	if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
		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;

	if (type == VK_FORMAT_TYPE_FIXED)
		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.'
		 */
		if (type != VK_FORMAT_TYPE_FLOAT &&
		    !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;
	}

	return V_008F0C_BUF_DATA_FORMAT_INVALID;
}

uint32_t radv_translate_buffer_numformat(const struct vk_format_description *desc,
					 int first_non_void)
{
	if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
		return V_008F0C_BUF_NUM_FORMAT_FLOAT;

	if (first_non_void < 0)
		return ~0;

	switch (desc->channel[first_non_void].type) {
	case VK_FORMAT_TYPE_SIGNED:
		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;
	case VK_FORMAT_TYPE_UNSIGNED:
		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;
	case VK_FORMAT_TYPE_FLOAT:
	default:
		return V_008F0C_BUF_NUM_FORMAT_FLOAT;
	}
}

uint32_t radv_translate_tex_dataformat(VkFormat format,
				       const struct vk_format_description *desc,
				       int first_non_void)
{
	bool uniform = true;
	int i;

	if (!desc)
		return ~0;
	/* Colorspace (return non-RGB formats directly). */
	switch (desc->colorspace) {
		/* Depth stencil formats */
	case VK_FORMAT_COLORSPACE_ZS:
		switch (format) {
		case VK_FORMAT_D16_UNORM:
			return V_008F14_IMG_DATA_FORMAT_16;
		case VK_FORMAT_D24_UNORM_S8_UINT:
		case VK_FORMAT_X8_D24_UNORM_PACK32:
			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;
		}

	case VK_FORMAT_COLORSPACE_YUV:
		goto out_unknown; /* TODO */

	case VK_FORMAT_COLORSPACE_SRGB:
		if (desc->nr_channels != 4 && desc->nr_channels != 1)
			goto out_unknown;
		break;

	default:
		break;
	}

	if (desc->layout == VK_FORMAT_LAYOUT_RGTC) {
		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;
		}
	}

	if (desc->layout == VK_FORMAT_LAYOUT_S3TC) {
		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;
		}
	}

	if (desc->layout == VK_FORMAT_LAYOUT_BPTC) {
		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;
		}
	}

	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).*/
	if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
		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 */
				if (desc->channel[0].type == VK_FORMAT_TYPE_SIGNED &&
				    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;
#if 0 /* Not supported for render targets */
		case 3:
			return V_008F14_IMG_DATA_FORMAT_32_32_32;
#endif
		case 4:
			return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
		}
	}

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,
				      const struct vk_format_description *desc,
				      int first_non_void)
{
	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:
					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:
					return V_008F14_IMG_NUM_FORMAT_SNORM;
				default:
					return V_008F14_IMG_NUM_FORMAT_UNORM;
				}
			} else if (desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
				return V_008F14_IMG_NUM_FORMAT_UNORM;
			} else {
				return V_008F14_IMG_NUM_FORMAT_FLOAT;
			}
		} else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
			return V_008F14_IMG_NUM_FORMAT_SRGB;
		} else {
			switch (desc->channel[first_non_void].type) {
			case VK_FORMAT_TYPE_FLOAT:
				return V_008F14_IMG_NUM_FORMAT_FLOAT;
			case VK_FORMAT_TYPE_SIGNED:
				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;
			case VK_FORMAT_TYPE_UNSIGNED:
				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,
					const struct vk_format_description *desc,
					int first_non_void)
{
	unsigned ntype;
	if (first_non_void == -1 || desc->channel[first_non_void].type == VK_FORMAT_TYPE_FLOAT)
		ntype = V_028C70_NUMBER_FLOAT;
	else {
		ntype = V_028C70_NUMBER_UNORM;
		if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB)
			ntype = V_028C70_NUMBER_SRGB;
		else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_SIGNED) {
			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;
		} else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_UNSIGNED) {
			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)
{
	const struct vk_format_description *desc = vk_format_description(format);
	uint32_t num_format;
	if (!desc || format == VK_FORMAT_UNDEFINED)
		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;
}


static bool radv_is_storage_image_format_supported(struct radv_physical_device *physical_device,
						   VkFormat format)
{
	const struct vk_format_description *desc = vk_format_description(format);
	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;
	}
}

static bool radv_is_buffer_format_supported(VkFormat format)
{
	const struct vk_format_description *desc = vk_format_description(format);
	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));

	return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID &&
		num_format != ~0;
}

bool radv_is_colorbuffer_format_supported(VkFormat format, bool *blendable)
{
	const struct vk_format_description *desc = vk_format_description(format);
	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;
	return color_format != V_028C70_COLOR_INVALID &&
		color_swap != ~0U &&
		color_num_format != ~0;
}

static bool radv_is_zs_format_supported(VkFormat format)
{
	return radv_translate_dbformat(format) != V_028040_Z_INVALID;
}

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;
	const struct vk_format_description *desc = vk_format_description(format);
	bool blendable;
	if (!desc) {
		out_properties->linearTilingFeatures = linear;
		out_properties->optimalTilingFeatures = tiled;
		out_properties->bufferFeatures = buffer;
		return;
	}

	if (radv_is_storage_image_format_supported(physical_device, format)) {
		tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
		linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
	}

	if (radv_is_buffer_format_supported(format)) {
		buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT |
			VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
			VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
	}

	if (vk_format_is_depth_or_stencil(format)) {
		if (radv_is_zs_format_supported(format)) {
			tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
			tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
			tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
			         VK_FORMAT_FEATURE_BLIT_DST_BIT;
			tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR |
			         VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR;
		}
	} 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;
			if (linear_sampling) {
				linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
				tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
			}
		}
		if (radv_is_colorbuffer_format_supported(format, &blendable)) {
			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;
			}
		}
		if (util_is_power_of_two(vk_format_get_blocksize(format))) {
			tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR |
			         VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR;
		}
	}

	if (util_is_power_of_two(vk_format_get_blocksize(format))) {
		linear |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR |
		          VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR;
	}

	if (format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT) {
		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;
	}

	out_properties->linearTilingFeatures = linear;
	out_properties->optimalTilingFeatures = tiled;
	out_properties->bufferFeatures = buffer;
}

uint32_t radv_translate_colorformat(VkFormat format)
{
	const struct vk_format_description *desc = vk_format_description(format);

#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;

	if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
		return V_028C70_COLOR_INVALID;

	/* hw cannot support mixed formats (except depth/stencil, since
	 * stencil is not written to). */
	if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
		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_X8_D24_UNORM_PACK32:
	case VK_FORMAT_D24_UNORM_S8_UINT:
		return V_028040_Z_24; /* deprecated on SI */
	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)
{
	const struct vk_format_description *desc = vk_format_description(format);

#define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz)

	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
		return V_0280A0_SWAP_STD;

	if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
		return ~0U;

	switch (desc->nr_channels) {
	case 1:
		if (HAS_SWIZZLE(0,X))
			return V_0280A0_SWAP_STD; /* X___ */
		else if (HAS_SWIZZLE(3,X))
			return V_0280A0_SWAP_ALT_REV; /* ___X */
		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)))
			return V_0280A0_SWAP_STD; /* XY__ */
		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__ */
			return (do_endian_swap ? V_0280A0_SWAP_STD : V_0280A0_SWAP_STD_REV);
		else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
			return V_0280A0_SWAP_ALT; /* X__Y */
		else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
			return V_0280A0_SWAP_ALT_REV; /* Y__X */
		break;
	case 3:
		if (HAS_SWIZZLE(0,X))
			return (do_endian_swap ? V_0280A0_SWAP_STD_REV : V_0280A0_SWAP_STD);
		else if (HAS_SWIZZLE(0,Z))
			return V_0280A0_SWAP_STD_REV; /* ZYX */
		break;
	case 4:
		/* check the middle channels, the 1st and 4th channel can be NONE */
		if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
			return V_0280A0_SWAP_STD; /* XYZW */
		} else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
			return V_0280A0_SWAP_STD_REV; /* WZYX */
		} else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
			return V_0280A0_SWAP_ALT; /* ZYXW */
		} else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
			/* YZWX */
			if (desc->is_array)
				return V_0280A0_SWAP_ALT_REV;
			else
				return (do_endian_swap ? V_0280A0_SWAP_ALT : V_0280A0_SWAP_ALT_REV);
		}
		break;
	}
	return ~0U;
}

bool radv_format_pack_clear_color(VkFormat format,
				  uint32_t clear_vals[2],
				  VkClearColorValue *value)
{
	uint8_t r = 0, g = 0, b = 0, a = 0;
	const struct vk_format_description *desc = vk_format_description(format);

	if (vk_format_get_component_bits(format, VK_FORMAT_COLORSPACE_RGB, 0) <= 8) {
		if (desc->colorspace == VK_FORMAT_COLORSPACE_RGB) {
			r = float_to_ubyte(value->float32[0]);
			g = float_to_ubyte(value->float32[1]);
			b = float_to_ubyte(value->float32[2]);
			a = float_to_ubyte(value->float32[3]);
		} else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
			r = util_format_linear_float_to_srgb_8unorm(value->float32[0]);
			g = util_format_linear_float_to_srgb_8unorm(value->float32[1]);
			b = util_format_linear_float_to_srgb_8unorm(value->float32[2]);
			a = float_to_ubyte(value->float32[3]);
		}
	}
	switch (format) {
	case VK_FORMAT_R8_UNORM:
	case VK_FORMAT_R8_SRGB:
		clear_vals[0] = r;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R8G8_UNORM:
	case VK_FORMAT_R8G8_SRGB:
		clear_vals[0] = r | g << 8;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R8G8B8A8_SRGB:
	case VK_FORMAT_R8G8B8A8_UNORM:
		clear_vals[0] = r | g << 8 | b << 16 | a << 24;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_B8G8R8A8_SRGB:
	case VK_FORMAT_B8G8R8A8_UNORM:
		clear_vals[0] = b | g << 8 | r << 16 | a << 24;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
	case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
		clear_vals[0] = r | g << 8 | b << 16 | a << 24;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R8_UINT:
		clear_vals[0] = value->uint32[0] & 0xff;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R16_UINT:
		clear_vals[0] = value->uint32[0] & 0xffff;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R8G8_UINT:
		clear_vals[0] = value->uint32[0] & 0xff;
		clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R8G8B8A8_UINT:
		clear_vals[0] = value->uint32[0] & 0xff;
		clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
		clear_vals[0] |= (value->uint32[2] & 0xff) << 16;
		clear_vals[0] |= (value->uint32[3] & 0xff) << 24;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_A8B8G8R8_UINT_PACK32:
		clear_vals[0] = value->uint32[0] & 0xff;
		clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
		clear_vals[0] |= (value->uint32[2] & 0xff) << 16;
		clear_vals[0] |= (value->uint32[3] & 0xff) << 24;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R16G16_UINT:
		clear_vals[0] = value->uint32[0] & 0xffff;
		clear_vals[0] |= (value->uint32[1] & 0xffff) << 16;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R16G16B16A16_UINT:
		clear_vals[0] = value->uint32[0] & 0xffff;
		clear_vals[0] |= (value->uint32[1] & 0xffff) << 16;
		clear_vals[1] = value->uint32[2] & 0xffff;
		clear_vals[1] |= (value->uint32[3] & 0xffff) << 16;
		break;
	case VK_FORMAT_R32_UINT:
		clear_vals[0] = value->uint32[0];
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R32G32_UINT:
		clear_vals[0] = value->uint32[0];
		clear_vals[1] = value->uint32[1];
		break;
	case VK_FORMAT_R32_SINT:
		clear_vals[0] = value->int32[0];
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R16_SFLOAT:
		clear_vals[0] = util_float_to_half(value->float32[0]);
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R16G16_SFLOAT:
		clear_vals[0] = util_float_to_half(value->float32[0]);
		clear_vals[0] |= (uint32_t)util_float_to_half(value->float32[1]) << 16;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R16G16B16A16_SFLOAT:
		clear_vals[0] = util_float_to_half(value->float32[0]);
		clear_vals[0] |= (uint32_t)util_float_to_half(value->float32[1]) << 16;
		clear_vals[1] = util_float_to_half(value->float32[2]);
		clear_vals[1] |= (uint32_t)util_float_to_half(value->float32[3]) << 16;
		break;
	case VK_FORMAT_R16_UNORM:
		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R16G16_UNORM:
		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
		clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0xffff)) << 16;
		clear_vals[1] = 0;
		break;
	case VK_FORMAT_R16G16B16A16_UNORM:
		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
		clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0xffff)) << 16;
		clear_vals[1] = ((uint16_t)util_iround(CLAMP(value->float32[2], 0.0f, 1.0f) * 0xffff)) & 0xffff;
		clear_vals[1] |= ((uint16_t)util_iround(CLAMP(value->float32[3], 0.0f, 1.0f) * 0xffff)) << 16;
		break;
	case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0x3ff)) & 0x3ff;
		clear_vals[0] |= (((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0x3ff)) & 0x3ff) << 10;
		clear_vals[0] |= (((uint16_t)util_iround(CLAMP(value->float32[2], 0.0f, 1.0f) * 0x3ff)) & 0x3ff) << 20;
		clear_vals[0] |= (((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0x3)) & 0x3) << 30;
		clear_vals[1] = 0;
		return true;
	case VK_FORMAT_R32G32_SFLOAT:
		clear_vals[0] = fui(value->float32[0]);
		clear_vals[1] = fui(value->float32[1]);
		break;
	case VK_FORMAT_R32_SFLOAT:
		clear_vals[1] = 0;
		clear_vals[0] = fui(value->float32[0]);
		break;
	default:
		fprintf(stderr, "failed to fast clear %d\n", format);
		return false;
	}
	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);
}

void radv_GetPhysicalDeviceFormatProperties2KHR(
	VkPhysicalDevice                            physicalDevice,
	VkFormat                                    format,
	VkFormatProperties2KHR*                         pFormatProperties)
{
	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);

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

VkResult radv_GetPhysicalDeviceImageFormatProperties(
	VkPhysicalDevice                            physicalDevice,
	VkFormat                                    format,
	VkImageType                                 type,
	VkImageTiling                               tiling,
	VkImageUsageFlags                           usage,
	VkImageCreateFlags                          createFlags,
	VkImageFormatProperties*                    pImageFormatProperties)
{
	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
	VkFormatProperties format_props;
	VkFormatFeatureFlags format_feature_flags;
	VkExtent3D maxExtent;
	uint32_t maxMipLevels;
	uint32_t maxArraySize;
	VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;

	radv_physical_device_get_format_properties(physical_device, format,
						   &format_props);
	if (tiling == VK_IMAGE_TILING_LINEAR) {
		format_feature_flags = format_props.linearTilingFeatures;
	} else if (tiling == VK_IMAGE_TILING_OPTIMAL) {
		format_feature_flags = format_props.optimalTilingFeatures;
	} else {
		unreachable("bad VkImageTiling");
	}

	if (format_feature_flags == 0)
		goto unsupported;

	switch (type) {
	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 */
		maxArraySize = 2048;
		break;
	case VK_IMAGE_TYPE_2D:
		maxExtent.width = 16384;
		maxExtent.height = 16384;
		maxExtent.depth = 1;
		maxMipLevels = 15; /* log2(maxWidth) + 1 */
		maxArraySize = 2048;
		break;
	case VK_IMAGE_TYPE_3D:
		maxExtent.width = 2048;
		maxExtent.height = 2048;
		maxExtent.depth = 2048;
		maxMipLevels = 12; /* log2(maxWidth) + 1 */
		maxArraySize = 1;
		break;
	}

	if (tiling == VK_IMAGE_TILING_OPTIMAL &&
	    type == VK_IMAGE_TYPE_2D &&
	    (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
				     VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
	    !(createFlags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
	    !(usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
		sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
	}

	if (usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
		if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
			goto unsupported;
		}
	}

	if (usage & VK_IMAGE_USAGE_STORAGE_BIT) {
		if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
			goto unsupported;
		}
	}

	if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
		if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
			goto unsupported;
		}
	}

	if (usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
		if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
			goto unsupported;
		}
	}

	*pImageFormatProperties = (VkImageFormatProperties) {
		.maxExtent = maxExtent,
		.maxMipLevels = maxMipLevels,
		.maxArrayLayers = maxArraySize,
		.sampleCounts = sampleCounts,

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

	return VK_SUCCESS;
unsupported:
	*pImageFormatProperties = (VkImageFormatProperties) {
		.maxExtent = { 0, 0, 0 },
		.maxMipLevels = 0,
		.maxArrayLayers = 0,
		.sampleCounts = 0,
		.maxResourceSize = 0,
	};

	return VK_ERROR_FORMAT_NOT_SUPPORTED;
}

VkResult radv_GetPhysicalDeviceImageFormatProperties2KHR(
	VkPhysicalDevice                            physicalDevice,
	const VkPhysicalDeviceImageFormatInfo2KHR*  pImageFormatInfo,
	VkImageFormatProperties2KHR                *pImageFormatProperties)
{
	return radv_GetPhysicalDeviceImageFormatProperties(physicalDevice,
							   pImageFormatInfo->format,
							   pImageFormatInfo->type,
							   pImageFormatInfo->tiling,
							   pImageFormatInfo->usage,
							   pImageFormatInfo->flags,
							   &pImageFormatProperties->imageFormatProperties);
}

void radv_GetPhysicalDeviceSparseImageFormatProperties(
	VkPhysicalDevice                            physicalDevice,
	VkFormat                                    format,
	VkImageType                                 type,
	uint32_t                                    samples,
	VkImageUsageFlags                           usage,
	VkImageTiling                               tiling,
	uint32_t*                                   pNumProperties,
	VkSparseImageFormatProperties*              pProperties)
{
	/* Sparse images are not yet supported. */
	*pNumProperties = 0;
}

void radv_GetPhysicalDeviceSparseImageFormatProperties2KHR(
	VkPhysicalDevice                            physicalDevice,
	const VkPhysicalDeviceSparseImageFormatInfo2KHR* pFormatInfo,
	uint32_t                                   *pPropertyCount,
	VkSparseImageFormatProperties2KHR*          pProperties)
{
	/* Sparse images are not yet supported. */
	*pPropertyCount = 0;
}