| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrGLSL_impl_DEFINED | 8 #ifndef GrGLSL_impl_DEFINED |
| 9 #define GrGLSL_impl_DEFINED | 9 #define GrGLSL_impl_DEFINED |
| 10 | 10 |
| 11 template<> | 11 template<typename Self> |
| 12 inline const char* GrGLSLExpr<4>::ZerosStr() { | 12 template<typename T> |
| 13 inline Self GrGLSLExpr<Self>::VectorCastImpl(const T& expr) { |
| 14 if (expr.isZeros()) { |
| 15 return Self(0); |
| 16 } |
| 17 if (expr.isOnes()) { |
| 18 return Self(1); |
| 19 } |
| 20 return Self(Self::CastStr(), expr.c_str()); |
| 21 } |
| 22 |
| 23 template<typename Self> |
| 24 template<typename T0, typename T1> |
| 25 inline Self GrGLSLExpr<Self>::Mul(T0 in0, T1 in1) { |
| 26 if (in0.isZeros() || in1.isZeros()) { |
| 27 return Self(0); |
| 28 } |
| 29 if (in0.isOnes()) { |
| 30 return Self::VectorCast(in1); |
| 31 } |
| 32 if (in1.isOnes()) { |
| 33 return Self::VectorCast(in0); |
| 34 } |
| 35 return Self("(%s * %s)", in0.c_str(), in1.c_str()); |
| 36 } |
| 37 |
| 38 template<typename Self> |
| 39 template<typename T0, typename T1> |
| 40 inline Self GrGLSLExpr<Self>::Add(T0 in0, T1 in1) { |
| 41 if (in1.isZeros()) { |
| 42 return Self::VectorCast(in0); |
| 43 } |
| 44 if (in0.isZeros()) { |
| 45 return Self::VectorCast(in1); |
| 46 } |
| 47 if (in0.isOnes() && in1.isOnes()) { |
| 48 return Self(2); |
| 49 } |
| 50 return Self("(%s + %s)", in0.c_str(), in1.c_str()); |
| 51 } |
| 52 |
| 53 template<typename Self> |
| 54 template<typename T0, typename T1> |
| 55 inline Self GrGLSLExpr<Self>::Sub(T0 in0, T1 in1) { |
| 56 if (in1.isZeros()) { |
| 57 return Self::VectorCast(in0); |
| 58 } |
| 59 if (in1.isOnes()) { |
| 60 if (in0.isOnes()) { |
| 61 return Self(0); |
| 62 } |
| 63 } |
| 64 |
| 65 return Self("(%s - %s)", in0.c_str(), in1.c_str()); |
| 66 } |
| 67 |
| 68 template <typename Self> |
| 69 template <typename T> |
| 70 T GrGLSLExpr<Self>::extractComponents(const char format[]) const { |
| 71 if (this->isZeros()) { |
| 72 return T(0); |
| 73 } |
| 74 if (this->isOnes()) { |
| 75 return T(1); |
| 76 } |
| 77 return T(format, this->c_str()); |
| 78 } |
| 79 |
| 80 inline GrGLSLExpr1 GrGLSLExpr1::VectorCast(const GrGLSLExpr1& expr) { |
| 81 return expr; |
| 82 } |
| 83 |
| 84 inline const char* GrGLSLExpr1::ZerosStr() { |
| 85 return "0"; |
| 86 } |
| 87 |
| 88 inline const char* GrGLSLExpr1::OnesStr() { |
| 89 return "1.0"; |
| 90 } |
| 91 |
| 92 // GrGLSLExpr1::CastStr() is unimplemented because using them is likely an |
| 93 // error. This is now caught compile-time. |
| 94 |
| 95 inline const char* GrGLSLExpr1::CastIntStr() { |
| 96 return "%d"; |
| 97 } |
| 98 |
| 99 inline GrGLSLExpr1 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { |
| 100 return GrGLSLExpr1::Mul(in0, in1); |
| 101 } |
| 102 |
| 103 inline GrGLSLExpr1 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { |
| 104 return GrGLSLExpr1::Add(in0, in1); |
| 105 } |
| 106 |
| 107 inline GrGLSLExpr1 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr1& in1) { |
| 108 return GrGLSLExpr1::Sub(in0, in1); |
| 109 } |
| 110 |
| 111 inline const char* GrGLSLExpr4::ZerosStr() { |
| 13 return "vec4(0)"; | 112 return "vec4(0)"; |
| 14 } | 113 } |
| 15 | 114 |
| 16 template<> | 115 inline const char* GrGLSLExpr4::OnesStr() { |
| 17 inline const char* GrGLSLExpr<4>::OnesStr() { | |
| 18 return "vec4(1)"; | 116 return "vec4(1)"; |
| 19 } | 117 } |
| 20 | 118 |
| 21 template<> | 119 inline const char* GrGLSLExpr4::CastStr() { |
| 22 inline const char* GrGLSLExpr<4>::ExtractAlphaStr() { | 120 return "vec4(%s)"; |
| 23 return "%s.a"; | |
| 24 } | 121 } |
| 25 | 122 |
| 26 template<> | 123 inline const char* GrGLSLExpr4::CastIntStr() { |
| 27 inline const char* GrGLSLExpr<4>::CastStr() { | |
| 28 return "vec4(%s)"; | |
| 29 } | |
| 30 template<> | |
| 31 inline const char* GrGLSLExpr<4>::CastIntStr() { | |
| 32 return "vec4(%d)"; | 124 return "vec4(%d)"; |
| 33 } | 125 } |
| 34 | 126 |
| 35 template<> | 127 inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr1& expr) { |
| 36 inline const char* GrGLSLExpr<1>::ZerosStr() { | 128 return INHERITED::VectorCastImpl(expr); |
| 37 return "0"; | |
| 38 } | 129 } |
| 39 | 130 |
| 40 template<> | 131 inline GrGLSLExpr4 GrGLSLExpr4::VectorCast(const GrGLSLExpr4& expr) { |
| 41 inline const char* GrGLSLExpr<1>::OnesStr() { | |
| 42 return "1"; | |
| 43 } | |
| 44 | |
| 45 // GrGLSLExpr<1>::ExtractAlphaStr() and GrGLSLExpr<1>::CastStr() are | |
| 46 // unimplemented because using them is likely an error. This is now caught | |
| 47 // compile-time. | |
| 48 | |
| 49 template<> | |
| 50 inline const char* GrGLSLExpr<1>::CastIntStr() { | |
| 51 return "%d"; | |
| 52 } | |
| 53 | |
| 54 template<> | |
| 55 template<> | |
| 56 inline GrGLSLExpr<4> GrGLSLExpr<4>::VectorCast(const GrGLSLExpr<4>& expr) { | |
| 57 return expr; | 132 return expr; |
| 58 } | 133 } |
| 59 | 134 |
| 60 template<> | 135 inline GrGLSLExpr4::AExpr GrGLSLExpr4::a() const { |
| 61 template<> | 136 return this->extractComponents<GrGLSLExpr4::AExpr>("%s.a"); |
| 62 inline GrGLSLExpr<1> GrGLSLExpr<1>::VectorCast(const GrGLSLExpr<1>& expr) { | |
| 63 return expr; | |
| 64 } | 137 } |
| 65 | 138 |
| 66 template<int N> | 139 inline GrGLSLExpr4 operator*(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { |
| 67 template<int M> | 140 return GrGLSLExpr4::Mul(in0, in1); |
| 68 inline GrGLSLExpr<N> GrGLSLExpr<N>::VectorCast(const GrGLSLExpr<M>& expr) { | |
| 69 if (expr.isZeros()) { | |
| 70 return GrGLSLExpr<N>(0); | |
| 71 } | |
| 72 if (expr.isOnes()) { | |
| 73 return GrGLSLExpr<N>(1); | |
| 74 } | |
| 75 return GrGLSLExpr<N>(GrGLSLExpr<N>::CastStr(), expr.c_str()); | |
| 76 } | 141 } |
| 77 | 142 |
| 78 template<int N> | 143 inline GrGLSLExpr4 operator+(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { |
| 79 template<int M> | 144 return GrGLSLExpr4::Add(in0, in1); |
| 80 inline GrGLSLExpr<N> GrGLSLExpr<N>::Mul(const GrGLSLExpr<N>& in0, const GrGLSLEx
pr<M>& in1) { | |
| 81 SK_COMPILE_ASSERT(N == M || M == 1, binary_op_dimensions_incompatible); | |
| 82 if (in0.isZeros() || in1.isZeros()) { | |
| 83 return GrGLSLExpr<N>(0); | |
| 84 } | |
| 85 if (in0.isOnes()) { | |
| 86 return VectorCast<M>(in1); | |
| 87 } | |
| 88 if (in1.isOnes()) { | |
| 89 return in0; | |
| 90 } | |
| 91 return GrGLSLExpr<N>("(%s * %s)", in0.c_str(), in1.c_str()); | |
| 92 } | 145 } |
| 93 | 146 |
| 94 template<int N> | 147 inline GrGLSLExpr4 operator-(const GrGLSLExpr1& in0, const GrGLSLExpr4& in1) { |
| 95 template<int M> | 148 return GrGLSLExpr4::Sub(in0, in1); |
| 96 inline GrGLSLExpr<N> GrGLSLExpr<N>::Add(const GrGLSLExpr<N>& in0, const GrGLSLEx
pr<M>& in1) { | |
| 97 SK_COMPILE_ASSERT(N == M || M == 1, binary_op_dimensions_incompatible); | |
| 98 if (in1.isZeros()) { | |
| 99 return in0; | |
| 100 } | |
| 101 if (in0.isZeros()) { | |
| 102 return VectorCast<M>(in1); | |
| 103 } | |
| 104 if (in0.isOnes() && in1.isOnes()) { | |
| 105 return GrGLSLExpr<N>(2); | |
| 106 } | |
| 107 return GrGLSLExpr<N>("(%s + %s)", in0.c_str(), in1.c_str()); | |
| 108 } | 149 } |
| 109 | 150 |
| 110 template<int N> | 151 inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { |
| 111 template<int M> | 152 return GrGLSLExpr4::Mul(in0, in1); |
| 112 inline GrGLSLExpr<N> GrGLSLExpr<N>::Sub(const GrGLSLExpr<N>& in0, const GrGLSLEx
pr<M>& in1) { | |
| 113 SK_COMPILE_ASSERT(N == M || M == 1, binary_op_dimensions_incompatible); | |
| 114 if (in1.isZeros()) { | |
| 115 return in0; | |
| 116 } | |
| 117 if (in1.isOnes()) { | |
| 118 if (in0.isOnes()) { | |
| 119 return GrGLSLExpr<N>(0); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 return GrGLSLExpr<N>("(%s - %s)", in0.c_str(), in1.c_str()); | |
| 124 } | 153 } |
| 125 | 154 |
| 126 inline GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr) { | 155 inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { |
| 127 return GrGLSLExpr<4>::VectorCast(expr); | 156 return GrGLSLExpr4::Add(in0, in1); |
| 128 } | 157 } |
| 129 | 158 |
| 130 inline GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr) { | 159 inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr1& in1) { |
| 131 if (expr.isZeros()) { | 160 return GrGLSLExpr4::Sub(in0, in1); |
| 132 return GrGLSLExpr<1>(0); | 161 } |
| 133 } | 162 |
| 134 if (expr.isOnes()) { | 163 inline GrGLSLExpr4 operator*(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { |
| 135 return GrGLSLExpr<1>(1); | 164 return GrGLSLExpr4::Mul(in0, in1); |
| 136 } | 165 } |
| 137 return GrGLSLExpr<1>(GrGLSLExpr<4>::ExtractAlphaStr(), expr.c_str()); | 166 |
| 167 inline GrGLSLExpr4 operator+(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { |
| 168 return GrGLSLExpr4::Add(in0, in1); |
| 169 } |
| 170 |
| 171 inline GrGLSLExpr4 operator-(const GrGLSLExpr4& in0, const GrGLSLExpr4& in1) { |
| 172 return GrGLSLExpr4::Sub(in0, in1); |
| 138 } | 173 } |
| 139 | 174 |
| 140 #endif | 175 #endif |
| OLD | NEW |