1 /*
2 * Copyright 2014 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 "GrShaderVar.h"
9 #include "GrShaderCaps.h"
10 #include "GrSwizzle.h"
11 #include "glsl/GrGLSLShaderBuilder.h"
12 #include "glsl/GrGLSLColorSpaceXformHelper.h"
13 #include "glsl/GrGLSLProgramBuilder.h"
14
GrGLSLShaderBuilder(GrGLSLProgramBuilder * program)15 GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program)
16 : fProgramBuilder(program)
17 , fInputs(GrGLSLProgramBuilder::kVarsPerBlock)
18 , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock)
19 , fFeaturesAddedMask(0)
20 , fCodeIndex(kCode)
21 , fFinalized(false) {
22 // We push back some dummy pointers which will later become our header
23 for (int i = 0; i <= kCode; i++) {
24 fShaderStrings.push_back();
25 fCompilerStrings.push_back(nullptr);
26 fCompilerStringLengths.push_back(0);
27 }
28
29 this->main() = "void main() {";
30 }
31
declAppend(const GrShaderVar & var)32 void GrGLSLShaderBuilder::declAppend(const GrShaderVar& var) {
33 SkString tempDecl;
34 var.appendDecl(fProgramBuilder->shaderCaps(), &tempDecl);
35 this->codeAppendf("%s;", tempDecl.c_str());
36 }
37
declareGlobal(const GrShaderVar & v)38 void GrGLSLShaderBuilder::declareGlobal(const GrShaderVar& v) {
39 v.appendDecl(this->getProgramBuilder()->shaderCaps(), &this->definitions());
40 this->definitions().append(";");
41 }
42
emitFunction(GrSLType returnType,const char * name,int argCnt,const GrShaderVar * args,const char * body,SkString * outName)43 void GrGLSLShaderBuilder::emitFunction(GrSLType returnType,
44 const char* name,
45 int argCnt,
46 const GrShaderVar* args,
47 const char* body,
48 SkString* outName) {
49 this->functions().append(GrGLSLTypeString(fProgramBuilder->shaderCaps(), returnType));
50 fProgramBuilder->nameVariable(outName, '\0', name);
51 this->functions().appendf(" %s", outName->c_str());
52 this->functions().append("(");
53 for (int i = 0; i < argCnt; ++i) {
54 args[i].appendDecl(fProgramBuilder->shaderCaps(), &this->functions());
55 if (i < argCnt - 1) {
56 this->functions().append(", ");
57 }
58 }
59 this->functions().append(") {\n");
60 this->functions().append(body);
61 this->functions().append("}\n\n");
62 }
63
append_texture_swizzle(SkString * out,GrSwizzle swizzle)64 static inline void append_texture_swizzle(SkString* out, GrSwizzle swizzle) {
65 if (swizzle != GrSwizzle::RGBA()) {
66 out->appendf(".%s", swizzle.c_str());
67 }
68 }
69
appendTextureLookup(SkString * out,SamplerHandle samplerHandle,const char * coordName,GrSLType varyingType) const70 void GrGLSLShaderBuilder::appendTextureLookup(SkString* out,
71 SamplerHandle samplerHandle,
72 const char* coordName,
73 GrSLType varyingType) const {
74 const GrShaderVar& sampler = fProgramBuilder->samplerVariable(samplerHandle);
75 out->appendf("texture(%s, %s)", sampler.c_str(), coordName);
76 append_texture_swizzle(out, fProgramBuilder->samplerSwizzle(samplerHandle));
77 }
78
appendTextureLookup(SamplerHandle samplerHandle,const char * coordName,GrSLType varyingType,GrGLSLColorSpaceXformHelper * colorXformHelper)79 void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle,
80 const char* coordName,
81 GrSLType varyingType,
82 GrGLSLColorSpaceXformHelper* colorXformHelper) {
83 SkString lookup;
84 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
85 this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
86 }
87
appendTextureLookupAndModulate(const char * modulation,SamplerHandle samplerHandle,const char * coordName,GrSLType varyingType,GrGLSLColorSpaceXformHelper * colorXformHelper)88 void GrGLSLShaderBuilder::appendTextureLookupAndModulate(
89 const char* modulation,
90 SamplerHandle samplerHandle,
91 const char* coordName,
92 GrSLType varyingType,
93 GrGLSLColorSpaceXformHelper* colorXformHelper) {
94 SkString lookup;
95 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
96 this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
97 if (modulation) {
98 this->codeAppendf(" * %s", modulation);
99 }
100 }
101
appendColorGamutXform(SkString * out,const char * srcColor,GrGLSLColorSpaceXformHelper * colorXformHelper)102 void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out,
103 const char* srcColor,
104 GrGLSLColorSpaceXformHelper* colorXformHelper) {
105 if (!colorXformHelper || colorXformHelper->isNoop()) {
106 *out = srcColor;
107 return;
108 }
109
110 GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler();
111
112 // We define up to three helper functions, to keep things clearer. One for the source transfer
113 // function, one for the (inverse) destination transfer function, and one for the gamut xform.
114 // Any combination of these may be present, although some configurations are much more likely.
115
116 auto emitTFFunc = [=](const char* name, GrGLSLProgramDataManager::UniformHandle uniform) {
117 const GrShaderVar gTFArgs[] = { GrShaderVar("x", kHalf_GrSLType) };
118 const char* coeffs = uniformHandler->getUniformCStr(uniform);
119 SkString body;
120 // Temporaries to make evaluation line readable
121 body.appendf("half G = %s[0];", coeffs);
122 body.appendf("half A = %s[1];", coeffs);
123 body.appendf("half B = %s[2];", coeffs);
124 body.appendf("half C = %s[3];", coeffs);
125 body.appendf("half D = %s[4];", coeffs);
126 body.appendf("half E = %s[5];", coeffs);
127 body.appendf("half F = %s[6];", coeffs);
128 body.append("half s = sign(x);");
129 body.append("x = abs(x);");
130 body.appendf("return s * ((x < D) ? (C * x) + F : pow(A * x + B, G) + E);");
131 SkString funcName;
132 this->emitFunction(kHalf_GrSLType, name, SK_ARRAY_COUNT(gTFArgs), gTFArgs, body.c_str(),
133 &funcName);
134 return funcName;
135 };
136
137 SkString srcTFFuncName;
138 if (colorXformHelper->applySrcTF()) {
139 srcTFFuncName = emitTFFunc("src_tf", colorXformHelper->srcTFUniform());
140 }
141
142 SkString dstTFFuncName;
143 if (colorXformHelper->applyDstTF()) {
144 dstTFFuncName = emitTFFunc("dst_tf", colorXformHelper->dstTFUniform());
145 }
146
147 SkString gamutXformFuncName;
148 if (colorXformHelper->applyGamutXform()) {
149 const GrShaderVar gGamutXformArgs[] = { GrShaderVar("color", kHalf4_GrSLType) };
150 const char* xform = uniformHandler->getUniformCStr(colorXformHelper->gamutXformUniform());
151 SkString body;
152 body.appendf("color.rgb = (%s * color.rgb);", xform);
153 body.append("return color;");
154 this->emitFunction(kHalf4_GrSLType, "gamut_xform", SK_ARRAY_COUNT(gGamutXformArgs),
155 gGamutXformArgs, body.c_str(), &gamutXformFuncName);
156 }
157
158 // Now define a wrapper function that applies all the intermediate steps
159 {
160 const GrShaderVar gColorXformArgs[] = { GrShaderVar("color", kHalf4_GrSLType) };
161 SkString body;
162 if (colorXformHelper->applyUnpremul()) {
163 body.append("half nonZeroAlpha = max(color.a, 0.00001);");
164 body.append("color = half4(color.rgb / nonZeroAlpha, nonZeroAlpha);");
165 }
166 if (colorXformHelper->applySrcTF()) {
167 body.appendf("color.r = %s(color.r);", srcTFFuncName.c_str());
168 body.appendf("color.g = %s(color.g);", srcTFFuncName.c_str());
169 body.appendf("color.b = %s(color.b);", srcTFFuncName.c_str());
170 }
171 if (colorXformHelper->applyGamutXform()) {
172 body.appendf("color = %s(color);", gamutXformFuncName.c_str());
173 }
174 if (colorXformHelper->applyDstTF()) {
175 body.appendf("color.r = %s(color.r);", dstTFFuncName.c_str());
176 body.appendf("color.g = %s(color.g);", dstTFFuncName.c_str());
177 body.appendf("color.b = %s(color.b);", dstTFFuncName.c_str());
178 }
179 if (colorXformHelper->applyPremul()) {
180 body.append("color.rgb *= color.a;");
181 }
182 body.append("return color;");
183 SkString colorXformFuncName;
184 this->emitFunction(kHalf4_GrSLType, "color_xform", SK_ARRAY_COUNT(gColorXformArgs),
185 gColorXformArgs, body.c_str(), &colorXformFuncName);
186 out->appendf("%s(%s)", colorXformFuncName.c_str(), srcColor);
187 }
188 }
189
appendColorGamutXform(const char * srcColor,GrGLSLColorSpaceXformHelper * colorXformHelper)190 void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor,
191 GrGLSLColorSpaceXformHelper* colorXformHelper) {
192 SkString xform;
193 this->appendColorGamutXform(&xform, srcColor, colorXformHelper);
194 this->codeAppend(xform.c_str());
195 }
196
addFeature(uint32_t featureBit,const char * extensionName)197 bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
198 if (featureBit & fFeaturesAddedMask) {
199 return false;
200 }
201 this->extensions().appendf("#extension %s: require\n", extensionName);
202 fFeaturesAddedMask |= featureBit;
203 return true;
204 }
205
appendDecls(const VarArray & vars,SkString * out) const206 void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
207 for (int i = 0; i < vars.count(); ++i) {
208 vars[i].appendDecl(fProgramBuilder->shaderCaps(), out);
209 out->append(";\n");
210 }
211 }
212
addLayoutQualifier(const char * param,InterfaceQualifier interface)213 void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
214 SkASSERT(fProgramBuilder->shaderCaps()->generation() >= k330_GrGLSLGeneration ||
215 fProgramBuilder->shaderCaps()->mustEnableAdvBlendEqs());
216 fLayoutParams[interface].push_back() = param;
217 }
218
compileAndAppendLayoutQualifiers()219 void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() {
220 static const char* interfaceQualifierNames[] = {
221 "in",
222 "out"
223 };
224
225 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
226 const SkTArray<SkString>& params = fLayoutParams[interface];
227 if (params.empty()) {
228 continue;
229 }
230 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
231 for (int i = 1; i < params.count(); ++i) {
232 this->layoutQualifiers().appendf(", %s", params[i].c_str());
233 }
234 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
235 }
236
237 GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kIn_InterfaceQualifier);
238 GR_STATIC_ASSERT(1 == GrGLSLShaderBuilder::kOut_InterfaceQualifier);
239 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
240 }
241
finalize(uint32_t visibility)242 void GrGLSLShaderBuilder::finalize(uint32_t visibility) {
243 SkASSERT(!fFinalized);
244 this->versionDecl() = fProgramBuilder->shaderCaps()->versionDeclString();
245 this->compileAndAppendLayoutQualifiers();
246 SkASSERT(visibility);
247 fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms());
248 this->appendDecls(fInputs, &this->inputs());
249 this->appendDecls(fOutputs, &this->outputs());
250 this->onFinalize();
251 // append the 'footer' to code
252 this->code().append("}");
253
254 for (int i = 0; i <= fCodeIndex; i++) {
255 fCompilerStrings[i] = fShaderStrings[i].c_str();
256 fCompilerStringLengths[i] = (int)fShaderStrings[i].size();
257 }
258
259 fFinalized = true;
260 }
261