blob: e885d36a4373708cf46c609a565af119737d265b [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
8#include "GrGLShaderBuilder.h"
joshualitt47bb3822014-10-07 16:43:25 -07009#include "GrGLProgramBuilder.h"
joshualitt43466a12015-02-13 17:18:27 -080010#include "GrGLShaderStringBuilder.h"
jvanverth39edf762014-12-22 11:44:19 -080011#include "../GrGLGpu.h"
joshualitt30ba4362014-08-21 20:18:45 -070012#include "../GrGLShaderVar.h"
13
14namespace {
15inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) {
16 if (kVec2f_GrSLType == type) {
17 return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D";
18 } else {
19 SkASSERT(kVec3f_GrSLType == type);
20 return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj";
21 }
22}
23void append_texture_lookup(SkString* out,
bsalomon861e1032014-12-16 07:33:49 -080024 GrGLGpu* gpu,
joshualitt30ba4362014-08-21 20:18:45 -070025 const char* samplerName,
26 const char* coordName,
27 uint32_t configComponentMask,
28 const char* swizzle,
29 GrSLType varyingType = kVec2f_GrSLType) {
bsalomon49f085d2014-09-05 13:34:00 -070030 SkASSERT(coordName);
joshualitt30ba4362014-08-21 20:18:45 -070031
32 out->appendf("%s(%s, %s)",
33 sample_function_name(varyingType, gpu->glslGeneration()),
34 samplerName,
35 coordName);
36
37 char mangledSwizzle[5];
38
39 // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle
40 // is available.
41 if (!gpu->glCaps().textureSwizzleSupport() &&
42 (kA_GrColorComponentFlag == configComponentMask)) {
43 char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a';
44 int i;
45 for (i = 0; '\0' != swizzle[i]; ++i) {
46 mangledSwizzle[i] = alphaChar;
47 }
48 mangledSwizzle[i] ='\0';
49 swizzle = mangledSwizzle;
50 }
51 // For shader prettiness we omit the swizzle rather than appending ".rgba".
52 if (memcmp(swizzle, "rgba", 4)) {
53 out->appendf(".%s", swizzle);
54 }
55}
joshualitt30ba4362014-08-21 20:18:45 -070056}
57
58GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program)
59 : fProgramBuilder(program)
joshualitt47bb3822014-10-07 16:43:25 -070060 , fInputs(GrGLProgramBuilder::kVarsPerBlock)
61 , fOutputs(GrGLProgramBuilder::kVarsPerBlock)
joshualitt43466a12015-02-13 17:18:27 -080062 , fFeaturesAddedMask(0)
63 , fCodeIndex(kCode)
64 , fFinalized(false) {
65 // We push back some dummy pointers which will later become our header
66 for (int i = 0; i <= kCode; i++) {
67 fShaderStrings.push_back();
68 fCompilerStrings.push_back(NULL);
69 fCompilerStringLengths.push_back(0);
70 }
71
72 this->main() = "void main() {";
joshualitt30ba4362014-08-21 20:18:45 -070073}
74
egdanielb2f94d12014-08-29 10:08:36 -070075void GrGLShaderBuilder::declAppend(const GrGLShaderVar& var) {
76 SkString tempDecl;
77 var.appendDecl(fProgramBuilder->ctxInfo(), &tempDecl);
78 this->codeAppendf("%s;", tempDecl.c_str());
79}
80
joshualitt30ba4362014-08-21 20:18:45 -070081void GrGLShaderBuilder::emitFunction(GrSLType returnType,
82 const char* name,
83 int argCnt,
84 const GrGLShaderVar* args,
85 const char* body,
86 SkString* outName) {
joshualitt43466a12015-02-13 17:18:27 -080087 this->functions().append(GrGLSLTypeString(returnType));
joshualitt30ba4362014-08-21 20:18:45 -070088 fProgramBuilder->nameVariable(outName, '\0', name);
joshualitt43466a12015-02-13 17:18:27 -080089 this->functions().appendf(" %s", outName->c_str());
90 this->functions().append("(");
joshualitt30ba4362014-08-21 20:18:45 -070091 const GrGLContextInfo& ctxInfo = fProgramBuilder->gpu()->ctxInfo();
92 for (int i = 0; i < argCnt; ++i) {
joshualitt43466a12015-02-13 17:18:27 -080093 args[i].appendDecl(ctxInfo, &this->functions());
joshualitt30ba4362014-08-21 20:18:45 -070094 if (i < argCnt - 1) {
joshualitt43466a12015-02-13 17:18:27 -080095 this->functions().append(", ");
joshualitt30ba4362014-08-21 20:18:45 -070096 }
97 }
joshualitt43466a12015-02-13 17:18:27 -080098 this->functions().append(") {\n");
99 this->functions().append(body);
100 this->functions().append("}\n\n");
joshualitt30ba4362014-08-21 20:18:45 -0700101}
102
103void GrGLShaderBuilder::appendTextureLookup(SkString* out,
104 const TextureSampler& sampler,
105 const char* coordName,
106 GrSLType varyingType) const {
107 append_texture_lookup(out,
108 fProgramBuilder->gpu(),
joshualitt23e280d2014-09-18 12:26:38 -0700109 fProgramBuilder->getUniformCStr(sampler.fSamplerUniform),
joshualitt30ba4362014-08-21 20:18:45 -0700110 coordName,
111 sampler.configComponentMask(),
112 sampler.swizzle(),
113 varyingType);
114}
115
116void GrGLShaderBuilder::appendTextureLookup(const TextureSampler& sampler,
117 const char* coordName,
118 GrSLType varyingType) {
joshualitt43466a12015-02-13 17:18:27 -0800119 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType);
joshualitt30ba4362014-08-21 20:18:45 -0700120}
121
122void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation,
123 const TextureSampler& sampler,
124 const char* coordName,
125 GrSLType varyingType) {
126 SkString lookup;
127 this->appendTextureLookup(&lookup, sampler, coordName, varyingType);
128 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str());
129}
130
131
132const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) {
133 if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) {
134 if (caps.textureRedSupport()) {
135 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED };
136 return gRedSmear;
137 } else {
138 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
139 GR_GL_ALPHA, GR_GL_ALPHA };
140 return gAlphaSmear;
141 }
142 } else {
143 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA };
144 return gStraight;
145 }
146}
147
148void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
149 if (!(featureBit & fFeaturesAddedMask)) {
joshualitt43466a12015-02-13 17:18:27 -0800150 this->extensions().appendf("#extension %s: require\n", extensionName);
151 fFeaturesAddedMask |= featureBit;
joshualitt30ba4362014-08-21 20:18:45 -0700152 }
153}
154
joshualitt47bb3822014-10-07 16:43:25 -0700155void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
156 for (int i = 0; i < vars.count(); ++i) {
157 vars[i].appendDecl(fProgramBuilder->ctxInfo(), out);
158 out->append(";\n");
159 }
160}
161
joshualitt30ba4362014-08-21 20:18:45 -0700162void GrGLShaderBuilder::appendTextureLookup(const char* samplerName,
163 const char* coordName,
164 uint32_t configComponentMask,
165 const char* swizzle) {
joshualitt43466a12015-02-13 17:18:27 -0800166 append_texture_lookup(&this->code(),
joshualitt30ba4362014-08-21 20:18:45 -0700167 fProgramBuilder->gpu(),
168 samplerName,
169 coordName,
170 configComponentMask,
171 swizzle,
172 kVec2f_GrSLType);
173}
joshualitt43466a12015-02-13 17:18:27 -0800174
cdaltone4017d82015-05-06 11:48:56 -0700175void GrGLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
176 SkASSERT(fProgramBuilder->gpu()->glslGeneration() >= k330_GrGLSLGeneration);
177 fLayoutParams[interface].push_back() = param;
178}
179
180void GrGLShaderBuilder::compileAndAppendLayoutQualifiers() {
181 static const char* interfaceQualifierNames[] = {
182 "out"
183 };
184
185 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
186 const SkTArray<SkString>& params = fLayoutParams[interface];
187 if (params.empty()) {
188 continue;
189 }
190 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
191 for (int i = 1; i < params.count(); ++i) {
192 this->layoutQualifiers().appendf(", %s", params[i].c_str());
193 }
194 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
195 }
196
197 GR_STATIC_ASSERT(0 == GrGLShaderBuilder::kOut_InterfaceQualifier);
198 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
199}
200
joshualitt43466a12015-02-13 17:18:27 -0800201bool
202GrGLShaderBuilder::finalize(GrGLuint programId, GrGLenum type, SkTDArray<GrGLuint>* shaderIds) {
203 SkASSERT(!fFinalized);
204 // append the 'footer' to code
205 this->code().append("}");
206
207 for (int i = 0; i <= fCodeIndex; i++) {
208 fCompilerStrings[i] = fShaderStrings[i].c_str();
209 fCompilerStringLengths[i] = (int)fShaderStrings[i].size();
210 }
211
212 GrGLGpu* gpu = fProgramBuilder->gpu();
213 GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(),
214 programId,
215 type,
216 fCompilerStrings.begin(),
217 fCompilerStringLengths.begin(),
218 fCompilerStrings.count(),
219 gpu->stats());
220
221 fFinalized = true;
222
223 if (!shaderId) {
224 return false;
225 }
226
227 *shaderIds->append() = shaderId;
228
229 return true;
230}