Index: src/gpu/gl/GrGLSL.h |
diff --git a/src/gpu/gl/GrGLSL.h b/src/gpu/gl/GrGLSL.h |
index 9387d2bbbb3640d0e0de78d7e9ad64620b1c1567..8c5da51bde097386f73da9a8df1f0053755459fe 100644 |
--- a/src/gpu/gl/GrGLSL.h |
+++ b/src/gpu/gl/GrGLSL.h |
@@ -76,34 +76,48 @@ static inline const char* GrGLSLTypeString(GrSLType t) { |
} |
} |
-/** A class representing a GLSL expression. |
+/** A generic base-class representing a GLSL expression. |
* The instance can be a variable name, expression or vecN(0) or vecN(1). Does simple constant |
* folding with help of 1 and 0. |
- * Complex expressions can be constructed with operators *, +, - |
+ * |
+ * Clients should not use this class, rather the specific instantiations defined |
+ * later, for example GrGLSLExpr4. |
*/ |
-template <int N> |
+template <typename Self> |
class GrGLSLExpr { |
public: |
+ bool isOnes() const { return kOnes_ExprType == fType; } |
+ bool isZeros() const { return kZeros_ExprType == fType; } |
+ |
+ const char* c_str() const { |
+ if (kZeros_ExprType == fType) { |
+ return Self::ZerosStr(); |
+ } else if (kOnes_ExprType == fType) { |
+ return Self::OnesStr(); |
+ } |
+ SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used. |
+ return fExpr.c_str(); |
+ } |
+ |
+protected: |
/** Constructs an invalid expression. |
* Useful only as a return value from functions that never actually return |
* this and instances that will be assigned to later. */ |
GrGLSLExpr() |
: fType(kFullExpr_ExprType) { |
- SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range); |
// The only constructor that is allowed to build an empty expression. |
SkASSERT(!this->isValid()); |
} |
/** Constructs an expression with all components as value v */ |
explicit GrGLSLExpr(int v) { |
- SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range); |
if (v == 0) { |
fType = kZeros_ExprType; |
} else if (v == 1) { |
fType = kOnes_ExprType; |
} else { |
fType = kFullExpr_ExprType; |
- fExpr.appendf(CastIntStr(), v); |
+ fExpr.appendf(Self::CastIntStr(), v); |
} |
} |
@@ -111,7 +125,6 @@ public: |
* Argument expr is a simple expression or a parenthesized expression. */ |
// TODO: make explicit once effects input Exprs. |
GrGLSLExpr(const char expr[]) { |
- SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range); |
if (NULL == expr) { // TODO: remove this once effects input Exprs. |
fType = kOnes_ExprType; |
} else { |
@@ -125,7 +138,6 @@ public: |
* Argument expr is a simple expression or a parenthesized expression. */ |
// TODO: make explicit once effects input Exprs. |
GrGLSLExpr(const SkString& expr) { |
- SK_COMPILE_ASSERT(N > 0 && N <= 4, dimensions_not_in_range); |
if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. |
fType = kOnes_ExprType; |
} else { |
@@ -135,77 +147,61 @@ public: |
SkASSERT(this->isValid()); |
} |
- bool isOnes() const { return kOnes_ExprType == fType; } |
- bool isZeros() const { return kZeros_ExprType == fType; } |
- |
- const char* c_str() const { |
- if (kZeros_ExprType == fType) { |
- return ZerosStr(); |
- } else if (kOnes_ExprType == fType) { |
- return OnesStr(); |
- } |
- SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used. |
- return fExpr.c_str(); |
- } |
- |
-private: |
+ /** Constructs an expression from a string with one substitution. */ |
GrGLSLExpr(const char format[], const char in0[]) |
: fType(kFullExpr_ExprType) { |
fExpr.appendf(format, in0); |
} |
+ /** Constructs an expression from a string with two substitutions. */ |
GrGLSLExpr(const char format[], const char in0[], const char in1[]) |
: fType(kFullExpr_ExprType) { |
fExpr.appendf(format, in0, in1); |
} |
- GrGLSLExpr(const char format[], const char in0[], char in1) |
- : fType(kFullExpr_ExprType) { |
- fExpr.appendf(format, in0, in1); |
- } |
- |
bool isValid() const { |
return kFullExpr_ExprType != fType || !fExpr.isEmpty(); |
} |
- static const char* ZerosStr(); |
- static const char* OnesStr(); |
- static const char* ExtractAlphaStr(); |
- static const char* CastStr(); |
- static const char* CastIntStr(); |
- |
- /** Casts the expression expr into smaller or bigger expression. |
- * Casting is done with GLSL rules: |
- * M==3, N==4 vec3(a, b, c) -> vec4(a, b, c, 0) |
- * N==4, M==3 vec4(a, b, c, d) -> vec3(a, b, c) |
+ /** Returns expression casted to another type. |
+ * Generic implementation that is called for non-trivial cases of casts. */ |
+ template <typename T> |
+ static Self VectorCastImpl(const T& other); |
+ |
+ /** Returns a GLSL multiplication: component-wise or component-by-scalar. |
+ * The multiplication will be component-wise or multiply each component by a scalar. |
+ * |
+ * The returned expression will compute the value of: |
+ * vecN(in0.x * in1.x, ...) if dim(T0) == dim(T1) (component-wise) |
+ * vecN(in0.x * in1, ...) if dim(T1) == 1 (vector by scalar) |
+ * vecN(in0 * in1.x, ...) if dim(T0) == 1 (scalar by vector) |
*/ |
- template <int M> |
- static GrGLSLExpr<N> VectorCast(const GrGLSLExpr<M>& expr); |
+ template <typename T0, typename T1> |
+ static Self Mul(T0 in0, T1 in1); |
- /** GLSL multiplication: component-wise or multiply each component by a scalar. |
- * M == N --> vecN(in0.x * in1.x, ...) |
- * M == 1 --> vecN(in0.x * in1, ...) |
- * otherwise --> compile-time error |
+ /** Returns a GLSL addition: component-wise or add a scalar to each component. |
+ * Return value computes: |
+ * vecN(in0.x + in1.x, ...) or vecN(in0.x + in1, ...) or vecN(in0 + in1.x, ...). |
*/ |
- template <int M> |
- static GrGLSLExpr<N> Mul(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1); |
+ template <typename T0, typename T1> |
+ static Self Add(T0 in0, T1 in1); |
- /** GLSL addition: component-wise or add a scalar to each compoment. |
- * M == N --> vecN(in0.x + in1.x, ...) |
- * M == 1 --> vecN(in0.x + in1, ...) |
- * otherwise --> compile-time error |
+ /** Returns a GLSL subtraction: component-wise or subtract compoments by a scalar. |
+ * Return value computes |
+ * vecN(in0.x - in1.x, ...) or vecN(in0.x - in1, ...) or vecN(in0 - in1.x, ...). |
*/ |
- template <int M> |
- static GrGLSLExpr<N> Add(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1); |
+ template <typename T0, typename T1> |
+ static Self Sub(T0 in0, T1 in1); |
- /** GLSL subtraction: component-wise or subtract compoments by a scalar. |
- * M == N --> vecN(in0.x - in1.x, ...) |
- * M == 1 --> vecN(in0.x - in1, ...) |
- * otherwise --> compile-time error |
+ /** Returns expression that accesses component(s) of the expression. |
+ * format should be the form "%s.x" where 'x' is the component(s) to access. |
+ * Caller is responsible for making sure the amount of components in the |
+ * format string is equal to dim(T). |
*/ |
- template <int M> |
- static GrGLSLExpr<N> Sub(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1); |
+ template <typename T> |
+ T extractComponents(const char format[]) const; |
+private: |
enum ExprType { |
kZeros_ExprType, |
kOnes_ExprType, |
@@ -213,66 +209,112 @@ private: |
}; |
ExprType fType; |
SkString fExpr; |
+}; |
- template <int> friend class GrGLSLExpr; |
- |
- /** Multiplies two expressions component-wise. */ |
- template <int M> friend GrGLSLExpr<M> operator*(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&); |
- /** Adds two expressions component-wise. */ |
- template <int M> friend GrGLSLExpr<M> operator+(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&); |
- /** Subtracts two expressions component-wise. */ |
- template <int M> friend GrGLSLExpr<M> operator-(const GrGLSLExpr<M>&, const GrGLSLExpr<M>&); |
- /** Multiplies every component of an expression with a scalar expression. */ |
- friend GrGLSLExpr<4> operator*(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); |
- /** Adds a scalar expression to every component of an expression. */ |
- friend GrGLSLExpr<4> operator+(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); |
- /** Subtracts a scalar expression from every component of an expression. */ |
- friend GrGLSLExpr<4> operator-(const GrGLSLExpr<4>&, const GrGLSLExpr<1>&); |
- |
- friend GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr); |
- friend GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr); |
+class GrGLSLExpr1; |
+class GrGLSLExpr4; |
+ |
+/** Class representing a float GLSL expression. */ |
+class GrGLSLExpr1 : public GrGLSLExpr<GrGLSLExpr1> { |
+public: |
+ GrGLSLExpr1() |
+ : INHERITED() { |
+ } |
+ explicit GrGLSLExpr1(int v) |
+ : INHERITED(v) { |
+ } |
+ GrGLSLExpr1(const char* expr) |
+ : INHERITED(expr) { |
+ } |
+ GrGLSLExpr1(const SkString& expr) |
+ : INHERITED(expr) { |
+ } |
+ |
+ static GrGLSLExpr1 VectorCast(const GrGLSLExpr1& expr); |
+ |
+private: |
+ GrGLSLExpr1(const char format[], const char in0[]) |
+ : INHERITED(format, in0) { |
+ } |
+ GrGLSLExpr1(const char format[], const char in0[], const char in1[]) |
+ : INHERITED(format, in0, in1) { |
+ } |
+ |
+ static const char* ZerosStr(); |
+ static const char* OnesStr(); |
+ static const char* CastStr(); |
+ static const char* CastIntStr(); |
+ |
+ friend GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
+ friend GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
+ friend GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1&in1); |
+ |
+ friend class GrGLSLExpr<GrGLSLExpr1>; |
+ friend class GrGLSLExpr<GrGLSLExpr4>; |
+ |
+ typedef GrGLSLExpr<GrGLSLExpr1> INHERITED; |
}; |
+/** Class representing a float vector (vec4) GLSL expression. */ |
+class GrGLSLExpr4 : public GrGLSLExpr<GrGLSLExpr4> { |
+public: |
+ GrGLSLExpr4() |
+ : INHERITED() { |
+ } |
+ explicit GrGLSLExpr4(int v) |
+ : INHERITED(v) { |
+ } |
+ GrGLSLExpr4(const char* expr) |
+ : INHERITED(expr) { |
+ } |
+ GrGLSLExpr4(const SkString& expr) |
+ : INHERITED(expr) { |
+ } |
-template <int N> |
-inline GrGLSLExpr<N> operator*(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1) { |
- return GrGLSLExpr<N>::Mul(in0, in1); |
-} |
+ typedef GrGLSLExpr1 AExpr; |
+ AExpr a() const; |
-template <int N> |
-inline GrGLSLExpr<N> operator+(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1) { |
- return GrGLSLExpr<N>::Add(in0, in1); |
-} |
+ /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */ |
+ static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr); |
+ static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr); |
-template <int N> |
-inline GrGLSLExpr<N> operator-(const GrGLSLExpr<N>& in0, const GrGLSLExpr<N>&in1) { |
- return GrGLSLExpr<N>::Sub(in0, in1); |
-} |
+private: |
+ GrGLSLExpr4(const char format[], const char in0[]) |
+ : INHERITED(format, in0) { |
+ } |
+ GrGLSLExpr4(const char format[], const char in0[], const char in1[]) |
+ : INHERITED(format, in0, in1) { |
+ } |
-inline GrGLSLExpr<4> operator*(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in1) { |
- return GrGLSLExpr<4>::Mul(in0, in1); |
-} |
+ static const char* ZerosStr(); |
+ static const char* OnesStr(); |
+ static const char* CastStr(); |
+ static const char* CastIntStr(); |
-inline GrGLSLExpr<4> operator+(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in1) { |
- return GrGLSLExpr<4>::Add(in0, in1); |
-} |
+ // The vector-by-scalar and scalar-by-vector binary operations. |
+ friend GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); |
+ friend GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); |
+ friend GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4&in1); |
+ friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); |
+ friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); |
+ friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1&in1); |
-inline GrGLSLExpr<4> operator-(const GrGLSLExpr<4>& in0, const GrGLSLExpr<1>& in1) { |
- return GrGLSLExpr<4>::Sub(in0, in1); |
-} |
+ // The vector-by-vector, i.e. component-wise, binary operations. |
+ friend GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); |
+ friend GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); |
+ friend GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4&in1); |
-/** Casts an vec1 expression to vec4 expresison, eg. vec1(v) -> vec4(v,v,v,v). */ |
-GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr); |
+ friend class GrGLSLExpr<GrGLSLExpr4>; |
-/** Extracts alpha component from an expression of vec<4>. */ |
-GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr); |
+ typedef GrGLSLExpr<GrGLSLExpr4> INHERITED; |
+}; |
/** |
* Does an inplace mul, *=, of vec4VarName by mulFactor. |
* A semicolon and newline are added after the assignment. |
*/ |
void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt, |
- const char* vec4VarName, const GrGLSLExpr<4>& mulFactor); |
+ const char* vec4VarName, const GrGLSLExpr4& mulFactor); |
#include "GrGLSL_impl.h" |