| 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 #include "SkString.h" | 11 template<> |
| 12 inline const char* GrGLSLExpr<4>::ZerosStr() { |
| 13 return "vec4(0)"; |
| 14 } |
| 12 | 15 |
| 13 namespace { | 16 template<> |
| 17 inline const char* GrGLSLExpr<4>::OnesStr() { |
| 18 return "vec4(1)"; |
| 19 } |
| 20 |
| 21 template<> |
| 22 inline const char* GrGLSLExpr<4>::ExtractAlphaStr() { |
| 23 return "%s.a"; |
| 24 } |
| 25 |
| 26 template<> |
| 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)"; |
| 33 } |
| 34 |
| 35 template<> |
| 36 inline const char* GrGLSLExpr<1>::ZerosStr() { |
| 37 return "0"; |
| 38 } |
| 39 |
| 40 template<> |
| 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; |
| 58 } |
| 59 |
| 60 template<> |
| 61 template<> |
| 62 inline GrGLSLExpr<1> GrGLSLExpr<1>::VectorCast(const GrGLSLExpr<1>& expr) { |
| 63 return expr; |
| 64 } |
| 65 |
| 14 template<int N> | 66 template<int N> |
| 15 GrSLConstantVec return_const_vecf(GrSLConstantVec constVec, SkString* outAppend,
bool omitAppend) { | 67 template<int M> |
| 16 SkASSERT(kNone_GrSLConstantVec != constVec); | 68 inline GrGLSLExpr<N> GrGLSLExpr<N>::VectorCast(const GrGLSLExpr<M>& expr) { |
| 17 if (!omitAppend) { | 69 if (expr.isZeros()) { |
| 18 if (kZeros_GrSLConstantVec == constVec) { | 70 return GrGLSLExpr<N>(0); |
| 19 outAppend->append(GrGLSLZerosVecf(N)); | 71 } |
| 20 } else { | 72 if (expr.isOnes()) { |
| 21 outAppend->append(GrGLSLOnesVecf(N)); | 73 return GrGLSLExpr<N>(1); |
| 74 } |
| 75 return GrGLSLExpr<N>(GrGLSLExpr<N>::CastStr(), expr.c_str()); |
| 76 } |
| 77 |
| 78 template<int N> |
| 79 template<int M> |
| 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 } |
| 93 |
| 94 template<int N> |
| 95 template<int M> |
| 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 } |
| 109 |
| 110 template<int N> |
| 111 template<int M> |
| 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); |
| 22 } | 120 } |
| 23 } | 121 } |
| 24 return constVec; | 122 |
| 25 } | 123 return GrGLSLExpr<N>("(%s - %s)", in0.c_str(), in1.c_str()); |
| 26 } | 124 } |
| 27 | 125 |
| 28 template <int N> | 126 inline GrGLSLExpr<4> GrGLSLExprCast4(const GrGLSLExpr<1>& expr) { |
| 29 GrSLConstantVec GrGLSLModulatef(SkString* outAppend, | 127 return GrGLSLExpr<4>::VectorCast(expr); |
| 30 const char* in0, | |
| 31 const char* in1, | |
| 32 GrSLConstantVec default0, | |
| 33 GrSLConstantVec default1, | |
| 34 bool omitIfConstVec) { | |
| 35 SkASSERT(N > 0 && N <= 4); | |
| 36 SkASSERT(NULL != outAppend); | |
| 37 | |
| 38 bool has0 = NULL != in0 && '\0' != *in0; | |
| 39 bool has1 = NULL != in1 && '\0' != *in1; | |
| 40 | |
| 41 SkASSERT(has0 || kNone_GrSLConstantVec != default0); | |
| 42 SkASSERT(has1 || kNone_GrSLConstantVec != default1); | |
| 43 | |
| 44 if (!has0 && !has1) { | |
| 45 SkASSERT(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec ==
default0); | |
| 46 SkASSERT(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec ==
default1); | |
| 47 if (kZeros_GrSLConstantVec == default0 || kZeros_GrSLConstantVec == defa
ult1) { | |
| 48 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI
fConstVec); | |
| 49 } else { | |
| 50 // both inputs are ones vectors | |
| 51 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIf
ConstVec); | |
| 52 } | |
| 53 } else if (!has0) { | |
| 54 SkASSERT(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec ==
default0); | |
| 55 if (kZeros_GrSLConstantVec == default0) { | |
| 56 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI
fConstVec); | |
| 57 } else { | |
| 58 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1); | |
| 59 return kNone_GrSLConstantVec; | |
| 60 } | |
| 61 } else if (!has1) { | |
| 62 SkASSERT(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec ==
default1); | |
| 63 if (kZeros_GrSLConstantVec == default1) { | |
| 64 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI
fConstVec); | |
| 65 } else { | |
| 66 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0); | |
| 67 return kNone_GrSLConstantVec; | |
| 68 } | |
| 69 } else { | |
| 70 outAppend->appendf("%s((%s) * (%s))", GrGLSLFloatVectorTypeString(N), in
0, in1); | |
| 71 return kNone_GrSLConstantVec; | |
| 72 } | |
| 73 } | 128 } |
| 74 | 129 |
| 75 template <int N> | 130 inline GrGLSLExpr<1> GrGLSLExprExtractAlpha(const GrGLSLExpr<4>& expr) { |
| 76 GrSLConstantVec GrGLSLAddf(SkString* outAppend, | 131 if (expr.isZeros()) { |
| 77 const char* in0, | 132 return GrGLSLExpr<1>(0); |
| 78 const char* in1, | |
| 79 GrSLConstantVec default0, | |
| 80 GrSLConstantVec default1, | |
| 81 bool omitIfConstVec) { | |
| 82 SkASSERT(N > 0 && N <= 4); | |
| 83 SkASSERT(NULL != outAppend); | |
| 84 | |
| 85 bool has0 = NULL != in0 && '\0' != *in0; | |
| 86 bool has1 = NULL != in1 && '\0' != *in1; | |
| 87 | |
| 88 if (!has0 && !has1) { | |
| 89 SkASSERT(kNone_GrSLConstantVec != default0); | |
| 90 SkASSERT(kNone_GrSLConstantVec != default1); | |
| 91 int sum = (kOnes_GrSLConstantVec == default0) + (kOnes_GrSLConstantVec =
= default1); | |
| 92 if (0 == sum) { | |
| 93 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI
fConstVec); | |
| 94 } else if (1 == sum) { | |
| 95 outAppend->append(GrGLSLOnesVecf(N)); | |
| 96 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIf
ConstVec); | |
| 97 } else { | |
| 98 SkASSERT(2 == sum); | |
| 99 outAppend->appendf("%s(2)", GrGLSLFloatVectorTypeString(N)); | |
| 100 return kNone_GrSLConstantVec; | |
| 101 } | |
| 102 } else if (!has0) { | |
| 103 SkASSERT(kNone_GrSLConstantVec != default0); | |
| 104 if (kZeros_GrSLConstantVec == default0) { | |
| 105 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1); | |
| 106 } else { | |
| 107 outAppend->appendf("%s(%s) + %s", | |
| 108 GrGLSLFloatVectorTypeString(N), | |
| 109 in1, | |
| 110 GrGLSLOnesVecf(N)); | |
| 111 } | |
| 112 return kNone_GrSLConstantVec; | |
| 113 } else if (!has1) { | |
| 114 SkASSERT(kNone_GrSLConstantVec != default1); | |
| 115 if (kZeros_GrSLConstantVec == default1) { | |
| 116 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0); | |
| 117 } else { | |
| 118 outAppend->appendf("%s(%s) + %s", | |
| 119 GrGLSLFloatVectorTypeString(N), | |
| 120 in0, | |
| 121 GrGLSLOnesVecf(N)); | |
| 122 } | |
| 123 return kNone_GrSLConstantVec; | |
| 124 } else { | |
| 125 outAppend->appendf("(%s(%s) + %s(%s))", | |
| 126 GrGLSLFloatVectorTypeString(N), | |
| 127 in0, | |
| 128 GrGLSLFloatVectorTypeString(N), | |
| 129 in1); | |
| 130 return kNone_GrSLConstantVec; | |
| 131 } | 133 } |
| 132 } | 134 if (expr.isOnes()) { |
| 133 | 135 return GrGLSLExpr<1>(1); |
| 134 template <int N> | |
| 135 GrSLConstantVec GrGLSLSubtractf(SkString* outAppend, | |
| 136 const char* in0, | |
| 137 const char* in1, | |
| 138 GrSLConstantVec default0, | |
| 139 GrSLConstantVec default1, | |
| 140 bool omitIfConstVec) { | |
| 141 SkASSERT(N > 0 && N <= 4); | |
| 142 SkASSERT(NULL != outAppend); | |
| 143 | |
| 144 bool has0 = NULL != in0 && '\0' != *in0; | |
| 145 bool has1 = NULL != in1 && '\0' != *in1; | |
| 146 | |
| 147 if (!has0 && !has1) { | |
| 148 SkASSERT(kNone_GrSLConstantVec != default0); | |
| 149 SkASSERT(kNone_GrSLConstantVec != default1); | |
| 150 int diff = (kOnes_GrSLConstantVec == default0) - (kOnes_GrSLConstantVec
== default1); | |
| 151 if (-1 == diff) { | |
| 152 outAppend->appendf("%s(-1)", GrGLSLFloatVectorTypeString(N)); | |
| 153 return kNone_GrSLConstantVec; | |
| 154 } else if (0 == diff) { | |
| 155 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitI
fConstVec); | |
| 156 } else { | |
| 157 SkASSERT(1 == diff); | |
| 158 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIf
ConstVec); | |
| 159 } | |
| 160 } else if (!has0) { | |
| 161 SkASSERT(kNone_GrSLConstantVec != default0); | |
| 162 if (kZeros_GrSLConstantVec == default0) { | |
| 163 outAppend->appendf("-%s(%s)", GrGLSLFloatVectorTypeString(N), in1); | |
| 164 } else { | |
| 165 outAppend->appendf("%s - %s(%s)", | |
| 166 GrGLSLOnesVecf(N), | |
| 167 GrGLSLFloatVectorTypeString(N), | |
| 168 in1); | |
| 169 } | |
| 170 return kNone_GrSLConstantVec; | |
| 171 } else if (!has1) { | |
| 172 SkASSERT(kNone_GrSLConstantVec != default1); | |
| 173 if (kZeros_GrSLConstantVec == default1) { | |
| 174 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in0); | |
| 175 } else { | |
| 176 outAppend->appendf("%s(%s) - %s", | |
| 177 GrGLSLFloatVectorTypeString(N), | |
| 178 in0, | |
| 179 GrGLSLOnesVecf(N)); | |
| 180 } | |
| 181 return kNone_GrSLConstantVec; | |
| 182 } else { | |
| 183 outAppend->appendf("(%s(%s) - %s(%s))", | |
| 184 GrGLSLFloatVectorTypeString(N), | |
| 185 in0, | |
| 186 GrGLSLFloatVectorTypeString(N), | |
| 187 in1); | |
| 188 return kNone_GrSLConstantVec; | |
| 189 } | 136 } |
| 137 return GrGLSLExpr<1>(GrGLSLExpr<4>::ExtractAlphaStr(), expr.c_str()); |
| 190 } | 138 } |
| 191 | 139 |
| 192 #endif | 140 #endif |
| OLD | NEW |