1// Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QT3DRENDER_RENDER_OPENGL_GLSHADER_P_H
5#define QT3DRENDER_RENDER_OPENGL_GLSHADER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18
19#include <shadervariables_p.h>
20#include <shaderparameterpack_p.h>
21#include <Qt3DRender/qshaderprogram.h>
22#include <QMutex>
23
24#ifdef QT_BUILD_INTERNAL
25 class tst_BenchShaderParameterPack;
26#endif
27
28QT_BEGIN_NAMESPACE
29
30class QOpenGLShaderProgram;
31
32namespace Qt3DRender {
33
34namespace Render {
35
36namespace OpenGL {
37
38class Q_AUTOTEST_EXPORT GLShader
39{
40public:
41 GLShader();
42 ~GLShader();
43
44 void setGraphicsContext(GraphicsContext *context);
45 GraphicsContext *graphicsContext();
46
47 bool isLoaded() const { return m_isLoaded; }
48 void setLoaded(bool loaded) { m_isLoaded = loaded; }
49
50 void prepareUniforms(ShaderParameterPack &pack);
51
52 void setFragOutputs(const QHash<QString, int> &fragOutputs);
53 const QHash<QString, int> fragOutputs() const;
54
55 inline const std::vector<int> &uniformsNamesIds() const { return m_uniformsNamesIds; }
56 inline const std::vector<int> &lightUniformsNamesIds() const { return m_lightUniformsNamesIds; }
57 inline const std::vector<int> &standardUniformNameIds() const { return m_standardUniformNamesIds; }
58 inline const std::vector<int> &uniformBlockNamesIds() const { return m_uniformBlockNamesIds; }
59 inline const std::vector<int> &storageBlockNamesIds() const { return m_shaderStorageBlockNamesIds; }
60 inline const std::vector<int> &attributeNamesIds() const { return m_attributeNamesIds; }
61
62 const std::vector<QString> &uniformsNames() const;
63 const std::vector<QString> &attributesNames() const;
64 const std::vector<QString> &uniformBlockNames() const;
65 const std::vector<QString> &storageBlockNames() const;
66
67 inline const std::vector<ShaderUniform> &uniforms() const { return m_uniforms; }
68 inline const std::vector<ShaderAttribute> &attributes() const { return m_attributes; }
69 inline const std::vector<ShaderUniformBlock> &uniformBlocks() const { return m_uniformBlocks; }
70 inline const std::vector<ShaderStorageBlock> &storageBlocks() const { return m_shaderStorageBlocks; }
71
72 QHash<QString, ShaderUniform> activeUniformsForUniformBlock(int blockIndex) const;
73
74 ShaderUniformBlock uniformBlockForBlockIndex(int blockNameId) const noexcept;
75 ShaderUniformBlock uniformBlockForBlockNameId(int blockIndex) const noexcept;
76 ShaderUniformBlock uniformBlockForBlockName(const QString &blockName) const noexcept;
77
78 ShaderStorageBlock storageBlockForBlockIndex(int blockIndex) const noexcept;
79 ShaderStorageBlock storageBlockForBlockNameId(int blockNameId) const noexcept;
80 ShaderStorageBlock storageBlockForBlockName(const QString &blockName) const noexcept;
81
82 enum ParameterKind {
83 Uniform,
84 UBO,
85 SSBO,
86 Struct
87 };
88 ParameterKind categorizeVariable(int nameId) const noexcept;
89
90 bool hasUniform(int nameId) const noexcept;
91 inline bool hasActiveVariables() const noexcept { return m_hasActiveVariables; }
92 inline int parameterPackSize() const noexcept { return m_parameterPackSize; }
93
94 QOpenGLShaderProgram *shaderProgram() { return &m_shader; }
95
96 void setShaderCode(const std::vector<QByteArray> shaderCode) { m_shaderCode = shaderCode; }
97 const std::vector<QByteArray> &shaderCode() const;
98
99private:
100 bool m_isLoaded;
101 QOpenGLShaderProgram m_shader;
102 GraphicsContext *m_graphicsContext;
103
104 std::vector<QString> m_uniformsNames;
105 std::vector<int> m_uniformsNamesIds;
106 std::vector<int> m_lightUniformsNamesIds;
107 std::vector<int> m_standardUniformNamesIds;
108 std::vector<ShaderUniform> m_uniforms;
109
110 std::vector<QString> m_attributesNames;
111 std::vector<int> m_attributeNamesIds;
112 std::vector<ShaderAttribute> m_attributes;
113
114 std::vector<QString> m_uniformBlockNames;
115 std::vector<int> m_uniformBlockNamesIds;
116 std::vector<ShaderUniformBlock> m_uniformBlocks;
117 QHash<int, QHash<QString, ShaderUniform> > m_uniformBlockIndexToShaderUniforms;
118
119 std::vector<QString> m_shaderStorageBlockNames;
120 std::vector<int> m_shaderStorageBlockNamesIds;
121 std::vector<ShaderStorageBlock> m_shaderStorageBlocks;
122
123 QHash<QString, int> m_fragOutputs;
124 std::vector<QByteArray> m_shaderCode;
125
126 int m_parameterPackSize;
127 int m_hasActiveVariables;
128
129 // Private so that only GraphicContext can call it
130 void initializeUniforms(const std::vector<ShaderUniform> &uniformsDescription);
131 void initializeAttributes(const std::vector<ShaderAttribute> &attributesDescription);
132 void initializeUniformBlocks(const std::vector<ShaderUniformBlock> &uniformBlockDescription);
133 void initializeShaderStorageBlocks(const std::vector<ShaderStorageBlock> &shaderStorageBlockDescription);
134
135 friend class GraphicsContext;
136#ifdef QT_BUILD_INTERNAL
137 friend class ::tst_BenchShaderParameterPack;
138#endif
139
140 mutable QMutex m_mutex;
141 QMetaObject::Connection m_contextConnection;
142};
143
144} // OpenGL
145
146} // Render
147
148} // Qt3DRender
149
150QT_END_NAMESPACE
151
152#endif // QT3DRENDER_RENDER_OPENGL_GLSHADER_P_H
153

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