blob: 1031f84460acf3089afac81ee8ffd5c4f93c5005 [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"
egdaniel0d3f0612015-10-21 10:45:48 -070011#include "glsl/GrGLSLShaderVar.h"
cdalton3f6f76f2016-04-11 12:18:09 -070012#include "glsl/GrGLSLSampler.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
egdaniel2d721d32015-11-11 13:06:05 -080065void GrGLSLShaderBuilder::appendTextureLookup(SkString* out,
cdalton3f6f76f2016-04-11 12:18:09 -070066 const GrGLSLSampler& sampler,
egdaniel2d721d32015-11-11 13:06:05 -080067 const char* coordName,
68 GrSLType varyingType) const {
bsalomoncdee0092016-01-08 13:20:12 -080069 const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
egdaniel7ea439b2015-12-03 09:20:44 -080070 GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler();
bsalomone5286e02016-01-14 09:24:09 -080071 GrSLType samplerType = uniformHandler->getUniformVariable(sampler.fSamplerUniform).getType();
bsalomone179a912016-01-20 06:18:10 -080072 if (samplerType == kSampler2DRect_GrSLType) {
73 if (varyingType == kVec2f_GrSLType) {
74 out->appendf("%s(%s, textureSize(%s) * %s)",
75 GrGLSLTexture2DFunctionName(varyingType, samplerType,
76 glslCaps->generation()),
77 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
78 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
79 coordName);
80 } else {
81 out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))",
82 GrGLSLTexture2DFunctionName(varyingType, samplerType,
83 glslCaps->generation()),
84 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
85 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
86 coordName,
87 coordName);
88 }
89 } else {
90 out->appendf("%s(%s, %s)",
91 GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()),
92 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
93 coordName);
94 }
bsalomoncdee0092016-01-08 13:20:12 -080095
cdaltonf8a6ce82016-04-11 13:02:05 -070096 this->appendTextureSwizzle(out, sampler.config());
joshualitt30ba4362014-08-21 20:18:45 -070097}
98
cdalton3f6f76f2016-04-11 12:18:09 -070099void GrGLSLShaderBuilder::appendTextureLookup(const GrGLSLSampler& sampler,
egdaniel2d721d32015-11-11 13:06:05 -0800100 const char* coordName,
101 GrSLType varyingType) {
joshualitt43466a12015-02-13 17:18:27 -0800102 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType);
joshualitt30ba4362014-08-21 20:18:45 -0700103}
104
egdaniel2d721d32015-11-11 13:06:05 -0800105void GrGLSLShaderBuilder::appendTextureLookupAndModulate(const char* modulation,
cdalton3f6f76f2016-04-11 12:18:09 -0700106 const GrGLSLSampler& sampler,
egdaniel2d721d32015-11-11 13:06:05 -0800107 const char* coordName,
108 GrSLType varyingType) {
joshualitt30ba4362014-08-21 20:18:45 -0700109 SkString lookup;
110 this->appendTextureLookup(&lookup, sampler, coordName, varyingType);
111 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str());
112}
113
cdaltonf8a6ce82016-04-11 13:02:05 -0700114void GrGLSLShaderBuilder::appendTexelFetch(SkString* out,
115 const GrGLSLSampler& sampler,
116 const char* coordExpr) const {
117 const GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler();
118 SkASSERT(fProgramBuilder->glslCaps()->texelFetchSupport());
119 SkASSERT(GrSLTypeIsSamplerType(
120 uniformHandler->getUniformVariable(sampler.fSamplerUniform).getType()));
121
122 out->appendf("texelFetch(%s, %s)",
123 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
124 coordExpr);
125
126 this->appendTextureSwizzle(out, sampler.config());
127}
128
129void GrGLSLShaderBuilder::appendTexelFetch(const GrGLSLSampler& sampler, const char* coordExpr) {
130 this->appendTexelFetch(&this->code(), sampler, coordExpr);
131}
132
133void GrGLSLShaderBuilder::appendTextureSwizzle(SkString* out, GrPixelConfig config) const {
134 const GrSwizzle& configSwizzle = fProgramBuilder->glslCaps()->configTextureSwizzle(config);
135
136 if (configSwizzle != GrSwizzle::RGBA()) {
137 out->appendf(".%s", configSwizzle.c_str());
138 }
139}
140
cdalton33ad7012016-02-22 07:55:44 -0800141bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
142 if (featureBit & fFeaturesAddedMask) {
143 return false;
joshualitt30ba4362014-08-21 20:18:45 -0700144 }
cdalton33ad7012016-02-22 07:55:44 -0800145 this->extensions().appendf("#extension %s: require\n", extensionName);
146 fFeaturesAddedMask |= featureBit;
147 return true;
joshualitt30ba4362014-08-21 20:18:45 -0700148}
149
egdaniel2d721d32015-11-11 13:06:05 -0800150void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
joshualitt47bb3822014-10-07 16:43:25 -0700151 for (int i = 0; i < vars.count(); ++i) {
egdanielf5294392015-10-21 07:14:17 -0700152 vars[i].appendDecl(fProgramBuilder->glslCaps(), out);
joshualitt47bb3822014-10-07 16:43:25 -0700153 out->append(";\n");
154 }
155}
156
egdaniel2d721d32015-11-11 13:06:05 -0800157void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
egdaniel574a4c12015-11-02 06:22:44 -0800158 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration ||
159 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs());
cdaltone4017d82015-05-06 11:48:56 -0700160 fLayoutParams[interface].push_back() = param;
161}
162
egdaniel2d721d32015-11-11 13:06:05 -0800163void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() {
cdaltone4017d82015-05-06 11:48:56 -0700164 static const char* interfaceQualifierNames[] = {
165 "out"
166 };
167
168 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
169 const SkTArray<SkString>& params = fLayoutParams[interface];
170 if (params.empty()) {
171 continue;
172 }
173 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
174 for (int i = 1; i < params.count(); ++i) {
175 this->layoutQualifiers().appendf(", %s", params[i].c_str());
176 }
177 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
178 }
179
egdaniel2d721d32015-11-11 13:06:05 -0800180 GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier);
cdaltone4017d82015-05-06 11:48:56 -0700181 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
182}
183
egdaniel2d721d32015-11-11 13:06:05 -0800184void GrGLSLShaderBuilder::finalize(uint32_t visibility) {
joshualitt43466a12015-02-13 17:18:27 -0800185 SkASSERT(!fFinalized);
egdaniel574a4c12015-11-02 06:22:44 -0800186 this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString();
187 this->compileAndAppendLayoutQualifiers();
egdanielc8a66eb2015-11-02 07:46:25 -0800188 SkASSERT(visibility);
cdalton5e58cee2016-02-11 12:49:47 -0800189 fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms());
egdaniel574a4c12015-11-02 06:22:44 -0800190 this->appendDecls(fInputs, &this->inputs());
egdaniel574a4c12015-11-02 06:22:44 -0800191 this->appendDecls(fOutputs, &this->outputs());
192 this->onFinalize();
joshualitt43466a12015-02-13 17:18:27 -0800193 // append the 'footer' to code
194 this->code().append("}");
195
196 for (int i = 0; i <= fCodeIndex; i++) {
197 fCompilerStrings[i] = fShaderStrings[i].c_str();
198 fCompilerStringLengths[i] = (int)fShaderStrings[i].size();
199 }
200
joshualitt43466a12015-02-13 17:18:27 -0800201 fFinalized = true;
joshualitt43466a12015-02-13 17:18:27 -0800202}