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 | |
| 58 | const char* GrGetGLSLVarPrecisionDeclType(GrGLBinding binding) { |
| 59 | if (kES2_GrGLBinding == binding) { |
| 60 | return "mediump"; |
| 61 | } else { |
| 62 | return " "; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | const char* GrGetGLSLShaderPrecisionDecl(GrGLBinding binding) { |
| 67 | if (kES2_GrGLBinding == binding) { |
| 68 | return "precision mediump float;\n"; |
| 69 | } else { |
| 70 | return ""; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, |
| 75 | const char* nameIfDeclared, |
| 76 | GrGLShaderVar* var) { |
| 77 | bool declaredOutput = k110_GrGLSLGeneration != gen; |
| 78 | var->set(GrGLShaderVar::kVec4f_Type, |
| 79 | GrGLShaderVar::kOut_TypeModifier, |
| 80 | declaredOutput ? nameIfDeclared : "gl_FragColor"); |
| 81 | return declaredOutput; |
| 82 | } |