aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/GrGLSL_impl.h
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-10-10 06:30:18 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-10-10 06:30:18 +0000
commit824c346b6e0e114063c1a8ad4ba7c3a669ee2cff (patch)
tree746fca51437b25e3a6e851a42ec40ec3d5ac6ee0 /src/gpu/gl/GrGLSL_impl.h
parentab9d30cb01d0c8401537dc115aba2842b0f4f27f (diff)
Express (GLSL expression, possibly known value) pairs as a class
Express (GLSL expression, possibly known value) pairs as a class instead of two variables Introduces GrGLSLExpr<N> to encapsulate the expression and possibly constant-folded value of the expression. This simplifies passing of the expressions to functions. Changes the shaders with following patterns: { // Stage 0: Linear Gradient vec4 colorTemp = mix(uGradientStartColor_Stage0, uGradientEndColor_Stage0, clamp(vMatrixCoord_Stage0.x, 0.0, 1 colorTemp.rgb *= colorTemp.a; - output_Stage0 = vec4((vColor) * (colorTemp)); + output_Stage0 = (vColor * colorTemp); + } Previously the vector cast was always added if constant folding was effective, regardless of the term dimensions. Now the vector upcast is not inserted in places where it is not needed, ie. when the binary operator term is of the target dimension. Also, some parentheses can be omitted. It is assumed that GrGLSLExpr<N>("string") constructors construct a simple expression or parenthesized expression. Otherwise the shader code remains identical. R=jvanverth@google.com, bsalomon@google.com, robertphillips@google.com Author: kkinnunen@nvidia.com Review URL: https://codereview.chromium.org/25048002 git-svn-id: http://skia.googlecode.com/svn/trunk@11690 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/gpu/gl/GrGLSL_impl.h')
-rw-r--r--src/gpu/gl/GrGLSL_impl.h284
1 files changed, 116 insertions, 168 deletions
diff --git a/src/gpu/gl/GrGLSL_impl.h b/src/gpu/gl/GrGLSL_impl.h
index 292048c6a6..008d512f9c 100644
--- a/src/gpu/gl/GrGLSL_impl.h
+++ b/src/gpu/gl/GrGLSL_impl.h
@@ -8,185 +8,133 @@
#ifndef GrGLSL_impl_DEFINED
#define GrGLSL_impl_DEFINED
-#include "SkString.h"
+template<>
+inline const char* GrGLSLExpr<4>::ZerosStr() {
+ return "vec4(0)";
+}
+
+template<>
+inline const char* GrGLSLExpr<4>::OnesStr() {
+ return "vec4(1)";
+}
+
+template<>
+inline const char* GrGLSLExpr<4>::ExtractAlphaStr() {
+ return "%s.a";
+}
+
+template<>
+inline const char* GrGLSLExpr<4>::CastStr() {
+ return "vec4(%s)";
+}
+template<>
+inline const char* GrGLSLExpr<4>::CastIntStr() {
+ return "vec4(%d)";
+}
+
+template<>
+inline const char* GrGLSLExpr<1>::ZerosStr() {
+ return "0";
+}
+
+template<>
+inline const char* GrGLSLExpr<1>::OnesStr() {
+ return "1";
+}
+
+// GrGLSLExpr<1>::ExtractAlphaStr() and GrGLSLExpr<1>::CastStr() are
+// unimplemented because using them is likely an error. This is now caught
+// compile-time.
+
+template<>
+inline const char* GrGLSLExpr<1>::CastIntStr() {
+ return "%d";
+}
+
+template<>
+template<>
+inline GrGLSLExpr<4> GrGLSLExpr<4>::VectorCast(const GrGLSLExpr<4>& expr) {
+ return expr;
+}
+
+template<>
+template<>
+inline GrGLSLExpr<1> GrGLSLExpr<1>::VectorCast(const GrGLSLExpr<1>& expr) {
+ return expr;
+}
-namespace {
template<int N>
-GrSLConstantVec return_const_vecf(GrSLConstantVec constVec, SkString* outAppend, bool omitAppend) {
- SkASSERT(kNone_GrSLConstantVec != constVec);
- if (!omitAppend) {
- if (kZeros_GrSLConstantVec == constVec) {
- outAppend->append(GrGLSLZerosVecf(N));
- } else {
- outAppend->append(GrGLSLOnesVecf(N));
- }
+template<int M>
+inline GrGLSLExpr<N> GrGLSLExpr<N>::VectorCast(const GrGLSLExpr<M>& expr) {
+ if (expr.isZeros()) {
+ return GrGLSLExpr<N>(0);
}
- return constVec;
-}
-}
-
-template <int N>
-GrSLConstantVec GrGLSLModulatef(SkString* outAppend,
- const char* in0,
- const char* in1,
- GrSLConstantVec default0,
- GrSLConstantVec default1,
- bool omitIfConstVec) {
- SkASSERT(N > 0 && N <= 4);
- SkASSERT(NULL != outAppend);
-
- bool has0 = NULL != in0 && '\0' != *in0;
- bool has1 = NULL != in1 && '\0' != *in1;
-
- SkASSERT(has0 || kNone_GrSLConstantVec != default0);
- SkASSERT(has1 || kNone_GrSLConstantVec != default1);
-
- if (!has0 && !has1) {
- SkASSERT(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0);
- SkASSERT(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1);
- if (kZeros_GrSLConstantVec == default0 || kZeros_GrSLConstantVec == default1) {
- return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
- } else {
- // both inputs are ones vectors
- return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec);
- }
- } else if (!has0) {
- SkASSERT(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0);
- if (kZeros_GrSLConstantVec == default0) {
- return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
- } else {
- outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
- return kNone_GrSLConstantVec;
- }
- } else if (!has1) {
- SkASSERT(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1);
- if (kZeros_GrSLConstantVec == default1) {
- return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
- } else {
- outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
- return kNone_GrSLConstantVec;
- }
- } else {
- outAppend->appendf("%s((%s) * (%s))", GrGLSLFloatVectorTypeString(N), in0, in1);
- return kNone_GrSLConstantVec;
+ if (expr.isOnes()) {
+ return GrGLSLExpr<N>(1);
}
+ return GrGLSLExpr<N>(GrGLSLExpr<N>::CastStr(), expr.c_str());
}
-template <int N>
-GrSLConstantVec GrGLSLAddf(SkString* outAppend,
- const char* in0,
- const char* in1,
- GrSLConstantVec default0,
- GrSLConstantVec default1,
- bool omitIfConstVec) {
- SkASSERT(N > 0 && N <= 4);
- SkASSERT(NULL != outAppend);
-
- bool has0 = NULL != in0 && '\0' != *in0;
- bool has1 = NULL != in1 && '\0' != *in1;
-
- if (!has0 && !has1) {
- SkASSERT(kNone_GrSLConstantVec != default0);
- SkASSERT(kNone_GrSLConstantVec != default1);
- int sum = (kOnes_GrSLConstantVec == default0) + (kOnes_GrSLConstantVec == default1);
- if (0 == sum) {
- return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
- } else if (1 == sum) {
- outAppend->append(GrGLSLOnesVecf(N));
- return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec);
- } else {
- SkASSERT(2 == sum);
- outAppend->appendf("%s(2)", GrGLSLFloatVectorTypeString(N));
- return kNone_GrSLConstantVec;
- }
- } else if (!has0) {
- SkASSERT(kNone_GrSLConstantVec != default0);
- if (kZeros_GrSLConstantVec == default0) {
- outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
- } else {
- outAppend->appendf("%s(%s) + %s",
- GrGLSLFloatVectorTypeString(N),
- in1,
- GrGLSLOnesVecf(N));
- }
- return kNone_GrSLConstantVec;
- } else if (!has1) {
- SkASSERT(kNone_GrSLConstantVec != default1);
- if (kZeros_GrSLConstantVec == default1) {
- outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
- } else {
- outAppend->appendf("%s(%s) + %s",
- GrGLSLFloatVectorTypeString(N),
- in0,
- GrGLSLOnesVecf(N));
- }
- return kNone_GrSLConstantVec;
- } else {
- outAppend->appendf("(%s(%s) + %s(%s))",
- GrGLSLFloatVectorTypeString(N),
- in0,
- GrGLSLFloatVectorTypeString(N),
- in1);
- return kNone_GrSLConstantVec;
+template<int N>
+template<int M>
+inline GrGLSLExpr<N> GrGLSLExpr<N>::Mul(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1) {
+ SK_COMPILE_ASSERT(N == M || M == 1, binary_op_dimensions_incompatible);
+ if (in0.isZeros() || in1.isZeros()) {
+ return GrGLSLExpr<N>(0);
+ }
+ if (in0.isOnes()) {
+ return VectorCast<M>(in1);
+ }
+ if (in1.isOnes()) {
+ return in0;
}
+ return GrGLSLExpr<N>("(%s * %s)", in0.c_str(), in1.c_str());
}
-template <int N>
-GrSLConstantVec GrGLSLSubtractf(SkString* outAppend,
- const char* in0,
- const char* in1,
- GrSLConstantVec default0,
- GrSLConstantVec default1,
- bool omitIfConstVec) {
- SkASSERT(N > 0 && N <= 4);
- SkASSERT(NULL != outAppend);
-
- bool has0 = NULL != in0 && '\0' != *in0;
- bool has1 = NULL != in1 && '\0' != *in1;
-
- if (!has0 && !has1) {
- SkASSERT(kNone_GrSLConstantVec != default0);
- SkASSERT(kNone_GrSLConstantVec != default1);
- int diff = (kOnes_GrSLConstantVec == default0) - (kOnes_GrSLConstantVec == default1);
- if (-1 == diff) {
- outAppend->appendf("%s(-1)", GrGLSLFloatVectorTypeString(N));
- return kNone_GrSLConstantVec;
- } else if (0 == diff) {
- return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec);
- } else {
- SkASSERT(1 == diff);
- return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec);
- }
- } else if (!has0) {
- SkASSERT(kNone_GrSLConstantVec != default0);
- if (kZeros_GrSLConstantVec == default0) {
- outAppend->appendf("-%s(%s)", GrGLSLFloatVectorTypeString(N), in1);
- } else {
- outAppend->appendf("%s - %s(%s)",
- GrGLSLOnesVecf(N),
- GrGLSLFloatVectorTypeString(N),
- in1);
- }
- return kNone_GrSLConstantVec;
- } else if (!has1) {
- SkASSERT(kNone_GrSLConstantVec != default1);
- if (kZeros_GrSLConstantVec == default1) {
- outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0);
- } else {
- outAppend->appendf("%s(%s) - %s",
- GrGLSLFloatVectorTypeString(N),
- in0,
- GrGLSLOnesVecf(N));
+template<int N>
+template<int M>
+inline GrGLSLExpr<N> GrGLSLExpr<N>::Add(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1) {
+ SK_COMPILE_ASSERT(N == M || M == 1, binary_op_dimensions_incompatible);
+ if (in1.isZeros()) {
+ return in0;
+ }
+ if (in0.isZeros()) {
+ return VectorCast<M>(in1);
+ }
+ if (in0.isOnes() && in1.isOnes()) {
+ return GrGLSLExpr<N>(2);
+ }
+ return GrGLSLExpr<N>("(%s + %s)", in0.c_str(), in1.c_str());
+}
+
+template<int N>
+template<int M>
+inline GrGLSLExpr<N> GrGLSLExpr<N>::Sub(const GrGLSLExpr<N>& in0, const GrGLSLExpr<M>& in1) {
+ SK_COMPILE_ASSERT(N == M || M == 1, binary_op_dimensions_incompatible);
+ if (in1.isZeros()) {
+ return in0;
+ }
+ if (in1.isOnes()) {
+ if (in0.isOnes()) {
+ return GrGLSLExpr<N>(0);
}
- return kNone_GrSLConstantVec;
- } else {
- outAppend->appendf("(%s(%s) - %s(%s))",
- GrGLSLFloatVectorTypeString(N),
- in0,
- GrGLSLFloatVectorTypeString(N),
- in1);
- return kNone_GrSLConstantVec;
}
+
+ return GrGLSLExpr<N>("(%s - %s)", in0.c_str(), in1.c_str());
+}
+
+inline GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr) {
+ return GrGLSLExpr<4>::VectorCast(expr);
+}
+
+inline GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr) {
+ if (expr.isZeros()) {
+ return GrGLSLExpr<1>(0);
+ }
+ if (expr.isOnes()) {
+ return GrGLSLExpr<1>(1);
+ }
+ return GrGLSLExpr<1>(GrGLSLExpr<4>::ExtractAlphaStr(), expr.c_str());
}
#endif