2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "GrGLShaderVar.h"
12 bool GrGetGLSLGeneration(const GrGLInterface* gl, GrGLSLGeneration* generation) {
13 SkASSERT(NULL != generation);
14 GrGLSLVersion ver = GrGLGetGLSLVersion(gl);
15 if (GR_GLSL_INVALID_VER == ver) {
18 switch (gl->fStandard) {
19 case kGL_GrGLStandard:
20 SkASSERT(ver >= GR_GLSL_VER(1,10));
21 if (ver >= GR_GLSL_VER(1,50)) {
22 *generation = k150_GrGLSLGeneration;
23 } else if (ver >= GR_GLSL_VER(1,40)) {
24 *generation = k140_GrGLSLGeneration;
25 } else if (ver >= GR_GLSL_VER(1,30)) {
26 *generation = k130_GrGLSLGeneration;
28 *generation = k110_GrGLSLGeneration;
31 case kGLES_GrGLStandard:
32 // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL
33 SkASSERT(ver >= GR_GL_VER(1,00));
34 *generation = k110_GrGLSLGeneration;
37 SkFAIL("Unknown GL Standard");
42 const char* GrGetGLSLVersionDecl(const GrGLContextInfo& info) {
43 switch (info.glslGeneration()) {
44 case k110_GrGLSLGeneration:
45 if (kGLES_GrGLStandard == info.standard()) {
46 // ES2s shader language is based on version 1.20 but is version
47 // 1.00 of the ES language.
48 return "#version 100\n";
50 SkASSERT(kGL_GrGLStandard == info.standard());
51 return "#version 110\n";
53 case k130_GrGLSLGeneration:
54 SkASSERT(kGL_GrGLStandard == info.standard());
55 return "#version 130\n";
56 case k140_GrGLSLGeneration:
57 SkASSERT(kGL_GrGLStandard == info.standard());
58 return "#version 140\n";
59 case k150_GrGLSLGeneration:
60 SkASSERT(kGL_GrGLStandard == info.standard());
61 if (info.caps()->isCoreProfile()) {
62 return "#version 150\n";
64 return "#version 150 compatibility\n";
67 SkFAIL("Unknown GL version.");
68 return ""; // suppress warning
73 void append_tabs(SkString* outAppend, int tabCnt) {
74 static const char kTabs[] = "\t\t\t\t\t\t\t\t";
76 int cnt = SkTMin((int)SK_ARRAY_COUNT(kTabs), tabCnt);
77 outAppend->append(kTabs, cnt);
83 void GrGLSLMulVarBy4f(SkString* outAppend,
85 const char* vec4VarName,
86 const GrGLSLExpr4& mulFactor) {
87 if (mulFactor.isOnes()) {
88 *outAppend = SkString();
91 append_tabs(outAppend, tabCnt);
93 if (mulFactor.isZeros()) {
94 outAppend->appendf("%s = vec4(0);\n", vec4VarName);
96 outAppend->appendf("%s *= %s;\n", vec4VarName, mulFactor.c_str());