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