Chromium Code Reviews| 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" | |
| 12 | 11 |
| 13 namespace { | 12 template<> |
| 13 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
| |
| 14 return "vec4(0,0,0,0)"; | |
| 15 } | |
| 16 | |
| 17 template<> | |
| 18 inline const char* GrGLSLExpr<4>::onesStr() { | |
| 19 return "vec4(1,1,1,1)"; | |
| 20 } | |
| 21 | |
| 22 template<> | |
| 23 inline const char* GrGLSLExpr<4>::extractAlphaStr() { | |
| 24 return "%s.a"; | |
| 25 } | |
| 26 | |
| 27 template<> | |
| 28 inline const char* GrGLSLExpr<4>::castStr() { | |
| 29 return "vec4(%s)"; | |
| 30 } | |
| 31 template<> | |
| 32 inline const char* GrGLSLExpr<4>::castIntStr() { | |
| 33 return "vec4(%d)"; | |
| 34 } | |
| 35 | |
| 36 template<> | |
| 37 inline const char* GrGLSLExpr<1>::zerosStr() { | |
| 38 return "0"; | |
| 39 } | |
| 40 | |
| 41 template<> | |
| 42 inline const char* GrGLSLExpr<1>::onesStr() { | |
| 43 return "1"; | |
| 44 } | |
| 45 | |
| 46 // GrGLSLExpr<1>::extractAlphaStr() and GrGLSLExpr<1>::castStr() are | |
| 47 // unimplemented because using them is likely an error. This is now caught | |
| 48 // compile-time. | |
| 49 | |
| 50 template<> | |
| 51 inline const char* GrGLSLExpr<1>::castIntStr() { | |
| 52 return "%d"; | |
| 53 } | |
| 54 | |
| 55 template<> | |
| 56 template<> | |
| 57 inline GrGLSLExpr<4> GrGLSLExpr<4>::VectorCast(const GrGLSLExpr<4>& other) { | |
| 58 return other; | |
| 59 } | |
| 60 | |
| 14 template<int N> | 61 template<int N> |
| 15 GrSLConstantVec return_const_vecf(GrSLConstantVec constVec, SkString* outAppend, bool omitAppend) { | 62 template<int M> |
| 16 SkASSERT(kNone_GrSLConstantVec != constVec); | 63 inline GrGLSLExpr<N> GrGLSLExpr<N>::VectorCast(const GrGLSLExpr<M>& other) { |
| 17 if (!omitAppend) { | 64 SK_COMPILE_ASSERT(M <= N, cast_from_wider_to_smaller_not_supported); |
| 18 if (kZeros_GrSLConstantVec == constVec) { | 65 if (other.isZeros()) { |
| 19 outAppend->append(GrGLSLZerosVecf(N)); | 66 return GrGLSLExpr<N>(0); |
| 20 } else { | 67 } |
| 21 outAppend->append(GrGLSLOnesVecf(N)); | 68 if (other.isOnes()) { |
| 69 return GrGLSLExpr<N>(1); | |
| 70 } | |
| 71 return GrGLSLExpr<N>(GrGLSLExpr<N>::castStr(), other.c_str()); | |
| 72 } | |
| 73 | |
| 74 template<int N> | |
| 75 template<int M> | |
| 76 inline GrGLSLExpr<N> GrGLSLExpr<N>::operator*(const GrGLSLExpr<M>& in1) const { | |
| 77 SK_COMPILE_ASSERT(N > 0 && N <= 4 && (M == 1 || M == N), dimensions_are_in_r ange); | |
| 78 | |
| 79 if (this->isZeros() || in1.isZeros()) { | |
| 80 return GrGLSLExpr<N>(0); | |
| 81 } | |
| 82 if (this->isOnes()) { | |
| 83 return VectorCast<M>(in1); | |
| 84 } | |
| 85 if (in1.isOnes()) { | |
| 86 return *this; | |
| 87 } | |
| 88 return GrGLSLExpr<N>("(%s * %s)", this->c_str(), in1.c_str()); | |
| 89 } | |
| 90 | |
| 91 template<int N> | |
| 92 template<int M> | |
| 93 GrGLSLExpr<N> GrGLSLExpr<N>::operator+(const GrGLSLExpr<M>& in1) const { | |
| 94 SK_COMPILE_ASSERT(N > 0 && N <= 4 && (M == 1 || M == N), dimensions_are_in_r ange); | |
| 95 | |
| 96 if (in1.isZeros()) { | |
| 97 return *this; | |
| 98 } | |
| 99 if (this->isZeros()) { | |
| 100 return VectorCast<M>(in1); | |
| 101 } | |
| 102 if (this->isOnes() && in1.isOnes()) { | |
| 103 return GrGLSLExpr<N>(2); | |
| 104 } | |
| 105 return GrGLSLExpr<N>("(%s + %s)", this->c_str(), in1.c_str()); | |
| 106 } | |
| 107 | |
| 108 template<int N> | |
| 109 template<int M> | |
| 110 GrGLSLExpr<N> GrGLSLExpr<N>::operator-(const GrGLSLExpr<M>& in1) const { | |
| 111 SK_COMPILE_ASSERT(N > 0 && N <= 4 && (M == 1 || M == N), dimensions_are_in_r ange); | |
| 112 | |
| 113 if (in1.isZeros()) { | |
| 114 return *this; | |
| 115 } | |
| 116 if (in1.isOnes()) { | |
| 117 if (this->isOnes()) { | |
| 118 return GrGLSLExpr<N>(0); | |
| 22 } | 119 } |
| 23 } | 120 } |
| 24 return constVec; | 121 |
| 25 } | 122 return GrGLSLExpr<N>("(%s - %s)", this->c_str(), in1.c_str()); |
| 26 } | 123 } |
| 27 | 124 |
| 28 template <int N> | 125 template<int N> |
| 29 GrSLConstantVec GrGLSLModulatef(SkString* outAppend, | 126 GrGLSLExpr<1> GrGLSLExpr<N>::a() const { |
| 30 const char* in0, | 127 if (this->isZeros()) { |
| 31 const char* in1, | 128 return GrGLSLExpr<1>(0); |
| 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 } | 129 } |
| 73 } | 130 if (this->isOnes()) { |
| 74 | 131 return GrGLSLExpr<1>(1); |
| 75 template <int N> | |
| 76 GrSLConstantVec GrGLSLAddf(SkString* outAppend, | |
| 77 const char* in0, | |
| 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 } | 132 } |
| 132 } | 133 return GrGLSLExpr<1>(this->extractAlphaStr(), this->c_str()); |
| 133 | |
| 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 } | |
| 190 } | 134 } |
| 191 | 135 |
| 192 #endif | 136 #endif |
| OLD | NEW |