blob: ff4d498eaf77352f27f4b2cfcef6b557e30cc870 [file] [log] [blame]
tomhudson@google.com086e5352011-12-08 14:44:10 +00001/*
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.come55fd0f2012-02-10 15:56:06 +00009#include "GrGLShaderVar.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000010
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000011GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
tomhudson@google.com086e5352011-12-08 14:44:10 +000012 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.come55fd0f2012-02-10 15:56:06 +000018 return k150_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000019 } else if (ver >= GR_GLSL_VER(1,30)) {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000020 return k130_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000021 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000022 return k110_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000023 }
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.come55fd0f2012-02-10 15:56:06 +000027 return k110_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000028 default:
29 GrCrash("Unknown GL Binding");
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000030 return k110_GrGLSLGeneration; // suppress warning
tomhudson@google.com086e5352011-12-08 14:44:10 +000031 }
32}
33
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000034const 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.come55fd0f2012-02-10 15:56:06 +000058bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
59 const char* nameIfDeclared,
60 GrGLShaderVar* var) {
61 bool declaredOutput = k110_GrGLSLGeneration != gen;
tomhudson@google.com168e6342012-04-18 17:49:20 +000062 var->set(kVec4f_GrSLType,
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000063 GrGLShaderVar::kOut_TypeModifier,
64 declaredOutput ? nameIfDeclared : "gl_FragColor");
65 return declaredOutput;
66}
tomhudson@google.com168e6342012-04-18 17:49:20 +000067
68GrSLType GrSLFloatVectorType (int count) {
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000069 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.com168e6342012-04-18 17:49:20 +000073 GrAssert(count > 0 && count <= 4);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000074 return (GrSLType)(count);
tomhudson@google.com168e6342012-04-18 17:49:20 +000075}
76
77const 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
83const 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