Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qopenglversionfunctions.cpp
Go to the documentation of this file.
1// Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB)
2// Copyright (C) 2016 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4/***************************************************************************
5** This file was generated by glgen version 0.1
6** Command line was: glgen
7**
8** glgen is Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB)
9**
10** This is an auto-generated file.
11** Do not edit! All changes made to it will be lost.
12**
13****************************************************************************/
14// Qt-Security score:significant reason:default
15
18#include "qopenglcontext.h"
19#include "qdebug.h"
20
22
23QOpenGLContextVersionData::~QOpenGLContextVersionData()
24{
25 for (auto *f : std::as_const(externalFunctions)) {
26 auto *fp = QAbstractOpenGLFunctionsPrivate::get(f);
27 fp->owningContext = nullptr;
28 fp->initialized = false;
29 }
30 externalFunctions.clear();
31 qDeleteAll(functions);
32 functions.clear();
33}
34
36{
37 QOpenGLContextPrivate *context_d = QOpenGLContextPrivate::get(context);
38 if (context_d->versionFunctions == nullptr)
39 context_d->versionFunctions = new QOpenGLContextVersionData;
40
41 return static_cast<QOpenGLContextVersionData *>(context_d->versionFunctions);
42}
43
44#define QT_OPENGL_COUNT_FUNCTIONS(ret, name, args) +1
45#define QT_OPENGL_FUNCTION_NAMES(ret, name, args)
46 "gl"#name"\0"
47#define QT_OPENGL_IMPLEMENT(CLASS, FUNCTIONS) void
48 CLASS::init() \
49{
50 const char *names = FUNCTIONS(QT_OPENGL_FUNCTION_NAMES);
51 const char *name = names;
52 for (int i = 0; i < FUNCTIONS(QT_OPENGL_COUNT_FUNCTIONS); ++i) {
53 functions[i] = context->getProcAddress(name);
54 name += strlen(name) + 1;
55 } \
56}
57
62
64{
65#if !QT_CONFIG(opengles2)
66 if (backends) {
67
68 int i = 0;
69
70#define DELETE_BACKEND(X)
71 if (backends[i] && !--backends[i]->refs)
72 delete static_cast<QOpenGLFunctions_##X##Backend*>(backends[i]);
73 ++i;
74
75 QT_OPENGL_VERSIONS(DELETE_BACKEND)
76#undef DELETE_BACKEND
77 delete[] backends;
78 }
79#endif
80}
81
83{
84#if QT_CONFIG(opengles2)
85 Q_UNUSED(context);
86 Q_UNUSED(v);
87 return 0;
88#else
89 if (!backends) {
92 }
93 if (backends[v])
94 return backends[v];
95
96 switch(v) {
97#define VERSION_ENUM(X) QOpenGLVersionFunctionsBackend::OpenGL_##X
98#define CREATE_BACKEND(X)
99 case VERSION_ENUM(X):
100 backends[VERSION_ENUM(X)] = new QOpenGLFunctions_##X##Backend(context);
101 break;
102 QT_OPENGL_VERSIONS(CREATE_BACKEND)
104 Q_UNREACHABLE();
105 }
106 // the storage keeps one ref
107 ++backends[v]->refs;
108 return backends[v];
109#endif
110}
111
117
118void QAbstractOpenGLFunctionsPrivate::insertExternalFunctions(QOpenGLContext *context, QAbstractOpenGLFunctions *f)
119{
120 Q_ASSERT(context);
121 QOpenGLContextVersionData::forContext(context)->externalFunctions.insert(f);
122}
123
124void QAbstractOpenGLFunctionsPrivate::removeExternalFunctions(QOpenGLContext *context, QAbstractOpenGLFunctions *f)
125{
126 Q_ASSERT(context);
127 QOpenGLContextVersionData::forContext(context)->externalFunctions.remove(f);
128}
129
130/*!
131 \class QAbstractOpenGLFunctions
132 \inmodule QtOpenGL
133 \since 5.1
134 \brief The QAbstractOpenGLFunctions class is the base class of a family of
135 classes that expose all functions for each OpenGL version and
136 profile.
137
138 OpenGL implementations on different platforms are able to link to a variable
139 number of OpenGL functions depending upon the OpenGL ABI on that platform.
140 For example, on Microsoft Windows only functions up to those in OpenGL 1.1
141 can be linked to at build time. All other functions must be resolved at
142 runtime. The traditional solution to this has been to use either
143 QOpenGLContext::getProcAddress() or QOpenGLFunctions. The former is tedious
144 and error prone and means dealing directly with function pointers. The
145 latter only exposes those functions common to OpenGL ES 2 and desktop
146 OpenGL. There is however much new OpenGL functionality that is useful when
147 writing real world OpenGL applications.
148
149 Qt now provides a family of classes which all inherit from
150 QAbstractOpenGLFunctions which expose every core OpenGL function by way of a
151 corresponding member function. There is a class for every valid combination
152 of OpenGL version and profile. Each class follows the naming convention:
153 \badcode
154 QOpenGLFunctions_<MAJOR VERSION>_<MINOR VERSION>[_PROFILE]
155 \endcode
156
157 For OpenGL versions 1.0 through to 3.0 there are no profiles, leading to the
158 classes:
159
160 \list
161 \li QOpenGLFunctions_1_0
162 \li QOpenGLFunctions_1_1
163 \li QOpenGLFunctions_1_2
164 \li QOpenGLFunctions_1_3
165 \li QOpenGLFunctions_1_4
166 \li QOpenGLFunctions_1_5
167 \li QOpenGLFunctions_2_0
168 \li QOpenGLFunctions_2_1
169 \li QOpenGLFunctions_3_0
170 \endlist
171
172 where each class inherits from QAbstractOpenGLFunctions.
173
174 OpenGL version 3.1 removed many deprecated functions leading to a much
175 simpler and generic API.
176
177 With OpenGL 3.2 the concept of profiles was introduced. Two profiles are
178 currently defined for OpenGL: Core and Compatibility.
179
180 The Core profile does not include any of the functions that were removed
181 in OpenGL 3.1. The Compatibility profile contains all functions in the
182 Core profile of the same version plus all of the functions that were
183 removed in OpenGL 3.1. In this way the Compatibility profile classes allow
184 use of newer OpenGL functionality but also allows you to keep using your
185 legacy OpenGL code. For new OpenGL code the Core profile should be
186 preferred.
187
188 Please note that some vendors, notably Apple, do not implement the
189 Compatibility profile. Therefore if you wish to target new OpenGL features
190 on \macos then you should ensure that you request a Core profile context via
191 QSurfaceFormat::setProfile().
192
193 Qt provides classes for all version and Core and Compatibility profile
194 combinations. The classes for OpenGL versions 3.1 through to 4.3 are:
195
196 \list
197 \li QOpenGLFunctions_3_1
198 \li QOpenGLFunctions_3_2_Core
199 \li QOpenGLFunctions_3_2_Compatibility
200 \li QOpenGLFunctions_3_3_Core
201 \li QOpenGLFunctions_3_3_Compatibility
202 \li QOpenGLFunctions_4_0_Core
203 \li QOpenGLFunctions_4_0_Compatibility
204 \li QOpenGLFunctions_4_1_Core
205 \li QOpenGLFunctions_4_1_Compatibility
206 \li QOpenGLFunctions_4_2_Core
207 \li QOpenGLFunctions_4_2_Compatibility
208 \li QOpenGLFunctions_4_3_Core
209 \li QOpenGLFunctions_4_3_Compatibility
210 \endlist
211
212 where each class inherits from QAbstractOpenGLFunctions.
213
214 A pointer to an object of the class corresponding to the version and
215 profile of OpenGL in use can be obtained from
216 QOpenGLVersionFunctionsFactory::get(). If obtained in this way, note that
217 the QOpenGLContext retains ownership of the object. This is so that the
218 instance can be cached and shared.
219
220 Before calling any of the exposed OpenGL functions you must ensure that the
221 object has resolved the function pointers to the OpenGL functions. This
222 only needs to be done once per instance with initializeOpenGLFunctions().
223 Once initialized, the object can be used to call any OpenGL function for
224 the corresponding version and profile. Note that initializeOpenGLFunctions()
225 can fail in some circumstances so check the return value. Situations in
226 which initialization can fail are if you have a functions object for a version
227 or profile that contains functions that are not part of the context being
228 used to resolve the function pointers.
229
230 If you exclusively use function objects then you will get compile time
231 errors if you attempt to use a function not included in that version and
232 profile. This is obviously a lot easier to debug than undefined behavior
233 at run time.
234
235 \sa QOpenGLVersionFunctionsFactory::get()
236*/
237/*!
238 Constructs a QAbstractOpenGLFunctions object.
239*/
240QAbstractOpenGLFunctions::QAbstractOpenGLFunctions()
241 : d_ptr(new QAbstractOpenGLFunctionsPrivate)
242{
243}
244
245/*!
246 Destroys a QAbstractOpenGLFunctions object.
247*/
248QAbstractOpenGLFunctions::~QAbstractOpenGLFunctions()
249{
250 Q_D(QAbstractOpenGLFunctions);
251 if (d->owningContext)
252 d->removeExternalFunctions(d->owningContext, this);
253 delete d_ptr;
254}
255
256/*! \internal
257 */
258bool QAbstractOpenGLFunctions::initializeOpenGLFunctions()
259{
260 Q_D(QAbstractOpenGLFunctions);
261 d->initialized = true;
262
263 // For a subclass whose instance is not created via
264 // QOpenGLContext::versionFunctions() owningContext is not set. Set it now
265 // and register such instances to the context as external ones. These are
266 // not owned by the context but still need certain cleanup when the context
267 // is destroyed.
268 if (!d->owningContext) {
269 d->owningContext = QOpenGLContext::currentContext();
270 if (d->owningContext)
271 d->insertExternalFunctions(d->owningContext, this);
272 }
273
274 return true;
275}
276
277/*! \internal
278 */
279bool QAbstractOpenGLFunctions::isInitialized() const
280{
281 Q_D(const QAbstractOpenGLFunctions);
282 return d->initialized;
283}
284
285/*! \internal
286 */
287void QAbstractOpenGLFunctions::setOwningContext(const QOpenGLContext *context)
288{
289 Q_D(QAbstractOpenGLFunctions);
290 d->owningContext = const_cast<QOpenGLContext*>(context);
291}
292
293/*! \internal
294 */
295QOpenGLContext *QAbstractOpenGLFunctions::owningContext() const
296{
297 Q_D(const QAbstractOpenGLFunctions);
298 return d->owningContext;
299}
300
301#if !QT_CONFIG(opengles2)
302
303QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_0_CoreBackend, QT_OPENGL_1_0_FUNCTIONS)
304QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_1_CoreBackend, QT_OPENGL_1_1_FUNCTIONS)
305
306QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_2_CoreBackend, QT_OPENGL_1_2_FUNCTIONS)
307QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_3_CoreBackend, QT_OPENGL_1_3_FUNCTIONS)
308QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_4_CoreBackend, QT_OPENGL_1_4_FUNCTIONS)
309QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_5_CoreBackend, QT_OPENGL_1_5_FUNCTIONS)
310QT_OPENGL_IMPLEMENT(QOpenGLFunctions_2_0_CoreBackend, QT_OPENGL_2_0_FUNCTIONS)
311QT_OPENGL_IMPLEMENT(QOpenGLFunctions_2_1_CoreBackend, QT_OPENGL_2_1_FUNCTIONS)
312QT_OPENGL_IMPLEMENT(QOpenGLFunctions_3_0_CoreBackend, QT_OPENGL_3_0_FUNCTIONS)
313QT_OPENGL_IMPLEMENT(QOpenGLFunctions_3_1_CoreBackend, QT_OPENGL_3_1_FUNCTIONS)
314QT_OPENGL_IMPLEMENT(QOpenGLFunctions_3_2_CoreBackend, QT_OPENGL_3_2_FUNCTIONS)
315QT_OPENGL_IMPLEMENT(QOpenGLFunctions_3_3_CoreBackend, QT_OPENGL_3_3_FUNCTIONS)
316QT_OPENGL_IMPLEMENT(QOpenGLFunctions_4_0_CoreBackend, QT_OPENGL_4_0_FUNCTIONS)
317QT_OPENGL_IMPLEMENT(QOpenGLFunctions_4_1_CoreBackend, QT_OPENGL_4_1_FUNCTIONS)
318QT_OPENGL_IMPLEMENT(QOpenGLFunctions_4_2_CoreBackend, QT_OPENGL_4_2_FUNCTIONS)
319QT_OPENGL_IMPLEMENT(QOpenGLFunctions_4_3_CoreBackend, QT_OPENGL_4_3_FUNCTIONS)
320QT_OPENGL_IMPLEMENT(QOpenGLFunctions_4_4_CoreBackend, QT_OPENGL_4_4_FUNCTIONS)
321QT_OPENGL_IMPLEMENT(QOpenGLFunctions_4_5_CoreBackend, QT_OPENGL_4_5_FUNCTIONS)
322
323QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_0_DeprecatedBackend, QT_OPENGL_1_0_DEPRECATED_FUNCTIONS)
324QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_1_DeprecatedBackend, QT_OPENGL_1_1_DEPRECATED_FUNCTIONS)
325
326QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_2_DeprecatedBackend, QT_OPENGL_1_2_DEPRECATED_FUNCTIONS)
327QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_3_DeprecatedBackend, QT_OPENGL_1_3_DEPRECATED_FUNCTIONS)
328QT_OPENGL_IMPLEMENT(QOpenGLFunctions_1_4_DeprecatedBackend, QT_OPENGL_1_4_DEPRECATED_FUNCTIONS)
329QT_OPENGL_IMPLEMENT(QOpenGLFunctions_2_0_DeprecatedBackend, QT_OPENGL_2_0_DEPRECATED_FUNCTIONS)
330QT_OPENGL_IMPLEMENT(QOpenGLFunctions_3_0_DeprecatedBackend, QT_OPENGL_3_0_DEPRECATED_FUNCTIONS)
331QT_OPENGL_IMPLEMENT(QOpenGLFunctions_3_3_DeprecatedBackend, QT_OPENGL_3_3_DEPRECATED_FUNCTIONS)
332QT_OPENGL_IMPLEMENT(QOpenGLFunctions_4_5_DeprecatedBackend, QT_OPENGL_4_5_DEPRECATED_FUNCTIONS)
333
334#else
335
336// No backends for OpenGL ES 2
337
338#endif // !QT_CONFIG(opengles2)
339
340QT_END_NAMESPACE
static QOpenGLVersionFunctionsBackend * functionsBackend(QOpenGLContext *context, QOpenGLVersionFunctionsBackend::Version v)
static void removeExternalFunctions(QOpenGLContext *context, QAbstractOpenGLFunctions *f)
static void insertExternalFunctions(QOpenGLContext *context, QAbstractOpenGLFunctions *f)
QOpenGLVersionFunctionsStorage functionsStorage
static QOpenGLContextVersionData * forContext(QOpenGLContext *context)
QOpenGLVersionFunctionsBackend ** backends
QOpenGLVersionFunctionsBackend * backend(QOpenGLContext *context, QOpenGLVersionFunctionsBackend::Version v)
Combined button and popup list for selecting options.
#define QT_OPENGL_COUNT_FUNCTIONS(ret, name, args)
#define QT_OPENGL_FUNCTION_NAMES(ret, name, args)
#define VERSION_ENUM(X)
#define QT_OPENGL_VERSIONS(F)