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" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 11 | GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 12 | const GrGLInterface* gl) { |
| 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; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 19 | } else if (ver >= GR_GLSL_VER(1,30)) { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 20 | return k130_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 21 | } else { |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 22 | return k110_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 23 | } |
| 24 | case kES2_GrGLBinding: |
| 25 | // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL |
| 26 | GrAssert(ver >= GR_GL_VER(1,00)); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 27 | return k110_GrGLSLGeneration; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 28 | default: |
| 29 | GrCrash("Unknown GL Binding"); |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 30 | return k110_GrGLSLGeneration; // suppress warning |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 34 | const char* GrGetGLSLVersionDecl(GrGLBinding binding, |
| 35 | GrGLSLGeneration gen) { |
| 36 | switch (gen) { |
| 37 | case k110_GrGLSLGeneration: |
| 38 | if (kES2_GrGLBinding == binding) { |
| 39 | // ES2s shader language is based on version 1.20 but is version |
| 40 | // 1.00 of the ES language. |
| 41 | return "#version 100\n"; |
| 42 | } else { |
| 43 | GrAssert(kDesktop_GrGLBinding == binding); |
| 44 | return "#version 110\n"; |
| 45 | } |
| 46 | case k130_GrGLSLGeneration: |
| 47 | GrAssert(kDesktop_GrGLBinding == binding); |
| 48 | return "#version 130\n"; |
| 49 | case k150_GrGLSLGeneration: |
| 50 | GrAssert(kDesktop_GrGLBinding == binding); |
| 51 | return "#version 150\n"; |
| 52 | default: |
| 53 | GrCrash("Unknown GL version."); |
| 54 | return ""; // suppress warning |
| 55 | } |
| 56 | } |
| 57 | |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 58 | bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, |
| 59 | const char* nameIfDeclared, |
| 60 | GrGLShaderVar* var) { |
| 61 | bool declaredOutput = k110_GrGLSLGeneration != gen; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 62 | var->set(kVec4f_GrSLType, |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 63 | GrGLShaderVar::kOut_TypeModifier, |
| 64 | declaredOutput ? nameIfDeclared : "gl_FragColor"); |
| 65 | return declaredOutput; |
| 66 | } |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 67 | |
| 68 | GrSLType GrSLFloatVectorType (int count) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame^] | 69 | GR_STATIC_ASSERT(kFloat_GrSLType == 1); |
| 70 | GR_STATIC_ASSERT(kVec2f_GrSLType == 2); |
| 71 | GR_STATIC_ASSERT(kVec3f_GrSLType == 3); |
| 72 | GR_STATIC_ASSERT(kVec4f_GrSLType == 4); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 73 | GrAssert(count > 0 && count <= 4); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame^] | 74 | return (GrSLType)(count); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | const char* GrGLSLVectorHomogCoord(int count) { |
| 78 | static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"}; |
| 79 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS)); |
| 80 | return HOMOGS[count]; |
| 81 | } |
| 82 | |
| 83 | const char* GrGLSLVectorNonhomogCoords(int count) { |
| 84 | static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"}; |
| 85 | GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS)); |
| 86 | return NONHOMOGS[count]; |
| 87 | } |
| 88 | |