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#include "shaderbuilder_p.h"
5
6#include <Qt3DRender/private/qshaderprogrambuilder_p.h>
7#include <Qt3DRender/qshaderprogram.h>
8#include <Qt3DRender/private/qshaderprogram_p.h>
9#include <Qt3DCore/private/qurlhelper_p.h>
10
11#include <Qt3DRender/private/qshaderformat_p.h>
12#include <Qt3DRender/private/qshadergraphloader_p.h>
13#include <Qt3DRender/private/qshadergenerator_p.h>
14#include <Qt3DRender/private/qshadernodesloader_p.h>
15#include <Qt3DRender/private/renderlogging_p.h>
16
17#include <QFile>
18#include <QFileInfo>
19#include <QUrl>
20#include <QCryptographicHash>
21#include <QDateTime>
22#include <QStandardPaths>
23#include <QDir>
24
25static void initResources()
26{
27#ifdef QT_STATIC
28 Q_INIT_RESOURCE(materialsystem);
29#endif
30}
31
32QT_BEGIN_NAMESPACE
33
34class GlobalShaderPrototypes
35{
36public:
37 GlobalShaderPrototypes()
38 {
39 initResources();
40 setPrototypesFile(QStringLiteral(":/prototypes/default.json"));
41 }
42
43 QString prototypesFile() const
44 {
45 return m_fileName;
46 }
47
48 void setPrototypesFile(const QString &fileName)
49 {
50 m_fileName = fileName;
51 load();
52 }
53
54 QHash<QString, Qt3DRender::QShaderNode> prototypes() const
55 {
56 return m_prototypes;
57 }
58
59private:
60 void load()
61 {
62 QFile file(m_fileName);
63 if (!file.open(flags: QFile::ReadOnly)) {
64 qWarning() << "Couldn't open file:" << m_fileName;
65 return;
66 }
67
68 Qt3DRender::QShaderNodesLoader loader;
69 loader.setDevice(&file);
70 loader.load();
71 m_prototypes = loader.nodes();
72 }
73
74 QString m_fileName;
75 QHash<QString, Qt3DRender::QShaderNode> m_prototypes;
76};
77
78Q_GLOBAL_STATIC(GlobalShaderPrototypes, qt3dGlobalShaderPrototypes)
79
80using namespace Qt3DCore;
81
82namespace Qt3DRender {
83namespace Render {
84
85QString ShaderBuilder::getPrototypesFile()
86{
87 return qt3dGlobalShaderPrototypes->prototypesFile();
88}
89
90void ShaderBuilder::setPrototypesFile(const QString &file)
91{
92 qt3dGlobalShaderPrototypes->setPrototypesFile(file);
93}
94
95QStringList ShaderBuilder::getPrototypeNames()
96{
97 return qt3dGlobalShaderPrototypes->prototypes().keys();
98}
99
100ShaderBuilder::ShaderBuilder()
101 : BackendNode(ReadWrite)
102{
103}
104
105ShaderBuilder::~ShaderBuilder()
106{
107}
108
109void ShaderBuilder::cleanup()
110{
111 m_shaderProgramId = Qt3DCore::QNodeId();
112 m_enabledLayers.clear();
113 m_graphs.clear();
114 m_dirtyTypes.clear();
115 m_pendingUpdates.clear();
116 QBackendNode::setEnabled(false);
117}
118
119Qt3DCore::QNodeId ShaderBuilder::shaderProgramId() const
120{
121 return m_shaderProgramId;
122}
123
124QStringList ShaderBuilder::enabledLayers() const
125{
126 return m_enabledLayers;
127}
128
129
130void ShaderBuilder::setEnabledLayers(const QStringList &layers)
131{
132 if (m_enabledLayers == layers)
133 return;
134
135 m_enabledLayers = layers;
136
137 for (auto it = m_graphs.cbegin(); it != m_graphs.cend(); ++it) {
138 if (!it.value().isEmpty())
139 m_dirtyTypes.insert(value: it.key());
140 }
141}
142
143GraphicsApiFilterData ShaderBuilder::graphicsApi() const
144{
145 return m_graphicsApi;
146}
147
148void ShaderBuilder::setGraphicsApi(const GraphicsApiFilterData &graphicsApi)
149{
150 if (m_graphicsApi == graphicsApi)
151 return;
152
153 m_graphicsApi = graphicsApi;
154 for (auto it = m_graphs.cbegin(); it != m_graphs.cend(); ++it) {
155 if (!it.value().isEmpty())
156 m_dirtyTypes.insert(value: it.key());
157 }
158}
159
160QUrl ShaderBuilder::shaderGraph(QShaderProgram::ShaderType type) const
161{
162 return m_graphs.value(key: type);
163}
164
165void ShaderBuilder::setShaderGraph(QShaderProgram::ShaderType type, const QUrl &url)
166{
167 if (url != m_graphs.value(key: type)) {
168 m_graphs.insert(key: type, value: url);
169 m_dirtyTypes.insert(value: type);
170 }
171}
172
173QByteArray ShaderBuilder::shaderCode(QShaderProgram::ShaderType type) const
174{
175 return m_codes.value(key: type);
176}
177
178bool ShaderBuilder::isShaderCodeDirty(QShaderProgram::ShaderType type) const
179{
180 return m_dirtyTypes.contains(value: type);
181}
182
183void ShaderBuilder::generateCode(QShaderProgram::ShaderType type)
184{
185 const auto graphPath = Qt3DCore::QUrlHelper::urlToLocalFileOrQrc(url: shaderGraph(type));
186 QFile file(graphPath);
187 if (!file.open(flags: QFile::ReadOnly)) {
188 qWarning() << "Couldn't open file:" << graphPath;
189 return;
190 }
191
192 auto updateShaderCodeAndClearDirty = [&] (const QByteArray &shaderCode) {
193 m_codes.insert(key: type, value: shaderCode);
194 m_dirtyTypes.remove(value: type);
195 m_pendingUpdates.push_back(x: { .builderId: peerId(),
196 .shaderType: type,
197 .shaderCode: m_codes.value(key: type) });
198 };
199
200 const QByteArray cacheKey = hashKeyForShaderGraph(type);
201 const bool forceRegenerate = qEnvironmentVariableIsSet(varName: "QT3D_REBUILD_SHADER_CACHE");
202 const bool useCache = !qEnvironmentVariableIsSet(varName: "QT3D_DISABLE_SHADER_CACHE") && !forceRegenerate;
203 const QByteArray userProvidedPath = qgetenv(varName: "QT3D_WRITABLE_CACHE_PATH");
204 const QString cachedFilterPath = QDir(userProvidedPath.isEmpty() ?
205 QStandardPaths::writableLocation(type: QStandardPaths::TempLocation)
206 : QString::fromUtf8(ba: userProvidedPath)).absoluteFilePath(fileName: QString::fromUtf8(ba: cacheKey) + QLatin1String(".qt3d"));
207 QFile cachedShaderFile(cachedFilterPath);
208
209 // Check our runtime cache to see if we have already loaded the shader previously
210 if (useCache) {
211 // We check if we already have generated a shader previously for the
212 // given type, the given graph, the given API and the current set of layer
213 // If that's the case it's faster to load the pre generated shader file
214
215 if (m_renderer && m_renderer->containsGeneratedShaderGraph(key: cacheKey)) {
216 qCDebug(ShaderCache) << "Using runtime cache for shader graph with key" << cacheKey;
217 updateShaderCodeAndClearDirty(m_renderer->cachedGeneratedShaderGraph(key: cacheKey));
218 return;
219 }
220
221 // else check if a cachedShader file exists
222 if (cachedShaderFile.exists()) {
223 if (!cachedShaderFile.open(flags: QFile::ReadOnly)) {
224 qCWarning(ShaderCache) << "Couldn't open cached shader file:" << graphPath;
225 // Too bad, we have to generate the shader below
226 } else {
227 // Use cached shader
228 qCDebug(ShaderCache) << "Using cached shader file" << cachedFilterPath;
229 const QByteArray shaderCode = cachedShaderFile.readAll();
230 updateShaderCodeAndClearDirty(shaderCode);
231
232 // Record to runtime cache
233 if (m_renderer) {
234 qCDebug(ShaderCache) << "Insert shader " << cacheKey << "into runtime cache";
235 m_renderer->insertGeneratedShaderGraph(key: cacheKey, shaderCode);
236 }
237 return;
238 }
239 }
240 }
241
242 // Generate Shader and Cache the result for subsequent uses
243 auto graphLoader = QShaderGraphLoader();
244 graphLoader.setPrototypes(qt3dGlobalShaderPrototypes->prototypes());
245 graphLoader.setDevice(&file);
246 graphLoader.load();
247
248 if (graphLoader.status() == QShaderGraphLoader::Error)
249 return;
250
251 const auto graph = graphLoader.graph();
252
253 auto format = QShaderFormat();
254 format.setApi(m_graphicsApi.m_api == QGraphicsApiFilter::OpenGLES ? QShaderFormat::OpenGLES
255 : m_graphicsApi.m_api == QGraphicsApiFilter::Vulkan ? QShaderFormat::VulkanFlavoredGLSL
256 : m_graphicsApi.m_api == QGraphicsApiFilter::RHI ? QShaderFormat::RHI
257 : m_graphicsApi.m_profile == QGraphicsApiFilter::CoreProfile ? QShaderFormat::OpenGLCoreProfile
258 : m_graphicsApi.m_profile == QGraphicsApiFilter::CompatibilityProfile ? QShaderFormat::OpenGLCompatibilityProfile
259 : QShaderFormat::OpenGLNoProfile);
260 format.setVersion(QVersionNumber(m_graphicsApi.m_major, m_graphicsApi.m_minor));
261 format.setExtensions(m_graphicsApi.m_extensions);
262 format.setVendor(m_graphicsApi.m_vendor);
263
264 auto generator = QShaderGenerator();
265 generator.format = format;
266 generator.graph = graph;
267
268 const auto code = generator.createShaderCode(enabledLayers: m_enabledLayers);
269 const auto deincludified = QShaderProgramPrivate::deincludify(contents: code, filePath: graphPath + QStringLiteral(".glsl"));
270
271 updateShaderCodeAndClearDirty(deincludified);
272
273 // Record to runtime cache
274 if (useCache || forceRegenerate) {
275 if (m_renderer) {
276 qCDebug(ShaderCache) << "Insert shader " << cacheKey << "into runtime cache";
277 m_renderer->insertGeneratedShaderGraph(key: cacheKey, shaderCode: deincludified);
278 }
279
280 // Record to file cache
281 if (cachedShaderFile.open(flags: QFile::WriteOnly)) {
282 cachedShaderFile.write(data: deincludified);
283 qCDebug(ShaderCache) << "Saving cached shader file" << cachedFilterPath;
284 } else {
285 qCWarning(ShaderCache) << "Unable to write cached shader file";
286 }
287 }
288}
289
290void ShaderBuilder::syncFromFrontEnd(const QNode *frontEnd, bool firstTime)
291{
292 const QShaderProgramBuilder *node = qobject_cast<const QShaderProgramBuilder *>(object: frontEnd);
293 if (!node)
294 return;
295
296 const bool oldEnabled = isEnabled();
297 BackendNode::syncFromFrontEnd(frontEnd, firstTime);
298
299 if (oldEnabled != isEnabled()) {
300 markDirty(changes: AbstractRenderer::ShadersDirty);
301 }
302
303 const Qt3DCore::QNodeId shaderProgramId = Qt3DCore::qIdForNode(node: node->shaderProgram());
304 if (shaderProgramId != m_shaderProgramId) {
305 m_shaderProgramId = shaderProgramId;
306 markDirty(changes: AbstractRenderer::ShadersDirty);
307 }
308
309 if (node->enabledLayers() != m_enabledLayers) {
310 setEnabledLayers(node->enabledLayers());
311 markDirty(changes: AbstractRenderer::ShadersDirty);
312 }
313
314 static const QVarLengthArray<std::pair<QShaderProgram::ShaderType, QUrl (QShaderProgramBuilder::*)() const>, 6> shaderTypesToGetters {
315 {QShaderProgram::Vertex, &QShaderProgramBuilder::vertexShaderGraph},
316 {QShaderProgram::TessellationControl, &QShaderProgramBuilder::tessellationControlShaderGraph},
317 {QShaderProgram::TessellationEvaluation, &QShaderProgramBuilder::tessellationEvaluationShaderGraph},
318 {QShaderProgram::Geometry, &QShaderProgramBuilder::geometryShaderGraph},
319 {QShaderProgram::Fragment, &QShaderProgramBuilder::fragmentShaderGraph},
320 {QShaderProgram::Compute, &QShaderProgramBuilder::computeShaderGraph},
321 };
322
323 for (auto it = shaderTypesToGetters.cbegin(), end = shaderTypesToGetters.cend(); it != end; ++it) {
324 const QUrl url = (node->*(it->second))();
325 if (url != m_graphs.value(key: it->first)) {
326 setShaderGraph(type: it->first, url);
327 markDirty(changes: AbstractRenderer::ShadersDirty);
328 }
329 }
330}
331
332QByteArray ShaderBuilder::hashKeyForShaderGraph(QShaderProgram::ShaderType type) const
333{
334 const auto graphPath = Qt3DCore::QUrlHelper::urlToLocalFileOrQrc(url: shaderGraph(type));
335 QFile file(graphPath);
336 if (!file.exists()) {
337 qWarning() << graphPath << "doesn't exist";
338 return {};
339 }
340
341 QCryptographicHash hashBuilder(QCryptographicHash::Sha1);
342 // Add graphPath
343 hashBuilder.addData(data: graphPath.toUtf8());
344 // Get TimeStamp and Graph file size
345 QFileInfo info(graphPath);
346 const QString fileInfo = QString::fromUtf8(utf8: "%1_%2")
347 .arg(a: info.lastModified().toSecsSinceEpoch())
348 .arg(a: info.size());
349 hashBuilder.addData(data: fileInfo.toUtf8());
350
351 // Add Layers
352 for (const QString &layer : m_enabledLayers)
353 hashBuilder.addData(data: layer.toUtf8());
354
355 // Add GraphicsInfo
356 const QString graphicsInfo = QString::fromUtf8(utf8: "API: %1 Profile: %2 Major: %3 Minor: %4")
357 .arg(a: int(m_graphicsApi.m_api))
358 .arg(a: int(m_graphicsApi.m_profile))
359 .arg(a: int(m_graphicsApi.m_major))
360 .arg(a: int(m_graphicsApi.m_minor));
361 hashBuilder.addData(data: graphicsInfo.toUtf8());
362
363 // Add Shader Type
364 hashBuilder.addData(data: QString::number(type).toUtf8());
365
366 return hashBuilder.result().toHex();
367}
368
369} // namespace Render
370} // namespace Qt3DRender
371
372QT_END_NAMESPACE
373

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