1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrGLSL_impl_DEFINED 9 #define GrGLSL_impl_DEFINED 10 11 #include "SkString.h" 12 13 namespace { 14 template<int N> 15 GrSLConstantVec return_const_vecf(GrSLConstantVec constVec, SkString* outAppend, bool omitAppend) { 16 GrAssert(kNone_GrSLConstantVec != constVec); 17 if (!omitAppend) { 18 if (kZeros_GrSLConstantVec == constVec) { 19 outAppend->append(GrGLSLZerosVecf(N)); 20 } else { 21 outAppend->append(GrGLSLOnesVecf(N)); 22 } 23 } 24 return constVec; 25 } 26 } 27 28 template <int N> 29 GrSLConstantVec GrGLSLModulatef(SkString* outAppend, 30 const char* in0, 31 const char* in1, 32 GrSLConstantVec default0, 33 GrSLConstantVec default1, 34 bool omitIfConstVec) { 35 GrAssert(N > 0 && N <= 4); 36 GrAssert(NULL != outAppend); 37 38 bool has0 = NULL != in0 && '\0' != *in0; 39 bool has1 = NULL != in1 && '\0' != *in1; 40 41 GrAssert(has0 || kNone_GrSLConstantVec != default0); 42 GrAssert(has1 || kNone_GrSLConstantVec != default1); 43 44 if (!has0 && !has1) { 45 GrAssert(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0); 46 GrAssert(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1); 47 if (kZeros_GrSLConstantVec == default0 || kZeros_GrSLConstantVec == default1) { 48 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec); 49 } else { 50 // both inputs are ones vectors 51 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec); 52 } 53 } else if (!has0) { 54 GrAssert(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0); 55 if (kZeros_GrSLConstantVec == default0) { 56 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec); 57 } else { 58 outAppend->appendf("%s(%s)", GrGLSLFloatVectorTypeString(N), in1); 59 return kNone_GrSLConstantVec; 60 } 61 } else if (!has1) { 62 GrAssert(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1); 63 if (kZeros_GrSLConstantVec == default1) { 64 return return_const_vecf<N>(kZeros_GrSLConstantVec, outAppend, omitIfConstVec); 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), in0, in1); 71 return kNone_GrSLConstantVec; 72 } 73 } 74 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 GrAssert(N > 0 && N <= 4); 83 GrAssert(NULL != outAppend); 84 85 bool has0 = NULL != in0 && '\0' != *in0; 86 bool has1 = NULL != in1 && '\0' != *in1; 87 88 if (!has0 && !has1) { 89 GrAssert(kNone_GrSLConstantVec != default0); 90 GrAssert(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, omitIfConstVec); 94 } else if (1 == sum) { 95 outAppend->append(GrGLSLOnesVecf(N)); 96 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec); 97 } else { 98 GrAssert(2 == sum); 99 outAppend->appendf("%s(2)", GrGLSLFloatVectorTypeString(N)); 100 return kNone_GrSLConstantVec; 101 } 102 } else if (!has0) { 103 GrAssert(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 GrAssert(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 } 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 GrAssert(N > 0 && N <= 4); 142 GrAssert(NULL != outAppend); 143 144 bool has0 = NULL != in0 && '\0' != *in0; 145 bool has1 = NULL != in1 && '\0' != *in1; 146 147 if (!has0 && !has1) { 148 GrAssert(kNone_GrSLConstantVec != default0); 149 GrAssert(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, omitIfConstVec); 156 } else { 157 GrAssert(1 == diff); 158 return return_const_vecf<N>(kOnes_GrSLConstantVec, outAppend, omitIfConstVec); 159 } 160 } else if (!has0) { 161 GrAssert(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 GrAssert(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 } 191 192 #endif 193