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