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"
9 #include "GrGLShaderVar.h"
10 #include "SkString.h"
11
GrGetGLSLGeneration(const GrGLInterface * gl,GrGLSLGeneration * generation)12 bool GrGetGLSLGeneration(const GrGLInterface* gl, GrGLSLGeneration* generation) {
13 SkASSERT(generation);
14 GrGLSLVersion ver = GrGLGetGLSLVersion(gl);
15 if (GR_GLSL_INVALID_VER == ver) {
16 return false;
17 }
18 switch (gl->fStandard) {
19 case kGL_GrGLStandard:
20 SkASSERT(ver >= GR_GLSL_VER(1,10));
21 if (ver >= GR_GLSL_VER(3,30)) {
22 *generation = k330_GrGLSLGeneration;
23 } else if (ver >= GR_GLSL_VER(1,50)) {
24 *generation = k150_GrGLSLGeneration;
25 } else if (ver >= GR_GLSL_VER(1,40)) {
26 *generation = k140_GrGLSLGeneration;
27 } else if (ver >= GR_GLSL_VER(1,30)) {
28 *generation = k130_GrGLSLGeneration;
29 } else {
30 *generation = k110_GrGLSLGeneration;
31 }
32 return true;
33 case kGLES_GrGLStandard:
34 SkASSERT(ver >= GR_GL_VER(1,00));
35 if (ver >= GR_GLSL_VER(3,1)) {
36 *generation = k310es_GrGLSLGeneration;
37 }
38 else if (ver >= GR_GLSL_VER(3,0)) {
39 *generation = k330_GrGLSLGeneration;
40 } else {
41 *generation = k110_GrGLSLGeneration;
42 }
43 return true;
44 default:
45 SkFAIL("Unknown GL Standard");
46 return false;
47 }
48 }
49
GrGetGLSLVersionDecl(const GrGLContextInfo & info)50 const char* GrGetGLSLVersionDecl(const GrGLContextInfo& info) {
51 switch (info.glslGeneration()) {
52 case k110_GrGLSLGeneration:
53 if (kGLES_GrGLStandard == info.standard()) {
54 // ES2s shader language is based on version 1.20 but is version
55 // 1.00 of the ES language.
56 return "#version 100\n";
57 } else {
58 SkASSERT(kGL_GrGLStandard == info.standard());
59 return "#version 110\n";
60 }
61 case k130_GrGLSLGeneration:
62 SkASSERT(kGL_GrGLStandard == info.standard());
63 return "#version 130\n";
64 case k140_GrGLSLGeneration:
65 SkASSERT(kGL_GrGLStandard == info.standard());
66 return "#version 140\n";
67 case k150_GrGLSLGeneration:
68 SkASSERT(kGL_GrGLStandard == info.standard());
69 if (info.caps()->isCoreProfile()) {
70 return "#version 150\n";
71 } else {
72 return "#version 150 compatibility\n";
73 }
74 case k330_GrGLSLGeneration:
75 if (kGLES_GrGLStandard == info.standard()) {
76 return "#version 300 es\n";
77 } else {
78 SkASSERT(kGL_GrGLStandard == info.standard());
79 if (info.caps()->isCoreProfile()) {
80 return "#version 330\n";
81 } else {
82 return "#version 330 compatibility\n";
83 }
84 }
85 case k310es_GrGLSLGeneration:
86 SkASSERT(kGLES_GrGLStandard == info.standard());
87 return "#version 310 es\n";
88 default:
89 SkFAIL("Unknown GL version.");
90 return ""; // suppress warning
91 }
92 }
93
GrGLSLMulVarBy4f(SkString * outAppend,const char * vec4VarName,const GrGLSLExpr4 & mulFactor)94 void GrGLSLMulVarBy4f(SkString* outAppend, const char* vec4VarName, const GrGLSLExpr4& mulFactor) {
95 if (mulFactor.isOnes()) {
96 *outAppend = SkString();
97 }
98
99 if (mulFactor.isZeros()) {
100 outAppend->appendf("%s = vec4(0);", vec4VarName);
101 } else {
102 outAppend->appendf("%s *= %s;", vec4VarName, mulFactor.c_str());
103 }
104 }
105