diff options
Diffstat (limited to 'src/opengl')
31 files changed, 16113 insertions, 3 deletions
diff --git a/src/opengl/opengl.pro b/src/opengl/opengl.pro index a726db2be9..ba9590ec07 100644 --- a/src/opengl/opengl.pro +++ b/src/opengl/opengl.pro @@ -10,11 +10,39 @@ qtConfig(opengl): CONFIG += opengl qtConfig(opengles2): CONFIG += opengles2 HEADERS += \ + qopengl2pexvertexarray_p.h \ + qopenglcustomshaderstage_p.h \ qopengldebug.h \ + qopenglengineshadermanager_p.h \ + qopenglengineshadersource_p.h \ + qopenglgradientcache_p.h \ + qopenglpaintdevice.h \ + qopenglpaintdevice_p.h \ + qopenglpaintengine_p.h \ + qopenglpixeltransferoptions.h \ + qopenglshadercache_p.h \ + qopengltexture.h \ + qopengltexture_p.h \ + qopengltexturehelper_p.h \ + qopengltexturecache_p.h \ + qopengltextureglyphcache_p.h \ + qopengltextureuploader_p.h \ qopenglwindow.h \ qtopenglglobal.h SOURCES += \ + qopengl2pexvertexarray.cpp \ + qopenglcustomshaderstage.cpp \ + qopenglengineshadermanager.cpp \ + qopenglgradientcache.cpp \ + qopenglpaintdevice.cpp \ + qopenglpaintengine.cpp \ + qopenglpixeltransferoptions.cpp \ + qopengltexture.cpp \ + qopengltexturehelper.cpp \ + qopengltexturecache.cpp \ + qopengltextureglyphcache.cpp \ + qopengltextureuploader.cpp \ qopenglwindow.cpp \ qopengldebug.cpp diff --git a/src/opengl/qopengl2pexvertexarray.cpp b/src/opengl/qopengl2pexvertexarray.cpp new file mode 100644 index 0000000000..df0fb764c9 --- /dev/null +++ b/src/opengl/qopengl2pexvertexarray.cpp @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 "qopengl2pexvertexarray_p.h" + +#include <private/qbezier_p.h> + +QT_BEGIN_NAMESPACE + +void QOpenGL2PEXVertexArray::clear() +{ + vertexArray.reset(); + vertexArrayStops.reset(); + boundingRectDirty = true; +} + + +QOpenGLRect QOpenGL2PEXVertexArray::boundingRect() const +{ + if (boundingRectDirty) + return QOpenGLRect(0.0, 0.0, 0.0, 0.0); + else + return QOpenGLRect(minX, minY, maxX, maxY); +} + +void QOpenGL2PEXVertexArray::addClosingLine(int index) +{ + QPointF point(vertexArray.at(index)); + if (point != QPointF(vertexArray.last())) + vertexArray.add(point); +} + +void QOpenGL2PEXVertexArray::addCentroid(const QVectorPath &path, int subPathIndex) +{ + const QPointF *const points = reinterpret_cast<const QPointF *>(path.points()); + const QPainterPath::ElementType *const elements = path.elements(); + + QPointF sum = points[subPathIndex]; + int count = 1; + + for (int i = subPathIndex + 1; i < path.elementCount() && (!elements || elements[i] != QPainterPath::MoveToElement); ++i) { + sum += points[i]; + ++count; + } + + const QPointF centroid = sum / qreal(count); + vertexArray.add(centroid); +} + +void QOpenGL2PEXVertexArray::addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline) +{ + const QPointF* const points = reinterpret_cast<const QPointF*>(path.points()); + const QPainterPath::ElementType* const elements = path.elements(); + + if (boundingRectDirty) { + minX = maxX = points[0].x(); + minY = maxY = points[0].y(); + boundingRectDirty = false; + } + + if (!outline && !path.isConvex()) + addCentroid(path, 0); + + int lastMoveTo = vertexArray.size(); + vertexArray.add(points[0]); // The first element is always a moveTo + + do { + if (!elements) { +// qDebug("QVectorPath has no elements"); + // If the path has a null elements pointer, the elements implicitly + // start with a moveTo (already added) and continue with lineTos: + for (int i=1; i<path.elementCount(); ++i) + lineToArray(points[i].x(), points[i].y()); + + break; + } +// qDebug("QVectorPath has element types"); + + for (int i=1; i<path.elementCount(); ++i) { + switch (elements[i]) { + case QPainterPath::MoveToElement: + if (!outline) + addClosingLine(lastMoveTo); +// qDebug("element[%d] is a MoveToElement", i); + vertexArrayStops.add(vertexArray.size()); + if (!outline) { + if (!path.isConvex()) addCentroid(path, i); + lastMoveTo = vertexArray.size(); + } + lineToArray(points[i].x(), points[i].y()); // Add the moveTo as a new vertex + break; + case QPainterPath::LineToElement: +// qDebug("element[%d] is a LineToElement", i); + lineToArray(points[i].x(), points[i].y()); + break; + case QPainterPath::CurveToElement: { + QBezier b = QBezier::fromPoints(*(((const QPointF *) points) + i - 1), + points[i], + points[i+1], + points[i+2]); + QRectF bounds = b.bounds(); + // threshold based on same algorithm as in qtriangulatingstroker.cpp + int threshold = qMin<float>(64, qMax(bounds.width(), bounds.height()) * 3.14f / (curveInverseScale * 6)); + if (threshold < 3) threshold = 3; + qreal one_over_threshold_minus_1 = qreal(1) / (threshold - 1); + for (int t=0; t<threshold; ++t) { + QPointF pt = b.pointAt(t * one_over_threshold_minus_1); + lineToArray(pt.x(), pt.y()); + } + i += 2; + break; } + default: + break; + } + } + } while (0); + + if (!outline) + addClosingLine(lastMoveTo); + vertexArrayStops.add(vertexArray.size()); +} + +void QOpenGL2PEXVertexArray::lineToArray(const GLfloat x, const GLfloat y) +{ + vertexArray.add(QOpenGLPoint(x, y)); + + if (x > maxX) + maxX = x; + else if (x < minX) + minX = x; + if (y > maxY) + maxY = y; + else if (y < minY) + minY = y; +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopengl2pexvertexarray_p.h b/src/opengl/qopengl2pexvertexarray_p.h new file mode 100644 index 0000000000..3ef26e908d --- /dev/null +++ b/src/opengl/qopengl2pexvertexarray_p.h @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef QOPENGL2PEXVERTEXARRAY_P_H +#define QOPENGL2PEXVERTEXARRAY_P_H + +#include <QRectF> + +#include <private/qdatabuffer_p.h> +#include <private/qvectorpath_p.h> +#include <private/qopenglcontext_p.h> + +QT_BEGIN_NAMESPACE + +class QOpenGLPoint +{ +public: + QOpenGLPoint(GLfloat new_x, GLfloat new_y) : + x(new_x), y(new_y) {}; + + QOpenGLPoint(const QPointF &p) : + x(p.x()), y(p.y()) {}; + + QOpenGLPoint(const QPointF* p) : + x(p->x()), y(p->y()) {}; + + GLfloat x; + GLfloat y; + + operator QPointF() {return QPointF(x,y);} + operator QPointF() const {return QPointF(x,y);} +}; + +struct QOpenGLRect +{ + QOpenGLRect(const QRectF &r) + : left(r.left()), top(r.top()), right(r.right()), bottom(r.bottom()) {} + + QOpenGLRect(GLfloat l, GLfloat t, GLfloat r, GLfloat b) + : left(l), top(t), right(r), bottom(b) {} + + GLfloat left; + GLfloat top; + GLfloat right; + GLfloat bottom; + + operator QRectF() const {return QRectF(left, top, right-left, bottom-top);} +}; + +class QOpenGL2PEXVertexArray +{ +public: + QOpenGL2PEXVertexArray() : + vertexArray(0), vertexArrayStops(0), + maxX(-2e10), maxY(-2e10), minX(2e10), minY(2e10), + boundingRectDirty(true) + { } + + inline void addRect(const QRectF &rect) + { + qreal top = rect.top(); + qreal left = rect.left(); + qreal bottom = rect.bottom(); + qreal right = rect.right(); + + vertexArray << QOpenGLPoint(left, top) + << QOpenGLPoint(right, top) + << QOpenGLPoint(right, bottom) + << QOpenGLPoint(right, bottom) + << QOpenGLPoint(left, bottom) + << QOpenGLPoint(left, top); + } + + inline void addQuad(const QRectF &rect) + { + qreal top = rect.top(); + qreal left = rect.left(); + qreal bottom = rect.bottom(); + qreal right = rect.right(); + + vertexArray << QOpenGLPoint(left, top) + << QOpenGLPoint(right, top) + << QOpenGLPoint(left, bottom) + << QOpenGLPoint(right, bottom); + + } + + inline void addVertex(const GLfloat x, const GLfloat y) + { + vertexArray.add(QOpenGLPoint(x, y)); + } + + void addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline = true); + void clear(); + + QOpenGLPoint* data() {return vertexArray.data();} + int *stops() const { return vertexArrayStops.data(); } + int stopCount() const { return vertexArrayStops.size(); } + QOpenGLRect boundingRect() const; + + int vertexCount() const { return vertexArray.size(); } + + void lineToArray(const GLfloat x, const GLfloat y); + +private: + QDataBuffer<QOpenGLPoint> vertexArray; + QDataBuffer<int> vertexArrayStops; + + GLfloat maxX; + GLfloat maxY; + GLfloat minX; + GLfloat minY; + bool boundingRectDirty; + void addClosingLine(int index); + void addCentroid(const QVectorPath &path, int subPathIndex); +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/opengl/qopenglcustomshaderstage.cpp b/src/opengl/qopenglcustomshaderstage.cpp new file mode 100644 index 0000000000..7a32b2fbc0 --- /dev/null +++ b/src/opengl/qopenglcustomshaderstage.cpp @@ -0,0 +1,137 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 "qopenglcustomshaderstage_p.h" +#include "qopenglengineshadermanager_p.h" +#include "qopenglpaintengine_p.h" +#include <private/qpainter_p.h> + +QT_BEGIN_NAMESPACE + +class QOpenGLCustomShaderStagePrivate +{ +public: + QOpenGLCustomShaderStagePrivate() : + m_manager(nullptr) {} + + QPointer<QOpenGLEngineShaderManager> m_manager; + QByteArray m_source; +}; + + + + +QOpenGLCustomShaderStage::QOpenGLCustomShaderStage() + : d_ptr(new QOpenGLCustomShaderStagePrivate) +{ +} + +QOpenGLCustomShaderStage::~QOpenGLCustomShaderStage() +{ + Q_D(QOpenGLCustomShaderStage); + if (d->m_manager) { + d->m_manager->removeCustomStage(); + d->m_manager->sharedShaders->cleanupCustomStage(this); + } + delete d_ptr; +} + +void QOpenGLCustomShaderStage::setUniformsDirty() +{ + Q_D(QOpenGLCustomShaderStage); + if (d->m_manager) + d->m_manager->setDirty(); // ### Probably a bit overkill! +} + +bool QOpenGLCustomShaderStage::setOnPainter(QPainter* p) +{ + Q_D(QOpenGLCustomShaderStage); + if (p->paintEngine()->type() != QPaintEngine::OpenGL2) { + qWarning("QOpenGLCustomShaderStage::setOnPainter() - paint engine not OpenGL2"); + return false; + } + if (d->m_manager) + qWarning("Custom shader is already set on a painter"); + + QOpenGL2PaintEngineEx *engine = static_cast<QOpenGL2PaintEngineEx*>(p->paintEngine()); + d->m_manager = QOpenGL2PaintEngineExPrivate::shaderManagerForEngine(engine); + Q_ASSERT(d->m_manager); + + d->m_manager->setCustomStage(this); + return true; +} + +void QOpenGLCustomShaderStage::removeFromPainter(QPainter* p) +{ + Q_D(QOpenGLCustomShaderStage); + if (p->paintEngine()->type() != QPaintEngine::OpenGL2) + return; + + QOpenGL2PaintEngineEx *engine = static_cast<QOpenGL2PaintEngineEx*>(p->paintEngine()); + d->m_manager = QOpenGL2PaintEngineExPrivate::shaderManagerForEngine(engine); + Q_ASSERT(d->m_manager); + + // Just set the stage to null, don't call removeCustomStage(). + // This should leave the program in a compiled/linked state + // if the next custom shader stage is this one again. + d->m_manager->setCustomStage(nullptr); + d->m_manager = nullptr; +} + +QByteArray QOpenGLCustomShaderStage::source() const +{ + Q_D(const QOpenGLCustomShaderStage); + return d->m_source; +} + +// Called by the shader manager if another custom shader is attached or +// the manager is deleted +void QOpenGLCustomShaderStage::setInactive() +{ + Q_D(QOpenGLCustomShaderStage); + d->m_manager = nullptr; +} + +void QOpenGLCustomShaderStage::setSource(const QByteArray& s) +{ + Q_D(QOpenGLCustomShaderStage); + d->m_source = s; +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopenglcustomshaderstage_p.h b/src/opengl/qopenglcustomshaderstage_p.h new file mode 100644 index 0000000000..255c115e3c --- /dev/null +++ b/src/opengl/qopenglcustomshaderstage_p.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 QOPENGL_CUSTOM_SHADER_STAGE_H +#define QOPENGL_CUSTOM_SHADER_STAGE_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtOpenGL/qtopenglglobal.h> +#include <QOpenGLShaderProgram> + +QT_BEGIN_NAMESPACE + + +class QPainter; +class QOpenGLCustomShaderStagePrivate; +class Q_OPENGL_EXPORT QOpenGLCustomShaderStage +{ + Q_DECLARE_PRIVATE(QOpenGLCustomShaderStage) +public: + QOpenGLCustomShaderStage(); + virtual ~QOpenGLCustomShaderStage(); + virtual void setUniforms(QOpenGLShaderProgram*) {} + + void setUniformsDirty(); + + bool setOnPainter(QPainter*); + void removeFromPainter(QPainter*); + QByteArray source() const; + + void setInactive(); +protected: + void setSource(const QByteArray&); + +private: + QOpenGLCustomShaderStagePrivate* d_ptr; + + Q_DISABLE_COPY_MOVE(QOpenGLCustomShaderStage) +}; + + +QT_END_NAMESPACE + + +#endif diff --git a/src/opengl/qopenglengineshadermanager.cpp b/src/opengl/qopenglengineshadermanager.cpp new file mode 100644 index 0000000000..09bd9ff096 --- /dev/null +++ b/src/opengl/qopenglengineshadermanager.cpp @@ -0,0 +1,898 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 "qopenglengineshadermanager_p.h" +#include "qopenglengineshadersource_p.h" +#include "qopenglpaintengine_p.h" +#include <private/qopenglshadercache_p.h> + +#include <QtGui/private/qopenglcontext_p.h> +#include <QtCore/qthreadstorage.h> + +#include <algorithm> + +#if defined(QT_DEBUG) +#include <QMetaEnum> +#endif + +// #define QT_GL_SHARED_SHADER_DEBUG + +QT_BEGIN_NAMESPACE + +class QOpenGLEngineSharedShadersResource : public QOpenGLSharedResource +{ +public: + QOpenGLEngineSharedShadersResource(QOpenGLContext *ctx) + : QOpenGLSharedResource(ctx->shareGroup()) + , m_shaders(new QOpenGLEngineSharedShaders(ctx)) + { + } + + ~QOpenGLEngineSharedShadersResource() + { + delete m_shaders; + } + + void invalidateResource() override + { + delete m_shaders; + m_shaders = nullptr; + } + + void freeResource(QOpenGLContext *) override + { + } + + QOpenGLEngineSharedShaders *shaders() const { return m_shaders; } + +private: + QOpenGLEngineSharedShaders *m_shaders; +}; + +class QOpenGLShaderStorage +{ +public: + QOpenGLEngineSharedShaders *shadersForThread(QOpenGLContext *context) { + QOpenGLMultiGroupSharedResource *&shaders = m_storage.localData(); + if (!shaders) + shaders = new QOpenGLMultiGroupSharedResource; + QOpenGLEngineSharedShadersResource *resource = + shaders->value<QOpenGLEngineSharedShadersResource>(context); + return resource ? resource->shaders() : nullptr; + } + +private: + QThreadStorage<QOpenGLMultiGroupSharedResource *> m_storage; +}; + +Q_GLOBAL_STATIC(QOpenGLShaderStorage, qt_shader_storage); + +QOpenGLEngineSharedShaders *QOpenGLEngineSharedShaders::shadersForContext(QOpenGLContext *context) +{ + return qt_shader_storage()->shadersForThread(context); +} + +const char* QOpenGLEngineSharedShaders::qShaderSnippets[] = { + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0 +}; + +QOpenGLEngineSharedShaders::QOpenGLEngineSharedShaders(QOpenGLContext* context) + : blitShaderProg(nullptr) + , simpleShaderProg(nullptr) +{ + +/* + Rather than having the shader source array statically initialised, it is initialised + here instead. This is to allow new shader names to be inserted or existing names moved + around without having to change the order of the glsl strings. It is hoped this will + make future hard-to-find runtime bugs more obvious and generally give more solid code. +*/ + + // Check if the user has requested an OpenGL 3.2 Core Profile or higher + // and if so use GLSL 1.50 core shaders instead of legacy ones. + const QSurfaceFormat &fmt = context->format(); + const bool isCoreProfile = fmt.profile() == QSurfaceFormat::CoreProfile && fmt.version() >= qMakePair(3,2); + + const char** code = qShaderSnippets; // shortcut + + if (isCoreProfile) { + code[MainVertexShader] = qopenglslMainVertexShader_core; + code[MainWithTexCoordsVertexShader] = qopenglslMainWithTexCoordsVertexShader_core; + code[MainWithTexCoordsAndOpacityVertexShader] = qopenglslMainWithTexCoordsAndOpacityVertexShader_core; + + code[UntransformedPositionVertexShader] = qopenglslUntransformedPositionVertexShader_core; + code[PositionOnlyVertexShader] = qopenglslPositionOnlyVertexShader_core; + code[ComplexGeometryPositionOnlyVertexShader] = qopenglslComplexGeometryPositionOnlyVertexShader_core; + code[PositionWithPatternBrushVertexShader] = qopenglslPositionWithPatternBrushVertexShader_core; + code[PositionWithLinearGradientBrushVertexShader] = qopenglslPositionWithLinearGradientBrushVertexShader_core; + code[PositionWithConicalGradientBrushVertexShader] = qopenglslPositionWithConicalGradientBrushVertexShader_core; + code[PositionWithRadialGradientBrushVertexShader] = qopenglslPositionWithRadialGradientBrushVertexShader_core; + code[PositionWithTextureBrushVertexShader] = qopenglslPositionWithTextureBrushVertexShader_core; + code[AffinePositionWithPatternBrushVertexShader] = qopenglslAffinePositionWithPatternBrushVertexShader_core; + code[AffinePositionWithLinearGradientBrushVertexShader] = qopenglslAffinePositionWithLinearGradientBrushVertexShader_core; + code[AffinePositionWithConicalGradientBrushVertexShader] = qopenglslAffinePositionWithConicalGradientBrushVertexShader_core; + code[AffinePositionWithRadialGradientBrushVertexShader] = qopenglslAffinePositionWithRadialGradientBrushVertexShader_core; + code[AffinePositionWithTextureBrushVertexShader] = qopenglslAffinePositionWithTextureBrushVertexShader_core; + + code[MainFragmentShader_MO] = qopenglslMainFragmentShader_MO_core; + code[MainFragmentShader_M] = qopenglslMainFragmentShader_M_core; + code[MainFragmentShader_O] = qopenglslMainFragmentShader_O_core; + code[MainFragmentShader] = qopenglslMainFragmentShader_core; + code[MainFragmentShader_ImageArrays] = qopenglslMainFragmentShader_ImageArrays_core; + + code[ImageSrcFragmentShader] = qopenglslImageSrcFragmentShader_core; + code[ImageSrcWithPatternFragmentShader] = qopenglslImageSrcWithPatternFragmentShader_core; + code[NonPremultipliedImageSrcFragmentShader] = qopenglslNonPremultipliedImageSrcFragmentShader_core; + code[GrayscaleImageSrcFragmentShader] = qopenglslGrayscaleImageSrcFragmentShader_core; + code[AlphaImageSrcFragmentShader] = qopenglslAlphaImageSrcFragmentShader_core; + code[CustomImageSrcFragmentShader] = qopenglslCustomSrcFragmentShader_core; // Calls "customShader", which must be appended + code[SolidBrushSrcFragmentShader] = qopenglslSolidBrushSrcFragmentShader_core; + + code[TextureBrushSrcFragmentShader] = qopenglslTextureBrushSrcFragmentShader_core; + code[TextureBrushSrcWithPatternFragmentShader] = qopenglslTextureBrushSrcWithPatternFragmentShader_core; + code[PatternBrushSrcFragmentShader] = qopenglslPatternBrushSrcFragmentShader_core; + code[LinearGradientBrushSrcFragmentShader] = qopenglslLinearGradientBrushSrcFragmentShader_core; + code[RadialGradientBrushSrcFragmentShader] = qopenglslRadialGradientBrushSrcFragmentShader_core; + code[ConicalGradientBrushSrcFragmentShader] = qopenglslConicalGradientBrushSrcFragmentShader_core; + code[ShockingPinkSrcFragmentShader] = qopenglslShockingPinkSrcFragmentShader_core; + + code[NoMaskFragmentShader] = ""; + code[MaskFragmentShader] = qopenglslMaskFragmentShader_core; + code[RgbMaskFragmentShaderPass1] = qopenglslRgbMaskFragmentShaderPass1_core; + code[RgbMaskFragmentShaderPass2] = qopenglslRgbMaskFragmentShaderPass2_core; + code[RgbMaskWithGammaFragmentShader] = ""; //### + } else { + code[MainVertexShader] = qopenglslMainVertexShader; + code[MainWithTexCoordsVertexShader] = qopenglslMainWithTexCoordsVertexShader; + code[MainWithTexCoordsAndOpacityVertexShader] = qopenglslMainWithTexCoordsAndOpacityVertexShader; + + code[UntransformedPositionVertexShader] = qopenglslUntransformedPositionVertexShader; + code[PositionOnlyVertexShader] = qopenglslPositionOnlyVertexShader; + code[ComplexGeometryPositionOnlyVertexShader] = qopenglslComplexGeometryPositionOnlyVertexShader; + code[PositionWithPatternBrushVertexShader] = qopenglslPositionWithPatternBrushVertexShader; + code[PositionWithLinearGradientBrushVertexShader] = qopenglslPositionWithLinearGradientBrushVertexShader; + code[PositionWithConicalGradientBrushVertexShader] = qopenglslPositionWithConicalGradientBrushVertexShader; + code[PositionWithRadialGradientBrushVertexShader] = qopenglslPositionWithRadialGradientBrushVertexShader; + code[PositionWithTextureBrushVertexShader] = qopenglslPositionWithTextureBrushVertexShader; + code[AffinePositionWithPatternBrushVertexShader] = qopenglslAffinePositionWithPatternBrushVertexShader; + code[AffinePositionWithLinearGradientBrushVertexShader] = qopenglslAffinePositionWithLinearGradientBrushVertexShader; + code[AffinePositionWithConicalGradientBrushVertexShader] = qopenglslAffinePositionWithConicalGradientBrushVertexShader; + code[AffinePositionWithRadialGradientBrushVertexShader] = qopenglslAffinePositionWithRadialGradientBrushVertexShader; + code[AffinePositionWithTextureBrushVertexShader] = qopenglslAffinePositionWithTextureBrushVertexShader; + + code[MainFragmentShader_MO] = qopenglslMainFragmentShader_MO; + code[MainFragmentShader_M] = qopenglslMainFragmentShader_M; + code[MainFragmentShader_O] = qopenglslMainFragmentShader_O; + code[MainFragmentShader] = qopenglslMainFragmentShader; + code[MainFragmentShader_ImageArrays] = qopenglslMainFragmentShader_ImageArrays; + + code[ImageSrcFragmentShader] = qopenglslImageSrcFragmentShader; + code[ImageSrcWithPatternFragmentShader] = qopenglslImageSrcWithPatternFragmentShader; + code[NonPremultipliedImageSrcFragmentShader] = qopenglslNonPremultipliedImageSrcFragmentShader; + code[GrayscaleImageSrcFragmentShader] = qopenglslGrayscaleImageSrcFragmentShader; + code[AlphaImageSrcFragmentShader] = qopenglslAlphaImageSrcFragmentShader; + code[CustomImageSrcFragmentShader] = qopenglslCustomSrcFragmentShader; // Calls "customShader", which must be appended + code[SolidBrushSrcFragmentShader] = qopenglslSolidBrushSrcFragmentShader; + code[TextureBrushSrcFragmentShader] = qopenglslTextureBrushSrcFragmentShader; + code[TextureBrushSrcWithPatternFragmentShader] = qopenglslTextureBrushSrcWithPatternFragmentShader; + code[PatternBrushSrcFragmentShader] = qopenglslPatternBrushSrcFragmentShader; + code[LinearGradientBrushSrcFragmentShader] = qopenglslLinearGradientBrushSrcFragmentShader; + code[RadialGradientBrushSrcFragmentShader] = qopenglslRadialGradientBrushSrcFragmentShader; + code[ConicalGradientBrushSrcFragmentShader] = qopenglslConicalGradientBrushSrcFragmentShader; + code[ShockingPinkSrcFragmentShader] = qopenglslShockingPinkSrcFragmentShader; + + code[NoMaskFragmentShader] = ""; + code[MaskFragmentShader] = qopenglslMaskFragmentShader; + code[RgbMaskFragmentShaderPass1] = qopenglslRgbMaskFragmentShaderPass1; + code[RgbMaskFragmentShaderPass2] = qopenglslRgbMaskFragmentShaderPass2; + code[RgbMaskWithGammaFragmentShader] = ""; //### + } + + // The composition shaders are just layout qualifiers and the same + // for all profiles that support them. + code[NoCompositionModeFragmentShader] = ""; + code[MultiplyCompositionModeFragmentShader] = qopenglslMultiplyCompositionModeFragmentShader; + code[ScreenCompositionModeFragmentShader] = qopenglslScreenCompositionModeFragmentShader; + code[OverlayCompositionModeFragmentShader] = qopenglslOverlayCompositionModeFragmentShader; + code[DarkenCompositionModeFragmentShader] = qopenglslDarkenCompositionModeFragmentShader; + code[LightenCompositionModeFragmentShader] = qopenglslLightenCompositionModeFragmentShader; + code[ColorDodgeCompositionModeFragmentShader] = qopenglslColorDodgeCompositionModeFragmentShader; + code[ColorBurnCompositionModeFragmentShader] = qopenglslColorBurnCompositionModeFragmentShader; + code[HardLightCompositionModeFragmentShader] = qopenglslHardLightCompositionModeFragmentShader; + code[SoftLightCompositionModeFragmentShader] = qopenglslSoftLightCompositionModeFragmentShader; + code[DifferenceCompositionModeFragmentShader] = qopenglslDifferenceCompositionModeFragmentShader; + code[ExclusionCompositionModeFragmentShader] = qopenglslExclusionCompositionModeFragmentShader; + +#if defined(QT_DEBUG) + // Check that all the elements have been filled: + for (int i = 0; i < TotalSnippetCount; ++i) { + if (Q_UNLIKELY(!qShaderSnippets[i])) { + qFatal("Shader snippet for %s (#%d) is missing!", + snippetNameStr(SnippetName(i)).constData(), i); + } + } +#endif + + QByteArray vertexSource; + QByteArray fragSource; + + // Compile up the simple shader: +#ifdef Q_OS_WASM + vertexSource.append(qShaderSnippets[PositionOnlyVertexShader]); + vertexSource.append(qShaderSnippets[MainVertexShader]); +#else + vertexSource.append(qShaderSnippets[MainVertexShader]); + vertexSource.append(qShaderSnippets[PositionOnlyVertexShader]); +#endif + fragSource.append(qShaderSnippets[MainFragmentShader]); + fragSource.append(qShaderSnippets[ShockingPinkSrcFragmentShader]); + + simpleShaderProg = new QOpenGLShaderProgram; + + CachedShader simpleShaderCache(fragSource, vertexSource); + + bool inCache = simpleShaderCache.load(simpleShaderProg, context); + + if (!inCache) { + if (!simpleShaderProg->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource)) + qWarning("Vertex shader for simpleShaderProg (MainVertexShader & PositionOnlyVertexShader) failed to compile"); + if (!simpleShaderProg->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fragSource)) + qWarning("Fragment shader for simpleShaderProg (MainFragmentShader & ShockingPinkSrcFragmentShader) failed to compile"); + + simpleShaderProg->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR); + simpleShaderProg->bindAttributeLocation("pmvMatrix1", QT_PMV_MATRIX_1_ATTR); + simpleShaderProg->bindAttributeLocation("pmvMatrix2", QT_PMV_MATRIX_2_ATTR); + simpleShaderProg->bindAttributeLocation("pmvMatrix3", QT_PMV_MATRIX_3_ATTR); + } + + simpleShaderProg->link(); + + if (Q_UNLIKELY(!simpleShaderProg->isLinked())) { + qCritical("Errors linking simple shader: %s", qPrintable(simpleShaderProg->log())); + } else { + if (!inCache) + simpleShaderCache.store(simpleShaderProg, context); + } + + // Compile the blit shader: + vertexSource.clear(); + vertexSource.append(qShaderSnippets[MainWithTexCoordsVertexShader]); + vertexSource.append(qShaderSnippets[UntransformedPositionVertexShader]); + + fragSource.clear(); + fragSource.append(qShaderSnippets[MainFragmentShader]); + fragSource.append(qShaderSnippets[ImageSrcFragmentShader]); + + blitShaderProg = new QOpenGLShaderProgram; + + CachedShader blitShaderCache(fragSource, vertexSource); + + inCache = blitShaderCache.load(blitShaderProg, context); + + if (!inCache) { + if (!blitShaderProg->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource)) + qWarning("Vertex shader for blitShaderProg (MainWithTexCoordsVertexShader & UntransformedPositionVertexShader) failed to compile"); + if (!blitShaderProg->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fragSource)) + qWarning("Fragment shader for blitShaderProg (MainFragmentShader & ImageSrcFragmentShader) failed to compile"); + + blitShaderProg->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR); + blitShaderProg->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR); + } + + blitShaderProg->link(); + if (Q_UNLIKELY(!blitShaderProg->isLinked())) { + qCritical("Errors linking blit shader: %s", qPrintable(blitShaderProg->log())); + } else { + if (!inCache) + blitShaderCache.store(blitShaderProg, context); + } + +#ifdef QT_GL_SHARED_SHADER_DEBUG + qDebug(" -> QOpenGLEngineSharedShaders() %p for thread %p.", this, QThread::currentThread()); +#endif +} + +QOpenGLEngineSharedShaders::~QOpenGLEngineSharedShaders() +{ +#ifdef QT_GL_SHARED_SHADER_DEBUG + qDebug(" -> ~QOpenGLEngineSharedShaders() %p for thread %p.", this, QThread::currentThread()); +#endif + qDeleteAll(cachedPrograms); + cachedPrograms.clear(); + + if (blitShaderProg) { + delete blitShaderProg; + blitShaderProg = nullptr; + } + + if (simpleShaderProg) { + delete simpleShaderProg; + simpleShaderProg = nullptr; + } +} + +#if defined (QT_DEBUG) +QByteArray QOpenGLEngineSharedShaders::snippetNameStr(SnippetName name) +{ + QMetaEnum m = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("SnippetName")); + return QByteArray(m.valueToKey(name)); +} +#endif + +// The address returned here will only be valid until next time this function is called. +// The program is return bound. +QOpenGLEngineShaderProg *QOpenGLEngineSharedShaders::findProgramInCache(const QOpenGLEngineShaderProg &prog) +{ + for (int i = 0; i < cachedPrograms.size(); ++i) { + QOpenGLEngineShaderProg *cachedProg = cachedPrograms[i]; + if (*cachedProg == prog) { + // Move the program to the top of the list as a poor-man's cache algo + cachedPrograms.move(i, 0); + cachedProg->program->bind(); + return cachedProg; + } + } + + QScopedPointer<QOpenGLEngineShaderProg> newProg; + + do { + QByteArray fragSource; + // Insert the custom stage before the srcPixel shader to work around an ATI driver bug + // where you cannot forward declare a function that takes a sampler as argument. + if (prog.srcPixelFragShader == CustomImageSrcFragmentShader) + fragSource.append(prog.customStageSource); + fragSource.append(qShaderSnippets[prog.mainFragShader]); + fragSource.append(qShaderSnippets[prog.srcPixelFragShader]); + if (prog.compositionFragShader) + fragSource.append(qShaderSnippets[prog.compositionFragShader]); + if (prog.maskFragShader) + fragSource.append(qShaderSnippets[prog.maskFragShader]); + + QByteArray vertexSource; +#ifdef Q_OS_WASM + vertexSource.append(qShaderSnippets[prog.positionVertexShader]); + vertexSource.append(qShaderSnippets[prog.mainVertexShader]); +#else + vertexSource.append(qShaderSnippets[prog.mainVertexShader]); + vertexSource.append(qShaderSnippets[prog.positionVertexShader]); +#endif + QScopedPointer<QOpenGLShaderProgram> shaderProgram(new QOpenGLShaderProgram); + + CachedShader shaderCache(fragSource, vertexSource); + bool inCache = shaderCache.load(shaderProgram.data(), QOpenGLContext::currentContext()); + + if (!inCache) { + if (!shaderProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, vertexSource)) { + QByteArray description; +#if defined(QT_DEBUG) + description.append("Vertex shader: main="); + description.append(snippetNameStr(prog.mainVertexShader)); + description.append(", position="); + description.append(snippetNameStr(prog.positionVertexShader)); +#endif + qWarning("Warning: \"%s\" failed to compile!", description.constData()); + break; + } + if (!shaderProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, fragSource)) { + QByteArray description; +#if defined(QT_DEBUG) + description.append("Fragment shader: main="); + description.append(snippetNameStr(prog.mainFragShader)); + description.append(", srcPixel="); + description.append(snippetNameStr(prog.srcPixelFragShader)); + if (prog.compositionFragShader) { + description.append(", composition="); + description.append(snippetNameStr(prog.compositionFragShader)); + } + if (prog.maskFragShader) { + description.append(", mask="); + description.append(snippetNameStr(prog.maskFragShader)); + } +#endif + qWarning("Warning: \"%s\" failed to compile!", description.constData()); + break; + } + + // We have to bind the vertex attribute names before the program is linked: + shaderProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR); + if (prog.useTextureCoords) + shaderProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR); + if (prog.useOpacityAttribute) + shaderProgram->bindAttributeLocation("opacityArray", QT_OPACITY_ATTR); + if (prog.usePmvMatrixAttribute) { + shaderProgram->bindAttributeLocation("pmvMatrix1", QT_PMV_MATRIX_1_ATTR); + shaderProgram->bindAttributeLocation("pmvMatrix2", QT_PMV_MATRIX_2_ATTR); + shaderProgram->bindAttributeLocation("pmvMatrix3", QT_PMV_MATRIX_3_ATTR); + } + } + + newProg.reset(new QOpenGLEngineShaderProg(prog)); + newProg->program = shaderProgram.take(); + + newProg->program->link(); + if (newProg->program->isLinked()) { + if (!inCache) + shaderCache.store(newProg->program, QOpenGLContext::currentContext()); + } else { + QString error; + error = QLatin1String("Shader program failed to link") + + QLatin1String(" Error Log:\n") + + QLatin1String(" ") + newProg->program->log(); + qWarning() << error; + break; + } + + newProg->program->bind(); + + if (newProg->maskFragShader != QOpenGLEngineSharedShaders::NoMaskFragmentShader) { + GLuint location = newProg->program->uniformLocation("maskTexture"); + newProg->program->setUniformValue(location, QT_MASK_TEXTURE_UNIT); + } + + if (cachedPrograms.count() > 30) { + // The cache is full, so delete the last 5 programs in the list. + // These programs will be least used, as a program us bumped to + // the top of the list when it's used. + for (int i = 0; i < 5; ++i) { + delete cachedPrograms.last(); + cachedPrograms.removeLast(); + } + } + + cachedPrograms.insert(0, newProg.data()); + } while (false); + + return newProg.take(); +} + +void QOpenGLEngineSharedShaders::cleanupCustomStage(QOpenGLCustomShaderStage* stage) +{ + auto hasStageAsCustomShaderSouce = [stage](QOpenGLEngineShaderProg *cachedProg) -> bool { + if (cachedProg->customStageSource == stage->source()) { + delete cachedProg; + return true; + } + return false; + }; + cachedPrograms.erase(std::remove_if(cachedPrograms.begin(), cachedPrograms.end(), + hasStageAsCustomShaderSouce), + cachedPrograms.end()); +} + + +QOpenGLEngineShaderManager::QOpenGLEngineShaderManager(QOpenGLContext* context) + : ctx(context), + shaderProgNeedsChanging(true), + complexGeometry(false), + srcPixelType(Qt::NoBrush), + opacityMode(NoOpacity), + maskType(NoMask), + compositionMode(QPainter::CompositionMode_SourceOver), + customSrcStage(nullptr), + currentShaderProg(nullptr) +{ + sharedShaders = QOpenGLEngineSharedShaders::shadersForContext(context); +} + +QOpenGLEngineShaderManager::~QOpenGLEngineShaderManager() +{ + //### + removeCustomStage(); +} + +GLuint QOpenGLEngineShaderManager::getUniformLocation(Uniform id) +{ + if (!currentShaderProg) + return 0; + + QVector<uint> &uniformLocations = currentShaderProg->uniformLocations; + if (uniformLocations.isEmpty()) + uniformLocations.fill(GLuint(-1), NumUniforms); + + const char uniformNames[][26] = { + "imageTexture", + "patternColor", + "globalOpacity", + "depth", + "maskTexture", + "fragmentColor", + "linearData", + "angle", + "halfViewportSize", + "fmp", + "fmp2_m_radius2", + "inverse_2_fmp2_m_radius2", + "sqrfr", + "bradius", + "invertedTextureSize", + "brushTransform", + "brushTexture", + "matrix" + }; + + if (uniformLocations.at(id) == GLuint(-1)) + uniformLocations[id] = currentShaderProg->program->uniformLocation(uniformNames[id]); + + return uniformLocations.at(id); +} + + +void QOpenGLEngineShaderManager::optimiseForBrushTransform(QTransform::TransformationType transformType) +{ + Q_UNUSED(transformType); // Currently ignored +} + +void QOpenGLEngineShaderManager::setDirty() +{ + shaderProgNeedsChanging = true; +} + +void QOpenGLEngineShaderManager::setSrcPixelType(Qt::BrushStyle style) +{ + Q_ASSERT(style != Qt::NoBrush); + if (srcPixelType == PixelSrcType(style)) + return; + + srcPixelType = style; + shaderProgNeedsChanging = true; //### +} + +void QOpenGLEngineShaderManager::setSrcPixelType(PixelSrcType type) +{ + if (srcPixelType == type) + return; + + srcPixelType = type; + shaderProgNeedsChanging = true; //### +} + +void QOpenGLEngineShaderManager::setOpacityMode(OpacityMode mode) +{ + if (opacityMode == mode) + return; + + opacityMode = mode; + shaderProgNeedsChanging = true; //### +} + +void QOpenGLEngineShaderManager::setMaskType(MaskType type) +{ + if (maskType == type) + return; + + maskType = type; + shaderProgNeedsChanging = true; //### +} + +void QOpenGLEngineShaderManager::setCompositionMode(QPainter::CompositionMode mode) +{ + if (compositionMode == mode) + return; + + bool wasAdvanced = compositionMode > QPainter::CompositionMode_Plus; + bool isAdvanced = mode > QPainter::CompositionMode_Plus; + + compositionMode = mode; + shaderProgNeedsChanging = shaderProgNeedsChanging || wasAdvanced || isAdvanced; +} + +void QOpenGLEngineShaderManager::setCustomStage(QOpenGLCustomShaderStage* stage) +{ + if (customSrcStage) + removeCustomStage(); + customSrcStage = stage; + shaderProgNeedsChanging = true; +} + +void QOpenGLEngineShaderManager::removeCustomStage() +{ + if (customSrcStage) + customSrcStage->setInactive(); + customSrcStage = nullptr; + shaderProgNeedsChanging = true; +} + +QOpenGLShaderProgram* QOpenGLEngineShaderManager::currentProgram() +{ + if (currentShaderProg) + return currentShaderProg->program; + else + return sharedShaders->simpleProgram(); +} + +void QOpenGLEngineShaderManager::useSimpleProgram() +{ + sharedShaders->simpleProgram()->bind(); + QOpenGLContextPrivate* ctx_d = ctx->d_func(); + Q_UNUSED(ctx_d); + + QOpenGL2PaintEngineEx *active_engine = static_cast<QOpenGL2PaintEngineEx *>(ctx_d->active_engine); + + active_engine->d_func()->setVertexAttribArrayEnabled(QT_VERTEX_COORDS_ATTR, true); + active_engine->d_func()->setVertexAttribArrayEnabled(QT_TEXTURE_COORDS_ATTR, false); + active_engine->d_func()->setVertexAttribArrayEnabled(QT_OPACITY_ATTR, false); + + shaderProgNeedsChanging = true; +} + +void QOpenGLEngineShaderManager::useBlitProgram() +{ + sharedShaders->blitProgram()->bind(); + QOpenGLContextPrivate* ctx_d = ctx->d_func(); + QOpenGL2PaintEngineEx *active_engine = static_cast<QOpenGL2PaintEngineEx *>(ctx_d->active_engine); + active_engine->d_func()->setVertexAttribArrayEnabled(QT_VERTEX_COORDS_ATTR, true); + active_engine->d_func()->setVertexAttribArrayEnabled(QT_TEXTURE_COORDS_ATTR, true); + active_engine->d_func()->setVertexAttribArrayEnabled(QT_OPACITY_ATTR, false); + shaderProgNeedsChanging = true; +} + +QOpenGLShaderProgram* QOpenGLEngineShaderManager::simpleProgram() +{ + return sharedShaders->simpleProgram(); +} + +QOpenGLShaderProgram* QOpenGLEngineShaderManager::blitProgram() +{ + return sharedShaders->blitProgram(); +} + + + +// Select & use the correct shader program using the current state. +// Returns \c true if program needed changing. +bool QOpenGLEngineShaderManager::useCorrectShaderProg() +{ + if (!shaderProgNeedsChanging) + return false; + + bool useCustomSrc = customSrcStage != nullptr; + if (useCustomSrc && srcPixelType != QOpenGLEngineShaderManager::ImageSrc && srcPixelType != Qt::TexturePattern) { + useCustomSrc = false; + qWarning("QOpenGLEngineShaderManager - Ignoring custom shader stage for non image src"); + } + + QOpenGLEngineShaderProg requiredProgram; + + bool texCoords = false; + + // Choose vertex shader shader position function (which typically also sets + // varyings) and the source pixel (srcPixel) fragment shader function: + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::InvalidSnippetName; + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::InvalidSnippetName; + bool isAffine = brushTransform.isAffine(); + if ( (srcPixelType >= Qt::Dense1Pattern) && (srcPixelType <= Qt::DiagCrossPattern) ) { + if (isAffine) + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::AffinePositionWithPatternBrushVertexShader; + else + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionWithPatternBrushVertexShader; + + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::PatternBrushSrcFragmentShader; + } + else switch (srcPixelType) { + default: + case Qt::NoBrush: + qFatal("QOpenGLEngineShaderManager::useCorrectShaderProg() - Qt::NoBrush style is set"); + break; + case QOpenGLEngineShaderManager::ImageSrc: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::ImageSrcFragmentShader; + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; + texCoords = true; + break; + case QOpenGLEngineShaderManager::NonPremultipliedImageSrc: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::NonPremultipliedImageSrcFragmentShader; + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; + texCoords = true; + break; + case QOpenGLEngineShaderManager::GrayscaleImageSrc: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::GrayscaleImageSrcFragmentShader; + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; + texCoords = true; + break; + case QOpenGLEngineShaderManager::AlphaImageSrc: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::AlphaImageSrcFragmentShader; + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; + texCoords = true; + break; + case QOpenGLEngineShaderManager::PatternSrc: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::ImageSrcWithPatternFragmentShader; + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; + texCoords = true; + break; + case QOpenGLEngineShaderManager::TextureSrcWithPattern: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::TextureBrushSrcWithPatternFragmentShader; + requiredProgram.positionVertexShader = isAffine ? QOpenGLEngineSharedShaders::AffinePositionWithTextureBrushVertexShader + : QOpenGLEngineSharedShaders::PositionWithTextureBrushVertexShader; + break; + case Qt::SolidPattern: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::SolidBrushSrcFragmentShader; + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; + break; + case Qt::LinearGradientPattern: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::LinearGradientBrushSrcFragmentShader; + requiredProgram.positionVertexShader = isAffine ? QOpenGLEngineSharedShaders::AffinePositionWithLinearGradientBrushVertexShader + : QOpenGLEngineSharedShaders::PositionWithLinearGradientBrushVertexShader; + break; + case Qt::ConicalGradientPattern: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::ConicalGradientBrushSrcFragmentShader; + requiredProgram.positionVertexShader = isAffine ? QOpenGLEngineSharedShaders::AffinePositionWithConicalGradientBrushVertexShader + : QOpenGLEngineSharedShaders::PositionWithConicalGradientBrushVertexShader; + break; + case Qt::RadialGradientPattern: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::RadialGradientBrushSrcFragmentShader; + requiredProgram.positionVertexShader = isAffine ? QOpenGLEngineSharedShaders::AffinePositionWithRadialGradientBrushVertexShader + : QOpenGLEngineSharedShaders::PositionWithRadialGradientBrushVertexShader; + break; + case Qt::TexturePattern: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::TextureBrushSrcFragmentShader; + requiredProgram.positionVertexShader = isAffine ? QOpenGLEngineSharedShaders::AffinePositionWithTextureBrushVertexShader + : QOpenGLEngineSharedShaders::PositionWithTextureBrushVertexShader; + break; + }; + + if (useCustomSrc) { + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::CustomImageSrcFragmentShader; + requiredProgram.customStageSource = customSrcStage->source(); + } + + const bool hasCompose = compositionMode > QPainter::CompositionMode_Plus; + const bool hasMask = maskType != QOpenGLEngineShaderManager::NoMask; + + // Choose fragment shader main function: + if (opacityMode == AttributeOpacity) { + Q_ASSERT(!hasCompose && !hasMask); + requiredProgram.mainFragShader = QOpenGLEngineSharedShaders::MainFragmentShader_ImageArrays; + } else { + bool useGlobalOpacity = (opacityMode == UniformOpacity); + if (hasMask && useGlobalOpacity) + requiredProgram.mainFragShader = QOpenGLEngineSharedShaders::MainFragmentShader_MO; + if (hasMask && !useGlobalOpacity) + requiredProgram.mainFragShader = QOpenGLEngineSharedShaders::MainFragmentShader_M; + if (!hasMask && useGlobalOpacity) + requiredProgram.mainFragShader = QOpenGLEngineSharedShaders::MainFragmentShader_O; + if (!hasMask && !useGlobalOpacity) + requiredProgram.mainFragShader = QOpenGLEngineSharedShaders::MainFragmentShader; + } + + if (hasMask) { + if (maskType == PixelMask) { + requiredProgram.maskFragShader = QOpenGLEngineSharedShaders::MaskFragmentShader; + texCoords = true; + } else if (maskType == SubPixelMaskPass1) { + requiredProgram.maskFragShader = QOpenGLEngineSharedShaders::RgbMaskFragmentShaderPass1; + texCoords = true; + } else if (maskType == SubPixelMaskPass2) { + requiredProgram.maskFragShader = QOpenGLEngineSharedShaders::RgbMaskFragmentShaderPass2; + texCoords = true; + } else if (maskType == SubPixelWithGammaMask) { + requiredProgram.maskFragShader = QOpenGLEngineSharedShaders::RgbMaskWithGammaFragmentShader; + texCoords = true; + } else { + qCritical("QOpenGLEngineShaderManager::useCorrectShaderProg() - Unknown mask type"); + } + } else { + requiredProgram.maskFragShader = QOpenGLEngineSharedShaders::NoMaskFragmentShader; + } + + if (hasCompose) { + switch (compositionMode) { + case QPainter::CompositionMode_Multiply: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::MultiplyCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_Screen: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::ScreenCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_Overlay: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::OverlayCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_Darken: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::DarkenCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_Lighten: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::LightenCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_ColorDodge: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::ColorDodgeCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_ColorBurn: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::ColorBurnCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_HardLight: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::HardLightCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_SoftLight: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::SoftLightCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_Difference: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::DifferenceCompositionModeFragmentShader; + break; + case QPainter::CompositionMode_Exclusion: + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::ExclusionCompositionModeFragmentShader; + break; + default: + qWarning("QOpenGLEngineShaderManager::useCorrectShaderProg() - Unsupported composition mode"); + } + } else { + requiredProgram.compositionFragShader = QOpenGLEngineSharedShaders::NoCompositionModeFragmentShader; + } + + // Choose vertex shader main function + if (opacityMode == AttributeOpacity) { + Q_ASSERT(texCoords); + requiredProgram.mainVertexShader = QOpenGLEngineSharedShaders::MainWithTexCoordsAndOpacityVertexShader; + } else if (texCoords) { + requiredProgram.mainVertexShader = QOpenGLEngineSharedShaders::MainWithTexCoordsVertexShader; + } else { + requiredProgram.mainVertexShader = QOpenGLEngineSharedShaders::MainVertexShader; + } + requiredProgram.useTextureCoords = texCoords; + requiredProgram.useOpacityAttribute = (opacityMode == AttributeOpacity); + if (complexGeometry && srcPixelType == Qt::SolidPattern) { + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::ComplexGeometryPositionOnlyVertexShader; + requiredProgram.usePmvMatrixAttribute = false; + } else { + requiredProgram.usePmvMatrixAttribute = true; + + // Force complexGeometry off, since we currently don't support that mode for + // non-solid brushes + complexGeometry = false; + } + + // At this point, requiredProgram is fully populated so try to find the program in the cache + currentShaderProg = sharedShaders->findProgramInCache(requiredProgram); + + if (currentShaderProg && useCustomSrc) { + customSrcStage->setUniforms(currentShaderProg->program); + } + + // Make sure all the vertex attribute arrays the program uses are enabled (and the ones it + // doesn't use are disabled) + QOpenGLContextPrivate* ctx_d = ctx->d_func(); + QOpenGL2PaintEngineEx *active_engine = static_cast<QOpenGL2PaintEngineEx *>(ctx_d->active_engine); + active_engine->d_func()->setVertexAttribArrayEnabled(QT_VERTEX_COORDS_ATTR, true); + active_engine->d_func()->setVertexAttribArrayEnabled(QT_TEXTURE_COORDS_ATTR, currentShaderProg && currentShaderProg->useTextureCoords); + active_engine->d_func()->setVertexAttribArrayEnabled(QT_OPACITY_ATTR, currentShaderProg && currentShaderProg->useOpacityAttribute); + + shaderProgNeedsChanging = false; + return true; +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopenglengineshadermanager_p.h b/src/opengl/qopenglengineshadermanager_p.h new file mode 100644 index 0000000000..71e6214278 --- /dev/null +++ b/src/opengl/qopenglengineshadermanager_p.h @@ -0,0 +1,506 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +/* + VERTEX SHADERS + ============== + + Vertex shaders are specified as multiple (partial) shaders. On desktop, + this works fine. On ES, QOpenGLShader & QOpenGLShaderProgram will make partial + shaders work by concatenating the source in each QOpenGLShader and compiling + it as a single shader. This is abstracted nicely by QOpenGLShaderProgram and + the GL2 engine doesn't need to worry about it. + + Generally, there's two vertex shader objects. The position shaders are + the ones which set gl_Position. There's also two "main" vertex shaders, + one which just calls the position shader and another which also passes + through some texture coordinates from a vertex attribute array to a + varying. These texture coordinates are used for mask position in text + rendering and for the source coordinates in drawImage/drawPixmap. There's + also a "Simple" vertex shader for rendering a solid colour (used to render + into the stencil buffer where the actual colour value is discarded). + + The position shaders for brushes look scary. This is because many of the + calculations which logically belong in the fragment shader have been moved + into the vertex shader to improve performance. This is why the position + calculation is in a separate shader. Not only does it calculate the + position, but it also calculates some data to be passed to the fragment + shader as a varying. It is optimal to move as much of the calculation as + possible into the vertex shader as this is executed less often. + + The varyings passed to the fragment shaders are interpolated (which is + cheap). Unfortunately, GL will apply perspective correction to the + interpolation calusing errors. To get around this, the vertex shader must + apply perspective correction itself and set the w-value of gl_Position to + zero. That way, GL will be tricked into thinking it doesn't need to apply a + perspective correction and use linear interpolation instead (which is what + we want). Of course, if the brush transform is affeine, no perspective + correction is needed and a simpler vertex shader can be used instead. + + So there are the following "main" vertex shaders: + qopenglslMainVertexShader + qopenglslMainWithTexCoordsVertexShader + + And the following position vertex shaders: + qopenglslPositionOnlyVertexShader + qopenglslPositionWithTextureBrushVertexShader + qopenglslPositionWithPatternBrushVertexShader + qopenglslPositionWithLinearGradientBrushVertexShader + qopenglslPositionWithRadialGradientBrushVertexShader + qopenglslPositionWithConicalGradientBrushVertexShader + qopenglslAffinePositionWithTextureBrushVertexShader + qopenglslAffinePositionWithPatternBrushVertexShader + qopenglslAffinePositionWithLinearGradientBrushVertexShader + qopenglslAffinePositionWithRadialGradientBrushVertexShader + qopenglslAffinePositionWithConicalGradientBrushVertexShader + + Leading to 23 possible vertex shaders + + + FRAGMENT SHADERS + ================ + + Fragment shaders are also specified as multiple (partial) shaders. The + different fragment shaders represent the different stages in Qt's fragment + pipeline. There are 1-3 stages in this pipeline: First stage is to get the + fragment's colour value. The next stage is to get the fragment's mask value + (coverage value for anti-aliasing) and the final stage is to blend the + incoming fragment with the background (for composition modes not supported + by GL). + + Of these, the first stage will always be present. If Qt doesn't need to + apply anti-aliasing (because it's off or handled by multisampling) then + the coverage value doesn't need to be applied. (Note: There are two types + of mask, one for regular anti-aliasing and one for sub-pixel anti- + aliasing.) If the composition mode is one which GL supports natively then + the blending stage doesn't need to be applied. + + As eash stage can have multiple implementations, they are abstracted as + GLSL function calls with the following signatures: + + Brushes & image drawing are implementations of "qcolorp vec4 srcPixel()": + qopenglslImageSrcFragShader + qopenglslImageSrcWithPatternFragShader + qopenglslNonPremultipliedImageSrcFragShader + qopenglslSolidBrushSrcFragShader + qopenglslTextureBrushSrcFragShader + qopenglslTextureBrushWithPatternFragShader + qopenglslPatternBrushSrcFragShader + qopenglslLinearGradientBrushSrcFragShader + qopenglslRadialGradientBrushSrcFragShader + qopenglslConicalGradientBrushSrcFragShader + NOTE: It is assumed the colour returned by srcPixel() is pre-multiplied + + Masks are implementations of "qcolorp vec4 applyMask(qcolorp vec4 src)": + qopenglslMaskFragmentShader + qopenglslRgbMaskFragmentShaderPass1 + qopenglslRgbMaskFragmentShaderPass2 + qopenglslRgbMaskWithGammaFragmentShader + + Composition modes are "qcolorp vec4 compose(qcolorp vec4 src)": + qopenglslColorBurnCompositionModeFragmentShader + qopenglslColorDodgeCompositionModeFragmentShader + qopenglslDarkenCompositionModeFragmentShader + qopenglslDifferenceCompositionModeFragmentShader + qopenglslExclusionCompositionModeFragmentShader + qopenglslHardLightCompositionModeFragmentShader + qopenglslLightenCompositionModeFragmentShader + qopenglslMultiplyCompositionModeFragmentShader + qopenglslOverlayCompositionModeFragmentShader + qopenglslScreenCompositionModeFragmentShader + qopenglslSoftLightCompositionModeFragmentShader + + + Note: In the future, some GLSL compilers will support an extension allowing + a new 'color' precision specifier. To support this, qcolorp is used for + all color components so it can be defined to colorp or lowp depending upon + the implementation. + + So there are differnt frament shader main functions, depending on the + number & type of pipelines the fragment needs to go through. + + The choice of which main() fragment shader string to use depends on: + - Use of global opacity + - Brush style (some brushes apply opacity themselves) + - Use & type of mask (TODO: Need to support high quality anti-aliasing & text) + - Use of non-GL Composition mode + + Leading to the following fragment shader main functions: + gl_FragColor = compose(applyMask(srcPixel()*globalOpacity)); + gl_FragColor = compose(applyMask(srcPixel())); + gl_FragColor = applyMask(srcPixel()*globalOpacity); + gl_FragColor = applyMask(srcPixel()); + gl_FragColor = compose(srcPixel()*globalOpacity); + gl_FragColor = compose(srcPixel()); + gl_FragColor = srcPixel()*globalOpacity; + gl_FragColor = srcPixel(); + + Called: + qopenglslMainFragmentShader_CMO + qopenglslMainFragmentShader_CM + qopenglslMainFragmentShader_MO + qopenglslMainFragmentShader_M + qopenglslMainFragmentShader_CO + qopenglslMainFragmentShader_C + qopenglslMainFragmentShader_O + qopenglslMainFragmentShader + + Where: + M = Mask + C = Composition + O = Global Opacity + + + CUSTOM SHADER CODE + ================== + + The use of custom shader code is supported by the engine for drawImage and + drawPixmap calls. This is implemented via hooks in the fragment pipeline. + + The custom shader is passed to the engine as a partial fragment shader + (QOpenGLCustomShaderStage). The shader will implement a pre-defined method name + which Qt's fragment pipeline will call: + + lowp vec4 customShader(lowp sampler2d imageTexture, highp vec2 textureCoords) + + The provided src and srcCoords parameters can be used to sample from the + source image. + + Transformations, clipping, opacity, and composition modes set using QPainter + will be respected when using the custom shader hook. +*/ + +#ifndef QOPENGLENGINE_SHADER_MANAGER_H +#define QOPENGLENGINE_SHADER_MANAGER_H + +#include <QOpenGLShader> +#include <QOpenGLShaderProgram> +#include <QPainter> +#include <private/qopenglcontext_p.h> +#include <private/qopenglcustomshaderstage_p.h> + +QT_BEGIN_NAMESPACE + + + +/* +struct QOpenGLEngineCachedShaderProg +{ + QOpenGLEngineCachedShaderProg(QOpenGLEngineShaderManager::ShaderName vertexMain, + QOpenGLEngineShaderManager::ShaderName vertexPosition, + QOpenGLEngineShaderManager::ShaderName fragMain, + QOpenGLEngineShaderManager::ShaderName pixelSrc, + QOpenGLEngineShaderManager::ShaderName mask, + QOpenGLEngineShaderManager::ShaderName composition); + + int cacheKey; + QOpenGLShaderProgram* program; +} +*/ + +static const GLuint QT_VERTEX_COORDS_ATTR = 0; +static const GLuint QT_TEXTURE_COORDS_ATTR = 1; +static const GLuint QT_OPACITY_ATTR = 2; +static const GLuint QT_PMV_MATRIX_1_ATTR = 3; +static const GLuint QT_PMV_MATRIX_2_ATTR = 4; +static const GLuint QT_PMV_MATRIX_3_ATTR = 5; + +class QOpenGLEngineShaderProg; + +class Q_OPENGL_EXPORT QOpenGLEngineSharedShaders +{ + Q_GADGET +public: + + enum SnippetName { + MainVertexShader, + MainWithTexCoordsVertexShader, + MainWithTexCoordsAndOpacityVertexShader, + + // UntransformedPositionVertexShader must be first in the list: + UntransformedPositionVertexShader, + PositionOnlyVertexShader, + ComplexGeometryPositionOnlyVertexShader, + PositionWithPatternBrushVertexShader, + PositionWithLinearGradientBrushVertexShader, + PositionWithConicalGradientBrushVertexShader, + PositionWithRadialGradientBrushVertexShader, + PositionWithTextureBrushVertexShader, + AffinePositionWithPatternBrushVertexShader, + AffinePositionWithLinearGradientBrushVertexShader, + AffinePositionWithConicalGradientBrushVertexShader, + AffinePositionWithRadialGradientBrushVertexShader, + AffinePositionWithTextureBrushVertexShader, + + // MainFragmentShader_CMO must be first in the list: + MainFragmentShader_MO, + MainFragmentShader_M, + MainFragmentShader_O, + MainFragmentShader, + MainFragmentShader_ImageArrays, + + // ImageSrcFragmentShader must be first in the list:: + ImageSrcFragmentShader, + ImageSrcWithPatternFragmentShader, + NonPremultipliedImageSrcFragmentShader, + GrayscaleImageSrcFragmentShader, + AlphaImageSrcFragmentShader, + CustomImageSrcFragmentShader, + SolidBrushSrcFragmentShader, + TextureBrushSrcFragmentShader, + TextureBrushSrcWithPatternFragmentShader, + PatternBrushSrcFragmentShader, + LinearGradientBrushSrcFragmentShader, + RadialGradientBrushSrcFragmentShader, + ConicalGradientBrushSrcFragmentShader, + ShockingPinkSrcFragmentShader, + + // NoMaskFragmentShader must be first in the list: + NoMaskFragmentShader, + MaskFragmentShader, + RgbMaskFragmentShaderPass1, + RgbMaskFragmentShaderPass2, + RgbMaskWithGammaFragmentShader, + + // NoCompositionModeFragmentShader must be first in the list: + NoCompositionModeFragmentShader, + MultiplyCompositionModeFragmentShader, + ScreenCompositionModeFragmentShader, + OverlayCompositionModeFragmentShader, + DarkenCompositionModeFragmentShader, + LightenCompositionModeFragmentShader, + ColorDodgeCompositionModeFragmentShader, + ColorBurnCompositionModeFragmentShader, + HardLightCompositionModeFragmentShader, + SoftLightCompositionModeFragmentShader, + DifferenceCompositionModeFragmentShader, + ExclusionCompositionModeFragmentShader, + + TotalSnippetCount, InvalidSnippetName + }; +#if defined (QT_DEBUG) + Q_ENUM(SnippetName) + static QByteArray snippetNameStr(SnippetName snippetName); +#endif + +/* + // These allow the ShaderName enum to be used as a cache key + const int mainVertexOffset = 0; + const int positionVertexOffset = (1<<2) - PositionOnlyVertexShader; + const int mainFragOffset = (1<<6) - MainFragmentShader_CMO; + const int srcPixelOffset = (1<<10) - ImageSrcFragmentShader; + const int maskOffset = (1<<14) - NoMaskShader; + const int compositionOffset = (1 << 16) - MultiplyCompositionModeFragmentShader; +*/ + + QOpenGLEngineSharedShaders(QOpenGLContext *context); + ~QOpenGLEngineSharedShaders(); + + QOpenGLShaderProgram *simpleProgram() { return simpleShaderProg; } + QOpenGLShaderProgram *blitProgram() { return blitShaderProg; } + // Compile the program if it's not already in the cache, return the item in the cache. + QOpenGLEngineShaderProg *findProgramInCache(const QOpenGLEngineShaderProg &prog); + // Compile the custom shader if it's not already in the cache, return the item in the cache. + + static QOpenGLEngineSharedShaders *shadersForContext(QOpenGLContext *context); + + // Ideally, this would be static and cleanup all programs in all contexts which + // contain the custom code. Currently it is just a hint and we rely on deleted + // custom shaders being cleaned up by being kicked out of the cache when it's + // full. + void cleanupCustomStage(QOpenGLCustomShaderStage* stage); + +private: + QOpenGLShaderProgram *blitShaderProg; + QOpenGLShaderProgram *simpleShaderProg; + QList<QOpenGLEngineShaderProg*> cachedPrograms; + + static const char* qShaderSnippets[TotalSnippetCount]; +}; + + +class QOpenGLEngineShaderProg +{ +public: + QOpenGLEngineShaderProg() : program(nullptr) {} + + ~QOpenGLEngineShaderProg() { + if (program) + delete program; + } + + QOpenGLEngineSharedShaders::SnippetName mainVertexShader; + QOpenGLEngineSharedShaders::SnippetName positionVertexShader; + QOpenGLEngineSharedShaders::SnippetName mainFragShader; + QOpenGLEngineSharedShaders::SnippetName srcPixelFragShader; + QOpenGLEngineSharedShaders::SnippetName maskFragShader; + QOpenGLEngineSharedShaders::SnippetName compositionFragShader; + + QByteArray customStageSource; //TODO: Decent cache key for custom stages + QOpenGLShaderProgram* program; + + QVector<uint> uniformLocations; + + bool useTextureCoords; + bool useOpacityAttribute; + bool usePmvMatrixAttribute; + + bool operator==(const QOpenGLEngineShaderProg& other) const { + // We don't care about the program + return ( mainVertexShader == other.mainVertexShader && + positionVertexShader == other.positionVertexShader && + mainFragShader == other.mainFragShader && + srcPixelFragShader == other.srcPixelFragShader && + maskFragShader == other.maskFragShader && + compositionFragShader == other.compositionFragShader && + customStageSource == other.customStageSource + ); + } +}; + +class Q_OPENGL_EXPORT QOpenGLEngineShaderManager : public QObject +{ + Q_OBJECT +public: + QOpenGLEngineShaderManager(QOpenGLContext* context); + ~QOpenGLEngineShaderManager(); + + enum MaskType {NoMask, PixelMask, SubPixelMaskPass1, SubPixelMaskPass2, SubPixelWithGammaMask}; + enum PixelSrcType { + ImageSrc = Qt::TexturePattern+1, + NonPremultipliedImageSrc = Qt::TexturePattern+2, + PatternSrc = Qt::TexturePattern+3, + TextureSrcWithPattern = Qt::TexturePattern+4, + GrayscaleImageSrc = Qt::TexturePattern+5, + AlphaImageSrc = Qt::TexturePattern+6, + }; + + enum Uniform { + ImageTexture, + PatternColor, + GlobalOpacity, + Depth, + MaskTexture, + FragmentColor, + LinearData, + Angle, + HalfViewportSize, + Fmp, + Fmp2MRadius2, + Inverse2Fmp2MRadius2, + SqrFr, + BRadius, + InvertedTextureSize, + BrushTransform, + BrushTexture, + Matrix, + NumUniforms + }; + + enum OpacityMode { + NoOpacity, + UniformOpacity, + AttributeOpacity + }; + + // There are optimizations we can do, depending on the brush transform: + // 1) May not have to apply perspective-correction + // 2) Can use lower precision for matrix + void optimiseForBrushTransform(QTransform::TransformationType transformType); + void setSrcPixelType(Qt::BrushStyle); + void setSrcPixelType(PixelSrcType); // For non-brush sources, like pixmaps & images + void setOpacityMode(OpacityMode); + void setMaskType(MaskType); + void setCompositionMode(QPainter::CompositionMode); + void setCustomStage(QOpenGLCustomShaderStage* stage); + void removeCustomStage(); + + GLuint getUniformLocation(Uniform id); + + void setDirty(); // someone has manually changed the current shader program + bool useCorrectShaderProg(); // returns true if the shader program needed to be changed + + void useSimpleProgram(); + void useBlitProgram(); + void setHasComplexGeometry(bool hasComplexGeometry) + { + complexGeometry = hasComplexGeometry; + shaderProgNeedsChanging = true; + } + bool hasComplexGeometry() const + { + return complexGeometry; + } + + QOpenGLShaderProgram* currentProgram(); // Returns pointer to the shader the manager has chosen + QOpenGLShaderProgram* simpleProgram(); // Used to draw into e.g. stencil buffers + QOpenGLShaderProgram* blitProgram(); // Used to blit a texture into the framebuffer + + QOpenGLEngineSharedShaders* sharedShaders; + +private: + QOpenGLContext* ctx; + bool shaderProgNeedsChanging; + bool complexGeometry; + + // Current state variables which influence the choice of shader: + QTransform brushTransform; + int srcPixelType; + OpacityMode opacityMode; + MaskType maskType; + QPainter::CompositionMode compositionMode; + QOpenGLCustomShaderStage* customSrcStage; + + QOpenGLEngineShaderProg* currentShaderProg; +}; + +QT_END_NAMESPACE + +#endif //QOPENGLENGINE_SHADER_MANAGER_H diff --git a/src/opengl/qopenglengineshadersource_p.h b/src/opengl/qopenglengineshadersource_p.h new file mode 100644 index 0000000000..49d17c8d79 --- /dev/null +++ b/src/opengl/qopenglengineshadersource_p.h @@ -0,0 +1,969 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + + +#ifndef QOPENGL_ENGINE_SHADER_SOURCE_H +#define QOPENGL_ENGINE_SHADER_SOURCE_H + +#include "qopenglengineshadermanager_p.h" + +QT_BEGIN_NAMESPACE + + +static const char* const qopenglslMainVertexShader = "\n\ + void setPosition(); \n\ + void main(void) \n\ + { \n\ + setPosition(); \n\ + }\n"; + +static const char* const qopenglslMainWithTexCoordsVertexShader = "\n\ + attribute highp vec2 textureCoordArray; \n\ + varying highp vec2 textureCoords; \n\ + void setPosition(); \n\ + void main(void) \n\ + { \n\ + setPosition(); \n\ + textureCoords = textureCoordArray; \n\ + }\n"; + +static const char* const qopenglslMainWithTexCoordsAndOpacityVertexShader = "\n\ + attribute highp vec2 textureCoordArray; \n\ + attribute lowp float opacityArray; \n\ + varying highp vec2 textureCoords; \n\ + varying lowp float opacity; \n\ + void setPosition(); \n\ + void main(void) \n\ + { \n\ + setPosition(); \n\ + textureCoords = textureCoordArray; \n\ + opacity = opacityArray; \n\ + }\n"; + +// NOTE: We let GL do the perspective correction so texture lookups in the fragment +// shader are also perspective corrected. +static const char* const qopenglslPositionOnlyVertexShader = "\n\ + attribute highp vec2 vertexCoordsArray; \n\ + attribute highp vec3 pmvMatrix1; \n\ + attribute highp vec3 pmvMatrix2; \n\ + attribute highp vec3 pmvMatrix3; \n\ + void setPosition(void) \n\ + { \n\ + highp mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position = vec4(transformedPos.xy, 0.0, transformedPos.z); \n\ + }\n"; + +static const char* const qopenglslComplexGeometryPositionOnlyVertexShader = "\n\ + uniform highp mat3 matrix; \n\ + attribute highp vec2 vertexCoordsArray; \n\ + void setPosition(void) \n\ + { \n\ + gl_Position = vec4(matrix * vec3(vertexCoordsArray, 1), 1);\n\ + } \n"; + +static const char* const qopenglslUntransformedPositionVertexShader = "\n\ + attribute highp vec4 vertexCoordsArray; \n\ + void setPosition(void) \n\ + { \n\ + gl_Position = vertexCoordsArray; \n\ + }\n"; + +// Pattern Brush - This assumes the texture size is 8x8 and thus, the inverted size is 0.125 +static const char* const qopenglslPositionWithPatternBrushVertexShader = "\n\ + attribute highp vec2 vertexCoordsArray; \n\ + attribute highp vec3 pmvMatrix1; \n\ + attribute highp vec3 pmvMatrix2; \n\ + attribute highp vec3 pmvMatrix3; \n\ + uniform mediump vec2 halfViewportSize; \n\ + uniform highp vec2 invertedTextureSize; \n\ + uniform highp mat3 brushTransform; \n\ + varying highp vec2 patternTexCoords; \n\ + void setPosition(void) \n\ + { \n\ + highp mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + mediump vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + mediump vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1.0); \n\ + mediump float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + patternTexCoords.xy = (hTexCoords.xy * 0.125) * invertedHTexCoordsZ; \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithPatternBrushVertexShader + = qopenglslPositionWithPatternBrushVertexShader; + +static const char* const qopenglslPatternBrushSrcFragmentShader = "\n\ + uniform sampler2D brushTexture; \n\ + uniform lowp vec4 patternColor; \n\ + varying highp vec2 patternTexCoords;\n\ + lowp vec4 srcPixel() \n\ + { \n\ + return patternColor * (1.0 - texture2D(brushTexture, patternTexCoords).r); \n\ + }\n"; + + +// Linear Gradient Brush +static const char* const qopenglslPositionWithLinearGradientBrushVertexShader = "\n\ + attribute highp vec2 vertexCoordsArray; \n\ + attribute highp vec3 pmvMatrix1; \n\ + attribute highp vec3 pmvMatrix2; \n\ + attribute highp vec3 pmvMatrix3; \n\ + uniform mediump vec2 halfViewportSize; \n\ + uniform highp vec3 linearData; \n\ + uniform highp mat3 brushTransform; \n\ + varying mediump float index; \n\ + void setPosition() \n\ + { \n\ + highp mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + mediump vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + mediump vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1); \n\ + mediump float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + index = (dot(linearData.xy, hTexCoords.xy) * linearData.z) * invertedHTexCoordsZ; \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithLinearGradientBrushVertexShader + = qopenglslPositionWithLinearGradientBrushVertexShader; + +static const char* const qopenglslLinearGradientBrushSrcFragmentShader = "\n\ + uniform sampler2D brushTexture; \n\ + varying mediump float index; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + mediump vec2 val = vec2(index, 0.5); \n\ + return texture2D(brushTexture, val); \n\ + }\n"; + + +// Conical Gradient Brush +static const char* const qopenglslPositionWithConicalGradientBrushVertexShader = "\n\ + attribute highp vec2 vertexCoordsArray; \n\ + attribute highp vec3 pmvMatrix1; \n\ + attribute highp vec3 pmvMatrix2; \n\ + attribute highp vec3 pmvMatrix3; \n\ + uniform mediump vec2 halfViewportSize; \n\ + uniform highp mat3 brushTransform; \n\ + varying highp vec2 A; \n\ + void setPosition(void) \n\ + { \n\ + highp mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + mediump vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + mediump vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1); \n\ + mediump float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + A = hTexCoords.xy * invertedHTexCoordsZ; \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithConicalGradientBrushVertexShader + = qopenglslPositionWithConicalGradientBrushVertexShader; + +static const char* const qopenglslConicalGradientBrushSrcFragmentShader = "\n\ + #define INVERSE_2PI 0.1591549430918953358 \n\ + uniform sampler2D brushTexture; \n\ + uniform mediump float angle; \n\ + varying highp vec2 A; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + highp float t; \n\ + if (abs(A.y) == abs(A.x)) \n\ + t = (atan(-A.y + 0.002, A.x) + angle) * INVERSE_2PI; \n\ + else \n\ + t = (atan(-A.y, A.x) + angle) * INVERSE_2PI; \n\ + return texture2D(brushTexture, vec2(t - floor(t), 0.5)); \n\ + }\n"; + + +// Radial Gradient Brush +static const char* const qopenglslPositionWithRadialGradientBrushVertexShader = "\n\ + attribute highp vec2 vertexCoordsArray;\n\ + attribute highp vec3 pmvMatrix1; \n\ + attribute highp vec3 pmvMatrix2; \n\ + attribute highp vec3 pmvMatrix3; \n\ + uniform mediump vec2 halfViewportSize; \n\ + uniform highp mat3 brushTransform; \n\ + uniform highp vec2 fmp; \n\ + uniform mediump vec3 bradius; \n\ + varying highp float b; \n\ + varying highp vec2 A; \n\ + void setPosition(void) \n\ + {\n\ + highp mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + mediump vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + mediump vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1); \n\ + mediump float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + A = hTexCoords.xy * invertedHTexCoordsZ; \n\ + b = bradius.x + 2.0 * dot(A, fmp); \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithRadialGradientBrushVertexShader + = qopenglslPositionWithRadialGradientBrushVertexShader; + +static const char* const qopenglslRadialGradientBrushSrcFragmentShader = "\n\ + uniform sampler2D brushTexture; \n\ + uniform highp float fmp2_m_radius2; \n\ + uniform highp float inverse_2_fmp2_m_radius2; \n\ + uniform highp float sqrfr; \n\ + varying highp float b; \n\ + varying highp vec2 A; \n\ + uniform mediump vec3 bradius; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + highp float c = sqrfr-dot(A, A); \n\ + highp float det = b*b - 4.0*fmp2_m_radius2*c; \n\ + lowp vec4 result = vec4(0.0); \n\ + if (det >= 0.0) { \n\ + highp float detSqrt = sqrt(det); \n\ + highp float w = max((-b - detSqrt) * inverse_2_fmp2_m_radius2, (-b + detSqrt) * inverse_2_fmp2_m_radius2); \n\ + if (bradius.y + w * bradius.z >= 0.0) \n\ + result = texture2D(brushTexture, vec2(w, 0.5)); \n\ + } \n\ + return result; \n\ + }\n"; + + +// Texture Brush +static const char* const qopenglslPositionWithTextureBrushVertexShader = "\n\ + attribute highp vec2 vertexCoordsArray; \n\ + attribute highp vec3 pmvMatrix1; \n\ + attribute highp vec3 pmvMatrix2; \n\ + attribute highp vec3 pmvMatrix3; \n\ + uniform mediump vec2 halfViewportSize; \n\ + uniform highp vec2 invertedTextureSize; \n\ + uniform highp mat3 brushTransform; \n\ + varying highp vec2 brushTextureCoords; \n\ + void setPosition(void) \n\ + { \n\ + highp mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + mediump vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + mediump vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1); \n\ + mediump float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + brushTextureCoords.xy = (hTexCoords.xy * invertedTextureSize) * gl_Position.w; \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithTextureBrushVertexShader + = qopenglslPositionWithTextureBrushVertexShader; + +static const char* const qopenglslTextureBrushSrcFragmentShader = "\n\ + varying highp vec2 brushTextureCoords; \n\ + uniform sampler2D brushTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return texture2D(brushTexture, brushTextureCoords); \n\ + }\n"; + +static const char* const qopenglslTextureBrushSrcWithPatternFragmentShader = "\n\ + varying highp vec2 brushTextureCoords; \n\ + uniform lowp vec4 patternColor; \n\ + uniform sampler2D brushTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return patternColor * (1.0 - texture2D(brushTexture, brushTextureCoords).r); \n\ + }\n"; + +// Solid Fill Brush +static const char* const qopenglslSolidBrushSrcFragmentShader = "\n\ + uniform lowp vec4 fragmentColor; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return fragmentColor; \n\ + }\n"; + +static const char* const qopenglslImageSrcFragmentShader = "\n\ + varying highp vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n" + "return texture2D(imageTexture, textureCoords); \n" + "}\n"; + +static const char* const qopenglslCustomSrcFragmentShader = "\n\ + varying highp vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return customShader(imageTexture, textureCoords); \n\ + }\n"; + +static const char* const qopenglslImageSrcWithPatternFragmentShader = "\n\ + varying highp vec2 textureCoords; \n\ + uniform lowp vec4 patternColor; \n\ + uniform sampler2D imageTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return patternColor * (1.0 - texture2D(imageTexture, textureCoords).r); \n\ + }\n"; + +static const char* const qopenglslNonPremultipliedImageSrcFragmentShader = "\n\ + varying highp vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + lowp vec4 sample = texture2D(imageTexture, textureCoords); \n\ + sample.rgb = sample.rgb * sample.a; \n\ + return sample; \n\ + }\n"; + +static const char* const qopenglslGrayscaleImageSrcFragmentShader = "\n\ + varying highp vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return texture2D(imageTexture, textureCoords).rrra; \n\ + }\n"; + +static const char* const qopenglslAlphaImageSrcFragmentShader = "\n\ + varying highp vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return vec4(0, 0, 0, texture2D(imageTexture, textureCoords).r); \n\ + }\n"; + +static const char* const qopenglslShockingPinkSrcFragmentShader = "\n\ + lowp vec4 srcPixel() \n\ + { \n\ + return vec4(0.98, 0.06, 0.75, 1.0); \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_ImageArrays = "\n\ + varying lowp float opacity; \n\ + lowp vec4 srcPixel(); \n\ + void main() \n\ + { \n\ + gl_FragColor = srcPixel() * opacity; \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_MO = "\n\ + uniform lowp float globalOpacity; \n\ + lowp vec4 srcPixel(); \n\ + lowp vec4 applyMask(lowp vec4); \n\ + void main() \n\ + { \n\ + gl_FragColor = applyMask(srcPixel()*globalOpacity); \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_M = "\n\ + lowp vec4 srcPixel(); \n\ + lowp vec4 applyMask(lowp vec4); \n\ + void main() \n\ + { \n\ + gl_FragColor = applyMask(srcPixel()); \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_O = "\n\ + uniform lowp float globalOpacity; \n\ + lowp vec4 srcPixel(); \n\ + void main() \n\ + { \n\ + gl_FragColor = srcPixel()*globalOpacity; \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader = "\n\ + lowp vec4 srcPixel(); \n\ + void main() \n\ + { \n\ + gl_FragColor = srcPixel(); \n\ + }\n"; + +static const char* const qopenglslMaskFragmentShader = "\n\ + varying highp vec2 textureCoords;\n\ + uniform sampler2D maskTexture;\n\ + lowp vec4 applyMask(lowp vec4 src) \n\ + {\n\ + lowp vec4 mask = texture2D(maskTexture, textureCoords); \n\ + return src * mask.a; \n\ + }\n"; + +// For source over with subpixel antialiasing, the final color is calculated per component as follows +// (.a is alpha component, .c is red, green or blue component): +// alpha = src.a * mask.c * opacity +// dest.c = dest.c * (1 - alpha) + src.c * alpha +// +// In the first pass, calculate: dest.c = dest.c * (1 - alpha) with blend funcs: zero, 1 - source color +// In the second pass, calculate: dest.c = dest.c + src.c * alpha with blend funcs: one, one +// +// If source is a solid color (src is constant), only the first pass is needed, with blend funcs: constant, 1 - source color + +// For source composition with subpixel antialiasing, the final color is calculated per component as follows: +// alpha = src.a * mask.c * opacity +// dest.c = dest.c * (1 - mask.c) + src.c * alpha +// + +static const char* const qopenglslRgbMaskFragmentShaderPass1 = "\n\ + varying highp vec2 textureCoords;\n\ + uniform sampler2D maskTexture;\n\ + lowp vec4 applyMask(lowp vec4 src) \n\ + { \n\ + lowp vec4 mask = texture2D(maskTexture, textureCoords); \n\ + return src.a * mask; \n\ + }\n"; + +static const char* const qopenglslRgbMaskFragmentShaderPass2 = "\n\ + varying highp vec2 textureCoords;\n\ + uniform sampler2D maskTexture;\n\ + lowp vec4 applyMask(lowp vec4 src) \n\ + { \n\ + lowp vec4 mask = texture2D(maskTexture, textureCoords); \n\ + return src * mask; \n\ + }\n"; + +static const char* const qopenglslMultiplyCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_multiply) out;\n\ + #endif\n"; + +static const char* const qopenglslScreenCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_screen) out;\n\ + #endif\n"; + +static const char* const qopenglslOverlayCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_overlay) out;\n\ + #endif\n"; + +static const char* const qopenglslDarkenCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_darken) out;\n\ + #endif\n"; + +static const char* const qopenglslLightenCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_lighten) out;\n\ + #endif\n"; + +static const char* const qopenglslColorDodgeCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_colordodge) out;\n\ + #endif\n"; + +static const char* const qopenglslColorBurnCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_colorburn) out;\n\ + #endif\n"; + +static const char* const qopenglslHardLightCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_hardlight) out;\n\ + #endif\n"; + +static const char* const qopenglslSoftLightCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_softlight) out;\n\ + #endif\n"; + +static const char* const qopenglslDifferenceCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_difference) out;\n\ + #endif\n"; + +static const char* const qopenglslExclusionCompositionModeFragmentShader = "\n\ + #ifdef GL_KHR_blend_equation_advanced\n\ + layout(blend_support_exclusion) out;\n\ + #endif\n"; + +/* + Left to implement: + RgbMaskFragmentShader, + RgbMaskWithGammaFragmentShader, +*/ + +/* + OpenGL 3.2+ Core Profile shaders + The following shader snippets are copies of the snippets above + but use the modern GLSL 1.5 keywords. New shaders should make + a snippet for both profiles and add them appropriately in the + shader manager. +*/ +static const char* const qopenglslMainVertexShader_core = + "#version 150 core\n\ + void setPosition(); \n\ + void main(void) \n\ + { \n\ + setPosition(); \n\ + }\n"; + +static const char* const qopenglslMainWithTexCoordsVertexShader_core = + "#version 150 core\n\ + in vec2 textureCoordArray; \n\ + out vec2 textureCoords; \n\ + void setPosition(); \n\ + void main(void) \n\ + { \n\ + setPosition(); \n\ + textureCoords = textureCoordArray; \n\ + }\n"; + +static const char* const qopenglslMainWithTexCoordsAndOpacityVertexShader_core = + "#version 150 core\n\ + in vec2 textureCoordArray; \n\ + in float opacityArray; \n\ + out vec2 textureCoords; \n\ + out float opacity; \n\ + void setPosition(); \n\ + void main(void) \n\ + { \n\ + setPosition(); \n\ + textureCoords = textureCoordArray; \n\ + opacity = opacityArray; \n\ + }\n"; + +// NOTE: We let GL do the perspective correction so texture lookups in the fragment +// shader are also perspective corrected. +static const char* const qopenglslPositionOnlyVertexShader_core = "\n\ + in vec2 vertexCoordsArray; \n\ + in vec3 pmvMatrix1; \n\ + in vec3 pmvMatrix2; \n\ + in vec3 pmvMatrix3; \n\ + void setPosition(void) \n\ + { \n\ + mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position = vec4(transformedPos.xy, 0.0, transformedPos.z); \n\ + }\n"; + +static const char* const qopenglslComplexGeometryPositionOnlyVertexShader_core = "\n\ + in vec2 vertexCoordsArray; \n\ + uniform mat3 matrix; \n\ + void setPosition(void) \n\ + { \n\ + gl_Position = vec4(matrix * vec3(vertexCoordsArray, 1), 1);\n\ + } \n"; + +static const char* const qopenglslUntransformedPositionVertexShader_core = "\n\ + in vec4 vertexCoordsArray; \n\ + void setPosition(void) \n\ + { \n\ + gl_Position = vertexCoordsArray; \n\ + }\n"; + +// Pattern Brush - This assumes the texture size is 8x8 and thus, the inverted size is 0.125 +static const char* const qopenglslPositionWithPatternBrushVertexShader_core = "\n\ + in vec2 vertexCoordsArray; \n\ + in vec3 pmvMatrix1; \n\ + in vec3 pmvMatrix2; \n\ + in vec3 pmvMatrix3; \n\ + out vec2 patternTexCoords; \n\ + uniform vec2 halfViewportSize; \n\ + uniform vec2 invertedTextureSize; \n\ + uniform mat3 brushTransform; \n\ + void setPosition(void) \n\ + { \n\ + mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1.0); \n\ + float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + patternTexCoords.xy = (hTexCoords.xy * 0.125) * invertedHTexCoordsZ; \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithPatternBrushVertexShader_core + = qopenglslPositionWithPatternBrushVertexShader_core; + +static const char* const qopenglslPatternBrushSrcFragmentShader_core = "\n\ + in vec2 patternTexCoords;\n\ + uniform sampler2D brushTexture; \n\ + uniform vec4 patternColor; \n\ + vec4 srcPixel() \n\ + { \n\ + return patternColor * (1.0 - texture(brushTexture, patternTexCoords).r); \n\ + }\n"; + + +// Linear Gradient Brush +static const char* const qopenglslPositionWithLinearGradientBrushVertexShader_core = "\n\ + in vec2 vertexCoordsArray; \n\ + in vec3 pmvMatrix1; \n\ + in vec3 pmvMatrix2; \n\ + in vec3 pmvMatrix3; \n\ + out float index; \n\ + uniform vec2 halfViewportSize; \n\ + uniform vec3 linearData; \n\ + uniform mat3 brushTransform; \n\ + void setPosition() \n\ + { \n\ + mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1); \n\ + float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + index = (dot(linearData.xy, hTexCoords.xy) * linearData.z) * invertedHTexCoordsZ; \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithLinearGradientBrushVertexShader_core + = qopenglslPositionWithLinearGradientBrushVertexShader_core; + +static const char* const qopenglslLinearGradientBrushSrcFragmentShader_core = "\n\ + uniform sampler2D brushTexture; \n\ + in float index; \n\ + vec4 srcPixel() \n\ + { \n\ + vec2 val = vec2(index, 0.5); \n\ + return texture(brushTexture, val); \n\ + }\n"; + + +// Conical Gradient Brush +static const char* const qopenglslPositionWithConicalGradientBrushVertexShader_core = "\n\ + in vec2 vertexCoordsArray; \n\ + in vec3 pmvMatrix1; \n\ + in vec3 pmvMatrix2; \n\ + in vec3 pmvMatrix3; \n\ + out vec2 A; \n\ + uniform vec2 halfViewportSize; \n\ + uniform mat3 brushTransform; \n\ + void setPosition(void) \n\ + { \n\ + mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1); \n\ + float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + A = hTexCoords.xy * invertedHTexCoordsZ; \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithConicalGradientBrushVertexShader_core + = qopenglslPositionWithConicalGradientBrushVertexShader_core; + +static const char* const qopenglslConicalGradientBrushSrcFragmentShader_core = "\n\ + #define INVERSE_2PI 0.1591549430918953358 \n\ + in vec2 A; \n\ + uniform sampler2D brushTexture; \n\ + uniform float angle; \n\ + vec4 srcPixel() \n\ + { \n\ + float t; \n\ + if (abs(A.y) == abs(A.x)) \n\ + t = (atan(-A.y + 0.002, A.x) + angle) * INVERSE_2PI; \n\ + else \n\ + t = (atan(-A.y, A.x) + angle) * INVERSE_2PI; \n\ + return texture(brushTexture, vec2(t - floor(t), 0.5)); \n\ + }\n"; + + +// Radial Gradient Brush +static const char* const qopenglslPositionWithRadialGradientBrushVertexShader_core = "\n\ + in vec2 vertexCoordsArray;\n\ + in vec3 pmvMatrix1; \n\ + in vec3 pmvMatrix2; \n\ + in vec3 pmvMatrix3; \n\ + out float b; \n\ + out vec2 A; \n\ + uniform vec2 halfViewportSize; \n\ + uniform mat3 brushTransform; \n\ + uniform vec2 fmp; \n\ + uniform vec3 bradius; \n\ + void setPosition(void) \n\ + {\n\ + mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1); \n\ + float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + A = hTexCoords.xy * invertedHTexCoordsZ; \n\ + b = bradius.x + 2.0 * dot(A, fmp); \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithRadialGradientBrushVertexShader_core + = qopenglslPositionWithRadialGradientBrushVertexShader_core; + +static const char* const qopenglslRadialGradientBrushSrcFragmentShader_core = "\n\ + in float b; \n\ + in vec2 A; \n\ + uniform sampler2D brushTexture; \n\ + uniform float fmp2_m_radius2; \n\ + uniform float inverse_2_fmp2_m_radius2; \n\ + uniform float sqrfr; \n\ + uniform vec3 bradius; \n\ + \n\ + vec4 srcPixel() \n\ + { \n\ + float c = sqrfr-dot(A, A); \n\ + float det = b*b - 4.0*fmp2_m_radius2*c; \n\ + vec4 result = vec4(0.0); \n\ + if (det >= 0.0) { \n\ + float detSqrt = sqrt(det); \n\ + float w = max((-b - detSqrt) * inverse_2_fmp2_m_radius2, (-b + detSqrt) * inverse_2_fmp2_m_radius2); \n\ + if (bradius.y + w * bradius.z >= 0.0) \n\ + result = texture(brushTexture, vec2(w, 0.5)); \n\ + } \n\ + return result; \n\ + }\n"; + + +// Texture Brush +static const char* const qopenglslPositionWithTextureBrushVertexShader_core = "\n\ + in vec2 vertexCoordsArray; \n\ + in vec3 pmvMatrix1; \n\ + in vec3 pmvMatrix2; \n\ + in vec3 pmvMatrix3; \n\ + out vec2 brushTextureCoords; \n\ + uniform vec2 halfViewportSize; \n\ + uniform vec2 invertedTextureSize; \n\ + uniform mat3 brushTransform; \n\ + \n\ + void setPosition(void) \n\ + { \n\ + mat3 pmvMatrix = mat3(pmvMatrix1, pmvMatrix2, pmvMatrix3); \n\ + vec3 transformedPos = pmvMatrix * vec3(vertexCoordsArray.xy, 1.0); \n\ + gl_Position.xy = transformedPos.xy / transformedPos.z; \n\ + vec2 viewportCoords = (gl_Position.xy + 1.0) * halfViewportSize; \n\ + vec3 hTexCoords = brushTransform * vec3(viewportCoords, 1); \n\ + float invertedHTexCoordsZ = 1.0 / hTexCoords.z; \n\ + gl_Position = vec4(gl_Position.xy * invertedHTexCoordsZ, 0.0, invertedHTexCoordsZ); \n\ + brushTextureCoords.xy = (hTexCoords.xy * invertedTextureSize) * gl_Position.w; \n\ + }\n"; + +static const char* const qopenglslAffinePositionWithTextureBrushVertexShader_core + = qopenglslPositionWithTextureBrushVertexShader_core; + +static const char* const qopenglslTextureBrushSrcFragmentShader_core = "\n\ + in vec2 brushTextureCoords; \n\ + uniform sampler2D brushTexture; \n\ + vec4 srcPixel() \n\ + { \n\ + return texture(brushTexture, brushTextureCoords); \n\ + }\n"; + +static const char* const qopenglslTextureBrushSrcWithPatternFragmentShader_core = "\n\ + in vec2 brushTextureCoords; \n\ + uniform vec4 patternColor; \n\ + uniform sampler2D brushTexture; \n\ + vec4 srcPixel() \n\ + { \n\ + return patternColor * (1.0 - texture(brushTexture, brushTextureCoords).r); \n\ + }\n"; + +// Solid Fill Brush +static const char* const qopenglslSolidBrushSrcFragmentShader_core = "\n\ + uniform vec4 fragmentColor; \n\ + vec4 srcPixel() \n\ + { \n\ + return fragmentColor; \n\ + }\n"; + +static const char* const qopenglslImageSrcFragmentShader_core = "\n\ + in vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + vec4 srcPixel() \n\ + { \n\ + return texture(imageTexture, textureCoords); \n\ + }\n"; + +static const char* const qopenglslCustomSrcFragmentShader_core = "\n\ + in vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + vec4 srcPixel() \n\ + { \n\ + return customShader(imageTexture, textureCoords); \n\ + }\n"; + +static const char* const qopenglslImageSrcWithPatternFragmentShader_core = "\n\ + in vec2 textureCoords; \n\ + uniform vec4 patternColor; \n\ + uniform sampler2D imageTexture; \n\ + vec4 srcPixel() \n\ + { \n\ + return patternColor * (1.0 - texture(imageTexture, textureCoords).r); \n\ + }\n"; + +static const char* const qopenglslNonPremultipliedImageSrcFragmentShader_core = "\n\ + in vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + vec4 srcPixel() \n\ + { \n\ + vec4 sample = texture(imageTexture, textureCoords); \n\ + sample.rgb = sample.rgb * sample.a; \n\ + return sample; \n\ + }\n"; + +static const char* const qopenglslGrayscaleImageSrcFragmentShader_core = "\n\ + in vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + vec4 srcPixel() \n\ + { \n\ + return texture(imageTexture, textureCoords).rrra; \n\ + }\n"; + +static const char* const qopenglslAlphaImageSrcFragmentShader_core = "\n\ + in vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + vec4 srcPixel() \n\ + { \n\ + return vec4(0, 0, 0, texture(imageTexture, textureCoords).r); \n\ + }\n"; + +static const char* const qopenglslShockingPinkSrcFragmentShader_core = "\n\ + vec4 srcPixel() \n\ + { \n\ + return vec4(0.98, 0.06, 0.75, 1.0); \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_ImageArrays_core = + "#version 150 core\n\ + in float opacity; \n\ + out vec4 fragColor; \n\ + vec4 srcPixel(); \n\ + void main() \n\ + { \n\ + fragColor = srcPixel() * opacity; \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_MO_core = + "#version 150 core\n\ + out vec4 fragColor; \n\ + uniform float globalOpacity; \n\ + vec4 srcPixel(); \n\ + vec4 applyMask(vec4); \n\ + void main() \n\ + { \n\ + fragColor = applyMask(srcPixel()*globalOpacity); \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_M_core = + "#version 150 core\n\ + out vec4 fragColor; \n\ + vec4 srcPixel(); \n\ + vec4 applyMask(vec4); \n\ + void main() \n\ + { \n\ + fragColor = applyMask(srcPixel()); \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_O_core = + "#version 150 core\n\ + out vec4 fragColor; \n\ + uniform float globalOpacity; \n\ + vec4 srcPixel(); \n\ + void main() \n\ + { \n\ + fragColor = srcPixel()*globalOpacity; \n\ + }\n"; + +static const char* const qopenglslMainFragmentShader_core = + "#version 150 core\n\ + out vec4 fragColor; \n\ + vec4 srcPixel(); \n\ + void main() \n\ + { \n\ + fragColor = srcPixel(); \n\ + }\n"; + +static const char* const qopenglslMaskFragmentShader_core = "\n\ + in vec2 textureCoords;\n\ + uniform sampler2D maskTexture;\n\ + vec4 applyMask(vec4 src) \n\ + {\n\ + vec4 mask = texture(maskTexture, textureCoords); \n\ + return src * mask.r; \n\ + }\n"; + +// For source over with subpixel antialiasing, the final color is calculated per component as follows +// (.a is alpha component, .c is red, green or blue component): +// alpha = src.a * mask.c * opacity +// dest.c = dest.c * (1 - alpha) + src.c * alpha +// +// In the first pass, calculate: dest.c = dest.c * (1 - alpha) with blend funcs: zero, 1 - source color +// In the second pass, calculate: dest.c = dest.c + src.c * alpha with blend funcs: one, one +// +// If source is a solid color (src is constant), only the first pass is needed, with blend funcs: constant, 1 - source color + +// For source composition with subpixel antialiasing, the final color is calculated per component as follows: +// alpha = src.a * mask.c * opacity +// dest.c = dest.c * (1 - mask.c) + src.c * alpha +// + +static const char* const qopenglslRgbMaskFragmentShaderPass1_core = "\n\ + in vec2 textureCoords;\n\ + uniform sampler2D maskTexture;\n\ + vec4 applyMask(vec4 src) \n\ + { \n\ + vec4 mask = texture(maskTexture, textureCoords); \n\ + return src.a * mask; \n\ + }\n"; + +static const char* const qopenglslRgbMaskFragmentShaderPass2_core = "\n\ + in vec2 textureCoords;\n\ + uniform sampler2D maskTexture;\n\ + vec4 applyMask(vec4 src) \n\ + { \n\ + vec4 mask = texture(maskTexture, textureCoords); \n\ + return src * mask; \n\ + }\n"; + +/* + Left to implement: + RgbMaskFragmentShader_core, + RgbMaskWithGammaFragmentShader_core, +*/ + +QT_END_NAMESPACE + +#endif // GLGC_SHADER_SOURCE_H diff --git a/src/opengl/qopenglgradientcache.cpp b/src/opengl/qopenglgradientcache.cpp new file mode 100644 index 0000000000..7a932a19bb --- /dev/null +++ b/src/opengl/qopenglgradientcache.cpp @@ -0,0 +1,276 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 "qopenglgradientcache_p.h" +#include <private/qdrawhelper_p.h> +#include <private/qopenglcontext_p.h> +#include <private/qrgba64_p.h> +#include <QtCore/qmutex.h> +#include <QtCore/qrandom.h> +#include "qopenglfunctions.h" +#include <private/qopenglextensions_p.h> + +#ifndef GL_RGBA16 +#define GL_RGBA16 0x805B +#endif + +QT_BEGIN_NAMESPACE + +class QOpenGL2GradientCacheWrapper +{ +public: + QOpenGL2GradientCache *cacheForContext(QOpenGLContext *context) { + QMutexLocker lock(&m_mutex); + return m_resource.value<QOpenGL2GradientCache>(context); + } + +private: + QOpenGLMultiGroupSharedResource m_resource; + QMutex m_mutex; +}; + +Q_GLOBAL_STATIC(QOpenGL2GradientCacheWrapper, qt_gradient_caches) + +QOpenGL2GradientCache::QOpenGL2GradientCache(QOpenGLContext *ctx) + : QOpenGLSharedResource(ctx->shareGroup()) +{ +} + +QOpenGL2GradientCache::~QOpenGL2GradientCache() +{ + cache.clear(); +} + +QOpenGL2GradientCache *QOpenGL2GradientCache::cacheForContext(QOpenGLContext *context) +{ + return qt_gradient_caches()->cacheForContext(context); +} + +void QOpenGL2GradientCache::invalidateResource() +{ + QMutexLocker lock(&m_mutex); + cache.clear(); +} + +void QOpenGL2GradientCache::freeResource(QOpenGLContext *) +{ + cleanCache(); +} + +void QOpenGL2GradientCache::cleanCache() +{ + QMutexLocker lock(&m_mutex); + QOpenGLGradientColorTableHash::const_iterator it = cache.constBegin(); + QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); + for (; it != cache.constEnd(); ++it) { + const CacheInfo &cache_info = it.value(); + funcs->glDeleteTextures(1, &cache_info.texId); + } + cache.clear(); +} + +GLuint QOpenGL2GradientCache::getBuffer(const QGradient &gradient, qreal opacity) +{ + quint64 hash_val = 0; + + const QGradientStops stops = gradient.stops(); + for (int i = 0; i < stops.size() && i <= 2; i++) + hash_val += stops[i].second.rgba(); + + const QMutexLocker lock(&m_mutex); + QOpenGLGradientColorTableHash::const_iterator it = cache.constFind(hash_val); + + if (it == cache.constEnd()) + return addCacheElement(hash_val, gradient, opacity); + else { + do { + const CacheInfo &cache_info = it.value(); + if (cache_info.stops == stops && cache_info.opacity == opacity + && cache_info.interpolationMode == gradient.interpolationMode()) + { + return cache_info.texId; + } + ++it; + } while (it != cache.constEnd() && it.key() == hash_val); + // an exact match for these stops and opacity was not found, create new cache + return addCacheElement(hash_val, gradient, opacity); + } +} + + +GLuint QOpenGL2GradientCache::addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity) +{ + QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); + if (cache.size() == maxCacheSize()) { + int elem_to_remove = QRandomGenerator::global()->bounded(maxCacheSize()); + quint64 key = cache.keys()[elem_to_remove]; + + // need to call glDeleteTextures on each removed cache entry: + QOpenGLGradientColorTableHash::const_iterator it = cache.constFind(key); + do { + funcs->glDeleteTextures(1, &it.value().texId); + } while (++it != cache.constEnd() && it.key() == key); + cache.remove(key); // may remove more than 1, but OK + } + + CacheInfo cache_entry(gradient.stops(), opacity, gradient.interpolationMode()); + funcs->glGenTextures(1, &cache_entry.texId); + funcs->glBindTexture(GL_TEXTURE_2D, cache_entry.texId); + if (static_cast<QOpenGLExtensions *>(funcs)->hasOpenGLExtension(QOpenGLExtensions::Sized16Formats)) { + QRgba64 buffer[1024]; + generateGradientColorTable(gradient, buffer, paletteSize(), opacity); + funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, paletteSize(), 1, + 0, GL_RGBA, GL_UNSIGNED_SHORT, buffer); + } else { + uint buffer[1024]; + generateGradientColorTable(gradient, buffer, paletteSize(), opacity); + funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, paletteSize(), 1, + 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); + } + return cache.insert(hash_val, cache_entry).value().texId; +} + + +//TODO: Let GL generate the texture using an FBO +void QOpenGL2GradientCache::generateGradientColorTable(const QGradient& gradient, QRgba64 *colorTable, int size, qreal opacity) const +{ + int pos = 0; + const QGradientStops s = gradient.stops(); + + bool colorInterpolation = (gradient.interpolationMode() == QGradient::ColorInterpolation); + + uint alpha = qRound(opacity * 256); + QRgba64 current_color = combineAlpha256(s[0].second.rgba64(), alpha); + qreal incr = 1.0 / qreal(size); + qreal fpos = 1.5 * incr; + colorTable[pos++] = qPremultiply(current_color); + + while (fpos <= s.first().first) { + colorTable[pos] = colorTable[pos - 1]; + pos++; + fpos += incr; + } + + if (colorInterpolation) + current_color = qPremultiply(current_color); + + const int sLast = s.size() - 1; + for (int i = 0; i < sLast; ++i) { + qreal delta = 1/(s[i+1].first - s[i].first); + QRgba64 next_color = combineAlpha256(s[i + 1].second.rgba64(), alpha); + if (colorInterpolation) + next_color = qPremultiply(next_color); + + while (fpos < s[i+1].first && pos < size) { + int dist = int(256 * ((fpos - s[i].first) * delta)); + int idist = 256 - dist; + if (colorInterpolation) + colorTable[pos] = interpolate256(current_color, idist, next_color, dist); + else + colorTable[pos] = qPremultiply(interpolate256(current_color, idist, next_color, dist)); + ++pos; + fpos += incr; + } + current_color = next_color; + } + + Q_ASSERT(s.size() > 0); + + QRgba64 last_color = qPremultiply(combineAlpha256(s[sLast].second.rgba64(), alpha)); + for (;pos < size; ++pos) + colorTable[pos] = last_color; + + // Make sure the last color stop is represented at the end of the table + colorTable[size-1] = last_color; +} + +void QOpenGL2GradientCache::generateGradientColorTable(const QGradient& gradient, uint *colorTable, int size, qreal opacity) const +{ + int pos = 0; + const QGradientStops s = gradient.stops(); + + bool colorInterpolation = (gradient.interpolationMode() == QGradient::ColorInterpolation); + + uint alpha = qRound(opacity * 256); + // Qt LIES! It returns ARGB (on little-endian AND on big-endian) + uint current_color = ARGB_COMBINE_ALPHA(s[0].second.rgba(), alpha); + qreal incr = 1.0 / qreal(size); + qreal fpos = 1.5 * incr; + colorTable[pos++] = ARGB2RGBA(qPremultiply(current_color)); + + while (fpos <= s.first().first) { + colorTable[pos] = colorTable[pos - 1]; + pos++; + fpos += incr; + } + + if (colorInterpolation) + current_color = qPremultiply(current_color); + + const int sLast = s.size() - 1; + for (int i = 0; i < sLast; ++i) { + qreal delta = 1/(s[i+1].first - s[i].first); + uint next_color = ARGB_COMBINE_ALPHA(s[i + 1].second.rgba(), alpha); + if (colorInterpolation) + next_color = qPremultiply(next_color); + + while (fpos < s[i+1].first && pos < size) { + int dist = int(256 * ((fpos - s[i].first) * delta)); + int idist = 256 - dist; + if (colorInterpolation) + colorTable[pos] = ARGB2RGBA(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist)); + else + colorTable[pos] = ARGB2RGBA(qPremultiply(INTERPOLATE_PIXEL_256(current_color, idist, next_color, dist))); + ++pos; + fpos += incr; + } + current_color = next_color; + } + + Q_ASSERT(s.size() > 0); + + uint last_color = ARGB2RGBA(qPremultiply(ARGB_COMBINE_ALPHA(s[sLast].second.rgba(), alpha))); + for (;pos < size; ++pos) + colorTable[pos] = last_color; + + // Make sure the last color stop is represented at the end of the table + colorTable[size-1] = last_color; +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopenglgradientcache_p.h b/src/opengl/qopenglgradientcache_p.h new file mode 100644 index 0000000000..da070ae233 --- /dev/null +++ b/src/opengl/qopenglgradientcache_p.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 QOPENGLGRADIENTCACHE_P_H +#define QOPENGLGRADIENTCACHE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QMultiHash> +#include <QObject> +#include <private/qopenglcontext_p.h> +#include <QtCore/qmutex.h> +#include <QGradient> +#include <qrgba64.h> + +QT_BEGIN_NAMESPACE + +class QOpenGL2GradientCache : public QOpenGLSharedResource +{ + struct CacheInfo + { + inline CacheInfo(QGradientStops s, qreal op, QGradient::InterpolationMode mode) : + stops(std::move(s)), opacity(op), interpolationMode(mode) {} + + GLuint texId; + QGradientStops stops; + qreal opacity; + QGradient::InterpolationMode interpolationMode; + }; + + typedef QMultiHash<quint64, CacheInfo> QOpenGLGradientColorTableHash; + +public: + static QOpenGL2GradientCache *cacheForContext(QOpenGLContext *context); + + QOpenGL2GradientCache(QOpenGLContext *); + ~QOpenGL2GradientCache(); + + GLuint getBuffer(const QGradient &gradient, qreal opacity); + inline int paletteSize() const { return 1024; } + + void invalidateResource() override; + void freeResource(QOpenGLContext *ctx) override; + +private: + inline int maxCacheSize() const { return 60; } + inline void generateGradientColorTable(const QGradient& gradient, + QRgba64 *colorTable, + int size, qreal opacity) const; + inline void generateGradientColorTable(const QGradient& gradient, + uint *colorTable, + int size, qreal opacity) const; + GLuint addCacheElement(quint64 hash_val, const QGradient &gradient, qreal opacity); + void cleanCache(); + + QOpenGLGradientColorTableHash cache; + QMutex m_mutex; +}; + +QT_END_NAMESPACE + +#endif // QOPENGLGRADIENTCACHE_P_H diff --git a/src/opengl/qopenglpaintdevice.cpp b/src/opengl/qopenglpaintdevice.cpp new file mode 100644 index 0000000000..811425cf69 --- /dev/null +++ b/src/opengl/qopenglpaintdevice.cpp @@ -0,0 +1,372 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 <qopenglpaintdevice.h> +#include <qpaintengine.h> +#include <qthreadstorage.h> + +#include <private/qopenglpaintdevice_p.h> +#include <private/qobject_p.h> +#include <private/qopenglcontext_p.h> +#include <private/qopenglframebufferobject_p.h> +#include <private/qopenglpaintengine_p.h> + +// for qt_defaultDpiX/Y +#include <private/qfont_p.h> + +#include <qopenglfunctions.h> + +QT_BEGIN_NAMESPACE + +/*! + \class QOpenGLPaintDevice + \brief The QOpenGLPaintDevice class enables painting to an OpenGL context using QPainter. + \since 5.0 + \inmodule QtOpenGL + + \ingroup painting-3D + + The QOpenGLPaintDevice uses the \b current QOpenGL context to render + QPainter draw commands. The context is captured upon construction. It + requires support for OpenGL (ES) 2.0 or higher. + + \section1 Performance + + The QOpenGLPaintDevice is almost always hardware accelerated and + has the potential of being much faster than software + rasterization. However, it is more sensitive to state changes, and + therefore requires the drawing commands to be carefully ordered to + achieve optimal performance. + + \section1 Antialiasing and Quality + + Antialiasing in the OpenGL paint engine is done using + multisampling. Most hardware require significantly more memory to + do multisampling and the resulting quality is not on par with the + quality of the software paint engine. The OpenGL paint engine's + strength lies in its performance, not its visual rendering + quality. + + \section1 State Changes + + When painting to a QOpenGLPaintDevice using QPainter, the state of + the current OpenGL context will be altered by the paint engine to + reflect its needs. Applications should not rely upon the OpenGL + state being reset to its original conditions, particularly the + current shader program, OpenGL viewport, texture units, and + drawing modes. + + \section1 Mixing QPainter and OpenGL + + When intermixing QPainter and OpenGL, it is important to notify + QPainter that the OpenGL state may have been cluttered so it can + restore its internal state. This is achieved by calling \l + QPainter::beginNativePainting() before starting the OpenGL + rendering and calling \l QPainter::endNativePainting() after + finishing. + + \sa {OpenGL Window Example} + +*/ + +/*! + Constructs a QOpenGLPaintDevice. + + The QOpenGLPaintDevice is only valid for the current context. + + \sa QOpenGLContext::currentContext() +*/ +QOpenGLPaintDevice::QOpenGLPaintDevice() + : d_ptr(new QOpenGLPaintDevicePrivate(QSize())) +{ +} + +/*! + Constructs a QOpenGLPaintDevice with the given \a size. + + The QOpenGLPaintDevice is only valid for the current context. + + \sa QOpenGLContext::currentContext() +*/ +QOpenGLPaintDevice::QOpenGLPaintDevice(const QSize &size) + : d_ptr(new QOpenGLPaintDevicePrivate(size)) +{ +} + +/*! + Constructs a QOpenGLPaintDevice with the given \a width and \a height. + + The QOpenGLPaintDevice is only valid for the current context. + + \sa QOpenGLContext::currentContext() +*/ +QOpenGLPaintDevice::QOpenGLPaintDevice(int width, int height) + : QOpenGLPaintDevice(QSize(width, height)) +{ +} + +/*! + \internal + */ +QOpenGLPaintDevice::QOpenGLPaintDevice(QOpenGLPaintDevicePrivate &dd) + : d_ptr(&dd) +{ +} + +/*! + Destroys the QOpenGLPaintDevice. +*/ + +QOpenGLPaintDevice::~QOpenGLPaintDevice() +{ + delete d_ptr->engine; +} + +/*! + \fn int QOpenGLPaintDevice::devType() const + \internal + \reimp +*/ + +QOpenGLPaintDevicePrivate::QOpenGLPaintDevicePrivate(const QSize &sz) + : size(sz) + , ctx(QOpenGLContext::currentContext()) + , dpmx(qt_defaultDpiX() * 100. / 2.54) + , dpmy(qt_defaultDpiY() * 100. / 2.54) + , devicePixelRatio(1.0) + , flipped(false) + , engine(nullptr) +{ +} + +QOpenGLPaintDevicePrivate::~QOpenGLPaintDevicePrivate() +{ +} + +class QOpenGLEngineThreadStorage +{ +public: + QPaintEngine *engine() { + QPaintEngine *&localEngine = storage.localData(); + if (!localEngine) + localEngine = new QOpenGL2PaintEngineEx; + return localEngine; + } + +private: + QThreadStorage<QPaintEngine *> storage; +}; + +Q_GLOBAL_STATIC(QOpenGLEngineThreadStorage, qt_opengl_engine) + +/*! + \reimp +*/ + +QPaintEngine *QOpenGLPaintDevice::paintEngine() const +{ + if (d_ptr->engine) + return d_ptr->engine; + + QPaintEngine *engine = qt_opengl_engine()->engine(); + if (engine->isActive() && engine->paintDevice() != this) { + d_ptr->engine = new QOpenGL2PaintEngineEx; + return d_ptr->engine; + } + + return engine; +} + +/*! + Returns the OpenGL context associated with the paint device. +*/ + +QOpenGLContext *QOpenGLPaintDevice::context() const +{ + return d_ptr->ctx; +} + +/*! + Returns the pixel size of the paint device. + + \sa setSize() +*/ + +QSize QOpenGLPaintDevice::size() const +{ + return d_ptr->size; +} + +/*! + Sets the pixel size of the paint device to \a size. + + \sa size() +*/ + +void QOpenGLPaintDevice::setSize(const QSize &size) +{ + d_ptr->size = size; +} + +/*! + Sets the device pixel ratio for the paint device to \a devicePixelRatio. +*/ +void QOpenGLPaintDevice::setDevicePixelRatio(qreal devicePixelRatio) +{ + d_ptr->devicePixelRatio = devicePixelRatio; +} + +/*! + \reimp +*/ + +int QOpenGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const +{ + switch (metric) { + case PdmWidth: + return d_ptr->size.width(); + case PdmHeight: + return d_ptr->size.height(); + case PdmDepth: + return 32; + case PdmWidthMM: + return qRound(d_ptr->size.width() * 1000 / d_ptr->dpmx); + case PdmHeightMM: + return qRound(d_ptr->size.height() * 1000 / d_ptr->dpmy); + case PdmNumColors: + return 0; + case PdmDpiX: + return qRound(d_ptr->dpmx * 0.0254); + case PdmDpiY: + return qRound(d_ptr->dpmy * 0.0254); + case PdmPhysicalDpiX: + return qRound(d_ptr->dpmx * 0.0254); + case PdmPhysicalDpiY: + return qRound(d_ptr->dpmy * 0.0254); + case PdmDevicePixelRatio: + return d_ptr->devicePixelRatio; + case PdmDevicePixelRatioScaled: + return d_ptr->devicePixelRatio * QPaintDevice::devicePixelRatioFScale(); + + default: + qWarning("QOpenGLPaintDevice::metric() - metric %d not known", metric); + return 0; + } +} + +/*! + Returns the number of pixels per meter horizontally. + + \sa setDotsPerMeterX() +*/ + +qreal QOpenGLPaintDevice::dotsPerMeterX() const +{ + return d_ptr->dpmx; +} + +/*! + Returns the number of pixels per meter vertically. + + \sa setDotsPerMeterY() +*/ + +qreal QOpenGLPaintDevice::dotsPerMeterY() const +{ + return d_ptr->dpmy; +} + +/*! + Sets the number of pixels per meter horizontally to \a dpmx. + + \sa dotsPerMeterX() +*/ + +void QOpenGLPaintDevice::setDotsPerMeterX(qreal dpmx) +{ + d_ptr->dpmx = dpmx; +} + +/*! + Sets the number of pixels per meter vertically to \a dpmy. + + \sa dotsPerMeterY() +*/ + +void QOpenGLPaintDevice::setDotsPerMeterY(qreal dpmy) +{ + d_ptr->dpmx = dpmy; +} + +/*! + Sets whether painting should be flipped around the Y-axis or not to \a flipped. + + \sa paintFlipped() +*/ +void QOpenGLPaintDevice::setPaintFlipped(bool flipped) +{ + d_ptr->flipped = flipped; +} + +/*! + Returns \c true if painting is flipped around the Y-axis. + + \sa setPaintFlipped() +*/ + +bool QOpenGLPaintDevice::paintFlipped() const +{ + return d_ptr->flipped; +} + +/*! + This virtual method is provided as a callback to allow re-binding a target + frame buffer object or context when different QOpenGLPaintDevice instances + are issuing draw calls alternately. + + \l{QPainter::beginNativePainting()}{beginNativePainting()} will also trigger + this method. + + The default implementation does nothing. +*/ +void QOpenGLPaintDevice::ensureActiveTarget() +{ +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopenglpaintdevice.h b/src/opengl/qopenglpaintdevice.h new file mode 100644 index 0000000000..f4e1ce4a2e --- /dev/null +++ b/src/opengl/qopenglpaintdevice.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 QOPENGLPAINTDEVICE_H +#define QOPENGLPAINTDEVICE_H + +#include <QtOpenGL/qtopenglglobal.h> + +#ifndef QT_NO_OPENGL + +#include <QtGui/qpaintdevice.h> +#include <QtGui/qopengl.h> +#include <QtGui/qopenglcontext.h> + +QT_BEGIN_NAMESPACE + +class QOpenGLPaintDevicePrivate; + +class Q_OPENGL_EXPORT QOpenGLPaintDevice : public QPaintDevice +{ + Q_DECLARE_PRIVATE(QOpenGLPaintDevice) +public: + QOpenGLPaintDevice(); + explicit QOpenGLPaintDevice(const QSize &size); + QOpenGLPaintDevice(int width, int height); + ~QOpenGLPaintDevice(); + + int devType() const override { return QInternal::OpenGL; } + QPaintEngine *paintEngine() const override; + + QOpenGLContext *context() const; + QSize size() const; + void setSize(const QSize &size); + void setDevicePixelRatio(qreal devicePixelRatio); + + qreal dotsPerMeterX() const; + qreal dotsPerMeterY() const; + + void setDotsPerMeterX(qreal); + void setDotsPerMeterY(qreal); + + void setPaintFlipped(bool flipped); + bool paintFlipped() const; + + virtual void ensureActiveTarget(); + +protected: + QOpenGLPaintDevice(QOpenGLPaintDevicePrivate &dd); + int metric(QPaintDevice::PaintDeviceMetric metric) const override; + + Q_DISABLE_COPY(QOpenGLPaintDevice) + QScopedPointer<QOpenGLPaintDevicePrivate> d_ptr; +}; + +QT_END_NAMESPACE + +#endif // QT_NO_OPENGL + +#endif // QOPENGLPAINTDEVICE_H diff --git a/src/opengl/qopenglpaintdevice_p.h b/src/opengl/qopenglpaintdevice_p.h new file mode 100644 index 0000000000..f4f02e7b57 --- /dev/null +++ b/src/opengl/qopenglpaintdevice_p.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 QOPENGL_PAINTDEVICE_P_H +#define QOPENGL_PAINTDEVICE_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 <qopenglpaintdevice.h> + +QT_BEGIN_NAMESPACE + +class QOpenGLContext; +class QPaintEngine; + +class Q_OPENGL_EXPORT QOpenGLPaintDevicePrivate +{ +public: + QOpenGLPaintDevicePrivate(const QSize &size); + virtual ~QOpenGLPaintDevicePrivate(); + + static QOpenGLPaintDevicePrivate *get(QOpenGLPaintDevice *dev) { return dev->d_func(); } + + virtual void beginPaint() { } + virtual void endPaint() { } + +public: + QSize size; + QOpenGLContext *ctx; + + qreal dpmx; + qreal dpmy; + qreal devicePixelRatio; + + bool flipped; + + QPaintEngine *engine; +}; + +QT_END_NAMESPACE + +#endif // QOPENGL_PAINTDEVICE_P_H diff --git a/src/opengl/qopenglpaintengine.cpp b/src/opengl/qopenglpaintengine.cpp new file mode 100644 index 0000000000..4168067e8f --- /dev/null +++ b/src/opengl/qopenglpaintengine.cpp @@ -0,0 +1,2702 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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$ +** +****************************************************************************/ + +/* + When the active program changes, we need to update it's uniforms. + We could track state for each program and only update stale uniforms + - Could lead to lots of overhead if there's a lot of programs + We could update all the uniforms when the program changes + - Could end up updating lots of uniforms which don't need updating + + Updating uniforms should be cheap, so the overhead of updating up-to-date + uniforms should be minimal. It's also less complex. + + Things which _may_ cause a different program to be used: + - Change in brush/pen style + - Change in painter opacity + - Change in composition mode + + Whenever we set a mode on the shader manager - it needs to tell us if it had + to switch to a different program. + + The shader manager should only switch when we tell it to. E.g. if we set a new + brush style and then switch to transparent painter, we only want it to compile + and use the correct program when we really need it. +*/ + +// #define QT_OPENGL_CACHE_AS_VBOS + +#include <private/qopenglgradientcache_p.h> +#include <private/qopengltexturecache_p.h> +#include "qopenglpaintengine_p.h" +#include "qopenglpaintdevice_p.h" + +#include <string.h> //for memcpy +#include <qmath.h> + +#include <private/qopengl_p.h> +#include <private/qopenglcontext_p.h> +#include <private/qopenglextensions_p.h> +#include <private/qpaintengineex_p.h> +#include <QPaintEngine> +#include <private/qpainter_p.h> +#include <private/qfontengine_p.h> +#include <private/qdatabuffer_p.h> +#include <private/qstatictext_p.h> +#include <private/qtriangulator_p.h> + +#include <private/qopenglengineshadermanager_p.h> +#include <private/qopengl2pexvertexarray_p.h> +#include <private/qopengltextureglyphcache_p.h> + +#include <QDebug> + +#ifndef GL_KHR_blend_equation_advanced +#define GL_KHR_blend_equation_advanced 1 +#define GL_MULTIPLY_KHR 0x9294 +#define GL_SCREEN_KHR 0x9295 +#define GL_OVERLAY_KHR 0x9296 +#define GL_DARKEN_KHR 0x9297 +#define GL_LIGHTEN_KHR 0x9298 +#define GL_COLORDODGE_KHR 0x9299 +#define GL_COLORBURN_KHR 0x929A +#define GL_HARDLIGHT_KHR 0x929B +#define GL_SOFTLIGHT_KHR 0x929C +#define GL_DIFFERENCE_KHR 0x929E +#define GL_EXCLUSION_KHR 0x92A0 +#endif /* GL_KHR_blend_equation_advanced */ + +#ifndef GL_KHR_blend_equation_advanced_coherent +#define GL_KHR_blend_equation_advanced_coherent 1 +#define GL_BLEND_ADVANCED_COHERENT_KHR 0x9285 +#endif /* GL_KHR_blend_equation_advanced_coherent */ + +QT_BEGIN_NAMESPACE + + +Q_OPENGL_EXPORT QImage qt_imageForBrush(int brushStyle, bool invert); + +////////////////////////////////// Private Methods ////////////////////////////////////////// + +QOpenGL2PaintEngineExPrivate::~QOpenGL2PaintEngineExPrivate() +{ + delete shaderManager; + + vertexBuffer.destroy(); + texCoordBuffer.destroy(); + opacityBuffer.destroy(); + indexBuffer.destroy(); + vao.destroy(); + + if (elementIndicesVBOId != 0) { + funcs.glDeleteBuffers(1, &elementIndicesVBOId); + elementIndicesVBOId = 0; + } +} + +inline QColor qt_premultiplyColor(QColor c, GLfloat opacity) +{ + qreal alpha = c.alphaF() * opacity; + c.setAlphaF(alpha); + c.setRedF(c.redF() * alpha); + c.setGreenF(c.greenF() * alpha); + c.setBlueF(c.blueF() * alpha); + return c; +} + + +void QOpenGL2PaintEngineExPrivate::setBrush(const QBrush& brush) +{ + if (qbrush_fast_equals(currentBrush, brush)) + return; + + const Qt::BrushStyle newStyle = qbrush_style(brush); + Q_ASSERT(newStyle != Qt::NoBrush); + + currentBrush = brush; + if (!currentBrushImage.isNull()) + currentBrushImage = QImage(); + brushUniformsDirty = true; // All brushes have at least one uniform + + if (newStyle > Qt::SolidPattern) + brushTextureDirty = true; + + if (currentBrush.style() == Qt::TexturePattern + && qHasPixmapTexture(brush) && brush.texture().isQBitmap()) + { + shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::TextureSrcWithPattern); + } else { + shaderManager->setSrcPixelType(newStyle); + } + shaderManager->optimiseForBrushTransform(currentBrush.transform().type()); +} + + +void QOpenGL2PaintEngineExPrivate::useSimpleShader() +{ + shaderManager->useSimpleProgram(); + + if (matrixDirty) + updateMatrix(); +} + +/* + Single entry-point for activating, binding, and setting properties. + + Allows keeping track of (caching) the latest texture unit and bound + texture in a central place, so that we can skip re-binding unless + needed. + + \note Any code or Qt API that internally activates or binds will + not affect the cache used by this function, which means they will + lead to inconsisent state. QPainter::beginNativePainting() takes + care of resetting the cache, so for user–code this is fine, but + internally in the paint engine care must be taken to not call + functions that may activate or bind under our feet. +*/ +template<typename T> +void QOpenGL2PaintEngineExPrivate::updateTexture(GLenum textureUnit, const T &texture, GLenum wrapMode, GLenum filterMode, TextureUpdateMode updateMode) +{ + static const GLenum target = GL_TEXTURE_2D; + + activateTextureUnit(textureUnit); + + GLuint textureId = bindTexture(texture); + + if (updateMode == UpdateIfNeeded && textureId == lastTextureUsed) + return; + + lastTextureUsed = textureId; + + funcs.glTexParameteri(target, GL_TEXTURE_WRAP_S, wrapMode); + funcs.glTexParameteri(target, GL_TEXTURE_WRAP_T, wrapMode); + + funcs.glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filterMode); + funcs.glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filterMode); +} + +void QOpenGL2PaintEngineExPrivate::activateTextureUnit(GLenum textureUnit) +{ + if (textureUnit != lastTextureUnitUsed) { + funcs.glActiveTexture(GL_TEXTURE0 + textureUnit); + lastTextureUnitUsed = textureUnit; + + // We simplify things by keeping a single cached value of the last + // texture that was bound, instead of one per texture unit. This + // means that switching texture units could potentially mean we + // need a re-bind and corresponding parameter updates. + lastTextureUsed = GLuint(-1); + } +} + +template<> +GLuint QOpenGL2PaintEngineExPrivate::bindTexture(const GLuint &textureId) +{ + if (textureId != lastTextureUsed) + funcs.glBindTexture(GL_TEXTURE_2D, textureId); + + return textureId; +} + +template<> +GLuint QOpenGL2PaintEngineExPrivate::bindTexture(const QImage &image) +{ + return QOpenGLTextureCache::cacheForContext(ctx)->bindTexture(ctx, image); +} + +template<> +GLuint QOpenGL2PaintEngineExPrivate::bindTexture(const QPixmap &pixmap) +{ + return QOpenGLTextureCache::cacheForContext(ctx)->bindTexture(ctx, pixmap); +} + +template<> +GLuint QOpenGL2PaintEngineExPrivate::bindTexture(const QGradient &gradient) +{ + // We apply global opacity in the fragment shaders, so we always pass 1.0 + // for opacity to the cache. + GLuint textureId = QOpenGL2GradientCache::cacheForContext(ctx)->getBuffer(gradient, 1.0); + + // QOpenGL2GradientCache::getBuffer() may bind and generate a new texture if it + // hasn't been cached yet, but will otherwise return an unbound texture id. To + // be sure that the texture is bound, we unfortunately have to bind again, + // which results in the initial generation of the texture doing two binds. + return bindTexture(textureId); +} + +struct ImageWithBindOptions +{ + const QImage ℑ + QOpenGLTextureUploader::BindOptions options; +}; + +template<> +GLuint QOpenGL2PaintEngineExPrivate::bindTexture(const ImageWithBindOptions &imageWithOptions) +{ + return QOpenGLTextureCache::cacheForContext(ctx)->bindTexture(ctx, imageWithOptions.image, imageWithOptions.options); +} + +inline static bool isPowerOfTwo(int x) +{ + // Assumption: x >= 1 + return x == (x & -x); +} + +void QOpenGL2PaintEngineExPrivate::updateBrushTexture() +{ + Q_Q(QOpenGL2PaintEngineEx); +// qDebug("QOpenGL2PaintEngineExPrivate::updateBrushTexture()"); + Qt::BrushStyle style = currentBrush.style(); + + bool smoothPixmapTransform = q->state()->renderHints & QPainter::SmoothPixmapTransform; + GLenum filterMode = smoothPixmapTransform ? GL_LINEAR : GL_NEAREST; + + if ( (style >= Qt::Dense1Pattern) && (style <= Qt::DiagCrossPattern) ) { + // Get the image data for the pattern + QImage textureImage = qt_imageForBrush(style, false); + + updateTexture(QT_BRUSH_TEXTURE_UNIT, textureImage, GL_REPEAT, filterMode, ForceUpdate); + } + else if (style >= Qt::LinearGradientPattern && style <= Qt::ConicalGradientPattern) { + // Gradiant brush: All the gradiants use the same texture + + const QGradient *gradient = currentBrush.gradient(); + + GLenum wrapMode = GL_CLAMP_TO_EDGE; + if (gradient->spread() == QGradient::RepeatSpread || gradient->type() == QGradient::ConicalGradient) + wrapMode = GL_REPEAT; + else if (gradient->spread() == QGradient::ReflectSpread) + wrapMode = GL_MIRRORED_REPEAT; + + updateTexture(QT_BRUSH_TEXTURE_UNIT, *gradient, wrapMode, filterMode, ForceUpdate); + } + else if (style == Qt::TexturePattern) { + currentBrushImage = currentBrush.textureImage(); + + int max_texture_size = ctx->d_func()->maxTextureSize(); + QSize newSize = currentBrushImage.size(); + newSize = newSize.boundedTo(QSize(max_texture_size, max_texture_size)); + if (!QOpenGLContext::currentContext()->functions()->hasOpenGLFeature(QOpenGLFunctions::NPOTTextureRepeat)) { + if (!isPowerOfTwo(newSize.width()) || !isPowerOfTwo(newSize.height())) { + newSize.setHeight(qNextPowerOfTwo(newSize.height() - 1)); + newSize.setWidth(qNextPowerOfTwo(newSize.width() - 1)); + } + } + if (currentBrushImage.size() != newSize) + currentBrushImage = currentBrushImage.scaled(newSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + + GLuint wrapMode = GL_REPEAT; + + updateTexture(QT_BRUSH_TEXTURE_UNIT, currentBrushImage, wrapMode, filterMode, ForceUpdate); + } + brushTextureDirty = false; +} + + +void QOpenGL2PaintEngineExPrivate::updateBrushUniforms() +{ +// qDebug("QOpenGL2PaintEngineExPrivate::updateBrushUniforms()"); + Qt::BrushStyle style = currentBrush.style(); + + if (style == Qt::NoBrush) + return; + + QTransform brushQTransform = currentBrush.transform(); + + if (style == Qt::SolidPattern) { + QColor col = qt_premultiplyColor(currentBrush.color(), (GLfloat)q->state()->opacity); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::FragmentColor), col); + } + else { + // All other brushes have a transform and thus need the translation point: + QPointF translationPoint; + + if (style <= Qt::DiagCrossPattern) { + QColor col = qt_premultiplyColor(currentBrush.color(), (GLfloat)q->state()->opacity); + + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::PatternColor), col); + + QVector2D halfViewportSize(width*0.5, height*0.5); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::HalfViewportSize), halfViewportSize); + } + else if (style == Qt::LinearGradientPattern) { + const QLinearGradient *g = static_cast<const QLinearGradient *>(currentBrush.gradient()); + + QPointF realStart = g->start(); + QPointF realFinal = g->finalStop(); + translationPoint = realStart; + + QPointF l = realFinal - realStart; + + QVector3D linearData( + l.x(), + l.y(), + 1.0f / (l.x() * l.x() + l.y() * l.y()) + ); + + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::LinearData), linearData); + + QVector2D halfViewportSize(width*0.5, height*0.5); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::HalfViewportSize), halfViewportSize); + } + else if (style == Qt::ConicalGradientPattern) { + const QConicalGradient *g = static_cast<const QConicalGradient *>(currentBrush.gradient()); + translationPoint = g->center(); + + GLfloat angle = -qDegreesToRadians(g->angle()); + + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::Angle), angle); + + QVector2D halfViewportSize(width*0.5, height*0.5); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::HalfViewportSize), halfViewportSize); + } + else if (style == Qt::RadialGradientPattern) { + const QRadialGradient *g = static_cast<const QRadialGradient *>(currentBrush.gradient()); + QPointF realCenter = g->center(); + QPointF realFocal = g->focalPoint(); + qreal realRadius = g->centerRadius() - g->focalRadius(); + translationPoint = realFocal; + + QPointF fmp = realCenter - realFocal; + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::Fmp), fmp); + + GLfloat fmp2_m_radius2 = -fmp.x() * fmp.x() - fmp.y() * fmp.y() + realRadius*realRadius; + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::Fmp2MRadius2), fmp2_m_radius2); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::Inverse2Fmp2MRadius2), + GLfloat(1.0 / (2.0*fmp2_m_radius2))); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::SqrFr), + GLfloat(g->focalRadius() * g->focalRadius())); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::BRadius), + GLfloat(2 * (g->centerRadius() - g->focalRadius()) * g->focalRadius()), + g->focalRadius(), + g->centerRadius() - g->focalRadius()); + + QVector2D halfViewportSize(width*0.5, height*0.5); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::HalfViewportSize), halfViewportSize); + } + else if (style == Qt::TexturePattern) { + const QPixmap& texPixmap = currentBrush.texture(); + + if (qHasPixmapTexture(currentBrush) && currentBrush.texture().isQBitmap()) { + QColor col = qt_premultiplyColor(currentBrush.color(), (GLfloat)q->state()->opacity); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::PatternColor), col); + } + + QSizeF invertedTextureSize(1.0 / texPixmap.width(), 1.0 / texPixmap.height()); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::InvertedTextureSize), invertedTextureSize); + + QVector2D halfViewportSize(width*0.5, height*0.5); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::HalfViewportSize), halfViewportSize); + } + else + qWarning("QOpenGL2PaintEngineEx: Unimplemented fill style"); + + const QPointF &brushOrigin = q->state()->brushOrigin; + QTransform matrix = q->state()->matrix; + matrix.translate(brushOrigin.x(), brushOrigin.y()); + + QTransform translate(1, 0, 0, 1, -translationPoint.x(), -translationPoint.y()); + qreal m22 = -1; + qreal dy = height; + if (device->paintFlipped()) { + m22 = 1; + dy = 0; + } + QTransform gl_to_qt(1, 0, 0, m22, 0, dy); + QTransform inv_matrix = gl_to_qt * (brushQTransform * matrix).inverted() * translate; + + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::BrushTransform), inv_matrix); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::BrushTexture), QT_BRUSH_TEXTURE_UNIT); + } + brushUniformsDirty = false; +} + + +// This assumes the shader manager has already setup the correct shader program +void QOpenGL2PaintEngineExPrivate::updateMatrix() +{ +// qDebug("QOpenGL2PaintEngineExPrivate::updateMatrix()"); + + const QTransform& transform = q->state()->matrix; + + // The projection matrix converts from Qt's coordinate system to GL's coordinate system + // * GL's viewport is 2x2, Qt's is width x height + // * GL has +y -> -y going from bottom -> top, Qt is the other way round + // * GL has [0,0] in the center, Qt has it in the top-left + // + // This results in the Projection matrix below, which is multiplied by the painter's + // transformation matrix, as shown below: + // + // Projection Matrix Painter Transform + // ------------------------------------------------ ------------------------ + // | 2.0 / width | 0.0 | -1.0 | | m11 | m21 | dx | + // | 0.0 | -2.0 / height | 1.0 | * | m12 | m22 | dy | + // | 0.0 | 0.0 | 1.0 | | m13 | m23 | m33 | + // ------------------------------------------------ ------------------------ + // + // NOTE: The resultant matrix is also transposed, as GL expects column-major matracies + + const GLfloat wfactor = 2.0f / width; + GLfloat hfactor = -2.0f / height; + + GLfloat dx = transform.dx(); + GLfloat dy = transform.dy(); + + if (device->paintFlipped()) { + hfactor *= -1; + dy -= height; + } + + // Non-integer translates can have strange effects for some rendering operations such as + // anti-aliased text rendering. In such cases, we snap the translate to the pixel grid. + if (snapToPixelGrid && transform.type() == QTransform::TxTranslate) { + // 0.50 needs to rounded down to 0.0 for consistency with raster engine: + dx = std::ceil(dx - 0.5f); + dy = std::ceil(dy - 0.5f); + } + pmvMatrix[0][0] = (wfactor * transform.m11()) - transform.m13(); + pmvMatrix[1][0] = (wfactor * transform.m21()) - transform.m23(); + pmvMatrix[2][0] = (wfactor * dx) - transform.m33(); + pmvMatrix[0][1] = (hfactor * transform.m12()) + transform.m13(); + pmvMatrix[1][1] = (hfactor * transform.m22()) + transform.m23(); + pmvMatrix[2][1] = (hfactor * dy) + transform.m33(); + pmvMatrix[0][2] = transform.m13(); + pmvMatrix[1][2] = transform.m23(); + pmvMatrix[2][2] = transform.m33(); + + // 1/10000 == 0.0001, so we have good enough res to cover curves + // that span the entire widget... + inverseScale = qMax(1 / qMax( qMax(qAbs(transform.m11()), qAbs(transform.m22())), + qMax(qAbs(transform.m12()), qAbs(transform.m21())) ), + qreal(0.0001)); + + matrixDirty = false; + matrixUniformDirty = true; + + // Set the PMV matrix attribute. As we use an attributes rather than uniforms, we only + // need to do this once for every matrix change and persists across all shader programs. + funcs.glVertexAttrib3fv(QT_PMV_MATRIX_1_ATTR, pmvMatrix[0]); + funcs.glVertexAttrib3fv(QT_PMV_MATRIX_2_ATTR, pmvMatrix[1]); + funcs.glVertexAttrib3fv(QT_PMV_MATRIX_3_ATTR, pmvMatrix[2]); + + dasher.setInvScale(inverseScale); + stroker.setInvScale(inverseScale); +} + + +void QOpenGL2PaintEngineExPrivate::updateCompositionMode() +{ + // NOTE: The entire paint engine works on pre-multiplied data - which is why some of these + // composition modes look odd. +// qDebug() << "QOpenGL2PaintEngineExPrivate::updateCompositionMode() - Setting GL composition mode for " << q->state()->composition_mode; + if (ctx->functions()->hasOpenGLFeature(QOpenGLFunctions::BlendEquationAdvanced)) { + if (q->state()->composition_mode <= QPainter::CompositionMode_Plus) { + funcs.glDisable(GL_BLEND_ADVANCED_COHERENT_KHR); + funcs.glBlendEquation(GL_FUNC_ADD); + } else { + funcs.glEnable(GL_BLEND_ADVANCED_COHERENT_KHR); + } + shaderManager->setCompositionMode(q->state()->composition_mode); + } else { + if (q->state()->composition_mode > QPainter::CompositionMode_Plus) { + qWarning("Unsupported composition mode"); + compositionModeDirty = false; + return; + } + } + switch(q->state()->composition_mode) { + case QPainter::CompositionMode_SourceOver: + funcs.glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + break; + case QPainter::CompositionMode_DestinationOver: + funcs.glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE); + break; + case QPainter::CompositionMode_Clear: + funcs.glBlendFunc(GL_ZERO, GL_ZERO); + break; + case QPainter::CompositionMode_Source: + funcs.glBlendFunc(GL_ONE, GL_ZERO); + break; + case QPainter::CompositionMode_Destination: + funcs.glBlendFunc(GL_ZERO, GL_ONE); + break; + case QPainter::CompositionMode_SourceIn: + funcs.glBlendFunc(GL_DST_ALPHA, GL_ZERO); + break; + case QPainter::CompositionMode_DestinationIn: + funcs.glBlendFunc(GL_ZERO, GL_SRC_ALPHA); + break; + case QPainter::CompositionMode_SourceOut: + funcs.glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ZERO); + break; + case QPainter::CompositionMode_DestinationOut: + funcs.glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA); + break; + case QPainter::CompositionMode_SourceAtop: + funcs.glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + break; + case QPainter::CompositionMode_DestinationAtop: + funcs.glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA); + break; + case QPainter::CompositionMode_Xor: + funcs.glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + break; + case QPainter::CompositionMode_Plus: + funcs.glBlendFunc(GL_ONE, GL_ONE); + break; + case QPainter::CompositionMode_Multiply: + funcs.glBlendEquation(GL_MULTIPLY_KHR); + break; + case QPainter::CompositionMode_Screen: + funcs.glBlendEquation(GL_SCREEN_KHR); + break; + case QPainter::CompositionMode_Overlay: + funcs.glBlendEquation(GL_OVERLAY_KHR); + break; + case QPainter::CompositionMode_Darken: + funcs.glBlendEquation(GL_DARKEN_KHR); + break; + case QPainter::CompositionMode_Lighten: + funcs.glBlendEquation(GL_LIGHTEN_KHR); + break; + case QPainter::CompositionMode_ColorDodge: + funcs.glBlendEquation(GL_COLORDODGE_KHR); + break; + case QPainter::CompositionMode_ColorBurn: + funcs.glBlendEquation(GL_COLORBURN_KHR); + break; + case QPainter::CompositionMode_HardLight: + funcs.glBlendEquation(GL_HARDLIGHT_KHR); + break; + case QPainter::CompositionMode_SoftLight: + funcs.glBlendEquation(GL_SOFTLIGHT_KHR); + break; + case QPainter::CompositionMode_Difference: + funcs.glBlendEquation(GL_DIFFERENCE_KHR); + break; + case QPainter::CompositionMode_Exclusion: + funcs.glBlendEquation(GL_EXCLUSION_KHR); + break; + default: + qWarning("Unsupported composition mode"); + break; + } + + compositionModeDirty = false; +} + +static inline void setCoords(GLfloat *coords, const QOpenGLRect &rect) +{ + coords[0] = rect.left; + coords[1] = rect.top; + coords[2] = rect.right; + coords[3] = rect.top; + coords[4] = rect.right; + coords[5] = rect.bottom; + coords[6] = rect.left; + coords[7] = rect.bottom; +} + +void QOpenGL2PaintEngineExPrivate::drawTexture(const QOpenGLRect& dest, const QOpenGLRect& src, const QSize &textureSize, bool opaque, bool pattern) +{ + // Setup for texture drawing + currentBrush = noBrush; + + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + + if (prepareForDraw(opaque)) + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT); + + if (pattern) { + QColor col = qt_premultiplyColor(q->state()->pen.color(), (GLfloat)q->state()->opacity); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::PatternColor), col); + } + + GLfloat dx = 1.0 / textureSize.width(); + GLfloat dy = 1.0 / textureSize.height(); + + QOpenGLRect srcTextureRect(src.left*dx, src.top*dy, src.right*dx, src.bottom*dy); + + setCoords(staticVertexCoordinateArray, dest); + setCoords(staticTextureCoordinateArray, srcTextureRect); + + setVertexAttribArrayEnabled(QT_VERTEX_COORDS_ATTR, true); + setVertexAttribArrayEnabled(QT_TEXTURE_COORDS_ATTR, true); + + uploadData(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray, 8); + uploadData(QT_TEXTURE_COORDS_ATTR, staticTextureCoordinateArray, 8); + + funcs.glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +} + +void QOpenGL2PaintEngineEx::beginNativePainting() +{ + Q_D(QOpenGL2PaintEngineEx); + ensureActive(); + d->transferMode(BrushDrawingMode); + + d->nativePaintingActive = true; + + d->funcs.glUseProgram(0); + + // Disable all the vertex attribute arrays: + for (int i = 0; i < QT_GL_VERTEX_ARRAY_TRACKED_COUNT; ++i) + d->funcs.glDisableVertexAttribArray(i); + +#if !defined(QT_OPENGL_ES_2) && !defined(QT_OPENGL_DYNAMIC) + Q_ASSERT(QOpenGLContext::currentContext()); + const QOpenGLContext *ctx = d->ctx; + const QSurfaceFormat &fmt = d->device->context()->format(); + if (fmt.majorVersion() < 3 || (fmt.majorVersion() == 3 && fmt.minorVersion() < 1) + || (fmt.majorVersion() == 3 && fmt.minorVersion() == 1 && ctx->hasExtension(QByteArrayLiteral("GL_ARB_compatibility"))) + || fmt.profile() == QSurfaceFormat::CompatibilityProfile) + { + // be nice to people who mix OpenGL 1.x code with QPainter commands + // by setting modelview and projection matrices to mirror the GL 1 + // paint engine + const QTransform& mtx = state()->matrix; + + float mv_matrix[4][4] = + { + { float(mtx.m11()), float(mtx.m12()), 0, float(mtx.m13()) }, + { float(mtx.m21()), float(mtx.m22()), 0, float(mtx.m23()) }, + { 0, 0, 1, 0 }, + { float(mtx.dx()), float(mtx.dy()), 0, float(mtx.m33()) } + }; + + const QSize sz = d->device->size(); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, sz.width(), sz.height(), 0, -999999, 999999); + + glMatrixMode(GL_MODELVIEW); + glLoadMatrixf(&mv_matrix[0][0]); + } +#endif // QT_OPENGL_ES_2 + + d->resetGLState(); + + // We don't know what texture units and textures the native painting + // will activate and bind, so we can't assume anything when we return + // from the native painting. + d->lastTextureUnitUsed = QT_UNKNOWN_TEXTURE_UNIT; + d->lastTextureUsed = GLuint(-1); + + d->dirtyStencilRegion = QRect(0, 0, d->width, d->height); + + d->shaderManager->setDirty(); + + d->needsSync = true; +} + +void QOpenGL2PaintEngineExPrivate::resetGLState() +{ + activateTextureUnit(QT_DEFAULT_TEXTURE_UNIT); + + funcs.glDisable(GL_BLEND); + funcs.glDisable(GL_STENCIL_TEST); + funcs.glDisable(GL_DEPTH_TEST); + funcs.glDisable(GL_SCISSOR_TEST); + funcs.glDepthMask(true); + funcs.glDepthFunc(GL_LESS); + funcs.glClearDepthf(1); + funcs.glStencilMask(0xff); + funcs.glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + funcs.glStencilFunc(GL_ALWAYS, 0, 0xff); + setVertexAttribArrayEnabled(QT_TEXTURE_COORDS_ATTR, false); + setVertexAttribArrayEnabled(QT_VERTEX_COORDS_ATTR, false); + setVertexAttribArrayEnabled(QT_OPACITY_ATTR, false); + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + // gl_Color, corresponding to vertex attribute 3, may have been changed + float color[] = { 1.0f, 1.0f, 1.0f, 1.0f }; + funcs.glVertexAttrib4fv(3, color); + } + if (vao.isCreated()) { + vao.release(); + funcs.glBindBuffer(GL_ARRAY_BUFFER, 0); + funcs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } +} + +void QOpenGL2PaintEngineEx::endNativePainting() +{ + Q_D(QOpenGL2PaintEngineEx); + d->needsSync = true; + d->nativePaintingActive = false; +} + +void QOpenGL2PaintEngineEx::invalidateState() +{ + Q_D(QOpenGL2PaintEngineEx); + d->needsSync = true; +} + +bool QOpenGL2PaintEngineEx::isNativePaintingActive() const { + Q_D(const QOpenGL2PaintEngineEx); + return d->nativePaintingActive; +} + +void QOpenGL2PaintEngineExPrivate::transferMode(EngineMode newMode) +{ + if (newMode == mode) + return; + + if (newMode == TextDrawingMode) { + shaderManager->setHasComplexGeometry(true); + } else { + shaderManager->setHasComplexGeometry(false); + } + + if (newMode == ImageDrawingMode) { + uploadData(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray, 8); + uploadData(QT_TEXTURE_COORDS_ATTR, staticTextureCoordinateArray, 8); + } + + if (newMode == ImageArrayDrawingMode || newMode == ImageOpacityArrayDrawingMode) { + uploadData(QT_VERTEX_COORDS_ATTR, (GLfloat*)vertexCoordinateArray.data(), vertexCoordinateArray.vertexCount() * 2); + uploadData(QT_TEXTURE_COORDS_ATTR, (GLfloat*)textureCoordinateArray.data(), textureCoordinateArray.vertexCount() * 2); + + if (newMode == ImageOpacityArrayDrawingMode) + uploadData(QT_OPACITY_ATTR, (GLfloat*)opacityArray.data(), opacityArray.size()); + } + + // This needs to change when we implement high-quality anti-aliasing... + if (newMode != TextDrawingMode) + shaderManager->setMaskType(QOpenGLEngineShaderManager::NoMask); + + mode = newMode; +} + +struct QOpenGL2PEVectorPathCache +{ +#ifdef QT_OPENGL_CACHE_AS_VBOS + GLuint vbo; + GLuint ibo; +#else + float *vertices; + void *indices; +#endif + int vertexCount; + int indexCount; + GLenum primitiveType; + qreal iscale; + QVertexIndexVector::Type indexType; +}; + +void QOpenGL2PaintEngineExPrivate::cleanupVectorPath(QPaintEngineEx *engine, void *data) +{ + QOpenGL2PEVectorPathCache *c = (QOpenGL2PEVectorPathCache *) data; +#ifdef QT_OPENGL_CACHE_AS_VBOS + Q_ASSERT(engine->type() == QPaintEngine::OpenGL2); + static_cast<QOpenGL2PaintEngineEx *>(engine)->d_func()->unusedVBOSToClean << c->vbo; + if (c->ibo) + d->unusedIBOSToClean << c->ibo; +#else + Q_UNUSED(engine); + free(c->vertices); + free(c->indices); +#endif + delete c; +} + +// Assumes everything is configured for the brush you want to use +void QOpenGL2PaintEngineExPrivate::fill(const QVectorPath& path) +{ + transferMode(BrushDrawingMode); + + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + + // Might need to call updateMatrix to re-calculate inverseScale + if (matrixDirty) + updateMatrix(); + + const bool supportsElementIndexUint = funcs.hasOpenGLExtension(QOpenGLExtensions::ElementIndexUint); + + const QPointF* const points = reinterpret_cast<const QPointF*>(path.points()); + + // Check to see if there's any hints + if (path.shape() == QVectorPath::RectangleHint) { + QOpenGLRect rect(points[0].x(), points[0].y(), points[2].x(), points[2].y()); + prepareForDraw(currentBrush.isOpaque()); + composite(rect); + } else if (path.isConvex()) { + + if (path.isCacheable()) { + QVectorPath::CacheEntry *data = path.lookupCacheData(q); + QOpenGL2PEVectorPathCache *cache; + + bool updateCache = false; + + if (data) { + cache = (QOpenGL2PEVectorPathCache *) data->data; + // Check if scale factor is exceeded and regenerate if so... + qreal scaleFactor = cache->iscale / inverseScale; + if (scaleFactor < 0.5 || scaleFactor > 2.0) { +#ifdef QT_OPENGL_CACHE_AS_VBOS + glDeleteBuffers(1, &cache->vbo); + cache->vbo = 0; + Q_ASSERT(cache->ibo == 0); +#else + free(cache->vertices); + Q_ASSERT(cache->indices == nullptr); +#endif + updateCache = true; + } + } else { + cache = new QOpenGL2PEVectorPathCache; + data = const_cast<QVectorPath &>(path).addCacheData(q, cache, cleanupVectorPath); + updateCache = true; + } + + // Flatten the path at the current scale factor and fill it into the cache struct. + if (updateCache) { + vertexCoordinateArray.clear(); + vertexCoordinateArray.addPath(path, inverseScale, false); + int vertexCount = vertexCoordinateArray.vertexCount(); + int floatSizeInBytes = vertexCount * 2 * sizeof(float); + cache->vertexCount = vertexCount; + cache->indexCount = 0; + cache->primitiveType = GL_TRIANGLE_FAN; + cache->iscale = inverseScale; +#ifdef QT_OPENGL_CACHE_AS_VBOS + funcs.glGenBuffers(1, &cache->vbo); + funcs.glBindBuffer(GL_ARRAY_BUFFER, cache->vbo); + funcs.glBufferData(GL_ARRAY_BUFFER, floatSizeInBytes, vertexCoordinateArray.data(), GL_STATIC_DRAW); + cache->ibo = 0; +#else + cache->vertices = (float *) malloc(floatSizeInBytes); + memcpy(cache->vertices, vertexCoordinateArray.data(), floatSizeInBytes); + cache->indices = nullptr; +#endif + } + + prepareForDraw(currentBrush.isOpaque()); +#ifdef QT_OPENGL_CACHE_AS_VBOS + funcs.glBindBuffer(GL_ARRAY_BUFFER, cache->vbo); + uploadData(QT_VERTEX_COORD_ATTR, 0, cache->vertexCount); + setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, 0); +#else + uploadData(QT_VERTEX_COORDS_ATTR, cache->vertices, cache->vertexCount * 2); +#endif + funcs.glDrawArrays(cache->primitiveType, 0, cache->vertexCount); + + } else { + // printf(" - Marking path as cachable...\n"); + // Tag it for later so that if the same path is drawn twice, it is assumed to be static and thus cachable + path.makeCacheable(); + vertexCoordinateArray.clear(); + vertexCoordinateArray.addPath(path, inverseScale, false); + prepareForDraw(currentBrush.isOpaque()); + drawVertexArrays(vertexCoordinateArray, GL_TRIANGLE_FAN); + } + + } else { + bool useCache = path.isCacheable(); + if (useCache) { + QRectF bbox = path.controlPointRect(); + // If the path doesn't fit within these limits, it is possible that the triangulation will fail. + useCache &= (bbox.left() > -0x8000 * inverseScale) + && (bbox.right() < 0x8000 * inverseScale) + && (bbox.top() > -0x8000 * inverseScale) + && (bbox.bottom() < 0x8000 * inverseScale); + } + + if (useCache) { + QVectorPath::CacheEntry *data = path.lookupCacheData(q); + QOpenGL2PEVectorPathCache *cache; + + bool updateCache = false; + + if (data) { + cache = (QOpenGL2PEVectorPathCache *) data->data; + // Check if scale factor is exceeded and regenerate if so... + qreal scaleFactor = cache->iscale / inverseScale; + if (scaleFactor < 0.5 || scaleFactor > 2.0) { +#ifdef QT_OPENGL_CACHE_AS_VBOS + glDeleteBuffers(1, &cache->vbo); + glDeleteBuffers(1, &cache->ibo); +#else + free(cache->vertices); + free(cache->indices); +#endif + updateCache = true; + } + } else { + cache = new QOpenGL2PEVectorPathCache; + data = const_cast<QVectorPath &>(path).addCacheData(q, cache, cleanupVectorPath); + updateCache = true; + } + + // Flatten the path at the current scale factor and fill it into the cache struct. + if (updateCache) { + QTriangleSet polys = qTriangulate(path, QTransform().scale(1 / inverseScale, 1 / inverseScale), 1, supportsElementIndexUint); + cache->vertexCount = polys.vertices.size() / 2; + cache->indexCount = polys.indices.size(); + cache->primitiveType = GL_TRIANGLES; + cache->iscale = inverseScale; + cache->indexType = polys.indices.type(); +#ifdef QT_OPENGL_CACHE_AS_VBOS + funcs.glGenBuffers(1, &cache->vbo); + funcs.glGenBuffers(1, &cache->ibo); + funcs.glBindBuffer(GL_ARRAY_BUFFER, cache->vbo); + funcs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cache->ibo); + + if (polys.indices.type() == QVertexIndexVector::UnsignedInt) + funcs.glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quint32) * polys.indices.size(), polys.indices.data(), GL_STATIC_DRAW); + else + funcs.glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quint16) * polys.indices.size(), polys.indices.data(), GL_STATIC_DRAW); + + QVarLengthArray<float> vertices(polys.vertices.size()); + for (int i = 0; i < polys.vertices.size(); ++i) + vertices[i] = float(inverseScale * polys.vertices.at(i)); + funcs.glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertices.size(), vertices.data(), GL_STATIC_DRAW); +#else + cache->vertices = (float *) malloc(sizeof(float) * polys.vertices.size()); + if (polys.indices.type() == QVertexIndexVector::UnsignedInt) { + cache->indices = (quint32 *) malloc(sizeof(quint32) * polys.indices.size()); + memcpy(cache->indices, polys.indices.data(), sizeof(quint32) * polys.indices.size()); + } else { + cache->indices = (quint16 *) malloc(sizeof(quint16) * polys.indices.size()); + memcpy(cache->indices, polys.indices.data(), sizeof(quint16) * polys.indices.size()); + } + for (int i = 0; i < polys.vertices.size(); ++i) + cache->vertices[i] = float(inverseScale * polys.vertices.at(i)); +#endif + } + + prepareForDraw(currentBrush.isOpaque()); +#ifdef QT_OPENGL_CACHE_AS_VBOS + funcs.glBindBuffer(GL_ARRAY_BUFFER, cache->vbo); + funcs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cache->ibo); + uploadData(QT_VERTEX_COORDS_ATTR, 0, cache->vertexCount); + setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, 0); + if (cache->indexType == QVertexIndexVector::UnsignedInt) + funcs.glDrawElements(cache->primitiveType, cache->indexCount, GL_UNSIGNED_INT, 0); + else + funcs.glDrawElements(cache->primitiveType, cache->indexCount, GL_UNSIGNED_SHORT, 0); + funcs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + funcs.glBindBuffer(GL_ARRAY_BUFFER, 0); +#else + uploadData(QT_VERTEX_COORDS_ATTR, cache->vertices, cache->vertexCount * 2); + const GLenum indexValueType = cache->indexType == QVertexIndexVector::UnsignedInt ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT; + const bool useIndexVbo = uploadIndexData(cache->indices, indexValueType, cache->indexCount); + funcs.glDrawElements(cache->primitiveType, cache->indexCount, indexValueType, useIndexVbo ? nullptr : cache->indices); +#endif + + } else { + // printf(" - Marking path as cachable...\n"); + // Tag it for later so that if the same path is drawn twice, it is assumed to be static and thus cachable + path.makeCacheable(); + + if (device->context()->format().stencilBufferSize() <= 0) { + // If there is no stencil buffer, triangulate the path instead. + + QRectF bbox = path.controlPointRect(); + // If the path doesn't fit within these limits, it is possible that the triangulation will fail. + bool withinLimits = (bbox.left() > -0x8000 * inverseScale) + && (bbox.right() < 0x8000 * inverseScale) + && (bbox.top() > -0x8000 * inverseScale) + && (bbox.bottom() < 0x8000 * inverseScale); + if (withinLimits) { + QTriangleSet polys = qTriangulate(path, QTransform().scale(1 / inverseScale, 1 / inverseScale), 1, supportsElementIndexUint); + + QVarLengthArray<float> vertices(polys.vertices.size()); + for (int i = 0; i < polys.vertices.size(); ++i) + vertices[i] = float(inverseScale * polys.vertices.at(i)); + + prepareForDraw(currentBrush.isOpaque()); + uploadData(QT_VERTEX_COORDS_ATTR, vertices.constData(), vertices.size()); + const GLenum indexValueType = funcs.hasOpenGLExtension(QOpenGLExtensions::ElementIndexUint) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT; + const bool useIndexVbo = uploadIndexData(polys.indices.data(), indexValueType, polys.indices.size()); + funcs.glDrawElements(GL_TRIANGLES, polys.indices.size(), indexValueType, useIndexVbo ? nullptr : polys.indices.data()); + } else { + // We can't handle big, concave painter paths with OpenGL without stencil buffer. + qWarning("Painter path exceeds +/-32767 pixels."); + } + return; + } + + // The path is too complicated & needs the stencil technique + vertexCoordinateArray.clear(); + vertexCoordinateArray.addPath(path, inverseScale, false); + + fillStencilWithVertexArray(vertexCoordinateArray, path.hasWindingFill()); + + funcs.glStencilMask(0xff); + funcs.glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE); + + if (q->state()->clipTestEnabled) { + // Pass when high bit is set, replace stencil value with current clip + funcs.glStencilFunc(GL_NOTEQUAL, q->state()->currentClip, GL_STENCIL_HIGH_BIT); + } else if (path.hasWindingFill()) { + // Pass when any bit is set, replace stencil value with 0 + funcs.glStencilFunc(GL_NOTEQUAL, 0, 0xff); + } else { + // Pass when high bit is set, replace stencil value with 0 + funcs.glStencilFunc(GL_NOTEQUAL, 0, GL_STENCIL_HIGH_BIT); + } + prepareForDraw(currentBrush.isOpaque()); + + // Stencil the brush onto the dest buffer + composite(vertexCoordinateArray.boundingRect()); + funcs.glStencilMask(0); + updateClipScissorTest(); + } + } +} + + +void QOpenGL2PaintEngineExPrivate::fillStencilWithVertexArray(const float *data, + int count, + int *stops, + int stopCount, + const QOpenGLRect &bounds, + StencilFillMode mode) +{ + Q_ASSERT(count || stops); + +// qDebug("QOpenGL2PaintEngineExPrivate::fillStencilWithVertexArray()"); + funcs.glStencilMask(0xff); // Enable stencil writes + + if (dirtyStencilRegion.intersects(currentScissorBounds)) { + const QRegion clearRegion = dirtyStencilRegion.intersected(currentScissorBounds); + funcs.glClearStencil(0); // Clear to zero + for (const QRect &rect : clearRegion) { +#ifndef QT_GL_NO_SCISSOR_TEST + setScissor(rect); +#endif + funcs.glClear(GL_STENCIL_BUFFER_BIT); + } + + dirtyStencilRegion -= currentScissorBounds; + +#ifndef QT_GL_NO_SCISSOR_TEST + updateClipScissorTest(); +#endif + } + + funcs.glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // Disable color writes + useSimpleShader(); + funcs.glEnable(GL_STENCIL_TEST); // For some reason, this has to happen _after_ the simple shader is use()'d + + if (mode == WindingFillMode) { + Q_ASSERT(stops && !count); + if (q->state()->clipTestEnabled) { + // Flatten clip values higher than current clip, and set high bit to match current clip + funcs.glStencilFunc(GL_LEQUAL, GL_STENCIL_HIGH_BIT | q->state()->currentClip, ~GL_STENCIL_HIGH_BIT); + funcs.glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE); + composite(bounds); + + funcs.glStencilFunc(GL_EQUAL, GL_STENCIL_HIGH_BIT, GL_STENCIL_HIGH_BIT); + } else if (!stencilClean) { + // Clear stencil buffer within bounding rect + funcs.glStencilFunc(GL_ALWAYS, 0, 0xff); + funcs.glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); + composite(bounds); + } + + // Inc. for front-facing triangle + funcs.glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_INCR_WRAP, GL_INCR_WRAP); + // Dec. for back-facing "holes" + funcs.glStencilOpSeparate(GL_BACK, GL_KEEP, GL_DECR_WRAP, GL_DECR_WRAP); + funcs.glStencilMask(~GL_STENCIL_HIGH_BIT); + drawVertexArrays(data, stops, stopCount, GL_TRIANGLE_FAN); + + if (q->state()->clipTestEnabled) { + // Clear high bit of stencil outside of path + funcs.glStencilFunc(GL_EQUAL, q->state()->currentClip, ~GL_STENCIL_HIGH_BIT); + funcs.glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE); + funcs.glStencilMask(GL_STENCIL_HIGH_BIT); + composite(bounds); + } + } else if (mode == OddEvenFillMode) { + funcs.glStencilMask(GL_STENCIL_HIGH_BIT); + funcs.glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); // Simply invert the stencil bit + drawVertexArrays(data, stops, stopCount, GL_TRIANGLE_FAN); + + } else { // TriStripStrokeFillMode + Q_ASSERT(count && !stops); // tristrips generated directly, so no vertexArray or stops + funcs.glStencilMask(GL_STENCIL_HIGH_BIT); +#if 0 + funcs.glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT); // Simply invert the stencil bit + setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, data); + funcs.glDrawArrays(GL_TRIANGLE_STRIP, 0, count); +#else + + funcs.glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); + if (q->state()->clipTestEnabled) { + funcs.glStencilFunc(GL_LEQUAL, q->state()->currentClip | GL_STENCIL_HIGH_BIT, + ~GL_STENCIL_HIGH_BIT); + } else { + funcs.glStencilFunc(GL_ALWAYS, GL_STENCIL_HIGH_BIT, 0xff); + } + + uploadData(QT_VERTEX_COORDS_ATTR, data, count * 2); + funcs.glDrawArrays(GL_TRIANGLE_STRIP, 0, count); +#endif + } + + // Enable color writes & disable stencil writes + funcs.glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); +} + +/* + If the maximum value in the stencil buffer is GL_STENCIL_HIGH_BIT - 1, + restore the stencil buffer to a pristine state. The current clip region + is set to 1, and the rest to 0. +*/ +void QOpenGL2PaintEngineExPrivate::resetClipIfNeeded() +{ + if (maxClip != (GL_STENCIL_HIGH_BIT - 1)) + return; + + Q_Q(QOpenGL2PaintEngineEx); + + useSimpleShader(); + funcs.glEnable(GL_STENCIL_TEST); + funcs.glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); + + QRectF bounds = q->state()->matrix.inverted().mapRect(QRectF(0, 0, width, height)); + QOpenGLRect rect(bounds.left(), bounds.top(), bounds.right(), bounds.bottom()); + + // Set high bit on clip region + funcs.glStencilFunc(GL_LEQUAL, q->state()->currentClip, 0xff); + funcs.glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT); + funcs.glStencilMask(GL_STENCIL_HIGH_BIT); + composite(rect); + + // Reset clipping to 1 and everything else to zero + funcs.glStencilFunc(GL_NOTEQUAL, 0x01, GL_STENCIL_HIGH_BIT); + funcs.glStencilOp(GL_ZERO, GL_REPLACE, GL_REPLACE); + funcs.glStencilMask(0xff); + composite(rect); + + q->state()->currentClip = 1; + q->state()->canRestoreClip = false; + + maxClip = 1; + + funcs.glStencilMask(0x0); + funcs.glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); +} + +bool QOpenGL2PaintEngineExPrivate::prepareForCachedGlyphDraw(const QFontEngineGlyphCache &cache) +{ + Q_Q(QOpenGL2PaintEngineEx); + + Q_ASSERT(cache.transform().type() <= QTransform::TxScale); + + QTransform &transform = q->state()->matrix; + transform.scale(1.0 / cache.transform().m11(), 1.0 / cache.transform().m22()); + bool ret = prepareForDraw(false); + transform.scale(cache.transform().m11(), cache.transform().m22()); + + return ret; +} + +bool QOpenGL2PaintEngineExPrivate::prepareForDraw(bool srcPixelsAreOpaque) +{ + if (brushTextureDirty && (mode == TextDrawingMode || mode == BrushDrawingMode)) + updateBrushTexture(); + + if (compositionModeDirty) + updateCompositionMode(); + + if (matrixDirty) + updateMatrix(); + + const bool stateHasOpacity = q->state()->opacity < 0.99f; + if (q->state()->composition_mode == QPainter::CompositionMode_Source + || (q->state()->composition_mode == QPainter::CompositionMode_SourceOver + && srcPixelsAreOpaque && !stateHasOpacity)) + { + funcs.glDisable(GL_BLEND); + } else { + funcs.glEnable(GL_BLEND); + } + + QOpenGLEngineShaderManager::OpacityMode opacityMode; + if (mode == ImageOpacityArrayDrawingMode) { + opacityMode = QOpenGLEngineShaderManager::AttributeOpacity; + } else { + opacityMode = stateHasOpacity ? QOpenGLEngineShaderManager::UniformOpacity + : QOpenGLEngineShaderManager::NoOpacity; + if (stateHasOpacity && (mode != ImageDrawingMode && mode != ImageArrayDrawingMode)) { + // Using a brush + bool brushIsPattern = (currentBrush.style() >= Qt::Dense1Pattern) && + (currentBrush.style() <= Qt::DiagCrossPattern); + + if ((currentBrush.style() == Qt::SolidPattern) || brushIsPattern) + opacityMode = QOpenGLEngineShaderManager::NoOpacity; // Global opacity handled by srcPixel shader + } + } + shaderManager->setOpacityMode(opacityMode); + + bool changed = shaderManager->useCorrectShaderProg(); + // If the shader program needs changing, we change it and mark all uniforms as dirty + if (changed) { + // The shader program has changed so mark all uniforms as dirty: + brushUniformsDirty = true; + opacityUniformDirty = true; + matrixUniformDirty = true; + } + + if (brushUniformsDirty && (mode == TextDrawingMode || mode == BrushDrawingMode)) + updateBrushUniforms(); + + if (opacityMode == QOpenGLEngineShaderManager::UniformOpacity && opacityUniformDirty) { + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::GlobalOpacity), (GLfloat)q->state()->opacity); + opacityUniformDirty = false; + } + + if (matrixUniformDirty && shaderManager->hasComplexGeometry()) { + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::Matrix), + pmvMatrix); + matrixUniformDirty = false; + } + + return changed; +} + +void QOpenGL2PaintEngineExPrivate::composite(const QOpenGLRect& boundingRect) +{ + setCoords(staticVertexCoordinateArray, boundingRect); + + uploadData(QT_VERTEX_COORDS_ATTR, staticVertexCoordinateArray, 8); + funcs.glDrawArrays(GL_TRIANGLE_FAN, 0, 4); +} + +// Draws the vertex array as a set of <vertexArrayStops.size()> triangle fans. +void QOpenGL2PaintEngineExPrivate::drawVertexArrays(const float *data, int *stops, int stopCount, + GLenum primitive) +{ + // Now setup the pointer to the vertex array: + uploadData(QT_VERTEX_COORDS_ATTR, data, stops[stopCount-1] * 2); + + int previousStop = 0; + for (int i=0; i<stopCount; ++i) { + int stop = stops[i]; + + funcs.glDrawArrays(primitive, previousStop, stop - previousStop); + previousStop = stop; + } +} + +/////////////////////////////////// Public Methods ////////////////////////////////////////// + +QOpenGL2PaintEngineEx::QOpenGL2PaintEngineEx() + : QPaintEngineEx(*(new QOpenGL2PaintEngineExPrivate(this))) +{ + gccaps &= ~QPaintEngine::RasterOpModes; +} + +QOpenGL2PaintEngineEx::~QOpenGL2PaintEngineEx() +{ +} + +void QOpenGL2PaintEngineEx::fill(const QVectorPath &path, const QBrush &brush) +{ + Q_D(QOpenGL2PaintEngineEx); + + if (qbrush_style(brush) == Qt::NoBrush) + return; + ensureActive(); + d->setBrush(brush); + d->fill(path); +} + +Q_GUI_EXPORT extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp + + +void QOpenGL2PaintEngineEx::stroke(const QVectorPath &path, const QPen &pen) +{ + Q_D(QOpenGL2PaintEngineEx); + + const QBrush &penBrush = qpen_brush(pen); + if (qpen_style(pen) == Qt::NoPen || qbrush_style(penBrush) == Qt::NoBrush) + return; + + QOpenGL2PaintEngineState *s = state(); + if (qt_pen_is_cosmetic(pen, state()->renderHints) && !qt_scaleForTransform(s->transform(), nullptr)) { + // QTriangulatingStroker class is not meant to support cosmetically sheared strokes. + QPaintEngineEx::stroke(path, pen); + return; + } + + ensureActive(); + d->setBrush(penBrush); + d->stroke(path, pen); +} + +void QOpenGL2PaintEngineExPrivate::stroke(const QVectorPath &path, const QPen &pen) +{ + const QOpenGL2PaintEngineState *s = q->state(); + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + + const Qt::PenStyle penStyle = qpen_style(pen); + const QBrush &penBrush = qpen_brush(pen); + const bool opaque = penBrush.isOpaque() && s->opacity > 0.99; + + transferMode(BrushDrawingMode); + + // updateMatrix() is responsible for setting the inverse scale on + // the strokers, so we need to call it here and not wait for + // prepareForDraw() down below. + updateMatrix(); + + QRectF clip = q->state()->matrix.inverted().mapRect(q->state()->clipEnabled + ? q->state()->rectangleClip + : QRectF(0, 0, width, height)); + + if (penStyle == Qt::SolidLine) { + stroker.process(path, pen, clip, s->renderHints); + + } else { // Some sort of dash + dasher.process(path, pen, clip, s->renderHints); + + QVectorPath dashStroke(dasher.points(), + dasher.elementCount(), + dasher.elementTypes()); + stroker.process(dashStroke, pen, clip, s->renderHints); + } + + if (!stroker.vertexCount()) + return; + + if (opaque) { + prepareForDraw(opaque); + + uploadData(QT_VERTEX_COORDS_ATTR, stroker.vertices(), stroker.vertexCount()); + funcs.glDrawArrays(GL_TRIANGLE_STRIP, 0, stroker.vertexCount() / 2); + } else { + qreal width = qpen_widthf(pen) / 2; + if (width == 0) + width = 0.5; + qreal extra = pen.joinStyle() == Qt::MiterJoin + ? qMax(pen.miterLimit() * width, width) + : width; + + if (qt_pen_is_cosmetic(pen, q->state()->renderHints)) + extra = extra * inverseScale; + + QRectF bounds = path.controlPointRect().adjusted(-extra, -extra, extra, extra); + + fillStencilWithVertexArray(stroker.vertices(), stroker.vertexCount() / 2, + nullptr, 0, bounds, QOpenGL2PaintEngineExPrivate::TriStripStrokeFillMode); + + funcs.glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE); + + // Pass when any bit is set, replace stencil value with 0 + funcs.glStencilFunc(GL_NOTEQUAL, 0, GL_STENCIL_HIGH_BIT); + prepareForDraw(false); + + // Stencil the brush onto the dest buffer + composite(bounds); + + funcs.glStencilMask(0); + + updateClipScissorTest(); + } +} + +void QOpenGL2PaintEngineEx::penChanged() { } +void QOpenGL2PaintEngineEx::brushChanged() { } +void QOpenGL2PaintEngineEx::brushOriginChanged() { } + +void QOpenGL2PaintEngineEx::opacityChanged() +{ +// qDebug("QOpenGL2PaintEngineEx::opacityChanged()"); + Q_D(QOpenGL2PaintEngineEx); + state()->opacityChanged = true; + + Q_ASSERT(d->shaderManager); + d->brushUniformsDirty = true; + d->opacityUniformDirty = true; +} + +void QOpenGL2PaintEngineEx::compositionModeChanged() +{ +// qDebug("QOpenGL2PaintEngineEx::compositionModeChanged()"); + Q_D(QOpenGL2PaintEngineEx); + state()->compositionModeChanged = true; + d->compositionModeDirty = true; +} + +void QOpenGL2PaintEngineEx::renderHintsChanged() +{ + state()->renderHintsChanged = true; + +#ifndef QT_OPENGL_ES_2 + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGL2PaintEngineEx); +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + if ((state()->renderHints & QPainter::Antialiasing) +#if QT_DEPRECATED_SINCE(5, 14) + || (state()->renderHints & QPainter::HighQualityAntialiasing) +#endif + ) + d->funcs.glEnable(GL_MULTISAMPLE); + else + d->funcs.glDisable(GL_MULTISAMPLE); +QT_WARNING_POP + } +#endif // QT_OPENGL_ES_2 + + Q_D(QOpenGL2PaintEngineEx); + + // This is a somewhat sneaky way of conceptually making the next call to + // updateTexture() use FoceUpdate for the TextureUpdateMode. We need this + // as new render hints may require updating the filter mode. + d->lastTextureUsed = GLuint(-1); + + d->brushTextureDirty = true; +// qDebug("QOpenGL2PaintEngineEx::renderHintsChanged() not implemented!"); +} + +void QOpenGL2PaintEngineEx::transformChanged() +{ + Q_D(QOpenGL2PaintEngineEx); + d->matrixDirty = true; + state()->matrixChanged = true; +} + + +static const QRectF scaleRect(const QRectF &r, qreal sx, qreal sy) +{ + return QRectF(r.x() * sx, r.y() * sy, r.width() * sx, r.height() * sy); +} + +void QOpenGL2PaintEngineEx::drawPixmap(const QRectF& dest, const QPixmap & pixmap, const QRectF & src) +{ + Q_D(QOpenGL2PaintEngineEx); + QOpenGLContext *ctx = d->ctx; + + // Draw pixmaps that are really images as images since drawImage has + // better handling of non-default image formats. + if (pixmap.paintEngine()->type() == QPaintEngine::Raster && !pixmap.isQBitmap()) + return drawImage(dest, pixmap.toImage(), src); + + int max_texture_size = ctx->d_func()->maxTextureSize(); + if (pixmap.width() > max_texture_size || pixmap.height() > max_texture_size) { + QPixmap scaled = pixmap.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio); + + const qreal sx = scaled.width() / qreal(pixmap.width()); + const qreal sy = scaled.height() / qreal(pixmap.height()); + + drawPixmap(dest, scaled, scaleRect(src, sx, sy)); + return; + } + + ensureActive(); + d->transferMode(ImageDrawingMode); + + GLenum filterMode = state()->renderHints & QPainter::SmoothPixmapTransform ? GL_LINEAR : GL_NEAREST; + d->updateTexture(QT_IMAGE_TEXTURE_UNIT, pixmap, GL_CLAMP_TO_EDGE, filterMode); + + bool isBitmap = pixmap.isQBitmap(); + bool isOpaque = !isBitmap && !pixmap.hasAlpha(); + + d->shaderManager->setSrcPixelType(isBitmap ? QOpenGLEngineShaderManager::PatternSrc : QOpenGLEngineShaderManager::ImageSrc); + + QOpenGLRect srcRect(src.left(), src.top(), src.right(), src.bottom()); + d->drawTexture(dest, srcRect, pixmap.size(), isOpaque, isBitmap); +} + +void QOpenGL2PaintEngineEx::drawImage(const QRectF& dest, const QImage& image, const QRectF& src, + Qt::ImageConversionFlags) +{ + Q_D(QOpenGL2PaintEngineEx); + QOpenGLContext *ctx = d->ctx; + + int max_texture_size = ctx->d_func()->maxTextureSize(); + if (image.width() > max_texture_size || image.height() > max_texture_size) { + QImage scaled = image.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio); + + const qreal sx = scaled.width() / qreal(image.width()); + const qreal sy = scaled.height() / qreal(image.height()); + + drawImage(dest, scaled, scaleRect(src, sx, sy)); + return; + } + + ensureActive(); + d->transferMode(ImageDrawingMode); + + QOpenGLTextureUploader::BindOptions bindOption = QOpenGLTextureUploader::PremultipliedAlphaBindOption; + // Use specialized bind for formats we have specialized shaders for. + switch (image.format()) { + case QImage::Format_RGBA8888: + case QImage::Format_ARGB32: + case QImage::Format_RGBA64: + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::NonPremultipliedImageSrc); + bindOption = { }; + break; + case QImage::Format_Alpha8: + if (ctx->functions()->hasOpenGLFeature(QOpenGLFunctions::TextureRGFormats)) { + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::AlphaImageSrc); + bindOption = QOpenGLTextureUploader::UseRedForAlphaAndLuminanceBindOption; + } else + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc); + break; + case QImage::Format_Grayscale8: + case QImage::Format_Grayscale16: + if (ctx->functions()->hasOpenGLFeature(QOpenGLFunctions::TextureRGFormats)) { + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::GrayscaleImageSrc); + bindOption = QOpenGLTextureUploader::UseRedForAlphaAndLuminanceBindOption; + } else + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc); + break; + default: + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc); + break; + } + + ImageWithBindOptions imageWithOptions = { image, bindOption }; + GLenum filterMode = state()->renderHints & QPainter::SmoothPixmapTransform ? GL_LINEAR : GL_NEAREST; + d->updateTexture(QT_IMAGE_TEXTURE_UNIT, imageWithOptions, GL_CLAMP_TO_EDGE, filterMode); + + d->drawTexture(dest, src, image.size(), !image.hasAlphaChannel()); +} + +void QOpenGL2PaintEngineEx::drawStaticTextItem(QStaticTextItem *textItem) +{ + Q_D(QOpenGL2PaintEngineEx); + + ensureActive(); + + QPainterState *s = state(); + + QFontEngine *fontEngine = textItem->fontEngine(); + if (shouldDrawCachedGlyphs(fontEngine, s->matrix)) { + QFontEngine::GlyphFormat glyphFormat = fontEngine->glyphFormat != QFontEngine::Format_None + ? fontEngine->glyphFormat : d->glyphCacheFormat; + if (glyphFormat == QFontEngine::Format_A32) { + if (d->device->context()->format().alphaBufferSize() > 0 || s->matrix.type() > QTransform::TxTranslate + || (s->composition_mode != QPainter::CompositionMode_Source + && s->composition_mode != QPainter::CompositionMode_SourceOver)) + { + glyphFormat = QFontEngine::Format_A8; + } + } + + d->drawCachedGlyphs(glyphFormat, textItem); + } else { + QPaintEngineEx::drawStaticTextItem(textItem); + } +} + +bool QOpenGL2PaintEngineEx::drawTexture(const QRectF &dest, GLuint textureId, const QSize &size, const QRectF &src) +{ + Q_D(QOpenGL2PaintEngineEx); + if (!d->shaderManager) + return false; + + ensureActive(); + d->transferMode(ImageDrawingMode); + + GLenum filterMode = state()->renderHints & QPainter::SmoothPixmapTransform ? GL_LINEAR : GL_NEAREST; + d->updateTexture(QT_IMAGE_TEXTURE_UNIT, textureId, GL_CLAMP_TO_EDGE, filterMode); + + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc); + + QOpenGLRect srcRect(src.left(), src.bottom(), src.right(), src.top()); + d->drawTexture(dest, srcRect, size, false); + + return true; +} + +void QOpenGL2PaintEngineEx::drawTextItem(const QPointF &p, const QTextItem &textItem) +{ + Q_D(QOpenGL2PaintEngineEx); + + ensureActive(); + QOpenGL2PaintEngineState *s = state(); + + const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem); + + QTransform::TransformationType txtype = s->matrix.type(); + + QFontEngine::GlyphFormat glyphFormat = ti.fontEngine->glyphFormat != QFontEngine::Format_None + ? ti.fontEngine->glyphFormat : d->glyphCacheFormat; + + if (glyphFormat == QFontEngine::Format_A32) { + if (d->device->context()->format().alphaBufferSize() > 0 || txtype > QTransform::TxTranslate + || (state()->composition_mode != QPainter::CompositionMode_Source + && state()->composition_mode != QPainter::CompositionMode_SourceOver)) + { + glyphFormat = QFontEngine::Format_A8; + } + } + + if (shouldDrawCachedGlyphs(ti.fontEngine, s->matrix)) { + QVarLengthArray<QFixedPoint> positions; + QVarLengthArray<glyph_t> glyphs; + QTransform matrix = QTransform::fromTranslate(p.x(), p.y()); + ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions); + + { + QStaticTextItem staticTextItem; + staticTextItem.setFontEngine(ti.fontEngine); + staticTextItem.glyphs = glyphs.data(); + staticTextItem.numGlyphs = glyphs.size(); + staticTextItem.glyphPositions = positions.data(); + + d->drawCachedGlyphs(glyphFormat, &staticTextItem); + } + return; + } + + QPaintEngineEx::drawTextItem(p, ti); +} + +namespace { + + class QOpenGLStaticTextUserData: public QStaticTextUserData + { + public: + QOpenGLStaticTextUserData() + : QStaticTextUserData(OpenGLUserData), cacheSize(0, 0), cacheSerialNumber(0) + { + } + + ~QOpenGLStaticTextUserData() + { + } + + QSize cacheSize; + QOpenGL2PEXVertexArray vertexCoordinateArray; + QOpenGL2PEXVertexArray textureCoordinateArray; + QFontEngine::GlyphFormat glyphFormat; + int cacheSerialNumber; + }; + +} + + +// #define QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO + +bool QOpenGL2PaintEngineEx::shouldDrawCachedGlyphs(QFontEngine *fontEngine, const QTransform &t) const +{ + // The paint engine does not support projected cached glyph drawing + if (t.type() == QTransform::TxProject) + return false; + + // The font engine might not support filling the glyph cache + // with the given transform applied, in which case we need to + // fall back to the QPainterPath code-path. + if (!fontEngine->supportsTransformation(t)) { + // Except that drawing paths is slow, so for scales between + // 0.5 and 2.0 we leave the glyph cache untransformed and deal + // with the transform ourselves when painting, resulting in + // drawing 1x cached glyphs with a smooth-scale. + float det = t.determinant(); + if (det >= 0.25f && det <= 4.f) { + // Assuming the baseclass still agrees + return QPaintEngineEx::shouldDrawCachedGlyphs(fontEngine, t); + } + + return false; // Fall back to path-drawing + } + + return QPaintEngineEx::shouldDrawCachedGlyphs(fontEngine, t); +} + +void QOpenGL2PaintEngineExPrivate::drawCachedGlyphs(QFontEngine::GlyphFormat glyphFormat, + QStaticTextItem *staticTextItem) +{ + Q_Q(QOpenGL2PaintEngineEx); + + QOpenGL2PaintEngineState *s = q->state(); + + void *cacheKey = ctx; // use context, not the shareGroup() -> the GL glyph cache uses FBOs which may not be shareable + bool recreateVertexArrays = false; + + QTransform glyphCacheTransform; + QFontEngine *fe = staticTextItem->fontEngine(); + if (fe->supportsTransformation(s->matrix)) { + // The font-engine supports rendering glyphs with the current transform, so we + // build a glyph-cache with the scale pre-applied, so that the cache contains + // glyphs with the appropriate resolution in the case of retina displays. + glyphCacheTransform = s->matrix.type() < QTransform::TxRotate ? + QTransform::fromScale(qAbs(s->matrix.m11()), qAbs(s->matrix.m22())) : + QTransform::fromScale( + QVector2D(s->matrix.m11(), s->matrix.m12()).length(), + QVector2D(s->matrix.m21(), s->matrix.m22()).length()); + } + + QOpenGLTextureGlyphCache *cache = + (QOpenGLTextureGlyphCache *) fe->glyphCache(cacheKey, glyphFormat, glyphCacheTransform); + if (!cache || cache->glyphFormat() != glyphFormat || cache->contextGroup() == nullptr) { + cache = new QOpenGLTextureGlyphCache(glyphFormat, glyphCacheTransform); + fe->setGlyphCache(cacheKey, cache); + recreateVertexArrays = true; + } + + if (staticTextItem->userDataNeedsUpdate) { + recreateVertexArrays = true; + } else if (staticTextItem->userData() == nullptr) { + recreateVertexArrays = true; + } else if (staticTextItem->userData()->type != QStaticTextUserData::OpenGLUserData) { + recreateVertexArrays = true; + } else { + QOpenGLStaticTextUserData *userData = static_cast<QOpenGLStaticTextUserData *>(staticTextItem->userData()); + if (userData->glyphFormat != glyphFormat) { + recreateVertexArrays = true; + } else if (userData->cacheSerialNumber != cache->serialNumber()) { + recreateVertexArrays = true; + } + } + + // We only need to update the cache with new glyphs if we are actually going to recreate the vertex arrays. + // If the cache size has changed, we do need to regenerate the vertices, but we don't need to repopulate the + // cache so this text is performed before we test if the cache size has changed. + if (recreateVertexArrays) { + cache->setPaintEnginePrivate(this); + if (!cache->populate(fe, staticTextItem->numGlyphs, + staticTextItem->glyphs, staticTextItem->glyphPositions)) { + // No space for glyphs in cache. We need to reset it and try again. + cache->clear(); + cache->populate(fe, staticTextItem->numGlyphs, + staticTextItem->glyphs, staticTextItem->glyphPositions); + } + + if (cache->hasPendingGlyphs()) { + // Filling in the glyphs binds and sets parameters, so we need to + // ensure that the glyph cache doesn't mess with whatever unit + // is currently active. Note that the glyph cache internally + // uses the image texture unit for blitting to the cache, while + // we switch between image and mask units when drawing. + static const GLenum glypchCacheTextureUnit = QT_IMAGE_TEXTURE_UNIT; + activateTextureUnit(glypchCacheTextureUnit); + + cache->fillInPendingGlyphs(); + + // We assume the cache can be trusted on which texture was bound + lastTextureUsed = cache->texture(); + + // But since the brush and image texture units are possibly shared + // we may have to re-bind brush textures after filling in the cache. + brushTextureDirty = (QT_BRUSH_TEXTURE_UNIT == glypchCacheTextureUnit); + } + cache->setPaintEnginePrivate(nullptr); + } + + if (cache->width() == 0 || cache->height() == 0) + return; + + if (glyphFormat == QFontEngine::Format_ARGB) + transferMode(ImageArrayDrawingMode); + else + transferMode(TextDrawingMode); + + int margin = fe->glyphMargin(glyphFormat); + + GLfloat dx = 1.0 / cache->width(); + GLfloat dy = 1.0 / cache->height(); + + // Use global arrays by default + QOpenGL2PEXVertexArray *vertexCoordinates = &vertexCoordinateArray; + QOpenGL2PEXVertexArray *textureCoordinates = &textureCoordinateArray; + + if (staticTextItem->useBackendOptimizations) { + QOpenGLStaticTextUserData *userData = nullptr; + + if (staticTextItem->userData() == nullptr + || staticTextItem->userData()->type != QStaticTextUserData::OpenGLUserData) { + + userData = new QOpenGLStaticTextUserData(); + staticTextItem->setUserData(userData); + + } else { + userData = static_cast<QOpenGLStaticTextUserData*>(staticTextItem->userData()); + } + + userData->glyphFormat = glyphFormat; + userData->cacheSerialNumber = cache->serialNumber(); + + // Use cache if backend optimizations is turned on + vertexCoordinates = &userData->vertexCoordinateArray; + textureCoordinates = &userData->textureCoordinateArray; + + QSize size(cache->width(), cache->height()); + if (userData->cacheSize != size) { + recreateVertexArrays = true; + userData->cacheSize = size; + } + } + + if (recreateVertexArrays) { + vertexCoordinates->clear(); + textureCoordinates->clear(); + + bool supportsSubPixelPositions = fe->supportsSubPixelPositions(); + for (int i=0; i<staticTextItem->numGlyphs; ++i) { + QFixed subPixelPosition; + if (supportsSubPixelPositions) + subPixelPosition = fe->subPixelPositionForX(staticTextItem->glyphPositions[i].x); + + QTextureGlyphCache::GlyphAndSubPixelPosition glyph(staticTextItem->glyphs[i], subPixelPosition); + + const QTextureGlyphCache::Coord &c = cache->coords[glyph]; + if (c.isNull()) + continue; + + int x = qFloor(staticTextItem->glyphPositions[i].x.toReal() * cache->transform().m11()) + c.baseLineX - margin; + int y = qRound(staticTextItem->glyphPositions[i].y.toReal() * cache->transform().m22()) - c.baseLineY - margin; + + vertexCoordinates->addQuad(QRectF(x, y, c.w, c.h)); + textureCoordinates->addQuad(QRectF(c.x*dx, c.y*dy, c.w * dx, c.h * dy)); + } + + staticTextItem->userDataNeedsUpdate = false; + } + + int numGlyphs = vertexCoordinates->vertexCount() / 4; + if (numGlyphs == 0) + return; + + if (elementIndices.size() < numGlyphs*6) { + Q_ASSERT(elementIndices.size() % 6 == 0); + int j = elementIndices.size() / 6 * 4; + while (j < numGlyphs*4) { + elementIndices.append(j + 0); + elementIndices.append(j + 0); + elementIndices.append(j + 1); + elementIndices.append(j + 2); + elementIndices.append(j + 3); + elementIndices.append(j + 3); + + j += 4; + } + +#if defined(QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO) + if (elementIndicesVBOId == 0) + funcs.glGenBuffers(1, &elementIndicesVBOId); + + funcs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementIndicesVBOId); + funcs.glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementIndices.size() * sizeof(GLushort), + elementIndices.constData(), GL_STATIC_DRAW); +#endif + } else { +#if defined(QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO) + funcs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementIndicesVBOId); +#endif + } + + if (glyphFormat != QFontEngine::Format_ARGB || recreateVertexArrays) { + uploadData(QT_VERTEX_COORDS_ATTR, (GLfloat*)vertexCoordinates->data(), vertexCoordinates->vertexCount() * 2); + uploadData(QT_TEXTURE_COORDS_ATTR, (GLfloat*)textureCoordinates->data(), textureCoordinates->vertexCount() * 2); + } + + if (!snapToPixelGrid) { + snapToPixelGrid = true; + matrixDirty = true; + } + + QBrush pensBrush = q->state()->pen.brush(); + setBrush(pensBrush); + + if (glyphFormat == QFontEngine::Format_A32) { + + // Subpixel antialiasing without gamma correction + + QPainter::CompositionMode compMode = q->state()->composition_mode; + Q_ASSERT(compMode == QPainter::CompositionMode_Source + || compMode == QPainter::CompositionMode_SourceOver); + + shaderManager->setMaskType(QOpenGLEngineShaderManager::SubPixelMaskPass1); + + if (pensBrush.style() == Qt::SolidPattern) { + // Solid patterns can get away with only one pass. + QColor c = pensBrush.color(); + qreal oldOpacity = q->state()->opacity; + if (compMode == QPainter::CompositionMode_Source) { + c = qt_premultiplyColor(c, q->state()->opacity); + q->state()->opacity = 1; + opacityUniformDirty = true; + } + + compositionModeDirty = false; // I can handle this myself, thank you very much + prepareForCachedGlyphDraw(*cache); + + // prepareForCachedGlyphDraw() have set the opacity on the current shader, so the opacity state can now be reset. + if (compMode == QPainter::CompositionMode_Source) { + q->state()->opacity = oldOpacity; + opacityUniformDirty = true; + } + + funcs.glEnable(GL_BLEND); + funcs.glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR); + funcs.glBlendColor(c.redF(), c.greenF(), c.blueF(), c.alphaF()); + } else { + // Other brush styles need two passes. + + qreal oldOpacity = q->state()->opacity; + if (compMode == QPainter::CompositionMode_Source) { + q->state()->opacity = 1; + opacityUniformDirty = true; + pensBrush = Qt::white; + setBrush(pensBrush); + } + + compositionModeDirty = false; // I can handle this myself, thank you very much + prepareForCachedGlyphDraw(*cache); + funcs.glEnable(GL_BLEND); + funcs.glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR); + + updateTexture(QT_MASK_TEXTURE_UNIT, cache->texture(), GL_REPEAT, GL_NEAREST, ForceUpdate); + +#if defined(QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO) + funcs.glDrawElements(GL_TRIANGLE_STRIP, 6 * numGlyphs, GL_UNSIGNED_SHORT, 0); +#else + const bool useIndexVbo = uploadIndexData(elementIndices.data(), GL_UNSIGNED_SHORT, 6 * numGlyphs); + funcs.glDrawElements(GL_TRIANGLE_STRIP, 6 * numGlyphs, GL_UNSIGNED_SHORT, useIndexVbo ? nullptr : elementIndices.data()); +#endif + + shaderManager->setMaskType(QOpenGLEngineShaderManager::SubPixelMaskPass2); + + if (compMode == QPainter::CompositionMode_Source) { + q->state()->opacity = oldOpacity; + opacityUniformDirty = true; + pensBrush = q->state()->pen.brush(); + setBrush(pensBrush); + } + + compositionModeDirty = false; + prepareForCachedGlyphDraw(*cache); + funcs.glEnable(GL_BLEND); + funcs.glBlendFunc(GL_ONE, GL_ONE); + } + compositionModeDirty = true; + } else if (glyphFormat == QFontEngine::Format_ARGB) { + currentBrush = noBrush; + shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc); + if (prepareForCachedGlyphDraw(*cache)) + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT); + } else { + // Grayscale/mono glyphs + + shaderManager->setMaskType(QOpenGLEngineShaderManager::PixelMask); + prepareForCachedGlyphDraw(*cache); + } + + GLenum textureUnit = QT_MASK_TEXTURE_UNIT; + if (glyphFormat == QFontEngine::Format_ARGB) + textureUnit = QT_IMAGE_TEXTURE_UNIT; + + QOpenGLTextureGlyphCache::FilterMode filterMode = (s->matrix.type() > QTransform::TxTranslate) ? + QOpenGLTextureGlyphCache::Linear : QOpenGLTextureGlyphCache::Nearest; + + GLenum glFilterMode = filterMode == QOpenGLTextureGlyphCache::Linear ? GL_LINEAR : GL_NEAREST; + + TextureUpdateMode updateMode = UpdateIfNeeded; + if (cache->filterMode() != filterMode) { + updateMode = ForceUpdate; + cache->setFilterMode(filterMode); + } + + updateTexture(textureUnit, cache->texture(), GL_REPEAT, glFilterMode, updateMode); + +#if defined(QT_OPENGL_DRAWCACHEDGLYPHS_INDEX_ARRAY_VBO) + funcs.glDrawElements(GL_TRIANGLE_STRIP, 6 * numGlyphs, GL_UNSIGNED_SHORT, 0); + funcs.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +#else + const bool useIndexVbo = uploadIndexData(elementIndices.data(), GL_UNSIGNED_SHORT, 6 * numGlyphs); + funcs.glDrawElements(GL_TRIANGLE_STRIP, 6 * numGlyphs, GL_UNSIGNED_SHORT, useIndexVbo ? nullptr : elementIndices.data()); +#endif +} + +void QOpenGL2PaintEngineEx::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints) +{ + Q_D(QOpenGL2PaintEngineEx); + // Use fallback for extended composition modes. + if (state()->composition_mode > QPainter::CompositionMode_Plus) { + QPaintEngineEx::drawPixmapFragments(fragments, fragmentCount, pixmap, hints); + return; + } + + ensureActive(); + int max_texture_size = d->ctx->d_func()->maxTextureSize(); + if (pixmap.width() > max_texture_size || pixmap.height() > max_texture_size) { + QPixmap scaled = pixmap.scaled(max_texture_size, max_texture_size, Qt::KeepAspectRatio); + d->drawPixmapFragments(fragments, fragmentCount, scaled, hints); + } else { + d->drawPixmapFragments(fragments, fragmentCount, pixmap, hints); + } +} + + +void QOpenGL2PaintEngineExPrivate::drawPixmapFragments(const QPainter::PixmapFragment *fragments, + int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints) +{ + GLfloat dx = 1.0f / pixmap.size().width(); + GLfloat dy = 1.0f / pixmap.size().height(); + + vertexCoordinateArray.clear(); + textureCoordinateArray.clear(); + opacityArray.reset(); + + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + + bool allOpaque = true; + + for (int i = 0; i < fragmentCount; ++i) { + qreal s = 0; + qreal c = 1; + if (fragments[i].rotation != 0) { + s = qFastSin(qDegreesToRadians(fragments[i].rotation)); + c = qFastCos(qDegreesToRadians(fragments[i].rotation)); + } + + qreal right = 0.5 * fragments[i].scaleX * fragments[i].width; + qreal bottom = 0.5 * fragments[i].scaleY * fragments[i].height; + QOpenGLPoint bottomRight(right * c - bottom * s, right * s + bottom * c); + QOpenGLPoint bottomLeft(-right * c - bottom * s, -right * s + bottom * c); + + vertexCoordinateArray.addVertex(bottomRight.x + fragments[i].x, bottomRight.y + fragments[i].y); + vertexCoordinateArray.addVertex(-bottomLeft.x + fragments[i].x, -bottomLeft.y + fragments[i].y); + vertexCoordinateArray.addVertex(-bottomRight.x + fragments[i].x, -bottomRight.y + fragments[i].y); + vertexCoordinateArray.addVertex(-bottomRight.x + fragments[i].x, -bottomRight.y + fragments[i].y); + vertexCoordinateArray.addVertex(bottomLeft.x + fragments[i].x, bottomLeft.y + fragments[i].y); + vertexCoordinateArray.addVertex(bottomRight.x + fragments[i].x, bottomRight.y + fragments[i].y); + + QOpenGLRect src(fragments[i].sourceLeft * dx, fragments[i].sourceTop * dy, + (fragments[i].sourceLeft + fragments[i].width) * dx, + (fragments[i].sourceTop + fragments[i].height) * dy); + + textureCoordinateArray.addVertex(src.right, src.bottom); + textureCoordinateArray.addVertex(src.right, src.top); + textureCoordinateArray.addVertex(src.left, src.top); + textureCoordinateArray.addVertex(src.left, src.top); + textureCoordinateArray.addVertex(src.left, src.bottom); + textureCoordinateArray.addVertex(src.right, src.bottom); + + qreal opacity = fragments[i].opacity * q->state()->opacity; + opacityArray << opacity << opacity << opacity << opacity << opacity << opacity; + allOpaque &= (opacity >= 0.99f); + } + + transferMode(ImageOpacityArrayDrawingMode); + + GLenum filterMode = q->state()->renderHints & QPainter::SmoothPixmapTransform ? GL_LINEAR : GL_NEAREST; + updateTexture(QT_IMAGE_TEXTURE_UNIT, pixmap, GL_CLAMP_TO_EDGE, filterMode); + + bool isBitmap = pixmap.isQBitmap(); + bool isOpaque = !isBitmap && (!pixmap.hasAlpha() || (hints & QPainter::OpaqueHint)) && allOpaque; + + // Setup for texture drawing + currentBrush = noBrush; + shaderManager->setSrcPixelType(isBitmap ? QOpenGLEngineShaderManager::PatternSrc + : QOpenGLEngineShaderManager::ImageSrc); + if (prepareForDraw(isOpaque)) + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::ImageTexture), QT_IMAGE_TEXTURE_UNIT); + + if (isBitmap) { + QColor col = qt_premultiplyColor(q->state()->pen.color(), (GLfloat)q->state()->opacity); + shaderManager->currentProgram()->setUniformValue(location(QOpenGLEngineShaderManager::PatternColor), col); + } + + funcs.glDrawArrays(GL_TRIANGLES, 0, 6 * fragmentCount); +} + +bool QOpenGL2PaintEngineEx::begin(QPaintDevice *pdev) +{ + Q_D(QOpenGL2PaintEngineEx); + + Q_ASSERT(pdev->devType() == QInternal::OpenGL); + d->device = static_cast<QOpenGLPaintDevice*>(pdev); + + if (!d->device) + return false; + + d->device->ensureActiveTarget(); + + if (d->device->context() != QOpenGLContext::currentContext() || !d->device->context()) { + qWarning("QPainter::begin(): QOpenGLPaintDevice's context needs to be current"); + return false; + } + + if (d->ctx != QOpenGLContext::currentContext() + || (d->ctx && QOpenGLContext::currentContext() && d->ctx->format() != QOpenGLContext::currentContext()->format())) { + d->vertexBuffer.destroy(); + d->texCoordBuffer.destroy(); + d->opacityBuffer.destroy(); + d->indexBuffer.destroy(); + d->vao.destroy(); + } + + d->ctx = QOpenGLContext::currentContext(); + d->ctx->d_func()->active_engine = this; + + QOpenGLPaintDevicePrivate::get(d->device)->beginPaint(); + + d->funcs.initializeOpenGLFunctions(); + + // Generate a new Vertex Array Object if we don't have one already. We can + // only hit the VAO-based path when using a core profile context. This is + // because while non-core contexts can support VAOs via extensions, legacy + // components like the QtOpenGL module do not know about VAOs. There are + // still tests for QGL-QOpenGL paint engine interoperability, so keep the + // status quo for now, and avoid introducing a VAO in non-core contexts. + const bool needsVAO = d->ctx->format().profile() == QSurfaceFormat::CoreProfile + && d->ctx->format().version() >= qMakePair(3, 2); + if (needsVAO && !d->vao.isCreated()) { + bool created = d->vao.create(); + + // If we managed to create it then we have a profile that supports VAOs + if (created) { + d->vao.bind(); + + // Generate a new Vertex Buffer Object if we don't have one already + if (!d->vertexBuffer.isCreated()) { + d->vertexBuffer.create(); + // Set its usage to StreamDraw, we will use this buffer only a few times before refilling it + d->vertexBuffer.setUsagePattern(QOpenGLBuffer::StreamDraw); + } + if (!d->texCoordBuffer.isCreated()) { + d->texCoordBuffer.create(); + d->texCoordBuffer.setUsagePattern(QOpenGLBuffer::StreamDraw); + } + if (!d->opacityBuffer.isCreated()) { + d->opacityBuffer.create(); + d->opacityBuffer.setUsagePattern(QOpenGLBuffer::StreamDraw); + } + if (!d->indexBuffer.isCreated()) { + d->indexBuffer.create(); + d->indexBuffer.setUsagePattern(QOpenGLBuffer::StreamDraw); + } + } + } + + for (int i = 0; i < QT_GL_VERTEX_ARRAY_TRACKED_COUNT; ++i) + d->vertexAttributeArraysEnabledState[i] = false; + + const QSize sz = d->device->size(); + d->width = sz.width(); + d->height = sz.height(); + d->mode = BrushDrawingMode; + d->brushTextureDirty = true; + d->brushUniformsDirty = true; + d->matrixUniformDirty = true; + d->matrixDirty = true; + d->compositionModeDirty = true; + d->opacityUniformDirty = true; + d->needsSync = true; + d->useSystemClip = !systemClip().isEmpty(); + d->currentBrush = QBrush(); + + d->dirtyStencilRegion = QRect(0, 0, d->width, d->height); + d->stencilClean = true; + + d->shaderManager = new QOpenGLEngineShaderManager(d->ctx); + + d->funcs.glDisable(GL_STENCIL_TEST); + d->funcs.glDisable(GL_DEPTH_TEST); + d->funcs.glDisable(GL_SCISSOR_TEST); + + d->glyphCacheFormat = QFontEngine::Format_A8; + +#ifndef QT_OPENGL_ES_2 + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + d->funcs.glDisable(GL_MULTISAMPLE); + d->glyphCacheFormat = QFontEngine::Format_A32; + d->multisamplingAlwaysEnabled = false; + } else +#endif // QT_OPENGL_ES_2 + { + // OpenGL ES can't switch MSAA off, so if the gl paint device is + // multisampled, it's always multisampled. + d->multisamplingAlwaysEnabled = d->device->context()->format().samples() > 1; + } + + return true; +} + +bool QOpenGL2PaintEngineEx::end() +{ + Q_D(QOpenGL2PaintEngineEx); + + QOpenGLPaintDevicePrivate::get(d->device)->endPaint(); + + QOpenGLContext *ctx = d->ctx; + d->funcs.glUseProgram(0); + d->transferMode(BrushDrawingMode); + + ctx->d_func()->active_engine = nullptr; + + d->resetGLState(); + + delete d->shaderManager; + d->shaderManager = nullptr; + d->currentBrush = QBrush(); + +#ifdef QT_OPENGL_CACHE_AS_VBOS + if (!d->unusedVBOSToClean.isEmpty()) { + glDeleteBuffers(d->unusedVBOSToClean.size(), d->unusedVBOSToClean.constData()); + d->unusedVBOSToClean.clear(); + } + if (!d->unusedIBOSToClean.isEmpty()) { + glDeleteBuffers(d->unusedIBOSToClean.size(), d->unusedIBOSToClean.constData()); + d->unusedIBOSToClean.clear(); + } +#endif + + return false; +} + +void QOpenGL2PaintEngineEx::ensureActive() +{ + Q_D(QOpenGL2PaintEngineEx); + QOpenGLContext *ctx = d->ctx; + + if (d->vao.isCreated()) + d->vao.bind(); + + if (isActive() && ctx->d_func()->active_engine != this) { + ctx->d_func()->active_engine = this; + d->needsSync = true; + } + + if (d->needsSync) { + d->device->ensureActiveTarget(); + + d->transferMode(BrushDrawingMode); + d->funcs.glViewport(0, 0, d->width, d->height); + d->needsSync = false; + d->shaderManager->setDirty(); + d->syncGlState(); + for (int i = 0; i < 3; ++i) + d->vertexAttribPointers[i] = (GLfloat*)-1; // Assume the pointers are clobbered + setState(state()); + } +} + +void QOpenGL2PaintEngineExPrivate::updateClipScissorTest() +{ + Q_Q(QOpenGL2PaintEngineEx); + if (q->state()->clipTestEnabled) { + funcs.glEnable(GL_STENCIL_TEST); + funcs.glStencilFunc(GL_LEQUAL, q->state()->currentClip, ~GL_STENCIL_HIGH_BIT); + } else { + funcs.glDisable(GL_STENCIL_TEST); + funcs.glStencilFunc(GL_ALWAYS, 0, 0xff); + } + +#ifdef QT_GL_NO_SCISSOR_TEST + currentScissorBounds = QRect(0, 0, width, height); +#else + QRect bounds = q->state()->rectangleClip; + if (!q->state()->clipEnabled) { + if (useSystemClip) + bounds = systemClip.boundingRect(); + else + bounds = QRect(0, 0, width, height); + } else { + if (useSystemClip) + bounds = bounds.intersected(systemClip.boundingRect()); + else + bounds = bounds.intersected(QRect(0, 0, width, height)); + } + + currentScissorBounds = bounds; + + if (bounds == QRect(0, 0, width, height)) { + funcs.glDisable(GL_SCISSOR_TEST); + } else { + funcs.glEnable(GL_SCISSOR_TEST); + setScissor(bounds); + } +#endif +} + +void QOpenGL2PaintEngineExPrivate::setScissor(const QRect &rect) +{ + const int left = rect.left(); + const int width = rect.width(); + int bottom = height - (rect.top() + rect.height()); + if (device->paintFlipped()) { + bottom = rect.top(); + } + const int height = rect.height(); + + funcs.glScissor(left, bottom, width, height); +} + +void QOpenGL2PaintEngineEx::clipEnabledChanged() +{ + Q_D(QOpenGL2PaintEngineEx); + + state()->clipChanged = true; + + if (painter()->hasClipping()) + d->regenerateClip(); + else + d->systemStateChanged(); +} + +void QOpenGL2PaintEngineExPrivate::clearClip(uint value) +{ + dirtyStencilRegion -= currentScissorBounds; + + funcs.glStencilMask(0xff); + funcs.glClearStencil(value); + funcs.glClear(GL_STENCIL_BUFFER_BIT); + funcs.glStencilMask(0x0); + + q->state()->needsClipBufferClear = false; +} + +void QOpenGL2PaintEngineExPrivate::writeClip(const QVectorPath &path, uint value) +{ + transferMode(BrushDrawingMode); + + if (snapToPixelGrid) { + snapToPixelGrid = false; + matrixDirty = true; + } + + if (matrixDirty) + updateMatrix(); + + stencilClean = false; + + const bool singlePass = !path.hasWindingFill() + && (((q->state()->currentClip == maxClip - 1) && q->state()->clipTestEnabled) + || q->state()->needsClipBufferClear); + const uint referenceClipValue = q->state()->needsClipBufferClear ? 1 : q->state()->currentClip; + + if (q->state()->needsClipBufferClear) + clearClip(1); + + if (path.isEmpty()) { + funcs.glEnable(GL_STENCIL_TEST); + funcs.glStencilFunc(GL_LEQUAL, value, ~GL_STENCIL_HIGH_BIT); + return; + } + + if (q->state()->clipTestEnabled) + funcs.glStencilFunc(GL_LEQUAL, q->state()->currentClip, ~GL_STENCIL_HIGH_BIT); + else + funcs.glStencilFunc(GL_ALWAYS, 0, 0xff); + + vertexCoordinateArray.clear(); + vertexCoordinateArray.addPath(path, inverseScale, false); + + if (!singlePass) + fillStencilWithVertexArray(vertexCoordinateArray, path.hasWindingFill()); + + funcs.glColorMask(false, false, false, false); + funcs.glEnable(GL_STENCIL_TEST); + useSimpleShader(); + + if (singlePass) { + // Under these conditions we can set the new stencil value in a single + // pass, by using the current value and the "new value" as the toggles + + funcs.glStencilFunc(GL_LEQUAL, referenceClipValue, ~GL_STENCIL_HIGH_BIT); + funcs.glStencilOp(GL_KEEP, GL_INVERT, GL_INVERT); + funcs.glStencilMask(value ^ referenceClipValue); + + drawVertexArrays(vertexCoordinateArray, GL_TRIANGLE_FAN); + } else { + funcs.glStencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE); + funcs.glStencilMask(0xff); + + if (!q->state()->clipTestEnabled && path.hasWindingFill()) { + // Pass when any clip bit is set, set high bit + funcs.glStencilFunc(GL_NOTEQUAL, GL_STENCIL_HIGH_BIT, ~GL_STENCIL_HIGH_BIT); + composite(vertexCoordinateArray.boundingRect()); + } + + // Pass when high bit is set, replace stencil value with new clip value + funcs.glStencilFunc(GL_NOTEQUAL, value, GL_STENCIL_HIGH_BIT); + + composite(vertexCoordinateArray.boundingRect()); + } + + funcs.glStencilFunc(GL_LEQUAL, value, ~GL_STENCIL_HIGH_BIT); + funcs.glStencilMask(0); + + funcs.glColorMask(true, true, true, true); +} + +void QOpenGL2PaintEngineEx::clip(const QVectorPath &path, Qt::ClipOperation op) +{ +// qDebug("QOpenGL2PaintEngineEx::clip()"); + Q_D(QOpenGL2PaintEngineEx); + + state()->clipChanged = true; + + ensureActive(); + + if (op == Qt::ReplaceClip) { + op = Qt::IntersectClip; + if (d->hasClipOperations()) { + d->systemStateChanged(); + state()->canRestoreClip = false; + } + } + +#ifndef QT_GL_NO_SCISSOR_TEST + if (!path.isEmpty() && op == Qt::IntersectClip && (path.shape() == QVectorPath::RectangleHint)) { + const QPointF* const points = reinterpret_cast<const QPointF*>(path.points()); + QRectF rect(points[0], points[2]); + + if (state()->matrix.type() <= QTransform::TxScale + || (state()->matrix.type() == QTransform::TxRotate + && qFuzzyIsNull(state()->matrix.m11()) + && qFuzzyIsNull(state()->matrix.m22()))) + { + state()->rectangleClip = state()->rectangleClip.intersected(state()->matrix.mapRect(rect).toRect()); + d->updateClipScissorTest(); + return; + } + } +#endif + + const QRect pathRect = state()->matrix.mapRect(path.controlPointRect()).toAlignedRect(); + + switch (op) { + case Qt::NoClip: + if (d->useSystemClip) { + state()->clipTestEnabled = true; + state()->currentClip = 1; + } else { + state()->clipTestEnabled = false; + } + state()->rectangleClip = QRect(0, 0, d->width, d->height); + state()->canRestoreClip = false; + d->updateClipScissorTest(); + break; + case Qt::IntersectClip: + state()->rectangleClip = state()->rectangleClip.intersected(pathRect); + d->updateClipScissorTest(); + d->resetClipIfNeeded(); + ++d->maxClip; + d->writeClip(path, d->maxClip); + state()->currentClip = d->maxClip; + state()->clipTestEnabled = true; + break; + default: + break; + } +} + +void QOpenGL2PaintEngineExPrivate::regenerateClip() +{ + systemStateChanged(); + replayClipOperations(); +} + +void QOpenGL2PaintEngineExPrivate::systemStateChanged() +{ + Q_Q(QOpenGL2PaintEngineEx); + + q->state()->clipChanged = true; + + if (systemClip.isEmpty()) { + useSystemClip = false; + } else { + if (q->paintDevice()->devType() == QInternal::Widget && currentClipDevice) { + //QWidgetPrivate *widgetPrivate = qt_widget_private(static_cast<QWidget *>(currentClipDevice)->window()); + //useSystemClip = widgetPrivate->extra && widgetPrivate->extra->inRenderWithPainter; + useSystemClip = true; + } else { + useSystemClip = true; + } + } + + q->state()->clipTestEnabled = false; + q->state()->needsClipBufferClear = true; + + q->state()->currentClip = 1; + maxClip = 1; + + q->state()->rectangleClip = useSystemClip ? systemClip.boundingRect() : QRect(0, 0, width, height); + updateClipScissorTest(); + + if (systemClip.rectCount() == 1) { + if (systemClip.boundingRect() == QRect(0, 0, width, height)) + useSystemClip = false; +#ifndef QT_GL_NO_SCISSOR_TEST + // scissoring takes care of the system clip + return; +#endif + } + + if (useSystemClip) { + clearClip(0); + + QPainterPath path; + path.addRegion(systemClip); + + q->state()->currentClip = 0; + writeClip(qtVectorPathForPath(q->state()->matrix.inverted().map(path)), 1); + q->state()->currentClip = 1; + q->state()->clipTestEnabled = true; + } +} + +void QOpenGL2PaintEngineEx::setState(QPainterState *new_state) +{ + // qDebug("QOpenGL2PaintEngineEx::setState()"); + + Q_D(QOpenGL2PaintEngineEx); + + QOpenGL2PaintEngineState *s = static_cast<QOpenGL2PaintEngineState *>(new_state); + QOpenGL2PaintEngineState *old_state = state(); + + QPaintEngineEx::setState(s); + + if (s->isNew) { + // Newly created state object. The call to setState() + // will either be followed by a call to begin(), or we are + // setting the state as part of a save(). + s->isNew = false; + return; + } + + // Setting the state as part of a restore(). + + if (old_state == s || old_state->renderHintsChanged) + renderHintsChanged(); + + if (old_state == s || old_state->matrixChanged) + d->matrixDirty = true; + + if (old_state == s || old_state->compositionModeChanged) + d->compositionModeDirty = true; + + if (old_state == s || old_state->opacityChanged) + d->opacityUniformDirty = true; + + if (old_state == s || old_state->clipChanged) { + if (old_state && old_state != s && old_state->canRestoreClip) { + d->updateClipScissorTest(); + d->funcs.glDepthFunc(GL_LEQUAL); + } else { + d->regenerateClip(); + } + } +} + +QPainterState *QOpenGL2PaintEngineEx::createState(QPainterState *orig) const +{ + if (orig) + const_cast<QOpenGL2PaintEngineEx *>(this)->ensureActive(); + + QOpenGL2PaintEngineState *s; + if (!orig) + s = new QOpenGL2PaintEngineState(); + else + s = new QOpenGL2PaintEngineState(*static_cast<QOpenGL2PaintEngineState *>(orig)); + + s->matrixChanged = false; + s->compositionModeChanged = false; + s->opacityChanged = false; + s->renderHintsChanged = false; + s->clipChanged = false; + + return s; +} + +QOpenGL2PaintEngineState::QOpenGL2PaintEngineState(QOpenGL2PaintEngineState &other) + : QPainterState(other) +{ + isNew = true; + needsClipBufferClear = other.needsClipBufferClear; + clipTestEnabled = other.clipTestEnabled; + currentClip = other.currentClip; + canRestoreClip = other.canRestoreClip; + rectangleClip = other.rectangleClip; +} + +QOpenGL2PaintEngineState::QOpenGL2PaintEngineState() +{ + isNew = true; + needsClipBufferClear = true; + clipTestEnabled = false; + canRestoreClip = true; +} + +QOpenGL2PaintEngineState::~QOpenGL2PaintEngineState() +{ +} + +void QOpenGL2PaintEngineExPrivate::setVertexAttribArrayEnabled(int arrayIndex, bool enabled) +{ + Q_ASSERT(arrayIndex < QT_GL_VERTEX_ARRAY_TRACKED_COUNT); + + if (vertexAttributeArraysEnabledState[arrayIndex] && !enabled) + funcs.glDisableVertexAttribArray(arrayIndex); + + if (!vertexAttributeArraysEnabledState[arrayIndex] && enabled) + funcs.glEnableVertexAttribArray(arrayIndex); + + vertexAttributeArraysEnabledState[arrayIndex] = enabled; +} + +void QOpenGL2PaintEngineExPrivate::syncGlState() +{ + for (int i = 0; i < QT_GL_VERTEX_ARRAY_TRACKED_COUNT; ++i) { + if (vertexAttributeArraysEnabledState[i]) + funcs.glEnableVertexAttribArray(i); + else + funcs.glDisableVertexAttribArray(i); + } +} + + +QT_END_NAMESPACE diff --git a/src/opengl/qopenglpaintengine_p.h b/src/opengl/qopenglpaintengine_p.h new file mode 100644 index 0000000000..9dc92e3810 --- /dev/null +++ b/src/opengl/qopenglpaintengine_p.h @@ -0,0 +1,395 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 QOPENGLPAINTENGINE_P_H +#define QOPENGLPAINTENGINE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QDebug> + +#include <qopenglpaintdevice.h> + +#include <private/qpaintengineex_p.h> +#include <private/qopenglengineshadermanager_p.h> +#include <private/qopengl2pexvertexarray_p.h> +#include <private/qfontengine_p.h> +#include <private/qdatabuffer_p.h> +#include <private/qtriangulatingstroker_p.h> + +#include <private/qopenglextensions_p.h> + +#include <QOpenGLVertexArrayObject> +#include <QOpenGLBuffer> + +enum EngineMode { + ImageDrawingMode, + TextDrawingMode, + BrushDrawingMode, + ImageArrayDrawingMode, + ImageOpacityArrayDrawingMode +}; + +QT_BEGIN_NAMESPACE + +#define GL_STENCIL_HIGH_BIT GLuint(0x80) +#define QT_UNKNOWN_TEXTURE_UNIT GLuint(-1) +#define QT_DEFAULT_TEXTURE_UNIT GLuint(0) +#define QT_BRUSH_TEXTURE_UNIT GLuint(0) +#define QT_IMAGE_TEXTURE_UNIT GLuint(0) //Can be the same as brush texture unit +#define QT_MASK_TEXTURE_UNIT GLuint(1) +#define QT_BACKGROUND_TEXTURE_UNIT GLuint(2) + +class QOpenGL2PaintEngineExPrivate; + +class QOpenGL2PaintEngineState : public QPainterState +{ +public: + QOpenGL2PaintEngineState(QOpenGL2PaintEngineState &other); + QOpenGL2PaintEngineState(); + ~QOpenGL2PaintEngineState(); + + uint isNew : 1; + uint needsClipBufferClear : 1; + uint clipTestEnabled : 1; + uint canRestoreClip : 1; + uint matrixChanged : 1; + uint compositionModeChanged : 1; + uint opacityChanged : 1; + uint renderHintsChanged : 1; + uint clipChanged : 1; + uint currentClip : 8; + + QRect rectangleClip; +}; + +class Q_OPENGL_EXPORT QOpenGL2PaintEngineEx : public QPaintEngineEx +{ + Q_DECLARE_PRIVATE(QOpenGL2PaintEngineEx) +public: + QOpenGL2PaintEngineEx(); + ~QOpenGL2PaintEngineEx(); + + bool begin(QPaintDevice *device) override; + void ensureActive(); + bool end() override; + + virtual void clipEnabledChanged() override; + virtual void penChanged() override; + virtual void brushChanged() override; + virtual void brushOriginChanged() override; + virtual void opacityChanged() override; + virtual void compositionModeChanged() override; + virtual void renderHintsChanged() override; + virtual void transformChanged() override; + + virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) override; + virtual void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints) override; + virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, + Qt::ImageConversionFlags flags = Qt::AutoColor) override; + virtual void drawTextItem(const QPointF &p, const QTextItem &textItem) override; + virtual void fill(const QVectorPath &path, const QBrush &brush) override; + virtual void stroke(const QVectorPath &path, const QPen &pen) override; + virtual void clip(const QVectorPath &path, Qt::ClipOperation op) override; + + virtual void drawStaticTextItem(QStaticTextItem *textItem) override; + + bool drawTexture(const QRectF &r, GLuint textureId, const QSize &size, const QRectF &sr); + + Type type() const override { return OpenGL2; } + + virtual void setState(QPainterState *s) override; + virtual QPainterState *createState(QPainterState *orig) const override; + inline QOpenGL2PaintEngineState *state() { + return static_cast<QOpenGL2PaintEngineState *>(QPaintEngineEx::state()); + } + inline const QOpenGL2PaintEngineState *state() const { + return static_cast<const QOpenGL2PaintEngineState *>(QPaintEngineEx::state()); + } + + void beginNativePainting() override; + void endNativePainting() override; + + void invalidateState(); + + void setRenderTextActive(bool); + + bool isNativePaintingActive() const; + bool requiresPretransformedGlyphPositions(QFontEngine *, const QTransform &) const override { return false; } + bool shouldDrawCachedGlyphs(QFontEngine *, const QTransform &) const override; + +private: + Q_DISABLE_COPY_MOVE(QOpenGL2PaintEngineEx) + + friend class QOpenGLEngineShaderManager; +}; + +// This probably needs to grow to GL_MAX_VERTEX_ATTRIBS, but 3 is ok for now as that's +// all the GL2 engine uses: +#define QT_GL_VERTEX_ARRAY_TRACKED_COUNT 3 + +class QOpenGL2PaintEngineExPrivate : public QPaintEngineExPrivate +{ + Q_DECLARE_PUBLIC(QOpenGL2PaintEngineEx) +public: + enum StencilFillMode { + OddEvenFillMode, + WindingFillMode, + TriStripStrokeFillMode + }; + + QOpenGL2PaintEngineExPrivate(QOpenGL2PaintEngineEx *q_ptr) : + q(q_ptr), + shaderManager(nullptr), + width(0), height(0), + ctx(nullptr), + useSystemClip(true), + elementIndicesVBOId(0), + opacityArray(0), + snapToPixelGrid(false), + nativePaintingActive(false), + inverseScale(1), + lastTextureUnitUsed(QT_UNKNOWN_TEXTURE_UNIT), + vertexBuffer(QOpenGLBuffer::VertexBuffer), + texCoordBuffer(QOpenGLBuffer::VertexBuffer), + opacityBuffer(QOpenGLBuffer::VertexBuffer), + indexBuffer(QOpenGLBuffer::IndexBuffer) + { } + + ~QOpenGL2PaintEngineExPrivate(); + + void updateBrushTexture(); + void updateBrushUniforms(); + void updateMatrix(); + void updateCompositionMode(); + + enum TextureUpdateMode { UpdateIfNeeded, ForceUpdate }; + template<typename T> + void updateTexture(GLenum textureUnit, const T &texture, GLenum wrapMode, GLenum filterMode, TextureUpdateMode updateMode = UpdateIfNeeded); + template<typename T> + GLuint bindTexture(const T &texture); + void activateTextureUnit(GLenum textureUnit); + + void resetGLState(); + + // fill, stroke, drawTexture, drawPixmaps & drawCachedGlyphs are the main rendering entry-points, + // however writeClip can also be thought of as en entry point as it does similar things. + void fill(const QVectorPath &path); + void stroke(const QVectorPath &path, const QPen &pen); + void drawTexture(const QOpenGLRect& dest, const QOpenGLRect& src, const QSize &textureSize, bool opaque, bool pattern = false); + void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, + QPainter::PixmapFragmentHints hints); + void drawCachedGlyphs(QFontEngine::GlyphFormat glyphFormat, QStaticTextItem *staticTextItem); + + // Calls glVertexAttributePointer if the pointer has changed + inline void uploadData(unsigned int arrayIndex, const GLfloat *data, GLuint count); + inline bool uploadIndexData(const void *data, GLenum indexValueType, GLuint count); + + // draws whatever is in the vertex array: + void drawVertexArrays(const float *data, int *stops, int stopCount, GLenum primitive); + void drawVertexArrays(QOpenGL2PEXVertexArray &vertexArray, GLenum primitive) { + drawVertexArrays((const float *) vertexArray.data(), vertexArray.stops(), vertexArray.stopCount(), primitive); + } + + // Composites the bounding rect onto dest buffer: + void composite(const QOpenGLRect& boundingRect); + + // Calls drawVertexArrays to render into stencil buffer: + void fillStencilWithVertexArray(const float *data, int count, int *stops, int stopCount, const QOpenGLRect &bounds, StencilFillMode mode); + void fillStencilWithVertexArray(QOpenGL2PEXVertexArray& vertexArray, bool useWindingFill) { + fillStencilWithVertexArray((const float *) vertexArray.data(), 0, vertexArray.stops(), vertexArray.stopCount(), + vertexArray.boundingRect(), + useWindingFill ? WindingFillMode : OddEvenFillMode); + } + + void setBrush(const QBrush& brush); + void transferMode(EngineMode newMode); + bool prepareForDraw(bool srcPixelsAreOpaque); // returns true if the program has changed + bool prepareForCachedGlyphDraw(const QFontEngineGlyphCache &cache); + inline void useSimpleShader(); + inline GLuint location(const QOpenGLEngineShaderManager::Uniform uniform) { + return shaderManager->getUniformLocation(uniform); + } + + void clearClip(uint value); + void writeClip(const QVectorPath &path, uint value); + void resetClipIfNeeded(); + + void updateClipScissorTest(); + void setScissor(const QRect &rect); + void regenerateClip(); + void systemStateChanged() override; + + void setVertexAttribArrayEnabled(int arrayIndex, bool enabled = true); + void syncGlState(); + + static QOpenGLEngineShaderManager* shaderManagerForEngine(QOpenGL2PaintEngineEx *engine) { return engine->d_func()->shaderManager; } + static QOpenGL2PaintEngineExPrivate *getData(QOpenGL2PaintEngineEx *engine) { return engine->d_func(); } + static void cleanupVectorPath(QPaintEngineEx *engine, void *data); + + QOpenGLExtensions funcs; + + QOpenGL2PaintEngineEx* q; + QOpenGLEngineShaderManager* shaderManager; + QOpenGLPaintDevice* device; + int width, height; + QOpenGLContext *ctx; + EngineMode mode; + QFontEngine::GlyphFormat glyphCacheFormat; + + bool vertexAttributeArraysEnabledState[QT_GL_VERTEX_ARRAY_TRACKED_COUNT]; + + // Dirty flags + bool matrixDirty; // Implies matrix uniforms are also dirty + bool compositionModeDirty; + bool brushTextureDirty; + bool brushUniformsDirty; + bool opacityUniformDirty; + bool matrixUniformDirty; + + bool stencilClean; // Has the stencil not been used for clipping so far? + bool useSystemClip; + QRegion dirtyStencilRegion; + QRect currentScissorBounds; + uint maxClip; + + QBrush currentBrush; // May not be the state's brush! + const QBrush noBrush; + + QImage currentBrushImage; + + QOpenGL2PEXVertexArray vertexCoordinateArray; + QOpenGL2PEXVertexArray textureCoordinateArray; + QVector<GLushort> elementIndices; + GLuint elementIndicesVBOId; + QDataBuffer<GLfloat> opacityArray; + GLfloat staticVertexCoordinateArray[8]; + GLfloat staticTextureCoordinateArray[8]; + + bool snapToPixelGrid; + bool nativePaintingActive; + GLfloat pmvMatrix[3][3]; + GLfloat inverseScale; + + GLenum lastTextureUnitUsed; + GLuint lastTextureUsed; + + QOpenGLVertexArrayObject vao; + QOpenGLBuffer vertexBuffer; + QOpenGLBuffer texCoordBuffer; + QOpenGLBuffer opacityBuffer; + QOpenGLBuffer indexBuffer; + + bool needsSync; + bool multisamplingAlwaysEnabled; + + QTriangulatingStroker stroker; + QDashedStrokeProcessor dasher; + + QVector<GLuint> unusedVBOSToClean; + QVector<GLuint> unusedIBOSToClean; + + const GLfloat *vertexAttribPointers[3]; +}; + + +void QOpenGL2PaintEngineExPrivate::uploadData(unsigned int arrayIndex, const GLfloat *data, GLuint count) +{ + Q_ASSERT(arrayIndex < 3); + + // If a vertex array object is created we have a profile that supports them + // and we will upload the data via a QOpenGLBuffer. Otherwise we will use + // the legacy way of uploading the data via glVertexAttribPointer. + if (vao.isCreated()) { + if (arrayIndex == QT_VERTEX_COORDS_ATTR) { + vertexBuffer.bind(); + vertexBuffer.allocate(data, count * sizeof(float)); + } + if (arrayIndex == QT_TEXTURE_COORDS_ATTR) { + texCoordBuffer.bind(); + texCoordBuffer.allocate(data, count * sizeof(float)); + } + if (arrayIndex == QT_OPACITY_ATTR) { + opacityBuffer.bind(); + opacityBuffer.allocate(data, count * sizeof(float)); + } + if (arrayIndex == QT_OPACITY_ATTR) + funcs.glVertexAttribPointer(arrayIndex, 1, GL_FLOAT, GL_FALSE, 0, nullptr); + else + funcs.glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, nullptr); + } else { + // If we already uploaded the data we don't have to do it again + if (data == vertexAttribPointers[arrayIndex]) + return; + + // Store the data in cache and upload it to the graphics card. + vertexAttribPointers[arrayIndex] = data; + if (arrayIndex == QT_OPACITY_ATTR) + funcs.glVertexAttribPointer(arrayIndex, 1, GL_FLOAT, GL_FALSE, 0, data); + else + funcs.glVertexAttribPointer(arrayIndex, 2, GL_FLOAT, GL_FALSE, 0, data); + } +} + +bool QOpenGL2PaintEngineExPrivate::uploadIndexData(const void *data, GLenum indexValueType, GLuint count) +{ + // Follow the uploadData() logic: VBOs are used only when VAO support is available. + // Otherwise the legacy client-side pointer path is used. + if (vao.isCreated()) { + Q_ASSERT(indexValueType == GL_UNSIGNED_SHORT || indexValueType == GL_UNSIGNED_INT); + indexBuffer.bind(); + indexBuffer.allocate(data, count * (indexValueType == GL_UNSIGNED_SHORT ? sizeof(quint16) : sizeof(quint32))); + return true; + } + return false; +} + +QT_END_NAMESPACE + +#endif diff --git a/src/opengl/qopenglpixeltransferoptions.cpp b/src/opengl/qopenglpixeltransferoptions.cpp new file mode 100644 index 0000000000..aa1af3b092 --- /dev/null +++ b/src/opengl/qopenglpixeltransferoptions.cpp @@ -0,0 +1,263 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB). +** 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 "qopenglpixeltransferoptions.h" +#include <QSharedData> + +QT_BEGIN_NAMESPACE + +/*! + * \class QOpenGLPixelTransferOptions + * + * \brief The QOpenGLPixelTransferOptions class describes the pixel storage + * modes that affect the unpacking of pixels during texture upload. + */ + +/*! + * \fn QOpenGLPixelTransferOptions & QOpenGLPixelTransferOptions::operator=(QOpenGLPixelTransferOptions &&other) + * \internal + */ + +/*! + * \fn void QOpenGLPixelTransferOptions::swap(QOpenGLPixelTransferOptions &other) + * \internal + */ + +class QOpenGLPixelTransferOptionsData : public QSharedData +{ +public: + QOpenGLPixelTransferOptionsData() + : alignment(4) + , skipImages(0) + , skipRows(0) + , skipPixels(0) + , imageHeight(0) + , rowLength(0) + , lsbFirst(false) + , swapBytes(false) + {} + + int alignment; + int skipImages; + int skipRows; + int skipPixels; + int imageHeight; + int rowLength; + bool lsbFirst; + bool swapBytes; +}; + +/*! + * Constructs a new QOpenGLPixelTransferOptions instance with the default settings. + */ +QOpenGLPixelTransferOptions::QOpenGLPixelTransferOptions() + : data(new QOpenGLPixelTransferOptionsData) +{ +} + +/*! + * \internal + */ +QOpenGLPixelTransferOptions::QOpenGLPixelTransferOptions(const QOpenGLPixelTransferOptions &rhs) + : data(rhs.data) +{ +} + +/*! + * \internal + */ +QOpenGLPixelTransferOptions &QOpenGLPixelTransferOptions::operator=(const QOpenGLPixelTransferOptions &rhs) +{ + if (this != &rhs) + data.operator=(rhs.data); + return *this; +} + +/*! + * Destructor. + */ +QOpenGLPixelTransferOptions::~QOpenGLPixelTransferOptions() +{ +} + +/*! + * Sets the \a alignment requirements for each pixel row. Corresponds to \c GL_UNPACK_ALIGNMENT. + * The default value is 4, as specified by OpenGL. + */ +void QOpenGLPixelTransferOptions::setAlignment(int alignment) +{ + data->alignment = alignment; +} + +/*! + * \return the current alignment requirement for each pixel row. + */ +int QOpenGLPixelTransferOptions::alignment() const +{ + return data->alignment; +} + +/*! + * Sets the number of images that are skipped to \a skipImages. + * Corresponds to \c GL_UNPACK_SKIP_IMAGES. Equivalent to incrementing the pointer + * passed to QOpenGLTexture::setData(). The default value is 0. + */ +void QOpenGLPixelTransferOptions::setSkipImages(int skipImages) +{ + data->skipImages = skipImages; +} + +/*! + * \return the number of images that are skipped. + */ +int QOpenGLPixelTransferOptions::skipImages() const +{ + return data->skipImages; +} + +/*! + * Sets the number of rows that are skipped to \a skipRows. + * Corresponds to \c GL_UNPACK_SKIP_ROWS. Equivalent to incrementing the pointer + * passed to QOpenGLTexture::setData(). The default value is 0. + */ +void QOpenGLPixelTransferOptions::setSkipRows(int skipRows) +{ + data->skipRows = skipRows; +} + +/*! + * \return the number of rows that are skipped. + */ +int QOpenGLPixelTransferOptions::skipRows() const +{ + return data->skipRows; +} + +/*! + * Sets the number of pixels that are skipped to \a skipPixels. + * Corresponds to \c GL_UNPACK_SKIP_PIXELS. Equivalent to incrementing the pointer + * passed to QOpenGLTexture::setData(). The default value is 0. + */ +void QOpenGLPixelTransferOptions::setSkipPixels(int skipPixels) +{ + data->skipPixels = skipPixels; +} + +/*! + * \return the number of pixels that are skipped. + */ +int QOpenGLPixelTransferOptions::skipPixels() const +{ + return data->skipPixels; +} + +/*! + * Sets the image height for 3D textures to \a imageHeight. + * Corresponds to \c GL_UNPACK_IMAGE_HEIGHT. + * The default value is 0. + */ +void QOpenGLPixelTransferOptions::setImageHeight(int imageHeight) +{ + data->imageHeight = imageHeight; +} + +/*! + * \return the currently set image height. + */ +int QOpenGLPixelTransferOptions::imageHeight() const +{ + return data->imageHeight; +} + +/*! + * Sets the number of pixels in a row to \a rowLength. + * Corresponds to \c GL_UNPACK_ROW_LENGTH. + * The default value is 0. + */ +void QOpenGLPixelTransferOptions::setRowLength(int rowLength) +{ + data->rowLength = rowLength; +} + +/*! + * \return the currently set row length. + */ +int QOpenGLPixelTransferOptions::rowLength() const +{ + return data->rowLength; +} + +/*! + * \a lsbFirst specifies if bits within a byte are ordered from least to most significat. + * The default value is \c false, meaning that the first bit in each byte is the + * most significant one. This is significant for bitmap data only. + * Corresponds to \c GL_UNPACK_LSB_FIRST. + */ +void QOpenGLPixelTransferOptions::setLeastSignificantByteFirst(bool lsbFirst) +{ + data->lsbFirst = lsbFirst; +} + +/*! + * \return \c true if bits within a byte are ordered from least to most significant. + */ +bool QOpenGLPixelTransferOptions::isLeastSignificantBitFirst() const +{ + return data->lsbFirst; +} + +/*! + * \a swapBytes specifies if the byte ordering for multibyte components is reversed. + * The default value is \c false. + * Corresponds to \c GL_UNPACK_SWAP_BYTES. + */ +void QOpenGLPixelTransferOptions::setSwapBytesEnabled(bool swapBytes) +{ + data->swapBytes = swapBytes; +} + +/*! + * \return \c true if the byte ordering for multibyte components is reversed. + */ +bool QOpenGLPixelTransferOptions::isSwapBytesEnabled() const +{ + return data->swapBytes; +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopenglpixeltransferoptions.h b/src/opengl/qopenglpixeltransferoptions.h new file mode 100644 index 0000000000..252c2a2f1e --- /dev/null +++ b/src/opengl/qopenglpixeltransferoptions.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB). +** 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 QOPENGLPIXELUPLOADOPTIONS_H +#define QOPENGLPIXELUPLOADOPTIONS_H + +#include <QtOpenGL/qtopenglglobal.h> + +#if !defined(QT_NO_OPENGL) + +#include <QtCore/QSharedDataPointer> + +QT_BEGIN_NAMESPACE + +class QOpenGLPixelTransferOptionsData; + +class Q_OPENGL_EXPORT QOpenGLPixelTransferOptions +{ +public: + QOpenGLPixelTransferOptions(); + QOpenGLPixelTransferOptions(const QOpenGLPixelTransferOptions &); + QOpenGLPixelTransferOptions &operator=(QOpenGLPixelTransferOptions &&other) noexcept + { swap(other); return *this; } + QOpenGLPixelTransferOptions &operator=(const QOpenGLPixelTransferOptions &); + ~QOpenGLPixelTransferOptions(); + + void swap(QOpenGLPixelTransferOptions &other) noexcept + { data.swap(other.data); } + + void setAlignment(int alignment); + int alignment() const; + + void setSkipImages(int skipImages); + int skipImages() const; + + void setSkipRows(int skipRows); + int skipRows() const; + + void setSkipPixels(int skipPixels); + int skipPixels() const; + + void setImageHeight(int imageHeight); + int imageHeight() const; + + void setRowLength(int rowLength); + int rowLength() const; + + void setLeastSignificantByteFirst(bool lsbFirst); + bool isLeastSignificantBitFirst() const; + + void setSwapBytesEnabled(bool swapBytes); + bool isSwapBytesEnabled() const; + +private: + QSharedDataPointer<QOpenGLPixelTransferOptionsData> data; +}; + +Q_DECLARE_SHARED(QOpenGLPixelTransferOptions) + +QT_END_NAMESPACE + +#endif // QT_NO_OPENGL + +#endif // QOPENGLPIXELUPLOADOPTIONS_H diff --git a/src/opengl/qopenglshadercache_p.h b/src/opengl/qopenglshadercache_p.h new file mode 100644 index 0000000000..88efa34216 --- /dev/null +++ b/src/opengl/qopenglshadercache_p.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef QOPENGLSHADERCACHE_P_H +#define QOPENGLSHADERCACHE_P_H + +#include <QtOpenGL/qtopenglglobal.h> + +QT_BEGIN_NAMESPACE + + +class QOpenGLShaderProgram; +class QOpenGLContext; + +class CachedShader +{ +public: + inline CachedShader(const QByteArray &, const QByteArray &) + {} + + inline bool isCached() + { + return false; + } + + inline bool load(QOpenGLShaderProgram *, QOpenGLContext *) + { + return false; + } + + inline bool store(QOpenGLShaderProgram *, QOpenGLContext *) + { + return false; + } +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/opengl/qopengltexture.cpp b/src/opengl/qopengltexture.cpp new file mode 100644 index 0000000000..35d90898e5 --- /dev/null +++ b/src/opengl/qopengltexture.cpp @@ -0,0 +1,4987 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB). +** 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 "qopengltexture.h" +#include "qopengltexture_p.h" +#include "qopengltexturehelper_p.h" +#include "qopenglfunctions.h" +#include <QtGui/qcolor.h> +#include <QtGui/qopenglcontext.h> +#include <QtCore/qdebug.h> +#include <private/qobject_p.h> +#include <private/qopenglcontext_p.h> + +QT_BEGIN_NAMESPACE + +//this is to work around GL_TEXTURE_WRAP_R_OES which also has 0x8072 as value +#if !defined(GL_TEXTURE_WRAP_R) + #define GL_TEXTURE_WRAP_R 0x8072 +#endif + +QOpenGLTexturePrivate::QOpenGLTexturePrivate(QOpenGLTexture::Target textureTarget, + QOpenGLTexture *qq) + : q_ptr(qq), + context(nullptr), + target(textureTarget), + textureId(0), + format(QOpenGLTexture::NoFormat), + formatClass(QOpenGLTexture::NoFormatClass), + requestedMipLevels(1), + mipLevels(-1), + layers(1), + faces(1), + samples(0), + fixedSamplePositions(true), + baseLevel(0), + maxLevel(1000), + depthStencilMode(QOpenGLTexture::DepthMode), + comparisonFunction(QOpenGLTexture::CompareLessEqual), + comparisonMode(QOpenGLTexture::CompareNone), + minFilter(QOpenGLTexture::Nearest), + magFilter(QOpenGLTexture::Nearest), + maxAnisotropy(1.0f), + minLevelOfDetail(-1000.0f), + maxLevelOfDetail(1000.0f), + levelOfDetailBias(0.0f), + textureView(false), + autoGenerateMipMaps(true), + storageAllocated(false), + texFuncs(nullptr), + functions(nullptr) +{ + dimensions[0] = dimensions[1] = dimensions[2] = 1; + + switch (target) { + case QOpenGLTexture::Target1D: + bindingTarget = QOpenGLTexture::BindingTarget1D; + break; + case QOpenGLTexture::Target1DArray: + bindingTarget = QOpenGLTexture::BindingTarget1DArray; + break; + case QOpenGLTexture::Target2D: + bindingTarget = QOpenGLTexture::BindingTarget2D; + break; + case QOpenGLTexture::Target2DArray: + bindingTarget = QOpenGLTexture::BindingTarget2DArray; + break; + case QOpenGLTexture::Target3D: + bindingTarget = QOpenGLTexture::BindingTarget3D; + break; + case QOpenGLTexture::TargetCubeMap: + bindingTarget = QOpenGLTexture::BindingTargetCubeMap; + faces = 6; + break; + case QOpenGLTexture::TargetCubeMapArray: + bindingTarget = QOpenGLTexture::BindingTargetCubeMapArray; + faces = 6; + break; + case QOpenGLTexture::Target2DMultisample: + bindingTarget = QOpenGLTexture::BindingTarget2DMultisample; + break; + case QOpenGLTexture::Target2DMultisampleArray: + bindingTarget = QOpenGLTexture::BindingTarget2DMultisampleArray; + break; + case QOpenGLTexture::TargetRectangle: + bindingTarget = QOpenGLTexture::BindingTargetRectangle; + break; + case QOpenGLTexture::TargetBuffer: + bindingTarget = QOpenGLTexture::BindingTargetBuffer; + break; + } + + swizzleMask[0] = QOpenGLTexture::RedValue; + swizzleMask[1] = QOpenGLTexture::GreenValue; + swizzleMask[2] = QOpenGLTexture::BlueValue; + swizzleMask[3] = QOpenGLTexture::AlphaValue; + + wrapModes[0] = wrapModes[1] = wrapModes[2] = target == QOpenGLTexture::TargetRectangle + ? QOpenGLTexture::ClampToEdge : QOpenGLTexture::Repeat; +} + +QOpenGLTexturePrivate::~QOpenGLTexturePrivate() +{ + destroy(); +} + +void QOpenGLTexturePrivate::initializeOpenGLFunctions() +{ + // If we already have a functions object, there is nothing to do + if (texFuncs) + return; + + // See if the context already has a suitable resource we can use. + // If not create a functions object and add it to the context in case + // others wish to use it too + texFuncs = context->textureFunctions(); + if (!texFuncs) { + texFuncs = new QOpenGLTextureHelper(context); + auto *funcs = texFuncs; // lets us capture by pointer value below + context->setTextureFunctions(funcs, [funcs] { delete funcs; }); + } +} + +bool QOpenGLTexturePrivate::create() +{ + if (textureId != 0) + return true; + + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (!ctx) { + qWarning("Requires a valid current OpenGL context.\n" + "Texture has not been created"); + return false; + } + context = ctx; + functions = ctx->functions(); + + // Resolve any functions we will need based upon context version and create the texture + initializeOpenGLFunctions(); + + // What features do we have? + QOpenGLTexture::Feature feature = QOpenGLTexture::ImmutableStorage; + while (feature != QOpenGLTexture::MaxFeatureFlag) { + if (QOpenGLTexture::hasFeature(feature)) + features |= feature; + feature = static_cast<QOpenGLTexture::Feature>(feature << 1); + } + + functions->glGenTextures(1, &textureId); + return textureId != 0; +} + +void QOpenGLTexturePrivate::destroy() +{ + if (!textureId) { + // not created or already destroyed + return; + } + QOpenGLContext *currentContext = QOpenGLContext::currentContext(); + if (!currentContext) { + qWarning("QOpenGLTexturePrivate::destroy() called without a current context.\n" + "Texture has not been destroyed"); + return; + } + if (!QOpenGLContext::areSharing(currentContext, context)) { + + qWarning("QOpenGLTexturePrivate::destroy() called but texture context %p" + " is not shared with current context %p.\n" + "Texture has not been destroyed", + static_cast<const void *>(context), + static_cast<const void *>(currentContext)); + return; + } + + functions->glDeleteTextures(1, &textureId); + + context = nullptr; + functions = nullptr; + textureId = 0; + format = QOpenGLTexture::NoFormat; + formatClass = QOpenGLTexture::NoFormatClass; + requestedMipLevels = 1; + mipLevels = -1; + layers = 1; + faces = 1; + samples = 0; + fixedSamplePositions = true, + baseLevel = 0; + maxLevel = 1000; + depthStencilMode = QOpenGLTexture::DepthMode; + minFilter = QOpenGLTexture::Nearest; + magFilter = QOpenGLTexture::Nearest; + maxAnisotropy = 1.0f; + minLevelOfDetail = -1000.0f; + maxLevelOfDetail = 1000.0f; + levelOfDetailBias = 0.0f; + textureView = false; + autoGenerateMipMaps = true; + storageAllocated = false; + texFuncs = nullptr; + + swizzleMask[0] = QOpenGLTexture::RedValue; + swizzleMask[1] = QOpenGLTexture::GreenValue; + swizzleMask[2] = QOpenGLTexture::BlueValue; + swizzleMask[3] = QOpenGLTexture::AlphaValue; + + wrapModes[0] = wrapModes[1] = wrapModes[2] = target == QOpenGLTexture::TargetRectangle + ? QOpenGLTexture::ClampToEdge : QOpenGLTexture::Repeat; +} + +void QOpenGLTexturePrivate::bind() +{ + functions->glBindTexture(target, textureId); +} + +void QOpenGLTexturePrivate::bind(uint unit, QOpenGLTexture::TextureUnitReset reset) +{ + GLint oldTextureUnit = 0; + if (reset == QOpenGLTexture::ResetTextureUnit) + functions->glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit); + + texFuncs->glActiveTexture(GL_TEXTURE0 + unit); + functions->glBindTexture(target, textureId); + + if (reset == QOpenGLTexture::ResetTextureUnit) + texFuncs->glActiveTexture(GL_TEXTURE0 + oldTextureUnit); +} + +void QOpenGLTexturePrivate::release() +{ + functions->glBindTexture(target, 0); +} + +void QOpenGLTexturePrivate::release(uint unit, QOpenGLTexture::TextureUnitReset reset) +{ + GLint oldTextureUnit = 0; + if (reset == QOpenGLTexture::ResetTextureUnit) + functions->glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit); + + texFuncs->glActiveTexture(GL_TEXTURE0 + unit); + functions->glBindTexture(target, 0); + + if (reset == QOpenGLTexture::ResetTextureUnit) + texFuncs->glActiveTexture(GL_TEXTURE0 + oldTextureUnit); +} + +bool QOpenGLTexturePrivate::isBound() const +{ + GLint boundTextureId = 0; + functions->glGetIntegerv(bindingTarget, &boundTextureId); + return (static_cast<GLuint>(boundTextureId) == textureId); +} + +bool QOpenGLTexturePrivate::isBound(uint unit) const +{ + GLint oldTextureUnit = 0; + functions->glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit); + + GLint boundTextureId = 0; + texFuncs->glActiveTexture(GL_TEXTURE0 + unit); + functions->glGetIntegerv(bindingTarget, &boundTextureId); + bool result = (static_cast<GLuint>(boundTextureId) == textureId); + + texFuncs->glActiveTexture(GL_TEXTURE0 + oldTextureUnit); + return result; +} + +int QOpenGLTexturePrivate::evaluateMipLevels() const +{ + switch (target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::Target3D: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + return qMin(maximumMipLevelCount(), qMax(1, requestedMipLevels)); + + case QOpenGLTexture::TargetRectangle: + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + case QOpenGLTexture::TargetBuffer: + default: + return 1; + } +} + +static bool isSizedTextureFormat(QOpenGLTexture::TextureFormat internalFormat) +{ + switch (internalFormat) { + case QOpenGLTexture::NoFormat: + return false; + + case QOpenGLTexture::R8_UNorm: + case QOpenGLTexture::RG8_UNorm: + case QOpenGLTexture::RGB8_UNorm: + case QOpenGLTexture::RGBA8_UNorm: + case QOpenGLTexture::R16_UNorm: + case QOpenGLTexture::RG16_UNorm: + case QOpenGLTexture::RGB16_UNorm: + case QOpenGLTexture::RGBA16_UNorm: + case QOpenGLTexture::R8_SNorm: + case QOpenGLTexture::RG8_SNorm: + case QOpenGLTexture::RGB8_SNorm: + case QOpenGLTexture::RGBA8_SNorm: + case QOpenGLTexture::R16_SNorm: + case QOpenGLTexture::RG16_SNorm: + case QOpenGLTexture::RGB16_SNorm: + case QOpenGLTexture::RGBA16_SNorm: + case QOpenGLTexture::R8U: + case QOpenGLTexture::RG8U: + case QOpenGLTexture::RGB8U: + case QOpenGLTexture::RGBA8U: + case QOpenGLTexture::R16U: + case QOpenGLTexture::RG16U: + case QOpenGLTexture::RGB16U: + case QOpenGLTexture::RGBA16U: + case QOpenGLTexture::R32U: + case QOpenGLTexture::RG32U: + case QOpenGLTexture::RGB32U: + case QOpenGLTexture::RGBA32U: + case QOpenGLTexture::R8I: + case QOpenGLTexture::RG8I: + case QOpenGLTexture::RGB8I: + case QOpenGLTexture::RGBA8I: + case QOpenGLTexture::R16I: + case QOpenGLTexture::RG16I: + case QOpenGLTexture::RGB16I: + case QOpenGLTexture::RGBA16I: + case QOpenGLTexture::R32I: + case QOpenGLTexture::RG32I: + case QOpenGLTexture::RGB32I: + case QOpenGLTexture::RGBA32I: + case QOpenGLTexture::R16F: + case QOpenGLTexture::RG16F: + case QOpenGLTexture::RGB16F: + case QOpenGLTexture::RGBA16F: + case QOpenGLTexture::R32F: + case QOpenGLTexture::RG32F: + case QOpenGLTexture::RGB32F: + case QOpenGLTexture::RGBA32F: + case QOpenGLTexture::RGB9E5: + case QOpenGLTexture::RG11B10F: + case QOpenGLTexture::RG3B2: + case QOpenGLTexture::R5G6B5: + case QOpenGLTexture::RGB5A1: + case QOpenGLTexture::RGBA4: + case QOpenGLTexture::RGB10A2: + + case QOpenGLTexture::D16: + case QOpenGLTexture::D24: + case QOpenGLTexture::D32: + case QOpenGLTexture::D32F: + + case QOpenGLTexture::D24S8: + case QOpenGLTexture::D32FS8X24: + + case QOpenGLTexture::S8: + + case QOpenGLTexture::RGB_DXT1: + case QOpenGLTexture::RGBA_DXT1: + case QOpenGLTexture::RGBA_DXT3: + case QOpenGLTexture::RGBA_DXT5: + case QOpenGLTexture::R_ATI1N_UNorm: + case QOpenGLTexture::R_ATI1N_SNorm: + case QOpenGLTexture::RG_ATI2N_UNorm: + case QOpenGLTexture::RG_ATI2N_SNorm: + case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_SIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_UNorm: + case QOpenGLTexture::SRGB8: + case QOpenGLTexture::SRGB8_Alpha8: + case QOpenGLTexture::SRGB_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT3: + case QOpenGLTexture::SRGB_Alpha_DXT5: + case QOpenGLTexture::SRGB_BP_UNorm: + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: + case QOpenGLTexture::RGBA_ASTC_4x4: + case QOpenGLTexture::RGBA_ASTC_5x4: + case QOpenGLTexture::RGBA_ASTC_5x5: + case QOpenGLTexture::RGBA_ASTC_6x5: + case QOpenGLTexture::RGBA_ASTC_6x6: + case QOpenGLTexture::RGBA_ASTC_8x5: + case QOpenGLTexture::RGBA_ASTC_8x6: + case QOpenGLTexture::RGBA_ASTC_8x8: + case QOpenGLTexture::RGBA_ASTC_10x5: + case QOpenGLTexture::RGBA_ASTC_10x6: + case QOpenGLTexture::RGBA_ASTC_10x8: + case QOpenGLTexture::RGBA_ASTC_10x10: + case QOpenGLTexture::RGBA_ASTC_12x10: + case QOpenGLTexture::RGBA_ASTC_12x12: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12: + return true; + + case QOpenGLTexture::RGB8_ETC1: + return false; + + case QOpenGLTexture::DepthFormat: + case QOpenGLTexture::AlphaFormat: + + case QOpenGLTexture::RGBFormat: + case QOpenGLTexture::RGBAFormat: + + case QOpenGLTexture::LuminanceFormat: + + case QOpenGLTexture::LuminanceAlphaFormat: + return false; + } + + Q_UNREACHABLE(); + return false; +} + +static bool isTextureTargetMultisample(QOpenGLTexture::Target target) +{ + switch (target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::Target3D: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + return false; + + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + return true; + + case QOpenGLTexture::TargetRectangle: + case QOpenGLTexture::TargetBuffer: + return false; + } + + Q_UNREACHABLE(); + return false; +} + +bool QOpenGLTexturePrivate::isUsingImmutableStorage() const +{ + // Use immutable storage whenever possible, falling back to mutable + // Note that if multisample textures are not supported at all, we'll still fail into + // the mutable storage allocation + return isSizedTextureFormat(format) + && (isTextureTargetMultisample(target) + ? features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage) + : features.testFlag(QOpenGLTexture::ImmutableStorage)); +} + +void QOpenGLTexturePrivate::allocateStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType) +{ + // Resolve the actual number of mipmap levels we can use + mipLevels = evaluateMipLevels(); + + if (isUsingImmutableStorage()) + allocateImmutableStorage(); + else + allocateMutableStorage(pixelFormat, pixelType); +} + +static QOpenGLTexture::PixelFormat pixelFormatCompatibleWithInternalFormat(QOpenGLTexture::TextureFormat internalFormat) +{ + switch (internalFormat) { + case QOpenGLTexture::NoFormat: + return QOpenGLTexture::NoSourceFormat; + + case QOpenGLTexture::R8_UNorm: + return QOpenGLTexture::Red; + + case QOpenGLTexture::RG8_UNorm: + return QOpenGLTexture::RG; + + case QOpenGLTexture::RGB8_UNorm: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGBA8_UNorm: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::R16_UNorm: + return QOpenGLTexture::Red; + + case QOpenGLTexture::RG16_UNorm: + return QOpenGLTexture::RG; + + case QOpenGLTexture::RGB16_UNorm: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGBA16_UNorm: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::R8_SNorm: + return QOpenGLTexture::Red; + + case QOpenGLTexture::RG8_SNorm: + return QOpenGLTexture::RG; + + case QOpenGLTexture::RGB8_SNorm: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGBA8_SNorm: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::R16_SNorm: + return QOpenGLTexture::Red; + + case QOpenGLTexture::RG16_SNorm: + return QOpenGLTexture::RG; + + case QOpenGLTexture::RGB16_SNorm: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGBA16_SNorm: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::R8U: + return QOpenGLTexture::Red_Integer; + + case QOpenGLTexture::RG8U: + return QOpenGLTexture::RG_Integer; + + case QOpenGLTexture::RGB8U: + return QOpenGLTexture::RGB_Integer; + + case QOpenGLTexture::RGBA8U: + return QOpenGLTexture::RGBA_Integer; + + case QOpenGLTexture::R16U: + return QOpenGLTexture::Red_Integer; + + case QOpenGLTexture::RG16U: + return QOpenGLTexture::RG_Integer; + + case QOpenGLTexture::RGB16U: + return QOpenGLTexture::RGB_Integer; + + case QOpenGLTexture::RGBA16U: + return QOpenGLTexture::RGBA_Integer; + + case QOpenGLTexture::R32U: + return QOpenGLTexture::Red_Integer; + + case QOpenGLTexture::RG32U: + return QOpenGLTexture::RG_Integer; + + case QOpenGLTexture::RGB32U: + return QOpenGLTexture::RGB_Integer; + + case QOpenGLTexture::RGBA32U: + return QOpenGLTexture::RGBA_Integer; + + case QOpenGLTexture::R8I: + return QOpenGLTexture::Red_Integer; + + case QOpenGLTexture::RG8I: + return QOpenGLTexture::RG_Integer; + + case QOpenGLTexture::RGB8I: + return QOpenGLTexture::RGB_Integer; + + case QOpenGLTexture::RGBA8I: + return QOpenGLTexture::RGBA_Integer; + + case QOpenGLTexture::R16I: + return QOpenGLTexture::Red_Integer; + + case QOpenGLTexture::RG16I: + return QOpenGLTexture::RG_Integer; + + case QOpenGLTexture::RGB16I: + return QOpenGLTexture::RGB_Integer; + + case QOpenGLTexture::RGBA16I: + return QOpenGLTexture::RGBA_Integer; + + case QOpenGLTexture::R32I: + return QOpenGLTexture::Red_Integer; + + case QOpenGLTexture::RG32I: + return QOpenGLTexture::RG_Integer; + + case QOpenGLTexture::RGB32I: + return QOpenGLTexture::RGB_Integer; + + case QOpenGLTexture::RGBA32I: + return QOpenGLTexture::RGBA_Integer; + + case QOpenGLTexture::R16F: + return QOpenGLTexture::Red; + + case QOpenGLTexture::RG16F: + return QOpenGLTexture::RG; + + case QOpenGLTexture::RGB16F: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGBA16F: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::R32F: + return QOpenGLTexture::Red; + + case QOpenGLTexture::RG32F: + return QOpenGLTexture::RG; + + case QOpenGLTexture::RGB32F: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGBA32F: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::RGB9E5: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RG11B10F: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RG3B2: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::R5G6B5: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGB5A1: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::RGBA4: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::RGB10A2: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::D16: + case QOpenGLTexture::D24: + case QOpenGLTexture::D32: + case QOpenGLTexture::D32F: + return QOpenGLTexture::Depth; + + case QOpenGLTexture::D24S8: + case QOpenGLTexture::D32FS8X24: + return QOpenGLTexture::DepthStencil; + + case QOpenGLTexture::S8: + return QOpenGLTexture::Stencil; + + case QOpenGLTexture::RGB_DXT1: + case QOpenGLTexture::RGBA_DXT1: + case QOpenGLTexture::RGBA_DXT3: + case QOpenGLTexture::RGBA_DXT5: + case QOpenGLTexture::R_ATI1N_UNorm: + case QOpenGLTexture::R_ATI1N_SNorm: + case QOpenGLTexture::RG_ATI2N_UNorm: + case QOpenGLTexture::RG_ATI2N_SNorm: + case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_SIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_UNorm: + case QOpenGLTexture::SRGB8: + case QOpenGLTexture::SRGB8_Alpha8: + case QOpenGLTexture::SRGB_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT3: + case QOpenGLTexture::SRGB_Alpha_DXT5: + case QOpenGLTexture::SRGB_BP_UNorm: + case QOpenGLTexture::RGB8_ETC1: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + return QOpenGLTexture::Red; + + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + return QOpenGLTexture::RG; + + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::RGBA_ASTC_4x4: + case QOpenGLTexture::RGBA_ASTC_5x4: + case QOpenGLTexture::RGBA_ASTC_5x5: + case QOpenGLTexture::RGBA_ASTC_6x5: + case QOpenGLTexture::RGBA_ASTC_6x6: + case QOpenGLTexture::RGBA_ASTC_8x5: + case QOpenGLTexture::RGBA_ASTC_8x6: + case QOpenGLTexture::RGBA_ASTC_8x8: + case QOpenGLTexture::RGBA_ASTC_10x5: + case QOpenGLTexture::RGBA_ASTC_10x6: + case QOpenGLTexture::RGBA_ASTC_10x8: + case QOpenGLTexture::RGBA_ASTC_10x10: + case QOpenGLTexture::RGBA_ASTC_12x10: + case QOpenGLTexture::RGBA_ASTC_12x12: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::DepthFormat: + return QOpenGLTexture::Depth; + + case QOpenGLTexture::AlphaFormat: + return QOpenGLTexture::Alpha; + + case QOpenGLTexture::RGBFormat: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGBAFormat: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::LuminanceFormat: + return QOpenGLTexture::Luminance; + + case QOpenGLTexture::LuminanceAlphaFormat: + return QOpenGLTexture::LuminanceAlpha; + } + + Q_UNREACHABLE(); + return QOpenGLTexture::NoSourceFormat; +} + +static QOpenGLTexture::PixelType pixelTypeCompatibleWithInternalFormat(QOpenGLTexture::TextureFormat internalFormat) +{ + switch (internalFormat) { + case QOpenGLTexture::NoFormat: + return QOpenGLTexture::NoPixelType; + + case QOpenGLTexture::R8_UNorm: + case QOpenGLTexture::RG8_UNorm: + case QOpenGLTexture::RGB8_UNorm: + case QOpenGLTexture::RGBA8_UNorm: + case QOpenGLTexture::R16_UNorm: + case QOpenGLTexture::RG16_UNorm: + case QOpenGLTexture::RGB16_UNorm: + case QOpenGLTexture::RGBA16_UNorm: + return QOpenGLTexture::UInt8; + + case QOpenGLTexture::R8_SNorm: + case QOpenGLTexture::RG8_SNorm: + case QOpenGLTexture::RGB8_SNorm: + case QOpenGLTexture::RGBA8_SNorm: + case QOpenGLTexture::R16_SNorm: + case QOpenGLTexture::RG16_SNorm: + case QOpenGLTexture::RGB16_SNorm: + case QOpenGLTexture::RGBA16_SNorm: + return QOpenGLTexture::Int8; + + case QOpenGLTexture::R8U: + case QOpenGLTexture::RG8U: + case QOpenGLTexture::RGB8U: + case QOpenGLTexture::RGBA8U: + case QOpenGLTexture::R16U: + case QOpenGLTexture::RG16U: + case QOpenGLTexture::RGB16U: + case QOpenGLTexture::RGBA16U: + case QOpenGLTexture::R32U: + case QOpenGLTexture::RG32U: + case QOpenGLTexture::RGB32U: + case QOpenGLTexture::RGBA32U: + return QOpenGLTexture::UInt8; + + case QOpenGLTexture::R8I: + case QOpenGLTexture::RG8I: + case QOpenGLTexture::RGB8I: + case QOpenGLTexture::RGBA8I: + case QOpenGLTexture::R16I: + case QOpenGLTexture::RG16I: + case QOpenGLTexture::RGB16I: + case QOpenGLTexture::RGBA16I: + case QOpenGLTexture::R32I: + case QOpenGLTexture::RG32I: + case QOpenGLTexture::RGB32I: + case QOpenGLTexture::RGBA32I: + return QOpenGLTexture::Int8; + + case QOpenGLTexture::R16F: + case QOpenGLTexture::RG16F: + case QOpenGLTexture::RGB16F: + case QOpenGLTexture::RGBA16F: + return QOpenGLTexture::Float16; + + case QOpenGLTexture::R32F: + case QOpenGLTexture::RG32F: + case QOpenGLTexture::RGB32F: + case QOpenGLTexture::RGBA32F: + return QOpenGLTexture::Float32; + + case QOpenGLTexture::RGB9E5: + return QOpenGLTexture::UInt16_RGB5A1_Rev; + + case QOpenGLTexture::RG11B10F: + return QOpenGLTexture::UInt32_RG11B10F; + + case QOpenGLTexture::RG3B2: + return QOpenGLTexture::UInt8_RG3B2; + + case QOpenGLTexture::R5G6B5: + return QOpenGLTexture::UInt16_R5G6B5; + + case QOpenGLTexture::RGB5A1: + return QOpenGLTexture::UInt16_RGB5A1; + + case QOpenGLTexture::RGBA4: + return QOpenGLTexture::UInt16_RGBA4; + + case QOpenGLTexture::RGB10A2: + return QOpenGLTexture::UInt32_RGB10A2; + + case QOpenGLTexture::D16: + return QOpenGLTexture::UInt16; + + case QOpenGLTexture::D24: + case QOpenGLTexture::D32: + return QOpenGLTexture::UInt32; + + case QOpenGLTexture::D32F: + return QOpenGLTexture::Float32; + + case QOpenGLTexture::D24S8: + return QOpenGLTexture::UInt32_D24S8; + + case QOpenGLTexture::D32FS8X24: + return QOpenGLTexture::Float32_D32_UInt32_S8_X24; + + case QOpenGLTexture::S8: + return QOpenGLTexture::UInt8; + + case QOpenGLTexture::RGB_DXT1: + case QOpenGLTexture::RGBA_DXT1: + case QOpenGLTexture::RGBA_DXT3: + case QOpenGLTexture::RGBA_DXT5: + case QOpenGLTexture::R_ATI1N_UNorm: + case QOpenGLTexture::R_ATI1N_SNorm: + case QOpenGLTexture::RG_ATI2N_UNorm: + case QOpenGLTexture::RG_ATI2N_SNorm: + case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_SIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_UNorm: + case QOpenGLTexture::SRGB8: + case QOpenGLTexture::SRGB8_Alpha8: + case QOpenGLTexture::SRGB_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT3: + case QOpenGLTexture::SRGB_Alpha_DXT5: + case QOpenGLTexture::SRGB_BP_UNorm: + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: + case QOpenGLTexture::RGB8_ETC1: + case QOpenGLTexture::RGBA_ASTC_4x4: + case QOpenGLTexture::RGBA_ASTC_5x4: + case QOpenGLTexture::RGBA_ASTC_5x5: + case QOpenGLTexture::RGBA_ASTC_6x5: + case QOpenGLTexture::RGBA_ASTC_6x6: + case QOpenGLTexture::RGBA_ASTC_8x5: + case QOpenGLTexture::RGBA_ASTC_8x6: + case QOpenGLTexture::RGBA_ASTC_8x8: + case QOpenGLTexture::RGBA_ASTC_10x5: + case QOpenGLTexture::RGBA_ASTC_10x6: + case QOpenGLTexture::RGBA_ASTC_10x8: + case QOpenGLTexture::RGBA_ASTC_10x10: + case QOpenGLTexture::RGBA_ASTC_12x10: + case QOpenGLTexture::RGBA_ASTC_12x12: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12: + return QOpenGLTexture::UInt8; + + case QOpenGLTexture::DepthFormat: + return QOpenGLTexture::UInt32; + + case QOpenGLTexture::AlphaFormat: + case QOpenGLTexture::RGBFormat: + case QOpenGLTexture::RGBAFormat: + case QOpenGLTexture::LuminanceFormat: + case QOpenGLTexture::LuminanceAlphaFormat: + return QOpenGLTexture::UInt8; + } + + Q_UNREACHABLE(); + return QOpenGLTexture::NoPixelType; +} + +static bool isCompressedFormat(QOpenGLTexture::TextureFormat internalFormat) +{ + switch (internalFormat) { + case QOpenGLTexture::NoFormat: + + case QOpenGLTexture::R8_UNorm: + case QOpenGLTexture::RG8_UNorm: + case QOpenGLTexture::RGB8_UNorm: + case QOpenGLTexture::RGBA8_UNorm: + case QOpenGLTexture::R16_UNorm: + case QOpenGLTexture::RG16_UNorm: + case QOpenGLTexture::RGB16_UNorm: + case QOpenGLTexture::RGBA16_UNorm: + case QOpenGLTexture::R8_SNorm: + case QOpenGLTexture::RG8_SNorm: + case QOpenGLTexture::RGB8_SNorm: + case QOpenGLTexture::RGBA8_SNorm: + case QOpenGLTexture::R16_SNorm: + case QOpenGLTexture::RG16_SNorm: + case QOpenGLTexture::RGB16_SNorm: + case QOpenGLTexture::RGBA16_SNorm: + case QOpenGLTexture::R8U: + case QOpenGLTexture::RG8U: + case QOpenGLTexture::RGB8U: + case QOpenGLTexture::RGBA8U: + case QOpenGLTexture::R16U: + case QOpenGLTexture::RG16U: + case QOpenGLTexture::RGB16U: + case QOpenGLTexture::RGBA16U: + case QOpenGLTexture::R32U: + case QOpenGLTexture::RG32U: + case QOpenGLTexture::RGB32U: + case QOpenGLTexture::RGBA32U: + case QOpenGLTexture::R8I: + case QOpenGLTexture::RG8I: + case QOpenGLTexture::RGB8I: + case QOpenGLTexture::RGBA8I: + case QOpenGLTexture::R16I: + case QOpenGLTexture::RG16I: + case QOpenGLTexture::RGB16I: + case QOpenGLTexture::RGBA16I: + case QOpenGLTexture::R32I: + case QOpenGLTexture::RG32I: + case QOpenGLTexture::RGB32I: + case QOpenGLTexture::RGBA32I: + case QOpenGLTexture::R16F: + case QOpenGLTexture::RG16F: + case QOpenGLTexture::RGB16F: + case QOpenGLTexture::RGBA16F: + case QOpenGLTexture::R32F: + case QOpenGLTexture::RG32F: + case QOpenGLTexture::RGB32F: + case QOpenGLTexture::RGBA32F: + case QOpenGLTexture::RGB9E5: + case QOpenGLTexture::RG11B10F: + case QOpenGLTexture::RG3B2: + case QOpenGLTexture::R5G6B5: + case QOpenGLTexture::RGB5A1: + case QOpenGLTexture::RGBA4: + case QOpenGLTexture::RGB10A2: + + case QOpenGLTexture::D16: + case QOpenGLTexture::D24: + case QOpenGLTexture::D32: + case QOpenGLTexture::D32F: + + case QOpenGLTexture::D24S8: + case QOpenGLTexture::D32FS8X24: + + case QOpenGLTexture::S8: + return false; + + case QOpenGLTexture::RGB_DXT1: + case QOpenGLTexture::RGBA_DXT1: + case QOpenGLTexture::RGBA_DXT3: + case QOpenGLTexture::RGBA_DXT5: + case QOpenGLTexture::R_ATI1N_UNorm: + case QOpenGLTexture::R_ATI1N_SNorm: + case QOpenGLTexture::RG_ATI2N_UNorm: + case QOpenGLTexture::RG_ATI2N_SNorm: + case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_SIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_UNorm: + case QOpenGLTexture::SRGB8: + case QOpenGLTexture::SRGB8_Alpha8: + case QOpenGLTexture::SRGB_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT3: + case QOpenGLTexture::SRGB_Alpha_DXT5: + case QOpenGLTexture::SRGB_BP_UNorm: + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: + case QOpenGLTexture::RGB8_ETC1: + case QOpenGLTexture::RGBA_ASTC_4x4: + case QOpenGLTexture::RGBA_ASTC_5x4: + case QOpenGLTexture::RGBA_ASTC_5x5: + case QOpenGLTexture::RGBA_ASTC_6x5: + case QOpenGLTexture::RGBA_ASTC_6x6: + case QOpenGLTexture::RGBA_ASTC_8x5: + case QOpenGLTexture::RGBA_ASTC_8x6: + case QOpenGLTexture::RGBA_ASTC_8x8: + case QOpenGLTexture::RGBA_ASTC_10x5: + case QOpenGLTexture::RGBA_ASTC_10x6: + case QOpenGLTexture::RGBA_ASTC_10x8: + case QOpenGLTexture::RGBA_ASTC_10x10: + case QOpenGLTexture::RGBA_ASTC_12x10: + case QOpenGLTexture::RGBA_ASTC_12x12: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12: + return true; + + case QOpenGLTexture::DepthFormat: + case QOpenGLTexture::AlphaFormat: + case QOpenGLTexture::RGBFormat: + case QOpenGLTexture::RGBAFormat: + case QOpenGLTexture::LuminanceFormat: + case QOpenGLTexture::LuminanceAlphaFormat: + return false; + } + + Q_UNREACHABLE(); + return false; +} + +void QOpenGLTexturePrivate::allocateMutableStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType) +{ + // There is no way to allocate mutable storage for compressed textures in in + // versions older than OpenGL 3.1 and OpenGL ES 3.0, because the older specs + // do not mandate accepting null data pointers for glCompressedTexImage*D, + // unlike glTexImage*D (which in turn does not accept compressed formats). + if (isCompressedFormat(format)) { + storageAllocated = true; + return; + } + + switch (target) { + case QOpenGLTexture::TargetBuffer: + // Buffer textures get their storage from an external OpenGL buffer + qWarning("Buffer textures do not allocate storage"); + return; + + case QOpenGLTexture::Target1D: + if (features.testFlag(QOpenGLTexture::Texture1D)) { + for (int level = 0; level < mipLevels; ++level) + texFuncs->glTextureImage1D(textureId, target, bindingTarget, level, format, + mipLevelSize(level, dimensions[0]), + 0, + pixelFormat, pixelType, nullptr); + } else { + qWarning("1D textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target1DArray: + if (features.testFlag(QOpenGLTexture::Texture1D) + && features.testFlag(QOpenGLTexture::TextureArrays)) { + for (int level = 0; level < mipLevels; ++level) + texFuncs->glTextureImage2D(textureId, target, bindingTarget, level, format, + mipLevelSize(level, dimensions[0]), + layers, + 0, + pixelFormat, pixelType, nullptr); + } else { + qWarning("1D array textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target2D: + case QOpenGLTexture::TargetRectangle: + for (int level = 0; level < mipLevels; ++level) + texFuncs->glTextureImage2D(textureId, target, bindingTarget, level, format, + mipLevelSize(level, dimensions[0]), + mipLevelSize(level, dimensions[1]), + 0, + pixelFormat, pixelType, nullptr); + break; + + case QOpenGLTexture::TargetCubeMap: { + // Cubemaps are the odd one out. We have to allocate storage for each + // face and miplevel using the special cubemap face targets rather than + // GL_TARGET_CUBEMAP. + const QOpenGLTexture::CubeMapFace faceTargets[] = { + QOpenGLTexture::CubeMapPositiveX, QOpenGLTexture::CubeMapNegativeX, + QOpenGLTexture::CubeMapPositiveY, QOpenGLTexture::CubeMapNegativeY, + QOpenGLTexture::CubeMapPositiveZ, QOpenGLTexture::CubeMapNegativeZ + }; + + for (int faceTarget = 0; faceTarget < 6; ++faceTarget) { + for (int level = 0; level < mipLevels; ++level) { + texFuncs->glTextureImage2D(textureId, faceTargets[faceTarget], bindingTarget, + level, format, + mipLevelSize(level, dimensions[0]), + mipLevelSize(level, dimensions[1]), + 0, + pixelFormat, pixelType, nullptr); + } + } + break; + } + + case QOpenGLTexture::Target2DArray: + if (features.testFlag(QOpenGLTexture::TextureArrays)) { + for (int level = 0; level < mipLevels; ++level) + texFuncs->glTextureImage3D(textureId, target, bindingTarget, level, format, + mipLevelSize(level, dimensions[0]), + mipLevelSize(level, dimensions[1]), + layers, + 0, + pixelFormat, pixelType, nullptr); + } else { + qWarning("Array textures are not supported"); + return; + } + break; + + case QOpenGLTexture::TargetCubeMapArray: + // Cubemap arrays must specify number of layer-faces (6 * layers) as depth parameter + if (features.testFlag(QOpenGLTexture::TextureCubeMapArrays)) { + for (int level = 0; level < mipLevels; ++level) + texFuncs->glTextureImage3D(textureId, target, bindingTarget, level, format, + mipLevelSize(level, dimensions[0]), + mipLevelSize(level, dimensions[1]), + 6 * layers, + 0, + pixelFormat, pixelType, nullptr); + } else { + qWarning("Cubemap Array textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target3D: + if (features.testFlag(QOpenGLTexture::Texture3D)) { + for (int level = 0; level < mipLevels; ++level) + texFuncs->glTextureImage3D(textureId, target, bindingTarget, level, format, + mipLevelSize(level, dimensions[0]), + mipLevelSize(level, dimensions[1]), + mipLevelSize(level, dimensions[2]), + 0, + pixelFormat, pixelType, nullptr); + } else { + qWarning("3D textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target2DMultisample: + if (features.testFlag(QOpenGLTexture::TextureMultisample)) { + texFuncs->glTextureImage2DMultisample(textureId, target, bindingTarget, samples, format, + dimensions[0], dimensions[1], + fixedSamplePositions); + } else { + qWarning("Multisample textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target2DMultisampleArray: + if (features.testFlag(QOpenGLTexture::TextureMultisample) + && features.testFlag(QOpenGLTexture::TextureArrays)) { + texFuncs->glTextureImage3DMultisample(textureId, target, bindingTarget, samples, format, + dimensions[0], dimensions[1], layers, + fixedSamplePositions); + } else { + qWarning("Multisample array textures are not supported"); + return; + } + break; + } + + storageAllocated = true; +} + +void QOpenGLTexturePrivate::allocateImmutableStorage() +{ + switch (target) { + case QOpenGLTexture::TargetBuffer: + // Buffer textures get their storage from an external OpenGL buffer + qWarning("Buffer textures do not allocate storage"); + return; + + case QOpenGLTexture::Target1D: + if (features.testFlag(QOpenGLTexture::Texture1D)) { + texFuncs->glTextureStorage1D(textureId, target, bindingTarget, mipLevels, format, + dimensions[0]); + } else { + qWarning("1D textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target1DArray: + if (features.testFlag(QOpenGLTexture::Texture1D) + && features.testFlag(QOpenGLTexture::TextureArrays)) { + texFuncs->glTextureStorage2D(textureId, target, bindingTarget, mipLevels, format, + dimensions[0], layers); + } else { + qWarning("1D array textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target2D: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetRectangle: + texFuncs->glTextureStorage2D(textureId, target, bindingTarget, mipLevels, format, + dimensions[0], dimensions[1]); + break; + + case QOpenGLTexture::Target2DArray: + if (features.testFlag(QOpenGLTexture::TextureArrays)) { + texFuncs->glTextureStorage3D(textureId, target, bindingTarget, mipLevels, format, + dimensions[0], dimensions[1], layers); + } else { + qWarning("Array textures are not supported"); + return; + } + break; + + case QOpenGLTexture::TargetCubeMapArray: + // Cubemap arrays must specify number of layer-faces (6 * layers) as depth parameter + if (features.testFlag(QOpenGLTexture::TextureCubeMapArrays)) { + texFuncs->glTextureStorage3D(textureId, target, bindingTarget, mipLevels, format, + dimensions[0], dimensions[1], 6 * layers); + } else { + qWarning("Cubemap Array textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target3D: + if (features.testFlag(QOpenGLTexture::Texture3D)) { + texFuncs->glTextureStorage3D(textureId, target, bindingTarget, mipLevels, format, + dimensions[0], dimensions[1], dimensions[2]); + } else { + qWarning("3D textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target2DMultisample: + if (features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage)) { + texFuncs->glTextureStorage2DMultisample(textureId, target, bindingTarget, samples, format, + dimensions[0], dimensions[1], + fixedSamplePositions); + } else { + qWarning("Multisample textures are not supported"); + return; + } + break; + + case QOpenGLTexture::Target2DMultisampleArray: + if (features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage) + && features.testFlag(QOpenGLTexture::TextureArrays)) { + texFuncs->glTextureStorage3DMultisample(textureId, target, bindingTarget, samples, format, + dimensions[0], dimensions[1], layers, + fixedSamplePositions); + } else { + qWarning("Multisample array textures are not supported"); + return; + } + break; + } + + storageAllocated = true; +} + +void QOpenGLTexturePrivate::setData(int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, + QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + switch (target) { + case QOpenGLTexture::Target1D: + Q_UNUSED(layer); + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + texFuncs->glTextureSubImage1D(textureId, target, bindingTarget, mipLevel, + 0, mipLevelSize( mipLevel, dimensions[0] ), + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target1DArray: + Q_UNUSED(cubeFace); + texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, mipLevel, + 0, layer, + mipLevelSize(mipLevel, dimensions[0]), + layerCount, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target2D: + Q_UNUSED(layer); + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, mipLevel, + 0, 0, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target2DArray: + Q_UNUSED(cubeFace); + texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + 0, 0, layer, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + layerCount, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target3D: + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + 0, 0, layer, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + mipLevelSize(mipLevel, dimensions[2]), + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::TargetCubeMap: + Q_UNUSED(layer); + Q_UNUSED(layerCount); + texFuncs->glTextureSubImage2D(textureId, cubeFace, bindingTarget, mipLevel, + 0, 0, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::TargetCubeMapArray: { + int faceIndex = cubeFace - QOpenGLTexture::CubeMapPositiveX; + int layerFace = 6 * layer + faceIndex; + texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + 0, 0, layerFace, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + layerCount, + sourceFormat, sourceType, data, options); + break; + } + + case QOpenGLTexture::TargetRectangle: + Q_UNUSED(mipLevel); + Q_UNUSED(layer); + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, 0, + 0, 0, + dimensions[0], + dimensions[1], + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + case QOpenGLTexture::TargetBuffer: + // We don't upload pixel data for these targets + qWarning("QOpenGLTexture::setData(): Texture target does not support pixel data upload"); + break; + } + + // If requested perform automatic mip map generation + if (mipLevel == 0 && autoGenerateMipMaps && mipLevels > 1) { + Q_Q(QOpenGLTexture); + q->generateMipMaps(); + } +} + +void QOpenGLTexturePrivate::setData(int xOffset, int yOffset, int zOffset, int width, int height, int depth, + int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, + QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + switch (target) { + case QOpenGLTexture::Target1D: + Q_UNUSED(layer); + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + Q_UNUSED(yOffset); + Q_UNUSED(zOffset); + Q_UNUSED(height); + Q_UNUSED(depth); + texFuncs->glTextureSubImage1D(textureId, target, bindingTarget, mipLevel, + xOffset, width, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target1DArray: + Q_UNUSED(cubeFace); + Q_UNUSED(yOffset); + Q_UNUSED(zOffset); + Q_UNUSED(height); + Q_UNUSED(depth); + texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, mipLevel, + xOffset, layer, + width, + layerCount, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target2D: + Q_UNUSED(layer); + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + Q_UNUSED(zOffset); + Q_UNUSED(depth); + texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, mipLevel, + xOffset, yOffset, + width, height, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target2DArray: + Q_UNUSED(cubeFace); + Q_UNUSED(zOffset); + Q_UNUSED(depth); + texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + xOffset, yOffset, layer, + width, height, layerCount, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target3D: + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + xOffset, yOffset, zOffset, + width, height, depth, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::TargetCubeMap: + Q_UNUSED(layer); + Q_UNUSED(layerCount); + Q_UNUSED(zOffset); + Q_UNUSED(depth); + texFuncs->glTextureSubImage2D(textureId, cubeFace, bindingTarget, mipLevel, + xOffset, yOffset, + width, height, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::TargetCubeMapArray: { + Q_UNUSED(zOffset); + Q_UNUSED(depth); + int faceIndex = cubeFace - QOpenGLTexture::CubeMapPositiveX; + int layerFace = 6 * layer + faceIndex; + texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + xOffset, yOffset, layerFace, + width, height, + layerCount, + sourceFormat, sourceType, data, options); + break; + } + + case QOpenGLTexture::TargetRectangle: + Q_UNUSED(mipLevel); + Q_UNUSED(layer); + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + Q_UNUSED(zOffset); + Q_UNUSED(depth); + texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, 0, + xOffset, yOffset, + width, height, + sourceFormat, sourceType, data, options); + break; + + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + case QOpenGLTexture::TargetBuffer: + // We don't upload pixel data for these targets + qWarning("QOpenGLTexture::setData(): Texture target does not support pixel data upload"); + break; + } + + // If requested perform automatic mip map generation + if (mipLevel == 0 && autoGenerateMipMaps && mipLevels > 1) { + Q_Q(QOpenGLTexture); + q->generateMipMaps(); + } +} + + +void QOpenGLTexturePrivate::setCompressedData(int mipLevel, int layer, int layerCount, + QOpenGLTexture::CubeMapFace cubeFace, + int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options) +{ + if (!isCompressedFormat(format)) { + qWarning("Cannot set compressed data for non-compressed format 0x%x", format); + return; + } + + const bool needsFullSpec = !isUsingImmutableStorage(); // was allocateStorage() a no-op? + + switch (target) { + case QOpenGLTexture::Target1D: + Q_UNUSED(layer); + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + if (needsFullSpec) { + texFuncs->glCompressedTextureImage1D(textureId, target, bindingTarget, mipLevel, + format, + mipLevelSize(mipLevel, dimensions[0]), + 0, dataSize, data, options); + } else { + texFuncs->glCompressedTextureSubImage1D(textureId, target, bindingTarget, mipLevel, + 0, mipLevelSize( mipLevel, dimensions[0] ), + format, dataSize, data, options); + } + break; + + case QOpenGLTexture::Target1DArray: + Q_UNUSED(cubeFace); + if (!needsFullSpec) { + texFuncs->glCompressedTextureSubImage2D(textureId, target, bindingTarget, mipLevel, + 0, layer, + mipLevelSize(mipLevel, dimensions[0]), + layerCount, + format, dataSize, data, options); + } + break; + + case QOpenGLTexture::Target2D: + Q_UNUSED(layer); + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + if (needsFullSpec) { + texFuncs->glCompressedTextureImage2D(textureId, target, bindingTarget, mipLevel, + format, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + 0, dataSize, data, options); + } else { + texFuncs->glCompressedTextureSubImage2D(textureId, target, bindingTarget, mipLevel, + 0, 0, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + format, dataSize, data, options); + } + break; + + case QOpenGLTexture::Target2DArray: + Q_UNUSED(cubeFace); + if (!needsFullSpec) { + texFuncs->glCompressedTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + 0, 0, layer, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + layerCount, + format, dataSize, data, options); + } + break; + + case QOpenGLTexture::Target3D: + Q_UNUSED(cubeFace); + Q_UNUSED(layerCount); + if (needsFullSpec) { + texFuncs->glCompressedTextureImage3D(textureId, target, bindingTarget, mipLevel, + format, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + mipLevelSize(mipLevel, dimensions[2]), + 0, dataSize, data, options); + } else { + texFuncs->glCompressedTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + 0, 0, layer, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + mipLevelSize(mipLevel, dimensions[2]), + format, dataSize, data, options); + } + break; + + case QOpenGLTexture::TargetCubeMap: + Q_UNUSED(layer); + Q_UNUSED(layerCount); + if (needsFullSpec) { + texFuncs->glCompressedTextureImage2D(textureId, cubeFace, bindingTarget, mipLevel, + format, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + 0, dataSize, data, options); + } else { + texFuncs->glCompressedTextureSubImage2D(textureId, cubeFace, bindingTarget, mipLevel, + 0, 0, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + format, dataSize, data, options); + } + break; + + case QOpenGLTexture::TargetCubeMapArray: { + int faceIndex = cubeFace - QOpenGLTexture::CubeMapPositiveX; + int layerFace = 6 * layer + faceIndex; + if (!needsFullSpec) { + texFuncs->glCompressedTextureSubImage3D(textureId, target, bindingTarget, mipLevel, + 0, 0, layerFace, + mipLevelSize(mipLevel, dimensions[0]), + mipLevelSize(mipLevel, dimensions[1]), + layerCount, + format, dataSize, data, options); + } + break; + } + + case QOpenGLTexture::TargetRectangle: + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + case QOpenGLTexture::TargetBuffer: + // We don't upload pixel data for these targets + qWarning("QOpenGLTexture::setCompressedData(): Texture target does not support pixel data upload"); + break; + } + + // If requested perform automatic mip map generation + if (mipLevel == 0 && autoGenerateMipMaps && mipLevels > 1) { + Q_Q(QOpenGLTexture); + q->generateMipMaps(); + } +} + +void QOpenGLTexturePrivate::setWrapMode(QOpenGLTexture::WrapMode mode) +{ + switch (target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::TargetBuffer: + wrapModes[0] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode); + break; + + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + case QOpenGLTexture::TargetRectangle: + wrapModes[0] = wrapModes[1] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode); + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_T, mode); + break; + + case QOpenGLTexture::Target3D: + wrapModes[0] = wrapModes[1] = wrapModes[2] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode); + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_T, mode); + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_R, mode); + break; + } +} + +void QOpenGLTexturePrivate::setWrapMode(QOpenGLTexture::CoordinateDirection direction, QOpenGLTexture::WrapMode mode) +{ + switch (target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::TargetBuffer: + switch (direction) { + case QOpenGLTexture::DirectionS: + wrapModes[0] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode); + break; + + case QOpenGLTexture::DirectionT: + case QOpenGLTexture::DirectionR: + qWarning("QOpenGLTexture::setWrapMode() direction not valid for this texture target"); + break; + } + break; + + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + case QOpenGLTexture::TargetRectangle: + switch (direction) { + case QOpenGLTexture::DirectionS: + wrapModes[0] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode); + break; + + case QOpenGLTexture::DirectionT: + wrapModes[1] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_T, mode); + break; + + case QOpenGLTexture::DirectionR: + qWarning("QOpenGLTexture::setWrapMode() direction not valid for this texture target"); + break; + } + break; + + case QOpenGLTexture::Target3D: + switch (direction) { + case QOpenGLTexture::DirectionS: + wrapModes[0] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, direction, mode); + break; + + case QOpenGLTexture::DirectionT: + wrapModes[1] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, direction, mode); + break; + + case QOpenGLTexture::DirectionR: + wrapModes[2] = mode; + texFuncs->glTextureParameteri(textureId, target, bindingTarget, direction, mode); + break; + } + break; + } +} + +QOpenGLTexture::WrapMode QOpenGLTexturePrivate::wrapMode(QOpenGLTexture::CoordinateDirection direction) const +{ + switch (target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::TargetBuffer: + switch (direction) { + case QOpenGLTexture::DirectionS: + return wrapModes[0]; + + case QOpenGLTexture::DirectionT: + case QOpenGLTexture::DirectionR: + qWarning("QOpenGLTexture::wrapMode() direction not valid for this texture target"); + return QOpenGLTexture::Repeat; + } + break; + + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + case QOpenGLTexture::TargetRectangle: + switch (direction) { + case QOpenGLTexture::DirectionS: + return wrapModes[0]; + + case QOpenGLTexture::DirectionT: + return wrapModes[1]; + + case QOpenGLTexture::DirectionR: + qWarning("QOpenGLTexture::wrapMode() direction not valid for this texture target"); + return QOpenGLTexture::Repeat; + } + break; + + case QOpenGLTexture::Target3D: + switch (direction) { + case QOpenGLTexture::DirectionS: + return wrapModes[0]; + + case QOpenGLTexture::DirectionT: + return wrapModes[1]; + + case QOpenGLTexture::DirectionR: + return wrapModes[2]; + } + break; + } + // Should never get here + Q_ASSERT(false); + return QOpenGLTexture::Repeat; +} + +QOpenGLTexture *QOpenGLTexturePrivate::createTextureView(QOpenGLTexture::Target viewTarget, + QOpenGLTexture::TextureFormat viewFormat, + int minimumMipmapLevel, int maximumMipmapLevel, + int minimumLayer, int maximumLayer) const +{ + // Do sanity checks - see http://www.opengl.org/wiki/GLAPI/glTextureView + + // Check the targets are compatible + bool viewTargetCompatible = false; + switch (target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + viewTargetCompatible = (viewTarget == QOpenGLTexture::Target1D + || viewTarget == QOpenGLTexture::Target1DArray); + break; + + + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + viewTargetCompatible = (viewTarget == QOpenGLTexture::Target2D + || viewTarget == QOpenGLTexture::Target2DArray); + break; + + case QOpenGLTexture::Target3D: + viewTargetCompatible = (viewTarget == QOpenGLTexture::Target3D); + break; + + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + viewTargetCompatible = (viewTarget == QOpenGLTexture::TargetCubeMap + || viewTarget == QOpenGLTexture::Target2D + || viewTarget == QOpenGLTexture::Target2DArray + || viewTarget == QOpenGLTexture::TargetCubeMapArray); + break; + + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + viewTargetCompatible = (viewTarget == QOpenGLTexture::Target2DMultisample + || viewTarget == QOpenGLTexture::Target2DMultisampleArray); + break; + + case QOpenGLTexture::TargetRectangle: + viewTargetCompatible = (viewTarget == QOpenGLTexture::TargetRectangle); + break; + + case QOpenGLTexture::TargetBuffer: + // Cannot be used with texture views + break; + } + + if (!viewTargetCompatible) { + qWarning("QOpenGLTexture::createTextureView(): Incompatible source and view targets"); + return nullptr; + } + + // Check the formats are compatible + bool viewFormatCompatible = false; + switch (formatClass) { + case QOpenGLTexture::NoFormatClass: + break; + + case QOpenGLTexture::FormatClass_128Bit: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA32F + || viewFormat == QOpenGLTexture::RGBA32U + || viewFormat == QOpenGLTexture::RGBA32I); + break; + + case QOpenGLTexture::FormatClass_96Bit: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB32F + || viewFormat == QOpenGLTexture::RGB32U + || viewFormat == QOpenGLTexture::RGB32I); + break; + + case QOpenGLTexture::FormatClass_64Bit: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA16F + || viewFormat == QOpenGLTexture::RG32F + || viewFormat == QOpenGLTexture::RGBA16U + || viewFormat == QOpenGLTexture::RG32U + || viewFormat == QOpenGLTexture::RGBA16I + || viewFormat == QOpenGLTexture::RG32I + || viewFormat == QOpenGLTexture::RGBA16_UNorm + || viewFormat == QOpenGLTexture::RGBA16_SNorm); + break; + + case QOpenGLTexture::FormatClass_48Bit: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB16_UNorm + || viewFormat == QOpenGLTexture::RGB16_SNorm + || viewFormat == QOpenGLTexture::RGB16F + || viewFormat == QOpenGLTexture::RGB16U + || viewFormat == QOpenGLTexture::RGB16I); + break; + + case QOpenGLTexture::FormatClass_32Bit: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RG16F + || viewFormat == QOpenGLTexture::RG11B10F + || viewFormat == QOpenGLTexture::R32F + || viewFormat == QOpenGLTexture::RGB10A2 + || viewFormat == QOpenGLTexture::RGBA8U + || viewFormat == QOpenGLTexture::RG16U + || viewFormat == QOpenGLTexture::R32U + || viewFormat == QOpenGLTexture::RGBA8I + || viewFormat == QOpenGLTexture::RG16I + || viewFormat == QOpenGLTexture::R32I + || viewFormat == QOpenGLTexture::RGBA8_UNorm + || viewFormat == QOpenGLTexture::RG16_UNorm + || viewFormat == QOpenGLTexture::RGBA8_SNorm + || viewFormat == QOpenGLTexture::RG16_SNorm + || viewFormat == QOpenGLTexture::SRGB8_Alpha8 + || viewFormat == QOpenGLTexture::RGB9E5); + break; + + case QOpenGLTexture::FormatClass_24Bit: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB8_UNorm + || viewFormat == QOpenGLTexture::RGB8_SNorm + || viewFormat == QOpenGLTexture::SRGB8 + || viewFormat == QOpenGLTexture::RGB8U + || viewFormat == QOpenGLTexture::RGB8I); + break; + + case QOpenGLTexture::FormatClass_16Bit: + viewFormatCompatible = (viewFormat == QOpenGLTexture::R16F + || viewFormat == QOpenGLTexture::RG8U + || viewFormat == QOpenGLTexture::R16U + || viewFormat == QOpenGLTexture::RG8I + || viewFormat == QOpenGLTexture::R16I + || viewFormat == QOpenGLTexture::RG8_UNorm + || viewFormat == QOpenGLTexture::R16_UNorm + || viewFormat == QOpenGLTexture::RG8_SNorm + || viewFormat == QOpenGLTexture::R16_SNorm); + break; + + case QOpenGLTexture::FormatClass_8Bit: + viewFormatCompatible = (viewFormat == QOpenGLTexture::R8U + || viewFormat == QOpenGLTexture::R8I + || viewFormat == QOpenGLTexture::R8_UNorm + || viewFormat == QOpenGLTexture::R8_SNorm); + break; + + case QOpenGLTexture::FormatClass_RGTC1_R: + viewFormatCompatible = (viewFormat == QOpenGLTexture::R_ATI1N_UNorm + || viewFormat == QOpenGLTexture::R_ATI1N_SNorm); + break; + + case QOpenGLTexture::FormatClass_RGTC2_RG: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RG_ATI2N_UNorm + || viewFormat == QOpenGLTexture::RG_ATI2N_SNorm); + break; + + case QOpenGLTexture::FormatClass_BPTC_Unorm: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB_BP_UNorm + || viewFormat == QOpenGLTexture::SRGB_BP_UNorm); + break; + + case QOpenGLTexture::FormatClass_BPTC_Float: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT + || viewFormat == QOpenGLTexture::RGB_BP_SIGNED_FLOAT); + break; + + case QOpenGLTexture::FormatClass_S3TC_DXT1_RGB: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB_DXT1 + || viewFormat == QOpenGLTexture::SRGB_DXT1); + break; + + case QOpenGLTexture::FormatClass_S3TC_DXT1_RGBA: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA_DXT1 + || viewFormat == QOpenGLTexture::SRGB_Alpha_DXT1); + break; + + case QOpenGLTexture::FormatClass_S3TC_DXT3_RGBA: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA_DXT3 + || viewFormat == QOpenGLTexture::SRGB_Alpha_DXT3); + break; + + case QOpenGLTexture::FormatClass_S3TC_DXT5_RGBA: + viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA_DXT5 + || viewFormat == QOpenGLTexture::SRGB_Alpha_DXT5); + break; + + case QOpenGLTexture::FormatClass_Unique: + viewFormatCompatible = (viewFormat == format); + break; + } + + if (!viewFormatCompatible) { + qWarning("QOpenGLTexture::createTextureView(): Incompatible source and view formats"); + return nullptr; + } + + + // Create a view + QOpenGLTexture *view = new QOpenGLTexture(viewTarget); + view->setFormat(viewFormat); + view->create(); + view->d_ptr->textureView = true; + texFuncs->glTextureView(view->textureId(), viewTarget, textureId, viewFormat, + minimumMipmapLevel, maximumMipmapLevel - minimumMipmapLevel + 1, + minimumLayer, maximumLayer - minimumLayer + 1); + return view; +} + + +/*! + \class QOpenGLTexture + \inmodule QtGui + \since 5.2 + \wrapper + \brief The QOpenGLTexture class encapsulates an OpenGL texture object. + + QOpenGLTexture makes it easy to work with OpenGL textures and the myriad features + and targets that they offer depending upon the capabilities of your OpenGL implementation. + + The typical usage pattern for QOpenGLTexture is + \list + \li Instantiate the object specifying the texture target type + \li Set properties that affect the storage requirements e.g. storage format, dimensions + \li Allocate the server-side storage + \li Optionally upload pixel data + \li Optionally set any additional properties e.g. filtering and border options + \li Render with texture or render to texture + \endlist + + In the common case of simply using a QImage as the source of texture pixel data + most of the above steps are performed automatically. + + \code + // Prepare texture + QOpenGLTexture *texture = new QOpenGLTexture(QImage(fileName).mirrored()); + texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear); + texture->setMagnificationFilter(QOpenGLTexture::Linear); + ... + // Render with texture + texture->bind(); + glDrawArrays(...); + \endcode + + Note that the QImage is mirrored vertically to account for the fact that + OpenGL and QImage use opposite directions for the y axis. Another option + would be to transform your texture coordinates. +*/ + +/*! + \enum QOpenGLTexture::Filter + This enum defines the filtering parameters for a QOpenGLTexture object. + \value Nearest Equivalent to GL_NEAREST + \value Linear Equivalent to GL_LINEAR + \value NearestMipMapNearest Equivalent to GL_NEAREST_MIPMAP_NEAREST + \value NearestMipMapLinear Equivalent to GL_NEAREST_MIPMAP_LINEAR + \value LinearMipMapNearest Equivalent to GL_LINEAR_MIPMAP_NEAREST + \value LinearMipMapLinear Equivalent to GL_LINEAR_MIPMAP_LINEAR +*/ + +/*! + \enum QOpenGLTexture::Target + This enum defines the texture target of a QOpenGLTexture object. + + \value Target1D A 1-dimensional texture. + Equivalent to GL_TEXTURE_1D. + \value Target1DArray An array of 1-dimensional textures. + Equivalent to GL_TEXTURE_1D_ARRAY + \value Target2D A 2-dimensional texture. + Equivalent to GL_TEXTURE_2D + \value Target2DArray An array of 1-dimensional textures. + Equivalent to GL_TEXTURE_2D_ARRAY + \value Target3D A 3-dimensional texture. + Equivalent to GL_TEXTURE_3D + \value TargetCubeMap A cubemap texture. + Equivalent to GL_TEXTURE_CUBE_MAP + \value TargetCubeMapArray An array of cubemap textures. + Equivalent to GL_TEXTURE_CUBE_MAP_ARRAY + \value Target2DMultisample A 2-dimensional texture with multisample support. + Equivalent to GL_TEXTURE_2D_MULTISAMPLE + \value Target2DMultisampleArray An array of 2-dimensional textures with multisample support. + Equivalent to GL_TEXTURE_2D_MULTISAMPLE_ARRAY + \value TargetRectangle A rectangular 2-dimensional texture. + Equivalent to GL_TEXTURE_RECTANGLE + \value TargetBuffer A texture with data from an OpenGL buffer object. + Equivalent to GL_TEXTURE_BUFFER +*/ + +/*! + \enum QOpenGLTexture::BindingTarget + This enum defines the possible binding targets of texture units. + + \value BindingTarget1D Equivalent to GL_TEXTURE_BINDING_1D + \value BindingTarget1DArray Equivalent to GL_TEXTURE_BINDING_1D_ARRAY + \value BindingTarget2D Equivalent to GL_TEXTURE_BINDING_2D + \value BindingTarget2DArray Equivalent to GL_TEXTURE_BINDING_2D_ARRAY + \value BindingTarget3D Equivalent to GL_TEXTURE_BINDING_3D + \value BindingTargetCubeMap Equivalent to GL_TEXTURE_BINDING_CUBE_MAP + \value BindingTargetCubeMapArray Equivalent to GL_TEXTURE_BINDING_CUBE_MAP_ARRAY + \value BindingTarget2DMultisample Equivalent to GL_TEXTURE_BINDING_2D_MULTISAMPLE + \value BindingTarget2DMultisampleArray Equivalent to GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY + \value BindingTargetRectangle Equivalent to GL_TEXTURE_BINDING_RECTANGLE + \value BindingTargetBuffer Equivalent to GL_TEXTURE_BINDING_BUFFER +*/ + +/*! + \enum QOpenGLTexture::MipMapGeneration + This enum defines the options to control mipmap generation. + + \value GenerateMipMaps Mipmaps should be generated + \value DontGenerateMipMaps Mipmaps should not be generated +*/ + +/*! + \enum QOpenGLTexture::TextureUnitReset + This enum defines options ot control texture unit activation. + + \value ResetTextureUnit The previous active texture unit will be reset + \value DontResetTextureUnit The previous active texture unit will not be rest +*/ + +/*! + \enum QOpenGLTexture::TextureFormat + This enum defines the possible texture formats. Depending upon your OpenGL + implementation only a subset of these may be supported. + + \value NoFormat Equivalent to GL_NONE + + \value R8_UNorm Equivalent to GL_R8 + \value RG8_UNorm Equivalent to GL_RG8 + \value RGB8_UNorm Equivalent to GL_RGB8 + \value RGBA8_UNorm Equivalent to GL_RGBA8 + + \value R16_UNorm Equivalent to GL_R16 + \value RG16_UNorm Equivalent to GL_RG16 + \value RGB16_UNorm Equivalent to GL_RGB16 + \value RGBA16_UNorm Equivalent to GL_RGBA16 + + \value R8_SNorm Equivalent to GL_R8_SNORM + \value RG8_SNorm Equivalent to GL_RG8_SNORM + \value RGB8_SNorm Equivalent to GL_RGB8_SNORM + \value RGBA8_SNorm Equivalent to GL_RGBA8_SNORM + + \value R16_SNorm Equivalent to GL_R16_SNORM + \value RG16_SNorm Equivalent to GL_RG16_SNORM + \value RGB16_SNorm Equivalent to GL_RGB16_SNORM + \value RGBA16_SNorm Equivalent to GL_RGBA16_SNORM + + \value R8U Equivalent to GL_R8UI + \value RG8U Equivalent to GL_RG8UI + \value RGB8U Equivalent to GL_RGB8UI + \value RGBA8U Equivalent to GL_RGBA8UI + + \value R16U Equivalent to GL_R16UI + \value RG16U Equivalent to GL_RG16UI + \value RGB16U Equivalent to GL_RGB16UI + \value RGBA16U Equivalent to GL_RGBA16UI + + \value R32U Equivalent to GL_R32UI + \value RG32U Equivalent to GL_RG32UI + \value RGB32U Equivalent to GL_RGB32UI + \value RGBA32U Equivalent to GL_RGBA32UI + + \value R8I Equivalent to GL_R8I + \value RG8I Equivalent to GL_RG8I + \value RGB8I Equivalent to GL_RGB8I + \value RGBA8I Equivalent to GL_RGBA8I + + \value R16I Equivalent to GL_R16I + \value RG16I Equivalent to GL_RG16I + \value RGB16I Equivalent to GL_RGB16I + \value RGBA16I Equivalent to GL_RGBA16I + + \value R32I Equivalent to GL_R32I + \value RG32I Equivalent to GL_RG32I + \value RGB32I Equivalent to GL_RGB32I + \value RGBA32I Equivalent to GL_RGBA32I + + \value R16F Equivalent to GL_R16F + \value RG16F Equivalent to GL_RG16F + \value RGB16F Equivalent to GL_RGB16F + \value RGBA16F Equivalent to GL_RGBA16F + + \value R32F Equivalent to GL_R32F + \value RG32F Equivalent to GL_RG32F + \value RGB32F Equivalent to GL_RGB32F + \value RGBA32F Equivalent to GL_RGBA32F + + \value RGB9E5 Equivalent to GL_RGB9_E5 + \value RG11B10F Equivalent to GL_R11F_G11F_B10F + \value RG3B2 Equivalent to GL_R3_G3_B2 + \value R5G6B5 Equivalent to GL_RGB565 + \value RGB5A1 Equivalent to GL_RGB5_A1 + \value RGBA4 Equivalent to GL_RGBA4 + \value RGB10A2 Equivalent to GL_RGB10_A2UI + + \value D16 Equivalent to GL_DEPTH_COMPONENT16 + \value D24 Equivalent to GL_DEPTH_COMPONENT24 + \value D24S8 Equivalent to GL_DEPTH24_STENCIL8 + \value D32 Equivalent to GL_DEPTH_COMPONENT32 + \value D32F Equivalent to GL_DEPTH_COMPONENT32F + \value D32FS8X24 Equivalent to GL_DEPTH32F_STENCIL8 + \value S8 Equivalent to GL_STENCIL_INDEX8. Introduced in Qt 5.4 + + \value RGB_DXT1 Equivalent to GL_COMPRESSED_RGB_S3TC_DXT1_EXT + \value RGBA_DXT1 Equivalent to GL_COMPRESSED_RGBA_S3TC_DXT1_EXT + \value RGBA_DXT3 Equivalent to GL_COMPRESSED_RGBA_S3TC_DXT3_EXT + \value RGBA_DXT5 Equivalent to GL_COMPRESSED_RGBA_S3TC_DXT5_EXT + \value R_ATI1N_UNorm Equivalent to GL_COMPRESSED_RED_RGTC1 + \value R_ATI1N_SNorm Equivalent to GL_COMPRESSED_SIGNED_RED_RGTC1 + \value RG_ATI2N_UNorm Equivalent to GL_COMPRESSED_RG_RGTC2 + \value RG_ATI2N_SNorm Equivalent to GL_COMPRESSED_SIGNED_RG_RGTC2 + \value RGB_BP_UNSIGNED_FLOAT Equivalent to GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB + \value RGB_BP_SIGNED_FLOAT Equivalent to GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB + \value RGB_BP_UNorm Equivalent to GL_COMPRESSED_RGBA_BPTC_UNORM_ARB + \value R11_EAC_UNorm Equivalent to GL_COMPRESSED_R11_EAC + \value R11_EAC_SNorm Equivalent to GL_COMPRESSED_SIGNED_R11_EAC + \value RG11_EAC_UNorm Equivalent to GL_COMPRESSED_RG11_EAC + \value RG11_EAC_SNorm Equivalent to GL_COMPRESSED_SIGNED_RG11_EAC + \value RGB8_ETC2 Equivalent to GL_COMPRESSED_RGB8_ETC2 + \value SRGB8_ETC2 Equivalent to GL_COMPRESSED_SRGB8_ETC2 + \value RGB8_PunchThrough_Alpha1_ETC2 Equivalent to GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 + \value SRGB8_PunchThrough_Alpha1_ETC2 Equivalent to GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 + \value RGBA8_ETC2_EAC Equivalent to GL_COMPRESSED_RGBA8_ETC2_EAC + \value SRGB8_Alpha8_ETC2_EAC Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC + \value RGB8_ETC1 Equivalent to GL_ETC1_RGB8_OES + \value RGBA_ASTC_4x4 Equivalent to GL_COMPRESSED_RGBA_ASTC_4x4_KHR + \value RGBA_ASTC_5x4 Equivalent to GL_COMPRESSED_RGBA_ASTC_5x4_KHR + \value RGBA_ASTC_5x5 Equivalent to GL_COMPRESSED_RGBA_ASTC_5x5_KHR + \value RGBA_ASTC_6x5 Equivalent to GL_COMPRESSED_RGBA_ASTC_6x5_KHR + \value RGBA_ASTC_6x6 Equivalent to GL_COMPRESSED_RGBA_ASTC_6x6_KHR + \value RGBA_ASTC_8x5 Equivalent to GL_COMPRESSED_RGBA_ASTC_8x5_KHR + \value RGBA_ASTC_8x6 Equivalent to GL_COMPRESSED_RGBA_ASTC_8x6_KHR + \value RGBA_ASTC_8x8 Equivalent to GL_COMPRESSED_RGBA_ASTC_8x8_KHR + \value RGBA_ASTC_10x5 Equivalent to GL_COMPRESSED_RGBA_ASTC_10x5_KHR + \value RGBA_ASTC_10x6 Equivalent to GL_COMPRESSED_RGBA_ASTC_10x6_KHR + \value RGBA_ASTC_10x8 Equivalent to GL_COMPRESSED_RGBA_ASTC_10x8_KHR + \value RGBA_ASTC_10x10 Equivalent to GL_COMPRESSED_RGBA_ASTC_10x10_KHR + \value RGBA_ASTC_12x10 Equivalent to GL_COMPRESSED_RGBA_ASTC_12x10_KHR + \value RGBA_ASTC_12x12 Equivalent to GL_COMPRESSED_RGBA_ASTC_12x12_KHR + \value SRGB8_Alpha8_ASTC_4x4 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR + \value SRGB8_Alpha8_ASTC_5x4 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR + \value SRGB8_Alpha8_ASTC_5x5 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR + \value SRGB8_Alpha8_ASTC_6x5 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR + \value SRGB8_Alpha8_ASTC_6x6 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR + \value SRGB8_Alpha8_ASTC_8x5 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR + \value SRGB8_Alpha8_ASTC_8x6 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR + \value SRGB8_Alpha8_ASTC_8x8 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR + \value SRGB8_Alpha8_ASTC_10x5 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR + \value SRGB8_Alpha8_ASTC_10x6 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR + \value SRGB8_Alpha8_ASTC_10x8 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR + \value SRGB8_Alpha8_ASTC_10x10 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR + \value SRGB8_Alpha8_ASTC_12x10 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR + \value SRGB8_Alpha8_ASTC_12x12 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR + + \value SRGB8 Equivalent to GL_SRGB8 + \value SRGB8_Alpha8 Equivalent to GL_SRGB8_ALPHA8 + \value SRGB_DXT1 Equivalent to GL_COMPRESSED_SRGB_S3TC_DXT1_EXT + \value SRGB_Alpha_DXT1 Equivalent to GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT + \value SRGB_Alpha_DXT3 Equivalent to GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT + \value SRGB_Alpha_DXT5 Equivalent to GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT + \value SRGB_BP_UNorm Equivalent to GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB + + \value DepthFormat Equivalent to GL_DEPTH_COMPONENT (only OpenGL ES 3 or ES 2 with OES_depth_texture) + \value AlphaFormat Equivalent to GL_ALPHA (OpenGL ES 2 only) + \value RGBFormat Equivalent to GL_RGB (OpenGL ES 2 only) + \value RGBAFormat Equivalent to GL_RGBA (OpenGL ES 2 only) + \value LuminanceFormat Equivalent to GL_LUMINANCE (OpenGL ES 2 only) + \value LuminanceAlphaFormat Equivalent to GL_LUMINANCE_ALPHA (OpenGL ES 2 only) +*/ + +/*! + \enum QOpenGLTexture::CubeMapFace + This enum defines the possible CubeMap faces. + + \value CubeMapPositiveX Equivalent to GL_TEXTURE_CUBE_MAP_POSITIVE_X + \value CubeMapNegativeX Equivalent to GL_TEXTURE_CUBE_MAP_NEGATIVE_X + \value CubeMapPositiveY Equivalent to GL_TEXTURE_CUBE_MAP_POSITIVE_Y + \value CubeMapNegativeY Equivalent to GL_TEXTURE_CUBE_MAP_NEGATIVE_Y + \value CubeMapPositiveZ Equivalent to GL_TEXTURE_CUBE_MAP_POSITIVE_Z + \value CubeMapNegativeZ Equivalent to GL_TEXTURE_CUBE_MAP_NEGATIVE_Z +*/ + +/*! + \enum QOpenGLTexture::PixelFormat + This enum defines the possible client-side pixel formats for a pixel + transfer operation. + + \value NoSourceFormat Equivalent to GL_NONE + \value Red Equivalent to GL_RED + \value RG Equivalent to GL_RG + \value RGB Equivalent to GL_RGB + \value BGR Equivalent to GL_BGR + \value RGBA Equivalent to GL_RGBA + \value BGRA Equivalent to GL_BGRA + \value Red_Integer Equivalent to GL_RED_INTEGER + \value RG_Integer Equivalent to GL_RG_INTEGER + \value RGB_Integer Equivalent to GL_RGB_INTEGER + \value BGR_Integer Equivalent to GL_BGR_INTEGER + \value RGBA_Integer Equivalent to GL_RGBA_INTEGER + \value BGRA_Integer Equivalent to GL_BGRA_INTEGER + \value Stencil Equivalent to GL_STENCIL_INDEX. Introduced in Qt 5.4 + \value Depth Equivalent to GL_DEPTH_COMPONENT + \value DepthStencil Equivalent to GL_DEPTH_STENCIL + \value Alpha Equivalent to GL_ALPHA (OpenGL ES 2 only) + \value Luminance Equivalent to GL_LUMINANCE (OpenGL ES 2 only) + \value LuminanceAlpha Equivalent to GL_LUMINANCE_ALPHA (OpenGL ES 2 only) + +*/ + +/*! + \enum QOpenGLTexture::PixelType + This enum defines the possible pixel data types for a pixel transfer operation + + \value NoPixelType Equivalent to GL_NONE + \value Int8 Equivalent to GL_BYTE + \value UInt8 Equivalent to GL_UNSIGNED_BYTE + \value Int16 Equivalent to GL_SHORT + \value UInt16 Equivalent to GL_UNSIGNED_SHORT + \value Int32 Equivalent to GL_INT + \value UInt32 Equivalent to GL_UNSIGNED_INT + \value Float16 Equivalent to GL_HALF_FLOAT + \value Float16OES Equivalent to GL_HALF_FLOAT_OES + \value Float32 Equivalent to GL_FLOAT + \value UInt32_RGB9_E5 Equivalent to GL_UNSIGNED_INT_5_9_9_9_REV + \value UInt32_RG11B10F Equivalent to GL_UNSIGNED_INT_10F_11F_11F_REV + \value UInt8_RG3B2 Equivalent to GL_UNSIGNED_BYTE_3_3_2 + \value UInt8_RG3B2_Rev Equivalent to GL_UNSIGNED_BYTE_2_3_3_REV + \value UInt16_RGB5A1 Equivalent to GL_UNSIGNED_SHORT_5_5_5_1 + \value UInt16_RGB5A1_Rev Equivalent to GL_UNSIGNED_SHORT_1_5_5_5_REV + \value UInt16_R5G6B5 Equivalent to GL_UNSIGNED_SHORT_5_6_5 + \value UInt16_R5G6B5_Rev Equivalent to GL_UNSIGNED_SHORT_5_6_5_REV + \value UInt16_RGBA4 Equivalent to GL_UNSIGNED_SHORT_4_4_4_4 + \value UInt16_RGBA4_Rev Equivalent to GL_UNSIGNED_SHORT_4_4_4_4_REV + \value UInt32_RGBA8 Equivalent to GL_UNSIGNED_INT_8_8_8_8 + \value UInt32_RGBA8_Rev Equivalent to GL_UNSIGNED_INT_8_8_8_8_REV + \value UInt32_RGB10A2 Equivalent to GL_UNSIGNED_INT_10_10_10_2 + \value UInt32_RGB10A2_Rev Equivalent to GL_UNSIGNED_INT_2_10_10_10_REV + \value UInt32_D24S8 Equivalent to GL_UNSIGNED_INT_24_8. Introduced in Qt 5.4 + \value Float32_D32_UInt32_S8_X24 Equivalent to GL_FLOAT_32_UNSIGNED_INT_24_8_REV. Introduced in Qt 5.4 +*/ + +/*! + \enum QOpenGLTexture::Feature + This enum defines the OpenGL texture-related features that can be tested for. + + \value ImmutableStorage Support for immutable texture storage + \value ImmutableMultisampleStorage Support for immutable texture storage with + multisample targets + \value TextureRectangle Support for the GL_TEXTURE_RECTANGLE target + \value TextureArrays Support for texture targets with array layers + \value Texture3D Support for the 3 dimensional texture target + \value TextureMultisample Support for texture targets that have multisample capabilities + \value TextureBuffer Support for textures that use OpenGL buffer objects + as their data source + \value TextureCubeMapArrays Support for cubemap array texture target + \value Swizzle Support for texture component swizzle masks + \value StencilTexturing Support for stencil texturing (i.e. looking up depth or stencil + components of a combined depth/stencil format texture in GLSL shaders). + \value AnisotropicFiltering Support for anisotropic texture filtering + \value NPOTTextures Basic support for non-power-of-two textures + \value NPOTTextureRepeat Full support for non-power-of-two textures including texture + repeat modes + \value Texture1D Support for the 1 dimensional texture target + \value TextureComparisonOperators Support for texture comparison operators + \value TextureMipMapLevel Support for setting the base and maximum mipmap levels +*/ + +/*! + \enum QOpenGLTexture::SwizzleComponent + This enum defines the texture color components that can be assigned a swizzle mask. + + \value SwizzleRed The red component. Equivalent to GL_TEXTURE_SWIZZLE_R + \value SwizzleGreen The green component. Equivalent to GL_TEXTURE_SWIZZLE_G + \value SwizzleBlue The blue component. Equivalent to GL_TEXTURE_SWIZZLE_B + \value SwizzleAlpha The alpha component. Equivalent to GL_TEXTURE_SWIZZLE_A +*/ + +/*! + \enum QOpenGLTexture::SwizzleValue + This enum defines the possible mask values for texture swizzling. + + \value RedValue Maps the component to the red channel. Equivalent to GL_RED + \value GreenValue Maps the component to the green channel. Equivalent to GL_GREEN + \value BlueValue Maps the component to the blue channel. Equivalent to GL_BLUE + \value AlphaValue Maps the component to the alpha channel. Equivalent to GL_ALPHA + \value ZeroValue Maps the component to a fixed value of 0. Equivalent to GL_ZERO + \value OneValue Maps the component to a fixed value of 1. Equivalent to GL_ONE +*/ + +/*! + \enum QOpenGLTexture::WrapMode + This enum defines the possible texture coordinate wrapping modes. + + \value Repeat Texture coordinate is repeated. Equivalent to GL_REPEAT + \value MirroredRepeat Texture coordinate is reflected about 0 and 1. Equivalent to GL_MIRRORED_REPEAT + \value ClampToEdge Clamps the texture coordinates to [0,1]. Equivalent to GL_CLAMP_TO_EDGE + \value ClampToBorder As for ClampToEdge but also blends samples at 0 and 1 with a + fixed border color. Equivalent to GL_CLAMP_TO_BORDER +*/ + +/*! + \enum QOpenGLTexture::CoordinateDirection + This enum defines the possible texture coordinate directions + + \value DirectionS The horizontal direction. Equivalent to GL_TEXTURE_WRAP_S + \value DirectionT The vertical direction. Equivalent to GL_TEXTURE_WRAP_T + \value DirectionR The depth direction. Equivalent to GL_TEXTURE_WRAP_R +*/ + +/*! + Creates a QOpenGLTexture object that can later be bound to \a target. + + This does not create the underlying OpenGL texture object. Therefore, + construction using this constructor does not require a valid current + OpenGL context. +*/ +QOpenGLTexture::QOpenGLTexture(Target target) + : d_ptr(new QOpenGLTexturePrivate(target, this)) +{ +} + +/*! + Creates a QOpenGLTexture object that can later be bound to the 2D texture + target and contains the pixel data contained in \a image. If you wish + to have a chain of mipmaps generated then set \a genMipMaps to \c true (this + is the default). + + This does create the underlying OpenGL texture object. Therefore, + construction using this constructor does require a valid current + OpenGL context. +*/ +QOpenGLTexture::QOpenGLTexture(const QImage& image, MipMapGeneration genMipMaps) + : QOpenGLTexture(QOpenGLTexture::Target2D) +{ + setData(image, genMipMaps); +} + +QOpenGLTexture::~QOpenGLTexture() +{ +} + +/*! + Returns the binding target of this texture. + + \since 5.4 +*/ +QOpenGLTexture::Target QOpenGLTexture::target() const +{ + Q_D(const QOpenGLTexture); + return d->target; +} + +/*! + Creates the underlying OpenGL texture object. This requires a current valid + OpenGL context. If the texture object already exists, this function does + nothing. + + Once the texture object is created you can obtain the object + name from the textureId() function. This may be useful if you wish to make + some raw OpenGL calls related to this texture. + + Normally it should not be necessary to call this function directly as all + functions that set properties of the texture object implicitly call create() + on your behalf. + + Returns \c true if the creation succeeded, otherwise returns \c false. + + \sa destroy(), isCreated(), textureId() +*/ +bool QOpenGLTexture::create() +{ + Q_D(QOpenGLTexture); + return d->create(); +} + +/*! + Destroys the underlying OpenGL texture object. This requires a current valid + OpenGL context. + + \sa create(), isCreated(), textureId() +*/ +void QOpenGLTexture::destroy() +{ + Q_D(QOpenGLTexture); + return d->destroy(); +} + +/*! + Returns \c true if the underlying OpenGL texture object has been created. + + \sa create(), destroy(), textureId() +*/ +bool QOpenGLTexture::isCreated() const +{ + Q_D(const QOpenGLTexture); + return d->textureId != 0; +} + +/*! + Returns the name of the underlying OpenGL texture object or 0 if it has + not yet been created. + + \sa create(), destroy(), isCreated() +*/ +GLuint QOpenGLTexture::textureId() const +{ + Q_D(const QOpenGLTexture); + return d->textureId; +} + +/*! + Binds this texture to the currently active texture unit ready for + rendering. Note that you do not need to bind QOpenGLTexture objects + in order to modify them as the implementation makes use of the + EXT_direct_state_access extension where available and simulates it + where it is not. + + \sa release() +*/ +void QOpenGLTexture::bind() +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->bind(); +} + +/*! + Binds this texture to texture unit \a unit ready for + rendering. Note that you do not need to bind QOpenGLTexture objects + in order to modify them as the implementation makes use of the + EXT_direct_state_access extension where available and simulates it + where it is not. + + If parameter \a reset is \c true then this function will restore + the active unit to the texture unit that was active upon entry. + + \sa release() +*/ +void QOpenGLTexture::bind(uint unit, TextureUnitReset reset) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->bind(unit, reset); +} + +/*! + Unbinds this texture from the currently active texture unit. + + \sa bind() +*/ +void QOpenGLTexture::release() +{ + Q_D(QOpenGLTexture); + d->release(); +} + +/*! + Unbinds this texture from texture unit \a unit. + + If parameter \a reset is \c true then this function + will restore the active unit to the texture unit that was active + upon entry. +*/ +void QOpenGLTexture::release(uint unit, TextureUnitReset reset) +{ + Q_D(QOpenGLTexture); + d->release(unit, reset); +} + +/*! + Returns \c true if this texture is bound to the corresponding target + of the currently active texture unit. + + \sa bind(), release() +*/ +bool QOpenGLTexture::isBound() const +{ + Q_D(const QOpenGLTexture); + Q_ASSERT(d->textureId); + return d->isBound(); +} + +/*! + Returns \c true if this texture is bound to the corresponding target + of texture unit \a unit. + + \sa bind(), release() +*/ +bool QOpenGLTexture::isBound(uint unit) +{ + Q_D(const QOpenGLTexture); + Q_ASSERT(d->textureId); + return d->isBound(unit); +} + +/*! + Returns the textureId of the texture that is bound to the \a target + of the currently active texture unit. +*/ +GLuint QOpenGLTexture::boundTextureId(BindingTarget target) +{ + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (!ctx) { + qWarning("QOpenGLTexture::boundTextureId() requires a valid current context"); + return 0; + } + + GLint textureId = 0; + ctx->functions()->glGetIntegerv(target, &textureId); + return static_cast<GLuint>(textureId); +} + +/*! + Returns the textureId of the texture that is bound to the \a target + of the texture unit \a unit. +*/ +GLuint QOpenGLTexture::boundTextureId(uint unit, BindingTarget target) +{ + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (!ctx) { + qWarning("QOpenGLTexture::boundTextureId() requires a valid current context"); + return 0; + } + + QOpenGLFunctions *funcs = ctx->functions(); + funcs->initializeOpenGLFunctions(); + + GLint oldTextureUnit = 0; + funcs->glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit); + + funcs->glActiveTexture(unit); + GLint textureId = 0; + funcs->glGetIntegerv(target, &textureId); + funcs->glActiveTexture(oldTextureUnit); + + return static_cast<GLuint>(textureId); +} + +/*! + Sets the format of this texture object to \a format. This function + must be called before texture storage is allocated. + + Note that all formats may not be supported. The exact set of supported + formats is dependent upon your OpenGL implementation and version. + + \sa format(), allocateStorage() +*/ +void QOpenGLTexture::setFormat(TextureFormat format) +{ + Q_D(QOpenGLTexture); + d->create(); + if (isStorageAllocated()) { + qWarning("QOpenGLTexture::setFormat(): Cannot change format once storage has been allocated"); + return; + } + + d->format = format; + + switch (format) { + case NoFormat: + d->formatClass = NoFormatClass; + break; + + case RGBA32F: + case RGBA32U: + case RGBA32I: + d->formatClass = FormatClass_128Bit; + break; + + case RGB32F: + case RGB32U: + case RGB32I: + d->formatClass = FormatClass_96Bit; + break; + + case RGBA16F: + case RG32F: + case RGBA16U: + case RG32U: + case RGBA16I: + case RG32I: + case RGBA16_UNorm: + case RGBA16_SNorm: + d->formatClass = FormatClass_64Bit; + break; + + case RGB16_UNorm: + case RGB16_SNorm: + case RGB16F: + case RGB16U: + case RGB16I: + d->formatClass = FormatClass_48Bit; + break; + + case RG16F: + case RG11B10F: + case R32F: + case RGB10A2: + case RGBA8U: + case RG16U: + case R32U: + case RGBA8I: + case RG16I: + case R32I: + case RGBA8_UNorm: + case RG16_UNorm: + case RGBA8_SNorm: + case RG16_SNorm: + case SRGB8_Alpha8: + case RGB9E5: + d->formatClass = FormatClass_32Bit; + break; + + case RGB8_UNorm: + case RGB8_SNorm: + case SRGB8: + case RGB8U: + case RGB8I: + d->formatClass = FormatClass_24Bit; + break; + + case R16F: + case RG8U: + case R16U: + case RG8I: + case R16I: + case RG8_UNorm: + case R16_UNorm: + case RG8_SNorm: + case R16_SNorm: + d->formatClass = FormatClass_16Bit; + break; + + case R8U: + case R8I: + case R8_UNorm: + case R8_SNorm: + d->formatClass = FormatClass_8Bit; + break; + + case R_ATI1N_UNorm: + case R_ATI1N_SNorm: + d->formatClass = FormatClass_RGTC1_R; + break; + + case RG_ATI2N_UNorm: + case RG_ATI2N_SNorm: + d->formatClass = FormatClass_RGTC2_RG; + break; + + case RGB_BP_UNorm: + case SRGB_BP_UNorm: + d->formatClass = FormatClass_BPTC_Unorm; + break; + + case RGB_BP_UNSIGNED_FLOAT: + case RGB_BP_SIGNED_FLOAT: + d->formatClass = FormatClass_BPTC_Float; + break; + + case RGB_DXT1: + case SRGB_DXT1: + d->formatClass = FormatClass_S3TC_DXT1_RGB; + break; + + case RGBA_DXT1: + case SRGB_Alpha_DXT1: + d->formatClass = FormatClass_S3TC_DXT1_RGBA; + break; + + case RGBA_DXT3: + case SRGB_Alpha_DXT3: + d->formatClass = FormatClass_S3TC_DXT3_RGBA; + break; + + case RGBA_DXT5: + case SRGB_Alpha_DXT5: + d->formatClass = FormatClass_S3TC_DXT5_RGBA; + break; + + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: + case QOpenGLTexture::RGB8_ETC1: + case RG3B2: + case R5G6B5: + case RGB5A1: + case RGBA4: + case D16: + case D24: + case D24S8: + case D32: + case D32F: + case D32FS8X24: + case S8: + case DepthFormat: + case AlphaFormat: + case RGBFormat: + case RGBAFormat: + case LuminanceFormat: + case LuminanceAlphaFormat: + case QOpenGLTexture::RGBA_ASTC_4x4: + case QOpenGLTexture::RGBA_ASTC_5x4: + case QOpenGLTexture::RGBA_ASTC_5x5: + case QOpenGLTexture::RGBA_ASTC_6x5: + case QOpenGLTexture::RGBA_ASTC_6x6: + case QOpenGLTexture::RGBA_ASTC_8x5: + case QOpenGLTexture::RGBA_ASTC_8x6: + case QOpenGLTexture::RGBA_ASTC_8x8: + case QOpenGLTexture::RGBA_ASTC_10x5: + case QOpenGLTexture::RGBA_ASTC_10x6: + case QOpenGLTexture::RGBA_ASTC_10x8: + case QOpenGLTexture::RGBA_ASTC_10x10: + case QOpenGLTexture::RGBA_ASTC_12x10: + case QOpenGLTexture::RGBA_ASTC_12x12: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10: + case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12: + d->formatClass = FormatClass_Unique; + break; + } +} + +/*! + Returns the format of this texture object. + + \sa setFormat() +*/ +QOpenGLTexture::TextureFormat QOpenGLTexture::format() const +{ + Q_D(const QOpenGLTexture); + return d->format; +} + +static bool isNpot(int width, int height = 1, int depth = 1) +{ + return width & (width-1) || height & (height-1) || depth & (depth-1); +} + +/*! + Sets the dimensions of this texture object to \a width, + \a height, and \a depth. The default for each dimension is 1. + The maximum allowable texture size is dependent upon your OpenGL + implementation. Allocating storage for a texture less than the + maximum size can still fail if your system is low on resources. + + If a non-power-of-two \a width, \a height or \a depth is provided and your + OpenGL implementation doesn't have support for repeating non-power-of-two + textures, then the wrap mode is automatically set to ClampToEdge. + + \sa width(), height(), depth() +*/ +void QOpenGLTexture::setSize(int width, int height, int depth) +{ + Q_D(QOpenGLTexture); + d->create(); + if (isStorageAllocated()) { + qWarning("Cannot resize a texture that already has storage allocated.\n" + "To do so, destroy() the texture and then create() and setSize()"); + return; + } + + if (isNpot(width, height, depth) && !hasFeature(Feature::NPOTTextureRepeat) && d->target != Target::TargetRectangle) + d->setWrapMode(WrapMode::ClampToEdge); + + switch (d->target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::TargetBuffer: + d->dimensions[0] = width; + Q_UNUSED(height); + Q_UNUSED(depth); + break; + + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::TargetRectangle: + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + d->dimensions[0] = width; + d->dimensions[1] = height; + Q_UNUSED(depth); + break; + + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + if (width != height) + qWarning("QAbstractOpenGLTexture::setSize(): Cube map textures must be square"); + d->dimensions[0] = d->dimensions[1] = width; + Q_UNUSED(depth); + break; + + case QOpenGLTexture::Target3D: + d->dimensions[0] = width; + d->dimensions[1] = height; + d->dimensions[2] = depth; + break; + } +} + +/*! + Returns the width of a 1D, 2D or 3D texture. + + \sa height(), depth(), setSize() +*/ +int QOpenGLTexture::width() const +{ + Q_D(const QOpenGLTexture); + return d->dimensions[0]; +} + +/*! + Returns the height of a 2D or 3D texture. + + \sa width(), depth(), setSize() +*/ +int QOpenGLTexture::height() const +{ + Q_D(const QOpenGLTexture); + return d->dimensions[1]; +} + +/*! + Returns the depth of a 3D texture. + + \sa width(), height(), setSize() +*/ +int QOpenGLTexture::depth() const +{ + Q_D(const QOpenGLTexture); + return d->dimensions[2]; +} + +/*! + For texture targets that support mipmaps, this function + sets the requested number of mipmap \a levels to allocate storage + for. This function should be called before storage is allocated + for the texture. + + If the texture target does not support mipmaps this function + has no effect. + + \sa mipLevels(), maximumMipLevels(), isStorageAllocated() +*/ +void QOpenGLTexture::setMipLevels(int levels) +{ + Q_D(QOpenGLTexture); + d->create(); + if (isStorageAllocated()) { + qWarning("Cannot set mip levels on a texture that already has storage allocated.\n" + "To do so, destroy() the texture and then create() and setMipLevels()"); + return; + } + + switch (d->target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + case QOpenGLTexture::Target3D: + d->requestedMipLevels = levels; + break; + + case QOpenGLTexture::TargetBuffer: + case QOpenGLTexture::TargetRectangle: + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + qWarning("QAbstractOpenGLTexture::setMipLevels(): This texture target does not support mipmaps"); + break; + } +} + +/*! + Returns the number of mipmap levels for this texture. If storage + has not yet been allocated for this texture it returns the + requested number of mipmap levels. + + \sa setMipLevels(), maximumMipLevels(), isStorageAllocated() +*/ +int QOpenGLTexture::mipLevels() const +{ + Q_D(const QOpenGLTexture); + return isStorageAllocated() ? d->mipLevels : d->requestedMipLevels; +} + +/*! + Returns the maximum number of mipmap levels that this texture + can have given the current dimensions. + + \sa setMipLevels(), mipLevels(), setSize() +*/ +int QOpenGLTexture::maximumMipLevels() const +{ + Q_D(const QOpenGLTexture); + return d->maximumMipLevelCount(); +} + +/*! + Sets the number of array \a layers to allocate storage for. This + function should be called before storage is allocated for the texture. + + For targets that do not support array layers this function has + no effect. + + \sa layers(), isStorageAllocated() +*/ +void QOpenGLTexture::setLayers(int layers) +{ + Q_D(QOpenGLTexture); + d->create(); + if (isStorageAllocated()) { + qWarning("Cannot set layers on a texture that already has storage allocated.\n" + "To do so, destroy() the texture and then create() and setLayers()"); + return; + } + + switch (d->target) { + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::TargetCubeMapArray: + case QOpenGLTexture::Target2DMultisampleArray: + d->layers = layers; + break; + + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target3D: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetBuffer: + case QOpenGLTexture::TargetRectangle: + case QOpenGLTexture::Target2DMultisample: + qWarning("Texture target does not support array layers"); + break; + } +} + +/*! + Returns the number of array layers for this texture. If + storage has not yet been allocated for this texture then + this function returns the requested number of array layers. + + For texture targets that do not support array layers this + will return 1. + + \sa setLayers(), isStorageAllocated() +*/ +int QOpenGLTexture::layers() const +{ + Q_D(const QOpenGLTexture); + return d->layers; +} + +/*! + Returns the number of faces for this texture. For cubemap + and cubemap array type targets this will be 6. + + For non-cubemap type targets this will return 1. +*/ +int QOpenGLTexture::faces() const +{ + Q_D(const QOpenGLTexture); + return d->faces; +} + +/*! + Sets the number of \a samples to allocate storage for when rendering to + a multisample capable texture target. This function should + be called before storage is allocated for the texture. + + For targets that do not support multisampling this function has + no effect. + + \sa samples(), isStorageAllocated() +*/ +void QOpenGLTexture::setSamples(int samples) +{ + Q_D(QOpenGLTexture); + d->create(); + if (isStorageAllocated()) { + qWarning("Cannot set sample count on a texture that already has storage allocated.\n" + "To do so, destroy() the texture and then create() and setSamples()"); + return; + } + + switch (d->target) { + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + d->samples = samples; + break; + + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target3D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + case QOpenGLTexture::TargetBuffer: + case QOpenGLTexture::TargetRectangle: + + qWarning("Texture target does not support multisampling"); + break; + } +} + +/*! + Returns the number of multisample sample points for this texture. + If storage has not yet been allocated for this texture then + this function returns the requested number of samples. + + For texture targets that do not support multisampling this + will return 0. + + \sa setSamples(), isStorageAllocated() +*/ +int QOpenGLTexture::samples() const +{ + Q_D(const QOpenGLTexture); + return d->samples; +} + +/*! + Sets whether the sample positions and number of samples used with + a multisample capable texture target to \a fixed. If set to \c true + the sample positions and number of samples used are the same for + all texels in the image and will not depend upon the image size or + internal format. This function should be called before storage is allocated + for the texture. + + For targets that do not support multisampling this function has + no effect. + + The default value is \c true. + + \sa isFixedSamplePositions(), isStorageAllocated() +*/ +void QOpenGLTexture::setFixedSamplePositions(bool fixed) +{ + Q_D(QOpenGLTexture); + d->create(); + if (isStorageAllocated()) { + qWarning("Cannot set sample positions on a texture that already has storage allocated.\n" + "To do so, destroy() the texture and then create() and setFixedSamplePositions()"); + return; + } + + switch (d->target) { + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + d->fixedSamplePositions = fixed; + break; + + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target3D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + case QOpenGLTexture::TargetBuffer: + case QOpenGLTexture::TargetRectangle: + + qWarning("Texture target does not support multisampling"); + break; + } +} + +/*! + Returns whether this texture uses a fixed pattern of multisample + samples. If storage has not yet been allocated for this texture then + this function returns the requested fixed sample position setting. + + For texture targets that do not support multisampling this + will return \c true. + + \sa setFixedSamplePositions(), isStorageAllocated() +*/ +bool QOpenGLTexture::isFixedSamplePositions() const +{ + Q_D(const QOpenGLTexture); + return d->fixedSamplePositions; +} + +/*! + Allocates server-side storage for this texture object taking + into account, the format, dimensions, mipmap levels, array + layers and cubemap faces. + + Once storage has been allocated it is no longer possible to change + these properties. + + If supported QOpenGLTexture makes use of immutable texture + storage. + + Once storage has been allocated for the texture then pixel data + can be uploaded via one of the setData() overloads. + + \note If immutable texture storage is not available, + then a default pixel format and pixel type will be used to + create the mutable storage. You can use the other + allocateStorage() overload to specify exactly the pixel format + and the pixel type to use when allocating mutable storage; + this is particulary useful under certain OpenGL ES implementations + (notably, OpenGL ES 2), where the pixel format and the pixel type + used at allocation time must perfectly match the format + and the type passed to any subsequent setData() call. + + \sa isStorageAllocated(), setData() +*/ +void QOpenGLTexture::allocateStorage() +{ + Q_D(QOpenGLTexture); + if (d->create()) { + const QOpenGLTexture::PixelFormat pixelFormat = pixelFormatCompatibleWithInternalFormat(d->format); + const QOpenGLTexture::PixelType pixelType = pixelTypeCompatibleWithInternalFormat(d->format); + d->allocateStorage(pixelFormat, pixelType); + } +} + +/*! + \since 5.5 + + Allocates server-side storage for this texture object taking + into account, the format, dimensions, mipmap levels, array + layers and cubemap faces. + + Once storage has been allocated it is no longer possible to change + these properties. + + If supported QOpenGLTexture makes use of immutable texture + storage. However, if immutable texture storage is not available, + then the specified \a pixelFormat and \a pixelType will be used + to allocate mutable storage; note that in certain OpenGL implementations + (notably, OpenGL ES 2) they must perfectly match the format + and the type passed to any subsequent setData() call. + + Once storage has been allocated for the texture then pixel data + can be uploaded via one of the setData() overloads. + + \sa isStorageAllocated(), setData() +*/ +void QOpenGLTexture::allocateStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType) +{ + Q_D(QOpenGLTexture); + if (d->create()) + d->allocateStorage(pixelFormat, pixelType); +} + +/*! + Returns \c true if server-side storage for this texture as been + allocated. + + The texture format, dimensions, mipmap levels and array layers + cannot be altered once storage ihas been allocated. + + \sa allocateStorage(), setSize(), setMipLevels(), setLayers(), setFormat() +*/ +bool QOpenGLTexture::isStorageAllocated() const +{ + Q_D(const QOpenGLTexture); + return d->storageAllocated; +} + +/*! + Attempts to create a texture view onto this texture. A texture + view is somewhat analogous to a view in SQL in that it presents + a restricted or reinterpreted view of the original data. Texture + views do not allocate any more server-side storage, insted relying + on the storage buffer of the source texture. + + Texture views are only available when using immutable storage. For + more information on texture views see + http://www.opengl.org/wiki/Texture_Storage#Texture_views. + + The \a target argument specifies the target to use for the view. + Only some targets can be used depending upon the target of the original + target. For e.g. a view onto a Target1DArray texture can specify + either Target1DArray or Target1D but for the latter the number of + array layers specified with \a minimumLayer and \a maximumLayer must + be exactly 1. + + Simpliar constraints apply for the \a viewFormat. See the above link + and the specification for more details. + + The \a minimumMipmapLevel, \a maximumMipmapLevel, \a minimumLayer, + and \a maximumLayer arguments serve to restrict the parts of the + texture accessible by the texture view. + + If creation of the texture view fails this function will return + 0. If the function succeeds it will return a pointer to a new + QOpenGLTexture object that will return \c true from its isTextureView() + function. + + \sa isTextureView() +*/ +QOpenGLTexture *QOpenGLTexture::createTextureView(Target target, + TextureFormat viewFormat, + int minimumMipmapLevel, int maximumMipmapLevel, + int minimumLayer, int maximumLayer) const +{ + Q_D(const QOpenGLTexture); + if (!isStorageAllocated()) { + qWarning("Cannot set create a texture view of a texture that does not have storage allocated."); + return nullptr; + } + Q_ASSERT(maximumMipmapLevel >= minimumMipmapLevel); + Q_ASSERT(maximumLayer >= minimumLayer); + return d->createTextureView(target, viewFormat, + minimumMipmapLevel, maximumMipmapLevel, + minimumLayer, maximumLayer); +} + +/*! + Returns \c true if this texture object is actually a view onto another + texture object. + + \sa createTextureView() +*/ +bool QOpenGLTexture::isTextureView() const +{ + Q_D(const QOpenGLTexture); + Q_ASSERT(d->textureId); + return d->textureView; +} + +/*! + Uploads pixel \a data for this texture object \a mipLevel, array \a layer, and \a cubeFace. + Storage must have been allocated before uploading pixel data. Some overloads of setData() + will set appropriate dimensions, mipmap levels, and array layers and then allocate storage + for you if they have enough information to do so. This will be noted in the function + documentation. + + The structure of the pixel data pointed to by \a data is specified by \a sourceFormat + and \a sourceType. The pixel data upload can optionally be controlled by \a options. + + If using a compressed format() then you should use setCompressedData() instead of this + function. + + \since 5.3 + \sa setCompressedData() +*/ +void QOpenGLTexture::setData(int mipLevel, int layer, CubeMapFace cubeFace, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + if (!isStorageAllocated()) { + qWarning("Cannot set data on a texture that does not have storage allocated.\n" + "To do so call allocateStorage() before this function"); + return; + } + d->setData(mipLevel, layer, 1, cubeFace, sourceFormat, sourceType, data, options); +} + +/*! + \since 5.9 + \overload + + Parameter \a layerCount is the number of layers in a texture array + that are being uploaded/populated by this call. +*/ +void QOpenGLTexture::setData(int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType, const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + if (!isStorageAllocated()) { + qWarning("Cannot set data on a texture that does not have storage allocated.\n" + "To do so call allocateStorage() before this function"); + return; + } + d->setData(mipLevel, layer, layerCount, cubeFace, sourceFormat, sourceType, data, options); +} + +/*! + \since 5.3 + \overload +*/ +void QOpenGLTexture::setData(int mipLevel, int layer, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(mipLevel, layer, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options); +} + +/*! + \since 5.3 + \overload +*/ +void QOpenGLTexture::setData(int mipLevel, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(mipLevel, 0, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options); +} + +/*! + \since 5.3 + \overload +*/ +void QOpenGLTexture::setData(PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(0, 0, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options); +} + +/*! + \since 5.14 + \overload + + This overload is to be used to update a part of the texture. Parameters \a + xOffset, \a yOffset, \a zOffset specify the texel offsets within the + texture. Parameters \a width, \a height and \a depth specify the dimensions + of the sub image. + + The structure of the pixel data pointed to by \a data is specified by \a + sourceFormat and \a sourceType. The pixel data upload can optionally be + controlled by \a options. +*/ +void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(xOffset, yOffset, zOffset, + width, height, depth, + 0, 0, 1, + QOpenGLTexture::CubeMapPositiveX, sourceFormat, + sourceType, data, options); +} + +/*! + \since 5.14 + \overload + + This overload is to be used to update a part of the texture. Parameters \a + xOffset, \a yOffset, \a zOffset specify the texel offsets within the + texture. Parameters \a width, \a height and \a depth specify the dimensions + of the sub image. The mip map level the sub image we want to + update is specified with \a mipLevel. + + The structure of the pixel data pointed to by \a data is specified by \a + sourceFormat and \a sourceType. The pixel data upload can optionally be + controlled by \a options. +*/ +void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + int mipLevel, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(xOffset, yOffset, zOffset, + width, height, depth, + mipLevel, 0, 1, + QOpenGLTexture::CubeMapPositiveX, sourceFormat, + sourceType, data, options); +} + +/*! + \since 5.14 + \overload + + This overload is to be used to update a part of the texture. Parameters \a + xOffset, \a yOffset, \a zOffset specify the texel offsets within the + texture. Parameters \a width, \a height and \a depth specify the dimensions + of the sub image. The mip map level and layerof the sub image we want to + update are specified with \a mipLevel and \a layer. + + The structure of the pixel data pointed to by \a data is specified by \a + sourceFormat and \a sourceType. The pixel data upload can optionally be + controlled by \a options. +*/ +void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + int mipLevel, int layer, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(xOffset, yOffset, zOffset, + width, height, depth, + mipLevel, layer, 1, + QOpenGLTexture::CubeMapPositiveX, sourceFormat, + sourceType, data, options); +} + +/*! + \since 5.14 + \overload + + This overload is to be used to update a part of the texture. Parameters \a + xOffset, \a yOffset, \a zOffset specify the texel offsets within the + texture. Parameters \a width, \a height and \a depth specify the dimensions + of the sub image.The mip map level, layer and cube map face of the sub + image we want to update are specified with \a mipLevel, \a layer and \a + face. + + The structure of the pixel data pointed to by \a data is specified by \a + sourceFormat and \a sourceType. The pixel data upload can optionally be + controlled by \a options. +*/ +void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + int mipLevel, int layer, + CubeMapFace face, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(xOffset, yOffset, zOffset, + width, height, depth, + mipLevel, layer, 1, + face, sourceFormat, + sourceType, data, options); +} + +/*! + \since 5.14 + \overload + + This overload is to be used to update a part of the texture. Parameters \a + xOffset, \a yOffset, \a zOffset specify the texel offsets within the + texture. Parameters \a width, \a height and \a depth specify the dimensions + of the sub image.The mip map level, starting layer, cube map face and + number of layers of the sub image we want to update are specified with \a + mipLevel, \a layer, \a face and \a layerCount. + + The structure of the pixel data pointed to by \a data is specified by \a + sourceFormat and \a sourceType. The pixel data upload can optionally be + controlled by \a options. +*/ +void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + int mipLevel, int layer, + CubeMapFace face, int layerCount, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(xOffset, yOffset, zOffset, + width, height, depth, + mipLevel, layer, layerCount, + face, sourceFormat, + sourceType, data, options); +} + +#if QT_DEPRECATED_SINCE(5, 3) +/*! + \obsolete + \overload + + \sa setCompressedData() +*/ +void QOpenGLTexture::setData(int mipLevel, int layer, CubeMapFace cubeFace, + PixelFormat sourceFormat, PixelType sourceType, + void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + if (!isStorageAllocated()) { + qWarning("Cannot set data on a texture that does not have storage allocated.\n" + "To do so call allocateStorage() before this function"); + return; + } + d->setData(mipLevel, layer, 1, cubeFace, sourceFormat, sourceType, data, options); +} + +/*! + \obsolete + \overload +*/ +void QOpenGLTexture::setData(int mipLevel, int layer, + PixelFormat sourceFormat, PixelType sourceType, + void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(mipLevel, layer, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options); +} + +/*! + \obsolete + \overload +*/ +void QOpenGLTexture::setData(int mipLevel, + PixelFormat sourceFormat, PixelType sourceType, + void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(mipLevel, 0, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options); +} + +/*! + \obsolete + \overload +*/ +void QOpenGLTexture::setData(PixelFormat sourceFormat, PixelType sourceType, + void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setData(0, 0, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options); +} +#endif + +/*! + This overload of setData() will allocate storage for you. + The pixel data is contained in \a image. Mipmaps are generated by default. + Set \a genMipMaps to \l DontGenerateMipMaps to turn off mipmap generation. + + \overload +*/ +void QOpenGLTexture::setData(const QImage& image, MipMapGeneration genMipMaps) +{ + QOpenGLContext *context = QOpenGLContext::currentContext(); + if (!context) { + qWarning("QOpenGLTexture::setData() requires a valid current context"); + return; + } + + if (image.isNull()) { + qWarning("QOpenGLTexture::setData() tried to set a null image"); + return; + } + + if (context->isOpenGLES() && context->format().majorVersion() < 3) + setFormat(QOpenGLTexture::RGBAFormat); + else + setFormat(QOpenGLTexture::RGBA8_UNorm); + + setSize(image.width(), image.height()); + setMipLevels(genMipMaps == GenerateMipMaps ? maximumMipLevels() : 1); + allocateStorage(QOpenGLTexture::RGBA, QOpenGLTexture::UInt8); + + // Upload pixel data and generate mipmaps + QImage glImage = image.convertToFormat(QImage::Format_RGBA8888); + QOpenGLPixelTransferOptions uploadOptions; + uploadOptions.setAlignment(1); + setData(0, QOpenGLTexture::RGBA, QOpenGLTexture::UInt8, glImage.constBits(), &uploadOptions); +} + +/*! + Uploads compressed pixel \a data to \a mipLevel, array \a layer, and \a cubeFace. + The pixel transfer can optionally be controlled with \a options. The \a dataSize + argument should specify the size of the data pointed to by \a data. + + If not using a compressed format() then you should use setData() instead of this + function. + + \since 5.3 +*/ +void QOpenGLTexture::setCompressedData(int mipLevel, int layer, CubeMapFace cubeFace, + int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + if (!isStorageAllocated()) { + qWarning("Cannot set data on a texture that does not have storage allocated.\n" + "To do so call allocateStorage() before this function"); + return; + } + d->setCompressedData(mipLevel, layer, 1, cubeFace, dataSize, data, options); +} + +/*! + \since 5.9 + \overload + + Parameter \a layerCount is the number of layers in a texture array + that are being uploaded/populated by this call. +*/ +void QOpenGLTexture::setCompressedData(int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, int dataSize, const void *data, const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + if (!isStorageAllocated()) { + qWarning("Cannot set data on a texture that does not have storage allocated.\n" + "To do so call allocateStorage() before this function"); + return; + } + d->setCompressedData(mipLevel, layer, layerCount, cubeFace, dataSize, data, options); +} + +/*! + \overload +*/ +void QOpenGLTexture::setCompressedData(int mipLevel, int layer, int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setCompressedData(mipLevel, layer, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options); +} + +/*! + \overload +*/ +void QOpenGLTexture::setCompressedData(int mipLevel, int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setCompressedData(mipLevel, 0, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options); +} + +/*! + \overload +*/ +void QOpenGLTexture::setCompressedData(int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setCompressedData(0, 0, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options); +} + +#if QT_DEPRECATED_SINCE(5, 3) +/*! + \obsolete + \overload +*/ +void QOpenGLTexture::setCompressedData(int mipLevel, int layer, CubeMapFace cubeFace, + int dataSize, void *data, + const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + if (!isStorageAllocated()) { + qWarning("Cannot set data on a texture that does not have storage allocated.\n" + "To do so call allocateStorage() before this function"); + return; + } + d->setCompressedData(mipLevel, layer, 1, cubeFace, dataSize, data, options); +} + +/*! + \obsolete + \overload +*/ +void QOpenGLTexture::setCompressedData(int mipLevel, int layer, int dataSize, void *data, + const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setCompressedData(mipLevel, layer, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options); +} + +/*! + \obsolete + \overload +*/ +void QOpenGLTexture::setCompressedData(int mipLevel, int dataSize, void *data, + const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setCompressedData(mipLevel, 0, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options); +} + +/*! + \obsolete + \overload +*/ +void QOpenGLTexture::setCompressedData(int dataSize, void *data, + const QOpenGLPixelTransferOptions * const options) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->textureId); + d->setCompressedData(0, 0, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options); +} +#endif + +/*! + Returns \c true if your OpenGL implementation and version supports the texture + feature \a feature. +*/ +bool QOpenGLTexture::hasFeature(Feature feature) +{ + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (!ctx) { + qWarning("QOpenGLTexture::hasFeature() requires a valid current context"); + return false; + } + + QSurfaceFormat f = ctx->format(); + + bool supported = false; + +#if !defined(QT_OPENGL_ES_2) + if (!ctx->isOpenGLES()) { + switch (feature) { + case ImmutableMultisampleStorage: + supported = f.version() >= qMakePair(4, 3) + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_storage_multisample")); + break; + + case TextureBuffer: + supported = f.version() >= qMakePair(3, 0) + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_buffer_object")); + break; + + case StencilTexturing: + supported = f.version() >= qMakePair(4, 3) + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_stencil_texturing")); + break; + + case ImmutableStorage: + supported = f.version() >= qMakePair(4, 2) + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_storage")) + || ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_storage")); + break; + + case TextureCubeMapArrays: + supported = f.version() >= qMakePair(4, 0) + || ctx->hasExtension(QByteArrayLiteral("ARB_texture_cube_map_array")); + break; + + case Swizzle: + supported = f.version() >= qMakePair(3, 3) + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_swizzle")); + break; + + case TextureMultisample: + supported = f.version() >= qMakePair(3, 2) + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_multisample")); + break; + + case TextureArrays: + supported = f.version() >= qMakePair(3, 0) + || ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_array")); + break; + + case TextureRectangle: + supported = f.version() >= qMakePair(2, 1) + || ctx->hasExtension(QByteArrayLiteral("ARB_texture_rectangle")); + break; + + case Texture3D: + supported = f.version() >= qMakePair(1, 3); + break; + + case AnisotropicFiltering: + supported = ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_filter_anisotropic")); + break; + + case NPOTTextures: + case NPOTTextureRepeat: + supported = ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_non_power_of_two")); + break; + + case Texture1D: + supported = f.version() >= qMakePair(1, 1); + break; + + case TextureComparisonOperators: + // GL 1.4 and GL_ARB_shadow alone support only LEQUAL and GEQUAL; + // since we're talking about history anyhow avoid to be extra pedantic + // in the feature set, and simply claim supported if we have the full set of operators + // (which has been added into 1.5 / GL_EXT_shadow_funcs). + supported = f.version() >= qMakePair(1, 5) + || (ctx->hasExtension(QByteArrayLiteral("GL_ARB_shadow")) + && ctx->hasExtension(QByteArrayLiteral("GL_EXT_shadow_funcs"))); + break; + + case TextureMipMapLevel: + supported = f.version() >= qMakePair(1, 2); + break; + + case MaxFeatureFlag: + break; + } + } + + if (ctx->isOpenGLES()) +#endif + { + const char *renderer = reinterpret_cast<const char *>(ctx->functions()->glGetString(GL_RENDERER)); + switch (feature) { + case ImmutableStorage: + supported = (f.version() >= qMakePair(3, 0) || ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_storage"))) + && !(renderer && strstr(renderer, "Mali")); // do not use on Mali: QTBUG-45106 + break; + + case ImmutableMultisampleStorage: + supported = f.version() >= qMakePair(3, 1); + break; + + case TextureRectangle: + break; + + case TextureArrays: + supported = f.version() >= qMakePair(3, 0); + break; + + case Texture3D: + supported = f.version() >= qMakePair(3, 0) + || ctx->hasExtension(QByteArrayLiteral("GL_OES_texture_3D")); + break; + + case TextureMultisample: + supported = f.version() >= qMakePair(3, 1); + break; + + case TextureBuffer: + break; + + case TextureCubeMapArrays: + break; + + case Swizzle: + supported = f.version() >= qMakePair(3, 0); + break; + + case StencilTexturing: + break; + + case AnisotropicFiltering: + supported = ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_filter_anisotropic")); + break; + + case NPOTTextures: + case NPOTTextureRepeat: + supported = f.version() >= qMakePair(3,0) + || ctx->hasExtension(QByteArrayLiteral("GL_OES_texture_npot")) + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_non_power_of_two")); + break; + + case Texture1D: + break; + + case TextureComparisonOperators: + supported = f.version() >= qMakePair(3, 0) + || ctx->hasExtension(QByteArrayLiteral("GL_EXT_shadow_samplers")); + break; + + case TextureMipMapLevel: + supported = f.version() >= qMakePair(3, 0); + break; + + case MaxFeatureFlag: + break; + } + } + + return supported; +} + +/*! + Sets the base mipmap level used for all texture lookups with this texture to \a baseLevel. + + \note This function has no effect on Qt built for OpenGL ES 2. + \sa mipBaseLevel(), setMipMaxLevel(), setMipLevelRange() +*/ +void QOpenGLTexture::setMipBaseLevel(int baseLevel) +{ + Q_D(QOpenGLTexture); + d->create(); + if (!d->features.testFlag(TextureMipMapLevel)) { + qWarning("QOpenGLTexture::setMipBaseLevel: requires OpenGL >= 1.2 or OpenGL ES >= 3.0"); + return; + } + Q_ASSERT(d->textureId); + Q_ASSERT(d->texFuncs); + Q_ASSERT(baseLevel <= d->maxLevel); + d->baseLevel = baseLevel; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BASE_LEVEL, baseLevel); +} + +/*! + Returns the mipmap base level used for all texture lookups with this texture. + The default is 0. + + \sa setMipBaseLevel(), mipMaxLevel(), mipLevelRange() +*/ +int QOpenGLTexture::mipBaseLevel() const +{ + Q_D(const QOpenGLTexture); + return d->baseLevel; +} + +/*! + Sets the maximum mipmap level used for all texture lookups with this texture to \a maxLevel. + + \note This function has no effect on Qt built for OpenGL ES 2. + \sa mipMaxLevel(), setMipBaseLevel(), setMipLevelRange() +*/ +void QOpenGLTexture::setMipMaxLevel(int maxLevel) +{ + Q_D(QOpenGLTexture); + d->create(); + if (!d->features.testFlag(TextureMipMapLevel)) { + qWarning("QOpenGLTexture::setMipMaxLevel: requires OpenGL >= 1.2 or OpenGL ES >= 3.0"); + return; + } + Q_ASSERT(d->textureId); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->baseLevel <= maxLevel); + d->maxLevel = maxLevel; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_LEVEL, maxLevel); +} + +/*! + Returns the mipmap maximum level used for all texture lookups with this texture. + + \sa setMipMaxLevel(), mipBaseLevel(), mipLevelRange() +*/ +int QOpenGLTexture::mipMaxLevel() const +{ + Q_D(const QOpenGLTexture); + return d->maxLevel; +} + +/*! + Sets the range of mipmap levels that can be used for texture lookups with this texture + to range from \a baseLevel to \a maxLevel. + + \note This function has no effect on Qt built for OpenGL ES 2. + \sa setMipBaseLevel(), setMipMaxLevel(), mipLevelRange() +*/ +void QOpenGLTexture::setMipLevelRange(int baseLevel, int maxLevel) +{ + Q_D(QOpenGLTexture); + d->create(); + if (!d->features.testFlag(TextureMipMapLevel)) { + qWarning("QOpenGLTexture::setMipLevelRange: requires OpenGL >= 1.2 or OpenGL ES >= 3.0"); + return; + } + Q_ASSERT(d->textureId); + Q_ASSERT(d->texFuncs); + Q_ASSERT(baseLevel <= maxLevel); + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BASE_LEVEL, baseLevel); + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_LEVEL, maxLevel); +} + +/*! + Returns the range of mipmap levels that can be used for texture lookups with this texture. + + \sa mipBaseLevel(), mipMaxLevel() +*/ +QPair<int, int> QOpenGLTexture::mipLevelRange() const +{ + Q_D(const QOpenGLTexture); + return qMakePair(d->baseLevel, d->maxLevel); +} + +/*! + If \a enabled is \c true, enables automatic mipmap generation for this texture object + to occur whenever the level 0 mipmap data is set via setData(). + + The automatic mipmap generation is enabled by default. + + \note Mipmap generation is not supported for compressed textures with OpenGL ES 2.0. + + \sa isAutoMipMapGenerationEnabled(), generateMipMaps() +*/ +void QOpenGLTexture::setAutoMipMapGenerationEnabled(bool enabled) +{ + Q_D(QOpenGLTexture); + d->autoGenerateMipMaps = enabled; +} + +/*! + Returns whether auto mipmap generation is enabled for this texture object. + + \sa setAutoMipMapGenerationEnabled(), generateMipMaps() +*/ +bool QOpenGLTexture::isAutoMipMapGenerationEnabled() const +{ + Q_D(const QOpenGLTexture); + return d->autoGenerateMipMaps; +} + +/*! + Generates mipmaps for this texture object from mipmap level 0. If you are + using a texture target and filtering option that requires mipmaps and you + have disabled automatic mipmap generation then you need to call this function + or the overload to create the mipmap chain. + + \note Mipmap generation is not supported for compressed textures with OpenGL ES. + + \sa setAutoMipMapGenerationEnabled(), setMipLevels(), mipLevels() +*/ +void QOpenGLTexture::generateMipMaps() +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + if (isCompressedFormat(d->format)) { + if (QOpenGLContext *ctx = QOpenGLContext::currentContext()) + if (ctx->isOpenGLES()) + return; + } + d->texFuncs->glGenerateTextureMipmap(d->textureId, d->target, d->bindingTarget); +} + +/*! + Generates mipmaps for this texture object from mipmap level \a baseLevel. If you are + using a texture target and filtering option that requires mipmaps and you + have disabled automatic mipmap generation then you need to call this function + or the overload to create the mipmap chain. + + The generation of mipmaps to above \a baseLevel is achieved by setting the mipmap + base level to \a baseLevel and then generating the mipmap chain. If \a resetBaseLevel + is \c true, then the baseLevel of the texture will be reset to its previous value. + + \sa setAutoMipMapGenerationEnabled(), setMipLevels(), mipLevels() +*/ +void QOpenGLTexture::generateMipMaps(int baseLevel, bool resetBaseLevel) +{ + Q_D(QOpenGLTexture); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + if (isCompressedFormat(d->format)) { + if (QOpenGLContext *ctx = QOpenGLContext::currentContext()) + if (ctx->isOpenGLES()) + return; + } + int oldBaseLevel; + if (resetBaseLevel) + oldBaseLevel = mipBaseLevel(); + setMipBaseLevel(baseLevel); + d->texFuncs->glGenerateTextureMipmap(d->textureId, d->target, d->bindingTarget); + if (resetBaseLevel) + setMipBaseLevel(oldBaseLevel); +} + +/*! + GLSL shaders are able to reorder the components of the vec4 returned by texture + functions. It is also desirable to be able to control this reordering from CPU + side code. This is made possible by swizzle masks since OpenGL 3.3. + + Each component of the texture can be mapped to one of the SwizzleValue options. + + This function maps \a component to the output \a value. + + \note This function has no effect on Mac and Qt built for OpenGL ES 2. + \sa swizzleMask() +*/ +void QOpenGLTexture::setSwizzleMask(SwizzleComponent component, SwizzleValue value) +{ +#if !defined(Q_OS_MAC) && !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + if (!d->features.testFlag(Swizzle)) { + qWarning("QOpenGLTexture::setSwizzleMask() requires OpenGL >= 3.3"); + return; + } + d->swizzleMask[component - SwizzleRed] = value; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, component, value); + return; + } +#else + Q_UNUSED(component); + Q_UNUSED(value); +#endif + qWarning("QOpenGLTexture: Texture swizzling is not supported"); +} + +/*! + Parameters \a {r}, \a {g}, \a {b}, and \a {a} are values used for setting + the colors red, green, blue, and the alpha value. + \overload +*/ +void QOpenGLTexture::setSwizzleMask(SwizzleValue r, SwizzleValue g, + SwizzleValue b, SwizzleValue a) +{ +#if !defined(Q_OS_MAC) && !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + if (!d->features.testFlag(Swizzle)) { + qWarning("QOpenGLTexture::setSwizzleMask() requires OpenGL >= 3.3"); + return; + } + GLint swizzleMask[] = {GLint(r), GLint(g), GLint(b), GLint(a)}; + d->swizzleMask[0] = r; + d->swizzleMask[1] = g; + d->swizzleMask[2] = b; + d->swizzleMask[3] = a; + d->texFuncs->glTextureParameteriv(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); + return; + } +#else + Q_UNUSED(r); + Q_UNUSED(g); + Q_UNUSED(b); + Q_UNUSED(a); +#endif + qWarning("QOpenGLTexture: Texture swizzling is not supported"); +} + +/*! + Returns the swizzle mask for texture \a component. +*/ +QOpenGLTexture::SwizzleValue QOpenGLTexture::swizzleMask(SwizzleComponent component) const +{ + Q_D(const QOpenGLTexture); + return d->swizzleMask[component - SwizzleRed]; +} + +/*! + \enum QOpenGLTexture::DepthStencilMode + \since 5.4 + This enum specifies which component of a depth/stencil texture is + accessed when the texture is sampled. + + \value DepthMode Equivalent to GL_DEPTH_COMPONENT. + \value StencilMode Equivalent to GL_STENCIL_INDEX. +*/ + +/*! + If using a texture that has a combined depth/stencil format this function sets + which component of the texture is accessed to \a mode. + + When the parameter is set to DepthMode, then accessing it from the + shader will access the depth component as a single float, as normal. But when + the parameter is set to StencilMode, the shader will access the stencil component. + + \note This function has no effect on Mac and Qt built for OpenGL ES 2. + \since 5.4 + \sa depthStencilMode() +*/ +void QOpenGLTexture::setDepthStencilMode(QOpenGLTexture::DepthStencilMode mode) +{ +#if !defined(Q_OS_MAC) && !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + if (!d->features.testFlag(StencilTexturing)) { + qWarning("QOpenGLTexture::setDepthStencilMode() requires OpenGL >= 4.3 or GL_ARB_stencil_texturing"); + return; + } + d->depthStencilMode = mode; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_DEPTH_STENCIL_TEXTURE_MODE, mode); + return; + } +#else + Q_UNUSED(mode); +#endif + qWarning("QOpenGLTexture: DepthStencil Mode is not supported"); +} + +/*! + Returns the depth stencil mode for textures using a combined depth/stencil format. + + \since 5.4 + \sa setDepthStencilMode() +*/ +QOpenGLTexture::DepthStencilMode QOpenGLTexture::depthStencilMode() const +{ + Q_D(const QOpenGLTexture); + return d->depthStencilMode; +} + +/*! + \enum QOpenGLTexture::ComparisonFunction + \since 5.5 + This enum specifies which comparison operator is used when texture comparison + is enabled on this texture. + + \value CompareLessEqual Equivalent to GL_LEQUAL. + \value CompareGreaterEqual Equivalent to GL_GEQUAL. + \value CompareLess Equivalent to GL_LESS. + \value CompareGreater Equivalent to GL_GREATER. + \value CompareEqual Equivalent to GL_EQUAL. + \value CommpareNotEqual Equivalent to GL_NOTEQUAL. + \value CompareAlways Equivalent to GL_ALWAYS. + \value CompareNever Equivalent to GL_NEVER. + +*/ + +/*! + \since 5.5 + + Sets the texture comparison function on this texture to \a function. The texture + comparison function is used by shadow samplers when sampling a depth texture. + + \sa comparisonFunction() +*/ +void QOpenGLTexture::setComparisonFunction(QOpenGLTexture::ComparisonFunction function) +{ + Q_D(QOpenGLTexture); + d->create(); + if (!d->features.testFlag(TextureComparisonOperators)) { + qWarning("QOpenGLTexture::setComparisonFunction: requires OpenGL >= 1.5 or OpenGL ES >= 3.0"); + return; + } + d->comparisonFunction = function; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_COMPARE_FUNC, function); +} + +/*! + \since 5.5 + + Returns the texture comparison operator set on this texture. By default, a + texture has a CompareLessEqual comparison function. + + \sa setComparisonFunction() +*/ +QOpenGLTexture::ComparisonFunction QOpenGLTexture::comparisonFunction() const +{ + Q_D(const QOpenGLTexture); + return d->comparisonFunction; +} + +/*! + \enum QOpenGLTexture::ComparisonMode + \since 5.5 + This enum specifies which comparison mode is used when sampling this texture. + + \value CompareRefToTexture Equivalent to GL_COMPARE_REF_TO_TEXTURE. + \value CompareNone Equivalent to GL_NONE. +*/ + +/*! + \since 5.5 + + Sets the texture comparison mode on this texture to \a mode. The texture + comparison mode is used by shadow samplers when sampling a depth texture. + + \sa comparisonMode() +*/ +void QOpenGLTexture::setComparisonMode(QOpenGLTexture::ComparisonMode mode) +{ + Q_D(QOpenGLTexture); + d->create(); + if (!d->features.testFlag(TextureComparisonOperators)) { + qWarning("QOpenGLTexture::setComparisonMode: requires OpenGL >= 1.5 or OpenGL ES >= 3.0"); + return; + } + d->comparisonMode = mode; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_COMPARE_MODE, mode); +} + +/*! + \since 5.5 + + Returns the texture comparison mode set on this texture. By default, a + texture has a CompareNone comparison mode (i.e. comparisons are disabled). + + \sa setComparisonMode() +*/ +QOpenGLTexture::ComparisonMode QOpenGLTexture::comparisonMode() const +{ + Q_D(const QOpenGLTexture); + return d->comparisonMode; +} + +/*! + Sets the filter used for minification to \a filter. + + \sa minificationFilter(), setMagnificationFilter(), setMinMagFilters() +*/ +void QOpenGLTexture::setMinificationFilter(QOpenGLTexture::Filter filter) +{ + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + d->minFilter = filter; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MIN_FILTER, filter); +} + +/*! + Returns the minification filter. + + \sa setMinificationFilter() +*/ +QOpenGLTexture::Filter QOpenGLTexture::minificationFilter() const +{ + Q_D(const QOpenGLTexture); + return d->minFilter; +} + +/*! + Sets the magnification filter to \a filter. + + \sa magnificationFilter(), setMinificationFilter(), setMinMagFilters() +*/ +void QOpenGLTexture::setMagnificationFilter(QOpenGLTexture::Filter filter) +{ + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + d->magFilter = filter; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAG_FILTER, filter); +} + +/*! + Returns the magnification filter. + + \sa setMagnificationFilter() +*/ +QOpenGLTexture::Filter QOpenGLTexture::magnificationFilter() const +{ + Q_D(const QOpenGLTexture); + return d->magFilter; +} + +/*! + Sets the minification filter to \a minificationFilter and the magnification filter + to \a magnificationFilter. + + \sa minMagFilters(), setMinificationFilter(), setMagnificationFilter() +*/ +void QOpenGLTexture::setMinMagFilters(QOpenGLTexture::Filter minificationFilter, + QOpenGLTexture::Filter magnificationFilter) +{ + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + d->minFilter = minificationFilter; + d->magFilter = magnificationFilter; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MIN_FILTER, minificationFilter); + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAG_FILTER, magnificationFilter); +} + +/*! + Returns the current minification and magnification filters. + + \sa setMinMagFilters() +*/ +QPair<QOpenGLTexture::Filter, QOpenGLTexture::Filter> QOpenGLTexture::minMagFilters() const +{ + Q_D(const QOpenGLTexture); + return QPair<QOpenGLTexture::Filter, QOpenGLTexture::Filter>(d->minFilter, d->magFilter); +} + +/*! + If your OpenGL implementation supports the GL_EXT_texture_filter_anisotropic extension + this function sets the maximum anisotropy level to \a anisotropy. + + \sa maximumAnisotropy() +*/ +void QOpenGLTexture::setMaximumAnisotropy(float anisotropy) +{ + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + if (!d->features.testFlag(AnisotropicFiltering)) { + qWarning("QOpenGLTexture::setMaximumAnisotropy() requires GL_EXT_texture_filter_anisotropic"); + return; + } + d->maxAnisotropy = anisotropy; + d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy); +} + +/*! + Returns the maximum level of anisotropy to be accounted for when performing texture lookups. + This requires the GL_EXT_texture_filter_anisotropic extension. + + \sa setMaximumAnisotropy() +*/ +float QOpenGLTexture::maximumAnisotropy() const +{ + Q_D(const QOpenGLTexture); + return d->maxAnisotropy; +} + +/*! + Sets the wrap (or repeat mode) for all texture dimentions to \a mode. + + \sa wrapMode() +*/ +void QOpenGLTexture::setWrapMode(QOpenGLTexture::WrapMode mode) +{ + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + d->setWrapMode(mode); +} + +/*! + Holds the texture dimension \a direction. + \overload +*/ +void QOpenGLTexture::setWrapMode(QOpenGLTexture::CoordinateDirection direction, QOpenGLTexture::WrapMode mode) +{ + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + d->setWrapMode(direction, mode); +} + +/*! + Returns the wrap mode for the texture dimension \a direction. + + \sa setWrapMode() +*/ +QOpenGLTexture::WrapMode QOpenGLTexture::wrapMode(QOpenGLTexture::CoordinateDirection direction) const +{ + Q_D(const QOpenGLTexture); + return d->wrapMode(direction); +} + +/*! + Sets the border color of the texture to \a color. + + \note This function has no effect on Mac and Qt built for OpenGL ES 2. + \sa borderColor() +*/ +void QOpenGLTexture::setBorderColor(const QColor &color) +{ + setBorderColor(static_cast<float>(color.redF()), static_cast<float>(color.greenF()), + static_cast<float>(color.blueF()), static_cast<float>(color.alphaF())); +} + +/*! + Sets the color red to \a {r}, green to \a {g}, blue to \a {b}, and \a {a} to the + alpha value. + \overload +*/ +void QOpenGLTexture::setBorderColor(float r, float g, float b, float a) +{ +#if !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + float values[4]; + values[0] = r; + values[1] = g; + values[2] = b; + values[3] = a; + d->borderColor.clear(); + for (int i = 0; i < 4; ++i) + d->borderColor.append(QVariant(values[i])); + d->texFuncs->glTextureParameterfv(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BORDER_COLOR, values); + return; + } +#else + Q_UNUSED(r); + Q_UNUSED(g); + Q_UNUSED(b); + Q_UNUSED(a); +#endif + qWarning("QOpenGLTexture: Border color is not supported"); +} + +/*! + Sets the color red to \a {r}, green to \a {g}, blue to \a {b}, and the alpha + value to \a {a}. + \overload +*/ +void QOpenGLTexture::setBorderColor(int r, int g, int b, int a) +{ +#if !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + int values[4]; + values[0] = r; + values[1] = g; + values[2] = b; + values[3] = a; + d->borderColor.clear(); + for (int i = 0; i < 4; ++i) + d->borderColor.append(QVariant(values[i])); + d->texFuncs->glTextureParameteriv(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BORDER_COLOR, values); + return; + } +#else + Q_UNUSED(r); + Q_UNUSED(g); + Q_UNUSED(b); + Q_UNUSED(a); +#endif + qWarning("QOpenGLTexture: Border color is not supported"); + + // TODO Handle case of using glTextureParameterIiv() based on format +} + +/*! + Sets the color red to \a {r}, green to \a {g}, blue to \a {b}, and the alpha + value to \a {a}. + \overload +*/ +void QOpenGLTexture::setBorderColor(uint r, uint g, uint b, uint a) +{ +#if !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + int values[4]; + values[0] = int(r); + values[1] = int(g); + values[2] = int(b); + values[3] = int(a); + d->borderColor.clear(); + for (int i = 0; i < 4; ++i) + d->borderColor.append(QVariant(values[i])); + d->texFuncs->glTextureParameteriv(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BORDER_COLOR, values); + return; + } +#else + Q_UNUSED(r); + Q_UNUSED(g); + Q_UNUSED(b); + Q_UNUSED(a); +#endif + qWarning("QOpenGLTexture: Border color is not supported"); + + // TODO Handle case of using glTextureParameterIuiv() based on format +} + +/*! + Returns the borderColor of this texture. + + \sa setBorderColor() +*/ +QColor QOpenGLTexture::borderColor() const +{ + Q_D(const QOpenGLTexture); + QColor c(0.0f, 0.0f, 0.0f, 0.0f); + if (!d->borderColor.isEmpty()) { + c.setRedF(d->borderColor.at(0).toFloat()); + c.setGreenF(d->borderColor.at(1).toFloat()); + c.setBlueF(d->borderColor.at(2).toFloat()); + c.setAlphaF(d->borderColor.at(3).toFloat()); + } + return c; +} + +/*! + Writes the texture border color into the first four elements + of the array pointed to by \a border. + + \sa setBorderColor() +*/ +void QOpenGLTexture::borderColor(float *border) const +{ + Q_D(const QOpenGLTexture); + Q_ASSERT(border); + if (d->borderColor.isEmpty()) { + for (int i = 0; i < 4; ++i) + border[i] = 0.0f; + } else { + for (int i = 0; i < 4; ++i) + border[i] = d->borderColor.at(i).toFloat(); + } +} + +/*! + Writes the texture border color into the first four elements + of the array pointed to by \a border. + + \overload +*/ +void QOpenGLTexture::borderColor(int *border) const +{ + Q_D(const QOpenGLTexture); + Q_ASSERT(border); + if (d->borderColor.isEmpty()) { + for (int i = 0; i < 4; ++i) + border[i] = 0; + } else { + for (int i = 0; i < 4; ++i) + border[i] = d->borderColor.at(i).toInt(); + } +} + +/*! + Writes the texture border color into the first four elements + of the array pointed to by \a border. + + \overload +*/ +void QOpenGLTexture::borderColor(unsigned int *border) const +{ + Q_D(const QOpenGLTexture); + Q_ASSERT(border); + if (d->borderColor.isEmpty()) { + for (int i = 0; i < 4; ++i) + border[i] = 0; + } else { + for (int i = 0; i < 4; ++i) + border[i] = d->borderColor.at(i).toUInt(); + } +} + +/*! + Sets the minimum level of detail to \a value. This limits the selection of highest + resolution mipmap (lowest mipmap level). The default value is -1000. + + \note This function has no effect on Qt built for OpenGL ES 2. + \sa minimumLevelOfDetail(), setMaximumLevelOfDetail(), setLevelOfDetailRange() +*/ +void QOpenGLTexture::setMinimumLevelOfDetail(float value) +{ +#if !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + Q_ASSERT(value < d->maxLevelOfDetail); + d->minLevelOfDetail = value; + d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MIN_LOD, value); + return; + } +#else + Q_UNUSED(value); +#endif + qWarning("QOpenGLTexture: Detail level is not supported"); +} + +/*! + Returns the minimum level of detail parameter. + + \sa setMinimumLevelOfDetail(), maximumLevelOfDetail(), levelOfDetailRange() +*/ +float QOpenGLTexture::minimumLevelOfDetail() const +{ + Q_D(const QOpenGLTexture); + return d->minLevelOfDetail; +} + +/*! + Sets the maximum level of detail to \a value. This limits the selection of lowest + resolution mipmap (highest mipmap level). The default value is 1000. + + \note This function has no effect on Qt built for OpenGL ES 2. + \sa maximumLevelOfDetail(), setMinimumLevelOfDetail(), setLevelOfDetailRange() +*/ +void QOpenGLTexture::setMaximumLevelOfDetail(float value) +{ +#if !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + Q_ASSERT(value > d->minLevelOfDetail); + d->maxLevelOfDetail = value; + d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_LOD, value); + return; + } +#else + Q_UNUSED(value); +#endif + qWarning("QOpenGLTexture: Detail level is not supported"); +} + +/*! + Returns the maximum level of detail parameter. + + \sa setMaximumLevelOfDetail(), minimumLevelOfDetail(), levelOfDetailRange() +*/ +float QOpenGLTexture::maximumLevelOfDetail() const +{ + Q_D(const QOpenGLTexture); + return d->maxLevelOfDetail; +} + +/*! + Sets the minimum level of detail parameters to \a min and the maximum level + to \a max. + \note This function has no effect on Qt built for OpenGL ES 2. + \sa levelOfDetailRange(), setMinimumLevelOfDetail(), setMaximumLevelOfDetail() +*/ +void QOpenGLTexture::setLevelOfDetailRange(float min, float max) +{ +#if !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + Q_ASSERT(min < max); + d->minLevelOfDetail = min; + d->maxLevelOfDetail = max; + d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MIN_LOD, min); + d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_LOD, max); + return; + } +#else + Q_UNUSED(min); + Q_UNUSED(max); +#endif + qWarning("QOpenGLTexture: Detail level is not supported"); +} + +/*! + Returns the minimum and maximum level of detail parameters. + + \sa setLevelOfDetailRange(), minimumLevelOfDetail(), maximumLevelOfDetail() +*/ +QPair<float, float> QOpenGLTexture::levelOfDetailRange() const +{ + Q_D(const QOpenGLTexture); + return qMakePair(d->minLevelOfDetail, d->maxLevelOfDetail); +} + +/*! + Sets the level of detail bias to \a bias. + Level of detail bias affects the point at which mipmapping levels change. + Increasing values for level of detail bias makes the overall images blurrier + or smoother. Decreasing values make the overall images sharper. + + \note This function has no effect on Qt built for OpenGL ES 2. + \sa levelofDetailBias() +*/ +void QOpenGLTexture::setLevelofDetailBias(float bias) +{ +#if !defined(QT_OPENGL_ES_2) + if (!QOpenGLContext::currentContext()->isOpenGLES()) { + Q_D(QOpenGLTexture); + d->create(); + Q_ASSERT(d->texFuncs); + Q_ASSERT(d->textureId); + d->levelOfDetailBias = bias; + d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_LOD_BIAS, bias); + return; + } +#else + Q_UNUSED(bias); +#endif + qWarning("QOpenGLTexture: Detail level is not supported"); +} + +/*! + Returns the level of detail bias parameter. + + \sa setLevelofDetailBias() +*/ +float QOpenGLTexture::levelofDetailBias() const +{ + Q_D(const QOpenGLTexture); + return d->levelOfDetailBias; +} + +#ifndef QT_NO_DEBUG_STREAM +QDebug operator<<(QDebug debug, const QOpenGLTexture *t) +{ + QDebugStateSaver saver(debug); + debug.nospace(); + debug << "QOpenGLTexture("; + if (t) { + const QOpenGLTexturePrivate *d = t->d_ptr.data(); + debug << d->target << ", bindingTarget=" << d->bindingTarget + << ", size=[" << d->dimensions[0] + << ", " << d->dimensions[1]; + if (d->target == QOpenGLTexture::Target3D) + debug << ", " << d->dimensions[2]; + debug << "], format=" << d->format << ", formatClass=" << d->formatClass; + if (t->isCreated()) + debug << ", textureId=" << d->textureId; + if (t->isBound()) + debug << ", [bound]"; + if (t->isTextureView()) + debug << ", [view]"; + if (d->fixedSamplePositions) + debug << ", [fixedSamplePositions]"; + debug << ", mipLevels=" << d->requestedMipLevels << ", layers=" << d->layers + << ", faces=" << d->faces << ", samples=" << d->samples + << ", depthStencilMode=" << d->depthStencilMode << ", comparisonFunction=" + << d->comparisonFunction << ", comparisonMode=" << d->comparisonMode + << ", features=" << d->features << ", minificationFilter=" << d->minFilter + << ", magnificationFilter=" << d->magFilter << ", wrapMode=" << d->wrapModes[0]; + } else { + debug << '0'; + } + debug << ')'; + return debug; +} +#endif // QT_NO_DEBUG_STREAM + +QT_END_NAMESPACE diff --git a/src/opengl/qopengltexture.h b/src/opengl/qopengltexture.h new file mode 100644 index 0000000000..8eba2724df --- /dev/null +++ b/src/opengl/qopengltexture.h @@ -0,0 +1,663 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB). +** 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 QOPENGLABSTRACTTEXTURE_H +#define QOPENGLABSTRACTTEXTURE_H + +#include <QtOpenGL/qtopenglglobal.h> + +#ifndef QT_NO_OPENGL + +#include <QtGui/qopengl.h> +#include <QtGui/qimage.h> +#include <QtCore/QScopedPointer> + +QT_BEGIN_NAMESPACE + +class QDebug; +class QOpenGLTexturePrivate; +class QOpenGLPixelTransferOptions; + +class Q_OPENGL_EXPORT QOpenGLTexture +{ + Q_GADGET +public: + enum Target { + Target1D = 0x0DE0, // GL_TEXTURE_1D + Target1DArray = 0x8C18, // GL_TEXTURE_1D_ARRAY + Target2D = 0x0DE1, // GL_TEXTURE_2D + Target2DArray = 0x8C1A, // GL_TEXTURE_2D_ARRAY + Target3D = 0x806F, // GL_TEXTURE_3D + TargetCubeMap = 0x8513, // GL_TEXTURE_CUBE_MAP + TargetCubeMapArray = 0x9009, // GL_TEXTURE_CUBE_MAP_ARRAY + Target2DMultisample = 0x9100, // GL_TEXTURE_2D_MULTISAMPLE + Target2DMultisampleArray = 0x9102, // GL_TEXTURE_2D_MULTISAMPLE_ARRAY + TargetRectangle = 0x84F5, // GL_TEXTURE_RECTANGLE + TargetBuffer = 0x8C2A // GL_TEXTURE_BUFFER + }; + Q_ENUM(Target) + + enum BindingTarget { + BindingTarget1D = 0x8068, // GL_TEXTURE_BINDING_1D + BindingTarget1DArray = 0x8C1C, // GL_TEXTURE_BINDING_1D_ARRAY + BindingTarget2D = 0x8069, // GL_TEXTURE_BINDING_2D + BindingTarget2DArray = 0x8C1D, // GL_TEXTURE_BINDING_2D_ARRAY + BindingTarget3D = 0x806A, // GL_TEXTURE_BINDING_3D + BindingTargetCubeMap = 0x8514, // GL_TEXTURE_BINDING_CUBE_MAP + BindingTargetCubeMapArray = 0x900A, // GL_TEXTURE_BINDING_CUBE_MAP_ARRAY + BindingTarget2DMultisample = 0x9104, // GL_TEXTURE_BINDING_2D_MULTISAMPLE + BindingTarget2DMultisampleArray = 0x9105, // GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY + BindingTargetRectangle = 0x84F6, // GL_TEXTURE_BINDING_RECTANGLE + BindingTargetBuffer = 0x8C2C // GL_TEXTURE_BINDING_BUFFER + }; + Q_ENUM(BindingTarget) + + enum MipMapGeneration { + GenerateMipMaps, + DontGenerateMipMaps + }; + Q_ENUM(MipMapGeneration) + + enum TextureUnitReset { + ResetTextureUnit, + DontResetTextureUnit + }; + Q_ENUM(TextureUnitReset) + + enum TextureFormat { + NoFormat = 0, // GL_NONE + + // Unsigned normalized formats + R8_UNorm = 0x8229, // GL_R8 + RG8_UNorm = 0x822B, // GL_RG8 + RGB8_UNorm = 0x8051, // GL_RGB8 + RGBA8_UNorm = 0x8058, // GL_RGBA8 + + R16_UNorm = 0x822A, // GL_R16 + RG16_UNorm = 0x822C, // GL_RG16 + RGB16_UNorm = 0x8054, // GL_RGB16 + RGBA16_UNorm = 0x805B, // GL_RGBA16 + + // Signed normalized formats + R8_SNorm = 0x8F94, // GL_R8_SNORM + RG8_SNorm = 0x8F95, // GL_RG8_SNORM + RGB8_SNorm = 0x8F96, // GL_RGB8_SNORM + RGBA8_SNorm = 0x8F97, // GL_RGBA8_SNORM + + R16_SNorm = 0x8F98, // GL_R16_SNORM + RG16_SNorm = 0x8F99, // GL_RG16_SNORM + RGB16_SNorm = 0x8F9A, // GL_RGB16_SNORM + RGBA16_SNorm = 0x8F9B, // GL_RGBA16_SNORM + + // Unsigned integer formats + R8U = 0x8232, // GL_R8UI + RG8U = 0x8238, // GL_RG8UI + RGB8U = 0x8D7D, // GL_RGB8UI + RGBA8U = 0x8D7C, // GL_RGBA8UI + + R16U = 0x8234, // GL_R16UI + RG16U = 0x823A, // GL_RG16UI + RGB16U = 0x8D77, // GL_RGB16UI + RGBA16U = 0x8D76, // GL_RGBA16UI + + R32U = 0x8236, // GL_R32UI + RG32U = 0x823C, // GL_RG32UI + RGB32U = 0x8D71, // GL_RGB32UI + RGBA32U = 0x8D70, // GL_RGBA32UI + + // Signed integer formats + R8I = 0x8231, // GL_R8I + RG8I = 0x8237, // GL_RG8I + RGB8I = 0x8D8F, // GL_RGB8I + RGBA8I = 0x8D8E, // GL_RGBA8I + + R16I = 0x8233, // GL_R16I + RG16I = 0x8239, // GL_RG16I + RGB16I = 0x8D89, // GL_RGB16I + RGBA16I = 0x8D88, // GL_RGBA16I + + R32I = 0x8235, // GL_R32I + RG32I = 0x823B, // GL_RG32I + RGB32I = 0x8D83, // GL_RGB32I + RGBA32I = 0x8D82, // GL_RGBA32I + + // Floating point formats + R16F = 0x822D, // GL_R16F + RG16F = 0x822F, // GL_RG16F + RGB16F = 0x881B, // GL_RGB16F + RGBA16F = 0x881A, // GL_RGBA16F + + R32F = 0x822E, // GL_R32F + RG32F = 0x8230, // GL_RG32F + RGB32F = 0x8815, // GL_RGB32F + RGBA32F = 0x8814, // GL_RGBA32F + + // Packed formats + RGB9E5 = 0x8C3D, // GL_RGB9_E5 + RG11B10F = 0x8C3A, // GL_R11F_G11F_B10F + RG3B2 = 0x2A10, // GL_R3_G3_B2 + R5G6B5 = 0x8D62, // GL_RGB565 + RGB5A1 = 0x8057, // GL_RGB5_A1 + RGBA4 = 0x8056, // GL_RGBA4 + RGB10A2 = 0x906F, // GL_RGB10_A2UI + + // Depth formats + D16 = 0x81A5, // GL_DEPTH_COMPONENT16 + D24 = 0x81A6, // GL_DEPTH_COMPONENT24 + D24S8 = 0x88F0, // GL_DEPTH24_STENCIL8 + D32 = 0x81A7, // GL_DEPTH_COMPONENT32 + D32F = 0x8CAC, // GL_DEPTH_COMPONENT32F + D32FS8X24 = 0x8CAD, // GL_DEPTH32F_STENCIL8 + S8 = 0x8D48, // GL_STENCIL_INDEX8 + + // Compressed formats + RGB_DXT1 = 0x83F0, // GL_COMPRESSED_RGB_S3TC_DXT1_EXT + RGBA_DXT1 = 0x83F1, // GL_COMPRESSED_RGBA_S3TC_DXT1_EXT + RGBA_DXT3 = 0x83F2, // GL_COMPRESSED_RGBA_S3TC_DXT3_EXT + RGBA_DXT5 = 0x83F3, // GL_COMPRESSED_RGBA_S3TC_DXT5_EXT + R_ATI1N_UNorm = 0x8DBB, // GL_COMPRESSED_RED_RGTC1 + R_ATI1N_SNorm = 0x8DBC, // GL_COMPRESSED_SIGNED_RED_RGTC1 + RG_ATI2N_UNorm = 0x8DBD, // GL_COMPRESSED_RG_RGTC2 + RG_ATI2N_SNorm = 0x8DBE, // GL_COMPRESSED_SIGNED_RG_RGTC2 + RGB_BP_UNSIGNED_FLOAT = 0x8E8F, // GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB + RGB_BP_SIGNED_FLOAT = 0x8E8E, // GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB + RGB_BP_UNorm = 0x8E8C, // GL_COMPRESSED_RGBA_BPTC_UNORM_ARB + R11_EAC_UNorm = 0x9270, // GL_COMPRESSED_R11_EAC + R11_EAC_SNorm = 0x9271, // GL_COMPRESSED_SIGNED_R11_EAC + RG11_EAC_UNorm = 0x9272, // GL_COMPRESSED_RG11_EAC + RG11_EAC_SNorm = 0x9273, // GL_COMPRESSED_SIGNED_RG11_EAC + RGB8_ETC2 = 0x9274, // GL_COMPRESSED_RGB8_ETC2 + SRGB8_ETC2 = 0x9275, // GL_COMPRESSED_SRGB8_ETC2 + RGB8_PunchThrough_Alpha1_ETC2 = 0x9276, // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 + SRGB8_PunchThrough_Alpha1_ETC2 = 0x9277, // GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 + RGBA8_ETC2_EAC = 0x9278, // GL_COMPRESSED_RGBA8_ETC2_EAC + SRGB8_Alpha8_ETC2_EAC = 0x9279, // GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC + RGB8_ETC1 = 0x8D64, // GL_ETC1_RGB8_OES + RGBA_ASTC_4x4 = 0x93B0, // GL_COMPRESSED_RGBA_ASTC_4x4_KHR + RGBA_ASTC_5x4 = 0x93B1, // GL_COMPRESSED_RGBA_ASTC_5x4_KHR + RGBA_ASTC_5x5 = 0x93B2, // GL_COMPRESSED_RGBA_ASTC_5x5_KHR + RGBA_ASTC_6x5 = 0x93B3, // GL_COMPRESSED_RGBA_ASTC_6x5_KHR + RGBA_ASTC_6x6 = 0x93B4, // GL_COMPRESSED_RGBA_ASTC_6x6_KHR + RGBA_ASTC_8x5 = 0x93B5, // GL_COMPRESSED_RGBA_ASTC_8x5_KHR + RGBA_ASTC_8x6 = 0x93B6, // GL_COMPRESSED_RGBA_ASTC_8x6_KHR + RGBA_ASTC_8x8 = 0x93B7, // GL_COMPRESSED_RGBA_ASTC_8x8_KHR + RGBA_ASTC_10x5 = 0x93B8, // GL_COMPRESSED_RGBA_ASTC_10x5_KHR + RGBA_ASTC_10x6 = 0x93B9, // GL_COMPRESSED_RGBA_ASTC_10x6_KHR + RGBA_ASTC_10x8 = 0x93BA, // GL_COMPRESSED_RGBA_ASTC_10x8_KHR + RGBA_ASTC_10x10 = 0x93BB, // GL_COMPRESSED_RGBA_ASTC_10x10_KHR + RGBA_ASTC_12x10 = 0x93BC, // GL_COMPRESSED_RGBA_ASTC_12x10_KHR + RGBA_ASTC_12x12 = 0x93BD, // GL_COMPRESSED_RGBA_ASTC_12x12_KHR + SRGB8_Alpha8_ASTC_4x4 = 0x93D0, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR + SRGB8_Alpha8_ASTC_5x4 = 0x93D1, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR + SRGB8_Alpha8_ASTC_5x5 = 0x93D2, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR + SRGB8_Alpha8_ASTC_6x5 = 0x93D3, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR + SRGB8_Alpha8_ASTC_6x6 = 0x93D4, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR + SRGB8_Alpha8_ASTC_8x5 = 0x93D5, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR + SRGB8_Alpha8_ASTC_8x6 = 0x93D6, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR + SRGB8_Alpha8_ASTC_8x8 = 0x93D7, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR + SRGB8_Alpha8_ASTC_10x5 = 0x93D8, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR + SRGB8_Alpha8_ASTC_10x6 = 0x93D9, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR + SRGB8_Alpha8_ASTC_10x8 = 0x93DA, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR + SRGB8_Alpha8_ASTC_10x10 = 0x93DB, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR + SRGB8_Alpha8_ASTC_12x10 = 0x93DC, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR + SRGB8_Alpha8_ASTC_12x12 = 0x93DD, // GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR + + // sRGB formats + SRGB8 = 0x8C41, // GL_SRGB8 + SRGB8_Alpha8 = 0x8C43, // GL_SRGB8_ALPHA8 + SRGB_DXT1 = 0x8C4C, // GL_COMPRESSED_SRGB_S3TC_DXT1_EXT + SRGB_Alpha_DXT1 = 0x8C4D, // GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT + SRGB_Alpha_DXT3 = 0x8C4E, // GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT + SRGB_Alpha_DXT5 = 0x8C4F, // GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT + SRGB_BP_UNorm = 0x8E8D, // GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB + + // ES 2 formats + DepthFormat = 0x1902, // GL_DEPTH_COMPONENT + AlphaFormat = 0x1906, // GL_ALPHA + RGBFormat = 0x1907, // GL_RGB + RGBAFormat = 0x1908, // GL_RGBA + LuminanceFormat = 0x1909, // GL_LUMINANCE + LuminanceAlphaFormat = 0x190A + + }; + Q_ENUM(TextureFormat) + + // This is not used externally yet but is reserved to allow checking of + // compatibility between texture formats +#ifndef Q_QDOC + enum TextureFormatClass { + NoFormatClass, + FormatClass_128Bit, + FormatClass_96Bit, + FormatClass_64Bit, + FormatClass_48Bit, + FormatClass_32Bit, + FormatClass_24Bit, + FormatClass_16Bit, + FormatClass_8Bit, + FormatClass_RGTC1_R, + FormatClass_RGTC2_RG, + FormatClass_BPTC_Unorm, + FormatClass_BPTC_Float, + FormatClass_S3TC_DXT1_RGB, + FormatClass_S3TC_DXT1_RGBA, + FormatClass_S3TC_DXT3_RGBA, + FormatClass_S3TC_DXT5_RGBA, + FormatClass_Unique + }; +#endif + + enum CubeMapFace { + CubeMapPositiveX = 0x8515, // GL_TEXTURE_CUBE_MAP_POSITIVE_X + CubeMapNegativeX = 0x8516, // GL_TEXTURE_CUBE_MAP_NEGATIVE_X + CubeMapPositiveY = 0x8517, // GL_TEXTURE_CUBE_MAP_POSITIVE_Y + CubeMapNegativeY = 0x8518, // GL_TEXTURE_CUBE_MAP_NEGATIVE_Y + CubeMapPositiveZ = 0x8519, // GL_TEXTURE_CUBE_MAP_POSITIVE_Z + CubeMapNegativeZ = 0x851A // GL_TEXTURE_CUBE_MAP_NEGATIVE_Z + }; + Q_ENUM(CubeMapFace) + + enum PixelFormat { + NoSourceFormat = 0, // GL_NONE + Red = 0x1903, // GL_RED + RG = 0x8227, // GL_RG + RGB = 0x1907, // GL_RGB + BGR = 0x80E0, // GL_BGR + RGBA = 0x1908, // GL_RGBA + BGRA = 0x80E1, // GL_BGRA + Red_Integer = 0x8D94, // GL_RED_INTEGER + RG_Integer = 0x8228, // GL_RG_INTEGER + RGB_Integer = 0x8D98, // GL_RGB_INTEGER + BGR_Integer = 0x8D9A, // GL_BGR_INTEGER + RGBA_Integer = 0x8D99, // GL_RGBA_INTEGER + BGRA_Integer = 0x8D9B, // GL_BGRA_INTEGER + Stencil = 0x1901, // GL_STENCIL_INDEX + Depth = 0x1902, // GL_DEPTH_COMPONENT + DepthStencil = 0x84F9, // GL_DEPTH_STENCIL + Alpha = 0x1906, // GL_ALPHA + Luminance = 0x1909, // GL_LUMINANCE + LuminanceAlpha = 0x190A // GL_LUMINANCE_ALPHA + }; + Q_ENUM(PixelFormat) + + enum PixelType { + NoPixelType = 0, // GL_NONE + Int8 = 0x1400, // GL_BYTE + UInt8 = 0x1401, // GL_UNSIGNED_BYTE + Int16 = 0x1402, // GL_SHORT + UInt16 = 0x1403, // GL_UNSIGNED_SHORT + Int32 = 0x1404, // GL_INT + UInt32 = 0x1405, // GL_UNSIGNED_INT + Float16 = 0x140B, // GL_HALF_FLOAT + Float16OES = 0x8D61, // GL_HALF_FLOAT_OES + Float32 = 0x1406, // GL_FLOAT + UInt32_RGB9_E5 = 0x8C3E, // GL_UNSIGNED_INT_5_9_9_9_REV + UInt32_RG11B10F = 0x8C3B, // GL_UNSIGNED_INT_10F_11F_11F_REV + UInt8_RG3B2 = 0x8032, // GL_UNSIGNED_BYTE_3_3_2 + UInt8_RG3B2_Rev = 0x8362, // GL_UNSIGNED_BYTE_2_3_3_REV + UInt16_RGB5A1 = 0x8034, // GL_UNSIGNED_SHORT_5_5_5_1 + UInt16_RGB5A1_Rev = 0x8366, // GL_UNSIGNED_SHORT_1_5_5_5_REV + UInt16_R5G6B5 = 0x8363, // GL_UNSIGNED_SHORT_5_6_5 + UInt16_R5G6B5_Rev = 0x8364, // GL_UNSIGNED_SHORT_5_6_5_REV + UInt16_RGBA4 = 0x8033, // GL_UNSIGNED_SHORT_4_4_4_4 + UInt16_RGBA4_Rev = 0x8365, // GL_UNSIGNED_SHORT_4_4_4_4_REV + UInt32_RGBA8 = 0x8035, // GL_UNSIGNED_INT_8_8_8_8 + UInt32_RGBA8_Rev = 0x8367, // GL_UNSIGNED_INT_8_8_8_8_REV + UInt32_RGB10A2 = 0x8036, // GL_UNSIGNED_INT_10_10_10_2 + UInt32_RGB10A2_Rev = 0x8368, // GL_UNSIGNED_INT_2_10_10_10_REV + UInt32_D24S8 = 0x84FA, // GL_UNSIGNED_INT_24_8 + Float32_D32_UInt32_S8_X24 = 0x8DAD // GL_FLOAT_32_UNSIGNED_INT_24_8_REV + }; + Q_ENUM(PixelType) + + enum SwizzleComponent { + SwizzleRed = 0x8E42, // GL_TEXTURE_SWIZZLE_R + SwizzleGreen = 0x8E43, // GL_TEXTURE_SWIZZLE_G + SwizzleBlue = 0x8E44, // GL_TEXTURE_SWIZZLE_B + SwizzleAlpha = 0x8E45 // GL_TEXTURE_SWIZZLE_A + }; + Q_ENUM(SwizzleComponent) + + enum SwizzleValue { + RedValue = 0x1903, // GL_RED + GreenValue = 0x1904, // GL_GREEN + BlueValue = 0x1905, // GL_BLUE + AlphaValue = 0x1906, // GL_ALPHA + ZeroValue = 0, // GL_ZERO + OneValue = 1 // GL_ONE + }; + Q_ENUM(SwizzleValue) + + enum WrapMode { + Repeat = 0x2901, // GL_REPEAT + MirroredRepeat = 0x8370, // GL_MIRRORED_REPEAT + ClampToEdge = 0x812F, // GL_CLAMP_TO_EDGE + ClampToBorder = 0x812D // GL_CLAMP_TO_BORDER + }; + Q_ENUM(WrapMode) + + enum CoordinateDirection { + DirectionS = 0x2802, // GL_TEXTURE_WRAP_S + DirectionT = 0x2803, // GL_TEXTURE_WRAP_T + DirectionR = 0x8072 // GL_TEXTURE_WRAP_R + }; + Q_ENUM(CoordinateDirection) + + // Features + enum Feature { + ImmutableStorage = 0x00000001, + ImmutableMultisampleStorage = 0x00000002, + TextureRectangle = 0x00000004, + TextureArrays = 0x00000008, + Texture3D = 0x00000010, + TextureMultisample = 0x00000020, + TextureBuffer = 0x00000040, + TextureCubeMapArrays = 0x00000080, + Swizzle = 0x00000100, + StencilTexturing = 0x00000200, + AnisotropicFiltering = 0x00000400, + NPOTTextures = 0x00000800, + NPOTTextureRepeat = 0x00001000, + Texture1D = 0x00002000, + TextureComparisonOperators = 0x00004000, + TextureMipMapLevel = 0x00008000, +#ifndef Q_QDOC + MaxFeatureFlag = 0x00010000 +#endif + }; + Q_DECLARE_FLAGS(Features, Feature) + Q_ENUM(Feature) + + explicit QOpenGLTexture(Target target); + explicit QOpenGLTexture(const QImage& image, MipMapGeneration genMipMaps = GenerateMipMaps); + ~QOpenGLTexture(); + + Target target() const; + + // Creation and destruction + bool create(); + void destroy(); + bool isCreated() const; + GLuint textureId() const; + + // Binding and releasing + void bind(); + void bind(uint unit, TextureUnitReset reset = DontResetTextureUnit); + void release(); + void release(uint unit, TextureUnitReset reset = DontResetTextureUnit); + + bool isBound() const; + bool isBound(uint unit); + static GLuint boundTextureId(BindingTarget target); + static GLuint boundTextureId(uint unit, BindingTarget target); + + // Storage allocation + void setFormat(TextureFormat format); + TextureFormat format() const; + void setSize(int width, int height = 1, int depth = 1); + int width() const; + int height() const; + int depth() const; + void setMipLevels(int levels); + int mipLevels() const; + int maximumMipLevels() const; + void setLayers(int layers); + int layers() const; + int faces() const; + void setSamples(int samples); + int samples() const; + void setFixedSamplePositions(bool fixed); + bool isFixedSamplePositions() const; + void allocateStorage(); + void allocateStorage(PixelFormat pixelFormat, PixelType pixelType); + bool isStorageAllocated() const; + + QOpenGLTexture *createTextureView(Target target, + TextureFormat viewFormat, + int minimumMipmapLevel, int maximumMipmapLevel, + int minimumLayer, int maximumLayer) const; + bool isTextureView() const; + + // Pixel transfer + // ### Qt 6: remove the non-const void * overloads +#if QT_DEPRECATED_SINCE(5, 3) + QT_DEPRECATED void setData(int mipLevel, int layer, CubeMapFace cubeFace, + PixelFormat sourceFormat, PixelType sourceType, + void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + QT_DEPRECATED void setData(int mipLevel, int layer, + PixelFormat sourceFormat, PixelType sourceType, + void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + QT_DEPRECATED void setData(int mipLevel, + PixelFormat sourceFormat, PixelType sourceType, + void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + QT_DEPRECATED void setData(PixelFormat sourceFormat, PixelType sourceType, + void *data, const QOpenGLPixelTransferOptions * const options = nullptr); +#endif // QT_DEPRECATED_SINCE(5, 3) + + void setData(int mipLevel, int layer, CubeMapFace cubeFace, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + void setData(int mipLevel, int layer, int layerCount, CubeMapFace cubeFace, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + void setData(int mipLevel, int layer, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + void setData(int mipLevel, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + void setData(PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + + void setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + void setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, int mipLevel, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + void setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + int mipLevel, int layer, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + void setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + int mipLevel, int layer, + CubeMapFace cubeFace, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + void setData(int xOffset, int yOffset, int zOffset, + int width, int height, int depth, + int mipLevel, int layer, + CubeMapFace cubeFace, int layerCount, + PixelFormat sourceFormat, PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options = nullptr); + + // Compressed data upload + // ### Qt 6: remove the non-const void * overloads +#if QT_DEPRECATED_SINCE(5, 3) + QT_DEPRECATED void setCompressedData(int mipLevel, int layer, CubeMapFace cubeFace, + int dataSize, void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); + QT_DEPRECATED void setCompressedData(int mipLevel, int layer, + int dataSize, void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); + QT_DEPRECATED void setCompressedData(int mipLevel, int dataSize, void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); + QT_DEPRECATED void setCompressedData(int dataSize, void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); +#endif // QT_DEPRECATED_SINCE(5, 3) + + void setCompressedData(int mipLevel, int layer, CubeMapFace cubeFace, + int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); + void setCompressedData(int mipLevel, int layer, int layerCount, CubeMapFace cubeFace, + int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); + void setCompressedData(int mipLevel, int layer, + int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); + void setCompressedData(int mipLevel, int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); + void setCompressedData(int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options = nullptr); + + // Helpful overloads for setData + void setData(const QImage& image, MipMapGeneration genMipMaps = GenerateMipMaps); + + static bool hasFeature(Feature feature); + + // Texture Parameters + void setMipBaseLevel(int baseLevel); + int mipBaseLevel() const; + void setMipMaxLevel(int maxLevel); + int mipMaxLevel() const; + void setMipLevelRange(int baseLevel, int maxLevel); + QPair<int, int> mipLevelRange() const; + + void setAutoMipMapGenerationEnabled(bool enabled); + bool isAutoMipMapGenerationEnabled() const; + + void generateMipMaps(); + void generateMipMaps(int baseLevel, bool resetBaseLevel = true); + + void setSwizzleMask(SwizzleComponent component, SwizzleValue value); + void setSwizzleMask(SwizzleValue r, SwizzleValue g, + SwizzleValue b, SwizzleValue a); + SwizzleValue swizzleMask(SwizzleComponent component) const; + + enum DepthStencilMode { + DepthMode = 0x1902, // GL_DEPTH_COMPONENT + StencilMode = 0x1901 // GL_STENCIL_INDEX + }; + Q_ENUM(DepthStencilMode) + + void setDepthStencilMode(DepthStencilMode mode); + DepthStencilMode depthStencilMode() const; + + enum ComparisonFunction { + CompareLessEqual = 0x0203, // GL_LEQUAL + CompareGreaterEqual = 0x0206, // GL_GEQUAL + CompareLess = 0x0201, // GL_LESS + CompareGreater = 0x0204, // GL_GREATER + CompareEqual = 0x0202, // GL_EQUAL + CommpareNotEqual = 0x0205, // GL_NOTEQUAL + CompareAlways = 0x0207, // GL_ALWAYS + CompareNever = 0x0200 // GL_NEVER + }; + Q_ENUM(ComparisonFunction) + + void setComparisonFunction(ComparisonFunction function); + ComparisonFunction comparisonFunction() const; + + enum ComparisonMode { + CompareRefToTexture = 0x884E, // GL_COMPARE_REF_TO_TEXTURE + CompareNone = 0x0000 // GL_NONE + }; + + void setComparisonMode(ComparisonMode mode); + ComparisonMode comparisonMode() const; + + // Sampling Parameters + enum Filter { + Nearest = 0x2600, // GL_NEAREST + Linear = 0x2601, // GL_LINEAR + NearestMipMapNearest = 0x2700, // GL_NEAREST_MIPMAP_NEAREST + NearestMipMapLinear = 0x2702, // GL_NEAREST_MIPMAP_LINEAR + LinearMipMapNearest = 0x2701, // GL_LINEAR_MIPMAP_NEAREST + LinearMipMapLinear = 0x2703 // GL_LINEAR_MIPMAP_LINEAR + }; + Q_ENUM(Filter) + + void setMinificationFilter(Filter filter); + Filter minificationFilter() const; + void setMagnificationFilter(Filter filter); + Filter magnificationFilter() const; + void setMinMagFilters(Filter minificationFilter, + Filter magnificationFilter); + QPair<Filter, Filter> minMagFilters() const; + void setMaximumAnisotropy(float anisotropy); + float maximumAnisotropy() const; + + void setWrapMode(WrapMode mode); + void setWrapMode(CoordinateDirection direction, WrapMode mode); + WrapMode wrapMode(CoordinateDirection direction) const; + + void setBorderColor(const QColor &color); + void setBorderColor(float r, float g, float b, float a); + void setBorderColor(int r, int g, int b, int a); + void setBorderColor(uint r, uint g, uint b, uint a); + + QColor borderColor() const; + void borderColor(float *border) const; + void borderColor(int *border) const; + void borderColor(unsigned int *border) const; + + void setMinimumLevelOfDetail(float value); + float minimumLevelOfDetail() const; + void setMaximumLevelOfDetail(float value); + float maximumLevelOfDetail() const; + void setLevelOfDetailRange(float min, float max); + QPair<float, float> levelOfDetailRange() const; + void setLevelofDetailBias(float bias); + float levelofDetailBias() const; + +#ifndef QT_NO_DEBUG_STREAM + friend Q_OPENGL_EXPORT QDebug operator<<(QDebug dbg, const QOpenGLTexture *t); +#endif + +private: + Q_DISABLE_COPY(QOpenGLTexture) + Q_DECLARE_PRIVATE(QOpenGLTexture) + QScopedPointer<QOpenGLTexturePrivate> d_ptr; +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(QOpenGLTexture::Features) + +#ifndef QT_NO_DEBUG_STREAM +Q_OPENGL_EXPORT QDebug operator<<(QDebug debug, const QOpenGLTexture *t); +#endif + +QT_END_NAMESPACE + +#endif // QT_NO_OPENGL + +#endif // QOPENGLABSTRACTTEXTURE_H diff --git a/src/opengl/qopengltexture_p.h b/src/opengl/qopengltexture_p.h new file mode 100644 index 0000000000..1dc0801644 --- /dev/null +++ b/src/opengl/qopengltexture_p.h @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB). +** 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 QABSTRACTOPENGLTEXTURE_P_H +#define QABSTRACTOPENGLTEXTURE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef QT_NO_OPENGL + +#include <QtOpenGL/qtopenglglobal.h> +#include "private/qobject_p.h" +#include "qopengltexture.h" +#include "qopengl.h" + +#include <cmath> + +namespace { +inline double qLog2(const double x) +{ + return std::log(x) / std::log(2.0); +} +} + +QT_BEGIN_NAMESPACE + +class QOpenGLContext; +class QOpenGLTextureHelper; +class QOpenGLFunctions; + +class QOpenGLTexturePrivate +{ +public: + QOpenGLTexturePrivate(QOpenGLTexture::Target textureTarget, + QOpenGLTexture *qq); + ~QOpenGLTexturePrivate(); + + Q_DECLARE_PUBLIC(QOpenGLTexture) + + void resetFuncs(QOpenGLTextureHelper *funcs); + void initializeOpenGLFunctions(); + + bool create(); + void destroy(); + + void bind(); + void bind(uint unit, QOpenGLTexture::TextureUnitReset reset = QOpenGLTexture::DontResetTextureUnit); + void release(); + void release(uint unit, QOpenGLTexture::TextureUnitReset reset = QOpenGLTexture::DontResetTextureUnit); + bool isBound() const; + bool isBound(uint unit) const; + + void allocateStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType); + void allocateMutableStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType); + void allocateImmutableStorage(); + void setData(int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, + QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options); + void setData(int xOffset, int yOffset, int zOffset, int width, int height, int depth, + int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, + QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType, + const void *data, const QOpenGLPixelTransferOptions * const options); + void setCompressedData(int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, + int dataSize, const void *data, + const QOpenGLPixelTransferOptions * const options); + + + void setWrapMode(QOpenGLTexture::WrapMode mode); + void setWrapMode(QOpenGLTexture::CoordinateDirection direction, QOpenGLTexture::WrapMode mode); + QOpenGLTexture::WrapMode wrapMode(QOpenGLTexture::CoordinateDirection direction) const; + + QOpenGLTexture *createTextureView(QOpenGLTexture::Target target, QOpenGLTexture::TextureFormat viewFormat, + int minimumMipmapLevel, int maximumMipmapLevel, + int minimumLayer, int maximumLayer) const; + + int evaluateMipLevels() const; + + inline int maximumMipLevelCount() const + { + return 1 + std::floor(qLog2(qMax(dimensions[0], qMax(dimensions[1], dimensions[2])))); + } + + static inline int mipLevelSize(int mipLevel, int baseLevelSize) + { + return std::floor(double(qMax(1, baseLevelSize >> mipLevel))); + } + + bool isUsingImmutableStorage() const; + + QOpenGLTexture *q_ptr; + QOpenGLContext *context; + QOpenGLTexture::Target target; + QOpenGLTexture::BindingTarget bindingTarget; + GLuint textureId; + QOpenGLTexture::TextureFormat format; + QOpenGLTexture::TextureFormatClass formatClass; + int dimensions[3]; + int requestedMipLevels; + int mipLevels; + int layers; + int faces; + + int samples; + bool fixedSamplePositions; + + int baseLevel; + int maxLevel; + + QOpenGLTexture::SwizzleValue swizzleMask[4]; + QOpenGLTexture::DepthStencilMode depthStencilMode; + QOpenGLTexture::ComparisonFunction comparisonFunction; + QOpenGLTexture::ComparisonMode comparisonMode; + + QOpenGLTexture::Filter minFilter; + QOpenGLTexture::Filter magFilter; + float maxAnisotropy; + QOpenGLTexture::WrapMode wrapModes[3]; + QVariantList borderColor; + float minLevelOfDetail; + float maxLevelOfDetail; + float levelOfDetailBias; + + bool textureView; + bool autoGenerateMipMaps; + bool storageAllocated; + + QOpenGLTextureHelper *texFuncs; + QOpenGLFunctions *functions; + + QOpenGLTexture::Features features; +}; + +QT_END_NAMESPACE + +#undef Q_CALL_MEMBER_FUNCTION + +#endif // QT_NO_OPENGL + +#endif // QABSTRACTOPENGLTEXTURE_P_H diff --git a/src/opengl/qopengltexturecache.cpp b/src/opengl/qopengltexturecache.cpp new file mode 100644 index 0000000000..5256c429e0 --- /dev/null +++ b/src/opengl/qopengltexturecache.cpp @@ -0,0 +1,198 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 "qopengltexturecache_p.h" +#include <private/qopengltextureuploader_p.h> +#include <qmath.h> +#include <qopenglfunctions.h> +#include <private/qimagepixmapcleanuphooks_p.h> +#include <qpa/qplatformpixmap.h> + +QT_BEGIN_NAMESPACE + +class QOpenGLTextureCacheWrapper +{ +public: + QOpenGLTextureCacheWrapper() + { + QImagePixmapCleanupHooks::instance()->addPlatformPixmapModificationHook(cleanupTexturesForPixmapData); + QImagePixmapCleanupHooks::instance()->addPlatformPixmapDestructionHook(cleanupTexturesForPixmapData); + QImagePixmapCleanupHooks::instance()->addImageHook(cleanupTexturesForCacheKey); + } + + ~QOpenGLTextureCacheWrapper() + { + QImagePixmapCleanupHooks::instance()->removePlatformPixmapModificationHook(cleanupTexturesForPixmapData); + QImagePixmapCleanupHooks::instance()->removePlatformPixmapDestructionHook(cleanupTexturesForPixmapData); + QImagePixmapCleanupHooks::instance()->removeImageHook(cleanupTexturesForCacheKey); + } + + QOpenGLTextureCache *cacheForContext(QOpenGLContext *context) { + QMutexLocker lock(&m_mutex); + return m_resource.value<QOpenGLTextureCache>(context); + } + + static void cleanupTexturesForCacheKey(qint64 key); + static void cleanupTexturesForPixmapData(QPlatformPixmap *pmd); + +private: + QOpenGLMultiGroupSharedResource m_resource; + QMutex m_mutex; +}; + +Q_GLOBAL_STATIC(QOpenGLTextureCacheWrapper, qt_texture_caches) + +QOpenGLTextureCache *QOpenGLTextureCache::cacheForContext(QOpenGLContext *context) +{ + return qt_texture_caches()->cacheForContext(context); +} + +void QOpenGLTextureCacheWrapper::cleanupTexturesForCacheKey(qint64 key) +{ + QList<QOpenGLSharedResource *> resources = qt_texture_caches()->m_resource.resources(); + for (QList<QOpenGLSharedResource *>::iterator it = resources.begin(); it != resources.end(); ++it) + static_cast<QOpenGLTextureCache *>(*it)->invalidate(key); +} + +void QOpenGLTextureCacheWrapper::cleanupTexturesForPixmapData(QPlatformPixmap *pmd) +{ + cleanupTexturesForCacheKey(pmd->cacheKey()); +} + +QOpenGLTextureCache::QOpenGLTextureCache(QOpenGLContext *ctx) + : QOpenGLSharedResource(ctx->shareGroup()) + , m_cache(256 * 1024) // 256 MB cache +{ +} + +QOpenGLTextureCache::~QOpenGLTextureCache() +{ +} + +GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QPixmap &pixmap, QOpenGLTextureUploader::BindOptions options) +{ + if (pixmap.isNull()) + return 0; + QMutexLocker locker(&m_mutex); + qint64 key = pixmap.cacheKey(); + + // A QPainter is active on the image - take the safe route and replace the texture. + if (!pixmap.paintingActive()) { + QOpenGLCachedTexture *entry = m_cache.object(key); + if (entry && entry->options() == options) { + context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id()); + return entry->id(); + } + } + + GLuint id = bindTexture(context, key, pixmap.toImage(), options); + if (id > 0) + QImagePixmapCleanupHooks::enableCleanupHooks(pixmap); + + return id; +} + +GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, const QImage &image, QOpenGLTextureUploader::BindOptions options) +{ + if (image.isNull()) + return 0; + QMutexLocker locker(&m_mutex); + qint64 key = image.cacheKey(); + + // A QPainter is active on the image - take the safe route and replace the texture. + if (!image.paintingActive()) { + QOpenGLCachedTexture *entry = m_cache.object(key); + if (entry && entry->options() == options) { + context->functions()->glBindTexture(GL_TEXTURE_2D, entry->id()); + return entry->id(); + } + } + + QImage img = image; + if (!context->functions()->hasOpenGLFeature(QOpenGLFunctions::NPOTTextures)) + options |= QOpenGLTextureUploader::PowerOfTwoBindOption; + + GLuint id = bindTexture(context, key, img, options); + if (id > 0) + QImagePixmapCleanupHooks::enableCleanupHooks(image); + + return id; +} + +GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, qint64 key, const QImage &image, QOpenGLTextureUploader::BindOptions options) +{ + GLuint id; + QOpenGLFunctions *funcs = context->functions(); + funcs->glGenTextures(1, &id); + funcs->glBindTexture(GL_TEXTURE_2D, id); + + int cost = QOpenGLTextureUploader::textureImage(GL_TEXTURE_2D, image, options); + + m_cache.insert(key, new QOpenGLCachedTexture(id, options, context), cost / 1024); + + return id; +} + +void QOpenGLTextureCache::invalidate(qint64 key) +{ + QMutexLocker locker(&m_mutex); + m_cache.remove(key); +} + +void QOpenGLTextureCache::invalidateResource() +{ + m_cache.clear(); +} + +void QOpenGLTextureCache::freeResource(QOpenGLContext *) +{ + Q_ASSERT(false); // the texture cache lives until the context group disappears +} + +static void freeTexture(QOpenGLFunctions *funcs, GLuint id) +{ + funcs->glDeleteTextures(1, &id); +} + +QOpenGLCachedTexture::QOpenGLCachedTexture(GLuint id, QOpenGLTextureUploader::BindOptions options, QOpenGLContext *context) : m_options(options) +{ + m_resource = new QOpenGLSharedResourceGuard(context, id, freeTexture); +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopengltexturecache_p.h b/src/opengl/qopengltexturecache_p.h new file mode 100644 index 0000000000..14e03ebea1 --- /dev/null +++ b/src/opengl/qopengltexturecache_p.h @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef QOPENGLTEXTURECACHE_P_H +#define QOPENGLTEXTURECACHE_P_H + +#include <QtOpenGL/qtopenglglobal.h> +#include <QHash> +#include <QObject> +#include <QCache> +#include <private/qopenglcontext_p.h> +#include <private/qopengltextureuploader_p.h> +#include <QtCore/qmutex.h> + +QT_BEGIN_NAMESPACE + +class QOpenGLCachedTexture; + +class Q_OPENGL_EXPORT QOpenGLTextureCache : public QOpenGLSharedResource +{ +public: + static QOpenGLTextureCache *cacheForContext(QOpenGLContext *context); + + QOpenGLTextureCache(QOpenGLContext *); + ~QOpenGLTextureCache(); + + GLuint bindTexture(QOpenGLContext *context, const QPixmap &pixmap, + QOpenGLTextureUploader::BindOptions options = QOpenGLTextureUploader::PremultipliedAlphaBindOption); + GLuint bindTexture(QOpenGLContext *context, const QImage &image, + QOpenGLTextureUploader::BindOptions options = QOpenGLTextureUploader::PremultipliedAlphaBindOption); + + void invalidate(qint64 key); + + void invalidateResource() override; + void freeResource(QOpenGLContext *ctx) override; + +private: + GLuint bindTexture(QOpenGLContext *context, qint64 key, const QImage &image, QOpenGLTextureUploader::BindOptions options); + + QMutex m_mutex; + QCache<quint64, QOpenGLCachedTexture> m_cache; +}; + +class QOpenGLCachedTexture +{ +public: + QOpenGLCachedTexture(GLuint id, QOpenGLTextureUploader::BindOptions options, QOpenGLContext *context); + ~QOpenGLCachedTexture() { m_resource->free(); } + + GLuint id() const { return m_resource->id(); } + QOpenGLTextureUploader::BindOptions options() const { return m_options; } + +private: + QOpenGLSharedResourceGuard *m_resource; + QOpenGLTextureUploader::BindOptions m_options; +}; + +QT_END_NAMESPACE + +#endif + diff --git a/src/opengl/qopengltextureglyphcache.cpp b/src/opengl/qopengltextureglyphcache.cpp new file mode 100644 index 0000000000..66fd3a7507 --- /dev/null +++ b/src/opengl/qopengltextureglyphcache.cpp @@ -0,0 +1,485 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 "qopengltextureglyphcache_p.h" +#include <private/qopenglpaintengine_p.h> +#include "private/qopenglengineshadersource_p.h" +#include <private/qopenglextensions_p.h> +#include <qrgb.h> +#include <private/qdrawhelper_p.h> + +QT_BEGIN_NAMESPACE + + +static int next_qopengltextureglyphcache_serial_number() +{ + static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(0); + return 1 + serial.fetchAndAddRelaxed(1); +} + +QOpenGLTextureGlyphCache::QOpenGLTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix, const QColor &color) + : QImageTextureGlyphCache(format, matrix, color) + , m_textureResource(nullptr) + , pex(nullptr) + , m_blitProgram(nullptr) + , m_filterMode(Nearest) + , m_serialNumber(next_qopengltextureglyphcache_serial_number()) + , m_buffer(QOpenGLBuffer::VertexBuffer) +{ +#ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG + qDebug(" -> QOpenGLTextureGlyphCache() %p for context %p.", this, QOpenGLContext::currentContext()); +#endif + m_vertexCoordinateArray[0] = -1.0f; + m_vertexCoordinateArray[1] = -1.0f; + m_vertexCoordinateArray[2] = 1.0f; + m_vertexCoordinateArray[3] = -1.0f; + m_vertexCoordinateArray[4] = 1.0f; + m_vertexCoordinateArray[5] = 1.0f; + m_vertexCoordinateArray[6] = -1.0f; + m_vertexCoordinateArray[7] = 1.0f; + + m_textureCoordinateArray[0] = 0.0f; + m_textureCoordinateArray[1] = 0.0f; + m_textureCoordinateArray[2] = 1.0f; + m_textureCoordinateArray[3] = 0.0f; + m_textureCoordinateArray[4] = 1.0f; + m_textureCoordinateArray[5] = 1.0f; + m_textureCoordinateArray[6] = 0.0f; + m_textureCoordinateArray[7] = 1.0f; +} + +QOpenGLTextureGlyphCache::~QOpenGLTextureGlyphCache() +{ +#ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG + qDebug(" -> ~QOpenGLTextureGlyphCache() %p.", this); +#endif + clear(); +} + +#if !defined(QT_OPENGL_ES_2) +static inline bool isCoreProfile() +{ + return QOpenGLContext::currentContext()->format().profile() == QSurfaceFormat::CoreProfile; +} +#endif + +void QOpenGLTextureGlyphCache::createTextureData(int width, int height) +{ + QOpenGLContext *ctx = const_cast<QOpenGLContext *>(QOpenGLContext::currentContext()); + if (ctx == nullptr) { + qWarning("QOpenGLTextureGlyphCache::createTextureData: Called with no context"); + return; + } + + // create in QImageTextureGlyphCache baseclass is meant to be called + // only to create the initial image and does not preserve the content, + // so we don't call when this function is called from resize. + if (ctx->d_func()->workaround_brokenFBOReadBack && image().isNull()) + QImageTextureGlyphCache::createTextureData(width, height); + + // Make the lower glyph texture size 16 x 16. + if (width < 16) + width = 16; + if (height < 16) + height = 16; + + if (m_textureResource && !m_textureResource->m_texture) { + delete m_textureResource; + m_textureResource = nullptr; + } + + if (!m_textureResource) + m_textureResource = new QOpenGLGlyphTexture(ctx); + + QOpenGLFunctions *funcs = ctx->functions(); + funcs->glGenTextures(1, &m_textureResource->m_texture); + funcs->glBindTexture(GL_TEXTURE_2D, m_textureResource->m_texture); + + m_textureResource->m_width = width; + m_textureResource->m_height = height; + + if (m_format == QFontEngine::Format_A32 || m_format == QFontEngine::Format_ARGB) { + QVarLengthArray<uchar> data(width * height * 4); + for (int i = 0; i < data.size(); ++i) + data[i] = 0; + funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data[0]); + } else { + QVarLengthArray<uchar> data(width * height); + for (int i = 0; i < data.size(); ++i) + data[i] = 0; +#if !defined(QT_OPENGL_ES_2) + const GLint internalFormat = isCoreProfile() ? GL_R8 : GL_ALPHA; + const GLenum format = isCoreProfile() ? GL_RED : GL_ALPHA; +#else + const GLint internalFormat = GL_ALPHA; + const GLenum format = GL_ALPHA; +#endif + funcs->glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, &data[0]); + } + + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + m_filterMode = Nearest; + + if (!m_buffer.isCreated()) { + m_buffer.create(); + m_buffer.bind(); + static GLfloat buf[sizeof(m_vertexCoordinateArray) + sizeof(m_textureCoordinateArray)]; + memcpy(buf, m_vertexCoordinateArray, sizeof(m_vertexCoordinateArray)); + memcpy(buf + (sizeof(m_vertexCoordinateArray) / sizeof(GLfloat)), + m_textureCoordinateArray, + sizeof(m_textureCoordinateArray)); + m_buffer.allocate(buf, sizeof(buf)); + m_buffer.release(); + } + + if (!m_vao.isCreated()) + m_vao.create(); +} + +void QOpenGLTextureGlyphCache::setupVertexAttribs() +{ + m_buffer.bind(); + m_blitProgram->setAttributeBuffer(int(QT_VERTEX_COORDS_ATTR), GL_FLOAT, 0, 2); + m_blitProgram->setAttributeBuffer(int(QT_TEXTURE_COORDS_ATTR), GL_FLOAT, sizeof(m_vertexCoordinateArray), 2); + m_blitProgram->enableAttributeArray(int(QT_VERTEX_COORDS_ATTR)); + m_blitProgram->enableAttributeArray(int(QT_TEXTURE_COORDS_ATTR)); + m_buffer.release(); +} + +static void load_glyph_image_to_texture(QOpenGLContext *ctx, + QImage &img, + GLuint texture, + int tx, int ty) +{ + QOpenGLFunctions *funcs = ctx->functions(); + + const int imgWidth = img.width(); + const int imgHeight = img.height(); + + if (img.format() == QImage::Format_Mono) { + img = img.convertToFormat(QImage::Format_Grayscale8); + } else if (img.depth() == 32) { + if (img.format() == QImage::Format_RGB32 + // We need to make the alpha component equal to the average of the RGB values. + // This is needed when drawing sub-pixel antialiased text on translucent targets. +#if Q_BYTE_ORDER == Q_BIG_ENDIAN + || img.format() == QImage::Format_ARGB32_Premultiplied +#else + || (img.format() == QImage::Format_ARGB32_Premultiplied + && ctx->isOpenGLES()) +#endif + ) { + for (int y = 0; y < imgHeight; ++y) { + QRgb *src = (QRgb *) img.scanLine(y); + for (int x = 0; x < imgWidth; ++x) { + int r = qRed(src[x]); + int g = qGreen(src[x]); + int b = qBlue(src[x]); + int avg; + if (img.format() == QImage::Format_RGB32) + avg = (r + g + b + 1) / 3; // "+1" for rounding. + else // Format_ARGB_Premultiplied + avg = qAlpha(src[x]); + + src[x] = qRgba(r, g, b, avg); + // swizzle the bits to accommodate for the GL_RGBA upload. +#if Q_BYTE_ORDER != Q_BIG_ENDIAN + if (ctx->isOpenGLES()) +#endif + src[x] = ARGB2RGBA(src[x]); + } + } + } + } + + funcs->glBindTexture(GL_TEXTURE_2D, texture); + if (img.depth() == 32) { +#ifdef QT_OPENGL_ES_2 + GLenum fmt = GL_RGBA; +#else + GLenum fmt = ctx->isOpenGLES() ? GL_RGBA : GL_BGRA; +#endif // QT_OPENGL_ES_2 + +#if Q_BYTE_ORDER == Q_BIG_ENDIAN + fmt = GL_RGBA; +#endif + funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, imgWidth, imgHeight, fmt, GL_UNSIGNED_BYTE, img.constBits()); + } else { + // The scanlines in image are 32-bit aligned, even for mono or 8-bit formats. This + // is good because it matches the default of 4 bytes for GL_UNPACK_ALIGNMENT. +#if !defined(QT_OPENGL_ES_2) + const GLenum format = isCoreProfile() ? GL_RED : GL_ALPHA; +#else + const GLenum format = GL_ALPHA; +#endif + funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, imgWidth, imgHeight, format, GL_UNSIGNED_BYTE, img.constBits()); + } +} + +static void load_glyph_image_region_to_texture(QOpenGLContext *ctx, + const QImage &srcImg, + int x, int y, + int w, int h, + GLuint texture, + int tx, int ty) +{ + Q_ASSERT(x + w <= srcImg.width() && y + h <= srcImg.height()); + + QImage img; + if (x != 0 || y != 0 || w != srcImg.width() || h != srcImg.height()) + img = srcImg.copy(x, y, w, h); + else + img = srcImg; + + load_glyph_image_to_texture(ctx, img, texture, tx, ty); +} + +void QOpenGLTextureGlyphCache::resizeTextureData(int width, int height) +{ + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (ctx == nullptr) { + qWarning("QOpenGLTextureGlyphCache::resizeTextureData: Called with no context"); + return; + } + + QOpenGLFunctions *funcs = ctx->functions(); + GLint oldFbo; + funcs->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oldFbo); + + int oldWidth = m_textureResource->m_width; + int oldHeight = m_textureResource->m_height; + + // Make the lower glyph texture size 16 x 16. + if (width < 16) + width = 16; + if (height < 16) + height = 16; + + GLuint oldTexture = m_textureResource->m_texture; + createTextureData(width, height); + + if (ctx->d_func()->workaround_brokenFBOReadBack) { + QImageTextureGlyphCache::resizeTextureData(width, height); + load_glyph_image_region_to_texture(ctx, image(), 0, 0, qMin(oldWidth, width), qMin(oldHeight, height), + m_textureResource->m_texture, 0, 0); + return; + } + + // ### the QTextureGlyphCache API needs to be reworked to allow + // ### resizeTextureData to fail + + funcs->glBindFramebuffer(GL_FRAMEBUFFER, m_textureResource->m_fbo); + + GLuint tmp_texture; + funcs->glGenTextures(1, &tmp_texture); + funcs->glBindTexture(GL_TEXTURE_2D, tmp_texture); + funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, oldWidth, oldHeight, 0, + GL_RGBA, GL_UNSIGNED_BYTE, nullptr); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + m_filterMode = Nearest; + funcs->glBindTexture(GL_TEXTURE_2D, 0); + funcs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_TEXTURE_2D, tmp_texture, 0); + + funcs->glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT); + funcs->glBindTexture(GL_TEXTURE_2D, oldTexture); + + if (pex != nullptr) + pex->transferMode(BrushDrawingMode); + + funcs->glDisable(GL_STENCIL_TEST); + funcs->glDisable(GL_DEPTH_TEST); + funcs->glDisable(GL_SCISSOR_TEST); + funcs->glDisable(GL_BLEND); + + funcs->glViewport(0, 0, oldWidth, oldHeight); + + QOpenGLShaderProgram *blitProgram = nullptr; + if (pex == nullptr) { + if (m_blitProgram == nullptr) { + m_blitProgram = new QOpenGLShaderProgram; + const bool isCoreProfile = ctx->format().profile() == QSurfaceFormat::CoreProfile; + + { + QString source; +#ifdef Q_OS_WASM + source.append(QLatin1String(isCoreProfile ? qopenglslUntransformedPositionVertexShader_core : qopenglslUntransformedPositionVertexShader)); + source.append(QLatin1String(isCoreProfile ? qopenglslMainWithTexCoordsVertexShader_core : qopenglslMainWithTexCoordsVertexShader)); +#else + source.append(QLatin1String(isCoreProfile ? qopenglslMainWithTexCoordsVertexShader_core : qopenglslMainWithTexCoordsVertexShader)); + source.append(QLatin1String(isCoreProfile ? qopenglslUntransformedPositionVertexShader_core : qopenglslUntransformedPositionVertexShader)); +#endif + m_blitProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, source); + } + + { + QString source; +#ifdef Q_OS_WASM + source.append(QLatin1String(isCoreProfile ? qopenglslImageSrcFragmentShader_core : qopenglslImageSrcFragmentShader)); + source.append(QLatin1String(isCoreProfile ? qopenglslMainFragmentShader_core : qopenglslMainFragmentShader)); +#else + source.append(QLatin1String(isCoreProfile ? qopenglslMainFragmentShader_core : qopenglslMainFragmentShader)); + source.append(QLatin1String(isCoreProfile ? qopenglslImageSrcFragmentShader_core : qopenglslImageSrcFragmentShader)); +#endif + m_blitProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, source); + } + + m_blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR); + m_blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR); + + m_blitProgram->link(); + + if (m_vao.isCreated()) { + m_vao.bind(); + setupVertexAttribs(); + } + } + + if (m_vao.isCreated()) + m_vao.bind(); + else + setupVertexAttribs(); + + m_blitProgram->bind(); + blitProgram = m_blitProgram; + + } else { + pex->uploadData(QT_VERTEX_COORDS_ATTR, m_vertexCoordinateArray, 8); + pex->uploadData(QT_TEXTURE_COORDS_ATTR, m_textureCoordinateArray, 8); + + pex->shaderManager->useBlitProgram(); + blitProgram = pex->shaderManager->blitProgram(); + } + + blitProgram->setUniformValue("imageTexture", QT_IMAGE_TEXTURE_UNIT); + + funcs->glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + + funcs->glBindTexture(GL_TEXTURE_2D, m_textureResource->m_texture); + + funcs->glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, oldWidth, oldHeight); + + funcs->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_RENDERBUFFER, 0); + funcs->glDeleteTextures(1, &tmp_texture); + funcs->glDeleteTextures(1, &oldTexture); + + funcs->glBindFramebuffer(GL_FRAMEBUFFER, (GLuint)oldFbo); + + if (pex != nullptr) { + funcs->glViewport(0, 0, pex->width, pex->height); + pex->updateClipScissorTest(); + } else { + if (m_vao.isCreated()) { + m_vao.release(); + } else { + m_blitProgram->disableAttributeArray(int(QT_VERTEX_COORDS_ATTR)); + m_blitProgram->disableAttributeArray(int(QT_TEXTURE_COORDS_ATTR)); + } + } +} + +void QOpenGLTextureGlyphCache::fillTexture(const Coord &c, glyph_t glyph, QFixed subPixelPosition) +{ + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (ctx == nullptr) { + qWarning("QOpenGLTextureGlyphCache::fillTexture: Called with no context"); + return; + } + + if (ctx->d_func()->workaround_brokenFBOReadBack) { + QImageTextureGlyphCache::fillTexture(c, glyph, subPixelPosition); + load_glyph_image_region_to_texture(ctx, image(), c.x, c.y, c.w, c.h, m_textureResource->m_texture, c.x, c.y); + return; + } + + QImage mask = textureMapForGlyph(glyph, subPixelPosition); + load_glyph_image_to_texture(ctx, mask, m_textureResource->m_texture, c.x, c.y); +} + +int QOpenGLTextureGlyphCache::glyphPadding() const +{ + return 1; +} + +int QOpenGLTextureGlyphCache::maxTextureWidth() const +{ + QOpenGLContext *ctx = const_cast<QOpenGLContext *>(QOpenGLContext::currentContext()); + if (ctx == nullptr) + return QImageTextureGlyphCache::maxTextureWidth(); + else + return ctx->d_func()->maxTextureSize(); +} + +int QOpenGLTextureGlyphCache::maxTextureHeight() const +{ + QOpenGLContext *ctx = const_cast<QOpenGLContext *>(QOpenGLContext::currentContext()); + if (ctx == nullptr) + return QImageTextureGlyphCache::maxTextureHeight(); + + if (ctx->d_func()->workaround_brokenTexSubImage) + return qMin(1024, ctx->d_func()->maxTextureSize()); + else + return ctx->d_func()->maxTextureSize(); +} + +void QOpenGLTextureGlyphCache::clear() +{ + if (m_textureResource) + m_textureResource->free(); + m_textureResource = nullptr; + + delete m_blitProgram; + m_blitProgram = nullptr; + + m_w = 0; + m_h = 0; + m_cx = 0; + m_cy = 0; + m_currentRowHeight = 0; + coords.clear(); +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopengltextureglyphcache_p.h b/src/opengl/qopengltextureglyphcache_p.h new file mode 100644 index 0000000000..15ecd6209b --- /dev/null +++ b/src/opengl/qopengltextureglyphcache_p.h @@ -0,0 +1,181 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** 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 QOPENGLTEXTUREGLYPHCACHE_P_H +#define QOPENGLTEXTUREGLYPHCACHE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtOpenGL/qtopenglglobal.h> +#include <private/qtextureglyphcache_p.h> +#include <private/qopenglcontext_p.h> +#include <qopenglshaderprogram.h> +#include <qopenglfunctions.h> +#include <qopenglbuffer.h> +#include <qopenglvertexarrayobject.h> + +// #define QT_GL_TEXTURE_GLYPH_CACHE_DEBUG + +QT_BEGIN_NAMESPACE + +class QOpenGL2PaintEngineExPrivate; + +class QOpenGLGlyphTexture : public QOpenGLSharedResource +{ +public: + explicit QOpenGLGlyphTexture(QOpenGLContext *ctx) + : QOpenGLSharedResource(ctx->shareGroup()) + , m_width(0) + , m_height(0) + { + if (!ctx->d_func()->workaround_brokenFBOReadBack) + QOpenGLFunctions(ctx).glGenFramebuffers(1, &m_fbo); + +#ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG + qDebug(" -> QOpenGLGlyphTexture() %p for context %p.", this, ctx); +#endif + } + + void freeResource(QOpenGLContext *context) override + { + QOpenGLContext *ctx = context; +#ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG + qDebug("~QOpenGLGlyphTexture() %p for context %p.", this, ctx); +#endif + if (!ctx->d_func()->workaround_brokenFBOReadBack) + ctx->functions()->glDeleteFramebuffers(1, &m_fbo); + if (m_width || m_height) + ctx->functions()->glDeleteTextures(1, &m_texture); + } + + void invalidateResource() override + { + m_texture = 0; + m_fbo = 0; + m_width = 0; + m_height = 0; + } + + GLuint m_texture; + GLuint m_fbo; + int m_width; + int m_height; +}; + +class Q_OPENGL_EXPORT QOpenGLTextureGlyphCache : public QImageTextureGlyphCache +{ +public: + QOpenGLTextureGlyphCache(QFontEngine::GlyphFormat glyphFormat, const QTransform &matrix, const QColor &color = QColor()); + ~QOpenGLTextureGlyphCache(); + + virtual void createTextureData(int width, int height) override; + virtual void resizeTextureData(int width, int height) override; + virtual void fillTexture(const Coord &c, glyph_t glyph, QFixed subPixelPosition) override; + virtual int glyphPadding() const override; + virtual int maxTextureWidth() const override; + virtual int maxTextureHeight() const override; + + inline GLuint texture() const { + QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this); + QOpenGLGlyphTexture *glyphTexture = that->m_textureResource; + return glyphTexture ? glyphTexture->m_texture : 0; + } + + inline int width() const { + QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this); + QOpenGLGlyphTexture *glyphTexture = that->m_textureResource; + return glyphTexture ? glyphTexture->m_width : 0; + } + inline int height() const { + QOpenGLTextureGlyphCache *that = const_cast<QOpenGLTextureGlyphCache *>(this); + QOpenGLGlyphTexture *glyphTexture = that->m_textureResource; + return glyphTexture ? glyphTexture->m_height : 0; + } + + inline void setPaintEnginePrivate(QOpenGL2PaintEngineExPrivate *p) { pex = p; } + + inline const QOpenGLContextGroup *contextGroup() const { return m_textureResource ? m_textureResource->group() : nullptr; } + + inline int serialNumber() const { return m_serialNumber; } + + enum FilterMode { + Nearest, + Linear + }; + FilterMode filterMode() const { return m_filterMode; } + void setFilterMode(FilterMode m) { m_filterMode = m; } + + void clear(); + + QOpenGL2PaintEngineExPrivate *paintEnginePrivate() const + { + return pex; + } + +private: + void setupVertexAttribs(); + + QOpenGLGlyphTexture *m_textureResource; + + QOpenGL2PaintEngineExPrivate *pex; + QOpenGLShaderProgram *m_blitProgram; + FilterMode m_filterMode; + + GLfloat m_vertexCoordinateArray[8]; + GLfloat m_textureCoordinateArray[8]; + + int m_serialNumber; + + QOpenGLBuffer m_buffer; + QOpenGLVertexArrayObject m_vao; +}; + +QT_END_NAMESPACE + +#endif // QOPENGLTEXTUREGLYPHCACHE_P_H + diff --git a/src/opengl/qopengltexturehelper.cpp b/src/opengl/qopengltexturehelper.cpp new file mode 100644 index 0000000000..8f1473ecc9 --- /dev/null +++ b/src/opengl/qopengltexturehelper.cpp @@ -0,0 +1,589 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB). +** 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 "qopengltexturehelper_p.h" + +#include <QOpenGLContext> +#include <private/qopenglextensions_p.h> + +QT_BEGIN_NAMESPACE + +QOpenGLTextureHelper::QOpenGLTextureHelper(QOpenGLContext *context) +{ + functions = context->functions(); + // Resolve EXT_direct_state_access entry points if present. + + // However, disable it on some systems where DSA is known to be unreliable. + bool allowDSA = true; + const char *renderer = reinterpret_cast<const char *>(context->functions()->glGetString(GL_RENDERER)); + // QTBUG-40653, QTBUG-44988 + if (renderer && strstr(renderer, "AMD Radeon HD")) + allowDSA = false; + + if (allowDSA && !context->isOpenGLES() + && context->hasExtension(QByteArrayLiteral("GL_EXT_direct_state_access"))) { + TextureParameteriEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLenum , GLint )>(context->getProcAddress("glTextureParameteriEXT")); + TextureParameterivEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLenum , const GLint *)>(context->getProcAddress("glTextureParameterivEXT")); + TextureParameterfEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLenum , GLfloat )>(context->getProcAddress("glTextureParameterfEXT")); + TextureParameterfvEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLenum , const GLfloat *)>(context->getProcAddress("glTextureParameterfvEXT")); + GenerateTextureMipmapEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum )>(context->getProcAddress("glGenerateTextureMipmapEXT")); + TextureStorage3DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLsizei , GLenum , GLsizei , GLsizei , GLsizei )>(context->getProcAddress("glTextureStorage3DEXT")); + TextureStorage2DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLsizei , GLenum , GLsizei , GLsizei )>(context->getProcAddress("glTextureStorage2DEXT")); + TextureStorage1DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLsizei , GLenum , GLsizei )>(context->getProcAddress("glTextureStorage1DEXT")); + TextureStorage3DMultisampleEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLsizei , GLenum , GLsizei , GLsizei , GLsizei , GLboolean )>(context->getProcAddress("glTextureStorage3DMultisampleEXT")); + TextureStorage2DMultisampleEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLsizei , GLenum , GLsizei , GLsizei , GLboolean )>(context->getProcAddress("glTextureStorage2DMultisampleEXT")); + TextureImage3DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLenum , GLsizei , GLsizei , GLsizei , GLint , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTextureImage3DEXT")); + TextureImage2DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLenum , GLsizei , GLsizei , GLint , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTextureImage2DEXT")); + TextureImage1DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLenum , GLsizei , GLint , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTextureImage1DEXT")); + TextureSubImage3DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLint , GLint , GLint , GLsizei , GLsizei , GLsizei , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTextureSubImage3DEXT")); + TextureSubImage2DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLint , GLint , GLsizei , GLsizei , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTextureSubImage2DEXT")); + TextureSubImage1DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLint , GLsizei , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTextureSubImage1DEXT")); + CompressedTextureSubImage1DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLint , GLsizei , GLenum , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTextureSubImage1DEXT")); + CompressedTextureSubImage2DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLint , GLint , GLsizei , GLsizei , GLenum , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTextureSubImage2DEXT")); + CompressedTextureSubImage3DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLint , GLint , GLint , GLsizei , GLsizei , GLsizei , GLenum , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTextureSubImage3DEXT")); + CompressedTextureImage1DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLenum , GLsizei , GLint , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTextureImage1DEXT")); + CompressedTextureImage2DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLenum , GLsizei , GLsizei , GLint , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTextureImage2DEXT")); + CompressedTextureImage3DEXT = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLint , GLenum , GLsizei , GLsizei , GLsizei , GLint , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTextureImage3DEXT")); + + // Use the real DSA functions + TextureParameteri = &QOpenGLTextureHelper::dsa_TextureParameteri; + TextureParameteriv = &QOpenGLTextureHelper::dsa_TextureParameteriv; + TextureParameterf = &QOpenGLTextureHelper::dsa_TextureParameterf; + TextureParameterfv = &QOpenGLTextureHelper::dsa_TextureParameterfv; + GenerateTextureMipmap = &QOpenGLTextureHelper::dsa_GenerateTextureMipmap; + TextureStorage3D = &QOpenGLTextureHelper::dsa_TextureStorage3D; + TextureStorage2D = &QOpenGLTextureHelper::dsa_TextureStorage2D; + TextureStorage1D = &QOpenGLTextureHelper::dsa_TextureStorage1D; + TextureStorage3DMultisample = &QOpenGLTextureHelper::dsa_TextureStorage3DMultisample; + TextureStorage2DMultisample = &QOpenGLTextureHelper::dsa_TextureStorage2DMultisample; + TextureImage3D = &QOpenGLTextureHelper::dsa_TextureImage3D; + TextureImage2D = &QOpenGLTextureHelper::dsa_TextureImage2D; + TextureImage1D = &QOpenGLTextureHelper::dsa_TextureImage1D; + TextureSubImage3D = &QOpenGLTextureHelper::dsa_TextureSubImage3D; + TextureSubImage2D = &QOpenGLTextureHelper::dsa_TextureSubImage2D; + TextureSubImage1D = &QOpenGLTextureHelper::dsa_TextureSubImage1D; + CompressedTextureSubImage1D = &QOpenGLTextureHelper::dsa_CompressedTextureSubImage1D; + CompressedTextureSubImage2D = &QOpenGLTextureHelper::dsa_CompressedTextureSubImage2D; + CompressedTextureSubImage3D = &QOpenGLTextureHelper::dsa_CompressedTextureSubImage3D; + CompressedTextureImage1D = &QOpenGLTextureHelper::dsa_CompressedTextureImage1D; + CompressedTextureImage2D = &QOpenGLTextureHelper::dsa_CompressedTextureImage2D; + CompressedTextureImage3D = &QOpenGLTextureHelper::dsa_CompressedTextureImage3D; + } else { + // Use our own DSA emulation + TextureParameteri = &QOpenGLTextureHelper::qt_TextureParameteri; + TextureParameteriv = &QOpenGLTextureHelper::qt_TextureParameteriv; + TextureParameterf = &QOpenGLTextureHelper::qt_TextureParameterf; + TextureParameterfv = &QOpenGLTextureHelper::qt_TextureParameterfv; + GenerateTextureMipmap = &QOpenGLTextureHelper::qt_GenerateTextureMipmap; + TextureStorage3D = &QOpenGLTextureHelper::qt_TextureStorage3D; + TextureStorage2D = &QOpenGLTextureHelper::qt_TextureStorage2D; + TextureStorage1D = &QOpenGLTextureHelper::qt_TextureStorage1D; + TextureStorage3DMultisample = &QOpenGLTextureHelper::qt_TextureStorage3DMultisample; + TextureStorage2DMultisample = &QOpenGLTextureHelper::qt_TextureStorage2DMultisample; + TextureImage3D = &QOpenGLTextureHelper::qt_TextureImage3D; + TextureImage2D = &QOpenGLTextureHelper::qt_TextureImage2D; + TextureImage1D = &QOpenGLTextureHelper::qt_TextureImage1D; + TextureSubImage3D = &QOpenGLTextureHelper::qt_TextureSubImage3D; + TextureSubImage2D = &QOpenGLTextureHelper::qt_TextureSubImage2D; + TextureSubImage1D = &QOpenGLTextureHelper::qt_TextureSubImage1D; + CompressedTextureSubImage1D = &QOpenGLTextureHelper::qt_CompressedTextureSubImage1D; + CompressedTextureSubImage2D = &QOpenGLTextureHelper::qt_CompressedTextureSubImage2D; + CompressedTextureSubImage3D = &QOpenGLTextureHelper::qt_CompressedTextureSubImage3D; + CompressedTextureImage1D = &QOpenGLTextureHelper::qt_CompressedTextureImage1D; + CompressedTextureImage2D = &QOpenGLTextureHelper::qt_CompressedTextureImage2D; + CompressedTextureImage3D = &QOpenGLTextureHelper::qt_CompressedTextureImage3D; + } + + // Some DSA functions are part of NV_texture_multisample instead + if (!context->isOpenGLES() + && context->hasExtension(QByteArrayLiteral("GL_NV_texture_multisample"))) { + TextureImage3DMultisampleNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLsizei , GLint , GLsizei , GLsizei , GLsizei , GLboolean )>(context->getProcAddress("glTextureImage3DMultisampleNV")); + TextureImage2DMultisampleNV = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLsizei , GLint , GLsizei , GLsizei , GLboolean )>(context->getProcAddress("glTextureImage2DMultisampleNV")); + + TextureImage3DMultisample = &QOpenGLTextureHelper::dsa_TextureImage3DMultisample; + TextureImage2DMultisample = &QOpenGLTextureHelper::dsa_TextureImage2DMultisample; + } else { + TextureImage3DMultisample = &QOpenGLTextureHelper::qt_TextureImage3DMultisample; + TextureImage2DMultisample = &QOpenGLTextureHelper::qt_TextureImage2DMultisample; + } + +#if defined(QT_OPENGL_ES_2) + // Here we are targeting OpenGL ES 2.0+ only. This is likely using EGL, where, + // similarly to WGL, non-extension functions (i.e. any function that is part of the + // GLES spec) *may* not be queried via eglGetProcAddress. + + // OpenGL 1.0 + TexImage1D = 0; + + // OpenGL 1.1 + TexSubImage1D = 0; + + // OpenGL 1.3 + GetCompressedTexImage = 0; + CompressedTexSubImage1D = 0; + CompressedTexSubImage2D = ::glCompressedTexSubImage2D; + CompressedTexImage1D = 0; + CompressedTexImage2D = ::glCompressedTexImage2D; + ActiveTexture = ::glActiveTexture; + + // OpenGL 3.0 + GenerateMipmap = ::glGenerateMipmap; + + // OpenGL 3.2 + TexImage3DMultisample = 0; + TexImage2DMultisample = 0; + + // OpenGL 4.2 + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (ctx->format().majorVersion() >= 3) { + // OpenGL ES 3.0+ has immutable storage for 2D and 3D at least. + QOpenGLExtraFunctionsPrivate *extra = static_cast<QOpenGLExtensions *>(context->extraFunctions())->d(); + TexStorage3D = extra->f.TexStorage3D; + TexStorage2D = extra->f.TexStorage2D; + } else { + TexStorage3D = 0; + TexStorage2D = 0; + } + TexStorage1D = 0; + + // OpenGL 4.3 + TexStorage3DMultisample = 0; + TexStorage2DMultisample = 0; + TexBufferRange = 0; + TextureView = 0; + + // OpenGL ES 3.1+ has TexStorage2DMultisample + if (ctx->format().version() >= qMakePair(3, 1)) { + QOpenGLExtraFunctionsPrivate *extra = static_cast<QOpenGLExtensions *>(context->extraFunctions())->d(); + TexStorage2DMultisample = extra->f.TexStorage2DMultisample; + } + +#endif + + if (context->isOpenGLES() && context->hasExtension(QByteArrayLiteral("GL_OES_texture_3D"))) { + TexImage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum, GLint, GLint, GLsizei, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid*)>(context->getProcAddress("glTexImage3DOES")); + TexSubImage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum, GLint, GLint, GLint, GLint, GLsizei, GLsizei, GLsizei, GLenum, GLenum, const GLvoid*)>(context->getProcAddress("glTexSubImage3DOES")); + CompressedTexImage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum, GLint, GLenum, GLsizei, GLsizei, GLsizei, GLint, GLsizei, const GLvoid*)>(context->getProcAddress("glCompressedTexImage3DOES")); + CompressedTexSubImage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum, GLint, GLint, GLint, GLint, GLsizei, GLsizei, GLsizei, GLenum, GLsizei, const GLvoid*)>(context->getProcAddress("glCompressedTexSubImage3DOES")); + } else { + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + if (ctx->isOpenGLES() && ctx->format().majorVersion() >= 3) { + // OpenGL ES 3.0+ has glTexImage3D. + QOpenGLExtraFunctionsPrivate *extra = static_cast<QOpenGLExtensions *>(context->extraFunctions())->d(); + TexImage3D = extra->f.TexImage3D; + TexSubImage3D = extra->f.TexSubImage3D; + CompressedTexImage3D = extra->f.CompressedTexImage3D; + CompressedTexSubImage3D = extra->f.CompressedTexSubImage3D; + } else { + // OpenGL 1.2 + TexImage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLint , GLsizei , GLsizei , GLsizei , GLint , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTexImage3D")); + TexSubImage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLint , GLint , GLint , GLsizei , GLsizei , GLsizei , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTexSubImage3D")); + + // OpenGL 1.3 + CompressedTexImage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLenum , GLsizei , GLsizei , GLsizei , GLint , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTexImage3D")); + CompressedTexSubImage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLint , GLint , GLint , GLsizei , GLsizei , GLsizei , GLenum , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTexSubImage3D")); + } + } + +#ifndef QT_OPENGL_ES_2 + // OpenGL 1.0 and 1.1 + TexImage1D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLint , GLsizei , GLint , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTexImage1D")); + TexSubImage1D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLint , GLsizei , GLenum , GLenum , const GLvoid *)>(context->getProcAddress("glTexSubImage1D"));\ + + // OpenGL 1.3 + GetCompressedTexImage = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLvoid *)>(context->getProcAddress("glGetCompressedTexImage")); + CompressedTexSubImage1D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLint , GLsizei , GLenum , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTexSubImage1D")); + CompressedTexSubImage2D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLint , GLint , GLsizei , GLsizei , GLenum , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTexSubImage2D")); + CompressedTexImage1D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLenum , GLsizei , GLint , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTexImage1D")); + CompressedTexImage2D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLint , GLenum , GLsizei , GLsizei , GLint , GLsizei , const GLvoid *)>(context->getProcAddress("glCompressedTexImage2D")); + ActiveTexture = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum )>(context->getProcAddress("glActiveTexture")); + + // OpenGL 3.0 + GenerateMipmap = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum )>(context->getProcAddress("glGenerateMipmap")); + + // OpenGL 3.2 + TexImage3DMultisample = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLsizei , GLint , GLsizei , GLsizei , GLsizei , GLboolean )>(context->getProcAddress("glTexImage3DMultisample")); + TexImage2DMultisample = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLsizei , GLint , GLsizei , GLsizei , GLboolean )>(context->getProcAddress("glTexImage2DMultisample")); + + // OpenGL 4.2 + TexStorage3D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLsizei , GLenum , GLsizei , GLsizei , GLsizei )>(context->getProcAddress("glTexStorage3D")); + TexStorage2D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLsizei , GLenum , GLsizei , GLsizei )>(context->getProcAddress("glTexStorage2D")); + TexStorage1D = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLsizei , GLenum , GLsizei )>(context->getProcAddress("glTexStorage1D")); + + // OpenGL 4.3 + TexStorage3DMultisample = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLsizei , GLenum , GLsizei , GLsizei , GLsizei , GLboolean )>(context->getProcAddress("glTexStorage3DMultisample")); + TexStorage2DMultisample = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLsizei , GLenum , GLsizei , GLsizei , GLboolean )>(context->getProcAddress("glTexStorage2DMultisample")); + TexBufferRange = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLenum , GLenum , GLuint , GLintptr , GLsizeiptr )>(context->getProcAddress("glTexBufferRange")); + TextureView = reinterpret_cast<void (QOPENGLF_APIENTRYP)(GLuint , GLenum , GLuint , GLenum , GLuint , GLuint , GLuint , GLuint )>(context->getProcAddress("glTextureView")); +#endif +} + +void QOpenGLTextureHelper::dsa_TextureParameteri(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLint param) +{ + Q_UNUSED(bindingTarget); + TextureParameteriEXT(texture, target, pname, param); +} + +void QOpenGLTextureHelper::dsa_TextureParameteriv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLint *params) +{ + Q_UNUSED(bindingTarget); + TextureParameterivEXT(texture, target, pname, params); +} + +void QOpenGLTextureHelper::dsa_TextureParameterf(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLfloat param) +{ + Q_UNUSED(bindingTarget); + TextureParameterfEXT(texture, target, pname, param); +} + +void QOpenGLTextureHelper::dsa_TextureParameterfv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLfloat *params) +{ + Q_UNUSED(bindingTarget); + TextureParameterfvEXT(texture, target, pname, params); +} + +void QOpenGLTextureHelper::dsa_GenerateTextureMipmap(GLuint texture, GLenum target, GLenum bindingTarget) +{ + Q_UNUSED(bindingTarget); + GenerateTextureMipmapEXT(texture, target); +} + +void QOpenGLTextureHelper::dsa_TextureStorage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth) +{ + Q_UNUSED(bindingTarget); + TextureStorage3DEXT(texture, target, levels, internalFormat, width, height, depth); +} + +void QOpenGLTextureHelper::dsa_TextureStorage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height) +{ + Q_UNUSED(bindingTarget); + TextureStorage2DEXT(texture, target, levels, internalFormat, width, height); +} + +void QOpenGLTextureHelper::dsa_TextureStorage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width) +{ + Q_UNUSED(bindingTarget); + TextureStorage1DEXT(texture, target, levels, internalFormat, width); +} + +void QOpenGLTextureHelper::dsa_TextureStorage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations) +{ + Q_UNUSED(bindingTarget); + TextureStorage3DMultisampleEXT(texture, target, samples, internalFormat, width, height, depth, fixedSampleLocations); +} + +void QOpenGLTextureHelper::dsa_TextureStorage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations) +{ + Q_UNUSED(bindingTarget); + TextureStorage2DMultisampleEXT(texture, target, samples, internalFormat, width, height, fixedSampleLocations); +} + +void QOpenGLTextureHelper::dsa_TextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) +{ + Q_UNUSED(bindingTarget); + TextureImage3DEXT(texture, target, level, internalFormat, width, height, depth, border, format, type, pixels); +} + +void QOpenGLTextureHelper::dsa_TextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels) +{ + Q_UNUSED(bindingTarget); + TextureImage2DEXT(texture, target, level, internalFormat, width, height, border, format, type, pixels); +} + +void QOpenGLTextureHelper::dsa_TextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels) +{ + Q_UNUSED(bindingTarget); + TextureImage1DEXT(texture, target, level, internalFormat, width, border, format, type, pixels); +} + +void QOpenGLTextureHelper::dsa_TextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) +{ + Q_UNUSED(bindingTarget); + TextureSubImage3DEXT(texture, target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels); +} + +void QOpenGLTextureHelper::dsa_TextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels) +{ + Q_UNUSED(bindingTarget); + TextureSubImage2DEXT(texture, target, level, xoffset, yoffset, width, height, format, type, pixels); +} + +void QOpenGLTextureHelper::dsa_TextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels) +{ + Q_UNUSED(bindingTarget); + TextureSubImage1DEXT(texture, target, level, xoffset, width, format, type, pixels); +} + +void QOpenGLTextureHelper::dsa_TextureImage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations) +{ + Q_UNUSED(bindingTarget); + TextureImage3DMultisampleNV(texture, target, samples, internalFormat, width, height, depth, fixedSampleLocations); +} + +void QOpenGLTextureHelper::dsa_TextureImage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations) +{ + Q_UNUSED(bindingTarget); + TextureImage2DMultisampleNV(texture, target, samples, internalFormat, width, height, fixedSampleLocations); +} + +void QOpenGLTextureHelper::dsa_CompressedTextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLsizei width, GLenum format, GLsizei imageSize, const GLvoid *bits) +{ + Q_UNUSED(bindingTarget); + CompressedTextureSubImage1DEXT(texture, target, level, xoffset, width, format, imageSize, bits); +} + +void QOpenGLTextureHelper::dsa_CompressedTextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid *bits) +{ + Q_UNUSED(bindingTarget); + CompressedTextureSubImage2DEXT(texture, target, level, xoffset, yoffset, width, height, format, imageSize, bits); +} + +void QOpenGLTextureHelper::dsa_CompressedTextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *bits) +{ + Q_UNUSED(bindingTarget); + CompressedTextureSubImage3DEXT(texture, target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, bits); +} + +void QOpenGLTextureHelper::dsa_CompressedTextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *bits) +{ + Q_UNUSED(bindingTarget); + CompressedTextureImage1DEXT(texture, target, level, internalFormat, width, border, imageSize, bits); +} + +void QOpenGLTextureHelper::dsa_CompressedTextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *bits) +{ + Q_UNUSED(bindingTarget); + CompressedTextureImage2DEXT(texture, target, level, internalFormat, width, height, border, imageSize, bits); +} + +void QOpenGLTextureHelper::dsa_CompressedTextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *bits) +{ + Q_UNUSED(bindingTarget); + CompressedTextureImage3DEXT(texture, target, level, internalFormat, width, height, depth, border, imageSize, bits); +} + +namespace { + +class TextureBinder +{ +public: + TextureBinder(QOpenGLFunctions *functions, GLuint texture, GLenum target, GLenum bindingTarget) + : m_functions(functions) + { + // For cubemaps we can't use the standard DSA emulation as it is illegal to + // try to bind a texture to one of the cubemap face targets. So we force the + // target and binding target to the cubemap values in this case. + switch (target) { + case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: + case GL_TEXTURE_CUBE_MAP_POSITIVE_X: + case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: + case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: + case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: + case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: + bindingTarget = GL_TEXTURE_BINDING_CUBE_MAP; + m_target = GL_TEXTURE_CUBE_MAP; + break; + + default: + m_target = target; + break; + } + + m_functions->glGetIntegerv(bindingTarget, &m_oldTexture); + m_functions->glBindTexture(m_target, texture); + } + + ~TextureBinder() + { + m_functions->glBindTexture(m_target, m_oldTexture); + } + +private: + QOpenGLFunctions *m_functions; + GLenum m_target; + GLint m_oldTexture; +}; + +} // namespace + +void QOpenGLTextureHelper::qt_TextureParameteri(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLint param) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + functions->glTexParameteri(target, pname, param); +} + +void QOpenGLTextureHelper::qt_TextureParameteriv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLint *params) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + functions->glTexParameteriv(target, pname, params); +} + +void QOpenGLTextureHelper::qt_TextureParameterf(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLfloat param) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + functions->glTexParameterf(target, pname, param); +} + +void QOpenGLTextureHelper::qt_TextureParameterfv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLfloat *params) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + functions->glTexParameterfv(target, pname, params); +} + +void QOpenGLTextureHelper::qt_GenerateTextureMipmap(GLuint texture, GLenum target, GLenum bindingTarget) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + functions->glGenerateMipmap(target); +} + +void QOpenGLTextureHelper::qt_TextureStorage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexStorage3D(target, levels, internalFormat, width, height, depth); +} + +void QOpenGLTextureHelper::qt_TextureStorage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexStorage2D(target, levels, internalFormat, width, height); +} + +void QOpenGLTextureHelper::qt_TextureStorage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexStorage1D(target, levels, internalFormat, width); +} + +void QOpenGLTextureHelper::qt_TextureStorage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexStorage3DMultisample(target, samples, internalFormat, width, height, depth, fixedSampleLocations); +} + +void QOpenGLTextureHelper::qt_TextureStorage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexStorage2DMultisample(target, samples, internalFormat, width, height, fixedSampleLocations); +} + +void QOpenGLTextureHelper::qt_TextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexImage3D(target, level, internalFormat, width, height, depth, border, format, type, pixels); +} + +void QOpenGLTextureHelper::qt_TextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + functions->glTexImage2D(target, level, internalFormat, width, height, border, format, type, pixels); +} + +void QOpenGLTextureHelper::qt_TextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexImage1D(target, level, internalFormat, width, border, format, type, pixels); +} + +void QOpenGLTextureHelper::qt_TextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels); +} + +void QOpenGLTextureHelper::qt_TextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + functions->glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels); +} + +void QOpenGLTextureHelper::qt_TextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexSubImage1D(target, level, xoffset, width, format, type, pixels); +} + +void QOpenGLTextureHelper::qt_TextureImage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexImage3DMultisample(target, samples, internalFormat, width, height, depth, fixedSampleLocations); +} + +void QOpenGLTextureHelper::qt_TextureImage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glTexImage2DMultisample(target, samples, internalFormat, width, height, fixedSampleLocations); +} + +void QOpenGLTextureHelper::qt_CompressedTextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLsizei width, GLenum format, GLsizei imageSize, const GLvoid *bits) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glCompressedTexSubImage1D(target, level, xoffset, width, format, imageSize, bits); +} + +void QOpenGLTextureHelper::qt_CompressedTextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid *bits) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glCompressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, imageSize, bits); +} + +void QOpenGLTextureHelper::qt_CompressedTextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *bits) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glCompressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, bits); +} + +void QOpenGLTextureHelper::qt_CompressedTextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *bits) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glCompressedTexImage1D(target, level, internalFormat, width, border, imageSize, bits); +} + +void QOpenGLTextureHelper::qt_CompressedTextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *bits) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glCompressedTexImage2D(target, level, internalFormat, width, height, border, imageSize, bits); +} + +void QOpenGLTextureHelper::qt_CompressedTextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *bits) +{ + TextureBinder binder(functions, texture, target, bindingTarget); + glCompressedTexImage3D(target, level, internalFormat, width, height, depth, border, imageSize, bits); +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopengltexturehelper_p.h b/src/opengl/qopengltexturehelper_p.h new file mode 100644 index 0000000000..a62a47d029 --- /dev/null +++ b/src/opengl/qopengltexturehelper_p.h @@ -0,0 +1,797 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB). +** 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 QOPENGLTEXTUREHELPER_P_H +#define QOPENGLTEXTUREHELPER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtOpenGL/qtopenglglobal.h> + +#ifndef QT_NO_OPENGL + +#include "qopengl.h" +#include "qopenglpixeltransferoptions.h" +#include "qopengltexture.h" +#include "qopenglfunctions.h" + +QT_BEGIN_NAMESPACE + +// Constants for OpenGL and OpenGL ES 3.0+ which are not available with OpenGL ES 2.0. +#ifndef GL_TEXTURE_BASE_LEVEL +#define GL_TEXTURE_BASE_LEVEL 0x813C +#endif +#ifndef GL_TEXTURE_MAX_LEVEL +#define GL_TEXTURE_MAX_LEVEL 0x813D +#endif +#ifndef GL_TEXTURE_COMPARE_MODE +#define GL_TEXTURE_COMPARE_MODE 0x884C +#endif +#ifndef GL_TEXTURE_COMPARE_FUNC +#define GL_TEXTURE_COMPARE_FUNC 0x884D +#endif + +// use GL_APICALL only on Android + __clang__ +#if !defined(Q_OS_ANDROID) || !defined(__clang__) +# undef GL_APICALL +# define GL_APICALL +#elif !defined(GL_APICALL) +# define GL_APICALL +#endif + +class QOpenGLContext; + +class QOpenGLTextureHelper +{ +public: + QOpenGLTextureHelper(QOpenGLContext *context); + + // DSA-like API. Will either use real DSA or our emulation + inline void glTextureParameteri(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLint param) + { + (this->*TextureParameteri)(texture, target, bindingTarget, pname, param); + } + + inline void glTextureParameteriv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLint *params) + { + (this->*TextureParameteriv)(texture, target, bindingTarget, pname, params); + } + + inline void glTextureParameterf(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLfloat param) + { + (this->*TextureParameterf)(texture, target, bindingTarget, pname, param); + } + + inline void glTextureParameterfv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLfloat *params) + { + (this->*TextureParameterfv)(texture, target, bindingTarget, pname, params); + } + + inline void glGenerateTextureMipmap(GLuint texture, GLenum target, GLenum bindingTarget) + { + (this->*GenerateTextureMipmap)(texture, target, bindingTarget); + } + + inline void glTextureStorage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth) + { + (this->*TextureStorage3D)(texture, target, bindingTarget, levels, internalFormat, width, height, depth); + } + + inline void glTextureStorage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, + GLsizei width, GLsizei height) + { + (this->*TextureStorage2D)(texture, target, bindingTarget, levels, internalFormat, width, height); + } + + inline void glTextureStorage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, + GLsizei width) + { + (this->*TextureStorage1D)(texture, target, bindingTarget, levels, internalFormat, width); + } + + inline void glTextureStorage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations) + { + (this->*TextureStorage3DMultisample)(texture, target, bindingTarget, samples, internalFormat, width, height, depth, fixedSampleLocations); + } + + inline void glTextureStorage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, + GLsizei width, GLsizei height, GLboolean fixedSampleLocations) + { + (this->*TextureStorage2DMultisample)(texture, target, bindingTarget, samples, internalFormat, width, height, fixedSampleLocations); + } + + inline void glTextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) + { + (this->*TextureImage3D)(texture, target, bindingTarget, level, internalFormat, width, height, depth, border, format, type, pixels); + } + + inline void glTextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels) + { + (this->*TextureImage2D)(texture, target, bindingTarget, level, internalFormat, width, height, border, format, type, pixels); + } + + inline void glTextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels) + { + (this->*TextureImage1D)(texture, target, bindingTarget, level, internalFormat, width, border, format, type, pixels); + } + + inline void glTextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, + GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, + const GLvoid *pixels, const QOpenGLPixelTransferOptions * const options = nullptr) + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*TextureSubImage3D)(texture, target, bindingTarget, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels); + setPixelUploadOptions(oldOptions); + } else { + (this->*TextureSubImage3D)(texture, target, bindingTarget, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels); + } + } + + inline void glTextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, + GLsizei width, GLsizei height, GLenum format, GLenum type, + const GLvoid *pixels, const QOpenGLPixelTransferOptions * const options = nullptr) + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*TextureSubImage2D)(texture, target, bindingTarget, level, xoffset, yoffset, width, height, format, type, pixels); + setPixelUploadOptions(oldOptions); + } else { + (this->*TextureSubImage2D)(texture, target, bindingTarget, level, xoffset, yoffset, width, height, format, type, pixels); + } + } + + inline void glTextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, + GLsizei width, GLenum format, GLenum type, + const GLvoid *pixels, const QOpenGLPixelTransferOptions * const options = nullptr) + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*TextureSubImage1D)(texture, target, bindingTarget, level, xoffset, width, format, type, pixels); + setPixelUploadOptions(oldOptions); + } else { + (this->*TextureSubImage1D)(texture, target, bindingTarget, level, xoffset, width, format, type, pixels); + } + } + + inline void glTextureImage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations) + { + (this->*TextureImage3DMultisample)(texture, target, bindingTarget, samples, internalFormat, width, height, depth, fixedSampleLocations); + } + + inline void glTextureImage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, + GLsizei width, GLsizei height, GLboolean fixedSampleLocations) + { + (this->*TextureImage2DMultisample)(texture, target, bindingTarget, samples, internalFormat, width, height, fixedSampleLocations); + } + + inline void glCompressedTextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLsizei width, + GLenum format, GLsizei imageSize, const GLvoid *bits, + const QOpenGLPixelTransferOptions * const options = nullptr) + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*CompressedTextureSubImage1D)(texture, target, bindingTarget, level, xoffset, width, format, imageSize, bits); + setPixelUploadOptions(oldOptions); + } else { + (this->*CompressedTextureSubImage1D)(texture, target, bindingTarget, level, xoffset, width, format, imageSize, bits); + } + } + + inline void glCompressedTextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLint yoffset, + GLsizei width, GLsizei height, + GLenum format, GLsizei imageSize, const GLvoid *bits, + const QOpenGLPixelTransferOptions * const options = nullptr) + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*CompressedTextureSubImage2D)(texture, target, bindingTarget, level, xoffset, yoffset, width, height, format, imageSize, bits); + setPixelUploadOptions(oldOptions); + } else { + (this->*CompressedTextureSubImage2D)(texture, target, bindingTarget, level, xoffset, yoffset, width, height, format, imageSize, bits); + } + } + + inline void glCompressedTextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLint yoffset, GLint zoffset, + GLsizei width, GLsizei height, GLsizei depth, + GLenum format, GLsizei imageSize, const GLvoid *bits, + const QOpenGLPixelTransferOptions * const options = nullptr) + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*CompressedTextureSubImage3D)(texture, target, bindingTarget, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, bits); + setPixelUploadOptions(oldOptions); + } else { + (this->*CompressedTextureSubImage3D)(texture, target, bindingTarget, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, bits); + } + } + + inline void glCompressedTextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLenum internalFormat, GLsizei width, + GLint border, GLsizei imageSize, const GLvoid *bits, + const QOpenGLPixelTransferOptions * const options = nullptr) + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*CompressedTextureImage1D)(texture, target, bindingTarget, level, internalFormat, width, border, imageSize, bits); + setPixelUploadOptions(oldOptions); + } else { + (this->*CompressedTextureImage1D)(texture, target, bindingTarget, level, internalFormat, width, border, imageSize, bits); + } + } + + inline void glCompressedTextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLenum internalFormat, GLsizei width, GLsizei height, + GLint border, GLsizei imageSize, const GLvoid *bits, + const QOpenGLPixelTransferOptions * const options = nullptr) + + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*CompressedTextureImage2D)(texture, target, bindingTarget, level, internalFormat, width, height, border, imageSize, bits); + setPixelUploadOptions(oldOptions); + } else { + (this->*CompressedTextureImage2D)(texture, target, bindingTarget, level, internalFormat, width, height, border, imageSize, bits); + } + } + + inline void glCompressedTextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, + GLint border, GLsizei imageSize, const GLvoid *bits, + const QOpenGLPixelTransferOptions * const options = nullptr) + { + if (options) { + QOpenGLPixelTransferOptions oldOptions = savePixelUploadOptions(); + setPixelUploadOptions(*options); + (this->*CompressedTextureImage3D)(texture, target, bindingTarget, level, internalFormat, width, height, depth, border, imageSize, bits); + setPixelUploadOptions(oldOptions); + } else { + (this->*CompressedTextureImage3D)(texture, target, bindingTarget, level, internalFormat, width, height, depth, border, imageSize, bits); + } + } + +private: + // DSA wrapper (so we can use pointer to member function as switch) + void dsa_TextureParameteri(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLint param); + + void dsa_TextureParameteriv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLint *params); + + void dsa_TextureParameterf(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLfloat param); + + void dsa_TextureParameterfv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLfloat *params); + + void dsa_GenerateTextureMipmap(GLuint texture, GLenum target, GLenum bindingTarget); + + void dsa_TextureStorage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth); + + void dsa_TextureStorage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, + GLsizei width, GLsizei height); + + void dsa_TextureStorage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, + GLsizei width); + + void dsa_TextureStorage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations); + + void dsa_TextureStorage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, + GLsizei width, GLsizei height, GLboolean fixedSampleLocations); + + void dsa_TextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + + void dsa_TextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + + void dsa_TextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + + void dsa_TextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, + GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); + + void dsa_TextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, + GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels); + + void dsa_TextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, + GLsizei width, GLenum format, GLenum type, const GLvoid *pixels); + + void dsa_TextureImage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations); + + void dsa_TextureImage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, + GLsizei width, GLsizei height, GLboolean fixedSampleLocations); + + void dsa_CompressedTextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLsizei width, + GLenum format, GLsizei imageSize, const GLvoid *bits); + + void dsa_CompressedTextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLint yoffset, + GLsizei width, GLsizei height, + GLenum format, GLsizei imageSize, const GLvoid *bits); + + void dsa_CompressedTextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLint yoffset, GLint zoffset, + GLsizei width, GLsizei height, GLsizei depth, + GLenum format, GLsizei imageSize, const GLvoid *bits); + + void dsa_CompressedTextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLenum internalFormat, GLsizei width, + GLint border, GLsizei imageSize, const GLvoid *bits); + + void dsa_CompressedTextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLenum internalFormat, GLsizei width, GLsizei height, + GLint border, GLsizei imageSize, const GLvoid *bits); + + void dsa_CompressedTextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, + GLint border, GLsizei imageSize, const GLvoid *bits); + + // DSA emulation API + void qt_TextureParameteri(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLint param); + + void qt_TextureParameteriv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLint *params); + + void qt_TextureParameterf(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLfloat param); + + void qt_TextureParameterfv(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLfloat *params); + + void qt_GenerateTextureMipmap(GLuint texture, GLenum target, GLenum bindingTarget); + + void qt_TextureStorage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, + GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth); + + void qt_TextureStorage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, + GLenum internalFormat, GLsizei width, GLsizei height); + + void qt_TextureStorage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, + GLenum internalFormat, GLsizei width); + + void qt_TextureStorage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, + GLenum internalFormat, GLsizei width, GLsizei height, + GLsizei depth, GLboolean fixedSampleLocations); + + void qt_TextureStorage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, + GLenum internalFormat, GLsizei width, GLsizei height, + GLboolean fixedSampleLocations); + + void qt_TextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth, + GLint border, GLenum format, GLenum type, + const GLvoid *pixels); + + void qt_TextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, + GLint border, GLenum format, GLenum type, + const GLvoid *pixels); + + void qt_TextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLint border, GLenum format, GLenum type, + const GLvoid *pixels); + + void qt_TextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLint yoffset, GLint zoffset, + GLsizei width, GLsizei height, GLsizei depth, + GLenum format, GLenum type, const GLvoid *pixels); + + void qt_TextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLint yoffset, + GLsizei width, GLsizei height, + GLenum format, GLenum type, const GLvoid *pixels); + + void qt_TextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLsizei width, + GLenum format, GLenum type, const GLvoid *pixels); + + void qt_TextureImage3DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, + GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, + GLboolean fixedSampleLocations); + + void qt_TextureImage2DMultisample(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, + GLint internalFormat, GLsizei width, GLsizei height, + GLboolean fixedSampleLocations); + + void qt_CompressedTextureSubImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLsizei width, GLenum format, + GLsizei imageSize, const GLvoid *bits); + + void qt_CompressedTextureSubImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLint yoffset, + GLsizei width, GLsizei height, + GLenum format, GLsizei imageSize, const GLvoid *bits); + + void qt_CompressedTextureSubImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, + GLint xoffset, GLint yoffset, GLint zoffset, + GLsizei width, GLsizei height, GLsizei depth, + GLenum format, GLsizei imageSize, const GLvoid *bits); + + void qt_CompressedTextureImage1D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLint border, + GLsizei imageSize, const GLvoid *bits); + + void qt_CompressedTextureImage2D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, GLint border, + GLsizei imageSize, const GLvoid *bits); + + void qt_CompressedTextureImage3D(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLint border, + GLsizei imageSize, const GLvoid *bits); + +public: + // Raw OpenGL functions, resolved and used by our DSA-like static functions if no EXT_direct_state_access is available + + // OpenGL 1.0 + inline void glTexImage1D(GLenum target, GLint level, GLint internalFormat, + GLsizei width, GLint border, + GLenum format, GLenum type, const GLvoid *pixels) + { + TexImage1D(target, level, internalFormat, width, border, format, type, pixels); + } + + // OpenGL 1.1 + inline void glTexSubImage1D(GLenum target, GLint level, GLint xoffset, GLsizei width, + GLenum format, GLenum type, const GLvoid *pixels) + { + TexSubImage1D(target, level, xoffset, width, format, type, pixels); + } + + // OpenGL 1.2 + inline void glTexImage3D(GLenum target, GLint level, GLint internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLint border, + GLenum format, GLenum type, const GLvoid *pixels) + { + TexImage3D(target, level, internalFormat, width, height, depth, border, format, type, pixels); + } + + inline void glTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, + GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) + { + TexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels); + } + + // OpenGL 1.3 + inline void glGetCompressedTexImage(GLenum target, GLint level, GLvoid *img) + { + GetCompressedTexImage(target, level, img); + } + + inline void glCompressedTexSubImage1D(GLenum target, GLint level, GLint xoffset, GLsizei width, + GLenum format, GLsizei imageSize, const GLvoid *data) + { + CompressedTexSubImage1D(target, level, xoffset, width, format, imageSize, data); + } + + inline void glCompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, + GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid *data) + { + CompressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, imageSize, data); + } + + inline void glCompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, + GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *data) + { + CompressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, data); + } + + inline void glCompressedTexImage1D(GLenum target, GLint level, GLenum internalFormat, GLsizei width, + GLint border, GLsizei imageSize, const GLvoid *data) + { + CompressedTexImage1D(target, level, internalFormat, width, border, imageSize, data); + } + + inline void glCompressedTexImage2D(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, + GLint border, GLsizei imageSize, const GLvoid *data) + { + CompressedTexImage2D(target, level, internalFormat, width, height, border, imageSize, data); + } + + inline void glCompressedTexImage3D(GLenum target, GLint level, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth, + GLint border, GLsizei imageSize, const GLvoid *data) + { + CompressedTexImage3D(target, level, internalFormat, width, height, depth, border, imageSize, data); + } + + inline void glActiveTexture(GLenum texture) + { + ActiveTexture(texture); + } + + // OpenGL 3.0 + inline void glGenerateMipmap(GLenum target) + { + GenerateMipmap(target); + } + + // OpenGL 3.2 + inline void glTexImage3DMultisample(GLenum target, GLsizei samples, GLint internalFormat, + GLsizei width, GLsizei height, GLsizei depth, + GLboolean fixedSampleLocations) + { + TexImage3DMultisample(target, samples, internalFormat, width, height, depth, fixedSampleLocations); + } + + inline void glTexImage2DMultisample(GLenum target, GLsizei samples, GLint internalFormat, + GLsizei width, GLsizei height, + GLboolean fixedSampleLocations) + { + TexImage2DMultisample(target, samples, internalFormat, width, height, fixedSampleLocations); + } + + // OpenGL 4.2 + inline void glTexStorage3D(GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth) + { + TexStorage3D(target, levels, internalFormat, width, height, depth); + } + + inline void glTexStorage2D(GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height) + { + TexStorage2D(target, levels, internalFormat, width, height); + } + + inline void glTexStorage1D(GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width) + { + TexStorage1D(target, levels, internalFormat, width); + } + + // OpenGL 4.3 + inline void glTexStorage3DMultisample(GLenum target, GLsizei samples, GLenum internalFormat, + GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations) + { + TexStorage3DMultisample(target, samples, internalFormat, width, height, depth, fixedSampleLocations); + } + + inline void glTexStorage2DMultisample(GLenum target, GLsizei samples, GLenum internalFormat, + GLsizei width, GLsizei height, GLboolean fixedSampleLocations) + { + TexStorage2DMultisample(target, samples, internalFormat, width, height, fixedSampleLocations); + } + + inline void glTexBufferRange(GLenum target, GLenum internalFormat, GLuint buffer, + GLintptr offset, GLsizeiptr size) + { + TexBufferRange(target, internalFormat, buffer, offset, size); + } + + inline void glTextureView(GLuint texture, GLenum target, GLuint origTexture, GLenum internalFormat, + GLuint minLevel, GLuint numLevels, GLuint minLayer, GLuint numLayers) + { + TextureView(texture, target, origTexture, internalFormat, minLevel, numLevels, minLayer, numLayers); + } + + // Helper functions + inline QOpenGLPixelTransferOptions savePixelUploadOptions() + { + QOpenGLPixelTransferOptions options; + int val = 0; + functions->glGetIntegerv(GL_UNPACK_ALIGNMENT, &val); + options.setAlignment(val); +#if !defined(QT_OPENGL_ES_2) + functions->glGetIntegerv(GL_UNPACK_SKIP_IMAGES, &val); + options.setSkipImages(val); + functions->glGetIntegerv(GL_UNPACK_SKIP_ROWS, &val); + options.setSkipRows(val); + functions->glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &val); + options.setSkipPixels(val); + functions->glGetIntegerv(GL_UNPACK_IMAGE_HEIGHT, &val); + options.setImageHeight(val); + functions->glGetIntegerv(GL_UNPACK_ROW_LENGTH, &val); + options.setRowLength(val); + GLboolean b = GL_FALSE; + functions->glGetBooleanv(GL_UNPACK_LSB_FIRST, &b); + options.setLeastSignificantByteFirst(b); + functions->glGetBooleanv(GL_UNPACK_SWAP_BYTES, &b); + options.setSwapBytesEnabled(b); +#endif + return options; + } + + inline void setPixelUploadOptions(const QOpenGLPixelTransferOptions &options) + { + functions->glPixelStorei(GL_UNPACK_ALIGNMENT, options.alignment()); +#if !defined(QT_OPENGL_ES_2) + functions->glPixelStorei(GL_UNPACK_SKIP_IMAGES, options.skipImages()); + functions->glPixelStorei(GL_UNPACK_SKIP_ROWS, options.skipRows()); + functions->glPixelStorei(GL_UNPACK_SKIP_PIXELS, options.skipPixels()); + functions->glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, options.imageHeight()); + functions->glPixelStorei(GL_UNPACK_ROW_LENGTH, options.rowLength()); + functions->glPixelStorei(GL_UNPACK_LSB_FIRST, options.isLeastSignificantBitFirst()); + functions->glPixelStorei(GL_UNPACK_SWAP_BYTES, options.isSwapBytesEnabled()); +#endif + } + + QOpenGLFunctions *functions; +private: + // Typedefs and pointers to member functions used to switch between EXT_direct_state_access and our own emulated DSA. + // The argument match the corresponding GL function, but there's an extra "GLenum bindingTarget" which gets used with + // the DSA emulation -- it contains the right GL_BINDING_TEXTURE_X to use. + typedef void (QOpenGLTextureHelper::*TextureParameteriMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLint param); + typedef void (QOpenGLTextureHelper::*TextureParameterivMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLint *params); + typedef void (QOpenGLTextureHelper::*TextureParameterfMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, GLfloat param); + typedef void (QOpenGLTextureHelper::*TextureParameterfvMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLenum pname, const GLfloat *params); + typedef void (QOpenGLTextureHelper::*GenerateTextureMipmapMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget); + typedef void (QOpenGLTextureHelper::*TextureStorage3DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth); + typedef void (QOpenGLTextureHelper::*TextureStorage2DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height); + typedef void (QOpenGLTextureHelper::*TextureStorage1DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei levels, GLenum internalFormat, GLsizei width); + typedef void (QOpenGLTextureHelper::*TextureStorage3DMultisampleMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations); + typedef void (QOpenGLTextureHelper::*TextureStorage2DMultisampleMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations); + typedef void (QOpenGLTextureHelper::*TextureImage3DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + typedef void (QOpenGLTextureHelper::*TextureImage2DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + typedef void (QOpenGLTextureHelper::*TextureImage1DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + typedef void (QOpenGLTextureHelper::*TextureSubImage3DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); + typedef void (QOpenGLTextureHelper::*TextureSubImage2DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels); + typedef void (QOpenGLTextureHelper::*TextureSubImage1DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels); + typedef void (QOpenGLTextureHelper::*TextureImage3DMultisampleMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations); + typedef void (QOpenGLTextureHelper::*TextureImage2DMultisampleMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations); + typedef void (QOpenGLTextureHelper::*CompressedTextureSubImage1DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLsizei width, GLenum format, GLsizei imageSize, const GLvoid *bits); + typedef void (QOpenGLTextureHelper::*CompressedTextureSubImage2DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid *bits); + typedef void (QOpenGLTextureHelper::*CompressedTextureSubImage3DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *bits); + typedef void (QOpenGLTextureHelper::*CompressedTextureImage1DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *bits); + typedef void (QOpenGLTextureHelper::*CompressedTextureImage2DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *bits); + typedef void (QOpenGLTextureHelper::*CompressedTextureImage3DMemberFunc)(GLuint texture, GLenum target, GLenum bindingTarget, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *bits); + + + TextureParameteriMemberFunc TextureParameteri; + TextureParameterivMemberFunc TextureParameteriv; + TextureParameterfMemberFunc TextureParameterf; + TextureParameterfvMemberFunc TextureParameterfv; + GenerateTextureMipmapMemberFunc GenerateTextureMipmap; + TextureStorage3DMemberFunc TextureStorage3D; + TextureStorage2DMemberFunc TextureStorage2D; + TextureStorage1DMemberFunc TextureStorage1D; + TextureStorage3DMultisampleMemberFunc TextureStorage3DMultisample; + TextureStorage2DMultisampleMemberFunc TextureStorage2DMultisample; + TextureImage3DMemberFunc TextureImage3D; + TextureImage2DMemberFunc TextureImage2D; + TextureImage1DMemberFunc TextureImage1D; + TextureSubImage3DMemberFunc TextureSubImage3D; + TextureSubImage2DMemberFunc TextureSubImage2D; + TextureSubImage1DMemberFunc TextureSubImage1D; + TextureImage3DMultisampleMemberFunc TextureImage3DMultisample; + TextureImage2DMultisampleMemberFunc TextureImage2DMultisample; + CompressedTextureSubImage1DMemberFunc CompressedTextureSubImage1D; + CompressedTextureSubImage2DMemberFunc CompressedTextureSubImage2D; + CompressedTextureSubImage3DMemberFunc CompressedTextureSubImage3D; + CompressedTextureImage1DMemberFunc CompressedTextureImage1D; + CompressedTextureImage2DMemberFunc CompressedTextureImage2D; + CompressedTextureImage3DMemberFunc CompressedTextureImage3D; + + // Raw function pointers for core and DSA functions + + // EXT_direct_state_access used when DSA is available + void (QOPENGLF_APIENTRYP TextureParameteriEXT)(GLuint texture, GLenum target, GLenum pname, GLint param); + void (QOPENGLF_APIENTRYP TextureParameterivEXT)(GLuint texture, GLenum target, GLenum pname, const GLint *params); + void (QOPENGLF_APIENTRYP TextureParameterfEXT)(GLuint texture, GLenum target, GLenum pname, GLfloat param); + void (QOPENGLF_APIENTRYP TextureParameterfvEXT)(GLuint texture, GLenum target, GLenum pname, const GLfloat *params); + void (QOPENGLF_APIENTRYP GenerateTextureMipmapEXT)(GLuint texture, GLenum target); + void (QOPENGLF_APIENTRYP TextureStorage3DEXT)(GLuint texture, GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth); + void (QOPENGLF_APIENTRYP TextureStorage2DEXT)(GLuint texture, GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height); + void (QOPENGLF_APIENTRYP TextureStorage1DEXT)(GLuint texture, GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width); + void (QOPENGLF_APIENTRYP TextureStorage3DMultisampleEXT)(GLuint texture, GLenum target, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations); + void (QOPENGLF_APIENTRYP TextureStorage2DMultisampleEXT)(GLuint texture, GLenum target, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations); + void (QOPENGLF_APIENTRYP TextureImage3DEXT)(GLuint texture, GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + void (QOPENGLF_APIENTRYP TextureImage2DEXT)(GLuint texture, GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + void (QOPENGLF_APIENTRYP TextureImage1DEXT)(GLuint texture, GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + void (QOPENGLF_APIENTRYP TextureSubImage3DEXT)(GLuint texture, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); + void (QOPENGLF_APIENTRYP TextureSubImage2DEXT)(GLuint texture, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels); + void (QOPENGLF_APIENTRYP TextureSubImage1DEXT)(GLuint texture, GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels); + void (QOPENGLF_APIENTRYP CompressedTextureSubImage1DEXT)(GLuint texture, GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLsizei imageSize, const GLvoid *bits); + void (QOPENGLF_APIENTRYP CompressedTextureSubImage2DEXT)(GLuint texture, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid *bits); + void (QOPENGLF_APIENTRYP CompressedTextureSubImage3DEXT)(GLuint texture, GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *bits); + void (QOPENGLF_APIENTRYP CompressedTextureImage1DEXT)(GLuint texture, GLenum target, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *bits); + void (QOPENGLF_APIENTRYP CompressedTextureImage2DEXT)(GLuint texture, GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *bits); + void (QOPENGLF_APIENTRYP CompressedTextureImage3DEXT)(GLuint texture, GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *bits); + + + // Plus some missing ones that are in the NV_texture_multisample extension instead + void (QOPENGLF_APIENTRYP TextureImage3DMultisampleNV)(GLuint texture, GLenum target, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations); + void (QOPENGLF_APIENTRYP TextureImage2DMultisampleNV)(GLuint texture, GLenum target, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations); + + // OpenGL 1.0 + void (QOPENGLF_APIENTRYP TexImage1D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + + // OpenGL 1.1 + void (QOPENGLF_APIENTRYP TexSubImage1D)(GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLenum type, const GLvoid *pixels); + + // OpenGL 1.2 + void (QOPENGLF_APIENTRYP TexImage3D)(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + void (QOPENGLF_APIENTRYP TexSubImage3D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); + + // OpenGL 1.3 + void (QOPENGLF_APIENTRYP GetCompressedTexImage)(GLenum target, GLint level, GLvoid *img); + void (QOPENGLF_APIENTRYP CompressedTexSubImage1D)(GLenum target, GLint level, GLint xoffset, GLsizei width, GLenum format, GLsizei imageSize, const GLvoid *data); + GL_APICALL void (QOPENGLF_APIENTRYP CompressedTexSubImage2D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const GLvoid *data); + void (QOPENGLF_APIENTRYP CompressedTexSubImage3D)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const GLvoid *data); + void (QOPENGLF_APIENTRYP CompressedTexImage1D)(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *data); + GL_APICALL void (QOPENGLF_APIENTRYP CompressedTexImage2D)(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data); + void (QOPENGLF_APIENTRYP CompressedTexImage3D)(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const GLvoid *data); + GL_APICALL void (QOPENGLF_APIENTRYP ActiveTexture)(GLenum texture); + + // OpenGL 3.0 + GL_APICALL void (QOPENGLF_APIENTRYP GenerateMipmap)(GLenum target); + + // OpenGL 3.2 + void (QOPENGLF_APIENTRYP TexImage3DMultisample)(GLenum target, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations); + void (QOPENGLF_APIENTRYP TexImage2DMultisample)(GLenum target, GLsizei samples, GLint internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations); + + // OpenGL 4.2 + void (QOPENGLF_APIENTRYP TexStorage3D)(GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth); + void (QOPENGLF_APIENTRYP TexStorage2D)(GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width, GLsizei height); + void (QOPENGLF_APIENTRYP TexStorage1D)(GLenum target, GLsizei levels, GLenum internalFormat, GLsizei width); + + // OpenGL 4.3 + void (QOPENGLF_APIENTRYP TexStorage3DMultisample)(GLenum target, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLboolean fixedSampleLocations); + void (QOPENGLF_APIENTRYP TexStorage2DMultisample)(GLenum target, GLsizei samples, GLenum internalFormat, GLsizei width, GLsizei height, GLboolean fixedSampleLocations); + void (QOPENGLF_APIENTRYP TexBufferRange)(GLenum target, GLenum internalFormat, GLuint buffer, GLintptr offset, GLsizeiptr size); + void (QOPENGLF_APIENTRYP TextureView)(GLuint texture, GLenum target, GLuint origtexture, GLenum internalformat, GLuint minlevel, GLuint numlevels, GLuint minlayer, GLuint numlayers); +}; + +QT_END_NAMESPACE + +#undef Q_CALL_MEMBER_FUNCTION + +#endif // QT_NO_OPENGL + +#endif // QOPENGLTEXTUREHELPER_P_H diff --git a/src/opengl/qopengltextureuploader.cpp b/src/opengl/qopengltextureuploader.cpp new file mode 100644 index 0000000000..469ddc56c1 --- /dev/null +++ b/src/opengl/qopengltextureuploader.cpp @@ -0,0 +1,381 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** 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 "qopengltextureuploader_p.h" + +#include <qimage.h> +#include <qmath.h> +#include <qopenglfunctions.h> +#include <private/qopenglcontext_p.h> +#include <private/qopenglextensions_p.h> + +#ifndef GL_RED +#define GL_RED 0x1903 +#endif + +#ifndef GL_GREEN +#define GL_GREEN 0x1904 +#endif + +#ifndef GL_BLUE +#define GL_BLUE 0x1905 +#endif + +#ifndef GL_RGB10_A2 +#define GL_RGB10_A2 0x8059 +#endif + +#ifndef GL_RGBA16 +#define GL_RGBA16 0x805B +#endif + +#ifndef GL_BGR +#define GL_BGR 0x80E0 +#endif + +#ifndef GL_BGRA +#define GL_BGRA 0x80E1 +#endif + +#ifndef GL_UNSIGNED_INT_8_8_8_8_REV +#define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367 +#endif + +#ifndef GL_UNSIGNED_INT_2_10_10_10_REV +#define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368 +#endif + +#ifndef GL_TEXTURE_SWIZZLE_R +#define GL_TEXTURE_SWIZZLE_R 0x8E42 +#endif + +#ifndef GL_TEXTURE_SWIZZLE_G +#define GL_TEXTURE_SWIZZLE_G 0x8E43 +#endif + +#ifndef GL_TEXTURE_SWIZZLE_B +#define GL_TEXTURE_SWIZZLE_B 0x8E44 +#endif + +#ifndef GL_TEXTURE_SWIZZLE_A +#define GL_TEXTURE_SWIZZLE_A 0x8E45 +#endif + +#ifndef GL_SRGB +#define GL_SRGB 0x8C40 +#endif +#ifndef GL_SRGB_ALPHA +#define GL_SRGB_ALPHA 0x8C42 +#endif + +QT_BEGIN_NAMESPACE + +qsizetype QOpenGLTextureUploader::textureImage(GLenum target, const QImage &image, QOpenGLTextureUploader::BindOptions options, QSize maxSize) +{ + QOpenGLContext *context = QOpenGLContext::currentContext(); + QOpenGLExtensions *funcs = static_cast<QOpenGLExtensions*>(context->functions()); + + QImage tx; + GLenum externalFormat; + GLenum internalFormat; + GLuint pixelType; + QImage::Format targetFormat = QImage::Format_Invalid; + const bool isOpenGL12orBetter = !context->isOpenGLES() && (context->format().majorVersion() >= 2 || context->format().minorVersion() >= 2); + const bool isOpenGLES3orBetter = context->isOpenGLES() && context->format().majorVersion() >= 3; + const bool sRgbBinding = (options & SRgbBindOption); + Q_ASSERT(isOpenGL12orBetter || context->isOpenGLES()); + Q_ASSERT((options & (SRgbBindOption | UseRedForAlphaAndLuminanceBindOption)) != (SRgbBindOption | UseRedForAlphaAndLuminanceBindOption)); + + switch (image.format()) { + case QImage::Format_RGB32: + case QImage::Format_ARGB32: + case QImage::Format_ARGB32_Premultiplied: + if (isOpenGL12orBetter) { + externalFormat = GL_BGRA; + internalFormat = GL_RGBA; + pixelType = GL_UNSIGNED_INT_8_8_8_8_REV; +#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN + // Without GL_UNSIGNED_INT_8_8_8_8_REV, BGRA only matches ARGB on little endian: + } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::BGRATextureFormat) && !sRgbBinding) { + // The GL_EXT_texture_format_BGRA8888 extension requires the internal format to match the external. + externalFormat = internalFormat = GL_BGRA; + pixelType = GL_UNSIGNED_BYTE; + } else if (context->isOpenGLES() && context->hasExtension(QByteArrayLiteral("GL_APPLE_texture_format_BGRA8888"))) { + // Is only allowed as an external format like OpenGL. + externalFormat = GL_BGRA; + internalFormat = GL_RGBA; + pixelType = GL_UNSIGNED_BYTE; +#endif + } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) { +#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_BLUE); + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_RED); +#else + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_GREEN); + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_G, GL_BLUE); + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_ALPHA); + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_A, GL_RED); +#endif + externalFormat = internalFormat = GL_RGBA; + pixelType = GL_UNSIGNED_BYTE; + } else { + // No support for direct ARGB32 upload. + break; + } + targetFormat = image.format(); + break; + case QImage::Format_BGR30: + case QImage::Format_A2BGR30_Premultiplied: + if (sRgbBinding) { + // Always needs conversion + break; + } else if (isOpenGL12orBetter || isOpenGLES3orBetter) { + pixelType = GL_UNSIGNED_INT_2_10_10_10_REV; + externalFormat = GL_RGBA; + internalFormat = GL_RGB10_A2; + targetFormat = image.format(); + } + break; + case QImage::Format_RGB30: + case QImage::Format_A2RGB30_Premultiplied: + if (sRgbBinding) { + // Always needs conversion + break; + } else if (isOpenGL12orBetter) { + pixelType = GL_UNSIGNED_INT_2_10_10_10_REV; + externalFormat = GL_BGRA; + internalFormat = GL_RGB10_A2; + targetFormat = image.format(); + } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) { + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_RED); + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_BLUE); + pixelType = GL_UNSIGNED_INT_2_10_10_10_REV; + externalFormat = GL_RGBA; + internalFormat = GL_RGB10_A2; + targetFormat = image.format(); + } + break; + case QImage::Format_RGB444: + case QImage::Format_RGB555: + case QImage::Format_RGB16: + if (isOpenGL12orBetter || context->isOpenGLES()) { + externalFormat = internalFormat = GL_RGB; + pixelType = GL_UNSIGNED_SHORT_5_6_5; + targetFormat = QImage::Format_RGB16; + } + break; + case QImage::Format_RGB666: + case QImage::Format_RGB888: + externalFormat = internalFormat = GL_RGB; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = QImage::Format_RGB888; + break; + case QImage::Format_BGR888: + if (isOpenGL12orBetter) { + externalFormat = GL_BGR; + internalFormat = GL_RGB; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = QImage::Format_BGR888; + } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) { + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_RED); + funcs->glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_BLUE); + externalFormat = internalFormat = GL_RGB; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = QImage::Format_BGR888; + } + break; + case QImage::Format_RGBX8888: + case QImage::Format_RGBA8888: + case QImage::Format_RGBA8888_Premultiplied: + externalFormat = internalFormat = GL_RGBA; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + break; + case QImage::Format_RGBX64: + case QImage::Format_RGBA64: + case QImage::Format_RGBA64_Premultiplied: + externalFormat = internalFormat = GL_RGBA; + if (isOpenGL12orBetter || (context->isOpenGLES() && context->format().majorVersion() >= 3)) + internalFormat = GL_RGBA16; + pixelType = GL_UNSIGNED_SHORT; + targetFormat = image.format(); + break; + case QImage::Format_Indexed8: + if (sRgbBinding) { + // Always needs conversion + break; + } else if (options & UseRedForAlphaAndLuminanceBindOption) { + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } + break; + case QImage::Format_Alpha8: + if (sRgbBinding) { + // Always needs conversion + break; + } else if (options & UseRedForAlphaAndLuminanceBindOption) { + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) { + externalFormat = internalFormat = GL_ALPHA; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) { + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ALPHA); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ZERO); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ZERO); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ZERO); + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } + break; + case QImage::Format_Grayscale8: + if (sRgbBinding) { + // Always needs conversion + break; + } else if (options & UseRedForAlphaAndLuminanceBindOption) { + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) { + externalFormat = internalFormat = GL_LUMINANCE; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) { + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE); + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } + break; + case QImage::Format_Grayscale16: + if (sRgbBinding) { + // Always needs conversion + break; + } else if (options & UseRedForAlphaAndLuminanceBindOption) { + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_SHORT; + targetFormat = image.format(); + } else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) { + externalFormat = internalFormat = GL_LUMINANCE; + pixelType = GL_UNSIGNED_SHORT; + targetFormat = image.format(); + } else if (funcs->hasOpenGLExtension(QOpenGLExtensions::TextureSwizzle)) { + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED); + funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ONE); + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_SHORT; + targetFormat = image.format(); + } + break; + default: + break; + } + + // If no direct upload was detected above, convert to RGBA8888 and upload that + if (targetFormat == QImage::Format_Invalid) { + externalFormat = internalFormat = GL_RGBA; + pixelType = GL_UNSIGNED_BYTE; + if (!image.hasAlphaChannel()) + targetFormat = QImage::Format_RGBX8888; + else + targetFormat = QImage::Format_RGBA8888; + } + + if (options & PremultipliedAlphaBindOption) { + if (targetFormat == QImage::Format_ARGB32) + targetFormat = QImage::Format_ARGB32_Premultiplied; + else if (targetFormat == QImage::Format_RGBA8888) + targetFormat = QImage::Format_RGBA8888_Premultiplied; + else if (targetFormat == QImage::Format_RGBA64) + targetFormat = QImage::Format_RGBA64_Premultiplied; + } else { + if (targetFormat == QImage::Format_ARGB32_Premultiplied) + targetFormat = QImage::Format_ARGB32; + else if (targetFormat == QImage::Format_RGBA8888_Premultiplied) + targetFormat = QImage::Format_RGBA8888; + else if (targetFormat == QImage::Format_RGBA64_Premultiplied) + targetFormat = QImage::Format_RGBA64; + } + + if (sRgbBinding) { + Q_ASSERT(internalFormat == GL_RGBA || internalFormat == GL_RGB); + if (image.hasAlphaChannel()) + internalFormat = GL_SRGB_ALPHA; + else + internalFormat = GL_SRGB; + } + + if (image.format() != targetFormat) + tx = image.convertToFormat(targetFormat); + else + tx = image; + + QSize newSize = tx.size(); + if (!maxSize.isEmpty()) + newSize = newSize.boundedTo(maxSize); + if (options & PowerOfTwoBindOption) { + newSize.setWidth(qNextPowerOfTwo(newSize.width() - 1)); + newSize.setHeight(qNextPowerOfTwo(newSize.height() - 1)); + } + + if (newSize != tx.size()) + tx = tx.scaled(newSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + + // Handle cases where the QImage is actually a sub image of its image data: + qsizetype naturalBpl = ((qsizetype(tx.width()) * tx.depth() + 31) >> 5) << 2; + if (tx.bytesPerLine() != naturalBpl) + tx = tx.copy(tx.rect()); + + funcs->glTexImage2D(target, 0, internalFormat, tx.width(), tx.height(), 0, externalFormat, pixelType, tx.constBits()); + + qsizetype cost = qint64(tx.width()) * tx.height() * tx.depth() / 8; + + return cost; +} + +QT_END_NAMESPACE diff --git a/src/opengl/qopengltextureuploader_p.h b/src/opengl/qopengltextureuploader_p.h new file mode 100644 index 0000000000..cea6d97658 --- /dev/null +++ b/src/opengl/qopengltextureuploader_p.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** 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$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef QOPENGLTEXTUREUPLOADER_P_H +#define QOPENGLTEXTUREUPLOADER_P_H + +#include <QtCore/qsize.h> +#include <QtOpenGL/qtopenglglobal.h> +#include <QtGui/private/qopenglcontext_p.h> + +QT_BEGIN_NAMESPACE + +class QImage; + +class Q_OPENGL_EXPORT QOpenGLTextureUploader +{ +public: + enum BindOption { + NoBindOption = 0x0000, + PremultipliedAlphaBindOption = 0x0001, + UseRedForAlphaAndLuminanceBindOption = 0x0002, + SRgbBindOption = 0x0004, + PowerOfTwoBindOption = 0x0008 + }; + Q_DECLARE_FLAGS(BindOptions, BindOption) + Q_FLAGS(BindOptions) + + static qsizetype textureImage(GLenum target, const QImage &image, BindOptions options, QSize maxSize = QSize()); + +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(QOpenGLTextureUploader::BindOptions) + +QT_END_NAMESPACE + +#endif + diff --git a/src/opengl/qopenglwidget.cpp b/src/opengl/qopenglwidget.cpp index a6c63ae034..91e9fd545d 100644 --- a/src/opengl/qopenglwidget.cpp +++ b/src/opengl/qopenglwidget.cpp @@ -45,14 +45,16 @@ #include <QtGui/QWindow> #include <QtGui/QGuiApplication> #include <QtGui/QScreen> -#include <QtGui/QOpenGLPaintDevice> #include <QtGui/qpa/qplatformwindow.h> #include <QtGui/qpa/qplatformintegration.h> +#include <QtOpenGL/QOpenGLPaintDevice> + #include <QtGui/private/qguiapplication_p.h> #include <QtGui/private/qopenglextensions_p.h> #include <QtGui/private/qfont_p.h> -#include <QtGui/private/qopenglpaintdevice_p.h> #include <QtGui/private/qopenglcontext_p.h> +#include <QtOpenGL/private/qopenglpaintdevice_p.h> + #include <QtWidgets/private/qwidget_p.h> QT_BEGIN_NAMESPACE diff --git a/src/opengl/qopenglwindow.cpp b/src/opengl/qopenglwindow.cpp index 8cdf134bfd..9328d9b46a 100644 --- a/src/opengl/qopenglwindow.cpp +++ b/src/opengl/qopenglwindow.cpp @@ -39,7 +39,6 @@ #include "qopenglwindow.h" #include <QtGui/QOpenGLFramebufferObject> -#include <QtGui/QOpenGLPaintDevice> #include <QtGui/QOpenGLFunctions> #include <QtGui/QOpenGLTextureBlitter> #include <QtGui/private/qpaintdevicewindow_p.h> @@ -47,6 +46,7 @@ #include <QtGui/private/qopenglcontext_p.h> #include <QtGui/QMatrix4x4> #include <QtGui/QOffscreenSurface> +#include <QtOpenGL/QOpenGLPaintDevice> QT_BEGIN_NAMESPACE |