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 | |
83 | QT_BEGIN_NAMESPACE |
84 | |
85 | static 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 | \fn static T *QOpenGLVersionFunctionsFactory::get(QOpenGLContext *context) |
166 | |
167 | \overload get() |
168 | |
169 | Returns a pointer to an object that provides access to all functions for |
170 | the version and profile of this context. There is no need to call |
171 | QAbstractOpenGLFunctions::initializeOpenGLFunctions() as long as the \a context |
172 | is current. It is also possible to call this function when the \a context is not |
173 | current, but in that case it is the caller's responsibility to ensure proper |
174 | initialization by calling QAbstractOpenGLFunctions::initializeOpenGLFunctions() |
175 | afterwards. |
176 | |
177 | Usually one would use the template version of this function to automatically |
178 | have the result cast to the correct type. |
179 | |
180 | \code |
181 | auto funcs = QOpenGLVersionFunctionsFactory::get<QOpenGLFunctions_3_3_Core>(context); |
182 | if (!funcs) { |
183 | qFatal("Could not obtain required OpenGL context version"); |
184 | } |
185 | \endcode |
186 | |
187 | It is possible to request a functions object for a different version and profile |
188 | than that for which the context was created. To do this either use the template |
189 | version of this function specifying the desired functions object type as the |
190 | template parameter or by passing in a QOpenGLVersionProfile object as an argument |
191 | to the non-template function. |
192 | |
193 | Note that requests for function objects of other versions or profiles can fail and |
194 | in doing so will return \nullptr. Situations in which creation of the functions |
195 | object can fail are if the request cannot be satisfied due to asking for functions |
196 | that are not in the version or profile of this context. For example: |
197 | |
198 | \list |
199 | \li Requesting a 3.3 core profile functions object would succeed. |
200 | \li Requesting a 3.3 compatibility profile functions object would fail. We would fail |
201 | to resolve the deprecated functions. |
202 | \li Requesting a 4.3 core profile functions object would fail. We would fail to resolve |
203 | the new core functions introduced in versions 4.0-4.3. |
204 | \li Requesting a 3.1 functions object would succeed. There is nothing in 3.1 that is not |
205 | also in 3.3 core. |
206 | \endlist |
207 | |
208 | Note that if creating a functions object via this method that the QOpenGLContext |
209 | retains ownership of the object. This is to allow the object to be cached and shared. |
210 | */ |
211 | |
212 | /*! |
213 | Returns a pointer to an object that provides access to all functions for the |
214 | \a versionProfile of the \a context. There is no need to call |
215 | QAbstractOpenGLFunctions::initializeOpenGLFunctions() as long as the \a context |
216 | is current. It is also possible to call this function when the \a context is not |
217 | current, but in that case it is the caller's responsibility to ensure proper |
218 | initialization by calling QAbstractOpenGLFunctions::initializeOpenGLFunctions() |
219 | afterwards. |
220 | |
221 | Usually one would use the template version of this function to automatically |
222 | have the result cast to the correct type. |
223 | */ |
224 | QAbstractOpenGLFunctions *QOpenGLVersionFunctionsFactory::get(const QOpenGLVersionProfile &versionProfile, QOpenGLContext *context) |
225 | { |
226 | if (!context) |
227 | context = QOpenGLContext::currentContext(); |
228 | |
229 | if (!context) { |
230 | qWarning("versionFunctions: No OpenGL context" ); |
231 | return nullptr; |
232 | } |
233 | |
234 | #if !QT_CONFIG(opengles2) |
235 | if (context->isOpenGLES()) { |
236 | qWarning("versionFunctions: Not supported on OpenGL ES" ); |
237 | return nullptr; |
238 | } |
239 | #endif // !QT_CONFIG(opengles2) |
240 | |
241 | const QSurfaceFormat f = context->format(); |
242 | |
243 | // Ensure we have a valid version and profile. Default to context's if none specified |
244 | QOpenGLVersionProfile vp = versionProfile; |
245 | if (!vp.isValid()) |
246 | vp = QOpenGLVersionProfile(f); |
247 | |
248 | // Check that context is compatible with requested version |
249 | const QPair<int, int> v = qMakePair(f.majorVersion(), f.minorVersion()); |
250 | if (v < vp.version()) |
251 | return nullptr; |
252 | |
253 | // If this context only offers core profile functions then we can't create |
254 | // function objects for legacy or compatibility profile requests |
255 | if (((vp.hasProfiles() && vp.profile() != QSurfaceFormat::CoreProfile) || vp.isLegacyVersion()) |
256 | && f.profile() == QSurfaceFormat::CoreProfile) |
257 | return nullptr; |
258 | |
259 | // Create object if suitable one not cached |
260 | QAbstractOpenGLFunctions* funcs = nullptr; |
261 | // TODO: replace with something else |
262 | auto *data = QOpenGLContextVersionData::forContext(context); |
263 | auto it = data->functions.constFind(vp); |
264 | if (it == data->functions.constEnd()) { |
265 | funcs = createFunctions(vp); |
266 | if (funcs) { |
267 | funcs->setOwningContext(context); |
268 | data->functions.insert(vp, funcs); |
269 | } |
270 | } else { |
271 | funcs = it.value(); |
272 | } |
273 | |
274 | if (funcs && QOpenGLContext::currentContext() == context) |
275 | funcs->initializeOpenGLFunctions(); |
276 | |
277 | return funcs; |
278 | } |
279 | |
280 | QT_END_NAMESPACE |
281 | |