File: materialshadermodel.cpp

package info (click to toggle)
gammaray 3.1.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 24,796 kB
  • sloc: cpp: 109,174; ansic: 2,156; sh: 336; python: 274; yacc: 90; lex: 82; xml: 61; makefile: 28; javascript: 9; ruby: 5
file content (200 lines) | stat: -rw-r--r-- 6,095 bytes parent folder | download
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
  materialshadermodel.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "materialshadermodel.h"

#include <core/metaenum.h>

#include <QFile>
#include <QSGMaterial>
#include <private/qsgmaterialshader_p.h>

using namespace GammaRay;

#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
#define S(x)                 \
    {                        \
        QOpenGLShader::x, #x \
    }
static const MetaEnum::Value<QOpenGLShader::ShaderTypeBit> qopengl_shader_type[] = {
    S(Vertex),
    S(Fragment),
    S(Geometry),
    S(TessellationControl),
    S(TessellationEvaluation),
    S(Compute)
};
#undef S

class SGMaterialShaderThief : public QSGMaterialShader
{
public:
    using QSGMaterialShader::fragmentShader;
    using QSGMaterialShader::vertexShader;

    const QHash<QOpenGLShader::ShaderType, QStringList> &getShaderSources()
    {
        return d_func()->m_sourceFiles;
    }
};
#endif // QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)

MaterialShaderModel::MaterialShaderModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_shader(nullptr)
    , m_shaderFileCount(0)
{
}

MaterialShaderModel::~MaterialShaderModel() = default;

void MaterialShaderModel::setMaterialShader(QSGMaterialShader *shader)
{
    if (m_shader) {
        beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
        m_shader = nullptr;
        endRemoveRows();
    }

    if (shader) {
        m_shaderFileCount = shaderFileCount(shader);
        beginInsertRows(QModelIndex(), 0, m_shaderFileCount == 0 ? 1 : m_shaderFileCount - 1);
        m_shader = shader;
        endInsertRows();
    }
}

QByteArray MaterialShaderModel::shaderForRow(int row) const
{
    if (row < 0 || row >= rowCount() || !m_shader)
        return QByteArray();

    if (m_shaderFileCount == 0) {
        switch (row) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        case 0:
            return reinterpret_cast<SGMaterialShaderThief *>(m_shader)->vertexShader();
        case 1:
            return reinterpret_cast<SGMaterialShaderThief *>(m_shader)->fragmentShader();
#endif
        }
        return QByteArray();
    }

    const auto fileName = shaderFileForRow(row);
    QFile shaderFile(fileName);
    if (!shaderFile.open(QIODevice::ReadOnly | QIODevice::Text))
        return QByteArray();
    return shaderFile.readAll();
}

int MaterialShaderModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid() || !m_shader)
        return 0;
    return m_shaderFileCount == 0 ? 2 : m_shaderFileCount;
}

QVariant MaterialShaderModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || !m_shader || role != Qt::DisplayRole)
        return QVariant();

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    if (QSGMaterialShaderPrivate *p = QSGMaterialShaderPrivate::get(m_shader)) {
        int i = 0;
        const auto &shaderFiles = p->shaderFileNames;
        for (auto it = shaderFiles.cbegin(); it != shaderFiles.cend(); ++it) {
            auto type = it.key();
            if (i == index.row()) {
                switch (type) {
                case QShader::VertexStage:
                    return QString::fromLatin1("Vertex");
                case QShader::TessellationControlStage:
                    return QString::fromLatin1("TessellationControlStage");
                case QShader::TessellationEvaluationStage:
                    return QString::fromLatin1("TessellationEvaluationStage");
                case QShader::GeometryStage:
                    return QString::fromLatin1("GeometryStage");
                case QShader::FragmentStage:
                    return QString::fromLatin1("FragmentStage");
                case QShader::ComputeStage:
                    return QString::fromLatin1("ComputeStage");
                }
            }
            i++;
        }
    }
#else
    if (m_shaderFileCount > 0) {
        const auto &files = reinterpret_cast<SGMaterialShaderThief *>(m_shader)->getShaderSources();
        int idx = index.row();
        for (auto it = files.begin(); it != files.end(); ++it) {
            if (idx < it.value().size())
                return QString(MetaEnum::flagsToString(it.key(), qopengl_shader_type) + QLatin1String(": ") + it.value().at(idx));
            idx -= it.value().size();
        }
        Q_ASSERT(false);
    } else {
        return MetaEnum::flagsToString((1 << index.row()), qopengl_shader_type);
    }
#endif

    return QVariant();
}

int MaterialShaderModel::shaderFileCount(QSGMaterialShader *shader)
{
    Q_ASSERT(shader);
    int fileCount = 0;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    if (QSGMaterialShaderPrivate *p = QSGMaterialShaderPrivate::get(shader)) {
        return p->shaderFileNames.size();
    }
#else
    const auto &files = reinterpret_cast<SGMaterialShaderThief *>(shader)->getShaderSources();
    for (auto it = files.begin(); it != files.end(); ++it)
        fileCount += it.value().size();
#endif
    return fileCount;
}

QString MaterialShaderModel::shaderFileForRow(int row) const
{
    Q_ASSERT(m_shader);
    Q_ASSERT(m_shaderFileCount > 0);
    Q_ASSERT(row < m_shaderFileCount);

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
    if (QSGMaterialShaderPrivate *p = QSGMaterialShaderPrivate::get(m_shader)) {
        const auto &shaderFiles = p->shaderFileNames;
        int i = 0;
        for (const auto &sf : shaderFiles) {
            if (i == row)
                return sf;
            i++;
        }
        return {};
    }
#else
    const auto &files = reinterpret_cast<SGMaterialShaderThief *>(m_shader)->getShaderSources();
    for (auto it = files.begin(); it != files.end(); ++it) {
        if (row < it.value().size())
            return it.value().at(row);
        row -= it.value().size();
    }
#endif

    Q_ASSERT(false);
    return QString();
}