Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: src/gpu/gl/GrGLSL.h

Issue 25048002: Express (GLSL expression, possibly known value) pairs as a class (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: rebase Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 inline const char* GrGLSLTypeString(GrSLType t) {
bsalomon 2013/10/02 13:20:26 probably want both static and inline here, right?
Kimmo Kinnunen 2013/10/08 12:18:29 Well, maybe from standpoint of being consistent wi
bsalomon 2013/10/08 14:45:27 If it's dead let's remove it.
Kimmo Kinnunen 2013/10/09 13:39:59 Sorry, mistook it for the other dead code. I remov
bsalomon 2013/10/09 19:53:31 Hmm.. Perhaps not. Though, if the function really
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 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 */
136 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". N ote that when the
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> 112 template <int N>
143 GrSLConstantVec GrGLSLModulatef(SkString* outAppend, 113 class GrGLSLExpr {
144 const char* in0, 114 public:
145 const char* in1, 115 GrGLSLExpr()
146 GrSLConstantVec default0 = kOnes_GrSLConstantVec , 116 : fType(kFullExpr_ExprType) {
147 GrSLConstantVec default1 = kOnes_GrSLConstantVec , 117 // The only constructor that is allowed to build an empty expression.
148 bool omitIfConstVec = false); 118 SkASSERT(!isValid());
bsalomon 2013/10/02 13:20:26 this->isValid() several more below
Kimmo Kinnunen 2013/10/08 12:18:29 Done.
119 }
120
121 explicit GrGLSLExpr(int v) {
bsalomon 2013/10/02 13:20:26 Comment that this replicates the int to all compon
Kimmo Kinnunen 2013/10/08 12:18:29 Done.
122 if (v == 0) {
123 fType = kZeros_ExprType;
124 } else if (v == 1) {
125 fType = kOnes_ExprType;
126 } else {
127 fType = kFullExpr_ExprType;
128 fExpr.appendf(castIntStr(), v);
bsalomon 2013/10/02 13:20:26 this->castIntStr()
Kimmo Kinnunen 2013/10/08 12:18:29 Done.
129 }
130 }
131
132 // Argument expr is a simple expression or a parenthesized expression.
133 GrGLSLExpr(const char expr[]) {
134 if (NULL == expr) { // TODO: remove this once effects input Exprs.
135 fType = kOnes_ExprType;
136 } else {
137 fType = kFullExpr_ExprType;
138 fExpr = expr;
139 }
140 SkASSERT(isValid());
141 }
142
143 // Argument expr is a simple expression or a parenthesized expression.
144 GrGLSLExpr(const SkString& expr) {
145 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
146 fType = kOnes_ExprType;
147 } else {
148 fType = kFullExpr_ExprType;
149 fExpr = expr;
150 }
151 SkASSERT(isValid());
152 }
153
154 /** Given an expression of vecM, cast it to vecN. */
bsalomon 2013/10/02 13:20:26 Document what happens if N > M?
Kimmo Kinnunen 2013/10/08 12:18:29 ...
155 template<int M>
156 static GrGLSLExpr<N> VectorCast(const GrGLSLExpr<M>&);
157
158 bool isOnes() const { return fType == kOnes_ExprType; }
159 bool isZeros() const { return fType == kZeros_ExprType; }
160
161 template<int M>
bsalomon 2013/10/02 13:20:26 Maybe a comment here about how a vecN and vecM are
Kimmo Kinnunen 2013/10/08 12:18:29 .. I see what you mean, good point. Changed the in
162 GrGLSLExpr<N> operator*(const GrGLSLExpr<M>& other) const;
163 template<int M>
164 GrGLSLExpr<N> operator+(const GrGLSLExpr<M>& other) const;
165 template<int M>
166 GrGLSLExpr<N> operator-(const GrGLSLExpr<M>& other) const;
167
168 /**
bsalomon 2013/10/02 15:18:04 Maybe we ought to go ahead and have r() g() and b(
Kimmo Kinnunen 2013/10/08 12:18:29 Well, they'd be dead code... I didn't add them y
bsalomon 2013/10/08 14:45:27 No, I just assume if we start really using this a
169 * Given an expression that evaluates to a GLSL vecN, extract alpha
170 * component. Valid only for expressions of dimensions 4.
171 */
172 GrGLSLExpr<1> a() const;
173
174 const char* c_str() const {
175 if (kZeros_ExprType == fType) {
176 return this->zerosStr();
177 } else if (kOnes_ExprType == fType) {
178 return this->onesStr();
179 }
180 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
181 return fExpr.c_str();
182 }
183
184 private:
185 GrGLSLExpr(const char format[], const char in0[])
186 : fType(kFullExpr_ExprType) {
187 fExpr.appendf(format, in0);
188 }
189
190 GrGLSLExpr(const char format[], const char in0[], const char in1[])
191 : fType(kFullExpr_ExprType) {
192 fExpr.appendf(format, in0, in1);
193 }
194
195 GrGLSLExpr(const char format[], const char in0[], const char in1)
196 : fType(kFullExpr_ExprType) {
197 fExpr.appendf(format, in0, in1);
198 }
199
200 bool isValid() const {
201 return kFullExpr_ExprType != fType || !fExpr.isEmpty();
202 }
203
204 static const char* zerosStr();
205 static const char* onesStr();
206 static const char* extractAlphaStr();
207 static const char* castStr();
208 static const char* castIntStr();
209
210 SkString fExpr;
211 enum ExprType {
212 kZeros_ExprType,
213 kOnes_ExprType,
214 kFullExpr_ExprType,
215 };
216 ExprType fType;
217
218 template <int> friend class GrGLSLExpr;
219 };
220
221 // Support for common case of (1 - expr.a()).
222 template<int N>
223 GrGLSLExpr<N> operator-(int a, const GrGLSLExpr<N>& b) {
224 return GrGLSLExpr<N>(a) - b;
225 }
149 226
150 /** 227 /**
151 * Produces a string that is the result of adding two inputs. The inputs must be vecN or 228 * 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. 229 * 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 */ 230 */
160 template <int N> 231 void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt,
161 GrSLConstantVec GrGLSLAddf(SkString* outAppend, 232 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 233
212 #include "GrGLSL_impl.h" 234 #include "GrGLSL_impl.h"
213 235
214 #endif 236 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698