Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / gpu / gl / GrGLSL.cpp
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
12 bool GrGetGLSLGeneration(const GrGLInterface* gl, GrGLSLGeneration* generation) {
13     SkASSERT(NULL != 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(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;
27             } else {
28                 *generation = k110_GrGLSLGeneration;
29             }
30             return true;
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;
35             return true;
36         default:
37             SkFAIL("Unknown GL Standard");
38             return false;
39     }
40 }
41
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";
49             } else {
50                 SkASSERT(kGL_GrGLStandard == info.standard());
51                 return "#version 110\n";
52             }
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";
63             } else {
64                 return "#version 150 compatibility\n";
65             }
66         default:
67             SkFAIL("Unknown GL version.");
68             return ""; // suppress warning
69     }
70 }
71
72 namespace {
73     void append_tabs(SkString* outAppend, int tabCnt) {
74         static const char kTabs[] = "\t\t\t\t\t\t\t\t";
75         while (tabCnt) {
76             int cnt = SkTMin((int)SK_ARRAY_COUNT(kTabs), tabCnt);
77             outAppend->append(kTabs, cnt);
78             tabCnt -= cnt;
79         }
80     }
81 }
82
83 void GrGLSLMulVarBy4f(SkString* outAppend,
84                       unsigned tabCnt,
85                       const char* vec4VarName,
86                       const GrGLSLExpr4& mulFactor) {
87     if (mulFactor.isOnes()) {
88         *outAppend = SkString();
89     }
90
91     append_tabs(outAppend, tabCnt);
92
93     if (mulFactor.isZeros()) {
94         outAppend->appendf("%s = vec4(0);\n", vec4VarName);
95     } else {
96         outAppend->appendf("%s *= %s;\n", vec4VarName, mulFactor.c_str());
97     }
98 }