Loading...
Searching...
No Matches
TextureFormat.hpp
1#pragma once
2#include <QtGui/private/qrhi_p.h>
3#include <avnd/concepts/gfx.hpp>
4
5#include <string_view>
6#include <type_traits>
7
8namespace gpp::qrhi
9{
10template <typename F>
11void toTextureFormat(QRhiTexture::Format fmt, F& tex)
12{
13 switch(fmt)
14 {
15 case QRhiTexture::Format::RGBA8:
16 if constexpr(requires { F::RGBA; })
17 tex.format = F::RGBA;
18 else if constexpr(requires { F::RGBA8; })
19 tex.format = F::RGBA8;
20 else
21 tex.format = {};
22 break;
23
24 case QRhiTexture::Format::BGRA8:
25 if constexpr(requires { F::BGRA; })
26 tex.format = F::BGRA;
27 else if constexpr(requires { F::BGRA8; })
28 tex.format = F::BGRA8;
29 else
30 tex.format = {};
31 break;
32 case QRhiTexture::Format::RGBA16F:
33 if constexpr(requires { F::RGBA16F; })
34 tex.format = F::RGBA16F;
35 else
36 tex.format = {};
37 break;
38 case QRhiTexture::Format::RGBA32F:
39 if constexpr(requires { F::RGBA32F; })
40 tex.format = F::RGBA32F;
41 else
42 tex.format = {};
43 break;
44
45 case QRhiTexture::Format::R8:
46 if constexpr(requires { F::R8; })
47 tex.format = F::R8;
48 else
49 tex.format = {};
50 break;
51 case QRhiTexture::Format::RED_OR_ALPHA8:
52 if constexpr(requires { F::RED_OR_ALPHA8; })
53 tex.format = F::RED_OR_ALPHA8;
54 else if constexpr(requires { F::R8; })
55 tex.format = F::R8;
56 else
57 tex.format = {};
58 break;
59
60 case QRhiTexture::Format::R16:
61 if constexpr(requires { F::R16; })
62 tex.format = F::R16;
63 else
64 tex.format = {};
65 break;
66 case QRhiTexture::Format::R16F:
67 if constexpr(requires { F::R16F; })
68 tex.format = F::R16F;
69 else
70 tex.format = {};
71 break;
72 case QRhiTexture::Format::R32F:
73 if constexpr(requires { F::R32F; })
74 tex.format = F::R32F;
75 else
76 tex.format = {};
77 break;
78
79#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
80 case QRhiTexture::RG8:
81 if constexpr(requires { F::RG8; })
82 tex.format = F::RG8;
83 else
84 tex.format = {};
85 break;
86 case QRhiTexture::RG16:
87 if constexpr(requires { F::RG16; })
88 tex.format = F::RG16;
89 else
90 tex.format = {};
91 break;
92 case QRhiTexture::RGB10A2:
93 if constexpr(requires { F::RGB10A2; })
94 tex.format = F::RGB10A2;
95 else
96 tex.format = {};
97 break;
98 case QRhiTexture::Format::D24:
99 if constexpr(requires { F::D24; })
100 tex.format = F::D24;
101 else
102 tex.format = {};
103 break;
104 case QRhiTexture::Format::D24S8:
105 if constexpr(requires { F::D24S8; })
106 tex.format = F::D24S8;
107 else
108 tex.format = {};
109 break;
110#endif
111
112#if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0)
113 case QRhiTexture::Format::R8UI:
114 if constexpr(requires { F::R8UI; })
115 tex.format = F::R8UI;
116 else if constexpr(requires { F::R8; })
117 tex.format = F::R8;
118 else
119 tex.format = {};
120 break;
121 case QRhiTexture::Format::R32UI:
122 if constexpr(requires { F::R32UI; })
123 tex.format = F::R32UI;
124 else
125 tex.format = {};
126 break;
127 case QRhiTexture::RG32UI:
128 if constexpr(requires { F::RG32UI; })
129 tex.format = F::RG32UI;
130 else
131 tex.format = {};
132 break;
133 case QRhiTexture::RGBA32UI:
134 if constexpr(requires { F::RGBA32UI; })
135 tex.format = F::RGBA32UI;
136 else
137 tex.format = {};
138 break;
139 case QRhiTexture::Format::D32FS8:
140 if constexpr(requires { F::D32FS8; })
141 tex.format = F::D32FS8;
142 else
143 tex.format = {};
144 break;
145#endif
146
147#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
148 case QRhiTexture::Format::R8SI:
149 if constexpr(requires { F::R8SI; })
150 tex.format = F::R8SI;
151 else if constexpr(requires { F::R8; })
152 tex.format = F::R8;
153 else
154 tex.format = {};
155 break;
156 case QRhiTexture::Format::R32SI:
157 if constexpr(requires { F::R32SI; })
158 tex.format = F::R32SI;
159 else
160 tex.format = {};
161 break;
162 case QRhiTexture::RG32SI:
163 if constexpr(requires { F::RG32SI; })
164 tex.format = F::RG32SI;
165 else
166 tex.format = {};
167 break;
168 case QRhiTexture::RGBA32SI:
169 if constexpr(requires { F::RGBA32SI; })
170 tex.format = F::RGBA32SI;
171 else
172 tex.format = {};
173 break;
174#endif
175
176 case QRhiTexture::Format::D16:
177 if constexpr(requires { F::D16; })
178 tex.format = F::D16;
179 else if constexpr(requires { F::R16; })
180 tex.format = F::R16;
181 else
182 tex.format = {};
183 break;
184 case QRhiTexture::Format::D32F:
185 if constexpr(requires { F::D32F; })
186 tex.format = F::D32F;
187 else if constexpr(requires { F::R32F; })
188 tex.format = F::R32F;
189 return;
190 case QRhiTexture::UnknownFormat:
191 default:
192 break;
193 }
194}
195template <typename F>
196 requires std::is_enum_v<F>
197constexpr QRhiTexture::Format textureFormat(F f) noexcept
198{
199 if constexpr(requires { F::RGBA; } || requires { F::RGBA8; })
200 if(f == F::RGBA8)
201 return QRhiTexture::RGBA8;
202 if constexpr(requires { F::BGRA; } || requires { F::BGRA8; })
203 if(f == F::BGRA8)
204 return QRhiTexture::BGRA8;
205 if constexpr(requires { F::R8; } || requires { F::GRAYSCALE; })
206 if(f == F::R8)
207 return QRhiTexture::R8;
208#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
209 if constexpr(requires { F::RG8; })
210 if(f == F::RG8)
211 return QRhiTexture::RG8;
212#endif
213 if constexpr(requires { F::R16; })
214 if(f == F::R16)
215 return QRhiTexture::R16;
216#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
217 if constexpr(requires { F::RG16; })
218 if(f == F::RG16)
219 return QRhiTexture::RG16;
220#endif
221 if constexpr(requires { F::RED_OR_ALPHA8; })
222 if(f == F::RED_OR_ALPHA8)
223 return QRhiTexture::RED_OR_ALPHA8;
224 if constexpr(requires { F::RGBA16F; })
225 if(f == F::RGBA16F)
226 return QRhiTexture::RGBA16F;
227 if constexpr(requires { F::RGBA32F; })
228 if(f == F::RGBA32F)
229 return QRhiTexture::RGBA32F;
230 if constexpr(requires { F::R16F; })
231 if(f == F::R16F)
232 return QRhiTexture::R16F;
233 if constexpr(requires { F::R32F; })
234 if(f == F::R32F)
235 return QRhiTexture::R32F;
236#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
237 if constexpr(requires { F::RGB10A2; })
238 if(f == F::RGB10A2)
239 return QRhiTexture::RGB10A2;
240#endif
241 if constexpr(requires { F::D16; })
242 if(f == F::D16)
243 return QRhiTexture::D16;
244
245#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
246 if constexpr(requires { F::D24; })
247 if(f == F::D24)
248 return QRhiTexture::D24;
249 if constexpr(requires { F::D24S8; })
250 if(f == F::D24S8)
251 return QRhiTexture::D24S8;
252#endif
253 if constexpr(requires { F::D32F; })
254 if(f == F::D32F)
255 return QRhiTexture::D32F;
256
257 if constexpr(requires { F::BC1; })
258 if(f == F::BC1)
259 return QRhiTexture::BC1;
260 if constexpr(requires { F::BC2; })
261 if(f == F::BC2)
262 return QRhiTexture::BC2;
263 if constexpr(requires { F::BC3; })
264 if(f == F::BC3)
265 return QRhiTexture::BC3;
266 if constexpr(requires { F::BC4; })
267 if(f == F::BC4)
268 return QRhiTexture::BC4;
269 if constexpr(requires { F::BC5; })
270 if(f == F::BC5)
271 return QRhiTexture::BC5;
272 if constexpr(requires { F::BC6H; })
273 if(f == F::BC6H)
274 return QRhiTexture::BC6H;
275 if constexpr(requires { F::BC7; })
276 if(f == F::BC7)
277 return QRhiTexture::BC7;
278 if constexpr(requires { F::ETC2_RGB8; })
279 if(f == F::ETC2_RGB8)
280 return QRhiTexture::ETC2_RGB8;
281 if constexpr(requires { F::ETC2_RGB8A1; })
282 if(f == F::ETC2_RGB8A1)
283 return QRhiTexture::ETC2_RGB8A1;
284 if constexpr(requires { F::ETC2_RGB8A8; })
285 if(f == F::ETC2_RGBA8)
286 return QRhiTexture::ETC2_RGBA8;
287 if constexpr(requires { F::ASTC_4X4; })
288 if(f == F::ASTC_4x4)
289 return QRhiTexture::ASTC_4x4;
290 if constexpr(requires { F::ASTC_5X4; })
291 if(f == F::ASTC_5x4)
292 return QRhiTexture::ASTC_5x4;
293 if constexpr(requires { F::ASTC_5X5; })
294 if(f == F::ASTC_5x5)
295 return QRhiTexture::ASTC_5x5;
296 if constexpr(requires { F::ASTC_6X5; })
297 if(f == F::ASTC_6x5)
298 return QRhiTexture::ASTC_6x5;
299 if constexpr(requires { F::ASTC_6X6; })
300 if(f == F::ASTC_6x6)
301 return QRhiTexture::ASTC_6x6;
302 if constexpr(requires { F::ASTC_8X5; })
303 if(f == F::ASTC_8x5)
304 return QRhiTexture::ASTC_8x5;
305 if constexpr(requires { F::ASTC_8X6; })
306 if(f == F::ASTC_8x6)
307 return QRhiTexture::ASTC_8x6;
308 if constexpr(requires { F::ASTC_8X8; })
309 if(f == F::ASTC_8x8)
310 return QRhiTexture::ASTC_8x8;
311 if constexpr(requires { F::ASTC_10X5; })
312 if(f == F::ASTC_10x5)
313 return QRhiTexture::ASTC_10x5;
314 if constexpr(requires { F::ASTC_10X6; })
315 if(f == F::ASTC_10x6)
316 return QRhiTexture::ASTC_10x6;
317 if constexpr(requires { F::ASTC_10X8; })
318 if(f == F::ASTC_10x8)
319 return QRhiTexture::ASTC_10x8;
320 if constexpr(requires { F::ASTC_10X10; })
321 if(f == F::ASTC_10x10)
322 return QRhiTexture::ASTC_10x10;
323 if constexpr(requires { F::ASTC_12X10; })
324 if(f == F::ASTC_12x10)
325 return QRhiTexture::ASTC_12x10;
326 if constexpr(requires { F::ASTC_12X12; })
327 if(f == F::ASTC_12x12)
328 return QRhiTexture::ASTC_12x12;
329 if constexpr(requires { F::RGB; })
330 if(f == F::RGB)
331 return QRhiTexture::RGBA8; // we'll have a CPU step to go to rgb
332
333 return QRhiTexture::RGBA8;
334}
335
336template <typename F>
337constexpr QRhiTexture::Format textureFormat() noexcept
338{
339 if constexpr(requires { std::string_view{F::format()}; })
340 {
341 constexpr std::string_view fmt = F::format();
342
343 if(fmt == "rgba" || fmt == "rgba8")
344 return QRhiTexture::RGBA8;
345 else if(fmt == "bgra" || fmt == "bgra8")
346 return QRhiTexture::BGRA8;
347 else if(fmt == "r8")
348 return QRhiTexture::R8;
349#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
350 else if(fmt == "rg8")
351 return QRhiTexture::RG8;
352#endif
353 else if(fmt == "r16")
354 return QRhiTexture::R16;
355#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
356 else if(fmt == "rg16")
357 return QRhiTexture::RG16;
358#endif
359 else if(fmt == "red_or_alpha8")
360 return QRhiTexture::RED_OR_ALPHA8;
361 else if(fmt == "rgba16f")
362 return QRhiTexture::RGBA16F;
363 else if(fmt == "rgba32f")
364 return QRhiTexture::RGBA32F;
365 else if(fmt == "r16f")
366 return QRhiTexture::R16F;
367 else if(fmt == "r32")
368 return QRhiTexture::R32F;
369#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
370 else if(fmt == "rgb10a2")
371 return QRhiTexture::RGB10A2;
372#endif
373
374 else if(fmt == "d16")
375 return QRhiTexture::D16;
376
377#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
378 else if(fmt == "d24")
379 return QRhiTexture::D24;
380 else if(fmt == "d24s8")
381 return QRhiTexture::D24S8;
382#endif
383 else if(fmt == "d32f")
384 return QRhiTexture::D32F;
385
386 else if(fmt == "bc1")
387 return QRhiTexture::BC1;
388 else if(fmt == "bc2")
389 return QRhiTexture::BC2;
390 else if(fmt == "bc3")
391 return QRhiTexture::BC3;
392 else if(fmt == "bc4")
393 return QRhiTexture::BC4;
394 else if(fmt == "bc5")
395 return QRhiTexture::BC5;
396 else if(fmt == "bc6h")
397 return QRhiTexture::BC6H;
398 else if(fmt == "bc7")
399 return QRhiTexture::BC7;
400 else if(fmt == "etc2_rgb8")
401 return QRhiTexture::ETC2_RGB8;
402 else if(fmt == "etc2_rgb8a1")
403 return QRhiTexture::ETC2_RGB8A1;
404 else if(fmt == "etc2_rgb8a8")
405 return QRhiTexture::ETC2_RGBA8;
406 else if(fmt == "astc_4x4")
407 return QRhiTexture::ASTC_4x4;
408 else if(fmt == "astc_5x4")
409 return QRhiTexture::ASTC_5x4;
410 else if(fmt == "astc_5x5")
411 return QRhiTexture::ASTC_5x5;
412 else if(fmt == "astc_6x5")
413 return QRhiTexture::ASTC_6x5;
414 else if(fmt == "astc_6x6")
415 return QRhiTexture::ASTC_6x6;
416 else if(fmt == "astc_8x5")
417 return QRhiTexture::ASTC_8x5;
418 else if(fmt == "astc_8x6")
419 return QRhiTexture::ASTC_8x6;
420 else if(fmt == "astc_8x8")
421 return QRhiTexture::ASTC_8x8;
422 else if(fmt == "astc_10x5")
423 return QRhiTexture::ASTC_10x5;
424 else if(fmt == "astc_10x6")
425 return QRhiTexture::ASTC_10x6;
426 else if(fmt == "astc_10x8")
427 return QRhiTexture::ASTC_10x8;
428 else if(fmt == "astc_10x10")
429 return QRhiTexture::ASTC_10x10;
430 else if(fmt == "astc_12x10")
431 return QRhiTexture::ASTC_12x10;
432 else if(fmt == "astc_12x12")
433 return QRhiTexture::ASTC_12x12;
434 else if(fmt == "rgb")
435 return QRhiTexture::RGBA8;
436 else
437 return QRhiTexture::RGBA8;
438 }
439 else if constexpr(std::is_enum_v<typename F::format>)
440 {
441 if constexpr(requires { F::RGBA; } || requires { F::RGBA8; })
442 return QRhiTexture::RGBA8;
443 else if constexpr(requires { F::BGRA; } || requires { F::BGRA8; })
444 return QRhiTexture::BGRA8;
445 else if constexpr(requires { F::R8; } || requires { F::GRAYSCALE; })
446 return QRhiTexture::R8;
447#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
448 else if constexpr(requires { F::RG8; })
449 return QRhiTexture::RG8;
450#endif
451 else if constexpr(requires { F::R16; })
452 return QRhiTexture::R16;
453#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
454 else if constexpr(requires { F::RG16; })
455 return QRhiTexture::RG16;
456#endif
457 else if constexpr(requires { F::RED_OR_ALPHA8; })
458 return QRhiTexture::RED_OR_ALPHA8;
459 else if constexpr(requires { F::RGBA16F; })
460 return QRhiTexture::RGBA16F;
461 else if constexpr(requires { F::RGBA32F; })
462 return QRhiTexture::RGBA32F;
463 else if constexpr(requires { F::R16F; })
464 return QRhiTexture::R16F;
465 else if constexpr(requires { F::R32F; })
466 return QRhiTexture::R32F;
467#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
468 else if constexpr(requires { F::RGB10A2; })
469 return QRhiTexture::RGB10A2;
470#endif
471 else if constexpr(requires { F::D16; })
472 return QRhiTexture::D16;
473
474#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
475 else if constexpr(requires { F::D24; })
476 return QRhiTexture::D24;
477 else if constexpr(requires { F::D24S8; })
478 return QRhiTexture::D24S8;
479#endif
480 else if constexpr(requires { F::D32F; })
481 return QRhiTexture::D32F;
482
483 else if constexpr(requires { F::BC1; })
484 return QRhiTexture::BC1;
485 else if constexpr(requires { F::BC2; })
486 return QRhiTexture::BC2;
487 else if constexpr(requires { F::BC3; })
488 return QRhiTexture::BC3;
489 else if constexpr(requires { F::BC4; })
490 return QRhiTexture::BC4;
491 else if constexpr(requires { F::BC5; })
492 return QRhiTexture::BC5;
493 else if constexpr(requires { F::BC6H; })
494 return QRhiTexture::BC6H;
495 else if constexpr(requires { F::BC7; })
496 return QRhiTexture::BC7;
497 else if constexpr(requires { F::ETC2_RGB8; })
498 return QRhiTexture::ETC2_RGB8;
499 else if constexpr(requires { F::ETC2_RGB8A1; })
500 return QRhiTexture::ETC2_RGB8A1;
501 else if constexpr(requires { F::ETC2_RGB8A8; })
502 return QRhiTexture::ETC2_RGBA8;
503 else if constexpr(requires { F::ASTC_4X4; })
504 return QRhiTexture::ASTC_4x4;
505 else if constexpr(requires { F::ASTC_5X4; })
506 return QRhiTexture::ASTC_5x4;
507 else if constexpr(requires { F::ASTC_5X5; })
508 return QRhiTexture::ASTC_5x5;
509 else if constexpr(requires { F::ASTC_6X5; })
510 return QRhiTexture::ASTC_6x5;
511 else if constexpr(requires { F::ASTC_6X6; })
512 return QRhiTexture::ASTC_6x6;
513 else if constexpr(requires { F::ASTC_8X5; })
514 return QRhiTexture::ASTC_8x5;
515 else if constexpr(requires { F::ASTC_8X6; })
516 return QRhiTexture::ASTC_8x6;
517 else if constexpr(requires { F::ASTC_8X8; })
518 return QRhiTexture::ASTC_8x8;
519 else if constexpr(requires { F::ASTC_10X5; })
520 return QRhiTexture::ASTC_10x5;
521 else if constexpr(requires { F::ASTC_10X6; })
522 return QRhiTexture::ASTC_10x6;
523 else if constexpr(requires { F::ASTC_10X8; })
524 return QRhiTexture::ASTC_10x8;
525 else if constexpr(requires { F::ASTC_10X10; })
526 return QRhiTexture::ASTC_10x10;
527 else if constexpr(requires { F::ASTC_12X10; })
528 return QRhiTexture::ASTC_12x10;
529 else if constexpr(requires { F::ASTC_12X12; })
530 return QRhiTexture::ASTC_12x12;
531 else if constexpr(requires { F::RGB; })
532 return QRhiTexture::RGBA8;
533 else
534 return QRhiTexture::RGBA8;
535 }
536}
537
538template <avnd::cpu_texture Tex>
539constexpr QRhiTexture::Format textureFormat(const Tex& t) noexcept
540{
541 QRhiTexture::Format fmt{};
542 if constexpr(requires (Tex tex) { tex.request_format; })
543 {
544 fmt = gpp::qrhi::textureFormat(t.request_format);
545 }
546 else if constexpr(requires (Tex tex) { tex.format = {}; })
547 {
548 fmt = gpp::qrhi::textureFormat(t.format);
549 }
550 else
551 {
552 constexpr auto c_fmt = gpp::qrhi::textureFormat<Tex>();
553 fmt = c_fmt;
554 }
555 return fmt;
556}
557
558template <avnd::gpu_texture Tex>
559constexpr QRhiTexture::Format textureFormat(const Tex& t) noexcept
560{
561 QRhiTexture::Format fmt{};
562 if constexpr(requires (Tex tex) { tex.request_format; })
563 {
564 fmt = gpp::qrhi::textureFormat(t.request_format);
565 }
566 else if constexpr(requires (Tex tex) { tex.format = {}; })
567 {
568 fmt = gpp::qrhi::textureFormat(t.format);
569 }
570 else
571 {
572 constexpr auto c_fmt = gpp::qrhi::textureFormat<Tex>();
573 fmt = c_fmt;
574 }
575 return fmt;
576}
577}