blob: 4b4140d206ade1a58c3e93cb151bf43638a790a4 [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
bsalomon@google.com4fa66942011-09-20 19:06:12 +000012#include "GrAllocator.h"
13#include "GrGLShaderVar.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000014#include "SkTrace.h"
Scroggo97c88c22011-05-11 14:05:25 +000015#include "SkXfermode.h"
16
junov@google.comf93e7172011-03-31 21:26:24 +000017namespace {
18
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019const char* GrPrecision(const GrGLInterface* gl) {
20 if (gl->supportsES()) {
junov@google.comf93e7172011-03-31 21:26:24 +000021 return "mediump";
22 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +000023 return " ";
junov@google.comf93e7172011-03-31 21:26:24 +000024 }
25}
26
bsalomon@google.com0b77d682011-08-19 13:28:54 +000027const char* GrShaderPrecision(const GrGLInterface* gl) {
28 if (gl->supportsES()) {
junov@google.comf93e7172011-03-31 21:26:24 +000029 return "precision mediump float;\n";
30 } else {
31 return "";
32 }
33}
34
35} // namespace
36
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000037#define PRINT_SHADERS 0
38
bsalomon@google.com4fa66942011-09-20 19:06:12 +000039typedef GrTAllocator<GrGLShaderVar> VarArray;
40
41// number of each input/output type in a single allocation block
42static const int gVarsPerBlock = 8;
43// except FS outputs where we expect 2 at most.
44static const int gMaxFSOutputs = 2;
45
46struct ShaderCodeSegments {
47 ShaderCodeSegments()
48 : fVSUnis(gVarsPerBlock)
49 , fVSAttrs(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000050 , fVSOutputs(gVarsPerBlock)
51 , fGSInputs(gVarsPerBlock)
52 , fGSOutputs(gVarsPerBlock)
53 , fFSInputs(gVarsPerBlock)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000054 , fFSUnis(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000055 , fFSOutputs(gMaxFSOutputs)
56 , fUsesGS(false) {}
bsalomon@google.com4fa66942011-09-20 19:06:12 +000057 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
58 VarArray fVSUnis;
59 VarArray fVSAttrs;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000060 VarArray fVSOutputs;
61 VarArray fGSInputs;
62 VarArray fGSOutputs;
63 VarArray fFSInputs;
64 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
bsalomon@google.com4fa66942011-09-20 19:06:12 +000065 VarArray fFSUnis;
66 VarArray fFSOutputs;
67 GrStringBuilder fFSFunctions;
68 GrStringBuilder fVSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000069 GrStringBuilder fGSCode;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000070 GrStringBuilder fFSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000071
72 bool fUsesGS;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000073};
74
75
bsalomon@google.com4be283f2011-04-19 21:15:09 +000076#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +000077 #define VIEW_MATRIX_NAME "aViewM"
78#else
79 #define VIEW_MATRIX_NAME "uViewM"
80#endif
81
82#define POS_ATTR_NAME "aPosition"
83#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000084#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000085#define EDGE_ATTR_NAME "aEdge"
bsalomon@google.com4be283f2011-04-19 21:15:09 +000086#define COL_UNI_NAME "uColor"
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000087#define EDGES_UNI_NAME "uEdges"
Scroggo97c88c22011-05-11 14:05:25 +000088#define COL_FILTER_UNI_NAME "uColorFilter"
junov@google.comf93e7172011-03-31 21:26:24 +000089
bsalomon@google.com4fa66942011-09-20 19:06:12 +000090namespace {
91inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000092 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +000093 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +000094}
95
bsalomon@google.com4fa66942011-09-20 19:06:12 +000096inline GrGLShaderVar::Type float_vector_type(int count) {
97 GR_STATIC_ASSERT(GrGLShaderVar::kFloat_Type == 0);
98 GR_STATIC_ASSERT(GrGLShaderVar::kVec2f_Type == 1);
99 GR_STATIC_ASSERT(GrGLShaderVar::kVec3f_Type == 2);
100 GR_STATIC_ASSERT(GrGLShaderVar::kVec4f_Type == 3);
101 GrAssert(count > 0 && count <= 4);
102 return (GrGLShaderVar::Type)(count - 1);
junov@google.comf93e7172011-03-31 21:26:24 +0000103}
104
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000105inline const char* float_vector_type_str(int count) {
106 return GrGLShaderVar::TypeString(float_vector_type(count));
107}
108
109inline const char* vector_homog_coord(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000110 static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"};
111 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS));
112 return HOMOGS[count];
113}
114
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000115inline const char* vector_nonhomog_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000116 static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"};
117 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS));
118 return NONHOMOGS[count];
119}
120
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000121inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000122 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
123 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
124 return ALL[count];
125}
126
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000127inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000128 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000129 "vec3(1,1,1)", "vec4(1,1,1,1)"};
130 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
131 return ONESVEC[count];
132}
133
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000134inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000135 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000136 "vec3(0,0,0)", "vec4(0,0,0,0)"};
137 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
138 return ZEROSVEC[count];
139}
140
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000141inline const char* declared_color_output_name() { return "fsColorOut"; }
142inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000143
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000144inline void tex_matrix_name(int stage, GrStringBuilder* s) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000145#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +0000146 *s = "aTexM";
147#else
148 *s = "uTexM";
149#endif
bsalomon@google.comfc296292011-05-06 13:53:47 +0000150 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000151}
152
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000153inline void normalized_texel_size_name(int stage, GrStringBuilder* s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000154 *s = "uTexelSize";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000155 s->appendS32(stage);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000156}
157
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000158inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000159 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000160 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000161}
162
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000163inline void radial2_param_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000164 *s = "uRadial2Params";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000165 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000166}
167
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000168inline void convolve_param_names(int stage, GrStringBuilder* k, GrStringBuilder* i) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000169 *k = "uKernel";
170 k->appendS32(stage);
171 *i = "uImageIncrement";
172 i->appendS32(stage);
173}
174
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000175inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000176 *s = "uTexDom";
177 s->appendS32(stage);
178}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000179}
junov@google.com6acc9b32011-05-16 18:32:07 +0000180
junov@google.comf93e7172011-03-31 21:26:24 +0000181GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000182}
183
184GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000185}
186
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000187void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000188 GrBlendCoeff* dstCoeff) const {
189 switch (fProgramDesc.fDualSrcOutput) {
190 case ProgramDesc::kNone_DualSrcOutput:
191 break;
192 // the prog will write a coverage value to the secondary
193 // output and the dst is blended by one minus that value.
194 case ProgramDesc::kCoverage_DualSrcOutput:
195 case ProgramDesc::kCoverageISA_DualSrcOutput:
196 case ProgramDesc::kCoverageISC_DualSrcOutput:
197 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff;
198 break;
199 default:
200 GrCrash("Unexpected dual source blend output");
201 break;
202 }
203}
204
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000205// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000206// vars can be vec4s or floats (or one of each)
207// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000208// if either var is "" then assign to the other var
209// if both are "" then assign all ones
210static inline void modulate_helper(const char* outputVar,
211 const char* var0,
212 const char* var1,
213 GrStringBuilder* code) {
214 GrAssert(NULL != outputVar);
215 GrAssert(NULL != var0);
216 GrAssert(NULL != var1);
217 GrAssert(NULL != code);
218
219 bool has0 = '\0' != *var0;
220 bool has1 = '\0' != *var1;
221
222 if (!has0 && !has1) {
223 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
224 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000225 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000226 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000227 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000228 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000229 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000230 }
231}
232
233// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000234// vars can be vec4s or floats (or one of each)
235// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000236// if either var is "" then assign to the other var
237// if both are "" then assign all zeros
238static inline void add_helper(const char* outputVar,
239 const char* var0,
240 const char* var1,
241 GrStringBuilder* code) {
242 GrAssert(NULL != outputVar);
243 GrAssert(NULL != var0);
244 GrAssert(NULL != var1);
245 GrAssert(NULL != code);
246
247 bool has0 = '\0' != *var0;
248 bool has1 = '\0' != *var1;
249
250 if (!has0 && !has1) {
251 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
252 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000253 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000254 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000255 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000256 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000257 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000258 }
259}
260
261// given two blend coeffecients determine whether the src
262// and/or dst computation can be omitted.
263static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
264 SkXfermode::Coeff dstCoeff,
265 bool* needSrcValue,
266 bool* needDstValue) {
267 if (SkXfermode::kZero_Coeff == srcCoeff) {
268 switch (dstCoeff) {
269 // these all read the src
270 case SkXfermode::kSC_Coeff:
271 case SkXfermode::kISC_Coeff:
272 case SkXfermode::kSA_Coeff:
273 case SkXfermode::kISA_Coeff:
274 *needSrcValue = true;
275 break;
276 default:
277 *needSrcValue = false;
278 break;
279 }
280 } else {
281 *needSrcValue = true;
282 }
283 if (SkXfermode::kZero_Coeff == dstCoeff) {
284 switch (srcCoeff) {
285 // these all read the dst
286 case SkXfermode::kDC_Coeff:
287 case SkXfermode::kIDC_Coeff:
288 case SkXfermode::kDA_Coeff:
289 case SkXfermode::kIDA_Coeff:
290 *needDstValue = true;
291 break;
292 default:
293 *needDstValue = false;
294 break;
295 }
296 } else {
297 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000298 }
299}
300
301/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000302 * Create a blend_coeff * value string to be used in shader code. Sets empty
303 * string if result is trivially zero.
304 */
305static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
306 const char* src, const char* dst,
307 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000308 switch (coeff) {
309 case SkXfermode::kZero_Coeff: /** 0 */
310 *str = "";
311 break;
312 case SkXfermode::kOne_Coeff: /** 1 */
313 *str = value;
314 break;
315 case SkXfermode::kSC_Coeff:
316 str->printf("(%s * %s)", src, value);
317 break;
318 case SkXfermode::kISC_Coeff:
319 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
320 break;
321 case SkXfermode::kDC_Coeff:
322 str->printf("(%s * %s)", dst, value);
323 break;
324 case SkXfermode::kIDC_Coeff:
325 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
326 break;
327 case SkXfermode::kSA_Coeff: /** src alpha */
328 str->printf("(%s.a * %s)", src, value);
329 break;
330 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
331 str->printf("((1.0 - %s.a) * %s)", src, value);
332 break;
333 case SkXfermode::kDA_Coeff: /** dst alpha */
334 str->printf("(%s.a * %s)", dst, value);
335 break;
336 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
337 str->printf("((1.0 - %s.a) * %s)", dst, value);
338 break;
339 default:
340 GrCrash("Unexpected xfer coeff.");
341 break;
342 }
343}
344/**
Scroggo97c88c22011-05-11 14:05:25 +0000345 * Adds a line to the fragment shader code which modifies the color by
346 * the specified color filter.
347 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000348static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000349 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000350 SkXfermode::Coeff colorCoeff,
351 const char* inColor) {
352 GrStringBuilder colorStr, constStr;
353 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
354 inColor, inColor);
355 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
356 inColor, COL_FILTER_UNI_NAME);
357
358 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000359}
360
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000361namespace {
362
363const char* glsl_version_string(const GrGLInterface* gl,
364 GrGLProgram::GLSLVersion v) {
365 switch (v) {
366 case GrGLProgram::k120_GLSLVersion:
367 if (gl->supportsES()) {
368 // ES2s shader language is based on version 1.20 but is version
369 // 1.00 of the ES language.
370 return "#version 100\n";
371 } else {
372 return "#version 120\n";
373 }
374 case GrGLProgram::k130_GLSLVersion:
375 GrAssert(!gl->supportsES());
376 return "#version 130\n";
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000377 case GrGLProgram::k150_GLSLVersion:
378 GrAssert(!gl->supportsES());
379 return "#version 150\n";
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000380 default:
381 GrCrash("Unknown GL version.");
382 return ""; // suppress warning
383 }
384}
385
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000386// Adds a var that is computed in the VS and read in FS.
387// If there is a GS it will just pass it through.
388void append_varying(GrGLShaderVar::Type type,
389 const char* name,
390 ShaderCodeSegments* segments,
391 const char** vsOutName = NULL,
392 const char** fsInName = NULL) {
393 segments->fVSOutputs.push_back();
394 segments->fVSOutputs.back().setType(type);
395 segments->fVSOutputs.back().accessName()->printf("v%s", name);
396 if (vsOutName) {
397 *vsOutName = segments->fVSOutputs.back().getName().c_str();
398 }
399 // input to FS comes either from VS or GS
400 const GrStringBuilder* fsName;
401 if (segments->fUsesGS) {
402 // if we have a GS take each varying in as an array
403 // and output as non-array.
404 segments->fGSInputs.push_back();
405 segments->fGSInputs.back().setType(type);
406 segments->fGSInputs.back().setUnsizedArray();
407 *segments->fGSInputs.back().accessName() =
408 segments->fVSOutputs.back().getName();
409 segments->fGSOutputs.push_back();
410 segments->fGSOutputs.back().setType(type);
411 segments->fGSOutputs.back().accessName()->printf("g%s", name);
412 fsName = segments->fGSOutputs.back().accessName();
413 } else {
414 fsName = segments->fVSOutputs.back().accessName();
415 }
416 segments->fFSInputs.push_back();
417 segments->fFSInputs.back().setType(type);
418 segments->fFSInputs.back().setName(*fsName);
419 if (fsInName) {
420 *fsInName = fsName->c_str();
421 }
422}
423
424// version of above that adds a stage number to the
425// the var name (for uniqueness)
426void append_varying(GrGLShaderVar::Type type,
427 const char* name,
428 int stageNum,
429 ShaderCodeSegments* segments,
430 const char** vsOutName = NULL,
431 const char** fsInName = NULL) {
432 GrStringBuilder nameWithStage(name);
433 nameWithStage.appendS32(stageNum);
434 append_varying(type, nameWithStage.c_str(), segments, vsOutName, fsInName);
435}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000436}
437
bsalomon@google.com66105672011-09-15 15:12:00 +0000438void GrGLProgram::genEdgeCoverage(const GrGLInterface* gl,
439 GrVertexLayout layout,
440 CachedData* programData,
441 GrStringBuilder* coverageVar,
442 ShaderCodeSegments* segments) const {
443 if (fProgramDesc.fEdgeAANumEdges > 0) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000444 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec3f_Type,
445 EDGES_UNI_NAME,
446 fProgramDesc.fEdgeAANumEdges);
bsalomon@google.com66105672011-09-15 15:12:00 +0000447 programData->fUniLocations.fEdgesUni = kUseUniform;
448 int count = fProgramDesc.fEdgeAANumEdges;
449 segments->fFSCode.append(
450 "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n");
451 for (int i = 0; i < count; i++) {
452 segments->fFSCode.append("\tfloat a");
453 segments->fFSCode.appendS32(i);
454 segments->fFSCode.append(" = clamp(dot(" EDGES_UNI_NAME "[");
455 segments->fFSCode.appendS32(i);
456 segments->fFSCode.append("], pos), 0.0, 1.0);\n");
457 }
458 if (fProgramDesc.fEdgeAAConcave && (count & 0x01) == 0) {
459 // For concave polys, we consider the edges in pairs.
460 segments->fFSFunctions.append("float cross2(vec2 a, vec2 b) {\n");
461 segments->fFSFunctions.append("\treturn dot(a, vec2(b.y, -b.x));\n");
462 segments->fFSFunctions.append("}\n");
463 for (int i = 0; i < count; i += 2) {
464 segments->fFSCode.appendf("\tfloat eb%d;\n", i / 2);
465 segments->fFSCode.appendf("\tif (cross2(" EDGES_UNI_NAME "[%d].xy, " EDGES_UNI_NAME "[%d].xy) < 0.0) {\n", i, i + 1);
466 segments->fFSCode.appendf("\t\teb%d = a%d * a%d;\n", i / 2, i, i + 1);
467 segments->fFSCode.append("\t} else {\n");
468 segments->fFSCode.appendf("\t\teb%d = a%d + a%d - a%d * a%d;\n", i / 2, i, i + 1, i, i + 1);
469 segments->fFSCode.append("\t}\n");
470 }
471 segments->fFSCode.append("\tfloat edgeAlpha = ");
472 for (int i = 0; i < count / 2 - 1; i++) {
473 segments->fFSCode.appendf("min(eb%d, ", i);
474 }
475 segments->fFSCode.appendf("eb%d", count / 2 - 1);
476 for (int i = 0; i < count / 2 - 1; i++) {
477 segments->fFSCode.append(")");
478 }
479 segments->fFSCode.append(";\n");
480 } else {
481 segments->fFSCode.append("\tfloat edgeAlpha = ");
482 for (int i = 0; i < count - 1; i++) {
483 segments->fFSCode.appendf("min(a%d * a%d, ", i, i + 1);
484 }
485 segments->fFSCode.appendf("a%d * a0", count - 1);
486 for (int i = 0; i < count - 1; i++) {
487 segments->fFSCode.append(")");
488 }
489 segments->fFSCode.append(";\n");
490 }
491 *coverageVar = "edgeAlpha";
492 } else if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000493 const char *vsName, *fsName;
494 append_varying(GrGLShaderVar::kVec4f_Type, "Edge", segments, &vsName, &fsName);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000495 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000496 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
bsalomon@google.com66105672011-09-15 15:12:00 +0000497 if (GrDrawTarget::kHairLine_EdgeType == fProgramDesc.fVertexEdgeType) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000498 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com66105672011-09-15 15:12:00 +0000499 } else {
500 GrAssert(GrDrawTarget::kHairQuad_EdgeType == fProgramDesc.fVertexEdgeType);
501 // for now we know we're not in perspective, so we could compute this
502 // per-quadratic rather than per pixel
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000503 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
504 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
505 segments->fFSCode.appendf("\tfloat dfdx = 2.0*%s.x*duvdx.x - duvdx.y;\n", fsName);
506 segments->fFSCode.appendf("\tfloat dfdy = 2.0*%s.x*duvdy.x - duvdy.y;\n", fsName);
507 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com66105672011-09-15 15:12:00 +0000508 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / (dfdx*dfdx + dfdy*dfdy));\n");
509 if (gl->supportsES()) {
510 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
511 }
512 }
513 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
514 *coverageVar = "edgeAlpha";
515 } else {
516 coverageVar->reset();
517 }
518}
519
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000520namespace {
521
522// returns true if the color output was explicitly declared or not.
523bool decl_and_get_fs_color_output(GrGLProgram::GLSLVersion v,
524 VarArray* fsOutputs,
525 const char** name) {
526 switch (v) {
527 case GrGLProgram::k120_GLSLVersion:
528 *name = "gl_FragColor";
529 return false;
530 break;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000531 case GrGLProgram::k130_GLSLVersion: // fallthru
532 case GrGLProgram::k150_GLSLVersion:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000533 *name = declared_color_output_name();
534 fsOutputs->push_back().set(GrGLShaderVar::kVec4f_Type,
535 declared_color_output_name());
536 return true;
537 break;
538 default:
539 GrCrash("Unknown GLSL version.");
540 return false; // suppress warning
541 }
542}
543
544}
545
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000546void GrGLProgram::genGeometryShader(const GrGLInterface* gl,
547 GLSLVersion glslVersion,
548 ShaderCodeSegments* segments) const {
549#if GR_GL_EXPERIMENTAL_GS
550 if (fProgramDesc.fExperimentalGS) {
551 GrAssert(glslVersion >= k150_GLSLVersion);
552 segments->fGSHeader.append("layout(triangles) in;\n"
553 "layout(triangle_strip, max_vertices = 6) out;\n");
554 segments->fGSCode.append("void main() {\n"
555 "\tfor (int i = 0; i < 3; ++i) {\n"
556 "\t\tgl_Position = gl_in[i].gl_Position;\n");
557 if (this->fProgramDesc.fEmitsPointSize) {
558 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
559 }
560 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
561 int count = segments->fGSInputs.count();
562 for (int i = 0; i < count; ++i) {
563 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
564 segments->fGSOutputs[i].getName().c_str(),
565 segments->fGSInputs[i].getName().c_str());
566 }
567 segments->fGSCode.append("\t\tEmitVertex();\n"
568 "\t}\n"
569 "\tEndPrimitive();\n"
570 "}\n");
571 }
572#endif
573}
574
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000575bool GrGLProgram::genProgram(const GrGLInterface* gl,
576 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000577 GrGLProgram::CachedData* programData) const {
junov@google.comf93e7172011-03-31 21:26:24 +0000578
579 ShaderCodeSegments segments;
580 const uint32_t& layout = fProgramDesc.fVertexLayout;
581
bsalomon@google.com91961302011-05-09 18:39:58 +0000582 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000583
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000584#if GR_GL_EXPERIMENTAL_GS
585 segments.fUsesGS = fProgramDesc.fExperimentalGS;
586#endif
587
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000588 SkXfermode::Coeff colorCoeff, uniformCoeff;
589 // The rest of transfer mode color filters have not been implemented
590 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
591 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000592 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
593 (fProgramDesc.fColorFilterXfermode),
594 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000595 GR_DEBUGASSERT(success);
596 } else {
597 colorCoeff = SkXfermode::kOne_Coeff;
598 uniformCoeff = SkXfermode::kZero_Coeff;
599 }
600
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000601 // If we know the final color is going to be all zeros then we can
602 // simplify the color filter coeffecients. needComputedColor will then
603 // come out false below.
604 if (ProgramDesc::kTransBlack_ColorType == fProgramDesc.fColorType) {
605 colorCoeff = SkXfermode::kZero_Coeff;
606 if (SkXfermode::kDC_Coeff == uniformCoeff ||
607 SkXfermode::kDA_Coeff == uniformCoeff) {
608 uniformCoeff = SkXfermode::kZero_Coeff;
609 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
610 SkXfermode::kIDA_Coeff == uniformCoeff) {
611 uniformCoeff = SkXfermode::kOne_Coeff;
612 }
613 }
614
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000615 bool needColorFilterUniform;
616 bool needComputedColor;
617 needBlendInputs(uniformCoeff, colorCoeff,
618 &needColorFilterUniform, &needComputedColor);
619
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000620 // the dual source output has no canonical var name, have to
621 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000622 const char* fsColorOutput = NULL;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000623 bool dualSourceOutputWritten = false;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000624 segments.fHeader.printf(glsl_version_string(gl, glslVersion));
625 bool isColorDeclared = decl_and_get_fs_color_output(glslVersion,
626 &segments.fFSOutputs,
627 &fsColorOutput);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000628
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000629#if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000630 segments.fVSAttrs.push_back().set(GrGLShaderVar::kMat33f_Type, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000631 programData->fUniLocations.fViewMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +0000632#else
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000633 segments.fVSUnis.push_back().set(GrGLShaderVar::kMat33f_Type, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000634 programData->fUniLocations.fViewMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +0000635#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000636 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000637
bsalomon@google.com91961302011-05-09 18:39:58 +0000638 segments.fVSCode.append(
639 "void main() {\n"
640 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
641 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000642
bsalomon@google.com91961302011-05-09 18:39:58 +0000643 // incoming color to current stage being processed.
644 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000645
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000646 if (needComputedColor) {
647 switch (fProgramDesc.fColorType) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000648 case ProgramDesc::kAttribute_ColorType: {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000649 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
650 COL_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000651 const char *vsName, *fsName;
652 append_varying(GrGLShaderVar::kVec4f_Type, "Color", &segments, &vsName, &fsName);
653 segments.fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
654 inColor = fsName;
655 } break;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000656 case ProgramDesc::kUniform_ColorType:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000657 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
658 COL_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000659 programData->fUniLocations.fColorUni = kUseUniform;
660 inColor = COL_UNI_NAME;
661 break;
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000662 case ProgramDesc::kTransBlack_ColorType:
663 GrAssert(!"needComputedColor should be false.");
664 break;
665 case ProgramDesc::kSolidWhite_ColorType:
666 break;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000667 default:
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000668 GrCrash("Unknown color type.");
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000669 break;
670 }
junov@google.comf93e7172011-03-31 21:26:24 +0000671 }
672
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000673 // we output point size in the GS if present
674 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000675 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000676 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000677
bsalomon@google.com91961302011-05-09 18:39:58 +0000678 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000679
680 // add texture coordinates that are used to the list of vertex attr decls
bsalomon@google.com91961302011-05-09 18:39:58 +0000681 GrStringBuilder texCoordAttrs[GrDrawTarget::kMaxTexCoords];
junov@google.comf93e7172011-03-31 21:26:24 +0000682 for (int t = 0; t < GrDrawTarget::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000683 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000684 tex_attr_name(t, texCoordAttrs + t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000685 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type,
686 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000687 }
688 }
689
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000690 ///////////////////////////////////////////////////////////////////////////
691 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000692
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000693 // if we have color stages string them together, feeding the output color
694 // of each to the next and generating code for each stage.
695 if (needComputedColor) {
696 GrStringBuilder outColor;
697 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000698 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000699 // create var to hold stage result
700 outColor = "color";
701 outColor.appendS32(s);
702 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
703
704 const char* inCoords;
705 // figure out what our input coords are
706 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
707 layout) {
708 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000709 } else {
710 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
711 // we better have input tex coordinates if stage is enabled.
712 GrAssert(tcIdx >= 0);
713 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000714 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000715 }
716
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000717 genStageCode(gl,
718 s,
junov@google.comf93e7172011-03-31 21:26:24 +0000719 fProgramDesc.fStages[s],
bsalomon@google.comfc296292011-05-06 13:53:47 +0000720 inColor.size() ? inColor.c_str() : NULL,
721 outColor.c_str(),
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000722 inCoords,
junov@google.comf93e7172011-03-31 21:26:24 +0000723 &segments,
724 &programData->fUniLocations.fStages[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000725 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000726 }
727 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000728 }
Scroggo97c88c22011-05-11 14:05:25 +0000729
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000730 // if have all ones or zeros for the "dst" input to the color filter then we
731 // may be able to make additional optimizations.
732 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
733 GrAssert(ProgramDesc::kSolidWhite_ColorType == fProgramDesc.fColorType);
734 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
735 SkXfermode::kIDA_Coeff == uniformCoeff;
736 if (uniformCoeffIsZero) {
737 uniformCoeff = SkXfermode::kZero_Coeff;
738 bool bogus;
739 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
740 &needColorFilterUniform, &bogus);
741 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000742 }
743 if (needColorFilterUniform) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000744 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
745 COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000746 programData->fUniLocations.fColorFilterUni = kUseUniform;
747 }
748
749 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000750 if (SkXfermode::kZero_Coeff == uniformCoeff &&
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000751 SkXfermode::kZero_Coeff == colorCoeff) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000752 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000753 fsColorOutput,
754 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000755 wroteFragColorZero = true;
756 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
757 segments.fFSCode.appendf("\tvec4 filteredColor;\n");
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000758 const char* color;
759 if (inColor.size()) {
760 color = inColor.c_str();
761 } else {
762 if (ProgramDesc::kSolidWhite_ColorType == fProgramDesc.fColorType) {
763 color = all_ones_vec(4);
764 } else {
765 color = all_zeros_vec(4);
766 }
767 }
tomhudson@google.com0d831722011-06-02 15:37:14 +0000768 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000769 colorCoeff, color);
770 inColor = "filteredColor";
771 }
772
773 ///////////////////////////////////////////////////////////////////////////
774 // compute the partial coverage (coverage stages and edge aa)
775
776 GrStringBuilder inCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000777
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000778 // we don't need to compute coverage at all if we know the final shader
779 // output will be zero and we don't have a dual src blend output.
780 if (!wroteFragColorZero ||
781 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000782
783 // get edge AA coverage and use it as inCoverage to first coverage stage
784 this->genEdgeCoverage(gl, layout, programData, &inCoverage, &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000785
bsalomon@google.coma3108262011-10-10 14:08:47 +0000786 // include explicit per-vertex coverage if we have it
787 if (GrDrawTarget::kCoverage_VertexLayoutBit & layout) {
788 segments.fVSAttrs.push_back().set(GrGLShaderVar::kFloat_Type,
789 COV_ATTR_NAME);
790 const char *vsName, *fsName;
791 append_varying(GrGLShaderVar::kFloat_Type, "Coverage",
792 &segments, &vsName, &fsName);
793 segments.fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
794 if (inCoverage.size()) {
795 segments.fFSCode.appendf("\tfloat edgeAndAttrCov = %s * %s;\n",
796 fsName, inCoverage.c_str());
797 inCoverage = "edgeAndAttrCov";
798 } else {
799 inCoverage = fsName;
800 }
801 }
802
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000803 GrStringBuilder outCoverage;
804 const int& startStage = fProgramDesc.fFirstCoverageStage;
805 for (int s = startStage; s < GrDrawTarget::kNumStages; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000806 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000807 // create var to hold stage output
808 outCoverage = "coverage";
809 outCoverage.appendS32(s);
810 segments.fFSCode.appendf("\tvec4 %s;\n", outCoverage.c_str());
811
812 const char* inCoords;
813 // figure out what our input coords are
814 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & layout) {
815 inCoords = POS_ATTR_NAME;
816 } else {
817 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
818 // we better have input tex coordinates if stage is enabled.
819 GrAssert(tcIdx >= 0);
820 GrAssert(texCoordAttrs[tcIdx].size());
821 inCoords = texCoordAttrs[tcIdx].c_str();
822 }
823
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000824 genStageCode(gl, s,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000825 fProgramDesc.fStages[s],
826 inCoverage.size() ? inCoverage.c_str() : NULL,
827 outCoverage.c_str(),
828 inCoords,
829 &segments,
830 &programData->fUniLocations.fStages[s]);
831 inCoverage = outCoverage;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000832 }
833 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000834 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000835 segments.fFSOutputs.push_back().set(GrGLShaderVar::kVec4f_Type,
836 dual_source_output_name());
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000837 bool outputIsZero = false;
838 GrStringBuilder coeff;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000839 if (ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000840 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
841 if (!inColor.size()) {
842 outputIsZero = true;
843 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000844 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000845 ProgramDesc::kCoverageISA_DualSrcOutput) {
846 coeff.printf("(1 - %s.a)", inColor.c_str());
847 } else {
848 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
849 }
850 }
851 }
852 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000853 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000854 dual_source_output_name(),
855 all_zeros_vec(4));
856 } else {
857 modulate_helper(dual_source_output_name(),
858 coeff.c_str(),
859 inCoverage.c_str(),
860 &segments.fFSCode);
861 }
862 dualSourceOutputWritten = true;
863 }
junov@google.comf93e7172011-03-31 21:26:24 +0000864 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000865
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000866 ///////////////////////////////////////////////////////////////////////////
867 // combine color and coverage as frag color
868
869 if (!wroteFragColorZero) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000870 modulate_helper(fsColorOutput,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000871 inColor.c_str(),
872 inCoverage.c_str(),
873 &segments.fFSCode);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000874 }
875
bsalomon@google.com91961302011-05-09 18:39:58 +0000876 segments.fVSCode.append("}\n");
877 segments.fFSCode.append("}\n");
878
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000879 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000880 // insert GS
881#if GR_DEBUG
882 this->genGeometryShader(gl, glslVersion, &segments);
883#endif
884
885 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000886 // compile and setup attribs and unis
887
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000888 if (!CompileShaders(gl, glslVersion, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000889 return false;
890 }
891
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000892 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000893 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000894 dualSourceOutputWritten,
895 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000896 return false;
897 }
898
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000899 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000900
901 return true;
902}
903
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000904namespace {
905
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000906inline void expand_decls(const VarArray& vars,
907 const GrGLInterface* gl,
908 const char* prefix,
909 GrStringBuilder* string) {
910 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000911 for (int i = 0; i < count; ++i) {
912 string->append(prefix);
913 string->append(" ");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000914 vars[i].appendDecl(gl, string);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000915 string->append(";\n");
916 }
917}
918
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000919inline void print_shader(int stringCnt,
920 const char** strings,
921 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000922 for (int i = 0; i < stringCnt; ++i) {
923 if (NULL == stringLengths || stringLengths[i] < 0) {
924 GrPrintf(strings[i]);
925 } else {
926 GrPrintf("%.*s", stringLengths[i], strings[i]);
927 }
928 }
929}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000930
931typedef SkTArray<const char*, true> StrArray;
932#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
933
934typedef SkTArray<int, true> LengthArray;
935#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
936
937// these shouldn't relocate
938typedef GrTAllocator<GrStringBuilder> TempArray;
939#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
940
941inline void append_string(const GrStringBuilder& str,
942 StrArray* strings,
943 LengthArray* lengths) {
944 int length = (int) str.size();
945 if (length) {
946 strings->push_back(str.c_str());
947 lengths->push_back(length);
948 }
949 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000950}
951
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000952inline void append_decls(const VarArray& vars,
953 const GrGLInterface* gl,
954 const char* prefix,
955 StrArray* strings,
956 LengthArray* lengths,
957 TempArray* temp) {
958 expand_decls(vars, gl, prefix, &temp->push_back());
959 append_string(temp->back(), strings, lengths);
960}
961
962}
963
964bool GrGLProgram::CompileShaders(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000965 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000966 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000967 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000968 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +0000969
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000970 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
971 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
972 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
973
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000974 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000975 GrStringBuilder inputs;
976 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000977
978 static const char* gVaryingPrefixes[2][2] = {{"varying", "varying"},
979 {"out", "in"}};
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000980 const char** varyingPrefixes = k120_GLSLVersion == glslVersion ?
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000981 gVaryingPrefixes[0] :
982 gVaryingPrefixes[1];
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000983 const char* attributePrefix = k120_GLSLVersion == glslVersion ?
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000984 "attribute" :
985 "in";
junov@google.comf93e7172011-03-31 21:26:24 +0000986
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000987 append_string(segments.fHeader, &strs, &lengths);
988 append_decls(segments.fVSUnis, gl, "uniform", &strs, &lengths, &temps);
989 append_decls(segments.fVSAttrs, gl, attributePrefix, &strs, &lengths, &temps);
990 append_decls(segments.fVSOutputs, gl, varyingPrefixes[0], &strs, &lengths, &temps);
991 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +0000992
993#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000994 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000995 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000996#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000997
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000998 programData->fVShaderID =
999 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
1000 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001001
bsalomon@google.com91961302011-05-09 18:39:58 +00001002 if (!programData->fVShaderID) {
1003 return false;
1004 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001005 if (segments.fUsesGS) {
1006 strs.reset();
1007 lengths.reset();
1008 temps.reset();
1009 append_string(segments.fHeader, &strs, &lengths);
1010 append_string(segments.fGSHeader, &strs, &lengths);
1011 append_decls(segments.fGSInputs, gl, "in", &strs, &lengths, &temps);
1012 append_decls(segments.fGSOutputs, gl, "out", &strs, &lengths, &temps);
1013 append_string(segments.fGSCode, &strs, &lengths);
1014#if PRINT_SHADERS
1015 print_shader(strs.count(), &strs[0], &lengths[0]);
1016 GrPrintf("\n");
1017#endif
1018 programData->fGShaderID =
1019 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
1020 &strs[0], &lengths[0]);
1021 } else {
1022 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +00001023 }
junov@google.comf93e7172011-03-31 21:26:24 +00001024
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001025 strs.reset();
1026 lengths.reset();
1027 temps.reset();
1028
1029 append_string(segments.fHeader, &strs, &lengths);
1030 GrStringBuilder precisionStr(GrShaderPrecision(gl));
1031 append_string(precisionStr, &strs, &lengths);
1032 append_decls(segments.fFSUnis, gl, "uniform", &strs, &lengths, &temps);
1033 append_decls(segments.fFSInputs, gl, varyingPrefixes[1], &strs, &lengths, &temps);
1034 // We shouldn't have declared outputs on 1.2
1035 GrAssert(k120_GLSLVersion != glslVersion || segments.fFSOutputs.empty());
1036 append_decls(segments.fFSOutputs, gl, "out", &strs, &lengths, &temps);
1037 append_string(segments.fFSFunctions, &strs, &lengths);
1038 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001039
1040#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001041 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001042 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001043#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001044
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001045 programData->fFShaderID =
1046 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
1047 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001048
bsalomon@google.com91961302011-05-09 18:39:58 +00001049 if (!programData->fFShaderID) {
1050 return false;
junov@google.comf93e7172011-03-31 21:26:24 +00001051 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001052
bsalomon@google.com91961302011-05-09 18:39:58 +00001053 return true;
junov@google.comf93e7172011-03-31 21:26:24 +00001054}
1055
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001056GrGLuint GrGLProgram::CompileShader(const GrGLInterface* gl,
1057 GrGLenum type,
1058 int stringCnt,
1059 const char** strings,
1060 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001061 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
1062 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
1063
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001064 GrGLuint shader;
1065 GR_GL_CALL_RET(gl, shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +00001066 if (0 == shader) {
1067 return 0;
1068 }
1069
1070 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001071 GR_GL_CALL(gl, ShaderSource(shader, stringCnt, strings, stringLengths));
1072 GR_GL_CALL(gl, CompileShader(shader));
1073 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +00001074
1075 if (!compiled) {
1076 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001077 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001078 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001079 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001080 // retrieve length even though we don't need it to workaround
1081 // bug in chrome cmd buffer param validation.
1082 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001083 GR_GL_CALL(gl, GetShaderInfoLog(shader, infoLen+1,
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001084 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001085 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001086 GrPrintf("\n%s", log.get());
1087 }
1088 GrAssert(!"Shader compilation failed!");
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001089 GR_GL_CALL(gl, DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001090 return 0;
1091 }
1092 return shader;
1093}
1094
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001095bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001096 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001097 GrStringBuilder texCoordAttrNames[],
1098 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001099 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001100 CachedData* programData) const {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001101 GR_GL_CALL_RET(gl, programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001102 if (!programData->fProgramID) {
1103 return false;
1104 }
1105 const GrGLint& progID = programData->fProgramID;
1106
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001107 GR_GL_CALL(gl, AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001108 if (programData->fGShaderID) {
1109 GR_GL_CALL(gl, AttachShader(progID, programData->fGShaderID));
1110 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001111 GR_GL_CALL(gl, AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001112
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001113 if (bindColorOut) {
bsalomon@google.combc5cf512011-09-21 16:21:07 +00001114 GR_GL_CALL(gl, BindFragDataLocation(programData->fProgramID,
1115 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001116 }
1117 if (bindDualSrcOut) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001118 GR_GL_CALL(gl, BindFragDataLocationIndexed(programData->fProgramID,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001119 0, 1, dual_source_output_name()));
1120 }
1121
bsalomon@google.com91961302011-05-09 18:39:58 +00001122 // Bind the attrib locations to same values for all shaders
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001123 GR_GL_CALL(gl, BindAttribLocation(progID, PositionAttributeIdx(),
1124 POS_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001125 for (int t = 0; t < GrDrawTarget::kMaxTexCoords; ++t) {
1126 if (texCoordAttrNames[t].size()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GR_GL_CALL(gl, BindAttribLocation(progID,
1128 TexCoordAttributeIdx(t),
1129 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001130 }
1131 }
1132
bsalomon@google.com91961302011-05-09 18:39:58 +00001133 if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001134 GR_GL_CALL(gl, BindAttribLocation(progID,
1135 ViewMatrixAttributeIdx(),
1136 VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001137 }
1138
1139 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
bsalomon@google.com72b4fcb2011-05-09 20:47:34 +00001140 const StageUniLocations& unis = programData->fUniLocations.fStages[s];
1141 if (kSetAsAttribute == unis.fTextureMatrixUni) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001142 GrStringBuilder matName;
1143 tex_matrix_name(s, &matName);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001144 GR_GL_CALL(gl, BindAttribLocation(progID,
1145 TextureMatrixAttributeIdx(s),
1146 matName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001147 }
1148 }
1149
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001150 GR_GL_CALL(gl, BindAttribLocation(progID, ColorAttributeIdx(),
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001151 COL_ATTR_NAME));
bsalomon@google.coma3108262011-10-10 14:08:47 +00001152 GR_GL_CALL(gl, BindAttribLocation(progID, CoverageAttributeIdx(),
1153 COV_ATTR_NAME));
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001154 GR_GL_CALL(gl, BindAttribLocation(progID, EdgeAttributeIdx(),
1155 EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001156
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GR_GL_CALL(gl, LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001158
1159 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001160 GR_GL_CALL(gl, GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001161 if (!linked) {
1162 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001163 GR_GL_CALL(gl, GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001164 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001165 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001166 // retrieve length even though we don't need it to workaround
1167 // bug in chrome cmd buffer param validation.
1168 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001169 GR_GL_CALL(gl, GetProgramInfoLog(progID, infoLen+1,
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001170 &length, (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001171 GrPrintf((char*)log.get());
1172 }
1173 GrAssert(!"Error linking program");
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001174 GR_GL_CALL(gl, DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001175 programData->fProgramID = 0;
1176 return false;
1177 }
1178 return true;
1179}
1180
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001181void GrGLProgram::getUniformLocationsAndInitCache(const GrGLInterface* gl,
1182 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001183 const GrGLint& progID = programData->fProgramID;
1184
1185 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001186 GR_GL_CALL_RET(gl, programData->fUniLocations.fViewMatrixUni,
1187 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001188 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1189 }
1190 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001191 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorUni,
1192 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001193 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1194 }
Scroggo97c88c22011-05-11 14:05:25 +00001195 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001196 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorFilterUni,
1197 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001198 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1199 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001200
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001201 if (kUseUniform == programData->fUniLocations.fEdgesUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001202 GR_GL_CALL_RET(gl, programData->fUniLocations.fEdgesUni,
1203 GetUniformLocation(progID, EDGES_UNI_NAME));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001204 GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni);
1205 } else {
1206 programData->fUniLocations.fEdgesUni = kUnusedUniform;
1207 }
1208
bsalomon@google.com91961302011-05-09 18:39:58 +00001209 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
1210 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001211 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001212 if (kUseUniform == locations.fTextureMatrixUni) {
1213 GrStringBuilder texMName;
1214 tex_matrix_name(s, &texMName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001215 GR_GL_CALL_RET(gl, locations.fTextureMatrixUni,
1216 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001217 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1218 }
1219
1220 if (kUseUniform == locations.fSamplerUni) {
1221 GrStringBuilder samplerName;
1222 sampler_name(s, &samplerName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001223 GR_GL_CALL_RET(gl, locations.fSamplerUni,
1224 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001225 GrAssert(kUnusedUniform != locations.fSamplerUni);
1226 }
1227
1228 if (kUseUniform == locations.fNormalizedTexelSizeUni) {
1229 GrStringBuilder texelSizeName;
1230 normalized_texel_size_name(s, &texelSizeName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001231 GR_GL_CALL_RET(gl, locations.fNormalizedTexelSizeUni,
1232 GetUniformLocation(progID, texelSizeName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001233 GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni);
1234 }
1235
1236 if (kUseUniform == locations.fRadial2Uni) {
1237 GrStringBuilder radial2ParamName;
1238 radial2_param_name(s, &radial2ParamName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001239 GR_GL_CALL_RET(gl, locations.fRadial2Uni,
1240 GetUniformLocation(progID, radial2ParamName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001241 GrAssert(kUnusedUniform != locations.fRadial2Uni);
1242 }
junov@google.com6acc9b32011-05-16 18:32:07 +00001243
1244 if (kUseUniform == locations.fTexDomUni) {
1245 GrStringBuilder texDomName;
1246 tex_domain_name(s, &texDomName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001247 GR_GL_CALL_RET(gl, locations.fTexDomUni,
1248 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001249 GrAssert(kUnusedUniform != locations.fTexDomUni);
1250 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001251
1252 GrStringBuilder kernelName, imageIncrementName;
1253 convolve_param_names(s, &kernelName, &imageIncrementName);
1254 if (kUseUniform == locations.fKernelUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001255 GR_GL_CALL_RET(gl, locations.fKernelUni,
1256 GetUniformLocation(progID, kernelName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001257 GrAssert(kUnusedUniform != locations.fKernelUni);
1258 }
1259
1260 if (kUseUniform == locations.fImageIncrementUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001261 GR_GL_CALL_RET(gl, locations.fImageIncrementUni,
1262 GetUniformLocation(progID,
1263 imageIncrementName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001264 GrAssert(kUnusedUniform != locations.fImageIncrementUni);
1265 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001266 }
1267 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001268 GR_GL_CALL(gl, UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001269
1270 // init sampler unis and set bogus values for state tracking
1271 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
1272 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001273 GR_GL_CALL(gl, Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001274 }
1275 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
1276 programData->fRadial2CenterX1[s] = GR_ScalarMax;
1277 programData->fRadial2Radius0[s] = -GR_ScalarMax;
1278 programData->fTextureWidth[s] = -1;
1279 programData->fTextureHeight[s] = -1;
1280 }
1281 programData->fViewMatrix = GrMatrix::InvalidMatrix();
1282 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001283 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001284}
1285
junov@google.comf93e7172011-03-31 21:26:24 +00001286//============================================================================
1287// Stage code generation
1288//============================================================================
1289
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001290void GrGLProgram::genStageCode(const GrGLInterface* gl,
1291 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001292 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001293 const char* fsInColor, // NULL means no incoming color
1294 const char* fsOutColor,
1295 const char* vsInCoord,
1296 ShaderCodeSegments* segments,
1297 StageUniLocations* locations) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001298
1299 GrAssert(stageNum >= 0 && stageNum <= 9);
1300
junov@google.comf93e7172011-03-31 21:26:24 +00001301 // First decide how many coords are needed to access the texture
1302 // Right now it's always 2 but we could start using 1D textures for
1303 // gradients.
1304 static const int coordDims = 2;
1305 int varyingDims;
1306 /// Vertex Shader Stuff
1307
1308 // decide whether we need a matrix to transform texture coords
1309 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001310 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001311 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001312 varyingDims = coordDims;
1313 } else {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001314 GrGLShaderVar* mat;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001315 #if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001316 mat = &segments->fVSAttrs.push_back();
bsalomon@google.com91961302011-05-09 18:39:58 +00001317 locations->fTextureMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +00001318 #else
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001319 mat = &segments->fVSUnis.push_back();
bsalomon@google.com91961302011-05-09 18:39:58 +00001320 locations->fTextureMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001321 #endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001322 tex_matrix_name(stageNum, mat->accessName());
1323 mat->setType(GrGLShaderVar::kMat33f_Type);
1324 matName = mat->getName().c_str();
1325
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001326 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001327 varyingDims = coordDims;
1328 } else {
1329 varyingDims = coordDims + 1;
1330 }
1331 }
1332
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001333 segments->fFSUnis.push_back().setType(GrGLShaderVar::kSampler2D_Type);
1334 sampler_name(stageNum, segments->fFSUnis.back().accessName());
bsalomon@google.com91961302011-05-09 18:39:58 +00001335 locations->fSamplerUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001336 const char* samplerName = segments->fFSUnis.back().getName().c_str();
junov@google.comf93e7172011-03-31 21:26:24 +00001337
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001338 const char* texelSizeName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001339 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001340 segments->fFSUnis.push_back().setType(GrGLShaderVar::kVec2f_Type);
1341 normalized_texel_size_name(stageNum, segments->fFSUnis.back().accessName());
1342 texelSizeName = segments->fFSUnis.back().getName().c_str();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001343 }
1344
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001345 const char *varyingVSName, *varyingFSName;
1346 append_varying(float_vector_type(varyingDims),
1347 "Stage",
1348 stageNum,
1349 segments,
1350 &varyingVSName,
1351 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001352
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001353 if (!matName) {
junov@google.comf93e7172011-03-31 21:26:24 +00001354 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001355 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001356 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001357 // varying = texMatrix * texCoord
1358 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001359 varyingVSName, matName, vsInCoord,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001360 vector_all_coords(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001361 }
1362
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001363 const char* radial2ParamsName = NULL;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001364 const char *radial2VaryingVSName = NULL;
1365 const char *radial2VaryingFSName = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001366
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001367 if (StageDesc::kRadial2Gradient_CoordMapping == desc.fCoordMapping ||
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001368 StageDesc::kRadial2GradientDegenerate_CoordMapping == desc.fCoordMapping) {
junov@google.comf93e7172011-03-31 21:26:24 +00001369
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001370 GrGLShaderVar* radial2FSParams = &segments->fFSUnis.push_back();
1371 radial2FSParams->setType(GrGLShaderVar::kFloat_Type);
1372 radial2FSParams->setArrayCount(6);
1373 radial2_param_name(stageNum, radial2FSParams->accessName());
1374 segments->fVSUnis.push_back(*radial2FSParams).setEmitPrecision(true);
1375 radial2ParamsName = radial2FSParams->getName().c_str();
1376
bsalomon@google.com91961302011-05-09 18:39:58 +00001377 locations->fRadial2Uni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001378
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001379 // for radial grads without perspective we can pass the linear
1380 // part of the quadratic as a varying.
junov@google.comf93e7172011-03-31 21:26:24 +00001381 if (varyingDims == coordDims) {
1382 GrAssert(2 == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001383 append_varying(GrGLShaderVar::kFloat_Type,
1384 "Radial2BCoeff",
1385 stageNum,
1386 segments,
1387 &radial2VaryingVSName,
1388 &radial2VaryingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001389
bsalomon@google.com91961302011-05-09 18:39:58 +00001390 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001391 const char* r2ParamName = radial2FSParams->getName().c_str();
bsalomon@google.com91961302011-05-09 18:39:58 +00001392 segments->fVSCode.appendf("\t%s = 2.0 *(%s[2] * %s.x - %s[3]);\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001393 radial2VaryingVSName, r2ParamName,
1394 varyingVSName, r2ParamName);
junov@google.comf93e7172011-03-31 21:26:24 +00001395 }
1396 }
1397
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001398 const char* kernelName = NULL;
1399 const char* imageIncrementName = NULL;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001400 if (ProgramDesc::StageDesc::kConvolution_FetchMode == desc.fFetchMode) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001401
1402 GrGLShaderVar* kernel = &segments->fFSUnis.push_back();
1403 kernel->setType(GrGLShaderVar::kFloat_Type);
1404 kernel->setArrayCount(desc.fKernelWidth);
1405 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
1406 imgInc->setType(GrGLShaderVar::kVec2f_Type);
1407
1408 convolve_param_names(stageNum,
1409 kernel->accessName(),
1410 imgInc->accessName());
1411 kernelName = kernel->getName().c_str();
1412 imageIncrementName = imgInc->getName().c_str();
1413
1414 // need image increment in both VS and FS
1415 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1416
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001417 locations->fKernelUni = kUseUniform;
1418 locations->fImageIncrementUni = kUseUniform;
1419 float scale = (desc.fKernelWidth - 1) * 0.5f;
1420 segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001421 varyingVSName, scale, scale,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001422 imageIncrementName);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001423 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001424
junov@google.comf93e7172011-03-31 21:26:24 +00001425 /// Fragment Shader Stuff
bsalomon@google.com91961302011-05-09 18:39:58 +00001426 GrStringBuilder fsCoordName;
junov@google.comf93e7172011-03-31 21:26:24 +00001427 // function used to access the shader, may be made projective
bsalomon@google.com91961302011-05-09 18:39:58 +00001428 GrStringBuilder texFunc("texture2D");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001429 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1430 StageDesc::kNoPerspective_OptFlagBit)) {
junov@google.comf93e7172011-03-31 21:26:24 +00001431 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001432 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001433 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001434 // if we have to do some special op on the varyings to get
junov@google.comf93e7172011-03-31 21:26:24 +00001435 // our final tex coords then when in perspective we have to
bsalomon@google.com91961302011-05-09 18:39:58 +00001436 // do an explicit divide. Otherwise, we can use a Proj func.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001437 if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
1438 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001439 texFunc.append("Proj");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001440 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001441 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001442 fsCoordName = "inCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +00001443 fsCoordName.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001444 segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001445 GrGLShaderVar::TypeString(float_vector_type(coordDims)),
1446 fsCoordName.c_str(),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001447 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001448 vector_nonhomog_coords(varyingDims),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001449 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001450 vector_homog_coord(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001451 }
1452 }
1453
bsalomon@google.comfc296292011-05-06 13:53:47 +00001454 GrStringBuilder sampleCoords;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001455 bool complexCoord = false;
junov@google.comf93e7172011-03-31 21:26:24 +00001456 switch (desc.fCoordMapping) {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001457 case StageDesc::kIdentity_CoordMapping:
junov@google.comf93e7172011-03-31 21:26:24 +00001458 sampleCoords = fsCoordName;
1459 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001460 case StageDesc::kSweepGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001461 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 +00001462 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001463 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001464 case StageDesc::kRadialGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001465 sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001466 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001467 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001468 case StageDesc::kRadial2Gradient_CoordMapping: {
bsalomon@google.com91961302011-05-09 18:39:58 +00001469 GrStringBuilder cName("c");
1470 GrStringBuilder ac4Name("ac4");
1471 GrStringBuilder rootName("root");
junov@google.comf93e7172011-03-31 21:26:24 +00001472
bsalomon@google.comfc296292011-05-06 13:53:47 +00001473 cName.appendS32(stageNum);
1474 ac4Name.appendS32(stageNum);
1475 rootName.appendS32(stageNum);
junov@google.comf93e7172011-03-31 21:26:24 +00001476
bsalomon@google.com91961302011-05-09 18:39:58 +00001477 // if we were able to interpolate the linear component bVar is the varying
1478 // otherwise compute it
1479 GrStringBuilder bVar;
junov@google.comf93e7172011-03-31 21:26:24 +00001480 if (coordDims == varyingDims) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001481 bVar = radial2VaryingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001482 GrAssert(2 == varyingDims);
1483 } else {
1484 GrAssert(3 == varyingDims);
1485 bVar = "b";
bsalomon@google.comfc296292011-05-06 13:53:47 +00001486 bVar.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001487 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s[2] * %s.x - %s[3]);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001488 bVar.c_str(), radial2ParamsName,
1489 fsCoordName.c_str(), radial2ParamsName);
junov@google.comf93e7172011-03-31 21:26:24 +00001490 }
1491
bsalomon@google.com91961302011-05-09 18:39:58 +00001492 // c = (x^2)+(y^2) - params[4]
1493 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s[4];\n",
1494 cName.c_str(), fsCoordName.c_str(),
1495 fsCoordName.c_str(),
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001496 radial2ParamsName);
bsalomon@google.com91961302011-05-09 18:39:58 +00001497 // ac4 = 4.0 * params[0] * c
1498 segments->fFSCode.appendf("\tfloat %s = %s[0] * 4.0 * %s;\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001499 ac4Name.c_str(), radial2ParamsName,
bsalomon@google.com91961302011-05-09 18:39:58 +00001500 cName.c_str());
junov@google.comf93e7172011-03-31 21:26:24 +00001501
bsalomon@google.com91961302011-05-09 18:39:58 +00001502 // root = sqrt(b^2-4ac)
1503 // (abs to avoid exception due to fp precision)
1504 segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
1505 rootName.c_str(), bVar.c_str(), bVar.c_str(),
1506 ac4Name.c_str());
junov@google.comf93e7172011-03-31 21:26:24 +00001507
bsalomon@google.com91961302011-05-09 18:39:58 +00001508 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
1509 // y coord is 0.5 (texture is effectively 1D)
1510 sampleCoords.printf("vec2((-%s + %s[5] * %s) * %s[1], 0.5)",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001511 bVar.c_str(), radial2ParamsName,
1512 rootName.c_str(), radial2ParamsName);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001513 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001514 break;}
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001515 case StageDesc::kRadial2GradientDegenerate_CoordMapping: {
1516 GrStringBuilder cName("c");
1517
1518 cName.appendS32(stageNum);
1519
1520 // if we were able to interpolate the linear component bVar is the varying
1521 // otherwise compute it
1522 GrStringBuilder bVar;
1523 if (coordDims == varyingDims) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001524 bVar = radial2VaryingFSName;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001525 GrAssert(2 == varyingDims);
1526 } else {
1527 GrAssert(3 == varyingDims);
1528 bVar = "b";
1529 bVar.appendS32(stageNum);
1530 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s[2] * %s.x - %s[3]);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001531 bVar.c_str(), radial2ParamsName,
1532 fsCoordName.c_str(), radial2ParamsName);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001533 }
1534
1535 // c = (x^2)+(y^2) - params[4]
1536 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s[4];\n",
1537 cName.c_str(), fsCoordName.c_str(),
1538 fsCoordName.c_str(),
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001539 radial2ParamsName);
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001540
1541 // x coord is: -c/b
1542 // y coord is 0.5 (texture is effectively 1D)
1543 sampleCoords.printf("vec2((-%s / %s), 0.5)", cName.c_str(), bVar.c_str());
1544 complexCoord = true;
1545 break;}
junov@google.comf93e7172011-03-31 21:26:24 +00001546 };
1547
bsalomon@google.com91961302011-05-09 18:39:58 +00001548 const char* smear;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001549 if (desc.fModulation == StageDesc::kAlpha_Modulation) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001550 smear = ".aaaa";
1551 } else {
1552 smear = "";
1553 }
1554 GrStringBuilder modulate;
1555 if (NULL != fsInColor) {
1556 modulate.printf(" * %s", fsInColor);
1557 }
1558
tomhudson@google.com0d831722011-06-02 15:37:14 +00001559 if (desc.fOptFlags &
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001560 StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001561 GrStringBuilder texDomainName;
1562 tex_domain_name(stageNum, &texDomainName);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001563 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type, texDomainName);
junov@google.com6acc9b32011-05-16 18:32:07 +00001564 GrStringBuilder coordVar("clampCoord");
1565 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001566 float_vector_type_str(coordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001567 coordVar.c_str(),
1568 sampleCoords.c_str(),
1569 texDomainName.c_str(),
1570 texDomainName.c_str());
1571 sampleCoords = coordVar;
1572 locations->fTexDomUni = kUseUniform;
1573 }
1574
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001575 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001576 locations->fNormalizedTexelSizeUni = kUseUniform;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001577 if (complexCoord) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001578 // assign the coord to a var rather than compute 4x.
1579 GrStringBuilder coordVar("tCoord");
bsalomon@google.comfc296292011-05-06 13:53:47 +00001580 coordVar.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001581 segments->fFSCode.appendf("\t%s %s = %s;\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001582 float_vector_type_str(coordDims),
1583 coordVar.c_str(), sampleCoords.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001584 sampleCoords = coordVar;
1585 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001586 GrAssert(2 == coordDims);
bsalomon@google.com91961302011-05-09 18:39:58 +00001587 GrStringBuilder accumVar("accum");
1588 accumVar.appendS32(stageNum);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001589 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, smear);
1590 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, smear);
1591 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, smear);
1592 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, smear);
bsalomon@google.com91961302011-05-09 18:39:58 +00001593 segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001594 } else if (ProgramDesc::StageDesc::kConvolution_FetchMode == desc.fFetchMode) {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001595 GrStringBuilder sumVar("sum");
1596 sumVar.appendS32(stageNum);
1597 GrStringBuilder coordVar("coord");
1598 coordVar.appendS32(stageNum);
1599
1600 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1601 sumVar.c_str());
1602 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1603 coordVar.c_str(),
1604 sampleCoords.c_str());
1605 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1606 desc.fKernelWidth);
1607 segments->fFSCode.appendf("\t\t%s += %s(%s, %s)%s * %s[i];\n",
1608 sumVar.c_str(), texFunc.c_str(),
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001609 samplerName, coordVar.c_str(), smear,
1610 kernelName);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001611 segments->fFSCode.appendf("\t\t%s += %s;\n",
1612 coordVar.c_str(),
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001613 imageIncrementName);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001614 segments->fFSCode.appendf("\t}\n");
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001615 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1616 sumVar.c_str(), modulate.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001617 } else {
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001618 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n",
1619 fsOutColor, texFunc.c_str(),
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001620 samplerName, sampleCoords.c_str(),
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001621 smear, modulate.c_str());
junov@google.comf93e7172011-03-31 21:26:24 +00001622 }
junov@google.comf93e7172011-03-31 21:26:24 +00001623}