1 /*
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"
9 #include "GrGLProgramBuilder.h"
10 #include "GrGLShaderStringBuilder.h"
11 #include "../GrGLGpu.h"
12 #include "../GrGLShaderVar.h"
13
14 namespace {
sample_function_name(GrSLType type,GrGLSLGeneration glslGen)15 inline 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 }
append_texture_lookup(SkString * out,GrGLGpu * gpu,const char * samplerName,const char * coordName,uint32_t configComponentMask,const char * swizzle,GrSLType varyingType=kVec2f_GrSLType)23 void append_texture_lookup(SkString* out,
24 GrGLGpu* gpu,
25 const char* samplerName,
26 const char* coordName,
27 uint32_t configComponentMask,
28 const char* swizzle,
29 GrSLType varyingType = kVec2f_GrSLType) {
30 SkASSERT(coordName);
31
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 }
56 }
57
GrGLShaderBuilder(GrGLProgramBuilder * program)58 GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program)
59 : fProgramBuilder(program)
60 , fInputs(GrGLProgramBuilder::kVarsPerBlock)
61 , fOutputs(GrGLProgramBuilder::kVarsPerBlock)
62 , 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() {";
73 }
74
declAppend(const GrGLShaderVar & var)75 void GrGLShaderBuilder::declAppend(const GrGLShaderVar& var) {
76 SkString tempDecl;
77 var.appendDecl(fProgramBuilder->ctxInfo(), &tempDecl);
78 this->codeAppendf("%s;", tempDecl.c_str());
79 }
80
emitFunction(GrSLType returnType,const char * name,int argCnt,const GrGLShaderVar * args,const char * body,SkString * outName)81 void GrGLShaderBuilder::emitFunction(GrSLType returnType,
82 const char* name,
83 int argCnt,
84 const GrGLShaderVar* args,
85 const char* body,
86 SkString* outName) {
87 this->functions().append(GrGLSLTypeString(returnType));
88 fProgramBuilder->nameVariable(outName, '\0', name);
89 this->functions().appendf(" %s", outName->c_str());
90 this->functions().append("(");
91 const GrGLContextInfo& ctxInfo = fProgramBuilder->gpu()->ctxInfo();
92 for (int i = 0; i < argCnt; ++i) {
93 args[i].appendDecl(ctxInfo, &this->functions());
94 if (i < argCnt - 1) {
95 this->functions().append(", ");
96 }
97 }
98 this->functions().append(") {\n");
99 this->functions().append(body);
100 this->functions().append("}\n\n");
101 }
102
appendTextureLookup(SkString * out,const TextureSampler & sampler,const char * coordName,GrSLType varyingType) const103 void GrGLShaderBuilder::appendTextureLookup(SkString* out,
104 const TextureSampler& sampler,
105 const char* coordName,
106 GrSLType varyingType) const {
107 append_texture_lookup(out,
108 fProgramBuilder->gpu(),
109 fProgramBuilder->getUniformCStr(sampler.fSamplerUniform),
110 coordName,
111 sampler.configComponentMask(),
112 sampler.swizzle(),
113 varyingType);
114 }
115
appendTextureLookup(const TextureSampler & sampler,const char * coordName,GrSLType varyingType)116 void GrGLShaderBuilder::appendTextureLookup(const TextureSampler& sampler,
117 const char* coordName,
118 GrSLType varyingType) {
119 this->appendTextureLookup(&this->code(), sampler, coordName, varyingType);
120 }
121
appendTextureLookupAndModulate(const char * modulation,const TextureSampler & sampler,const char * coordName,GrSLType varyingType)122 void 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
GetTexParamSwizzle(GrPixelConfig config,const GrGLCaps & caps)132 const 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
addFeature(uint32_t featureBit,const char * extensionName)148 void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
149 if (!(featureBit & fFeaturesAddedMask)) {
150 this->extensions().appendf("#extension %s: require\n", extensionName);
151 fFeaturesAddedMask |= featureBit;
152 }
153 }
154
appendDecls(const VarArray & vars,SkString * out) const155 void 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
appendTextureLookup(const char * samplerName,const char * coordName,uint32_t configComponentMask,const char * swizzle)162 void GrGLShaderBuilder::appendTextureLookup(const char* samplerName,
163 const char* coordName,
164 uint32_t configComponentMask,
165 const char* swizzle) {
166 append_texture_lookup(&this->code(),
167 fProgramBuilder->gpu(),
168 samplerName,
169 coordName,
170 configComponentMask,
171 swizzle,
172 kVec2f_GrSLType);
173 }
174
addLayoutQualifier(const char * param,InterfaceQualifier interface)175 void GrGLShaderBuilder::addLayoutQualifier(const char* param, InterfaceQualifier interface) {
176 SkASSERT(fProgramBuilder->gpu()->glslGeneration() >= k330_GrGLSLGeneration ||
177 fProgramBuilder->gpu()->glCaps().glslCaps()->mustEnableAdvBlendEqs());
178 fLayoutParams[interface].push_back() = param;
179 }
180
compileAndAppendLayoutQualifiers()181 void GrGLShaderBuilder::compileAndAppendLayoutQualifiers() {
182 static const char* interfaceQualifierNames[] = {
183 "out"
184 };
185
186 for (int interface = 0; interface <= kLastInterfaceQualifier; ++interface) {
187 const SkTArray<SkString>& params = fLayoutParams[interface];
188 if (params.empty()) {
189 continue;
190 }
191 this->layoutQualifiers().appendf("layout(%s", params[0].c_str());
192 for (int i = 1; i < params.count(); ++i) {
193 this->layoutQualifiers().appendf(", %s", params[i].c_str());
194 }
195 this->layoutQualifiers().appendf(") %s;\n", interfaceQualifierNames[interface]);
196 }
197
198 GR_STATIC_ASSERT(0 == GrGLShaderBuilder::kOut_InterfaceQualifier);
199 GR_STATIC_ASSERT(SK_ARRAY_COUNT(interfaceQualifierNames) == kLastInterfaceQualifier + 1);
200 }
201
202 bool
finalize(GrGLuint programId,GrGLenum type,SkTDArray<GrGLuint> * shaderIds)203 GrGLShaderBuilder::finalize(GrGLuint programId, GrGLenum type, SkTDArray<GrGLuint>* shaderIds) {
204 SkASSERT(!fFinalized);
205 // append the 'footer' to code
206 this->code().append("}");
207
208 for (int i = 0; i <= fCodeIndex; i++) {
209 fCompilerStrings[i] = fShaderStrings[i].c_str();
210 fCompilerStringLengths[i] = (int)fShaderStrings[i].size();
211 }
212
213 GrGLGpu* gpu = fProgramBuilder->gpu();
214 GrGLuint shaderId = GrGLCompileAndAttachShader(gpu->glContext(),
215 programId,
216 type,
217 fCompilerStrings.begin(),
218 fCompilerStringLengths.begin(),
219 fCompilerStrings.count(),
220 gpu->stats());
221
222 fFinalized = true;
223
224 if (!shaderId) {
225 return false;
226 }
227
228 *shaderIds->append() = shaderId;
229
230 return true;
231 }
232