blob: fb98b4d3a74894b3574e1d62b067ef3ad96c584c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
junov@google.comf93e7172011-03-31 21:26:24 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
junov@google.comf93e7172011-03-31 21:26:24 +000010#include "GrGLProgram.h"
11
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000012#include "../GrAllocator.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000013#include "GrCustomStage.h"
14#include "GrGLProgramStage.h"
bsalomon@google.com4fa66942011-09-20 19:06:12 +000015#include "GrGLShaderVar.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000016#include "SkTrace.h"
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkXfermode.h"
18
junov@google.comf93e7172011-03-31 21:26:24 +000019namespace {
20
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +000021enum {
22 /// Used to mark a StageUniLocation field that should be bound
23 /// to a uniform during getUniformLocationsAndInitCache().
24 kUseUniform = 2000
25};
26
junov@google.comf93e7172011-03-31 21:26:24 +000027} // namespace
28
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000029#define PRINT_SHADERS 0
30
bsalomon@google.com4fa66942011-09-20 19:06:12 +000031typedef GrTAllocator<GrGLShaderVar> VarArray;
32
33// number of each input/output type in a single allocation block
34static const int gVarsPerBlock = 8;
35// except FS outputs where we expect 2 at most.
36static const int gMaxFSOutputs = 2;
37
38struct ShaderCodeSegments {
39 ShaderCodeSegments()
40 : fVSUnis(gVarsPerBlock)
41 , fVSAttrs(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000042 , fVSOutputs(gVarsPerBlock)
43 , fGSInputs(gVarsPerBlock)
44 , fGSOutputs(gVarsPerBlock)
45 , fFSInputs(gVarsPerBlock)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000046 , fFSUnis(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000047 , fFSOutputs(gMaxFSOutputs)
48 , fUsesGS(false) {}
bsalomon@google.com4fa66942011-09-20 19:06:12 +000049 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
50 VarArray fVSUnis;
51 VarArray fVSAttrs;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000052 VarArray fVSOutputs;
53 VarArray fGSInputs;
54 VarArray fGSOutputs;
55 VarArray fFSInputs;
56 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
bsalomon@google.com4fa66942011-09-20 19:06:12 +000057 VarArray fFSUnis;
58 VarArray fFSOutputs;
59 GrStringBuilder fFSFunctions;
60 GrStringBuilder fVSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000061 GrStringBuilder fGSCode;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000062 GrStringBuilder fFSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000063
64 bool fUsesGS;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000065};
66
bsalomon@google.com85b505b2011-11-07 14:56:51 +000067typedef GrGLProgram::ProgramDesc::StageDesc StageDesc;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000068
bsalomon@google.com4be283f2011-04-19 21:15:09 +000069#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +000070 #define VIEW_MATRIX_NAME "aViewM"
71#else
72 #define VIEW_MATRIX_NAME "uViewM"
73#endif
74
75#define POS_ATTR_NAME "aPosition"
76#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000077#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000078#define EDGE_ATTR_NAME "aEdge"
bsalomon@google.com4be283f2011-04-19 21:15:09 +000079#define COL_UNI_NAME "uColor"
bsalomon@google.com2401ae82012-01-17 21:03:05 +000080#define COV_UNI_NAME "uCoverage"
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000081#define EDGES_UNI_NAME "uEdges"
Scroggo97c88c22011-05-11 14:05:25 +000082#define COL_FILTER_UNI_NAME "uColorFilter"
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +000083#define COL_MATRIX_UNI_NAME "uColorMatrix"
84#define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec"
junov@google.comf93e7172011-03-31 21:26:24 +000085
bsalomon@google.com4fa66942011-09-20 19:06:12 +000086namespace {
87inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000088 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +000089 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +000090}
91
bsalomon@google.com4fa66942011-09-20 19:06:12 +000092inline const char* float_vector_type_str(int count) {
tomhudson@google.com168e6342012-04-18 17:49:20 +000093 return GrGLShaderVar::TypeString(GrSLFloatVectorType(count));
junov@google.comf93e7172011-03-31 21:26:24 +000094}
95
bsalomon@google.com4fa66942011-09-20 19:06:12 +000096inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +000097 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
98 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
99 return ALL[count];
100}
101
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000102inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000103 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000104 "vec3(1,1,1)", "vec4(1,1,1,1)"};
105 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
106 return ONESVEC[count];
107}
108
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000109inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000110 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000111 "vec3(0,0,0)", "vec4(0,0,0,0)"};
112 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
113 return ZEROSVEC[count];
114}
115
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000116inline const char* declared_color_output_name() { return "fsColorOut"; }
117inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000118
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000119inline void tex_matrix_name(int stage, GrStringBuilder* s) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000120#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +0000121 *s = "aTexM";
122#else
123 *s = "uTexM";
124#endif
bsalomon@google.comfc296292011-05-06 13:53:47 +0000125 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000126}
127
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000128inline void normalized_texel_size_name(int stage, GrStringBuilder* s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000129 *s = "uTexelSize";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000130 s->appendS32(stage);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000131}
132
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000133inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000134 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000135 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000136}
137
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000138inline void radial2_param_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000139 *s = "uRadial2Params";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000140 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000141}
142
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000143inline void convolve_param_names(int stage, GrStringBuilder* k, GrStringBuilder* i) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000144 *k = "uKernel";
145 k->appendS32(stage);
146 *i = "uImageIncrement";
147 i->appendS32(stage);
148}
149
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000150inline void image_increment_param_name(int stage, GrStringBuilder* i) {
151 *i = "uImageIncrement";
152 i->appendS32(stage);
153}
154
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000155inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000156 *s = "uTexDom";
157 s->appendS32(stage);
158}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000159}
junov@google.com6acc9b32011-05-16 18:32:07 +0000160
junov@google.comf93e7172011-03-31 21:26:24 +0000161GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000162}
163
164GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000165}
166
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000167void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000168 GrBlendCoeff* dstCoeff) const {
169 switch (fProgramDesc.fDualSrcOutput) {
170 case ProgramDesc::kNone_DualSrcOutput:
171 break;
172 // the prog will write a coverage value to the secondary
173 // output and the dst is blended by one minus that value.
174 case ProgramDesc::kCoverage_DualSrcOutput:
175 case ProgramDesc::kCoverageISA_DualSrcOutput:
176 case ProgramDesc::kCoverageISC_DualSrcOutput:
177 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff;
178 break;
179 default:
180 GrCrash("Unexpected dual source blend output");
181 break;
182 }
183}
184
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000185// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000186// vars can be vec4s or floats (or one of each)
187// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000188// if either var is "" then assign to the other var
189// if both are "" then assign all ones
190static inline void modulate_helper(const char* outputVar,
191 const char* var0,
192 const char* var1,
193 GrStringBuilder* code) {
194 GrAssert(NULL != outputVar);
195 GrAssert(NULL != var0);
196 GrAssert(NULL != var1);
197 GrAssert(NULL != code);
198
199 bool has0 = '\0' != *var0;
200 bool has1 = '\0' != *var1;
201
202 if (!has0 && !has1) {
203 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
204 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000205 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000206 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000207 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000208 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000209 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000210 }
211}
212
213// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000214// vars can be vec4s or floats (or one of each)
215// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000216// if either var is "" then assign to the other var
217// if both are "" then assign all zeros
218static inline void add_helper(const char* outputVar,
219 const char* var0,
220 const char* var1,
221 GrStringBuilder* code) {
222 GrAssert(NULL != outputVar);
223 GrAssert(NULL != var0);
224 GrAssert(NULL != var1);
225 GrAssert(NULL != code);
226
227 bool has0 = '\0' != *var0;
228 bool has1 = '\0' != *var1;
229
230 if (!has0 && !has1) {
231 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
232 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000233 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000234 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000235 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000236 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000237 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000238 }
239}
240
241// given two blend coeffecients determine whether the src
242// and/or dst computation can be omitted.
243static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
244 SkXfermode::Coeff dstCoeff,
245 bool* needSrcValue,
246 bool* needDstValue) {
247 if (SkXfermode::kZero_Coeff == srcCoeff) {
248 switch (dstCoeff) {
249 // these all read the src
250 case SkXfermode::kSC_Coeff:
251 case SkXfermode::kISC_Coeff:
252 case SkXfermode::kSA_Coeff:
253 case SkXfermode::kISA_Coeff:
254 *needSrcValue = true;
255 break;
256 default:
257 *needSrcValue = false;
258 break;
259 }
260 } else {
261 *needSrcValue = true;
262 }
263 if (SkXfermode::kZero_Coeff == dstCoeff) {
264 switch (srcCoeff) {
265 // these all read the dst
266 case SkXfermode::kDC_Coeff:
267 case SkXfermode::kIDC_Coeff:
268 case SkXfermode::kDA_Coeff:
269 case SkXfermode::kIDA_Coeff:
270 *needDstValue = true;
271 break;
272 default:
273 *needDstValue = false;
274 break;
275 }
276 } else {
277 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000278 }
279}
280
281/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000282 * Create a blend_coeff * value string to be used in shader code. Sets empty
283 * string if result is trivially zero.
284 */
285static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
286 const char* src, const char* dst,
287 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000288 switch (coeff) {
289 case SkXfermode::kZero_Coeff: /** 0 */
290 *str = "";
291 break;
292 case SkXfermode::kOne_Coeff: /** 1 */
293 *str = value;
294 break;
295 case SkXfermode::kSC_Coeff:
296 str->printf("(%s * %s)", src, value);
297 break;
298 case SkXfermode::kISC_Coeff:
299 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
300 break;
301 case SkXfermode::kDC_Coeff:
302 str->printf("(%s * %s)", dst, value);
303 break;
304 case SkXfermode::kIDC_Coeff:
305 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
306 break;
307 case SkXfermode::kSA_Coeff: /** src alpha */
308 str->printf("(%s.a * %s)", src, value);
309 break;
310 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
311 str->printf("((1.0 - %s.a) * %s)", src, value);
312 break;
313 case SkXfermode::kDA_Coeff: /** dst alpha */
314 str->printf("(%s.a * %s)", dst, value);
315 break;
316 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
317 str->printf("((1.0 - %s.a) * %s)", dst, value);
318 break;
319 default:
320 GrCrash("Unexpected xfer coeff.");
321 break;
322 }
323}
324/**
Scroggo97c88c22011-05-11 14:05:25 +0000325 * Adds a line to the fragment shader code which modifies the color by
326 * the specified color filter.
327 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000328static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000329 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000330 SkXfermode::Coeff colorCoeff,
331 const char* inColor) {
332 GrStringBuilder colorStr, constStr;
333 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
334 inColor, inColor);
335 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
336 inColor, COL_FILTER_UNI_NAME);
337
338 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000339}
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000340/**
341 * Adds code to the fragment shader code which modifies the color by
342 * the specified color matrix.
343 */
344static void addColorMatrix(GrStringBuilder* fsCode, const char * outputVar,
345 const char* inColor) {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000346 fsCode->appendf("\t%s = %s * vec4(%s.rgb / %s.a, %s.a) + %s;\n", outputVar, COL_MATRIX_UNI_NAME, inColor, inColor, inColor, COL_MATRIX_VEC_UNI_NAME);
347 fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000348}
Scroggo97c88c22011-05-11 14:05:25 +0000349
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000350namespace {
351
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000352// Adds a var that is computed in the VS and read in FS.
353// If there is a GS it will just pass it through.
tomhudson@google.com168e6342012-04-18 17:49:20 +0000354void append_varying(GrSLType type,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000355 const char* name,
356 ShaderCodeSegments* segments,
357 const char** vsOutName = NULL,
358 const char** fsInName = NULL) {
359 segments->fVSOutputs.push_back();
360 segments->fVSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000361 segments->fVSOutputs.back().setTypeModifier(
362 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000363 segments->fVSOutputs.back().accessName()->printf("v%s", name);
364 if (vsOutName) {
365 *vsOutName = segments->fVSOutputs.back().getName().c_str();
366 }
367 // input to FS comes either from VS or GS
368 const GrStringBuilder* fsName;
369 if (segments->fUsesGS) {
370 // if we have a GS take each varying in as an array
371 // and output as non-array.
372 segments->fGSInputs.push_back();
373 segments->fGSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000374 segments->fGSInputs.back().setTypeModifier(
375 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000376 segments->fGSInputs.back().setUnsizedArray();
377 *segments->fGSInputs.back().accessName() =
378 segments->fVSOutputs.back().getName();
379 segments->fGSOutputs.push_back();
380 segments->fGSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000381 segments->fGSOutputs.back().setTypeModifier(
382 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000383 segments->fGSOutputs.back().accessName()->printf("g%s", name);
384 fsName = segments->fGSOutputs.back().accessName();
385 } else {
386 fsName = segments->fVSOutputs.back().accessName();
387 }
388 segments->fFSInputs.push_back();
389 segments->fFSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000390 segments->fFSInputs.back().setTypeModifier(
391 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000392 segments->fFSInputs.back().setName(*fsName);
393 if (fsInName) {
394 *fsInName = fsName->c_str();
395 }
396}
397
398// version of above that adds a stage number to the
399// the var name (for uniqueness)
tomhudson@google.com168e6342012-04-18 17:49:20 +0000400void append_varying(GrSLType type,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000401 const char* name,
402 int stageNum,
403 ShaderCodeSegments* segments,
404 const char** vsOutName = NULL,
405 const char** fsInName = NULL) {
406 GrStringBuilder nameWithStage(name);
407 nameWithStage.appendS32(stageNum);
408 append_varying(type, nameWithStage.c_str(), segments, vsOutName, fsInName);
409}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000410}
411
bsalomon@google.com96399942012-02-13 14:39:16 +0000412void GrGLProgram::genEdgeCoverage(const GrGLContextInfo& gl,
bsalomon@google.com66105672011-09-15 15:12:00 +0000413 GrVertexLayout layout,
414 CachedData* programData,
415 GrStringBuilder* coverageVar,
416 ShaderCodeSegments* segments) const {
417 if (fProgramDesc.fEdgeAANumEdges > 0) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000418 segments->fFSUnis.push_back().set(kVec3f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000419 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000420 EDGES_UNI_NAME,
421 fProgramDesc.fEdgeAANumEdges);
bsalomon@google.com66105672011-09-15 15:12:00 +0000422 programData->fUniLocations.fEdgesUni = kUseUniform;
423 int count = fProgramDesc.fEdgeAANumEdges;
424 segments->fFSCode.append(
425 "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n");
426 for (int i = 0; i < count; i++) {
427 segments->fFSCode.append("\tfloat a");
428 segments->fFSCode.appendS32(i);
429 segments->fFSCode.append(" = clamp(dot(" EDGES_UNI_NAME "[");
430 segments->fFSCode.appendS32(i);
431 segments->fFSCode.append("], pos), 0.0, 1.0);\n");
432 }
433 if (fProgramDesc.fEdgeAAConcave && (count & 0x01) == 0) {
434 // For concave polys, we consider the edges in pairs.
435 segments->fFSFunctions.append("float cross2(vec2 a, vec2 b) {\n");
436 segments->fFSFunctions.append("\treturn dot(a, vec2(b.y, -b.x));\n");
437 segments->fFSFunctions.append("}\n");
438 for (int i = 0; i < count; i += 2) {
439 segments->fFSCode.appendf("\tfloat eb%d;\n", i / 2);
440 segments->fFSCode.appendf("\tif (cross2(" EDGES_UNI_NAME "[%d].xy, " EDGES_UNI_NAME "[%d].xy) < 0.0) {\n", i, i + 1);
441 segments->fFSCode.appendf("\t\teb%d = a%d * a%d;\n", i / 2, i, i + 1);
442 segments->fFSCode.append("\t} else {\n");
443 segments->fFSCode.appendf("\t\teb%d = a%d + a%d - a%d * a%d;\n", i / 2, i, i + 1, i, i + 1);
444 segments->fFSCode.append("\t}\n");
445 }
446 segments->fFSCode.append("\tfloat edgeAlpha = ");
447 for (int i = 0; i < count / 2 - 1; i++) {
448 segments->fFSCode.appendf("min(eb%d, ", i);
449 }
450 segments->fFSCode.appendf("eb%d", count / 2 - 1);
451 for (int i = 0; i < count / 2 - 1; i++) {
452 segments->fFSCode.append(")");
453 }
454 segments->fFSCode.append(";\n");
455 } else {
456 segments->fFSCode.append("\tfloat edgeAlpha = ");
457 for (int i = 0; i < count - 1; i++) {
458 segments->fFSCode.appendf("min(a%d * a%d, ", i, i + 1);
459 }
460 segments->fFSCode.appendf("a%d * a0", count - 1);
461 for (int i = 0; i < count - 1; i++) {
462 segments->fFSCode.append(")");
463 }
464 segments->fFSCode.append(";\n");
465 }
466 *coverageVar = "edgeAlpha";
467 } else if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000468 const char *vsName, *fsName;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000469 append_varying(kVec4f_GrSLType, "Edge", segments,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000470 &vsName, &fsName);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000471 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000472 GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000473 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
tomhudson@google.com93813632011-10-27 20:21:16 +0000474 if (GrDrawState::kHairLine_EdgeType == fProgramDesc.fVertexEdgeType) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000475 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000476 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000477 } else if (GrDrawState::kQuad_EdgeType == fProgramDesc.fVertexEdgeType) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000478 segments->fFSCode.append("\tfloat edgeAlpha;\n");
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000479 // keep the derivative instructions outside the conditional
480 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
481 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
482 segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
483 // today we know z and w are in device space. We could use derivatives
484 segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName);
485 segments->fFSCode.append ("\t} else {\n");
486 segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
487 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
488 fsName, fsName);
489 segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000490 segments->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n"
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000491 "\t}\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000492 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000493 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
494 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000495 } else {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000496 GrAssert(GrDrawState::kHairQuad_EdgeType == fProgramDesc.fVertexEdgeType);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000497 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
498 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000499 segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
500 "\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
501 fsName, fsName);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000502 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000503 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
504 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000505 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000506 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
507 }
508 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000509 *coverageVar = "edgeAlpha";
510 } else {
511 coverageVar->reset();
512 }
513}
514
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000515namespace {
516
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000517void genInputColor(GrGLProgram::ProgramDesc::ColorInput colorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000518 GrGLProgram::CachedData* programData,
519 ShaderCodeSegments* segments,
520 GrStringBuilder* inColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000521 switch (colorInput) {
522 case GrGLProgram::ProgramDesc::kAttribute_ColorInput: {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000523 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000524 GrGLShaderVar::kAttribute_TypeModifier,
525 COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000526 const char *vsName, *fsName;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000527 append_varying(kVec4f_GrSLType, "Color", segments, &vsName, &fsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000528 segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
529 *inColor = fsName;
530 } break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000531 case GrGLProgram::ProgramDesc::kUniform_ColorInput:
tomhudson@google.com168e6342012-04-18 17:49:20 +0000532 segments->fFSUnis.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000533 GrGLShaderVar::kUniform_TypeModifier,
534 COL_UNI_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000535 programData->fUniLocations.fColorUni = kUseUniform;
536 *inColor = COL_UNI_NAME;
537 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000538 case GrGLProgram::ProgramDesc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000539 GrAssert(!"needComputedColor should be false.");
540 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000541 case GrGLProgram::ProgramDesc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000542 break;
543 default:
544 GrCrash("Unknown color type.");
545 break;
546 }
547}
548
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000549void genAttributeCoverage(ShaderCodeSegments* segments,
550 GrStringBuilder* inOutCoverage) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000551 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000552 GrGLShaderVar::kAttribute_TypeModifier,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000553 COV_ATTR_NAME);
554 const char *vsName, *fsName;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000555 append_varying(kVec4f_GrSLType, "Coverage",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000556 segments, &vsName, &fsName);
557 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000558 if (inOutCoverage->size()) {
559 segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n",
560 fsName, inOutCoverage->c_str());
561 *inOutCoverage = "attrCoverage";
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000562 } else {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000563 *inOutCoverage = fsName;
564 }
565}
566
567void genUniformCoverage(ShaderCodeSegments* segments,
568 GrGLProgram::CachedData* programData,
569 GrStringBuilder* inOutCoverage) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000570 segments->fFSUnis.push_back().set(kVec4f_GrSLType,
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000571 GrGLShaderVar::kUniform_TypeModifier,
572 COV_UNI_NAME);
573 programData->fUniLocations.fCoverageUni = kUseUniform;
574 if (inOutCoverage->size()) {
575 segments->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
576 COV_UNI_NAME, inOutCoverage->c_str());
577 *inOutCoverage = "uniCoverage";
578 } else {
579 *inOutCoverage = COV_UNI_NAME;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000580 }
581}
582
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000583}
584
bsalomon@google.com96399942012-02-13 14:39:16 +0000585void GrGLProgram::genGeometryShader(const GrGLContextInfo& gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000586 ShaderCodeSegments* segments) const {
587#if GR_GL_EXPERIMENTAL_GS
588 if (fProgramDesc.fExperimentalGS) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000589 GrAssert(gl.glslGeneration() >= k150_GrGLSLGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000590 segments->fGSHeader.append("layout(triangles) in;\n"
591 "layout(triangle_strip, max_vertices = 6) out;\n");
592 segments->fGSCode.append("void main() {\n"
593 "\tfor (int i = 0; i < 3; ++i) {\n"
594 "\t\tgl_Position = gl_in[i].gl_Position;\n");
595 if (this->fProgramDesc.fEmitsPointSize) {
596 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
597 }
598 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
599 int count = segments->fGSInputs.count();
600 for (int i = 0; i < count; ++i) {
601 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
602 segments->fGSOutputs[i].getName().c_str(),
603 segments->fGSInputs[i].getName().c_str());
604 }
605 segments->fGSCode.append("\t\tEmitVertex();\n"
606 "\t}\n"
607 "\tEndPrimitive();\n"
608 "}\n");
609 }
610#endif
611}
612
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000613const char* GrGLProgram::adjustInColor(const GrStringBuilder& inColor) const {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000614 if (inColor.size()) {
615 return inColor.c_str();
616 } else {
617 if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) {
618 return all_ones_vec(4);
619 } else {
620 return all_zeros_vec(4);
621 }
622 }
623}
624
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000625// If this destructor is in the header file, we must include GrGLProgramStage
626// instead of just forward-declaring it.
627GrGLProgram::CachedData::~CachedData() {
628 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
629 delete fCustomStage[i];
630 }
631}
632
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000633
bsalomon@google.com96399942012-02-13 14:39:16 +0000634bool GrGLProgram::genProgram(const GrGLContextInfo& gl,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000635 GrCustomStage** customStages,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000636 GrGLProgram::CachedData* programData) const {
junov@google.comf93e7172011-03-31 21:26:24 +0000637
638 ShaderCodeSegments segments;
639 const uint32_t& layout = fProgramDesc.fVertexLayout;
640
bsalomon@google.com91961302011-05-09 18:39:58 +0000641 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000642
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000643#if GR_GL_EXPERIMENTAL_GS
644 segments.fUsesGS = fProgramDesc.fExperimentalGS;
645#endif
646
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000647 SkXfermode::Coeff colorCoeff, uniformCoeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000648 bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000649 // The rest of transfer mode color filters have not been implemented
650 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
651 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000652 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
653 (fProgramDesc.fColorFilterXfermode),
654 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000655 GR_DEBUGASSERT(success);
656 } else {
657 colorCoeff = SkXfermode::kOne_Coeff;
658 uniformCoeff = SkXfermode::kZero_Coeff;
659 }
660
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000661 // no need to do the color filter / matrix at all if coverage is 0. The
662 // output color is scaled by the coverage. All the dual source outputs are
663 // scaled by the coverage as well.
664 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) {
665 colorCoeff = SkXfermode::kZero_Coeff;
666 uniformCoeff = SkXfermode::kZero_Coeff;
667 applyColorMatrix = false;
668 }
669
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000670 // If we know the final color is going to be all zeros then we can
671 // simplify the color filter coeffecients. needComputedColor will then
672 // come out false below.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000673 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000674 colorCoeff = SkXfermode::kZero_Coeff;
675 if (SkXfermode::kDC_Coeff == uniformCoeff ||
676 SkXfermode::kDA_Coeff == uniformCoeff) {
677 uniformCoeff = SkXfermode::kZero_Coeff;
678 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
679 SkXfermode::kIDA_Coeff == uniformCoeff) {
680 uniformCoeff = SkXfermode::kOne_Coeff;
681 }
682 }
683
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000684 bool needColorFilterUniform;
685 bool needComputedColor;
686 needBlendInputs(uniformCoeff, colorCoeff,
687 &needColorFilterUniform, &needComputedColor);
688
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000689 // the dual source output has no canonical var name, have to
690 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000691 bool dualSourceOutputWritten = false;
bsalomon@google.com96399942012-02-13 14:39:16 +0000692 segments.fHeader.printf(GrGetGLSLVersionDecl(gl.binding(),
693 gl.glslGeneration()));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000694
695 GrGLShaderVar colorOutput;
bsalomon@google.com96399942012-02-13 14:39:16 +0000696 bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(),
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000697 declared_color_output_name(),
698 &colorOutput);
699 if (isColorDeclared) {
700 segments.fFSOutputs.push_back(colorOutput);
701 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000702
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000703#if GR_GL_ATTRIBUTE_MATRICES
tomhudson@google.com168e6342012-04-18 17:49:20 +0000704 segments.fVSAttrs.push_back().set(kMat33f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000705 GrGLShaderVar::kAttribute_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000706 programData->fUniLocations.fViewMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +0000707#else
tomhudson@google.com168e6342012-04-18 17:49:20 +0000708 segments.fVSUnis.push_back().set(kMat33f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000709 GrGLShaderVar::kUniform_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000710 programData->fUniLocations.fViewMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +0000711#endif
tomhudson@google.com168e6342012-04-18 17:49:20 +0000712 segments.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000713 GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000714
bsalomon@google.com91961302011-05-09 18:39:58 +0000715 segments.fVSCode.append(
716 "void main() {\n"
717 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
718 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000719
bsalomon@google.com91961302011-05-09 18:39:58 +0000720 // incoming color to current stage being processed.
721 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000722
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000723 if (needComputedColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000724 genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000725 programData, &segments, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000726 }
727
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000728 // we output point size in the GS if present
729 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000730 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000731 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000732
bsalomon@google.com91961302011-05-09 18:39:58 +0000733 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000734
735 // add texture coordinates that are used to the list of vertex attr decls
tomhudson@google.com93813632011-10-27 20:21:16 +0000736 GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords];
737 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000738 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000739 tex_attr_name(t, texCoordAttrs + t);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000740 segments.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000741 GrGLShaderVar::kAttribute_TypeModifier,
742 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000743 }
744 }
745
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000746 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000747 // Convert generic effect representation to GL-specific backend so they
748 // can be accesseed in genStageCode() and in subsequent uses of
749 // programData.
750 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
751 GrCustomStage* customStage = customStages[s];
752 if (NULL != customStage) {
753 GrGLProgramStageFactory* factory = customStage->getGLFactory();
754 programData->fCustomStage[s] =
755 factory->createGLInstance(customStage);
756 } else {
757 programData->fCustomStage[s] = NULL;
758 }
759 }
760
761 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000762 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000763
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000764 // if we have color stages string them together, feeding the output color
765 // of each to the next and generating code for each stage.
766 if (needComputedColor) {
767 GrStringBuilder outColor;
768 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000769 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000770 // create var to hold stage result
771 outColor = "color";
772 outColor.appendS32(s);
773 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
774
775 const char* inCoords;
776 // figure out what our input coords are
777 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
778 layout) {
779 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000780 } else {
781 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
782 // we better have input tex coordinates if stage is enabled.
783 GrAssert(tcIdx >= 0);
784 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000785 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000786 }
787
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000788 this->genStageCode(gl,
789 s,
790 fProgramDesc.fStages[s],
791 inColor.size() ? inColor.c_str() : NULL,
792 outColor.c_str(),
793 inCoords,
794 &segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000795 &programData->fUniLocations.fStages[s],
796 programData->fCustomStage[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000797 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000798 }
799 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000800 }
Scroggo97c88c22011-05-11 14:05:25 +0000801
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000802 // if have all ones or zeros for the "dst" input to the color filter then we
803 // may be able to make additional optimizations.
804 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000805 GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000806 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
807 SkXfermode::kIDA_Coeff == uniformCoeff;
808 if (uniformCoeffIsZero) {
809 uniformCoeff = SkXfermode::kZero_Coeff;
810 bool bogus;
811 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
812 &needColorFilterUniform, &bogus);
813 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000814 }
815 if (needColorFilterUniform) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000816 segments.fFSUnis.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000817 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000818 COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000819 programData->fUniLocations.fColorFilterUni = kUseUniform;
820 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000821 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000822 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000823 SkXfermode::kZero_Coeff == colorCoeff &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000824 !applyColorMatrix) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000825 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000826 colorOutput.getName().c_str(),
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000827 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000828 wroteFragColorZero = true;
829 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000830 segments.fFSCode.append("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000831 const char* color = adjustInColor(inColor);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000832 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000833 colorCoeff, color);
834 inColor = "filteredColor";
835 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000836 if (applyColorMatrix) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000837 segments.fFSUnis.push_back().set(kMat44f_GrSLType,
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000838 GrGLShaderVar::kUniform_TypeModifier,
839 COL_MATRIX_UNI_NAME);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000840 segments.fFSUnis.push_back().set(kVec4f_GrSLType,
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000841 GrGLShaderVar::kUniform_TypeModifier,
842 COL_MATRIX_VEC_UNI_NAME);
843 programData->fUniLocations.fColorMatrixUni = kUseUniform;
844 programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000845 segments.fFSCode.append("\tvec4 matrixedColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000846 const char* color = adjustInColor(inColor);
847 addColorMatrix(&segments.fFSCode, "matrixedColor", color);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000848 inColor = "matrixedColor";
849 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000850
851 ///////////////////////////////////////////////////////////////////////////
852 // compute the partial coverage (coverage stages and edge aa)
853
854 GrStringBuilder inCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000855 bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput ==
856 fProgramDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000857 // we don't need to compute coverage at all if we know the final shader
858 // output will be zero and we don't have a dual src blend output.
859 if (!wroteFragColorZero ||
860 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000861
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000862 if (!coverageIsZero) {
863 this->genEdgeCoverage(gl,
864 layout,
865 programData,
866 &inCoverage,
867 &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000868
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000869 switch (fProgramDesc.fCoverageInput) {
870 case ProgramDesc::kSolidWhite_ColorInput:
871 // empty string implies solid white
872 break;
873 case ProgramDesc::kAttribute_ColorInput:
874 genAttributeCoverage(&segments, &inCoverage);
875 break;
876 case ProgramDesc::kUniform_ColorInput:
877 genUniformCoverage(&segments, programData, &inCoverage);
878 break;
879 default:
880 GrCrash("Unexpected input coverage.");
881 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000882
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000883 GrStringBuilder outCoverage;
884 const int& startStage = fProgramDesc.fFirstCoverageStage;
885 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
886 if (fProgramDesc.fStages[s].isEnabled()) {
887 // create var to hold stage output
888 outCoverage = "coverage";
889 outCoverage.appendS32(s);
890 segments.fFSCode.appendf("\tvec4 %s;\n",
891 outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000892
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000893 const char* inCoords;
894 // figure out what our input coords are
895 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
896 layout) {
897 inCoords = POS_ATTR_NAME;
898 } else {
899 int tcIdx =
900 GrDrawTarget::VertexTexCoordsForStage(s, layout);
901 // we better have input tex coordinates if stage is
902 // enabled.
903 GrAssert(tcIdx >= 0);
904 GrAssert(texCoordAttrs[tcIdx].size());
905 inCoords = texCoordAttrs[tcIdx].c_str();
906 }
907
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000908 this->genStageCode(gl, s,
909 fProgramDesc.fStages[s],
910 inCoverage.size() ? inCoverage.c_str() : NULL,
911 outCoverage.c_str(),
912 inCoords,
913 &segments,
914 &programData->fUniLocations.fStages[s],
915 programData->fCustomStage[s]);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000916 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000917 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000918 }
919 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000920 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000921 segments.fFSOutputs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000922 GrGLShaderVar::kOut_TypeModifier,
923 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000924 bool outputIsZero = coverageIsZero;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000925 GrStringBuilder coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000926 if (!outputIsZero &&
927 ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000928 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
929 if (!inColor.size()) {
930 outputIsZero = true;
931 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000932 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000933 ProgramDesc::kCoverageISA_DualSrcOutput) {
934 coeff.printf("(1 - %s.a)", inColor.c_str());
935 } else {
936 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
937 }
938 }
939 }
940 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000941 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000942 dual_source_output_name(),
943 all_zeros_vec(4));
944 } else {
945 modulate_helper(dual_source_output_name(),
946 coeff.c_str(),
947 inCoverage.c_str(),
948 &segments.fFSCode);
949 }
950 dualSourceOutputWritten = true;
951 }
junov@google.comf93e7172011-03-31 21:26:24 +0000952 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000953
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000954 ///////////////////////////////////////////////////////////////////////////
955 // combine color and coverage as frag color
956
957 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000958 if (coverageIsZero) {
959 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000960 colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000961 all_zeros_vec(4));
962 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000963 modulate_helper(colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000964 inColor.c_str(),
965 inCoverage.c_str(),
966 &segments.fFSCode);
967 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000968 if (ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig ==
969 fProgramDesc.fOutputConfig) {
970 segments.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n",
971 colorOutput.getName().c_str(),
972 colorOutput.getName().c_str(),
973 colorOutput.getName().c_str(),
974 colorOutput.getName().c_str(),
975 colorOutput.getName().c_str());
976 } else if (ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig ==
977 fProgramDesc.fOutputConfig) {
978 segments.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n",
979 colorOutput.getName().c_str(),
980 colorOutput.getName().c_str(),
981 colorOutput.getName().c_str(),
982 colorOutput.getName().c_str(),
983 colorOutput.getName().c_str());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000984 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000985 }
986
bsalomon@google.com91961302011-05-09 18:39:58 +0000987 segments.fVSCode.append("}\n");
988 segments.fFSCode.append("}\n");
989
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000990 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000991 // insert GS
992#if GR_DEBUG
bsalomon@google.com96399942012-02-13 14:39:16 +0000993 this->genGeometryShader(gl, &segments);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000994#endif
995
996 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000997 // compile and setup attribs and unis
998
bsalomon@google.com96399942012-02-13 14:39:16 +0000999 if (!CompileShaders(gl, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001000 return false;
1001 }
1002
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001003 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001004 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001005 dualSourceOutputWritten,
1006 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001007 return false;
1008 }
1009
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001010 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +00001011
1012 return true;
1013}
1014
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001015namespace {
1016
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001017inline void expand_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +00001018 const GrGLContextInfo& gl,
1019 GrStringBuilder* string) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001020 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001021 for (int i = 0; i < count; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001022 vars[i].appendDecl(gl, string);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001023 }
1024}
1025
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001026inline void print_shader(int stringCnt,
1027 const char** strings,
1028 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001029 for (int i = 0; i < stringCnt; ++i) {
1030 if (NULL == stringLengths || stringLengths[i] < 0) {
1031 GrPrintf(strings[i]);
1032 } else {
1033 GrPrintf("%.*s", stringLengths[i], strings[i]);
1034 }
1035 }
1036}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001037
1038typedef SkTArray<const char*, true> StrArray;
1039#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
1040
1041typedef SkTArray<int, true> LengthArray;
1042#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
1043
1044// these shouldn't relocate
1045typedef GrTAllocator<GrStringBuilder> TempArray;
1046#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
1047
1048inline void append_string(const GrStringBuilder& str,
1049 StrArray* strings,
1050 LengthArray* lengths) {
1051 int length = (int) str.size();
1052 if (length) {
1053 strings->push_back(str.c_str());
1054 lengths->push_back(length);
1055 }
1056 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001057}
1058
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001059inline void append_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +00001060 const GrGLContextInfo& gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001061 StrArray* strings,
1062 LengthArray* lengths,
bsalomon@google.com96399942012-02-13 14:39:16 +00001063 TempArray* temp) {
1064 expand_decls(vars, gl, &temp->push_back());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001065 append_string(temp->back(), strings, lengths);
1066}
1067
1068}
1069
bsalomon@google.com96399942012-02-13 14:39:16 +00001070bool GrGLProgram::CompileShaders(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +00001072 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001073 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +00001074
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001075 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
1076 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
1077 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
1078
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001079 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001080 GrStringBuilder inputs;
1081 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001082
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001083 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001084 append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps);
1085 append_decls(segments.fVSAttrs, gl, &strs, &lengths, &temps);
1086 append_decls(segments.fVSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001087 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001088
1089#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001090 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001091 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001092#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001093
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001094 programData->fVShaderID =
1095 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
1096 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001097
bsalomon@google.com91961302011-05-09 18:39:58 +00001098 if (!programData->fVShaderID) {
1099 return false;
1100 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001101 if (segments.fUsesGS) {
1102 strs.reset();
1103 lengths.reset();
1104 temps.reset();
1105 append_string(segments.fHeader, &strs, &lengths);
1106 append_string(segments.fGSHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001107 append_decls(segments.fGSInputs, gl, &strs, &lengths, &temps);
1108 append_decls(segments.fGSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001109 append_string(segments.fGSCode, &strs, &lengths);
1110#if PRINT_SHADERS
1111 print_shader(strs.count(), &strs[0], &lengths[0]);
1112 GrPrintf("\n");
1113#endif
1114 programData->fGShaderID =
1115 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
1116 &strs[0], &lengths[0]);
1117 } else {
1118 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +00001119 }
junov@google.comf93e7172011-03-31 21:26:24 +00001120
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001121 strs.reset();
1122 lengths.reset();
1123 temps.reset();
1124
1125 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001126 GrStringBuilder precisionStr(GrGetGLSLShaderPrecisionDecl(gl.binding()));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001127 append_string(precisionStr, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001128 append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps);
1129 append_decls(segments.fFSInputs, gl, &strs, &lengths, &temps);
bsalomon@google.com373a6632011-10-19 20:43:20 +00001130 // We shouldn't have declared outputs on 1.10
bsalomon@google.com96399942012-02-13 14:39:16 +00001131 GrAssert(k110_GrGLSLGeneration != gl.glslGeneration() ||
tomhudson@google.com086e5352011-12-08 14:44:10 +00001132 segments.fFSOutputs.empty());
bsalomon@google.com96399942012-02-13 14:39:16 +00001133 append_decls(segments.fFSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001134 append_string(segments.fFSFunctions, &strs, &lengths);
1135 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001136
1137#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001138 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001139 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001140#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001141
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001142 programData->fFShaderID =
1143 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
1144 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001145
bsalomon@google.com91961302011-05-09 18:39:58 +00001146 if (!programData->fFShaderID) {
1147 return false;
junov@google.comf93e7172011-03-31 21:26:24 +00001148 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001149
bsalomon@google.com91961302011-05-09 18:39:58 +00001150 return true;
junov@google.comf93e7172011-03-31 21:26:24 +00001151}
1152
bsalomon@google.com96399942012-02-13 14:39:16 +00001153#define GL_CALL(X) GR_GL_CALL(gl.interface(), X)
1154#define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X)
1155
1156GrGLuint GrGLProgram::CompileShader(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GrGLenum type,
1158 int stringCnt,
1159 const char** strings,
1160 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001161 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
1162 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
1163
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001164 GrGLuint shader;
bsalomon@google.com96399942012-02-13 14:39:16 +00001165 GL_CALL_RET(shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +00001166 if (0 == shader) {
1167 return 0;
1168 }
1169
1170 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001171 GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths));
1172 GL_CALL(CompileShader(shader));
1173 GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +00001174
1175 if (!compiled) {
1176 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001177 GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001178 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001179 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001180 // retrieve length even though we don't need it to workaround
1181 // bug in chrome cmd buffer param validation.
1182 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001183 GL_CALL(GetShaderInfoLog(shader, infoLen+1,
1184 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001185 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001186 GrPrintf("\n%s", log.get());
1187 }
1188 GrAssert(!"Shader compilation failed!");
bsalomon@google.com96399942012-02-13 14:39:16 +00001189 GL_CALL(DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001190 return 0;
1191 }
1192 return shader;
1193}
1194
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001195bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com96399942012-02-13 14:39:16 +00001196 const GrGLContextInfo& gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001197 GrStringBuilder texCoordAttrNames[],
1198 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001199 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001200 CachedData* programData) const {
bsalomon@google.com96399942012-02-13 14:39:16 +00001201 GL_CALL_RET(programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001202 if (!programData->fProgramID) {
1203 return false;
1204 }
1205 const GrGLint& progID = programData->fProgramID;
1206
bsalomon@google.com96399942012-02-13 14:39:16 +00001207 GL_CALL(AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001208 if (programData->fGShaderID) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001209 GL_CALL(AttachShader(progID, programData->fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001210 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001211 GL_CALL(AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001212
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001213 if (bindColorOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001214 GL_CALL(BindFragDataLocation(programData->fProgramID,
1215 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001216 }
1217 if (bindDualSrcOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001218 GL_CALL(BindFragDataLocationIndexed(programData->fProgramID,
1219 0, 1, dual_source_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001220 }
1221
bsalomon@google.com91961302011-05-09 18:39:58 +00001222 // Bind the attrib locations to same values for all shaders
bsalomon@google.com96399942012-02-13 14:39:16 +00001223 GL_CALL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +00001224 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001225 if (texCoordAttrNames[t].size()) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001226 GL_CALL(BindAttribLocation(progID,
1227 TexCoordAttributeIdx(t),
1228 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001229 }
1230 }
1231
bsalomon@google.com91961302011-05-09 18:39:58 +00001232 if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001233 GL_CALL(BindAttribLocation(progID,
1234 ViewMatrixAttributeIdx(),
1235 VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001236 }
1237
tomhudson@google.com93813632011-10-27 20:21:16 +00001238 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com72b4fcb2011-05-09 20:47:34 +00001239 const StageUniLocations& unis = programData->fUniLocations.fStages[s];
1240 if (kSetAsAttribute == unis.fTextureMatrixUni) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001241 GrStringBuilder matName;
1242 tex_matrix_name(s, &matName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001243 GL_CALL(BindAttribLocation(progID,
1244 TextureMatrixAttributeIdx(s),
1245 matName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001246 }
1247 }
1248
bsalomon@google.com96399942012-02-13 14:39:16 +00001249 GL_CALL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME));
1250 GL_CALL(BindAttribLocation(progID, CoverageAttributeIdx(), COV_ATTR_NAME));
1251 GL_CALL(BindAttribLocation(progID, EdgeAttributeIdx(), EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001252
bsalomon@google.com96399942012-02-13 14:39:16 +00001253 GL_CALL(LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001254
1255 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001256 GL_CALL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001257 if (!linked) {
1258 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001259 GL_CALL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001260 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001261 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001262 // retrieve length even though we don't need it to workaround
1263 // bug in chrome cmd buffer param validation.
1264 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001265 GL_CALL(GetProgramInfoLog(progID,
1266 infoLen+1,
1267 &length,
1268 (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001269 GrPrintf((char*)log.get());
1270 }
1271 GrAssert(!"Error linking program");
bsalomon@google.com96399942012-02-13 14:39:16 +00001272 GL_CALL(DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001273 programData->fProgramID = 0;
1274 return false;
1275 }
1276 return true;
1277}
1278
bsalomon@google.com96399942012-02-13 14:39:16 +00001279void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001280 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001281 const GrGLint& progID = programData->fProgramID;
1282
1283 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001284 GL_CALL_RET(programData->fUniLocations.fViewMatrixUni,
1285 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001286 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1287 }
1288 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001289 GL_CALL_RET(programData->fUniLocations.fColorUni,
1290 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001291 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1292 }
Scroggo97c88c22011-05-11 14:05:25 +00001293 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001294 GL_CALL_RET(programData->fUniLocations.fColorFilterUni,
1295 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001296 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1297 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001298
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001299 if (kUseUniform == programData->fUniLocations.fColorMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001300 GL_CALL_RET(programData->fUniLocations.fColorMatrixUni,
1301 GetUniformLocation(progID, COL_MATRIX_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001302 }
1303
1304 if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001305 GL_CALL_RET(programData->fUniLocations.fColorMatrixVecUni,
1306 GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001307 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001308 if (kUseUniform == programData->fUniLocations.fCoverageUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001309 GL_CALL_RET(programData->fUniLocations.fCoverageUni,
1310 GetUniformLocation(progID, COV_UNI_NAME));
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001311 GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni);
1312 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001313
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001314 if (kUseUniform == programData->fUniLocations.fEdgesUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001315 GL_CALL_RET(programData->fUniLocations.fEdgesUni,
1316 GetUniformLocation(progID, EDGES_UNI_NAME));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001317 GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni);
1318 } else {
1319 programData->fUniLocations.fEdgesUni = kUnusedUniform;
1320 }
1321
tomhudson@google.com93813632011-10-27 20:21:16 +00001322 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001323 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001324 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001325 if (kUseUniform == locations.fTextureMatrixUni) {
1326 GrStringBuilder texMName;
1327 tex_matrix_name(s, &texMName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001328 GL_CALL_RET(locations.fTextureMatrixUni,
1329 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001330 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1331 }
1332
1333 if (kUseUniform == locations.fSamplerUni) {
1334 GrStringBuilder samplerName;
1335 sampler_name(s, &samplerName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001336 GL_CALL_RET(locations.fSamplerUni,
1337 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001338 GrAssert(kUnusedUniform != locations.fSamplerUni);
1339 }
1340
1341 if (kUseUniform == locations.fNormalizedTexelSizeUni) {
1342 GrStringBuilder texelSizeName;
1343 normalized_texel_size_name(s, &texelSizeName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001344 GL_CALL_RET(locations.fNormalizedTexelSizeUni,
1345 GetUniformLocation(progID, texelSizeName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001346 GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni);
1347 }
1348
1349 if (kUseUniform == locations.fRadial2Uni) {
1350 GrStringBuilder radial2ParamName;
1351 radial2_param_name(s, &radial2ParamName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001352 GL_CALL_RET(locations.fRadial2Uni,
1353 GetUniformLocation(progID, radial2ParamName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001354 GrAssert(kUnusedUniform != locations.fRadial2Uni);
1355 }
junov@google.com6acc9b32011-05-16 18:32:07 +00001356
1357 if (kUseUniform == locations.fTexDomUni) {
1358 GrStringBuilder texDomName;
1359 tex_domain_name(s, &texDomName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001360 GL_CALL_RET(locations.fTexDomUni,
1361 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001362 GrAssert(kUnusedUniform != locations.fTexDomUni);
1363 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001364
1365 GrStringBuilder kernelName, imageIncrementName;
1366 convolve_param_names(s, &kernelName, &imageIncrementName);
1367 if (kUseUniform == locations.fKernelUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001368 GL_CALL_RET(locations.fKernelUni,
1369 GetUniformLocation(progID, kernelName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001370 GrAssert(kUnusedUniform != locations.fKernelUni);
1371 }
1372
1373 if (kUseUniform == locations.fImageIncrementUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001374 GL_CALL_RET(locations.fImageIncrementUni,
1375 GetUniformLocation(progID,
1376 imageIncrementName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001377 GrAssert(kUnusedUniform != locations.fImageIncrementUni);
1378 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001379
1380 if (NULL != programData->fCustomStage[s]) {
1381 programData->fCustomStage[s]->
1382 initUniforms(gl.interface(), progID);
1383 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001384 }
1385 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001386 GL_CALL(UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001387
1388 // init sampler unis and set bogus values for state tracking
tomhudson@google.com93813632011-10-27 20:21:16 +00001389 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001390 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001391 GL_CALL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001392 }
1393 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
1394 programData->fRadial2CenterX1[s] = GR_ScalarMax;
1395 programData->fRadial2Radius0[s] = -GR_ScalarMax;
1396 programData->fTextureWidth[s] = -1;
1397 programData->fTextureHeight[s] = -1;
tomhudson@google.com24878f72012-03-22 14:44:46 +00001398 programData->fTextureDomain[s].setEmpty();
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001399 // Must not reset fStageOverride[] here.
bsalomon@google.com91961302011-05-09 18:39:58 +00001400 }
1401 programData->fViewMatrix = GrMatrix::InvalidMatrix();
1402 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001403 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001404}
1405
junov@google.comf93e7172011-03-31 21:26:24 +00001406//============================================================================
1407// Stage code generation
1408//============================================================================
1409
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001410namespace {
1411
1412bool isRadialMapping(GrGLProgram::StageDesc::CoordMapping mapping) {
1413 return
1414 (GrGLProgram::StageDesc::kRadial2Gradient_CoordMapping == mapping ||
1415 GrGLProgram::StageDesc::kRadial2GradientDegenerate_CoordMapping == mapping);
1416}
1417
tomhudson@google.comda668982011-12-07 15:06:29 +00001418GrGLShaderVar* genRadialVS(int stageNum,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001419 ShaderCodeSegments* segments,
1420 GrGLProgram::StageUniLocations* locations,
1421 const char** radial2VaryingVSName,
1422 const char** radial2VaryingFSName,
1423 const char* varyingVSName,
1424 int varyingDims, int coordDims) {
1425
1426 GrGLShaderVar* radial2FSParams = &segments->fFSUnis.push_back();
tomhudson@google.com168e6342012-04-18 17:49:20 +00001427 radial2FSParams->setType(kFloat_GrSLType);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001428 radial2FSParams->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001429 radial2FSParams->setArrayCount(6);
1430 radial2_param_name(stageNum, radial2FSParams->accessName());
1431 segments->fVSUnis.push_back(*radial2FSParams).setEmitPrecision(true);
1432
1433 locations->fRadial2Uni = kUseUniform;
1434
1435 // for radial grads without perspective we can pass the linear
1436 // part of the quadratic as a varying.
1437 if (varyingDims == coordDims) {
1438 GrAssert(2 == coordDims);
tomhudson@google.com168e6342012-04-18 17:49:20 +00001439 append_varying(kFloat_GrSLType,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001440 "Radial2BCoeff",
1441 stageNum,
1442 segments,
1443 radial2VaryingVSName,
1444 radial2VaryingFSName);
1445
tomhudson@google.comda668982011-12-07 15:06:29 +00001446 GrStringBuilder radial2p2;
1447 GrStringBuilder radial2p3;
1448 radial2FSParams->appendArrayAccess(2, &radial2p2);
1449 radial2FSParams->appendArrayAccess(3, &radial2p3);
1450
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001451 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
1452 const char* r2ParamName = radial2FSParams->getName().c_str();
tomhudson@google.comda668982011-12-07 15:06:29 +00001453 segments->fVSCode.appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
1454 *radial2VaryingVSName, radial2p2.c_str(),
1455 varyingVSName, radial2p3.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001456 }
1457
tomhudson@google.comda668982011-12-07 15:06:29 +00001458 return radial2FSParams;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001459}
1460
1461bool genRadial2GradientCoordMapping(int stageNum,
1462 ShaderCodeSegments* segments,
1463 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001464 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001465 GrStringBuilder& sampleCoords,
1466 GrStringBuilder& fsCoordName,
1467 int varyingDims,
1468 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001469 GrStringBuilder cName("c");
1470 GrStringBuilder ac4Name("ac4");
1471 GrStringBuilder rootName("root");
1472
1473 cName.appendS32(stageNum);
1474 ac4Name.appendS32(stageNum);
1475 rootName.appendS32(stageNum);
1476
tomhudson@google.comda668982011-12-07 15:06:29 +00001477 GrStringBuilder radial2p0;
1478 GrStringBuilder radial2p1;
1479 GrStringBuilder radial2p2;
1480 GrStringBuilder radial2p3;
1481 GrStringBuilder radial2p4;
1482 GrStringBuilder radial2p5;
1483 radial2Params->appendArrayAccess(0, &radial2p0);
1484 radial2Params->appendArrayAccess(1, &radial2p1);
1485 radial2Params->appendArrayAccess(2, &radial2p2);
1486 radial2Params->appendArrayAccess(3, &radial2p3);
1487 radial2Params->appendArrayAccess(4, &radial2p4);
1488 radial2Params->appendArrayAccess(5, &radial2p5);
1489
tomhudson@google.com5960d002011-10-19 20:21:48 +00001490 // if we were able to interpolate the linear component bVar is the varying
1491 // otherwise compute it
1492 GrStringBuilder bVar;
1493 if (coordDims == varyingDims) {
1494 bVar = radial2VaryingFSName;
1495 GrAssert(2 == varyingDims);
1496 } else {
1497 GrAssert(3 == varyingDims);
1498 bVar = "b";
1499 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001500 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1501 bVar.c_str(), radial2p2.c_str(),
1502 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001503 }
1504
1505 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001506 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001507 cName.c_str(), fsCoordName.c_str(),
1508 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001509 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001510 // ac4 = 4.0 * params[0] * c
tomhudson@google.comda668982011-12-07 15:06:29 +00001511 segments->fFSCode.appendf("\tfloat %s = %s * 4.0 * %s;\n",
1512 ac4Name.c_str(), radial2p0.c_str(),
tomhudson@google.com5960d002011-10-19 20:21:48 +00001513 cName.c_str());
1514
1515 // root = sqrt(b^2-4ac)
1516 // (abs to avoid exception due to fp precision)
1517 segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
1518 rootName.c_str(), bVar.c_str(), bVar.c_str(),
1519 ac4Name.c_str());
1520
1521 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
1522 // y coord is 0.5 (texture is effectively 1D)
tomhudson@google.comda668982011-12-07 15:06:29 +00001523 sampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)",
1524 bVar.c_str(), radial2p5.c_str(),
1525 rootName.c_str(), radial2p1.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001526 return true;
1527}
1528
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001529bool genRadial2GradientDegenerateCoordMapping(int stageNum,
1530 ShaderCodeSegments* segments,
1531 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001532 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001533 GrStringBuilder& sampleCoords,
1534 GrStringBuilder& fsCoordName,
1535 int varyingDims,
1536 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001537 GrStringBuilder cName("c");
1538
1539 cName.appendS32(stageNum);
1540
tomhudson@google.comda668982011-12-07 15:06:29 +00001541 GrStringBuilder radial2p2;
1542 GrStringBuilder radial2p3;
1543 GrStringBuilder radial2p4;
1544 radial2Params->appendArrayAccess(2, &radial2p2);
1545 radial2Params->appendArrayAccess(3, &radial2p3);
1546 radial2Params->appendArrayAccess(4, &radial2p4);
1547
tomhudson@google.com5960d002011-10-19 20:21:48 +00001548 // if we were able to interpolate the linear component bVar is the varying
1549 // otherwise compute it
1550 GrStringBuilder bVar;
1551 if (coordDims == varyingDims) {
1552 bVar = radial2VaryingFSName;
1553 GrAssert(2 == varyingDims);
1554 } else {
1555 GrAssert(3 == varyingDims);
1556 bVar = "b";
1557 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001558 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1559 bVar.c_str(), radial2p2.c_str(),
1560 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001561 }
1562
1563 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001564 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001565 cName.c_str(), fsCoordName.c_str(),
1566 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001567 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001568
1569 // x coord is: -c/b
1570 // y coord is 0.5 (texture is effectively 1D)
1571 sampleCoords.printf("vec2((-%s / %s), 0.5)", cName.c_str(), bVar.c_str());
1572 return true;
1573}
1574
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001575void gen2x2FS(int stageNum,
1576 ShaderCodeSegments* segments,
1577 GrGLProgram::StageUniLocations* locations,
1578 GrStringBuilder* sampleCoords,
1579 const char* samplerName,
1580 const char* texelSizeName,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001581 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001582 const char* fsOutColor,
1583 GrStringBuilder& texFunc,
1584 GrStringBuilder& modulate,
1585 bool complexCoord,
1586 int coordDims) {
1587 locations->fNormalizedTexelSizeUni = kUseUniform;
1588 if (complexCoord) {
1589 // assign the coord to a var rather than compute 4x.
1590 GrStringBuilder coordVar("tCoord");
1591 coordVar.appendS32(stageNum);
1592 segments->fFSCode.appendf("\t%s %s = %s;\n",
1593 float_vector_type_str(coordDims),
1594 coordVar.c_str(), sampleCoords->c_str());
1595 *sampleCoords = coordVar;
1596 }
1597 GrAssert(2 == coordDims);
1598 GrStringBuilder accumVar("accum");
1599 accumVar.appendS32(stageNum);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001600 segments->fFSCode.appendf("\tvec4 %s = %s(%s, %s + vec2(-%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, sampleCoords->c_str(), texelSizeName, texelSizeName, swizzle);
1601 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, sampleCoords->c_str(), texelSizeName, texelSizeName, swizzle);
1602 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(-%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, sampleCoords->c_str(), texelSizeName, texelSizeName, swizzle);
1603 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, sampleCoords->c_str(), texelSizeName, texelSizeName, swizzle);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001604 segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str());
1605
1606}
1607
1608void genConvolutionVS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001609 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001610 ShaderCodeSegments* segments,
1611 GrGLProgram::StageUniLocations* locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001612 GrGLShaderVar** kernel,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001613 const char** imageIncrementName,
1614 const char* varyingVSName) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001615 //GrGLShaderVar* kernel = &segments->fFSUnis.push_back();
1616 *kernel = &segments->fFSUnis.push_back();
tomhudson@google.com168e6342012-04-18 17:49:20 +00001617 (*kernel)->setType(kFloat_GrSLType);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001618 (*kernel)->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.comda668982011-12-07 15:06:29 +00001619 (*kernel)->setArrayCount(desc.fKernelWidth);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001620 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
tomhudson@google.com168e6342012-04-18 17:49:20 +00001621 imgInc->setType(kVec2f_GrSLType);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001622 imgInc->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001623
1624 convolve_param_names(stageNum,
tomhudson@google.comda668982011-12-07 15:06:29 +00001625 (*kernel)->accessName(),
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001626 imgInc->accessName());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001627 *imageIncrementName = imgInc->getName().c_str();
1628
1629 // need image increment in both VS and FS
1630 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1631
1632 locations->fKernelUni = kUseUniform;
1633 locations->fImageIncrementUni = kUseUniform;
1634 float scale = (desc.fKernelWidth - 1) * 0.5f;
1635 segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n",
1636 varyingVSName, scale, scale,
1637 *imageIncrementName);
1638}
1639
1640void genConvolutionFS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001641 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001642 ShaderCodeSegments* segments,
1643 const char* samplerName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001644 GrGLShaderVar* kernel,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001645 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001646 const char* imageIncrementName,
1647 const char* fsOutColor,
1648 GrStringBuilder& sampleCoords,
1649 GrStringBuilder& texFunc,
1650 GrStringBuilder& modulate) {
1651 GrStringBuilder sumVar("sum");
1652 sumVar.appendS32(stageNum);
1653 GrStringBuilder coordVar("coord");
1654 coordVar.appendS32(stageNum);
1655
tomhudson@google.comda668982011-12-07 15:06:29 +00001656 GrStringBuilder kernelIndex;
1657 kernel->appendArrayAccess("i", &kernelIndex);
1658
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001659 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1660 sumVar.c_str());
1661 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1662 coordVar.c_str(),
1663 sampleCoords.c_str());
1664 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1665 desc.fKernelWidth);
tomhudson@google.comda668982011-12-07 15:06:29 +00001666 segments->fFSCode.appendf("\t\t%s += %s(%s, %s)%s * %s;\n",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001667 sumVar.c_str(), texFunc.c_str(),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001668 samplerName, coordVar.c_str(), swizzle,
tomhudson@google.comda668982011-12-07 15:06:29 +00001669 kernelIndex.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001670 segments->fFSCode.appendf("\t\t%s += %s;\n",
1671 coordVar.c_str(),
1672 imageIncrementName);
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +00001673 segments->fFSCode.append("\t}\n");
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001674 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1675 sumVar.c_str(), modulate.c_str());
1676}
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001677
1678void genMorphologyVS(int stageNum,
1679 const StageDesc& desc,
1680 ShaderCodeSegments* segments,
1681 GrGLProgram::StageUniLocations* locations,
1682 const char** imageIncrementName,
1683 const char* varyingVSName) {
1684 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
tomhudson@google.com168e6342012-04-18 17:49:20 +00001685 imgInc->setType(kVec2f_GrSLType);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001686 imgInc->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
1687
1688 image_increment_param_name(stageNum, imgInc->accessName());
1689 *imageIncrementName = imgInc->getName().c_str();
1690
1691 // need image increment in both VS and FS
1692 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1693
1694 locations->fImageIncrementUni = kUseUniform;
1695 segments->fVSCode.appendf("\t%s -= vec2(%d, %d) * %s;\n",
1696 varyingVSName, desc.fKernelWidth,
1697 desc.fKernelWidth, *imageIncrementName);
1698}
1699
1700void genMorphologyFS(int stageNum,
1701 const StageDesc& desc,
1702 ShaderCodeSegments* segments,
1703 const char* samplerName,
1704 const char* swizzle,
1705 const char* imageIncrementName,
1706 const char* fsOutColor,
1707 GrStringBuilder& sampleCoords,
1708 GrStringBuilder& texFunc,
1709 GrStringBuilder& modulate) {
1710 GrStringBuilder valueVar("value");
1711 valueVar.appendS32(stageNum);
1712 GrStringBuilder coordVar("coord");
1713 coordVar.appendS32(stageNum);
1714 bool isDilate = StageDesc::kDilate_FetchMode == desc.fFetchMode;
1715
1716 if (isDilate) {
1717 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1718 valueVar.c_str());
1719 } else {
1720 segments->fFSCode.appendf("\tvec4 %s = vec4(1, 1, 1, 1);\n",
1721 valueVar.c_str());
1722 }
1723 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1724 coordVar.c_str(),
1725 sampleCoords.c_str());
1726 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1727 desc.fKernelWidth * 2 + 1);
1728 segments->fFSCode.appendf("\t\t%s = %s(%s, %s(%s, %s)%s);\n",
1729 valueVar.c_str(), isDilate ? "max" : "min",
1730 valueVar.c_str(), texFunc.c_str(),
1731 samplerName, coordVar.c_str(), swizzle);
1732 segments->fFSCode.appendf("\t\t%s += %s;\n",
1733 coordVar.c_str(),
1734 imageIncrementName);
1735 segments->fFSCode.appendf("\t}\n");
1736 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1737 valueVar.c_str(), modulate.c_str());
1738}
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001739
1740}
1741
bsalomon@google.com96399942012-02-13 14:39:16 +00001742void GrGLProgram::genStageCode(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001743 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001744 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001745 const char* fsInColor, // NULL means no incoming color
1746 const char* fsOutColor,
1747 const char* vsInCoord,
1748 ShaderCodeSegments* segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001749 StageUniLocations* locations,
1750 GrGLProgramStage* customStage) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001751
bsalomon@google.com74b98712011-11-11 19:46:16 +00001752 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
1753 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) ==
1754 desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001755
junov@google.comf93e7172011-03-31 21:26:24 +00001756 // First decide how many coords are needed to access the texture
1757 // Right now it's always 2 but we could start using 1D textures for
1758 // gradients.
1759 static const int coordDims = 2;
1760 int varyingDims;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001761
junov@google.comf93e7172011-03-31 21:26:24 +00001762 /// Vertex Shader Stuff
1763
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001764 if (NULL != customStage) {
1765 customStage->setupVSUnis(segments->fVSUnis, stageNum);
1766 }
1767
junov@google.comf93e7172011-03-31 21:26:24 +00001768 // decide whether we need a matrix to transform texture coords
1769 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001770 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001771 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001772 varyingDims = coordDims;
1773 } else {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001774 GrGLShaderVar* mat;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001775 #if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001776 mat = &segments->fVSAttrs.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001777 mat->setTypeModifier(GrGLShaderVar::kAttribute_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001778 locations->fTextureMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +00001779 #else
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001780 mat = &segments->fVSUnis.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001781 mat->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001782 locations->fTextureMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001783 #endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001784 tex_matrix_name(stageNum, mat->accessName());
tomhudson@google.com168e6342012-04-18 17:49:20 +00001785 mat->setType(kMat33f_GrSLType);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001786 matName = mat->getName().c_str();
1787
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001788 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001789 varyingDims = coordDims;
1790 } else {
1791 varyingDims = coordDims + 1;
1792 }
1793 }
1794
tomhudson@google.com168e6342012-04-18 17:49:20 +00001795 segments->fFSUnis.push_back().set(kSampler2D_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001796 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001797 sampler_name(stageNum, segments->fFSUnis.back().accessName());
bsalomon@google.com91961302011-05-09 18:39:58 +00001798 locations->fSamplerUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001799 const char* samplerName = segments->fFSUnis.back().getName().c_str();
junov@google.comf93e7172011-03-31 21:26:24 +00001800
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001801 const char* texelSizeName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001802 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
tomhudson@google.com168e6342012-04-18 17:49:20 +00001803 segments->fFSUnis.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001804 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001805 normalized_texel_size_name(stageNum, segments->fFSUnis.back().accessName());
1806 texelSizeName = segments->fFSUnis.back().getName().c_str();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001807 }
1808
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001809 const char *varyingVSName, *varyingFSName;
tomhudson@google.com168e6342012-04-18 17:49:20 +00001810 append_varying(GrSLFloatVectorType(varyingDims),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001811 "Stage",
1812 stageNum,
1813 segments,
1814 &varyingVSName,
1815 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001816
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001817 if (!matName) {
junov@google.comf93e7172011-03-31 21:26:24 +00001818 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001819 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001820 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001821 // varying = texMatrix * texCoord
1822 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001823 varyingVSName, matName, vsInCoord,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001824 vector_all_coords(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001825 }
1826
tomhudson@google.comda668982011-12-07 15:06:29 +00001827 GrGLShaderVar* radial2Params = NULL;
1828 const char* radial2VaryingVSName = NULL;
1829 const char* radial2VaryingFSName = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001830
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001831 if (isRadialMapping((StageDesc::CoordMapping) desc.fCoordMapping)) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001832 radial2Params = genRadialVS(stageNum, segments,
1833 locations,
1834 &radial2VaryingVSName,
1835 &radial2VaryingFSName,
1836 varyingVSName,
1837 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001838 }
1839
tomhudson@google.comda668982011-12-07 15:06:29 +00001840 GrGLShaderVar* kernel = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001841 const char* imageIncrementName = NULL;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001842 if (StageDesc::kConvolution_FetchMode == desc.fFetchMode) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001843 genConvolutionVS(stageNum, desc, segments, locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001844 &kernel, &imageIncrementName, varyingVSName);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001845 } else if (StageDesc::kDilate_FetchMode == desc.fFetchMode ||
1846 StageDesc::kErode_FetchMode == desc.fFetchMode) {
1847 genMorphologyVS(stageNum, desc, segments, locations,
1848 &imageIncrementName, varyingVSName);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001849 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001850
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001851 if (NULL != customStage) {
1852 GrStringBuilder vertexShader;
1853 customStage->emitVS(&vertexShader, varyingVSName);
1854 segments->fVSCode.appendf("{\n");
1855 segments->fVSCode.append(vertexShader);
1856 segments->fVSCode.appendf("}\n");
1857 }
1858
junov@google.comf93e7172011-03-31 21:26:24 +00001859 /// Fragment Shader Stuff
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001860
1861 if (NULL != customStage) {
1862 customStage->setupFSUnis(segments->fFSUnis, stageNum);
1863 }
1864
bsalomon@google.com91961302011-05-09 18:39:58 +00001865 GrStringBuilder fsCoordName;
junov@google.comf93e7172011-03-31 21:26:24 +00001866 // function used to access the shader, may be made projective
bsalomon@google.com91961302011-05-09 18:39:58 +00001867 GrStringBuilder texFunc("texture2D");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001868 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1869 StageDesc::kNoPerspective_OptFlagBit)) {
junov@google.comf93e7172011-03-31 21:26:24 +00001870 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001871 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001872 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001873 // if we have to do some special op on the varyings to get
junov@google.comf93e7172011-03-31 21:26:24 +00001874 // our final tex coords then when in perspective we have to
bsalomon@google.com91961302011-05-09 18:39:58 +00001875 // do an explicit divide. Otherwise, we can use a Proj func.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001876 if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
1877 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001878 texFunc.append("Proj");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001879 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001880 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001881 fsCoordName = "inCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +00001882 fsCoordName.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001883 segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n",
tomhudson@google.com168e6342012-04-18 17:49:20 +00001884 GrGLShaderVar::TypeString(GrSLFloatVectorType(coordDims)),
1885 fsCoordName.c_str(),
1886 varyingFSName,
1887 GrGLSLVectorNonhomogCoords(varyingDims),
1888 varyingFSName,
1889 GrGLSLVectorHomogCoord(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001890 }
1891 }
1892
bsalomon@google.comfc296292011-05-06 13:53:47 +00001893 GrStringBuilder sampleCoords;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001894 bool complexCoord = false;
junov@google.comf93e7172011-03-31 21:26:24 +00001895 switch (desc.fCoordMapping) {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001896 case StageDesc::kIdentity_CoordMapping:
junov@google.comf93e7172011-03-31 21:26:24 +00001897 sampleCoords = fsCoordName;
1898 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001899 case StageDesc::kSweepGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001900 sampleCoords.printf("vec2(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5, 0.5)", fsCoordName.c_str(), fsCoordName.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001901 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001902 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001903 case StageDesc::kRadialGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001904 sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001905 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001906 break;
tomhudson@google.com5960d002011-10-19 20:21:48 +00001907 case StageDesc::kRadial2Gradient_CoordMapping:
1908 complexCoord = genRadial2GradientCoordMapping(
1909 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001910 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001911 sampleCoords, fsCoordName,
1912 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001913
tomhudson@google.com5960d002011-10-19 20:21:48 +00001914 break;
1915 case StageDesc::kRadial2GradientDegenerate_CoordMapping:
1916 complexCoord = genRadial2GradientDegenerateCoordMapping(
1917 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001918 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001919 sampleCoords, fsCoordName,
1920 varyingDims, coordDims);
1921 break;
junov@google.comf93e7172011-03-31 21:26:24 +00001922
junov@google.comf93e7172011-03-31 21:26:24 +00001923 };
1924
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001925 static const uint32_t kMulByAlphaMask =
1926 (StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
1927 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag);
1928
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001929 const char* swizzle = "";
bsalomon@google.com74b98712011-11-11 19:46:16 +00001930 if (desc.fInConfigFlags & StageDesc::kSwapRAndB_InConfigFlag) {
1931 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag));
robertphillips@google.com69950682012-04-06 18:06:10 +00001932 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearRed_InConfigFlag));
bsalomon@google.com74b98712011-11-11 19:46:16 +00001933 swizzle = ".bgra";
1934 } else if (desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001935 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
robertphillips@google.com69950682012-04-06 18:06:10 +00001936 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearRed_InConfigFlag));
bsalomon@google.com74b98712011-11-11 19:46:16 +00001937 swizzle = ".aaaa";
robertphillips@google.com69950682012-04-06 18:06:10 +00001938 } else if (desc.fInConfigFlags & StageDesc::kSmearRed_InConfigFlag) {
1939 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
1940 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag));
1941 swizzle = ".rrrr";
1942 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001943
bsalomon@google.com91961302011-05-09 18:39:58 +00001944 GrStringBuilder modulate;
1945 if (NULL != fsInColor) {
1946 modulate.printf(" * %s", fsInColor);
1947 }
1948
tomhudson@google.com0d831722011-06-02 15:37:14 +00001949 if (desc.fOptFlags &
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001950 StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001951 GrStringBuilder texDomainName;
1952 tex_domain_name(stageNum, &texDomainName);
tomhudson@google.com168e6342012-04-18 17:49:20 +00001953 segments->fFSUnis.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001954 GrGLShaderVar::kUniform_TypeModifier, texDomainName);
junov@google.com6acc9b32011-05-16 18:32:07 +00001955 GrStringBuilder coordVar("clampCoord");
1956 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001957 float_vector_type_str(coordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001958 coordVar.c_str(),
1959 sampleCoords.c_str(),
1960 texDomainName.c_str(),
1961 texDomainName.c_str());
1962 sampleCoords = coordVar;
1963 locations->fTexDomUni = kUseUniform;
1964 }
1965
tomhudson@google.com5960d002011-10-19 20:21:48 +00001966 switch (desc.fFetchMode) {
1967 case StageDesc::k2x2_FetchMode:
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001968 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001969 gen2x2FS(stageNum, segments, locations, &sampleCoords,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001970 samplerName, texelSizeName, swizzle, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001971 texFunc, modulate, complexCoord, coordDims);
1972 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001973 case StageDesc::kConvolution_FetchMode:
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001974 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001975 genConvolutionFS(stageNum, desc, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001976 samplerName, kernel, swizzle, imageIncrementName, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001977 sampleCoords, texFunc, modulate);
1978 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001979 case StageDesc::kDilate_FetchMode:
1980 case StageDesc::kErode_FetchMode:
1981 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
1982 genMorphologyFS(stageNum, desc, segments,
1983 samplerName, swizzle, imageIncrementName, fsOutColor,
1984 sampleCoords, texFunc, modulate);
1985 break;
tomhudson@google.com5960d002011-10-19 20:21:48 +00001986 default:
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001987 if (desc.fInConfigFlags & kMulByAlphaMask) {
1988 // only one of the mul by alpha flags should be set
1989 GrAssert(GrIsPow2(kMulByAlphaMask & desc.fInConfigFlags));
bsalomon@google.com74b98712011-11-11 19:46:16 +00001990 GrAssert(!(desc.fInConfigFlags &
1991 StageDesc::kSmearAlpha_InConfigFlag));
robertphillips@google.com69950682012-04-06 18:06:10 +00001992 GrAssert(!(desc.fInConfigFlags &
1993 StageDesc::kSmearRed_InConfigFlag));
bsalomon@google.com74b98712011-11-11 19:46:16 +00001994 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
1995 fsOutColor, texFunc.c_str(),
1996 samplerName, sampleCoords.c_str(),
1997 swizzle);
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001998 if (desc.fInConfigFlags &
1999 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) {
2000 segments->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
2001 fsOutColor, fsOutColor, fsOutColor,
2002 fsOutColor, modulate.c_str());
2003 } else {
2004 segments->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
2005 fsOutColor, fsOutColor, fsOutColor,
2006 fsOutColor, modulate.c_str());
2007 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00002008 } else {
2009 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n",
2010 fsOutColor, texFunc.c_str(),
2011 samplerName, sampleCoords.c_str(),
2012 swizzle, modulate.c_str());
2013 }
junov@google.comf93e7172011-03-31 21:26:24 +00002014 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00002015
2016 if (NULL != customStage) {
2017 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
2018 StageDesc::kNoPerspective_OptFlagBit)) {
2019 customStage->setSamplerMode(GrGLProgramStage::kDefault_SamplerMode);
2020 } else if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
2021 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
2022 customStage->setSamplerMode(GrGLProgramStage::kProj_SamplerMode);
2023 } else {
2024 customStage->setSamplerMode(
2025 GrGLProgramStage::kExplicitDivide_SamplerMode);
2026 }
2027
2028 GrStringBuilder fragmentShader;
2029 fsCoordName = customStage->emitTextureSetup(
2030 &fragmentShader, varyingFSName,
2031 stageNum, coordDims, varyingDims);
2032 customStage->emitFS(&fragmentShader, fsOutColor, fsInColor,
2033 samplerName, fsCoordName.c_str());
2034
2035 // Enclose custom code in a block to avoid namespace conflicts
2036 segments->fFSCode.appendf("{\n");
2037 segments->fFSCode.append(fragmentShader);
2038 segments->fFSCode.appendf("}\n");
2039 }
junov@google.comf93e7172011-03-31 21:26:24 +00002040}
tomhudson@google.com5960d002011-10-19 20:21:48 +00002041
tomhudson@google.com5960d002011-10-19 20:21:48 +00002042