aboutsummaryrefslogtreecommitdiff
path: root/src/deps/skia/include/sksl/DSLVar.h
blob: 391474c5a9b46c9ddaaf8cbbe83db3ffe0992959 (plain) (blame)
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
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright 2020 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_DSL_VAR
#define SKSL_DSL_VAR

#include "include/sksl/DSLExpression.h"
#include "include/sksl/DSLModifiers.h"
#include "include/sksl/DSLType.h"

namespace SkSL {

class Expression;
class IRGenerator;
class SPIRVCodeGenerator;
class Variable;
enum class VariableStorage : int8_t;

namespace dsl {

class DSLVarBase {
public:
    /**
     * Creates an empty, unpopulated var. Can be replaced with a real var later via `swap`.
     */
    DSLVarBase() : fType(kVoid_Type), fDeclared(true) {}

    /**
     * Constructs a new variable with the specified type and name. The name is used (in mangled
     * form) in the resulting shader code; it is not otherwise important. Since mangling prevents
     * name conflicts and the variable's name is only important when debugging shaders, the name
     * parameter is optional.
     */
    DSLVarBase(DSLType type, skstd::string_view name, DSLExpression initialValue, PositionInfo pos);

    DSLVarBase(DSLType type, DSLExpression initialValue, PositionInfo pos);

    DSLVarBase(const DSLModifiers& modifiers, DSLType type, skstd::string_view name,
               DSLExpression initialValue, PositionInfo pos);

    DSLVarBase(const DSLModifiers& modifiers, DSLType type, DSLExpression initialValue,
               PositionInfo pos);

    DSLVarBase(DSLVarBase&&) = default;

    virtual ~DSLVarBase();

    skstd::string_view name() const {
        return fName;
    }

    const DSLModifiers& modifiers() const {
        return fModifiers;
    }

    virtual VariableStorage storage() const = 0;

    DSLExpression x() {
        return DSLExpression(*this, PositionInfo()).x();
    }

    DSLExpression y() {
        return DSLExpression(*this, PositionInfo()).y();
    }

    DSLExpression z() {
        return DSLExpression(*this, PositionInfo()).z();
    }

    DSLExpression w() {
        return DSLExpression(*this, PositionInfo()).w();
    }

    DSLExpression r() {
        return DSLExpression(*this, PositionInfo()).r();
    }

    DSLExpression g() {
        return DSLExpression(*this, PositionInfo()).g();
    }

    DSLExpression b() {
        return DSLExpression(*this, PositionInfo()).b();
    }

    DSLExpression a() {
        return DSLExpression(*this, PositionInfo()).a();
    }

    DSLExpression field(skstd::string_view name) {
        return DSLExpression(*this, PositionInfo()).field(name);
    }

    DSLPossibleExpression operator[](DSLExpression&& index);

    DSLPossibleExpression operator++() {
        return ++DSLExpression(*this, PositionInfo());
    }

    DSLPossibleExpression operator++(int) {
        return DSLExpression(*this, PositionInfo())++;
    }

    DSLPossibleExpression operator--() {
        return --DSLExpression(*this, PositionInfo());
    }

    DSLPossibleExpression operator--(int) {
        return DSLExpression(*this, PositionInfo())--;
    }

protected:
    DSLPossibleExpression assign(DSLExpression other);

    void swap(DSLVarBase& other);

    DSLModifiers fModifiers;
    // We only need to keep track of the type here so that we can create the SkSL::Variable. For
    // predefined variables this field is unnecessary, so we don't bother tracking it and just set
    // it to kVoid; in other words, you shouldn't generally be relying on this field to be correct.
    // If you need to determine the variable's type, look at DSLWriter::Var(...)->type() instead.
    DSLType fType;
    int fUniformHandle = -1;
    std::unique_ptr<SkSL::Statement> fDeclaration;
    const SkSL::Variable* fVar = nullptr;
    skstd::string_view fRawName; // for error reporting
    skstd::string_view fName;
    DSLExpression fInitialValue;
    // true if we have attempted to create the SkSL var
    bool fInitialized = false;
    bool fDeclared = false;
    PositionInfo fPosition;

    friend class DSLCore;
    friend class DSLExpression;
    friend class DSLFunction;
    friend class DSLWriter;
    friend class ::SkSL::IRGenerator;
    friend class ::SkSL::SPIRVCodeGenerator;
};

/**
 * A local variable.
 */
class DSLVar : public DSLVarBase {
public:
    DSLVar() = default;

    DSLVar(DSLType type, skstd::string_view name = "var",
           DSLExpression initialValue = DSLExpression(),
           PositionInfo pos = PositionInfo::Capture())
        : INHERITED(type, name, std::move(initialValue), pos) {}

    DSLVar(DSLType type, const char* name, DSLExpression initialValue = DSLExpression(),
           PositionInfo pos = PositionInfo::Capture())
        : DSLVar(type, skstd::string_view(name), std::move(initialValue), pos) {}

    DSLVar(DSLType type, DSLExpression initialValue, PositionInfo pos = PositionInfo::Capture())
        : INHERITED(type, std::move(initialValue), pos) {}

    DSLVar(const DSLModifiers& modifiers, DSLType type, skstd::string_view name = "var",
           DSLExpression initialValue = DSLExpression(), PositionInfo pos = PositionInfo::Capture())
        : INHERITED(modifiers, type, name, std::move(initialValue), pos) {}

    DSLVar(const DSLModifiers& modifiers, DSLType type, const char* name,
           DSLExpression initialValue = DSLExpression(), PositionInfo pos = PositionInfo::Capture())
        : DSLVar(modifiers, type, skstd::string_view(name), std::move(initialValue), pos) {}

    DSLVar(DSLVar&&) = default;

    VariableStorage storage() const override;

    void swap(DSLVar& other);

    DSLPossibleExpression operator=(DSLExpression expr);

    DSLPossibleExpression operator=(DSLVar& param) {
        return this->operator=(DSLExpression(param));
    }

    template<class Param>
    DSLPossibleExpression operator=(Param& param) {
        return this->operator=(DSLExpression(param));
    }

private:
    using INHERITED = DSLVarBase;
};

/**
 * A global variable.
 */
class DSLGlobalVar : public DSLVarBase {
public:
    DSLGlobalVar() = default;

    DSLGlobalVar(DSLType type, skstd::string_view name = "var",
           DSLExpression initialValue = DSLExpression(), PositionInfo pos = PositionInfo::Capture())
        : INHERITED(type, name, std::move(initialValue), pos) {}

    DSLGlobalVar(DSLType type, const char* name, DSLExpression initialValue = DSLExpression(),
                 PositionInfo pos = PositionInfo::Capture())
        : DSLGlobalVar(type, skstd::string_view(name), std::move(initialValue), pos) {}

    DSLGlobalVar(DSLType type, DSLExpression initialValue,
                 PositionInfo pos = PositionInfo::Capture())
        : INHERITED(type, std::move(initialValue), pos) {}

    DSLGlobalVar(const DSLModifiers& modifiers, DSLType type, skstd::string_view name = "var",
           DSLExpression initialValue = DSLExpression(), PositionInfo pos = PositionInfo::Capture())
        : INHERITED(modifiers, type, name, std::move(initialValue), pos) {}

    DSLGlobalVar(const DSLModifiers& modifiers, DSLType type, const char* name,
           DSLExpression initialValue = DSLExpression(), PositionInfo pos = PositionInfo::Capture())
        : DSLGlobalVar(modifiers, type, skstd::string_view(name), std::move(initialValue), pos) {}

    DSLGlobalVar(const char* name);

    DSLGlobalVar(DSLGlobalVar&&) = default;

    VariableStorage storage() const override;

    void swap(DSLGlobalVar& other);

    DSLPossibleExpression operator=(DSLExpression expr);

    DSLPossibleExpression operator=(DSLGlobalVar& param) {
        return this->operator=(DSLExpression(param));
    }

    template<class Param>
    DSLPossibleExpression operator=(Param& param) {
        return this->operator=(DSLExpression(param));
    }

    /**
     * Implements the following method calls:
     *     half4 shader::eval(float2 coords);
     *     half4 colorFilter::eval(half4 input);
     */
    DSLExpression eval(DSLExpression x, PositionInfo pos = PositionInfo::Capture());

    /**
     * Implements the following method call:
     *     half4 blender::eval(half4 src, half4 dst);
     */
    DSLExpression eval(DSLExpression x, DSLExpression y,
            PositionInfo pos = PositionInfo::Capture());

private:
    DSLExpression eval(ExpressionArray args, PositionInfo pos);

    std::unique_ptr<SkSL::Expression> methodCall(skstd::string_view methodName, PositionInfo pos);

    using INHERITED = DSLVarBase;
};

/**
 * A function parameter.
 */
class DSLParameter : public DSLVarBase {
public:
    DSLParameter() = default;

    DSLParameter(DSLType type, skstd::string_view name = "var",
                 PositionInfo pos = PositionInfo::Capture())
        : INHERITED(type, name, DSLExpression(), pos) {}

    DSLParameter(DSLType type, const char* name, PositionInfo pos = PositionInfo::Capture())
        : DSLParameter(type, skstd::string_view(name), pos) {}

    DSLParameter(const DSLModifiers& modifiers, DSLType type, skstd::string_view name = "var",
                 PositionInfo pos = PositionInfo::Capture())
        : INHERITED(modifiers, type, name, DSLExpression(), pos) {}

    DSLParameter(const DSLModifiers& modifiers, DSLType type, const char* name,
                 PositionInfo pos = PositionInfo::Capture())
        : DSLParameter(modifiers, type, skstd::string_view(name), pos) {}

    DSLParameter(DSLParameter&&) = default;

    VariableStorage storage() const override;

    void swap(DSLParameter& other);

    DSLPossibleExpression operator=(DSLExpression expr);

    DSLPossibleExpression operator=(DSLParameter& param) {
        return this->operator=(DSLExpression(param));
    }

    template<class Param>
    DSLPossibleExpression operator=(Param& param) {
        return this->operator=(DSLExpression(param));
    }

private:
    using INHERITED = DSLVarBase;
};

} // namespace dsl

} // namespace SkSL


#endif