blob: a2cf66cb4097c591f6a5014328356291d8acc09c [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"
egdaniel7dc4bd02015-10-29 07:57:01 -070012#include "glsl/GrGLSLTextureSampler.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
egdaniel2d721d32015-11-11 13:06:05 -080038void GrGLSLShaderBuilder::emitFunction(GrSLType returnType,
39 const char* name,
40 int argCnt,
41 const GrGLSLShaderVar* args,
42 const char* body,
43 SkString* outName) {
joshualitt43466a12015-02-13 17:18:27 -080044 this->functions().append(GrGLSLTypeString(returnType));
joshualitt30ba4362014-08-21 20:18:45 -070045 fProgramBuilder->nameVariable(outName, '\0', name);
joshualitt43466a12015-02-13 17:18:27 -080046 this->functions().appendf(" %s", outName->c_str());
47 this->functions().append("(");
joshualitt30ba4362014-08-21 20:18:45 -070048 for (int i = 0; i < argCnt; ++i) {
egdanielf5294392015-10-21 07:14:17 -070049 args[i].appendDecl(fProgramBuilder->glslCaps(), &this->functions());
joshualitt30ba4362014-08-21 20:18:45 -070050 if (i < argCnt - 1) {
joshualitt43466a12015-02-13 17:18:27 -080051 this->functions().append(", ");
joshualitt30ba4362014-08-21 20:18:45 -070052 }
53 }
joshualitt43466a12015-02-13 17:18:27 -080054 this->functions().append(") {\n");
55 this->functions().append(body);
56 this->functions().append("}\n\n");
joshualitt30ba4362014-08-21 20:18:45 -070057}
58
egdaniel2d721d32015-11-11 13:06:05 -080059void GrGLSLShaderBuilder::appendTextureLookup(SkString* out,
60 const GrGLSLTextureSampler& sampler,
61 const char* coordName,
62 GrSLType varyingType) const {
bsalomoncdee0092016-01-08 13:20:12 -080063 const GrGLSLCaps* glslCaps = fProgramBuilder->glslCaps();
egdaniel7ea439b2015-12-03 09:20:44 -080064 GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler();
bsalomone5286e02016-01-14 09:24:09 -080065 GrSLType samplerType = uniformHandler->getUniformVariable(sampler.fSamplerUniform).getType();
bsalomone179a912016-01-20 06:18:10 -080066 if (samplerType == kSampler2DRect_GrSLType) {
67 if (varyingType == kVec2f_GrSLType) {
68 out->appendf("%s(%s, textureSize(%s) * %s)",
69 GrGLSLTexture2DFunctionName(varyingType, samplerType,
70 glslCaps->generation()),
71 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
72 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
73 coordName);
74 } else {
75 out->appendf("%s(%s, vec3(textureSize(%s) * %s.xy, %s.z))",
76 GrGLSLTexture2DFunctionName(varyingType, samplerType,
77 glslCaps->generation()),
78 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
79 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
80 coordName,
81 coordName);
82 }
83 } else {
84 out->appendf("%s(%s, %s)",
85 GrGLSLTexture2DFunctionName(varyingType, samplerType, glslCaps->generation()),
86 uniformHandler->getUniformCStr(sampler.fSamplerUniform),
87 coordName);
88 }
bsalomoncdee0092016-01-08 13:20:12 -080089
90 // This refers to any swizzling we may need to get from some backend internal format to the
91 // format used in GrPixelConfig. If this is implemented by the GrGpu object, then swizzle will
92 // be rgba. For shader prettiness we omit the swizzle rather than appending ".rgba".
93 const GrSwizzle& configSwizzle = glslCaps->configTextureSwizzle(sampler.config());
94
95 if (configSwizzle != GrSwizzle::RGBA()) {
96 out->appendf(".%s", configSwizzle.c_str());
97 }
joshualitt30ba4362014-08-21 20:18:45 -070098}
99
egdaniel2d721d32015-11-11 13:06:05 -0800100void GrGLSLShaderBuilder::appendTextureLookup(const GrGLSLTextureSampler& sampler,
101 const char* coordName,
102 GrSLType varyingType) {
joshualitt43466a12015-02-13 17:18:27 -0800103 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType);
joshualitt30ba4362014-08-21 20:18:45 -0700104}
105
egdaniel2d721d32015-11-11 13:06:05 -0800106void GrGLSLShaderBuilder::appendTextureLookupAndModulate(const char* modulation,
107 const GrGLSLTextureSampler& sampler,
108 const char* coordName,
109 GrSLType varyingType) {
joshualitt30ba4362014-08-21 20:18:45 -0700110 SkString lookup;
111 this->appendTextureLookup(&lookup, sampler, coordName, varyingType);
112 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str());
113}
114
egdaniel2d721d32015-11-11 13:06:05 -0800115void GrGLSLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
joshualitt30ba4362014-08-21 20:18:45 -0700116 if (!(featureBit & fFeaturesAddedMask)) {
joshualitt43466a12015-02-13 17:18:27 -0800117 this->extensions().appendf("#extension %s: require\n", extensionName);
118 fFeaturesAddedMask |= featureBit;
joshualitt30ba4362014-08-21 20:18:45 -0700119 }
120}
121
egdaniel2d721d32015-11-11 13:06:05 -0800122void GrGLSLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
joshualitt47bb3822014-10-07 16:43:25 -0700123 for (int i = 0; i < vars.count(); ++i) {
egdanielf5294392015-10-21 07:14:17 -0700124 vars[i].appendDecl(fProgramBuilder->glslCaps(), out);
joshualitt47bb3822014-10-07 16:43:25 -0700125 out->append(";\n");
126 }
127}
128
egdaniel2d721d32015-11-11 13:06:05 -0800129void GrGLSLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
egdaniel574a4c12015-11-02 06:22:44 -0800130 SkASSERT(fProgramBuilder->glslCaps()->generation() >= k330_GrGLSLGeneration ||
131 fProgramBuilder->glslCaps()->mustEnableAdvBlendEqs());
cdaltone4017d82015-05-06 11:48:56 -0700132 fLayoutParams[interface].push_back() = param;
133}
134
egdaniel2d721d32015-11-11 13:06:05 -0800135void GrGLSLShaderBuilder::compileAndAppendLayoutQualifiers() {
cdaltone4017d82015-05-06 11:48:56 -0700136 static const char* interfaceQualifierNames[] = {
137 "out"
138 };
139
140 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
141 const SkTArray<SkString>& params = fLayoutParams[interface];
142 if (params.empty()) {
143 continue;
144 }
145 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
146 for (int i = 1; i < params.count(); ++i) {
147 this->layoutQualifiers().appendf(", %s", params[i].c_str());
148 }
149 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
150 }
151
egdaniel2d721d32015-11-11 13:06:05 -0800152 GR_STATIC_ASSERT(0 == GrGLSLShaderBuilder::kOut_InterfaceQualifier);
cdaltone4017d82015-05-06 11:48:56 -0700153 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
154}
155
egdaniel2d721d32015-11-11 13:06:05 -0800156void GrGLSLShaderBuilder::finalize(uint32_t visibility) {
joshualitt43466a12015-02-13 17:18:27 -0800157 SkASSERT(!fFinalized);
egdaniel574a4c12015-11-02 06:22:44 -0800158 this->versionDecl() = fProgramBuilder->glslCaps()->versionDeclString();
159 this->compileAndAppendLayoutQualifiers();
egdanielc8a66eb2015-11-02 07:46:25 -0800160 SkASSERT(visibility);
egdaniel7ea439b2015-12-03 09:20:44 -0800161 fProgramBuilder->appendUniformDecls((GrGLSLUniformHandler::ShaderVisibility) visibility,
egdaniel574a4c12015-11-02 06:22:44 -0800162 &this->uniforms());
163 this->appendDecls(fInputs, &this->inputs());
egdaniel574a4c12015-11-02 06:22:44 -0800164 this->appendDecls(fOutputs, &this->outputs());
165 this->onFinalize();
joshualitt43466a12015-02-13 17:18:27 -0800166 // append the 'footer' to code
167 this->code().append("}");
168
169 for (int i = 0; i <= fCodeIndex; i++) {
170 fCompilerStrings[i] = fShaderStrings[i].c_str();
171 fCompilerStringLengths[i] = (int)fShaderStrings[i].size();
172 }
173
joshualitt43466a12015-02-13 17:18:27 -0800174 fFinalized = true;
joshualitt43466a12015-02-13 17:18:27 -0800175}
egdaniel574a4c12015-11-02 06:22:44 -0800176