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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: src/gpu/gl/GrGLSL.h
diff --git a/src/gpu/gl/GrGLSL.h b/src/gpu/gl/GrGLSL.h
index b97e709aca39001b43757e0a1834e3ba9e856ac1..b48cecaef35f1f8e272ebd7decfbd1171a8b4f6e 100644
--- a/src/gpu/gl/GrGLSL.h
+++ b/src/gpu/gl/GrGLSL.h
@@ -11,10 +11,10 @@
#include "gl/GrGLInterface.h"
#include "GrColor.h"
#include "GrTypesPriv.h"
+#include "SkString.h"
class GrGLContextInfo;
class GrGLShaderVar;
-class SkString;
// Limited set of GLSL versions we build shaders for. Caller should round
// down the GLSL version to one of these enums.
@@ -37,12 +37,6 @@ enum GrGLSLGeneration {
k150_GrGLSLGeneration,
};
-enum GrSLConstantVec {
- kZeros_GrSLConstantVec,
- kOnes_GrSLConstantVec,
- kNone_GrSLConstantVec,
-};
-
namespace {
static inline int GrSLTypeToVecLength(GrSLType type) {
static const int kVecLengths[] = {
@@ -59,20 +53,8 @@ static inline int GrSLTypeToVecLength(GrSLType type) {
return kVecLengths[type];
}
-static inline const char* GrGLSLOnesVecf(int count) {
- static const char* kONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
- "vec3(1,1,1)", "vec4(1,1,1,1)"};
- SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kONESVEC));
- return kONESVEC[count];
}
-static inline const char* GrGLSLZerosVecf(int count) {
- static const char* kZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
- "vec3(0,0,0)", "vec4(0,0,0,0)"};
- SkASSERT(count >= 1 && count < (int)GR_ARRAY_COUNT(kZEROSVEC));
- return kZEROSVEC[count];
-}
-}
/**
* Gets the most recent GLSL Generation compatible with the OpenGL context.
@@ -89,7 +71,7 @@ const char* GrGetGLSLVersionDecl(const GrGLContextInfo&);
/**
* Converts a GrSLType to a string containing the name of the equivalent GLSL type.
*/
-static const char* GrGLSLTypeString(GrSLType t) {
+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
switch (t) {
case kVoid_GrSLType:
return "void";
@@ -113,12 +95,6 @@ static const char* GrGLSLTypeString(GrSLType t) {
}
}
-/** Return the type enum for a vector of floats of length n (1..4),
- e.g. 1 -> "float", 2 -> "vec2", ... */
-static inline const char* GrGLSLFloatVectorTypeString(int n) {
- return GrGLSLTypeString(GrSLFloatVectorType(n));
-}
-
/** Return the GLSL swizzle operator for a homogenous component of a vector
with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */
const char* GrGLSLVectorHomogCoord(int count);
@@ -129,85 +105,131 @@ const char* GrGLSLVectorHomogCoord(GrSLType type);
const char* GrGLSLVectorNonhomogCoords(int count);
const char* GrGLSLVectorNonhomogCoords(GrSLType type);
-/**
- * Produces a string that is the result of modulating two inputs. The inputs must be vecN or
- * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
- * Either can be NULL or "" in which case the default params control whether a vector of ones or
- * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
- * function determines that the result is a zeros or ones vec then any expression represented by
- * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
- * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
- * by passing true for omitIfConstVec.
- */
-template <int N>
-GrSLConstantVec GrGLSLModulatef(SkString* outAppend,
- const char* in0,
- const char* in1,
- GrSLConstantVec default0 = kOnes_GrSLConstantVec,
- GrSLConstantVec default1 = kOnes_GrSLConstantVec,
- bool omitIfConstVec = false);
-
-/**
- * Produces a string that is the result of adding two inputs. The inputs must be vecN or
- * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
- * Either can be NULL or "" in which case the default params control whether a vector of ones or
- * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
- * function determines that the result is a zeros or ones vec then any expression represented by
- * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
- * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
- * by passing true for omitIfConstVec.
+/** A class representing 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.
*/
template <int N>
-GrSLConstantVec GrGLSLAddf(SkString* outAppend,
- const char* in0,
- const char* in1,
- GrSLConstantVec default0 = kZeros_GrSLConstantVec,
- GrSLConstantVec default1 = kZeros_GrSLConstantVec,
- bool omitIfConstVec = false);
+class GrGLSLExpr {
+public:
+ GrGLSLExpr()
+ : fType(kFullExpr_ExprType) {
+ // The only constructor that is allowed to build an empty expression.
+ SkASSERT(!isValid());
bsalomon 2013/10/02 13:20:26 this->isValid() several more below
Kimmo Kinnunen 2013/10/08 12:18:29 Done.
+ }
-/**
- * Produces a string that is the result of subtracting two inputs. The inputs must be vecN or
- * float. The result is always a vecN. The inputs may be expressions, not just identifier names.
- * Either can be NULL or "" in which case the default params control whether a vector of ones or
- * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". Note that when the
- * function determines that the result is a zeros or ones vec then any expression represented by
- * or in1 will not be emitted (side effects won't occur). The return value indicates whether a
- * known zeros or ones vector resulted. The output can be suppressed when known vector is produced
- * by passing true for omitIfConstVec.
- */
-template <int N>
-GrSLConstantVec GrGLSLSubtractf(SkString* outAppend,
- const char* in0,
- const char* in1,
- GrSLConstantVec default0 = kZeros_GrSLConstantVec,
- GrSLConstantVec default1 = kZeros_GrSLConstantVec,
- bool omitIfConstVec = false);
+ 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.
+ if (v == 0) {
+ fType = kZeros_ExprType;
+ } else if (v == 1) {
+ fType = kOnes_ExprType;
+ } else {
+ fType = kFullExpr_ExprType;
+ fExpr.appendf(castIntStr(), v);
bsalomon 2013/10/02 13:20:26 this->castIntStr()
Kimmo Kinnunen 2013/10/08 12:18:29 Done.
+ }
+ }
-/**
- * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is not kNone then
- * mulFactor may be either "" or NULL. In this case either nothing will be appended (kOnes) or an
- * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepended by tabCnt tabs.
- * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt when we auto-insert
- * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the return value is
- * kZeros, otherwise kNone.
- */
-GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend,
- int tabCnt,
- const char* vec4VarName,
- const char* mulFactor,
- GrSLConstantVec mulFactorDefault = kOnes_GrSLConstantVec);
+ // Argument expr is a simple expression or a parenthesized expression.
+ GrGLSLExpr(const char expr[]) {
+ if (NULL == expr) { // TODO: remove this once effects input Exprs.
+ fType = kOnes_ExprType;
+ } else {
+ fType = kFullExpr_ExprType;
+ fExpr = expr;
+ }
+ SkASSERT(isValid());
+ }
+
+ // Argument expr is a simple expression or a parenthesized expression.
+ GrGLSLExpr(const SkString& expr) {
+ if (expr.isEmpty()) { // TODO: remove this once effects input Exprs.
+ fType = kOnes_ExprType;
+ } else {
+ fType = kFullExpr_ExprType;
+ fExpr = expr;
+ }
+ SkASSERT(isValid());
+ }
+
+ /** 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 ...
+ template<int M>
+ static GrGLSLExpr<N> VectorCast(const GrGLSLExpr<M>&);
+
+ bool isOnes() const { return fType == kOnes_ExprType; }
+ bool isZeros() const { return fType == kZeros_ExprType; }
+
+ 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
+ GrGLSLExpr<N> operator*(const GrGLSLExpr<M>& other) const;
+ template<int M>
+ GrGLSLExpr<N> operator+(const GrGLSLExpr<M>& other) const;
+ template<int M>
+ GrGLSLExpr<N> operator-(const GrGLSLExpr<M>& other) const;
+
+ /**
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
+ * Given an expression that evaluates to a GLSL vecN, extract alpha
+ * component. Valid only for expressions of dimensions 4.
+ */
+ GrGLSLExpr<1> a() const;
+
+ const char* c_str() const {
+ if (kZeros_ExprType == fType) {
+ return this->zerosStr();
+ } else if (kOnes_ExprType == fType) {
+ return this->onesStr();
+ }
+ SkASSERT(!fExpr.isEmpty()); // Empty expressions should not be used.
+ return fExpr.c_str();
+ }
+
+private:
+ GrGLSLExpr(const char format[], const char in0[])
+ : fType(kFullExpr_ExprType) {
+ fExpr.appendf(format, in0);
+ }
+
+ 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[], const 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();
+
+ SkString fExpr;
+ enum ExprType {
+ kZeros_ExprType,
+ kOnes_ExprType,
+ kFullExpr_ExprType,
+ };
+ ExprType fType;
+
+ template <int> friend class GrGLSLExpr;
+};
+
+// Support for common case of (1 - expr.a()).
+template<int N>
+GrGLSLExpr<N> operator-(int a, const GrGLSLExpr<N>& b) {
+ return GrGLSLExpr<N>(a) - b;
+}
/**
- * Given an expression that evaluates to a GLSL vec4, extract a component. If expr is NULL or ""
- * the value of defaultExpr is used. It is an error to pass an empty expr and have set defaultExpr
- * to kNone. The return value indicates whether the value is known to be 0 or 1. If omitIfConst is
- * set then nothing is appended when the return is not kNone.
+ * Does an inplace mul, *=, of vec4VarName by mulFactor.
+ * A semicolon and newline are added after the assignment.
*/
-GrSLConstantVec GrGLSLGetComponent4f(SkString* outAppend,
- const char* expr,
- GrColorComponentFlags component,
- GrSLConstantVec defaultExpr = kNone_GrSLConstantVec,
- bool omitIfConst = false);
+void GrGLSLMulVarBy4f(SkString* outAppend, unsigned tabCnt,
+ const char* vec4VarName, const GrGLSLExpr<4>& mulFactor);
#include "GrGLSL_impl.h"

Powered by Google App Engine
This is Rietveld 408576698