1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrGLSL_DEFINED
9#define GrGLSL_DEFINED
10
11#include "gl/GrGLInterface.h"
12#include "GrColor.h"
13#include "GrTypesPriv.h"
14#include "SkString.h"
15
16class GrGLContextInfo;
17class GrGLShaderVar;
18
19// Limited set of GLSL versions we build shaders for. Caller should round
20// down the GLSL version to one of these enums.
21enum GrGLSLGeneration {
22    /**
23     * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20)
24     */
25    k110_GrGLSLGeneration,
26    /**
27     * Desktop GLSL 1.30
28     */
29    k130_GrGLSLGeneration,
30    /**
31     * Desktop GLSL 1.40
32     */
33    k140_GrGLSLGeneration,
34    /**
35     * Desktop GLSL 1.50
36     */
37    k150_GrGLSLGeneration,
38};
39
40/**
41 * Gets the most recent GLSL Generation compatible with the OpenGL context.
42 */
43GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
44                                     const GrGLInterface* gl);
45
46/**
47 * Returns a string to include at the beginning of a shader to declare the GLSL
48 * version.
49 */
50const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);
51
52/**
53 * Converts a GrSLType to a string containing the name of the equivalent GLSL type.
54 */
55static inline const char* GrGLSLTypeString(GrSLType t) {
56    switch (t) {
57        case kVoid_GrSLType:
58            return "void";
59        case kFloat_GrSLType:
60            return "float";
61        case kVec2f_GrSLType:
62            return "vec2";
63        case kVec3f_GrSLType:
64            return "vec3";
65        case kVec4f_GrSLType:
66            return "vec4";
67        case kMat33f_GrSLType:
68            return "mat3";
69        case kMat44f_GrSLType:
70            return "mat4";
71        case kSampler2D_GrSLType:
72            return "sampler2D";
73        default:
74            GrCrash("Unknown shader var type.");
75            return ""; // suppress warning
76    }
77}
78
79/** A generic base-class representing a GLSL expression.
80 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant
81 * folding with help of 1 and 0.
82 *
83 * Clients should not use this class, rather the specific instantiations defined
84 * later, for example GrGLSLExpr4.
85 */
86template <typename Self>
87class GrGLSLExpr {
88public:
89    bool isOnes() const { return kOnes_ExprType == fType; }
90    bool isZeros() const { return kZeros_ExprType == fType; }
91
92    const char* c_str() const {
93        if (kZeros_ExprType == fType) {
94            return Self::ZerosStr();
95        } else if (kOnes_ExprType == fType) {
96            return Self::OnesStr();
97        }
98        SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
99        return fExpr.c_str();
100    }
101
102protected:
103    /** Constructs an invalid expression.
104     * Useful only as a return value from functions that never actually return
105     * this and instances that will be assigned to later. */
106    GrGLSLExpr()
107        : fType(kFullExpr_ExprType) {
108        // The only constructor that is allowed to build an empty expression.
109        SkASSERT(!this->isValid());
110    }
111
112    /** Constructs an expression with all components as value v */
113    explicit GrGLSLExpr(int v) {
114        if (v == 0) {
115            fType = kZeros_ExprType;
116        } else if (v == 1) {
117            fType = kOnes_ExprType;
118        } else {
119            fType = kFullExpr_ExprType;
120            fExpr.appendf(Self::CastIntStr(), v);
121        }
122    }
123
124    /** Constructs an expression from a string.
125     * Argument expr is a simple expression or a parenthesized expression. */
126    // TODO: make explicit once effects input Exprs.
127    GrGLSLExpr(const char expr[]) {
128        if (NULL == expr) {  // TODO: remove this once effects input Exprs.
129            fType = kOnes_ExprType;
130        } else {
131            fType = kFullExpr_ExprType;
132            fExpr = expr;
133        }
134        SkASSERT(this->isValid());
135    }
136
137    /** Constructs an expression from a string.
138     * Argument expr is a simple expression or a parenthesized expression. */
139    // TODO: make explicit once effects input Exprs.
140    GrGLSLExpr(const SkString& expr) {
141        if (expr.isEmpty()) {  // TODO: remove this once effects input Exprs.
142            fType = kOnes_ExprType;
143        } else {
144            fType = kFullExpr_ExprType;
145            fExpr = expr;
146        }
147        SkASSERT(this->isValid());
148    }
149
150    /** Constructs an expression from a string with one substitution. */
151    GrGLSLExpr(const char format[], const char in0[])
152        : fType(kFullExpr_ExprType) {
153        fExpr.appendf(format, in0);
154    }
155
156    /** Constructs an expression from a string with two substitutions. */
157    GrGLSLExpr(const char format[], const char in0[], const char in1[])
158        : fType(kFullExpr_ExprType) {
159        fExpr.appendf(format, in0, in1);
160    }
161
162    bool isValid() const {
163        return kFullExpr_ExprType != fType || !fExpr.isEmpty();
164    }
165
166    /** Returns expression casted to another type.
167     * Generic implementation that is called for non-trivial cases of casts. */
168    template <typename T>
169    static Self VectorCastImpl(const T& other);
170
171    /** Returns a GLSL multiplication: component-wise or component-by-scalar.
172     * The multiplication will be component-wise or multiply each component by a scalar.
173     *
174     * The returned expression will compute the value of:
175     *    vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise)
176     *    vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar)
177     *    vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector)
178     */
179    template <typename T0, typename T1>
180    static Self Mul(T0 in0, T1 in1);
181
182    /** Returns a GLSL addition: component-wise or add a scalar to each component.
183     * Return value computes:
184     *   vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...).
185     */
186    template <typename T0, typename T1>
187    static Self Add(T0 in0, T1 in1);
188
189    /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar.
190     * Return value computes
191     *   vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...).
192     */
193    template <typename T0, typename T1>
194    static Self Sub(T0 in0, T1 in1);
195
196    /** Returns expression that accesses component(s) of the expression.
197     * format should be the form "%s.x" where 'x' is the component(s) to access.
198     * Caller is responsible for making sure the amount of components in the
199     * format string is equal to dim(T).
200     */
201    template <typename T>
202    T extractComponents(const char format[]) const;
203
204private:
205    enum ExprType {
206        kZeros_ExprType,
207        kOnes_ExprType,
208        kFullExpr_ExprType,
209    };
210    ExprType fType;
211    SkString fExpr;
212};
213
214class GrGLSLExpr1;
215class GrGLSLExpr4;
216
217/** Class representing a float GLSL expression. */
218class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> {
219public:
220    GrGLSLExpr1()
221        : INHERITED() {
222    }
223    explicit GrGLSLExpr1(int v)
224        : INHERITED(v) {
225    }
226    GrGLSLExpr1(const char* expr)
227        : INHERITED(expr) {
228    }
229    GrGLSLExpr1(const SkString& expr)
230        : INHERITED(expr) {
231    }
232
233    static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr);
234
235private:
236    GrGLSLExpr1(const char format[], const char in0[])
237        : INHERITED(format, in0) {
238    }
239    GrGLSLExpr1(const char format[], const char in0[], const char in1[])
240        : INHERITED(format, in0, in1) {
241    }
242
243    static const char* ZerosStr();
244    static const char* OnesStr();
245    static const char* CastStr();
246    static const char* CastIntStr();
247
248    friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
249    friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
250    friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1);
251
252    friend class GrGLSLExpr<GrGLSLExpr1>;
253    friend class GrGLSLExpr<GrGLSLExpr4>;
254
255    typedef GrGLSLExpr<GrGLSLExpr1> INHERITED;
256};
257
258/** Class representing a float vector (vec4) GLSL expression. */
259class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> {
260public:
261    GrGLSLExpr4()
262        : INHERITED() {
263    }
264    explicit GrGLSLExpr4(int v)
265        : INHERITED(v) {
266    }
267    GrGLSLExpr4(const char* expr)
268        : INHERITED(expr) {
269    }
270    GrGLSLExpr4(const SkString& expr)
271        : INHERITED(expr) {
272    }
273
274    typedef GrGLSLExpr1 AExpr;
275    AExpr a() const;
276
277    /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */
278    static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr);
279    static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr);
280
281private:
282    GrGLSLExpr4(const char format[], const char in0[])
283        : INHERITED(format, in0) {
284    }
285    GrGLSLExpr4(const char format[], const char in0[], const char in1[])
286        : INHERITED(format, in0, in1) {
287    }
288
289    static const char* ZerosStr();
290    static const char* OnesStr();
291    static const char* CastStr();
292    static const char* CastIntStr();
293
294    // The vector-by-scalar and scalar-by-vector binary operations.
295    friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
296    friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
297    friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1);
298    friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
299    friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
300    friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1);
301
302    // The vector-by-vector, i.e. component-wise, binary operations.
303    friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
304    friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
305    friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1);
306
307    friend class GrGLSLExpr<GrGLSLExpr4>;
308
309    typedef GrGLSLExpr<GrGLSLExpr4> INHERITED;
310};
311
312/**
313 * Does an inplace mul, *=, of vec4VarName by mulFactor.
314 * A semicolon and newline are added after the assignment.
315 */
316void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt,
317                      const char* vec4VarName, const GrGLSLExpr4& mulFactor);
318
319#include "GrGLSL_impl.h"
320
321#endif
322