blob: 6b3e1d12dd7e3948a365b428520b915df77052c9 [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"
9#include "GrGLProgramBuilder.h"
10#include "../GrGpuGL.h"
11#include "../GrGLShaderVar.h"
12
13namespace {
14inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) {
15 if (kVec2f_GrSLType == type) {
16 return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D";
17 } else {
18 SkASSERT(kVec3f_GrSLType == type);
19 return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj";
20 }
21}
22void append_texture_lookup(SkString* out,
23 GrGpuGL* gpu,
24 const char* samplerName,
25 const char* coordName,
26 uint32_t configComponentMask,
27 const char* swizzle,
28 GrSLType varyingType = kVec2f_GrSLType) {
29 SkASSERT(NULL != coordName);
30
31 out->appendf("%s(%s, %s)",
32 sample_function_name(varyingType, gpu->glslGeneration()),
33 samplerName,
34 coordName);
35
36 char mangledSwizzle[5];
37
38 // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle
39 // is available.
40 if (!gpu->glCaps().textureSwizzleSupport() &&
41 (kA_GrColorComponentFlag == configComponentMask)) {
42 char alphaChar = gpu->glCaps().textureRedSupport() ? 'r' : 'a';
43 int i;
44 for (i = 0; '\0' != swizzle[i]; ++i) {
45 mangledSwizzle[i] = alphaChar;
46 }
47 mangledSwizzle[i] ='\0';
48 swizzle = mangledSwizzle;
49 }
50 // For shader prettiness we omit the swizzle rather than appending ".rgba".
51 if (memcmp(swizzle, "rgba", 4)) {
52 out->appendf(".%s", swizzle);
53 }
54}
55static const int kVarsPerBlock = 8;
56}
57
58GrGLShaderBuilder::GrGLShaderBuilder(GrGLProgramBuilder* program)
59 : fProgramBuilder(program)
60 , fInputs(kVarsPerBlock)
61 , fOutputs(kVarsPerBlock)
62 , fFeaturesAddedMask(0) {
63}
64
egdanielb2f94d12014-08-29 10:08:36 -070065void GrGLShaderBuilder::declAppend(const GrGLShaderVar& var) {
66 SkString tempDecl;
67 var.appendDecl(fProgramBuilder->ctxInfo(), &tempDecl);
68 this->codeAppendf("%s;", tempDecl.c_str());
69}
70
joshualitt30ba4362014-08-21 20:18:45 -070071void GrGLShaderBuilder::emitFunction(GrSLType returnType,
72 const char* name,
73 int argCnt,
74 const GrGLShaderVar* args,
75 const char* body,
76 SkString* outName) {
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);
84 if (i < argCnt - 1) {
85 fFunctions.append(", ");
86 }
87 }
88 fFunctions.append(") {\n");
89 fFunctions.append(body);
90 fFunctions.append("}\n\n");
91}
92
93void 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.samplerUniform()),
100 coordName,
101 sampler.configComponentMask(),
102 sampler.swizzle(),
103 varyingType);
104}
105
106void GrGLShaderBuilder::appendTextureLookup(const TextureSampler& sampler,
107 const char* coordName,
108 GrSLType varyingType) {
109 this->appendTextureLookup(&fCode, sampler, coordName, varyingType);
110}
111
112void GrGLShaderBuilder::appendTextureLookupAndModulate(const char* modulation,
113 const TextureSampler& sampler,
114 const char* coordName,
115 GrSLType varyingType) {
116 SkString lookup;
117 this->appendTextureLookup(&lookup, sampler, coordName, varyingType);
118 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str());
119}
120
121
122const 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 };
126 return gRedSmear;
127 } else {
128 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA,
129 GR_GL_ALPHA, GR_GL_ALPHA };
130 return gAlphaSmear;
131 }
132 } else {
133 static const GrGLenum gStraight[] = { GR_GL_RED, GR_GL_GREEN, GR_GL_BLUE, GR_GL_ALPHA };
134 return gStraight;
135 }
136}
137
138void GrGLShaderBuilder::addFeature(uint32_t featureBit, const char* extensionName) {
139 if (!(featureBit & fFeaturesAddedMask)) {
140 fExtensions.appendf("#extension %s: require\n", extensionName);
141 fFeaturesAddedMask |= featureBit;
142 }
143}
144
145void GrGLShaderBuilder::appendTextureLookup(const char* samplerName,
146 const char* coordName,
147 uint32_t configComponentMask,
148 const char* swizzle) {
149 append_texture_lookup(&fCode,
150 fProgramBuilder->gpu(),
151 samplerName,
152 coordName,
153 configComponentMask,
154 swizzle,
155 kVec2f_GrSLType);
156}
157
158///////////////////////////////////////////////////////////////////////////////////////////////////
159GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGLFullProgramBuilder* program)
160 : INHERITED(program)
161 , fFullProgramBuilder(program) {}