1/****************************************************************************
2**
3** Copyright (C) 2014 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_SHADER_H
41#define QT3DRENDER_RENDER_SHADER_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#include <Qt3DRender/private/backendnode_p.h>
55#include <Qt3DRender/private/shaderparameterpack_p.h>
56#include <Qt3DRender/private/shadervariables_p.h>
57#include <Qt3DRender/qshaderprogram.h>
58#include <Qt3DCore/qpropertyupdatedchange.h>
59#include <QMutex>
60#include <QVector>
61
62QT_BEGIN_NAMESPACE
63
64class QOpenGLShaderProgram;
65
66namespace Qt3DRender {
67
68namespace Render {
69
70class ShaderManager;
71class AttachmentPack;
72
73typedef uint ProgramDNA;
74
75class Q_AUTOTEST_EXPORT Shader : public BackendNode
76{
77public:
78 Shader();
79 ~Shader();
80
81 void cleanup();
82
83 void setGraphicsContext(GraphicsContext *context);
84 GraphicsContext *graphicsContext();
85
86 void prepareUniforms(ShaderParameterPack &pack);
87 void setFragOutputs(const QHash<QString, int> &fragOutputs);
88 const QHash<QString, int> fragOutputs() const;
89
90 inline QVector<int> uniformsNamesIds() const { return m_uniformsNamesIds; }
91 inline QVector<int> uniformBlockNamesIds() const { return m_uniformBlockNamesIds; }
92 inline QVector<int> storageBlockNamesIds() const { return m_shaderStorageBlockNamesIds; }
93 inline QVector<int> attributeNamesIds() const { return m_attributeNamesIds; }
94
95 QVector<QString> uniformsNames() const;
96 QVector<QString> attributesNames() const;
97 QVector<QString> uniformBlockNames() const;
98 QVector<QString> storageBlockNames() const;
99 QVector<QByteArray> shaderCode() const;
100 void setShaderCode(QShaderProgram::ShaderType type, const QByteArray &code);
101
102 void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) override;
103 bool isLoaded() const { QMutexLocker lock(&m_mutex); return m_isLoaded; }
104 void setLoaded(bool loaded) { QMutexLocker lock(&m_mutex); m_isLoaded = loaded; }
105 ProgramDNA dna() const Q_DECL_NOTHROW { return m_dna; }
106
107 inline QVector<ShaderUniform> uniforms() const { return m_uniforms; }
108 inline QVector<ShaderAttribute> attributes() const { return m_attributes; }
109 inline QVector<ShaderUniformBlock> uniformBlocks() const { return m_uniformBlocks; }
110 inline QVector<ShaderStorageBlock> storageBlocks() const { return m_shaderStorageBlocks; }
111
112 QHash<QString, ShaderUniform> activeUniformsForUniformBlock(int blockIndex) const;
113
114 ShaderUniformBlock uniformBlockForBlockIndex(int blockNameId);
115 ShaderUniformBlock uniformBlockForBlockNameId(int blockIndex);
116 ShaderUniformBlock uniformBlockForBlockName(const QString &blockName);
117
118 ShaderStorageBlock storageBlockForBlockIndex(int blockIndex);
119 ShaderStorageBlock storageBlockForBlockNameId(int blockNameId);
120 ShaderStorageBlock storageBlockForBlockName(const QString &blockName);
121
122 inline QString log() const { return m_log; }
123 inline QShaderProgram::Status status() const { return m_status; }
124
125 void submitPendingNotifications();
126 inline bool hasPendingNotifications() const { return !m_pendingNotifications.empty(); }
127
128private:
129 void initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) final;
130
131 QVector<QString> m_uniformsNames;
132 QVector<int> m_uniformsNamesIds;
133 QVector<ShaderUniform> m_uniforms;
134
135 QVector<QString> m_attributesNames;
136 QVector<int> m_attributeNamesIds;
137 QVector<ShaderAttribute> m_attributes;
138
139 QVector<QString> m_uniformBlockNames;
140 QVector<int> m_uniformBlockNamesIds;
141 QVector<ShaderUniformBlock> m_uniformBlocks;
142 QHash<int, QHash<QString, ShaderUniform> > m_uniformBlockIndexToShaderUniforms;
143
144 QVector<QString> m_shaderStorageBlockNames;
145 QVector<int> m_shaderStorageBlockNamesIds;
146 QVector<ShaderStorageBlock> m_shaderStorageBlocks;
147
148 QHash<QString, int> m_fragOutputs;
149
150 QVector<QByteArray> m_shaderCode;
151
152 bool m_isLoaded;
153 ProgramDNA m_dna;
154 ProgramDNA m_oldDna;
155 mutable QMutex m_mutex;
156 GraphicsContext *m_graphicsContext;
157 QMetaObject::Connection m_contextConnection;
158 QString m_log;
159 QShaderProgram::Status m_status;
160
161 QVector<Qt3DCore::QPropertyUpdatedChangePtr> m_pendingNotifications;
162
163 void updateDNA();
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 void initializeFromReference(const Shader &other);
172 void setLog(const QString &log);
173 void setStatus(QShaderProgram::Status status);
174
175 friend class GraphicsContext;
176};
177
178#ifndef QT_NO_DEBUG_STREAM
179inline QDebug operator<<(QDebug dbg, const Shader &shader)
180{
181 QDebugStateSaver saver(dbg);
182 dbg << "QNodeId =" << shader.peerId() << "dna =" << shader.dna() << endl;
183 return dbg;
184}
185#endif
186
187} // namespace Render
188} // namespace Qt3DRender
189
190QT_END_NAMESPACE
191
192#endif // QT3DRENDER_RENDER_SHADER_H
193