blob: 079c08232f88f792ec67ee2de84817a31c8bf353 [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"
bsalomon@google.com4af0af62012-08-29 12:59:57 +000010#include "SkString.h"
tomhudson@google.com086e5352011-12-08 14:44:10 +000011
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000012GrGLSLGeneration GrGetGLSLGeneration(GrGLBinding binding,
tomhudson@google.com086e5352011-12-08 14:44:10 +000013 const GrGLInterface* gl) {
14 GrGLSLVersion ver = GrGLGetGLSLVersion(gl);
15 switch (binding) {
16 case kDesktop_GrGLBinding:
17 GrAssert(ver >= GR_GLSL_VER(1,10));
18 if (ver >= GR_GLSL_VER(1,50)) {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000019 return k150_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000020 } else if (ver >= GR_GLSL_VER(1,30)) {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000021 return k130_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000022 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000023 return k110_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000024 }
25 case kES2_GrGLBinding:
26 // version 1.00 of ES GLSL based on ver 1.20 of desktop GLSL
27 GrAssert(ver >= GR_GL_VER(1,00));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000028 return k110_GrGLSLGeneration;
tomhudson@google.com086e5352011-12-08 14:44:10 +000029 default:
30 GrCrash("Unknown GL Binding");
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000031 return k110_GrGLSLGeneration; // suppress warning
tomhudson@google.com086e5352011-12-08 14:44:10 +000032 }
33}
34
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000035const char* GrGetGLSLVersionDecl(GrGLBinding binding,
36 GrGLSLGeneration gen) {
37 switch (gen) {
38 case k110_GrGLSLGeneration:
39 if (kES2_GrGLBinding == binding) {
40 // ES2s shader language is based on version 1.20 but is version
41 // 1.00 of the ES language.
42 return "#version 100\n";
43 } else {
44 GrAssert(kDesktop_GrGLBinding == binding);
45 return "#version 110\n";
46 }
47 case k130_GrGLSLGeneration:
48 GrAssert(kDesktop_GrGLBinding == binding);
49 return "#version 130\n";
50 case k150_GrGLSLGeneration:
51 GrAssert(kDesktop_GrGLBinding == binding);
52 return "#version 150\n";
53 default:
54 GrCrash("Unknown GL version.");
55 return ""; // suppress warning
56 }
57}
58
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000059bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen,
60 const char* nameIfDeclared,
61 GrGLShaderVar* var) {
62 bool declaredOutput = k110_GrGLSLGeneration != gen;
tomhudson@google.com168e6342012-04-18 17:49:20 +000063 var->set(kVec4f_GrSLType,
bsalomon@google.come55fd0f2012-02-10 15:56:06 +000064 GrGLShaderVar::kOut_TypeModifier,
65 declaredOutput ? nameIfDeclared : "gl_FragColor");
66 return declaredOutput;
67}
tomhudson@google.com168e6342012-04-18 17:49:20 +000068
69GrSLType GrSLFloatVectorType (int count) {
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000070 GR_STATIC_ASSERT(kFloat_GrSLType == 1);
71 GR_STATIC_ASSERT(kVec2f_GrSLType == 2);
72 GR_STATIC_ASSERT(kVec3f_GrSLType == 3);
73 GR_STATIC_ASSERT(kVec4f_GrSLType == 4);
tomhudson@google.com168e6342012-04-18 17:49:20 +000074 GrAssert(count > 0 && count <= 4);
bsalomon@google.coma1bf0ff2012-08-07 17:36:29 +000075 return (GrSLType)(count);
tomhudson@google.com168e6342012-04-18 17:49:20 +000076}
77
78const char* GrGLSLVectorHomogCoord(int count) {
79 static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"};
80 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS));
81 return HOMOGS[count];
82}
83
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000084const char* GrGLSLVectorHomogCoord(GrSLType type) {
85 return GrGLSLVectorHomogCoord(GrSLTypeToVecLength(type));
86}
87
tomhudson@google.com168e6342012-04-18 17:49:20 +000088const char* GrGLSLVectorNonhomogCoords(int count) {
89 static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"};
90 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS));
91 return NONHOMOGS[count];
92}
93
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000094const char* GrGLSLVectorNonhomogCoord(GrSLType type) {
95 return GrGLSLVectorNonhomogCoords(GrSLTypeToVecLength(type));
96}
bsalomon@google.com4af0af62012-08-29 12:59:57 +000097
98GrSLConstantVec GrGLSLModulate4f(SkString* outAppend,
99 const char* in0,
100 const char* in1,
101 GrSLConstantVec default0,
102 GrSLConstantVec default1) {
103 GrAssert(NULL != outAppend);
104
105 bool has0 = NULL != in0 && '\0' != *in0;
106 bool has1 = NULL != in1 && '\0' != *in1;
107
108 GrAssert(has0 || kNone_GrSLConstantVec != default0);
109 GrAssert(has1 || kNone_GrSLConstantVec != default1);
110
111 if (!has0 && !has1) {
112 GrAssert(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0);
113 GrAssert(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1);
114 if (kZeros_GrSLConstantVec == default0 || kZeros_GrSLConstantVec == default1) {
115 outAppend->append(GrGLSLZerosVecf(4));
116 return kZeros_GrSLConstantVec;
117 } else {
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000118 // both inputs are ones vectors
bsalomon@google.comd9e01812012-08-29 19:35:44 +0000119 outAppend->append(GrGLSLOnesVecf(4));
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000120 return kOnes_GrSLConstantVec;
121 }
122 } else if (!has0) {
123 GrAssert(kZeros_GrSLConstantVec == default0 || kOnes_GrSLConstantVec == default0);
124 if (kZeros_GrSLConstantVec == default0) {
125 outAppend->append(GrGLSLZerosVecf(4));
126 return kZeros_GrSLConstantVec;
127 } else {
128 outAppend->appendf("vec4(%s)", in1);
129 return kNone_GrSLConstantVec;
130 }
131 } else if (!has1) {
132 GrAssert(kZeros_GrSLConstantVec == default1 || kOnes_GrSLConstantVec == default1);
133 if (kZeros_GrSLConstantVec == default1) {
134 outAppend->append(GrGLSLZerosVecf(4));
135 return kZeros_GrSLConstantVec;
136 } else {
137 outAppend->appendf("vec4(%s)", in0);
138 return kNone_GrSLConstantVec;
139 }
140 } else {
141 outAppend->appendf("vec4(%s * %s)", in0, in1);
142 return kNone_GrSLConstantVec;
143 }
144}
145
bsalomon@google.com868a8e72012-08-30 19:11:34 +0000146namespace {
147void append_tabs(SkString* outAppend, int tabCnt) {
148 static const char kTabs[] = "\t\t\t\t\t\t\t\t";
149 while (tabCnt) {
150 int cnt = GrMin((int)GR_ARRAY_COUNT(kTabs), tabCnt);
151 outAppend->append(kTabs, cnt);
152 tabCnt -= cnt;
153 }
154}
155}
156
157GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend,
158 int tabCnt,
159 const char* vec4VarName,
160 const char* mulFactor,
161 GrSLConstantVec mulFactorDefault) {
162 bool haveFactor = NULL != mulFactor && '\0' != *mulFactor;
163
164 GrAssert(NULL != outAppend);
165 GrAssert(NULL != vec4VarName);
166 GrAssert(kNone_GrSLConstantVec != mulFactorDefault || haveFactor);
167
168 if (!haveFactor) {
169 if (kOnes_GrSLConstantVec == mulFactorDefault) {
170 return kNone_GrSLConstantVec;
171 } else {
172 GrAssert(kZeros_GrSLConstantVec == mulFactorDefault);
173 append_tabs(outAppend, tabCnt);
174 outAppend->appendf("%s = vec4(0, 0, 0, 0);\n", vec4VarName);
175 return kZeros_GrSLConstantVec;
176 }
177 }
178 append_tabs(outAppend, tabCnt);
179 outAppend->appendf("%s *= %s;\n", vec4VarName, mulFactor);
180 return kNone_GrSLConstantVec;
181}
182
bsalomon@google.com4af0af62012-08-29 12:59:57 +0000183GrSLConstantVec GrGLSLAdd4f(SkString* outAppend,
184 const char* in0,
185 const char* in1,
186 GrSLConstantVec default0,
187 GrSLConstantVec default1) {
188 GrAssert(NULL != outAppend);
189
190 bool has0 = NULL != in0 && '\0' != *in0;
191 bool has1 = NULL != in1 && '\0' != *in1;
192
193 if (!has0 && !has1) {
194 GrAssert(kZeros_GrSLConstantVec == default0);
195 GrAssert(kZeros_GrSLConstantVec == default1);
196 outAppend->append(GrGLSLZerosVecf(4));
197 return kZeros_GrSLConstantVec;
198 } else if (!has0) {
199 GrAssert(kZeros_GrSLConstantVec == default0);
200 outAppend->appendf("vec4(%s)", in1);
201 return kNone_GrSLConstantVec;
202 } else if (!has1) {
203 GrAssert(kZeros_GrSLConstantVec == default1);
204 outAppend->appendf("vec4(%s)", in0);
205 return kNone_GrSLConstantVec;
206 } else {
207 outAppend->appendf("(vec4(%s) + vec4(%s))", in0, in1);
208 return kNone_GrSLConstantVec;
209 }
210}