blob: e190c917bf29f81116633616a637cda95171fc4e [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"
bsalomon@google.com4fa66942011-09-20 19:06:12 +000013#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
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +000019enum {
20 /// Used to mark a StageUniLocation field that should be bound
21 /// to a uniform during getUniformLocationsAndInitCache().
22 kUseUniform = 2000
23};
24
junov@google.comf93e7172011-03-31 21:26:24 +000025} // namespace
26
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000027#define PRINT_SHADERS 0
28
bsalomon@google.com4fa66942011-09-20 19:06:12 +000029typedef GrTAllocator<GrGLShaderVar> VarArray;
30
31// number of each input/output type in a single allocation block
32static const int gVarsPerBlock = 8;
33// except FS outputs where we expect 2 at most.
34static const int gMaxFSOutputs = 2;
35
36struct ShaderCodeSegments {
37 ShaderCodeSegments()
38 : fVSUnis(gVarsPerBlock)
39 , fVSAttrs(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000040 , fVSOutputs(gVarsPerBlock)
41 , fGSInputs(gVarsPerBlock)
42 , fGSOutputs(gVarsPerBlock)
43 , fFSInputs(gVarsPerBlock)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000044 , fFSUnis(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000045 , fFSOutputs(gMaxFSOutputs)
46 , fUsesGS(false) {}
bsalomon@google.com4fa66942011-09-20 19:06:12 +000047 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
48 VarArray fVSUnis;
49 VarArray fVSAttrs;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000050 VarArray fVSOutputs;
51 VarArray fGSInputs;
52 VarArray fGSOutputs;
53 VarArray fFSInputs;
54 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
bsalomon@google.com4fa66942011-09-20 19:06:12 +000055 VarArray fFSUnis;
56 VarArray fFSOutputs;
57 GrStringBuilder fFSFunctions;
58 GrStringBuilder fVSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000059 GrStringBuilder fGSCode;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000060 GrStringBuilder fFSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000061
62 bool fUsesGS;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000063};
64
bsalomon@google.com85b505b2011-11-07 14:56:51 +000065typedef GrGLProgram::ProgramDesc::StageDesc StageDesc;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000066
bsalomon@google.com4be283f2011-04-19 21:15:09 +000067#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +000068 #define VIEW_MATRIX_NAME "aViewM"
69#else
70 #define VIEW_MATRIX_NAME "uViewM"
71#endif
72
73#define POS_ATTR_NAME "aPosition"
74#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000075#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000076#define EDGE_ATTR_NAME "aEdge"
bsalomon@google.com4be283f2011-04-19 21:15:09 +000077#define COL_UNI_NAME "uColor"
bsalomon@google.com2401ae82012-01-17 21:03:05 +000078#define COV_UNI_NAME "uCoverage"
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000079#define EDGES_UNI_NAME "uEdges"
Scroggo97c88c22011-05-11 14:05:25 +000080#define COL_FILTER_UNI_NAME "uColorFilter"
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +000081#define COL_MATRIX_UNI_NAME "uColorMatrix"
82#define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec"
junov@google.comf93e7172011-03-31 21:26:24 +000083
bsalomon@google.com4fa66942011-09-20 19:06:12 +000084namespace {
85inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000086 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +000087 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +000088}
89
bsalomon@google.com4fa66942011-09-20 19:06:12 +000090inline GrGLShaderVar::Type float_vector_type(int count) {
91 GR_STATIC_ASSERT(GrGLShaderVar::kFloat_Type == 0);
92 GR_STATIC_ASSERT(GrGLShaderVar::kVec2f_Type == 1);
93 GR_STATIC_ASSERT(GrGLShaderVar::kVec3f_Type == 2);
94 GR_STATIC_ASSERT(GrGLShaderVar::kVec4f_Type == 3);
95 GrAssert(count > 0 && count <= 4);
96 return (GrGLShaderVar::Type)(count - 1);
junov@google.comf93e7172011-03-31 21:26:24 +000097}
98
bsalomon@google.com4fa66942011-09-20 19:06:12 +000099inline const char* float_vector_type_str(int count) {
100 return GrGLShaderVar::TypeString(float_vector_type(count));
101}
102
103inline const char* vector_homog_coord(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000104 static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"};
105 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS));
106 return HOMOGS[count];
107}
108
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000109inline const char* vector_nonhomog_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000110 static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"};
111 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS));
112 return NONHOMOGS[count];
113}
114
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000115inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000116 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
117 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
118 return ALL[count];
119}
120
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000121inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000122 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000123 "vec3(1,1,1)", "vec4(1,1,1,1)"};
124 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
125 return ONESVEC[count];
126}
127
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000128inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000129 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000130 "vec3(0,0,0)", "vec4(0,0,0,0)"};
131 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
132 return ZEROSVEC[count];
133}
134
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000135inline const char* declared_color_output_name() { return "fsColorOut"; }
136inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000137
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000138inline void tex_matrix_name(int stage, GrStringBuilder* s) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000139#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +0000140 *s = "aTexM";
141#else
142 *s = "uTexM";
143#endif
bsalomon@google.comfc296292011-05-06 13:53:47 +0000144 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000145}
146
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000147inline void normalized_texel_size_name(int stage, GrStringBuilder* s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000148 *s = "uTexelSize";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000149 s->appendS32(stage);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000150}
151
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000152inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000153 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000154 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000155}
156
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000157inline void radial2_param_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000158 *s = "uRadial2Params";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000159 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000160}
161
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000162inline void convolve_param_names(int stage, GrStringBuilder* k, GrStringBuilder* i) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000163 *k = "uKernel";
164 k->appendS32(stage);
165 *i = "uImageIncrement";
166 i->appendS32(stage);
167}
168
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000169inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000170 *s = "uTexDom";
171 s->appendS32(stage);
172}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000173}
junov@google.com6acc9b32011-05-16 18:32:07 +0000174
junov@google.comf93e7172011-03-31 21:26:24 +0000175GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000176}
177
178GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000179}
180
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000181void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000182 GrBlendCoeff* dstCoeff) const {
183 switch (fProgramDesc.fDualSrcOutput) {
184 case ProgramDesc::kNone_DualSrcOutput:
185 break;
186 // the prog will write a coverage value to the secondary
187 // output and the dst is blended by one minus that value.
188 case ProgramDesc::kCoverage_DualSrcOutput:
189 case ProgramDesc::kCoverageISA_DualSrcOutput:
190 case ProgramDesc::kCoverageISC_DualSrcOutput:
191 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff;
192 break;
193 default:
194 GrCrash("Unexpected dual source blend output");
195 break;
196 }
197}
198
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000199// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000200// vars can be vec4s or floats (or one of each)
201// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000202// if either var is "" then assign to the other var
203// if both are "" then assign all ones
204static inline void modulate_helper(const char* outputVar,
205 const char* var0,
206 const char* var1,
207 GrStringBuilder* code) {
208 GrAssert(NULL != outputVar);
209 GrAssert(NULL != var0);
210 GrAssert(NULL != var1);
211 GrAssert(NULL != code);
212
213 bool has0 = '\0' != *var0;
214 bool has1 = '\0' != *var1;
215
216 if (!has0 && !has1) {
217 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
218 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000219 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000220 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000221 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000222 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000223 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000224 }
225}
226
227// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000228// vars can be vec4s or floats (or one of each)
229// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000230// if either var is "" then assign to the other var
231// if both are "" then assign all zeros
232static inline void add_helper(const char* outputVar,
233 const char* var0,
234 const char* var1,
235 GrStringBuilder* code) {
236 GrAssert(NULL != outputVar);
237 GrAssert(NULL != var0);
238 GrAssert(NULL != var1);
239 GrAssert(NULL != code);
240
241 bool has0 = '\0' != *var0;
242 bool has1 = '\0' != *var1;
243
244 if (!has0 && !has1) {
245 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
246 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000247 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000248 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000249 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000250 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000251 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000252 }
253}
254
255// given two blend coeffecients determine whether the src
256// and/or dst computation can be omitted.
257static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
258 SkXfermode::Coeff dstCoeff,
259 bool* needSrcValue,
260 bool* needDstValue) {
261 if (SkXfermode::kZero_Coeff == srcCoeff) {
262 switch (dstCoeff) {
263 // these all read the src
264 case SkXfermode::kSC_Coeff:
265 case SkXfermode::kISC_Coeff:
266 case SkXfermode::kSA_Coeff:
267 case SkXfermode::kISA_Coeff:
268 *needSrcValue = true;
269 break;
270 default:
271 *needSrcValue = false;
272 break;
273 }
274 } else {
275 *needSrcValue = true;
276 }
277 if (SkXfermode::kZero_Coeff == dstCoeff) {
278 switch (srcCoeff) {
279 // these all read the dst
280 case SkXfermode::kDC_Coeff:
281 case SkXfermode::kIDC_Coeff:
282 case SkXfermode::kDA_Coeff:
283 case SkXfermode::kIDA_Coeff:
284 *needDstValue = true;
285 break;
286 default:
287 *needDstValue = false;
288 break;
289 }
290 } else {
291 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000292 }
293}
294
295/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000296 * Create a blend_coeff * value string to be used in shader code. Sets empty
297 * string if result is trivially zero.
298 */
299static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
300 const char* src, const char* dst,
301 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000302 switch (coeff) {
303 case SkXfermode::kZero_Coeff: /** 0 */
304 *str = "";
305 break;
306 case SkXfermode::kOne_Coeff: /** 1 */
307 *str = value;
308 break;
309 case SkXfermode::kSC_Coeff:
310 str->printf("(%s * %s)", src, value);
311 break;
312 case SkXfermode::kISC_Coeff:
313 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
314 break;
315 case SkXfermode::kDC_Coeff:
316 str->printf("(%s * %s)", dst, value);
317 break;
318 case SkXfermode::kIDC_Coeff:
319 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
320 break;
321 case SkXfermode::kSA_Coeff: /** src alpha */
322 str->printf("(%s.a * %s)", src, value);
323 break;
324 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
325 str->printf("((1.0 - %s.a) * %s)", src, value);
326 break;
327 case SkXfermode::kDA_Coeff: /** dst alpha */
328 str->printf("(%s.a * %s)", dst, value);
329 break;
330 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
331 str->printf("((1.0 - %s.a) * %s)", dst, value);
332 break;
333 default:
334 GrCrash("Unexpected xfer coeff.");
335 break;
336 }
337}
338/**
Scroggo97c88c22011-05-11 14:05:25 +0000339 * Adds a line to the fragment shader code which modifies the color by
340 * the specified color filter.
341 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000342static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000343 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000344 SkXfermode::Coeff colorCoeff,
345 const char* inColor) {
346 GrStringBuilder colorStr, constStr;
347 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
348 inColor, inColor);
349 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
350 inColor, COL_FILTER_UNI_NAME);
351
352 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000353}
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000354/**
355 * Adds code to the fragment shader code which modifies the color by
356 * the specified color matrix.
357 */
358static void addColorMatrix(GrStringBuilder* fsCode, const char * outputVar,
359 const char* inColor) {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000360 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);
361 fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000362}
Scroggo97c88c22011-05-11 14:05:25 +0000363
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000364namespace {
365
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000366// Adds a var that is computed in the VS and read in FS.
367// If there is a GS it will just pass it through.
368void append_varying(GrGLShaderVar::Type type,
369 const char* name,
370 ShaderCodeSegments* segments,
371 const char** vsOutName = NULL,
372 const char** fsInName = NULL) {
373 segments->fVSOutputs.push_back();
374 segments->fVSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000375 segments->fVSOutputs.back().setTypeModifier(
376 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000377 segments->fVSOutputs.back().accessName()->printf("v%s", name);
378 if (vsOutName) {
379 *vsOutName = segments->fVSOutputs.back().getName().c_str();
380 }
381 // input to FS comes either from VS or GS
382 const GrStringBuilder* fsName;
383 if (segments->fUsesGS) {
384 // if we have a GS take each varying in as an array
385 // and output as non-array.
386 segments->fGSInputs.push_back();
387 segments->fGSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000388 segments->fGSInputs.back().setTypeModifier(
389 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000390 segments->fGSInputs.back().setUnsizedArray();
391 *segments->fGSInputs.back().accessName() =
392 segments->fVSOutputs.back().getName();
393 segments->fGSOutputs.push_back();
394 segments->fGSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000395 segments->fGSOutputs.back().setTypeModifier(
396 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000397 segments->fGSOutputs.back().accessName()->printf("g%s", name);
398 fsName = segments->fGSOutputs.back().accessName();
399 } else {
400 fsName = segments->fVSOutputs.back().accessName();
401 }
402 segments->fFSInputs.push_back();
403 segments->fFSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000404 segments->fFSInputs.back().setTypeModifier(
405 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000406 segments->fFSInputs.back().setName(*fsName);
407 if (fsInName) {
408 *fsInName = fsName->c_str();
409 }
410}
411
412// version of above that adds a stage number to the
413// the var name (for uniqueness)
414void append_varying(GrGLShaderVar::Type type,
415 const char* name,
416 int stageNum,
417 ShaderCodeSegments* segments,
418 const char** vsOutName = NULL,
419 const char** fsInName = NULL) {
420 GrStringBuilder nameWithStage(name);
421 nameWithStage.appendS32(stageNum);
422 append_varying(type, nameWithStage.c_str(), segments, vsOutName, fsInName);
423}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000424}
425
bsalomon@google.com96399942012-02-13 14:39:16 +0000426void GrGLProgram::genEdgeCoverage(const GrGLContextInfo& gl,
bsalomon@google.com66105672011-09-15 15:12:00 +0000427 GrVertexLayout layout,
428 CachedData* programData,
429 GrStringBuilder* coverageVar,
430 ShaderCodeSegments* segments) const {
431 if (fProgramDesc.fEdgeAANumEdges > 0) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000432 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec3f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000433 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000434 EDGES_UNI_NAME,
435 fProgramDesc.fEdgeAANumEdges);
bsalomon@google.com66105672011-09-15 15:12:00 +0000436 programData->fUniLocations.fEdgesUni = kUseUniform;
437 int count = fProgramDesc.fEdgeAANumEdges;
438 segments->fFSCode.append(
439 "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n");
440 for (int i = 0; i < count; i++) {
441 segments->fFSCode.append("\tfloat a");
442 segments->fFSCode.appendS32(i);
443 segments->fFSCode.append(" = clamp(dot(" EDGES_UNI_NAME "[");
444 segments->fFSCode.appendS32(i);
445 segments->fFSCode.append("], pos), 0.0, 1.0);\n");
446 }
447 if (fProgramDesc.fEdgeAAConcave && (count & 0x01) == 0) {
448 // For concave polys, we consider the edges in pairs.
449 segments->fFSFunctions.append("float cross2(vec2 a, vec2 b) {\n");
450 segments->fFSFunctions.append("\treturn dot(a, vec2(b.y, -b.x));\n");
451 segments->fFSFunctions.append("}\n");
452 for (int i = 0; i < count; i += 2) {
453 segments->fFSCode.appendf("\tfloat eb%d;\n", i / 2);
454 segments->fFSCode.appendf("\tif (cross2(" EDGES_UNI_NAME "[%d].xy, " EDGES_UNI_NAME "[%d].xy) < 0.0) {\n", i, i + 1);
455 segments->fFSCode.appendf("\t\teb%d = a%d * a%d;\n", i / 2, i, i + 1);
456 segments->fFSCode.append("\t} else {\n");
457 segments->fFSCode.appendf("\t\teb%d = a%d + a%d - a%d * a%d;\n", i / 2, i, i + 1, i, i + 1);
458 segments->fFSCode.append("\t}\n");
459 }
460 segments->fFSCode.append("\tfloat edgeAlpha = ");
461 for (int i = 0; i < count / 2 - 1; i++) {
462 segments->fFSCode.appendf("min(eb%d, ", i);
463 }
464 segments->fFSCode.appendf("eb%d", count / 2 - 1);
465 for (int i = 0; i < count / 2 - 1; i++) {
466 segments->fFSCode.append(")");
467 }
468 segments->fFSCode.append(";\n");
469 } else {
470 segments->fFSCode.append("\tfloat edgeAlpha = ");
471 for (int i = 0; i < count - 1; i++) {
472 segments->fFSCode.appendf("min(a%d * a%d, ", i, i + 1);
473 }
474 segments->fFSCode.appendf("a%d * a0", count - 1);
475 for (int i = 0; i < count - 1; i++) {
476 segments->fFSCode.append(")");
477 }
478 segments->fFSCode.append(";\n");
479 }
480 *coverageVar = "edgeAlpha";
481 } else if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000482 const char *vsName, *fsName;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000483 append_varying(GrGLShaderVar::kVec4f_Type, "Edge", segments,
484 &vsName, &fsName);
485 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
486 GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000487 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
tomhudson@google.com93813632011-10-27 20:21:16 +0000488 if (GrDrawState::kHairLine_EdgeType == fProgramDesc.fVertexEdgeType) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000489 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000490 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000491 } else if (GrDrawState::kQuad_EdgeType == fProgramDesc.fVertexEdgeType) {
492 segments->fFSCode.appendf("\tfloat edgeAlpha;\n");
493 // keep the derivative instructions outside the conditional
494 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
495 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
496 segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
497 // today we know z and w are in device space. We could use derivatives
498 segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName);
499 segments->fFSCode.append ("\t} else {\n");
500 segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
501 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
502 fsName, fsName);
503 segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
504 segments->fFSCode.appendf("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n"
505 "\t}\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000506 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000507 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
508 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000509 } else {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000510 GrAssert(GrDrawState::kHairQuad_EdgeType == fProgramDesc.fVertexEdgeType);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000511 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
512 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000513 segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
514 "\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
515 fsName, fsName);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000516 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000517 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
518 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000519 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000520 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
521 }
522 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000523 *coverageVar = "edgeAlpha";
524 } else {
525 coverageVar->reset();
526 }
527}
528
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000529namespace {
530
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000531void genInputColor(GrGLProgram::ProgramDesc::ColorInput colorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000532 GrGLProgram::CachedData* programData,
533 ShaderCodeSegments* segments,
534 GrStringBuilder* inColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000535 switch (colorInput) {
536 case GrGLProgram::ProgramDesc::kAttribute_ColorInput: {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000537 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000538 GrGLShaderVar::kAttribute_TypeModifier,
539 COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000540 const char *vsName, *fsName;
541 append_varying(GrGLShaderVar::kVec4f_Type, "Color", segments, &vsName, &fsName);
542 segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
543 *inColor = fsName;
544 } break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000545 case GrGLProgram::ProgramDesc::kUniform_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000546 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000547 GrGLShaderVar::kUniform_TypeModifier,
548 COL_UNI_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000549 programData->fUniLocations.fColorUni = kUseUniform;
550 *inColor = COL_UNI_NAME;
551 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000552 case GrGLProgram::ProgramDesc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000553 GrAssert(!"needComputedColor should be false.");
554 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000555 case GrGLProgram::ProgramDesc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000556 break;
557 default:
558 GrCrash("Unknown color type.");
559 break;
560 }
561}
562
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000563void genAttributeCoverage(ShaderCodeSegments* segments,
564 GrStringBuilder* inOutCoverage) {
565 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000566 GrGLShaderVar::kAttribute_TypeModifier,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000567 COV_ATTR_NAME);
568 const char *vsName, *fsName;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000569 append_varying(GrGLShaderVar::kVec4f_Type, "Coverage",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000570 segments, &vsName, &fsName);
571 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000572 if (inOutCoverage->size()) {
573 segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n",
574 fsName, inOutCoverage->c_str());
575 *inOutCoverage = "attrCoverage";
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000576 } else {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000577 *inOutCoverage = fsName;
578 }
579}
580
581void genUniformCoverage(ShaderCodeSegments* segments,
582 GrGLProgram::CachedData* programData,
583 GrStringBuilder* inOutCoverage) {
584 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
585 GrGLShaderVar::kUniform_TypeModifier,
586 COV_UNI_NAME);
587 programData->fUniLocations.fCoverageUni = kUseUniform;
588 if (inOutCoverage->size()) {
589 segments->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
590 COV_UNI_NAME, inOutCoverage->c_str());
591 *inOutCoverage = "uniCoverage";
592 } else {
593 *inOutCoverage = COV_UNI_NAME;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000594 }
595}
596
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000597}
598
bsalomon@google.com96399942012-02-13 14:39:16 +0000599void GrGLProgram::genGeometryShader(const GrGLContextInfo& gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000600 ShaderCodeSegments* segments) const {
601#if GR_GL_EXPERIMENTAL_GS
602 if (fProgramDesc.fExperimentalGS) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000603 GrAssert(gl.glslGeneration() >= k150_GrGLSLGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000604 segments->fGSHeader.append("layout(triangles) in;\n"
605 "layout(triangle_strip, max_vertices = 6) out;\n");
606 segments->fGSCode.append("void main() {\n"
607 "\tfor (int i = 0; i < 3; ++i) {\n"
608 "\t\tgl_Position = gl_in[i].gl_Position;\n");
609 if (this->fProgramDesc.fEmitsPointSize) {
610 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
611 }
612 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
613 int count = segments->fGSInputs.count();
614 for (int i = 0; i < count; ++i) {
615 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
616 segments->fGSOutputs[i].getName().c_str(),
617 segments->fGSInputs[i].getName().c_str());
618 }
619 segments->fGSCode.append("\t\tEmitVertex();\n"
620 "\t}\n"
621 "\tEndPrimitive();\n"
622 "}\n");
623 }
624#endif
625}
626
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000627const char* GrGLProgram::adjustInColor(const GrStringBuilder& inColor) const {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000628 if (inColor.size()) {
629 return inColor.c_str();
630 } else {
631 if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) {
632 return all_ones_vec(4);
633 } else {
634 return all_zeros_vec(4);
635 }
636 }
637}
638
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000639
bsalomon@google.com96399942012-02-13 14:39:16 +0000640bool GrGLProgram::genProgram(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000641 GrGLProgram::CachedData* programData) const {
junov@google.comf93e7172011-03-31 21:26:24 +0000642
643 ShaderCodeSegments segments;
644 const uint32_t& layout = fProgramDesc.fVertexLayout;
645
bsalomon@google.com91961302011-05-09 18:39:58 +0000646 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000647
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000648#if GR_GL_EXPERIMENTAL_GS
649 segments.fUsesGS = fProgramDesc.fExperimentalGS;
650#endif
651
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000652 SkXfermode::Coeff colorCoeff, uniformCoeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000653 bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000654 // The rest of transfer mode color filters have not been implemented
655 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
656 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000657 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
658 (fProgramDesc.fColorFilterXfermode),
659 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000660 GR_DEBUGASSERT(success);
661 } else {
662 colorCoeff = SkXfermode::kOne_Coeff;
663 uniformCoeff = SkXfermode::kZero_Coeff;
664 }
665
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000666 // no need to do the color filter / matrix at all if coverage is 0. The
667 // output color is scaled by the coverage. All the dual source outputs are
668 // scaled by the coverage as well.
669 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) {
670 colorCoeff = SkXfermode::kZero_Coeff;
671 uniformCoeff = SkXfermode::kZero_Coeff;
672 applyColorMatrix = false;
673 }
674
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000675 // If we know the final color is going to be all zeros then we can
676 // simplify the color filter coeffecients. needComputedColor will then
677 // come out false below.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000678 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000679 colorCoeff = SkXfermode::kZero_Coeff;
680 if (SkXfermode::kDC_Coeff == uniformCoeff ||
681 SkXfermode::kDA_Coeff == uniformCoeff) {
682 uniformCoeff = SkXfermode::kZero_Coeff;
683 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
684 SkXfermode::kIDA_Coeff == uniformCoeff) {
685 uniformCoeff = SkXfermode::kOne_Coeff;
686 }
687 }
688
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000689 bool needColorFilterUniform;
690 bool needComputedColor;
691 needBlendInputs(uniformCoeff, colorCoeff,
692 &needColorFilterUniform, &needComputedColor);
693
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000694 // the dual source output has no canonical var name, have to
695 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000696 bool dualSourceOutputWritten = false;
bsalomon@google.com96399942012-02-13 14:39:16 +0000697 segments.fHeader.printf(GrGetGLSLVersionDecl(gl.binding(),
698 gl.glslGeneration()));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000699
700 GrGLShaderVar colorOutput;
bsalomon@google.com96399942012-02-13 14:39:16 +0000701 bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(),
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000702 declared_color_output_name(),
703 &colorOutput);
704 if (isColorDeclared) {
705 segments.fFSOutputs.push_back(colorOutput);
706 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000707
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000708#if GR_GL_ATTRIBUTE_MATRICES
tomhudson@google.com086e5352011-12-08 14:44:10 +0000709 segments.fVSAttrs.push_back().set(GrGLShaderVar::kMat33f_Type,
710 GrGLShaderVar::kAttribute_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000711 programData->fUniLocations.fViewMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +0000712#else
tomhudson@google.com086e5352011-12-08 14:44:10 +0000713 segments.fVSUnis.push_back().set(GrGLShaderVar::kMat33f_Type,
714 GrGLShaderVar::kUniform_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000715 programData->fUniLocations.fViewMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +0000716#endif
tomhudson@google.com086e5352011-12-08 14:44:10 +0000717 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type,
718 GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000719
bsalomon@google.com91961302011-05-09 18:39:58 +0000720 segments.fVSCode.append(
721 "void main() {\n"
722 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
723 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000724
bsalomon@google.com91961302011-05-09 18:39:58 +0000725 // incoming color to current stage being processed.
726 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000727
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000728 if (needComputedColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000729 genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000730 programData, &segments, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000731 }
732
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000733 // we output point size in the GS if present
734 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000735 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000736 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000737
bsalomon@google.com91961302011-05-09 18:39:58 +0000738 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000739
740 // add texture coordinates that are used to the list of vertex attr decls
tomhudson@google.com93813632011-10-27 20:21:16 +0000741 GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords];
742 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000743 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000744 tex_attr_name(t, texCoordAttrs + t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000745 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000746 GrGLShaderVar::kAttribute_TypeModifier,
747 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000748 }
749 }
750
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000751 ///////////////////////////////////////////////////////////////////////////
752 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000753
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000754 // if we have color stages string them together, feeding the output color
755 // of each to the next and generating code for each stage.
756 if (needComputedColor) {
757 GrStringBuilder outColor;
758 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000759 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000760 // create var to hold stage result
761 outColor = "color";
762 outColor.appendS32(s);
763 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
764
765 const char* inCoords;
766 // figure out what our input coords are
767 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
768 layout) {
769 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000770 } else {
771 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
772 // we better have input tex coordinates if stage is enabled.
773 GrAssert(tcIdx >= 0);
774 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000775 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000776 }
777
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000778 this->genStageCode(gl,
779 s,
780 fProgramDesc.fStages[s],
781 inColor.size() ? inColor.c_str() : NULL,
782 outColor.c_str(),
783 inCoords,
784 &segments,
785 &programData->fUniLocations.fStages[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000786 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000787 }
788 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000789 }
Scroggo97c88c22011-05-11 14:05:25 +0000790
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000791 // if have all ones or zeros for the "dst" input to the color filter then we
792 // may be able to make additional optimizations.
793 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000794 GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000795 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
796 SkXfermode::kIDA_Coeff == uniformCoeff;
797 if (uniformCoeffIsZero) {
798 uniformCoeff = SkXfermode::kZero_Coeff;
799 bool bogus;
800 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
801 &needColorFilterUniform, &bogus);
802 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000803 }
804 if (needColorFilterUniform) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000805 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000806 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000807 COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000808 programData->fUniLocations.fColorFilterUni = kUseUniform;
809 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000810 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000811 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000812 SkXfermode::kZero_Coeff == colorCoeff &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000813 !applyColorMatrix) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000814 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000815 colorOutput.getName().c_str(),
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000816 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000817 wroteFragColorZero = true;
818 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
819 segments.fFSCode.appendf("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000820 const char* color = adjustInColor(inColor);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000821 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000822 colorCoeff, color);
823 inColor = "filteredColor";
824 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000825 if (applyColorMatrix) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000826 segments.fFSUnis.push_back().set(GrGLShaderVar::kMat44f_Type,
827 GrGLShaderVar::kUniform_TypeModifier,
828 COL_MATRIX_UNI_NAME);
829 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
830 GrGLShaderVar::kUniform_TypeModifier,
831 COL_MATRIX_VEC_UNI_NAME);
832 programData->fUniLocations.fColorMatrixUni = kUseUniform;
833 programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
834 segments.fFSCode.appendf("\tvec4 matrixedColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000835 const char* color = adjustInColor(inColor);
836 addColorMatrix(&segments.fFSCode, "matrixedColor", color);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000837 inColor = "matrixedColor";
838 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000839
840 ///////////////////////////////////////////////////////////////////////////
841 // compute the partial coverage (coverage stages and edge aa)
842
843 GrStringBuilder inCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000844 bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput ==
845 fProgramDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000846 // we don't need to compute coverage at all if we know the final shader
847 // output will be zero and we don't have a dual src blend output.
848 if (!wroteFragColorZero ||
849 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000850
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000851 if (!coverageIsZero) {
852 this->genEdgeCoverage(gl,
853 layout,
854 programData,
855 &inCoverage,
856 &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000857
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000858 switch (fProgramDesc.fCoverageInput) {
859 case ProgramDesc::kSolidWhite_ColorInput:
860 // empty string implies solid white
861 break;
862 case ProgramDesc::kAttribute_ColorInput:
863 genAttributeCoverage(&segments, &inCoverage);
864 break;
865 case ProgramDesc::kUniform_ColorInput:
866 genUniformCoverage(&segments, programData, &inCoverage);
867 break;
868 default:
869 GrCrash("Unexpected input coverage.");
870 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000871
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000872 GrStringBuilder outCoverage;
873 const int& startStage = fProgramDesc.fFirstCoverageStage;
874 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
875 if (fProgramDesc.fStages[s].isEnabled()) {
876 // create var to hold stage output
877 outCoverage = "coverage";
878 outCoverage.appendS32(s);
879 segments.fFSCode.appendf("\tvec4 %s;\n",
880 outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000881
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000882 const char* inCoords;
883 // figure out what our input coords are
884 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
885 layout) {
886 inCoords = POS_ATTR_NAME;
887 } else {
888 int tcIdx =
889 GrDrawTarget::VertexTexCoordsForStage(s, layout);
890 // we better have input tex coordinates if stage is
891 // enabled.
892 GrAssert(tcIdx >= 0);
893 GrAssert(texCoordAttrs[tcIdx].size());
894 inCoords = texCoordAttrs[tcIdx].c_str();
895 }
896
897 genStageCode(gl, s,
898 fProgramDesc.fStages[s],
899 inCoverage.size() ? inCoverage.c_str() : NULL,
900 outCoverage.c_str(),
901 inCoords,
902 &segments,
903 &programData->fUniLocations.fStages[s]);
904 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000905 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000906 }
907 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000908 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000909 segments.fFSOutputs.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000910 GrGLShaderVar::kOut_TypeModifier,
911 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000912 bool outputIsZero = coverageIsZero;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000913 GrStringBuilder coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000914 if (!outputIsZero &&
915 ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000916 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
917 if (!inColor.size()) {
918 outputIsZero = true;
919 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000920 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000921 ProgramDesc::kCoverageISA_DualSrcOutput) {
922 coeff.printf("(1 - %s.a)", inColor.c_str());
923 } else {
924 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
925 }
926 }
927 }
928 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000929 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000930 dual_source_output_name(),
931 all_zeros_vec(4));
932 } else {
933 modulate_helper(dual_source_output_name(),
934 coeff.c_str(),
935 inCoverage.c_str(),
936 &segments.fFSCode);
937 }
938 dualSourceOutputWritten = true;
939 }
junov@google.comf93e7172011-03-31 21:26:24 +0000940 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000941
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000942 ///////////////////////////////////////////////////////////////////////////
943 // combine color and coverage as frag color
944
945 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000946 if (coverageIsZero) {
947 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000948 colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000949 all_zeros_vec(4));
950 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000951 modulate_helper(colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000952 inColor.c_str(),
953 inCoverage.c_str(),
954 &segments.fFSCode);
955 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000956 if (ProgramDesc::kNo_OutputPM == fProgramDesc.fOutputPM) {
bsalomon@google.come63799d2011-11-09 14:14:38 +0000957 segments.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(%s.rgb / %s.a, %s.a);\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000958 colorOutput.getName().c_str(),
959 colorOutput.getName().c_str(),
960 colorOutput.getName().c_str(),
961 colorOutput.getName().c_str(),
962 colorOutput.getName().c_str());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000963 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000964 }
965
bsalomon@google.com91961302011-05-09 18:39:58 +0000966 segments.fVSCode.append("}\n");
967 segments.fFSCode.append("}\n");
968
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000969 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000970 // insert GS
971#if GR_DEBUG
bsalomon@google.com96399942012-02-13 14:39:16 +0000972 this->genGeometryShader(gl, &segments);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000973#endif
974
975 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000976 // compile and setup attribs and unis
977
bsalomon@google.com96399942012-02-13 14:39:16 +0000978 if (!CompileShaders(gl, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000979 return false;
980 }
981
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000982 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000983 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000984 dualSourceOutputWritten,
985 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000986 return false;
987 }
988
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000989 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000990
991 return true;
992}
993
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000994namespace {
995
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000996inline void expand_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +0000997 const GrGLContextInfo& gl,
998 GrStringBuilder* string) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000999 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001000 for (int i = 0; i < count; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001001 vars[i].appendDecl(gl, string);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001002 }
1003}
1004
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001005inline void print_shader(int stringCnt,
1006 const char** strings,
1007 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001008 for (int i = 0; i < stringCnt; ++i) {
1009 if (NULL == stringLengths || stringLengths[i] < 0) {
1010 GrPrintf(strings[i]);
1011 } else {
1012 GrPrintf("%.*s", stringLengths[i], strings[i]);
1013 }
1014 }
1015}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001016
1017typedef SkTArray<const char*, true> StrArray;
1018#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
1019
1020typedef SkTArray<int, true> LengthArray;
1021#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
1022
1023// these shouldn't relocate
1024typedef GrTAllocator<GrStringBuilder> TempArray;
1025#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
1026
1027inline void append_string(const GrStringBuilder& str,
1028 StrArray* strings,
1029 LengthArray* lengths) {
1030 int length = (int) str.size();
1031 if (length) {
1032 strings->push_back(str.c_str());
1033 lengths->push_back(length);
1034 }
1035 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001036}
1037
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001038inline void append_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +00001039 const GrGLContextInfo& gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001040 StrArray* strings,
1041 LengthArray* lengths,
bsalomon@google.com96399942012-02-13 14:39:16 +00001042 TempArray* temp) {
1043 expand_decls(vars, gl, &temp->push_back());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001044 append_string(temp->back(), strings, lengths);
1045}
1046
1047}
1048
bsalomon@google.com96399942012-02-13 14:39:16 +00001049bool GrGLProgram::CompileShaders(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001050 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +00001051 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001052 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +00001053
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001054 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
1055 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
1056 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
1057
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001058 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001059 GrStringBuilder inputs;
1060 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001061
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001062 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001063 append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps);
1064 append_decls(segments.fVSAttrs, gl, &strs, &lengths, &temps);
1065 append_decls(segments.fVSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001066 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001067
1068#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001069 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001070 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001071#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001072
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001073 programData->fVShaderID =
1074 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
1075 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001076
bsalomon@google.com91961302011-05-09 18:39:58 +00001077 if (!programData->fVShaderID) {
1078 return false;
1079 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001080 if (segments.fUsesGS) {
1081 strs.reset();
1082 lengths.reset();
1083 temps.reset();
1084 append_string(segments.fHeader, &strs, &lengths);
1085 append_string(segments.fGSHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001086 append_decls(segments.fGSInputs, gl, &strs, &lengths, &temps);
1087 append_decls(segments.fGSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001088 append_string(segments.fGSCode, &strs, &lengths);
1089#if PRINT_SHADERS
1090 print_shader(strs.count(), &strs[0], &lengths[0]);
1091 GrPrintf("\n");
1092#endif
1093 programData->fGShaderID =
1094 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
1095 &strs[0], &lengths[0]);
1096 } else {
1097 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +00001098 }
junov@google.comf93e7172011-03-31 21:26:24 +00001099
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001100 strs.reset();
1101 lengths.reset();
1102 temps.reset();
1103
1104 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001105 GrStringBuilder precisionStr(GrGetGLSLShaderPrecisionDecl(gl.binding()));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001106 append_string(precisionStr, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001107 append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps);
1108 append_decls(segments.fFSInputs, gl, &strs, &lengths, &temps);
bsalomon@google.com373a6632011-10-19 20:43:20 +00001109 // We shouldn't have declared outputs on 1.10
bsalomon@google.com96399942012-02-13 14:39:16 +00001110 GrAssert(k110_GrGLSLGeneration != gl.glslGeneration() ||
tomhudson@google.com086e5352011-12-08 14:44:10 +00001111 segments.fFSOutputs.empty());
bsalomon@google.com96399942012-02-13 14:39:16 +00001112 append_decls(segments.fFSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001113 append_string(segments.fFSFunctions, &strs, &lengths);
1114 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001115
1116#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001117 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001118 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001119#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001120
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001121 programData->fFShaderID =
1122 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
1123 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001124
bsalomon@google.com91961302011-05-09 18:39:58 +00001125 if (!programData->fFShaderID) {
1126 return false;
junov@google.comf93e7172011-03-31 21:26:24 +00001127 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001128
bsalomon@google.com91961302011-05-09 18:39:58 +00001129 return true;
junov@google.comf93e7172011-03-31 21:26:24 +00001130}
1131
bsalomon@google.com96399942012-02-13 14:39:16 +00001132#define GL_CALL(X) GR_GL_CALL(gl.interface(), X)
1133#define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X)
1134
1135GrGLuint GrGLProgram::CompileShader(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001136 GrGLenum type,
1137 int stringCnt,
1138 const char** strings,
1139 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001140 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
1141 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
1142
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001143 GrGLuint shader;
bsalomon@google.com96399942012-02-13 14:39:16 +00001144 GL_CALL_RET(shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +00001145 if (0 == shader) {
1146 return 0;
1147 }
1148
1149 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001150 GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths));
1151 GL_CALL(CompileShader(shader));
1152 GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +00001153
1154 if (!compiled) {
1155 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001156 GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001157 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001158 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001159 // retrieve length even though we don't need it to workaround
1160 // bug in chrome cmd buffer param validation.
1161 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001162 GL_CALL(GetShaderInfoLog(shader, infoLen+1,
1163 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001164 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001165 GrPrintf("\n%s", log.get());
1166 }
1167 GrAssert(!"Shader compilation failed!");
bsalomon@google.com96399942012-02-13 14:39:16 +00001168 GL_CALL(DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001169 return 0;
1170 }
1171 return shader;
1172}
1173
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001174bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com96399942012-02-13 14:39:16 +00001175 const GrGLContextInfo& gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001176 GrStringBuilder texCoordAttrNames[],
1177 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001178 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001179 CachedData* programData) const {
bsalomon@google.com96399942012-02-13 14:39:16 +00001180 GL_CALL_RET(programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001181 if (!programData->fProgramID) {
1182 return false;
1183 }
1184 const GrGLint& progID = programData->fProgramID;
1185
bsalomon@google.com96399942012-02-13 14:39:16 +00001186 GL_CALL(AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001187 if (programData->fGShaderID) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001188 GL_CALL(AttachShader(progID, programData->fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001189 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001190 GL_CALL(AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001191
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001192 if (bindColorOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001193 GL_CALL(BindFragDataLocation(programData->fProgramID,
1194 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001195 }
1196 if (bindDualSrcOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001197 GL_CALL(BindFragDataLocationIndexed(programData->fProgramID,
1198 0, 1, dual_source_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001199 }
1200
bsalomon@google.com91961302011-05-09 18:39:58 +00001201 // Bind the attrib locations to same values for all shaders
bsalomon@google.com96399942012-02-13 14:39:16 +00001202 GL_CALL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +00001203 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001204 if (texCoordAttrNames[t].size()) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001205 GL_CALL(BindAttribLocation(progID,
1206 TexCoordAttributeIdx(t),
1207 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001208 }
1209 }
1210
bsalomon@google.com91961302011-05-09 18:39:58 +00001211 if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001212 GL_CALL(BindAttribLocation(progID,
1213 ViewMatrixAttributeIdx(),
1214 VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001215 }
1216
tomhudson@google.com93813632011-10-27 20:21:16 +00001217 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com72b4fcb2011-05-09 20:47:34 +00001218 const StageUniLocations& unis = programData->fUniLocations.fStages[s];
1219 if (kSetAsAttribute == unis.fTextureMatrixUni) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001220 GrStringBuilder matName;
1221 tex_matrix_name(s, &matName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001222 GL_CALL(BindAttribLocation(progID,
1223 TextureMatrixAttributeIdx(s),
1224 matName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001225 }
1226 }
1227
bsalomon@google.com96399942012-02-13 14:39:16 +00001228 GL_CALL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME));
1229 GL_CALL(BindAttribLocation(progID, CoverageAttributeIdx(), COV_ATTR_NAME));
1230 GL_CALL(BindAttribLocation(progID, EdgeAttributeIdx(), EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001231
bsalomon@google.com96399942012-02-13 14:39:16 +00001232 GL_CALL(LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001233
1234 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001235 GL_CALL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001236 if (!linked) {
1237 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001238 GL_CALL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001239 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001240 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001241 // retrieve length even though we don't need it to workaround
1242 // bug in chrome cmd buffer param validation.
1243 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001244 GL_CALL(GetProgramInfoLog(progID,
1245 infoLen+1,
1246 &length,
1247 (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001248 GrPrintf((char*)log.get());
1249 }
1250 GrAssert(!"Error linking program");
bsalomon@google.com96399942012-02-13 14:39:16 +00001251 GL_CALL(DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001252 programData->fProgramID = 0;
1253 return false;
1254 }
1255 return true;
1256}
1257
bsalomon@google.com96399942012-02-13 14:39:16 +00001258void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001259 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001260 const GrGLint& progID = programData->fProgramID;
1261
1262 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001263 GL_CALL_RET(programData->fUniLocations.fViewMatrixUni,
1264 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001265 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1266 }
1267 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001268 GL_CALL_RET(programData->fUniLocations.fColorUni,
1269 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001270 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1271 }
Scroggo97c88c22011-05-11 14:05:25 +00001272 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001273 GL_CALL_RET(programData->fUniLocations.fColorFilterUni,
1274 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001275 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1276 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001277
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001278 if (kUseUniform == programData->fUniLocations.fColorMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001279 GL_CALL_RET(programData->fUniLocations.fColorMatrixUni,
1280 GetUniformLocation(progID, COL_MATRIX_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001281 }
1282
1283 if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001284 GL_CALL_RET(programData->fUniLocations.fColorMatrixVecUni,
1285 GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001286 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001287 if (kUseUniform == programData->fUniLocations.fCoverageUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001288 GL_CALL_RET(programData->fUniLocations.fCoverageUni,
1289 GetUniformLocation(progID, COV_UNI_NAME));
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001290 GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni);
1291 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001292
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001293 if (kUseUniform == programData->fUniLocations.fEdgesUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001294 GL_CALL_RET(programData->fUniLocations.fEdgesUni,
1295 GetUniformLocation(progID, EDGES_UNI_NAME));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001296 GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni);
1297 } else {
1298 programData->fUniLocations.fEdgesUni = kUnusedUniform;
1299 }
1300
tomhudson@google.com93813632011-10-27 20:21:16 +00001301 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001302 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001303 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001304 if (kUseUniform == locations.fTextureMatrixUni) {
1305 GrStringBuilder texMName;
1306 tex_matrix_name(s, &texMName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001307 GL_CALL_RET(locations.fTextureMatrixUni,
1308 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001309 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1310 }
1311
1312 if (kUseUniform == locations.fSamplerUni) {
1313 GrStringBuilder samplerName;
1314 sampler_name(s, &samplerName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001315 GL_CALL_RET(locations.fSamplerUni,
1316 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001317 GrAssert(kUnusedUniform != locations.fSamplerUni);
1318 }
1319
1320 if (kUseUniform == locations.fNormalizedTexelSizeUni) {
1321 GrStringBuilder texelSizeName;
1322 normalized_texel_size_name(s, &texelSizeName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001323 GL_CALL_RET(locations.fNormalizedTexelSizeUni,
1324 GetUniformLocation(progID, texelSizeName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001325 GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni);
1326 }
1327
1328 if (kUseUniform == locations.fRadial2Uni) {
1329 GrStringBuilder radial2ParamName;
1330 radial2_param_name(s, &radial2ParamName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001331 GL_CALL_RET(locations.fRadial2Uni,
1332 GetUniformLocation(progID, radial2ParamName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001333 GrAssert(kUnusedUniform != locations.fRadial2Uni);
1334 }
junov@google.com6acc9b32011-05-16 18:32:07 +00001335
1336 if (kUseUniform == locations.fTexDomUni) {
1337 GrStringBuilder texDomName;
1338 tex_domain_name(s, &texDomName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001339 GL_CALL_RET(locations.fTexDomUni,
1340 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001341 GrAssert(kUnusedUniform != locations.fTexDomUni);
1342 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001343
1344 GrStringBuilder kernelName, imageIncrementName;
1345 convolve_param_names(s, &kernelName, &imageIncrementName);
1346 if (kUseUniform == locations.fKernelUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001347 GL_CALL_RET(locations.fKernelUni,
1348 GetUniformLocation(progID, kernelName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001349 GrAssert(kUnusedUniform != locations.fKernelUni);
1350 }
1351
1352 if (kUseUniform == locations.fImageIncrementUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001353 GL_CALL_RET(locations.fImageIncrementUni,
1354 GetUniformLocation(progID,
1355 imageIncrementName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001356 GrAssert(kUnusedUniform != locations.fImageIncrementUni);
1357 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001358 }
1359 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001360 GL_CALL(UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001361
1362 // init sampler unis and set bogus values for state tracking
tomhudson@google.com93813632011-10-27 20:21:16 +00001363 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001364 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001365 GL_CALL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001366 }
1367 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
1368 programData->fRadial2CenterX1[s] = GR_ScalarMax;
1369 programData->fRadial2Radius0[s] = -GR_ScalarMax;
1370 programData->fTextureWidth[s] = -1;
1371 programData->fTextureHeight[s] = -1;
1372 }
1373 programData->fViewMatrix = GrMatrix::InvalidMatrix();
1374 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001375 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001376}
1377
junov@google.comf93e7172011-03-31 21:26:24 +00001378//============================================================================
1379// Stage code generation
1380//============================================================================
1381
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001382namespace {
1383
1384bool isRadialMapping(GrGLProgram::StageDesc::CoordMapping mapping) {
1385 return
1386 (GrGLProgram::StageDesc::kRadial2Gradient_CoordMapping == mapping ||
1387 GrGLProgram::StageDesc::kRadial2GradientDegenerate_CoordMapping == mapping);
1388}
1389
tomhudson@google.comda668982011-12-07 15:06:29 +00001390GrGLShaderVar* genRadialVS(int stageNum,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001391 ShaderCodeSegments* segments,
1392 GrGLProgram::StageUniLocations* locations,
1393 const char** radial2VaryingVSName,
1394 const char** radial2VaryingFSName,
1395 const char* varyingVSName,
1396 int varyingDims, int coordDims) {
1397
1398 GrGLShaderVar* radial2FSParams = &segments->fFSUnis.push_back();
1399 radial2FSParams->setType(GrGLShaderVar::kFloat_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001400 radial2FSParams->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001401 radial2FSParams->setArrayCount(6);
1402 radial2_param_name(stageNum, radial2FSParams->accessName());
1403 segments->fVSUnis.push_back(*radial2FSParams).setEmitPrecision(true);
1404
1405 locations->fRadial2Uni = kUseUniform;
1406
1407 // for radial grads without perspective we can pass the linear
1408 // part of the quadratic as a varying.
1409 if (varyingDims == coordDims) {
1410 GrAssert(2 == coordDims);
1411 append_varying(GrGLShaderVar::kFloat_Type,
1412 "Radial2BCoeff",
1413 stageNum,
1414 segments,
1415 radial2VaryingVSName,
1416 radial2VaryingFSName);
1417
tomhudson@google.comda668982011-12-07 15:06:29 +00001418 GrStringBuilder radial2p2;
1419 GrStringBuilder radial2p3;
1420 radial2FSParams->appendArrayAccess(2, &radial2p2);
1421 radial2FSParams->appendArrayAccess(3, &radial2p3);
1422
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001423 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
1424 const char* r2ParamName = radial2FSParams->getName().c_str();
tomhudson@google.comda668982011-12-07 15:06:29 +00001425 segments->fVSCode.appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
1426 *radial2VaryingVSName, radial2p2.c_str(),
1427 varyingVSName, radial2p3.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001428 }
1429
tomhudson@google.comda668982011-12-07 15:06:29 +00001430 return radial2FSParams;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001431}
1432
1433bool genRadial2GradientCoordMapping(int stageNum,
1434 ShaderCodeSegments* segments,
1435 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001436 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001437 GrStringBuilder& sampleCoords,
1438 GrStringBuilder& fsCoordName,
1439 int varyingDims,
1440 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001441 GrStringBuilder cName("c");
1442 GrStringBuilder ac4Name("ac4");
1443 GrStringBuilder rootName("root");
1444
1445 cName.appendS32(stageNum);
1446 ac4Name.appendS32(stageNum);
1447 rootName.appendS32(stageNum);
1448
tomhudson@google.comda668982011-12-07 15:06:29 +00001449 GrStringBuilder radial2p0;
1450 GrStringBuilder radial2p1;
1451 GrStringBuilder radial2p2;
1452 GrStringBuilder radial2p3;
1453 GrStringBuilder radial2p4;
1454 GrStringBuilder radial2p5;
1455 radial2Params->appendArrayAccess(0, &radial2p0);
1456 radial2Params->appendArrayAccess(1, &radial2p1);
1457 radial2Params->appendArrayAccess(2, &radial2p2);
1458 radial2Params->appendArrayAccess(3, &radial2p3);
1459 radial2Params->appendArrayAccess(4, &radial2p4);
1460 radial2Params->appendArrayAccess(5, &radial2p5);
1461
tomhudson@google.com5960d002011-10-19 20:21:48 +00001462 // if we were able to interpolate the linear component bVar is the varying
1463 // otherwise compute it
1464 GrStringBuilder bVar;
1465 if (coordDims == varyingDims) {
1466 bVar = radial2VaryingFSName;
1467 GrAssert(2 == varyingDims);
1468 } else {
1469 GrAssert(3 == varyingDims);
1470 bVar = "b";
1471 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001472 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1473 bVar.c_str(), radial2p2.c_str(),
1474 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001475 }
1476
1477 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001478 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001479 cName.c_str(), fsCoordName.c_str(),
1480 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001481 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001482 // ac4 = 4.0 * params[0] * c
tomhudson@google.comda668982011-12-07 15:06:29 +00001483 segments->fFSCode.appendf("\tfloat %s = %s * 4.0 * %s;\n",
1484 ac4Name.c_str(), radial2p0.c_str(),
tomhudson@google.com5960d002011-10-19 20:21:48 +00001485 cName.c_str());
1486
1487 // root = sqrt(b^2-4ac)
1488 // (abs to avoid exception due to fp precision)
1489 segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
1490 rootName.c_str(), bVar.c_str(), bVar.c_str(),
1491 ac4Name.c_str());
1492
1493 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
1494 // y coord is 0.5 (texture is effectively 1D)
tomhudson@google.comda668982011-12-07 15:06:29 +00001495 sampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)",
1496 bVar.c_str(), radial2p5.c_str(),
1497 rootName.c_str(), radial2p1.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001498 return true;
1499}
1500
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001501bool genRadial2GradientDegenerateCoordMapping(int stageNum,
1502 ShaderCodeSegments* segments,
1503 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001504 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001505 GrStringBuilder& sampleCoords,
1506 GrStringBuilder& fsCoordName,
1507 int varyingDims,
1508 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001509 GrStringBuilder cName("c");
1510
1511 cName.appendS32(stageNum);
1512
tomhudson@google.comda668982011-12-07 15:06:29 +00001513 GrStringBuilder radial2p2;
1514 GrStringBuilder radial2p3;
1515 GrStringBuilder radial2p4;
1516 radial2Params->appendArrayAccess(2, &radial2p2);
1517 radial2Params->appendArrayAccess(3, &radial2p3);
1518 radial2Params->appendArrayAccess(4, &radial2p4);
1519
tomhudson@google.com5960d002011-10-19 20:21:48 +00001520 // if we were able to interpolate the linear component bVar is the varying
1521 // otherwise compute it
1522 GrStringBuilder bVar;
1523 if (coordDims == varyingDims) {
1524 bVar = radial2VaryingFSName;
1525 GrAssert(2 == varyingDims);
1526 } else {
1527 GrAssert(3 == varyingDims);
1528 bVar = "b";
1529 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001530 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1531 bVar.c_str(), radial2p2.c_str(),
1532 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001533 }
1534
1535 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001536 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001537 cName.c_str(), fsCoordName.c_str(),
1538 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001539 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +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 return true;
1545}
1546
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001547void gen2x2FS(int stageNum,
1548 ShaderCodeSegments* segments,
1549 GrGLProgram::StageUniLocations* locations,
1550 GrStringBuilder* sampleCoords,
1551 const char* samplerName,
1552 const char* texelSizeName,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001553 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001554 const char* fsOutColor,
1555 GrStringBuilder& texFunc,
1556 GrStringBuilder& modulate,
1557 bool complexCoord,
1558 int coordDims) {
1559 locations->fNormalizedTexelSizeUni = kUseUniform;
1560 if (complexCoord) {
1561 // assign the coord to a var rather than compute 4x.
1562 GrStringBuilder coordVar("tCoord");
1563 coordVar.appendS32(stageNum);
1564 segments->fFSCode.appendf("\t%s %s = %s;\n",
1565 float_vector_type_str(coordDims),
1566 coordVar.c_str(), sampleCoords->c_str());
1567 *sampleCoords = coordVar;
1568 }
1569 GrAssert(2 == coordDims);
1570 GrStringBuilder accumVar("accum");
1571 accumVar.appendS32(stageNum);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001572 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);
1573 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);
1574 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);
1575 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 +00001576 segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str());
1577
1578}
1579
1580void genConvolutionVS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001581 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001582 ShaderCodeSegments* segments,
1583 GrGLProgram::StageUniLocations* locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001584 GrGLShaderVar** kernel,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001585 const char** imageIncrementName,
1586 const char* varyingVSName) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001587 //GrGLShaderVar* kernel = &segments->fFSUnis.push_back();
1588 *kernel = &segments->fFSUnis.push_back();
1589 (*kernel)->setType(GrGLShaderVar::kFloat_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001590 (*kernel)->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.comda668982011-12-07 15:06:29 +00001591 (*kernel)->setArrayCount(desc.fKernelWidth);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001592 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
1593 imgInc->setType(GrGLShaderVar::kVec2f_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001594 imgInc->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001595
1596 convolve_param_names(stageNum,
tomhudson@google.comda668982011-12-07 15:06:29 +00001597 (*kernel)->accessName(),
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001598 imgInc->accessName());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001599 *imageIncrementName = imgInc->getName().c_str();
1600
1601 // need image increment in both VS and FS
1602 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1603
1604 locations->fKernelUni = kUseUniform;
1605 locations->fImageIncrementUni = kUseUniform;
1606 float scale = (desc.fKernelWidth - 1) * 0.5f;
1607 segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n",
1608 varyingVSName, scale, scale,
1609 *imageIncrementName);
1610}
1611
1612void genConvolutionFS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001613 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001614 ShaderCodeSegments* segments,
1615 const char* samplerName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001616 GrGLShaderVar* kernel,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001617 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001618 const char* imageIncrementName,
1619 const char* fsOutColor,
1620 GrStringBuilder& sampleCoords,
1621 GrStringBuilder& texFunc,
1622 GrStringBuilder& modulate) {
1623 GrStringBuilder sumVar("sum");
1624 sumVar.appendS32(stageNum);
1625 GrStringBuilder coordVar("coord");
1626 coordVar.appendS32(stageNum);
1627
tomhudson@google.comda668982011-12-07 15:06:29 +00001628 GrStringBuilder kernelIndex;
1629 kernel->appendArrayAccess("i", &kernelIndex);
1630
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001631 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1632 sumVar.c_str());
1633 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1634 coordVar.c_str(),
1635 sampleCoords.c_str());
1636 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1637 desc.fKernelWidth);
tomhudson@google.comda668982011-12-07 15:06:29 +00001638 segments->fFSCode.appendf("\t\t%s += %s(%s, %s)%s * %s;\n",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001639 sumVar.c_str(), texFunc.c_str(),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001640 samplerName, coordVar.c_str(), swizzle,
tomhudson@google.comda668982011-12-07 15:06:29 +00001641 kernelIndex.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001642 segments->fFSCode.appendf("\t\t%s += %s;\n",
1643 coordVar.c_str(),
1644 imageIncrementName);
1645 segments->fFSCode.appendf("\t}\n");
1646 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1647 sumVar.c_str(), modulate.c_str());
1648}
1649
1650}
1651
bsalomon@google.com96399942012-02-13 14:39:16 +00001652void GrGLProgram::genStageCode(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001653 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001654 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001655 const char* fsInColor, // NULL means no incoming color
1656 const char* fsOutColor,
1657 const char* vsInCoord,
1658 ShaderCodeSegments* segments,
1659 StageUniLocations* locations) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001660
bsalomon@google.com74b98712011-11-11 19:46:16 +00001661 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
1662 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) ==
1663 desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001664
junov@google.comf93e7172011-03-31 21:26:24 +00001665 // First decide how many coords are needed to access the texture
1666 // Right now it's always 2 but we could start using 1D textures for
1667 // gradients.
1668 static const int coordDims = 2;
1669 int varyingDims;
1670 /// Vertex Shader Stuff
1671
1672 // decide whether we need a matrix to transform texture coords
1673 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001674 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001675 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001676 varyingDims = coordDims;
1677 } else {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001678 GrGLShaderVar* mat;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001679 #if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001680 mat = &segments->fVSAttrs.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001681 mat->setTypeModifier(GrGLShaderVar::kAttribute_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001682 locations->fTextureMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +00001683 #else
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001684 mat = &segments->fVSUnis.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001685 mat->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001686 locations->fTextureMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001687 #endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001688 tex_matrix_name(stageNum, mat->accessName());
1689 mat->setType(GrGLShaderVar::kMat33f_Type);
1690 matName = mat->getName().c_str();
1691
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001692 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001693 varyingDims = coordDims;
1694 } else {
1695 varyingDims = coordDims + 1;
1696 }
1697 }
1698
tomhudson@google.com086e5352011-12-08 14:44:10 +00001699 segments->fFSUnis.push_back().set(GrGLShaderVar::kSampler2D_Type,
1700 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001701 sampler_name(stageNum, segments->fFSUnis.back().accessName());
bsalomon@google.com91961302011-05-09 18:39:58 +00001702 locations->fSamplerUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001703 const char* samplerName = segments->fFSUnis.back().getName().c_str();
junov@google.comf93e7172011-03-31 21:26:24 +00001704
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001705 const char* texelSizeName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001706 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
tomhudson@google.com086e5352011-12-08 14:44:10 +00001707 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec2f_Type,
1708 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001709 normalized_texel_size_name(stageNum, segments->fFSUnis.back().accessName());
1710 texelSizeName = segments->fFSUnis.back().getName().c_str();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001711 }
1712
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001713 const char *varyingVSName, *varyingFSName;
1714 append_varying(float_vector_type(varyingDims),
1715 "Stage",
1716 stageNum,
1717 segments,
1718 &varyingVSName,
1719 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001720
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001721 if (!matName) {
junov@google.comf93e7172011-03-31 21:26:24 +00001722 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001723 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001724 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001725 // varying = texMatrix * texCoord
1726 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001727 varyingVSName, matName, vsInCoord,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001728 vector_all_coords(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001729 }
1730
tomhudson@google.comda668982011-12-07 15:06:29 +00001731 GrGLShaderVar* radial2Params = NULL;
1732 const char* radial2VaryingVSName = NULL;
1733 const char* radial2VaryingFSName = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001734
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001735 if (isRadialMapping((StageDesc::CoordMapping) desc.fCoordMapping)) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001736 radial2Params = genRadialVS(stageNum, segments,
1737 locations,
1738 &radial2VaryingVSName,
1739 &radial2VaryingFSName,
1740 varyingVSName,
1741 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001742 }
1743
tomhudson@google.comda668982011-12-07 15:06:29 +00001744 GrGLShaderVar* kernel = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001745 const char* imageIncrementName = NULL;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001746 if (StageDesc::kConvolution_FetchMode == desc.fFetchMode) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001747 genConvolutionVS(stageNum, desc, segments, locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001748 &kernel, &imageIncrementName, varyingVSName);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001749 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001750
junov@google.comf93e7172011-03-31 21:26:24 +00001751 /// Fragment Shader Stuff
bsalomon@google.com91961302011-05-09 18:39:58 +00001752 GrStringBuilder fsCoordName;
junov@google.comf93e7172011-03-31 21:26:24 +00001753 // function used to access the shader, may be made projective
bsalomon@google.com91961302011-05-09 18:39:58 +00001754 GrStringBuilder texFunc("texture2D");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001755 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1756 StageDesc::kNoPerspective_OptFlagBit)) {
junov@google.comf93e7172011-03-31 21:26:24 +00001757 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001758 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001759 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001760 // if we have to do some special op on the varyings to get
junov@google.comf93e7172011-03-31 21:26:24 +00001761 // our final tex coords then when in perspective we have to
bsalomon@google.com91961302011-05-09 18:39:58 +00001762 // do an explicit divide. Otherwise, we can use a Proj func.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001763 if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
1764 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001765 texFunc.append("Proj");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001766 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001767 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001768 fsCoordName = "inCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +00001769 fsCoordName.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001770 segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001771 GrGLShaderVar::TypeString(float_vector_type(coordDims)),
1772 fsCoordName.c_str(),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001773 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001774 vector_nonhomog_coords(varyingDims),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001775 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001776 vector_homog_coord(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001777 }
1778 }
1779
bsalomon@google.comfc296292011-05-06 13:53:47 +00001780 GrStringBuilder sampleCoords;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001781 bool complexCoord = false;
junov@google.comf93e7172011-03-31 21:26:24 +00001782 switch (desc.fCoordMapping) {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001783 case StageDesc::kIdentity_CoordMapping:
junov@google.comf93e7172011-03-31 21:26:24 +00001784 sampleCoords = fsCoordName;
1785 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001786 case StageDesc::kSweepGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001787 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 +00001788 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001789 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001790 case StageDesc::kRadialGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001791 sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001792 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001793 break;
tomhudson@google.com5960d002011-10-19 20:21:48 +00001794 case StageDesc::kRadial2Gradient_CoordMapping:
1795 complexCoord = genRadial2GradientCoordMapping(
1796 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001797 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001798 sampleCoords, fsCoordName,
1799 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001800
tomhudson@google.com5960d002011-10-19 20:21:48 +00001801 break;
1802 case StageDesc::kRadial2GradientDegenerate_CoordMapping:
1803 complexCoord = genRadial2GradientDegenerateCoordMapping(
1804 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001805 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001806 sampleCoords, fsCoordName,
1807 varyingDims, coordDims);
1808 break;
junov@google.comf93e7172011-03-31 21:26:24 +00001809
junov@google.comf93e7172011-03-31 21:26:24 +00001810 };
1811
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001812 const char* swizzle = "";
bsalomon@google.com74b98712011-11-11 19:46:16 +00001813 if (desc.fInConfigFlags & StageDesc::kSwapRAndB_InConfigFlag) {
1814 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag));
1815 swizzle = ".bgra";
1816 } else if (desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag) {
1817 GrAssert(!(desc.fInConfigFlags &
1818 StageDesc::kMulRGBByAlpha_InConfigFlag));
1819 swizzle = ".aaaa";
1820 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001821
bsalomon@google.com91961302011-05-09 18:39:58 +00001822 GrStringBuilder modulate;
1823 if (NULL != fsInColor) {
1824 modulate.printf(" * %s", fsInColor);
1825 }
1826
tomhudson@google.com0d831722011-06-02 15:37:14 +00001827 if (desc.fOptFlags &
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001828 StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001829 GrStringBuilder texDomainName;
1830 tex_domain_name(stageNum, &texDomainName);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001831 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
1832 GrGLShaderVar::kUniform_TypeModifier, texDomainName);
junov@google.com6acc9b32011-05-16 18:32:07 +00001833 GrStringBuilder coordVar("clampCoord");
1834 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001835 float_vector_type_str(coordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001836 coordVar.c_str(),
1837 sampleCoords.c_str(),
1838 texDomainName.c_str(),
1839 texDomainName.c_str());
1840 sampleCoords = coordVar;
1841 locations->fTexDomUni = kUseUniform;
1842 }
1843
tomhudson@google.com5960d002011-10-19 20:21:48 +00001844 switch (desc.fFetchMode) {
1845 case StageDesc::k2x2_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001846 GrAssert(!(desc.fInConfigFlags &
1847 StageDesc::kMulRGBByAlpha_InConfigFlag));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001848 gen2x2FS(stageNum, segments, locations, &sampleCoords,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001849 samplerName, texelSizeName, swizzle, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001850 texFunc, modulate, complexCoord, coordDims);
1851 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001852 case StageDesc::kConvolution_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001853 GrAssert(!(desc.fInConfigFlags &
1854 StageDesc::kMulRGBByAlpha_InConfigFlag));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001855 genConvolutionFS(stageNum, desc, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001856 samplerName, kernel, swizzle, imageIncrementName, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001857 sampleCoords, texFunc, modulate);
1858 break;
1859 default:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001860 if (desc.fInConfigFlags & StageDesc::kMulRGBByAlpha_InConfigFlag) {
1861 GrAssert(!(desc.fInConfigFlags &
1862 StageDesc::kSmearAlpha_InConfigFlag));
1863 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
1864 fsOutColor, texFunc.c_str(),
1865 samplerName, sampleCoords.c_str(),
1866 swizzle);
1867 segments->fFSCode.appendf("\t%s = vec4(%s.rgb*%s.a,%s.a)%s;\n",
1868 fsOutColor, fsOutColor, fsOutColor,
1869 fsOutColor, modulate.c_str());
1870 } else {
1871 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n",
1872 fsOutColor, texFunc.c_str(),
1873 samplerName, sampleCoords.c_str(),
1874 swizzle, modulate.c_str());
1875 }
junov@google.comf93e7172011-03-31 21:26:24 +00001876 }
junov@google.comf93e7172011-03-31 21:26:24 +00001877}
tomhudson@google.com5960d002011-10-19 20:21:48 +00001878
tomhudson@google.com5960d002011-10-19 20:21:48 +00001879