| 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 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 case kMat44f_GrSLType: | 69 case kMat44f_GrSLType: |
| 70 return "mat4"; | 70 return "mat4"; |
| 71 case kSampler2D_GrSLType: | 71 case kSampler2D_GrSLType: |
| 72 return "sampler2D"; | 72 return "sampler2D"; |
| 73 default: | 73 default: |
| 74 GrCrash("Unknown shader var type."); | 74 GrCrash("Unknown shader var type."); |
| 75 return ""; // suppress warning | 75 return ""; // suppress warning |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 /** A class representing a GLSL expression. | 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 s
imple constant | 80 * The instance can be a variable name, expression or vecN(0) or vecN(1). Does s
imple constant |
| 81 * folding with help of 1 and 0. | 81 * folding with help of 1 and 0. |
| 82 * Complex expressions can be constructed with operators *, +, - | 82 * |
| 83 * Clients should not use this class, rather the specific instantiations defined |
| 84 * later, for example GrGLSLExpr4. |
| 83 */ | 85 */ |
| 84 template <int N> | 86 template <typename Self> |
| 85 class GrGLSLExpr { | 87 class GrGLSLExpr { |
| 86 public: | 88 public: |
| 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 |
| 102 protected: |
| 87 /** Constructs an invalid expression. | 103 /** Constructs an invalid expression. |
| 88 * Useful only as a return value from functions that never actually return | 104 * Useful only as a return value from functions that never actually return |
| 89 * this and instances that will be assigned to later. */ | 105 * this and instances that will be assigned to later. */ |
| 90 GrGLSLExpr() | 106 GrGLSLExpr() |
| 91 : fType(kFullExpr_ExprType) { | 107 : fType(kFullExpr_ExprType) { |
| 92 SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range); | |
| 93 // The only constructor that is allowed to build an empty expression. | 108 // The only constructor that is allowed to build an empty expression. |
| 94 SkASSERT(!this->isValid()); | 109 SkASSERT(!this->isValid()); |
| 95 } | 110 } |
| 96 | 111 |
| 97 /** Constructs an expression with all components as value v */ | 112 /** Constructs an expression with all components as value v */ |
| 98 explicit GrGLSLExpr(int v) { | 113 explicit GrGLSLExpr(int v) { |
| 99 SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range); | |
| 100 if (v == 0) { | 114 if (v == 0) { |
| 101 fType = kZeros_ExprType; | 115 fType = kZeros_ExprType; |
| 102 } else if (v == 1) { | 116 } else if (v == 1) { |
| 103 fType = kOnes_ExprType; | 117 fType = kOnes_ExprType; |
| 104 } else { | 118 } else { |
| 105 fType = kFullExpr_ExprType; | 119 fType = kFullExpr_ExprType; |
| 106 fExpr.appendf(CastIntStr(), v); | 120 fExpr.appendf(Self::CastIntStr(), v); |
| 107 } | 121 } |
| 108 } | 122 } |
| 109 | 123 |
| 110 /** Constructs an expression from a string. | 124 /** Constructs an expression from a string. |
| 111 * Argument expr is a simple expression or a parenthesized expression. */ | 125 * Argument expr is a simple expression or a parenthesized expression. */ |
| 112 // TODO: make explicit once effects input Exprs. | 126 // TODO: make explicit once effects input Exprs. |
| 113 GrGLSLExpr(const char expr[]) { | 127 GrGLSLExpr(const char expr[]) { |
| 114 SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range); | |
| 115 if (NULL == expr) { // TODO: remove this once effects input Exprs. | 128 if (NULL == expr) { // TODO: remove this once effects input Exprs. |
| 116 fType = kOnes_ExprType; | 129 fType = kOnes_ExprType; |
| 117 } else { | 130 } else { |
| 118 fType = kFullExpr_ExprType; | 131 fType = kFullExpr_ExprType; |
| 119 fExpr = expr; | 132 fExpr = expr; |
| 120 } | 133 } |
| 121 SkASSERT(this->isValid()); | 134 SkASSERT(this->isValid()); |
| 122 } | 135 } |
| 123 | 136 |
| 124 /** Constructs an expression from a string. | 137 /** Constructs an expression from a string. |
| 125 * Argument expr is a simple expression or a parenthesized expression. */ | 138 * Argument expr is a simple expression or a parenthesized expression. */ |
| 126 // TODO: make explicit once effects input Exprs. | 139 // TODO: make explicit once effects input Exprs. |
| 127 GrGLSLExpr(const SkString& expr) { | 140 GrGLSLExpr(const SkString& expr) { |
| 128 SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range); | |
| 129 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. | 141 if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. |
| 130 fType = kOnes_ExprType; | 142 fType = kOnes_ExprType; |
| 131 } else { | 143 } else { |
| 132 fType = kFullExpr_ExprType; | 144 fType = kFullExpr_ExprType; |
| 133 fExpr = expr; | 145 fExpr = expr; |
| 134 } | 146 } |
| 135 SkASSERT(this->isValid()); | 147 SkASSERT(this->isValid()); |
| 136 } | 148 } |
| 137 | 149 |
| 138 bool isOnes() const { return kOnes_ExprType == fType; } | 150 /** Constructs an expression from a string with one substitution. */ |
| 139 bool isZeros() const { return kZeros_ExprType == fType; } | |
| 140 | |
| 141 const char* c_str() const { | |
| 142 if (kZeros_ExprType == fType) { | |
| 143 return ZerosStr(); | |
| 144 } else if (kOnes_ExprType == fType) { | |
| 145 return OnesStr(); | |
| 146 } | |
| 147 SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used. | |
| 148 return fExpr.c_str(); | |
| 149 } | |
| 150 | |
| 151 private: | |
| 152 GrGLSLExpr(const char format[], const char in0[]) | 151 GrGLSLExpr(const char format[], const char in0[]) |
| 153 : fType(kFullExpr_ExprType) { | 152 : fType(kFullExpr_ExprType) { |
| 154 fExpr.appendf(format, in0); | 153 fExpr.appendf(format, in0); |
| 155 } | 154 } |
| 156 | 155 |
| 156 /** Constructs an expression from a string with two substitutions. */ |
| 157 GrGLSLExpr(const char format[], const char in0[], const char in1[]) | 157 GrGLSLExpr(const char format[], const char in0[], const char in1[]) |
| 158 : fType(kFullExpr_ExprType) { | 158 : fType(kFullExpr_ExprType) { |
| 159 fExpr.appendf(format, in0, in1); | 159 fExpr.appendf(format, in0, in1); |
| 160 } | 160 } |
| 161 | 161 |
| 162 GrGLSLExpr(const char format[], const char in0[], char in1) | |
| 163 : fType(kFullExpr_ExprType) { | |
| 164 fExpr.appendf(format, in0, in1); | |
| 165 } | |
| 166 | |
| 167 bool isValid() const { | 162 bool isValid() const { |
| 168 return kFullExpr_ExprType != fType || !fExpr.isEmpty(); | 163 return kFullExpr_ExprType != fType || !fExpr.isEmpty(); |
| 169 } | 164 } |
| 170 | 165 |
| 171 static const char* ZerosStr(); | 166 /** Returns expression casted to another type. |
| 172 static const char* OnesStr(); | 167 * Generic implementation that is called for non-trivial cases of casts. */ |
| 173 static const char* ExtractAlphaStr(); | 168 template <typename T> |
| 174 static const char* CastStr(); | 169 static Self VectorCastImpl(const T& other); |
| 175 static const char* CastIntStr(); | |
| 176 | 170 |
| 177 /** Casts the expression expr into smaller or bigger expression. | 171 /** Returns a GLSL multiplication: component-wise or component-by-scalar. |
| 178 * Casting is done with GLSL rules: | 172 * The multiplication will be component-wise or multiply each component by a
scalar. |
| 179 * M==3, N==4 vec3(a, b, c) -> vec4(a, b, c, 0) | 173 * |
| 180 * N==4, M==3 vec4(a, b, c, d) -> vec3(a, b, c) | 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) |
| 181 */ | 178 */ |
| 182 template <int M> | 179 template <typename T0, typename T1> |
| 183 static GrGLSLExpr<N> VectorCast(const GrGLSLExpr<M>& expr); | 180 static Self Mul(T0 in0, T1 in1); |
| 184 | 181 |
| 185 /** GLSL multiplication: component-wise or multiply each component by a scal
ar. | 182 /** Returns a GLSL addition: component-wise or add a scalar to each componen
t. |
| 186 * M == N --> vecN(in0.x * in1.x, ...) | 183 * Return value computes: |
| 187 * M == 1 --> vecN(in0.x * in1, ...) | 184 * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x,
...). |
| 188 * otherwise --> compile-time error | |
| 189 */ | 185 */ |
| 190 template <int M> | 186 template <typename T0, typename T1> |
| 191 static GrGLSLExpr<N> Mul(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1)
; | 187 static Self Add(T0 in0, T1 in1); |
| 192 | 188 |
| 193 /** GLSL addition: component-wise or add a scalar to each compoment. | 189 /** Returns a GLSL subtraction: component-wise or subtract compoments by a s
calar. |
| 194 * M == N --> vecN(in0.x + in1.x, ...) | 190 * Return value computes |
| 195 * M == 1 --> vecN(in0.x + in1, ...) | 191 * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x,
...). |
| 196 * otherwise --> compile-time error | |
| 197 */ | 192 */ |
| 198 template <int M> | 193 template <typename T0, typename T1> |
| 199 static GrGLSLExpr<N> Add(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1)
; | 194 static Self Sub(T0 in0, T1 in1); |
| 200 | 195 |
| 201 /** GLSL subtraction: component-wise or subtract compoments by a scalar. | 196 /** Returns expression that accesses component(s) of the expression. |
| 202 * M == N --> vecN(in0.x - in1.x, ...) | 197 * format should be the form "%s.x" where 'x' is the component(s) to access. |
| 203 * M == 1 --> vecN(in0.x - in1, ...) | 198 * Caller is responsible for making sure the amount of components in the |
| 204 * otherwise --> compile-time error | 199 * format string is equal to dim(T). |
| 205 */ | 200 */ |
| 206 template <int M> | 201 template <typename T> |
| 207 static GrGLSLExpr<N> Sub(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1)
; | 202 T extractComponents(const char format[]) const; |
| 208 | 203 |
| 204 private: |
| 209 enum ExprType { | 205 enum ExprType { |
| 210 kZeros_ExprType, | 206 kZeros_ExprType, |
| 211 kOnes_ExprType, | 207 kOnes_ExprType, |
| 212 kFullExpr_ExprType, | 208 kFullExpr_ExprType, |
| 213 }; | 209 }; |
| 214 ExprType fType; | 210 ExprType fType; |
| 215 SkString fExpr; | 211 SkString fExpr; |
| 216 | |
| 217 template <int> friend class GrGLSLExpr; | |
| 218 | |
| 219 /** Multiplies two expressions component-wise. */ | |
| 220 template <int M> friend GrGLSLExpr<M> operator*(const GrGLSLExpr<M>&, const
GrGLSLExpr<M>&); | |
| 221 /** Adds two expressions component-wise. */ | |
| 222 template <int M> friend GrGLSLExpr<M> operator+(const GrGLSLExpr<M>&, const
GrGLSLExpr<M>&); | |
| 223 /** Subtracts two expressions component-wise. */ | |
| 224 template <int M> friend GrGLSLExpr<M> operator-(const GrGLSLExpr<M>&, const
GrGLSLExpr<M>&); | |
| 225 /** Multiplies every component of an expression with a scalar expression. */ | |
| 226 friend GrGLSLExpr<4> operator*(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); | |
| 227 /** Adds a scalar expression to every component of an expression. */ | |
| 228 friend GrGLSLExpr<4> operator+(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); | |
| 229 /** Subtracts a scalar expression from every component of an expression. */ | |
| 230 friend GrGLSLExpr<4> operator-(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); | |
| 231 | |
| 232 friend GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr); | |
| 233 friend GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr); | |
| 234 }; | 212 }; |
| 235 | 213 |
| 214 class GrGLSLExpr1; |
| 215 class GrGLSLExpr4; |
| 236 | 216 |
| 237 template <int N> | 217 /** Class representing a float GLSL expression. */ |
| 238 inline GrGLSLExpr<N> operator*(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1
) { | 218 class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> { |
| 239 return GrGLSLExpr<N>::Mul(in0, in1); | 219 public: |
| 240 } | 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 } |
| 241 | 232 |
| 242 template <int N> | 233 static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr); |
| 243 inline GrGLSLExpr<N> operator+(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1
) { | |
| 244 return GrGLSLExpr<N>::Add(in0, in1); | |
| 245 } | |
| 246 | 234 |
| 247 template <int N> | 235 private: |
| 248 inline GrGLSLExpr<N> operator-(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1
) { | 236 GrGLSLExpr1(const char format[], const char in0[]) |
| 249 return GrGLSLExpr<N>::Sub(in0, in1); | 237 : INHERITED(format, in0) { |
| 250 } | 238 } |
| 239 GrGLSLExpr1(const char format[], const char in0[], const char in1[]) |
| 240 : INHERITED(format, in0, in1) { |
| 241 } |
| 251 | 242 |
| 252 inline GrGLSLExpr<4> operator*(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in
1) { | 243 static const char* ZerosStr(); |
| 253 return GrGLSLExpr<4>::Mul(in0, in1); | 244 static const char* OnesStr(); |
| 254 } | 245 static const char* CastStr(); |
| 246 static const char* CastIntStr(); |
| 255 | 247 |
| 256 inline GrGLSLExpr<4> operator+(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in
1) { | 248 friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
| 257 return GrGLSLExpr<4>::Add(in0, in1); | 249 friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
| 258 } | 250 friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
| 259 | 251 |
| 260 inline GrGLSLExpr<4> operator-(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in
1) { | 252 friend class GrGLSLExpr<GrGLSLExpr1>; |
| 261 return GrGLSLExpr<4>::Sub(in0, in1); | 253 friend class GrGLSLExpr<GrGLSLExpr4>; |
| 262 } | |
| 263 | 254 |
| 264 /** Casts an vec1 expression to vec4 expresison, eg. vec1(v) -> vec4(v,v,v,v).
*/ | 255 typedef GrGLSLExpr<GrGLSLExpr1> INHERITED; |
| 265 GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr); | 256 }; |
| 266 | 257 |
| 267 /** Extracts alpha component from an expression of vec<4>. */ | 258 /** Class representing a float vector (vec4) GLSL expression. */ |
| 268 GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr); | 259 class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> { |
| 260 public: |
| 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, fl
oatv, floatv) */ |
| 278 static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr); |
| 279 static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr); |
| 280 |
| 281 private: |
| 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 }; |
| 269 | 311 |
| 270 /** | 312 /** |
| 271 * Does an inplace mul, *=, of vec4VarName by mulFactor. | 313 * Does an inplace mul, *=, of vec4VarName by mulFactor. |
| 272 * A semicolon and newline are added after the assignment. | 314 * A semicolon and newline are added after the assignment. |
| 273 */ | 315 */ |
| 274 void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt, | 316 void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt, |
| 275 const char* vec4VarName, const GrGLSLExpr<4>& mulFactor); | 317 const char* vec4VarName, const GrGLSLExpr4& mulFactor); |
| 276 | 318 |
| 277 #include "GrGLSL_impl.h" | 319 #include "GrGLSL_impl.h" |
| 278 | 320 |
| 279 #endif | 321 #endif |
| OLD | NEW |