Chromium Code Reviews| Index: src/gpu/gl/GrGLSL_impl.h |
| diff --git a/src/gpu/gl/GrGLSL_impl.h b/src/gpu/gl/GrGLSL_impl.h |
| index 292048c6a61147ae3f78359abeb43f0775adfe3e..10243728f50e64a8860a9e09a20f0f6c369119d2 100644 |
| --- a/src/gpu/gl/GrGLSL_impl.h |
| +++ b/src/gpu/gl/GrGLSL_impl.h |
| @@ -8,185 +8,129 @@ |
| #ifndef GrGLSL_impl_DEFINED |
| #define GrGLSL_impl_DEFINED |
| -#include "SkString.h" |
| -namespace { |
| +template<> |
| +inline const char* GrGLSLExpr<4>::zerosStr() { |
|
bsalomon
2013/10/02 13:20:26
should zeros and ones be static?
Kimmo Kinnunen
2013/10/08 12:18:29
I'm not sure what you mean? At the moment, they ar
|
| + return "vec4(0,0,0,0)"; |
| +} |
| + |
| +template<> |
| +inline const char* GrGLSLExpr<4>::onesStr() { |
| + return "vec4(1,1,1,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>& other) { |
| + return other; |
| +} |
| + |
| 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>& other) { |
| + SK_COMPILE_ASSERT(M <= N, cast_from_wider_to_smaller_not_supported); |
| + if (other.isZeros()) { |
| + return GrGLSLExpr<N>(0); |
| + } |
| + if (other.isOnes()) { |
| + return GrGLSLExpr<N>(1); |
| } |
| - return constVec; |
| + return GrGLSLExpr<N>(GrGLSLExpr<N>::castStr(), other.c_str()); |
| } |
| + |
| +template<int N> |
| +template<int M> |
| +inline GrGLSLExpr<N> GrGLSLExpr<N>::operator*(const GrGLSLExpr<M>& in1) const { |
| + SK_COMPILE_ASSERT(N > 0 && N <= 4 && (M == 1 || M == N), dimensions_are_in_range); |
| + |
| + if (this->isZeros() || in1.isZeros()) { |
| + return GrGLSLExpr<N>(0); |
| + } |
| + if (this->isOnes()) { |
| + return VectorCast<M>(in1); |
| + } |
| + if (in1.isOnes()) { |
| + return *this; |
| + } |
| + return GrGLSLExpr<N>("(%s * %s)", this->c_str(), in1.c_str()); |
| } |
| -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; |
| +template<int N> |
| +template<int M> |
| +GrGLSLExpr<N> GrGLSLExpr<N>::operator+(const GrGLSLExpr<M>& in1) const { |
| + SK_COMPILE_ASSERT(N > 0 && N <= 4 && (M == 1 || M == N), dimensions_are_in_range); |
| + |
| + if (in1.isZeros()) { |
| + return *this; |
| + } |
| + if (this->isZeros()) { |
| + return VectorCast<M>(in1); |
| + } |
| + if (this->isOnes() && in1.isOnes()) { |
| + return GrGLSLExpr<N>(2); |
| } |
| + return GrGLSLExpr<N>("(%s + %s)", this->c_str(), in1.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)); |
| +template<int N> |
| +template<int M> |
| +GrGLSLExpr<N> GrGLSLExpr<N>::operator-(const GrGLSLExpr<M>& in1) const { |
| + SK_COMPILE_ASSERT(N > 0 && N <= 4 && (M == 1 || M == N), dimensions_are_in_range); |
| + |
| + if (in1.isZeros()) { |
| + return *this; |
| + } |
| + if (in1.isOnes()) { |
| + if (this->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)", this->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)); |
| - } |
| - return kNone_GrSLConstantVec; |
| - } else { |
| - outAppend->appendf("(%s(%s) - %s(%s))", |
| - GrGLSLFloatVectorTypeString(N), |
| - in0, |
| - GrGLSLFloatVectorTypeString(N), |
| - in1); |
| - return kNone_GrSLConstantVec; |
| +template<int N> |
| +GrGLSLExpr<1> GrGLSLExpr<N>::a() const { |
| + if (this->isZeros()) { |
| + return GrGLSLExpr<1>(0); |
| + } |
| + if (this->isOnes()) { |
| + return GrGLSLExpr<1>(1); |
| } |
| + return GrGLSLExpr<1>(this->extractAlphaStr(), this->c_str()); |
| } |
| #endif |