OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrGLSL_DEFINED | 8 #ifndef GrGLSL_DEFINED |
9 #define GrGLSL_DEFINED | 9 #define GrGLSL_DEFINED |
10 | 10 |
11 #include "gl/GrGLInterface.h" | 11 #include "gl/GrGLInterface.h" |
12 #include "GrColor.h" | 12 #include "GrColor.h" |
13 #include "GrTypesPriv.h" | 13 #include "GrTypesPriv.h" |
14 #include "SkString.h" | |
14 | 15 |
15 class GrGLContextInfo; | 16 class GrGLContextInfo; |
16 class GrGLShaderVar; | 17 class GrGLShaderVar; |
17 class SkString; | |
18 | 18 |
19 // Limited set of GLSL versions we build shaders for. Caller should round | 19 // Limited set of GLSL versions we build shaders for. Caller should round |
20 // down the GLSL version to one of these enums. | 20 // down the GLSL version to one of these enums. |
21 enum GrGLSLGeneration { | 21 enum GrGLSLGeneration { |
22 /** | 22 /** |
23 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20) | 23 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20) |
24 */ | 24 */ |
25 k110_GrGLSLGeneration, | 25 k110_GrGLSLGeneration, |
26 /** | 26 /** |
27 * Desktop GLSL 1.30 | 27 * Desktop GLSL 1.30 |
28 */ | 28 */ |
29 k130_GrGLSLGeneration, | 29 k130_GrGLSLGeneration, |
30 /** | 30 /** |
31 * Desktop GLSL 1.40 | 31 * Desktop GLSL 1.40 |
32 */ | 32 */ |
33 k140_GrGLSLGeneration, | 33 k140_GrGLSLGeneration, |
34 /** | 34 /** |
35 * Desktop GLSL 1.50 | 35 * Desktop GLSL 1.50 |
36 */ | 36 */ |
37 k150_GrGLSLGeneration, | 37 k150_GrGLSLGeneration, |
38 }; | 38 }; |
39 | 39 |
40 enum GrSLConstantVec { | |
41 kZeros_GrSLConstantVec, | |
42 kOnes_GrSLConstantVec, | |
43 kNone_GrSLConstantVec, | |
44 }; | |
45 | |
46 namespace { | 40 namespace { |
47 static inline int GrSLTypeToVecLength(GrSLType type) { | 41 static inline int GrSLTypeToVecLength(GrSLType type) { |
48 static const int kVecLengths[] = { | 42 static const int kVecLengths[] = { |
49 0, // kVoid_GrSLType | 43 0, // kVoid_GrSLType |
50 1, // kFloat_GrSLType | 44 1, // kFloat_GrSLType |
51 2, // kVec2f_GrSLType | 45 2, // kVec2f_GrSLType |
52 3, // kVec3f_GrSLType | 46 3, // kVec3f_GrSLType |
53 4, // kVec4f_GrSLType | 47 4, // kVec4f_GrSLType |
54 1, // kMat33f_GrSLType | 48 1, // kMat33f_GrSLType |
55 1, // kMat44f_GrSLType | 49 1, // kMat44f_GrSLType |
56 1, // kSampler2D_GrSLType | 50 1, // kSampler2D_GrSLType |
57 }; | 51 }; |
58 GR_STATIC_ASSERT(kGrSLTypeCount == GR_ARRAY_COUNT(kVecLengths)); | 52 GR_STATIC_ASSERT(kGrSLTypeCount == GR_ARRAY_COUNT(kVecLengths)); |
59 return kVecLengths[type]; | 53 return kVecLengths[type]; |
60 } | 54 } |
61 | 55 |
62 static inline const char* GrGLSLOnesVecf(int count) { | |
63 static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)", | |
64 "vec3(1,1,1)", "vec4(1,1,1,1)"}; | |
65 SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC)); | |
66 return kONESVEC[count]; | |
67 } | 56 } |
68 | 57 |
69 static inline const char* GrGLSLZerosVecf(int count) { | |
70 static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)", | |
71 "vec3(0,0,0)", "vec4(0,0,0,0)"}; | |
72 SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC)); | |
73 return kZEROSVEC[count]; | |
74 } | |
75 } | |
76 | 58 |
77 /** | 59 /** |
78 * Gets the most recent GLSL Generation compatible with the OpenGL context. | 60 * Gets the most recent GLSL Generation compatible with the OpenGL context. |
79 */ | 61 */ |
80 GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding, | 62 GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding, |
81 const GrGLInterface* gl); | 63 const GrGLInterface* gl); |
82 | 64 |
83 /** | 65 /** |
84 * Returns a string to include at the beginning of a shader to declare the GLSL | 66 * Returns a string to include at the beginning of a shader to declare the GLSL |
85 * version. | 67 * version. |
86 */ | 68 */ |
87 const char* GrGetGLSLVersionDecl(const GrGLContextInfo&); | 69 const char* GrGetGLSLVersionDecl(const GrGLContextInfo&); |
88 | 70 |
89 /** | 71 /** |
90 * Converts a GrSLType to a string containing the name of the equivalent GLSL ty pe. | 72 * Converts a GrSLType to a string containing the name of the equivalent GLSL ty pe. |
91 */ | 73 */ |
92 static const char* GrGLSLTypeString(GrSLType t) { | 74 static inline const char* GrGLSLTypeString(GrSLType t) { |
93 switch (t) { | 75 switch (t) { |
94 case kVoid_GrSLType: | 76 case kVoid_GrSLType: |
95 return "void"; | 77 return "void"; |
96 case kFloat_GrSLType: | 78 case kFloat_GrSLType: |
97 return "float"; | 79 return "float"; |
98 case kVec2f_GrSLType: | 80 case kVec2f_GrSLType: |
99 return "vec2"; | 81 return "vec2"; |
100 case kVec3f_GrSLType: | 82 case kVec3f_GrSLType: |
101 return "vec3"; | 83 return "vec3"; |
102 case kVec4f_GrSLType: | 84 case kVec4f_GrSLType: |
103 return "vec4"; | 85 return "vec4"; |
104 case kMat33f_GrSLType: | 86 case kMat33f_GrSLType: |
105 return "mat3"; | 87 return "mat3"; |
106 case kMat44f_GrSLType: | 88 case kMat44f_GrSLType: |
107 return "mat4"; | 89 return "mat4"; |
108 case kSampler2D_GrSLType: | 90 case kSampler2D_GrSLType: |
109 return "sampler2D"; | 91 return "sampler2D"; |
110 default: | 92 default: |
111 GrCrash("Unknown shader var type."); | 93 GrCrash("Unknown shader var type."); |
112 return ""; // suppress warning | 94 return ""; // suppress warning |
113 } | 95 } |
114 } | 96 } |
115 | 97 |
116 /** Return the type enum for a vector of floats of length n (1..4), | |
117 e.g. 1 -> "float", 2 -> "vec2", ... */ | |
118 static inline const char* GrGLSLFloatVectorTypeString(int n) { | |
119 return GrGLSLTypeString(GrSLFloatVectorType(n)); | |
120 } | |
121 | |
122 /** Return the GLSL swizzle operator for a homogenous component of a vector | 98 /** Return the GLSL swizzle operator for a homogenous component of a vector |
123 with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */ | 99 with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */ |
124 const char* GrGLSLVectorHomogCoord(int count); | 100 const char* GrGLSLVectorHomogCoord(int count); |
125 const char* GrGLSLVectorHomogCoord(GrSLType type); | 101 const char* GrGLSLVectorHomogCoord(GrSLType type); |
126 | 102 |
127 /** Return the GLSL swizzle operator for a nonhomogenous components of a vector | 103 /** Return the GLSL swizzle operator for a nonhomogenous components of a vector |
128 with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */ | 104 with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */ |
129 const char* GrGLSLVectorNonhomogCoords(int count); | 105 const char* GrGLSLVectorNonhomogCoords(int count); |
130 const char* GrGLSLVectorNonhomogCoords(GrSLType type); | 106 const char* GrGLSLVectorNonhomogCoords(GrSLType type); |
131 | 107 |
132 /** | 108 /** A class representing a GLSL expression. |
133 * Produces a string that is the result of modulating two inputs. The inputs mu st be vecN or | 109 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does s imple constant |
134 * float. The result is always a vecN. The inputs may be expressions, not just identifier names. | 110 * folding with help of 1 and 0. |
135 * Either can be NULL or "" in which case the default params control whether a vector of ones or | 111 * Complex expressions can be constructed with operators *, +, - |
136 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". N ote that when the | 112 */ |
137 * function determines that the result is a zeros or ones vec then any expressi on represented by | |
138 * or in1 will not be emitted (side effects won't occur). The return value indi cates whether a | |
139 * known zeros or ones vector resulted. The output can be suppressed when known vector is produced | |
140 * by passing true for omitIfConstVec. | |
141 */ | |
142 template <int N> | 113 template <int N> |
143 GrSLConstantVec GrGLSLModulatef(SkString* outAppend, | 114 class GrGLSLExpr { |
144 const char* in0, | 115 public: |
145 const char* in1, | 116 /** Constructs an invalid expression. |
146 GrSLConstantVec default0 = kOnes_GrSLConstantVec , | 117 * Useful only as a return value from functions that never actually return |
147 GrSLConstantVec default1 = kOnes_GrSLConstantVec , | 118 * this and instances that will be assigned to later. */ |
148 bool omitIfConstVec = false); | 119 GrGLSLExpr() |
120 : fType(kFullExpr_ExprType) { | |
121 SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_are_in_range); | |
122 // The only constructor that is allowed to build an empty expression. | |
123 SkASSERT(!this->isValid()); | |
124 } | |
125 | |
126 /** Constructs an expression with all components as value v */ | |
127 explicit GrGLSLExpr(int v) { | |
128 SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_are_in_range); | |
129 if (v == 0) { | |
130 fType = kZeros_ExprType; | |
131 } else if (v == 1) { | |
132 fType = kOnes_ExprType; | |
133 } else { | |
134 fType = kFullExpr_ExprType; | |
135 fExpr.appendf(this->castIntStr(), v); | |
136 } | |
137 } | |
138 | |
139 /** Constructs an expression from a string. | |
140 * Argument expr is a simple expression or a parenthesized expression. */ | |
141 GrGLSLExpr(const char expr[]) { | |
bsalomon
2013/10/08 14:45:27
explicit? What's the use case for implicit convers
Kimmo Kinnunen
2013/10/09 13:39:59
Mostly due to effects inputting char*s instead of
| |
142 SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_are_in_range); | |
143 if (NULL == expr) { // TODO: remove this once effects input Exprs. | |
144 fType = kOnes_ExprType; | |
145 } else { | |
146 fType = kFullExpr_ExprType; | |
147 fExpr = expr; | |
148 } | |
149 SkASSERT(this->isValid()); | |
150 } | |
151 | |
152 /** Constructs an expression from a string. | |
153 * Argument expr is a simple expression or a parenthesized expression. */ | |
154 GrGLSLExpr(const SkString& expr) { | |
bsalomon
2013/10/08 14:45:27
ditto.
| |
155 SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_are_in_range); | |
156 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. | |
157 fType = kOnes_ExprType; | |
158 } else { | |
159 fType = kFullExpr_ExprType; | |
160 fExpr = expr; | |
161 } | |
162 SkASSERT(this->isValid()); | |
163 } | |
164 | |
165 bool isOnes() const { return kOnes_ExprType == fType; } | |
166 bool isZeros() const { return kZeros_ExprType == fType; } | |
167 | |
168 const char* c_str() const { | |
169 if (kZeros_ExprType == fType) { | |
170 return this->zerosStr(); | |
171 } else if (kOnes_ExprType == fType) { | |
172 return this->onesStr(); | |
173 } | |
174 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used. | |
175 return fExpr.c_str(); | |
176 } | |
177 | |
178 private: | |
179 GrGLSLExpr(const char format[], const char in0[]) | |
180 : fType(kFullExpr_ExprType) { | |
181 fExpr.appendf(format, in0); | |
182 } | |
183 | |
184 GrGLSLExpr(const char format[], const char in0[], const char in1[]) | |
185 : fType(kFullExpr_ExprType) { | |
186 fExpr.appendf(format, in0, in1); | |
187 } | |
188 | |
189 GrGLSLExpr(const char format[], const char in0[], char in1) | |
190 : fType(kFullExpr_ExprType) { | |
191 fExpr.appendf(format, in0, in1); | |
192 } | |
193 | |
194 bool isValid() const { | |
195 return kFullExpr_ExprType != fType || !fExpr.isEmpty(); | |
196 } | |
197 | |
198 static const char* zerosStr(); | |
bsalomon
2013/10/08 14:45:27
The reason I thought these were non-static is that
Kimmo Kinnunen
2013/10/09 13:39:59
Done.
| |
199 static const char* onesStr(); | |
200 static const char* extractAlphaStr(); | |
201 static const char* castStr(); | |
202 static const char* castIntStr(); | |
203 | |
204 template <int M> | |
205 static GrGLSLExpr<N> VectorCast(const GrGLSLExpr<M>& expr); | |
bsalomon
2013/10/08 14:45:27
Document what happens when N>M?
Kimmo Kinnunen
2013/10/09 13:39:59
I'll add the docstrings back. See below, though..
| |
206 | |
207 template <int M> | |
208 static GrGLSLExpr<N> mul(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1) ; | |
bsalomon
2013/10/08 14:45:27
comment about what it means to mul, add, sub diffe
Kimmo Kinnunen
2013/10/09 13:39:59
Well, I switched from member operators to private
| |
209 template <int M> | |
210 static GrGLSLExpr<N> add(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1) ; | |
211 template <int M> | |
212 static GrGLSLExpr<N> sub(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1) ; | |
213 | |
214 enum ExprType { | |
215 kZeros_ExprType, | |
216 kOnes_ExprType, | |
217 kFullExpr_ExprType, | |
218 }; | |
219 ExprType fType; | |
220 SkString fExpr; | |
221 | |
222 template <int> friend class GrGLSLExpr; | |
223 | |
224 template <int M> friend GrGLSLExpr<M> operator*(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&); | |
225 template <int M> friend GrGLSLExpr<M> operator+(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&); | |
226 template <int M> friend GrGLSLExpr<M> operator-(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&); | |
227 friend GrGLSLExpr<4> operator*(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); | |
bsalomon
2013/10/08 14:45:27
Should these be limited to 4 for the first input a
Kimmo Kinnunen
2013/10/09 13:39:59
Intention was that once they're used, they're adde
| |
228 friend GrGLSLExpr<4> operator+(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); | |
229 friend GrGLSLExpr<4> operator-(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); | |
230 | |
231 friend GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr); | |
232 friend GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr); | |
233 }; | |
234 | |
235 /** Multiplies two expressions component-wise. */ | |
236 template <int N> | |
237 inline GrGLSLExpr<N> operator*(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1 ) { | |
238 return GrGLSLExpr<N>::mul(in0, in1); | |
239 } | |
240 /** Adds two expressions component-wise. */ | |
241 template <int N> | |
242 inline GrGLSLExpr<N> operator+(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1 ) { | |
243 return GrGLSLExpr<N>::add(in0, in1); | |
244 } | |
245 /** Subtracts two expressions component-wise. */ | |
246 template <int N> | |
247 inline GrGLSLExpr<N> operator-(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1 ) { | |
248 return GrGLSLExpr<N>::sub(in0, in1); | |
249 } | |
250 /** Multiplies every component of an expression with a scalar expression. */ | |
251 inline GrGLSLExpr<4> operator*(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in 1) { | |
252 return GrGLSLExpr<4>::mul(in0, in1); | |
253 } | |
254 /** Adds a scalar expression to every component of an expression. */ | |
255 inline GrGLSLExpr<4> operator+(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in 1) { | |
256 return GrGLSLExpr<4>::add(in0, in1); | |
257 } | |
258 /** Subtracts a scalar expression from every component of an expression. */ | |
259 inline GrGLSLExpr<4> operator-(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in 1) { | |
260 return GrGLSLExpr<4>::sub(in0, in1); | |
261 } | |
262 /** Constructs a vector filled with a and subtracts expression b from it compone nt-wise. | |
263 * For supporting common use-case of (1 - expr). */ | |
264 template<int N> | |
265 inline GrGLSLExpr<N> operator-(int a, const GrGLSLExpr<N>& b) { | |
266 return GrGLSLExpr<N>(a) - b; | |
267 } | |
268 | |
269 /** Casts an vec1 expression to vec4 expresison, eg. vec1(v) -> vec4(v,v,v,v). */ | |
270 GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr); | |
271 | |
272 /** Extracts alpha component from an expression of vec<4>. */ | |
273 GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr); | |
149 | 274 |
150 /** | 275 /** |
151 * Produces a string that is the result of adding two inputs. The inputs must be vecN or | 276 * Does an inplace mul, *=, of vec4VarName by mulFactor. |
152 * float. The result is always a vecN. The inputs may be expressions, not just i dentifier names. | 277 * A semicolon and newline are added after the assignment. |
153 * Either can be NULL or "" in which case the default params control whether a v ector of ones or | |
154 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". No te that when the | |
155 * function determines that the result is a zeros or ones vec then any expressio n represented by | |
156 * or in1 will not be emitted (side effects won't occur). The return value indic ates whether a | |
157 * known zeros or ones vector resulted. The output can be suppressed when known vector is produced | |
158 * by passing true for omitIfConstVec. | |
159 */ | 278 */ |
160 template <int N> | 279 void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt, |
161 GrSLConstantVec GrGLSLAddf(SkString* outAppend, | 280 const char* vec4VarName, const GrGLSLExpr<4>& mulFactor); |
162 const char* in0, | |
163 const char* in1, | |
164 GrSLConstantVec default0 = kZeros_GrSLConstantVec, | |
165 GrSLConstantVec default1 = kZeros_GrSLConstantVec, | |
166 bool omitIfConstVec = false); | |
167 | |
168 /** | |
169 * Produces a string that is the result of subtracting two inputs. The inputs mu st be vecN or | |
170 * float. The result is always a vecN. The inputs may be expressions, not just i dentifier names. | |
171 * Either can be NULL or "" in which case the default params control whether a v ector of ones or | |
172 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". No te that when the | |
173 * function determines that the result is a zeros or ones vec then any expressio n represented by | |
174 * or in1 will not be emitted (side effects won't occur). The return value indic ates whether a | |
175 * known zeros or ones vector resulted. The output can be suppressed when known vector is produced | |
176 * by passing true for omitIfConstVec. | |
177 */ | |
178 template <int N> | |
179 GrSLConstantVec GrGLSLSubtractf(SkString* outAppend, | |
180 const char* in0, | |
181 const char* in1, | |
182 GrSLConstantVec default0 = kZeros_GrSLConstantVe c, | |
183 GrSLConstantVec default1 = kZeros_GrSLConstantVe c, | |
184 bool omitIfConstVec = false); | |
185 | |
186 /** | |
187 * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then | |
188 * mulFactor may be either "" or NULL. In this case either nothing will be appen ded (kOnes) or an | |
189 * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepe nded by tabCnt tabs. | |
190 * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt when we auto-insert | |
191 * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the retu rn value is | |
192 * kZeros, otherwise kNone. | |
193 */ | |
194 GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend, | |
195 int tabCnt, | |
196 const char* vec4VarName, | |
197 const char* mulFactor, | |
198 GrSLConstantVec mulFactorDefault = kOnes_GrSLCo nstantVec); | |
199 | |
200 /** | |
201 * Given an expression that evaluates to a GLSL vec4, extract a component. If ex pr is NULL or "" | |
202 * the value of defaultExpr is used. It is an error to pass an empty expr and ha ve set defaultExpr | |
203 * to kNone. The return value indicates whether the value is known to be 0 or 1. If omitIfConst is | |
204 * set then nothing is appended when the return is not kNone. | |
205 */ | |
206 GrSLConstantVec GrGLSLGetComponent4f(SkString* outAppend, | |
207 const char* expr, | |
208 GrColorComponentFlags component, | |
209 GrSLConstantVec defaultExpr = kNone_GrSLCon stantVec, | |
210 bool omitIfConst = false); | |
211 | 281 |
212 #include "GrGLSL_impl.h" | 282 #include "GrGLSL_impl.h" |
213 | 283 |
214 #endif | 284 #endif |
OLD | NEW |