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