blob: 929ee502b810ab1b11a2acc8d5422aa6ca60334f [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) {
27 if (gl->supportsES()) {
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) {
35 if (gl->supportsES()) {
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
82
bsalomon@google.com4be283f2011-04-19 21:15:09 +000083#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +000084 #define VIEW_MATRIX_NAME "aViewM"
85#else
86 #define VIEW_MATRIX_NAME "uViewM"
87#endif
88
89#define POS_ATTR_NAME "aPosition"
90#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000091#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000092#define EDGE_ATTR_NAME "aEdge"
bsalomon@google.com4be283f2011-04-19 21:15:09 +000093#define COL_UNI_NAME "uColor"
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000094#define EDGES_UNI_NAME "uEdges"
Scroggo97c88c22011-05-11 14:05:25 +000095#define COL_FILTER_UNI_NAME "uColorFilter"
junov@google.comf93e7172011-03-31 21:26:24 +000096
bsalomon@google.com4fa66942011-09-20 19:06:12 +000097namespace {
98inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000099 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000100 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +0000101}
102
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000103inline GrGLShaderVar::Type float_vector_type(int count) {
104 GR_STATIC_ASSERT(GrGLShaderVar::kFloat_Type == 0);
105 GR_STATIC_ASSERT(GrGLShaderVar::kVec2f_Type == 1);
106 GR_STATIC_ASSERT(GrGLShaderVar::kVec3f_Type == 2);
107 GR_STATIC_ASSERT(GrGLShaderVar::kVec4f_Type == 3);
108 GrAssert(count > 0 && count <= 4);
109 return (GrGLShaderVar::Type)(count - 1);
junov@google.comf93e7172011-03-31 21:26:24 +0000110}
111
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000112inline const char* float_vector_type_str(int count) {
113 return GrGLShaderVar::TypeString(float_vector_type(count));
114}
115
116inline const char* vector_homog_coord(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000117 static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"};
118 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS));
119 return HOMOGS[count];
120}
121
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000122inline const char* vector_nonhomog_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000123 static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"};
124 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS));
125 return NONHOMOGS[count];
126}
127
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000128inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000129 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
130 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
131 return ALL[count];
132}
133
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000134inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000135 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000136 "vec3(1,1,1)", "vec4(1,1,1,1)"};
137 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
138 return ONESVEC[count];
139}
140
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000141inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000142 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000143 "vec3(0,0,0)", "vec4(0,0,0,0)"};
144 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
145 return ZEROSVEC[count];
146}
147
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000148inline const char* declared_color_output_name() { return "fsColorOut"; }
149inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000150
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000151inline void tex_matrix_name(int stage, GrStringBuilder* s) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000152#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +0000153 *s = "aTexM";
154#else
155 *s = "uTexM";
156#endif
bsalomon@google.comfc296292011-05-06 13:53:47 +0000157 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000158}
159
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000160inline void normalized_texel_size_name(int stage, GrStringBuilder* s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000161 *s = "uTexelSize";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000162 s->appendS32(stage);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000163}
164
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000165inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000166 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000167 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000168}
169
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000170inline void radial2_param_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000171 *s = "uRadial2Params";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000172 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000173}
174
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000175inline void convolve_param_names(int stage, GrStringBuilder* k, GrStringBuilder* i) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000176 *k = "uKernel";
177 k->appendS32(stage);
178 *i = "uImageIncrement";
179 i->appendS32(stage);
180}
181
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000182inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000183 *s = "uTexDom";
184 s->appendS32(stage);
185}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000186}
junov@google.com6acc9b32011-05-16 18:32:07 +0000187
junov@google.comf93e7172011-03-31 21:26:24 +0000188GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000189}
190
191GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000192}
193
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000194void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000195 GrBlendCoeff* dstCoeff) const {
196 switch (fProgramDesc.fDualSrcOutput) {
197 case ProgramDesc::kNone_DualSrcOutput:
198 break;
199 // the prog will write a coverage value to the secondary
200 // output and the dst is blended by one minus that value.
201 case ProgramDesc::kCoverage_DualSrcOutput:
202 case ProgramDesc::kCoverageISA_DualSrcOutput:
203 case ProgramDesc::kCoverageISC_DualSrcOutput:
204 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff;
205 break;
206 default:
207 GrCrash("Unexpected dual source blend output");
208 break;
209 }
210}
211
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000212// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000213// vars can be vec4s or floats (or one of each)
214// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000215// if either var is "" then assign to the other var
216// if both are "" then assign all ones
217static inline void modulate_helper(const char* outputVar,
218 const char* var0,
219 const char* var1,
220 GrStringBuilder* code) {
221 GrAssert(NULL != outputVar);
222 GrAssert(NULL != var0);
223 GrAssert(NULL != var1);
224 GrAssert(NULL != code);
225
226 bool has0 = '\0' != *var0;
227 bool has1 = '\0' != *var1;
228
229 if (!has0 && !has1) {
230 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
231 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000232 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000233 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000234 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000235 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000236 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000237 }
238}
239
240// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000241// vars can be vec4s or floats (or one of each)
242// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000243// if either var is "" then assign to the other var
244// if both are "" then assign all zeros
245static inline void add_helper(const char* outputVar,
246 const char* var0,
247 const char* var1,
248 GrStringBuilder* code) {
249 GrAssert(NULL != outputVar);
250 GrAssert(NULL != var0);
251 GrAssert(NULL != var1);
252 GrAssert(NULL != code);
253
254 bool has0 = '\0' != *var0;
255 bool has1 = '\0' != *var1;
256
257 if (!has0 && !has1) {
258 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
259 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000260 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000261 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000262 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000263 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000264 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000265 }
266}
267
268// given two blend coeffecients determine whether the src
269// and/or dst computation can be omitted.
270static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
271 SkXfermode::Coeff dstCoeff,
272 bool* needSrcValue,
273 bool* needDstValue) {
274 if (SkXfermode::kZero_Coeff == srcCoeff) {
275 switch (dstCoeff) {
276 // these all read the src
277 case SkXfermode::kSC_Coeff:
278 case SkXfermode::kISC_Coeff:
279 case SkXfermode::kSA_Coeff:
280 case SkXfermode::kISA_Coeff:
281 *needSrcValue = true;
282 break;
283 default:
284 *needSrcValue = false;
285 break;
286 }
287 } else {
288 *needSrcValue = true;
289 }
290 if (SkXfermode::kZero_Coeff == dstCoeff) {
291 switch (srcCoeff) {
292 // these all read the dst
293 case SkXfermode::kDC_Coeff:
294 case SkXfermode::kIDC_Coeff:
295 case SkXfermode::kDA_Coeff:
296 case SkXfermode::kIDA_Coeff:
297 *needDstValue = true;
298 break;
299 default:
300 *needDstValue = false;
301 break;
302 }
303 } else {
304 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000305 }
306}
307
308/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000309 * Create a blend_coeff * value string to be used in shader code. Sets empty
310 * string if result is trivially zero.
311 */
312static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
313 const char* src, const char* dst,
314 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000315 switch (coeff) {
316 case SkXfermode::kZero_Coeff: /** 0 */
317 *str = "";
318 break;
319 case SkXfermode::kOne_Coeff: /** 1 */
320 *str = value;
321 break;
322 case SkXfermode::kSC_Coeff:
323 str->printf("(%s * %s)", src, value);
324 break;
325 case SkXfermode::kISC_Coeff:
326 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
327 break;
328 case SkXfermode::kDC_Coeff:
329 str->printf("(%s * %s)", dst, value);
330 break;
331 case SkXfermode::kIDC_Coeff:
332 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
333 break;
334 case SkXfermode::kSA_Coeff: /** src alpha */
335 str->printf("(%s.a * %s)", src, value);
336 break;
337 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
338 str->printf("((1.0 - %s.a) * %s)", src, value);
339 break;
340 case SkXfermode::kDA_Coeff: /** dst alpha */
341 str->printf("(%s.a * %s)", dst, value);
342 break;
343 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
344 str->printf("((1.0 - %s.a) * %s)", dst, value);
345 break;
346 default:
347 GrCrash("Unexpected xfer coeff.");
348 break;
349 }
350}
351/**
Scroggo97c88c22011-05-11 14:05:25 +0000352 * Adds a line to the fragment shader code which modifies the color by
353 * the specified color filter.
354 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000355static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000356 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000357 SkXfermode::Coeff colorCoeff,
358 const char* inColor) {
359 GrStringBuilder colorStr, constStr;
360 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
361 inColor, inColor);
362 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
363 inColor, COL_FILTER_UNI_NAME);
364
365 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000366}
367
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000368namespace {
369
370const char* glsl_version_string(const GrGLInterface* gl,
371 GrGLProgram::GLSLVersion v) {
372 switch (v) {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000373 case GrGLProgram::k110_GLSLVersion:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000374 if (gl->supportsES()) {
375 // ES2s shader language is based on version 1.20 but is version
376 // 1.00 of the ES language.
377 return "#version 100\n";
378 } else {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000379 return "#version 110\n";
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000380 }
381 case GrGLProgram::k130_GLSLVersion:
382 GrAssert(!gl->supportsES());
383 return "#version 130\n";
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000384 case GrGLProgram::k150_GLSLVersion:
385 GrAssert(!gl->supportsES());
386 return "#version 150\n";
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000387 default:
388 GrCrash("Unknown GL version.");
389 return ""; // suppress warning
390 }
391}
392
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000393// Adds a var that is computed in the VS and read in FS.
394// If there is a GS it will just pass it through.
395void append_varying(GrGLShaderVar::Type type,
396 const char* name,
397 ShaderCodeSegments* segments,
398 const char** vsOutName = NULL,
399 const char** fsInName = NULL) {
400 segments->fVSOutputs.push_back();
401 segments->fVSOutputs.back().setType(type);
402 segments->fVSOutputs.back().accessName()->printf("v%s", name);
403 if (vsOutName) {
404 *vsOutName = segments->fVSOutputs.back().getName().c_str();
405 }
406 // input to FS comes either from VS or GS
407 const GrStringBuilder* fsName;
408 if (segments->fUsesGS) {
409 // if we have a GS take each varying in as an array
410 // and output as non-array.
411 segments->fGSInputs.push_back();
412 segments->fGSInputs.back().setType(type);
413 segments->fGSInputs.back().setUnsizedArray();
414 *segments->fGSInputs.back().accessName() =
415 segments->fVSOutputs.back().getName();
416 segments->fGSOutputs.push_back();
417 segments->fGSOutputs.back().setType(type);
418 segments->fGSOutputs.back().accessName()->printf("g%s", name);
419 fsName = segments->fGSOutputs.back().accessName();
420 } else {
421 fsName = segments->fVSOutputs.back().accessName();
422 }
423 segments->fFSInputs.push_back();
424 segments->fFSInputs.back().setType(type);
425 segments->fFSInputs.back().setName(*fsName);
426 if (fsInName) {
427 *fsInName = fsName->c_str();
428 }
429}
430
431// version of above that adds a stage number to the
432// the var name (for uniqueness)
433void append_varying(GrGLShaderVar::Type type,
434 const char* name,
435 int stageNum,
436 ShaderCodeSegments* segments,
437 const char** vsOutName = NULL,
438 const char** fsInName = NULL) {
439 GrStringBuilder nameWithStage(name);
440 nameWithStage.appendS32(stageNum);
441 append_varying(type, nameWithStage.c_str(), segments, vsOutName, fsInName);
442}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000443}
444
bsalomon@google.com66105672011-09-15 15:12:00 +0000445void GrGLProgram::genEdgeCoverage(const GrGLInterface* gl,
446 GrVertexLayout layout,
447 CachedData* programData,
448 GrStringBuilder* coverageVar,
449 ShaderCodeSegments* segments) const {
450 if (fProgramDesc.fEdgeAANumEdges > 0) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000451 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec3f_Type,
452 EDGES_UNI_NAME,
453 fProgramDesc.fEdgeAANumEdges);
bsalomon@google.com66105672011-09-15 15:12:00 +0000454 programData->fUniLocations.fEdgesUni = kUseUniform;
455 int count = fProgramDesc.fEdgeAANumEdges;
456 segments->fFSCode.append(
457 "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n");
458 for (int i = 0; i < count; i++) {
459 segments->fFSCode.append("\tfloat a");
460 segments->fFSCode.appendS32(i);
461 segments->fFSCode.append(" = clamp(dot(" EDGES_UNI_NAME "[");
462 segments->fFSCode.appendS32(i);
463 segments->fFSCode.append("], pos), 0.0, 1.0);\n");
464 }
465 if (fProgramDesc.fEdgeAAConcave && (count & 0x01) == 0) {
466 // For concave polys, we consider the edges in pairs.
467 segments->fFSFunctions.append("float cross2(vec2 a, vec2 b) {\n");
468 segments->fFSFunctions.append("\treturn dot(a, vec2(b.y, -b.x));\n");
469 segments->fFSFunctions.append("}\n");
470 for (int i = 0; i < count; i += 2) {
471 segments->fFSCode.appendf("\tfloat eb%d;\n", i / 2);
472 segments->fFSCode.appendf("\tif (cross2(" EDGES_UNI_NAME "[%d].xy, " EDGES_UNI_NAME "[%d].xy) < 0.0) {\n", i, i + 1);
473 segments->fFSCode.appendf("\t\teb%d = a%d * a%d;\n", i / 2, i, i + 1);
474 segments->fFSCode.append("\t} else {\n");
475 segments->fFSCode.appendf("\t\teb%d = a%d + a%d - a%d * a%d;\n", i / 2, i, i + 1, i, i + 1);
476 segments->fFSCode.append("\t}\n");
477 }
478 segments->fFSCode.append("\tfloat edgeAlpha = ");
479 for (int i = 0; i < count / 2 - 1; i++) {
480 segments->fFSCode.appendf("min(eb%d, ", i);
481 }
482 segments->fFSCode.appendf("eb%d", count / 2 - 1);
483 for (int i = 0; i < count / 2 - 1; i++) {
484 segments->fFSCode.append(")");
485 }
486 segments->fFSCode.append(";\n");
487 } else {
488 segments->fFSCode.append("\tfloat edgeAlpha = ");
489 for (int i = 0; i < count - 1; i++) {
490 segments->fFSCode.appendf("min(a%d * a%d, ", i, i + 1);
491 }
492 segments->fFSCode.appendf("a%d * a0", count - 1);
493 for (int i = 0; i < count - 1; i++) {
494 segments->fFSCode.append(")");
495 }
496 segments->fFSCode.append(";\n");
497 }
498 *coverageVar = "edgeAlpha";
499 } else if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000500 const char *vsName, *fsName;
501 append_varying(GrGLShaderVar::kVec4f_Type, "Edge", segments, &vsName, &fsName);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000502 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000503 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
bsalomon@google.com66105672011-09-15 15:12:00 +0000504 if (GrDrawTarget::kHairLine_EdgeType == fProgramDesc.fVertexEdgeType) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000505 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com66105672011-09-15 15:12:00 +0000506 } else {
507 GrAssert(GrDrawTarget::kHairQuad_EdgeType == fProgramDesc.fVertexEdgeType);
508 // for now we know we're not in perspective, so we could compute this
509 // per-quadratic rather than per pixel
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000510 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
511 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
512 segments->fFSCode.appendf("\tfloat dfdx = 2.0*%s.x*duvdx.x - duvdx.y;\n", fsName);
513 segments->fFSCode.appendf("\tfloat dfdy = 2.0*%s.x*duvdy.x - duvdy.y;\n", fsName);
514 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com66105672011-09-15 15:12:00 +0000515 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / (dfdx*dfdx + dfdy*dfdy));\n");
516 if (gl->supportsES()) {
517 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
518 }
519 }
520 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
521 *coverageVar = "edgeAlpha";
522 } else {
523 coverageVar->reset();
524 }
525}
526
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000527namespace {
528
529// returns true if the color output was explicitly declared or not.
530bool decl_and_get_fs_color_output(GrGLProgram::GLSLVersion v,
531 VarArray* fsOutputs,
532 const char** name) {
533 switch (v) {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000534 case GrGLProgram::k110_GLSLVersion:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000535 *name = "gl_FragColor";
536 return false;
537 break;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000538 case GrGLProgram::k130_GLSLVersion: // fallthru
539 case GrGLProgram::k150_GLSLVersion:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000540 *name = declared_color_output_name();
541 fsOutputs->push_back().set(GrGLShaderVar::kVec4f_Type,
542 declared_color_output_name());
543 return true;
544 break;
545 default:
546 GrCrash("Unknown GLSL version.");
547 return false; // suppress warning
548 }
549}
550
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000551void genInputColor(GrGLProgram::ProgramDesc::ColorType colorType,
552 GrGLProgram::CachedData* programData,
553 ShaderCodeSegments* segments,
554 GrStringBuilder* inColor) {
555 switch (colorType) {
556 case GrGLProgram::ProgramDesc::kAttribute_ColorType: {
557 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
558 COL_ATTR_NAME);
559 const char *vsName, *fsName;
560 append_varying(GrGLShaderVar::kVec4f_Type, "Color", segments, &vsName, &fsName);
561 segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
562 *inColor = fsName;
563 } break;
564 case GrGLProgram::ProgramDesc::kUniform_ColorType:
565 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
566 COL_UNI_NAME);
567 programData->fUniLocations.fColorUni = kUseUniform;
568 *inColor = COL_UNI_NAME;
569 break;
570 case GrGLProgram::ProgramDesc::kTransBlack_ColorType:
571 GrAssert(!"needComputedColor should be false.");
572 break;
573 case GrGLProgram::ProgramDesc::kSolidWhite_ColorType:
574 break;
575 default:
576 GrCrash("Unknown color type.");
577 break;
578 }
579}
580
581void genPerVertexCoverage(ShaderCodeSegments* segments,
582 GrStringBuilder* inCoverage) {
583 segments->fVSAttrs.push_back().set(GrGLShaderVar::kFloat_Type,
584 COV_ATTR_NAME);
585 const char *vsName, *fsName;
586 append_varying(GrGLShaderVar::kFloat_Type, "Coverage",
587 segments, &vsName, &fsName);
588 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
589 if (inCoverage->size()) {
590 segments->fFSCode.appendf("\tfloat edgeAndAttrCov = %s * %s;\n",
591 fsName, inCoverage->c_str());
592 *inCoverage = "edgeAndAttrCov";
593 } else {
594 *inCoverage = fsName;
595 }
596}
597
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000598}
599
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000600void GrGLProgram::genGeometryShader(const GrGLInterface* gl,
601 GLSLVersion glslVersion,
602 ShaderCodeSegments* segments) const {
603#if GR_GL_EXPERIMENTAL_GS
604 if (fProgramDesc.fExperimentalGS) {
605 GrAssert(glslVersion >= k150_GLSLVersion);
606 segments->fGSHeader.append("layout(triangles) in;\n"
607 "layout(triangle_strip, max_vertices = 6) out;\n");
608 segments->fGSCode.append("void main() {\n"
609 "\tfor (int i = 0; i < 3; ++i) {\n"
610 "\t\tgl_Position = gl_in[i].gl_Position;\n");
611 if (this->fProgramDesc.fEmitsPointSize) {
612 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
613 }
614 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
615 int count = segments->fGSInputs.count();
616 for (int i = 0; i < count; ++i) {
617 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
618 segments->fGSOutputs[i].getName().c_str(),
619 segments->fGSInputs[i].getName().c_str());
620 }
621 segments->fGSCode.append("\t\tEmitVertex();\n"
622 "\t}\n"
623 "\tEndPrimitive();\n"
624 "}\n");
625 }
626#endif
627}
628
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000629bool GrGLProgram::genProgram(const GrGLInterface* gl,
630 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000631 GrGLProgram::CachedData* programData) const {
junov@google.comf93e7172011-03-31 21:26:24 +0000632
633 ShaderCodeSegments segments;
634 const uint32_t& layout = fProgramDesc.fVertexLayout;
635
bsalomon@google.com91961302011-05-09 18:39:58 +0000636 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000637
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000638#if GR_GL_EXPERIMENTAL_GS
639 segments.fUsesGS = fProgramDesc.fExperimentalGS;
640#endif
641
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000642 SkXfermode::Coeff colorCoeff, uniformCoeff;
643 // The rest of transfer mode color filters have not been implemented
644 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
645 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000646 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
647 (fProgramDesc.fColorFilterXfermode),
648 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000649 GR_DEBUGASSERT(success);
650 } else {
651 colorCoeff = SkXfermode::kOne_Coeff;
652 uniformCoeff = SkXfermode::kZero_Coeff;
653 }
654
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000655 // If we know the final color is going to be all zeros then we can
656 // simplify the color filter coeffecients. needComputedColor will then
657 // come out false below.
658 if (ProgramDesc::kTransBlack_ColorType == fProgramDesc.fColorType) {
659 colorCoeff = SkXfermode::kZero_Coeff;
660 if (SkXfermode::kDC_Coeff == uniformCoeff ||
661 SkXfermode::kDA_Coeff == uniformCoeff) {
662 uniformCoeff = SkXfermode::kZero_Coeff;
663 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
664 SkXfermode::kIDA_Coeff == uniformCoeff) {
665 uniformCoeff = SkXfermode::kOne_Coeff;
666 }
667 }
668
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000669 bool needColorFilterUniform;
670 bool needComputedColor;
671 needBlendInputs(uniformCoeff, colorCoeff,
672 &needColorFilterUniform, &needComputedColor);
673
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000674 // the dual source output has no canonical var name, have to
675 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000676 const char* fsColorOutput = NULL;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000677 bool dualSourceOutputWritten = false;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000678 segments.fHeader.printf(glsl_version_string(gl, glslVersion));
679 bool isColorDeclared = decl_and_get_fs_color_output(glslVersion,
680 &segments.fFSOutputs,
681 &fsColorOutput);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000682
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000683#if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000684 segments.fVSAttrs.push_back().set(GrGLShaderVar::kMat33f_Type, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000685 programData->fUniLocations.fViewMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +0000686#else
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000687 segments.fVSUnis.push_back().set(GrGLShaderVar::kMat33f_Type, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000688 programData->fUniLocations.fViewMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +0000689#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000690 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000691
bsalomon@google.com91961302011-05-09 18:39:58 +0000692 segments.fVSCode.append(
693 "void main() {\n"
694 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
695 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000696
bsalomon@google.com91961302011-05-09 18:39:58 +0000697 // incoming color to current stage being processed.
698 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000699
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000700 if (needComputedColor) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000701 genInputColor((ProgramDesc::ColorType) fProgramDesc.fColorType,
702 programData, &segments, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000703 }
704
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000705 // we output point size in the GS if present
706 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000707 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000708 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000709
bsalomon@google.com91961302011-05-09 18:39:58 +0000710 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000711
712 // add texture coordinates that are used to the list of vertex attr decls
bsalomon@google.com91961302011-05-09 18:39:58 +0000713 GrStringBuilder texCoordAttrs[GrDrawTarget::kMaxTexCoords];
junov@google.comf93e7172011-03-31 21:26:24 +0000714 for (int t = 0; t < GrDrawTarget::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000715 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000716 tex_attr_name(t, texCoordAttrs + t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000717 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type,
718 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000719 }
720 }
721
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000722 ///////////////////////////////////////////////////////////////////////////
723 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000724
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000725 // if we have color stages string them together, feeding the output color
726 // of each to the next and generating code for each stage.
727 if (needComputedColor) {
728 GrStringBuilder outColor;
729 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000730 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000731 // create var to hold stage result
732 outColor = "color";
733 outColor.appendS32(s);
734 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
735
736 const char* inCoords;
737 // figure out what our input coords are
738 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
739 layout) {
740 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000741 } else {
742 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
743 // we better have input tex coordinates if stage is enabled.
744 GrAssert(tcIdx >= 0);
745 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000746 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000747 }
748
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000749 this->genStageCode(gl,
750 s,
751 fProgramDesc.fStages[s],
752 inColor.size() ? inColor.c_str() : NULL,
753 outColor.c_str(),
754 inCoords,
755 &segments,
756 &programData->fUniLocations.fStages[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000757 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000758 }
759 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000760 }
Scroggo97c88c22011-05-11 14:05:25 +0000761
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000762 // if have all ones or zeros for the "dst" input to the color filter then we
763 // may be able to make additional optimizations.
764 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
765 GrAssert(ProgramDesc::kSolidWhite_ColorType == fProgramDesc.fColorType);
766 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
767 SkXfermode::kIDA_Coeff == uniformCoeff;
768 if (uniformCoeffIsZero) {
769 uniformCoeff = SkXfermode::kZero_Coeff;
770 bool bogus;
771 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
772 &needColorFilterUniform, &bogus);
773 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000774 }
775 if (needColorFilterUniform) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000776 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
777 COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000778 programData->fUniLocations.fColorFilterUni = kUseUniform;
779 }
780
781 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000782 if (SkXfermode::kZero_Coeff == uniformCoeff &&
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000783 SkXfermode::kZero_Coeff == colorCoeff) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000784 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000785 fsColorOutput,
786 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000787 wroteFragColorZero = true;
788 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
789 segments.fFSCode.appendf("\tvec4 filteredColor;\n");
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000790 const char* color;
791 if (inColor.size()) {
792 color = inColor.c_str();
793 } else {
794 if (ProgramDesc::kSolidWhite_ColorType == fProgramDesc.fColorType) {
795 color = all_ones_vec(4);
796 } else {
797 color = all_zeros_vec(4);
798 }
799 }
tomhudson@google.com0d831722011-06-02 15:37:14 +0000800 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000801 colorCoeff, color);
802 inColor = "filteredColor";
803 }
804
805 ///////////////////////////////////////////////////////////////////////////
806 // compute the partial coverage (coverage stages and edge aa)
807
808 GrStringBuilder inCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000809
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000810 // we don't need to compute coverage at all if we know the final shader
811 // output will be zero and we don't have a dual src blend output.
812 if (!wroteFragColorZero ||
813 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000814
815 // get edge AA coverage and use it as inCoverage to first coverage stage
816 this->genEdgeCoverage(gl, layout, programData, &inCoverage, &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000817
bsalomon@google.coma3108262011-10-10 14:08:47 +0000818 // include explicit per-vertex coverage if we have it
819 if (GrDrawTarget::kCoverage_VertexLayoutBit & layout) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000820 genPerVertexCoverage(&segments, &inCoverage);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000821 }
822
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000823 GrStringBuilder outCoverage;
824 const int& startStage = fProgramDesc.fFirstCoverageStage;
825 for (int s = startStage; s < GrDrawTarget::kNumStages; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000826 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000827 // create var to hold stage output
828 outCoverage = "coverage";
829 outCoverage.appendS32(s);
830 segments.fFSCode.appendf("\tvec4 %s;\n", outCoverage.c_str());
831
832 const char* inCoords;
833 // figure out what our input coords are
834 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) & layout) {
835 inCoords = POS_ATTR_NAME;
836 } else {
837 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
838 // we better have input tex coordinates if stage is enabled.
839 GrAssert(tcIdx >= 0);
840 GrAssert(texCoordAttrs[tcIdx].size());
841 inCoords = texCoordAttrs[tcIdx].c_str();
842 }
843
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000844 genStageCode(gl, s,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000845 fProgramDesc.fStages[s],
846 inCoverage.size() ? inCoverage.c_str() : NULL,
847 outCoverage.c_str(),
848 inCoords,
849 &segments,
850 &programData->fUniLocations.fStages[s]);
851 inCoverage = outCoverage;
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000852 }
853 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000854 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000855 segments.fFSOutputs.push_back().set(GrGLShaderVar::kVec4f_Type,
856 dual_source_output_name());
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000857 bool outputIsZero = false;
858 GrStringBuilder coeff;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000859 if (ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000860 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
861 if (!inColor.size()) {
862 outputIsZero = true;
863 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000864 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000865 ProgramDesc::kCoverageISA_DualSrcOutput) {
866 coeff.printf("(1 - %s.a)", inColor.c_str());
867 } else {
868 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
869 }
870 }
871 }
872 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000873 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000874 dual_source_output_name(),
875 all_zeros_vec(4));
876 } else {
877 modulate_helper(dual_source_output_name(),
878 coeff.c_str(),
879 inCoverage.c_str(),
880 &segments.fFSCode);
881 }
882 dualSourceOutputWritten = true;
883 }
junov@google.comf93e7172011-03-31 21:26:24 +0000884 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000885
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000886 ///////////////////////////////////////////////////////////////////////////
887 // combine color and coverage as frag color
888
889 if (!wroteFragColorZero) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000890 modulate_helper(fsColorOutput,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000891 inColor.c_str(),
892 inCoverage.c_str(),
893 &segments.fFSCode);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000894 }
895
bsalomon@google.com91961302011-05-09 18:39:58 +0000896 segments.fVSCode.append("}\n");
897 segments.fFSCode.append("}\n");
898
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000899 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000900 // insert GS
901#if GR_DEBUG
902 this->genGeometryShader(gl, glslVersion, &segments);
903#endif
904
905 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000906 // compile and setup attribs and unis
907
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000908 if (!CompileShaders(gl, glslVersion, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000909 return false;
910 }
911
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000912 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000913 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000914 dualSourceOutputWritten,
915 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +0000916 return false;
917 }
918
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000919 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +0000920
921 return true;
922}
923
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000924namespace {
925
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000926inline void expand_decls(const VarArray& vars,
927 const GrGLInterface* gl,
928 const char* prefix,
929 GrStringBuilder* string) {
930 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000931 for (int i = 0; i < count; ++i) {
932 string->append(prefix);
933 string->append(" ");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000934 vars[i].appendDecl(gl, string);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000935 string->append(";\n");
936 }
937}
938
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000939inline void print_shader(int stringCnt,
940 const char** strings,
941 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000942 for (int i = 0; i < stringCnt; ++i) {
943 if (NULL == stringLengths || stringLengths[i] < 0) {
944 GrPrintf(strings[i]);
945 } else {
946 GrPrintf("%.*s", stringLengths[i], strings[i]);
947 }
948 }
949}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000950
951typedef SkTArray<const char*, true> StrArray;
952#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
953
954typedef SkTArray<int, true> LengthArray;
955#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
956
957// these shouldn't relocate
958typedef GrTAllocator<GrStringBuilder> TempArray;
959#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
960
961inline void append_string(const GrStringBuilder& str,
962 StrArray* strings,
963 LengthArray* lengths) {
964 int length = (int) str.size();
965 if (length) {
966 strings->push_back(str.c_str());
967 lengths->push_back(length);
968 }
969 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000970}
971
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000972inline void append_decls(const VarArray& vars,
973 const GrGLInterface* gl,
974 const char* prefix,
975 StrArray* strings,
976 LengthArray* lengths,
977 TempArray* temp) {
978 expand_decls(vars, gl, prefix, &temp->push_back());
979 append_string(temp->back(), strings, lengths);
980}
981
982}
983
984bool GrGLProgram::CompileShaders(const GrGLInterface* gl,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000985 GLSLVersion glslVersion,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000986 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +0000987 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000988 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +0000989
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000990 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
991 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
992 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
993
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000994 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000995 GrStringBuilder inputs;
996 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000997
998 static const char* gVaryingPrefixes[2][2] = {{"varying", "varying"},
999 {"out", "in"}};
bsalomon@google.com373a6632011-10-19 20:43:20 +00001000 const char** varyingPrefixes = k110_GLSLVersion == glslVersion ?
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001001 gVaryingPrefixes[0] :
1002 gVaryingPrefixes[1];
bsalomon@google.com373a6632011-10-19 20:43:20 +00001003 const char* attributePrefix = k110_GLSLVersion == glslVersion ?
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001004 "attribute" :
1005 "in";
junov@google.comf93e7172011-03-31 21:26:24 +00001006
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001007 append_string(segments.fHeader, &strs, &lengths);
1008 append_decls(segments.fVSUnis, gl, "uniform", &strs, &lengths, &temps);
1009 append_decls(segments.fVSAttrs, gl, attributePrefix, &strs, &lengths, &temps);
1010 append_decls(segments.fVSOutputs, gl, varyingPrefixes[0], &strs, &lengths, &temps);
1011 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001012
1013#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001014 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001015 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001016#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001017
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001018 programData->fVShaderID =
1019 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
1020 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001021
bsalomon@google.com91961302011-05-09 18:39:58 +00001022 if (!programData->fVShaderID) {
1023 return false;
1024 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001025 if (segments.fUsesGS) {
1026 strs.reset();
1027 lengths.reset();
1028 temps.reset();
1029 append_string(segments.fHeader, &strs, &lengths);
1030 append_string(segments.fGSHeader, &strs, &lengths);
1031 append_decls(segments.fGSInputs, gl, "in", &strs, &lengths, &temps);
1032 append_decls(segments.fGSOutputs, gl, "out", &strs, &lengths, &temps);
1033 append_string(segments.fGSCode, &strs, &lengths);
1034#if PRINT_SHADERS
1035 print_shader(strs.count(), &strs[0], &lengths[0]);
1036 GrPrintf("\n");
1037#endif
1038 programData->fGShaderID =
1039 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
1040 &strs[0], &lengths[0]);
1041 } else {
1042 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +00001043 }
junov@google.comf93e7172011-03-31 21:26:24 +00001044
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001045 strs.reset();
1046 lengths.reset();
1047 temps.reset();
1048
1049 append_string(segments.fHeader, &strs, &lengths);
1050 GrStringBuilder precisionStr(GrShaderPrecision(gl));
1051 append_string(precisionStr, &strs, &lengths);
1052 append_decls(segments.fFSUnis, gl, "uniform", &strs, &lengths, &temps);
1053 append_decls(segments.fFSInputs, gl, varyingPrefixes[1], &strs, &lengths, &temps);
bsalomon@google.com373a6632011-10-19 20:43:20 +00001054 // We shouldn't have declared outputs on 1.10
1055 GrAssert(k110_GLSLVersion != glslVersion || segments.fFSOutputs.empty());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001056 append_decls(segments.fFSOutputs, gl, "out", &strs, &lengths, &temps);
1057 append_string(segments.fFSFunctions, &strs, &lengths);
1058 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001059
1060#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001061 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001062 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001063#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001064
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001065 programData->fFShaderID =
1066 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
1067 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001068
bsalomon@google.com91961302011-05-09 18:39:58 +00001069 if (!programData->fFShaderID) {
1070 return false;
junov@google.comf93e7172011-03-31 21:26:24 +00001071 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001072
bsalomon@google.com91961302011-05-09 18:39:58 +00001073 return true;
junov@google.comf93e7172011-03-31 21:26:24 +00001074}
1075
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001076GrGLuint GrGLProgram::CompileShader(const GrGLInterface* gl,
1077 GrGLenum type,
1078 int stringCnt,
1079 const char** strings,
1080 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001081 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
1082 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
1083
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001084 GrGLuint shader;
1085 GR_GL_CALL_RET(gl, shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +00001086 if (0 == shader) {
1087 return 0;
1088 }
1089
1090 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001091 GR_GL_CALL(gl, ShaderSource(shader, stringCnt, strings, stringLengths));
1092 GR_GL_CALL(gl, CompileShader(shader));
1093 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +00001094
1095 if (!compiled) {
1096 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001097 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001098 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001099 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001100 // retrieve length even though we don't need it to workaround
1101 // bug in chrome cmd buffer param validation.
1102 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001103 GR_GL_CALL(gl, GetShaderInfoLog(shader, infoLen+1,
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001104 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001105 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001106 GrPrintf("\n%s", log.get());
1107 }
1108 GrAssert(!"Shader compilation failed!");
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001109 GR_GL_CALL(gl, DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001110 return 0;
1111 }
1112 return shader;
1113}
1114
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001115bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001116 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001117 GrStringBuilder texCoordAttrNames[],
1118 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001119 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001120 CachedData* programData) const {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001121 GR_GL_CALL_RET(gl, programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001122 if (!programData->fProgramID) {
1123 return false;
1124 }
1125 const GrGLint& progID = programData->fProgramID;
1126
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001127 GR_GL_CALL(gl, AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001128 if (programData->fGShaderID) {
1129 GR_GL_CALL(gl, AttachShader(progID, programData->fGShaderID));
1130 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001131 GR_GL_CALL(gl, AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001132
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001133 if (bindColorOut) {
bsalomon@google.combc5cf512011-09-21 16:21:07 +00001134 GR_GL_CALL(gl, BindFragDataLocation(programData->fProgramID,
1135 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001136 }
1137 if (bindDualSrcOut) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001138 GR_GL_CALL(gl, BindFragDataLocationIndexed(programData->fProgramID,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001139 0, 1, dual_source_output_name()));
1140 }
1141
bsalomon@google.com91961302011-05-09 18:39:58 +00001142 // Bind the attrib locations to same values for all shaders
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001143 GR_GL_CALL(gl, BindAttribLocation(progID, PositionAttributeIdx(),
1144 POS_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001145 for (int t = 0; t < GrDrawTarget::kMaxTexCoords; ++t) {
1146 if (texCoordAttrNames[t].size()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001147 GR_GL_CALL(gl, BindAttribLocation(progID,
1148 TexCoordAttributeIdx(t),
1149 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001150 }
1151 }
1152
bsalomon@google.com91961302011-05-09 18:39:58 +00001153 if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001154 GR_GL_CALL(gl, BindAttribLocation(progID,
1155 ViewMatrixAttributeIdx(),
1156 VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001157 }
1158
1159 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
bsalomon@google.com72b4fcb2011-05-09 20:47:34 +00001160 const StageUniLocations& unis = programData->fUniLocations.fStages[s];
1161 if (kSetAsAttribute == unis.fTextureMatrixUni) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001162 GrStringBuilder matName;
1163 tex_matrix_name(s, &matName);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001164 GR_GL_CALL(gl, BindAttribLocation(progID,
1165 TextureMatrixAttributeIdx(s),
1166 matName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001167 }
1168 }
1169
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001170 GR_GL_CALL(gl, BindAttribLocation(progID, ColorAttributeIdx(),
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001171 COL_ATTR_NAME));
bsalomon@google.coma3108262011-10-10 14:08:47 +00001172 GR_GL_CALL(gl, BindAttribLocation(progID, CoverageAttributeIdx(),
1173 COV_ATTR_NAME));
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001174 GR_GL_CALL(gl, BindAttribLocation(progID, EdgeAttributeIdx(),
1175 EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001176
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001177 GR_GL_CALL(gl, LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001178
1179 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001180 GR_GL_CALL(gl, GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001181 if (!linked) {
1182 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001183 GR_GL_CALL(gl, GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001184 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001185 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001186 // retrieve length even though we don't need it to workaround
1187 // bug in chrome cmd buffer param validation.
1188 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001189 GR_GL_CALL(gl, GetProgramInfoLog(progID, infoLen+1,
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001190 &length, (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001191 GrPrintf((char*)log.get());
1192 }
1193 GrAssert(!"Error linking program");
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001194 GR_GL_CALL(gl, DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001195 programData->fProgramID = 0;
1196 return false;
1197 }
1198 return true;
1199}
1200
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201void GrGLProgram::getUniformLocationsAndInitCache(const GrGLInterface* gl,
1202 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001203 const GrGLint& progID = programData->fProgramID;
1204
1205 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001206 GR_GL_CALL_RET(gl, programData->fUniLocations.fViewMatrixUni,
1207 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001208 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1209 }
1210 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001211 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorUni,
1212 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001213 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1214 }
Scroggo97c88c22011-05-11 14:05:25 +00001215 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001216 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorFilterUni,
1217 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001218 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1219 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001220
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001221 if (kUseUniform == programData->fUniLocations.fEdgesUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001222 GR_GL_CALL_RET(gl, programData->fUniLocations.fEdgesUni,
1223 GetUniformLocation(progID, EDGES_UNI_NAME));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001224 GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni);
1225 } else {
1226 programData->fUniLocations.fEdgesUni = kUnusedUniform;
1227 }
1228
bsalomon@google.com91961302011-05-09 18:39:58 +00001229 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
1230 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001231 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001232 if (kUseUniform == locations.fTextureMatrixUni) {
1233 GrStringBuilder texMName;
1234 tex_matrix_name(s, &texMName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001235 GR_GL_CALL_RET(gl, locations.fTextureMatrixUni,
1236 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001237 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1238 }
1239
1240 if (kUseUniform == locations.fSamplerUni) {
1241 GrStringBuilder samplerName;
1242 sampler_name(s, &samplerName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001243 GR_GL_CALL_RET(gl, locations.fSamplerUni,
1244 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001245 GrAssert(kUnusedUniform != locations.fSamplerUni);
1246 }
1247
1248 if (kUseUniform == locations.fNormalizedTexelSizeUni) {
1249 GrStringBuilder texelSizeName;
1250 normalized_texel_size_name(s, &texelSizeName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001251 GR_GL_CALL_RET(gl, locations.fNormalizedTexelSizeUni,
1252 GetUniformLocation(progID, texelSizeName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001253 GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni);
1254 }
1255
1256 if (kUseUniform == locations.fRadial2Uni) {
1257 GrStringBuilder radial2ParamName;
1258 radial2_param_name(s, &radial2ParamName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001259 GR_GL_CALL_RET(gl, locations.fRadial2Uni,
1260 GetUniformLocation(progID, radial2ParamName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001261 GrAssert(kUnusedUniform != locations.fRadial2Uni);
1262 }
junov@google.com6acc9b32011-05-16 18:32:07 +00001263
1264 if (kUseUniform == locations.fTexDomUni) {
1265 GrStringBuilder texDomName;
1266 tex_domain_name(s, &texDomName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001267 GR_GL_CALL_RET(gl, locations.fTexDomUni,
1268 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001269 GrAssert(kUnusedUniform != locations.fTexDomUni);
1270 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001271
1272 GrStringBuilder kernelName, imageIncrementName;
1273 convolve_param_names(s, &kernelName, &imageIncrementName);
1274 if (kUseUniform == locations.fKernelUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001275 GR_GL_CALL_RET(gl, locations.fKernelUni,
1276 GetUniformLocation(progID, kernelName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001277 GrAssert(kUnusedUniform != locations.fKernelUni);
1278 }
1279
1280 if (kUseUniform == locations.fImageIncrementUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001281 GR_GL_CALL_RET(gl, locations.fImageIncrementUni,
1282 GetUniformLocation(progID,
1283 imageIncrementName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001284 GrAssert(kUnusedUniform != locations.fImageIncrementUni);
1285 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001286 }
1287 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001288 GR_GL_CALL(gl, UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001289
1290 // init sampler unis and set bogus values for state tracking
1291 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
1292 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 GR_GL_CALL(gl, Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001294 }
1295 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
1296 programData->fRadial2CenterX1[s] = GR_ScalarMax;
1297 programData->fRadial2Radius0[s] = -GR_ScalarMax;
1298 programData->fTextureWidth[s] = -1;
1299 programData->fTextureHeight[s] = -1;
1300 }
1301 programData->fViewMatrix = GrMatrix::InvalidMatrix();
1302 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001303 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001304}
1305
junov@google.comf93e7172011-03-31 21:26:24 +00001306//============================================================================
1307// Stage code generation
1308//============================================================================
1309
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001310namespace {
1311
1312bool isRadialMapping(GrGLProgram::StageDesc::CoordMapping mapping) {
1313 return
1314 (GrGLProgram::StageDesc::kRadial2Gradient_CoordMapping == mapping ||
1315 GrGLProgram::StageDesc::kRadial2GradientDegenerate_CoordMapping == mapping);
1316}
1317
1318const char* genRadialVS(int stageNum,
1319 ShaderCodeSegments* segments,
1320 GrGLProgram::StageUniLocations* locations,
1321 const char** radial2VaryingVSName,
1322 const char** radial2VaryingFSName,
1323 const char* varyingVSName,
1324 int varyingDims, int coordDims) {
1325
1326 GrGLShaderVar* radial2FSParams = &segments->fFSUnis.push_back();
1327 radial2FSParams->setType(GrGLShaderVar::kFloat_Type);
1328 radial2FSParams->setArrayCount(6);
1329 radial2_param_name(stageNum, radial2FSParams->accessName());
1330 segments->fVSUnis.push_back(*radial2FSParams).setEmitPrecision(true);
1331
1332 locations->fRadial2Uni = kUseUniform;
1333
1334 // for radial grads without perspective we can pass the linear
1335 // part of the quadratic as a varying.
1336 if (varyingDims == coordDims) {
1337 GrAssert(2 == coordDims);
1338 append_varying(GrGLShaderVar::kFloat_Type,
1339 "Radial2BCoeff",
1340 stageNum,
1341 segments,
1342 radial2VaryingVSName,
1343 radial2VaryingFSName);
1344
1345 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
1346 const char* r2ParamName = radial2FSParams->getName().c_str();
1347 segments->fVSCode.appendf("\t%s = 2.0 *(%s[2] * %s.x - %s[3]);\n",
1348 *radial2VaryingVSName, r2ParamName,
1349 varyingVSName, r2ParamName);
1350 }
1351
1352 return radial2FSParams->getName().c_str();
1353}
1354
1355bool genRadial2GradientCoordMapping(int stageNum,
1356 ShaderCodeSegments* segments,
1357 const char* radial2VaryingFSName,
1358 const char* radial2ParamsName,
1359 GrStringBuilder& sampleCoords,
1360 GrStringBuilder& fsCoordName,
1361 int varyingDims,
1362 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001363 GrStringBuilder cName("c");
1364 GrStringBuilder ac4Name("ac4");
1365 GrStringBuilder rootName("root");
1366
1367 cName.appendS32(stageNum);
1368 ac4Name.appendS32(stageNum);
1369 rootName.appendS32(stageNum);
1370
1371 // if we were able to interpolate the linear component bVar is the varying
1372 // otherwise compute it
1373 GrStringBuilder bVar;
1374 if (coordDims == varyingDims) {
1375 bVar = radial2VaryingFSName;
1376 GrAssert(2 == varyingDims);
1377 } else {
1378 GrAssert(3 == varyingDims);
1379 bVar = "b";
1380 bVar.appendS32(stageNum);
1381 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s[2] * %s.x - %s[3]);\n",
1382 bVar.c_str(), radial2ParamsName,
1383 fsCoordName.c_str(), radial2ParamsName);
1384 }
1385
1386 // c = (x^2)+(y^2) - params[4]
1387 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s[4];\n",
1388 cName.c_str(), fsCoordName.c_str(),
1389 fsCoordName.c_str(),
1390 radial2ParamsName);
1391 // ac4 = 4.0 * params[0] * c
1392 segments->fFSCode.appendf("\tfloat %s = %s[0] * 4.0 * %s;\n",
1393 ac4Name.c_str(), radial2ParamsName,
1394 cName.c_str());
1395
1396 // root = sqrt(b^2-4ac)
1397 // (abs to avoid exception due to fp precision)
1398 segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
1399 rootName.c_str(), bVar.c_str(), bVar.c_str(),
1400 ac4Name.c_str());
1401
1402 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
1403 // y coord is 0.5 (texture is effectively 1D)
1404 sampleCoords.printf("vec2((-%s + %s[5] * %s) * %s[1], 0.5)",
1405 bVar.c_str(), radial2ParamsName,
1406 rootName.c_str(), radial2ParamsName);
1407 return true;
1408}
1409
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001410bool genRadial2GradientDegenerateCoordMapping(int stageNum,
1411 ShaderCodeSegments* segments,
1412 const char* radial2VaryingFSName,
1413 const char* radial2ParamsName,
1414 GrStringBuilder& sampleCoords,
1415 GrStringBuilder& fsCoordName,
1416 int varyingDims,
1417 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001418 GrStringBuilder cName("c");
1419
1420 cName.appendS32(stageNum);
1421
1422 // if we were able to interpolate the linear component bVar is the varying
1423 // otherwise compute it
1424 GrStringBuilder bVar;
1425 if (coordDims == varyingDims) {
1426 bVar = radial2VaryingFSName;
1427 GrAssert(2 == varyingDims);
1428 } else {
1429 GrAssert(3 == varyingDims);
1430 bVar = "b";
1431 bVar.appendS32(stageNum);
1432 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s[2] * %s.x - %s[3]);\n",
1433 bVar.c_str(), radial2ParamsName,
1434 fsCoordName.c_str(), radial2ParamsName);
1435 }
1436
1437 // c = (x^2)+(y^2) - params[4]
1438 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s[4];\n",
1439 cName.c_str(), fsCoordName.c_str(),
1440 fsCoordName.c_str(),
1441 radial2ParamsName);
1442
1443 // x coord is: -c/b
1444 // y coord is 0.5 (texture is effectively 1D)
1445 sampleCoords.printf("vec2((-%s / %s), 0.5)", cName.c_str(), bVar.c_str());
1446 return true;
1447}
1448
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001449void gen2x2FS(int stageNum,
1450 ShaderCodeSegments* segments,
1451 GrGLProgram::StageUniLocations* locations,
1452 GrStringBuilder* sampleCoords,
1453 const char* samplerName,
1454 const char* texelSizeName,
1455 const char* smear,
1456 const char* fsOutColor,
1457 GrStringBuilder& texFunc,
1458 GrStringBuilder& modulate,
1459 bool complexCoord,
1460 int coordDims) {
1461 locations->fNormalizedTexelSizeUni = kUseUniform;
1462 if (complexCoord) {
1463 // assign the coord to a var rather than compute 4x.
1464 GrStringBuilder coordVar("tCoord");
1465 coordVar.appendS32(stageNum);
1466 segments->fFSCode.appendf("\t%s %s = %s;\n",
1467 float_vector_type_str(coordDims),
1468 coordVar.c_str(), sampleCoords->c_str());
1469 *sampleCoords = coordVar;
1470 }
1471 GrAssert(2 == coordDims);
1472 GrStringBuilder accumVar("accum");
1473 accumVar.appendS32(stageNum);
1474 segments->fFSCode.appendf("\tvec4 %s = %s(%s, %s + vec2(-%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, sampleCoords->c_str(), texelSizeName, texelSizeName, smear);
1475 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,-%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, sampleCoords->c_str(), texelSizeName, texelSizeName, smear);
1476 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(-%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, sampleCoords->c_str(), texelSizeName, texelSizeName, smear);
1477 segments->fFSCode.appendf("\t%s += %s(%s, %s + vec2(+%s.x,+%s.y))%s;\n", accumVar.c_str(), texFunc.c_str(), samplerName, sampleCoords->c_str(), texelSizeName, texelSizeName, smear);
1478 segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str());
1479
1480}
1481
1482void genConvolutionVS(int stageNum,
1483 const GrGLProgram::ProgramDesc::StageDesc& desc,
1484 ShaderCodeSegments* segments,
1485 GrGLProgram::StageUniLocations* locations,
1486 const char** kernelName,
1487 const char** imageIncrementName,
1488 const char* varyingVSName) {
1489 GrGLShaderVar* kernel = &segments->fFSUnis.push_back();
1490 kernel->setType(GrGLShaderVar::kFloat_Type);
1491 kernel->setArrayCount(desc.fKernelWidth);
1492 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
1493 imgInc->setType(GrGLShaderVar::kVec2f_Type);
1494
1495 convolve_param_names(stageNum,
1496 kernel->accessName(),
1497 imgInc->accessName());
1498 *kernelName = kernel->getName().c_str();
1499 *imageIncrementName = imgInc->getName().c_str();
1500
1501 // need image increment in both VS and FS
1502 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1503
1504 locations->fKernelUni = kUseUniform;
1505 locations->fImageIncrementUni = kUseUniform;
1506 float scale = (desc.fKernelWidth - 1) * 0.5f;
1507 segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n",
1508 varyingVSName, scale, scale,
1509 *imageIncrementName);
1510}
1511
1512void genConvolutionFS(int stageNum,
1513 const GrGLProgram::ProgramDesc::StageDesc& desc,
1514 ShaderCodeSegments* segments,
1515 const char* samplerName,
1516 const char* kernelName,
1517 const char* smear,
1518 const char* imageIncrementName,
1519 const char* fsOutColor,
1520 GrStringBuilder& sampleCoords,
1521 GrStringBuilder& texFunc,
1522 GrStringBuilder& modulate) {
1523 GrStringBuilder sumVar("sum");
1524 sumVar.appendS32(stageNum);
1525 GrStringBuilder coordVar("coord");
1526 coordVar.appendS32(stageNum);
1527
1528 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1529 sumVar.c_str());
1530 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1531 coordVar.c_str(),
1532 sampleCoords.c_str());
1533 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1534 desc.fKernelWidth);
1535 segments->fFSCode.appendf("\t\t%s += %s(%s, %s)%s * %s[i];\n",
1536 sumVar.c_str(), texFunc.c_str(),
1537 samplerName, coordVar.c_str(), smear,
1538 kernelName);
1539 segments->fFSCode.appendf("\t\t%s += %s;\n",
1540 coordVar.c_str(),
1541 imageIncrementName);
1542 segments->fFSCode.appendf("\t}\n");
1543 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1544 sumVar.c_str(), modulate.c_str());
1545}
1546
1547}
1548
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001549void GrGLProgram::genStageCode(const GrGLInterface* gl,
1550 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001551 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001552 const char* fsInColor, // NULL means no incoming color
1553 const char* fsOutColor,
1554 const char* vsInCoord,
1555 ShaderCodeSegments* segments,
1556 StageUniLocations* locations) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001557
1558 GrAssert(stageNum >= 0 && stageNum <= 9);
1559
junov@google.comf93e7172011-03-31 21:26:24 +00001560 // First decide how many coords are needed to access the texture
1561 // Right now it's always 2 but we could start using 1D textures for
1562 // gradients.
1563 static const int coordDims = 2;
1564 int varyingDims;
1565 /// Vertex Shader Stuff
1566
1567 // decide whether we need a matrix to transform texture coords
1568 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001569 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001570 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001571 varyingDims = coordDims;
1572 } else {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001573 GrGLShaderVar* mat;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001574 #if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001575 mat = &segments->fVSAttrs.push_back();
bsalomon@google.com91961302011-05-09 18:39:58 +00001576 locations->fTextureMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +00001577 #else
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001578 mat = &segments->fVSUnis.push_back();
bsalomon@google.com91961302011-05-09 18:39:58 +00001579 locations->fTextureMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001580 #endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001581 tex_matrix_name(stageNum, mat->accessName());
1582 mat->setType(GrGLShaderVar::kMat33f_Type);
1583 matName = mat->getName().c_str();
1584
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001585 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001586 varyingDims = coordDims;
1587 } else {
1588 varyingDims = coordDims + 1;
1589 }
1590 }
1591
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001592 segments->fFSUnis.push_back().setType(GrGLShaderVar::kSampler2D_Type);
1593 sampler_name(stageNum, segments->fFSUnis.back().accessName());
bsalomon@google.com91961302011-05-09 18:39:58 +00001594 locations->fSamplerUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001595 const char* samplerName = segments->fFSUnis.back().getName().c_str();
junov@google.comf93e7172011-03-31 21:26:24 +00001596
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001597 const char* texelSizeName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001598 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001599 segments->fFSUnis.push_back().setType(GrGLShaderVar::kVec2f_Type);
1600 normalized_texel_size_name(stageNum, segments->fFSUnis.back().accessName());
1601 texelSizeName = segments->fFSUnis.back().getName().c_str();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001602 }
1603
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001604 const char *varyingVSName, *varyingFSName;
1605 append_varying(float_vector_type(varyingDims),
1606 "Stage",
1607 stageNum,
1608 segments,
1609 &varyingVSName,
1610 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001611
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001612 if (!matName) {
junov@google.comf93e7172011-03-31 21:26:24 +00001613 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001614 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001615 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001616 // varying = texMatrix * texCoord
1617 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001618 varyingVSName, matName, vsInCoord,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001619 vector_all_coords(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001620 }
1621
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001622 const char* radial2ParamsName = NULL;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001623 const char *radial2VaryingVSName = NULL;
1624 const char *radial2VaryingFSName = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001625
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001626 if (isRadialMapping((StageDesc::CoordMapping) desc.fCoordMapping)) {
1627 radial2ParamsName = genRadialVS(stageNum, segments,
1628 locations,
1629 &radial2VaryingVSName,
1630 &radial2VaryingFSName,
1631 varyingVSName,
1632 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001633 }
1634
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001635 const char* kernelName = NULL;
1636 const char* imageIncrementName = NULL;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001637 if (ProgramDesc::StageDesc::kConvolution_FetchMode == desc.fFetchMode) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001638 genConvolutionVS(stageNum, desc, segments, locations,
1639 &kernelName, &imageIncrementName, varyingVSName);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001640 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001641
junov@google.comf93e7172011-03-31 21:26:24 +00001642 /// Fragment Shader Stuff
bsalomon@google.com91961302011-05-09 18:39:58 +00001643 GrStringBuilder fsCoordName;
junov@google.comf93e7172011-03-31 21:26:24 +00001644 // function used to access the shader, may be made projective
bsalomon@google.com91961302011-05-09 18:39:58 +00001645 GrStringBuilder texFunc("texture2D");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001646 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1647 StageDesc::kNoPerspective_OptFlagBit)) {
junov@google.comf93e7172011-03-31 21:26:24 +00001648 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001649 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001650 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001651 // if we have to do some special op on the varyings to get
junov@google.comf93e7172011-03-31 21:26:24 +00001652 // our final tex coords then when in perspective we have to
bsalomon@google.com91961302011-05-09 18:39:58 +00001653 // do an explicit divide. Otherwise, we can use a Proj func.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001654 if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
1655 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001656 texFunc.append("Proj");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001657 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001658 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001659 fsCoordName = "inCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +00001660 fsCoordName.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001661 segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001662 GrGLShaderVar::TypeString(float_vector_type(coordDims)),
1663 fsCoordName.c_str(),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001664 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001665 vector_nonhomog_coords(varyingDims),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001666 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001667 vector_homog_coord(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001668 }
1669 }
1670
bsalomon@google.comfc296292011-05-06 13:53:47 +00001671 GrStringBuilder sampleCoords;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001672 bool complexCoord = false;
junov@google.comf93e7172011-03-31 21:26:24 +00001673 switch (desc.fCoordMapping) {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001674 case StageDesc::kIdentity_CoordMapping:
junov@google.comf93e7172011-03-31 21:26:24 +00001675 sampleCoords = fsCoordName;
1676 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001677 case StageDesc::kSweepGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001678 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 +00001679 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001680 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001681 case StageDesc::kRadialGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001682 sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001683 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001684 break;
tomhudson@google.com5960d002011-10-19 20:21:48 +00001685 case StageDesc::kRadial2Gradient_CoordMapping:
1686 complexCoord = genRadial2GradientCoordMapping(
1687 stageNum, segments,
1688 radial2VaryingFSName, radial2ParamsName,
1689 sampleCoords, fsCoordName,
1690 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001691
tomhudson@google.com5960d002011-10-19 20:21:48 +00001692 break;
1693 case StageDesc::kRadial2GradientDegenerate_CoordMapping:
1694 complexCoord = genRadial2GradientDegenerateCoordMapping(
1695 stageNum, segments,
1696 radial2VaryingFSName, radial2ParamsName,
1697 sampleCoords, fsCoordName,
1698 varyingDims, coordDims);
1699 break;
junov@google.comf93e7172011-03-31 21:26:24 +00001700
junov@google.comf93e7172011-03-31 21:26:24 +00001701 };
1702
bsalomon@google.com91961302011-05-09 18:39:58 +00001703 const char* smear;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001704 if (desc.fModulation == StageDesc::kAlpha_Modulation) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001705 smear = ".aaaa";
1706 } else {
1707 smear = "";
1708 }
1709 GrStringBuilder modulate;
1710 if (NULL != fsInColor) {
1711 modulate.printf(" * %s", fsInColor);
1712 }
1713
tomhudson@google.com0d831722011-06-02 15:37:14 +00001714 if (desc.fOptFlags &
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001715 StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001716 GrStringBuilder texDomainName;
1717 tex_domain_name(stageNum, &texDomainName);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001718 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type, texDomainName);
junov@google.com6acc9b32011-05-16 18:32:07 +00001719 GrStringBuilder coordVar("clampCoord");
1720 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001721 float_vector_type_str(coordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001722 coordVar.c_str(),
1723 sampleCoords.c_str(),
1724 texDomainName.c_str(),
1725 texDomainName.c_str());
1726 sampleCoords = coordVar;
1727 locations->fTexDomUni = kUseUniform;
1728 }
1729
tomhudson@google.com5960d002011-10-19 20:21:48 +00001730 switch (desc.fFetchMode) {
1731 case StageDesc::k2x2_FetchMode:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001732 gen2x2FS(stageNum, segments, locations, &sampleCoords,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001733 samplerName, texelSizeName, smear, fsOutColor,
1734 texFunc, modulate, complexCoord, coordDims);
1735 break;
1736 case ProgramDesc::StageDesc::kConvolution_FetchMode:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001737 genConvolutionFS(stageNum, desc, segments,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001738 samplerName, kernelName, smear, imageIncrementName, fsOutColor,
1739 sampleCoords, texFunc, modulate);
1740 break;
1741 default:
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001742 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n",
1743 fsOutColor, texFunc.c_str(),
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001744 samplerName, sampleCoords.c_str(),
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001745 smear, modulate.c_str());
junov@google.comf93e7172011-03-31 21:26:24 +00001746 }
junov@google.comf93e7172011-03-31 21:26:24 +00001747}
tomhudson@google.com5960d002011-10-19 20:21:48 +00001748
tomhudson@google.com5960d002011-10-19 20:21:48 +00001749