tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | #include "GrGLSL.h" |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 9 | #include "GrGLShaderVar.h" |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 10 | #include "SkString.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 11 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 12 | GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 13 | const GrGLInterface* gl) { |
| 14 | GrGLSLVersion ver = GrGLGetGLSLVersion(gl); |
| 15 | switch (binding) { |
| 16 | case kDesktop_GrGLBinding: |
| 17 | GrAssert(ver >= GR_GLSL_VER(1,10)); |
| 18 | if (ver >= GR_GLSL_VER(1,50)) { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 19 | return k150_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 20 | } else if (ver >= GR_GLSL_VER(1,30)) { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 21 | return k130_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 22 | } else { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 23 | return k110_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 24 | } |
| 25 | case kES2_GrGLBinding: |
| 26 | // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL |
| 27 | GrAssert(ver >= GR_GL_VER(1,00)); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 28 | return k110_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 29 | default: |
| 30 | GrCrash("Unknown GL Binding"); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 31 | return k110_GrGLSLGeneration; // suppress warning |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 35 | const char* GrGetGLSLVersionDecl(GrGLBinding binding, |
| 36 | GrGLSLGeneration gen) { |
| 37 | switch (gen) { |
| 38 | case k110_GrGLSLGeneration: |
| 39 | if (kES2_GrGLBinding == binding) { |
| 40 | // ES2s shader language is based on version 1.20 but is version |
| 41 | // 1.00 of the ES language. |
| 42 | return "#version 100\n"; |
| 43 | } else { |
| 44 | GrAssert(kDesktop_GrGLBinding == binding); |
| 45 | return "#version 110\n"; |
| 46 | } |
| 47 | case k130_GrGLSLGeneration: |
| 48 | GrAssert(kDesktop_GrGLBinding == binding); |
| 49 | return "#version 130\n"; |
| 50 | case k150_GrGLSLGeneration: |
| 51 | GrAssert(kDesktop_GrGLBinding == binding); |
| 52 | return "#version 150\n"; |
| 53 | default: |
| 54 | GrCrash("Unknown GL version."); |
| 55 | return ""; // suppress warning |
| 56 | } |
| 57 | } |
| 58 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 59 | bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, |
| 60 | const char* nameIfDeclared, |
| 61 | GrGLShaderVar* var) { |
| 62 | bool declaredOutput = k110_GrGLSLGeneration != gen; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 63 | var->set(kVec4f_GrSLType, |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 64 | GrGLShaderVar::kOut_TypeModifier, |
| 65 | declaredOutput ? nameIfDeclared : "gl_FragColor"); |
| 66 | return declaredOutput; |
| 67 | } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 68 | |
| 69 | GrSLType GrSLFloatVectorType (int count) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 70 | GR_STATIC_ASSERT(kFloat_GrSLType == 1); |
| 71 | GR_STATIC_ASSERT(kVec2f_GrSLType == 2); |
| 72 | GR_STATIC_ASSERT(kVec3f_GrSLType == 3); |
| 73 | GR_STATIC_ASSERT(kVec4f_GrSLType == 4); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 74 | GrAssert(count > 0 && count <= 4); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 75 | return (GrSLType)(count); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | const char* GrGLSLVectorHomogCoord(int count) { |
| 79 | static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"}; |
| 80 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS)); |
| 81 | return HOMOGS[count]; |
| 82 | } |
| 83 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 84 | const char* GrGLSLVectorHomogCoord(GrSLType type) { |
| 85 | return GrGLSLVectorHomogCoord(GrSLTypeToVecLength(type)); |
| 86 | } |
| 87 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 88 | const char* GrGLSLVectorNonhomogCoords(int count) { |
| 89 | static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"}; |
| 90 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS)); |
| 91 | return NONHOMOGS[count]; |
| 92 | } |
| 93 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 94 | const char* GrGLSLVectorNonhomogCoord(GrSLType type) { |
| 95 | return GrGLSLVectorNonhomogCoords(GrSLTypeToVecLength(type)); |
| 96 | } |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 97 | |
| 98 | GrSLConstantVec GrGLSLModulate4f(SkString* outAppend, |
| 99 | const char* in0, |
| 100 | const char* in1, |
| 101 | GrSLConstantVec default0, |
| 102 | GrSLConstantVec default1) { |
| 103 | GrAssert(NULL != outAppend); |
| 104 | |
| 105 | bool has0 = NULL != in0 && '\0' != *in0; |
| 106 | bool has1 = NULL != in1 && '\0' != *in1; |
| 107 | |
| 108 | GrAssert(has0 || kNone_GrSLConstantVec != default0); |
| 109 | GrAssert(has1 || kNone_GrSLConstantVec != default1); |
| 110 | |
| 111 | if (!has0 && !has1) { |
| 112 | GrAssert(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0); |
| 113 | GrAssert(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1); |
| 114 | if (kZeros_GrSLConstantVec == default0 || kZeros_GrSLConstantVec == default1) { |
| 115 | outAppend->append(GrGLSLZerosVecf(4)); |
| 116 | return kZeros_GrSLConstantVec; |
| 117 | } else { |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 118 | // both inputs are ones vectors |
bsalomon@google.com | d9e0181 | 2012-08-29 19:35:44 +0000 | [diff] [blame] | 119 | outAppend->append(GrGLSLOnesVecf(4)); |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 120 | return kOnes_GrSLConstantVec; |
| 121 | } |
| 122 | } else if (!has0) { |
| 123 | GrAssert(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0); |
| 124 | if (kZeros_GrSLConstantVec == default0) { |
| 125 | outAppend->append(GrGLSLZerosVecf(4)); |
| 126 | return kZeros_GrSLConstantVec; |
| 127 | } else { |
| 128 | outAppend->appendf("vec4(%s)", in1); |
| 129 | return kNone_GrSLConstantVec; |
| 130 | } |
| 131 | } else if (!has1) { |
| 132 | GrAssert(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1); |
| 133 | if (kZeros_GrSLConstantVec == default1) { |
| 134 | outAppend->append(GrGLSLZerosVecf(4)); |
| 135 | return kZeros_GrSLConstantVec; |
| 136 | } else { |
| 137 | outAppend->appendf("vec4(%s)", in0); |
| 138 | return kNone_GrSLConstantVec; |
| 139 | } |
| 140 | } else { |
| 141 | outAppend->appendf("vec4(%s * %s)", in0, in1); |
| 142 | return kNone_GrSLConstantVec; |
| 143 | } |
| 144 | } |
| 145 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame^] | 146 | namespace { |
| 147 | void append_tabs(SkString* outAppend, int tabCnt) { |
| 148 | static const char kTabs[] = "\t\t\t\t\t\t\t\t"; |
| 149 | while (tabCnt) { |
| 150 | int cnt = GrMin((int)GR_ARRAY_COUNT(kTabs), tabCnt); |
| 151 | outAppend->append(kTabs, cnt); |
| 152 | tabCnt -= cnt; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend, |
| 158 | int tabCnt, |
| 159 | const char* vec4VarName, |
| 160 | const char* mulFactor, |
| 161 | GrSLConstantVec mulFactorDefault) { |
| 162 | bool haveFactor = NULL != mulFactor && '\0' != *mulFactor; |
| 163 | |
| 164 | GrAssert(NULL != outAppend); |
| 165 | GrAssert(NULL != vec4VarName); |
| 166 | GrAssert(kNone_GrSLConstantVec != mulFactorDefault || haveFactor); |
| 167 | |
| 168 | if (!haveFactor) { |
| 169 | if (kOnes_GrSLConstantVec == mulFactorDefault) { |
| 170 | return kNone_GrSLConstantVec; |
| 171 | } else { |
| 172 | GrAssert(kZeros_GrSLConstantVec == mulFactorDefault); |
| 173 | append_tabs(outAppend, tabCnt); |
| 174 | outAppend->appendf("%s = vec4(0, 0, 0, 0);\n", vec4VarName); |
| 175 | return kZeros_GrSLConstantVec; |
| 176 | } |
| 177 | } |
| 178 | append_tabs(outAppend, tabCnt); |
| 179 | outAppend->appendf("%s *= %s;\n", vec4VarName, mulFactor); |
| 180 | return kNone_GrSLConstantVec; |
| 181 | } |
| 182 | |
bsalomon@google.com | 4af0af6 | 2012-08-29 12:59:57 +0000 | [diff] [blame] | 183 | GrSLConstantVec GrGLSLAdd4f(SkString* outAppend, |
| 184 | const char* in0, |
| 185 | const char* in1, |
| 186 | GrSLConstantVec default0, |
| 187 | GrSLConstantVec default1) { |
| 188 | GrAssert(NULL != outAppend); |
| 189 | |
| 190 | bool has0 = NULL != in0 && '\0' != *in0; |
| 191 | bool has1 = NULL != in1 && '\0' != *in1; |
| 192 | |
| 193 | if (!has0 && !has1) { |
| 194 | GrAssert(kZeros_GrSLConstantVec == default0); |
| 195 | GrAssert(kZeros_GrSLConstantVec == default1); |
| 196 | outAppend->append(GrGLSLZerosVecf(4)); |
| 197 | return kZeros_GrSLConstantVec; |
| 198 | } else if (!has0) { |
| 199 | GrAssert(kZeros_GrSLConstantVec == default0); |
| 200 | outAppend->appendf("vec4(%s)", in1); |
| 201 | return kNone_GrSLConstantVec; |
| 202 | } else if (!has1) { |
| 203 | GrAssert(kZeros_GrSLConstantVec == default1); |
| 204 | outAppend->appendf("vec4(%s)", in0); |
| 205 | return kNone_GrSLConstantVec; |
| 206 | } else { |
| 207 | outAppend->appendf("(vec4(%s) + vec4(%s))", in0, in1); |
| 208 | return kNone_GrSLConstantVec; |
| 209 | } |
| 210 | } |