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