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