blob: ce87b857af9f7132ac1c91b1bdf7d31da4d63431 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
junov@google.comf93e7172011-03-31 21:26:24 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
junov@google.comf93e7172011-03-31 21:26:24 +000010#include "GrGLProgram.h"
11
bsalomon@google.com4fa66942011-09-20 19:06:12 +000012#include "GrAllocator.h"
13#include "GrGLShaderVar.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000014#include "SkTrace.h"
Scroggo97c88c22011-05-11 14:05:25 +000015#include "SkXfermode.h"
16
junov@google.comf93e7172011-03-31 21:26:24 +000017namespace {
18
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
25
bsalomon@google.com0b77d682011-08-19 13:28:54 +000026const char* GrPrecision(const GrGLInterface* gl) {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +000027 if (gl->supportsES2()) {
junov@google.comf93e7172011-03-31 21:26:24 +000028 return "mediump";
29 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +000030 return " ";
junov@google.comf93e7172011-03-31 21:26:24 +000031 }
32}
33
bsalomon@google.com0b77d682011-08-19 13:28:54 +000034const char* GrShaderPrecision(const GrGLInterface* gl) {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +000035 if (gl->supportsES2()) {
junov@google.comf93e7172011-03-31 21:26:24 +000036 return "precision mediump float;\n";
37 } else {
38 return "";
39 }
40}
41
42} // namespace
43
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000044#define PRINT_SHADERS 0
45
bsalomon@google.com4fa66942011-09-20 19:06:12 +000046typedef GrTAllocator<GrGLShaderVar> VarArray;
47
48// number of each input/output type in a single allocation block
49static const int gVarsPerBlock = 8;
50// except FS outputs where we expect 2 at most.
51static const int gMaxFSOutputs = 2;
52
53struct ShaderCodeSegments {
54 ShaderCodeSegments()
55 : fVSUnis(gVarsPerBlock)
56 , fVSAttrs(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000057 , fVSOutputs(gVarsPerBlock)
58 , fGSInputs(gVarsPerBlock)
59 , fGSOutputs(gVarsPerBlock)
60 , fFSInputs(gVarsPerBlock)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000061 , fFSUnis(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000062 , fFSOutputs(gMaxFSOutputs)
63 , fUsesGS(false) {}
bsalomon@google.com4fa66942011-09-20 19:06:12 +000064 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
65 VarArray fVSUnis;
66 VarArray fVSAttrs;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000067 VarArray fVSOutputs;
68 VarArray fGSInputs;
69 VarArray fGSOutputs;
70 VarArray fFSInputs;
71 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
bsalomon@google.com4fa66942011-09-20 19:06:12 +000072 VarArray fFSUnis;
73 VarArray fFSOutputs;
74 GrStringBuilder fFSFunctions;
75 GrStringBuilder fVSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000076 GrStringBuilder fGSCode;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000077 GrStringBuilder fFSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000078
79 bool fUsesGS;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000080};
81
bsalomon@google.com85b505b2011-11-07 14:56:51 +000082typedef GrGLProgram::ProgramDesc::StageDesc StageDesc;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000083
bsalomon@google.com4be283f2011-04-19 21:15:09 +000084#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +000085 #define VIEW_MATRIX_NAME "aViewM"
86#else
87 #define VIEW_MATRIX_NAME "uViewM"
88#endif
89
90#define POS_ATTR_NAME "aPosition"
91#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000092#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000093#define EDGE_ATTR_NAME "aEdge"
bsalomon@google.com4be283f2011-04-19 21:15:09 +000094#define COL_UNI_NAME "uColor"
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000095#define EDGES_UNI_NAME "uEdges"
Scroggo97c88c22011-05-11 14:05:25 +000096#define COL_FILTER_UNI_NAME "uColorFilter"
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +000097#define COL_MATRIX_UNI_NAME "uColorMatrix"
98#define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec"
junov@google.comf93e7172011-03-31 21:26:24 +000099
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000100namespace {
101inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000102 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000103 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +0000104}
105
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000106inline GrGLShaderVar::Type float_vector_type(int count) {
107 GR_STATIC_ASSERT(GrGLShaderVar::kFloat_Type == 0);
108 GR_STATIC_ASSERT(GrGLShaderVar::kVec2f_Type == 1);
109 GR_STATIC_ASSERT(GrGLShaderVar::kVec3f_Type == 2);
110 GR_STATIC_ASSERT(GrGLShaderVar::kVec4f_Type == 3);
111 GrAssert(count > 0 && count <= 4);
112 return (GrGLShaderVar::Type)(count - 1);
junov@google.comf93e7172011-03-31 21:26:24 +0000113}
114
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000115inline const char* float_vector_type_str(int count) {
116 return GrGLShaderVar::TypeString(float_vector_type(count));
117}
118
119inline const char* vector_homog_coord(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000120 static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"};
121 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS));
122 return HOMOGS[count];
123}
124
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000125inline const char* vector_nonhomog_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000126 static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"};
127 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS));
128 return NONHOMOGS[count];
129}
130
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000131inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000132 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
133 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
134 return ALL[count];
135}
136
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000137inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000138 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000139 "vec3(1,1,1)", "vec4(1,1,1,1)"};
140 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
141 return ONESVEC[count];
142}
143
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000144inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000145 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000146 "vec3(0,0,0)", "vec4(0,0,0,0)"};
147 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
148 return ZEROSVEC[count];
149}
150
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000151inline const char* declared_color_output_name() { return "fsColorOut"; }
152inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000153
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000154inline void tex_matrix_name(int stage, GrStringBuilder* s) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000155#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +0000156 *s = "aTexM";
157#else
158 *s = "uTexM";
159#endif
bsalomon@google.comfc296292011-05-06 13:53:47 +0000160 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000161}
162
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000163inline void normalized_texel_size_name(int stage, GrStringBuilder* s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000164 *s = "uTexelSize";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000165 s->appendS32(stage);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000166}
167
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000168inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000169 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000170 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000171}
172
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000173inline void radial2_param_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000174 *s = "uRadial2Params";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000175 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000176}
177
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000178inline void convolve_param_names(int stage, GrStringBuilder* k, GrStringBuilder* i) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000179 *k = "uKernel";
180 k->appendS32(stage);
181 *i = "uImageIncrement";
182 i->appendS32(stage);
183}
184
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000185inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000186 *s = "uTexDom";
187 s->appendS32(stage);
188}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000189}
junov@google.com6acc9b32011-05-16 18:32:07 +0000190
junov@google.comf93e7172011-03-31 21:26:24 +0000191GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000192}
193
194GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000195}
196
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000197void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000198 GrBlendCoeff* dstCoeff) const {
199 switch (fProgramDesc.fDualSrcOutput) {
200 case ProgramDesc::kNone_DualSrcOutput:
201 break;
202 // the prog will write a coverage value to the secondary
203 // output and the dst is blended by one minus that value.
204 case ProgramDesc::kCoverage_DualSrcOutput:
205 case ProgramDesc::kCoverageISA_DualSrcOutput:
206 case ProgramDesc::kCoverageISC_DualSrcOutput:
207 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff;
208 break;
209 default:
210 GrCrash("Unexpected dual source blend output");
211 break;
212 }
213}
214
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000215// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000216// vars can be vec4s or floats (or one of each)
217// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000218// if either var is "" then assign to the other var
219// if both are "" then assign all ones
220static inline void modulate_helper(const char* outputVar,
221 const char* var0,
222 const char* var1,
223 GrStringBuilder* code) {
224 GrAssert(NULL != outputVar);
225 GrAssert(NULL != var0);
226 GrAssert(NULL != var1);
227 GrAssert(NULL != code);
228
229 bool has0 = '\0' != *var0;
230 bool has1 = '\0' != *var1;
231
232 if (!has0 && !has1) {
233 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
234 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000235 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000236 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000237 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000238 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000239 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000240 }
241}
242
243// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000244// vars can be vec4s or floats (or one of each)
245// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000246// if either var is "" then assign to the other var
247// if both are "" then assign all zeros
248static inline void add_helper(const char* outputVar,
249 const char* var0,
250 const char* var1,
251 GrStringBuilder* code) {
252 GrAssert(NULL != outputVar);
253 GrAssert(NULL != var0);
254 GrAssert(NULL != var1);
255 GrAssert(NULL != code);
256
257 bool has0 = '\0' != *var0;
258 bool has1 = '\0' != *var1;
259
260 if (!has0 && !has1) {
261 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
262 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000263 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000264 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000265 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000266 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000267 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000268 }
269}
270
271// given two blend coeffecients determine whether the src
272// and/or dst computation can be omitted.
273static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
274 SkXfermode::Coeff dstCoeff,
275 bool* needSrcValue,
276 bool* needDstValue) {
277 if (SkXfermode::kZero_Coeff == srcCoeff) {
278 switch (dstCoeff) {
279 // these all read the src
280 case SkXfermode::kSC_Coeff:
281 case SkXfermode::kISC_Coeff:
282 case SkXfermode::kSA_Coeff:
283 case SkXfermode::kISA_Coeff:
284 *needSrcValue = true;
285 break;
286 default:
287 *needSrcValue = false;
288 break;
289 }
290 } else {
291 *needSrcValue = true;
292 }
293 if (SkXfermode::kZero_Coeff == dstCoeff) {
294 switch (srcCoeff) {
295 // these all read the dst
296 case SkXfermode::kDC_Coeff:
297 case SkXfermode::kIDC_Coeff:
298 case SkXfermode::kDA_Coeff:
299 case SkXfermode::kIDA_Coeff:
300 *needDstValue = true;
301 break;
302 default:
303 *needDstValue = false;
304 break;
305 }
306 } else {
307 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000308 }
309}
310
311/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000312 * Create a blend_coeff * value string to be used in shader code. Sets empty
313 * string if result is trivially zero.
314 */
315static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
316 const char* src, const char* dst,
317 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000318 switch (coeff) {
319 case SkXfermode::kZero_Coeff: /** 0 */
320 *str = "";
321 break;
322 case SkXfermode::kOne_Coeff: /** 1 */
323 *str = value;
324 break;
325 case SkXfermode::kSC_Coeff:
326 str->printf("(%s * %s)", src, value);
327 break;
328 case SkXfermode::kISC_Coeff:
329 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
330 break;
331 case SkXfermode::kDC_Coeff:
332 str->printf("(%s * %s)", dst, value);
333 break;
334 case SkXfermode::kIDC_Coeff:
335 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
336 break;
337 case SkXfermode::kSA_Coeff: /** src alpha */
338 str->printf("(%s.a * %s)", src, value);
339 break;
340 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
341 str->printf("((1.0 - %s.a) * %s)", src, value);
342 break;
343 case SkXfermode::kDA_Coeff: /** dst alpha */
344 str->printf("(%s.a * %s)", dst, value);
345 break;
346 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
347 str->printf("((1.0 - %s.a) * %s)", dst, value);
348 break;
349 default:
350 GrCrash("Unexpected xfer coeff.");
351 break;
352 }
353}
354/**
Scroggo97c88c22011-05-11 14:05:25 +0000355 * Adds a line to the fragment shader code which modifies the color by
356 * the specified color filter.
357 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000358static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000359 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000360 SkXfermode::Coeff colorCoeff,
361 const char* inColor) {
362 GrStringBuilder colorStr, constStr;
363 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
364 inColor, inColor);
365 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
366 inColor, COL_FILTER_UNI_NAME);
367
368 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000369}
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000370/**
371 * Adds code to the fragment shader code which modifies the color by
372 * the specified color matrix.
373 */
374static void addColorMatrix(GrStringBuilder* fsCode, const char * outputVar,
375 const char* inColor) {
376 fsCode->appendf("%s = %s * %s + %s;\n", outputVar, COL_MATRIX_UNI_NAME, inColor, COL_MATRIX_VEC_UNI_NAME);
377}
Scroggo97c88c22011-05-11 14:05:25 +0000378
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000379namespace {
380
381const char* glsl_version_string(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000382 GrGLSLGeneration v) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000383 switch (v) {
tomhudson@google.com086e5352011-12-08 14:44:10 +0000384 case k110_GLSLGeneration:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000385 if (gl->supportsES2()) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000386 // ES2s shader language is based on version 1.20 but is version
387 // 1.00 of the ES language.
388 return "#version 100\n";
389 } else {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000390 return "#version 110\n";
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000391 }
tomhudson@google.com086e5352011-12-08 14:44:10 +0000392 case k130_GLSLGeneration:
bsalomon@google.com4c5de582011-11-14 19:35:08 +0000393 GrAssert(!gl->supportsES2());
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000394 return "#version 130\n";
tomhudson@google.com086e5352011-12-08 14:44:10 +0000395 case k150_GLSLGeneration:
bsalomon@google.com4c5de582011-11-14 19:35:08 +0000396 GrAssert(!gl->supportsES2());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000397 return "#version 150\n";
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000398 default:
399 GrCrash("Unknown GL version.");
400 return ""; // suppress warning
401 }
402}
403
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000404// Adds a var that is computed in the VS and read in FS.
405// If there is a GS it will just pass it through.
406void append_varying(GrGLShaderVar::Type type,
407 const char* name,
408 ShaderCodeSegments* segments,
409 const char** vsOutName = NULL,
410 const char** fsInName = NULL) {
411 segments->fVSOutputs.push_back();
412 segments->fVSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000413 segments->fVSOutputs.back().setTypeModifier(
414 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000415 segments->fVSOutputs.back().accessName()->printf("v%s", name);
416 if (vsOutName) {
417 *vsOutName = segments->fVSOutputs.back().getName().c_str();
418 }
419 // input to FS comes either from VS or GS
420 const GrStringBuilder* fsName;
421 if (segments->fUsesGS) {
422 // if we have a GS take each varying in as an array
423 // and output as non-array.
424 segments->fGSInputs.push_back();
425 segments->fGSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000426 segments->fGSInputs.back().setTypeModifier(
427 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000428 segments->fGSInputs.back().setUnsizedArray();
429 *segments->fGSInputs.back().accessName() =
430 segments->fVSOutputs.back().getName();
431 segments->fGSOutputs.push_back();
432 segments->fGSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000433 segments->fGSOutputs.back().setTypeModifier(
434 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000435 segments->fGSOutputs.back().accessName()->printf("g%s", name);
436 fsName = segments->fGSOutputs.back().accessName();
437 } else {
438 fsName = segments->fVSOutputs.back().accessName();
439 }
440 segments->fFSInputs.push_back();
441 segments->fFSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000442 segments->fFSInputs.back().setTypeModifier(
443 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000444 segments->fFSInputs.back().setName(*fsName);
445 if (fsInName) {
446 *fsInName = fsName->c_str();
447 }
448}
449
450// version of above that adds a stage number to the
451// the var name (for uniqueness)
452void append_varying(GrGLShaderVar::Type type,
453 const char* name,
454 int stageNum,
455 ShaderCodeSegments* segments,
456 const char** vsOutName = NULL,
457 const char** fsInName = NULL) {
458 GrStringBuilder nameWithStage(name);
459 nameWithStage.appendS32(stageNum);
460 append_varying(type, nameWithStage.c_str(), segments, vsOutName, fsInName);
461}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000462}
463
bsalomon@google.com66105672011-09-15 15:12:00 +0000464void GrGLProgram::genEdgeCoverage(const GrGLInterface* gl,
465 GrVertexLayout layout,
466 CachedData* programData,
467 GrStringBuilder* coverageVar,
468 ShaderCodeSegments* segments) const {
469 if (fProgramDesc.fEdgeAANumEdges > 0) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000470 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec3f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000471 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000472 EDGES_UNI_NAME,
473 fProgramDesc.fEdgeAANumEdges);
bsalomon@google.com66105672011-09-15 15:12:00 +0000474 programData->fUniLocations.fEdgesUni = kUseUniform;
475 int count = fProgramDesc.fEdgeAANumEdges;
476 segments->fFSCode.append(
477 "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n");
478 for (int i = 0; i < count; i++) {
479 segments->fFSCode.append("\tfloat a");
480 segments->fFSCode.appendS32(i);
481 segments->fFSCode.append(" = clamp(dot(" EDGES_UNI_NAME "[");
482 segments->fFSCode.appendS32(i);
483 segments->fFSCode.append("], pos), 0.0, 1.0);\n");
484 }
485 if (fProgramDesc.fEdgeAAConcave && (count & 0x01) == 0) {
486 // For concave polys, we consider the edges in pairs.
487 segments->fFSFunctions.append("float cross2(vec2 a, vec2 b) {\n");
488 segments->fFSFunctions.append("\treturn dot(a, vec2(b.y, -b.x));\n");
489 segments->fFSFunctions.append("}\n");
490 for (int i = 0; i < count; i += 2) {
491 segments->fFSCode.appendf("\tfloat eb%d;\n", i / 2);
492 segments->fFSCode.appendf("\tif (cross2(" EDGES_UNI_NAME "[%d].xy, " EDGES_UNI_NAME "[%d].xy) < 0.0) {\n", i, i + 1);
493 segments->fFSCode.appendf("\t\teb%d = a%d * a%d;\n", i / 2, i, i + 1);
494 segments->fFSCode.append("\t} else {\n");
495 segments->fFSCode.appendf("\t\teb%d = a%d + a%d - a%d * a%d;\n", i / 2, i, i + 1, i, i + 1);
496 segments->fFSCode.append("\t}\n");
497 }
498 segments->fFSCode.append("\tfloat edgeAlpha = ");
499 for (int i = 0; i < count / 2 - 1; i++) {
500 segments->fFSCode.appendf("min(eb%d, ", i);
501 }
502 segments->fFSCode.appendf("eb%d", count / 2 - 1);
503 for (int i = 0; i < count / 2 - 1; i++) {
504 segments->fFSCode.append(")");
505 }
506 segments->fFSCode.append(";\n");
507 } else {
508 segments->fFSCode.append("\tfloat edgeAlpha = ");
509 for (int i = 0; i < count - 1; i++) {
510 segments->fFSCode.appendf("min(a%d * a%d, ", i, i + 1);
511 }
512 segments->fFSCode.appendf("a%d * a0", count - 1);
513 for (int i = 0; i < count - 1; i++) {
514 segments->fFSCode.append(")");
515 }
516 segments->fFSCode.append(";\n");
517 }
518 *coverageVar = "edgeAlpha";
519 } else if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000520 const char *vsName, *fsName;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000521 append_varying(GrGLShaderVar::kVec4f_Type, "Edge", segments,
522 &vsName, &fsName);
523 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
524 GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000525 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
tomhudson@google.com93813632011-10-27 20:21:16 +0000526 if (GrDrawState::kHairLine_EdgeType == fProgramDesc.fVertexEdgeType) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000527 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com66105672011-09-15 15:12:00 +0000528 } else {
tomhudson@google.com93813632011-10-27 20:21:16 +0000529 GrAssert(GrDrawState::kHairQuad_EdgeType == fProgramDesc.fVertexEdgeType);
bsalomon@google.com66105672011-09-15 15:12:00 +0000530 // for now we know we're not in perspective, so we could compute this
531 // per-quadratic rather than per pixel
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000532 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
533 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
534 segments->fFSCode.appendf("\tfloat dfdx = 2.0*%s.x*duvdx.x - duvdx.y;\n", fsName);
535 segments->fFSCode.appendf("\tfloat dfdy = 2.0*%s.x*duvdy.x - duvdy.y;\n", fsName);
536 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com66105672011-09-15 15:12:00 +0000537 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / (dfdx*dfdx + dfdy*dfdy));\n");
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000538 if (gl->supportsES2()) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000539 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
540 }
541 }
542 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
543 *coverageVar = "edgeAlpha";
544 } else {
545 coverageVar->reset();
546 }
547}
548
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000549namespace {
550
551// returns true if the color output was explicitly declared or not.
tomhudson@google.com086e5352011-12-08 14:44:10 +0000552bool decl_and_get_fs_color_output(GrGLSLGeneration v,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000553 VarArray* fsOutputs,
554 const char** name) {
555 switch (v) {
tomhudson@google.com086e5352011-12-08 14:44:10 +0000556 case k110_GLSLGeneration:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000557 *name = "gl_FragColor";
558 return false;
559 break;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000560 case k130_GLSLGeneration: // fallthru
561 case k150_GLSLGeneration:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000562 *name = declared_color_output_name();
563 fsOutputs->push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000564 GrGLShaderVar::kOut_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000565 declared_color_output_name());
566 return true;
567 break;
568 default:
569 GrCrash("Unknown GLSL version.");
570 return false; // suppress warning
571 }
572}
573
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000574void genInputColor(GrGLProgram::ProgramDesc::ColorInput colorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000575 GrGLProgram::CachedData* programData,
576 ShaderCodeSegments* segments,
577 GrStringBuilder* inColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000578 switch (colorInput) {
579 case GrGLProgram::ProgramDesc::kAttribute_ColorInput: {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000580 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000581 GrGLShaderVar::kAttribute_TypeModifier,
582 COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000583 const char *vsName, *fsName;
584 append_varying(GrGLShaderVar::kVec4f_Type, "Color", segments, &vsName, &fsName);
585 segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
586 *inColor = fsName;
587 } break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000588 case GrGLProgram::ProgramDesc::kUniform_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000589 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000590 GrGLShaderVar::kUniform_TypeModifier,
591 COL_UNI_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000592 programData->fUniLocations.fColorUni = kUseUniform;
593 *inColor = COL_UNI_NAME;
594 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000595 case GrGLProgram::ProgramDesc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000596 GrAssert(!"needComputedColor should be false.");
597 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000598 case GrGLProgram::ProgramDesc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000599 break;
600 default:
601 GrCrash("Unknown color type.");
602 break;
603 }
604}
605
606void genPerVertexCoverage(ShaderCodeSegments* segments,
607 GrStringBuilder* inCoverage) {
608 segments->fVSAttrs.push_back().set(GrGLShaderVar::kFloat_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000609 GrGLShaderVar::kAttribute_TypeModifier,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000610 COV_ATTR_NAME);
611 const char *vsName, *fsName;
612 append_varying(GrGLShaderVar::kFloat_Type, "Coverage",
613 segments, &vsName, &fsName);
614 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
615 if (inCoverage->size()) {
616 segments->fFSCode.appendf("\tfloat edgeAndAttrCov = %s * %s;\n",
617 fsName, inCoverage->c_str());
618 *inCoverage = "edgeAndAttrCov";
619 } else {
620 *inCoverage = fsName;
621 }
622}
623
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000624}
625
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000626void GrGLProgram::genGeometryShader(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000627 GrGLSLGeneration glslGeneration,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000628 ShaderCodeSegments* segments) const {
629#if GR_GL_EXPERIMENTAL_GS
630 if (fProgramDesc.fExperimentalGS) {
tomhudson@google.com086e5352011-12-08 14:44:10 +0000631 GrAssert(glslGeneration >= k150_GLSLGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000632 segments->fGSHeader.append("layout(triangles) in;\n"
633 "layout(triangle_strip, max_vertices = 6) out;\n");
634 segments->fGSCode.append("void main() {\n"
635 "\tfor (int i = 0; i < 3; ++i) {\n"
636 "\t\tgl_Position = gl_in[i].gl_Position;\n");
637 if (this->fProgramDesc.fEmitsPointSize) {
638 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
639 }
640 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
641 int count = segments->fGSInputs.count();
642 for (int i = 0; i < count; ++i) {
643 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
644 segments->fGSOutputs[i].getName().c_str(),
645 segments->fGSInputs[i].getName().c_str());
646 }
647 segments->fGSCode.append("\t\tEmitVertex();\n"
648 "\t}\n"
649 "\tEndPrimitive();\n"
650 "}\n");
651 }
652#endif
653}
654
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000655bool GrGLProgram::genProgram(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000656 GrGLSLGeneration glslGeneration,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000657 GrGLProgram::CachedData* programData) const {
junov@google.comf93e7172011-03-31 21:26:24 +0000658
659 ShaderCodeSegments segments;
660 const uint32_t& layout = fProgramDesc.fVertexLayout;
661
bsalomon@google.com91961302011-05-09 18:39:58 +0000662 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000663
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000664#if GR_GL_EXPERIMENTAL_GS
665 segments.fUsesGS = fProgramDesc.fExperimentalGS;
666#endif
667
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000668 SkXfermode::Coeff colorCoeff, uniformCoeff;
669 // The rest of transfer mode color filters have not been implemented
670 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
671 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000672 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
673 (fProgramDesc.fColorFilterXfermode),
674 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000675 GR_DEBUGASSERT(success);
676 } else {
677 colorCoeff = SkXfermode::kOne_Coeff;
678 uniformCoeff = SkXfermode::kZero_Coeff;
679 }
680
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000681 // If we know the final color is going to be all zeros then we can
682 // simplify the color filter coeffecients. needComputedColor will then
683 // come out false below.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000684 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000685 colorCoeff = SkXfermode::kZero_Coeff;
686 if (SkXfermode::kDC_Coeff == uniformCoeff ||
687 SkXfermode::kDA_Coeff == uniformCoeff) {
688 uniformCoeff = SkXfermode::kZero_Coeff;
689 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
690 SkXfermode::kIDA_Coeff == uniformCoeff) {
691 uniformCoeff = SkXfermode::kOne_Coeff;
692 }
693 }
694
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000695 bool needColorFilterUniform;
696 bool needComputedColor;
697 needBlendInputs(uniformCoeff, colorCoeff,
698 &needColorFilterUniform, &needComputedColor);
699
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000700 // the dual source output has no canonical var name, have to
701 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000702 const char* fsColorOutput = NULL;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000703 bool dualSourceOutputWritten = false;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000704 segments.fHeader.printf(glsl_version_string(gl, glslGeneration));
705 bool isColorDeclared = decl_and_get_fs_color_output(glslGeneration,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000706 &segments.fFSOutputs,
707 &fsColorOutput);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000708
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000709#if GR_GL_ATTRIBUTE_MATRICES
tomhudson@google.com086e5352011-12-08 14:44:10 +0000710 segments.fVSAttrs.push_back().set(GrGLShaderVar::kMat33f_Type,
711 GrGLShaderVar::kAttribute_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000712 programData->fUniLocations.fViewMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +0000713#else
tomhudson@google.com086e5352011-12-08 14:44:10 +0000714 segments.fVSUnis.push_back().set(GrGLShaderVar::kMat33f_Type,
715 GrGLShaderVar::kUniform_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000716 programData->fUniLocations.fViewMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +0000717#endif
tomhudson@google.com086e5352011-12-08 14:44:10 +0000718 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type,
719 GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000720
bsalomon@google.com91961302011-05-09 18:39:58 +0000721 segments.fVSCode.append(
722 "void main() {\n"
723 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
724 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000725
bsalomon@google.com91961302011-05-09 18:39:58 +0000726 // incoming color to current stage being processed.
727 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000728
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000729 if (needComputedColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000730 genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000731 programData, &segments, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000732 }
733
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000734 // we output point size in the GS if present
735 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000736 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000737 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000738
bsalomon@google.com91961302011-05-09 18:39:58 +0000739 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000740
741 // add texture coordinates that are used to the list of vertex attr decls
tomhudson@google.com93813632011-10-27 20:21:16 +0000742 GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords];
743 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000744 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000745 tex_attr_name(t, texCoordAttrs + t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000746 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000747 GrGLShaderVar::kAttribute_TypeModifier,
748 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000749 }
750 }
751
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000752 ///////////////////////////////////////////////////////////////////////////
753 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000754
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000755 // if we have color stages string them together, feeding the output color
756 // of each to the next and generating code for each stage.
757 if (needComputedColor) {
758 GrStringBuilder outColor;
759 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000760 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000761 // create var to hold stage result
762 outColor = "color";
763 outColor.appendS32(s);
764 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
765
766 const char* inCoords;
767 // figure out what our input coords are
768 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
769 layout) {
770 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000771 } else {
772 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
773 // we better have input tex coordinates if stage is enabled.
774 GrAssert(tcIdx >= 0);
775 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000776 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000777 }
778
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000779 this->genStageCode(gl,
780 s,
781 fProgramDesc.fStages[s],
782 inColor.size() ? inColor.c_str() : NULL,
783 outColor.c_str(),
784 inCoords,
785 &segments,
786 &programData->fUniLocations.fStages[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000787 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000788 }
789 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000790 }
Scroggo97c88c22011-05-11 14:05:25 +0000791
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000792 // if have all ones or zeros for the "dst" input to the color filter then we
793 // may be able to make additional optimizations.
794 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000795 GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000796 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
797 SkXfermode::kIDA_Coeff == uniformCoeff;
798 if (uniformCoeffIsZero) {
799 uniformCoeff = SkXfermode::kZero_Coeff;
800 bool bogus;
801 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
802 &needColorFilterUniform, &bogus);
803 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000804 }
805 if (needColorFilterUniform) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000806 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000807 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000808 COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000809 programData->fUniLocations.fColorFilterUni = kUseUniform;
810 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000811 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000812 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000813 SkXfermode::kZero_Coeff == colorCoeff &&
814 !fProgramDesc.fColorMatrixEnabled) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000815 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000816 fsColorOutput,
817 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000818 wroteFragColorZero = true;
819 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
820 segments.fFSCode.appendf("\tvec4 filteredColor;\n");
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000821 const char* color;
822 if (inColor.size()) {
823 color = inColor.c_str();
824 } else {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000825 if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000826 color = all_ones_vec(4);
827 } else {
828 color = all_zeros_vec(4);
829 }
830 }
tomhudson@google.com0d831722011-06-02 15:37:14 +0000831 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000832 colorCoeff, color);
833 inColor = "filteredColor";
834 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000835 if (fProgramDesc.fColorMatrixEnabled) {
836 segments.fFSUnis.push_back().set(GrGLShaderVar::kMat44f_Type,
837 GrGLShaderVar::kUniform_TypeModifier,
838 COL_MATRIX_UNI_NAME);
839 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
840 GrGLShaderVar::kUniform_TypeModifier,
841 COL_MATRIX_VEC_UNI_NAME);
842 programData->fUniLocations.fColorMatrixUni = kUseUniform;
843 programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
844 segments.fFSCode.appendf("\tvec4 matrixedColor;\n");
845 addColorMatrix(&segments.fFSCode, "matrixedColor", inColor.c_str());
846 inColor = "matrixedColor";
847 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000848
849 ///////////////////////////////////////////////////////////////////////////
850 // compute the partial coverage (coverage stages and edge aa)
851
852 GrStringBuilder inCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000853
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000854 // we don't need to compute coverage at all if we know the final shader
855 // output will be zero and we don't have a dual src blend output.
856 if (!wroteFragColorZero ||
857 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000858
859 // get edge AA coverage and use it as inCoverage to first coverage stage
860 this->genEdgeCoverage(gl, layout, programData, &inCoverage, &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000861
bsalomon@google.coma3108262011-10-10 14:08:47 +0000862 // include explicit per-vertex coverage if we have it
863 if (GrDrawTarget::kCoverage_VertexLayoutBit & layout) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000864 genPerVertexCoverage(&segments, &inCoverage);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000865 }
866
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000867 GrStringBuilder outCoverage;
868 const int& startStage = fProgramDesc.fFirstCoverageStage;
tomhudson@google.com93813632011-10-27 20:21:16 +0000869 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000870 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000871 // create var to hold stage output
872 outCoverage = "coverage";
873 outCoverage.appendS32(s);
874 segments.fFSCode.appendf("\tvec4 %s;\n", outCoverage.c_str());
875
876 const char* inCoords;
877 // figure out what our input coords are
878 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & layout) {
879 inCoords = POS_ATTR_NAME;
880 } else {
881 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
882 // we better have input tex coordinates if stage is enabled.
883 GrAssert(tcIdx >= 0);
884 GrAssert(texCoordAttrs[tcIdx].size());
885 inCoords = texCoordAttrs[tcIdx].c_str();
886 }
887
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000888 genStageCode(gl, s,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000889 fProgramDesc.fStages[s],
890 inCoverage.size() ? inCoverage.c_str() : NULL,
891 outCoverage.c_str(),
892 inCoords,
893 &segments,
894 &programData->fUniLocations.fStages[s]);
895 inCoverage = outCoverage;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000896 }
897 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000898 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000899 segments.fFSOutputs.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000900 GrGLShaderVar::kOut_TypeModifier,
901 dual_source_output_name());
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000902 bool outputIsZero = false;
903 GrStringBuilder coeff;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000904 if (ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000905 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
906 if (!inColor.size()) {
907 outputIsZero = true;
908 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000909 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000910 ProgramDesc::kCoverageISA_DualSrcOutput) {
911 coeff.printf("(1 - %s.a)", inColor.c_str());
912 } else {
913 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
914 }
915 }
916 }
917 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000918 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000919 dual_source_output_name(),
920 all_zeros_vec(4));
921 } else {
922 modulate_helper(dual_source_output_name(),
923 coeff.c_str(),
924 inCoverage.c_str(),
925 &segments.fFSCode);
926 }
927 dualSourceOutputWritten = true;
928 }
junov@google.comf93e7172011-03-31 21:26:24 +0000929 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000930
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000931 ///////////////////////////////////////////////////////////////////////////
932 // combine color and coverage as frag color
933
934 if (!wroteFragColorZero) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000935 modulate_helper(fsColorOutput,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000936 inColor.c_str(),
937 inCoverage.c_str(),
938 &segments.fFSCode);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000939 if (ProgramDesc::kNo_OutputPM == fProgramDesc.fOutputPM) {
bsalomon@google.come63799d2011-11-09 14:14:38 +0000940 segments.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(%s.rgb / %s.a, %s.a);\n",
941 fsColorOutput,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000942 fsColorOutput,
943 fsColorOutput,
944 fsColorOutput,
945 fsColorOutput);
946 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000947 }
948
bsalomon@google.com91961302011-05-09 18:39:58 +0000949 segments.fVSCode.append("}\n");
950 segments.fFSCode.append("}\n");
951
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000952 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000953 // insert GS
954#if GR_DEBUG
tomhudson@google.com086e5352011-12-08 14:44:10 +0000955 this->genGeometryShader(gl, glslGeneration, &segments);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000956#endif
957
958 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000959 // compile and setup attribs and unis
960
tomhudson@google.com086e5352011-12-08 14:44:10 +0000961 if (!CompileShaders(gl, glslGeneration, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000962 return false;
963 }
964
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000965 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000966 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000967 dualSourceOutputWritten,
968 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000969 return false;
970 }
971
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000972 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000973
974 return true;
975}
976
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000977namespace {
978
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000979inline void expand_decls(const VarArray& vars,
980 const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000981 GrStringBuilder* string,
982 GrGLSLGeneration gen) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000983 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000984 for (int i = 0; i < count; ++i) {
tomhudson@google.com086e5352011-12-08 14:44:10 +0000985 vars[i].appendDecl(gl, string, gen);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000986 }
987}
988
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000989inline void print_shader(int stringCnt,
990 const char** strings,
991 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000992 for (int i = 0; i < stringCnt; ++i) {
993 if (NULL == stringLengths || stringLengths[i] < 0) {
994 GrPrintf(strings[i]);
995 } else {
996 GrPrintf("%.*s", stringLengths[i], strings[i]);
997 }
998 }
999}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001000
1001typedef SkTArray<const char*, true> StrArray;
1002#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
1003
1004typedef SkTArray<int, true> LengthArray;
1005#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
1006
1007// these shouldn't relocate
1008typedef GrTAllocator<GrStringBuilder> TempArray;
1009#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
1010
1011inline void append_string(const GrStringBuilder& str,
1012 StrArray* strings,
1013 LengthArray* lengths) {
1014 int length = (int) str.size();
1015 if (length) {
1016 strings->push_back(str.c_str());
1017 lengths->push_back(length);
1018 }
1019 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001020}
1021
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001022inline void append_decls(const VarArray& vars,
1023 const GrGLInterface* gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001024 StrArray* strings,
1025 LengthArray* lengths,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001026 TempArray* temp,
1027 GrGLSLGeneration gen) {
1028 expand_decls(vars, gl, &temp->push_back(), gen);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001029 append_string(temp->back(), strings, lengths);
1030}
1031
1032}
1033
1034bool GrGLProgram::CompileShaders(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001035 GrGLSLGeneration glslGeneration,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001036 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +00001037 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001038 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +00001039
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001040 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
1041 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
1042 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
1043
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001044 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001045 GrStringBuilder inputs;
1046 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001047
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001048 append_string(segments.fHeader, &strs, &lengths);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001049 append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps, glslGeneration);
1050 append_decls(segments.fVSAttrs, gl, &strs, &lengths,
1051 &temps, glslGeneration);
1052 append_decls(segments.fVSOutputs, gl, &strs, &lengths,
1053 &temps, glslGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001054 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001055
1056#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001057 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001058 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001059#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001060
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001061 programData->fVShaderID =
1062 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
1063 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001064
bsalomon@google.com91961302011-05-09 18:39:58 +00001065 if (!programData->fVShaderID) {
1066 return false;
1067 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001068 if (segments.fUsesGS) {
1069 strs.reset();
1070 lengths.reset();
1071 temps.reset();
1072 append_string(segments.fHeader, &strs, &lengths);
1073 append_string(segments.fGSHeader, &strs, &lengths);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001074 append_decls(segments.fGSInputs, gl, &strs, &lengths,
1075 &temps, glslGeneration);
1076 append_decls(segments.fGSOutputs, gl, &strs, &lengths,
1077 &temps, glslGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001078 append_string(segments.fGSCode, &strs, &lengths);
1079#if PRINT_SHADERS
1080 print_shader(strs.count(), &strs[0], &lengths[0]);
1081 GrPrintf("\n");
1082#endif
1083 programData->fGShaderID =
1084 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
1085 &strs[0], &lengths[0]);
1086 } else {
1087 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +00001088 }
junov@google.comf93e7172011-03-31 21:26:24 +00001089
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001090 strs.reset();
1091 lengths.reset();
1092 temps.reset();
1093
1094 append_string(segments.fHeader, &strs, &lengths);
1095 GrStringBuilder precisionStr(GrShaderPrecision(gl));
1096 append_string(precisionStr, &strs, &lengths);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001097 append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps, glslGeneration);
1098 append_decls(segments.fFSInputs, gl, &strs, &lengths,
1099 &temps, glslGeneration);
bsalomon@google.com373a6632011-10-19 20:43:20 +00001100 // We shouldn't have declared outputs on 1.10
tomhudson@google.com086e5352011-12-08 14:44:10 +00001101 GrAssert(k110_GLSLGeneration != glslGeneration ||
1102 segments.fFSOutputs.empty());
1103 append_decls(segments.fFSOutputs, gl, &strs, &lengths,
1104 &temps, glslGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001105 append_string(segments.fFSFunctions, &strs, &lengths);
1106 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001107
1108#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001109 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001110 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001111#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001112
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001113 programData->fFShaderID =
1114 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
1115 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001116
bsalomon@google.com91961302011-05-09 18:39:58 +00001117 if (!programData->fFShaderID) {
1118 return false;
junov@google.comf93e7172011-03-31 21:26:24 +00001119 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001120
bsalomon@google.com91961302011-05-09 18:39:58 +00001121 return true;
junov@google.comf93e7172011-03-31 21:26:24 +00001122}
1123
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001124GrGLuint GrGLProgram::CompileShader(const GrGLInterface* gl,
1125 GrGLenum type,
1126 int stringCnt,
1127 const char** strings,
1128 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001129 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
1130 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
1131
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001132 GrGLuint shader;
1133 GR_GL_CALL_RET(gl, shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +00001134 if (0 == shader) {
1135 return 0;
1136 }
1137
1138 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001139 GR_GL_CALL(gl, ShaderSource(shader, stringCnt, strings, stringLengths));
1140 GR_GL_CALL(gl, CompileShader(shader));
1141 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +00001142
1143 if (!compiled) {
1144 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001145 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001146 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001147 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001148 // retrieve length even though we don't need it to workaround
1149 // bug in chrome cmd buffer param validation.
1150 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001151 GR_GL_CALL(gl, GetShaderInfoLog(shader, infoLen+1,
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001152 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001153 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001154 GrPrintf("\n%s", log.get());
1155 }
1156 GrAssert(!"Shader compilation failed!");
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001157 GR_GL_CALL(gl, DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001158 return 0;
1159 }
1160 return shader;
1161}
1162
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001163bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001165 GrStringBuilder texCoordAttrNames[],
1166 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001167 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001168 CachedData* programData) const {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001169 GR_GL_CALL_RET(gl, programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001170 if (!programData->fProgramID) {
1171 return false;
1172 }
1173 const GrGLint& progID = programData->fProgramID;
1174
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001175 GR_GL_CALL(gl, AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001176 if (programData->fGShaderID) {
1177 GR_GL_CALL(gl, AttachShader(progID, programData->fGShaderID));
1178 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001179 GR_GL_CALL(gl, AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001180
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001181 if (bindColorOut) {
bsalomon@google.combc5cf512011-09-21 16:21:07 +00001182 GR_GL_CALL(gl, BindFragDataLocation(programData->fProgramID,
1183 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001184 }
1185 if (bindDualSrcOut) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001186 GR_GL_CALL(gl, BindFragDataLocationIndexed(programData->fProgramID,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001187 0, 1, dual_source_output_name()));
1188 }
1189
bsalomon@google.com91961302011-05-09 18:39:58 +00001190 // Bind the attrib locations to same values for all shaders
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001191 GR_GL_CALL(gl, BindAttribLocation(progID, PositionAttributeIdx(),
1192 POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +00001193 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001194 if (texCoordAttrNames[t].size()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001195 GR_GL_CALL(gl, BindAttribLocation(progID,
1196 TexCoordAttributeIdx(t),
1197 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001198 }
1199 }
1200
bsalomon@google.com91961302011-05-09 18:39:58 +00001201 if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001202 GR_GL_CALL(gl, BindAttribLocation(progID,
1203 ViewMatrixAttributeIdx(),
1204 VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001205 }
1206
tomhudson@google.com93813632011-10-27 20:21:16 +00001207 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com72b4fcb2011-05-09 20:47:34 +00001208 const StageUniLocations& unis = programData->fUniLocations.fStages[s];
1209 if (kSetAsAttribute == unis.fTextureMatrixUni) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001210 GrStringBuilder matName;
1211 tex_matrix_name(s, &matName);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001212 GR_GL_CALL(gl, BindAttribLocation(progID,
1213 TextureMatrixAttributeIdx(s),
1214 matName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001215 }
1216 }
1217
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001218 GR_GL_CALL(gl, BindAttribLocation(progID, ColorAttributeIdx(),
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001219 COL_ATTR_NAME));
bsalomon@google.coma3108262011-10-10 14:08:47 +00001220 GR_GL_CALL(gl, BindAttribLocation(progID, CoverageAttributeIdx(),
1221 COV_ATTR_NAME));
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001222 GR_GL_CALL(gl, BindAttribLocation(progID, EdgeAttributeIdx(),
1223 EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001224
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001225 GR_GL_CALL(gl, LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001226
1227 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001228 GR_GL_CALL(gl, GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001229 if (!linked) {
1230 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001231 GR_GL_CALL(gl, GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001232 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001233 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001234 // retrieve length even though we don't need it to workaround
1235 // bug in chrome cmd buffer param validation.
1236 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001237 GR_GL_CALL(gl, GetProgramInfoLog(progID, infoLen+1,
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001238 &length, (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001239 GrPrintf((char*)log.get());
1240 }
1241 GrAssert(!"Error linking program");
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001242 GR_GL_CALL(gl, DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001243 programData->fProgramID = 0;
1244 return false;
1245 }
1246 return true;
1247}
1248
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001249void GrGLProgram::getUniformLocationsAndInitCache(const GrGLInterface* gl,
1250 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001251 const GrGLint& progID = programData->fProgramID;
1252
1253 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001254 GR_GL_CALL_RET(gl, programData->fUniLocations.fViewMatrixUni,
1255 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001256 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1257 }
1258 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001259 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorUni,
1260 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001261 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1262 }
Scroggo97c88c22011-05-11 14:05:25 +00001263 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001264 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorFilterUni,
1265 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001266 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1267 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001268
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001269 if (kUseUniform == programData->fUniLocations.fColorMatrixUni) {
1270 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorMatrixUni,
1271 GetUniformLocation(progID, COL_MATRIX_UNI_NAME));
1272 }
1273
1274 if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) {
1275 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorMatrixVecUni,
1276 GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
1277 }
1278
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001279 if (kUseUniform == programData->fUniLocations.fEdgesUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001280 GR_GL_CALL_RET(gl, programData->fUniLocations.fEdgesUni,
1281 GetUniformLocation(progID, EDGES_UNI_NAME));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001282 GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni);
1283 } else {
1284 programData->fUniLocations.fEdgesUni = kUnusedUniform;
1285 }
1286
tomhudson@google.com93813632011-10-27 20:21:16 +00001287 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001288 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001289 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001290 if (kUseUniform == locations.fTextureMatrixUni) {
1291 GrStringBuilder texMName;
1292 tex_matrix_name(s, &texMName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001293 GR_GL_CALL_RET(gl, locations.fTextureMatrixUni,
1294 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001295 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1296 }
1297
1298 if (kUseUniform == locations.fSamplerUni) {
1299 GrStringBuilder samplerName;
1300 sampler_name(s, &samplerName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001301 GR_GL_CALL_RET(gl, locations.fSamplerUni,
1302 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001303 GrAssert(kUnusedUniform != locations.fSamplerUni);
1304 }
1305
1306 if (kUseUniform == locations.fNormalizedTexelSizeUni) {
1307 GrStringBuilder texelSizeName;
1308 normalized_texel_size_name(s, &texelSizeName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001309 GR_GL_CALL_RET(gl, locations.fNormalizedTexelSizeUni,
1310 GetUniformLocation(progID, texelSizeName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001311 GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni);
1312 }
1313
1314 if (kUseUniform == locations.fRadial2Uni) {
1315 GrStringBuilder radial2ParamName;
1316 radial2_param_name(s, &radial2ParamName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001317 GR_GL_CALL_RET(gl, locations.fRadial2Uni,
1318 GetUniformLocation(progID, radial2ParamName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001319 GrAssert(kUnusedUniform != locations.fRadial2Uni);
1320 }
junov@google.com6acc9b32011-05-16 18:32:07 +00001321
1322 if (kUseUniform == locations.fTexDomUni) {
1323 GrStringBuilder texDomName;
1324 tex_domain_name(s, &texDomName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001325 GR_GL_CALL_RET(gl, locations.fTexDomUni,
1326 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001327 GrAssert(kUnusedUniform != locations.fTexDomUni);
1328 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001329
1330 GrStringBuilder kernelName, imageIncrementName;
1331 convolve_param_names(s, &kernelName, &imageIncrementName);
1332 if (kUseUniform == locations.fKernelUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001333 GR_GL_CALL_RET(gl, locations.fKernelUni,
1334 GetUniformLocation(progID, kernelName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001335 GrAssert(kUnusedUniform != locations.fKernelUni);
1336 }
1337
1338 if (kUseUniform == locations.fImageIncrementUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001339 GR_GL_CALL_RET(gl, locations.fImageIncrementUni,
1340 GetUniformLocation(progID,
1341 imageIncrementName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001342 GrAssert(kUnusedUniform != locations.fImageIncrementUni);
1343 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001344 }
1345 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001346 GR_GL_CALL(gl, UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001347
1348 // init sampler unis and set bogus values for state tracking
tomhudson@google.com93813632011-10-27 20:21:16 +00001349 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001350 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001351 GR_GL_CALL(gl, Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001352 }
1353 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
1354 programData->fRadial2CenterX1[s] = GR_ScalarMax;
1355 programData->fRadial2Radius0[s] = -GR_ScalarMax;
1356 programData->fTextureWidth[s] = -1;
1357 programData->fTextureHeight[s] = -1;
1358 }
1359 programData->fViewMatrix = GrMatrix::InvalidMatrix();
1360 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001361 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001362}
1363
junov@google.comf93e7172011-03-31 21:26:24 +00001364//============================================================================
1365// Stage code generation
1366//============================================================================
1367
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001368namespace {
1369
1370bool isRadialMapping(GrGLProgram::StageDesc::CoordMapping mapping) {
1371 return
1372 (GrGLProgram::StageDesc::kRadial2Gradient_CoordMapping == mapping ||
1373 GrGLProgram::StageDesc::kRadial2GradientDegenerate_CoordMapping == mapping);
1374}
1375
tomhudson@google.comda668982011-12-07 15:06:29 +00001376GrGLShaderVar* genRadialVS(int stageNum,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001377 ShaderCodeSegments* segments,
1378 GrGLProgram::StageUniLocations* locations,
1379 const char** radial2VaryingVSName,
1380 const char** radial2VaryingFSName,
1381 const char* varyingVSName,
1382 int varyingDims, int coordDims) {
1383
1384 GrGLShaderVar* radial2FSParams = &segments->fFSUnis.push_back();
1385 radial2FSParams->setType(GrGLShaderVar::kFloat_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001386 radial2FSParams->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001387 radial2FSParams->setArrayCount(6);
1388 radial2_param_name(stageNum, radial2FSParams->accessName());
1389 segments->fVSUnis.push_back(*radial2FSParams).setEmitPrecision(true);
1390
1391 locations->fRadial2Uni = kUseUniform;
1392
1393 // for radial grads without perspective we can pass the linear
1394 // part of the quadratic as a varying.
1395 if (varyingDims == coordDims) {
1396 GrAssert(2 == coordDims);
1397 append_varying(GrGLShaderVar::kFloat_Type,
1398 "Radial2BCoeff",
1399 stageNum,
1400 segments,
1401 radial2VaryingVSName,
1402 radial2VaryingFSName);
1403
tomhudson@google.comda668982011-12-07 15:06:29 +00001404 GrStringBuilder radial2p2;
1405 GrStringBuilder radial2p3;
1406 radial2FSParams->appendArrayAccess(2, &radial2p2);
1407 radial2FSParams->appendArrayAccess(3, &radial2p3);
1408
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001409 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
1410 const char* r2ParamName = radial2FSParams->getName().c_str();
tomhudson@google.comda668982011-12-07 15:06:29 +00001411 segments->fVSCode.appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
1412 *radial2VaryingVSName, radial2p2.c_str(),
1413 varyingVSName, radial2p3.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001414 }
1415
tomhudson@google.comda668982011-12-07 15:06:29 +00001416 return radial2FSParams;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001417}
1418
1419bool genRadial2GradientCoordMapping(int stageNum,
1420 ShaderCodeSegments* segments,
1421 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001422 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001423 GrStringBuilder& sampleCoords,
1424 GrStringBuilder& fsCoordName,
1425 int varyingDims,
1426 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001427 GrStringBuilder cName("c");
1428 GrStringBuilder ac4Name("ac4");
1429 GrStringBuilder rootName("root");
1430
1431 cName.appendS32(stageNum);
1432 ac4Name.appendS32(stageNum);
1433 rootName.appendS32(stageNum);
1434
tomhudson@google.comda668982011-12-07 15:06:29 +00001435 GrStringBuilder radial2p0;
1436 GrStringBuilder radial2p1;
1437 GrStringBuilder radial2p2;
1438 GrStringBuilder radial2p3;
1439 GrStringBuilder radial2p4;
1440 GrStringBuilder radial2p5;
1441 radial2Params->appendArrayAccess(0, &radial2p0);
1442 radial2Params->appendArrayAccess(1, &radial2p1);
1443 radial2Params->appendArrayAccess(2, &radial2p2);
1444 radial2Params->appendArrayAccess(3, &radial2p3);
1445 radial2Params->appendArrayAccess(4, &radial2p4);
1446 radial2Params->appendArrayAccess(5, &radial2p5);
1447
tomhudson@google.com5960d002011-10-19 20:21:48 +00001448 // if we were able to interpolate the linear component bVar is the varying
1449 // otherwise compute it
1450 GrStringBuilder bVar;
1451 if (coordDims == varyingDims) {
1452 bVar = radial2VaryingFSName;
1453 GrAssert(2 == varyingDims);
1454 } else {
1455 GrAssert(3 == varyingDims);
1456 bVar = "b";
1457 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001458 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1459 bVar.c_str(), radial2p2.c_str(),
1460 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001461 }
1462
1463 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001464 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001465 cName.c_str(), fsCoordName.c_str(),
1466 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001467 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001468 // ac4 = 4.0 * params[0] * c
tomhudson@google.comda668982011-12-07 15:06:29 +00001469 segments->fFSCode.appendf("\tfloat %s = %s * 4.0 * %s;\n",
1470 ac4Name.c_str(), radial2p0.c_str(),
tomhudson@google.com5960d002011-10-19 20:21:48 +00001471 cName.c_str());
1472
1473 // root = sqrt(b^2-4ac)
1474 // (abs to avoid exception due to fp precision)
1475 segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
1476 rootName.c_str(), bVar.c_str(), bVar.c_str(),
1477 ac4Name.c_str());
1478
1479 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
1480 // y coord is 0.5 (texture is effectively 1D)
tomhudson@google.comda668982011-12-07 15:06:29 +00001481 sampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)",
1482 bVar.c_str(), radial2p5.c_str(),
1483 rootName.c_str(), radial2p1.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001484 return true;
1485}
1486
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001487bool genRadial2GradientDegenerateCoordMapping(int stageNum,
1488 ShaderCodeSegments* segments,
1489 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001490 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001491 GrStringBuilder& sampleCoords,
1492 GrStringBuilder& fsCoordName,
1493 int varyingDims,
1494 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001495 GrStringBuilder cName("c");
1496
1497 cName.appendS32(stageNum);
1498
tomhudson@google.comda668982011-12-07 15:06:29 +00001499 GrStringBuilder radial2p2;
1500 GrStringBuilder radial2p3;
1501 GrStringBuilder radial2p4;
1502 radial2Params->appendArrayAccess(2, &radial2p2);
1503 radial2Params->appendArrayAccess(3, &radial2p3);
1504 radial2Params->appendArrayAccess(4, &radial2p4);
1505
tomhudson@google.com5960d002011-10-19 20:21:48 +00001506 // if we were able to interpolate the linear component bVar is the varying
1507 // otherwise compute it
1508 GrStringBuilder bVar;
1509 if (coordDims == varyingDims) {
1510 bVar = radial2VaryingFSName;
1511 GrAssert(2 == varyingDims);
1512 } else {
1513 GrAssert(3 == varyingDims);
1514 bVar = "b";
1515 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001516 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1517 bVar.c_str(), radial2p2.c_str(),
1518 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001519 }
1520
1521 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001522 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001523 cName.c_str(), fsCoordName.c_str(),
1524 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001525 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001526
1527 // x coord is: -c/b
1528 // y coord is 0.5 (texture is effectively 1D)
1529 sampleCoords.printf("vec2((-%s / %s), 0.5)", cName.c_str(), bVar.c_str());
1530 return true;
1531}
1532
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001533void gen2x2FS(int stageNum,
1534 ShaderCodeSegments* segments,
1535 GrGLProgram::StageUniLocations* locations,
1536 GrStringBuilder* sampleCoords,
1537 const char* samplerName,
1538 const char* texelSizeName,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001539 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001540 const char* fsOutColor,
1541 GrStringBuilder& texFunc,
1542 GrStringBuilder& modulate,
1543 bool complexCoord,
1544 int coordDims) {
1545 locations->fNormalizedTexelSizeUni = kUseUniform;
1546 if (complexCoord) {
1547 // assign the coord to a var rather than compute 4x.
1548 GrStringBuilder coordVar("tCoord");
1549 coordVar.appendS32(stageNum);
1550 segments->fFSCode.appendf("\t%s %s = %s;\n",
1551 float_vector_type_str(coordDims),
1552 coordVar.c_str(), sampleCoords->c_str());
1553 *sampleCoords = coordVar;
1554 }
1555 GrAssert(2 == coordDims);
1556 GrStringBuilder accumVar("accum");
1557 accumVar.appendS32(stageNum);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001558 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);
1559 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);
1560 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);
1561 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 +00001562 segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str());
1563
1564}
1565
1566void genConvolutionVS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001567 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001568 ShaderCodeSegments* segments,
1569 GrGLProgram::StageUniLocations* locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001570 GrGLShaderVar** kernel,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001571 const char** imageIncrementName,
1572 const char* varyingVSName) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001573 //GrGLShaderVar* kernel = &segments->fFSUnis.push_back();
1574 *kernel = &segments->fFSUnis.push_back();
1575 (*kernel)->setType(GrGLShaderVar::kFloat_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001576 (*kernel)->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.comda668982011-12-07 15:06:29 +00001577 (*kernel)->setArrayCount(desc.fKernelWidth);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001578 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
1579 imgInc->setType(GrGLShaderVar::kVec2f_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001580 imgInc->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001581
1582 convolve_param_names(stageNum,
tomhudson@google.comda668982011-12-07 15:06:29 +00001583 (*kernel)->accessName(),
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001584 imgInc->accessName());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001585 *imageIncrementName = imgInc->getName().c_str();
1586
1587 // need image increment in both VS and FS
1588 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1589
1590 locations->fKernelUni = kUseUniform;
1591 locations->fImageIncrementUni = kUseUniform;
1592 float scale = (desc.fKernelWidth - 1) * 0.5f;
1593 segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n",
1594 varyingVSName, scale, scale,
1595 *imageIncrementName);
1596}
1597
1598void genConvolutionFS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001599 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001600 ShaderCodeSegments* segments,
1601 const char* samplerName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001602 GrGLShaderVar* kernel,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001603 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001604 const char* imageIncrementName,
1605 const char* fsOutColor,
1606 GrStringBuilder& sampleCoords,
1607 GrStringBuilder& texFunc,
1608 GrStringBuilder& modulate) {
1609 GrStringBuilder sumVar("sum");
1610 sumVar.appendS32(stageNum);
1611 GrStringBuilder coordVar("coord");
1612 coordVar.appendS32(stageNum);
1613
tomhudson@google.comda668982011-12-07 15:06:29 +00001614 GrStringBuilder kernelIndex;
1615 kernel->appendArrayAccess("i", &kernelIndex);
1616
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001617 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1618 sumVar.c_str());
1619 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1620 coordVar.c_str(),
1621 sampleCoords.c_str());
1622 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1623 desc.fKernelWidth);
tomhudson@google.comda668982011-12-07 15:06:29 +00001624 segments->fFSCode.appendf("\t\t%s += %s(%s, %s)%s * %s;\n",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001625 sumVar.c_str(), texFunc.c_str(),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001626 samplerName, coordVar.c_str(), swizzle,
tomhudson@google.comda668982011-12-07 15:06:29 +00001627 kernelIndex.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001628 segments->fFSCode.appendf("\t\t%s += %s;\n",
1629 coordVar.c_str(),
1630 imageIncrementName);
1631 segments->fFSCode.appendf("\t}\n");
1632 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1633 sumVar.c_str(), modulate.c_str());
1634}
1635
1636}
1637
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001638void GrGLProgram::genStageCode(const GrGLInterface* gl,
1639 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001640 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001641 const char* fsInColor, // NULL means no incoming color
1642 const char* fsOutColor,
1643 const char* vsInCoord,
1644 ShaderCodeSegments* segments,
1645 StageUniLocations* locations) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001646
bsalomon@google.com74b98712011-11-11 19:46:16 +00001647 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
1648 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) ==
1649 desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001650
junov@google.comf93e7172011-03-31 21:26:24 +00001651 // First decide how many coords are needed to access the texture
1652 // Right now it's always 2 but we could start using 1D textures for
1653 // gradients.
1654 static const int coordDims = 2;
1655 int varyingDims;
1656 /// Vertex Shader Stuff
1657
1658 // decide whether we need a matrix to transform texture coords
1659 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001660 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001661 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001662 varyingDims = coordDims;
1663 } else {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001664 GrGLShaderVar* mat;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001665 #if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001666 mat = &segments->fVSAttrs.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001667 mat->setTypeModifier(GrGLShaderVar::kAttribute_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001668 locations->fTextureMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +00001669 #else
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001670 mat = &segments->fVSUnis.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001671 mat->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001672 locations->fTextureMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001673 #endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001674 tex_matrix_name(stageNum, mat->accessName());
1675 mat->setType(GrGLShaderVar::kMat33f_Type);
1676 matName = mat->getName().c_str();
1677
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001678 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001679 varyingDims = coordDims;
1680 } else {
1681 varyingDims = coordDims + 1;
1682 }
1683 }
1684
tomhudson@google.com086e5352011-12-08 14:44:10 +00001685 segments->fFSUnis.push_back().set(GrGLShaderVar::kSampler2D_Type,
1686 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001687 sampler_name(stageNum, segments->fFSUnis.back().accessName());
bsalomon@google.com91961302011-05-09 18:39:58 +00001688 locations->fSamplerUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001689 const char* samplerName = segments->fFSUnis.back().getName().c_str();
junov@google.comf93e7172011-03-31 21:26:24 +00001690
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001691 const char* texelSizeName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001692 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
tomhudson@google.com086e5352011-12-08 14:44:10 +00001693 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec2f_Type,
1694 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001695 normalized_texel_size_name(stageNum, segments->fFSUnis.back().accessName());
1696 texelSizeName = segments->fFSUnis.back().getName().c_str();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001697 }
1698
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001699 const char *varyingVSName, *varyingFSName;
1700 append_varying(float_vector_type(varyingDims),
1701 "Stage",
1702 stageNum,
1703 segments,
1704 &varyingVSName,
1705 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001706
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001707 if (!matName) {
junov@google.comf93e7172011-03-31 21:26:24 +00001708 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001709 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001710 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001711 // varying = texMatrix * texCoord
1712 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001713 varyingVSName, matName, vsInCoord,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001714 vector_all_coords(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001715 }
1716
tomhudson@google.comda668982011-12-07 15:06:29 +00001717 GrGLShaderVar* radial2Params = NULL;
1718 const char* radial2VaryingVSName = NULL;
1719 const char* radial2VaryingFSName = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001720
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001721 if (isRadialMapping((StageDesc::CoordMapping) desc.fCoordMapping)) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001722 radial2Params = genRadialVS(stageNum, segments,
1723 locations,
1724 &radial2VaryingVSName,
1725 &radial2VaryingFSName,
1726 varyingVSName,
1727 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001728 }
1729
tomhudson@google.comda668982011-12-07 15:06:29 +00001730 GrGLShaderVar* kernel = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001731 const char* imageIncrementName = NULL;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001732 if (StageDesc::kConvolution_FetchMode == desc.fFetchMode) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001733 genConvolutionVS(stageNum, desc, segments, locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001734 &kernel, &imageIncrementName, varyingVSName);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001735 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001736
junov@google.comf93e7172011-03-31 21:26:24 +00001737 /// Fragment Shader Stuff
bsalomon@google.com91961302011-05-09 18:39:58 +00001738 GrStringBuilder fsCoordName;
junov@google.comf93e7172011-03-31 21:26:24 +00001739 // function used to access the shader, may be made projective
bsalomon@google.com91961302011-05-09 18:39:58 +00001740 GrStringBuilder texFunc("texture2D");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001741 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1742 StageDesc::kNoPerspective_OptFlagBit)) {
junov@google.comf93e7172011-03-31 21:26:24 +00001743 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001744 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001745 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001746 // if we have to do some special op on the varyings to get
junov@google.comf93e7172011-03-31 21:26:24 +00001747 // our final tex coords then when in perspective we have to
bsalomon@google.com91961302011-05-09 18:39:58 +00001748 // do an explicit divide. Otherwise, we can use a Proj func.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001749 if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
1750 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001751 texFunc.append("Proj");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001752 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001753 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001754 fsCoordName = "inCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +00001755 fsCoordName.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001756 segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001757 GrGLShaderVar::TypeString(float_vector_type(coordDims)),
1758 fsCoordName.c_str(),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001759 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001760 vector_nonhomog_coords(varyingDims),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001761 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001762 vector_homog_coord(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001763 }
1764 }
1765
bsalomon@google.comfc296292011-05-06 13:53:47 +00001766 GrStringBuilder sampleCoords;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001767 bool complexCoord = false;
junov@google.comf93e7172011-03-31 21:26:24 +00001768 switch (desc.fCoordMapping) {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001769 case StageDesc::kIdentity_CoordMapping:
junov@google.comf93e7172011-03-31 21:26:24 +00001770 sampleCoords = fsCoordName;
1771 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001772 case StageDesc::kSweepGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001773 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 +00001774 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001775 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001776 case StageDesc::kRadialGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001777 sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001778 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001779 break;
tomhudson@google.com5960d002011-10-19 20:21:48 +00001780 case StageDesc::kRadial2Gradient_CoordMapping:
1781 complexCoord = genRadial2GradientCoordMapping(
1782 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001783 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001784 sampleCoords, fsCoordName,
1785 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001786
tomhudson@google.com5960d002011-10-19 20:21:48 +00001787 break;
1788 case StageDesc::kRadial2GradientDegenerate_CoordMapping:
1789 complexCoord = genRadial2GradientDegenerateCoordMapping(
1790 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001791 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001792 sampleCoords, fsCoordName,
1793 varyingDims, coordDims);
1794 break;
junov@google.comf93e7172011-03-31 21:26:24 +00001795
junov@google.comf93e7172011-03-31 21:26:24 +00001796 };
1797
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001798 const char* swizzle = "";
bsalomon@google.com74b98712011-11-11 19:46:16 +00001799 if (desc.fInConfigFlags & StageDesc::kSwapRAndB_InConfigFlag) {
1800 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag));
1801 swizzle = ".bgra";
1802 } else if (desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag) {
1803 GrAssert(!(desc.fInConfigFlags &
1804 StageDesc::kMulRGBByAlpha_InConfigFlag));
1805 swizzle = ".aaaa";
1806 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001807
bsalomon@google.com91961302011-05-09 18:39:58 +00001808 GrStringBuilder modulate;
1809 if (NULL != fsInColor) {
1810 modulate.printf(" * %s", fsInColor);
1811 }
1812
tomhudson@google.com0d831722011-06-02 15:37:14 +00001813 if (desc.fOptFlags &
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001814 StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001815 GrStringBuilder texDomainName;
1816 tex_domain_name(stageNum, &texDomainName);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001817 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
1818 GrGLShaderVar::kUniform_TypeModifier, texDomainName);
junov@google.com6acc9b32011-05-16 18:32:07 +00001819 GrStringBuilder coordVar("clampCoord");
1820 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001821 float_vector_type_str(coordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001822 coordVar.c_str(),
1823 sampleCoords.c_str(),
1824 texDomainName.c_str(),
1825 texDomainName.c_str());
1826 sampleCoords = coordVar;
1827 locations->fTexDomUni = kUseUniform;
1828 }
1829
tomhudson@google.com5960d002011-10-19 20:21:48 +00001830 switch (desc.fFetchMode) {
1831 case StageDesc::k2x2_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001832 GrAssert(!(desc.fInConfigFlags &
1833 StageDesc::kMulRGBByAlpha_InConfigFlag));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001834 gen2x2FS(stageNum, segments, locations, &sampleCoords,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001835 samplerName, texelSizeName, swizzle, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001836 texFunc, modulate, complexCoord, coordDims);
1837 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001838 case StageDesc::kConvolution_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001839 GrAssert(!(desc.fInConfigFlags &
1840 StageDesc::kMulRGBByAlpha_InConfigFlag));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001841 genConvolutionFS(stageNum, desc, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001842 samplerName, kernel, swizzle, imageIncrementName, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001843 sampleCoords, texFunc, modulate);
1844 break;
1845 default:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001846 if (desc.fInConfigFlags & StageDesc::kMulRGBByAlpha_InConfigFlag) {
1847 GrAssert(!(desc.fInConfigFlags &
1848 StageDesc::kSmearAlpha_InConfigFlag));
1849 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
1850 fsOutColor, texFunc.c_str(),
1851 samplerName, sampleCoords.c_str(),
1852 swizzle);
1853 segments->fFSCode.appendf("\t%s = vec4(%s.rgb*%s.a,%s.a)%s;\n",
1854 fsOutColor, fsOutColor, fsOutColor,
1855 fsOutColor, modulate.c_str());
1856 } else {
1857 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n",
1858 fsOutColor, texFunc.c_str(),
1859 samplerName, sampleCoords.c_str(),
1860 swizzle, modulate.c_str());
1861 }
junov@google.comf93e7172011-03-31 21:26:24 +00001862 }
junov@google.comf93e7172011-03-31 21:26:24 +00001863}
tomhudson@google.com5960d002011-10-19 20:21:48 +00001864
tomhudson@google.com5960d002011-10-19 20:21:48 +00001865