1/****************************************************************************
2**
3** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB)
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtOpenGL module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38**
39** This file was generated by glgen version 0.1
40** Command line was: glgen
41**
42** glgen is Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB)
43**
44** This is an auto-generated file.
45** Do not edit! All changes made to it will be lost.
46**
47****************************************************************************/
48
49#include "qopenglversionfunctionsfactory.h"
50#include "qopenglversionfunctions_p.h"
51
52#if !QT_CONFIG(opengles2)
53#include "qopenglfunctions_4_5_core.h"
54#include "qopenglfunctions_4_5_compatibility.h"
55#include "qopenglfunctions_4_4_core.h"
56#include "qopenglfunctions_4_4_compatibility.h"
57#include "qopenglfunctions_4_3_core.h"
58#include "qopenglfunctions_4_3_compatibility.h"
59#include "qopenglfunctions_4_2_core.h"
60#include "qopenglfunctions_4_2_compatibility.h"
61#include "qopenglfunctions_4_1_core.h"
62#include "qopenglfunctions_4_1_compatibility.h"
63#include "qopenglfunctions_4_0_core.h"
64#include "qopenglfunctions_4_0_compatibility.h"
65#include "qopenglfunctions_3_3_core.h"
66#include "qopenglfunctions_3_3_compatibility.h"
67#include "qopenglfunctions_3_2_core.h"
68#include "qopenglfunctions_3_2_compatibility.h"
69#include "qopenglfunctions_3_1.h"
70#include "qopenglfunctions_3_0.h"
71#include "qopenglfunctions_2_1.h"
72#include "qopenglfunctions_2_0.h"
73#include "qopenglfunctions_1_5.h"
74#include "qopenglfunctions_1_4.h"
75#include "qopenglfunctions_1_3.h"
76#include "qopenglfunctions_1_2.h"
77#include "qopenglfunctions_1_1.h"
78#include "qopenglfunctions_1_0.h"
79#else
80#include "qopenglfunctions_es2.h"
81#endif
82
83QT_BEGIN_NAMESPACE
84
85static QAbstractOpenGLFunctions *createFunctions(const QOpenGLVersionProfile &versionProfile)
86{
87#if !QT_CONFIG(opengles2)
88 const int major = versionProfile.version().first;
89 const int minor = versionProfile.version().second;
90
91 if (versionProfile.hasProfiles()) {
92 switch (versionProfile.profile()) {
93 case QSurfaceFormat::CoreProfile:
94 if (major == 4 && minor == 5)
95 return new QOpenGLFunctions_4_5_Core;
96 else if (major == 4 && minor == 4)
97 return new QOpenGLFunctions_4_4_Core;
98 else if (major == 4 && minor == 3)
99 return new QOpenGLFunctions_4_3_Core;
100 else if (major == 4 && minor == 2)
101 return new QOpenGLFunctions_4_2_Core;
102 else if (major == 4 && minor == 1)
103 return new QOpenGLFunctions_4_1_Core;
104 else if (major == 4 && minor == 0)
105 return new QOpenGLFunctions_4_0_Core;
106 else if (major == 3 && minor == 3)
107 return new QOpenGLFunctions_3_3_Core;
108 else if (major == 3 && minor == 2)
109 return new QOpenGLFunctions_3_2_Core;
110 break;
111
112 case QSurfaceFormat::CompatibilityProfile:
113 if (major == 4 && minor == 5)
114 return new QOpenGLFunctions_4_5_Compatibility;
115 else if (major == 4 && minor == 4)
116 return new QOpenGLFunctions_4_4_Compatibility;
117 else if (major == 4 && minor == 3)
118 return new QOpenGLFunctions_4_3_Compatibility;
119 else if (major == 4 && minor == 2)
120 return new QOpenGLFunctions_4_2_Compatibility;
121 else if (major == 4 && minor == 1)
122 return new QOpenGLFunctions_4_1_Compatibility;
123 else if (major == 4 && minor == 0)
124 return new QOpenGLFunctions_4_0_Compatibility;
125 else if (major == 3 && minor == 3)
126 return new QOpenGLFunctions_3_3_Compatibility;
127 else if (major == 3 && minor == 2)
128 return new QOpenGLFunctions_3_2_Compatibility;
129 break;
130
131 case QSurfaceFormat::NoProfile:
132 default:
133 break;
134 };
135 } else {
136 if (major == 3 && minor == 1)
137 return new QOpenGLFunctions_3_1;
138 else if (major == 3 && minor == 0)
139 return new QOpenGLFunctions_3_0;
140 else if (major == 2 && minor == 1)
141 return new QOpenGLFunctions_2_1;
142 else if (major == 2 && minor == 0)
143 return new QOpenGLFunctions_2_0;
144 else if (major == 1 && minor == 5)
145 return new QOpenGLFunctions_1_5;
146 else if (major == 1 && minor == 4)
147 return new QOpenGLFunctions_1_4;
148 else if (major == 1 && minor == 3)
149 return new QOpenGLFunctions_1_3;
150 else if (major == 1 && minor == 2)
151 return new QOpenGLFunctions_1_2;
152 else if (major == 1 && minor == 1)
153 return new QOpenGLFunctions_1_1;
154 else if (major == 1 && minor == 0)
155 return new QOpenGLFunctions_1_0;
156 }
157 return nullptr;
158#else
159 Q_UNUSED(versionProfile);
160 return new QOpenGLFunctions_ES2;
161#endif
162}
163
164/*!
165 \class QOpenGLVersionFunctionsFactory
166 \inmodule QtOpenGL
167 \since 6.0
168 \brief Provides access to OpenGL functions for a specified version and
169 profile.
170*/
171
172/*!
173 \fn static T *QOpenGLVersionFunctionsFactory::get(QOpenGLContext *context)
174
175 \overload get()
176
177 Returns a pointer to an object that provides access to all functions for
178 the version and profile of this context. There is no need to call
179 QAbstractOpenGLFunctions::initializeOpenGLFunctions() as long as the \a context
180 is current. It is also possible to call this function when the \a context is not
181 current, but in that case it is the caller's responsibility to ensure proper
182 initialization by calling QAbstractOpenGLFunctions::initializeOpenGLFunctions()
183 afterwards.
184
185 Usually one would use the template version of this function to automatically
186 have the result cast to the correct type.
187
188 \code
189 auto funcs = QOpenGLVersionFunctionsFactory::get<QOpenGLFunctions_3_3_Core>(context);
190 if (!funcs) {
191 qFatal("Could not obtain required OpenGL context version");
192 }
193 \endcode
194
195 It is possible to request a functions object for a different version and profile
196 than that for which the context was created. To do this either use the template
197 version of this function specifying the desired functions object type as the
198 template parameter or by passing in a QOpenGLVersionProfile object as an argument
199 to the non-template function.
200
201 Note that requests for function objects of other versions or profiles can fail and
202 in doing so will return \nullptr. Situations in which creation of the functions
203 object can fail are if the request cannot be satisfied due to asking for functions
204 that are not in the version or profile of this context. For example:
205
206 \list
207 \li Requesting a 3.3 core profile functions object would succeed.
208 \li Requesting a 3.3 compatibility profile functions object would fail. We would fail
209 to resolve the deprecated functions.
210 \li Requesting a 4.3 core profile functions object would fail. We would fail to resolve
211 the new core functions introduced in versions 4.0-4.3.
212 \li Requesting a 3.1 functions object would succeed. There is nothing in 3.1 that is not
213 also in 3.3 core.
214 \endlist
215
216 Note that if creating a functions object via this method that the QOpenGLContext
217 retains ownership of the object. This is to allow the object to be cached and shared.
218*/
219
220/*!
221 Returns a pointer to an object that provides access to all functions for the
222 \a versionProfile of the \a context. There is no need to call
223 QAbstractOpenGLFunctions::initializeOpenGLFunctions() as long as the \a context
224 is current. It is also possible to call this function when the \a context is not
225 current, but in that case it is the caller's responsibility to ensure proper
226 initialization by calling QAbstractOpenGLFunctions::initializeOpenGLFunctions()
227 afterwards.
228
229 Usually one would use the template version of this function to automatically
230 have the result cast to the correct type.
231*/
232QAbstractOpenGLFunctions *QOpenGLVersionFunctionsFactory::get(const QOpenGLVersionProfile &versionProfile, QOpenGLContext *context)
233{
234 if (!context)
235 context = QOpenGLContext::currentContext();
236
237 if (!context) {
238 qWarning("versionFunctions: No OpenGL context");
239 return nullptr;
240 }
241
242#if !QT_CONFIG(opengles2)
243 if (context->isOpenGLES()) {
244 qWarning("versionFunctions: Not supported on OpenGL ES");
245 return nullptr;
246 }
247#endif // !QT_CONFIG(opengles2)
248
249 const QSurfaceFormat f = context->format();
250
251 // Ensure we have a valid version and profile. Default to context's if none specified
252 QOpenGLVersionProfile vp = versionProfile;
253 if (!vp.isValid())
254 vp = QOpenGLVersionProfile(f);
255
256 // Check that context is compatible with requested version
257 const QPair<int, int> v = qMakePair(f.majorVersion(), f.minorVersion());
258 if (v < vp.version())
259 return nullptr;
260
261 // If this context only offers core profile functions then we can't create
262 // function objects for legacy or compatibility profile requests
263 if (((vp.hasProfiles() && vp.profile() != QSurfaceFormat::CoreProfile) || vp.isLegacyVersion())
264 && f.profile() == QSurfaceFormat::CoreProfile)
265 return nullptr;
266
267 // Create object if suitable one not cached
268 QAbstractOpenGLFunctions* funcs = nullptr;
269 // TODO: replace with something else
270 auto *data = QOpenGLContextVersionData::forContext(context);
271 auto it = data->functions.constFind(vp);
272 if (it == data->functions.constEnd()) {
273 funcs = createFunctions(vp);
274 if (funcs) {
275 funcs->setOwningContext(context);
276 data->functions.insert(vp, funcs);
277 }
278 } else {
279 funcs = it.value();
280 }
281
282 if (funcs && QOpenGLContext::currentContext() == context)
283 funcs->initializeOpenGLFunctions();
284
285 return funcs;
286}
287
288QT_END_NAMESPACE
289