1 /****************************************************************************
2 **
3 ** Copyright (C) 2008-2012 NVIDIA Corporation.
4 ** Copyright (C) 2019 The Qt Company Ltd.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of Qt Quick 3D.
8 **
9 ** $QT_BEGIN_LICENSE:GPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU
20 ** General Public License version 3 or (at your option) any later version
21 ** approved by the KDE Free Qt Foundation. The licenses are as published by
22 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
23 ** included in the packaging of this file. Please review the following
24 ** information to ensure the GNU General Public License requirements will
25 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
26 **
27 ** $QT_END_LICENSE$
28 **
29 ****************************************************************************/
30 
31 
32 #ifndef QSSGOPENGLUTIL_H
33 #define QSSGOPENGLUTIL_H
34 
35 //
36 //  W A R N I N G
37 //  -------------
38 //
39 // This file is not part of the Qt API.  It exists purely as an
40 // implementation detail.  This header file may change from version to
41 // version without notice, or even be removed.
42 //
43 // We mean it.
44 //
45 
46 #include <QtQuick3DRender/private/qssgopengltokens_p.h>
47 #include <QtQuick3DRender/private/qssgrenderbasetypes_p.h>
48 
49 #include <QtGui/QOpenGLContext>
50 #include <QtGui/QOpenGLExtraFunctions>
51 
52 #include <QVector2D>
53 #include <QVector3D>
54 #include <QVector4D>
55 
56 QT_BEGIN_NAMESPACE
57 
58 #if !defined(QT_OPENGL_ES)
IsGlEsContext(QSSGRenderContextType inContextType)59 static bool IsGlEsContext(QSSGRenderContextType inContextType)
60 {
61     QSSGRenderContextTypes esContextTypes(QSSGRenderContextType::GLES2 |
62                                           QSSGRenderContextType::GLES3 |
63                                           QSSGRenderContextType::GLES3PLUS);
64     return ((esContextTypes & inContextType) != 0);
65 }
66 #endif
67 
68 struct GLConversion
69 {
GLConversionGLConversion70     GLConversion() {}
71 
processGLErrorGLConversion72     static const char *processGLError(GLenum error)
73     {
74         const char *errorString;
75         switch (error) {
76         case GL_NO_ERROR:
77             errorString = "GL_NO_ERROR";
78             break;
79         case GL_INVALID_ENUM:
80             errorString = "GL_INVALID_ENUM";
81             break;
82         case GL_INVALID_VALUE:
83             errorString = "GL_INVALID_VALUE";
84             break;
85         case GL_INVALID_OPERATION:
86             errorString = "GL_INVALID_OPERATION";
87             break;
88         case GL_INVALID_FRAMEBUFFER_OPERATION:
89             errorString = "GL_INVALID_FRAMEBUFFER_OPERATION";
90             break;
91         case GL_OUT_OF_MEMORY:
92             errorString = "GL_OUT_OF_MEMORY";
93             break;
94         default:
95             errorString = "Unknown GL error";
96             break;
97         }
98         return errorString;
99     }
100 
fromGLToSrcBlendFuncGLConversion101     static QSSGRenderSrcBlendFunc fromGLToSrcBlendFunc(qint32 value)
102     {
103         switch (value) {
104         case GL_ZERO:
105             return QSSGRenderSrcBlendFunc::Zero;
106         case GL_ONE:
107             return QSSGRenderSrcBlendFunc::One;
108         case GL_SRC_COLOR:
109             return QSSGRenderSrcBlendFunc::SrcColor;
110         case GL_ONE_MINUS_SRC_COLOR:
111             return QSSGRenderSrcBlendFunc::OneMinusSrcColor;
112         case GL_DST_COLOR:
113             return QSSGRenderSrcBlendFunc::DstColor;
114         case GL_ONE_MINUS_DST_COLOR:
115             return QSSGRenderSrcBlendFunc::OneMinusDstColor;
116         case GL_SRC_ALPHA:
117             return QSSGRenderSrcBlendFunc::SrcAlpha;
118         case GL_ONE_MINUS_SRC_ALPHA:
119             return QSSGRenderSrcBlendFunc::OneMinusSrcAlpha;
120         case GL_DST_ALPHA:
121             return QSSGRenderSrcBlendFunc::DstAlpha;
122         case GL_ONE_MINUS_DST_ALPHA:
123             return QSSGRenderSrcBlendFunc::OneMinusDstAlpha;
124         case GL_CONSTANT_COLOR:
125             return QSSGRenderSrcBlendFunc::ConstantColor;
126         case GL_ONE_MINUS_CONSTANT_COLOR:
127             return QSSGRenderSrcBlendFunc::OneMinusConstantColor;
128         case GL_CONSTANT_ALPHA:
129             return QSSGRenderSrcBlendFunc::ConstantAlpha;
130         case GL_ONE_MINUS_CONSTANT_ALPHA:
131             return QSSGRenderSrcBlendFunc::OneMinusConstantAlpha;
132         case GL_SRC_ALPHA_SATURATE:
133             return QSSGRenderSrcBlendFunc::SrcAlphaSaturate;
134         default:
135             Q_ASSERT(false);
136             return QSSGRenderSrcBlendFunc::Unknown;
137         }
138     }
139 
fromSrcBlendFuncToGLGLConversion140     static GLenum fromSrcBlendFuncToGL(QSSGRenderSrcBlendFunc value)
141     {
142         switch (value) {
143         case QSSGRenderSrcBlendFunc::Zero:
144             return GL_ZERO;
145         case QSSGRenderSrcBlendFunc::One:
146             return GL_ONE;
147         case QSSGRenderSrcBlendFunc::SrcColor:
148             return GL_SRC_COLOR;
149         case QSSGRenderSrcBlendFunc::OneMinusSrcColor:
150             return GL_ONE_MINUS_SRC_COLOR;
151         case QSSGRenderSrcBlendFunc::DstColor:
152             return GL_DST_COLOR;
153         case QSSGRenderSrcBlendFunc::OneMinusDstColor:
154             return GL_ONE_MINUS_DST_COLOR;
155         case QSSGRenderSrcBlendFunc::SrcAlpha:
156             return GL_SRC_ALPHA;
157         case QSSGRenderSrcBlendFunc::OneMinusSrcAlpha:
158             return GL_ONE_MINUS_SRC_ALPHA;
159         case QSSGRenderSrcBlendFunc::DstAlpha:
160             return GL_DST_ALPHA;
161         case QSSGRenderSrcBlendFunc::OneMinusDstAlpha:
162             return GL_ONE_MINUS_DST_ALPHA;
163         case QSSGRenderSrcBlendFunc::ConstantColor:
164             return GL_CONSTANT_COLOR;
165         case QSSGRenderSrcBlendFunc::OneMinusConstantColor:
166             return GL_ONE_MINUS_CONSTANT_COLOR;
167         case QSSGRenderSrcBlendFunc::ConstantAlpha:
168             return GL_CONSTANT_ALPHA;
169         case QSSGRenderSrcBlendFunc::OneMinusConstantAlpha:
170             return GL_ONE_MINUS_CONSTANT_ALPHA;
171         case QSSGRenderSrcBlendFunc::SrcAlphaSaturate:
172             return GL_SRC_ALPHA_SATURATE;
173         default:
174             Q_ASSERT(false);
175             return 0;
176         }
177     }
178 
fromGLToDstBlendFuncGLConversion179     static QSSGRenderDstBlendFunc fromGLToDstBlendFunc(qint32 value)
180     {
181         switch (value) {
182         case GL_ZERO:
183             return QSSGRenderDstBlendFunc::Zero;
184         case GL_ONE:
185             return QSSGRenderDstBlendFunc::One;
186         case GL_SRC_COLOR:
187             return QSSGRenderDstBlendFunc::SrcColor;
188         case GL_ONE_MINUS_SRC_COLOR:
189             return QSSGRenderDstBlendFunc::OneMinusSrcColor;
190         case GL_DST_COLOR:
191             return QSSGRenderDstBlendFunc::DstColor;
192         case GL_ONE_MINUS_DST_COLOR:
193             return QSSGRenderDstBlendFunc::OneMinusDstColor;
194         case GL_SRC_ALPHA:
195             return QSSGRenderDstBlendFunc::SrcAlpha;
196         case GL_ONE_MINUS_SRC_ALPHA:
197             return QSSGRenderDstBlendFunc::OneMinusSrcAlpha;
198         case GL_DST_ALPHA:
199             return QSSGRenderDstBlendFunc::DstAlpha;
200         case GL_ONE_MINUS_DST_ALPHA:
201             return QSSGRenderDstBlendFunc::OneMinusDstAlpha;
202         case GL_CONSTANT_COLOR:
203             return QSSGRenderDstBlendFunc::ConstantColor;
204         case GL_ONE_MINUS_CONSTANT_COLOR:
205             return QSSGRenderDstBlendFunc::OneMinusConstantColor;
206         case GL_CONSTANT_ALPHA:
207             return QSSGRenderDstBlendFunc::ConstantAlpha;
208         case GL_ONE_MINUS_CONSTANT_ALPHA:
209             return QSSGRenderDstBlendFunc::OneMinusConstantAlpha;
210         default:
211             Q_ASSERT(false);
212             return QSSGRenderDstBlendFunc::Unknown;
213         }
214     }
215 
fromDstBlendFuncToGLGLConversion216     static GLenum fromDstBlendFuncToGL(QSSGRenderDstBlendFunc value)
217     {
218         switch (value) {
219         case QSSGRenderDstBlendFunc::Zero:
220             return GL_ZERO;
221         case QSSGRenderDstBlendFunc::One:
222             return GL_ONE;
223         case QSSGRenderDstBlendFunc::SrcColor:
224             return GL_SRC_COLOR;
225         case QSSGRenderDstBlendFunc::OneMinusSrcColor:
226             return GL_ONE_MINUS_SRC_COLOR;
227         case QSSGRenderDstBlendFunc::DstColor:
228             return GL_DST_COLOR;
229         case QSSGRenderDstBlendFunc::OneMinusDstColor:
230             return GL_ONE_MINUS_DST_COLOR;
231         case QSSGRenderDstBlendFunc::SrcAlpha:
232             return GL_SRC_ALPHA;
233         case QSSGRenderDstBlendFunc::OneMinusSrcAlpha:
234             return GL_ONE_MINUS_SRC_ALPHA;
235         case QSSGRenderDstBlendFunc::DstAlpha:
236             return GL_DST_ALPHA;
237         case QSSGRenderDstBlendFunc::OneMinusDstAlpha:
238             return GL_ONE_MINUS_DST_ALPHA;
239         case QSSGRenderDstBlendFunc::ConstantColor:
240             return GL_CONSTANT_COLOR;
241         case QSSGRenderDstBlendFunc::OneMinusConstantColor:
242             return GL_ONE_MINUS_CONSTANT_COLOR;
243         case QSSGRenderDstBlendFunc::ConstantAlpha:
244             return GL_CONSTANT_ALPHA;
245         case QSSGRenderDstBlendFunc::OneMinusConstantAlpha:
246             return GL_ONE_MINUS_CONSTANT_ALPHA;
247         default:
248             Q_ASSERT(false);
249             return 0;
250         }
251     }
252 
fromBlendEquationToGLGLConversion253     static GLenum fromBlendEquationToGL(QSSGRenderBlendEquation value, bool nvAdvancedBlendSupported, bool khrAdvancedBlendSupported)
254     {
255         switch (value) {
256         case QSSGRenderBlendEquation::Add:
257             return GL_FUNC_ADD;
258         case QSSGRenderBlendEquation::Subtract:
259             return GL_FUNC_SUBTRACT;
260         case QSSGRenderBlendEquation::ReverseSubtract:
261             return GL_FUNC_REVERSE_SUBTRACT;
262         default:
263             Q_ASSERT(nvAdvancedBlendSupported || khrAdvancedBlendSupported);
264             break;
265         }
266 
267         if (nvAdvancedBlendSupported) {
268             switch (value) {
269             case QSSGRenderBlendEquation::Overlay:
270                 return GL_OVERLAY_NV;
271             case QSSGRenderBlendEquation::ColorBurn:
272                 return GL_COLORBURN_NV;
273             case QSSGRenderBlendEquation::ColorDodge:
274                 return GL_COLORDODGE_NV;
275             default:
276                 break;
277             }
278         }
279 
280 #if defined(GL_KHR_blend_equation_advanced)
281         if (khrAdvancedBlendSupported) {
282             switch (value) {
283             case QSSGRenderBlendEquation::Overlay:
284                 return GL_OVERLAY_KHR;
285             case QSSGRenderBlendEquation::ColorBurn:
286                 return GL_COLORBURN_KHR;
287             case QSSGRenderBlendEquation::ColorDodge:
288                 return GL_COLORDODGE_KHR;
289             default:
290                 break;
291             }
292         }
293 #endif
294         Q_ASSERT(false);
295         return GL_FUNC_ADD;
296     }
297 
fromGLToCullFaceModeGLConversion298     static QSSGCullFaceMode fromGLToCullFaceMode(GLenum value)
299     {
300         switch (value) {
301         case GL_FRONT:
302             return QSSGCullFaceMode::Front;
303         case GL_BACK:
304             return QSSGCullFaceMode::Back;
305         case GL_FRONT_AND_BACK:
306             return QSSGCullFaceMode::FrontAndBack;
307         default:
308             break;
309         }
310         Q_ASSERT(false);
311         return QSSGCullFaceMode::Unknown;
312     }
313 
fromCullFaceModeToGLGLConversion314     static GLenum fromCullFaceModeToGL(QSSGCullFaceMode value)
315     {
316         switch (value) {
317         case QSSGCullFaceMode::Front:
318             return GL_FRONT;
319         case QSSGCullFaceMode::Back:
320             return GL_BACK;
321         case QSSGCullFaceMode::FrontAndBack:
322             return GL_FRONT_AND_BACK;
323         default:
324             break;
325         }
326         Q_ASSERT(false);
327         return 0;
328     }
329 
fromGLToReadFacesGLConversion330     static QSSGReadFace fromGLToReadFaces(GLenum value)
331     {
332         switch (value) {
333         case GL_FRONT:
334             return QSSGReadFace::Front;
335         case GL_BACK:
336             return QSSGReadFace::Back;
337         case GL_COLOR_ATTACHMENT0:
338             return QSSGReadFace::Color0;
339         case GL_COLOR_ATTACHMENT1:
340             return QSSGReadFace::Color1;
341         case GL_COLOR_ATTACHMENT2:
342             return QSSGReadFace::Color2;
343         case GL_COLOR_ATTACHMENT3:
344             return QSSGReadFace::Color3;
345         case GL_COLOR_ATTACHMENT4:
346             return QSSGReadFace::Color4;
347         case GL_COLOR_ATTACHMENT5:
348             return QSSGReadFace::Color5;
349         case GL_COLOR_ATTACHMENT6:
350             return QSSGReadFace::Color6;
351         case GL_COLOR_ATTACHMENT7:
352             return QSSGReadFace::Color7;
353         default:
354             break;
355         }
356         Q_ASSERT(false);
357         return QSSGReadFace::Unknown;
358     }
359 
fromReadFacesToGLGLConversion360     static GLenum fromReadFacesToGL(QSSGReadFace value)
361     {
362         switch (value) {
363         case QSSGReadFace::Front:
364             return GL_FRONT;
365         case QSSGReadFace::Back:
366             return GL_BACK;
367         case QSSGReadFace::Color0:
368             return GL_COLOR_ATTACHMENT0;
369         case QSSGReadFace::Color1:
370             return GL_COLOR_ATTACHMENT1;
371         case QSSGReadFace::Color2:
372             return GL_COLOR_ATTACHMENT2;
373         case QSSGReadFace::Color3:
374             return GL_COLOR_ATTACHMENT3;
375         case QSSGReadFace::Color4:
376             return GL_COLOR_ATTACHMENT4;
377         case QSSGReadFace::Color5:
378             return GL_COLOR_ATTACHMENT5;
379         case QSSGReadFace::Color6:
380             return GL_COLOR_ATTACHMENT6;
381         case QSSGReadFace::Color7:
382             return GL_COLOR_ATTACHMENT7;
383         default:
384             break;
385         }
386         Q_ASSERT(false);
387         return 0;
388     }
389 
fromGLToWindingGLConversion390     static QSSGRenderWinding fromGLToWinding(GLenum value)
391     {
392         switch (value) {
393         case GL_CW:
394             return QSSGRenderWinding::Clockwise;
395         case GL_CCW:
396             return QSSGRenderWinding::CounterClockwise;
397         default:
398             break;
399         }
400         Q_ASSERT(false);
401         return QSSGRenderWinding::Unknown;
402     }
403 
fromWindingToGLGLConversion404     static GLenum fromWindingToGL(QSSGRenderWinding value)
405     {
406         switch (value) {
407         case QSSGRenderWinding::Clockwise:
408             return GL_CW;
409         case QSSGRenderWinding::CounterClockwise:
410             return GL_CCW;
411         default:
412             break;
413         }
414         Q_ASSERT(false);
415         return 0;
416     }
417 
fromGLToBoolOpGLConversion418     static QSSGRenderBoolOp fromGLToBoolOp(GLenum value)
419     {
420         switch (value) {
421         case GL_NEVER:
422             return QSSGRenderBoolOp::Never;
423         case GL_LESS:
424             return QSSGRenderBoolOp::Less;
425         case GL_EQUAL:
426             return QSSGRenderBoolOp::Equal;
427         case GL_LEQUAL:
428             return QSSGRenderBoolOp::LessThanOrEqual;
429         case GL_GREATER:
430             return QSSGRenderBoolOp::Greater;
431         case GL_NOTEQUAL:
432             return QSSGRenderBoolOp::NotEqual;
433         case GL_GEQUAL:
434             return QSSGRenderBoolOp::GreaterThanOrEqual;
435         case GL_ALWAYS:
436             return QSSGRenderBoolOp::AlwaysTrue;
437         default:
438             break;
439         }
440         Q_ASSERT(false);
441         return QSSGRenderBoolOp::Unknown;
442     }
443 
fromBoolOpToGLGLConversion444     static GLenum fromBoolOpToGL(QSSGRenderBoolOp value)
445     {
446         switch (value) {
447         case QSSGRenderBoolOp::Never:
448             return GL_NEVER;
449         case QSSGRenderBoolOp::Less:
450             return GL_LESS;
451         case QSSGRenderBoolOp::Equal:
452             return GL_EQUAL;
453         case QSSGRenderBoolOp::LessThanOrEqual:
454             return GL_LEQUAL;
455         case QSSGRenderBoolOp::Greater:
456             return GL_GREATER;
457         case QSSGRenderBoolOp::NotEqual:
458             return GL_NOTEQUAL;
459         case QSSGRenderBoolOp::GreaterThanOrEqual:
460             return GL_GEQUAL;
461         case QSSGRenderBoolOp::AlwaysTrue:
462             return GL_ALWAYS;
463         default:
464             break;
465         }
466         Q_ASSERT(false);
467         return 0;
468     }
469 
fromGLToHintGLConversion470     static QSSGRenderHint fromGLToHint(GLenum value)
471     {
472         switch (value) {
473         case GL_FASTEST:
474             return QSSGRenderHint::Fastest;
475         case GL_NICEST:
476             return QSSGRenderHint::Nicest;
477         case GL_DONT_CARE:
478             return QSSGRenderHint::Unspecified;
479         default:
480             break;
481         }
482         Q_ASSERT(false);
483         return QSSGRenderHint::Unknown;
484     }
485 
fromHintToGLGLConversion486     static GLenum fromHintToGL(QSSGRenderHint value)
487     {
488         switch (value) {
489         case QSSGRenderHint::Fastest:
490             return GL_FASTEST;
491         case QSSGRenderHint::Nicest:
492             return GL_NICEST;
493         case QSSGRenderHint::Unspecified:
494             return GL_DONT_CARE;
495         default:
496             break;
497         }
498         Q_ASSERT(false);
499         return 0;
500     }
501 
fromGLToStencilOpGLConversion502     static QSSGRenderStencilOp fromGLToStencilOp(GLenum value)
503     {
504         switch (value) {
505         case GL_KEEP:
506             return QSSGRenderStencilOp::Keep;
507         case GL_ZERO:
508             return QSSGRenderStencilOp::Zero;
509         case GL_REPLACE:
510             return QSSGRenderStencilOp::Replace;
511         case GL_INCR:
512             return QSSGRenderStencilOp::Increment;
513         case GL_INCR_WRAP:
514             return QSSGRenderStencilOp::IncrementWrap;
515         case GL_DECR:
516             return QSSGRenderStencilOp::Decrement;
517         case GL_DECR_WRAP:
518             return QSSGRenderStencilOp::DecrementWrap;
519         case GL_INVERT:
520             return QSSGRenderStencilOp::Invert;
521         default:
522             break;
523         }
524         Q_ASSERT(false);
525         return QSSGRenderStencilOp::Unknown;
526     }
527 
fromStencilOpToGLGLConversion528     static GLenum fromStencilOpToGL(QSSGRenderStencilOp value)
529     {
530         switch (value) {
531         case QSSGRenderStencilOp::Keep:
532             return GL_KEEP;
533         case QSSGRenderStencilOp::Zero:
534             return GL_ZERO;
535         case QSSGRenderStencilOp::Replace:
536             return GL_REPLACE;
537         case QSSGRenderStencilOp::Increment:
538             return GL_INCR;
539         case QSSGRenderStencilOp::IncrementWrap:
540             return GL_INCR_WRAP;
541         case QSSGRenderStencilOp::Decrement:
542             return GL_DECR;
543         case QSSGRenderStencilOp::DecrementWrap:
544             return GL_DECR_WRAP;
545         case QSSGRenderStencilOp::Invert:
546             return GL_INVERT;
547         default:
548             break;
549         }
550 
551         Q_ASSERT(false);
552         return 0;
553     }
554 
fromGLToBufferComponentTypesGLConversion555     static QSSGRenderComponentType fromGLToBufferComponentTypes(GLenum value)
556     {
557         switch (value) {
558         case GL_UNSIGNED_BYTE:
559             return QSSGRenderComponentType::UnsignedInteger8;
560         case GL_BYTE:
561             return QSSGRenderComponentType::Integer8;
562         case GL_UNSIGNED_SHORT:
563             return QSSGRenderComponentType::UnsignedInteger16;
564         case GL_SHORT:
565             return QSSGRenderComponentType::Integer16;
566         case GL_UNSIGNED_INT:
567             return QSSGRenderComponentType::UnsignedInteger32;
568         case GL_FLOAT:
569             return QSSGRenderComponentType::Float32;
570         default:
571             break;
572         }
573         Q_ASSERT(false);
574         return QSSGRenderComponentType::Unknown;
575     }
576 
fromBufferComponentTypesToGLGLConversion577     static GLenum fromBufferComponentTypesToGL(QSSGRenderComponentType value)
578     {
579         switch (value) {
580         case QSSGRenderComponentType::UnsignedInteger8:
581             return GL_UNSIGNED_BYTE;
582         case QSSGRenderComponentType::Integer8:
583             return GL_BYTE;
584         case QSSGRenderComponentType::UnsignedInteger16:
585             return GL_UNSIGNED_SHORT;
586         case QSSGRenderComponentType::Integer16:
587             return GL_SHORT;
588         case QSSGRenderComponentType::UnsignedInteger32:
589             return GL_UNSIGNED_INT;
590         case QSSGRenderComponentType::Integer32:
591             return GL_INT;
592         case QSSGRenderComponentType::Float32:
593             return GL_FLOAT;
594         default:
595             break;
596         }
597         Q_ASSERT(false);
598         return 0;
599     }
600 
fromIndexBufferComponentsTypesToGLGLConversion601     static GLenum fromIndexBufferComponentsTypesToGL(QSSGRenderComponentType value)
602     {
603         switch (value) {
604         case QSSGRenderComponentType::UnsignedInteger8:
605             return GL_UNSIGNED_BYTE;
606         case QSSGRenderComponentType::UnsignedInteger16:
607             return GL_UNSIGNED_SHORT;
608         case QSSGRenderComponentType::UnsignedInteger32:
609             return GL_UNSIGNED_INT;
610         default:
611             break;
612         }
613         Q_ASSERT(false);
614         return 0;
615     }
616 
fromBindBufferFlagsToGLGLConversion617     static GLenum fromBindBufferFlagsToGL(QSSGRenderBufferType type)
618     {
619         switch(type) {
620         case QSSGRenderBufferType::Vertex:
621             return GL_ARRAY_BUFFER;
622         case QSSGRenderBufferType::Index:
623             return GL_ELEMENT_ARRAY_BUFFER;
624         case QSSGRenderBufferType::Constant:
625             return GL_UNIFORM_BUFFER;
626         case QSSGRenderBufferType::Storage:
627             return GL_SHADER_STORAGE_BUFFER;
628         }
629         Q_ASSERT(false);
630         return 0;
631     }
632 
fromGLToBindBufferFlagsGLConversion633     static QSSGRenderBufferType fromGLToBindBufferFlags(GLenum value)
634     {
635         switch (value) {
636         case GL_ARRAY_BUFFER:
637             return QSSGRenderBufferType::Vertex;
638         case GL_ELEMENT_ARRAY_BUFFER:
639             return QSSGRenderBufferType::Index;
640         case GL_UNIFORM_BUFFER:
641             return QSSGRenderBufferType::Constant;
642         case GL_SHADER_STORAGE_BUFFER:
643             return QSSGRenderBufferType::Storage;
644         default:
645             Q_UNREACHABLE();
646         }
647 
648         return QSSGRenderBufferType(0);
649     }
650 
fromGLToBufferUsageTypeGLConversion651     static QSSGRenderBufferUsageType fromGLToBufferUsageType(GLenum value)
652     {
653         switch (value) {
654         case GL_STATIC_DRAW:
655             return QSSGRenderBufferUsageType::Static;
656         case GL_DYNAMIC_DRAW:
657             return QSSGRenderBufferUsageType::Dynamic;
658         default:
659             break;
660         }
661         Q_ASSERT(false);
662         return QSSGRenderBufferUsageType::Unknown;
663     }
664 
fromBufferUsageTypeToGLGLConversion665     static GLenum fromBufferUsageTypeToGL(QSSGRenderBufferUsageType value)
666     {
667         switch (value) {
668         case QSSGRenderBufferUsageType::Static:
669             return GL_STATIC_DRAW;
670         case QSSGRenderBufferUsageType::Dynamic:
671             return GL_DYNAMIC_DRAW;
672         default:
673             break;
674         }
675         Q_ASSERT(false);
676         return 0;
677     }
678 
fromQueryTypeToGLGLConversion679     static GLenum fromQueryTypeToGL(QSSGRenderQueryType type)
680     {
681         GLenum retval = GL_INVALID_ENUM;
682         if (type == QSSGRenderQueryType::Samples)
683             retval = GL_ANY_SAMPLES_PASSED;
684 #if defined(GL_TIME_ELAPSED)
685         else if (type == QSSGRenderQueryType::Timer)
686             retval = GL_TIME_ELAPSED;
687 #elif defined(GL_TIME_ELAPSED_EXT)
688         else if (type == QSSGRenderQueryType::Timer)
689             retval = GL_TIME_ELAPSED_EXT;
690 #endif
691         else
692             Q_ASSERT(false);
693 
694         return retval;
695     }
696 
fromQueryResultTypeToGLGLConversion697     static GLenum fromQueryResultTypeToGL(QSSGRenderQueryResultType type)
698     {
699         GLenum retval = GL_INVALID_ENUM;
700         if (type == QSSGRenderQueryResultType::ResultAvailable)
701             retval = GL_QUERY_RESULT_AVAILABLE;
702         else if (type == QSSGRenderQueryResultType::Result)
703             retval = GL_QUERY_RESULT;
704         else
705             Q_ASSERT(false);
706 
707         return retval;
708     }
709 
fromSyncTypeToGLGLConversion710     static GLenum fromSyncTypeToGL(QSSGRenderSyncType type)
711     {
712         GLenum retval = GL_INVALID_ENUM;
713         if (type == QSSGRenderSyncType::GpuCommandsComplete)
714             retval = GL_SYNC_GPU_COMMANDS_COMPLETE;
715         else
716             Q_ASSERT(false);
717 
718         return retval;
719     }
720 
replaceDeprecatedTextureFormatGLConversion721     static QSSGRenderTextureFormat replaceDeprecatedTextureFormat(QSSGRenderContextType type,
722                                                                   QSSGRenderTextureFormat value,
723                                                                   QSSGRenderTextureSwizzleMode &swizzleMode)
724     {
725         QSSGRenderContextTypes deprecatedContextFlags(QSSGRenderContextType::GL2 | QSSGRenderContextType::GLES2);
726         QSSGRenderTextureFormat newValue = value;
727         swizzleMode = QSSGRenderTextureSwizzleMode::NoSwizzle;
728 
729         if (!(deprecatedContextFlags & type)) {
730             switch (value.format) {
731             case QSSGRenderTextureFormat::Luminance8:
732                 newValue = QSSGRenderTextureFormat::R8;
733                 swizzleMode = QSSGRenderTextureSwizzleMode::L8toR8;
734                 break;
735             case QSSGRenderTextureFormat::LuminanceAlpha8:
736                 newValue = QSSGRenderTextureFormat::RG8;
737                 swizzleMode = QSSGRenderTextureSwizzleMode::L8A8toRG8;
738                 break;
739             case QSSGRenderTextureFormat::Alpha8:
740                 newValue = QSSGRenderTextureFormat::R8;
741                 swizzleMode = QSSGRenderTextureSwizzleMode::A8toR8;
742                 break;
743             case QSSGRenderTextureFormat::Luminance16:
744                 newValue = QSSGRenderTextureFormat::R16;
745                 swizzleMode = QSSGRenderTextureSwizzleMode::L16toR16;
746                 break;
747             default:
748                 break;
749             }
750         }
751 
752         return newValue;
753     }
754 
NVRenderConvertSwizzleModeToGLGLConversion755     static void NVRenderConvertSwizzleModeToGL(const QSSGRenderTextureSwizzleMode swizzleMode, GLint glSwizzle[4])
756     {
757         switch (swizzleMode) {
758         case QSSGRenderTextureSwizzleMode::L16toR16:
759         case QSSGRenderTextureSwizzleMode::L8toR8:
760             glSwizzle[0] = GL_RED;
761             glSwizzle[1] = GL_RED;
762             glSwizzle[2] = GL_RED;
763             glSwizzle[3] = GL_ONE;
764             break;
765         case QSSGRenderTextureSwizzleMode::L8A8toRG8:
766             glSwizzle[0] = GL_RED;
767             glSwizzle[1] = GL_RED;
768             glSwizzle[2] = GL_RED;
769             glSwizzle[3] = GL_GREEN;
770             break;
771         case QSSGRenderTextureSwizzleMode::A8toR8:
772             glSwizzle[0] = GL_ZERO;
773             glSwizzle[1] = GL_ZERO;
774             glSwizzle[2] = GL_ZERO;
775             glSwizzle[3] = GL_RED;
776             break;
777         case QSSGRenderTextureSwizzleMode::NoSwizzle:
778         default:
779             glSwizzle[0] = GL_RED;
780             glSwizzle[1] = GL_GREEN;
781             glSwizzle[2] = GL_BLUE;
782             glSwizzle[3] = GL_ALPHA;
783             break;
784         }
785     }
786 
787 #ifndef GL_ALPHA8
788 #define GL_ALPHA8                         0x803C
789 #endif
790 
791 #ifndef GL_LUMINANCE8
792 #define GL_LUMINANCE8                     0x8040
793 #endif
794 
795 #ifndef GL_LUMINANCE16
796 #define GL_LUMINANCE16                    0x8042
797 #endif
798 
799 #ifndef GL_LUMINANCE8_ALPHA8
800 #define GL_LUMINANCE8_ALPHA8              0x8045
801 #endif
802 
803 #ifndef GL_DEPTH_COMPONENT32
804 #define GL_DEPTH_COMPONENT32              0x81A7
805 #endif
806 
fromGLtoTextureFormatGLConversion807     static QSSGRenderTextureFormat fromGLtoTextureFormat(GLenum internalFormat)
808     {
809         switch (internalFormat) {
810         case GL_R8:
811             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R8);
812         case GL_R16:
813             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R16);
814         case GL_R16F:
815             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R16F);
816         case GL_R32I:
817             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R32I);
818         case GL_R32UI:
819             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R32UI);
820         case GL_R32F:
821             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R32F);
822         case GL_RG8:
823             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG8);
824         case GL_RGBA8:
825             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA8);
826         case GL_RGB8:
827             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8);
828         case GL_SRGB8:
829             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8);
830         case GL_SRGB8_ALPHA8:
831             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8A8);
832         case GL_RGB565:
833             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB565);
834         case GL_ALPHA8:
835             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Alpha8);
836         case GL_LUMINANCE8:
837             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Luminance8);
838         case GL_LUMINANCE16:
839             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Luminance16);
840         case GL_LUMINANCE8_ALPHA8:
841             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::LuminanceAlpha8);
842         case GL_RGBA16F:
843             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA16F);
844         case GL_RG16F:
845             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG16F);
846         case GL_RG32F:
847             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG32F);
848         case GL_RGB32F:
849             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB32F);
850         case GL_RGBA32F:
851             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA32F);
852         case GL_R11F_G11F_B10F:
853             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R11G11B10);
854         case GL_RGB9_E5:
855             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB9E5);
856         case GL_RGB10_A2:
857             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB10_A2);
858         case GL_RGB16F:
859             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB16F);
860         case GL_RGBA32UI:
861             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA32UI);
862         case GL_RGB32UI:
863             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB32UI);
864         case GL_RGBA16UI:
865             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA16UI);
866         case GL_RGB16UI:
867             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB16UI);
868         case GL_RGBA8UI:
869             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA8UI);
870         case GL_RGB8UI:
871             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8UI);
872         case GL_RGBA32I:
873             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA32I);
874         case GL_RGB32I:
875             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB32I);
876         case GL_RGBA16I:
877             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA16I);
878         case GL_RGB16I:
879             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB16I);
880         case GL_RGBA8I:
881             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA8I);
882         case GL_RGB8I:
883             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8I);
884         case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
885             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_DXT1);
886         case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
887             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB_DXT1);
888         case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
889             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_DXT3);
890         case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
891             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_DXT5);
892         case GL_COMPRESSED_R11_EAC:
893             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R11_EAC_UNorm);
894         case GL_COMPRESSED_SIGNED_R11_EAC:
895             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::R11_EAC_SNorm);
896         case GL_COMPRESSED_RG11_EAC:
897             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG11_EAC_UNorm);
898         case GL_COMPRESSED_SIGNED_RG11_EAC:
899             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RG11_EAC_SNorm);
900         case GL_COMPRESSED_RGB8_ETC2:
901             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8_ETC2);
902         case GL_COMPRESSED_SRGB8_ETC2:
903             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_ETC2);
904         case GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2:
905             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8_PunchThrough_Alpha1_ETC2);
906         case GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2:
907             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_PunchThrough_Alpha1_ETC2);
908         case GL_COMPRESSED_RGBA8_ETC2_EAC:
909             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA8_ETC2_EAC);
910         case GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC:
911             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ETC2_EAC);
912 //        case GL_ETC1_RGB8_OES:
913 //            return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGB8_ETC1);
914         case GL_COMPRESSED_RGBA_ASTC_4x4_KHR:
915             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_4x4);
916         case GL_COMPRESSED_RGBA_ASTC_5x4_KHR:
917             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_5x4);
918         case GL_COMPRESSED_RGBA_ASTC_5x5_KHR:
919             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_5x5);
920         case GL_COMPRESSED_RGBA_ASTC_6x5_KHR:
921             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_6x5);
922         case GL_COMPRESSED_RGBA_ASTC_6x6_KHR:
923             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_6x6);
924         case GL_COMPRESSED_RGBA_ASTC_8x5_KHR:
925             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_8x5);
926         case GL_COMPRESSED_RGBA_ASTC_8x6_KHR:
927             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_8x6);
928         case GL_COMPRESSED_RGBA_ASTC_8x8_KHR:
929             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_8x8);
930         case GL_COMPRESSED_RGBA_ASTC_10x5_KHR:
931             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_10x5);
932         case GL_COMPRESSED_RGBA_ASTC_10x6_KHR:
933             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_10x6);
934         case GL_COMPRESSED_RGBA_ASTC_10x8_KHR:
935             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_10x8);
936         case GL_COMPRESSED_RGBA_ASTC_10x10_KHR:
937             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_10x10);
938         case GL_COMPRESSED_RGBA_ASTC_12x10_KHR:
939             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_12x10);
940         case GL_COMPRESSED_RGBA_ASTC_12x12_KHR:
941             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::RGBA_ASTC_12x12);
942         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
943             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_4x4);
944         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
945             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_5x4);
946         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
947             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_5x5);
948         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
949             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_6x5);
950         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
951             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_6x6);
952         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
953             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x5);
954         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
955             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x6);
956         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
957             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x8);
958         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
959             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x5);
960         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
961             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x6);
962         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
963             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x8);
964         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
965             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x10);
966         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
967             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_12x10);
968         case GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
969             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_12x12);
970         case GL_DEPTH_COMPONENT16:
971             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Depth16);
972         case GL_DEPTH_COMPONENT24:
973             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Depth24);
974         case GL_DEPTH_COMPONENT32:
975             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Depth32);
976         case GL_DEPTH24_STENCIL8:
977             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Depth24Stencil8);
978         default:
979             return QSSGRenderTextureFormat(QSSGRenderTextureFormat::Unknown);
980         }
981     }
982 
983 
fromUncompressedTextureFormatToGLGLConversion984     static bool fromUncompressedTextureFormatToGL(QSSGRenderContextType type,
985                                                   QSSGRenderTextureFormat value,
986                                                   GLenum &outFormat,
987                                                   GLenum &outDataType,
988                                                   GLenum &outInternalFormat)
989     {
990         switch (value.format) {
991         case QSSGRenderTextureFormat::R8:
992             if (type == QSSGRenderContextType::GLES2) {
993                 outFormat = GL_ALPHA;
994                 outInternalFormat = GL_ALPHA;
995             } else {
996                 outFormat = GL_RED;
997                 outInternalFormat = GL_R8;
998             }
999             outDataType = GL_UNSIGNED_BYTE;
1000             return true;
1001         case QSSGRenderTextureFormat::RG8:
1002             outFormat = GL_RG;
1003             outInternalFormat = GL_RG8;
1004             outDataType = GL_UNSIGNED_BYTE;
1005             return true;
1006         case QSSGRenderTextureFormat::RGBE8:
1007         case QSSGRenderTextureFormat::RGBA8:
1008             outFormat = GL_RGBA;
1009             outInternalFormat = GL_RGBA8;
1010             outDataType = GL_UNSIGNED_BYTE;
1011             return true;
1012         case QSSGRenderTextureFormat::RGB8:
1013             outFormat = GL_RGB;
1014             outInternalFormat = GL_RGB8;
1015             outDataType = GL_UNSIGNED_BYTE;
1016             return true;
1017         case QSSGRenderTextureFormat::RGB565:
1018             outFormat = GL_RGB;
1019             outInternalFormat = GL_RGB8;
1020             outDataType = GL_UNSIGNED_SHORT_5_6_5;
1021             return true;
1022         case QSSGRenderTextureFormat::RGBA5551:
1023             outFormat = GL_RGBA;
1024             outInternalFormat = GL_RGBA8;
1025             outDataType = GL_UNSIGNED_SHORT_5_5_5_1;
1026             return true;
1027         case QSSGRenderTextureFormat::Alpha8:
1028             outFormat = GL_ALPHA;
1029             outInternalFormat = GL_ALPHA;
1030             outDataType = GL_UNSIGNED_BYTE;
1031             return true;
1032         case QSSGRenderTextureFormat::Luminance8:
1033             outFormat = GL_LUMINANCE;
1034             outInternalFormat = GL_LUMINANCE;
1035             outDataType = GL_UNSIGNED_BYTE;
1036             return true;
1037         case QSSGRenderTextureFormat::LuminanceAlpha8:
1038             outFormat = GL_LUMINANCE_ALPHA;
1039             outInternalFormat = GL_LUMINANCE_ALPHA;
1040             outDataType = GL_UNSIGNED_BYTE;
1041             return true;
1042         case QSSGRenderTextureFormat::Luminance16:
1043 #if defined(QT_OPENGL_ES)
1044             outFormat = GL_LUMINANCE16F_EXT;
1045             outInternalFormat = GL_LUMINANCE16F_EXT;
1046 #else
1047             outFormat = GL_LUMINANCE;
1048             outInternalFormat = GL_LUMINANCE;
1049 #endif
1050             outDataType = GL_UNSIGNED_INT;
1051             return true;
1052         case QSSGRenderTextureFormat::RGB10_A2:
1053             outFormat = GL_RGBA;
1054             outInternalFormat = GL_RGB10_A2;
1055             outDataType = GL_UNSIGNED_INT_10_10_10_2;
1056             return true;
1057         case QSSGRenderTextureFormat::RGB16F:
1058             outFormat = GL_RGB;
1059             outInternalFormat = GL_RGB16F;
1060             outDataType = GL_HALF_FLOAT;
1061             return true;
1062         case QSSGRenderTextureFormat::RGBA32UI:
1063             outFormat = GL_RGBA_INTEGER;
1064             outInternalFormat = GL_RGBA32UI;
1065             outDataType = GL_UNSIGNED_INT;
1066             return true;
1067         case QSSGRenderTextureFormat::RGB32UI:
1068             outFormat = GL_RGB_INTEGER;
1069             outInternalFormat = GL_RGB32UI;
1070             outDataType = GL_UNSIGNED_INT;
1071             return true;
1072         case QSSGRenderTextureFormat::RGBA16UI:
1073             outFormat = GL_RGBA_INTEGER;
1074             outInternalFormat = GL_RGBA16UI;
1075             outDataType = GL_UNSIGNED_SHORT;
1076             return true;
1077         case QSSGRenderTextureFormat::RGB16UI:
1078             outFormat = GL_RGB_INTEGER;
1079             outInternalFormat = GL_RGB16UI;
1080             outDataType = GL_UNSIGNED_SHORT;
1081             return true;
1082         case QSSGRenderTextureFormat::RGBA8UI:
1083             outFormat = GL_RGBA_INTEGER;
1084             outInternalFormat = GL_RGBA8UI;
1085             outDataType = GL_UNSIGNED_BYTE;
1086             return true;
1087         case QSSGRenderTextureFormat::RGB8UI:
1088             outFormat = GL_RGB_INTEGER;
1089             outInternalFormat = GL_RGB8UI;
1090             outDataType = GL_UNSIGNED_BYTE;
1091             return true;
1092         case QSSGRenderTextureFormat::RGBA32I:
1093             outFormat = GL_RGBA_INTEGER;
1094             outInternalFormat = GL_RGBA32I;
1095             outDataType = GL_INT;
1096             return true;
1097         case QSSGRenderTextureFormat::RGB32I:
1098             outFormat = GL_RGB_INTEGER;
1099             outInternalFormat = GL_RGB32I;
1100             outDataType = GL_INT;
1101             return true;
1102         case QSSGRenderTextureFormat::RGBA16I:
1103             outFormat = GL_RGBA_INTEGER;
1104             outInternalFormat = GL_RGBA16I;
1105             outDataType = GL_SHORT;
1106             return true;
1107         case QSSGRenderTextureFormat::RGB16I:
1108             outFormat = GL_RGB_INTEGER;
1109             outInternalFormat = GL_RGB16I;
1110             outDataType = GL_SHORT;
1111             return true;
1112         case QSSGRenderTextureFormat::RGBA8I:
1113             outFormat = GL_RGBA_INTEGER;
1114             outInternalFormat = GL_RGBA8I;
1115             outDataType = GL_BYTE;
1116             return true;
1117         case QSSGRenderTextureFormat::RGB8I:
1118             outFormat = GL_RGB_INTEGER;
1119             outInternalFormat = GL_RGB8I;
1120             outDataType = GL_BYTE;
1121             return true;
1122         default:
1123             break;
1124         }
1125 
1126         QSSGRenderContextTypes contextFlags(QSSGRenderContextType::GL2 | QSSGRenderContextType::GLES2);
1127         // check extented texture formats
1128         if (!(contextFlags & type)) {
1129             switch (value.format) {
1130 #if !defined(QT_OPENGL_ES)
1131             case QSSGRenderTextureFormat::R16: {
1132                 if (IsGlEsContext(type)) {
1133                     outFormat = GL_RED_INTEGER;
1134                     outInternalFormat = GL_R16UI;
1135                 } else {
1136                     outFormat = GL_RED;
1137                     outInternalFormat = GL_R16;
1138                 }
1139                 outDataType = GL_UNSIGNED_SHORT;
1140                 return true;
1141             }
1142 #endif
1143             case QSSGRenderTextureFormat::R16F:
1144                 outFormat = GL_RED;
1145                 outInternalFormat = GL_R16F;
1146                 outDataType = GL_HALF_FLOAT;
1147                 return true;
1148             case QSSGRenderTextureFormat::R32UI:
1149                 outFormat = GL_RED_INTEGER;
1150                 outInternalFormat = GL_R32UI;
1151                 outDataType = GL_UNSIGNED_INT;
1152                 return true;
1153             case QSSGRenderTextureFormat::R32F:
1154                 outFormat = GL_RED;
1155                 outInternalFormat = GL_R32F;
1156                 outDataType = GL_FLOAT;
1157                 return true;
1158             case QSSGRenderTextureFormat::RGBA16F:
1159                 outFormat = GL_RGBA;
1160                 outInternalFormat = GL_RGBA16F;
1161                 outDataType = GL_HALF_FLOAT;
1162                 return true;
1163             case QSSGRenderTextureFormat::RG16F:
1164                 outFormat = GL_RG;
1165                 outInternalFormat = GL_RG16F;
1166                 outDataType = GL_HALF_FLOAT;
1167                 return true;
1168             case QSSGRenderTextureFormat::RG32F:
1169                 outFormat = GL_RG;
1170                 outInternalFormat = GL_RG32F;
1171                 outDataType = GL_FLOAT;
1172                 return true;
1173             case QSSGRenderTextureFormat::RGBA32F:
1174                 outFormat = GL_RGBA;
1175                 outInternalFormat = GL_RGBA32F;
1176                 outDataType = GL_FLOAT;
1177                 return true;
1178             case QSSGRenderTextureFormat::RGB32F:
1179                 outFormat = GL_RGB;
1180                 outInternalFormat = GL_RGB32F;
1181                 outDataType = GL_FLOAT;
1182                 return true;
1183             case QSSGRenderTextureFormat::R11G11B10:
1184                 outFormat = GL_RGB;
1185                 outInternalFormat = GL_R11F_G11F_B10F;
1186                 outDataType = GL_UNSIGNED_INT_10F_11F_11F_REV;
1187                 return true;
1188             case QSSGRenderTextureFormat::RGB9E5:
1189                 outFormat = GL_RGB;
1190                 outInternalFormat = GL_RGB9_E5;
1191                 outDataType = GL_UNSIGNED_INT_5_9_9_9_REV;
1192                 return true;
1193             case QSSGRenderTextureFormat::SRGB8:
1194                 outFormat = GL_RGB;
1195                 outInternalFormat = GL_SRGB8;
1196                 outDataType = GL_UNSIGNED_BYTE;
1197                 return true;
1198             case QSSGRenderTextureFormat::SRGB8A8:
1199                 outFormat = GL_RGBA;
1200                 outInternalFormat = GL_SRGB8_ALPHA8;
1201                 outDataType = GL_UNSIGNED_BYTE;
1202                 return true;
1203             default:
1204                 break;
1205             }
1206         }
1207 
1208         Q_ASSERT(false);
1209         return false;
1210     }
1211 
fromCompressedTextureFormatToGLGLConversion1212     static GLenum fromCompressedTextureFormatToGL(QSSGRenderTextureFormat value)
1213     {
1214         switch (value.format) {
1215         case QSSGRenderTextureFormat::RGBA_DXT1:
1216             return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1217         case QSSGRenderTextureFormat::RGB_DXT1:
1218             return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1219         case QSSGRenderTextureFormat::RGBA_DXT3:
1220             return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
1221         case QSSGRenderTextureFormat::RGBA_DXT5:
1222             return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
1223         case QSSGRenderTextureFormat::R11_EAC_UNorm:
1224             return GL_COMPRESSED_R11_EAC;
1225         case QSSGRenderTextureFormat::R11_EAC_SNorm:
1226             return GL_COMPRESSED_SIGNED_R11_EAC;
1227         case QSSGRenderTextureFormat::RG11_EAC_UNorm:
1228             return GL_COMPRESSED_RG11_EAC;
1229         case QSSGRenderTextureFormat::RG11_EAC_SNorm:
1230             return GL_COMPRESSED_SIGNED_RG11_EAC;
1231         case QSSGRenderTextureFormat::RGB8_ETC2:
1232             return GL_COMPRESSED_RGB8_ETC2;
1233         case QSSGRenderTextureFormat::SRGB8_ETC2:
1234             return GL_COMPRESSED_SRGB8_ETC2;
1235         case QSSGRenderTextureFormat::RGB8_PunchThrough_Alpha1_ETC2:
1236             return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2;
1237         case QSSGRenderTextureFormat::SRGB8_PunchThrough_Alpha1_ETC2:
1238             return GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2;
1239         case QSSGRenderTextureFormat::RGBA8_ETC2_EAC:
1240             return GL_COMPRESSED_RGBA8_ETC2_EAC;
1241         case QSSGRenderTextureFormat::SRGB8_Alpha8_ETC2_EAC:
1242             return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;
1243 //        case QSSGRenderTextureFormat::RGB8_ETC1:
1244 //            return GL_ETC1_RGB8_OES;
1245         case QSSGRenderTextureFormat::RGBA_ASTC_4x4:
1246             return GL_COMPRESSED_RGBA_ASTC_4x4_KHR;
1247         case QSSGRenderTextureFormat::RGBA_ASTC_5x4:
1248             return GL_COMPRESSED_RGBA_ASTC_5x4_KHR;
1249         case QSSGRenderTextureFormat::RGBA_ASTC_5x5:
1250             return GL_COMPRESSED_RGBA_ASTC_5x5_KHR;
1251         case QSSGRenderTextureFormat::RGBA_ASTC_6x5:
1252             return GL_COMPRESSED_RGBA_ASTC_6x5_KHR;
1253         case QSSGRenderTextureFormat::RGBA_ASTC_6x6:
1254             return GL_COMPRESSED_RGBA_ASTC_6x6_KHR;
1255         case QSSGRenderTextureFormat::RGBA_ASTC_8x5:
1256             return GL_COMPRESSED_RGBA_ASTC_8x5_KHR;
1257         case QSSGRenderTextureFormat::RGBA_ASTC_8x6:
1258             return GL_COMPRESSED_RGBA_ASTC_8x6_KHR;
1259         case QSSGRenderTextureFormat::RGBA_ASTC_8x8:
1260             return GL_COMPRESSED_RGBA_ASTC_8x8_KHR;
1261         case QSSGRenderTextureFormat::RGBA_ASTC_10x5:
1262             return GL_COMPRESSED_RGBA_ASTC_10x5_KHR;
1263         case QSSGRenderTextureFormat::RGBA_ASTC_10x6:
1264             return GL_COMPRESSED_RGBA_ASTC_10x6_KHR;
1265         case QSSGRenderTextureFormat::RGBA_ASTC_10x8:
1266             return GL_COMPRESSED_RGBA_ASTC_10x8_KHR;
1267         case QSSGRenderTextureFormat::RGBA_ASTC_10x10:
1268             return GL_COMPRESSED_RGBA_ASTC_10x10_KHR;
1269         case QSSGRenderTextureFormat::RGBA_ASTC_12x10:
1270             return GL_COMPRESSED_RGBA_ASTC_12x10_KHR;
1271         case QSSGRenderTextureFormat::RGBA_ASTC_12x12:
1272             return GL_COMPRESSED_RGBA_ASTC_12x12_KHR;
1273         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_4x4:
1274             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;
1275         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_5x4:
1276             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR;
1277         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_5x5:
1278             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR;
1279         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_6x5:
1280             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR;
1281         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_6x6:
1282             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR;
1283         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x5:
1284             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR;
1285         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x6:
1286             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR;
1287         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_8x8:
1288             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR;
1289         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x5:
1290             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR;
1291         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x6:
1292             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR;
1293         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x8:
1294             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR;
1295         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_10x10:
1296             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR;
1297         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_12x10:
1298             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR;
1299         case QSSGRenderTextureFormat::SRGB8_Alpha8_ASTC_12x12:
1300             return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR;
1301         default:
1302             break;
1303         }
1304 
1305         Q_ASSERT(false);
1306         return 0;
1307     }
1308 
fromDepthTextureFormatToGLGLConversion1309     static bool fromDepthTextureFormatToGL(QSSGRenderContextType type,
1310                                            QSSGRenderTextureFormat value,
1311                                            GLenum &outFormat,
1312                                            GLenum &outDataType,
1313                                            GLenum &outInternalFormat)
1314     {
1315         QSSGRenderContextTypes theContextFlags(QSSGRenderContextType::GLES2 | QSSGRenderContextType::GL2);
1316 
1317         bool supportDepth24 = !(theContextFlags & type);
1318         bool supportDepth32f = !(theContextFlags & type);
1319         bool supportDepth24Stencil8 = !(theContextFlags & type);
1320 
1321         switch (value.format) {
1322         case QSSGRenderTextureFormat::Depth16:
1323             outFormat = GL_DEPTH_COMPONENT;
1324             outInternalFormat = GL_DEPTH_COMPONENT16;
1325             outDataType = GL_UNSIGNED_SHORT;
1326             return true;
1327         case QSSGRenderTextureFormat::Depth24:
1328             outFormat = GL_DEPTH_COMPONENT;
1329             outInternalFormat = (supportDepth24) ? GL_DEPTH_COMPONENT24 : GL_DEPTH_COMPONENT16;
1330             outDataType = (supportDepth24) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
1331             return true;
1332         case QSSGRenderTextureFormat::Depth32:
1333             outFormat = GL_DEPTH_COMPONENT;
1334             outInternalFormat = (supportDepth32f) ? GL_DEPTH_COMPONENT32F : GL_DEPTH_COMPONENT16;
1335             outDataType = (supportDepth32f) ? GL_FLOAT : GL_UNSIGNED_SHORT;
1336             return true;
1337         case QSSGRenderTextureFormat::Depth24Stencil8:
1338             outFormat = (supportDepth24Stencil8) ? GL_DEPTH_STENCIL : GL_DEPTH_COMPONENT;
1339             outInternalFormat = (supportDepth24Stencil8) ? GL_DEPTH24_STENCIL8 : GL_DEPTH_COMPONENT16;
1340             outDataType = (supportDepth24Stencil8) ? GL_UNSIGNED_INT_24_8 : GL_UNSIGNED_SHORT;
1341             return true;
1342         default:
1343             break;
1344         }
1345 
1346         Q_ASSERT(false);
1347         return false;
1348     }
1349 
fromTextureTargetToGLGLConversion1350     static GLenum fromTextureTargetToGL(QSSGRenderTextureTargetType value)
1351     {
1352         GLenum retval = 0;
1353         if (value == QSSGRenderTextureTargetType::Texture2D)
1354             retval = GL_TEXTURE_2D;
1355         else if (value == QSSGRenderTextureTargetType::Texture2D_MS)
1356             retval = GL_TEXTURE_2D_MULTISAMPLE;
1357         else if (value == QSSGRenderTextureTargetType::TextureCube)
1358             retval = GL_TEXTURE_CUBE_MAP;
1359         else if (value == QSSGRenderTextureTargetType::TextureCubeNegX)
1360             retval = GL_TEXTURE_CUBE_MAP_NEGATIVE_X;
1361         else if (value == QSSGRenderTextureTargetType::TextureCubePosX)
1362             retval = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
1363         else if (value == QSSGRenderTextureTargetType::TextureCubeNegY)
1364             retval = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
1365         else if (value == QSSGRenderTextureTargetType::TextureCubePosY)
1366             retval = GL_TEXTURE_CUBE_MAP_POSITIVE_Y;
1367         else if (value == QSSGRenderTextureTargetType::TextureCubeNegZ)
1368             retval = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
1369         else if (value == QSSGRenderTextureTargetType::TextureCubePosZ)
1370             retval = GL_TEXTURE_CUBE_MAP_POSITIVE_Z;
1371         else
1372             Q_ASSERT(false);
1373 
1374         return retval;
1375     }
1376 
fromGLToTextureTargetGLConversion1377     static QSSGRenderTextureTargetType fromGLToTextureTarget(GLenum value)
1378     {
1379         QSSGRenderTextureTargetType retval = QSSGRenderTextureTargetType::Unknown;
1380 
1381         if (value == GL_TEXTURE_2D)
1382             retval = QSSGRenderTextureTargetType::Texture2D;
1383         else if (value == GL_TEXTURE_2D_MULTISAMPLE)
1384             retval = QSSGRenderTextureTargetType::Texture2D_MS;
1385         else
1386             Q_ASSERT(false);
1387 
1388         return retval;
1389     }
1390 
fromTextureUnitToGLGLConversion1391     static GLenum fromTextureUnitToGL(QSSGRenderTextureUnit value)
1392     {
1393         quint32 v = static_cast<quint32>(value);
1394         GLenum retval = GL_TEXTURE0;
1395         retval = GL_TEXTURE0 + v;
1396 
1397         return retval;
1398     }
1399 
fromGLToTextureUnitGLConversion1400     static QSSGRenderTextureUnit fromGLToTextureUnit(GLenum value)
1401     {
1402         Q_ASSERT(value > GL_TEXTURE0);
1403 
1404         quint32 v = value - GL_TEXTURE0;
1405         QSSGRenderTextureUnit retval = QSSGRenderTextureUnit(v);
1406 
1407         return retval;
1408     }
1409 
fromTextureMinifyingOpToGLGLConversion1410     static GLenum fromTextureMinifyingOpToGL(QSSGRenderTextureMinifyingOp value)
1411     {
1412         switch (value) {
1413         case QSSGRenderTextureMinifyingOp::Nearest:
1414             return GL_NEAREST;
1415         case QSSGRenderTextureMinifyingOp::Linear:
1416             return GL_LINEAR;
1417         case QSSGRenderTextureMinifyingOp::NearestMipmapNearest:
1418             return GL_NEAREST_MIPMAP_NEAREST;
1419         case QSSGRenderTextureMinifyingOp::LinearMipmapNearest:
1420             return GL_LINEAR_MIPMAP_NEAREST;
1421         case QSSGRenderTextureMinifyingOp::NearestMipmapLinear:
1422             return GL_NEAREST_MIPMAP_LINEAR;
1423         case QSSGRenderTextureMinifyingOp::LinearMipmapLinear:
1424             return GL_LINEAR_MIPMAP_LINEAR;
1425         default:
1426             break;
1427         }
1428         Q_ASSERT(false);
1429         return 0;
1430     }
1431 
fromGLToTextureMinifyingOpGLConversion1432     static QSSGRenderTextureMinifyingOp fromGLToTextureMinifyingOp(GLenum value)
1433     {
1434         switch (value) {
1435         case GL_NEAREST:
1436             return QSSGRenderTextureMinifyingOp::Nearest;
1437         case GL_LINEAR:
1438             return QSSGRenderTextureMinifyingOp::Linear;
1439         case GL_NEAREST_MIPMAP_NEAREST:
1440             return QSSGRenderTextureMinifyingOp::NearestMipmapNearest;
1441         case GL_LINEAR_MIPMAP_NEAREST:
1442             return QSSGRenderTextureMinifyingOp::LinearMipmapNearest;
1443         case GL_NEAREST_MIPMAP_LINEAR:
1444             return QSSGRenderTextureMinifyingOp::NearestMipmapLinear;
1445         case GL_LINEAR_MIPMAP_LINEAR:
1446             return QSSGRenderTextureMinifyingOp::LinearMipmapLinear;
1447         default:
1448             break;
1449         }
1450         Q_ASSERT(false);
1451         return QSSGRenderTextureMinifyingOp::Unknown;
1452     }
1453 
fromTextureMagnifyingOpToGLGLConversion1454     static GLenum fromTextureMagnifyingOpToGL(QSSGRenderTextureMagnifyingOp value)
1455     {
1456         switch (value) {
1457         case QSSGRenderTextureMagnifyingOp::Nearest:
1458             return GL_NEAREST;
1459         case QSSGRenderTextureMagnifyingOp::Linear:
1460             return GL_LINEAR;
1461         default:
1462             break;
1463         }
1464         Q_ASSERT(false);
1465         return 0;
1466     }
1467 
fromGLToTextureMagnifyingOpGLConversion1468     static QSSGRenderTextureMagnifyingOp fromGLToTextureMagnifyingOp(GLenum value)
1469     {
1470         switch (value) {
1471         case GL_NEAREST:
1472             return QSSGRenderTextureMagnifyingOp::Nearest;
1473         case GL_LINEAR:
1474             return QSSGRenderTextureMagnifyingOp::Linear;
1475         default:
1476             break;
1477         }
1478         Q_ASSERT(false);
1479         return QSSGRenderTextureMagnifyingOp::Unknown;
1480     }
1481 
fromTextureCoordOpToGLGLConversion1482     static GLenum fromTextureCoordOpToGL(QSSGRenderTextureCoordOp value)
1483     {
1484         switch (value) {
1485         case QSSGRenderTextureCoordOp::ClampToEdge:
1486             return GL_CLAMP_TO_EDGE;
1487         case QSSGRenderTextureCoordOp::MirroredRepeat:
1488             return GL_MIRRORED_REPEAT;
1489         case QSSGRenderTextureCoordOp::Repeat:
1490             return GL_REPEAT;
1491         default:
1492             break;
1493         }
1494         Q_ASSERT(false);
1495         return 0;
1496     }
1497 
fromGLToTextureCoordOpGLConversion1498     static QSSGRenderTextureCoordOp fromGLToTextureCoordOp(GLenum value)
1499     {
1500         switch (value) {
1501         case GL_CLAMP_TO_EDGE:
1502             return QSSGRenderTextureCoordOp::ClampToEdge;
1503         case GL_MIRRORED_REPEAT:
1504             return QSSGRenderTextureCoordOp::MirroredRepeat;
1505         case GL_REPEAT:
1506             return QSSGRenderTextureCoordOp::Repeat;
1507         default:
1508             break;
1509         }
1510         Q_ASSERT(false);
1511         return QSSGRenderTextureCoordOp::Unknown;
1512     }
1513 
fromTextureCompareModeToGLGLConversion1514     static GLenum fromTextureCompareModeToGL(QSSGRenderTextureCompareMode value)
1515     {
1516         switch (value) {
1517         case QSSGRenderTextureCompareMode::NoCompare:
1518             return GL_NONE;
1519         case QSSGRenderTextureCompareMode::CompareToRef:
1520             return GL_COMPARE_REF_TO_TEXTURE;
1521         default:
1522             break;
1523         }
1524         Q_ASSERT(false);
1525         return GL_INVALID_ENUM;
1526     }
1527 
fromGLToTextureCompareModeGLConversion1528     static QSSGRenderTextureCompareMode fromGLToTextureCompareMode(GLenum value)
1529     {
1530         switch (value) {
1531         case GL_NONE:
1532             return QSSGRenderTextureCompareMode::NoCompare;
1533         case GL_COMPARE_REF_TO_TEXTURE:
1534             return QSSGRenderTextureCompareMode::CompareToRef;
1535         default:
1536             break;
1537         }
1538         Q_ASSERT(false);
1539         return QSSGRenderTextureCompareMode::Unknown;
1540     }
1541 
fromTextureCompareFuncToGLGLConversion1542     static GLenum fromTextureCompareFuncToGL(QSSGRenderTextureCompareOp value)
1543     {
1544         switch (value) {
1545         case QSSGRenderTextureCompareOp::Never:
1546             return GL_NEVER;
1547         case QSSGRenderTextureCompareOp::Less:
1548             return GL_LESS;
1549         case QSSGRenderTextureCompareOp::Equal:
1550             return GL_EQUAL;
1551         case QSSGRenderTextureCompareOp::LessThanOrEqual:
1552             return GL_LEQUAL;
1553         case QSSGRenderTextureCompareOp::Greater:
1554             return GL_GREATER;
1555         case QSSGRenderTextureCompareOp::NotEqual:
1556             return GL_NOTEQUAL;
1557         case QSSGRenderTextureCompareOp::GreaterThanOrEqual:
1558             return GL_GEQUAL;
1559         case QSSGRenderTextureCompareOp::AlwaysTrue:
1560             return GL_ALWAYS;
1561         default:
1562             break;
1563         }
1564         Q_ASSERT(false);
1565         return 0;
1566     }
1567 
fromImageFormatToGLGLConversion1568     static GLenum fromImageFormatToGL(QSSGRenderTextureFormat value)
1569     {
1570         switch (value.format) {
1571         case QSSGRenderTextureFormat::R8:
1572             return GL_R8;
1573         case QSSGRenderTextureFormat::R32I:
1574             return GL_R32I;
1575         case QSSGRenderTextureFormat::R32UI:
1576             return GL_R32UI;
1577         case QSSGRenderTextureFormat::R32F:
1578             return GL_R32F;
1579         case QSSGRenderTextureFormat::RGBE8:
1580         case QSSGRenderTextureFormat::RGBA8:
1581             return GL_RGBA8;
1582         case QSSGRenderTextureFormat::SRGB8A8:
1583             return GL_RGBA8_SNORM;
1584         case QSSGRenderTextureFormat::RG16F:
1585             return GL_RG16F;
1586         case QSSGRenderTextureFormat::RGBA16F:
1587             return GL_RGBA16F;
1588         case QSSGRenderTextureFormat::RGBA32F:
1589             return GL_RGBA32F;
1590         default:
1591             break;
1592         }
1593 
1594         Q_ASSERT(false);
1595         return GL_INVALID_ENUM;
1596     }
1597 
fromImageAccessToGLGLConversion1598     static GLenum fromImageAccessToGL(QSSGRenderImageAccessType value)
1599     {
1600         switch (value) {
1601         case QSSGRenderImageAccessType::Read:
1602             return GL_READ_ONLY;
1603         case QSSGRenderImageAccessType::Write:
1604             return GL_WRITE_ONLY;
1605         case QSSGRenderImageAccessType::ReadWrite:
1606             return GL_READ_WRITE;
1607         default:
1608             break;
1609         }
1610         Q_ASSERT(false);
1611         return GL_INVALID_ENUM;
1612     }
1613 
fromBufferAccessBitToGLGLConversion1614     static GLbitfield fromBufferAccessBitToGL(QSSGRenderBufferAccessFlags flags)
1615     {
1616         GLbitfield retval = 0;
1617 
1618         if (flags & QSSGRenderBufferAccessTypeValues::Read)
1619             retval |= GL_MAP_READ_BIT;
1620         if (flags & QSSGRenderBufferAccessTypeValues::Write)
1621             retval |= GL_MAP_WRITE_BIT;
1622         if (flags & QSSGRenderBufferAccessTypeValues::Invalid)
1623             retval |= GL_MAP_INVALIDATE_BUFFER_BIT;
1624         if (flags & QSSGRenderBufferAccessTypeValues::InvalidRange)
1625             retval |= GL_MAP_INVALIDATE_RANGE_BIT;
1626 
1627         Q_ASSERT(retval);
1628         return retval;
1629     }
1630 
fromMemoryBarrierFlagsToGLGLConversion1631     static GLbitfield fromMemoryBarrierFlagsToGL(QSSGRenderBufferBarrierFlags flags)
1632     {
1633         GLbitfield retval = 0;
1634 #if !defined(QT_OPENGL_ES)
1635         if (flags & QSSGRenderBufferBarrierValues::AtomicCounter)
1636             retval |= GL_ATOMIC_COUNTER_BARRIER_BIT;
1637         if (flags & QSSGRenderBufferBarrierValues::BufferUpdate)
1638             retval |= GL_BUFFER_UPDATE_BARRIER_BIT;
1639         if (flags & QSSGRenderBufferBarrierValues::CommandBuffer)
1640             retval |= GL_COMMAND_BARRIER_BIT;
1641         if (flags & QSSGRenderBufferBarrierValues::ElementArray)
1642             retval |= GL_ELEMENT_ARRAY_BARRIER_BIT;
1643         if (flags & QSSGRenderBufferBarrierValues::Framebuffer)
1644             retval |= GL_FRAMEBUFFER_BARRIER_BIT;
1645         if (flags & QSSGRenderBufferBarrierValues::PixelBuffer)
1646             retval |= GL_PIXEL_BUFFER_BARRIER_BIT;
1647         if (flags & QSSGRenderBufferBarrierValues::ShaderImageAccess)
1648             retval |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT;
1649         if (flags & QSSGRenderBufferBarrierValues::ShaderStorage)
1650             retval |= GL_SHADER_STORAGE_BARRIER_BIT;
1651         if (flags & QSSGRenderBufferBarrierValues::TextureFetch)
1652             retval |= GL_TEXTURE_FETCH_BARRIER_BIT;
1653         if (flags & QSSGRenderBufferBarrierValues::TextureUpdate)
1654             retval |= GL_TEXTURE_UPDATE_BARRIER_BIT;
1655         if (flags & QSSGRenderBufferBarrierValues::TransformFeedback)
1656             retval |= GL_TRANSFORM_FEEDBACK_BARRIER_BIT;
1657         if (flags & QSSGRenderBufferBarrierValues::UniformBuffer)
1658             retval |= GL_UNIFORM_BARRIER_BIT;
1659         if (flags & QSSGRenderBufferBarrierValues::VertexAttribArray)
1660             retval |= GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT;
1661         Q_ASSERT(retval);
1662 #else
1663         Q_UNUSED(flags);
1664 #endif
1665         return retval;
1666     }
1667 
fromShaderTypeFlagsToGLGLConversion1668     static GLbitfield fromShaderTypeFlagsToGL(QSSGRenderShaderTypeFlags flags)
1669     {
1670         GLbitfield retval = 0;
1671         if (flags & QSSGRenderShaderTypeValue::Vertex)
1672             retval |= GL_VERTEX_SHADER_BIT;
1673         if (flags & QSSGRenderShaderTypeValue::Fragment)
1674             retval |= GL_FRAGMENT_SHADER_BIT;
1675         if (flags & QSSGRenderShaderTypeValue::TessControl)
1676             retval |= GL_TESS_CONTROL_SHADER_BIT;
1677         if (flags & QSSGRenderShaderTypeValue::TessEvaluation)
1678             retval |= GL_TESS_EVALUATION_SHADER_BIT;
1679         if (flags & QSSGRenderShaderTypeValue::Geometry)
1680 #if defined(QT_OPENGL_ES_3_1)
1681             retval |= GL_GEOMETRY_SHADER_BIT_EXT;
1682 #else
1683             retval |= GL_GEOMETRY_SHADER_BIT;
1684 #endif
1685         Q_ASSERT(retval || !flags);
1686         return retval;
1687     }
1688 
fromPropertyDataTypesToShaderGLGLConversion1689     static GLenum fromPropertyDataTypesToShaderGL(QSSGRenderShaderDataType value)
1690     {
1691         // cube Sampler and mat22 unsupported
1692         switch (value) {
1693         case QSSGRenderShaderDataType::Float:
1694             return GL_FLOAT;
1695         case QSSGRenderShaderDataType::Vec2:
1696             return GL_FLOAT_VEC2;
1697         case QSSGRenderShaderDataType::Vec3:
1698             return GL_FLOAT_VEC3;
1699         case QSSGRenderShaderDataType::Vec4:
1700             return GL_FLOAT_VEC4;
1701         case QSSGRenderShaderDataType::Integer:
1702             return GL_INT;
1703         case QSSGRenderShaderDataType::IntegerVec2:
1704             return GL_INT_VEC2;
1705         case QSSGRenderShaderDataType::IntegerVec3:
1706             return GL_INT_VEC3;
1707         case QSSGRenderShaderDataType::IntegerVec4:
1708             return GL_INT_VEC4;
1709         case QSSGRenderShaderDataType::Boolean:
1710             return GL_BOOL;
1711         case QSSGRenderShaderDataType::BooleanVec2:
1712             return GL_BOOL_VEC2;
1713         case QSSGRenderShaderDataType::BooleanVec3:
1714             return GL_BOOL_VEC3;
1715         case QSSGRenderShaderDataType::BooleanVec4:
1716             return GL_BOOL_VEC4;
1717         case QSSGRenderShaderDataType::UnsignedInteger:
1718             return GL_UNSIGNED_INT;
1719         case QSSGRenderShaderDataType::UnsignedIntegerVec2:
1720             return GL_UNSIGNED_INT_VEC2;
1721         case QSSGRenderShaderDataType::UnsignedIntegerVec3:
1722             return GL_UNSIGNED_INT_VEC3;
1723         case QSSGRenderShaderDataType::UnsignedIntegerVec4:
1724             return GL_UNSIGNED_INT_VEC4;
1725         case QSSGRenderShaderDataType::Matrix3x3:
1726             return GL_FLOAT_MAT3;
1727         case QSSGRenderShaderDataType::Matrix4x4:
1728             return GL_FLOAT_MAT4;
1729         case QSSGRenderShaderDataType::Texture2D:
1730             return GL_SAMPLER_2D;
1731         case QSSGRenderShaderDataType::TextureCube:
1732             return GL_SAMPLER_CUBE;
1733         case QSSGRenderShaderDataType::Image2D:
1734             return GL_IMAGE_2D;
1735         default:
1736             break;
1737         }
1738         Q_ASSERT(false);
1739         return 0;
1740     }
1741 
fromShaderGLToPropertyDataTypesGLConversion1742     static QSSGRenderShaderDataType fromShaderGLToPropertyDataTypes(GLenum value)
1743     {
1744         // cube Sampler and mat22 unsupported
1745         switch (value) {
1746         case GL_FLOAT:
1747             return QSSGRenderShaderDataType::Float;
1748         case GL_FLOAT_VEC2:
1749             return QSSGRenderShaderDataType::Vec2;
1750         case GL_FLOAT_VEC3:
1751             return QSSGRenderShaderDataType::Vec3;
1752         case GL_FLOAT_VEC4:
1753             return QSSGRenderShaderDataType::Vec4;
1754         case GL_INT:
1755             return QSSGRenderShaderDataType::Integer;
1756         case GL_INT_VEC2:
1757             return QSSGRenderShaderDataType::IntegerVec2;
1758         case GL_INT_VEC3:
1759             return QSSGRenderShaderDataType::IntegerVec3;
1760         case GL_INT_VEC4:
1761             return QSSGRenderShaderDataType::IntegerVec4;
1762         case GL_BOOL:
1763             return QSSGRenderShaderDataType::Boolean;
1764         case GL_BOOL_VEC2:
1765             return QSSGRenderShaderDataType::BooleanVec2;
1766         case GL_BOOL_VEC3:
1767             return QSSGRenderShaderDataType::BooleanVec3;
1768         case GL_BOOL_VEC4:
1769             return QSSGRenderShaderDataType::BooleanVec4;
1770         case GL_UNSIGNED_INT:
1771             return QSSGRenderShaderDataType::UnsignedInteger;
1772         case GL_UNSIGNED_INT_VEC2:
1773             return QSSGRenderShaderDataType::UnsignedIntegerVec2;
1774         case GL_UNSIGNED_INT_VEC3:
1775             return QSSGRenderShaderDataType::UnsignedIntegerVec3;
1776         case GL_UNSIGNED_INT_VEC4:
1777             return QSSGRenderShaderDataType::UnsignedIntegerVec4;
1778         case GL_FLOAT_MAT3:
1779             return QSSGRenderShaderDataType::Matrix3x3;
1780         case GL_FLOAT_MAT4:
1781             return QSSGRenderShaderDataType::Matrix4x4;
1782         case GL_SAMPLER_2D:
1783             return QSSGRenderShaderDataType::Texture2D;
1784         case GL_SAMPLER_CUBE:
1785             return QSSGRenderShaderDataType::TextureCube;
1786         case GL_IMAGE_2D:
1787             return QSSGRenderShaderDataType::Image2D;
1788         case GL_SAMPLER_2D_SHADOW:
1789             return QSSGRenderShaderDataType::Texture2D;
1790 #if !defined(QT_OPENGL_ES)
1791         case GL_UNSIGNED_INT_ATOMIC_COUNTER:
1792             return QSSGRenderShaderDataType::Integer;
1793         case GL_UNSIGNED_INT_IMAGE_2D:
1794             return QSSGRenderShaderDataType::Image2D;
1795 #endif
1796         default:
1797             break;
1798         }
1799         Q_ASSERT(false);
1800         return QSSGRenderShaderDataType::Unknown;
1801     }
1802 
fromComponentTypeAndNumCompsToAttribGLGLConversion1803     static GLenum fromComponentTypeAndNumCompsToAttribGL(QSSGRenderComponentType compType, quint32 numComps)
1804     {
1805         if (compType == QSSGRenderComponentType::Float32) {
1806             switch (numComps) {
1807             case 1:
1808                 return GL_FLOAT;
1809             case 2:
1810                 return GL_FLOAT_VEC2;
1811             case 3:
1812                 return GL_FLOAT_VEC3;
1813             case 4:
1814                 return GL_FLOAT_VEC4;
1815             case 9:
1816                 return GL_FLOAT_MAT3;
1817             case 16:
1818                 return GL_FLOAT_MAT4;
1819             default:
1820                 break;
1821             }
1822         }
1823         Q_ASSERT(false);
1824         return 0;
1825     }
1826 
fromAttribGLToComponentTypeAndNumCompsGLConversion1827     static void fromAttribGLToComponentTypeAndNumComps(GLenum enumVal, QSSGRenderComponentType &outCompType, quint32 &outNumComps)
1828     {
1829         switch (enumVal) {
1830         case GL_FLOAT:
1831             outCompType = QSSGRenderComponentType::Float32;
1832             outNumComps = 1;
1833             return;
1834         case GL_FLOAT_VEC2:
1835             outCompType = QSSGRenderComponentType::Float32;
1836             outNumComps = 2;
1837             return;
1838         case GL_FLOAT_VEC3:
1839             outCompType = QSSGRenderComponentType::Float32;
1840             outNumComps = 3;
1841             return;
1842         case GL_FLOAT_VEC4:
1843             outCompType = QSSGRenderComponentType::Float32;
1844             outNumComps = 4;
1845             return;
1846         case GL_FLOAT_MAT2:
1847             outCompType = QSSGRenderComponentType::Float32;
1848             outNumComps = 4;
1849             return;
1850         case GL_FLOAT_MAT3:
1851             outCompType = QSSGRenderComponentType::Float32;
1852             outNumComps = 9;
1853             return;
1854         case GL_FLOAT_MAT4:
1855             outCompType = QSSGRenderComponentType::Float32;
1856             outNumComps = 16;
1857             return;
1858         default:
1859             break;
1860         }
1861         Q_ASSERT(false);
1862         outCompType = QSSGRenderComponentType::Unknown;
1863         outNumComps = 0;
1864     }
1865 
fromRenderBufferFormatsToRenderBufferGLGLConversion1866     static GLenum fromRenderBufferFormatsToRenderBufferGL(QSSGRenderRenderBufferFormat value)
1867     {
1868         switch (value) {
1869 #if defined(GL_DEPTH_COMPONENT32)
1870         case QSSGRenderRenderBufferFormat::RGBA4:
1871             return GL_RGBA4;
1872         case QSSGRenderRenderBufferFormat::RGB565:
1873             return GL_RGB565;
1874         case QSSGRenderRenderBufferFormat::RGBA5551:
1875             return GL_RGB5_A1;
1876         case QSSGRenderRenderBufferFormat::Depth16:
1877             return GL_DEPTH_COMPONENT16;
1878         case QSSGRenderRenderBufferFormat::Depth24:
1879             return GL_DEPTH_COMPONENT24;
1880         case QSSGRenderRenderBufferFormat::Depth32:
1881             return GL_DEPTH_COMPONENT32;
1882         case QSSGRenderRenderBufferFormat::StencilIndex8:
1883             return GL_STENCIL_INDEX8;
1884 #else
1885         case QSSGRenderRenderBufferFormat::RGBA4:
1886             return GL_RGBA4;
1887         case QSSGRenderRenderBufferFormat::RGB565:
1888             return GL_RGB565;
1889         case QSSGRenderRenderBufferFormat::RGBA5551:
1890             return GL_RGB5_A1;
1891         case QSSGRenderRenderBufferFormat::Depth16:
1892             return GL_DEPTH_COMPONENT16;
1893         case QSSGRenderRenderBufferFormat::Depth24:
1894             return GL_DEPTH_COMPONENT24;
1895         case QSSGRenderRenderBufferFormat::StencilIndex8:
1896             return GL_STENCIL_INDEX8;
1897 #endif
1898         default:
1899             break;
1900         }
1901         Q_ASSERT(false);
1902         return 0;
1903     }
1904 
fromRenderBufferGLToRenderBufferFormatsGLConversion1905     static QSSGRenderRenderBufferFormat fromRenderBufferGLToRenderBufferFormats(GLenum value)
1906     {
1907 #if defined(GL_DEPTH_COMPONENT32)
1908         switch (value) {
1909         case GL_RGBA4:
1910             return QSSGRenderRenderBufferFormat::RGBA4;
1911         case GL_RGB565:
1912             return QSSGRenderRenderBufferFormat::RGB565;
1913         case GL_RGB5_A1:
1914             return QSSGRenderRenderBufferFormat::RGBA5551;
1915         case GL_DEPTH_COMPONENT16:
1916             return QSSGRenderRenderBufferFormat::Depth16;
1917         case GL_DEPTH_COMPONENT24:
1918             return QSSGRenderRenderBufferFormat::Depth24;
1919         case GL_DEPTH_COMPONENT32:
1920             return QSSGRenderRenderBufferFormat::Depth32;
1921         case GL_STENCIL_INDEX8:
1922             return QSSGRenderRenderBufferFormat::StencilIndex8;
1923 #else
1924         switch (value) {
1925         case GL_RGBA4:
1926             return QSSGRenderRenderBufferFormat::RGBA4;
1927         case GL_RGB565:
1928             return QSSGRenderRenderBufferFormat::RGB565;
1929         case GL_RGB5_A1:
1930             return QSSGRenderRenderBufferFormat::RGBA5551;
1931         case GL_DEPTH_COMPONENT16:
1932             return QSSGRenderRenderBufferFormat::Depth16;
1933         case GL_DEPTH_COMPONENT24:
1934             return QSSGRenderRenderBufferFormat::Depth24;
1935         case GL_STENCIL_INDEX8:
1936             return QSSGRenderRenderBufferFormat::StencilIndex8;
1937 #endif
1938         default:
1939             break;
1940         }
1941         Q_ASSERT(false);
1942         return QSSGRenderRenderBufferFormat::Unknown;
1943     }
1944 
1945     static GLenum fromFramebufferAttachmentsToGL(QSSGRenderFrameBufferAttachment value)
1946     {
1947         switch (value) {
1948         case QSSGRenderFrameBufferAttachment::Color0:
1949             return GL_COLOR_ATTACHMENT0;
1950         case QSSGRenderFrameBufferAttachment::Color1:
1951             return GL_COLOR_ATTACHMENT1;
1952         case QSSGRenderFrameBufferAttachment::Color2:
1953             return GL_COLOR_ATTACHMENT2;
1954         case QSSGRenderFrameBufferAttachment::Color3:
1955             return GL_COLOR_ATTACHMENT3;
1956         case QSSGRenderFrameBufferAttachment::Color4:
1957             return GL_COLOR_ATTACHMENT4;
1958         case QSSGRenderFrameBufferAttachment::Color5:
1959             return GL_COLOR_ATTACHMENT5;
1960         case QSSGRenderFrameBufferAttachment::Color6:
1961             return GL_COLOR_ATTACHMENT6;
1962         case QSSGRenderFrameBufferAttachment::Color7:
1963             return GL_COLOR_ATTACHMENT7;
1964         case QSSGRenderFrameBufferAttachment::Depth:
1965             return GL_DEPTH_ATTACHMENT;
1966         case QSSGRenderFrameBufferAttachment::Stencil:
1967             return GL_STENCIL_ATTACHMENT;
1968         case QSSGRenderFrameBufferAttachment::DepthStencil:
1969             return GL_DEPTH_STENCIL_ATTACHMENT;
1970         default:
1971             break;
1972         }
1973         Q_ASSERT(false);
1974         return 0;
1975     }
1976 
1977     static QSSGRenderFrameBufferAttachment fromGLToFramebufferAttachments(GLenum value)
1978     {
1979         switch (value) {
1980         case GL_COLOR_ATTACHMENT0:
1981             return QSSGRenderFrameBufferAttachment::Color0;
1982         case GL_COLOR_ATTACHMENT1:
1983             return QSSGRenderFrameBufferAttachment::Color1;
1984         case GL_COLOR_ATTACHMENT2:
1985             return QSSGRenderFrameBufferAttachment::Color2;
1986         case GL_COLOR_ATTACHMENT3:
1987             return QSSGRenderFrameBufferAttachment::Color3;
1988         case GL_COLOR_ATTACHMENT4:
1989             return QSSGRenderFrameBufferAttachment::Color4;
1990         case GL_COLOR_ATTACHMENT5:
1991             return QSSGRenderFrameBufferAttachment::Color5;
1992         case GL_COLOR_ATTACHMENT6:
1993             return QSSGRenderFrameBufferAttachment::Color6;
1994         case GL_COLOR_ATTACHMENT7:
1995             return QSSGRenderFrameBufferAttachment::Color7;
1996         case GL_DEPTH_ATTACHMENT:
1997             return QSSGRenderFrameBufferAttachment::Depth;
1998         case GL_STENCIL_ATTACHMENT:
1999             return QSSGRenderFrameBufferAttachment::Stencil;
2000         case GL_DEPTH_STENCIL_ATTACHMENT:
2001             return QSSGRenderFrameBufferAttachment::DepthStencil;
2002         }
2003         Q_ASSERT(false);
2004         return QSSGRenderFrameBufferAttachment::Unknown;
2005     }
2006 
2007     static GLbitfield fromClearFlagsToGL(QSSGRenderClearFlags flags)
2008     {
2009         GLbitfield retval = 0;
2010         if ((flags & QSSGRenderClearValues::Color))
2011             retval |= GL_COLOR_BUFFER_BIT;
2012         if ((flags & QSSGRenderClearValues::Depth))
2013             retval |= GL_DEPTH_BUFFER_BIT;
2014         if ((flags & QSSGRenderClearValues::Stencil))
2015             retval |= GL_STENCIL_BUFFER_BIT;
2016 
2017         return retval;
2018     }
2019 
2020     static QSSGRenderClearFlags fromGLToClearFlags(GLbitfield value)
2021     {
2022         QSSGRenderClearFlags retval;
2023         if ((value & GL_COLOR_BUFFER_BIT))
2024             retval |= QSSGRenderClearValues::Color;
2025         if ((value & GL_DEPTH_BUFFER_BIT))
2026             retval |= QSSGRenderClearValues::Depth;
2027         if ((value & GL_STENCIL_BUFFER_BIT))
2028             retval |= QSSGRenderClearValues::Stencil;
2029 
2030         return retval;
2031     }
2032 
2033     static GLenum fromDrawModeToGL(QSSGRenderDrawMode value, bool inTesselationSupported)
2034     {
2035         switch (value) {
2036         case QSSGRenderDrawMode::Points:
2037             return GL_POINTS;
2038         case QSSGRenderDrawMode::Lines:
2039             return GL_LINES;
2040         case QSSGRenderDrawMode::LineStrip:
2041             return GL_LINE_STRIP;
2042         case QSSGRenderDrawMode::LineLoop:
2043             return GL_LINE_LOOP;
2044         case QSSGRenderDrawMode::TriangleStrip:
2045             return GL_TRIANGLE_STRIP;
2046         case QSSGRenderDrawMode::TriangleFan:
2047             return GL_TRIANGLE_FAN;
2048         case QSSGRenderDrawMode::Triangles:
2049             return GL_TRIANGLES;
2050         case QSSGRenderDrawMode::Patches:
2051             return (inTesselationSupported) ? GL_PATCHES : GL_TRIANGLES;
2052         default:
2053             break;
2054         }
2055         Q_ASSERT(false);
2056         return GL_INVALID_ENUM;
2057     }
2058 
2059     static QSSGRenderDrawMode fromGLToDrawMode(GLenum value)
2060     {
2061         switch (value) {
2062         case GL_POINTS:
2063             return QSSGRenderDrawMode::Points;
2064         case GL_LINES:
2065             return QSSGRenderDrawMode::Lines;
2066         case GL_LINE_STRIP:
2067             return QSSGRenderDrawMode::LineStrip;
2068         case GL_LINE_LOOP:
2069             return QSSGRenderDrawMode::LineLoop;
2070         case GL_TRIANGLE_STRIP:
2071             return QSSGRenderDrawMode::TriangleStrip;
2072         case GL_TRIANGLE_FAN:
2073             return QSSGRenderDrawMode::TriangleFan;
2074         case GL_TRIANGLES:
2075             return QSSGRenderDrawMode::Triangles;
2076         case GL_PATCHES:
2077             return QSSGRenderDrawMode::Patches;
2078         default:
2079             break;
2080         }
2081         Q_ASSERT(false);
2082         return QSSGRenderDrawMode::Unknown;
2083     }
2084 
2085     static GLenum fromRenderStateToGL(QSSGRenderState value)
2086     {
2087         switch (value) {
2088         case QSSGRenderState::Blend:
2089             return GL_BLEND;
2090         case QSSGRenderState::CullFace:
2091             return GL_CULL_FACE;
2092         case QSSGRenderState::DepthTest:
2093             return GL_DEPTH_TEST;
2094         case QSSGRenderState::Multisample:
2095             return GL_MULTISAMPLE;
2096         case QSSGRenderState::StencilTest:
2097             return GL_STENCIL_TEST;
2098         case QSSGRenderState::ScissorTest:
2099             return GL_SCISSOR_TEST;
2100         default:
2101             break;
2102         }
2103         Q_ASSERT(false);
2104         return 0;
2105     }
2106 
2107     static QSSGRenderState fromGLToRenderState(GLenum value)
2108     {
2109         switch (value) {
2110         case GL_BLEND:
2111             return QSSGRenderState::Blend;
2112         case GL_CULL_FACE:
2113             return QSSGRenderState::CullFace;
2114         case GL_DEPTH_TEST:
2115             return QSSGRenderState::DepthTest;
2116         case GL_MULTISAMPLE:
2117             return QSSGRenderState::Multisample;
2118         case GL_STENCIL_TEST:
2119             return QSSGRenderState::StencilTest;
2120         case GL_SCISSOR_TEST:
2121             return QSSGRenderState::ScissorTest;
2122         default:
2123             break;
2124         }
2125         Q_ASSERT(false);
2126         return QSSGRenderState::Unknown;
2127     }
2128 
2129     static bool fromReadPixelsToGlFormatAndType(QSSGRenderReadPixelFormat inReadPixels, GLuint *outFormat, GLuint *outType)
2130     {
2131         switch (inReadPixels) {
2132         case QSSGRenderReadPixelFormat::Alpha8:
2133             *outFormat = GL_ALPHA;
2134             *outType = GL_UNSIGNED_BYTE;
2135             break;
2136         case QSSGRenderReadPixelFormat::RGB565:
2137             *outFormat = GL_RGB;
2138             *outType = GL_UNSIGNED_SHORT_5_6_5;
2139             break;
2140         case QSSGRenderReadPixelFormat::RGB8:
2141             *outFormat = GL_RGB;
2142             *outType = GL_UNSIGNED_BYTE;
2143             break;
2144         case QSSGRenderReadPixelFormat::RGBA4444:
2145             *outFormat = GL_RGBA;
2146             *outType = GL_UNSIGNED_SHORT_4_4_4_4;
2147             break;
2148         case QSSGRenderReadPixelFormat::RGBA5551:
2149             *outFormat = GL_RGBA;
2150             *outType = GL_UNSIGNED_SHORT_5_5_5_1;
2151             break;
2152         case QSSGRenderReadPixelFormat::RGBA8:
2153             *outFormat = GL_RGBA;
2154             *outType = GL_UNSIGNED_BYTE;
2155             break;
2156         default:
2157             *outFormat = 0;
2158             *outType = 0;
2159             Q_ASSERT(false);
2160             return false;
2161         };
2162 
2163         return true;
2164     }
2165 };
2166 
2167 QT_END_NAMESPACE
2168 
2169 #endif // QSSGOPENGLUTIL_H
2170