1/*
2 * Copyright © 2016 Red Hat.
3 * Copyright © 2016 Bas Nieuwenhuizen
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#include "radv_private.h"
26
27#include "vk_format.h"
28#include "sid.h"
29
30#include "vk_util.h"
31
32#include "util/u_half.h"
33#include "util/format_srgb.h"
34#include "util/format_r11g11b10f.h"
35
36uint32_t radv_translate_buffer_dataformat(const struct vk_format_description *desc,
37					  int first_non_void)
38{
39	unsigned type;
40	int i;
41
42	assert(desc->layout != VK_FORMAT_LAYOUT_MULTIPLANE);
43
44	if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
45		return V_008F0C_BUF_DATA_FORMAT_10_11_11;
46
47	if (first_non_void < 0)
48		return V_008F0C_BUF_DATA_FORMAT_INVALID;
49	type = desc->channel[first_non_void].type;
50
51	if (type == VK_FORMAT_TYPE_FIXED)
52		return V_008F0C_BUF_DATA_FORMAT_INVALID;
53	if (desc->nr_channels == 4 &&
54	    desc->channel[0].size == 10 &&
55	    desc->channel[1].size == 10 &&
56	    desc->channel[2].size == 10 &&
57	    desc->channel[3].size == 2)
58		return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
59
60	/* See whether the components are of the same size. */
61	for (i = 0; i < desc->nr_channels; i++) {
62		if (desc->channel[first_non_void].size != desc->channel[i].size)
63			return V_008F0C_BUF_DATA_FORMAT_INVALID;
64	}
65
66	switch (desc->channel[first_non_void].size) {
67	case 8:
68		switch (desc->nr_channels) {
69		case 1:
70			return V_008F0C_BUF_DATA_FORMAT_8;
71		case 2:
72			return V_008F0C_BUF_DATA_FORMAT_8_8;
73		case 4:
74			return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
75		}
76		break;
77	case 16:
78		switch (desc->nr_channels) {
79		case 1:
80			return V_008F0C_BUF_DATA_FORMAT_16;
81		case 2:
82			return V_008F0C_BUF_DATA_FORMAT_16_16;
83		case 4:
84			return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
85		}
86		break;
87	case 32:
88		/* From the Southern Islands ISA documentation about MTBUF:
89		 * 'Memory reads of data in memory that is 32 or 64 bits do not
90		 * undergo any format conversion.'
91		 */
92		if (type != VK_FORMAT_TYPE_FLOAT &&
93		    !desc->channel[first_non_void].pure_integer)
94			return V_008F0C_BUF_DATA_FORMAT_INVALID;
95
96		switch (desc->nr_channels) {
97		case 1:
98			return V_008F0C_BUF_DATA_FORMAT_32;
99		case 2:
100			return V_008F0C_BUF_DATA_FORMAT_32_32;
101		case 3:
102			return V_008F0C_BUF_DATA_FORMAT_32_32_32;
103		case 4:
104			return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
105		}
106		break;
107	}
108
109	return V_008F0C_BUF_DATA_FORMAT_INVALID;
110}
111
112uint32_t radv_translate_buffer_numformat(const struct vk_format_description *desc,
113					 int first_non_void)
114{
115	assert(desc->layout != VK_FORMAT_LAYOUT_MULTIPLANE);
116
117	if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
118		return V_008F0C_BUF_NUM_FORMAT_FLOAT;
119
120	if (first_non_void < 0)
121		return ~0;
122
123	switch (desc->channel[first_non_void].type) {
124	case VK_FORMAT_TYPE_SIGNED:
125		if (desc->channel[first_non_void].normalized)
126			return V_008F0C_BUF_NUM_FORMAT_SNORM;
127		else if (desc->channel[first_non_void].pure_integer)
128			return V_008F0C_BUF_NUM_FORMAT_SINT;
129		else
130			return V_008F0C_BUF_NUM_FORMAT_SSCALED;
131		break;
132	case VK_FORMAT_TYPE_UNSIGNED:
133		if (desc->channel[first_non_void].normalized)
134			return V_008F0C_BUF_NUM_FORMAT_UNORM;
135		else if (desc->channel[first_non_void].pure_integer)
136			return V_008F0C_BUF_NUM_FORMAT_UINT;
137		else
138			return V_008F0C_BUF_NUM_FORMAT_USCALED;
139		break;
140	case VK_FORMAT_TYPE_FLOAT:
141	default:
142		return V_008F0C_BUF_NUM_FORMAT_FLOAT;
143	}
144}
145
146uint32_t radv_translate_tex_dataformat(VkFormat format,
147				       const struct vk_format_description *desc,
148				       int first_non_void)
149{
150	bool uniform = true;
151	int i;
152
153	assert(vk_format_get_plane_count(format) == 1);
154
155	if (!desc)
156		return ~0;
157	/* Colorspace (return non-RGB formats directly). */
158	switch (desc->colorspace) {
159		/* Depth stencil formats */
160	case VK_FORMAT_COLORSPACE_ZS:
161		switch (format) {
162		case VK_FORMAT_D16_UNORM:
163			return V_008F14_IMG_DATA_FORMAT_16;
164		case VK_FORMAT_D24_UNORM_S8_UINT:
165		case VK_FORMAT_X8_D24_UNORM_PACK32:
166			return V_008F14_IMG_DATA_FORMAT_8_24;
167		case VK_FORMAT_S8_UINT:
168			return V_008F14_IMG_DATA_FORMAT_8;
169		case VK_FORMAT_D32_SFLOAT:
170			return V_008F14_IMG_DATA_FORMAT_32;
171		case VK_FORMAT_D32_SFLOAT_S8_UINT:
172			return V_008F14_IMG_DATA_FORMAT_X24_8_32;
173		default:
174			goto out_unknown;
175		}
176
177	case VK_FORMAT_COLORSPACE_YUV:
178		goto out_unknown; /* TODO */
179
180	case VK_FORMAT_COLORSPACE_SRGB:
181		if (desc->nr_channels != 4 && desc->nr_channels != 1)
182			goto out_unknown;
183		break;
184
185	default:
186		break;
187	}
188
189	if (desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
190		switch(format) {
191		/* Don't ask me why this looks inverted. PAL does the same. */
192		case VK_FORMAT_G8B8G8R8_422_UNORM:
193			return V_008F14_IMG_DATA_FORMAT_BG_RG;
194		case VK_FORMAT_B8G8R8G8_422_UNORM:
195			return V_008F14_IMG_DATA_FORMAT_GB_GR;
196		default:
197			goto out_unknown;
198		}
199	}
200
201	if (desc->layout == VK_FORMAT_LAYOUT_RGTC) {
202		switch(format) {
203		case VK_FORMAT_BC4_UNORM_BLOCK:
204		case VK_FORMAT_BC4_SNORM_BLOCK:
205			return V_008F14_IMG_DATA_FORMAT_BC4;
206		case VK_FORMAT_BC5_UNORM_BLOCK:
207		case VK_FORMAT_BC5_SNORM_BLOCK:
208			return V_008F14_IMG_DATA_FORMAT_BC5;
209		default:
210			break;
211		}
212	}
213
214	if (desc->layout == VK_FORMAT_LAYOUT_S3TC) {
215		switch(format) {
216		case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
217		case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
218		case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
219		case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
220			return V_008F14_IMG_DATA_FORMAT_BC1;
221		case VK_FORMAT_BC2_UNORM_BLOCK:
222		case VK_FORMAT_BC2_SRGB_BLOCK:
223			return V_008F14_IMG_DATA_FORMAT_BC2;
224		case VK_FORMAT_BC3_UNORM_BLOCK:
225		case VK_FORMAT_BC3_SRGB_BLOCK:
226			return V_008F14_IMG_DATA_FORMAT_BC3;
227		default:
228			break;
229		}
230	}
231
232	if (desc->layout == VK_FORMAT_LAYOUT_BPTC) {
233		switch(format) {
234		case VK_FORMAT_BC6H_UFLOAT_BLOCK:
235		case VK_FORMAT_BC6H_SFLOAT_BLOCK:
236			return V_008F14_IMG_DATA_FORMAT_BC6;
237		case VK_FORMAT_BC7_UNORM_BLOCK:
238		case VK_FORMAT_BC7_SRGB_BLOCK:
239			return V_008F14_IMG_DATA_FORMAT_BC7;
240		default:
241			break;
242		}
243	}
244
245	if (desc->layout == VK_FORMAT_LAYOUT_ETC) {
246		switch (format) {
247		case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
248		case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
249			return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
250		case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
251		case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
252			return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
253		case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
254		case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
255			return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
256		case VK_FORMAT_EAC_R11_UNORM_BLOCK:
257		case VK_FORMAT_EAC_R11_SNORM_BLOCK:
258			return V_008F14_IMG_DATA_FORMAT_ETC2_R;
259		case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
260		case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
261			return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
262		default:
263			break;
264		}
265	}
266
267	if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
268		return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
269	} else if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
270		return V_008F14_IMG_DATA_FORMAT_10_11_11;
271	}
272
273	/* R8G8Bx_SNORM - TODO CxV8U8 */
274
275	/* hw cannot support mixed formats (except depth/stencil, since only
276	 * depth is read).*/
277	if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
278		goto out_unknown;
279
280	/* See whether the components are of the same size. */
281	for (i = 1; i < desc->nr_channels; i++) {
282		uniform = uniform && desc->channel[0].size == desc->channel[i].size;
283	}
284
285	/* Non-uniform formats. */
286	if (!uniform) {
287		switch(desc->nr_channels) {
288		case 3:
289			if (desc->channel[0].size == 5 &&
290			    desc->channel[1].size == 6 &&
291			    desc->channel[2].size == 5) {
292				return V_008F14_IMG_DATA_FORMAT_5_6_5;
293			}
294			goto out_unknown;
295		case 4:
296			if (desc->channel[0].size == 5 &&
297			    desc->channel[1].size == 5 &&
298			    desc->channel[2].size == 5 &&
299			    desc->channel[3].size == 1) {
300				return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
301			}
302			if (desc->channel[0].size == 1 &&
303			    desc->channel[1].size == 5 &&
304			    desc->channel[2].size == 5 &&
305			    desc->channel[3].size == 5) {
306				return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
307			}
308			if (desc->channel[0].size == 10 &&
309			    desc->channel[1].size == 10 &&
310			    desc->channel[2].size == 10 &&
311			    desc->channel[3].size == 2) {
312				/* Closed VK driver does this also no 2/10/10/10 snorm */
313				if (desc->channel[0].type == VK_FORMAT_TYPE_SIGNED &&
314				    desc->channel[0].normalized)
315					goto out_unknown;
316				return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
317			}
318			goto out_unknown;
319		}
320		goto out_unknown;
321	}
322
323	if (first_non_void < 0 || first_non_void > 3)
324		goto out_unknown;
325
326	/* uniform formats */
327	switch (desc->channel[first_non_void].size) {
328	case 4:
329		switch (desc->nr_channels) {
330#if 0 /* Not supported for render targets */
331		case 2:
332			return V_008F14_IMG_DATA_FORMAT_4_4;
333#endif
334		case 4:
335			return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
336		}
337		break;
338	case 8:
339		switch (desc->nr_channels) {
340		case 1:
341			return V_008F14_IMG_DATA_FORMAT_8;
342		case 2:
343			return V_008F14_IMG_DATA_FORMAT_8_8;
344		case 4:
345			return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
346		}
347		break;
348	case 16:
349		switch (desc->nr_channels) {
350		case 1:
351			return V_008F14_IMG_DATA_FORMAT_16;
352		case 2:
353			return V_008F14_IMG_DATA_FORMAT_16_16;
354		case 4:
355			return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
356		}
357		break;
358	case 32:
359		switch (desc->nr_channels) {
360		case 1:
361			return V_008F14_IMG_DATA_FORMAT_32;
362		case 2:
363			return V_008F14_IMG_DATA_FORMAT_32_32;
364		case 3:
365			return V_008F14_IMG_DATA_FORMAT_32_32_32;
366		case 4:
367			return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
368		}
369	}
370
371out_unknown:
372	/* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
373	return ~0;
374}
375
376uint32_t radv_translate_tex_numformat(VkFormat format,
377				      const struct vk_format_description *desc,
378				      int first_non_void)
379{
380	assert(vk_format_get_plane_count(format) == 1);
381
382	switch (format) {
383	case VK_FORMAT_D24_UNORM_S8_UINT:
384		return V_008F14_IMG_NUM_FORMAT_UNORM;
385	default:
386		if (first_non_void < 0) {
387			if (vk_format_is_compressed(format)) {
388				switch (format) {
389				case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
390				case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
391				case VK_FORMAT_BC2_SRGB_BLOCK:
392				case VK_FORMAT_BC3_SRGB_BLOCK:
393				case VK_FORMAT_BC7_SRGB_BLOCK:
394				case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
395				case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
396				case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
397					return V_008F14_IMG_NUM_FORMAT_SRGB;
398				case VK_FORMAT_BC4_SNORM_BLOCK:
399				case VK_FORMAT_BC5_SNORM_BLOCK:
400			        case VK_FORMAT_BC6H_SFLOAT_BLOCK:
401				case VK_FORMAT_EAC_R11_SNORM_BLOCK:
402				case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
403					return V_008F14_IMG_NUM_FORMAT_SNORM;
404				default:
405					return V_008F14_IMG_NUM_FORMAT_UNORM;
406				}
407			} else if (desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
408				return V_008F14_IMG_NUM_FORMAT_UNORM;
409			} else {
410				return V_008F14_IMG_NUM_FORMAT_FLOAT;
411			}
412		} else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
413			return V_008F14_IMG_NUM_FORMAT_SRGB;
414		} else {
415			switch (desc->channel[first_non_void].type) {
416			case VK_FORMAT_TYPE_FLOAT:
417				return V_008F14_IMG_NUM_FORMAT_FLOAT;
418			case VK_FORMAT_TYPE_SIGNED:
419				if (desc->channel[first_non_void].normalized)
420					return V_008F14_IMG_NUM_FORMAT_SNORM;
421				else if (desc->channel[first_non_void].pure_integer)
422					return V_008F14_IMG_NUM_FORMAT_SINT;
423				else
424					return V_008F14_IMG_NUM_FORMAT_SSCALED;
425			case VK_FORMAT_TYPE_UNSIGNED:
426				if (desc->channel[first_non_void].normalized)
427					return V_008F14_IMG_NUM_FORMAT_UNORM;
428				else if (desc->channel[first_non_void].pure_integer)
429					return V_008F14_IMG_NUM_FORMAT_UINT;
430				else
431					return V_008F14_IMG_NUM_FORMAT_USCALED;
432			default:
433				return V_008F14_IMG_NUM_FORMAT_UNORM;
434			}
435		}
436	}
437}
438
439uint32_t radv_translate_color_numformat(VkFormat format,
440					const struct vk_format_description *desc,
441					int first_non_void)
442{
443	unsigned ntype;
444
445	assert(vk_format_get_plane_count(format) == 1);
446
447	if (first_non_void == -1 || desc->channel[first_non_void].type == VK_FORMAT_TYPE_FLOAT)
448		ntype = V_028C70_NUMBER_FLOAT;
449	else {
450		ntype = V_028C70_NUMBER_UNORM;
451		if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB)
452			ntype = V_028C70_NUMBER_SRGB;
453		else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_SIGNED) {
454			if (desc->channel[first_non_void].pure_integer) {
455				ntype = V_028C70_NUMBER_SINT;
456			} else if (desc->channel[first_non_void].normalized) {
457				ntype = V_028C70_NUMBER_SNORM;
458			} else
459				ntype = ~0u;
460		} else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_UNSIGNED) {
461			if (desc->channel[first_non_void].pure_integer) {
462				ntype = V_028C70_NUMBER_UINT;
463			} else if (desc->channel[first_non_void].normalized) {
464				ntype = V_028C70_NUMBER_UNORM;
465			} else
466				ntype = ~0u;
467		}
468	}
469	return ntype;
470}
471
472static bool radv_is_sampler_format_supported(VkFormat format, bool *linear_sampling)
473{
474	const struct vk_format_description *desc = vk_format_description(format);
475	uint32_t num_format;
476	if (!desc || format == VK_FORMAT_UNDEFINED)
477		return false;
478	num_format = radv_translate_tex_numformat(format, desc,
479						  vk_format_get_first_non_void_channel(format));
480
481	if (num_format == V_008F14_IMG_NUM_FORMAT_USCALED ||
482	    num_format == V_008F14_IMG_NUM_FORMAT_SSCALED)
483		return false;
484
485	if (num_format == V_008F14_IMG_NUM_FORMAT_UNORM ||
486	    num_format == V_008F14_IMG_NUM_FORMAT_SNORM ||
487	    num_format == V_008F14_IMG_NUM_FORMAT_FLOAT ||
488	    num_format == V_008F14_IMG_NUM_FORMAT_SRGB)
489		*linear_sampling = true;
490	else
491		*linear_sampling = false;
492	return radv_translate_tex_dataformat(format, vk_format_description(format),
493					     vk_format_get_first_non_void_channel(format)) != ~0U;
494}
495
496
497static bool radv_is_storage_image_format_supported(struct radv_physical_device *physical_device,
498						   VkFormat format)
499{
500	const struct vk_format_description *desc = vk_format_description(format);
501	unsigned data_format, num_format;
502	if (!desc || format == VK_FORMAT_UNDEFINED)
503		return false;
504
505	data_format = radv_translate_tex_dataformat(format, desc,
506						    vk_format_get_first_non_void_channel(format));
507	num_format = radv_translate_tex_numformat(format, desc,
508						  vk_format_get_first_non_void_channel(format));
509
510	if(data_format == ~0 || num_format == ~0)
511		return false;
512
513	/* Extracted from the GCN3 ISA document. */
514	switch(num_format) {
515	case V_008F14_IMG_NUM_FORMAT_UNORM:
516	case V_008F14_IMG_NUM_FORMAT_SNORM:
517	case V_008F14_IMG_NUM_FORMAT_UINT:
518	case V_008F14_IMG_NUM_FORMAT_SINT:
519	case V_008F14_IMG_NUM_FORMAT_FLOAT:
520		break;
521	default:
522		return false;
523	}
524
525	switch(data_format) {
526	case V_008F14_IMG_DATA_FORMAT_8:
527	case V_008F14_IMG_DATA_FORMAT_16:
528	case V_008F14_IMG_DATA_FORMAT_8_8:
529	case V_008F14_IMG_DATA_FORMAT_32:
530	case V_008F14_IMG_DATA_FORMAT_16_16:
531	case V_008F14_IMG_DATA_FORMAT_10_11_11:
532	case V_008F14_IMG_DATA_FORMAT_11_11_10:
533	case V_008F14_IMG_DATA_FORMAT_10_10_10_2:
534	case V_008F14_IMG_DATA_FORMAT_2_10_10_10:
535	case V_008F14_IMG_DATA_FORMAT_8_8_8_8:
536	case V_008F14_IMG_DATA_FORMAT_32_32:
537	case V_008F14_IMG_DATA_FORMAT_16_16_16_16:
538	case V_008F14_IMG_DATA_FORMAT_32_32_32_32:
539	case V_008F14_IMG_DATA_FORMAT_5_6_5:
540	case V_008F14_IMG_DATA_FORMAT_1_5_5_5:
541	case V_008F14_IMG_DATA_FORMAT_5_5_5_1:
542	case V_008F14_IMG_DATA_FORMAT_4_4_4_4:
543		/* TODO: FMASK formats. */
544		return true;
545	default:
546		return false;
547	}
548}
549
550bool radv_is_buffer_format_supported(VkFormat format, bool *scaled)
551{
552	const struct vk_format_description *desc = vk_format_description(format);
553	unsigned data_format, num_format;
554	if (!desc || format == VK_FORMAT_UNDEFINED)
555		return false;
556
557	data_format = radv_translate_buffer_dataformat(desc,
558						       vk_format_get_first_non_void_channel(format));
559	num_format = radv_translate_buffer_numformat(desc,
560						     vk_format_get_first_non_void_channel(format));
561
562	if (scaled)
563		*scaled = (num_format == V_008F0C_BUF_NUM_FORMAT_SSCALED) || (num_format == V_008F0C_BUF_NUM_FORMAT_USCALED);
564	return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID &&
565		num_format != ~0;
566}
567
568bool radv_is_colorbuffer_format_supported(VkFormat format, bool *blendable)
569{
570	const struct vk_format_description *desc = vk_format_description(format);
571	uint32_t color_format = radv_translate_colorformat(format);
572	uint32_t color_swap = radv_translate_colorswap(format, false);
573	uint32_t color_num_format = radv_translate_color_numformat(format,
574								   desc,
575								   vk_format_get_first_non_void_channel(format));
576
577	if (color_num_format == V_028C70_NUMBER_UINT || color_num_format == V_028C70_NUMBER_SINT ||
578	    color_format == V_028C70_COLOR_8_24 || color_format == V_028C70_COLOR_24_8 ||
579	    color_format == V_028C70_COLOR_X24_8_32_FLOAT) {
580		*blendable = false;
581	} else
582		*blendable = true;
583	return color_format != V_028C70_COLOR_INVALID &&
584		color_swap != ~0U &&
585		color_num_format != ~0;
586}
587
588static bool radv_is_zs_format_supported(VkFormat format)
589{
590	return radv_translate_dbformat(format) != V_028040_Z_INVALID || format == VK_FORMAT_S8_UINT;
591}
592
593static bool radv_is_filter_minmax_format_supported(VkFormat format)
594{
595	/* From the Vulkan spec 1.1.71:
596	 *
597	 * "The following formats must support the
598	 *  VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT feature with
599	 *  VK_IMAGE_TILING_OPTIMAL, if they support
600	 *  VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT."
601	 */
602	/* TODO: enable more formats. */
603	switch (format) {
604	case VK_FORMAT_R8_UNORM:
605	case VK_FORMAT_R8_SNORM:
606	case VK_FORMAT_R16_UNORM:
607	case VK_FORMAT_R16_SNORM:
608	case VK_FORMAT_R16_SFLOAT:
609	case VK_FORMAT_R32_SFLOAT:
610	case VK_FORMAT_D16_UNORM:
611	case VK_FORMAT_X8_D24_UNORM_PACK32:
612	case VK_FORMAT_D32_SFLOAT:
613	case VK_FORMAT_D16_UNORM_S8_UINT:
614	case VK_FORMAT_D24_UNORM_S8_UINT:
615	case VK_FORMAT_D32_SFLOAT_S8_UINT:
616		return true;
617	default:
618		return false;
619	}
620}
621
622bool
623radv_device_supports_etc(struct radv_physical_device *physical_device)
624{
625	return physical_device->rad_info.family == CHIP_VEGA10 ||
626	       physical_device->rad_info.family == CHIP_RAVEN ||
627	       physical_device->rad_info.family == CHIP_STONEY;
628}
629
630static void
631radv_physical_device_get_format_properties(struct radv_physical_device *physical_device,
632					   VkFormat format,
633					   VkFormatProperties *out_properties)
634{
635	VkFormatFeatureFlags linear = 0, tiled = 0, buffer = 0;
636	const struct vk_format_description *desc = vk_format_description(format);
637	bool blendable;
638	bool scaled = false;
639	/* TODO: implement some software emulation of SUBSAMPLED formats. */
640	if (!desc || desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
641		out_properties->linearTilingFeatures = linear;
642		out_properties->optimalTilingFeatures = tiled;
643		out_properties->bufferFeatures = buffer;
644		return;
645	}
646
647	if (desc->layout == VK_FORMAT_LAYOUT_ETC &&
648	    !radv_device_supports_etc(physical_device)) {
649		out_properties->linearTilingFeatures = linear;
650		out_properties->optimalTilingFeatures = tiled;
651		out_properties->bufferFeatures = buffer;
652		return;
653	}
654
655	if (desc->layout == VK_FORMAT_LAYOUT_MULTIPLANE ||
656	    desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
657		uint32_t tiling = VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
658		                  VK_FORMAT_FEATURE_TRANSFER_DST_BIT |
659		                  VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
660		                  VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT |
661		                  VK_FORMAT_FEATURE_MIDPOINT_CHROMA_SAMPLES_BIT;
662
663		/* The subsampled formats have no support for linear filters. */
664		if (desc->layout != VK_FORMAT_LAYOUT_SUBSAMPLED) {
665			tiling |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT;
666		}
667
668		/* Fails for unknown reasons with linear tiling & subsampled formats. */
669		out_properties->linearTilingFeatures = desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED ? 0 : tiling;
670		out_properties->optimalTilingFeatures = tiling;
671		out_properties->bufferFeatures = 0;
672		return;
673	}
674
675	if (radv_is_storage_image_format_supported(physical_device, format)) {
676		tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
677		linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
678	}
679
680	if (radv_is_buffer_format_supported(format, &scaled)) {
681		buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
682		if (!scaled)
683			buffer |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
684				VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
685	}
686
687	if (vk_format_is_depth_or_stencil(format)) {
688		if (radv_is_zs_format_supported(format)) {
689			tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
690			tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
691			tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
692			         VK_FORMAT_FEATURE_BLIT_DST_BIT;
693			tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
694			         VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
695
696			if (radv_is_filter_minmax_format_supported(format))
697				 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT;
698
699			/* Don't support blitting surfaces with depth/stencil. */
700			if (vk_format_is_depth(format) && vk_format_is_stencil(format))
701				tiled &= ~VK_FORMAT_FEATURE_BLIT_DST_BIT;
702
703			/* Don't support linear depth surfaces */
704			linear = 0;
705		}
706	} else {
707		bool linear_sampling;
708		if (radv_is_sampler_format_supported(format, &linear_sampling)) {
709			linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
710				VK_FORMAT_FEATURE_BLIT_SRC_BIT;
711			tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
712				VK_FORMAT_FEATURE_BLIT_SRC_BIT;
713
714			if (radv_is_filter_minmax_format_supported(format))
715				 tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT_EXT;
716
717			if (linear_sampling) {
718				linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
719				tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
720			}
721
722			/* Don't support blitting for R32G32B32 formats. */
723			if (format == VK_FORMAT_R32G32B32_SFLOAT ||
724			    format == VK_FORMAT_R32G32B32_UINT ||
725			    format == VK_FORMAT_R32G32B32_SINT) {
726				linear &= ~VK_FORMAT_FEATURE_BLIT_SRC_BIT;
727			}
728		}
729		if (radv_is_colorbuffer_format_supported(format, &blendable)) {
730			linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
731			tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
732			if (blendable) {
733				linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
734				tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
735			}
736		}
737		if (tiled && !scaled) {
738			tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
739			         VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
740		}
741
742		/* Tiled formatting does not support NPOT pixel sizes */
743		if (!util_is_power_of_two_or_zero(vk_format_get_blocksize(format)))
744			tiled = 0;
745	}
746
747	if (linear && !scaled) {
748		linear |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
749		          VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
750	}
751
752	if (format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT) {
753		buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
754		linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
755		tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
756	}
757
758	switch(format) {
759	case VK_FORMAT_A2R10G10B10_SNORM_PACK32:
760	case VK_FORMAT_A2B10G10R10_SNORM_PACK32:
761	case VK_FORMAT_A2R10G10B10_SSCALED_PACK32:
762	case VK_FORMAT_A2B10G10R10_SSCALED_PACK32:
763	case VK_FORMAT_A2R10G10B10_SINT_PACK32:
764	case VK_FORMAT_A2B10G10R10_SINT_PACK32:
765		if (physical_device->rad_info.chip_class <= VI &&
766		    physical_device->rad_info.family != CHIP_STONEY) {
767			buffer &= ~(VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
768			            VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT);
769			linear = 0;
770			tiled = 0;
771		}
772		break;
773	default:
774		break;
775	}
776
777	out_properties->linearTilingFeatures = linear;
778	out_properties->optimalTilingFeatures = tiled;
779	out_properties->bufferFeatures = buffer;
780}
781
782uint32_t radv_translate_colorformat(VkFormat format)
783{
784	const struct vk_format_description *desc = vk_format_description(format);
785
786#define HAS_SIZE(x,y,z,w)						\
787	(desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
788         desc->channel[2].size == (z) && desc->channel[3].size == (w))
789
790	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) /* isn't plain */
791		return V_028C70_COLOR_10_11_11;
792
793	if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
794		return V_028C70_COLOR_INVALID;
795
796	/* hw cannot support mixed formats (except depth/stencil, since
797	 * stencil is not written to). */
798	if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
799		return V_028C70_COLOR_INVALID;
800
801	switch (desc->nr_channels) {
802	case 1:
803		switch (desc->channel[0].size) {
804		case 8:
805			return V_028C70_COLOR_8;
806		case 16:
807			return V_028C70_COLOR_16;
808		case 32:
809			return V_028C70_COLOR_32;
810		}
811		break;
812	case 2:
813		if (desc->channel[0].size == desc->channel[1].size) {
814			switch (desc->channel[0].size) {
815			case 8:
816				return V_028C70_COLOR_8_8;
817			case 16:
818				return V_028C70_COLOR_16_16;
819			case 32:
820				return V_028C70_COLOR_32_32;
821			}
822		} else if (HAS_SIZE(8,24,0,0)) {
823			return V_028C70_COLOR_24_8;
824		} else if (HAS_SIZE(24,8,0,0)) {
825			return V_028C70_COLOR_8_24;
826		}
827		break;
828	case 3:
829		if (HAS_SIZE(5,6,5,0)) {
830			return V_028C70_COLOR_5_6_5;
831		} else if (HAS_SIZE(32,8,24,0)) {
832			return V_028C70_COLOR_X24_8_32_FLOAT;
833		}
834		break;
835	case 4:
836		if (desc->channel[0].size == desc->channel[1].size &&
837		    desc->channel[0].size == desc->channel[2].size &&
838		    desc->channel[0].size == desc->channel[3].size) {
839			switch (desc->channel[0].size) {
840			case 4:
841				return V_028C70_COLOR_4_4_4_4;
842			case 8:
843				return V_028C70_COLOR_8_8_8_8;
844			case 16:
845				return V_028C70_COLOR_16_16_16_16;
846			case 32:
847				return V_028C70_COLOR_32_32_32_32;
848			}
849		} else if (HAS_SIZE(5,5,5,1)) {
850			return V_028C70_COLOR_1_5_5_5;
851		} else if (HAS_SIZE(1,5,5,5)) {
852			return V_028C70_COLOR_5_5_5_1;
853		} else if (HAS_SIZE(10,10,10,2)) {
854			return V_028C70_COLOR_2_10_10_10;
855		}
856		break;
857	}
858	return V_028C70_COLOR_INVALID;
859}
860
861uint32_t radv_colorformat_endian_swap(uint32_t colorformat)
862{
863	if (0/*SI_BIG_ENDIAN*/) {
864		switch(colorformat) {
865			/* 8-bit buffers. */
866		case V_028C70_COLOR_8:
867			return V_028C70_ENDIAN_NONE;
868
869			/* 16-bit buffers. */
870		case V_028C70_COLOR_5_6_5:
871		case V_028C70_COLOR_1_5_5_5:
872		case V_028C70_COLOR_4_4_4_4:
873		case V_028C70_COLOR_16:
874		case V_028C70_COLOR_8_8:
875			return V_028C70_ENDIAN_8IN16;
876
877			/* 32-bit buffers. */
878		case V_028C70_COLOR_8_8_8_8:
879		case V_028C70_COLOR_2_10_10_10:
880		case V_028C70_COLOR_8_24:
881		case V_028C70_COLOR_24_8:
882		case V_028C70_COLOR_16_16:
883			return V_028C70_ENDIAN_8IN32;
884
885			/* 64-bit buffers. */
886		case V_028C70_COLOR_16_16_16_16:
887			return V_028C70_ENDIAN_8IN16;
888
889		case V_028C70_COLOR_32_32:
890			return V_028C70_ENDIAN_8IN32;
891
892			/* 128-bit buffers. */
893		case V_028C70_COLOR_32_32_32_32:
894			return V_028C70_ENDIAN_8IN32;
895		default:
896			return V_028C70_ENDIAN_NONE; /* Unsupported. */
897		}
898	} else {
899		return V_028C70_ENDIAN_NONE;
900	}
901}
902
903uint32_t radv_translate_dbformat(VkFormat format)
904{
905	switch (format) {
906	case VK_FORMAT_D16_UNORM:
907	case VK_FORMAT_D16_UNORM_S8_UINT:
908		return V_028040_Z_16;
909	case VK_FORMAT_D32_SFLOAT:
910	case VK_FORMAT_D32_SFLOAT_S8_UINT:
911		return V_028040_Z_32_FLOAT;
912	default:
913		return V_028040_Z_INVALID;
914	}
915}
916
917unsigned radv_translate_colorswap(VkFormat format, bool do_endian_swap)
918{
919	const struct vk_format_description *desc = vk_format_description(format);
920
921#define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz)
922
923	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
924		return V_028C70_SWAP_STD;
925
926	if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
927		return ~0U;
928
929	switch (desc->nr_channels) {
930	case 1:
931		if (HAS_SWIZZLE(0,X))
932			return V_028C70_SWAP_STD; /* X___ */
933		else if (HAS_SWIZZLE(3,X))
934			return V_028C70_SWAP_ALT_REV; /* ___X */
935		break;
936	case 2:
937		if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
938		    (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
939		    (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
940			return V_028C70_SWAP_STD; /* XY__ */
941		else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
942			 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
943		         (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
944			/* YX__ */
945			return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
946		else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
947			return V_028C70_SWAP_ALT; /* X__Y */
948		else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
949			return V_028C70_SWAP_ALT_REV; /* Y__X */
950		break;
951	case 3:
952		if (HAS_SWIZZLE(0,X))
953			return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
954		else if (HAS_SWIZZLE(0,Z))
955			return V_028C70_SWAP_STD_REV; /* ZYX */
956		break;
957	case 4:
958		/* check the middle channels, the 1st and 4th channel can be NONE */
959		if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
960			return V_028C70_SWAP_STD; /* XYZW */
961		} else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
962			return V_028C70_SWAP_STD_REV; /* WZYX */
963		} else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
964			return V_028C70_SWAP_ALT; /* ZYXW */
965		} else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
966			/* YZWX */
967			if (desc->is_array)
968				return V_028C70_SWAP_ALT_REV;
969			else
970				return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
971		}
972		break;
973	}
974	return ~0U;
975}
976
977bool radv_format_pack_clear_color(VkFormat format,
978				  uint32_t clear_vals[2],
979				  VkClearColorValue *value)
980{
981	const struct vk_format_description *desc = vk_format_description(format);
982
983	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
984		clear_vals[0] = float3_to_r11g11b10f(value->float32);
985		clear_vals[1] = 0;
986		return true;
987	}
988
989	if (desc->layout != VK_FORMAT_LAYOUT_PLAIN) {
990		fprintf(stderr, "failed to fast clear for non-plain format %d\n", format);
991		return false;
992	}
993
994	if (!util_is_power_of_two_or_zero(desc->block.bits)) {
995		fprintf(stderr, "failed to fast clear for NPOT format %d\n", format);
996		return false;
997	}
998
999	if (desc->block.bits > 64) {
1000		/*
1001		 * We have a 128 bits format, check if the first 3 components are the same.
1002		 * Every elements has to be 32 bits since we don't support 64-bit formats,
1003		 * and we can skip swizzling checks as alpha always comes last for these and
1004		 * we do not care about the rest as they have to be the same.
1005		 */
1006		if (desc->channel[0].type == VK_FORMAT_TYPE_FLOAT) {
1007			if (value->float32[0] != value->float32[1] ||
1008			    value->float32[0] != value->float32[2])
1009				return false;
1010		} else {
1011			if (value->uint32[0] != value->uint32[1] ||
1012			    value->uint32[0] != value->uint32[2])
1013				return false;
1014		}
1015		clear_vals[0] = value->uint32[0];
1016		clear_vals[1] = value->uint32[3];
1017		return true;
1018	}
1019	uint64_t clear_val = 0;
1020
1021	for (unsigned c = 0; c < 4; ++c) {
1022		if (desc->swizzle[c] >= 4)
1023			continue;
1024
1025		const struct vk_format_channel_description *channel = &desc->channel[desc->swizzle[c]];
1026		assert(channel->size);
1027
1028		uint64_t v = 0;
1029		if (channel->pure_integer) {
1030			v = value->uint32[c]  & ((1ULL << channel->size) - 1);
1031		} else if (channel->normalized) {
1032			if (channel->type == VK_FORMAT_TYPE_UNSIGNED &&
1033			    desc->swizzle[c] < 3 &&
1034			    desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
1035				assert(channel->size == 8);
1036
1037				v = util_format_linear_float_to_srgb_8unorm(value->float32[c]);
1038			} else {
1039				float f = MIN2(value->float32[c], 1.0f);
1040
1041				if (channel->type == VK_FORMAT_TYPE_UNSIGNED) {
1042					f = MAX2(f, 0.0f) * ((1ULL << channel->size) - 1);
1043				} else {
1044					f = MAX2(f, -1.0f) * ((1ULL << (channel->size - 1)) - 1);
1045				}
1046
1047				/* The hardware rounds before conversion. */
1048				if (f > 0)
1049					f += 0.5f;
1050				else
1051					f -= 0.5f;
1052
1053				v = (uint64_t)f;
1054			}
1055		} else if (channel->type == VK_FORMAT_TYPE_FLOAT) {
1056			if (channel->size == 32) {
1057				memcpy(&v, &value->float32[c], 4);
1058			} else if(channel->size == 16) {
1059				v = util_float_to_half(value->float32[c]);
1060			} else {
1061				fprintf(stderr, "failed to fast clear for unhandled float size in format %d\n", format);
1062				return false;
1063			}
1064		} else {
1065			fprintf(stderr, "failed to fast clear for unhandled component type in format %d\n", format);
1066			return false;
1067		}
1068		clear_val |= (v & ((1ULL << channel->size) - 1)) << channel->shift;
1069	}
1070
1071	clear_vals[0] = clear_val;
1072	clear_vals[1] = clear_val >> 32;
1073
1074	return true;
1075}
1076
1077void radv_GetPhysicalDeviceFormatProperties(
1078	VkPhysicalDevice                            physicalDevice,
1079	VkFormat                                    format,
1080	VkFormatProperties*                         pFormatProperties)
1081{
1082	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1083
1084	radv_physical_device_get_format_properties(physical_device,
1085						   format,
1086						   pFormatProperties);
1087}
1088
1089void radv_GetPhysicalDeviceFormatProperties2(
1090	VkPhysicalDevice                            physicalDevice,
1091	VkFormat                                    format,
1092	VkFormatProperties2*                        pFormatProperties)
1093{
1094	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1095
1096	radv_physical_device_get_format_properties(physical_device,
1097						   format,
1098						   &pFormatProperties->formatProperties);
1099}
1100
1101static VkResult radv_get_image_format_properties(struct radv_physical_device *physical_device,
1102						 const VkPhysicalDeviceImageFormatInfo2 *info,
1103						 VkImageFormatProperties *pImageFormatProperties)
1104
1105{
1106	VkFormatProperties format_props;
1107	VkFormatFeatureFlags format_feature_flags;
1108	VkExtent3D maxExtent;
1109	uint32_t maxMipLevels;
1110	uint32_t maxArraySize;
1111	VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1112	const struct vk_format_description *desc = vk_format_description(info->format);
1113
1114	radv_physical_device_get_format_properties(physical_device, info->format,
1115						   &format_props);
1116	if (info->tiling == VK_IMAGE_TILING_LINEAR) {
1117		format_feature_flags = format_props.linearTilingFeatures;
1118	} else if (info->tiling == VK_IMAGE_TILING_OPTIMAL) {
1119		format_feature_flags = format_props.optimalTilingFeatures;
1120	} else {
1121		unreachable("bad VkImageTiling");
1122	}
1123
1124	if (format_feature_flags == 0)
1125		goto unsupported;
1126
1127	if (info->type != VK_IMAGE_TYPE_2D && vk_format_is_depth_or_stencil(info->format))
1128		goto unsupported;
1129
1130	switch (info->type) {
1131	default:
1132		unreachable("bad vkimage type\n");
1133	case VK_IMAGE_TYPE_1D:
1134		maxExtent.width = 16384;
1135		maxExtent.height = 1;
1136		maxExtent.depth = 1;
1137		maxMipLevels = 15; /* log2(maxWidth) + 1 */
1138		maxArraySize = 2048;
1139		break;
1140	case VK_IMAGE_TYPE_2D:
1141		maxExtent.width = 16384;
1142		maxExtent.height = 16384;
1143		maxExtent.depth = 1;
1144		maxMipLevels = 15; /* log2(maxWidth) + 1 */
1145		maxArraySize = 2048;
1146		break;
1147	case VK_IMAGE_TYPE_3D:
1148		maxExtent.width = 2048;
1149		maxExtent.height = 2048;
1150		maxExtent.depth = 2048;
1151		maxMipLevels = 12; /* log2(maxWidth) + 1 */
1152		maxArraySize = 1;
1153		break;
1154	}
1155
1156	if (desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
1157		/* Might be able to support but the entire format support is
1158		 * messy, so taking the lazy way out. */
1159		maxArraySize = 1;
1160	}
1161
1162	if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
1163	    info->type == VK_IMAGE_TYPE_2D &&
1164	    (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
1165				     VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1166	    !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)) {
1167		sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
1168	}
1169
1170	if (info->tiling == VK_IMAGE_TILING_LINEAR &&
1171	    (info->format == VK_FORMAT_R32G32B32_SFLOAT ||
1172	     info->format == VK_FORMAT_R32G32B32_SINT ||
1173	     info->format == VK_FORMAT_R32G32B32_UINT)) {
1174		/* R32G32B32 is a weird format and the driver currently only
1175		 * supports the barely minimum.
1176		 * TODO: Implement more if we really need to.
1177		 */
1178		if (info->type == VK_IMAGE_TYPE_3D)
1179			goto unsupported;
1180		maxArraySize = 1;
1181		maxMipLevels = 1;
1182	}
1183
1184
1185	/* We can't create 3d compressed 128bpp images that can be rendered to on GFX9 */
1186	if (physical_device->rad_info.chip_class >= GFX9 &&
1187	    info->type == VK_IMAGE_TYPE_3D &&
1188	    vk_format_get_blocksizebits(info->format) == 128 &&
1189	    vk_format_is_compressed(info->format) &&
1190	    (info->flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT) &&
1191	    ((info->flags & VK_IMAGE_CREATE_EXTENDED_USAGE_BIT) ||
1192	     (info->usage & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))) {
1193		goto unsupported;
1194	}
1195
1196	if (info->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
1197		if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
1198			goto unsupported;
1199		}
1200	}
1201
1202	if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1203		if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
1204			goto unsupported;
1205		}
1206	}
1207
1208	if (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
1209		if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
1210			goto unsupported;
1211		}
1212	}
1213
1214	if (info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1215		if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1216			goto unsupported;
1217		}
1218	}
1219
1220	if (info->usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) {
1221		if (!(format_feature_flags & VK_FORMAT_FEATURE_TRANSFER_SRC_BIT)) {
1222			goto unsupported;
1223		}
1224	}
1225
1226	if (info->usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT) {
1227		if (!(format_feature_flags & VK_FORMAT_FEATURE_TRANSFER_DST_BIT)) {
1228			goto unsupported;
1229		}
1230	}
1231
1232	if (info->usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) {
1233		if (!(format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
1234		                              VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))) {
1235			goto unsupported;
1236		}
1237	}
1238
1239	*pImageFormatProperties = (VkImageFormatProperties) {
1240		.maxExtent = maxExtent,
1241		.maxMipLevels = maxMipLevels,
1242		.maxArrayLayers = maxArraySize,
1243		.sampleCounts = sampleCounts,
1244
1245		/* FINISHME: Accurately calculate
1246		 * VkImageFormatProperties::maxResourceSize.
1247		 */
1248		.maxResourceSize = UINT32_MAX,
1249	};
1250
1251	return VK_SUCCESS;
1252unsupported:
1253	*pImageFormatProperties = (VkImageFormatProperties) {
1254		.maxExtent = { 0, 0, 0 },
1255		.maxMipLevels = 0,
1256		.maxArrayLayers = 0,
1257		.sampleCounts = 0,
1258		.maxResourceSize = 0,
1259	};
1260
1261	return VK_ERROR_FORMAT_NOT_SUPPORTED;
1262}
1263
1264VkResult radv_GetPhysicalDeviceImageFormatProperties(
1265	VkPhysicalDevice                            physicalDevice,
1266	VkFormat                                    format,
1267	VkImageType                                 type,
1268	VkImageTiling                               tiling,
1269	VkImageUsageFlags                           usage,
1270	VkImageCreateFlags                          createFlags,
1271	VkImageFormatProperties*                    pImageFormatProperties)
1272{
1273	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1274
1275	const VkPhysicalDeviceImageFormatInfo2 info = {
1276		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1277		.pNext = NULL,
1278		.format = format,
1279		.type = type,
1280		.tiling = tiling,
1281		.usage = usage,
1282		.flags = createFlags,
1283	};
1284
1285	return radv_get_image_format_properties(physical_device, &info,
1286						pImageFormatProperties);
1287}
1288
1289static void
1290get_external_image_format_properties(const VkPhysicalDeviceImageFormatInfo2 *pImageFormatInfo,
1291				     VkExternalMemoryHandleTypeFlagBits handleType,
1292				     VkExternalMemoryProperties *external_properties)
1293{
1294	VkExternalMemoryFeatureFlagBits flags = 0;
1295	VkExternalMemoryHandleTypeFlags export_flags = 0;
1296	VkExternalMemoryHandleTypeFlags compat_flags = 0;
1297	switch (handleType) {
1298	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1299	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1300		switch (pImageFormatInfo->type) {
1301		case VK_IMAGE_TYPE_2D:
1302			flags = VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT|VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT|VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1303			compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1304						      VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1305			break;
1306		default:
1307			break;
1308		}
1309		break;
1310	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1311		flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1312		compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1313		break;
1314	default:
1315		break;
1316	}
1317
1318	*external_properties = (VkExternalMemoryProperties) {
1319		.externalMemoryFeatures = flags,
1320		.exportFromImportedHandleTypes = export_flags,
1321		.compatibleHandleTypes = compat_flags,
1322	};
1323}
1324
1325VkResult radv_GetPhysicalDeviceImageFormatProperties2(
1326	VkPhysicalDevice                            physicalDevice,
1327	const VkPhysicalDeviceImageFormatInfo2     *base_info,
1328	VkImageFormatProperties2                   *base_props)
1329{
1330	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1331	const VkPhysicalDeviceExternalImageFormatInfo *external_info = NULL;
1332	VkExternalImageFormatProperties *external_props = NULL;
1333	VkSamplerYcbcrConversionImageFormatProperties *ycbcr_props = NULL;
1334	VkResult result;
1335
1336	result = radv_get_image_format_properties(physical_device, base_info,
1337						&base_props->imageFormatProperties);
1338	if (result != VK_SUCCESS)
1339		return result;
1340
1341	   /* Extract input structs */
1342	vk_foreach_struct_const(s, base_info->pNext) {
1343		switch (s->sType) {
1344		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO:
1345			external_info = (const void *) s;
1346			break;
1347		default:
1348			break;
1349		}
1350	}
1351
1352	/* Extract output structs */
1353	vk_foreach_struct(s, base_props->pNext) {
1354		switch (s->sType) {
1355		case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES:
1356			external_props = (void *) s;
1357			break;
1358		case VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES:
1359			ycbcr_props = (void *) s;
1360			break;
1361		default:
1362			break;
1363		}
1364	}
1365
1366	/* From the Vulkan 1.0.97 spec:
1367	 *
1368	 *    If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2 will
1369	 *    behave as if VkPhysicalDeviceExternalImageFormatInfo was not
1370	 *    present and VkExternalImageFormatProperties will be ignored.
1371	 */
1372	if (external_info && external_info->handleType != 0) {
1373		switch (external_info->handleType) {
1374		case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1375		case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1376		case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1377			get_external_image_format_properties(base_info, external_info->handleType,
1378			                                     &external_props->externalMemoryProperties);
1379			break;
1380		default:
1381			/* From the Vulkan 1.0.97 spec:
1382			 *
1383			 *    If handleType is not compatible with the [parameters] specified
1384			 *    in VkPhysicalDeviceImageFormatInfo2, then
1385			 *    vkGetPhysicalDeviceImageFormatProperties2 returns
1386			 *    VK_ERROR_FORMAT_NOT_SUPPORTED.
1387			 */
1388			result = vk_errorf(physical_device->instance, VK_ERROR_FORMAT_NOT_SUPPORTED,
1389					   "unsupported VkExternalMemoryTypeFlagBitsKHR 0x%x",
1390					   external_info->handleType);
1391			goto fail;
1392		}
1393	}
1394
1395	if (ycbcr_props) {
1396		ycbcr_props->combinedImageSamplerDescriptorCount = vk_format_get_plane_count(base_info->format);
1397	}
1398
1399	return VK_SUCCESS;
1400
1401fail:
1402	if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1403		/* From the Vulkan 1.0.97 spec:
1404		 *
1405		 *    If the combination of parameters to
1406		 *    vkGetPhysicalDeviceImageFormatProperties2 is not supported by
1407		 *    the implementation for use in vkCreateImage, then all members of
1408		 *    imageFormatProperties will be filled with zero.
1409		 */
1410		base_props->imageFormatProperties = (VkImageFormatProperties) {0};
1411	}
1412
1413	return result;
1414}
1415
1416void radv_GetPhysicalDeviceSparseImageFormatProperties(
1417	VkPhysicalDevice                            physicalDevice,
1418	VkFormat                                    format,
1419	VkImageType                                 type,
1420	uint32_t                                    samples,
1421	VkImageUsageFlags                           usage,
1422	VkImageTiling                               tiling,
1423	uint32_t*                                   pNumProperties,
1424	VkSparseImageFormatProperties*              pProperties)
1425{
1426	/* Sparse images are not yet supported. */
1427	*pNumProperties = 0;
1428}
1429
1430void radv_GetPhysicalDeviceSparseImageFormatProperties2(
1431	VkPhysicalDevice                            physicalDevice,
1432	const VkPhysicalDeviceSparseImageFormatInfo2 *pFormatInfo,
1433	uint32_t                                   *pPropertyCount,
1434	VkSparseImageFormatProperties2             *pProperties)
1435{
1436	/* Sparse images are not yet supported. */
1437	*pPropertyCount = 0;
1438}
1439
1440void radv_GetPhysicalDeviceExternalBufferProperties(
1441	VkPhysicalDevice                            physicalDevice,
1442	const VkPhysicalDeviceExternalBufferInfo    *pExternalBufferInfo,
1443	VkExternalBufferProperties                  *pExternalBufferProperties)
1444{
1445	VkExternalMemoryFeatureFlagBits flags = 0;
1446	VkExternalMemoryHandleTypeFlags export_flags = 0;
1447	VkExternalMemoryHandleTypeFlags compat_flags = 0;
1448	switch(pExternalBufferInfo->handleType) {
1449	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT:
1450	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1451		flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT |
1452		        VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1453		compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT |
1454					      VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1455		break;
1456	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT:
1457		flags = VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT;
1458		compat_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT;
1459		break;
1460	default:
1461		break;
1462	}
1463	pExternalBufferProperties->externalMemoryProperties = (VkExternalMemoryProperties) {
1464		.externalMemoryFeatures = flags,
1465		.exportFromImportedHandleTypes = export_flags,
1466		.compatibleHandleTypes = compat_flags,
1467	};
1468}
1469
1470/* DCC channel type categories within which formats can be reinterpreted
1471 * while keeping the same DCC encoding. The swizzle must also match. */
1472enum dcc_channel_type {
1473        dcc_channel_float32,
1474        dcc_channel_uint32,
1475        dcc_channel_sint32,
1476        dcc_channel_float16,
1477        dcc_channel_uint16,
1478        dcc_channel_sint16,
1479        dcc_channel_uint_10_10_10_2,
1480        dcc_channel_uint8,
1481        dcc_channel_sint8,
1482        dcc_channel_incompatible,
1483};
1484
1485/* Return the type of DCC encoding. */
1486static enum dcc_channel_type
1487radv_get_dcc_channel_type(const struct vk_format_description *desc)
1488{
1489        int i;
1490
1491        /* Find the first non-void channel. */
1492        for (i = 0; i < desc->nr_channels; i++)
1493                if (desc->channel[i].type != VK_FORMAT_TYPE_VOID)
1494                        break;
1495        if (i == desc->nr_channels)
1496                return dcc_channel_incompatible;
1497
1498        switch (desc->channel[i].size) {
1499        case 32:
1500                if (desc->channel[i].type == VK_FORMAT_TYPE_FLOAT)
1501                        return dcc_channel_float32;
1502                if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1503                        return dcc_channel_uint32;
1504                return dcc_channel_sint32;
1505        case 16:
1506                if (desc->channel[i].type == VK_FORMAT_TYPE_FLOAT)
1507                        return dcc_channel_float16;
1508                if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1509                        return dcc_channel_uint16;
1510                return dcc_channel_sint16;
1511        case 10:
1512                return dcc_channel_uint_10_10_10_2;
1513        case 8:
1514                if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1515                        return dcc_channel_uint8;
1516                return dcc_channel_sint8;
1517        default:
1518                return dcc_channel_incompatible;
1519        }
1520}
1521
1522/* Return if it's allowed to reinterpret one format as another with DCC enabled. */
1523bool radv_dcc_formats_compatible(VkFormat format1,
1524                                 VkFormat format2)
1525{
1526        const struct vk_format_description *desc1, *desc2;
1527        enum dcc_channel_type type1, type2;
1528        int i;
1529
1530        if (format1 == format2)
1531                return true;
1532
1533        desc1 = vk_format_description(format1);
1534        desc2 = vk_format_description(format2);
1535
1536        if (desc1->nr_channels != desc2->nr_channels)
1537                return false;
1538
1539        /* Swizzles must be the same. */
1540        for (i = 0; i < desc1->nr_channels; i++)
1541                if (desc1->swizzle[i] <= VK_SWIZZLE_W &&
1542                    desc2->swizzle[i] <= VK_SWIZZLE_W &&
1543                    desc1->swizzle[i] != desc2->swizzle[i])
1544                        return false;
1545
1546        type1 = radv_get_dcc_channel_type(desc1);
1547        type2 = radv_get_dcc_channel_type(desc2);
1548
1549        return type1 != dcc_channel_incompatible &&
1550               type2 != dcc_channel_incompatible &&
1551               type1 == type2;
1552}
1553
1554