From 421fe443f833c916e6c3616f92570c8920b030f9 Mon Sep 17 00:00:00 2001 From: Ethan Nicholas Date: Thu, 4 May 2017 13:10:59 +0000 Subject: Revert "Revert "Revert "eliminated GrGLSLExpr""" This reverts commit 796001c82eca5651bc6a221204f6186918781daf. Reason for revert: looks to be causing problems in Chrome (https://storage.googleapis.com/chromium-layout-test-archives/WebKit_Linux_Trusty__dbg_/1553/layout-test-results/results.html) Original change's description: > Revert "Revert "eliminated GrGLSLExpr"" > > This reverts commit 5e550ab57e0204bfadd2cb69c47d2a85e38d6a4c. > > Bug: skia: > Change-Id: I4705e47dbd209aa8f43db3d28c856bd3aa9e49ab > Reviewed-on: https://skia-review.googlesource.com/15187 > Reviewed-by: Ethan Nicholas > Commit-Queue: Ethan Nicholas > TBR=ethannicholas@google.com NOPRESUBMIT=true NOTREECHECKS=true NOTRY=true Change-Id: I6455a4f16b2dc0d6d1265541f7117e0cfb8dd91c Reviewed-on: https://skia-review.googlesource.com/15309 Reviewed-by: Ethan Nicholas Commit-Queue: Ethan Nicholas --- include/private/GrGLSL.h | 241 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) (limited to 'include/private/GrGLSL.h') diff --git a/include/private/GrGLSL.h b/include/private/GrGLSL.h index 26954b4f80..66f7be84d1 100644 --- a/include/private/GrGLSL.h +++ b/include/private/GrGLSL.h @@ -138,4 +138,245 @@ static inline const char* GrGLSLTypeString(GrSLType t) { return ""; // suppress warning } +/** 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. + * + * Clients should not use this class, rather the specific instantiations defined + * later, for example GrGLSLExpr4. + */ +template +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(); + } + + bool isValid() const { + return kFullExpr_ExprType != fType || !fExpr.isEmpty(); + } + +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) { + // 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) { + if (v == 0) { + fType = kZeros_ExprType; + } else if (v == 1) { + fType = kOnes_ExprType; + } else { + fType = kFullExpr_ExprType; + fExpr.appendf(Self::CastIntStr(), v); + } + } + + /** Constructs an expression from a string. + * Argument expr is a simple expression or a parenthesized expression. */ + // TODO: make explicit once effects input Exprs. + GrGLSLExpr(const char expr[]) { + if (nullptr == expr) { // TODO: remove this once effects input Exprs. + fType = kOnes_ExprType; + } else { + fType = kFullExpr_ExprType; + fExpr = expr; + } + SkASSERT(this->isValid()); + } + + /** Constructs an expression from a string. + * Argument expr is a simple expression or a parenthesized expression. */ + // TODO: make explicit once effects input Exprs. + GrGLSLExpr(const SkString& expr) { + if (expr.isEmpty()) { // TODO: remove this once effects input Exprs. + fType = kOnes_ExprType; + } else { + fType = kFullExpr_ExprType; + fExpr = expr; + } + SkASSERT(this->isValid()); + } + + /** 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); + } + + /** Returns expression casted to another type. + * Generic implementation that is called for non-trivial cases of casts. */ + template + 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 + static Self Mul(T0 in0, T1 in1); + + /** 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 + static Self Add(T0 in0, T1 in1); + + /** 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 + static Self Sub(T0 in0, T1 in1); + + /** 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 + T extractComponents(const char format[]) const; + +private: + enum ExprType { + kZeros_ExprType, + kOnes_ExprType, + kFullExpr_ExprType, + }; + ExprType fType; + SkString fExpr; +}; + +class GrGLSLExpr1; +class GrGLSLExpr4; + +/** Class representing a float GLSL expression. */ +class GrGLSLExpr1 : public GrGLSLExpr { +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; + friend class GrGLSLExpr; + + typedef GrGLSLExpr INHERITED; +}; + +/** Class representing a float vector (vec4) GLSL expression. */ +class GrGLSLExpr4 : public GrGLSLExpr { +public: + GrGLSLExpr4() + : INHERITED() { + } + explicit GrGLSLExpr4(int v) + : INHERITED(v) { + } + GrGLSLExpr4(const char* expr) + : INHERITED(expr) { + } + GrGLSLExpr4(const SkString& expr) + : INHERITED(expr) { + } + + typedef GrGLSLExpr1 AExpr; + AExpr a() const; + + /** GLSL vec4 cast / constructor, eg vec4(floatv) -> vec4(floatv, floatv, floatv, floatv) */ + static GrGLSLExpr4 VectorCast(const GrGLSLExpr1& expr); + static GrGLSLExpr4 VectorCast(const GrGLSLExpr4& expr); + +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) { + } + + static const char* ZerosStr(); + static const char* OnesStr(); + static const char* CastStr(); + static const char* CastIntStr(); + + // 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); + + // 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); + + friend class GrGLSLExpr; + + typedef GrGLSLExpr INHERITED; +}; + +/** + * Does an inplace mul, *=, of vec4VarName by mulFactor. + * A semicolon is added after the assignment. + */ +void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor); + +#include "GrGLSL_impl.h" + #endif -- cgit v1.2.3