1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "shaderfeatures.h"
5#include <QStringList>
6#include <QDebug>
7
8ShaderFeatures::ShaderFeatures()
9{
10}
11
12// Browse the shaders and check which features are used in them.
13void ShaderFeatures::update(const QString &vs, const QString &fs, const QString &qml)
14{
15 QStringList vsList = vs.split(sep: "\n");
16 QStringList fsList = fs.split(sep: "\n");
17
18 QStringList code = vsList + fsList;
19 Features newFeatures = {};
20 m_gridMeshWidth = 1;
21 m_gridMeshHeight = 1;
22 for (const auto &line : code)
23 checkLine(line, features&: newFeatures);
24
25 // iTime may also be used in QML side, without being used in shaders.
26 // In this case enable the time helpers creation.
27 if (qml.contains(s: "iTime"))
28 newFeatures.setFlag(flag: Time, on: true);
29
30 if (newFeatures != m_enabledFeatures)
31 m_enabledFeatures = newFeatures;
32}
33
34void ShaderFeatures::checkLine(const QString &line, Features &features) {
35 if (line.contains(s: "iTime"))
36 features.setFlag(flag: Time, on: true);
37
38 if (line.contains(s: "iFrame"))
39 features.setFlag(flag: Frame, on: true);
40
41 if (line.contains(s: "iResolution"))
42 features.setFlag(flag: Resolution, on: true);
43
44 if (line.contains(s: "iSource"))
45 features.setFlag(flag: Source, on: true);
46
47 if (line.contains(s: "iMouse"))
48 features.setFlag(flag: Mouse, on: true);
49
50 if (line.contains(s: "fragCoord"))
51 features.setFlag(flag: FragCoord, on: true);
52
53 if (line.contains(s: "@mesh")) {
54 // Get the mesh size, remove "@mesh"
55 QString l = line.trimmed().sliced(pos: 5);
56 QStringList list = l.split(sep: QLatin1Char(','));
57 if (list.size() >= 2) {
58 int w = list.at(i: 0).trimmed().toInt();
59 int h = list.at(i: 1).trimmed().toInt();
60 // Set size to max values
61 m_gridMeshWidth = std::max(a: m_gridMeshWidth, b: w);
62 m_gridMeshHeight = std::max(a: m_gridMeshHeight, b: h);
63 }
64 // If is bigger than default (1, 1), set the feature
65 if (m_gridMeshWidth > 1 || m_gridMeshHeight > 1)
66 features.setFlag(flag: GridMesh, on: true);
67 }
68 if (line.contains(s: "@blursources"))
69 features.setFlag(flag: BlurSources, on: true);
70}
71

source code of qtquickeffectmaker/tools/qqem/shaderfeatures.cpp