Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/opengl/qopenglvertexarrayobject.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sean Harmer <sean.harmer@kdab.com> | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | #include "qopenglvertexarrayobject.h" | - | ||||||||||||||||||
35 | - | |||||||||||||||||||
36 | #include <QtCore/private/qobject_p.h> | - | ||||||||||||||||||
37 | #include <QtGui/qopenglcontext.h> | - | ||||||||||||||||||
38 | #include <QtGui/qoffscreensurface.h> | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | #include <QtGui/qopenglfunctions_3_0.h> | - | ||||||||||||||||||
41 | #include <QtGui/qopenglfunctions_3_2_core.h> | - | ||||||||||||||||||
42 | - | |||||||||||||||||||
43 | #include <private/qopenglextensions_p.h> | - | ||||||||||||||||||
44 | #include <private/qopenglvertexarrayobject_p.h> | - | ||||||||||||||||||
45 | - | |||||||||||||||||||
46 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | class QOpenGLFunctions_3_0; | - | ||||||||||||||||||
49 | class QOpenGLFunctions_3_2_Core; | - | ||||||||||||||||||
50 | - | |||||||||||||||||||
51 | void qtInitializeVertexArrayObjectHelper(QOpenGLVertexArrayObjectHelper *helper, QOpenGLContext *context) | - | ||||||||||||||||||
52 | { | - | ||||||||||||||||||
53 | Q_ASSERT(helper); | - | ||||||||||||||||||
54 | Q_ASSERT(context); | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | bool tryARB = true; | - | ||||||||||||||||||
57 | - | |||||||||||||||||||
58 | if (context->isOpenGLES()) {
| 0 | ||||||||||||||||||
59 | if (context->format().majorVersion() >= 3) {
| 0 | ||||||||||||||||||
60 | QOpenGLES3Helper *es3 = static_cast<QOpenGLExtensions *>(context->functions())->gles3Helper(); | - | ||||||||||||||||||
61 | helper->GenVertexArrays = es3->GenVertexArrays; | - | ||||||||||||||||||
62 | helper->DeleteVertexArrays = es3->DeleteVertexArrays; | - | ||||||||||||||||||
63 | helper->BindVertexArray = es3->BindVertexArray; | - | ||||||||||||||||||
64 | helper->IsVertexArray = es3->IsVertexArray; | - | ||||||||||||||||||
65 | tryARB = false; | - | ||||||||||||||||||
66 | } else if (context->hasExtension(QByteArrayLiteral("GL_OES_vertex_array_object"))) { never executed: end of block never executed: return ba;
| 0 | ||||||||||||||||||
67 | helper->GenVertexArrays = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_GenVertexArrays_t>(context->getProcAddress(QByteArrayLiteral("glGenVertexArraysOES"))); never executed: return ba; | 0 | ||||||||||||||||||
68 | helper->DeleteVertexArrays = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_DeleteVertexArrays_t>(context->getProcAddress(QByteArrayLiteral("glDeleteVertexArraysOES"))); never executed: return ba; | 0 | ||||||||||||||||||
69 | helper->BindVertexArray = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_BindVertexArray_t>(context->getProcAddress(QByteArrayLiteral("glBindVertexArrayOES"))); never executed: return ba; | 0 | ||||||||||||||||||
70 | helper->IsVertexArray = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_IsVertexArray_t>(context->getProcAddress(QByteArrayLiteral("glIsVertexArrayOES"))); never executed: return ba; | 0 | ||||||||||||||||||
71 | tryARB = false; | - | ||||||||||||||||||
72 | } never executed: end of block | 0 | ||||||||||||||||||
73 | } else if (context->hasExtension(QByteArrayLiteral("GL_APPLE_vertex_array_object")) && never executed: end of block never executed: return ba;
| 0 | ||||||||||||||||||
74 | !context->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { never executed: return ba;
| 0 | ||||||||||||||||||
75 | helper->GenVertexArrays = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_GenVertexArrays_t>(context->getProcAddress(QByteArrayLiteral("glGenVertexArraysAPPLE"))); never executed: return ba; | 0 | ||||||||||||||||||
76 | helper->DeleteVertexArrays = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_DeleteVertexArrays_t>(context->getProcAddress(QByteArrayLiteral("glDeleteVertexArraysAPPLE"))); never executed: return ba; | 0 | ||||||||||||||||||
77 | helper->BindVertexArray = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_BindVertexArray_t>(context->getProcAddress(QByteArrayLiteral("glBindVertexArrayAPPLE"))); never executed: return ba; | 0 | ||||||||||||||||||
78 | helper->IsVertexArray = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_IsVertexArray_t>(context->getProcAddress(QByteArrayLiteral("glIsVertexArrayAPPLE"))); never executed: return ba; | 0 | ||||||||||||||||||
79 | tryARB = false; | - | ||||||||||||||||||
80 | } never executed: end of block | 0 | ||||||||||||||||||
81 | - | |||||||||||||||||||
82 | if (tryARB && context->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { never executed: return ba;
| 0 | ||||||||||||||||||
83 | helper->GenVertexArrays = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_GenVertexArrays_t>(context->getProcAddress(QByteArrayLiteral("glGenVertexArrays"))); never executed: return ba; | 0 | ||||||||||||||||||
84 | helper->DeleteVertexArrays = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_DeleteVertexArrays_t>(context->getProcAddress(QByteArrayLiteral("glDeleteVertexArrays"))); never executed: return ba; | 0 | ||||||||||||||||||
85 | helper->BindVertexArray = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_BindVertexArray_t>(context->getProcAddress(QByteArrayLiteral("glBindVertexArray"))); never executed: return ba; | 0 | ||||||||||||||||||
86 | helper->IsVertexArray = reinterpret_cast<QOpenGLVertexArrayObjectHelper::qt_IsVertexArray_t>(context->getProcAddress(QByteArrayLiteral("glIsVertexArray"))); never executed: return ba; | 0 | ||||||||||||||||||
87 | } never executed: end of block | 0 | ||||||||||||||||||
88 | } never executed: end of block | 0 | ||||||||||||||||||
89 | - | |||||||||||||||||||
90 | class QOpenGLVertexArrayObjectPrivate : public QObjectPrivate | - | ||||||||||||||||||
91 | { | - | ||||||||||||||||||
92 | public: | - | ||||||||||||||||||
93 | QOpenGLVertexArrayObjectPrivate() | - | ||||||||||||||||||
94 | : vao(0) | - | ||||||||||||||||||
95 | , vaoFuncsType(NotSupported) | - | ||||||||||||||||||
96 | , context(0) | - | ||||||||||||||||||
97 | { | - | ||||||||||||||||||
98 | } never executed: end of block | 0 | ||||||||||||||||||
99 | - | |||||||||||||||||||
100 | ~QOpenGLVertexArrayObjectPrivate() | - | ||||||||||||||||||
101 | { | - | ||||||||||||||||||
102 | if (vaoFuncsType == ARB || vaoFuncsType == APPLE || vaoFuncsType == OES)
| 0 | ||||||||||||||||||
103 | delete vaoFuncs.helper; never executed: delete vaoFuncs.helper; | 0 | ||||||||||||||||||
104 | } never executed: end of block | 0 | ||||||||||||||||||
105 | - | |||||||||||||||||||
106 | bool create(); | - | ||||||||||||||||||
107 | void destroy(); | - | ||||||||||||||||||
108 | void bind(); | - | ||||||||||||||||||
109 | void release(); | - | ||||||||||||||||||
110 | void _q_contextAboutToBeDestroyed(); | - | ||||||||||||||||||
111 | - | |||||||||||||||||||
112 | Q_DECLARE_PUBLIC(QOpenGLVertexArrayObject) | - | ||||||||||||||||||
113 | - | |||||||||||||||||||
114 | GLuint vao; | - | ||||||||||||||||||
115 | - | |||||||||||||||||||
116 | union { | - | ||||||||||||||||||
117 | QOpenGLFunctions_3_0 *core_3_0; | - | ||||||||||||||||||
118 | QOpenGLFunctions_3_2_Core *core_3_2; | - | ||||||||||||||||||
119 | QOpenGLVertexArrayObjectHelper *helper; | - | ||||||||||||||||||
120 | } vaoFuncs; | - | ||||||||||||||||||
121 | enum { | - | ||||||||||||||||||
122 | NotSupported, | - | ||||||||||||||||||
123 | Core_3_0, | - | ||||||||||||||||||
124 | Core_3_2, | - | ||||||||||||||||||
125 | ARB, | - | ||||||||||||||||||
126 | APPLE, | - | ||||||||||||||||||
127 | OES | - | ||||||||||||||||||
128 | } vaoFuncsType; | - | ||||||||||||||||||
129 | - | |||||||||||||||||||
130 | QOpenGLContext *context; | - | ||||||||||||||||||
131 | }; | - | ||||||||||||||||||
132 | - | |||||||||||||||||||
133 | bool QOpenGLVertexArrayObjectPrivate::create() | - | ||||||||||||||||||
134 | { | - | ||||||||||||||||||
135 | if (vao) {
| 0 | ||||||||||||||||||
136 | qWarning("QOpenGLVertexArrayObject::create() VAO is already created"); | - | ||||||||||||||||||
137 | return false; never executed: return false; | 0 | ||||||||||||||||||
138 | } | - | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | Q_Q(QOpenGLVertexArrayObject); | - | ||||||||||||||||||
141 | - | |||||||||||||||||||
142 | QOpenGLContext *ctx = QOpenGLContext::currentContext(); | - | ||||||||||||||||||
143 | if (!ctx) {
| 0 | ||||||||||||||||||
144 | qWarning("QOpenGLVertexArrayObject::create() requires a valid current OpenGL context"); | - | ||||||||||||||||||
145 | return false; never executed: return false; | 0 | ||||||||||||||||||
146 | } | - | ||||||||||||||||||
147 | - | |||||||||||||||||||
148 | //Fail early, if context is the same as ctx, it means we have tried to initialize for this context and failed | - | ||||||||||||||||||
149 | if (ctx == context)
| 0 | ||||||||||||||||||
150 | return false; never executed: return false; | 0 | ||||||||||||||||||
151 | - | |||||||||||||||||||
152 | context = ctx; | - | ||||||||||||||||||
153 | QObject::connect(context, SIGNAL(aboutToBeDestroyed()), q, SLOT(_q_contextAboutToBeDestroyed())); | - | ||||||||||||||||||
154 | - | |||||||||||||||||||
155 | if (ctx->isOpenGLES()) {
| 0 | ||||||||||||||||||
156 | if (ctx->format().majorVersion() >= 3 || ctx->hasExtension(QByteArrayLiteral("GL_OES_vertex_array_object"))) { never executed: return ba;
| 0 | ||||||||||||||||||
157 | vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); | - | ||||||||||||||||||
158 | vaoFuncsType = OES; | - | ||||||||||||||||||
159 | vaoFuncs.helper->glGenVertexArrays(1, &vao); | - | ||||||||||||||||||
160 | } never executed: end of block | 0 | ||||||||||||||||||
161 | } else { never executed: end of block | 0 | ||||||||||||||||||
162 | vaoFuncs.core_3_0 = 0; | - | ||||||||||||||||||
163 | vaoFuncsType = NotSupported; | - | ||||||||||||||||||
164 | QSurfaceFormat format = ctx->format(); | - | ||||||||||||||||||
165 | #ifndef QT_OPENGL_ES_2 | - | ||||||||||||||||||
166 | if (format.version() >= qMakePair<int, int>(3,2)) {
| 0 | ||||||||||||||||||
167 | vaoFuncs.core_3_2 = ctx->versionFunctions<QOpenGLFunctions_3_2_Core>(); | - | ||||||||||||||||||
168 | vaoFuncsType = Core_3_2; | - | ||||||||||||||||||
169 | vaoFuncs.core_3_2->glGenVertexArrays(1, &vao); | - | ||||||||||||||||||
170 | } else if (format.majorVersion() >= 3) { never executed: end of block
| 0 | ||||||||||||||||||
171 | vaoFuncs.core_3_0 = ctx->versionFunctions<QOpenGLFunctions_3_0>(); | - | ||||||||||||||||||
172 | vaoFuncsType = Core_3_0; | - | ||||||||||||||||||
173 | vaoFuncs.core_3_0->glGenVertexArrays(1, &vao); | - | ||||||||||||||||||
174 | } else never executed: end of block | 0 | ||||||||||||||||||
175 | #endif | - | ||||||||||||||||||
176 | if (ctx->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { never executed: return ba;
| 0 | ||||||||||||||||||
177 | vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); | - | ||||||||||||||||||
178 | vaoFuncsType = ARB; | - | ||||||||||||||||||
179 | vaoFuncs.helper->glGenVertexArrays(1, &vao); | - | ||||||||||||||||||
180 | } else if (ctx->hasExtension(QByteArrayLiteral("GL_APPLE_vertex_array_object"))) { never executed: end of block never executed: return ba;
| 0 | ||||||||||||||||||
181 | vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); | - | ||||||||||||||||||
182 | vaoFuncsType = APPLE; | - | ||||||||||||||||||
183 | vaoFuncs.helper->glGenVertexArrays(1, &vao); | - | ||||||||||||||||||
184 | } never executed: end of block | 0 | ||||||||||||||||||
185 | } never executed: end of block | 0 | ||||||||||||||||||
186 | - | |||||||||||||||||||
187 | return (vao != 0); never executed: return (vao != 0); | 0 | ||||||||||||||||||
188 | } | - | ||||||||||||||||||
189 | - | |||||||||||||||||||
190 | void QOpenGLVertexArrayObjectPrivate::destroy() | - | ||||||||||||||||||
191 | { | - | ||||||||||||||||||
192 | Q_Q(QOpenGLVertexArrayObject); | - | ||||||||||||||||||
193 | - | |||||||||||||||||||
194 | if (context) {
| 0 | ||||||||||||||||||
195 | QObject::disconnect(context, SIGNAL(aboutToBeDestroyed()), q, SLOT(_q_contextAboutToBeDestroyed())); | - | ||||||||||||||||||
196 | context = 0; | - | ||||||||||||||||||
197 | } never executed: end of block | 0 | ||||||||||||||||||
198 | - | |||||||||||||||||||
199 | if (!vao)
| 0 | ||||||||||||||||||
200 | return; never executed: return; | 0 | ||||||||||||||||||
201 | - | |||||||||||||||||||
202 | switch (vaoFuncsType) { | - | ||||||||||||||||||
203 | #ifndef QT_OPENGL_ES_2 | - | ||||||||||||||||||
204 | case Core_3_2: never executed: case Core_3_2: | 0 | ||||||||||||||||||
205 | vaoFuncs.core_3_2->glDeleteVertexArrays(1, &vao); | - | ||||||||||||||||||
206 | break; never executed: break; | 0 | ||||||||||||||||||
207 | case Core_3_0: never executed: case Core_3_0: | 0 | ||||||||||||||||||
208 | vaoFuncs.core_3_0->glDeleteVertexArrays(1, &vao); | - | ||||||||||||||||||
209 | break; never executed: break; | 0 | ||||||||||||||||||
210 | #endif | - | ||||||||||||||||||
211 | case ARB: never executed: case ARB: | 0 | ||||||||||||||||||
212 | case APPLE: never executed: case APPLE: | 0 | ||||||||||||||||||
213 | case OES: never executed: case OES: | 0 | ||||||||||||||||||
214 | vaoFuncs.helper->glDeleteVertexArrays(1, &vao); | - | ||||||||||||||||||
215 | break; never executed: break; | 0 | ||||||||||||||||||
216 | default: never executed: default: | 0 | ||||||||||||||||||
217 | break; never executed: break; | 0 | ||||||||||||||||||
218 | } | - | ||||||||||||||||||
219 | - | |||||||||||||||||||
220 | vao = 0; | - | ||||||||||||||||||
221 | } never executed: end of block | 0 | ||||||||||||||||||
222 | - | |||||||||||||||||||
223 | /*! | - | ||||||||||||||||||
224 | \internal | - | ||||||||||||||||||
225 | */ | - | ||||||||||||||||||
226 | void QOpenGLVertexArrayObjectPrivate::_q_contextAboutToBeDestroyed() | - | ||||||||||||||||||
227 | { | - | ||||||||||||||||||
228 | destroy(); | - | ||||||||||||||||||
229 | } never executed: end of block | 0 | ||||||||||||||||||
230 | - | |||||||||||||||||||
231 | void QOpenGLVertexArrayObjectPrivate::bind() | - | ||||||||||||||||||
232 | { | - | ||||||||||||||||||
233 | switch (vaoFuncsType) { | - | ||||||||||||||||||
234 | #ifndef QT_OPENGL_ES_2 | - | ||||||||||||||||||
235 | case Core_3_2: never executed: case Core_3_2: | 0 | ||||||||||||||||||
236 | vaoFuncs.core_3_2->glBindVertexArray(vao); | - | ||||||||||||||||||
237 | break; never executed: break; | 0 | ||||||||||||||||||
238 | case Core_3_0: never executed: case Core_3_0: | 0 | ||||||||||||||||||
239 | vaoFuncs.core_3_0->glBindVertexArray(vao); | - | ||||||||||||||||||
240 | break; never executed: break; | 0 | ||||||||||||||||||
241 | #endif | - | ||||||||||||||||||
242 | case ARB: never executed: case ARB: | 0 | ||||||||||||||||||
243 | case APPLE: never executed: case APPLE: | 0 | ||||||||||||||||||
244 | case OES: never executed: case OES: | 0 | ||||||||||||||||||
245 | vaoFuncs.helper->glBindVertexArray(vao); | - | ||||||||||||||||||
246 | break; never executed: break; | 0 | ||||||||||||||||||
247 | default: never executed: default: | 0 | ||||||||||||||||||
248 | break; never executed: break; | 0 | ||||||||||||||||||
249 | } | - | ||||||||||||||||||
250 | } | - | ||||||||||||||||||
251 | - | |||||||||||||||||||
252 | void QOpenGLVertexArrayObjectPrivate::release() | - | ||||||||||||||||||
253 | { | - | ||||||||||||||||||
254 | switch (vaoFuncsType) { | - | ||||||||||||||||||
255 | #ifndef QT_OPENGL_ES_2 | - | ||||||||||||||||||
256 | case Core_3_2: never executed: case Core_3_2: | 0 | ||||||||||||||||||
257 | vaoFuncs.core_3_2->glBindVertexArray(0); | - | ||||||||||||||||||
258 | break; never executed: break; | 0 | ||||||||||||||||||
259 | case Core_3_0: never executed: case Core_3_0: | 0 | ||||||||||||||||||
260 | vaoFuncs.core_3_0->glBindVertexArray(0); | - | ||||||||||||||||||
261 | break; never executed: break; | 0 | ||||||||||||||||||
262 | #endif | - | ||||||||||||||||||
263 | case ARB: never executed: case ARB: | 0 | ||||||||||||||||||
264 | case APPLE: never executed: case APPLE: | 0 | ||||||||||||||||||
265 | case OES: never executed: case OES: | 0 | ||||||||||||||||||
266 | vaoFuncs.helper->glBindVertexArray(0); | - | ||||||||||||||||||
267 | break; never executed: break; | 0 | ||||||||||||||||||
268 | default: never executed: default: | 0 | ||||||||||||||||||
269 | break; never executed: break; | 0 | ||||||||||||||||||
270 | } | - | ||||||||||||||||||
271 | } | - | ||||||||||||||||||
272 | - | |||||||||||||||||||
273 | - | |||||||||||||||||||
274 | /*! | - | ||||||||||||||||||
275 | \class QOpenGLVertexArrayObject | - | ||||||||||||||||||
276 | \brief The QOpenGLVertexArrayObject class wraps an OpenGL Vertex Array Object. | - | ||||||||||||||||||
277 | \inmodule QtGui | - | ||||||||||||||||||
278 | \since 5.1 | - | ||||||||||||||||||
279 | \ingroup painting-3D | - | ||||||||||||||||||
280 | - | |||||||||||||||||||
281 | A Vertex Array Object (VAO) is an OpenGL container object that encapsulates | - | ||||||||||||||||||
282 | the state needed to specify per-vertex attribute data to the OpenGL pipeline. | - | ||||||||||||||||||
283 | To put it another way, a VAO remembers the states of buffer objects (see | - | ||||||||||||||||||
284 | QOpenGLBuffer) and their associated state (e.g. vertex attribute divisors). | - | ||||||||||||||||||
285 | This allows a very easy and efficient method of switching between OpenGL buffer | - | ||||||||||||||||||
286 | states for rendering different "objects" in a scene. The QOpenGLVertexArrayObject | - | ||||||||||||||||||
287 | class is a thin wrapper around an OpenGL VAO. | - | ||||||||||||||||||
288 | - | |||||||||||||||||||
289 | For the desktop, VAOs are supported as a core feature in OpenGL 3.0 or newer and by the | - | ||||||||||||||||||
290 | GL_ARB_vertex_array_object for older versions. On OpenGL ES 2, VAOs are provided by | - | ||||||||||||||||||
291 | the optional GL_OES_vertex_array_object extension. You can check the version of | - | ||||||||||||||||||
292 | OpenGL with QOpenGLContext::surfaceFormat() and check for the presence of extensions | - | ||||||||||||||||||
293 | with QOpenGLContext::hasExtension(). | - | ||||||||||||||||||
294 | - | |||||||||||||||||||
295 | As with the other Qt OpenGL classes, QOpenGLVertexArrayObject has a create() | - | ||||||||||||||||||
296 | function to create the underlying OpenGL object. This is to allow the developer to | - | ||||||||||||||||||
297 | ensure that there is a valid current OpenGL context at the time. | - | ||||||||||||||||||
298 | - | |||||||||||||||||||
299 | Once you have successfully created a VAO the typical usage pattern is: | - | ||||||||||||||||||
300 | - | |||||||||||||||||||
301 | \list | - | ||||||||||||||||||
302 | \li In scene initialization function, for each visual object: | - | ||||||||||||||||||
303 | \list | - | ||||||||||||||||||
304 | \li Bind the VAO | - | ||||||||||||||||||
305 | \li Set vertex data state for this visual object (vertices, normals, texture coordinates etc.) | - | ||||||||||||||||||
306 | \li Unbind (release()) the VAO | - | ||||||||||||||||||
307 | \endlist | - | ||||||||||||||||||
308 | \li In render function, for each visual object: | - | ||||||||||||||||||
309 | \list | - | ||||||||||||||||||
310 | \li Bind the VAO (and shader program if needed) | - | ||||||||||||||||||
311 | \li Call a glDraw*() function | - | ||||||||||||||||||
312 | \li Unbind (release()) the VAO | - | ||||||||||||||||||
313 | \endlist | - | ||||||||||||||||||
314 | \endlist | - | ||||||||||||||||||
315 | - | |||||||||||||||||||
316 | The act of binding the VAO in the render function has the effect of restoring | - | ||||||||||||||||||
317 | all of the vertex data state setup in the initialization phase. In this way we can | - | ||||||||||||||||||
318 | set a great deal of state when setting up a VAO and efficiently switch between | - | ||||||||||||||||||
319 | state sets of objects to be rendered. Using VAOs also allows the OpenGL driver | - | ||||||||||||||||||
320 | to amortise the validation checks of the vertex data. | - | ||||||||||||||||||
321 | - | |||||||||||||||||||
322 | \note Vertex Array Objects, like all other OpenGL container objects, are specific | - | ||||||||||||||||||
323 | to the context for which they were created and cannot be shared amongst a | - | ||||||||||||||||||
324 | context group. | - | ||||||||||||||||||
325 | - | |||||||||||||||||||
326 | \sa QOpenGLVertexArrayObject::Binder, QOpenGLBuffer | - | ||||||||||||||||||
327 | */ | - | ||||||||||||||||||
328 | - | |||||||||||||||||||
329 | /*! | - | ||||||||||||||||||
330 | Creates a QOpenGLVertexArrayObject with the given \a parent. You must call create() | - | ||||||||||||||||||
331 | with a valid OpenGL context before using. | - | ||||||||||||||||||
332 | */ | - | ||||||||||||||||||
333 | QOpenGLVertexArrayObject::QOpenGLVertexArrayObject(QObject* parent) | - | ||||||||||||||||||
334 | : QObject(*new QOpenGLVertexArrayObjectPrivate, parent) | - | ||||||||||||||||||
335 | { | - | ||||||||||||||||||
336 | } never executed: end of block | 0 | ||||||||||||||||||
337 | - | |||||||||||||||||||
338 | /*! | - | ||||||||||||||||||
339 | \internal | - | ||||||||||||||||||
340 | */ | - | ||||||||||||||||||
341 | QOpenGLVertexArrayObject::QOpenGLVertexArrayObject(QOpenGLVertexArrayObjectPrivate &dd) | - | ||||||||||||||||||
342 | : QObject(dd) | - | ||||||||||||||||||
343 | { | - | ||||||||||||||||||
344 | } never executed: end of block | 0 | ||||||||||||||||||
345 | - | |||||||||||||||||||
346 | /*! | - | ||||||||||||||||||
347 | Destroys the QOpenGLVertexArrayObject and the underlying OpenGL resource. | - | ||||||||||||||||||
348 | */ | - | ||||||||||||||||||
349 | QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject() | - | ||||||||||||||||||
350 | { | - | ||||||||||||||||||
351 | QOpenGLContext* ctx = QOpenGLContext::currentContext(); | - | ||||||||||||||||||
352 | - | |||||||||||||||||||
353 | Q_D(QOpenGLVertexArrayObject); | - | ||||||||||||||||||
354 | QOpenGLContext *oldContext = 0; | - | ||||||||||||||||||
355 | QSurface *oldContextSurface = 0; | - | ||||||||||||||||||
356 | QScopedPointer<QOffscreenSurface> offscreenSurface; | - | ||||||||||||||||||
357 | if (d->context && ctx && d->context != ctx) {
| 0 | ||||||||||||||||||
358 | oldContext = ctx; | - | ||||||||||||||||||
359 | oldContextSurface = ctx->surface(); | - | ||||||||||||||||||
360 | // Cannot just make the current surface current again with another context. | - | ||||||||||||||||||
361 | // The format may be incompatible and some platforms (iOS) may impose | - | ||||||||||||||||||
362 | // restrictions on using a window with different contexts. Create an | - | ||||||||||||||||||
363 | // offscreen surface (a pbuffer or a hidden window) instead to be safe. | - | ||||||||||||||||||
364 | offscreenSurface.reset(new QOffscreenSurface); | - | ||||||||||||||||||
365 | offscreenSurface->setFormat(d->context->format()); | - | ||||||||||||||||||
366 | offscreenSurface->create(); | - | ||||||||||||||||||
367 | if (d->context->makeCurrent(offscreenSurface.data())) {
| 0 | ||||||||||||||||||
368 | ctx = d->context; | - | ||||||||||||||||||
369 | } else { never executed: end of block | 0 | ||||||||||||||||||
370 | qWarning("QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject() failed to make VAO's context current"); | - | ||||||||||||||||||
371 | ctx = 0; | - | ||||||||||||||||||
372 | } never executed: end of block | 0 | ||||||||||||||||||
373 | } | - | ||||||||||||||||||
374 | - | |||||||||||||||||||
375 | if (ctx)
| 0 | ||||||||||||||||||
376 | destroy(); never executed: destroy(); | 0 | ||||||||||||||||||
377 | - | |||||||||||||||||||
378 | if (oldContext) {
| 0 | ||||||||||||||||||
379 | if (!oldContext->makeCurrent(oldContextSurface))
| 0 | ||||||||||||||||||
380 | qWarning("QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject() failed to restore current context"); never executed: QMessageLogger(__FILE__, 380, __PRETTY_FUNCTION__).warning("QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject() failed to restore current context"); | 0 | ||||||||||||||||||
381 | } never executed: end of block | 0 | ||||||||||||||||||
382 | } never executed: end of block | 0 | ||||||||||||||||||
383 | - | |||||||||||||||||||
384 | /*! | - | ||||||||||||||||||
385 | Creates the underlying OpenGL vertex array object. There must be a valid OpenGL context | - | ||||||||||||||||||
386 | that supports vertex array objects current for this function to succeed. | - | ||||||||||||||||||
387 | - | |||||||||||||||||||
388 | Returns \c true if the OpenGL vertex array object was successfully created. | - | ||||||||||||||||||
389 | - | |||||||||||||||||||
390 | When the return value is \c false, vertex array object support is not available. This | - | ||||||||||||||||||
391 | is not an error: on systems with OpenGL 2.x or OpenGL ES 2.0 vertex array objects may | - | ||||||||||||||||||
392 | not be supported. The application is free to continue execution in this case, but it | - | ||||||||||||||||||
393 | then has to be prepared to operate in a VAO-less manner too. This means that instead | - | ||||||||||||||||||
394 | of merely calling bind(), the value of isCreated() must be checked and the vertex | - | ||||||||||||||||||
395 | arrays has to be initialized in the traditional way when there is no vertex array | - | ||||||||||||||||||
396 | object present. | - | ||||||||||||||||||
397 | - | |||||||||||||||||||
398 | \sa isCreated() | - | ||||||||||||||||||
399 | */ | - | ||||||||||||||||||
400 | bool QOpenGLVertexArrayObject::create() | - | ||||||||||||||||||
401 | { | - | ||||||||||||||||||
402 | Q_D(QOpenGLVertexArrayObject); | - | ||||||||||||||||||
403 | return d->create(); never executed: return d->create(); | 0 | ||||||||||||||||||
404 | } | - | ||||||||||||||||||
405 | - | |||||||||||||||||||
406 | /*! | - | ||||||||||||||||||
407 | Destroys the underlying OpenGL vertex array object. There must be a valid OpenGL context | - | ||||||||||||||||||
408 | that supports vertex array objects current for this function to succeed. | - | ||||||||||||||||||
409 | */ | - | ||||||||||||||||||
410 | void QOpenGLVertexArrayObject::destroy() | - | ||||||||||||||||||
411 | { | - | ||||||||||||||||||
412 | Q_D(QOpenGLVertexArrayObject); | - | ||||||||||||||||||
413 | d->destroy(); | - | ||||||||||||||||||
414 | } never executed: end of block | 0 | ||||||||||||||||||
415 | - | |||||||||||||||||||
416 | /*! | - | ||||||||||||||||||
417 | Returns \c true is the underlying OpenGL vertex array object has been created. If this | - | ||||||||||||||||||
418 | returns \c true and the associated OpenGL context is current, then you are able to bind() | - | ||||||||||||||||||
419 | this object. | - | ||||||||||||||||||
420 | */ | - | ||||||||||||||||||
421 | bool QOpenGLVertexArrayObject::isCreated() const | - | ||||||||||||||||||
422 | { | - | ||||||||||||||||||
423 | Q_D(const QOpenGLVertexArrayObject); | - | ||||||||||||||||||
424 | return (d->vao != 0); never executed: return (d->vao != 0); | 0 | ||||||||||||||||||
425 | } | - | ||||||||||||||||||
426 | - | |||||||||||||||||||
427 | /*! | - | ||||||||||||||||||
428 | Returns the id of the underlying OpenGL vertex array object. | - | ||||||||||||||||||
429 | */ | - | ||||||||||||||||||
430 | GLuint QOpenGLVertexArrayObject::objectId() const | - | ||||||||||||||||||
431 | { | - | ||||||||||||||||||
432 | Q_D(const QOpenGLVertexArrayObject); | - | ||||||||||||||||||
433 | return d->vao; never executed: return d->vao; | 0 | ||||||||||||||||||
434 | } | - | ||||||||||||||||||
435 | - | |||||||||||||||||||
436 | /*! | - | ||||||||||||||||||
437 | Binds this vertex array object to the OpenGL binding point. From this point on | - | ||||||||||||||||||
438 | and until release() is called or another vertex array object is bound, any | - | ||||||||||||||||||
439 | modifications made to vertex data state are stored inside this vertex array object. | - | ||||||||||||||||||
440 | - | |||||||||||||||||||
441 | If another vertex array object is then bound you can later restore the set of | - | ||||||||||||||||||
442 | state associated with this object by calling bind() on this object once again. | - | ||||||||||||||||||
443 | This allows efficient changes between vertex data states in rendering functions. | - | ||||||||||||||||||
444 | */ | - | ||||||||||||||||||
445 | void QOpenGLVertexArrayObject::bind() | - | ||||||||||||||||||
446 | { | - | ||||||||||||||||||
447 | Q_D(QOpenGLVertexArrayObject); | - | ||||||||||||||||||
448 | d->bind(); | - | ||||||||||||||||||
449 | } never executed: end of block | 0 | ||||||||||||||||||
450 | - | |||||||||||||||||||
451 | /*! | - | ||||||||||||||||||
452 | Unbinds this vertex array object by binding the default vertex array object (id = 0). | - | ||||||||||||||||||
453 | */ | - | ||||||||||||||||||
454 | void QOpenGLVertexArrayObject::release() | - | ||||||||||||||||||
455 | { | - | ||||||||||||||||||
456 | Q_D(QOpenGLVertexArrayObject); | - | ||||||||||||||||||
457 | d->release(); | - | ||||||||||||||||||
458 | } never executed: end of block | 0 | ||||||||||||||||||
459 | - | |||||||||||||||||||
460 | - | |||||||||||||||||||
461 | /*! | - | ||||||||||||||||||
462 | \class QOpenGLVertexArrayObject::Binder | - | ||||||||||||||||||
463 | \brief The QOpenGLVertexArrayObject::Binder class is a convenience class to help | - | ||||||||||||||||||
464 | with the binding and releasing of OpenGL Vertex Array Objects. | - | ||||||||||||||||||
465 | \inmodule QtGui | - | ||||||||||||||||||
466 | \reentrant | - | ||||||||||||||||||
467 | \since 5.1 | - | ||||||||||||||||||
468 | \ingroup painting-3D | - | ||||||||||||||||||
469 | - | |||||||||||||||||||
470 | QOpenGLVertexArrayObject::Binder is a simple convenience class that can be used | - | ||||||||||||||||||
471 | to assist with the binding and releasing of QOpenGLVertexArrayObject instances. | - | ||||||||||||||||||
472 | This class is to QOpenGLVertexArrayObject as QMutexLocker is to QMutex. | - | ||||||||||||||||||
473 | - | |||||||||||||||||||
474 | This class implements the RAII principle which helps to ensure behavior in | - | ||||||||||||||||||
475 | complex code or in the presence of exceptions. | - | ||||||||||||||||||
476 | - | |||||||||||||||||||
477 | The constructor of this class accepts a QOpenGLVertexArrayObject (VAO) as an | - | ||||||||||||||||||
478 | argument and attempts to bind the VAO, calling QOpenGLVertexArrayObject::create() | - | ||||||||||||||||||
479 | if necessary. The destructor of this class calls QOpenGLVertexArrayObject::release() | - | ||||||||||||||||||
480 | which unbinds the VAO. | - | ||||||||||||||||||
481 | - | |||||||||||||||||||
482 | If needed the VAO can be temporarily unbound with the release() function and bound | - | ||||||||||||||||||
483 | once more with rebind(). | - | ||||||||||||||||||
484 | - | |||||||||||||||||||
485 | \sa QOpenGLVertexArrayObject | - | ||||||||||||||||||
486 | */ | - | ||||||||||||||||||
487 | - | |||||||||||||||||||
488 | /*! | - | ||||||||||||||||||
489 | \fn QOpenGLVertexArrayObject::Binder::Binder(QOpenGLVertexArrayObject *v) | - | ||||||||||||||||||
490 | - | |||||||||||||||||||
491 | Creates a QOpenGLVertexArrayObject::Binder object and binds \a v by calling | - | ||||||||||||||||||
492 | QOpenGLVertexArrayObject::bind(). If necessary it first calls | - | ||||||||||||||||||
493 | QOpenGLVertexArrayObject::create(). | - | ||||||||||||||||||
494 | */ | - | ||||||||||||||||||
495 | - | |||||||||||||||||||
496 | /*! | - | ||||||||||||||||||
497 | \fn QOpenGLVertexArrayObject::Binder::~Binder() | - | ||||||||||||||||||
498 | - | |||||||||||||||||||
499 | Destroys the QOpenGLVertexArrayObject::Binder and releases the associated vertex array object. | - | ||||||||||||||||||
500 | */ | - | ||||||||||||||||||
501 | - | |||||||||||||||||||
502 | /*! | - | ||||||||||||||||||
503 | \fn QOpenGLVertexArrayObject::Binder::release() | - | ||||||||||||||||||
504 | - | |||||||||||||||||||
505 | Can be used to temporarily release the associated vertex array object. | - | ||||||||||||||||||
506 | - | |||||||||||||||||||
507 | \sa rebind() | - | ||||||||||||||||||
508 | */ | - | ||||||||||||||||||
509 | - | |||||||||||||||||||
510 | /*! | - | ||||||||||||||||||
511 | \fn QOpenGLVertexArrayObject::Binder::rebind() | - | ||||||||||||||||||
512 | - | |||||||||||||||||||
513 | Can be used to rebind the associated vertex array object. | - | ||||||||||||||||||
514 | - | |||||||||||||||||||
515 | \sa release() | - | ||||||||||||||||||
516 | */ | - | ||||||||||||||||||
517 | - | |||||||||||||||||||
518 | QT_END_NAMESPACE | - | ||||||||||||||||||
519 | - | |||||||||||||||||||
520 | #include "moc_qopenglvertexarrayobject.cpp" | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |