• 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/u_half.h"
33 #include "util/format_srgb.h"
34 #include "util/format_r11g11b10f.h"
35 
radv_translate_buffer_dataformat(const struct vk_format_description * desc,int first_non_void)36 uint32_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 	if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
43 		return V_008F0C_BUF_DATA_FORMAT_10_11_11;
44 
45 	if (first_non_void < 0)
46 		return V_008F0C_BUF_DATA_FORMAT_INVALID;
47 	type = desc->channel[first_non_void].type;
48 
49 	if (type == VK_FORMAT_TYPE_FIXED)
50 		return V_008F0C_BUF_DATA_FORMAT_INVALID;
51 	if (desc->nr_channels == 4 &&
52 	    desc->channel[0].size == 10 &&
53 	    desc->channel[1].size == 10 &&
54 	    desc->channel[2].size == 10 &&
55 	    desc->channel[3].size == 2)
56 		return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
57 
58 	/* See whether the components are of the same size. */
59 	for (i = 0; i < desc->nr_channels; i++) {
60 		if (desc->channel[first_non_void].size != desc->channel[i].size)
61 			return V_008F0C_BUF_DATA_FORMAT_INVALID;
62 	}
63 
64 	switch (desc->channel[first_non_void].size) {
65 	case 8:
66 		switch (desc->nr_channels) {
67 		case 1:
68 			return V_008F0C_BUF_DATA_FORMAT_8;
69 		case 2:
70 			return V_008F0C_BUF_DATA_FORMAT_8_8;
71 		case 4:
72 			return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
73 		}
74 		break;
75 	case 16:
76 		switch (desc->nr_channels) {
77 		case 1:
78 			return V_008F0C_BUF_DATA_FORMAT_16;
79 		case 2:
80 			return V_008F0C_BUF_DATA_FORMAT_16_16;
81 		case 4:
82 			return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
83 		}
84 		break;
85 	case 32:
86 		/* From the Southern Islands ISA documentation about MTBUF:
87 		 * 'Memory reads of data in memory that is 32 or 64 bits do not
88 		 * undergo any format conversion.'
89 		 */
90 		if (type != VK_FORMAT_TYPE_FLOAT &&
91 		    !desc->channel[first_non_void].pure_integer)
92 			return V_008F0C_BUF_DATA_FORMAT_INVALID;
93 
94 		switch (desc->nr_channels) {
95 		case 1:
96 			return V_008F0C_BUF_DATA_FORMAT_32;
97 		case 2:
98 			return V_008F0C_BUF_DATA_FORMAT_32_32;
99 		case 3:
100 			return V_008F0C_BUF_DATA_FORMAT_32_32_32;
101 		case 4:
102 			return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
103 		}
104 		break;
105 	}
106 
107 	return V_008F0C_BUF_DATA_FORMAT_INVALID;
108 }
109 
radv_translate_buffer_numformat(const struct vk_format_description * desc,int first_non_void)110 uint32_t radv_translate_buffer_numformat(const struct vk_format_description *desc,
111 					 int first_non_void)
112 {
113 	if (desc->format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
114 		return V_008F0C_BUF_NUM_FORMAT_FLOAT;
115 
116 	if (first_non_void < 0)
117 		return ~0;
118 
119 	switch (desc->channel[first_non_void].type) {
120 	case VK_FORMAT_TYPE_SIGNED:
121 		if (desc->channel[first_non_void].normalized)
122 			return V_008F0C_BUF_NUM_FORMAT_SNORM;
123 		else if (desc->channel[first_non_void].pure_integer)
124 			return V_008F0C_BUF_NUM_FORMAT_SINT;
125 		else
126 			return V_008F0C_BUF_NUM_FORMAT_SSCALED;
127 		break;
128 	case VK_FORMAT_TYPE_UNSIGNED:
129 		if (desc->channel[first_non_void].normalized)
130 			return V_008F0C_BUF_NUM_FORMAT_UNORM;
131 		else if (desc->channel[first_non_void].pure_integer)
132 			return V_008F0C_BUF_NUM_FORMAT_UINT;
133 		else
134 			return V_008F0C_BUF_NUM_FORMAT_USCALED;
135 		break;
136 	case VK_FORMAT_TYPE_FLOAT:
137 	default:
138 		return V_008F0C_BUF_NUM_FORMAT_FLOAT;
139 	}
140 }
141 
radv_translate_tex_dataformat(VkFormat format,const struct vk_format_description * desc,int first_non_void)142 uint32_t radv_translate_tex_dataformat(VkFormat format,
143 				       const struct vk_format_description *desc,
144 				       int first_non_void)
145 {
146 	bool uniform = true;
147 	int i;
148 
149 	if (!desc)
150 		return ~0;
151 	/* Colorspace (return non-RGB formats directly). */
152 	switch (desc->colorspace) {
153 		/* Depth stencil formats */
154 	case VK_FORMAT_COLORSPACE_ZS:
155 		switch (format) {
156 		case VK_FORMAT_D16_UNORM:
157 			return V_008F14_IMG_DATA_FORMAT_16;
158 		case VK_FORMAT_D24_UNORM_S8_UINT:
159 		case VK_FORMAT_X8_D24_UNORM_PACK32:
160 			return V_008F14_IMG_DATA_FORMAT_8_24;
161 		case VK_FORMAT_S8_UINT:
162 			return V_008F14_IMG_DATA_FORMAT_8;
163 		case VK_FORMAT_D32_SFLOAT:
164 			return V_008F14_IMG_DATA_FORMAT_32;
165 		case VK_FORMAT_D32_SFLOAT_S8_UINT:
166 			return V_008F14_IMG_DATA_FORMAT_X24_8_32;
167 		default:
168 			goto out_unknown;
169 		}
170 
171 	case VK_FORMAT_COLORSPACE_YUV:
172 		goto out_unknown; /* TODO */
173 
174 	case VK_FORMAT_COLORSPACE_SRGB:
175 		if (desc->nr_channels != 4 && desc->nr_channels != 1)
176 			goto out_unknown;
177 		break;
178 
179 	default:
180 		break;
181 	}
182 
183 	if (desc->layout == VK_FORMAT_LAYOUT_RGTC) {
184 		switch(format) {
185 		case VK_FORMAT_BC4_UNORM_BLOCK:
186 		case VK_FORMAT_BC4_SNORM_BLOCK:
187 			return V_008F14_IMG_DATA_FORMAT_BC4;
188 		case VK_FORMAT_BC5_UNORM_BLOCK:
189 		case VK_FORMAT_BC5_SNORM_BLOCK:
190 			return V_008F14_IMG_DATA_FORMAT_BC5;
191 		default:
192 			break;
193 		}
194 	}
195 
196 	if (desc->layout == VK_FORMAT_LAYOUT_S3TC) {
197 		switch(format) {
198 		case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
199 		case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
200 		case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
201 		case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
202 			return V_008F14_IMG_DATA_FORMAT_BC1;
203 		case VK_FORMAT_BC2_UNORM_BLOCK:
204 		case VK_FORMAT_BC2_SRGB_BLOCK:
205 			return V_008F14_IMG_DATA_FORMAT_BC2;
206 		case VK_FORMAT_BC3_UNORM_BLOCK:
207 		case VK_FORMAT_BC3_SRGB_BLOCK:
208 			return V_008F14_IMG_DATA_FORMAT_BC3;
209 		default:
210 			break;
211 		}
212 	}
213 
214 	if (desc->layout == VK_FORMAT_LAYOUT_BPTC) {
215 		switch(format) {
216 		case VK_FORMAT_BC6H_UFLOAT_BLOCK:
217 		case VK_FORMAT_BC6H_SFLOAT_BLOCK:
218 			return V_008F14_IMG_DATA_FORMAT_BC6;
219 		case VK_FORMAT_BC7_UNORM_BLOCK:
220 		case VK_FORMAT_BC7_SRGB_BLOCK:
221 			return V_008F14_IMG_DATA_FORMAT_BC7;
222 		default:
223 			break;
224 		}
225 	}
226 
227 	if (format == VK_FORMAT_E5B9G9R9_UFLOAT_PACK32) {
228 		return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
229 	} else if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) {
230 		return V_008F14_IMG_DATA_FORMAT_10_11_11;
231 	}
232 
233 	/* R8G8Bx_SNORM - TODO CxV8U8 */
234 
235 	/* hw cannot support mixed formats (except depth/stencil, since only
236 	 * depth is read).*/
237 	if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
238 		goto out_unknown;
239 
240 	/* See whether the components are of the same size. */
241 	for (i = 1; i < desc->nr_channels; i++) {
242 		uniform = uniform && desc->channel[0].size == desc->channel[i].size;
243 	}
244 
245 	/* Non-uniform formats. */
246 	if (!uniform) {
247 		switch(desc->nr_channels) {
248 		case 3:
249 			if (desc->channel[0].size == 5 &&
250 			    desc->channel[1].size == 6 &&
251 			    desc->channel[2].size == 5) {
252 				return V_008F14_IMG_DATA_FORMAT_5_6_5;
253 			}
254 			goto out_unknown;
255 		case 4:
256 			if (desc->channel[0].size == 5 &&
257 			    desc->channel[1].size == 5 &&
258 			    desc->channel[2].size == 5 &&
259 			    desc->channel[3].size == 1) {
260 				return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
261 			}
262 			if (desc->channel[0].size == 1 &&
263 			    desc->channel[1].size == 5 &&
264 			    desc->channel[2].size == 5 &&
265 			    desc->channel[3].size == 5) {
266 				return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
267 			}
268 			if (desc->channel[0].size == 10 &&
269 			    desc->channel[1].size == 10 &&
270 			    desc->channel[2].size == 10 &&
271 			    desc->channel[3].size == 2) {
272 				/* Closed VK driver does this also no 2/10/10/10 snorm */
273 				if (desc->channel[0].type == VK_FORMAT_TYPE_SIGNED &&
274 				    desc->channel[0].normalized)
275 					goto out_unknown;
276 				return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
277 			}
278 			goto out_unknown;
279 		}
280 		goto out_unknown;
281 	}
282 
283 	if (first_non_void < 0 || first_non_void > 3)
284 		goto out_unknown;
285 
286 	/* uniform formats */
287 	switch (desc->channel[first_non_void].size) {
288 	case 4:
289 		switch (desc->nr_channels) {
290 #if 0 /* Not supported for render targets */
291 		case 2:
292 			return V_008F14_IMG_DATA_FORMAT_4_4;
293 #endif
294 		case 4:
295 			return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
296 		}
297 		break;
298 	case 8:
299 		switch (desc->nr_channels) {
300 		case 1:
301 			return V_008F14_IMG_DATA_FORMAT_8;
302 		case 2:
303 			return V_008F14_IMG_DATA_FORMAT_8_8;
304 		case 4:
305 			return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
306 		}
307 		break;
308 	case 16:
309 		switch (desc->nr_channels) {
310 		case 1:
311 			return V_008F14_IMG_DATA_FORMAT_16;
312 		case 2:
313 			return V_008F14_IMG_DATA_FORMAT_16_16;
314 		case 4:
315 			return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
316 		}
317 		break;
318 	case 32:
319 		switch (desc->nr_channels) {
320 		case 1:
321 			return V_008F14_IMG_DATA_FORMAT_32;
322 		case 2:
323 			return V_008F14_IMG_DATA_FORMAT_32_32;
324 #if 0 /* Not supported for render targets */
325 		case 3:
326 			return V_008F14_IMG_DATA_FORMAT_32_32_32;
327 #endif
328 		case 4:
329 			return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
330 		}
331 	}
332 
333 out_unknown:
334 	/* R600_ERR("Unable to handle texformat %d %s\n", format, vk_format_name(format)); */
335 	return ~0;
336 }
337 
radv_translate_tex_numformat(VkFormat format,const struct vk_format_description * desc,int first_non_void)338 uint32_t radv_translate_tex_numformat(VkFormat format,
339 				      const struct vk_format_description *desc,
340 				      int first_non_void)
341 {
342 	switch (format) {
343 	case VK_FORMAT_D24_UNORM_S8_UINT:
344 		return V_008F14_IMG_NUM_FORMAT_UNORM;
345 	default:
346 		if (first_non_void < 0) {
347 			if (vk_format_is_compressed(format)) {
348 				switch (format) {
349 				case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
350 				case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
351 				case VK_FORMAT_BC2_SRGB_BLOCK:
352 				case VK_FORMAT_BC3_SRGB_BLOCK:
353 				case VK_FORMAT_BC7_SRGB_BLOCK:
354 					return V_008F14_IMG_NUM_FORMAT_SRGB;
355 				case VK_FORMAT_BC4_SNORM_BLOCK:
356 				case VK_FORMAT_BC5_SNORM_BLOCK:
357 			        case VK_FORMAT_BC6H_SFLOAT_BLOCK:
358 					return V_008F14_IMG_NUM_FORMAT_SNORM;
359 				default:
360 					return V_008F14_IMG_NUM_FORMAT_UNORM;
361 				}
362 			} else if (desc->layout == VK_FORMAT_LAYOUT_SUBSAMPLED) {
363 				return V_008F14_IMG_NUM_FORMAT_UNORM;
364 			} else {
365 				return V_008F14_IMG_NUM_FORMAT_FLOAT;
366 			}
367 		} else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
368 			return V_008F14_IMG_NUM_FORMAT_SRGB;
369 		} else {
370 			switch (desc->channel[first_non_void].type) {
371 			case VK_FORMAT_TYPE_FLOAT:
372 				return V_008F14_IMG_NUM_FORMAT_FLOAT;
373 			case VK_FORMAT_TYPE_SIGNED:
374 				if (desc->channel[first_non_void].normalized)
375 					return V_008F14_IMG_NUM_FORMAT_SNORM;
376 				else if (desc->channel[first_non_void].pure_integer)
377 					return V_008F14_IMG_NUM_FORMAT_SINT;
378 				else
379 					return V_008F14_IMG_NUM_FORMAT_SSCALED;
380 			case VK_FORMAT_TYPE_UNSIGNED:
381 				if (desc->channel[first_non_void].normalized)
382 					return V_008F14_IMG_NUM_FORMAT_UNORM;
383 				else if (desc->channel[first_non_void].pure_integer)
384 					return V_008F14_IMG_NUM_FORMAT_UINT;
385 				else
386 					return V_008F14_IMG_NUM_FORMAT_USCALED;
387 			default:
388 				return V_008F14_IMG_NUM_FORMAT_UNORM;
389 			}
390 		}
391 	}
392 }
393 
radv_translate_color_numformat(VkFormat format,const struct vk_format_description * desc,int first_non_void)394 uint32_t radv_translate_color_numformat(VkFormat format,
395 					const struct vk_format_description *desc,
396 					int first_non_void)
397 {
398 	unsigned ntype;
399 	if (first_non_void == -1 || desc->channel[first_non_void].type == VK_FORMAT_TYPE_FLOAT)
400 		ntype = V_028C70_NUMBER_FLOAT;
401 	else {
402 		ntype = V_028C70_NUMBER_UNORM;
403 		if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB)
404 			ntype = V_028C70_NUMBER_SRGB;
405 		else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_SIGNED) {
406 			if (desc->channel[first_non_void].pure_integer) {
407 				ntype = V_028C70_NUMBER_SINT;
408 			} else if (desc->channel[first_non_void].normalized) {
409 				ntype = V_028C70_NUMBER_SNORM;
410 			} else
411 				ntype = ~0u;
412 		} else if (desc->channel[first_non_void].type == VK_FORMAT_TYPE_UNSIGNED) {
413 			if (desc->channel[first_non_void].pure_integer) {
414 				ntype = V_028C70_NUMBER_UINT;
415 			} else if (desc->channel[first_non_void].normalized) {
416 				ntype = V_028C70_NUMBER_UNORM;
417 			} else
418 				ntype = ~0u;
419 		}
420 	}
421 	return ntype;
422 }
423 
radv_is_sampler_format_supported(VkFormat format,bool * linear_sampling)424 static bool radv_is_sampler_format_supported(VkFormat format, bool *linear_sampling)
425 {
426 	const struct vk_format_description *desc = vk_format_description(format);
427 	uint32_t num_format;
428 	if (!desc || format == VK_FORMAT_UNDEFINED)
429 		return false;
430 	num_format = radv_translate_tex_numformat(format, desc,
431 						  vk_format_get_first_non_void_channel(format));
432 
433 	if (num_format == V_008F14_IMG_NUM_FORMAT_USCALED ||
434 	    num_format == V_008F14_IMG_NUM_FORMAT_SSCALED)
435 		return false;
436 
437 	if (num_format == V_008F14_IMG_NUM_FORMAT_UNORM ||
438 	    num_format == V_008F14_IMG_NUM_FORMAT_SNORM ||
439 	    num_format == V_008F14_IMG_NUM_FORMAT_FLOAT ||
440 	    num_format == V_008F14_IMG_NUM_FORMAT_SRGB)
441 		*linear_sampling = true;
442 	else
443 		*linear_sampling = false;
444 	return radv_translate_tex_dataformat(format, vk_format_description(format),
445 					     vk_format_get_first_non_void_channel(format)) != ~0U;
446 }
447 
448 
radv_is_storage_image_format_supported(struct radv_physical_device * physical_device,VkFormat format)449 static bool radv_is_storage_image_format_supported(struct radv_physical_device *physical_device,
450 						   VkFormat format)
451 {
452 	const struct vk_format_description *desc = vk_format_description(format);
453 	unsigned data_format, num_format;
454 	if (!desc || format == VK_FORMAT_UNDEFINED)
455 		return false;
456 
457 	data_format = radv_translate_tex_dataformat(format, desc,
458 						    vk_format_get_first_non_void_channel(format));
459 	num_format = radv_translate_tex_numformat(format, desc,
460 						  vk_format_get_first_non_void_channel(format));
461 
462 	if(data_format == ~0 || num_format == ~0)
463 		return false;
464 
465 	/* Extracted from the GCN3 ISA document. */
466 	switch(num_format) {
467 	case V_008F14_IMG_NUM_FORMAT_UNORM:
468 	case V_008F14_IMG_NUM_FORMAT_SNORM:
469 	case V_008F14_IMG_NUM_FORMAT_UINT:
470 	case V_008F14_IMG_NUM_FORMAT_SINT:
471 	case V_008F14_IMG_NUM_FORMAT_FLOAT:
472 		break;
473 	default:
474 		return false;
475 	}
476 
477 	switch(data_format) {
478 	case V_008F14_IMG_DATA_FORMAT_8:
479 	case V_008F14_IMG_DATA_FORMAT_16:
480 	case V_008F14_IMG_DATA_FORMAT_8_8:
481 	case V_008F14_IMG_DATA_FORMAT_32:
482 	case V_008F14_IMG_DATA_FORMAT_16_16:
483 	case V_008F14_IMG_DATA_FORMAT_10_11_11:
484 	case V_008F14_IMG_DATA_FORMAT_11_11_10:
485 	case V_008F14_IMG_DATA_FORMAT_10_10_10_2:
486 	case V_008F14_IMG_DATA_FORMAT_2_10_10_10:
487 	case V_008F14_IMG_DATA_FORMAT_8_8_8_8:
488 	case V_008F14_IMG_DATA_FORMAT_32_32:
489 	case V_008F14_IMG_DATA_FORMAT_16_16_16_16:
490 	case V_008F14_IMG_DATA_FORMAT_32_32_32_32:
491 	case V_008F14_IMG_DATA_FORMAT_5_6_5:
492 	case V_008F14_IMG_DATA_FORMAT_1_5_5_5:
493 	case V_008F14_IMG_DATA_FORMAT_5_5_5_1:
494 	case V_008F14_IMG_DATA_FORMAT_4_4_4_4:
495 		/* TODO: FMASK formats. */
496 		return true;
497 	default:
498 		return false;
499 	}
500 }
501 
radv_is_buffer_format_supported(VkFormat format,bool * scaled)502 static bool radv_is_buffer_format_supported(VkFormat format, bool *scaled)
503 {
504 	const struct vk_format_description *desc = vk_format_description(format);
505 	unsigned data_format, num_format;
506 	if (!desc || format == VK_FORMAT_UNDEFINED)
507 		return false;
508 
509 	data_format = radv_translate_buffer_dataformat(desc,
510 						       vk_format_get_first_non_void_channel(format));
511 	num_format = radv_translate_buffer_numformat(desc,
512 						     vk_format_get_first_non_void_channel(format));
513 
514 	*scaled = (num_format == V_008F0C_BUF_NUM_FORMAT_SSCALED) || (num_format == V_008F0C_BUF_NUM_FORMAT_USCALED);
515 	return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID &&
516 		num_format != ~0;
517 }
518 
radv_is_colorbuffer_format_supported(VkFormat format,bool * blendable)519 bool radv_is_colorbuffer_format_supported(VkFormat format, bool *blendable)
520 {
521 	const struct vk_format_description *desc = vk_format_description(format);
522 	uint32_t color_format = radv_translate_colorformat(format);
523 	uint32_t color_swap = radv_translate_colorswap(format, false);
524 	uint32_t color_num_format = radv_translate_color_numformat(format,
525 								   desc,
526 								   vk_format_get_first_non_void_channel(format));
527 
528 	if (color_num_format == V_028C70_NUMBER_UINT || color_num_format == V_028C70_NUMBER_SINT ||
529 	    color_format == V_028C70_COLOR_8_24 || color_format == V_028C70_COLOR_24_8 ||
530 	    color_format == V_028C70_COLOR_X24_8_32_FLOAT) {
531 		*blendable = false;
532 	} else
533 		*blendable = true;
534 	return color_format != V_028C70_COLOR_INVALID &&
535 		color_swap != ~0U &&
536 		color_num_format != ~0;
537 }
538 
radv_is_zs_format_supported(VkFormat format)539 static bool radv_is_zs_format_supported(VkFormat format)
540 {
541 	return radv_translate_dbformat(format) != V_028040_Z_INVALID || format == VK_FORMAT_S8_UINT;
542 }
543 
544 static void
radv_physical_device_get_format_properties(struct radv_physical_device * physical_device,VkFormat format,VkFormatProperties * out_properties)545 radv_physical_device_get_format_properties(struct radv_physical_device *physical_device,
546 					   VkFormat format,
547 					   VkFormatProperties *out_properties)
548 {
549 	VkFormatFeatureFlags linear = 0, tiled = 0, buffer = 0;
550 	const struct vk_format_description *desc = vk_format_description(format);
551 	bool blendable;
552 	bool scaled = false;
553 	if (!desc) {
554 		out_properties->linearTilingFeatures = linear;
555 		out_properties->optimalTilingFeatures = tiled;
556 		out_properties->bufferFeatures = buffer;
557 		return;
558 	}
559 
560 	if (radv_is_storage_image_format_supported(physical_device, format)) {
561 		tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
562 		linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
563 	}
564 
565 	if (radv_is_buffer_format_supported(format, &scaled)) {
566 		buffer |= VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
567 		if (!scaled)
568 			buffer |= VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT |
569 				VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
570 	}
571 
572 	if (vk_format_is_depth_or_stencil(format)) {
573 		if (radv_is_zs_format_supported(format)) {
574 			tiled |= VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
575 			tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
576 			tiled |= VK_FORMAT_FEATURE_BLIT_SRC_BIT |
577 			         VK_FORMAT_FEATURE_BLIT_DST_BIT;
578 			tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR |
579 			         VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR;
580 
581 			/* GFX9 doesn't support linear depth surfaces */
582 			if (physical_device->rad_info.chip_class >= GFX9)
583 				linear = 0;
584 		}
585 	} else {
586 		bool linear_sampling;
587 		if (radv_is_sampler_format_supported(format, &linear_sampling)) {
588 			linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
589 				VK_FORMAT_FEATURE_BLIT_SRC_BIT;
590 			tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT |
591 				VK_FORMAT_FEATURE_BLIT_SRC_BIT;
592 			if (linear_sampling) {
593 				linear |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
594 				tiled |= VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
595 			}
596 		}
597 		if (radv_is_colorbuffer_format_supported(format, &blendable)) {
598 			linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
599 			tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
600 			if (blendable) {
601 				linear |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
602 				tiled |= VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
603 			}
604 		}
605 		if (tiled && util_is_power_of_two(vk_format_get_blocksize(format)) && !scaled) {
606 			tiled |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR |
607 			         VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR;
608 		}
609 	}
610 
611 	if (linear && util_is_power_of_two(vk_format_get_blocksize(format)) && !scaled) {
612 		linear |= VK_FORMAT_FEATURE_TRANSFER_SRC_BIT_KHR |
613 		          VK_FORMAT_FEATURE_TRANSFER_DST_BIT_KHR;
614 	}
615 
616 	if (format == VK_FORMAT_R32_UINT || format == VK_FORMAT_R32_SINT) {
617 		buffer |= VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
618 		linear |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
619 		tiled |= VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
620 	}
621 
622 	out_properties->linearTilingFeatures = linear;
623 	out_properties->optimalTilingFeatures = tiled;
624 	out_properties->bufferFeatures = buffer;
625 }
626 
radv_translate_colorformat(VkFormat format)627 uint32_t radv_translate_colorformat(VkFormat format)
628 {
629 	const struct vk_format_description *desc = vk_format_description(format);
630 
631 #define HAS_SIZE(x,y,z,w)						\
632 	(desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
633          desc->channel[2].size == (z) && desc->channel[3].size == (w))
634 
635 	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32) /* isn't plain */
636 		return V_028C70_COLOR_10_11_11;
637 
638 	if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
639 		return V_028C70_COLOR_INVALID;
640 
641 	/* hw cannot support mixed formats (except depth/stencil, since
642 	 * stencil is not written to). */
643 	if (desc->is_mixed && desc->colorspace != VK_FORMAT_COLORSPACE_ZS)
644 		return V_028C70_COLOR_INVALID;
645 
646 	switch (desc->nr_channels) {
647 	case 1:
648 		switch (desc->channel[0].size) {
649 		case 8:
650 			return V_028C70_COLOR_8;
651 		case 16:
652 			return V_028C70_COLOR_16;
653 		case 32:
654 			return V_028C70_COLOR_32;
655 		}
656 		break;
657 	case 2:
658 		if (desc->channel[0].size == desc->channel[1].size) {
659 			switch (desc->channel[0].size) {
660 			case 8:
661 				return V_028C70_COLOR_8_8;
662 			case 16:
663 				return V_028C70_COLOR_16_16;
664 			case 32:
665 				return V_028C70_COLOR_32_32;
666 			}
667 		} else if (HAS_SIZE(8,24,0,0)) {
668 			return V_028C70_COLOR_24_8;
669 		} else if (HAS_SIZE(24,8,0,0)) {
670 			return V_028C70_COLOR_8_24;
671 		}
672 		break;
673 	case 3:
674 		if (HAS_SIZE(5,6,5,0)) {
675 			return V_028C70_COLOR_5_6_5;
676 		} else if (HAS_SIZE(32,8,24,0)) {
677 			return V_028C70_COLOR_X24_8_32_FLOAT;
678 		}
679 		break;
680 	case 4:
681 		if (desc->channel[0].size == desc->channel[1].size &&
682 		    desc->channel[0].size == desc->channel[2].size &&
683 		    desc->channel[0].size == desc->channel[3].size) {
684 			switch (desc->channel[0].size) {
685 			case 4:
686 				return V_028C70_COLOR_4_4_4_4;
687 			case 8:
688 				return V_028C70_COLOR_8_8_8_8;
689 			case 16:
690 				return V_028C70_COLOR_16_16_16_16;
691 			case 32:
692 				return V_028C70_COLOR_32_32_32_32;
693 			}
694 		} else if (HAS_SIZE(5,5,5,1)) {
695 			return V_028C70_COLOR_1_5_5_5;
696 		} else if (HAS_SIZE(1,5,5,5)) {
697 			return V_028C70_COLOR_5_5_5_1;
698 		} else if (HAS_SIZE(10,10,10,2)) {
699 			return V_028C70_COLOR_2_10_10_10;
700 		}
701 		break;
702 	}
703 	return V_028C70_COLOR_INVALID;
704 }
705 
radv_colorformat_endian_swap(uint32_t colorformat)706 uint32_t radv_colorformat_endian_swap(uint32_t colorformat)
707 {
708 	if (0/*SI_BIG_ENDIAN*/) {
709 		switch(colorformat) {
710 			/* 8-bit buffers. */
711 		case V_028C70_COLOR_8:
712 			return V_028C70_ENDIAN_NONE;
713 
714 			/* 16-bit buffers. */
715 		case V_028C70_COLOR_5_6_5:
716 		case V_028C70_COLOR_1_5_5_5:
717 		case V_028C70_COLOR_4_4_4_4:
718 		case V_028C70_COLOR_16:
719 		case V_028C70_COLOR_8_8:
720 			return V_028C70_ENDIAN_8IN16;
721 
722 			/* 32-bit buffers. */
723 		case V_028C70_COLOR_8_8_8_8:
724 		case V_028C70_COLOR_2_10_10_10:
725 		case V_028C70_COLOR_8_24:
726 		case V_028C70_COLOR_24_8:
727 		case V_028C70_COLOR_16_16:
728 			return V_028C70_ENDIAN_8IN32;
729 
730 			/* 64-bit buffers. */
731 		case V_028C70_COLOR_16_16_16_16:
732 			return V_028C70_ENDIAN_8IN16;
733 
734 		case V_028C70_COLOR_32_32:
735 			return V_028C70_ENDIAN_8IN32;
736 
737 			/* 128-bit buffers. */
738 		case V_028C70_COLOR_32_32_32_32:
739 			return V_028C70_ENDIAN_8IN32;
740 		default:
741 			return V_028C70_ENDIAN_NONE; /* Unsupported. */
742 		}
743 	} else {
744 		return V_028C70_ENDIAN_NONE;
745 	}
746 }
747 
radv_translate_dbformat(VkFormat format)748 uint32_t radv_translate_dbformat(VkFormat format)
749 {
750 	switch (format) {
751 	case VK_FORMAT_D16_UNORM:
752 	case VK_FORMAT_D16_UNORM_S8_UINT:
753 		return V_028040_Z_16;
754 	case VK_FORMAT_D32_SFLOAT:
755 	case VK_FORMAT_D32_SFLOAT_S8_UINT:
756 		return V_028040_Z_32_FLOAT;
757 	default:
758 		return V_028040_Z_INVALID;
759 	}
760 }
761 
radv_translate_colorswap(VkFormat format,bool do_endian_swap)762 unsigned radv_translate_colorswap(VkFormat format, bool do_endian_swap)
763 {
764 	const struct vk_format_description *desc = vk_format_description(format);
765 
766 #define HAS_SWIZZLE(chan,swz) (desc->swizzle[chan] == VK_SWIZZLE_##swz)
767 
768 	if (format == VK_FORMAT_B10G11R11_UFLOAT_PACK32)
769 		return V_028C70_SWAP_STD;
770 
771 	if (desc->layout != VK_FORMAT_LAYOUT_PLAIN)
772 		return ~0U;
773 
774 	switch (desc->nr_channels) {
775 	case 1:
776 		if (HAS_SWIZZLE(0,X))
777 			return V_028C70_SWAP_STD; /* X___ */
778 		else if (HAS_SWIZZLE(3,X))
779 			return V_028C70_SWAP_ALT_REV; /* ___X */
780 		break;
781 	case 2:
782 		if ((HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,Y)) ||
783 		    (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(1,NONE)) ||
784 		    (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,Y)))
785 			return V_028C70_SWAP_STD; /* XY__ */
786 		else if ((HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,X)) ||
787 			 (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(1,NONE)) ||
788 		         (HAS_SWIZZLE(0,NONE) && HAS_SWIZZLE(1,X)))
789 			/* YX__ */
790 			return (do_endian_swap ? V_028C70_SWAP_STD : V_028C70_SWAP_STD_REV);
791 		else if (HAS_SWIZZLE(0,X) && HAS_SWIZZLE(3,Y))
792 			return V_028C70_SWAP_ALT; /* X__Y */
793 		else if (HAS_SWIZZLE(0,Y) && HAS_SWIZZLE(3,X))
794 			return V_028C70_SWAP_ALT_REV; /* Y__X */
795 		break;
796 	case 3:
797 		if (HAS_SWIZZLE(0,X))
798 			return (do_endian_swap ? V_028C70_SWAP_STD_REV : V_028C70_SWAP_STD);
799 		else if (HAS_SWIZZLE(0,Z))
800 			return V_028C70_SWAP_STD_REV; /* ZYX */
801 		break;
802 	case 4:
803 		/* check the middle channels, the 1st and 4th channel can be NONE */
804 		if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,Z)) {
805 			return V_028C70_SWAP_STD; /* XYZW */
806 		} else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,Y)) {
807 			return V_028C70_SWAP_STD_REV; /* WZYX */
808 		} else if (HAS_SWIZZLE(1,Y) && HAS_SWIZZLE(2,X)) {
809 			return V_028C70_SWAP_ALT; /* ZYXW */
810 		} else if (HAS_SWIZZLE(1,Z) && HAS_SWIZZLE(2,W)) {
811 			/* YZWX */
812 			if (desc->is_array)
813 				return V_028C70_SWAP_ALT_REV;
814 			else
815 				return (do_endian_swap ? V_028C70_SWAP_ALT : V_028C70_SWAP_ALT_REV);
816 		}
817 		break;
818 	}
819 	return ~0U;
820 }
821 
radv_format_pack_clear_color(VkFormat format,uint32_t clear_vals[2],VkClearColorValue * value)822 bool radv_format_pack_clear_color(VkFormat format,
823 				  uint32_t clear_vals[2],
824 				  VkClearColorValue *value)
825 {
826 	uint8_t r = 0, g = 0, b = 0, a = 0;
827 	const struct vk_format_description *desc = vk_format_description(format);
828 
829 	if (vk_format_get_component_bits(format, VK_FORMAT_COLORSPACE_RGB, 0) <= 8) {
830 		if (desc->colorspace == VK_FORMAT_COLORSPACE_RGB) {
831 			r = float_to_ubyte(value->float32[0]);
832 			g = float_to_ubyte(value->float32[1]);
833 			b = float_to_ubyte(value->float32[2]);
834 			a = float_to_ubyte(value->float32[3]);
835 		} else if (desc->colorspace == VK_FORMAT_COLORSPACE_SRGB) {
836 			r = util_format_linear_float_to_srgb_8unorm(value->float32[0]);
837 			g = util_format_linear_float_to_srgb_8unorm(value->float32[1]);
838 			b = util_format_linear_float_to_srgb_8unorm(value->float32[2]);
839 			a = float_to_ubyte(value->float32[3]);
840 		}
841 	}
842 	switch (format) {
843 	case VK_FORMAT_R8_UNORM:
844 	case VK_FORMAT_R8_SRGB:
845 		clear_vals[0] = r;
846 		clear_vals[1] = 0;
847 		break;
848 	case VK_FORMAT_R8G8_UNORM:
849 	case VK_FORMAT_R8G8_SRGB:
850 		clear_vals[0] = r | g << 8;
851 		clear_vals[1] = 0;
852 		break;
853 	case VK_FORMAT_R8G8B8A8_SRGB:
854 	case VK_FORMAT_R8G8B8A8_UNORM:
855 		clear_vals[0] = r | g << 8 | b << 16 | a << 24;
856 		clear_vals[1] = 0;
857 		break;
858 	case VK_FORMAT_B8G8R8A8_SRGB:
859 	case VK_FORMAT_B8G8R8A8_UNORM:
860 		clear_vals[0] = b | g << 8 | r << 16 | a << 24;
861 		clear_vals[1] = 0;
862 		break;
863 	case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
864 	case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
865 		clear_vals[0] = r | g << 8 | b << 16 | a << 24;
866 		clear_vals[1] = 0;
867 		break;
868 	case VK_FORMAT_R8_UINT:
869 		clear_vals[0] = value->uint32[0] & 0xff;
870 		clear_vals[1] = 0;
871 		break;
872 	case VK_FORMAT_R8_SINT:
873 		clear_vals[0] = value->int32[0] & 0xff;
874 		clear_vals[1] = 0;
875 		break;
876 	case VK_FORMAT_R16_UINT:
877 		clear_vals[0] = value->uint32[0] & 0xffff;
878 		clear_vals[1] = 0;
879 		break;
880 	case VK_FORMAT_R8G8_UINT:
881 		clear_vals[0] = value->uint32[0] & 0xff;
882 		clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
883 		clear_vals[1] = 0;
884 		break;
885 	case VK_FORMAT_R8G8_SINT:
886 		clear_vals[0] = value->int32[0] & 0xff;
887 		clear_vals[0] |= (value->int32[1] & 0xff) << 8;
888 		clear_vals[1] = 0;
889 		break;
890 	case VK_FORMAT_R8G8B8A8_UINT:
891 		clear_vals[0] = value->uint32[0] & 0xff;
892 		clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
893 		clear_vals[0] |= (value->uint32[2] & 0xff) << 16;
894 		clear_vals[0] |= (value->uint32[3] & 0xff) << 24;
895 		clear_vals[1] = 0;
896 		break;
897 	case VK_FORMAT_R8G8B8A8_SINT:
898 		clear_vals[0] = value->int32[0] & 0xff;
899 		clear_vals[0] |= (value->int32[1] & 0xff) << 8;
900 		clear_vals[0] |= (value->int32[2] & 0xff) << 16;
901 		clear_vals[0] |= (value->int32[3] & 0xff) << 24;
902 		clear_vals[1] = 0;
903 		break;
904 	case VK_FORMAT_A8B8G8R8_UINT_PACK32:
905 		clear_vals[0] = value->uint32[0] & 0xff;
906 		clear_vals[0] |= (value->uint32[1] & 0xff) << 8;
907 		clear_vals[0] |= (value->uint32[2] & 0xff) << 16;
908 		clear_vals[0] |= (value->uint32[3] & 0xff) << 24;
909 		clear_vals[1] = 0;
910 		break;
911 	case VK_FORMAT_R16G16_UINT:
912 		clear_vals[0] = value->uint32[0] & 0xffff;
913 		clear_vals[0] |= (value->uint32[1] & 0xffff) << 16;
914 		clear_vals[1] = 0;
915 		break;
916 	case VK_FORMAT_R16G16B16A16_UINT:
917 		clear_vals[0] = value->uint32[0] & 0xffff;
918 		clear_vals[0] |= (value->uint32[1] & 0xffff) << 16;
919 		clear_vals[1] = value->uint32[2] & 0xffff;
920 		clear_vals[1] |= (value->uint32[3] & 0xffff) << 16;
921 		break;
922 	case VK_FORMAT_R32_UINT:
923 		clear_vals[0] = value->uint32[0];
924 		clear_vals[1] = 0;
925 		break;
926 	case VK_FORMAT_R32G32_UINT:
927 		clear_vals[0] = value->uint32[0];
928 		clear_vals[1] = value->uint32[1];
929 		break;
930 	case VK_FORMAT_R32_SINT:
931 		clear_vals[0] = value->int32[0];
932 		clear_vals[1] = 0;
933 		break;
934 	case VK_FORMAT_R16_SFLOAT:
935 		clear_vals[0] = util_float_to_half(value->float32[0]);
936 		clear_vals[1] = 0;
937 		break;
938 	case VK_FORMAT_R16G16_SFLOAT:
939 		clear_vals[0] = util_float_to_half(value->float32[0]);
940 		clear_vals[0] |= (uint32_t)util_float_to_half(value->float32[1]) << 16;
941 		clear_vals[1] = 0;
942 		break;
943 	case VK_FORMAT_R16G16B16A16_SFLOAT:
944 		clear_vals[0] = util_float_to_half(value->float32[0]);
945 		clear_vals[0] |= (uint32_t)util_float_to_half(value->float32[1]) << 16;
946 		clear_vals[1] = util_float_to_half(value->float32[2]);
947 		clear_vals[1] |= (uint32_t)util_float_to_half(value->float32[3]) << 16;
948 		break;
949 	case VK_FORMAT_R16_UNORM:
950 		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
951 		clear_vals[1] = 0;
952 		break;
953 	case VK_FORMAT_R16G16_UNORM:
954 		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
955 		clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0xffff)) << 16;
956 		clear_vals[1] = 0;
957 		break;
958 	case VK_FORMAT_R16G16B16A16_UNORM:
959 		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0xffff)) & 0xffff;
960 		clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0xffff)) << 16;
961 		clear_vals[1] = ((uint16_t)util_iround(CLAMP(value->float32[2], 0.0f, 1.0f) * 0xffff)) & 0xffff;
962 		clear_vals[1] |= ((uint16_t)util_iround(CLAMP(value->float32[3], 0.0f, 1.0f) * 0xffff)) << 16;
963 		break;
964 	case VK_FORMAT_R16G16B16A16_SNORM:
965 		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], -1.0f, 1.0f) * 0x7fff)) & 0xffff;
966 		clear_vals[0] |= ((uint16_t)util_iround(CLAMP(value->float32[1], -1.0f, 1.0f) * 0x7fff)) << 16;
967 		clear_vals[1] = ((uint16_t)util_iround(CLAMP(value->float32[2], -1.0f, 1.0f) * 0x7fff)) & 0xffff;
968 		clear_vals[1] |= ((uint16_t)util_iround(CLAMP(value->float32[3], -1.0f, 1.0f) * 0x7fff)) << 16;
969 		break;
970 	case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
971 		clear_vals[0] = ((uint16_t)util_iround(CLAMP(value->float32[0], 0.0f, 1.0f) * 0x3ff)) & 0x3ff;
972 		clear_vals[0] |= (((uint16_t)util_iround(CLAMP(value->float32[1], 0.0f, 1.0f) * 0x3ff)) & 0x3ff) << 10;
973 		clear_vals[0] |= (((uint16_t)util_iround(CLAMP(value->float32[2], 0.0f, 1.0f) * 0x3ff)) & 0x3ff) << 20;
974 		clear_vals[0] |= (((uint16_t)util_iround(CLAMP(value->float32[3], 0.0f, 1.0f) * 0x3)) & 0x3) << 30;
975 		clear_vals[1] = 0;
976 		return true;
977 	case VK_FORMAT_R32G32_SFLOAT:
978 		clear_vals[0] = fui(value->float32[0]);
979 		clear_vals[1] = fui(value->float32[1]);
980 		break;
981 	case VK_FORMAT_R32_SFLOAT:
982 		clear_vals[1] = 0;
983 		clear_vals[0] = fui(value->float32[0]);
984 		break;
985 	case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
986 		clear_vals[0] = float3_to_r11g11b10f(value->float32);
987 		clear_vals[1] = 0;
988 		break;
989 	case VK_FORMAT_R32G32B32A32_SFLOAT:
990 		if (value->float32[0] != value->float32[1] ||
991 		    value->float32[0] != value->float32[2])
992 			return false;
993 		clear_vals[0] = fui(value->float32[0]);
994 		clear_vals[1] = fui(value->float32[3]);
995 		break;
996 	case VK_FORMAT_R32G32B32A32_UINT:
997 		if (value->uint32[0] != value->uint32[1] ||
998 		    value->uint32[0] != value->uint32[2])
999 			return false;
1000 		clear_vals[0] = value->uint32[0];
1001 		clear_vals[1] = value->uint32[3];
1002 		break;
1003 	case VK_FORMAT_R32G32B32A32_SINT:
1004 		if (value->int32[0] != value->int32[1] ||
1005 		    value->int32[0] != value->int32[2])
1006 			return false;
1007 		clear_vals[0] = value->int32[0];
1008 		clear_vals[1] = value->int32[3];
1009 		break;
1010 	default:
1011 		fprintf(stderr, "failed to fast clear %d\n", format);
1012 		return false;
1013 	}
1014 	return true;
1015 }
1016 
radv_GetPhysicalDeviceFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties * pFormatProperties)1017 void radv_GetPhysicalDeviceFormatProperties(
1018 	VkPhysicalDevice                            physicalDevice,
1019 	VkFormat                                    format,
1020 	VkFormatProperties*                         pFormatProperties)
1021 {
1022 	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1023 
1024 	radv_physical_device_get_format_properties(physical_device,
1025 						   format,
1026 						   pFormatProperties);
1027 }
1028 
radv_GetPhysicalDeviceFormatProperties2KHR(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties2KHR * pFormatProperties)1029 void radv_GetPhysicalDeviceFormatProperties2KHR(
1030 	VkPhysicalDevice                            physicalDevice,
1031 	VkFormat                                    format,
1032 	VkFormatProperties2KHR*                         pFormatProperties)
1033 {
1034 	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1035 
1036 	radv_physical_device_get_format_properties(physical_device,
1037 						   format,
1038 						   &pFormatProperties->formatProperties);
1039 }
1040 
radv_get_image_format_properties(struct radv_physical_device * physical_device,const VkPhysicalDeviceImageFormatInfo2KHR * info,VkImageFormatProperties * pImageFormatProperties)1041 static VkResult radv_get_image_format_properties(struct radv_physical_device *physical_device,
1042 						 const VkPhysicalDeviceImageFormatInfo2KHR *info,
1043 						 VkImageFormatProperties *pImageFormatProperties)
1044 
1045 {
1046 	VkFormatProperties format_props;
1047 	VkFormatFeatureFlags format_feature_flags;
1048 	VkExtent3D maxExtent;
1049 	uint32_t maxMipLevels;
1050 	uint32_t maxArraySize;
1051 	VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT;
1052 
1053 	radv_physical_device_get_format_properties(physical_device, info->format,
1054 						   &format_props);
1055 	if (info->tiling == VK_IMAGE_TILING_LINEAR) {
1056 		format_feature_flags = format_props.linearTilingFeatures;
1057 	} else if (info->tiling == VK_IMAGE_TILING_OPTIMAL) {
1058 		format_feature_flags = format_props.optimalTilingFeatures;
1059 	} else {
1060 		unreachable("bad VkImageTiling");
1061 	}
1062 
1063 	if (format_feature_flags == 0)
1064 		goto unsupported;
1065 
1066 	if (info->type != VK_IMAGE_TYPE_2D && vk_format_is_depth_or_stencil(info->format))
1067 		goto unsupported;
1068 
1069 	switch (info->type) {
1070 	default:
1071 		unreachable("bad vkimage type\n");
1072 	case VK_IMAGE_TYPE_1D:
1073 		maxExtent.width = 16384;
1074 		maxExtent.height = 1;
1075 		maxExtent.depth = 1;
1076 		maxMipLevels = 15; /* log2(maxWidth) + 1 */
1077 		maxArraySize = 2048;
1078 		break;
1079 	case VK_IMAGE_TYPE_2D:
1080 		maxExtent.width = 16384;
1081 		maxExtent.height = 16384;
1082 		maxExtent.depth = 1;
1083 		maxMipLevels = 15; /* log2(maxWidth) + 1 */
1084 		maxArraySize = 2048;
1085 		break;
1086 	case VK_IMAGE_TYPE_3D:
1087 		maxExtent.width = 2048;
1088 		maxExtent.height = 2048;
1089 		maxExtent.depth = 2048;
1090 		maxMipLevels = 12; /* log2(maxWidth) + 1 */
1091 		maxArraySize = 1;
1092 		break;
1093 	}
1094 
1095 	if (info->tiling == VK_IMAGE_TILING_OPTIMAL &&
1096 	    info->type == VK_IMAGE_TYPE_2D &&
1097 	    (format_feature_flags & (VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT |
1098 				     VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) &&
1099 	    !(info->flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) &&
1100 	    !(info->usage & VK_IMAGE_USAGE_STORAGE_BIT)) {
1101 		sampleCounts |= VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT | VK_SAMPLE_COUNT_8_BIT;
1102 	}
1103 
1104 	if (info->usage & VK_IMAGE_USAGE_SAMPLED_BIT) {
1105 		if (!(format_feature_flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) {
1106 			goto unsupported;
1107 		}
1108 	}
1109 
1110 	if (info->usage & VK_IMAGE_USAGE_STORAGE_BIT) {
1111 		if (!(format_feature_flags & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) {
1112 			goto unsupported;
1113 		}
1114 	}
1115 
1116 	if (info->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
1117 		if (!(format_feature_flags & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT)) {
1118 			goto unsupported;
1119 		}
1120 	}
1121 
1122 	if (info->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
1123 		if (!(format_feature_flags & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
1124 			goto unsupported;
1125 		}
1126 	}
1127 
1128 	*pImageFormatProperties = (VkImageFormatProperties) {
1129 		.maxExtent = maxExtent,
1130 		.maxMipLevels = maxMipLevels,
1131 		.maxArrayLayers = maxArraySize,
1132 		.sampleCounts = sampleCounts,
1133 
1134 		/* FINISHME: Accurately calculate
1135 		 * VkImageFormatProperties::maxResourceSize.
1136 		 */
1137 		.maxResourceSize = UINT32_MAX,
1138 	};
1139 
1140 	return VK_SUCCESS;
1141 unsupported:
1142 	*pImageFormatProperties = (VkImageFormatProperties) {
1143 		.maxExtent = { 0, 0, 0 },
1144 		.maxMipLevels = 0,
1145 		.maxArrayLayers = 0,
1146 		.sampleCounts = 0,
1147 		.maxResourceSize = 0,
1148 	};
1149 
1150 	return VK_ERROR_FORMAT_NOT_SUPPORTED;
1151 }
1152 
radv_GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,VkImageTiling tiling,VkImageUsageFlags usage,VkImageCreateFlags createFlags,VkImageFormatProperties * pImageFormatProperties)1153 VkResult radv_GetPhysicalDeviceImageFormatProperties(
1154 	VkPhysicalDevice                            physicalDevice,
1155 	VkFormat                                    format,
1156 	VkImageType                                 type,
1157 	VkImageTiling                               tiling,
1158 	VkImageUsageFlags                           usage,
1159 	VkImageCreateFlags                          createFlags,
1160 	VkImageFormatProperties*                    pImageFormatProperties)
1161 {
1162 	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1163 
1164 	const VkPhysicalDeviceImageFormatInfo2KHR info = {
1165 		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2_KHR,
1166 		.pNext = NULL,
1167 		.format = format,
1168 		.type = type,
1169 		.tiling = tiling,
1170 		.usage = usage,
1171 		.flags = createFlags,
1172 	};
1173 
1174 	return radv_get_image_format_properties(physical_device, &info,
1175 						pImageFormatProperties);
1176 }
1177 
1178 static void
get_external_image_format_properties(const VkPhysicalDeviceImageFormatInfo2KHR * pImageFormatInfo,VkExternalMemoryPropertiesKHR * external_properties)1179 get_external_image_format_properties(const VkPhysicalDeviceImageFormatInfo2KHR *pImageFormatInfo,
1180 				     VkExternalMemoryPropertiesKHR *external_properties)
1181 {
1182 	VkExternalMemoryFeatureFlagBitsKHR flags = 0;
1183 	VkExternalMemoryHandleTypeFlagsKHR export_flags = 0;
1184 	VkExternalMemoryHandleTypeFlagsKHR compat_flags = 0;
1185 	switch (pImageFormatInfo->type) {
1186 	case VK_IMAGE_TYPE_2D:
1187 		flags = VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT_KHR|VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT_KHR|VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT_KHR;
1188 		compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR |
1189 					      VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1190 		break;
1191 	default:
1192 		break;
1193 	}
1194 
1195 	*external_properties = (VkExternalMemoryPropertiesKHR) {
1196 		.externalMemoryFeatures = flags,
1197 		.exportFromImportedHandleTypes = export_flags,
1198 		.compatibleHandleTypes = compat_flags,
1199 	};
1200 }
1201 
radv_GetPhysicalDeviceImageFormatProperties2KHR(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2KHR * base_info,VkImageFormatProperties2KHR * base_props)1202 VkResult radv_GetPhysicalDeviceImageFormatProperties2KHR(
1203 	VkPhysicalDevice                            physicalDevice,
1204 	const VkPhysicalDeviceImageFormatInfo2KHR  *base_info,
1205 	VkImageFormatProperties2KHR                *base_props)
1206 {
1207 	RADV_FROM_HANDLE(radv_physical_device, physical_device, physicalDevice);
1208 	const VkPhysicalDeviceExternalImageFormatInfoKHR *external_info = NULL;
1209 	VkExternalImageFormatPropertiesKHR *external_props = NULL;
1210 	VkResult result;
1211 
1212 	result = radv_get_image_format_properties(physical_device, base_info,
1213 						&base_props->imageFormatProperties);
1214 	if (result != VK_SUCCESS)
1215 		return result;
1216 
1217 	   /* Extract input structs */
1218 	vk_foreach_struct_const(s, base_info->pNext) {
1219 		switch (s->sType) {
1220 		case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO_KHR:
1221 			external_info = (const void *) s;
1222 			break;
1223 		default:
1224 			break;
1225 		}
1226 	}
1227 
1228 	/* Extract output structs */
1229 	vk_foreach_struct(s, base_props->pNext) {
1230 		switch (s->sType) {
1231 		case VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHR:
1232 			external_props = (void *) s;
1233 			break;
1234 		default:
1235 			break;
1236 		}
1237 	}
1238 
1239 	/* From the Vulkan 1.0.42 spec:
1240 	 *
1241 	 *    If handleType is 0, vkGetPhysicalDeviceImageFormatProperties2KHR will
1242 	 *    behave as if VkPhysicalDeviceExternalImageFormatInfoKHR was not
1243 	 *    present and VkExternalImageFormatPropertiesKHR will be ignored.
1244 	 */
1245 	if (external_info && external_info->handleType != 0) {
1246 		switch (external_info->handleType) {
1247 		case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
1248 		case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1249 			get_external_image_format_properties(base_info, &external_props->externalMemoryProperties);
1250 			break;
1251 		default:
1252 			/* From the Vulkan 1.0.42 spec:
1253 			 *
1254 			 *    If handleType is not compatible with the [parameters] specified
1255 			 *    in VkPhysicalDeviceImageFormatInfo2KHR, then
1256 			 *    vkGetPhysicalDeviceImageFormatProperties2KHR returns
1257 			 *    VK_ERROR_FORMAT_NOT_SUPPORTED.
1258 			 */
1259 			result = vk_errorf(VK_ERROR_FORMAT_NOT_SUPPORTED,
1260 					   "unsupported VkExternalMemoryTypeFlagBitsKHR 0x%x",
1261 					   external_info->handleType);
1262 			goto fail;
1263 		}
1264 	}
1265 
1266 	return VK_SUCCESS;
1267 
1268 fail:
1269 	if (result == VK_ERROR_FORMAT_NOT_SUPPORTED) {
1270 		/* From the Vulkan 1.0.42 spec:
1271 		 *
1272 		 *    If the combination of parameters to
1273 		 *    vkGetPhysicalDeviceImageFormatProperties2KHR is not supported by
1274 		 *    the implementation for use in vkCreateImage, then all members of
1275 		 *    imageFormatProperties will be filled with zero.
1276 		 */
1277 		base_props->imageFormatProperties = (VkImageFormatProperties) {0};
1278 	}
1279 
1280 	return result;
1281 }
1282 
radv_GetPhysicalDeviceSparseImageFormatProperties(VkPhysicalDevice physicalDevice,VkFormat format,VkImageType type,uint32_t samples,VkImageUsageFlags usage,VkImageTiling tiling,uint32_t * pNumProperties,VkSparseImageFormatProperties * pProperties)1283 void radv_GetPhysicalDeviceSparseImageFormatProperties(
1284 	VkPhysicalDevice                            physicalDevice,
1285 	VkFormat                                    format,
1286 	VkImageType                                 type,
1287 	uint32_t                                    samples,
1288 	VkImageUsageFlags                           usage,
1289 	VkImageTiling                               tiling,
1290 	uint32_t*                                   pNumProperties,
1291 	VkSparseImageFormatProperties*              pProperties)
1292 {
1293 	/* Sparse images are not yet supported. */
1294 	*pNumProperties = 0;
1295 }
1296 
radv_GetPhysicalDeviceSparseImageFormatProperties2KHR(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2KHR * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2KHR * pProperties)1297 void radv_GetPhysicalDeviceSparseImageFormatProperties2KHR(
1298 	VkPhysicalDevice                            physicalDevice,
1299 	const VkPhysicalDeviceSparseImageFormatInfo2KHR* pFormatInfo,
1300 	uint32_t                                   *pPropertyCount,
1301 	VkSparseImageFormatProperties2KHR*          pProperties)
1302 {
1303 	/* Sparse images are not yet supported. */
1304 	*pPropertyCount = 0;
1305 }
1306 
radv_GetPhysicalDeviceExternalBufferPropertiesKHR(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfoKHR * pExternalBufferInfo,VkExternalBufferPropertiesKHR * pExternalBufferProperties)1307 void radv_GetPhysicalDeviceExternalBufferPropertiesKHR(
1308 	VkPhysicalDevice                            physicalDevice,
1309 	const VkPhysicalDeviceExternalBufferInfoKHR *pExternalBufferInfo,
1310 	VkExternalBufferPropertiesKHR               *pExternalBufferProperties)
1311 {
1312 	VkExternalMemoryFeatureFlagBitsKHR flags = 0;
1313 	VkExternalMemoryHandleTypeFlagsKHR export_flags = 0;
1314 	VkExternalMemoryHandleTypeFlagsKHR compat_flags = 0;
1315 	switch(pExternalBufferInfo->handleType) {
1316 	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR:
1317 	case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
1318 		flags = VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT_KHR |
1319 		        VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT_KHR;
1320 		compat_flags = export_flags = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR |
1321 					      VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1322 		break;
1323 	default:
1324 		break;
1325 	}
1326 	pExternalBufferProperties->externalMemoryProperties = (VkExternalMemoryPropertiesKHR) {
1327 		.externalMemoryFeatures = flags,
1328 		.exportFromImportedHandleTypes = export_flags,
1329 		.compatibleHandleTypes = compat_flags,
1330 	};
1331 }
1332 
1333 /* DCC channel type categories within which formats can be reinterpreted
1334  * while keeping the same DCC encoding. The swizzle must also match. */
1335 enum dcc_channel_type {
1336         dcc_channel_float32,
1337         dcc_channel_uint32,
1338         dcc_channel_sint32,
1339         dcc_channel_float16,
1340         dcc_channel_uint16,
1341         dcc_channel_sint16,
1342         dcc_channel_uint_10_10_10_2,
1343         dcc_channel_uint8,
1344         dcc_channel_sint8,
1345         dcc_channel_incompatible,
1346 };
1347 
1348 /* Return the type of DCC encoding. */
1349 static enum dcc_channel_type
radv_get_dcc_channel_type(const struct vk_format_description * desc)1350 radv_get_dcc_channel_type(const struct vk_format_description *desc)
1351 {
1352         int i;
1353 
1354         /* Find the first non-void channel. */
1355         for (i = 0; i < desc->nr_channels; i++)
1356                 if (desc->channel[i].type != VK_FORMAT_TYPE_VOID)
1357                         break;
1358         if (i == desc->nr_channels)
1359                 return dcc_channel_incompatible;
1360 
1361         switch (desc->channel[i].size) {
1362         case 32:
1363                 if (desc->channel[i].type == VK_FORMAT_TYPE_FLOAT)
1364                         return dcc_channel_float32;
1365                 if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1366                         return dcc_channel_uint32;
1367                 return dcc_channel_sint32;
1368         case 16:
1369                 if (desc->channel[i].type == VK_FORMAT_TYPE_FLOAT)
1370                         return dcc_channel_float16;
1371                 if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1372                         return dcc_channel_uint16;
1373                 return dcc_channel_sint16;
1374         case 10:
1375                 return dcc_channel_uint_10_10_10_2;
1376         case 8:
1377                 if (desc->channel[i].type == VK_FORMAT_TYPE_UNSIGNED)
1378                         return dcc_channel_uint8;
1379                 return dcc_channel_sint8;
1380         default:
1381                 return dcc_channel_incompatible;
1382         }
1383 }
1384 
1385 /* Return if it's allowed to reinterpret one format as another with DCC enabled. */
radv_dcc_formats_compatible(VkFormat format1,VkFormat format2)1386 bool radv_dcc_formats_compatible(VkFormat format1,
1387                                  VkFormat format2)
1388 {
1389         const struct vk_format_description *desc1, *desc2;
1390         enum dcc_channel_type type1, type2;
1391         int i;
1392 
1393         if (format1 == format2)
1394                 return true;
1395 
1396         desc1 = vk_format_description(format1);
1397         desc2 = vk_format_description(format2);
1398 
1399         if (desc1->nr_channels != desc2->nr_channels)
1400                 return false;
1401 
1402         /* Swizzles must be the same. */
1403         for (i = 0; i < desc1->nr_channels; i++)
1404                 if (desc1->swizzle[i] <= VK_SWIZZLE_W &&
1405                     desc2->swizzle[i] <= VK_SWIZZLE_W &&
1406                     desc1->swizzle[i] != desc2->swizzle[i])
1407                         return false;
1408 
1409         type1 = radv_get_dcc_channel_type(desc1);
1410         type2 = radv_get_dcc_channel_type(desc2);
1411 
1412         return type1 != dcc_channel_incompatible &&
1413                type2 != dcc_channel_incompatible &&
1414                type1 == type2;
1415 }
1416 
1417