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#include "shaderbuilder_p.h"
41
42#include <Qt3DCore/qpropertyupdatedchange.h>
43
44#include <Qt3DRender/private/qshaderprogrambuilder_p.h>
45#include <Qt3DRender/qshaderprogram.h>
46#include <Qt3DRender/private/qshaderprogram_p.h>
47#include <Qt3DRender/private/qurlhelper_p.h>
48
49#include <Qt3DRender/private/qshaderformat_p.h>
50#include <Qt3DRender/private/qshadergraphloader_p.h>
51#include <Qt3DRender/private/qshadergenerator_p.h>
52#include <Qt3DRender/private/qshadernodesloader_p.h>
53
54#include <QFile>
55#include <QFileInfo>
56#include <QUrl>
57
58static void initResources()
59{
60#ifdef QT_STATIC
61 Q_INIT_RESOURCE(materialsystem);
62#endif
63}
64
65QT_BEGIN_NAMESPACE
66
67class GlobalShaderPrototypes
68{
69public:
70 GlobalShaderPrototypes()
71 {
72 initResources();
73 setPrototypesFile(QStringLiteral(":/prototypes/default.json"));
74 }
75
76 QString prototypesFile() const
77 {
78 return m_fileName;
79 }
80
81 void setPrototypesFile(const QString &fileName)
82 {
83 m_fileName = fileName;
84 load();
85 }
86
87 QHash<QString, Qt3DRender::QShaderNode> prototypes() const
88 {
89 return m_prototypes;
90 }
91
92private:
93 void load()
94 {
95 QFile file(m_fileName);
96 if (!file.open(QFile::ReadOnly)) {
97 qWarning() << "Couldn't open file:" << m_fileName;
98 return;
99 }
100
101 Qt3DRender::QShaderNodesLoader loader;
102 loader.setDevice(&file);
103 loader.load();
104 m_prototypes = loader.nodes();
105 }
106
107 QString m_fileName;
108 QHash<QString, Qt3DRender::QShaderNode> m_prototypes;
109};
110
111Q_GLOBAL_STATIC(GlobalShaderPrototypes, qt3dGlobalShaderPrototypes)
112
113using namespace Qt3DCore;
114
115namespace Qt3DRender {
116namespace Render {
117
118QString ShaderBuilder::getPrototypesFile()
119{
120 return qt3dGlobalShaderPrototypes->prototypesFile();
121}
122
123void ShaderBuilder::setPrototypesFile(const QString &file)
124{
125 qt3dGlobalShaderPrototypes->setPrototypesFile(file);
126}
127
128QStringList ShaderBuilder::getPrototypeNames()
129{
130 return qt3dGlobalShaderPrototypes->prototypes().keys();
131}
132
133ShaderBuilder::ShaderBuilder()
134 : BackendNode(ReadWrite)
135{
136}
137
138ShaderBuilder::~ShaderBuilder()
139{
140}
141
142void ShaderBuilder::cleanup()
143{
144 m_shaderProgramId = Qt3DCore::QNodeId();
145 m_enabledLayers.clear();
146 m_graphs.clear();
147 m_dirtyTypes.clear();
148 m_pendingUpdates.clear();
149 QBackendNode::setEnabled(false);
150}
151
152Qt3DCore::QNodeId ShaderBuilder::shaderProgramId() const
153{
154 return m_shaderProgramId;
155}
156
157QStringList ShaderBuilder::enabledLayers() const
158{
159 return m_enabledLayers;
160}
161
162
163void ShaderBuilder::setEnabledLayers(const QStringList &layers)
164{
165 if (m_enabledLayers == layers)
166 return;
167
168 m_enabledLayers = layers;
169
170 for (auto it = m_graphs.cbegin(); it != m_graphs.cend(); ++it) {
171 if (!it.value().isEmpty())
172 m_dirtyTypes.insert(it.key());
173 }
174}
175
176GraphicsApiFilterData ShaderBuilder::graphicsApi() const
177{
178 return m_graphicsApi;
179}
180
181void ShaderBuilder::setGraphicsApi(const GraphicsApiFilterData &graphicsApi)
182{
183 if (m_graphicsApi == graphicsApi)
184 return;
185
186 m_graphicsApi = graphicsApi;
187 for (auto it = m_graphs.cbegin(); it != m_graphs.cend(); ++it) {
188 if (!it.value().isEmpty())
189 m_dirtyTypes.insert(it.key());
190 }
191}
192
193QUrl ShaderBuilder::shaderGraph(QShaderProgram::ShaderType type) const
194{
195 return m_graphs.value(type);
196}
197
198void ShaderBuilder::setShaderGraph(QShaderProgram::ShaderType type, const QUrl &url)
199{
200 if (url != m_graphs.value(type)) {
201 m_graphs.insert(type, url);
202 m_dirtyTypes.insert(type);
203 }
204}
205
206QByteArray ShaderBuilder::shaderCode(QShaderProgram::ShaderType type) const
207{
208 return m_codes.value(type);
209}
210
211bool ShaderBuilder::isShaderCodeDirty(QShaderProgram::ShaderType type) const
212{
213 return m_dirtyTypes.contains(type);
214}
215
216void ShaderBuilder::generateCode(QShaderProgram::ShaderType type)
217{
218 const auto graphPath = QUrlHelper::urlToLocalFileOrQrc(shaderGraph(type));
219 QFile file(graphPath);
220 if (!file.open(QFile::ReadOnly)) {
221 qWarning() << "Couldn't open file:" << graphPath;
222 return;
223 }
224
225 auto graphLoader = QShaderGraphLoader();
226 graphLoader.setPrototypes(qt3dGlobalShaderPrototypes->prototypes());
227 graphLoader.setDevice(&file);
228 graphLoader.load();
229
230 if (graphLoader.status() == QShaderGraphLoader::Error)
231 return;
232
233 const auto graph = graphLoader.graph();
234
235 auto format = QShaderFormat();
236 format.setApi(m_graphicsApi.m_api == QGraphicsApiFilter::OpenGLES ? QShaderFormat::OpenGLES
237 : m_graphicsApi.m_api == QGraphicsApiFilter::Vulkan ? QShaderFormat::VulkanFlavoredGLSL
238 : m_graphicsApi.m_api == QGraphicsApiFilter::RHI ? QShaderFormat::RHI
239 : m_graphicsApi.m_profile == QGraphicsApiFilter::CoreProfile ? QShaderFormat::OpenGLCoreProfile
240 : m_graphicsApi.m_profile == QGraphicsApiFilter::CompatibilityProfile ? QShaderFormat::OpenGLCompatibilityProfile
241 : QShaderFormat::OpenGLNoProfile);
242 format.setVersion(QVersionNumber(m_graphicsApi.m_major, m_graphicsApi.m_minor));
243 format.setExtensions(m_graphicsApi.m_extensions);
244 format.setVendor(m_graphicsApi.m_vendor);
245
246 auto generator = QShaderGenerator();
247 generator.format = format;
248 generator.graph = graph;
249
250 const auto code = generator.createShaderCode(m_enabledLayers);
251 const auto deincludified = QShaderProgramPrivate::deincludify(code, graphPath + QStringLiteral(".glsl"));
252 m_codes.insert(type, deincludified);
253 m_dirtyTypes.remove(type);
254
255 m_pendingUpdates.push_back({ peerId(),
256 type,
257 m_codes.value(type) });
258}
259
260void ShaderBuilder::syncFromFrontEnd(const QNode *frontEnd, bool firstTime)
261{
262 const QShaderProgramBuilder *node = qobject_cast<const QShaderProgramBuilder *>(frontEnd);
263 if (!node)
264 return;
265
266 const bool oldEnabled = isEnabled();
267 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
268
269 if (oldEnabled != isEnabled()) {
270 markDirty(AbstractRenderer::ShadersDirty);
271 }
272
273 const Qt3DCore::QNodeId shaderProgramId = Qt3DCore::qIdForNode(node->shaderProgram());
274 if (shaderProgramId != m_shaderProgramId) {
275 m_shaderProgramId = shaderProgramId;
276 markDirty(AbstractRenderer::ShadersDirty);
277 }
278
279 if (node->enabledLayers() != m_enabledLayers) {
280 setEnabledLayers(node->enabledLayers());
281 markDirty(AbstractRenderer::ShadersDirty);
282 }
283
284 static const QVarLengthArray<std::pair<QShaderProgram::ShaderType, QUrl (QShaderProgramBuilder::*)() const>, 6> shaderTypesToGetters {
285 {QShaderProgram::Vertex, &QShaderProgramBuilder::vertexShaderGraph},
286 {QShaderProgram::TessellationControl, &QShaderProgramBuilder::tessellationControlShaderGraph},
287 {QShaderProgram::TessellationEvaluation, &QShaderProgramBuilder::tessellationEvaluationShaderGraph},
288 {QShaderProgram::Geometry, &QShaderProgramBuilder::geometryShaderGraph},
289 {QShaderProgram::Fragment, &QShaderProgramBuilder::fragmentShaderGraph},
290 {QShaderProgram::Compute, &QShaderProgramBuilder::computeShaderGraph},
291 };
292
293 for (auto it = shaderTypesToGetters.cbegin(), end = shaderTypesToGetters.cend(); it != end; ++it) {
294 const QUrl url = (node->*(it->second))();
295 if (url != m_graphs.value(it->first)) {
296 setShaderGraph(it->first, url);
297 markDirty(AbstractRenderer::ShadersDirty);
298 }
299 }
300}
301
302} // namespace Render
303} // namespace Qt3DRender
304
305QT_END_NAMESPACE
306

source code of qt3d/src/render/materialsystem/shaderbuilder.cpp