2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrGLShaderBuilder.h"
9 #include "GrGLProgramBuilder.h"
10 #include "GrGLProgramBuilder.h"
11 #include "../GrGpuGL.h"
12 #include "../GrGLShaderVar.h"
15 inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) {
16 if (kVec2f_GrSLType == type) {
17 return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D";
19 SkASSERT(kVec3f_GrSLType == type);
20 return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj";
23 void append_texture_lookup(SkString* out,
25 const char* samplerName,
26 const char* coordName,
27 uint32_t configComponentMask,
29 GrSLType varyingType = kVec2f_GrSLType) {
32 out->appendf("%s(%s, %s)",
33 sample_function_name(varyingType, gpu->glslGeneration()),
37 char mangledSwizzle[5];
39 // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle
41 if (!gpu->glCaps().textureSwizzleSupport() &&
42 (kA_GrColorComponentFlag == configComponentMask)) {
43 char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a';
45 for (i = 0; '\0' != swizzle[i]; ++i) {
46 mangledSwizzle[i] = alphaChar;
48 mangledSwizzle[i] ='\0';
49 swizzle = mangledSwizzle;
51 // For shader prettiness we omit the swizzle rather than appending ".rgba".
52 if (memcmp(swizzle, "rgba", 4)) {
53 out->appendf(".%s", swizzle);
58 GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program)
59 : fProgramBuilder(program)
60 , fInputs(GrGLProgramBuilder::kVarsPerBlock)
61 , fOutputs(GrGLProgramBuilder::kVarsPerBlock)
62 , fFeaturesAddedMask(0) {
65 void GrGLShaderBuilder::declAppend(const GrGLShaderVar& var) {
67 var.appendDecl(fProgramBuilder->ctxInfo(), &tempDecl);
68 this->codeAppendf("%s;", tempDecl.c_str());
71 void GrGLShaderBuilder::emitFunction(GrSLType returnType,
74 const GrGLShaderVar* args,
77 fFunctions.append(GrGLSLTypeString(returnType));
78 fProgramBuilder->nameVariable(outName, '\0', name);
79 fFunctions.appendf(" %s", outName->c_str());
80 fFunctions.append("(");
81 const GrGLContextInfo& ctxInfo = fProgramBuilder->gpu()->ctxInfo();
82 for (int i = 0; i < argCnt; ++i) {
83 args[i].appendDecl(ctxInfo, &fFunctions);
85 fFunctions.append(", ");
88 fFunctions.append(") {\n");
89 fFunctions.append(body);
90 fFunctions.append("}\n\n");
93 void GrGLShaderBuilder::appendTextureLookup(SkString* out,
94 const TextureSampler& sampler,
95 const char* coordName,
96 GrSLType varyingType) const {
97 append_texture_lookup(out,
98 fProgramBuilder->gpu(),
99 fProgramBuilder->getUniformCStr(sampler.fSamplerUniform),
101 sampler.configComponentMask(),
106 void GrGLShaderBuilder::appendTextureLookup(const TextureSampler& sampler,
107 const char* coordName,
108 GrSLType varyingType) {
109 this->appendTextureLookup(&fCode, sampler, coordName, varyingType);
112 void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation,
113 const TextureSampler& sampler,
114 const char* coordName,
115 GrSLType varyingType) {
117 this->appendTextureLookup(&lookup, sampler, coordName, varyingType);
118 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str());
122 const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps) {
123 if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) {
124 if (caps.textureRedSupport()) {
125 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RED, GR_GL_RED };
128 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
129 GR_GL_ALPHA, GR_GL_ALPHA };
133 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA };
138 void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
139 if (!(featureBit & fFeaturesAddedMask)) {
140 fExtensions.appendf("#extension %s: require\n", extensionName);
141 fFeaturesAddedMask |= featureBit;
145 void GrGLShaderBuilder::appendDecls(const VarArray& vars, SkString* out) const {
146 for (int i = 0; i < vars.count(); ++i) {
147 vars[i].appendDecl(fProgramBuilder->ctxInfo(), out);
152 void GrGLShaderBuilder::appendTextureLookup(const char* samplerName,
153 const char* coordName,
154 uint32_t configComponentMask,
155 const char* swizzle) {
156 append_texture_lookup(&fCode,
157 fProgramBuilder->gpu(),