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>
11 requires std::is_enum_v<F>
12constexpr QRhiTexture::Format textureFormat(F f) noexcept
13{
14 if constexpr(requires { F::RGBA; } || requires { F::RGBA8; })
15 if(f == F::RGBA8)
16 return QRhiTexture::RGBA8;
17 if constexpr(requires { F::BGRA; } || requires { F::BGRA8; })
18 if(f == F::BGRA8)
19 return QRhiTexture::BGRA8;
20 if constexpr(requires { F::R8; } || requires { F::GRAYSCALE; })
21 if(f == F::R8)
22 return QRhiTexture::R8;
23#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
24 if constexpr(requires { F::RG8; })
25 if(f == F::RG8)
26 return QRhiTexture::RG8;
27#endif
28 if constexpr(requires { F::R16; })
29 if(f == F::R16)
30 return QRhiTexture::R16;
31#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
32 if constexpr(requires { F::RG16; })
33 if(f == F::RG16)
34 return QRhiTexture::RG16;
35#endif
36 if constexpr(requires { F::RED_OR_ALPHA8; })
37 if(f == F::RED_OR_ALPHA8)
38 return QRhiTexture::RED_OR_ALPHA8;
39 if constexpr(requires { F::RGBA16F; })
40 if(f == F::RGBA16F)
41 return QRhiTexture::RGBA16F;
42 if constexpr(requires { F::RGBA32F; })
43 if(f == F::RGBA32F)
44 return QRhiTexture::RGBA32F;
45 if constexpr(requires { F::R16F; })
46 if(f == F::R16F)
47 return QRhiTexture::R16F;
48 if constexpr(requires { F::R32F; })
49 if(f == F::R32F)
50 return QRhiTexture::R32F;
51#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
52 if constexpr(requires { F::RGB10A2; })
53 if(f == F::RGB10A2)
54 return QRhiTexture::RGB10A2;
55#endif
56 if constexpr(requires { F::D16; })
57 if(f == F::D16)
58 return QRhiTexture::D16;
59
60#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
61 if constexpr(requires { F::D24; })
62 if(f == F::D24)
63 return QRhiTexture::D24;
64 if constexpr(requires { F::D24S8; })
65 if(f == F::D24S8)
66 return QRhiTexture::D24S8;
67#endif
68 if constexpr(requires { F::D32F; })
69 if(f == F::D32F)
70 return QRhiTexture::D32F;
71
72 if constexpr(requires { F::BC1; })
73 if(f == F::BC1)
74 return QRhiTexture::BC1;
75 if constexpr(requires { F::BC2; })
76 if(f == F::BC2)
77 return QRhiTexture::BC2;
78 if constexpr(requires { F::BC3; })
79 if(f == F::BC3)
80 return QRhiTexture::BC3;
81 if constexpr(requires { F::BC4; })
82 if(f == F::BC4)
83 return QRhiTexture::BC4;
84 if constexpr(requires { F::BC5; })
85 if(f == F::BC5)
86 return QRhiTexture::BC5;
87 if constexpr(requires { F::BC6H; })
88 if(f == F::BC6H)
89 return QRhiTexture::BC6H;
90 if constexpr(requires { F::BC7; })
91 if(f == F::BC7)
92 return QRhiTexture::BC7;
93 if constexpr(requires { F::ETC2_RGB8; })
94 if(f == F::ETC2_RGB8)
95 return QRhiTexture::ETC2_RGB8;
96 if constexpr(requires { F::ETC2_RGB8A1; })
97 if(f == F::ETC2_RGB8A1)
98 return QRhiTexture::ETC2_RGB8A1;
99 if constexpr(requires { F::ETC2_RGB8A8; })
100 if(f == F::ETC2_RGBA8)
101 return QRhiTexture::ETC2_RGBA8;
102 if constexpr(requires { F::ASTC_4X4; })
103 if(f == F::ASTC_4x4)
104 return QRhiTexture::ASTC_4x4;
105 if constexpr(requires { F::ASTC_5X4; })
106 if(f == F::ASTC_5x4)
107 return QRhiTexture::ASTC_5x4;
108 if constexpr(requires { F::ASTC_5X5; })
109 if(f == F::ASTC_5x5)
110 return QRhiTexture::ASTC_5x5;
111 if constexpr(requires { F::ASTC_6X5; })
112 if(f == F::ASTC_6x5)
113 return QRhiTexture::ASTC_6x5;
114 if constexpr(requires { F::ASTC_6X6; })
115 if(f == F::ASTC_6x6)
116 return QRhiTexture::ASTC_6x6;
117 if constexpr(requires { F::ASTC_8X5; })
118 if(f == F::ASTC_8x5)
119 return QRhiTexture::ASTC_8x5;
120 if constexpr(requires { F::ASTC_8X6; })
121 if(f == F::ASTC_8x6)
122 return QRhiTexture::ASTC_8x6;
123 if constexpr(requires { F::ASTC_8X8; })
124 if(f == F::ASTC_8x8)
125 return QRhiTexture::ASTC_8x8;
126 if constexpr(requires { F::ASTC_10X5; })
127 if(f == F::ASTC_10x5)
128 return QRhiTexture::ASTC_10x5;
129 if constexpr(requires { F::ASTC_10X6; })
130 if(f == F::ASTC_10x6)
131 return QRhiTexture::ASTC_10x6;
132 if constexpr(requires { F::ASTC_10X8; })
133 if(f == F::ASTC_10x8)
134 return QRhiTexture::ASTC_10x8;
135 if constexpr(requires { F::ASTC_10X10; })
136 if(f == F::ASTC_10x10)
137 return QRhiTexture::ASTC_10x10;
138 if constexpr(requires { F::ASTC_12X10; })
139 if(f == F::ASTC_12x10)
140 return QRhiTexture::ASTC_12x10;
141 if constexpr(requires { F::ASTC_12X12; })
142 if(f == F::ASTC_12x12)
143 return QRhiTexture::ASTC_12x12;
144 if constexpr(requires { F::RGB; })
145 if(f == F::RGB)
146 return QRhiTexture::RGBA8; // we'll have a CPU step to go to rgb
147
148 return QRhiTexture::RGBA8;
149}
150
151template <typename F>
152constexpr QRhiTexture::Format textureFormat() noexcept
153{
154 if constexpr(requires { std::string_view{F::format()}; })
155 {
156 constexpr std::string_view fmt = F::format();
157
158 if(fmt == "rgba" || fmt == "rgba8")
159 return QRhiTexture::RGBA8;
160 else if(fmt == "bgra" || fmt == "bgra8")
161 return QRhiTexture::BGRA8;
162 else if(fmt == "r8")
163 return QRhiTexture::R8;
164#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
165 else if(fmt == "rg8")
166 return QRhiTexture::RG8;
167#endif
168 else if(fmt == "r16")
169 return QRhiTexture::R16;
170#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
171 else if(fmt == "rg16")
172 return QRhiTexture::RG16;
173#endif
174 else if(fmt == "red_or_alpha8")
175 return QRhiTexture::RED_OR_ALPHA8;
176 else if(fmt == "rgba16f")
177 return QRhiTexture::RGBA16F;
178 else if(fmt == "rgba32f")
179 return QRhiTexture::RGBA32F;
180 else if(fmt == "r16f")
181 return QRhiTexture::R16F;
182 else if(fmt == "r32")
183 return QRhiTexture::R32F;
184#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
185 else if(fmt == "rgb10a2")
186 return QRhiTexture::RGB10A2;
187#endif
188
189 else if(fmt == "d16")
190 return QRhiTexture::D16;
191
192#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
193 else if(fmt == "d24")
194 return QRhiTexture::D24;
195 else if(fmt == "d24s8")
196 return QRhiTexture::D24S8;
197#endif
198 else if(fmt == "d32f")
199 return QRhiTexture::D32F;
200
201 else if(fmt == "bc1")
202 return QRhiTexture::BC1;
203 else if(fmt == "bc2")
204 return QRhiTexture::BC2;
205 else if(fmt == "bc3")
206 return QRhiTexture::BC3;
207 else if(fmt == "bc4")
208 return QRhiTexture::BC4;
209 else if(fmt == "bc5")
210 return QRhiTexture::BC5;
211 else if(fmt == "bc6h")
212 return QRhiTexture::BC6H;
213 else if(fmt == "bc7")
214 return QRhiTexture::BC7;
215 else if(fmt == "etc2_rgb8")
216 return QRhiTexture::ETC2_RGB8;
217 else if(fmt == "etc2_rgb8a1")
218 return QRhiTexture::ETC2_RGB8A1;
219 else if(fmt == "etc2_rgb8a8")
220 return QRhiTexture::ETC2_RGBA8;
221 else if(fmt == "astc_4x4")
222 return QRhiTexture::ASTC_4x4;
223 else if(fmt == "astc_5x4")
224 return QRhiTexture::ASTC_5x4;
225 else if(fmt == "astc_5x5")
226 return QRhiTexture::ASTC_5x5;
227 else if(fmt == "astc_6x5")
228 return QRhiTexture::ASTC_6x5;
229 else if(fmt == "astc_6x6")
230 return QRhiTexture::ASTC_6x6;
231 else if(fmt == "astc_8x5")
232 return QRhiTexture::ASTC_8x5;
233 else if(fmt == "astc_8x6")
234 return QRhiTexture::ASTC_8x6;
235 else if(fmt == "astc_8x8")
236 return QRhiTexture::ASTC_8x8;
237 else if(fmt == "astc_10x5")
238 return QRhiTexture::ASTC_10x5;
239 else if(fmt == "astc_10x6")
240 return QRhiTexture::ASTC_10x6;
241 else if(fmt == "astc_10x8")
242 return QRhiTexture::ASTC_10x8;
243 else if(fmt == "astc_10x10")
244 return QRhiTexture::ASTC_10x10;
245 else if(fmt == "astc_12x10")
246 return QRhiTexture::ASTC_12x10;
247 else if(fmt == "astc_12x12")
248 return QRhiTexture::ASTC_12x12;
249 else if(fmt == "rgb")
250 return QRhiTexture::RGBA8;
251 else
252 return QRhiTexture::RGBA8;
253 }
254 else if constexpr(std::is_enum_v<typename F::format>)
255 {
256 if constexpr(requires { F::RGBA; } || requires { F::RGBA8; })
257 return QRhiTexture::RGBA8;
258 else if constexpr(requires { F::BGRA; } || requires { F::BGRA8; })
259 return QRhiTexture::BGRA8;
260 else if constexpr(requires { F::R8; } || requires { F::GRAYSCALE; })
261 return QRhiTexture::R8;
262#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
263 else if constexpr(requires { F::RG8; })
264 return QRhiTexture::RG8;
265#endif
266 else if constexpr(requires { F::R16; })
267 return QRhiTexture::R16;
268#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
269 else if constexpr(requires { F::RG16; })
270 return QRhiTexture::RG16;
271#endif
272 else if constexpr(requires { F::RED_OR_ALPHA8; })
273 return QRhiTexture::RED_OR_ALPHA8;
274 else if constexpr(requires { F::RGBA16F; })
275 return QRhiTexture::RGBA16F;
276 else if constexpr(requires { F::RGBA32F; })
277 return QRhiTexture::RGBA32F;
278 else if constexpr(requires { F::R16F; })
279 return QRhiTexture::R16F;
280 else if constexpr(requires { F::R32F; })
281 return QRhiTexture::R32F;
282#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
283 else if constexpr(requires { F::RGB10A2; })
284 return QRhiTexture::RGB10A2;
285#endif
286 else if constexpr(requires { F::D16; })
287 return QRhiTexture::D16;
288
289#if QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)
290 else if constexpr(requires { F::D24; })
291 return QRhiTexture::D24;
292 else if constexpr(requires { F::D24S8; })
293 return QRhiTexture::D24S8;
294#endif
295 else if constexpr(requires { F::D32F; })
296 return QRhiTexture::D32F;
297
298 else if constexpr(requires { F::BC1; })
299 return QRhiTexture::BC1;
300 else if constexpr(requires { F::BC2; })
301 return QRhiTexture::BC2;
302 else if constexpr(requires { F::BC3; })
303 return QRhiTexture::BC3;
304 else if constexpr(requires { F::BC4; })
305 return QRhiTexture::BC4;
306 else if constexpr(requires { F::BC5; })
307 return QRhiTexture::BC5;
308 else if constexpr(requires { F::BC6H; })
309 return QRhiTexture::BC6H;
310 else if constexpr(requires { F::BC7; })
311 return QRhiTexture::BC7;
312 else if constexpr(requires { F::ETC2_RGB8; })
313 return QRhiTexture::ETC2_RGB8;
314 else if constexpr(requires { F::ETC2_RGB8A1; })
315 return QRhiTexture::ETC2_RGB8A1;
316 else if constexpr(requires { F::ETC2_RGB8A8; })
317 return QRhiTexture::ETC2_RGBA8;
318 else if constexpr(requires { F::ASTC_4X4; })
319 return QRhiTexture::ASTC_4x4;
320 else if constexpr(requires { F::ASTC_5X4; })
321 return QRhiTexture::ASTC_5x4;
322 else if constexpr(requires { F::ASTC_5X5; })
323 return QRhiTexture::ASTC_5x5;
324 else if constexpr(requires { F::ASTC_6X5; })
325 return QRhiTexture::ASTC_6x5;
326 else if constexpr(requires { F::ASTC_6X6; })
327 return QRhiTexture::ASTC_6x6;
328 else if constexpr(requires { F::ASTC_8X5; })
329 return QRhiTexture::ASTC_8x5;
330 else if constexpr(requires { F::ASTC_8X6; })
331 return QRhiTexture::ASTC_8x6;
332 else if constexpr(requires { F::ASTC_8X8; })
333 return QRhiTexture::ASTC_8x8;
334 else if constexpr(requires { F::ASTC_10X5; })
335 return QRhiTexture::ASTC_10x5;
336 else if constexpr(requires { F::ASTC_10X6; })
337 return QRhiTexture::ASTC_10x6;
338 else if constexpr(requires { F::ASTC_10X8; })
339 return QRhiTexture::ASTC_10x8;
340 else if constexpr(requires { F::ASTC_10X10; })
341 return QRhiTexture::ASTC_10x10;
342 else if constexpr(requires { F::ASTC_12X10; })
343 return QRhiTexture::ASTC_12x10;
344 else if constexpr(requires { F::ASTC_12X12; })
345 return QRhiTexture::ASTC_12x12;
346 else if constexpr(requires { F::RGB; })
347 return QRhiTexture::RGBA8;
348 else
349 return QRhiTexture::RGBA8;
350 }
351}
352
353template <avnd::cpu_texture Tex>
354constexpr QRhiTexture::Format textureFormat(const Tex& t) noexcept
355{
356 QRhiTexture::Format fmt{};
357 if constexpr(avnd::cpu_dynamic_format_texture<Tex>)
358 {
359 fmt = gpp::qrhi::textureFormat(t.format);
360 }
361 else
362 {
363 constexpr auto c_fmt = gpp::qrhi::textureFormat<Tex>();
364 fmt = c_fmt;
365 }
366 return fmt;
367}
368
369}