aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/effectcomposer/shaderfeatures.cpp
blob: fc1cf95a34be2e1e4548e8874a46dddcb4a9f17e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "shaderfeatures.h"
#include <QStringList>
#include <QDebug>

namespace EffectComposer {

ShaderFeatures::ShaderFeatures()
{
}

// Browse the shaders and check which features are used in them.
void ShaderFeatures::update(const QString &vs, const QString &fs, const QString &qml)
{
    QStringList vsList = vs.split("\n");
    QStringList fsList = fs.split("\n");

    const QStringList code = vsList + fsList;
    Features newFeatures = {};
    m_gridMeshWidth = 1;
    m_gridMeshHeight = 1;
    for (const QString &line : code)
        checkLine(line, newFeatures);

    // iTime may also be used in QML side, without being used in shaders.
    // In this case enable the time helpers creation.
    if (qml.contains("iTime"))
        newFeatures.setFlag(Time, true);

    if (newFeatures != m_enabledFeatures)
        m_enabledFeatures = newFeatures;
}

bool ShaderFeatures::enabled(ShaderFeatures::Feature feature) const
{
    return m_enabledFeatures.testFlag(feature);
}

void ShaderFeatures::checkLine(const QString &line, Features &features)
{
    if (line.contains("iTime"))
        features.setFlag(Time, true);

    if (line.contains("iFrame"))
        features.setFlag(Frame, true);

    if (line.contains("iResolution"))
        features.setFlag(Resolution, true);

    if (line.contains("iSource"))
        features.setFlag(Source, true);

    if (line.contains("iMouse"))
        features.setFlag(Mouse, true);

    if (line.contains("fragCoord"))
        features.setFlag(FragCoord, true);

    if (line.contains("@mesh")) {
        // Get the mesh size, remove "@mesh"
        QString l = line.trimmed().sliced(5);
        QStringList list = l.split(QLatin1Char(','));
        if (list.size() >= 2) {
            int w = list.at(0).trimmed().toInt();
            int h = list.at(1).trimmed().toInt();
            // Set size to max values
            m_gridMeshWidth = std::max(m_gridMeshWidth, w);
            m_gridMeshHeight = std::max(m_gridMeshHeight, h);
        }
        // If is bigger than default (1, 1), set the feature
        if (m_gridMeshWidth > 1 || m_gridMeshHeight > 1)
            features.setFlag(GridMesh, true);
    }

    if (line.contains("@blursources"))
        features.setFlag(BlurSources, true);

    if (line.contains("textureLod("))
        features.setFlag(Mipmap, true);
}

int ShaderFeatures::gridMeshHeight() const
{
    return m_gridMeshHeight;
}

int ShaderFeatures::gridMeshWidth() const
{
    return m_gridMeshWidth;
}

} // namespace EffectComposer