1/****************************************************************************
2**
3** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D 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#ifndef QT3DRENDER_RENDER_OPENGL_GLSHADER_P_H
41#define QT3DRENDER_RENDER_OPENGL_GLSHADER_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists for the convenience
48// of other Qt classes. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54
55#include <shadervariables_p.h>
56#include <shaderparameterpack_p.h>
57#include <Qt3DRender/qshaderprogram.h>
58#include <QMutex>
59
60#ifdef QT_BUILD_INTERNAL
61 class tst_BenchShaderParameterPack;
62#endif
63
64QT_BEGIN_NAMESPACE
65
66class QOpenGLShaderProgram;
67
68namespace Qt3DRender {
69
70namespace Render {
71
72namespace OpenGL {
73
74class Q_AUTOTEST_EXPORT GLShader
75{
76public:
77 GLShader();
78 ~GLShader();
79
80 void setGraphicsContext(GraphicsContext *context);
81 GraphicsContext *graphicsContext();
82
83 bool isLoaded() const { return m_isLoaded; }
84 void setLoaded(bool loaded) { m_isLoaded = loaded; }
85
86 void prepareUniforms(ShaderParameterPack &pack);
87
88 void setFragOutputs(const QHash<QString, int> &fragOutputs);
89 const QHash<QString, int> fragOutputs() const;
90
91 inline const QVector<int> &uniformsNamesIds() const { return m_uniformsNamesIds; }
92 inline const QVector<int> &lightUniformsNamesIds() const { return m_lightUniformsNamesIds; }
93 inline const QVector<int> &standardUniformNameIds() const { return m_standardUniformNamesIds; }
94 inline const QVector<int> &uniformBlockNamesIds() const { return m_uniformBlockNamesIds; }
95 inline const QVector<int> &storageBlockNamesIds() const { return m_shaderStorageBlockNamesIds; }
96 inline const QVector<int> &attributeNamesIds() const { return m_attributeNamesIds; }
97
98 QVector<QString> uniformsNames() const;
99 QVector<QString> attributesNames() const;
100 QVector<QString> uniformBlockNames() const;
101 QVector<QString> storageBlockNames() const;
102
103 inline const QVector<ShaderUniform> &uniforms() const { return m_uniforms; }
104 inline const QVector<ShaderAttribute> &attributes() const { return m_attributes; }
105 inline const QVector<ShaderUniformBlock> &uniformBlocks() const { return m_uniformBlocks; }
106 inline const QVector<ShaderStorageBlock> &storageBlocks() const { return m_shaderStorageBlocks; }
107
108 QHash<QString, ShaderUniform> activeUniformsForUniformBlock(int blockIndex) const;
109
110 ShaderUniformBlock uniformBlockForBlockIndex(int blockNameId) const noexcept;
111 ShaderUniformBlock uniformBlockForBlockNameId(int blockIndex) const noexcept;
112 ShaderUniformBlock uniformBlockForBlockName(const QString &blockName) const noexcept;
113
114 ShaderStorageBlock storageBlockForBlockIndex(int blockIndex) const noexcept;
115 ShaderStorageBlock storageBlockForBlockNameId(int blockNameId) const noexcept;
116 ShaderStorageBlock storageBlockForBlockName(const QString &blockName) const noexcept;
117
118 enum ParameterKind {
119 Uniform,
120 UBO,
121 SSBO,
122 Struct
123 };
124 ParameterKind categorizeVariable(int nameId) const noexcept;
125
126 bool hasUniform(int nameId) const noexcept;
127 inline bool hasActiveVariables() const noexcept { return m_hasActiveVariables; }
128 inline int parameterPackSize() const noexcept { return m_parameterPackSize; }
129
130 QOpenGLShaderProgram *shaderProgram() { return &m_shader; }
131
132 void setShaderCode(const QVector<QByteArray> shaderCode) { m_shaderCode = shaderCode; }
133 QVector<QByteArray> shaderCode() const;
134
135private:
136 bool m_isLoaded;
137 QOpenGLShaderProgram m_shader;
138 GraphicsContext *m_graphicsContext;
139
140 QVector<QString> m_uniformsNames;
141 QVector<int> m_uniformsNamesIds;
142 QVector<int> m_lightUniformsNamesIds;
143 QVector<int> m_standardUniformNamesIds;
144 QVector<ShaderUniform> m_uniforms;
145
146 QVector<QString> m_attributesNames;
147 QVector<int> m_attributeNamesIds;
148 QVector<ShaderAttribute> m_attributes;
149
150 QVector<QString> m_uniformBlockNames;
151 QVector<int> m_uniformBlockNamesIds;
152 QVector<ShaderUniformBlock> m_uniformBlocks;
153 QHash<int, QHash<QString, ShaderUniform> > m_uniformBlockIndexToShaderUniforms;
154
155 QVector<QString> m_shaderStorageBlockNames;
156 QVector<int> m_shaderStorageBlockNamesIds;
157 QVector<ShaderStorageBlock> m_shaderStorageBlocks;
158
159 QHash<QString, int> m_fragOutputs;
160 QVector<QByteArray> m_shaderCode;
161
162 int m_parameterPackSize;
163 int m_hasActiveVariables;
164
165 // Private so that only GraphicContext can call it
166 void initializeUniforms(const QVector<ShaderUniform> &uniformsDescription);
167 void initializeAttributes(const QVector<ShaderAttribute> &attributesDescription);
168 void initializeUniformBlocks(const QVector<ShaderUniformBlock> &uniformBlockDescription);
169 void initializeShaderStorageBlocks(const QVector<ShaderStorageBlock> &shaderStorageBlockDescription);
170
171 friend class GraphicsContext;
172#ifdef QT_BUILD_INTERNAL
173 friend class ::tst_BenchShaderParameterPack;
174#endif
175
176 mutable QMutex m_mutex;
177 QMetaObject::Connection m_contextConnection;
178};
179
180} // OpenGL
181
182} // Render
183
184} // Qt3DRender
185
186QT_END_NAMESPACE
187
188#endif // QT3DRENDER_RENDER_OPENGL_GLSHADER_P_H
189

source code of qt3d/src/plugins/renderers/opengl/renderer/glshader_p.h