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

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