blob: bd30080df9ad613a9aeb082afbfc687b0d367872 [file] [log] [blame]
joshualitt30ba4362014-08-21 20:18:45 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrShaderCaps.h"
9#include "src/gpu/GrShaderVar.h"
Brian Salomon3ec1f542019-06-17 17:54:57 +000010#include "src/gpu/GrSwizzle.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
12#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
Brian Salomon3ec1f542019-06-17 17:54:57 +000013#include "src/gpu/glsl/GrGLSLShaderBuilder.h"
joshualitt30ba4362014-08-21 20:18:45 -070014
egdaniel2d721d32015-11-11 13:06:05 -080015GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program)
joshualitt30ba4362014-08-21 20:18:45 -070016 : fProgramBuilder(program)
egdaniel8dcdedc2015-11-11 06:27:20 -080017 , fInputs(GrGLSLProgramBuilder::kVarsPerBlock)
18 , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock)
joshualitt43466a12015-02-13 17:18:27 -080019 , 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();
joshualitt43466a12015-02-13 17:18:27 -080025 }
26
27 this->main() = "void main() {";
joshualitt30ba4362014-08-21 20:18:45 -070028}
29
Brian Salomon99938a82016-11-21 13:41:08 -050030void GrGLSLShaderBuilder::declAppend(const GrShaderVar& var) {
egdanielb2f94d12014-08-29 10:08:36 -070031 SkString tempDecl;
Brian Salomon94efbf52016-11-29 13:43:05 -050032 var.appendDecl(fProgramBuilder->shaderCaps(), &tempDecl);
egdanielb2f94d12014-08-29 10:08:36 -070033 this->codeAppendf("%s;", tempDecl.c_str());
34}
35
csmartdalton75864b02017-02-09 09:47:21 -050036void GrGLSLShaderBuilder::declareGlobal(const GrShaderVar& v) {
37 v.appendDecl(this->getProgramBuilder()->shaderCaps(), &this->definitions());
38 this->definitions().append(";");
39}
40
egdaniel2d721d32015-11-11 13:06:05 -080041void GrGLSLShaderBuilder::emitFunction(GrSLType returnType,
42 const char* name,
43 int argCnt,
Brian Salomon99938a82016-11-21 13:41:08 -050044 const GrShaderVar* args,
egdaniel2d721d32015-11-11 13:06:05 -080045 const char* body,
46 SkString* outName) {
Chris Dalton4f5cbcd2019-02-13 14:04:34 -070047 this->functions().append(GrGLSLTypeString(returnType));
joshualitt30ba4362014-08-21 20:18:45 -070048 fProgramBuilder->nameVariable(outName, '\0', name);
joshualitt43466a12015-02-13 17:18:27 -080049 this->functions().appendf(" %s", outName->c_str());
50 this->functions().append("(");
joshualitt30ba4362014-08-21 20:18:45 -070051 for (int i = 0; i < argCnt; ++i) {
Brian Salomon94efbf52016-11-29 13:43:05 -050052 args[i].appendDecl(fProgramBuilder->shaderCaps(), &this->functions());
joshualitt30ba4362014-08-21 20:18:45 -070053 if (i < argCnt - 1) {
joshualitt43466a12015-02-13 17:18:27 -080054 this->functions().append(", ");
joshualitt30ba4362014-08-21 20:18:45 -070055 }
56 }
joshualitt43466a12015-02-13 17:18:27 -080057 this->functions().append(") {\n");
58 this->functions().append(body);
59 this->functions().append("}\n\n");
joshualitt30ba4362014-08-21 20:18:45 -070060}
61
Brian Salomon101b8442016-11-18 11:58:54 -050062static inline void append_texture_swizzle(SkString* out, GrSwizzle swizzle) {
63 if (swizzle != GrSwizzle::RGBA()) {
64 out->appendf(".%s", swizzle.c_str());
65 }
66}
67
egdaniel2d721d32015-11-11 13:06:05 -080068void GrGLSLShaderBuilder::appendTextureLookup(SkString* out,
egdaniel09aa1fc2016-04-20 07:09:46 -070069 SamplerHandle samplerHandle,
egdaniel2d721d32015-11-11 13:06:05 -080070 const char* coordName,
71 GrSLType varyingType) const {
Brian Salomon99938a82016-11-21 13:41:08 -050072 const GrShaderVar& sampler = fProgramBuilder->samplerVariable(samplerHandle);
Ethan Nicholas5338f992017-04-19 15:54:07 -040073 out->appendf("texture(%s, %s)", sampler.c_str(), coordName);
Brian Salomon101b8442016-11-18 11:58:54 -050074 append_texture_swizzle(out, fProgramBuilder->samplerSwizzle(samplerHandle));
joshualitt30ba4362014-08-21 20:18:45 -070075}
76
egdaniel09aa1fc2016-04-20 07:09:46 -070077void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle,
egdaniel2d721d32015-11-11 13:06:05 -080078 const char* coordName,
brianosman77320db2016-09-07 08:09:10 -070079 GrSLType varyingType,
80 GrGLSLColorSpaceXformHelper* colorXformHelper) {
Brian Osmanc891b102018-06-14 14:50:17 -040081 SkString lookup;
82 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
83 this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
joshualitt30ba4362014-08-21 20:18:45 -070084}
85
brianosman77320db2016-09-07 08:09:10 -070086void GrGLSLShaderBuilder::appendTextureLookupAndModulate(
87 const char* modulation,
88 SamplerHandle samplerHandle,
89 const char* coordName,
90 GrSLType varyingType,
91 GrGLSLColorSpaceXformHelper* colorXformHelper) {
joshualitt30ba4362014-08-21 20:18:45 -070092 SkString lookup;
egdaniel09aa1fc2016-04-20 07:09:46 -070093 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
Brian Osmanc891b102018-06-14 14:50:17 -040094 this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
95 if (modulation) {
96 this->codeAppendf(" * %s", modulation);
brianosman77320db2016-09-07 08:09:10 -070097 }
98}
99
100void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out,
101 const char* srcColor,
102 GrGLSLColorSpaceXformHelper* colorXformHelper) {
Brian Osmanc891b102018-06-14 14:50:17 -0400103 if (!colorXformHelper || colorXformHelper->isNoop()) {
104 *out = srcColor;
105 return;
106 }
107
Brian Osmanc624d9d2017-03-08 11:42:02 -0500108 GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler();
Brian Osmanf06ead92017-10-30 13:47:41 -0400109
Brian Osman3567c142018-06-18 10:20:32 -0400110 // We define up to three helper functions, to keep things clearer. One for the source transfer
111 // function, one for the (inverse) destination transfer function, and one for the gamut xform.
112 // Any combination of these may be present, although some configurations are much more likely.
Brian Osmanf06ead92017-10-30 13:47:41 -0400113
Brian Osman3567c142018-06-18 10:20:32 -0400114 auto emitTFFunc = [=](const char* name, GrGLSLProgramDataManager::UniformHandle uniform) {
Nico Webere50efdf2018-10-01 14:40:44 -0400115 const GrShaderVar gTFArgs[] = { GrShaderVar("x", kHalf_GrSLType) };
Brian Osman3567c142018-06-18 10:20:32 -0400116 const char* coeffs = uniformHandler->getUniformCStr(uniform);
Brian Osmanf06ead92017-10-30 13:47:41 -0400117 SkString body;
118 // Temporaries to make evaluation line readable
119 body.appendf("half G = %s[0];", coeffs);
120 body.appendf("half A = %s[1];", coeffs);
121 body.appendf("half B = %s[2];", coeffs);
122 body.appendf("half C = %s[3];", coeffs);
123 body.appendf("half D = %s[4];", coeffs);
124 body.appendf("half E = %s[5];", coeffs);
125 body.appendf("half F = %s[6];", coeffs);
126 body.append("half s = sign(x);");
127 body.append("x = abs(x);");
128 body.appendf("return s * ((x < D) ? (C * x) + F : pow(A * x + B, G) + E);");
Brian Osman3567c142018-06-18 10:20:32 -0400129 SkString funcName;
130 this->emitFunction(kHalf_GrSLType, name, SK_ARRAY_COUNT(gTFArgs), gTFArgs, body.c_str(),
131 &funcName);
132 return funcName;
133 };
134
135 SkString srcTFFuncName;
136 if (colorXformHelper->applySrcTF()) {
137 srcTFFuncName = emitTFFunc("src_tf", colorXformHelper->srcTFUniform());
138 }
139
140 SkString dstTFFuncName;
141 if (colorXformHelper->applyDstTF()) {
142 dstTFFuncName = emitTFFunc("dst_tf", colorXformHelper->dstTFUniform());
Brian Osmanf06ead92017-10-30 13:47:41 -0400143 }
144
145 SkString gamutXformFuncName;
146 if (colorXformHelper->applyGamutXform()) {
Nico Webere50efdf2018-10-01 14:40:44 -0400147 const GrShaderVar gGamutXformArgs[] = { GrShaderVar("color", kHalf4_GrSLType) };
Brian Osmanf06ead92017-10-30 13:47:41 -0400148 const char* xform = uniformHandler->getUniformCStr(colorXformHelper->gamutXformUniform());
149 SkString body;
Brian Osman3567c142018-06-18 10:20:32 -0400150 body.appendf("color.rgb = (%s * color.rgb);", xform);
Brian Osmanf06ead92017-10-30 13:47:41 -0400151 body.append("return color;");
152 this->emitFunction(kHalf4_GrSLType, "gamut_xform", SK_ARRAY_COUNT(gGamutXformArgs),
153 gGamutXformArgs, body.c_str(), &gamutXformFuncName);
154 }
155
156 // Now define a wrapper function that applies all the intermediate steps
157 {
Nico Webere50efdf2018-10-01 14:40:44 -0400158 const GrShaderVar gColorXformArgs[] = { GrShaderVar("color", kHalf4_GrSLType) };
Brian Osmanf06ead92017-10-30 13:47:41 -0400159 SkString body;
Brian Osman3567c142018-06-18 10:20:32 -0400160 if (colorXformHelper->applyUnpremul()) {
Brian Salomond1bc2402019-06-05 11:47:50 -0400161 body.append("half nonZeroAlpha = max(color.a, 0.0001);");
Brian Osman3567c142018-06-18 10:20:32 -0400162 body.append("color = half4(color.rgb / nonZeroAlpha, nonZeroAlpha);");
Brian Osmanf06ead92017-10-30 13:47:41 -0400163 }
Brian Osman3567c142018-06-18 10:20:32 -0400164 if (colorXformHelper->applySrcTF()) {
165 body.appendf("color.r = %s(color.r);", srcTFFuncName.c_str());
166 body.appendf("color.g = %s(color.g);", srcTFFuncName.c_str());
167 body.appendf("color.b = %s(color.b);", srcTFFuncName.c_str());
Brian Osmanf06ead92017-10-30 13:47:41 -0400168 }
169 if (colorXformHelper->applyGamutXform()) {
170 body.appendf("color = %s(color);", gamutXformFuncName.c_str());
171 }
Brian Osman3567c142018-06-18 10:20:32 -0400172 if (colorXformHelper->applyDstTF()) {
173 body.appendf("color.r = %s(color.r);", dstTFFuncName.c_str());
174 body.appendf("color.g = %s(color.g);", dstTFFuncName.c_str());
175 body.appendf("color.b = %s(color.b);", dstTFFuncName.c_str());
176 }
177 if (colorXformHelper->applyPremul()) {
178 body.append("color.rgb *= color.a;");
179 }
Brian Osmanf06ead92017-10-30 13:47:41 -0400180 body.append("return color;");
181 SkString colorXformFuncName;
182 this->emitFunction(kHalf4_GrSLType, "color_xform", SK_ARRAY_COUNT(gColorXformArgs),
183 gColorXformArgs, body.c_str(), &colorXformFuncName);
184 out->appendf("%s(%s)", colorXformFuncName.c_str(), srcColor);
185 }
brianosman77320db2016-09-07 08:09:10 -0700186}
187
188void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor,
189 GrGLSLColorSpaceXformHelper* colorXformHelper) {
190 SkString xform;
191 this->appendColorGamutXform(&xform, srcColor, colorXformHelper);
192 this->codeAppend(xform.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700193}
194
cdalton33ad7012016-02-22 07:55:44 -0800195bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
196 if (featureBit & fFeaturesAddedMask) {
197 return false;
joshualitt30ba4362014-08-21 20:18:45 -0700198 }
cdalton33ad7012016-02-22 07:55:44 -0800199 this->extensions().appendf("#extension %s: require\n", extensionName);
200 fFeaturesAddedMask |= featureBit;
201 return true;
joshualitt30ba4362014-08-21 20:18:45 -0700202}
203
egdaniel2d721d32015-11-11 13:06:05 -0800204void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
joshualitt47bb3822014-10-07 16:43:25 -0700205 for (int i = 0; i < vars.count(); ++i) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500206 vars[i].appendDecl(fProgramBuilder->shaderCaps(), out);
joshualitt47bb3822014-10-07 16:43:25 -0700207 out->append(";\n");
208 }
209}
210
egdaniel2d721d32015-11-11 13:06:05 -0800211void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500212 SkASSERT(fProgramBuilder->shaderCaps()->generation() >= k330_GrGLSLGeneration ||
213 fProgramBuilder->shaderCaps()->mustEnableAdvBlendEqs());
cdaltone4017d82015-05-06 11:48:56 -0700214 fLayoutParams[interface].push_back() = param;
215}
216
egdaniel2d721d32015-11-11 13:06:05 -0800217void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() {
cdaltone4017d82015-05-06 11:48:56 -0700218 static const char* interfaceQualifierNames[] = {
csmartdalton276cc412016-11-21 11:55:00 -0700219 "in",
cdaltone4017d82015-05-06 11:48:56 -0700220 "out"
221 };
222
223 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
224 const SkTArray<SkString>& params = fLayoutParams[interface];
225 if (params.empty()) {
226 continue;
227 }
228 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
229 for (int i = 1; i < params.count(); ++i) {
230 this->layoutQualifiers().appendf(", %s", params[i].c_str());
231 }
232 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
233 }
234
csmartdalton276cc412016-11-21 11:55:00 -0700235 GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kIn_InterfaceQualifier);
236 GR_STATIC_ASSERT(1 == GrGLSLShaderBuilder::kOut_InterfaceQualifier);
cdaltone4017d82015-05-06 11:48:56 -0700237 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
238}
239
egdaniel2d721d32015-11-11 13:06:05 -0800240void GrGLSLShaderBuilder::finalize(uint32_t visibility) {
joshualitt43466a12015-02-13 17:18:27 -0800241 SkASSERT(!fFinalized);
egdaniel574a4c12015-11-02 06:22:44 -0800242 this->compileAndAppendLayoutQualifiers();
egdanielc8a66eb2015-11-02 07:46:25 -0800243 SkASSERT(visibility);
cdalton5e58cee2016-02-11 12:49:47 -0800244 fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms());
egdaniel574a4c12015-11-02 06:22:44 -0800245 this->appendDecls(fInputs, &this->inputs());
egdaniel574a4c12015-11-02 06:22:44 -0800246 this->appendDecls(fOutputs, &this->outputs());
247 this->onFinalize();
joshualitt43466a12015-02-13 17:18:27 -0800248 // append the 'footer' to code
249 this->code().append("}");
250
251 for (int i = 0; i <= fCodeIndex; i++) {
Brian Osman6c431d52019-04-15 16:31:54 -0400252 fCompilerString.append(fShaderStrings[i].c_str(), fShaderStrings[i].size());
joshualitt43466a12015-02-13 17:18:27 -0800253 }
254
joshualitt43466a12015-02-13 17:18:27 -0800255 fFinalized = true;
joshualitt43466a12015-02-13 17:18:27 -0800256}