blob: 1e52a11013942636fb75495db4b1b8ebc4ff2527 [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,
egdaniel09aa1fc2016-04-20 07:09:46 -070066 SamplerHandle samplerHandle,
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();
egdaniel09aa1fc2016-04-20 07:09:46 -070070 const GrGLSLSampler& sampler = fProgramBuilder->getSampler(samplerHandle);
71 GrSLType samplerType = sampler.type();
egdaniel990dbc82016-07-13 14:09:30 -070072 if (samplerType == kTexture2DRectSampler_GrSLType) {
bsalomone179a912016-01-20 06:18:10 -080073 if (varyingType == kVec2f_GrSLType) {
74 out->appendf("%s(%s, textureSize(%s) * %s)",
75 GrGLSLTexture2DFunctionName(varyingType, samplerType,
76 glslCaps->generation()),
egdaniel09aa1fc2016-04-20 07:09:46 -070077 sampler.getSamplerNameForTexture2D(),
78 sampler.getSamplerNameForTexture2D(),
bsalomone179a912016-01-20 06:18:10 -080079 coordName);
80 } else {
81 out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))",
82 GrGLSLTexture2DFunctionName(varyingType, samplerType,
83 glslCaps->generation()),
egdaniel09aa1fc2016-04-20 07:09:46 -070084 sampler.getSamplerNameForTexture2D(),
85 sampler.getSamplerNameForTexture2D(),
bsalomone179a912016-01-20 06:18:10 -080086 coordName,
87 coordName);
88 }
89 } else {
90 out->appendf("%s(%s, %s)",
91 GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()),
egdaniel09aa1fc2016-04-20 07:09:46 -070092 sampler.getSamplerNameForTexture2D(),
bsalomone179a912016-01-20 06:18:10 -080093 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
egdaniel09aa1fc2016-04-20 07:09:46 -070099void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle,
egdaniel2d721d32015-11-11 13:06:05 -0800100 const char* coordName,
101 GrSLType varyingType) {
egdaniel09aa1fc2016-04-20 07:09:46 -0700102 this->appendTextureLookup(&this->code(), samplerHandle, coordName, varyingType);
joshualitt30ba4362014-08-21 20:18:45 -0700103}
104
egdaniel2d721d32015-11-11 13:06:05 -0800105void GrGLSLShaderBuilder::appendTextureLookupAndModulate(const char* modulation,
egdaniel09aa1fc2016-04-20 07:09:46 -0700106 SamplerHandle samplerHandle,
egdaniel2d721d32015-11-11 13:06:05 -0800107 const char* coordName,
108 GrSLType varyingType) {
joshualitt30ba4362014-08-21 20:18:45 -0700109 SkString lookup;
egdaniel09aa1fc2016-04-20 07:09:46 -0700110 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
joshualitt30ba4362014-08-21 20:18:45 -0700111 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str());
112}
113
cdaltonf8a6ce82016-04-11 13:02:05 -0700114void GrGLSLShaderBuilder::appendTexelFetch(SkString* out,
egdaniel09aa1fc2016-04-20 07:09:46 -0700115 SamplerHandle samplerHandle,
cdaltonf8a6ce82016-04-11 13:02:05 -0700116 const char* coordExpr) const {
egdaniel09aa1fc2016-04-20 07:09:46 -0700117 const GrGLSLSampler& sampler = fProgramBuilder->getSampler(samplerHandle);
cdaltonf8a6ce82016-04-11 13:02:05 -0700118 SkASSERT(fProgramBuilder->glslCaps()->texelFetchSupport());
egdaniel990dbc82016-07-13 14:09:30 -0700119 SkASSERT(GrSLTypeIsCombinedSamplerType(sampler.type()));
cdaltonf8a6ce82016-04-11 13:02:05 -0700120
egdaniel09aa1fc2016-04-20 07:09:46 -0700121 out->appendf("texelFetch(%s, %s)", sampler.getSamplerNameForTexelFetch(), coordExpr);
cdaltonf8a6ce82016-04-11 13:02:05 -0700122
123 this->appendTextureSwizzle(out, sampler.config());
124}
125
egdaniel09aa1fc2016-04-20 07:09:46 -0700126void GrGLSLShaderBuilder::appendTexelFetch(SamplerHandle samplerHandle, const char* coordExpr) {
127 this->appendTexelFetch(&this->code(), samplerHandle, coordExpr);
cdaltonf8a6ce82016-04-11 13:02:05 -0700128}
129
130void GrGLSLShaderBuilder::appendTextureSwizzle(SkString* out, GrPixelConfig config) const {
131 const GrSwizzle& configSwizzle = fProgramBuilder->glslCaps()->configTextureSwizzle(config);
132
133 if (configSwizzle != GrSwizzle::RGBA()) {
134 out->appendf(".%s", configSwizzle.c_str());
135 }
136}
137
cdalton33ad7012016-02-22 07:55:44 -0800138bool GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
139 if (featureBit & fFeaturesAddedMask) {
140 return false;
joshualitt30ba4362014-08-21 20:18:45 -0700141 }
cdalton33ad7012016-02-22 07:55:44 -0800142 this->extensions().appendf("#extension %s: require\n", extensionName);
143 fFeaturesAddedMask |= featureBit;
144 return true;
joshualitt30ba4362014-08-21 20:18:45 -0700145}
146
egdaniel2d721d32015-11-11 13:06:05 -0800147void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
joshualitt47bb3822014-10-07 16:43:25 -0700148 for (int i = 0; i < vars.count(); ++i) {
egdanielf5294392015-10-21 07:14:17 -0700149 vars[i].appendDecl(fProgramBuilder->glslCaps(), out);
joshualitt47bb3822014-10-07 16:43:25 -0700150 out->append(";\n");
151 }
152}
153
egdaniel2d721d32015-11-11 13:06:05 -0800154void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
egdaniel574a4c12015-11-02 06:22:44 -0800155 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration ||
156 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs());
cdaltone4017d82015-05-06 11:48:56 -0700157 fLayoutParams[interface].push_back() = param;
158}
159
egdaniel2d721d32015-11-11 13:06:05 -0800160void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() {
cdaltone4017d82015-05-06 11:48:56 -0700161 static const char* interfaceQualifierNames[] = {
162 "out"
163 };
164
165 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
166 const SkTArray<SkString>& params = fLayoutParams[interface];
167 if (params.empty()) {
168 continue;
169 }
170 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
171 for (int i = 1; i < params.count(); ++i) {
172 this->layoutQualifiers().appendf(", %s", params[i].c_str());
173 }
174 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
175 }
176
egdaniel2d721d32015-11-11 13:06:05 -0800177 GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier);
cdaltone4017d82015-05-06 11:48:56 -0700178 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
179}
180
egdaniel2d721d32015-11-11 13:06:05 -0800181void GrGLSLShaderBuilder::finalize(uint32_t visibility) {
joshualitt43466a12015-02-13 17:18:27 -0800182 SkASSERT(!fFinalized);
egdaniel574a4c12015-11-02 06:22:44 -0800183 this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString();
184 this->compileAndAppendLayoutQualifiers();
egdanielc8a66eb2015-11-02 07:46:25 -0800185 SkASSERT(visibility);
cdalton5e58cee2016-02-11 12:49:47 -0800186 fProgramBuilder->appendUniformDecls((GrShaderFlags) visibility, &this->uniforms());
egdaniel574a4c12015-11-02 06:22:44 -0800187 this->appendDecls(fInputs, &this->inputs());
egdaniel574a4c12015-11-02 06:22:44 -0800188 this->appendDecls(fOutputs, &this->outputs());
189 this->onFinalize();
joshualitt43466a12015-02-13 17:18:27 -0800190 // append the 'footer' to code
191 this->code().append("}");
192
193 for (int i = 0; i <= fCodeIndex; i++) {
194 fCompilerStrings[i] = fShaderStrings[i].c_str();
195 fCompilerStringLengths[i] = (int)fShaderStrings[i].size();
196 }
197
joshualitt43466a12015-02-13 17:18:27 -0800198 fFinalized = true;
joshualitt43466a12015-02-13 17:18:27 -0800199}