blob: 2e7977938a2199b116483ede92a0ae0f0c71b495 [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
bsalomoncdee0092016-01-08 13:20:12 -08008#include "GrSwizzle.h"
egdaniel2d721d32015-11-11 13:06:05 -08009#include "glsl/GrGLSLShaderBuilder.h"
jvanverthcba99b82015-06-24 06:59:57 -070010#include "glsl/GrGLSLCaps.h"
brianosman77320db2016-09-07 08:09:10 -070011#include "glsl/GrGLSLColorSpaceXformHelper.h"
egdaniel0d3f0612015-10-21 10:45:48 -070012#include "glsl/GrGLSLShaderVar.h"
egdaniel8dcdedc2015-11-11 06:27:20 -080013#include "glsl/GrGLSLProgramBuilder.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();
halcanary96fcdcc2015-08-27 07:41:13 -070025 fCompilerStrings.push_back(nullptr);
joshualitt43466a12015-02-13 17:18:27 -080026 fCompilerStringLengths.push_back(0);
27 }
28
29 this->main() = "void main() {";
joshualitt30ba4362014-08-21 20:18:45 -070030}
31
egdaniel2d721d32015-11-11 13:06:05 -080032void GrGLSLShaderBuilder::declAppend(const GrGLSLShaderVar& var) {
egdanielb2f94d12014-08-29 10:08:36 -070033 SkString tempDecl;
egdanielf5294392015-10-21 07:14:17 -070034 var.appendDecl(fProgramBuilder->glslCaps(), &tempDecl);
egdanielb2f94d12014-08-29 10:08:36 -070035 this->codeAppendf("%s;", tempDecl.c_str());
36}
37
cdalton1f50acf2016-04-11 11:30:50 -070038void GrGLSLShaderBuilder::appendPrecisionModifier(GrSLPrecision precision) {
39 if (fProgramBuilder->glslCaps()->usesPrecisionModifiers()) {
40 this->codeAppendf("%s ", GrGLSLPrecisionString(precision));
41 }
42}
43
egdaniel2d721d32015-11-11 13:06:05 -080044void GrGLSLShaderBuilder::emitFunction(GrSLType returnType,
45 const char* name,
46 int argCnt,
47 const GrGLSLShaderVar* args,
48 const char* body,
49 SkString* outName) {
joshualitt43466a12015-02-13 17:18:27 -080050 this->functions().append(GrGLSLTypeString(returnType));
joshualitt30ba4362014-08-21 20:18:45 -070051 fProgramBuilder->nameVariable(outName, '\0', name);
joshualitt43466a12015-02-13 17:18:27 -080052 this->functions().appendf(" %s", outName->c_str());
53 this->functions().append("(");
joshualitt30ba4362014-08-21 20:18:45 -070054 for (int i = 0; i < argCnt; ++i) {
egdanielf5294392015-10-21 07:14:17 -070055 args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions());
joshualitt30ba4362014-08-21 20:18:45 -070056 if (i < argCnt - 1) {
joshualitt43466a12015-02-13 17:18:27 -080057 this->functions().append(", ");
joshualitt30ba4362014-08-21 20:18:45 -070058 }
59 }
joshualitt43466a12015-02-13 17:18:27 -080060 this->functions().append(") {\n");
61 this->functions().append(body);
62 this->functions().append("}\n\n");
joshualitt30ba4362014-08-21 20:18:45 -070063}
64
Brian Salomon101b8442016-11-18 11:58:54 -050065static inline void append_texture_swizzle(SkString* out, GrSwizzle swizzle) {
66 if (swizzle != GrSwizzle::RGBA()) {
67 out->appendf(".%s", swizzle.c_str());
68 }
69}
70
egdaniel2d721d32015-11-11 13:06:05 -080071void GrGLSLShaderBuilder::appendTextureLookup(SkString* out,
egdaniel09aa1fc2016-04-20 07:09:46 -070072 SamplerHandle samplerHandle,
egdaniel2d721d32015-11-11 13:06:05 -080073 const char* coordName,
74 GrSLType varyingType) const {
bsalomoncdee0092016-01-08 13:20:12 -080075 const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
Brian Salomon101b8442016-11-18 11:58:54 -050076 const GrGLSLShaderVar& sampler = fProgramBuilder->samplerVariable(samplerHandle);
77 GrSLType samplerType = sampler.getType();
egdaniel990dbc82016-07-13 14:09:30 -070078 if (samplerType == kTexture2DRectSampler_GrSLType) {
bsalomone179a912016-01-20 06:18:10 -080079 if (varyingType == kVec2f_GrSLType) {
80 out->appendf("%s(%s, textureSize(%s) * %s)",
81 GrGLSLTexture2DFunctionName(varyingType, samplerType,
82 glslCaps->generation()),
Brian Salomon101b8442016-11-18 11:58:54 -050083 sampler.c_str(), sampler.c_str(), coordName);
bsalomone179a912016-01-20 06:18:10 -080084 } else {
85 out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))",
86 GrGLSLTexture2DFunctionName(varyingType, samplerType,
87 glslCaps->generation()),
Brian Salomon101b8442016-11-18 11:58:54 -050088 sampler.c_str(), sampler.c_str(), coordName, coordName);
bsalomone179a912016-01-20 06:18:10 -080089 }
90 } else {
91 out->appendf("%s(%s, %s)",
92 GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()),
Brian Salomon101b8442016-11-18 11:58:54 -050093 sampler.c_str(), coordName);
bsalomone179a912016-01-20 06:18:10 -080094 }
Brian Salomon101b8442016-11-18 11:58:54 -050095 append_texture_swizzle(out, fProgramBuilder->samplerSwizzle(samplerHandle));
joshualitt30ba4362014-08-21 20:18:45 -070096}
97
egdaniel09aa1fc2016-04-20 07:09:46 -070098void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle,
egdaniel2d721d32015-11-11 13:06:05 -080099 const char* coordName,
brianosman77320db2016-09-07 08:09:10 -0700100 GrSLType varyingType,
101 GrGLSLColorSpaceXformHelper* colorXformHelper) {
102 if (colorXformHelper && colorXformHelper->getXformMatrix()) {
103 // With a color gamut transform, we need to wrap the lookup in another function call
104 SkString lookup;
105 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
106 this->appendColorGamutXform(lookup.c_str(), colorXformHelper);
107 } else {
108 this->appendTextureLookup(&this->code(), samplerHandle, coordName, varyingType);
109 }
joshualitt30ba4362014-08-21 20:18:45 -0700110}
111
brianosman77320db2016-09-07 08:09:10 -0700112void GrGLSLShaderBuilder::appendTextureLookupAndModulate(
113 const char* modulation,
114 SamplerHandle samplerHandle,
115 const char* coordName,
116 GrSLType varyingType,
117 GrGLSLColorSpaceXformHelper* colorXformHelper) {
joshualitt30ba4362014-08-21 20:18:45 -0700118 SkString lookup;
egdaniel09aa1fc2016-04-20 07:09:46 -0700119 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
brianosman77320db2016-09-07 08:09:10 -0700120 if (colorXformHelper && colorXformHelper->getXformMatrix()) {
121 SkString xform;
122 this->appendColorGamutXform(&xform, lookup.c_str(), colorXformHelper);
123 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(xform)).c_str());
124 } else {
125 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str());
126 }
127}
128
129void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out,
130 const char* srcColor,
131 GrGLSLColorSpaceXformHelper* colorXformHelper) {
132 // Our color is (r, g, b, a), but we want to multiply (r, g, b, 1) by our matrix, then
133 // re-insert the original alpha. The supplied srcColor is likely to be of the form
brianosman51924752016-09-12 08:50:19 -0700134 // "texture(...)", and we don't want to evaluate that twice, so wrap everything in a function.
brianosman77320db2016-09-07 08:09:10 -0700135 static const GrGLSLShaderVar gColorGamutXformArgs[] = {
136 GrGLSLShaderVar("color", kVec4f_GrSLType),
137 GrGLSLShaderVar("xform", kMat44f_GrSLType),
138 };
139 SkString functionBody;
brianosman77320db2016-09-07 08:09:10 -0700140 // Gamut xform, clamp to destination gamut
brianosman8d914902016-09-09 11:30:55 -0700141 functionBody.append("\tcolor.rgb = clamp((xform * vec4(color.rgb, 1.0)).rgb, 0.0, 1.0);\n");
brianosman77320db2016-09-07 08:09:10 -0700142 functionBody.append("\treturn color;");
143 SkString colorGamutXformFuncName;
144 this->emitFunction(kVec4f_GrSLType,
145 "colorGamutXform",
146 SK_ARRAY_COUNT(gColorGamutXformArgs),
147 gColorGamutXformArgs,
148 functionBody.c_str(),
149 &colorGamutXformFuncName);
150
151 out->appendf("%s(%s, %s)", colorGamutXformFuncName.c_str(), srcColor,
152 colorXformHelper->getXformMatrix());
153}
154
155void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor,
156 GrGLSLColorSpaceXformHelper* colorXformHelper) {
157 SkString xform;
158 this->appendColorGamutXform(&xform, srcColor, colorXformHelper);
159 this->codeAppend(xform.c_str());
joshualitt30ba4362014-08-21 20:18:45 -0700160}
161
cdaltonf8a6ce82016-04-11 13:02:05 -0700162void GrGLSLShaderBuilder::appendTexelFetch(SkString* out,
egdaniel09aa1fc2016-04-20 07:09:46 -0700163 SamplerHandle samplerHandle,
cdaltonf8a6ce82016-04-11 13:02:05 -0700164 const char* coordExpr) const {
Brian Salomon101b8442016-11-18 11:58:54 -0500165 const GrGLSLShaderVar& sampler = fProgramBuilder->samplerVariable(samplerHandle);
cdaltonf8a6ce82016-04-11 13:02:05 -0700166 SkASSERT(fProgramBuilder->glslCaps()->texelFetchSupport());
Brian Salomon101b8442016-11-18 11:58:54 -0500167 SkASSERT(GrSLTypeIsCombinedSamplerType(sampler.getType()));
cdaltonf8a6ce82016-04-11 13:02:05 -0700168
Brian Salomon101b8442016-11-18 11:58:54 -0500169 out->appendf("texelFetch(%s, %s)", sampler.c_str(), coordExpr);
cdaltonf8a6ce82016-04-11 13:02:05 -0700170
Brian Salomon101b8442016-11-18 11:58:54 -0500171 append_texture_swizzle(out, fProgramBuilder->samplerSwizzle(samplerHandle));
cdaltonf8a6ce82016-04-11 13:02:05 -0700172}
173
egdaniel09aa1fc2016-04-20 07:09:46 -0700174void GrGLSLShaderBuilder::appendTexelFetch(SamplerHandle samplerHandle, const char* coordExpr) {
175 this->appendTexelFetch(&this->code(), samplerHandle, coordExpr);
cdaltonf8a6ce82016-04-11 13:02:05 -0700176}
177
cdalton33ad7012016-02-22 07:55:44 -0800178bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
179 if (featureBit & fFeaturesAddedMask) {
180 return false;
joshualitt30ba4362014-08-21 20:18:45 -0700181 }
cdalton33ad7012016-02-22 07:55:44 -0800182 this->extensions().appendf("#extension %s: require\n", extensionName);
183 fFeaturesAddedMask |= featureBit;
184 return true;
joshualitt30ba4362014-08-21 20:18:45 -0700185}
186
egdaniel2d721d32015-11-11 13:06:05 -0800187void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
joshualitt47bb3822014-10-07 16:43:25 -0700188 for (int i = 0; i < vars.count(); ++i) {
egdanielf5294392015-10-21 07:14:17 -0700189 vars[i].appendDecl(fProgramBuilder->glslCaps(), out);
joshualitt47bb3822014-10-07 16:43:25 -0700190 out->append(";\n");
191 }
192}
193
egdaniel2d721d32015-11-11 13:06:05 -0800194void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
egdaniel574a4c12015-11-02 06:22:44 -0800195 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration ||
196 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs());
cdaltone4017d82015-05-06 11:48:56 -0700197 fLayoutParams[interface].push_back() = param;
198}
199
egdaniel2d721d32015-11-11 13:06:05 -0800200void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() {
cdaltone4017d82015-05-06 11:48:56 -0700201 static const char* interfaceQualifierNames[] = {
202 "out"
203 };
204
205 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
206 const SkTArray<SkString>& params = fLayoutParams[interface];
207 if (params.empty()) {
208 continue;
209 }
210 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
211 for (int i = 1; i < params.count(); ++i) {
212 this->layoutQualifiers().appendf(", %s", params[i].c_str());
213 }
214 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
215 }
216
egdaniel2d721d32015-11-11 13:06:05 -0800217 GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier);
cdaltone4017d82015-05-06 11:48:56 -0700218 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
219}
220
egdaniel2d721d32015-11-11 13:06:05 -0800221void GrGLSLShaderBuilder::finalize(uint32_t visibility) {
joshualitt43466a12015-02-13 17:18:27 -0800222 SkASSERT(!fFinalized);
egdaniel574a4c12015-11-02 06:22:44 -0800223 this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString();
224 this->compileAndAppendLayoutQualifiers();
egdanielc8a66eb2015-11-02 07:46:25 -0800225 SkASSERT(visibility);
cdalton5e58cee2016-02-11 12:49:47 -0800226 fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms());
egdaniel574a4c12015-11-02 06:22:44 -0800227 this->appendDecls(fInputs, &this->inputs());
egdaniel574a4c12015-11-02 06:22:44 -0800228 this->appendDecls(fOutputs, &this->outputs());
229 this->onFinalize();
joshualitt43466a12015-02-13 17:18:27 -0800230 // append the 'footer' to code
231 this->code().append("}");
232
233 for (int i = 0; i <= fCodeIndex; i++) {
234 fCompilerStrings[i] = fShaderStrings[i].c_str();
235 fCompilerStringLengths[i] = (int)fShaderStrings[i].size();
236 }
237
joshualitt43466a12015-02-13 17:18:27 -0800238 fFinalized = true;
joshualitt43466a12015-02-13 17:18:27 -0800239}