aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/coreapi/qsgshaderrewriter.cpp
blob: f8f1253fbfe54be44bae168a4fb7179fb1a41c4f (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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include <QtCore/QByteArray>
#include <QtCore/QString>
#include <QtGui/QSurfaceFormat>

// Duct Tape tokenizer for the purpose of parsing and rewriting
// shader source code

QT_BEGIN_NAMESPACE

namespace QSGShaderRewriter {

struct Tokenizer {

    enum Token {
        Token_Void,
        Token_OpenBrace,
        Token_CloseBrace,
        Token_SemiColon,
        Token_Identifier,
        Token_Macro,
        Token_Unspecified,

        Token_EOF
    };

    static const char *NAMES[];

    void initialize(const char *input);
    Token next();

    const char *stream;
    const char *pos;
    const char *identifier;
};

const char *Tokenizer::NAMES[] = {
    "Void",
    "OpenBrace",
    "CloseBrace",
    "SemiColon",
    "Identifier",
    "Macro",
    "Unspecified",
    "EOF"
};

void Tokenizer::initialize(const char *input)
{
    stream = input;
    pos = input;
    identifier = input;
}

Tokenizer::Token Tokenizer::next()
{
    while (*pos != 0) {
        char c = *pos++;
        switch (c) {
        case '/':

            if (*pos == '/') {
                // '//' comment
                ++pos;
                while (*pos != 0 && *pos != '\n') ++pos;
                if (*pos != 0) ++pos; // skip the newline

            } else if (*pos == '*') {
                // /* */ comment
                ++pos;
                while (*pos != 0 && *pos != '*' && pos[1] != '/') ++pos;
                if (*pos != 0) pos += 2;
            }
            break;

        case '#': {
            while (*pos != 0) {
                if (*pos == '\n') {
                    ++pos;
                    break;
                } else if (*pos == '\\') {
                    ++pos;
                    while (*pos != 0 && (*pos == ' ' || *pos == '\t'))
                        ++pos;
                    if (*pos != 0 && (*pos == '\n' || (*pos == '\r' && pos[1] == '\n')))
                        pos+=2;
                } else {
                    ++pos;
                }
            }
            break;
        }

        case 'v': {
            if (*pos == 'o' && pos[1] == 'i' && pos[2] == 'd') {
                pos += 3;
                return Token_Void;
            }
            Q_FALLTHROUGH();
        }

        case ';': return Token_SemiColon;
        case 0: return Token_EOF;
        case '{': return Token_OpenBrace;
        case '}': return Token_CloseBrace;

        case ' ':
        case '\n':
        case '\r': break;
        default:
            // Identifier...
            if ((c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' ) || c == '_') {
                identifier = pos - 1;
                while (*pos != 0 && ((*pos >= 'a' && *pos <= 'z')
                                     || (*pos >= 'A' && *pos <= 'Z')
                                     || *pos == '_'
                                     || (*pos >= '0' && *pos <= '9'))) {
                    ++pos;
                }
                return Token_Identifier;
            } else {
                return Token_Unspecified;
            }
        }
    }

    return Token_EOF;
}

}

using namespace QSGShaderRewriter;

QByteArray qsgShaderRewriter_insertZAttributes(const char *input, QSurfaceFormat::OpenGLContextProfile profile)
{
    Tokenizer tok;
    tok.initialize(input);

    Tokenizer::Token lt = tok.next();
    Tokenizer::Token t = tok.next();

    // First find "void main() { ... "
    const char* voidPos = input;
    while (t != Tokenizer::Token_EOF) {
        if (lt == Tokenizer::Token_Void && t == Tokenizer::Token_Identifier) {
            if (qstrncmp("main", tok.identifier, 4) == 0)
                break;
        }
        voidPos = tok.pos - 4;
        lt = t;
        t = tok.next();
    }

    QByteArray result;
    result.reserve(1024);
    result += QByteArray::fromRawData(input, voidPos - input);
    switch (profile) {
    case QSurfaceFormat::NoProfile:
    case QSurfaceFormat::CompatibilityProfile:
        result += "attribute highp float _qt_order;\n"
                  "uniform highp float _qt_zRange;\n";
        break;

    case QSurfaceFormat::CoreProfile:
        result += "in float _qt_order;\n"
                  "uniform float _qt_zRange;\n";
        break;
    }

    // Find first brace '{'
    while (t != Tokenizer::Token_EOF && t != Tokenizer::Token_OpenBrace) t = tok.next();
    int braceDepth = 1;
    t = tok.next();

    // Find matching brace and insert our code there...
    while (t != Tokenizer::Token_EOF) {
        switch (t) {
        case Tokenizer::Token_CloseBrace:
            braceDepth--;
            if (braceDepth == 0) {
                result += QByteArray::fromRawData(voidPos, tok.pos - 1 - voidPos)
                        + "    gl_Position.z = (gl_Position.z * _qt_zRange + _qt_order) * gl_Position.w;\n"
                        + QByteArray(tok.pos - 1);
                return result;
            }
            break;
        case Tokenizer::Token_OpenBrace:
            ++braceDepth;
            break;
        default:
            break;
        }
        t = tok.next();
    }
    return QByteArray();
}

#ifdef QSGSHADERREWRITER_STANDALONE

const char *selftest =
        "#define highp lowp stuff                           \n"
        "#define multiline \\                               \n"
        "   continue defining multiline                     \n"
        "                                                   \n"
        "attribute highp vec4 qt_Position;                  \n"
        "attribute highp vec2 qt_TexCoord;                  \n"
        "                                                   \n"
        "uniform highp mat4 qt_Matrix;                      \n"
        "                                                   \n"
        "varying lowp vec2 vTexCoord;                       \n"
        "                                                   \n"
        "// commented out main(){}                          \n"
        "/* commented out main() { } again */               \n"
        "/*                                                 \n"
        "   multline comment with main() { }                \n"
        " */                                                \n"
        "                                                   \n"
        "void main() {                                      \n"
        "    gl_Position = qt_Matrix * qt_Position;         \n"
        "    vTexCoord = qt_TexCoord;                       \n"
        "    if (gl_Position < 0) {                         \n"
        "        vTexCoord.y = -vTexCoord.y;                \n"
        "    }                                              \n"
        "}                                                  \n"
        "";

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    QString fileName;
    QStringList args = app.arguments();

    QByteArray content;

    for (int i=0; i<args.length(); ++i) {
        const QString &a = args.at(i);
        if (a == QLatin1String("--file") && i < args.length() - 1) {
            qDebug() << "Reading file: " << args.at(i);
            QFile file(args.at(++i));
            if (!file.open(QFile::ReadOnly)) {
                qDebug() << "Error: failed to open file," << file.errorString();
                return 1;
            }
            content = file.readAll();
        } else if (a == QLatin1String("--selftest")) {
            qDebug() << "doing a selftest";
            content = QByteArray(selftest);
        } else if (a == QLatin1String("--help") || a == QLatin1String("-h")) {
            qDebug() << "usage:" << endl
                     << "  --file [name]        A vertex shader file to rewrite" << endl;
        }
    }

    QByteArray rewritten = qsgShaderRewriter_insertZAttributes(content);

    qDebug() << "Rewritten to:";
    qDebug() << rewritten.constData();
}
#endif

QT_END_NAMESPACE