1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gl/GrGLShaderBuilder.h"
#include "gl/GrGLProgram.h"
namespace {
// number of each input/output type in a single allocation block
static const int sVarsPerBlock = 8;
// except FS outputs where we expect 2 at most.
static const int sMaxFSOutputs = 2;
}
// Architectural assumption: always 2-d input coords.
// Likely to become non-constant and non-static, perhaps even
// varying by stage, if we use 1D textures for gradients!
//const int GrGLShaderBuilder::fCoordDims = 2;
GrGLShaderBuilder::GrGLShaderBuilder()
: fVSUnis(sVarsPerBlock)
, fVSAttrs(sVarsPerBlock)
, fVSOutputs(sVarsPerBlock)
, fGSInputs(sVarsPerBlock)
, fGSOutputs(sVarsPerBlock)
, fFSInputs(sVarsPerBlock)
, fFSUnis(sVarsPerBlock)
, fFSOutputs(sMaxFSOutputs)
, fUsesGS(false)
, fVaryingDims(0)
, fSamplerMode(kDefault_SamplerMode)
, fComplexCoord(false) {
}
void GrGLShaderBuilder::computeSwizzle(uint32_t configFlags) {
static const uint32_t kMulByAlphaMask =
(GrGLProgram::StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
GrGLProgram::StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag);
fSwizzle = "";
if (configFlags & GrGLProgram::StageDesc::kSwapRAndB_InConfigFlag) {
GrAssert(!(configFlags &
GrGLProgram::StageDesc::kSmearAlpha_InConfigFlag));
GrAssert(!(configFlags &
GrGLProgram::StageDesc::kSmearRed_InConfigFlag));
fSwizzle = ".bgra";
} else if (configFlags & GrGLProgram::StageDesc::kSmearAlpha_InConfigFlag) {
GrAssert(!(configFlags & kMulByAlphaMask));
GrAssert(!(configFlags &
GrGLProgram::StageDesc::kSmearRed_InConfigFlag));
fSwizzle = ".aaaa";
} else if (configFlags & GrGLProgram::StageDesc::kSmearRed_InConfigFlag) {
GrAssert(!(configFlags & kMulByAlphaMask));
GrAssert(!(configFlags &
GrGLProgram::StageDesc::kSmearAlpha_InConfigFlag));
fSwizzle = ".rrrr";
}
}
void GrGLShaderBuilder::computeModulate(const char* fsInColor) {
if (NULL != fsInColor) {
fModulate.printf(" * %s", fsInColor);
} else {
fModulate.reset();
}
}
void GrGLShaderBuilder::emitTextureSetup() {
GrStringBuilder retval;
switch (fSamplerMode) {
case kDefault_SamplerMode:
// Fall through
case kProj_SamplerMode:
// Do nothing
break;
case kExplicitDivide_SamplerMode:
retval = "inCoord";
fFSCode.appendf("\t %s %s = %s%s / %s%s\n",
GrGLShaderVar::TypeString
(GrSLFloatVectorType(fCoordDims)),
retval.c_str(),
fSampleCoords.c_str(),
GrGLSLVectorNonhomogCoords(fVaryingDims),
fSampleCoords.c_str(),
GrGLSLVectorHomogCoord(fVaryingDims));
fSampleCoords = retval;
break;
}
}
void GrGLShaderBuilder::emitTextureLookup(const char* samplerName,
const char* coordName) {
if (NULL == coordName) {
coordName = fSampleCoords.c_str();
}
switch (fSamplerMode) {
default:
SkDEBUGFAIL("Unknown sampler mode");
// Fall through
case kDefault_SamplerMode:
// Fall through
case kExplicitDivide_SamplerMode:
fFSCode.appendf("texture2D(%s, %s)", samplerName, coordName);
break;
case kProj_SamplerMode:
fFSCode.appendf("texture2DProj(%s, %s)", samplerName, coordName);
break;
}
}
void GrGLShaderBuilder::emitDefaultFetch(const char* outColor,
const char* samplerName) {
fFSCode.appendf("\t%s = ", outColor);
this->emitTextureLookup(samplerName);
fFSCode.appendf("%s%s;\n", fSwizzle.c_str(), fModulate.c_str());
}
const GrGLShaderVar& GrGLShaderBuilder::addUniform(VariableLifetime lifetime,
GrSLType type,
const char* name,
int stageNum,
int count) {
GrAssert(name && strlen(name));
GrGLShaderVar* var = NULL;
if (kVertex_VariableLifetime & lifetime) {
var = &fVSUnis.push_back();
} else {
GrAssert(kFragment_VariableLifetime & lifetime);
var = &fFSUnis.push_back();
}
var->setType(type);
var->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
var->setName(name);
if (stageNum >= 0) {
var->accessName()->appendS32(stageNum);
}
var->setArrayCount(count);
if ((kVertex_VariableLifetime |
kFragment_VariableLifetime) == lifetime) {
fFSUnis.push_back(*var);
// If it's shared between VS and FS, VS must override
// default highp and specify mediump.
var->setEmitPrecision(true);
}
return *var;
}
void GrGLShaderBuilder::addVarying(GrSLType type,
const char* name,
const char** vsOutName,
const char** fsInName) {
fVSOutputs.push_back();
fVSOutputs.back().setType(type);
fVSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier);
fVSOutputs.back().accessName()->printf("v%s", name);
if (vsOutName) {
*vsOutName = fVSOutputs.back().getName().c_str();
}
// input to FS comes either from VS or GS
const GrStringBuilder* fsName;
if (fUsesGS) {
// if we have a GS take each varying in as an array
// and output as non-array.
fGSInputs.push_back();
fGSInputs.back().setType(type);
fGSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier);
fGSInputs.back().setUnsizedArray();
*fGSInputs.back().accessName() = fVSOutputs.back().getName();
fGSOutputs.push_back();
fGSOutputs.back().setType(type);
fGSOutputs.back().setTypeModifier(GrGLShaderVar::kOut_TypeModifier);
fGSOutputs.back().accessName()->printf("g%s", name);
fsName = fGSOutputs.back().accessName();
} else {
fsName = fVSOutputs.back().accessName();
}
fFSInputs.push_back();
fFSInputs.back().setType(type);
fFSInputs.back().setTypeModifier(GrGLShaderVar::kIn_TypeModifier);
fFSInputs.back().setName(*fsName);
if (fsInName) {
*fsInName = fsName->c_str();
}
}
void GrGLShaderBuilder::addVarying(GrSLType type,
const char* name,
int stageNum,
const char** vsOutName,
const char** fsInName) {
GrStringBuilder nameWithStage(name);
nameWithStage.appendS32(stageNum);
this->addVarying(type, nameWithStage.c_str(), vsOutName, fsInName);
}
|