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