blob: 4ef839a4e28a549aca8047365e8705ab47e7c1a9 [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
Greg Daniel2c19e7f2019-06-18 13:29:21 -04008#include "src/gpu/glsl/GrGLSLShaderBuilder.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrShaderCaps.h"
11#include "src/gpu/GrShaderVar.h"
Brian Salomon3ec1f542019-06-17 17:54:57 +000012#include "src/gpu/GrSwizzle.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
14#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
joshualitt30ba4362014-08-21 20:18:45 -070015
egdaniel2d721d32015-11-11 13:06:05 -080016GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program)
joshualitt30ba4362014-08-21 20:18:45 -070017 : fProgramBuilder(program)
egdaniel8dcdedc2015-11-11 06:27:20 -080018 , fInputs(GrGLSLProgramBuilder::kVarsPerBlock)
19 , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock)
joshualitt43466a12015-02-13 17:18:27 -080020 , 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();
joshualitt43466a12015-02-13 17:18:27 -080026 }
27
28 this->main() = "void main() {";
joshualitt30ba4362014-08-21 20:18:45 -070029}
30
Brian Salomon99938a82016-11-21 13:41:08 -050031void GrGLSLShaderBuilder::declAppend(const GrShaderVar& var) {
egdanielb2f94d12014-08-29 10:08:36 -070032 SkString tempDecl;
Brian Salomon94efbf52016-11-29 13:43:05 -050033 var.appendDecl(fProgramBuilder->shaderCaps(), &tempDecl);
egdanielb2f94d12014-08-29 10:08:36 -070034 this->codeAppendf("%s;", tempDecl.c_str());
35}
36
csmartdalton75864b02017-02-09 09:47:21 -050037void GrGLSLShaderBuilder::declareGlobal(const GrShaderVar& v) {
38 v.appendDecl(this->getProgramBuilder()->shaderCaps(), &this->definitions());
39 this->definitions().append(";");
40}
41
egdaniel2d721d32015-11-11 13:06:05 -080042void GrGLSLShaderBuilder::emitFunction(GrSLType returnType,
43 const char* name,
44 int argCnt,
Brian Salomon99938a82016-11-21 13:41:08 -050045 const GrShaderVar* args,
egdaniel2d721d32015-11-11 13:06:05 -080046 const char* body,
47 SkString* outName) {
Chris Dalton4f5cbcd2019-02-13 14:04:34 -070048 this->functions().append(GrGLSLTypeString(returnType));
joshualitt30ba4362014-08-21 20:18:45 -070049 fProgramBuilder->nameVariable(outName, '\0', name);
joshualitt43466a12015-02-13 17:18:27 -080050 this->functions().appendf(" %s", outName->c_str());
51 this->functions().append("(");
joshualitt30ba4362014-08-21 20:18:45 -070052 for (int i = 0; i < argCnt; ++i) {
Brian Salomon94efbf52016-11-29 13:43:05 -050053 args[i].appendDecl(fProgramBuilder->shaderCaps(), &this->functions());
joshualitt30ba4362014-08-21 20:18:45 -070054 if (i < argCnt - 1) {
joshualitt43466a12015-02-13 17:18:27 -080055 this->functions().append(", ");
joshualitt30ba4362014-08-21 20:18:45 -070056 }
57 }
joshualitt43466a12015-02-13 17:18:27 -080058 this->functions().append(") {\n");
59 this->functions().append(body);
60 this->functions().append("}\n\n");
joshualitt30ba4362014-08-21 20:18:45 -070061}
62
Brian Salomon101b8442016-11-18 11:58:54 -050063static inline void append_texture_swizzle(SkString* out, GrSwizzle swizzle) {
64 if (swizzle != GrSwizzle::RGBA()) {
Greg Daniel369ee6b2019-12-02 15:30:02 -050065 out->appendf(".%s", swizzle.asString().c_str());
Brian Salomon101b8442016-11-18 11:58:54 -050066 }
67}
68
egdaniel2d721d32015-11-11 13:06:05 -080069void GrGLSLShaderBuilder::appendTextureLookup(SkString* out,
egdaniel09aa1fc2016-04-20 07:09:46 -070070 SamplerHandle samplerHandle,
egdaniel2d721d32015-11-11 13:06:05 -080071 const char* coordName,
72 GrSLType varyingType) const {
Stephen Whited523a062019-06-19 13:12:46 -040073 const char* sampler = fProgramBuilder->samplerVariable(samplerHandle);
Ethan Nicholas13863662019-07-29 13:05:15 -040074 out->appendf("sample(%s, %s)", sampler, coordName);
Brian Salomon101b8442016-11-18 11:58:54 -050075 append_texture_swizzle(out, fProgramBuilder->samplerSwizzle(samplerHandle));
joshualitt30ba4362014-08-21 20:18:45 -070076}
77
egdaniel09aa1fc2016-04-20 07:09:46 -070078void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle,
egdaniel2d721d32015-11-11 13:06:05 -080079 const char* coordName,
brianosman77320db2016-09-07 08:09:10 -070080 GrSLType varyingType,
81 GrGLSLColorSpaceXformHelper* colorXformHelper) {
Brian Osmanc891b102018-06-14 14:50:17 -040082 SkString lookup;
83 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
84 this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
joshualitt30ba4362014-08-21 20:18:45 -070085}
86
brianosman77320db2016-09-07 08:09:10 -070087void GrGLSLShaderBuilder::appendTextureLookupAndModulate(
88 const char* modulation,
89 SamplerHandle samplerHandle,
90 const char* coordName,
91 GrSLType varyingType,
92 GrGLSLColorSpaceXformHelper* colorXformHelper) {
joshualitt30ba4362014-08-21 20:18:45 -070093 SkString lookup;
egdaniel09aa1fc2016-04-20 07:09:46 -070094 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
Brian Osmanc891b102018-06-14 14:50:17 -040095 this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
96 if (modulation) {
97 this->codeAppendf(" * %s", modulation);
brianosman77320db2016-09-07 08:09:10 -070098 }
99}
100
101void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out,
102 const char* srcColor,
103 GrGLSLColorSpaceXformHelper* colorXformHelper) {
Brian Osmanc891b102018-06-14 14:50:17 -0400104 if (!colorXformHelper || colorXformHelper->isNoop()) {
105 *out = srcColor;
106 return;
107 }
108
Brian Osmanc624d9d2017-03-08 11:42:02 -0500109 GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler();
Brian Osmanf06ead92017-10-30 13:47:41 -0400110
Brian Osman3567c142018-06-18 10:20:32 -0400111 // 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.
Brian Osmanf06ead92017-10-30 13:47:41 -0400114
Brian Osman11e6aa82019-10-16 13:58:42 -0400115 auto emitTFFunc = [=](const char* name, GrGLSLProgramDataManager::UniformHandle uniform,
116 TFKind kind) {
Nico Webere50efdf2018-10-01 14:40:44 -0400117 const GrShaderVar gTFArgs[] = { GrShaderVar("x", kHalf_GrSLType) };
Brian Osman3567c142018-06-18 10:20:32 -0400118 const char* coeffs = uniformHandler->getUniformCStr(uniform);
Brian Osmanf06ead92017-10-30 13:47:41 -0400119 SkString body;
Brian Osman11e6aa82019-10-16 13:58:42 -0400120 // Temporaries to make evaluation line readable. We always use the sRGBish names, so the
121 // PQ and HLG math is confusing.
Brian Osmanf06ead92017-10-30 13:47:41 -0400122 body.appendf("half G = %s[0];", coeffs);
123 body.appendf("half A = %s[1];", coeffs);
124 body.appendf("half B = %s[2];", coeffs);
125 body.appendf("half C = %s[3];", coeffs);
126 body.appendf("half D = %s[4];", coeffs);
127 body.appendf("half E = %s[5];", coeffs);
128 body.appendf("half F = %s[6];", coeffs);
129 body.append("half s = sign(x);");
130 body.append("x = abs(x);");
Brian Osman11e6aa82019-10-16 13:58:42 -0400131 switch (kind) {
132 case TFKind::sRGBish_TF:
133 body.append("x = (x < D) ? (C * x) + F : pow(A * x + B, G) + E;");
134 break;
135 case TFKind::PQish_TF:
136 body.append("x = pow(max(A + B * pow(x, C), 0) / (D + E * pow(x, C)), F);");
137 break;
138 case TFKind::HLGish_TF:
139 body.append("x = (x*A <= 1) ? pow(x*A, B) : exp((x-E)*C) + D;");
140 break;
141 case TFKind::HLGinvish_TF:
142 body.append("x = (x <= 1) ? A * pow(x, B) : C * log(x - D) + E;");
143 break;
144 default:
145 SkASSERT(false);
146 break;
147 }
148 body.append("return s * x;");
Brian Osman3567c142018-06-18 10:20:32 -0400149 SkString funcName;
150 this->emitFunction(kHalf_GrSLType, name, SK_ARRAY_COUNT(gTFArgs), gTFArgs, body.c_str(),
151 &funcName);
152 return funcName;
153 };
154
155 SkString srcTFFuncName;
156 if (colorXformHelper->applySrcTF()) {
Brian Osman11e6aa82019-10-16 13:58:42 -0400157 srcTFFuncName = emitTFFunc("src_tf", colorXformHelper->srcTFUniform(),
158 colorXformHelper->srcTFKind());
Brian Osman3567c142018-06-18 10:20:32 -0400159 }
160
161 SkString dstTFFuncName;
162 if (colorXformHelper->applyDstTF()) {
Brian Osman11e6aa82019-10-16 13:58:42 -0400163 dstTFFuncName = emitTFFunc("dst_tf", colorXformHelper->dstTFUniform(),
164 colorXformHelper->dstTFKind());
Brian Osmanf06ead92017-10-30 13:47:41 -0400165 }
166
167 SkString gamutXformFuncName;
168 if (colorXformHelper->applyGamutXform()) {
Nico Webere50efdf2018-10-01 14:40:44 -0400169 const GrShaderVar gGamutXformArgs[] = { GrShaderVar("color", kHalf4_GrSLType) };
Brian Osmanf06ead92017-10-30 13:47:41 -0400170 const char* xform = uniformHandler->getUniformCStr(colorXformHelper->gamutXformUniform());
171 SkString body;
Brian Osman3567c142018-06-18 10:20:32 -0400172 body.appendf("color.rgb = (%s * color.rgb);", xform);
Brian Osmanf06ead92017-10-30 13:47:41 -0400173 body.append("return color;");
174 this->emitFunction(kHalf4_GrSLType, "gamut_xform", SK_ARRAY_COUNT(gGamutXformArgs),
175 gGamutXformArgs, body.c_str(), &gamutXformFuncName);
176 }
177
178 // Now define a wrapper function that applies all the intermediate steps
179 {
Nico Webere50efdf2018-10-01 14:40:44 -0400180 const GrShaderVar gColorXformArgs[] = { GrShaderVar("color", kHalf4_GrSLType) };
Brian Osmanf06ead92017-10-30 13:47:41 -0400181 SkString body;
Brian Osman3567c142018-06-18 10:20:32 -0400182 if (colorXformHelper->applyUnpremul()) {
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -0400183 body.append("half nonZeroAlpha = max(color.a, 0.0001);");
184 body.append("color = half4(color.rgb / nonZeroAlpha, nonZeroAlpha);");
Brian Osmanf06ead92017-10-30 13:47:41 -0400185 }
Brian Osman3567c142018-06-18 10:20:32 -0400186 if (colorXformHelper->applySrcTF()) {
187 body.appendf("color.r = %s(color.r);", srcTFFuncName.c_str());
188 body.appendf("color.g = %s(color.g);", srcTFFuncName.c_str());
189 body.appendf("color.b = %s(color.b);", srcTFFuncName.c_str());
Brian Osmanf06ead92017-10-30 13:47:41 -0400190 }
191 if (colorXformHelper->applyGamutXform()) {
192 body.appendf("color = %s(color);", gamutXformFuncName.c_str());
193 }
Brian Osman3567c142018-06-18 10:20:32 -0400194 if (colorXformHelper->applyDstTF()) {
195 body.appendf("color.r = %s(color.r);", dstTFFuncName.c_str());
196 body.appendf("color.g = %s(color.g);", dstTFFuncName.c_str());
197 body.appendf("color.b = %s(color.b);", dstTFFuncName.c_str());
198 }
199 if (colorXformHelper->applyPremul()) {
200 body.append("color.rgb *= color.a;");
201 }
Brian Osmanf06ead92017-10-30 13:47:41 -0400202 body.append("return color;");
203 SkString colorXformFuncName;
204 this->emitFunction(kHalf4_GrSLType, "color_xform", SK_ARRAY_COUNT(gColorXformArgs),
205 gColorXformArgs, body.c_str(), &colorXformFuncName);
206 out->appendf("%s(%s)", colorXformFuncName.c_str(), srcColor);
207 }
brianosman77320db2016-09-07 08:09:10 -0700208}
209
210void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor,
211 GrGLSLColorSpaceXformHelper* colorXformHelper) {
212 SkString xform;
213 this->appendColorGamutXform(&xform, srcColor, colorXformHelper);
214 this->codeAppend(xform.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700215}
216
cdalton33ad7012016-02-22 07:55:44 -0800217bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
218 if (featureBit & fFeaturesAddedMask) {
219 return false;
joshualitt30ba4362014-08-21 20:18:45 -0700220 }
cdalton33ad7012016-02-22 07:55:44 -0800221 this->extensions().appendf("#extension %s: require\n", extensionName);
222 fFeaturesAddedMask |= featureBit;
223 return true;
joshualitt30ba4362014-08-21 20:18:45 -0700224}
225
egdaniel2d721d32015-11-11 13:06:05 -0800226void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
joshualitt47bb3822014-10-07 16:43:25 -0700227 for (int i = 0; i < vars.count(); ++i) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500228 vars[i].appendDecl(fProgramBuilder->shaderCaps(), out);
joshualitt47bb3822014-10-07 16:43:25 -0700229 out->append(";\n");
230 }
231}
232
egdaniel2d721d32015-11-11 13:06:05 -0800233void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
Brian Salomon94efbf52016-11-29 13:43:05 -0500234 SkASSERT(fProgramBuilder->shaderCaps()->generation() >= k330_GrGLSLGeneration ||
235 fProgramBuilder->shaderCaps()->mustEnableAdvBlendEqs());
cdaltone4017d82015-05-06 11:48:56 -0700236 fLayoutParams[interface].push_back() = param;
237}
238
egdaniel2d721d32015-11-11 13:06:05 -0800239void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() {
cdaltone4017d82015-05-06 11:48:56 -0700240 static const char* interfaceQualifierNames[] = {
csmartdalton276cc412016-11-21 11:55:00 -0700241 "in",
cdaltone4017d82015-05-06 11:48:56 -0700242 "out"
243 };
244
245 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
246 const SkTArray<SkString>& params = fLayoutParams[interface];
247 if (params.empty()) {
248 continue;
249 }
250 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
251 for (int i = 1; i < params.count(); ++i) {
252 this->layoutQualifiers().appendf(", %s", params[i].c_str());
253 }
254 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
255 }
256
Brian Salomon4dea72a2019-12-18 10:43:10 -0500257 static_assert(0 == GrGLSLShaderBuilder::kIn_InterfaceQualifier);
258 static_assert(1 == GrGLSLShaderBuilder::kOut_InterfaceQualifier);
259 static_assert(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
cdaltone4017d82015-05-06 11:48:56 -0700260}
261
egdaniel2d721d32015-11-11 13:06:05 -0800262void GrGLSLShaderBuilder::finalize(uint32_t visibility) {
joshualitt43466a12015-02-13 17:18:27 -0800263 SkASSERT(!fFinalized);
egdaniel574a4c12015-11-02 06:22:44 -0800264 this->compileAndAppendLayoutQualifiers();
egdanielc8a66eb2015-11-02 07:46:25 -0800265 SkASSERT(visibility);
cdalton5e58cee2016-02-11 12:49:47 -0800266 fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms());
egdaniel574a4c12015-11-02 06:22:44 -0800267 this->appendDecls(fInputs, &this->inputs());
egdaniel574a4c12015-11-02 06:22:44 -0800268 this->appendDecls(fOutputs, &this->outputs());
269 this->onFinalize();
joshualitt43466a12015-02-13 17:18:27 -0800270 // append the 'footer' to code
271 this->code().append("}");
272
273 for (int i = 0; i <= fCodeIndex; i++) {
Brian Osman6c431d52019-04-15 16:31:54 -0400274 fCompilerString.append(fShaderStrings[i].c_str(), fShaderStrings[i].size());
joshualitt43466a12015-02-13 17:18:27 -0800275 }
276
joshualitt43466a12015-02-13 17:18:27 -0800277 fFinalized = true;
joshualitt43466a12015-02-13 17:18:27 -0800278}