1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtOpenGL module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qopenglcustomshaderstage_p.h"
41#include "qopenglengineshadermanager_p.h"
42#include "qopenglpaintengine_p.h"
43#include <private/qpainter_p.h>
44
45QT_BEGIN_NAMESPACE
46
47class QOpenGLCustomShaderStagePrivate
48{
49public:
50 QOpenGLCustomShaderStagePrivate() :
51 m_manager(nullptr) {}
52
53 QPointer<QOpenGLEngineShaderManager> m_manager;
54 QByteArray m_source;
55};
56
57
58
59
60QOpenGLCustomShaderStage::QOpenGLCustomShaderStage()
61 : d_ptr(new QOpenGLCustomShaderStagePrivate)
62{
63}
64
65QOpenGLCustomShaderStage::~QOpenGLCustomShaderStage()
66{
67 Q_D(QOpenGLCustomShaderStage);
68 if (d->m_manager) {
69 d->m_manager->removeCustomStage();
70 d->m_manager->sharedShaders->cleanupCustomStage(this);
71 }
72 delete d_ptr;
73}
74
75void QOpenGLCustomShaderStage::setUniformsDirty()
76{
77 Q_D(QOpenGLCustomShaderStage);
78 if (d->m_manager)
79 d->m_manager->setDirty(); // ### Probably a bit overkill!
80}
81
82bool QOpenGLCustomShaderStage::setOnPainter(QPainter* p)
83{
84 Q_D(QOpenGLCustomShaderStage);
85 if (p->paintEngine()->type() != QPaintEngine::OpenGL2) {
86 qWarning("QOpenGLCustomShaderStage::setOnPainter() - paint engine not OpenGL2");
87 return false;
88 }
89 if (d->m_manager)
90 qWarning("Custom shader is already set on a painter");
91
92 QOpenGL2PaintEngineEx *engine = static_cast<QOpenGL2PaintEngineEx*>(p->paintEngine());
93 d->m_manager = QOpenGL2PaintEngineExPrivate::shaderManagerForEngine(engine);
94 Q_ASSERT(d->m_manager);
95
96 d->m_manager->setCustomStage(this);
97 return true;
98}
99
100void QOpenGLCustomShaderStage::removeFromPainter(QPainter* p)
101{
102 Q_D(QOpenGLCustomShaderStage);
103 if (p->paintEngine()->type() != QPaintEngine::OpenGL2)
104 return;
105
106 QOpenGL2PaintEngineEx *engine = static_cast<QOpenGL2PaintEngineEx*>(p->paintEngine());
107 d->m_manager = QOpenGL2PaintEngineExPrivate::shaderManagerForEngine(engine);
108 Q_ASSERT(d->m_manager);
109
110 // Just set the stage to null, don't call removeCustomStage().
111 // This should leave the program in a compiled/linked state
112 // if the next custom shader stage is this one again.
113 d->m_manager->setCustomStage(nullptr);
114 d->m_manager = nullptr;
115}
116
117QByteArray QOpenGLCustomShaderStage::source() const
118{
119 Q_D(const QOpenGLCustomShaderStage);
120 return d->m_source;
121}
122
123// Called by the shader manager if another custom shader is attached or
124// the manager is deleted
125void QOpenGLCustomShaderStage::setInactive()
126{
127 Q_D(QOpenGLCustomShaderStage);
128 d->m_manager = nullptr;
129}
130
131void QOpenGLCustomShaderStage::setSource(const QByteArray& s)
132{
133 Q_D(QOpenGLCustomShaderStage);
134 d->m_source = s;
135}
136
137QT_END_NAMESPACE
138