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