aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/glsl/GrGLSL.h
blob: e4e165b284b0052cce552f4db2a87a952dff0ca2 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrGLSL_DEFINED
#define GrGLSL_DEFINED

#include "GrTypesPriv.h"
#include "SkString.h"

class GrGLSLCaps;

// Limited set of GLSL versions we build shaders for. Caller should round
// down the GLSL version to one of these enums.
enum GrGLSLGeneration {
    /**
     * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
     */
    k110_GrGLSLGeneration,
    /**
     * Desktop GLSL 1.30
     */
    k130_GrGLSLGeneration,
    /**
     * Desktop GLSL 1.40
     */
    k140_GrGLSLGeneration,
    /**
     * Desktop GLSL 1.50
     */
    k150_GrGLSLGeneration,
    /**
     * Desktop GLSL 3.30, and ES GLSL 3.00
     */
    k330_GrGLSLGeneration,
    /**
     * Desktop GLSL 4.00
     */
    k400_GrGLSLGeneration,
    /**
     * ES GLSL 3.10 only TODO Make GLSLCap objects to make this more granular
     */
    k310es_GrGLSLGeneration,
    /**
     * ES GLSL 3.20
     */
    k320es_GrGLSLGeneration,
};

bool GrGLSLSupportsNamedFragmentShaderOutputs(GrGLSLGeneration);

/**
 * Gets the name of the function that should be used to sample a 2D texture. Coord type is used
 * to indicate whether the texture is sampled using projective textured (kVec3f) or not (kVec2f).
 */
inline const char* GrGLSLTexture2DFunctionName(GrSLType coordType, GrSLType samplerType,
                                               GrGLSLGeneration glslGen) {
    SkASSERT(GrSLTypeIs2DCombinedSamplerType(samplerType));
    SkASSERT(kVec2f_GrSLType == coordType || kVec3f_GrSLType == coordType);
    // GL_TEXTURE_RECTANGLE_ARB is written against OpenGL 2.0/GLSL 1.10. At that time there were
    // separate texture*() functions. In OpenGL 3.0/GLSL 1.30 the different texture*() functions
    // were deprecated in favor or the unified texture() function. RECTANGLE textures became
    // standard in OpenGL 3.2/GLSL 1.50 and use texture(). It isn't completely clear what function
    // should be used for RECTANGLE textures in GLSL versions >= 1.30 && < 1.50. We're going with
    // using texture().
    if (glslGen >= k130_GrGLSLGeneration) {
        return (kVec2f_GrSLType == coordType) ? "texture" : "textureProj";
    }
    if (kVec2f_GrSLType == coordType) {
        return (samplerType == kTexture2DRectSampler_GrSLType) ? "texture2DRect" : "texture2D";
    } else {
        return (samplerType == kTexture2DRectSampler_GrSLType) ? "texture2DRectProj"
                                                               : "texture2DProj";
    }
}

/**
 * Adds a line of GLSL code to declare the default precision for float types.
 */
void GrGLSLAppendDefaultFloatPrecisionDeclaration(GrSLPrecision,
                                                  const GrGLSLCaps& glslCaps,
                                                  SkString* out);

/**
 * Converts a GrSLPrecision to its corresponding GLSL precision qualifier.
 */
static inline const char* GrGLSLPrecisionString(GrSLPrecision p) {
    switch (p) {
        case kLow_GrSLPrecision:
            return "lowp";
        case kMedium_GrSLPrecision:
            return "mediump";
        case kHigh_GrSLPrecision:
            return "highp";
        default:
            SkFAIL("Unexpected precision type.");
            return "";
    }
}

/**
 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
 */
static inline const char* GrGLSLTypeString(GrSLType t) {
    switch (t) {
        case kVoid_GrSLType:
            return "void";
        case kFloat_GrSLType:
            return "float";
        case kVec2f_GrSLType:
            return "vec2";
        case kVec3f_GrSLType:
            return "vec3";
        case kVec4f_GrSLType:
            return "vec4";
        case kMat22f_GrSLType:
            return "mat2";
        case kMat33f_GrSLType:
            return "mat3";
        case kMat44f_GrSLType:
            return "mat4";
        case kTexture2DSampler_GrSLType:
            return "sampler2D";
        case kTextureExternalSampler_GrSLType:
            return "samplerExternalOES";
        case kTexture2DRectSampler_GrSLType:
            return "sampler2DRect";
        case kTextureBufferSampler_GrSLType:
            return "samplerBuffer";
        case kBool_GrSLType:
            return "bool";
        case kInt_GrSLType:
            return "int";
        case kUint_GrSLType:
            return "uint";
        case kTexture2D_GrSLType:
            return "texture2D";
        case kSampler_GrSLType:
            return "sampler";
        default:
            SkFAIL("Unknown shader var type.");
            return ""; // suppress warning
    }
}

/** A generic base-class representing a GLSL expression.
 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
 * folding with help of 1 and 0.
 *
 * Clients should not use this class, rather the specific instantiations defined
 * later, for example GrGLSLExpr4.
 */
template <typename Self>
class GrGLSLExpr {
public:
    bool isOnes() const { return kOnes_ExprType == fType; }
    bool isZeros() const { return kZeros_ExprType == fType; }

    const char* c_str() const {
        if (kZeros_ExprType == fType) {
            return Self::ZerosStr();
        } else if (kOnes_ExprType == fType) {
            return Self::OnesStr();
        }
        SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
        return fExpr.c_str();
    }

    bool isValid() const {
        return kFullExpr_ExprType != fType || !fExpr.isEmpty();
    }

protected:
    /** Constructs an invalid expression.
     * Useful only as a return value from functions that never actually return
     * this and instances that will be assigned to later. */
    GrGLSLExpr()
        : fType(kFullExpr_ExprType) {
        // The only constructor that is allowed to build an empty expression.
        SkASSERT(!this->isValid());
    }

    /** Constructs an expression with all components as value v */
    explicit GrGLSLExpr(int v) {
        if (v == 0) {
            fType = kZeros_ExprType;
        } else if (v == 1) {
            fType = kOnes_ExprType;
        } else {
            fType = kFullExpr_ExprType;
            fExpr.appendf(Self::CastIntStr(), v);
        }
    }

    /** Constructs an expression from a string.
     * Argument expr is a simple expression or a parenthesized expression. */
    // TODO: make explicit once effects input Exprs.
    GrGLSLExpr(const char expr[]) {
        if (nullptr == expr) {  // TODO: remove this once effects input Exprs.
            fType = kOnes_ExprType;
        } else {
            fType = kFullExpr_ExprType;
            fExpr = expr;
        }
        SkASSERT(this->isValid());
    }

    /** Constructs an expression from a string.
     * Argument expr is a simple expression or a parenthesized expression. */
    // TODO: make explicit once effects input Exprs.
    GrGLSLExpr(const SkString& expr) {
        if (expr.isEmpty()) {  // TODO: remove this once effects input Exprs.
            fType = kOnes_ExprType;
        } else {
            fType = kFullExpr_ExprType;
            fExpr = expr;
        }
        SkASSERT(this->isValid());
    }

    /** Constructs an expression from a string with one substitution. */
    GrGLSLExpr(const char format[], const char in0[])
        : fType(kFullExpr_ExprType) {
        fExpr.appendf(format, in0);
    }

    /** Constructs an expression from a string with two substitutions. */
    GrGLSLExpr(const char format[], const char in0[], const char in1[])
        : fType(kFullExpr_ExprType) {
        fExpr.appendf(format, in0, in1);
    }

    /** Returns expression casted to another type.
     * Generic implementation that is called for non-trivial cases of casts. */
    template <typename T>
    static Self VectorCastImpl(const T& other);

    /** Returns a GLSL multiplication: component-wise or component-by-scalar.
     * The multiplication will be component-wise or multiply each component by a scalar.
     *
     * The returned expression will compute the value of:
     *    vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
     *    vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
     *    vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
     */
    template <typename T0, typename T1>
    static Self Mul(T0 in0, T1 in1);

    /** Returns a GLSL addition: component-wise or add a scalar to each component.
     * Return value computes:
     *   vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
     */
    template <typename T0, typename T1>
    static Self Add(T0 in0, T1 in1);

    /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
     * Return value computes
     *   vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
     */
    template <typename T0, typename T1>
    static Self Sub(T0 in0, T1 in1);

    /** Returns expression that accesses component(s) of the expression.
     * format should be the form "%s.x" where 'x' is the component(s) to access.
     * Caller is responsible for making sure the amount of components in the
     * format string is equal to dim(T).
     */
    template <typename T>
    T extractComponents(const char format[]) const;

private:
    enum ExprType {
        kZeros_ExprType,
        kOnes_ExprType,
        kFullExpr_ExprType,
    };
    ExprType fType;
    SkString fExpr;
};

class GrGLSLExpr1;
class GrGLSLExpr4;

/** Class representing a float GLSL expression. */
class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
public:
    GrGLSLExpr1()
        : INHERITED() {
    }
    explicit GrGLSLExpr1(int v)
        : INHERITED(v) {
    }
    GrGLSLExpr1(const char* expr)
        : INHERITED(expr) {
    }
    GrGLSLExpr1(const SkString& expr)
        : INHERITED(expr) {
    }

    static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);

private:
    GrGLSLExpr1(const char format[], const char in0[])
        : INHERITED(format, in0) {
    }
    GrGLSLExpr1(const char format[], const char in0[], const char in1[])
        : INHERITED(format, in0, in1) {
    }

    static const char* ZerosStr();
    static const char* OnesStr();
    static const char* CastStr();
    static const char* CastIntStr();

    friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
    friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
    friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);

    friend class GrGLSLExpr<GrGLSLExpr1>;
    friend class GrGLSLExpr<GrGLSLExpr4>;

    typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
};

/** Class representing a float vector (vec4) GLSL expression. */
class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
public:
    GrGLSLExpr4()
        : INHERITED() {
    }
    explicit GrGLSLExpr4(int v)
        : INHERITED(v) {
    }
    GrGLSLExpr4(const char* expr)
        : INHERITED(expr) {
    }
    GrGLSLExpr4(const SkString& expr)
        : INHERITED(expr) {
    }

    typedef GrGLSLExpr1 AExpr;
    AExpr a() const;

    /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
    static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
    static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);

private:
    GrGLSLExpr4(const char format[], const char in0[])
        : INHERITED(format, in0) {
    }
    GrGLSLExpr4(const char format[], const char in0[], const char in1[])
        : INHERITED(format, in0, in1) {
    }

    static const char* ZerosStr();
    static const char* OnesStr();
    static const char* CastStr();
    static const char* CastIntStr();

    // The vector-by-scalar and scalar-by-vector binary operations.
    friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
    friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
    friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
    friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
    friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
    friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);

    // The vector-by-vector, i.e. component-wise, binary operations.
    friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
    friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
    friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);

    friend class GrGLSLExpr<GrGLSLExpr4>;

    typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
};

/**
 * Does an inplace mul, *=, of vec4VarName by mulFactor.
 * A semicolon is added after the assignment.
 */
void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor);

#include "GrGLSL_impl.h"

#endif