aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/GrGLSL.h
blob: 9387d2bbbb3640d0e0de78d7e9ad64620b1c1567 (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
/*
 * 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 "gl/GrGLInterface.h"
#include "GrColor.h"
#include "GrTypesPriv.h"
#include "SkString.h"

class GrGLContextInfo;
class GrGLShaderVar;

// 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,
};

/**
 * Gets the most recent GLSL Generation compatible with the OpenGL context.
 */
GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
                                     const GrGLInterface* gl);

/**
 * Returns a string to include at the beginning of a shader to declare the GLSL
 * version.
 */
const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);

/**
 * 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 kMat33f_GrSLType:
            return "mat3";
        case kMat44f_GrSLType:
            return "mat4";
        case kSampler2D_GrSLType:
            return "sampler2D";
        default:
            GrCrash("Unknown shader var type.");
            return ""; // suppress warning
    }
}

/** A 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.
 * Complex expressions can be constructed with operators *, +, -
 */
template <int N>
class GrGLSLExpr {
public:
    /** 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) {
        SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range);
        // 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) {
        SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range);
        if (v == 0) {
            fType = kZeros_ExprType;
        } else if (v == 1) {
            fType = kOnes_ExprType;
        } else {
            fType = kFullExpr_ExprType;
            fExpr.appendf(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[]) {
        SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range);
        if (NULL == 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) {
        SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range);
        if (expr.isEmpty()) {  // TODO: remove this once effects input Exprs.
            fType = kOnes_ExprType;
        } else {
            fType = kFullExpr_ExprType;
            fExpr = expr;
        }
        SkASSERT(this->isValid());
    }

    bool isOnes() const { return kOnes_ExprType == fType; }
    bool isZeros() const { return kZeros_ExprType == fType; }

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

private:
    GrGLSLExpr(const char format[], const char in0[])
        : fType(kFullExpr_ExprType) {
        fExpr.appendf(format, in0);
    }

    GrGLSLExpr(const char format[], const char in0[], const char in1[])
        : fType(kFullExpr_ExprType) {
        fExpr.appendf(format, in0, in1);
    }

    GrGLSLExpr(const char format[], const char in0[], char in1)
        : fType(kFullExpr_ExprType) {
        fExpr.appendf(format, in0, in1);
    }

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

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

    /** Casts the expression expr into smaller or bigger expression.
     * Casting is done with GLSL rules:
     * M==3, N==4 vec3(a, b, c) -> vec4(a, b, c, 0)
     * N==4, M==3 vec4(a, b, c, d) -> vec3(a, b, c)
     */
    template <int M>
    static GrGLSLExpr<N> VectorCast(const GrGLSLExpr<M>& expr);

    /** GLSL multiplication: component-wise or multiply each component by a scalar.
     * M == N --> vecN(in0.x * in1.x, ...)
     * M == 1 --> vecN(in0.x * in1, ...)
     * otherwise --> compile-time error
     */
    template <int M>
    static GrGLSLExpr<N> Mul(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1);

    /** GLSL addition: component-wise or add a scalar to each compoment.
     * M == N --> vecN(in0.x + in1.x, ...)
     * M == 1 --> vecN(in0.x + in1, ...)
     * otherwise --> compile-time error
     */
    template <int M>
    static GrGLSLExpr<N> Add(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1);

    /** GLSL subtraction: component-wise or subtract compoments by a scalar.
     * M == N --> vecN(in0.x - in1.x, ...)
     * M == 1 --> vecN(in0.x - in1, ...)
     * otherwise --> compile-time error
     */
    template <int M>
    static GrGLSLExpr<N> Sub(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1);

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

    template <int> friend class GrGLSLExpr;

    /** Multiplies two expressions component-wise. */
    template <int M> friend GrGLSLExpr<M> operator*(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&);
    /** Adds two expressions component-wise. */
    template <int M> friend GrGLSLExpr<M> operator+(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&);
    /** Subtracts two expressions component-wise. */
    template <int M> friend GrGLSLExpr<M> operator-(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&);
    /** Multiplies every component of an expression with a scalar expression. */
    friend GrGLSLExpr<4> operator*(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&);
    /** Adds a scalar expression to every component of an expression. */
    friend GrGLSLExpr<4> operator+(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&);
    /** Subtracts a scalar expression from every component of an expression. */
    friend GrGLSLExpr<4> operator-(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&);

    friend GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr);
    friend GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr);
};


template <int N>
inline GrGLSLExpr<N> operator*(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1) {
    return GrGLSLExpr<N>::Mul(in0, in1);
}

template <int N>
inline GrGLSLExpr<N> operator+(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1) {
    return GrGLSLExpr<N>::Add(in0, in1);
}

template <int N>
inline GrGLSLExpr<N> operator-(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1) {
    return GrGLSLExpr<N>::Sub(in0, in1);
}

inline GrGLSLExpr<4> operator*(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in1) {
    return GrGLSLExpr<4>::Mul(in0, in1);
}

inline GrGLSLExpr<4> operator+(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in1) {
    return GrGLSLExpr<4>::Add(in0, in1);
}

inline GrGLSLExpr<4> operator-(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in1) {
    return GrGLSLExpr<4>::Sub(in0, in1);
}

/** Casts an vec1 expression  to vec4 expresison, eg. vec1(v) -> vec4(v,v,v,v). */
GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr);

/** Extracts alpha component from an expression of vec<4>. */
GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr);

/**
 * Does an inplace mul, *=, of vec4VarName by mulFactor.
 * A semicolon and newline are added after the assignment.
 */
void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt,
                      const char* vec4VarName, const GrGLSLExpr<4>& mulFactor);

#include "GrGLSL_impl.h"

#endif