From 8193f9e0ecc0a7592310e1c26310ba502c091374 Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Wed, 22 Jan 2020 16:01:53 +0100 Subject: Move QOpenGLVertexArrayObject from QtGui to QtOpenGL Task-number: QTBUG-74409 Change-Id: I6442693ca2b28b3a66f0f4fb41843e1e404df32b Reviewed-by: Laszlo Agocs --- src/gui/.prev_CMakeLists.txt | 1 - src/gui/CMakeLists.txt | 1 - src/gui/opengl/opengl.pri | 2 - src/gui/opengl/qopenglvertexarrayobject.cpp | 531 ----------------------- src/gui/opengl/qopenglvertexarrayobject.h | 111 ----- src/gui/opengl/qopenglvertexarrayobject_p.h | 125 ------ src/opengl/CMakeLists.txt | 1 + src/opengl/opengl.pro | 2 + src/opengl/qopengltextureblitter.cpp | 2 +- src/opengl/qopenglvertexarrayobject.cpp | 531 +++++++++++++++++++++++ src/opengl/qopenglvertexarrayobject.h | 111 +++++ src/opengl/qopenglvertexarrayobject_p.h | 121 ++++++ src/plugins/platforms/eglfs/.prev_CMakeLists.txt | 2 + src/plugins/platforms/eglfs/CMakeLists.txt | 2 + src/plugins/platforms/eglfs/api/api.pri | 1 + src/plugins/platforms/eglfs/api/qeglfscursor.cpp | 2 +- tests/auto/gui/qopengl/tst_qopengl.cpp | 2 +- 17 files changed, 774 insertions(+), 774 deletions(-) delete mode 100644 src/gui/opengl/qopenglvertexarrayobject.cpp delete mode 100644 src/gui/opengl/qopenglvertexarrayobject.h delete mode 100644 src/gui/opengl/qopenglvertexarrayobject_p.h create mode 100644 src/opengl/qopenglvertexarrayobject.cpp create mode 100644 src/opengl/qopenglvertexarrayobject.h create mode 100644 src/opengl/qopenglvertexarrayobject_p.h diff --git a/src/gui/.prev_CMakeLists.txt b/src/gui/.prev_CMakeLists.txt index 84c3749947..1fea63419b 100644 --- a/src/gui/.prev_CMakeLists.txt +++ b/src/gui/.prev_CMakeLists.txt @@ -262,7 +262,6 @@ qt_extend_target(Gui CONDITION QT_FEATURE_opengl opengl/qopenglprogrambinarycache.cpp opengl/qopenglprogrambinarycache_p.h opengl/qopenglversionfunctions.cpp opengl/qopenglversionfunctions.h opengl/qopenglversionfunctionsfactory.cpp opengl/qopenglversionfunctionsfactory_p.h - opengl/qopenglvertexarrayobject.cpp opengl/qopenglvertexarrayobject.h rhi/qrhigles2.cpp rhi/qrhigles2_p.h rhi/qrhigles2_p_p.h ) diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index bd8c59e198..6a9951412c 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -344,7 +344,6 @@ qt_extend_target(Gui CONDITION QT_FEATURE_opengl opengl/qopenglprogrambinarycache.cpp opengl/qopenglprogrambinarycache_p.h opengl/qopenglversionfunctions.cpp opengl/qopenglversionfunctions.h opengl/qopenglversionfunctionsfactory.cpp opengl/qopenglversionfunctionsfactory_p.h - opengl/qopenglvertexarrayobject.cpp opengl/qopenglvertexarrayobject.h rhi/qrhigles2.cpp rhi/qrhigles2_p.h rhi/qrhigles2_p_p.h ) diff --git a/src/gui/opengl/opengl.pri b/src/gui/opengl/opengl.pri index c606e9ec02..4962e57a62 100644 --- a/src/gui/opengl/opengl.pri +++ b/src/gui/opengl/opengl.pri @@ -12,7 +12,6 @@ qtConfig(opengl) { opengl/qopenglextensions_p.h \ opengl/qopenglversionfunctions.h \ opengl/qopenglversionfunctionsfactory_p.h \ - opengl/qopenglvertexarrayobject.h \ opengl/qopenglextrafunctions.h \ opengl/qopenglprogrambinarycache_p.h @@ -21,7 +20,6 @@ qtConfig(opengl) { opengl/qopenglbuffer.cpp \ opengl/qopenglversionfunctions.cpp \ opengl/qopenglversionfunctionsfactory.cpp \ - opengl/qopenglvertexarrayobject.cpp \ opengl/qopenglprogrambinarycache.cpp !qtConfig(opengles2) { diff --git a/src/gui/opengl/qopenglvertexarrayobject.cpp b/src/gui/opengl/qopenglvertexarrayobject.cpp deleted file mode 100644 index f15fe06ee8..0000000000 --- a/src/gui/opengl/qopenglvertexarrayobject.cpp +++ /dev/null @@ -1,531 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sean Harmer -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qopenglvertexarrayobject.h" - -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -QT_BEGIN_NAMESPACE - -class QOpenGLFunctions_3_0; -class QOpenGLFunctions_3_2_Core; - -void qtInitializeVertexArrayObjectHelper(QOpenGLVertexArrayObjectHelper *helper, QOpenGLContext *context) -{ - Q_ASSERT(helper); - Q_ASSERT(context); - - bool tryARB = true; - - if (context->isOpenGLES()) { - if (context->format().majorVersion() >= 3) { - QOpenGLExtraFunctionsPrivate *extra = static_cast(context->extraFunctions())->d(); - helper->GenVertexArrays = extra->f.GenVertexArrays; - helper->DeleteVertexArrays = extra->f.DeleteVertexArrays; - helper->BindVertexArray = extra->f.BindVertexArray; - helper->IsVertexArray = extra->f.IsVertexArray; - tryARB = false; - } else if (context->hasExtension(QByteArrayLiteral("GL_OES_vertex_array_object"))) { - helper->GenVertexArrays = reinterpret_cast(context->getProcAddress("glGenVertexArraysOES")); - helper->DeleteVertexArrays = reinterpret_cast(context->getProcAddress("glDeleteVertexArraysOES")); - helper->BindVertexArray = reinterpret_cast(context->getProcAddress("glBindVertexArrayOES")); - helper->IsVertexArray = reinterpret_cast(context->getProcAddress("glIsVertexArrayOES")); - tryARB = false; - } - } else if (context->hasExtension(QByteArrayLiteral("GL_APPLE_vertex_array_object")) && - !context->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { - helper->GenVertexArrays = reinterpret_cast(context->getProcAddress("glGenVertexArraysAPPLE")); - helper->DeleteVertexArrays = reinterpret_cast(context->getProcAddress("glDeleteVertexArraysAPPLE")); - helper->BindVertexArray = reinterpret_cast(context->getProcAddress("glBindVertexArrayAPPLE")); - helper->IsVertexArray = reinterpret_cast(context->getProcAddress("glIsVertexArrayAPPLE")); - tryARB = false; - } - - if (tryARB && context->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { - helper->GenVertexArrays = reinterpret_cast(context->getProcAddress("glGenVertexArrays")); - helper->DeleteVertexArrays = reinterpret_cast(context->getProcAddress("glDeleteVertexArrays")); - helper->BindVertexArray = reinterpret_cast(context->getProcAddress("glBindVertexArray")); - helper->IsVertexArray = reinterpret_cast(context->getProcAddress("glIsVertexArray")); - } -} - -class QOpenGLVertexArrayObjectPrivate : public QObjectPrivate -{ -public: - QOpenGLVertexArrayObjectPrivate() - : vao(0) - , vaoFuncsType(NotSupported) - , context(nullptr) - { - } - - ~QOpenGLVertexArrayObjectPrivate() - { - if (vaoFuncsType == ARB || vaoFuncsType == APPLE || vaoFuncsType == OES) - delete vaoFuncs.helper; - } - - bool create(); - void destroy(); - void bind(); - void release(); - void _q_contextAboutToBeDestroyed(); - - Q_DECLARE_PUBLIC(QOpenGLVertexArrayObject) - - GLuint vao; - - union { - QOpenGLFunctions_3_0 *core_3_0; - QOpenGLFunctions_3_2_Core *core_3_2; - QOpenGLVertexArrayObjectHelper *helper; - } vaoFuncs; - enum { - NotSupported, - Core_3_0, - Core_3_2, - ARB, - APPLE, - OES - } vaoFuncsType; - - QOpenGLContext *context; -}; - -bool QOpenGLVertexArrayObjectPrivate::create() -{ - if (vao) { - qWarning("QOpenGLVertexArrayObject::create() VAO is already created"); - return false; - } - - Q_Q(QOpenGLVertexArrayObject); - - QOpenGLContext *ctx = QOpenGLContext::currentContext(); - if (!ctx) { - qWarning("QOpenGLVertexArrayObject::create() requires a valid current OpenGL context"); - return false; - } - - //Fail early, if context is the same as ctx, it means we have tried to initialize for this context and failed - if (ctx == context) - return false; - - context = ctx; - QObject::connect(context, SIGNAL(aboutToBeDestroyed()), q, SLOT(_q_contextAboutToBeDestroyed())); - - if (ctx->isOpenGLES()) { - if (ctx->format().majorVersion() >= 3 || ctx->hasExtension(QByteArrayLiteral("GL_OES_vertex_array_object"))) { - vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); - vaoFuncsType = OES; - vaoFuncs.helper->glGenVertexArrays(1, &vao); - } - } else { - vaoFuncs.core_3_0 = nullptr; - vaoFuncsType = NotSupported; - QSurfaceFormat format = ctx->format(); -#ifndef QT_OPENGL_ES_2 - if (format.version() >= qMakePair(3,2)) { - vaoFuncs.core_3_2 = ctx->versionFunctions(); - vaoFuncsType = Core_3_2; - vaoFuncs.core_3_2->glGenVertexArrays(1, &vao); - } else if (format.majorVersion() >= 3) { - vaoFuncs.core_3_0 = ctx->versionFunctions(); - vaoFuncsType = Core_3_0; - vaoFuncs.core_3_0->glGenVertexArrays(1, &vao); - } else -#endif - if (ctx->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { - vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); - vaoFuncsType = ARB; - vaoFuncs.helper->glGenVertexArrays(1, &vao); - } else if (ctx->hasExtension(QByteArrayLiteral("GL_APPLE_vertex_array_object"))) { - vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); - vaoFuncsType = APPLE; - vaoFuncs.helper->glGenVertexArrays(1, &vao); - } - } - - return (vao != 0); -} - -void QOpenGLVertexArrayObjectPrivate::destroy() -{ - Q_Q(QOpenGLVertexArrayObject); - - QOpenGLContext *ctx = QOpenGLContext::currentContext(); - QOpenGLContext *oldContext = nullptr; - QSurface *oldContextSurface = nullptr; - QScopedPointer offscreenSurface; - if (context && context != ctx) { - oldContext = ctx; - oldContextSurface = ctx ? ctx->surface() : nullptr; - // Before going through the effort of creating an offscreen surface - // check that we are on the GUI thread because otherwise many platforms - // will not able to create that offscreen surface. - if (QThread::currentThread() != qGuiApp->thread()) { - ctx = nullptr; - } else { - // Cannot just make the current surface current again with another context. - // The format may be incompatible and some platforms (iOS) may impose - // restrictions on using a window with different contexts. Create an - // offscreen surface (a pbuffer or a hidden window) instead to be safe. - offscreenSurface.reset(new QOffscreenSurface); - offscreenSurface->setFormat(context->format()); - offscreenSurface->create(); - if (context->makeCurrent(offscreenSurface.data())) { - ctx = context; - } else { - qWarning("QOpenGLVertexArrayObject::destroy() failed to make VAO's context current"); - ctx = nullptr; - } - } - } - - if (context) { - QObject::disconnect(context, SIGNAL(aboutToBeDestroyed()), q, SLOT(_q_contextAboutToBeDestroyed())); - context = nullptr; - } - - if (vao && ctx) { - switch (vaoFuncsType) { -#ifndef QT_OPENGL_ES_2 - case Core_3_2: - vaoFuncs.core_3_2->glDeleteVertexArrays(1, &vao); - break; - case Core_3_0: - vaoFuncs.core_3_0->glDeleteVertexArrays(1, &vao); - break; -#endif - case ARB: - case APPLE: - case OES: - vaoFuncs.helper->glDeleteVertexArrays(1, &vao); - break; - default: - break; - } - - vao = 0; - } - - if (oldContext && oldContextSurface) { - if (!oldContext->makeCurrent(oldContextSurface)) - qWarning("QOpenGLVertexArrayObject::destroy() failed to restore current context"); - } -} - -/*! - \internal -*/ -void QOpenGLVertexArrayObjectPrivate::_q_contextAboutToBeDestroyed() -{ - destroy(); -} - -void QOpenGLVertexArrayObjectPrivate::bind() -{ - switch (vaoFuncsType) { -#ifndef QT_OPENGL_ES_2 - case Core_3_2: - vaoFuncs.core_3_2->glBindVertexArray(vao); - break; - case Core_3_0: - vaoFuncs.core_3_0->glBindVertexArray(vao); - break; -#endif - case ARB: - case APPLE: - case OES: - vaoFuncs.helper->glBindVertexArray(vao); - break; - default: - break; - } -} - -void QOpenGLVertexArrayObjectPrivate::release() -{ - switch (vaoFuncsType) { -#ifndef QT_OPENGL_ES_2 - case Core_3_2: - vaoFuncs.core_3_2->glBindVertexArray(0); - break; - case Core_3_0: - vaoFuncs.core_3_0->glBindVertexArray(0); - break; -#endif - case ARB: - case APPLE: - case OES: - vaoFuncs.helper->glBindVertexArray(0); - break; - default: - break; - } -} - - -/*! - \class QOpenGLVertexArrayObject - \brief The QOpenGLVertexArrayObject class wraps an OpenGL Vertex Array Object. - \inmodule QtGui - \since 5.1 - \ingroup painting-3D - - A Vertex Array Object (VAO) is an OpenGL container object that encapsulates - the state needed to specify per-vertex attribute data to the OpenGL pipeline. - To put it another way, a VAO remembers the states of buffer objects (see - QOpenGLBuffer) and their associated state (e.g. vertex attribute divisors). - This allows a very easy and efficient method of switching between OpenGL buffer - states for rendering different "objects" in a scene. The QOpenGLVertexArrayObject - class is a thin wrapper around an OpenGL VAO. - - For the desktop, VAOs are supported as a core feature in OpenGL 3.0 or newer and by the - GL_ARB_vertex_array_object for older versions. On OpenGL ES 2, VAOs are provided by - the optional GL_OES_vertex_array_object extension. You can check the version of - OpenGL with QOpenGLContext::surfaceFormat() and check for the presence of extensions - with QOpenGLContext::hasExtension(). - - As with the other Qt OpenGL classes, QOpenGLVertexArrayObject has a create() - function to create the underlying OpenGL object. This is to allow the developer to - ensure that there is a valid current OpenGL context at the time. - - Once you have successfully created a VAO the typical usage pattern is: - - \list - \li In scene initialization function, for each visual object: - \list - \li Bind the VAO - \li Set vertex data state for this visual object (vertices, normals, texture coordinates etc.) - \li Unbind (release()) the VAO - \endlist - \li In render function, for each visual object: - \list - \li Bind the VAO (and shader program if needed) - \li Call a glDraw*() function - \li Unbind (release()) the VAO - \endlist - \endlist - - The act of binding the VAO in the render function has the effect of restoring - all of the vertex data state setup in the initialization phase. In this way we can - set a great deal of state when setting up a VAO and efficiently switch between - state sets of objects to be rendered. Using VAOs also allows the OpenGL driver - to amortise the validation checks of the vertex data. - - \note Vertex Array Objects, like all other OpenGL container objects, are specific - to the context for which they were created and cannot be shared amongst a - context group. - - \sa QOpenGLVertexArrayObject::Binder, QOpenGLBuffer -*/ - -/*! - Creates a QOpenGLVertexArrayObject with the given \a parent. You must call create() - with a valid OpenGL context before using. -*/ -QOpenGLVertexArrayObject::QOpenGLVertexArrayObject(QObject* parent) - : QObject(*new QOpenGLVertexArrayObjectPrivate, parent) -{ -} - -/*! - \internal -*/ -QOpenGLVertexArrayObject::QOpenGLVertexArrayObject(QOpenGLVertexArrayObjectPrivate &dd) - : QObject(dd) -{ -} - -/*! - Destroys the QOpenGLVertexArrayObject and the underlying OpenGL resource. -*/ -QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject() -{ - destroy(); -} - -/*! - Creates the underlying OpenGL vertex array object. There must be a valid OpenGL context - that supports vertex array objects current for this function to succeed. - - Returns \c true if the OpenGL vertex array object was successfully created. - - When the return value is \c false, vertex array object support is not available. This - is not an error: on systems with OpenGL 2.x or OpenGL ES 2.0 vertex array objects may - not be supported. The application is free to continue execution in this case, but it - then has to be prepared to operate in a VAO-less manner too. This means that instead - of merely calling bind(), the value of isCreated() must be checked and the vertex - arrays has to be initialized in the traditional way when there is no vertex array - object present. - - \sa isCreated() -*/ -bool QOpenGLVertexArrayObject::create() -{ - Q_D(QOpenGLVertexArrayObject); - return d->create(); -} - -/*! - Destroys the underlying OpenGL vertex array object. There must be a valid OpenGL context - that supports vertex array objects current for this function to succeed. -*/ -void QOpenGLVertexArrayObject::destroy() -{ - Q_D(QOpenGLVertexArrayObject); - d->destroy(); -} - -/*! - Returns \c true is the underlying OpenGL vertex array object has been created. If this - returns \c true and the associated OpenGL context is current, then you are able to bind() - this object. -*/ -bool QOpenGLVertexArrayObject::isCreated() const -{ - Q_D(const QOpenGLVertexArrayObject); - return (d->vao != 0); -} - -/*! - Returns the id of the underlying OpenGL vertex array object. -*/ -GLuint QOpenGLVertexArrayObject::objectId() const -{ - Q_D(const QOpenGLVertexArrayObject); - return d->vao; -} - -/*! - Binds this vertex array object to the OpenGL binding point. From this point on - and until release() is called or another vertex array object is bound, any - modifications made to vertex data state are stored inside this vertex array object. - - If another vertex array object is then bound you can later restore the set of - state associated with this object by calling bind() on this object once again. - This allows efficient changes between vertex data states in rendering functions. -*/ -void QOpenGLVertexArrayObject::bind() -{ - Q_D(QOpenGLVertexArrayObject); - d->bind(); -} - -/*! - Unbinds this vertex array object by binding the default vertex array object (id = 0). -*/ -void QOpenGLVertexArrayObject::release() -{ - Q_D(QOpenGLVertexArrayObject); - d->release(); -} - - -/*! - \class QOpenGLVertexArrayObject::Binder - \brief The QOpenGLVertexArrayObject::Binder class is a convenience class to help - with the binding and releasing of OpenGL Vertex Array Objects. - \inmodule QtGui - \reentrant - \since 5.1 - \ingroup painting-3D - - QOpenGLVertexArrayObject::Binder is a simple convenience class that can be used - to assist with the binding and releasing of QOpenGLVertexArrayObject instances. - This class is to QOpenGLVertexArrayObject as QMutexLocker is to QMutex. - - This class implements the RAII principle which helps to ensure behavior in - complex code or in the presence of exceptions. - - The constructor of this class accepts a QOpenGLVertexArrayObject (VAO) as an - argument and attempts to bind the VAO, calling QOpenGLVertexArrayObject::create() - if necessary. The destructor of this class calls QOpenGLVertexArrayObject::release() - which unbinds the VAO. - - If needed the VAO can be temporarily unbound with the release() function and bound - once more with rebind(). - - \sa QOpenGLVertexArrayObject -*/ - -/*! - \fn QOpenGLVertexArrayObject::Binder::Binder(QOpenGLVertexArrayObject *v) - - Creates a QOpenGLVertexArrayObject::Binder object and binds \a v by calling - QOpenGLVertexArrayObject::bind(). If necessary it first calls - QOpenGLVertexArrayObject::create(). -*/ - -/*! - \fn QOpenGLVertexArrayObject::Binder::~Binder() - - Destroys the QOpenGLVertexArrayObject::Binder and releases the associated vertex array object. -*/ - -/*! - \fn QOpenGLVertexArrayObject::Binder::release() - - Can be used to temporarily release the associated vertex array object. - - \sa rebind() -*/ - -/*! - \fn QOpenGLVertexArrayObject::Binder::rebind() - - Can be used to rebind the associated vertex array object. - - \sa release() -*/ - -QT_END_NAMESPACE - -#include "moc_qopenglvertexarrayobject.cpp" diff --git a/src/gui/opengl/qopenglvertexarrayobject.h b/src/gui/opengl/qopenglvertexarrayobject.h deleted file mode 100644 index b81ae6a2a9..0000000000 --- a/src/gui/opengl/qopenglvertexarrayobject.h +++ /dev/null @@ -1,111 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sean Harmer -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QOPENGLVERTEXARRAYOBJECT_H -#define QOPENGLVERTEXARRAYOBJECT_H - -#include - -#ifndef QT_NO_OPENGL - -#include -#include - -QT_BEGIN_NAMESPACE - -class QOpenGLVertexArrayObjectPrivate; - -class Q_GUI_EXPORT QOpenGLVertexArrayObject : public QObject -{ - Q_OBJECT - -public: - explicit QOpenGLVertexArrayObject(QObject* parent = nullptr); - ~QOpenGLVertexArrayObject(); - - bool create(); - void destroy(); - bool isCreated() const; - GLuint objectId() const; - void bind(); - void release(); - - class Q_GUI_EXPORT Binder - { - public: - inline Binder(QOpenGLVertexArrayObject *v) - : vao(v) - { - Q_ASSERT(v); - if (vao->isCreated() || vao->create()) - vao->bind(); - } - - inline ~Binder() - { - release(); - } - - inline void release() - { - vao->release(); - } - - inline void rebind() - { - vao->bind(); - } - - private: - Q_DISABLE_COPY(Binder) - QOpenGLVertexArrayObject *vao; - }; - -private: - Q_DISABLE_COPY(QOpenGLVertexArrayObject) - Q_DECLARE_PRIVATE(QOpenGLVertexArrayObject) - Q_PRIVATE_SLOT(d_func(), void _q_contextAboutToBeDestroyed()) - QOpenGLVertexArrayObject(QOpenGLVertexArrayObjectPrivate &dd); -}; - -QT_END_NAMESPACE - -#endif - -#endif // QOPENGLVERTEXARRAYOBJECT_H diff --git a/src/gui/opengl/qopenglvertexarrayobject_p.h b/src/gui/opengl/qopenglvertexarrayobject_p.h deleted file mode 100644 index fd3a6f0f89..0000000000 --- a/src/gui/opengl/qopenglvertexarrayobject_p.h +++ /dev/null @@ -1,125 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sean Harmer -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtGui module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QOPENGLVERTEXARRAYOBJECT_P_H -#define QOPENGLVERTEXARRAYOBJECT_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists for the convenience -// of the Qt OpenGL classes. This header file may change from -// version to version without notice, or even be removed. -// -// We mean it. -// - -#include - -#ifndef QT_NO_OPENGL - -#include - -QT_BEGIN_NAMESPACE - -class QOpenGLVertexArrayObjectHelper; -class QOpenGLContext; - -void Q_GUI_EXPORT qtInitializeVertexArrayObjectHelper(QOpenGLVertexArrayObjectHelper *helper, QOpenGLContext *context); - -class QOpenGLVertexArrayObjectHelper -{ - Q_DISABLE_COPY(QOpenGLVertexArrayObjectHelper) - -public: - explicit inline QOpenGLVertexArrayObjectHelper(QOpenGLContext *context) - : GenVertexArrays(nullptr) - , DeleteVertexArrays(nullptr) - , BindVertexArray(nullptr) - , IsVertexArray(nullptr) - { - qtInitializeVertexArrayObjectHelper(this, context); - } - - inline bool isValid() const - { - return GenVertexArrays && DeleteVertexArrays && BindVertexArray && IsVertexArray; - } - - inline void glGenVertexArrays(GLsizei n, GLuint *arrays) const - { - GenVertexArrays(n, arrays); - } - - inline void glDeleteVertexArrays(GLsizei n, const GLuint *arrays) const - { - DeleteVertexArrays(n, arrays); - } - - inline void glBindVertexArray(GLuint array) const - { - BindVertexArray(array); - } - - inline GLboolean glIsVertexArray(GLuint array) const - { - return IsVertexArray(array); - } - -private: - friend void Q_GUI_EXPORT qtInitializeVertexArrayObjectHelper(QOpenGLVertexArrayObjectHelper *helper, QOpenGLContext *context); - - // Function signatures are equivalent between desktop core, ARB, APPLE, ES 3 and ES 2 extensions - typedef void (QOPENGLF_APIENTRYP qt_GenVertexArrays_t)(GLsizei n, GLuint *arrays); - typedef void (QOPENGLF_APIENTRYP qt_DeleteVertexArrays_t)(GLsizei n, const GLuint *arrays); - typedef void (QOPENGLF_APIENTRYP qt_BindVertexArray_t)(GLuint array); - typedef GLboolean (QOPENGLF_APIENTRYP qt_IsVertexArray_t)(GLuint array); - - qt_GenVertexArrays_t GenVertexArrays; - qt_DeleteVertexArrays_t DeleteVertexArrays; - qt_BindVertexArray_t BindVertexArray; - qt_IsVertexArray_t IsVertexArray; -}; - -QT_END_NAMESPACE - -#endif // QT_NO_OPENGL - -#endif // QOPENGLVERTEXARRAYOBJECT_P_H diff --git a/src/opengl/CMakeLists.txt b/src/opengl/CMakeLists.txt index 17366f7cf0..6063ca58be 100644 --- a/src/opengl/CMakeLists.txt +++ b/src/opengl/CMakeLists.txt @@ -24,6 +24,7 @@ qt_add_module(OpenGL qopengltextureglyphcache.cpp qopengltextureglyphcache_p.h qopengltexturehelper.cpp qopengltexturehelper_p.h qopengltextureuploader.cpp qopengltextureuploader_p.h + qopenglvertexarrayobject.cpp qopenglvertexarrayobject.h qopenglwindow.cpp qopenglwindow.h qtopenglglobal.h DEFINES diff --git a/src/opengl/opengl.pro b/src/opengl/opengl.pro index 950a72cc03..e2b27984c7 100644 --- a/src/opengl/opengl.pro +++ b/src/opengl/opengl.pro @@ -30,6 +30,7 @@ HEADERS += \ qopengltexturecache_p.h \ qopengltextureglyphcache_p.h \ qopengltextureuploader_p.h \ + qopenglvertexarrayobject.h \ qopenglwindow.h \ qtopenglglobal.h @@ -49,6 +50,7 @@ SOURCES += \ qopengltexturecache.cpp \ qopengltextureglyphcache.cpp \ qopengltextureuploader.cpp \ + qopenglvertexarrayobject.cpp \ qopenglwindow.cpp \ qopengldebug.cpp diff --git a/src/opengl/qopengltextureblitter.cpp b/src/opengl/qopengltextureblitter.cpp index 6f0ee863d7..c695ae626d 100644 --- a/src/opengl/qopengltextureblitter.cpp +++ b/src/opengl/qopengltextureblitter.cpp @@ -40,8 +40,8 @@ #include "qopengltextureblitter.h" #include +#include #include -#include #include #include diff --git a/src/opengl/qopenglvertexarrayobject.cpp b/src/opengl/qopenglvertexarrayobject.cpp new file mode 100644 index 0000000000..a6ac282d4c --- /dev/null +++ b/src/opengl/qopenglvertexarrayobject.cpp @@ -0,0 +1,531 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sean Harmer +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtOpenGL module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qopenglvertexarrayobject.h" + +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +QT_BEGIN_NAMESPACE + +class QOpenGLFunctions_3_0; +class QOpenGLFunctions_3_2_Core; + +void qtInitializeVertexArrayObjectHelper(QOpenGLVertexArrayObjectHelper *helper, QOpenGLContext *context) +{ + Q_ASSERT(helper); + Q_ASSERT(context); + + bool tryARB = true; + + if (context->isOpenGLES()) { + if (context->format().majorVersion() >= 3) { + QOpenGLExtraFunctionsPrivate *extra = static_cast(context->extraFunctions())->d(); + helper->GenVertexArrays = extra->f.GenVertexArrays; + helper->DeleteVertexArrays = extra->f.DeleteVertexArrays; + helper->BindVertexArray = extra->f.BindVertexArray; + helper->IsVertexArray = extra->f.IsVertexArray; + tryARB = false; + } else if (context->hasExtension(QByteArrayLiteral("GL_OES_vertex_array_object"))) { + helper->GenVertexArrays = reinterpret_cast(context->getProcAddress("glGenVertexArraysOES")); + helper->DeleteVertexArrays = reinterpret_cast(context->getProcAddress("glDeleteVertexArraysOES")); + helper->BindVertexArray = reinterpret_cast(context->getProcAddress("glBindVertexArrayOES")); + helper->IsVertexArray = reinterpret_cast(context->getProcAddress("glIsVertexArrayOES")); + tryARB = false; + } + } else if (context->hasExtension(QByteArrayLiteral("GL_APPLE_vertex_array_object")) && + !context->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { + helper->GenVertexArrays = reinterpret_cast(context->getProcAddress("glGenVertexArraysAPPLE")); + helper->DeleteVertexArrays = reinterpret_cast(context->getProcAddress("glDeleteVertexArraysAPPLE")); + helper->BindVertexArray = reinterpret_cast(context->getProcAddress("glBindVertexArrayAPPLE")); + helper->IsVertexArray = reinterpret_cast(context->getProcAddress("glIsVertexArrayAPPLE")); + tryARB = false; + } + + if (tryARB && context->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { + helper->GenVertexArrays = reinterpret_cast(context->getProcAddress("glGenVertexArrays")); + helper->DeleteVertexArrays = reinterpret_cast(context->getProcAddress("glDeleteVertexArrays")); + helper->BindVertexArray = reinterpret_cast(context->getProcAddress("glBindVertexArray")); + helper->IsVertexArray = reinterpret_cast(context->getProcAddress("glIsVertexArray")); + } +} + +class QOpenGLVertexArrayObjectPrivate : public QObjectPrivate +{ +public: + QOpenGLVertexArrayObjectPrivate() + : vao(0) + , vaoFuncsType(NotSupported) + , context(nullptr) + { + } + + ~QOpenGLVertexArrayObjectPrivate() + { + if (vaoFuncsType == ARB || vaoFuncsType == APPLE || vaoFuncsType == OES) + delete vaoFuncs.helper; + } + + bool create(); + void destroy(); + void bind(); + void release(); + void _q_contextAboutToBeDestroyed(); + + Q_DECLARE_PUBLIC(QOpenGLVertexArrayObject) + + GLuint vao; + + union { + QOpenGLFunctions_3_0 *core_3_0; + QOpenGLFunctions_3_2_Core *core_3_2; + QOpenGLVertexArrayObjectHelper *helper; + } vaoFuncs; + enum { + NotSupported, + Core_3_0, + Core_3_2, + ARB, + APPLE, + OES + } vaoFuncsType; + + QOpenGLContext *context; +}; + +bool QOpenGLVertexArrayObjectPrivate::create() +{ + if (vao) { + qWarning("QOpenGLVertexArrayObject::create() VAO is already created"); + return false; + } + + Q_Q(QOpenGLVertexArrayObject); + + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (!ctx) { + qWarning("QOpenGLVertexArrayObject::create() requires a valid current OpenGL context"); + return false; + } + + //Fail early, if context is the same as ctx, it means we have tried to initialize for this context and failed + if (ctx == context) + return false; + + context = ctx; + QObject::connect(context, SIGNAL(aboutToBeDestroyed()), q, SLOT(_q_contextAboutToBeDestroyed())); + + if (ctx->isOpenGLES()) { + if (ctx->format().majorVersion() >= 3 || ctx->hasExtension(QByteArrayLiteral("GL_OES_vertex_array_object"))) { + vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); + vaoFuncsType = OES; + vaoFuncs.helper->glGenVertexArrays(1, &vao); + } + } else { + vaoFuncs.core_3_0 = nullptr; + vaoFuncsType = NotSupported; + QSurfaceFormat format = ctx->format(); +#ifndef QT_OPENGL_ES_2 + if (format.version() >= qMakePair(3,2)) { + vaoFuncs.core_3_2 = ctx->versionFunctions(); + vaoFuncsType = Core_3_2; + vaoFuncs.core_3_2->glGenVertexArrays(1, &vao); + } else if (format.majorVersion() >= 3) { + vaoFuncs.core_3_0 = ctx->versionFunctions(); + vaoFuncsType = Core_3_0; + vaoFuncs.core_3_0->glGenVertexArrays(1, &vao); + } else +#endif + if (ctx->hasExtension(QByteArrayLiteral("GL_ARB_vertex_array_object"))) { + vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); + vaoFuncsType = ARB; + vaoFuncs.helper->glGenVertexArrays(1, &vao); + } else if (ctx->hasExtension(QByteArrayLiteral("GL_APPLE_vertex_array_object"))) { + vaoFuncs.helper = new QOpenGLVertexArrayObjectHelper(ctx); + vaoFuncsType = APPLE; + vaoFuncs.helper->glGenVertexArrays(1, &vao); + } + } + + return (vao != 0); +} + +void QOpenGLVertexArrayObjectPrivate::destroy() +{ + Q_Q(QOpenGLVertexArrayObject); + + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + QOpenGLContext *oldContext = nullptr; + QSurface *oldContextSurface = nullptr; + QScopedPointer offscreenSurface; + if (context && context != ctx) { + oldContext = ctx; + oldContextSurface = ctx ? ctx->surface() : nullptr; + // Before going through the effort of creating an offscreen surface + // check that we are on the GUI thread because otherwise many platforms + // will not able to create that offscreen surface. + if (QThread::currentThread() != qGuiApp->thread()) { + ctx = nullptr; + } else { + // Cannot just make the current surface current again with another context. + // The format may be incompatible and some platforms (iOS) may impose + // restrictions on using a window with different contexts. Create an + // offscreen surface (a pbuffer or a hidden window) instead to be safe. + offscreenSurface.reset(new QOffscreenSurface); + offscreenSurface->setFormat(context->format()); + offscreenSurface->create(); + if (context->makeCurrent(offscreenSurface.data())) { + ctx = context; + } else { + qWarning("QOpenGLVertexArrayObject::destroy() failed to make VAO's context current"); + ctx = nullptr; + } + } + } + + if (context) { + QObject::disconnect(context, SIGNAL(aboutToBeDestroyed()), q, SLOT(_q_contextAboutToBeDestroyed())); + context = nullptr; + } + + if (vao && ctx) { + switch (vaoFuncsType) { +#ifndef QT_OPENGL_ES_2 + case Core_3_2: + vaoFuncs.core_3_2->glDeleteVertexArrays(1, &vao); + break; + case Core_3_0: + vaoFuncs.core_3_0->glDeleteVertexArrays(1, &vao); + break; +#endif + case ARB: + case APPLE: + case OES: + vaoFuncs.helper->glDeleteVertexArrays(1, &vao); + break; + default: + break; + } + + vao = 0; + } + + if (oldContext && oldContextSurface) { + if (!oldContext->makeCurrent(oldContextSurface)) + qWarning("QOpenGLVertexArrayObject::destroy() failed to restore current context"); + } +} + +/*! + \internal +*/ +void QOpenGLVertexArrayObjectPrivate::_q_contextAboutToBeDestroyed() +{ + destroy(); +} + +void QOpenGLVertexArrayObjectPrivate::bind() +{ + switch (vaoFuncsType) { +#ifndef QT_OPENGL_ES_2 + case Core_3_2: + vaoFuncs.core_3_2->glBindVertexArray(vao); + break; + case Core_3_0: + vaoFuncs.core_3_0->glBindVertexArray(vao); + break; +#endif + case ARB: + case APPLE: + case OES: + vaoFuncs.helper->glBindVertexArray(vao); + break; + default: + break; + } +} + +void QOpenGLVertexArrayObjectPrivate::release() +{ + switch (vaoFuncsType) { +#ifndef QT_OPENGL_ES_2 + case Core_3_2: + vaoFuncs.core_3_2->glBindVertexArray(0); + break; + case Core_3_0: + vaoFuncs.core_3_0->glBindVertexArray(0); + break; +#endif + case ARB: + case APPLE: + case OES: + vaoFuncs.helper->glBindVertexArray(0); + break; + default: + break; + } +} + + +/*! + \class QOpenGLVertexArrayObject + \brief The QOpenGLVertexArrayObject class wraps an OpenGL Vertex Array Object. + \inmodule QtOpenGL + \since 5.1 + \ingroup painting-3D + + A Vertex Array Object (VAO) is an OpenGL container object that encapsulates + the state needed to specify per-vertex attribute data to the OpenGL pipeline. + To put it another way, a VAO remembers the states of buffer objects (see + QOpenGLBuffer) and their associated state (e.g. vertex attribute divisors). + This allows a very easy and efficient method of switching between OpenGL buffer + states for rendering different "objects" in a scene. The QOpenGLVertexArrayObject + class is a thin wrapper around an OpenGL VAO. + + For the desktop, VAOs are supported as a core feature in OpenGL 3.0 or newer and by the + GL_ARB_vertex_array_object for older versions. On OpenGL ES 2, VAOs are provided by + the optional GL_OES_vertex_array_object extension. You can check the version of + OpenGL with QOpenGLContext::surfaceFormat() and check for the presence of extensions + with QOpenGLContext::hasExtension(). + + As with the other Qt OpenGL classes, QOpenGLVertexArrayObject has a create() + function to create the underlying OpenGL object. This is to allow the developer to + ensure that there is a valid current OpenGL context at the time. + + Once you have successfully created a VAO the typical usage pattern is: + + \list + \li In scene initialization function, for each visual object: + \list + \li Bind the VAO + \li Set vertex data state for this visual object (vertices, normals, texture coordinates etc.) + \li Unbind (release()) the VAO + \endlist + \li In render function, for each visual object: + \list + \li Bind the VAO (and shader program if needed) + \li Call a glDraw*() function + \li Unbind (release()) the VAO + \endlist + \endlist + + The act of binding the VAO in the render function has the effect of restoring + all of the vertex data state setup in the initialization phase. In this way we can + set a great deal of state when setting up a VAO and efficiently switch between + state sets of objects to be rendered. Using VAOs also allows the OpenGL driver + to amortise the validation checks of the vertex data. + + \note Vertex Array Objects, like all other OpenGL container objects, are specific + to the context for which they were created and cannot be shared amongst a + context group. + + \sa QOpenGLVertexArrayObject::Binder, QOpenGLBuffer +*/ + +/*! + Creates a QOpenGLVertexArrayObject with the given \a parent. You must call create() + with a valid OpenGL context before using. +*/ +QOpenGLVertexArrayObject::QOpenGLVertexArrayObject(QObject* parent) + : QObject(*new QOpenGLVertexArrayObjectPrivate, parent) +{ +} + +/*! + \internal +*/ +QOpenGLVertexArrayObject::QOpenGLVertexArrayObject(QOpenGLVertexArrayObjectPrivate &dd) + : QObject(dd) +{ +} + +/*! + Destroys the QOpenGLVertexArrayObject and the underlying OpenGL resource. +*/ +QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject() +{ + destroy(); +} + +/*! + Creates the underlying OpenGL vertex array object. There must be a valid OpenGL context + that supports vertex array objects current for this function to succeed. + + Returns \c true if the OpenGL vertex array object was successfully created. + + When the return value is \c false, vertex array object support is not available. This + is not an error: on systems with OpenGL 2.x or OpenGL ES 2.0 vertex array objects may + not be supported. The application is free to continue execution in this case, but it + then has to be prepared to operate in a VAO-less manner too. This means that instead + of merely calling bind(), the value of isCreated() must be checked and the vertex + arrays has to be initialized in the traditional way when there is no vertex array + object present. + + \sa isCreated() +*/ +bool QOpenGLVertexArrayObject::create() +{ + Q_D(QOpenGLVertexArrayObject); + return d->create(); +} + +/*! + Destroys the underlying OpenGL vertex array object. There must be a valid OpenGL context + that supports vertex array objects current for this function to succeed. +*/ +void QOpenGLVertexArrayObject::destroy() +{ + Q_D(QOpenGLVertexArrayObject); + d->destroy(); +} + +/*! + Returns \c true is the underlying OpenGL vertex array object has been created. If this + returns \c true and the associated OpenGL context is current, then you are able to bind() + this object. +*/ +bool QOpenGLVertexArrayObject::isCreated() const +{ + Q_D(const QOpenGLVertexArrayObject); + return (d->vao != 0); +} + +/*! + Returns the id of the underlying OpenGL vertex array object. +*/ +GLuint QOpenGLVertexArrayObject::objectId() const +{ + Q_D(const QOpenGLVertexArrayObject); + return d->vao; +} + +/*! + Binds this vertex array object to the OpenGL binding point. From this point on + and until release() is called or another vertex array object is bound, any + modifications made to vertex data state are stored inside this vertex array object. + + If another vertex array object is then bound you can later restore the set of + state associated with this object by calling bind() on this object once again. + This allows efficient changes between vertex data states in rendering functions. +*/ +void QOpenGLVertexArrayObject::bind() +{ + Q_D(QOpenGLVertexArrayObject); + d->bind(); +} + +/*! + Unbinds this vertex array object by binding the default vertex array object (id = 0). +*/ +void QOpenGLVertexArrayObject::release() +{ + Q_D(QOpenGLVertexArrayObject); + d->release(); +} + + +/*! + \class QOpenGLVertexArrayObject::Binder + \brief The QOpenGLVertexArrayObject::Binder class is a convenience class to help + with the binding and releasing of OpenGL Vertex Array Objects. + \inmodule QtOpenGL + \reentrant + \since 5.1 + \ingroup painting-3D + + QOpenGLVertexArrayObject::Binder is a simple convenience class that can be used + to assist with the binding and releasing of QOpenGLVertexArrayObject instances. + This class is to QOpenGLVertexArrayObject as QMutexLocker is to QMutex. + + This class implements the RAII principle which helps to ensure behavior in + complex code or in the presence of exceptions. + + The constructor of this class accepts a QOpenGLVertexArrayObject (VAO) as an + argument and attempts to bind the VAO, calling QOpenGLVertexArrayObject::create() + if necessary. The destructor of this class calls QOpenGLVertexArrayObject::release() + which unbinds the VAO. + + If needed the VAO can be temporarily unbound with the release() function and bound + once more with rebind(). + + \sa QOpenGLVertexArrayObject +*/ + +/*! + \fn QOpenGLVertexArrayObject::Binder::Binder(QOpenGLVertexArrayObject *v) + + Creates a QOpenGLVertexArrayObject::Binder object and binds \a v by calling + QOpenGLVertexArrayObject::bind(). If necessary it first calls + QOpenGLVertexArrayObject::create(). +*/ + +/*! + \fn QOpenGLVertexArrayObject::Binder::~Binder() + + Destroys the QOpenGLVertexArrayObject::Binder and releases the associated vertex array object. +*/ + +/*! + \fn QOpenGLVertexArrayObject::Binder::release() + + Can be used to temporarily release the associated vertex array object. + + \sa rebind() +*/ + +/*! + \fn QOpenGLVertexArrayObject::Binder::rebind() + + Can be used to rebind the associated vertex array object. + + \sa release() +*/ + +QT_END_NAMESPACE + +#include "moc_qopenglvertexarrayobject.cpp" diff --git a/src/opengl/qopenglvertexarrayobject.h b/src/opengl/qopenglvertexarrayobject.h new file mode 100644 index 0000000000..0675c844e9 --- /dev/null +++ b/src/opengl/qopenglvertexarrayobject.h @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sean Harmer +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtOpenGL module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QOPENGLVERTEXARRAYOBJECT_H +#define QOPENGLVERTEXARRAYOBJECT_H + +#include + +#ifndef QT_NO_OPENGL + +#include +#include + +QT_BEGIN_NAMESPACE + +class QOpenGLVertexArrayObjectPrivate; + +class Q_OPENGL_EXPORT QOpenGLVertexArrayObject : public QObject +{ + Q_OBJECT + +public: + explicit QOpenGLVertexArrayObject(QObject* parent = nullptr); + ~QOpenGLVertexArrayObject(); + + bool create(); + void destroy(); + bool isCreated() const; + GLuint objectId() const; + void bind(); + void release(); + + class Q_OPENGL_EXPORT Binder + { + public: + inline Binder(QOpenGLVertexArrayObject *v) + : vao(v) + { + Q_ASSERT(v); + if (vao->isCreated() || vao->create()) + vao->bind(); + } + + inline ~Binder() + { + release(); + } + + inline void release() + { + vao->release(); + } + + inline void rebind() + { + vao->bind(); + } + + private: + Q_DISABLE_COPY(Binder) + QOpenGLVertexArrayObject *vao; + }; + +private: + Q_DISABLE_COPY(QOpenGLVertexArrayObject) + Q_DECLARE_PRIVATE(QOpenGLVertexArrayObject) + Q_PRIVATE_SLOT(d_func(), void _q_contextAboutToBeDestroyed()) + QOpenGLVertexArrayObject(QOpenGLVertexArrayObjectPrivate &dd); +}; + +QT_END_NAMESPACE + +#endif + +#endif // QOPENGLVERTEXARRAYOBJECT_H diff --git a/src/opengl/qopenglvertexarrayobject_p.h b/src/opengl/qopenglvertexarrayobject_p.h new file mode 100644 index 0000000000..0d0fca4d81 --- /dev/null +++ b/src/opengl/qopenglvertexarrayobject_p.h @@ -0,0 +1,121 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Sean Harmer +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtOpenGL module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QOPENGLVERTEXARRAYOBJECT_P_H +#define QOPENGLVERTEXARRAYOBJECT_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of the Qt OpenGL classes. This header file may change from +// version to version without notice, or even be removed. +// +// We mean it. +// + +#include + +#include + +QT_BEGIN_NAMESPACE + +class QOpenGLVertexArrayObjectHelper; +class QOpenGLContext; + +void Q_OPENGL_EXPORT qtInitializeVertexArrayObjectHelper(QOpenGLVertexArrayObjectHelper *helper, QOpenGLContext *context); + +class QOpenGLVertexArrayObjectHelper +{ + Q_DISABLE_COPY(QOpenGLVertexArrayObjectHelper) + +public: + explicit inline QOpenGLVertexArrayObjectHelper(QOpenGLContext *context) + : GenVertexArrays(nullptr) + , DeleteVertexArrays(nullptr) + , BindVertexArray(nullptr) + , IsVertexArray(nullptr) + { + qtInitializeVertexArrayObjectHelper(this, context); + } + + inline bool isValid() const + { + return GenVertexArrays && DeleteVertexArrays && BindVertexArray && IsVertexArray; + } + + inline void glGenVertexArrays(GLsizei n, GLuint *arrays) const + { + GenVertexArrays(n, arrays); + } + + inline void glDeleteVertexArrays(GLsizei n, const GLuint *arrays) const + { + DeleteVertexArrays(n, arrays); + } + + inline void glBindVertexArray(GLuint array) const + { + BindVertexArray(array); + } + + inline GLboolean glIsVertexArray(GLuint array) const + { + return IsVertexArray(array); + } + +private: + friend void Q_OPENGL_EXPORT qtInitializeVertexArrayObjectHelper(QOpenGLVertexArrayObjectHelper *helper, QOpenGLContext *context); + + // Function signatures are equivalent between desktop core, ARB, APPLE, ES 3 and ES 2 extensions + typedef void (QOPENGLF_APIENTRYP qt_GenVertexArrays_t)(GLsizei n, GLuint *arrays); + typedef void (QOPENGLF_APIENTRYP qt_DeleteVertexArrays_t)(GLsizei n, const GLuint *arrays); + typedef void (QOPENGLF_APIENTRYP qt_BindVertexArray_t)(GLuint array); + typedef GLboolean (QOPENGLF_APIENTRYP qt_IsVertexArray_t)(GLuint array); + + qt_GenVertexArrays_t GenVertexArrays; + qt_DeleteVertexArrays_t DeleteVertexArrays; + qt_BindVertexArray_t BindVertexArray; + qt_IsVertexArray_t IsVertexArray; +}; + +QT_END_NAMESPACE + +#endif // QOPENGLVERTEXARRAYOBJECT_P_H diff --git a/src/plugins/platforms/eglfs/.prev_CMakeLists.txt b/src/plugins/platforms/eglfs/.prev_CMakeLists.txt index 4fed525392..896b8cd3e8 100644 --- a/src/plugins/platforms/eglfs/.prev_CMakeLists.txt +++ b/src/plugins/platforms/eglfs/.prev_CMakeLists.txt @@ -84,6 +84,8 @@ qt_extend_target(EglFSDeviceIntegration CONDITION QT_FEATURE_opengl SOURCES api/qeglfscontext.cpp api/qeglfscontext_p.h api/qeglfscursor.cpp api/qeglfscursor_p.h + PUBLIC_LIBRARIES + Qt::OpenGLPrivate ) ##################################################################### ## QEglFSIntegrationPlugin Plugin: diff --git a/src/plugins/platforms/eglfs/CMakeLists.txt b/src/plugins/platforms/eglfs/CMakeLists.txt index 03de79d6f0..0078c1f0d7 100644 --- a/src/plugins/platforms/eglfs/CMakeLists.txt +++ b/src/plugins/platforms/eglfs/CMakeLists.txt @@ -80,6 +80,8 @@ qt_extend_target(EglFSDeviceIntegration CONDITION QT_FEATURE_opengl SOURCES api/qeglfscontext.cpp api/qeglfscontext_p.h api/qeglfscursor.cpp api/qeglfscursor_p.h + PUBLIC_LIBRARIES + Qt::OpenGLPrivate ) ##################################################################### ## QEglFSIntegrationPlugin Plugin: diff --git a/src/plugins/platforms/eglfs/api/api.pri b/src/plugins/platforms/eglfs/api/api.pri index 68965b58d8..bcb72c9ea7 100644 --- a/src/plugins/platforms/eglfs/api/api.pri +++ b/src/plugins/platforms/eglfs/api/api.pri @@ -15,6 +15,7 @@ HEADERS += $$PWD/qeglfswindow_p.h \ $$PWD/qeglfsglobal_p.h qtConfig(opengl) { + QT += opengl-private SOURCES += \ $$PWD/qeglfscursor.cpp \ $$PWD/qeglfscontext.cpp diff --git a/src/plugins/platforms/eglfs/api/qeglfscursor.cpp b/src/plugins/platforms/eglfs/api/qeglfscursor.cpp index 98e05195ee..b8bd7e038b 100644 --- a/src/plugins/platforms/eglfs/api/qeglfscursor.cpp +++ b/src/plugins/platforms/eglfs/api/qeglfscursor.cpp @@ -50,7 +50,7 @@ #include #include -#include +#include #ifndef GL_VERTEX_ARRAY_BINDING #define GL_VERTEX_ARRAY_BINDING 0x85B5 diff --git a/tests/auto/gui/qopengl/tst_qopengl.cpp b/tests/auto/gui/qopengl/tst_qopengl.cpp index 03508056f3..2ca844cf1b 100644 --- a/tests/auto/gui/qopengl/tst_qopengl.cpp +++ b/tests/auto/gui/qopengl/tst_qopengl.cpp @@ -30,10 +30,10 @@ #include #include #include +#include #include #include #include -#include #include #include #include -- cgit v1.2.3