blob: 3e1f1d40c274ff3013a758312cc1bf77dcf015b7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
junov@google.comf93e7172011-03-31 21:26:24 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
junov@google.comf93e7172011-03-31 21:26:24 +000010#include "GrGLProgram.h"
11
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000012#include "../GrAllocator.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000013#include "GrCustomStage.h"
14#include "GrGLProgramStage.h"
bsalomon@google.com4fa66942011-09-20 19:06:12 +000015#include "GrGLShaderVar.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000016#include "SkTrace.h"
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkXfermode.h"
18
junov@google.comf93e7172011-03-31 21:26:24 +000019namespace {
20
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +000021enum {
22 /// Used to mark a StageUniLocation field that should be bound
23 /// to a uniform during getUniformLocationsAndInitCache().
24 kUseUniform = 2000
25};
26
junov@google.comf93e7172011-03-31 21:26:24 +000027} // namespace
28
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000029#define PRINT_SHADERS 0
30
bsalomon@google.com4fa66942011-09-20 19:06:12 +000031typedef GrTAllocator<GrGLShaderVar> VarArray;
32
33// number of each input/output type in a single allocation block
34static const int gVarsPerBlock = 8;
35// except FS outputs where we expect 2 at most.
36static const int gMaxFSOutputs = 2;
37
38struct ShaderCodeSegments {
39 ShaderCodeSegments()
40 : fVSUnis(gVarsPerBlock)
41 , fVSAttrs(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000042 , fVSOutputs(gVarsPerBlock)
43 , fGSInputs(gVarsPerBlock)
44 , fGSOutputs(gVarsPerBlock)
45 , fFSInputs(gVarsPerBlock)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000046 , fFSUnis(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000047 , fFSOutputs(gMaxFSOutputs)
48 , fUsesGS(false) {}
bsalomon@google.com4fa66942011-09-20 19:06:12 +000049 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
50 VarArray fVSUnis;
51 VarArray fVSAttrs;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000052 VarArray fVSOutputs;
53 VarArray fGSInputs;
54 VarArray fGSOutputs;
55 VarArray fFSInputs;
56 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
bsalomon@google.com4fa66942011-09-20 19:06:12 +000057 VarArray fFSUnis;
58 VarArray fFSOutputs;
59 GrStringBuilder fFSFunctions;
60 GrStringBuilder fVSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000061 GrStringBuilder fGSCode;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000062 GrStringBuilder fFSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000063
64 bool fUsesGS;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000065};
66
bsalomon@google.com85b505b2011-11-07 14:56:51 +000067typedef GrGLProgram::ProgramDesc::StageDesc StageDesc;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000068
bsalomon@google.com4be283f2011-04-19 21:15:09 +000069#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +000070 #define VIEW_MATRIX_NAME "aViewM"
71#else
72 #define VIEW_MATRIX_NAME "uViewM"
73#endif
74
75#define POS_ATTR_NAME "aPosition"
76#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000077#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000078#define EDGE_ATTR_NAME "aEdge"
bsalomon@google.com4be283f2011-04-19 21:15:09 +000079#define COL_UNI_NAME "uColor"
bsalomon@google.com2401ae82012-01-17 21:03:05 +000080#define COV_UNI_NAME "uCoverage"
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000081#define EDGES_UNI_NAME "uEdges"
Scroggo97c88c22011-05-11 14:05:25 +000082#define COL_FILTER_UNI_NAME "uColorFilter"
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +000083#define COL_MATRIX_UNI_NAME "uColorMatrix"
84#define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec"
junov@google.comf93e7172011-03-31 21:26:24 +000085
bsalomon@google.com4fa66942011-09-20 19:06:12 +000086namespace {
87inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +000088 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +000089 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +000090}
91
bsalomon@google.com4fa66942011-09-20 19:06:12 +000092inline const char* float_vector_type_str(int count) {
tomhudson@google.com168e6342012-04-18 17:49:20 +000093 return GrGLShaderVar::TypeString(GrSLFloatVectorType(count));
junov@google.comf93e7172011-03-31 21:26:24 +000094}
95
bsalomon@google.com4fa66942011-09-20 19:06:12 +000096inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +000097 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
98 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
99 return ALL[count];
100}
101
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000102inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000103 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000104 "vec3(1,1,1)", "vec4(1,1,1,1)"};
105 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
106 return ONESVEC[count];
107}
108
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000109inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000110 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000111 "vec3(0,0,0)", "vec4(0,0,0,0)"};
112 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
113 return ZEROSVEC[count];
114}
115
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000116inline const char* declared_color_output_name() { return "fsColorOut"; }
117inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000118
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000119inline void tex_matrix_name(int stage, GrStringBuilder* s) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000120#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +0000121 *s = "aTexM";
122#else
123 *s = "uTexM";
124#endif
bsalomon@google.comfc296292011-05-06 13:53:47 +0000125 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000126}
127
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000128inline void normalized_texel_size_name(int stage, GrStringBuilder* s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000129 *s = "uTexelSize";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000130 s->appendS32(stage);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000131}
132
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000133inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000134 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000135 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000136}
137
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000138inline void radial2_param_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000139 *s = "uRadial2Params";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000140 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000141}
142
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000143inline void convolve_param_names(int stage, GrStringBuilder* k, GrStringBuilder* i) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000144 *k = "uKernel";
145 k->appendS32(stage);
146 *i = "uImageIncrement";
147 i->appendS32(stage);
148}
149
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000150inline void image_increment_param_name(int stage, GrStringBuilder* i) {
151 *i = "uImageIncrement";
152 i->appendS32(stage);
153}
154
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000155inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000156 *s = "uTexDom";
157 s->appendS32(stage);
158}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000159}
junov@google.com6acc9b32011-05-16 18:32:07 +0000160
junov@google.comf93e7172011-03-31 21:26:24 +0000161GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000162}
163
164GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000165}
166
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000167void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000168 GrBlendCoeff* dstCoeff) const {
169 switch (fProgramDesc.fDualSrcOutput) {
170 case ProgramDesc::kNone_DualSrcOutput:
171 break;
172 // the prog will write a coverage value to the secondary
173 // output and the dst is blended by one minus that value.
174 case ProgramDesc::kCoverage_DualSrcOutput:
175 case ProgramDesc::kCoverageISA_DualSrcOutput:
176 case ProgramDesc::kCoverageISC_DualSrcOutput:
177 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff;
178 break;
179 default:
180 GrCrash("Unexpected dual source blend output");
181 break;
182 }
183}
184
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000185// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000186// vars can be vec4s or floats (or one of each)
187// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000188// if either var is "" then assign to the other var
189// if both are "" then assign all ones
190static inline void modulate_helper(const char* outputVar,
191 const char* var0,
192 const char* var1,
193 GrStringBuilder* code) {
194 GrAssert(NULL != outputVar);
195 GrAssert(NULL != var0);
196 GrAssert(NULL != var1);
197 GrAssert(NULL != code);
198
199 bool has0 = '\0' != *var0;
200 bool has1 = '\0' != *var1;
201
202 if (!has0 && !has1) {
203 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
204 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000205 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000206 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000207 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000208 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000209 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000210 }
211}
212
213// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000214// vars can be vec4s or floats (or one of each)
215// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000216// if either var is "" then assign to the other var
217// if both are "" then assign all zeros
218static inline void add_helper(const char* outputVar,
219 const char* var0,
220 const char* var1,
221 GrStringBuilder* code) {
222 GrAssert(NULL != outputVar);
223 GrAssert(NULL != var0);
224 GrAssert(NULL != var1);
225 GrAssert(NULL != code);
226
227 bool has0 = '\0' != *var0;
228 bool has1 = '\0' != *var1;
229
230 if (!has0 && !has1) {
231 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
232 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000233 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000234 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000235 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000236 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000237 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000238 }
239}
240
241// given two blend coeffecients determine whether the src
242// and/or dst computation can be omitted.
243static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
244 SkXfermode::Coeff dstCoeff,
245 bool* needSrcValue,
246 bool* needDstValue) {
247 if (SkXfermode::kZero_Coeff == srcCoeff) {
248 switch (dstCoeff) {
249 // these all read the src
250 case SkXfermode::kSC_Coeff:
251 case SkXfermode::kISC_Coeff:
252 case SkXfermode::kSA_Coeff:
253 case SkXfermode::kISA_Coeff:
254 *needSrcValue = true;
255 break;
256 default:
257 *needSrcValue = false;
258 break;
259 }
260 } else {
261 *needSrcValue = true;
262 }
263 if (SkXfermode::kZero_Coeff == dstCoeff) {
264 switch (srcCoeff) {
265 // these all read the dst
266 case SkXfermode::kDC_Coeff:
267 case SkXfermode::kIDC_Coeff:
268 case SkXfermode::kDA_Coeff:
269 case SkXfermode::kIDA_Coeff:
270 *needDstValue = true;
271 break;
272 default:
273 *needDstValue = false;
274 break;
275 }
276 } else {
277 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000278 }
279}
280
281/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000282 * Create a blend_coeff * value string to be used in shader code. Sets empty
283 * string if result is trivially zero.
284 */
285static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
286 const char* src, const char* dst,
287 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000288 switch (coeff) {
289 case SkXfermode::kZero_Coeff: /** 0 */
290 *str = "";
291 break;
292 case SkXfermode::kOne_Coeff: /** 1 */
293 *str = value;
294 break;
295 case SkXfermode::kSC_Coeff:
296 str->printf("(%s * %s)", src, value);
297 break;
298 case SkXfermode::kISC_Coeff:
299 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
300 break;
301 case SkXfermode::kDC_Coeff:
302 str->printf("(%s * %s)", dst, value);
303 break;
304 case SkXfermode::kIDC_Coeff:
305 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
306 break;
307 case SkXfermode::kSA_Coeff: /** src alpha */
308 str->printf("(%s.a * %s)", src, value);
309 break;
310 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
311 str->printf("((1.0 - %s.a) * %s)", src, value);
312 break;
313 case SkXfermode::kDA_Coeff: /** dst alpha */
314 str->printf("(%s.a * %s)", dst, value);
315 break;
316 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
317 str->printf("((1.0 - %s.a) * %s)", dst, value);
318 break;
319 default:
320 GrCrash("Unexpected xfer coeff.");
321 break;
322 }
323}
324/**
Scroggo97c88c22011-05-11 14:05:25 +0000325 * Adds a line to the fragment shader code which modifies the color by
326 * the specified color filter.
327 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000328static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000329 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000330 SkXfermode::Coeff colorCoeff,
331 const char* inColor) {
332 GrStringBuilder colorStr, constStr;
333 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
334 inColor, inColor);
335 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
336 inColor, COL_FILTER_UNI_NAME);
337
338 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000339}
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000340/**
341 * Adds code to the fragment shader code which modifies the color by
342 * the specified color matrix.
343 */
344static void addColorMatrix(GrStringBuilder* fsCode, const char * outputVar,
345 const char* inColor) {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000346 fsCode->appendf("\t%s = %s * vec4(%s.rgb / %s.a, %s.a) + %s;\n", outputVar, COL_MATRIX_UNI_NAME, inColor, inColor, inColor, COL_MATRIX_VEC_UNI_NAME);
347 fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000348}
Scroggo97c88c22011-05-11 14:05:25 +0000349
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000350namespace {
351
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000352// Adds a var that is computed in the VS and read in FS.
353// If there is a GS it will just pass it through.
tomhudson@google.com168e6342012-04-18 17:49:20 +0000354void append_varying(GrSLType type,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000355 const char* name,
356 ShaderCodeSegments* segments,
357 const char** vsOutName = NULL,
358 const char** fsInName = NULL) {
359 segments->fVSOutputs.push_back();
360 segments->fVSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000361 segments->fVSOutputs.back().setTypeModifier(
362 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000363 segments->fVSOutputs.back().accessName()->printf("v%s", name);
364 if (vsOutName) {
365 *vsOutName = segments->fVSOutputs.back().getName().c_str();
366 }
367 // input to FS comes either from VS or GS
368 const GrStringBuilder* fsName;
369 if (segments->fUsesGS) {
370 // if we have a GS take each varying in as an array
371 // and output as non-array.
372 segments->fGSInputs.push_back();
373 segments->fGSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000374 segments->fGSInputs.back().setTypeModifier(
375 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000376 segments->fGSInputs.back().setUnsizedArray();
377 *segments->fGSInputs.back().accessName() =
378 segments->fVSOutputs.back().getName();
379 segments->fGSOutputs.push_back();
380 segments->fGSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000381 segments->fGSOutputs.back().setTypeModifier(
382 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000383 segments->fGSOutputs.back().accessName()->printf("g%s", name);
384 fsName = segments->fGSOutputs.back().accessName();
385 } else {
386 fsName = segments->fVSOutputs.back().accessName();
387 }
388 segments->fFSInputs.push_back();
389 segments->fFSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000390 segments->fFSInputs.back().setTypeModifier(
391 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000392 segments->fFSInputs.back().setName(*fsName);
393 if (fsInName) {
394 *fsInName = fsName->c_str();
395 }
396}
397
398// version of above that adds a stage number to the
399// the var name (for uniqueness)
tomhudson@google.com168e6342012-04-18 17:49:20 +0000400void append_varying(GrSLType type,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000401 const char* name,
402 int stageNum,
403 ShaderCodeSegments* segments,
404 const char** vsOutName = NULL,
405 const char** fsInName = NULL) {
406 GrStringBuilder nameWithStage(name);
407 nameWithStage.appendS32(stageNum);
408 append_varying(type, nameWithStage.c_str(), segments, vsOutName, fsInName);
409}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000410}
411
bsalomon@google.com96399942012-02-13 14:39:16 +0000412void GrGLProgram::genEdgeCoverage(const GrGLContextInfo& gl,
bsalomon@google.com66105672011-09-15 15:12:00 +0000413 GrVertexLayout layout,
414 CachedData* programData,
415 GrStringBuilder* coverageVar,
416 ShaderCodeSegments* segments) const {
417 if (fProgramDesc.fEdgeAANumEdges > 0) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000418 segments->fFSUnis.push_back().set(kVec3f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000419 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000420 EDGES_UNI_NAME,
421 fProgramDesc.fEdgeAANumEdges);
bsalomon@google.com66105672011-09-15 15:12:00 +0000422 programData->fUniLocations.fEdgesUni = kUseUniform;
423 int count = fProgramDesc.fEdgeAANumEdges;
424 segments->fFSCode.append(
425 "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n");
426 for (int i = 0; i < count; i++) {
427 segments->fFSCode.append("\tfloat a");
428 segments->fFSCode.appendS32(i);
429 segments->fFSCode.append(" = clamp(dot(" EDGES_UNI_NAME "[");
430 segments->fFSCode.appendS32(i);
431 segments->fFSCode.append("], pos), 0.0, 1.0);\n");
432 }
433 if (fProgramDesc.fEdgeAAConcave && (count & 0x01) == 0) {
434 // For concave polys, we consider the edges in pairs.
435 segments->fFSFunctions.append("float cross2(vec2 a, vec2 b) {\n");
436 segments->fFSFunctions.append("\treturn dot(a, vec2(b.y, -b.x));\n");
437 segments->fFSFunctions.append("}\n");
438 for (int i = 0; i < count; i += 2) {
439 segments->fFSCode.appendf("\tfloat eb%d;\n", i / 2);
440 segments->fFSCode.appendf("\tif (cross2(" EDGES_UNI_NAME "[%d].xy, " EDGES_UNI_NAME "[%d].xy) < 0.0) {\n", i, i + 1);
441 segments->fFSCode.appendf("\t\teb%d = a%d * a%d;\n", i / 2, i, i + 1);
442 segments->fFSCode.append("\t} else {\n");
443 segments->fFSCode.appendf("\t\teb%d = a%d + a%d - a%d * a%d;\n", i / 2, i, i + 1, i, i + 1);
444 segments->fFSCode.append("\t}\n");
445 }
446 segments->fFSCode.append("\tfloat edgeAlpha = ");
447 for (int i = 0; i < count / 2 - 1; i++) {
448 segments->fFSCode.appendf("min(eb%d, ", i);
449 }
450 segments->fFSCode.appendf("eb%d", count / 2 - 1);
451 for (int i = 0; i < count / 2 - 1; i++) {
452 segments->fFSCode.append(")");
453 }
454 segments->fFSCode.append(";\n");
455 } else {
456 segments->fFSCode.append("\tfloat edgeAlpha = ");
457 for (int i = 0; i < count - 1; i++) {
458 segments->fFSCode.appendf("min(a%d * a%d, ", i, i + 1);
459 }
460 segments->fFSCode.appendf("a%d * a0", count - 1);
461 for (int i = 0; i < count - 1; i++) {
462 segments->fFSCode.append(")");
463 }
464 segments->fFSCode.append(";\n");
465 }
466 *coverageVar = "edgeAlpha";
467 } else if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000468 const char *vsName, *fsName;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000469 append_varying(kVec4f_GrSLType, "Edge", segments,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000470 &vsName, &fsName);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000471 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000472 GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000473 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
bsalomon@google.com93c96602012-04-27 13:05:21 +0000474 switch (fProgramDesc.fVertexEdgeType) {
475 case GrDrawState::kHairLine_EdgeType:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000476 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000477 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com93c96602012-04-27 13:05:21 +0000478 break;
479 case GrDrawState::kQuad_EdgeType:
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000480 segments->fFSCode.append("\tfloat edgeAlpha;\n");
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000481 // keep the derivative instructions outside the conditional
482 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
483 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
484 segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
485 // today we know z and w are in device space. We could use derivatives
486 segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName);
487 segments->fFSCode.append ("\t} else {\n");
488 segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
489 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
490 fsName, fsName);
491 segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000492 segments->fFSCode.append("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n"
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000493 "\t}\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000494 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000495 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
496 }
bsalomon@google.com93c96602012-04-27 13:05:21 +0000497 break;
498 case GrDrawState::kHairQuad_EdgeType:
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000499 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
500 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000501 segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
502 "\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
503 fsName, fsName);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000504 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000505 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
506 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com96399942012-02-13 14:39:16 +0000507 if (kES2_GrGLBinding == gl.binding()) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000508 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
509 }
bsalomon@google.com93c96602012-04-27 13:05:21 +0000510 break;
511 case GrDrawState::kCircle_EdgeType:
512 segments->fFSCode.append("\tfloat edgeAlpha;\n");
513 segments->fFSCode.appendf("\tfloat d = distance(gl_FragCoord.xy, %s.xy);\n", fsName);
514 segments->fFSCode.appendf("\tfloat outerAlpha = smoothstep(d - 0.5, d + 0.5, %s.z);\n", fsName);
515 segments->fFSCode.appendf("\tfloat innerAlpha = %s.w == 0.0 ? 1.0 : smoothstep(%s.w - 0.5, %s.w + 0.5, d);\n", fsName, fsName, fsName);
516 segments->fFSCode.append("\tedgeAlpha = outerAlpha * innerAlpha;\n");
517 break;
518 default:
519 GrCrash("Unknown Edge Type!");
520 break;
bsalomon@google.com66105672011-09-15 15:12:00 +0000521 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000522 *coverageVar = "edgeAlpha";
523 } else {
524 coverageVar->reset();
525 }
526}
527
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000528namespace {
529
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000530void genInputColor(GrGLProgram::ProgramDesc::ColorInput colorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000531 GrGLProgram::CachedData* programData,
532 ShaderCodeSegments* segments,
533 GrStringBuilder* inColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000534 switch (colorInput) {
535 case GrGLProgram::ProgramDesc::kAttribute_ColorInput: {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000536 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000537 GrGLShaderVar::kAttribute_TypeModifier,
538 COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000539 const char *vsName, *fsName;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000540 append_varying(kVec4f_GrSLType, "Color", segments, &vsName, &fsName);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000541 segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
542 *inColor = fsName;
543 } break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000544 case GrGLProgram::ProgramDesc::kUniform_ColorInput:
tomhudson@google.com168e6342012-04-18 17:49:20 +0000545 segments->fFSUnis.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000546 GrGLShaderVar::kUniform_TypeModifier,
547 COL_UNI_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000548 programData->fUniLocations.fColorUni = kUseUniform;
549 *inColor = COL_UNI_NAME;
550 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000551 case GrGLProgram::ProgramDesc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000552 GrAssert(!"needComputedColor should be false.");
553 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000554 case GrGLProgram::ProgramDesc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000555 break;
556 default:
557 GrCrash("Unknown color type.");
558 break;
559 }
560}
561
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000562void genAttributeCoverage(ShaderCodeSegments* segments,
563 GrStringBuilder* inOutCoverage) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000564 segments->fVSAttrs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000565 GrGLShaderVar::kAttribute_TypeModifier,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000566 COV_ATTR_NAME);
567 const char *vsName, *fsName;
tomhudson@google.com168e6342012-04-18 17:49:20 +0000568 append_varying(kVec4f_GrSLType, "Coverage",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000569 segments, &vsName, &fsName);
570 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000571 if (inOutCoverage->size()) {
572 segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n",
573 fsName, inOutCoverage->c_str());
574 *inOutCoverage = "attrCoverage";
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000575 } else {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000576 *inOutCoverage = fsName;
577 }
578}
579
580void genUniformCoverage(ShaderCodeSegments* segments,
581 GrGLProgram::CachedData* programData,
582 GrStringBuilder* inOutCoverage) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000583 segments->fFSUnis.push_back().set(kVec4f_GrSLType,
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000584 GrGLShaderVar::kUniform_TypeModifier,
585 COV_UNI_NAME);
586 programData->fUniLocations.fCoverageUni = kUseUniform;
587 if (inOutCoverage->size()) {
588 segments->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
589 COV_UNI_NAME, inOutCoverage->c_str());
590 *inOutCoverage = "uniCoverage";
591 } else {
592 *inOutCoverage = COV_UNI_NAME;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000593 }
594}
595
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000596}
597
bsalomon@google.com96399942012-02-13 14:39:16 +0000598void GrGLProgram::genGeometryShader(const GrGLContextInfo& gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000599 ShaderCodeSegments* segments) const {
600#if GR_GL_EXPERIMENTAL_GS
601 if (fProgramDesc.fExperimentalGS) {
bsalomon@google.com96399942012-02-13 14:39:16 +0000602 GrAssert(gl.glslGeneration() >= k150_GrGLSLGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000603 segments->fGSHeader.append("layout(triangles) in;\n"
604 "layout(triangle_strip, max_vertices = 6) out;\n");
605 segments->fGSCode.append("void main() {\n"
606 "\tfor (int i = 0; i < 3; ++i) {\n"
607 "\t\tgl_Position = gl_in[i].gl_Position;\n");
608 if (this->fProgramDesc.fEmitsPointSize) {
609 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
610 }
611 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
612 int count = segments->fGSInputs.count();
613 for (int i = 0; i < count; ++i) {
614 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
615 segments->fGSOutputs[i].getName().c_str(),
616 segments->fGSInputs[i].getName().c_str());
617 }
618 segments->fGSCode.append("\t\tEmitVertex();\n"
619 "\t}\n"
620 "\tEndPrimitive();\n"
621 "}\n");
622 }
623#endif
624}
625
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000626const char* GrGLProgram::adjustInColor(const GrStringBuilder& inColor) const {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000627 if (inColor.size()) {
628 return inColor.c_str();
629 } else {
630 if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) {
631 return all_ones_vec(4);
632 } else {
633 return all_zeros_vec(4);
634 }
635 }
636}
637
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000638// If this destructor is in the header file, we must include GrGLProgramStage
639// instead of just forward-declaring it.
640GrGLProgram::CachedData::~CachedData() {
641 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
642 delete fCustomStage[i];
643 }
644}
645
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000646
bsalomon@google.com96399942012-02-13 14:39:16 +0000647bool GrGLProgram::genProgram(const GrGLContextInfo& gl,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000648 GrCustomStage** customStages,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000649 GrGLProgram::CachedData* programData) const {
junov@google.comf93e7172011-03-31 21:26:24 +0000650
651 ShaderCodeSegments segments;
652 const uint32_t& layout = fProgramDesc.fVertexLayout;
653
bsalomon@google.com91961302011-05-09 18:39:58 +0000654 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000655
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000656#if GR_GL_EXPERIMENTAL_GS
657 segments.fUsesGS = fProgramDesc.fExperimentalGS;
658#endif
659
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000660 SkXfermode::Coeff colorCoeff, uniformCoeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000661 bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000662 // The rest of transfer mode color filters have not been implemented
663 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
664 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000665 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
666 (fProgramDesc.fColorFilterXfermode),
667 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000668 GR_DEBUGASSERT(success);
669 } else {
670 colorCoeff = SkXfermode::kOne_Coeff;
671 uniformCoeff = SkXfermode::kZero_Coeff;
672 }
673
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000674 // no need to do the color filter / matrix at all if coverage is 0. The
675 // output color is scaled by the coverage. All the dual source outputs are
676 // scaled by the coverage as well.
677 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) {
678 colorCoeff = SkXfermode::kZero_Coeff;
679 uniformCoeff = SkXfermode::kZero_Coeff;
680 applyColorMatrix = false;
681 }
682
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000683 // If we know the final color is going to be all zeros then we can
684 // simplify the color filter coeffecients. needComputedColor will then
685 // come out false below.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000686 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000687 colorCoeff = SkXfermode::kZero_Coeff;
688 if (SkXfermode::kDC_Coeff == uniformCoeff ||
689 SkXfermode::kDA_Coeff == uniformCoeff) {
690 uniformCoeff = SkXfermode::kZero_Coeff;
691 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
692 SkXfermode::kIDA_Coeff == uniformCoeff) {
693 uniformCoeff = SkXfermode::kOne_Coeff;
694 }
695 }
696
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000697 bool needColorFilterUniform;
698 bool needComputedColor;
699 needBlendInputs(uniformCoeff, colorCoeff,
700 &needColorFilterUniform, &needComputedColor);
701
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000702 // the dual source output has no canonical var name, have to
703 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000704 bool dualSourceOutputWritten = false;
bsalomon@google.com96399942012-02-13 14:39:16 +0000705 segments.fHeader.printf(GrGetGLSLVersionDecl(gl.binding(),
706 gl.glslGeneration()));
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000707
708 GrGLShaderVar colorOutput;
bsalomon@google.com96399942012-02-13 14:39:16 +0000709 bool isColorDeclared = GrGLSLSetupFSColorOuput(gl.glslGeneration(),
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000710 declared_color_output_name(),
711 &colorOutput);
712 if (isColorDeclared) {
713 segments.fFSOutputs.push_back(colorOutput);
714 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000715
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000716#if GR_GL_ATTRIBUTE_MATRICES
tomhudson@google.com168e6342012-04-18 17:49:20 +0000717 segments.fVSAttrs.push_back().set(kMat33f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000718 GrGLShaderVar::kAttribute_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000719 programData->fUniLocations.fViewMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +0000720#else
tomhudson@google.com168e6342012-04-18 17:49:20 +0000721 segments.fVSUnis.push_back().set(kMat33f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000722 GrGLShaderVar::kUniform_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000723 programData->fUniLocations.fViewMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +0000724#endif
tomhudson@google.com168e6342012-04-18 17:49:20 +0000725 segments.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000726 GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000727
bsalomon@google.com91961302011-05-09 18:39:58 +0000728 segments.fVSCode.append(
729 "void main() {\n"
730 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
731 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000732
bsalomon@google.com91961302011-05-09 18:39:58 +0000733 // incoming color to current stage being processed.
734 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000735
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000736 if (needComputedColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000737 genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000738 programData, &segments, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000739 }
740
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000741 // we output point size in the GS if present
742 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000743 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000744 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000745
bsalomon@google.com91961302011-05-09 18:39:58 +0000746 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000747
748 // add texture coordinates that are used to the list of vertex attr decls
tomhudson@google.com93813632011-10-27 20:21:16 +0000749 GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords];
750 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000751 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000752 tex_attr_name(t, texCoordAttrs + t);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000753 segments.fVSAttrs.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000754 GrGLShaderVar::kAttribute_TypeModifier,
755 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000756 }
757 }
758
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000759 ///////////////////////////////////////////////////////////////////////////
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000760 // Convert generic effect representation to GL-specific backend so they
761 // can be accesseed in genStageCode() and in subsequent uses of
762 // programData.
763 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
764 GrCustomStage* customStage = customStages[s];
765 if (NULL != customStage) {
766 GrGLProgramStageFactory* factory = customStage->getGLFactory();
767 programData->fCustomStage[s] =
768 factory->createGLInstance(customStage);
769 } else {
770 programData->fCustomStage[s] = NULL;
771 }
772 }
773
774 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000775 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000776
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000777 // if we have color stages string them together, feeding the output color
778 // of each to the next and generating code for each stage.
779 if (needComputedColor) {
780 GrStringBuilder outColor;
781 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000782 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000783 // create var to hold stage result
784 outColor = "color";
785 outColor.appendS32(s);
786 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
787
788 const char* inCoords;
789 // figure out what our input coords are
790 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
791 layout) {
792 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000793 } else {
794 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
795 // we better have input tex coordinates if stage is enabled.
796 GrAssert(tcIdx >= 0);
797 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000798 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000799 }
800
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000801 this->genStageCode(gl,
802 s,
803 fProgramDesc.fStages[s],
804 inColor.size() ? inColor.c_str() : NULL,
805 outColor.c_str(),
806 inCoords,
807 &segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000808 &programData->fUniLocations.fStages[s],
809 programData->fCustomStage[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000810 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000811 }
812 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000813 }
Scroggo97c88c22011-05-11 14:05:25 +0000814
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000815 // if have all ones or zeros for the "dst" input to the color filter then we
816 // may be able to make additional optimizations.
817 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000818 GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000819 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
820 SkXfermode::kIDA_Coeff == uniformCoeff;
821 if (uniformCoeffIsZero) {
822 uniformCoeff = SkXfermode::kZero_Coeff;
823 bool bogus;
824 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
825 &needColorFilterUniform, &bogus);
826 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000827 }
828 if (needColorFilterUniform) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000829 segments.fFSUnis.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000830 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000831 COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000832 programData->fUniLocations.fColorFilterUni = kUseUniform;
833 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000834 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000835 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000836 SkXfermode::kZero_Coeff == colorCoeff &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000837 !applyColorMatrix) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000838 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000839 colorOutput.getName().c_str(),
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000840 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000841 wroteFragColorZero = true;
842 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000843 segments.fFSCode.append("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000844 const char* color = adjustInColor(inColor);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000845 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000846 colorCoeff, color);
847 inColor = "filteredColor";
848 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000849 if (applyColorMatrix) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000850 segments.fFSUnis.push_back().set(kMat44f_GrSLType,
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000851 GrGLShaderVar::kUniform_TypeModifier,
852 COL_MATRIX_UNI_NAME);
tomhudson@google.com168e6342012-04-18 17:49:20 +0000853 segments.fFSUnis.push_back().set(kVec4f_GrSLType,
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000854 GrGLShaderVar::kUniform_TypeModifier,
855 COL_MATRIX_VEC_UNI_NAME);
856 programData->fUniLocations.fColorMatrixUni = kUseUniform;
857 programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +0000858 segments.fFSCode.append("\tvec4 matrixedColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000859 const char* color = adjustInColor(inColor);
860 addColorMatrix(&segments.fFSCode, "matrixedColor", color);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000861 inColor = "matrixedColor";
862 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000863
864 ///////////////////////////////////////////////////////////////////////////
865 // compute the partial coverage (coverage stages and edge aa)
866
867 GrStringBuilder inCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000868 bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput ==
869 fProgramDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000870 // we don't need to compute coverage at all if we know the final shader
871 // output will be zero and we don't have a dual src blend output.
872 if (!wroteFragColorZero ||
873 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000874
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000875 if (!coverageIsZero) {
876 this->genEdgeCoverage(gl,
877 layout,
878 programData,
879 &inCoverage,
880 &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000881
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000882 switch (fProgramDesc.fCoverageInput) {
883 case ProgramDesc::kSolidWhite_ColorInput:
884 // empty string implies solid white
885 break;
886 case ProgramDesc::kAttribute_ColorInput:
887 genAttributeCoverage(&segments, &inCoverage);
888 break;
889 case ProgramDesc::kUniform_ColorInput:
890 genUniformCoverage(&segments, programData, &inCoverage);
891 break;
892 default:
893 GrCrash("Unexpected input coverage.");
894 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000895
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000896 GrStringBuilder outCoverage;
897 const int& startStage = fProgramDesc.fFirstCoverageStage;
898 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
899 if (fProgramDesc.fStages[s].isEnabled()) {
900 // create var to hold stage output
901 outCoverage = "coverage";
902 outCoverage.appendS32(s);
903 segments.fFSCode.appendf("\tvec4 %s;\n",
904 outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000905
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000906 const char* inCoords;
907 // figure out what our input coords are
908 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
909 layout) {
910 inCoords = POS_ATTR_NAME;
911 } else {
912 int tcIdx =
913 GrDrawTarget::VertexTexCoordsForStage(s, layout);
914 // we better have input tex coordinates if stage is
915 // enabled.
916 GrAssert(tcIdx >= 0);
917 GrAssert(texCoordAttrs[tcIdx].size());
918 inCoords = texCoordAttrs[tcIdx].c_str();
919 }
920
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000921 this->genStageCode(gl, s,
922 fProgramDesc.fStages[s],
923 inCoverage.size() ? inCoverage.c_str() : NULL,
924 outCoverage.c_str(),
925 inCoords,
926 &segments,
927 &programData->fUniLocations.fStages[s],
928 programData->fCustomStage[s]);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000929 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000930 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000931 }
932 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000933 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
tomhudson@google.com168e6342012-04-18 17:49:20 +0000934 segments.fFSOutputs.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000935 GrGLShaderVar::kOut_TypeModifier,
936 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000937 bool outputIsZero = coverageIsZero;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000938 GrStringBuilder coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000939 if (!outputIsZero &&
940 ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000941 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
942 if (!inColor.size()) {
943 outputIsZero = true;
944 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000945 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000946 ProgramDesc::kCoverageISA_DualSrcOutput) {
947 coeff.printf("(1 - %s.a)", inColor.c_str());
948 } else {
949 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
950 }
951 }
952 }
953 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000954 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000955 dual_source_output_name(),
956 all_zeros_vec(4));
957 } else {
958 modulate_helper(dual_source_output_name(),
959 coeff.c_str(),
960 inCoverage.c_str(),
961 &segments.fFSCode);
962 }
963 dualSourceOutputWritten = true;
964 }
junov@google.comf93e7172011-03-31 21:26:24 +0000965 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000966
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000967 ///////////////////////////////////////////////////////////////////////////
968 // combine color and coverage as frag color
969
970 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000971 if (coverageIsZero) {
972 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000973 colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000974 all_zeros_vec(4));
975 } else {
bsalomon@google.come55fd0f2012-02-10 15:56:06 +0000976 modulate_helper(colorOutput.getName().c_str(),
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000977 inColor.c_str(),
978 inCoverage.c_str(),
979 &segments.fFSCode);
980 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000981 if (ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig ==
982 fProgramDesc.fOutputConfig) {
983 segments.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(floor(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n",
984 colorOutput.getName().c_str(),
985 colorOutput.getName().c_str(),
986 colorOutput.getName().c_str(),
987 colorOutput.getName().c_str(),
988 colorOutput.getName().c_str());
989 } else if (ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig ==
990 fProgramDesc.fOutputConfig) {
991 segments.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(ceil(%s.rgb / %s.a * 255.0)/255.0, %s.a);\n",
992 colorOutput.getName().c_str(),
993 colorOutput.getName().c_str(),
994 colorOutput.getName().c_str(),
995 colorOutput.getName().c_str(),
996 colorOutput.getName().c_str());
bsalomon@google.comc4364992011-11-07 15:54:49 +0000997 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000998 }
999
bsalomon@google.com91961302011-05-09 18:39:58 +00001000 segments.fVSCode.append("}\n");
1001 segments.fFSCode.append("}\n");
1002
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001003 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001004 // insert GS
1005#if GR_DEBUG
bsalomon@google.com96399942012-02-13 14:39:16 +00001006 this->genGeometryShader(gl, &segments);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001007#endif
1008
1009 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001010 // compile and setup attribs and unis
1011
bsalomon@google.com96399942012-02-13 14:39:16 +00001012 if (!CompileShaders(gl, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001013 return false;
1014 }
1015
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001016 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001017 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001018 dualSourceOutputWritten,
1019 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001020 return false;
1021 }
1022
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001023 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +00001024
1025 return true;
1026}
1027
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001028namespace {
1029
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001030inline void expand_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +00001031 const GrGLContextInfo& gl,
1032 GrStringBuilder* string) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001033 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001034 for (int i = 0; i < count; ++i) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001035 vars[i].appendDecl(gl, string);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001036 }
1037}
1038
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001039inline void print_shader(int stringCnt,
1040 const char** strings,
1041 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001042 for (int i = 0; i < stringCnt; ++i) {
1043 if (NULL == stringLengths || stringLengths[i] < 0) {
1044 GrPrintf(strings[i]);
1045 } else {
1046 GrPrintf("%.*s", stringLengths[i], strings[i]);
1047 }
1048 }
1049}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001050
1051typedef SkTArray<const char*, true> StrArray;
1052#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
1053
1054typedef SkTArray<int, true> LengthArray;
1055#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
1056
1057// these shouldn't relocate
1058typedef GrTAllocator<GrStringBuilder> TempArray;
1059#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
1060
1061inline void append_string(const GrStringBuilder& str,
1062 StrArray* strings,
1063 LengthArray* lengths) {
1064 int length = (int) str.size();
1065 if (length) {
1066 strings->push_back(str.c_str());
1067 lengths->push_back(length);
1068 }
1069 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001070}
1071
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001072inline void append_decls(const VarArray& vars,
bsalomon@google.com96399942012-02-13 14:39:16 +00001073 const GrGLContextInfo& gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001074 StrArray* strings,
1075 LengthArray* lengths,
bsalomon@google.com96399942012-02-13 14:39:16 +00001076 TempArray* temp) {
1077 expand_decls(vars, gl, &temp->push_back());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001078 append_string(temp->back(), strings, lengths);
1079}
1080
1081}
1082
bsalomon@google.com96399942012-02-13 14:39:16 +00001083bool GrGLProgram::CompileShaders(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001084 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +00001085 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001086 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +00001087
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001088 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
1089 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
1090 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
1091
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001092 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001093 GrStringBuilder inputs;
1094 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001095
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001096 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001097 append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps);
1098 append_decls(segments.fVSAttrs, gl, &strs, &lengths, &temps);
1099 append_decls(segments.fVSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001100 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001101
1102#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001103 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001104 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001105#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001106
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001107 programData->fVShaderID =
1108 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
1109 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001110
bsalomon@google.com91961302011-05-09 18:39:58 +00001111 if (!programData->fVShaderID) {
1112 return false;
1113 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001114 if (segments.fUsesGS) {
1115 strs.reset();
1116 lengths.reset();
1117 temps.reset();
1118 append_string(segments.fHeader, &strs, &lengths);
1119 append_string(segments.fGSHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001120 append_decls(segments.fGSInputs, gl, &strs, &lengths, &temps);
1121 append_decls(segments.fGSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001122 append_string(segments.fGSCode, &strs, &lengths);
1123#if PRINT_SHADERS
1124 print_shader(strs.count(), &strs[0], &lengths[0]);
1125 GrPrintf("\n");
1126#endif
1127 programData->fGShaderID =
1128 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
1129 &strs[0], &lengths[0]);
1130 } else {
1131 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +00001132 }
junov@google.comf93e7172011-03-31 21:26:24 +00001133
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001134 strs.reset();
1135 lengths.reset();
1136 temps.reset();
1137
1138 append_string(segments.fHeader, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001139 GrStringBuilder precisionStr(GrGetGLSLShaderPrecisionDecl(gl.binding()));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001140 append_string(precisionStr, &strs, &lengths);
bsalomon@google.com96399942012-02-13 14:39:16 +00001141 append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps);
1142 append_decls(segments.fFSInputs, gl, &strs, &lengths, &temps);
bsalomon@google.com373a6632011-10-19 20:43:20 +00001143 // We shouldn't have declared outputs on 1.10
bsalomon@google.com96399942012-02-13 14:39:16 +00001144 GrAssert(k110_GrGLSLGeneration != gl.glslGeneration() ||
tomhudson@google.com086e5352011-12-08 14:44:10 +00001145 segments.fFSOutputs.empty());
bsalomon@google.com96399942012-02-13 14:39:16 +00001146 append_decls(segments.fFSOutputs, gl, &strs, &lengths, &temps);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001147 append_string(segments.fFSFunctions, &strs, &lengths);
1148 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001149
1150#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001151 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001152 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001153#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001154
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001155 programData->fFShaderID =
1156 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
1157 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001158
bsalomon@google.com91961302011-05-09 18:39:58 +00001159 if (!programData->fFShaderID) {
1160 return false;
junov@google.comf93e7172011-03-31 21:26:24 +00001161 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001162
bsalomon@google.com91961302011-05-09 18:39:58 +00001163 return true;
junov@google.comf93e7172011-03-31 21:26:24 +00001164}
1165
bsalomon@google.com96399942012-02-13 14:39:16 +00001166#define GL_CALL(X) GR_GL_CALL(gl.interface(), X)
1167#define GL_CALL_RET(R, X) GR_GL_CALL_RET(gl.interface(), R, X)
1168
1169GrGLuint GrGLProgram::CompileShader(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001170 GrGLenum type,
1171 int stringCnt,
1172 const char** strings,
1173 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001174 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
1175 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
1176
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001177 GrGLuint shader;
bsalomon@google.com96399942012-02-13 14:39:16 +00001178 GL_CALL_RET(shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +00001179 if (0 == shader) {
1180 return 0;
1181 }
1182
1183 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001184 GL_CALL(ShaderSource(shader, stringCnt, strings, stringLengths));
1185 GL_CALL(CompileShader(shader));
1186 GL_CALL(GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +00001187
1188 if (!compiled) {
1189 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001190 GL_CALL(GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001191 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001192 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001193 // retrieve length even though we don't need it to workaround
1194 // bug in chrome cmd buffer param validation.
1195 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001196 GL_CALL(GetShaderInfoLog(shader, infoLen+1,
1197 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001198 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001199 GrPrintf("\n%s", log.get());
1200 }
1201 GrAssert(!"Shader compilation failed!");
bsalomon@google.com96399942012-02-13 14:39:16 +00001202 GL_CALL(DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001203 return 0;
1204 }
1205 return shader;
1206}
1207
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001208bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com96399942012-02-13 14:39:16 +00001209 const GrGLContextInfo& gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001210 GrStringBuilder texCoordAttrNames[],
1211 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001212 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001213 CachedData* programData) const {
bsalomon@google.com96399942012-02-13 14:39:16 +00001214 GL_CALL_RET(programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001215 if (!programData->fProgramID) {
1216 return false;
1217 }
1218 const GrGLint& progID = programData->fProgramID;
1219
bsalomon@google.com96399942012-02-13 14:39:16 +00001220 GL_CALL(AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001221 if (programData->fGShaderID) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001222 GL_CALL(AttachShader(progID, programData->fGShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001223 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001224 GL_CALL(AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001225
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001226 if (bindColorOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001227 GL_CALL(BindFragDataLocation(programData->fProgramID,
1228 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001229 }
1230 if (bindDualSrcOut) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001231 GL_CALL(BindFragDataLocationIndexed(programData->fProgramID,
1232 0, 1, dual_source_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001233 }
1234
bsalomon@google.com91961302011-05-09 18:39:58 +00001235 // Bind the attrib locations to same values for all shaders
bsalomon@google.com96399942012-02-13 14:39:16 +00001236 GL_CALL(BindAttribLocation(progID, PositionAttributeIdx(), POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +00001237 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001238 if (texCoordAttrNames[t].size()) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001239 GL_CALL(BindAttribLocation(progID,
1240 TexCoordAttributeIdx(t),
1241 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001242 }
1243 }
1244
bsalomon@google.com91961302011-05-09 18:39:58 +00001245 if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001246 GL_CALL(BindAttribLocation(progID,
1247 ViewMatrixAttributeIdx(),
1248 VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001249 }
1250
tomhudson@google.com93813632011-10-27 20:21:16 +00001251 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com72b4fcb2011-05-09 20:47:34 +00001252 const StageUniLocations& unis = programData->fUniLocations.fStages[s];
1253 if (kSetAsAttribute == unis.fTextureMatrixUni) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001254 GrStringBuilder matName;
1255 tex_matrix_name(s, &matName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001256 GL_CALL(BindAttribLocation(progID,
1257 TextureMatrixAttributeIdx(s),
1258 matName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001259 }
1260 }
1261
bsalomon@google.com96399942012-02-13 14:39:16 +00001262 GL_CALL(BindAttribLocation(progID, ColorAttributeIdx(), COL_ATTR_NAME));
1263 GL_CALL(BindAttribLocation(progID, CoverageAttributeIdx(), COV_ATTR_NAME));
1264 GL_CALL(BindAttribLocation(progID, EdgeAttributeIdx(), EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001265
bsalomon@google.com96399942012-02-13 14:39:16 +00001266 GL_CALL(LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001267
1268 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001269 GL_CALL(GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001270 if (!linked) {
1271 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001272 GL_CALL(GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001273 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001274 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001275 // retrieve length even though we don't need it to workaround
1276 // bug in chrome cmd buffer param validation.
1277 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com96399942012-02-13 14:39:16 +00001278 GL_CALL(GetProgramInfoLog(progID,
1279 infoLen+1,
1280 &length,
1281 (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001282 GrPrintf((char*)log.get());
1283 }
1284 GrAssert(!"Error linking program");
bsalomon@google.com96399942012-02-13 14:39:16 +00001285 GL_CALL(DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001286 programData->fProgramID = 0;
1287 return false;
1288 }
1289 return true;
1290}
1291
bsalomon@google.com96399942012-02-13 14:39:16 +00001292void GrGLProgram::getUniformLocationsAndInitCache(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001293 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001294 const GrGLint& progID = programData->fProgramID;
1295
1296 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001297 GL_CALL_RET(programData->fUniLocations.fViewMatrixUni,
1298 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001299 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1300 }
1301 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001302 GL_CALL_RET(programData->fUniLocations.fColorUni,
1303 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001304 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1305 }
Scroggo97c88c22011-05-11 14:05:25 +00001306 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001307 GL_CALL_RET(programData->fUniLocations.fColorFilterUni,
1308 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001309 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1310 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001311
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001312 if (kUseUniform == programData->fUniLocations.fColorMatrixUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001313 GL_CALL_RET(programData->fUniLocations.fColorMatrixUni,
1314 GetUniformLocation(progID, COL_MATRIX_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001315 }
1316
1317 if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001318 GL_CALL_RET(programData->fUniLocations.fColorMatrixVecUni,
1319 GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001320 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001321 if (kUseUniform == programData->fUniLocations.fCoverageUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001322 GL_CALL_RET(programData->fUniLocations.fCoverageUni,
1323 GetUniformLocation(progID, COV_UNI_NAME));
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001324 GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni);
1325 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001326
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001327 if (kUseUniform == programData->fUniLocations.fEdgesUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001328 GL_CALL_RET(programData->fUniLocations.fEdgesUni,
1329 GetUniformLocation(progID, EDGES_UNI_NAME));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001330 GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni);
1331 } else {
1332 programData->fUniLocations.fEdgesUni = kUnusedUniform;
1333 }
1334
tomhudson@google.com93813632011-10-27 20:21:16 +00001335 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001336 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001337 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001338 if (kUseUniform == locations.fTextureMatrixUni) {
1339 GrStringBuilder texMName;
1340 tex_matrix_name(s, &texMName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001341 GL_CALL_RET(locations.fTextureMatrixUni,
1342 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001343 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1344 }
1345
1346 if (kUseUniform == locations.fSamplerUni) {
1347 GrStringBuilder samplerName;
1348 sampler_name(s, &samplerName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001349 GL_CALL_RET(locations.fSamplerUni,
1350 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001351 GrAssert(kUnusedUniform != locations.fSamplerUni);
1352 }
1353
1354 if (kUseUniform == locations.fNormalizedTexelSizeUni) {
1355 GrStringBuilder texelSizeName;
1356 normalized_texel_size_name(s, &texelSizeName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001357 GL_CALL_RET(locations.fNormalizedTexelSizeUni,
1358 GetUniformLocation(progID, texelSizeName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001359 GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni);
1360 }
1361
1362 if (kUseUniform == locations.fRadial2Uni) {
1363 GrStringBuilder radial2ParamName;
1364 radial2_param_name(s, &radial2ParamName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001365 GL_CALL_RET(locations.fRadial2Uni,
1366 GetUniformLocation(progID, radial2ParamName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001367 GrAssert(kUnusedUniform != locations.fRadial2Uni);
1368 }
junov@google.com6acc9b32011-05-16 18:32:07 +00001369
1370 if (kUseUniform == locations.fTexDomUni) {
1371 GrStringBuilder texDomName;
1372 tex_domain_name(s, &texDomName);
bsalomon@google.com96399942012-02-13 14:39:16 +00001373 GL_CALL_RET(locations.fTexDomUni,
1374 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001375 GrAssert(kUnusedUniform != locations.fTexDomUni);
1376 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001377
1378 GrStringBuilder kernelName, imageIncrementName;
1379 convolve_param_names(s, &kernelName, &imageIncrementName);
1380 if (kUseUniform == locations.fKernelUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001381 GL_CALL_RET(locations.fKernelUni,
1382 GetUniformLocation(progID, kernelName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001383 GrAssert(kUnusedUniform != locations.fKernelUni);
1384 }
1385
1386 if (kUseUniform == locations.fImageIncrementUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001387 GL_CALL_RET(locations.fImageIncrementUni,
1388 GetUniformLocation(progID,
1389 imageIncrementName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001390 GrAssert(kUnusedUniform != locations.fImageIncrementUni);
1391 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001392
1393 if (NULL != programData->fCustomStage[s]) {
1394 programData->fCustomStage[s]->
1395 initUniforms(gl.interface(), progID);
1396 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001397 }
1398 }
bsalomon@google.com96399942012-02-13 14:39:16 +00001399 GL_CALL(UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001400
1401 // init sampler unis and set bogus values for state tracking
tomhudson@google.com93813632011-10-27 20:21:16 +00001402 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001403 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com96399942012-02-13 14:39:16 +00001404 GL_CALL(Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001405 }
1406 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
1407 programData->fRadial2CenterX1[s] = GR_ScalarMax;
1408 programData->fRadial2Radius0[s] = -GR_ScalarMax;
1409 programData->fTextureWidth[s] = -1;
1410 programData->fTextureHeight[s] = -1;
tomhudson@google.com24878f72012-03-22 14:44:46 +00001411 programData->fTextureDomain[s].setEmpty();
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001412 // Must not reset fStageOverride[] here.
bsalomon@google.com91961302011-05-09 18:39:58 +00001413 }
1414 programData->fViewMatrix = GrMatrix::InvalidMatrix();
1415 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001416 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001417}
1418
junov@google.comf93e7172011-03-31 21:26:24 +00001419//============================================================================
1420// Stage code generation
1421//============================================================================
1422
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001423namespace {
1424
1425bool isRadialMapping(GrGLProgram::StageDesc::CoordMapping mapping) {
1426 return
1427 (GrGLProgram::StageDesc::kRadial2Gradient_CoordMapping == mapping ||
1428 GrGLProgram::StageDesc::kRadial2GradientDegenerate_CoordMapping == mapping);
1429}
1430
tomhudson@google.comda668982011-12-07 15:06:29 +00001431GrGLShaderVar* genRadialVS(int stageNum,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001432 ShaderCodeSegments* segments,
1433 GrGLProgram::StageUniLocations* locations,
1434 const char** radial2VaryingVSName,
1435 const char** radial2VaryingFSName,
1436 const char* varyingVSName,
1437 int varyingDims, int coordDims) {
1438
1439 GrGLShaderVar* radial2FSParams = &segments->fFSUnis.push_back();
tomhudson@google.com168e6342012-04-18 17:49:20 +00001440 radial2FSParams->setType(kFloat_GrSLType);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001441 radial2FSParams->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001442 radial2FSParams->setArrayCount(6);
1443 radial2_param_name(stageNum, radial2FSParams->accessName());
1444 segments->fVSUnis.push_back(*radial2FSParams).setEmitPrecision(true);
1445
1446 locations->fRadial2Uni = kUseUniform;
1447
1448 // for radial grads without perspective we can pass the linear
1449 // part of the quadratic as a varying.
1450 if (varyingDims == coordDims) {
1451 GrAssert(2 == coordDims);
tomhudson@google.com168e6342012-04-18 17:49:20 +00001452 append_varying(kFloat_GrSLType,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001453 "Radial2BCoeff",
1454 stageNum,
1455 segments,
1456 radial2VaryingVSName,
1457 radial2VaryingFSName);
1458
tomhudson@google.comda668982011-12-07 15:06:29 +00001459 GrStringBuilder radial2p2;
1460 GrStringBuilder radial2p3;
1461 radial2FSParams->appendArrayAccess(2, &radial2p2);
1462 radial2FSParams->appendArrayAccess(3, &radial2p3);
1463
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001464 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
1465 const char* r2ParamName = radial2FSParams->getName().c_str();
tomhudson@google.comda668982011-12-07 15:06:29 +00001466 segments->fVSCode.appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
1467 *radial2VaryingVSName, radial2p2.c_str(),
1468 varyingVSName, radial2p3.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001469 }
1470
tomhudson@google.comda668982011-12-07 15:06:29 +00001471 return radial2FSParams;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001472}
1473
1474bool genRadial2GradientCoordMapping(int stageNum,
1475 ShaderCodeSegments* segments,
1476 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001477 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001478 GrStringBuilder& sampleCoords,
1479 GrStringBuilder& fsCoordName,
1480 int varyingDims,
1481 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001482 GrStringBuilder cName("c");
1483 GrStringBuilder ac4Name("ac4");
1484 GrStringBuilder rootName("root");
1485
1486 cName.appendS32(stageNum);
1487 ac4Name.appendS32(stageNum);
1488 rootName.appendS32(stageNum);
1489
tomhudson@google.comda668982011-12-07 15:06:29 +00001490 GrStringBuilder radial2p0;
1491 GrStringBuilder radial2p1;
1492 GrStringBuilder radial2p2;
1493 GrStringBuilder radial2p3;
1494 GrStringBuilder radial2p4;
1495 GrStringBuilder radial2p5;
1496 radial2Params->appendArrayAccess(0, &radial2p0);
1497 radial2Params->appendArrayAccess(1, &radial2p1);
1498 radial2Params->appendArrayAccess(2, &radial2p2);
1499 radial2Params->appendArrayAccess(3, &radial2p3);
1500 radial2Params->appendArrayAccess(4, &radial2p4);
1501 radial2Params->appendArrayAccess(5, &radial2p5);
1502
tomhudson@google.com5960d002011-10-19 20:21:48 +00001503 // if we were able to interpolate the linear component bVar is the varying
1504 // otherwise compute it
1505 GrStringBuilder bVar;
1506 if (coordDims == varyingDims) {
1507 bVar = radial2VaryingFSName;
1508 GrAssert(2 == varyingDims);
1509 } else {
1510 GrAssert(3 == varyingDims);
1511 bVar = "b";
1512 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001513 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1514 bVar.c_str(), radial2p2.c_str(),
1515 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001516 }
1517
1518 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001519 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001520 cName.c_str(), fsCoordName.c_str(),
1521 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001522 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001523 // ac4 = 4.0 * params[0] * c
tomhudson@google.comda668982011-12-07 15:06:29 +00001524 segments->fFSCode.appendf("\tfloat %s = %s * 4.0 * %s;\n",
1525 ac4Name.c_str(), radial2p0.c_str(),
tomhudson@google.com5960d002011-10-19 20:21:48 +00001526 cName.c_str());
1527
1528 // root = sqrt(b^2-4ac)
1529 // (abs to avoid exception due to fp precision)
1530 segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
1531 rootName.c_str(), bVar.c_str(), bVar.c_str(),
1532 ac4Name.c_str());
1533
1534 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
1535 // y coord is 0.5 (texture is effectively 1D)
tomhudson@google.comda668982011-12-07 15:06:29 +00001536 sampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)",
1537 bVar.c_str(), radial2p5.c_str(),
1538 rootName.c_str(), radial2p1.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001539 return true;
1540}
1541
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001542bool genRadial2GradientDegenerateCoordMapping(int stageNum,
1543 ShaderCodeSegments* segments,
1544 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001545 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001546 GrStringBuilder& sampleCoords,
1547 GrStringBuilder& fsCoordName,
1548 int varyingDims,
1549 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001550 GrStringBuilder cName("c");
1551
1552 cName.appendS32(stageNum);
1553
tomhudson@google.comda668982011-12-07 15:06:29 +00001554 GrStringBuilder radial2p2;
1555 GrStringBuilder radial2p3;
1556 GrStringBuilder radial2p4;
1557 radial2Params->appendArrayAccess(2, &radial2p2);
1558 radial2Params->appendArrayAccess(3, &radial2p3);
1559 radial2Params->appendArrayAccess(4, &radial2p4);
1560
tomhudson@google.com5960d002011-10-19 20:21:48 +00001561 // if we were able to interpolate the linear component bVar is the varying
1562 // otherwise compute it
1563 GrStringBuilder bVar;
1564 if (coordDims == varyingDims) {
1565 bVar = radial2VaryingFSName;
1566 GrAssert(2 == varyingDims);
1567 } else {
1568 GrAssert(3 == varyingDims);
1569 bVar = "b";
1570 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001571 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1572 bVar.c_str(), radial2p2.c_str(),
1573 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001574 }
1575
1576 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001577 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001578 cName.c_str(), fsCoordName.c_str(),
1579 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001580 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001581
1582 // x coord is: -c/b
1583 // y coord is 0.5 (texture is effectively 1D)
1584 sampleCoords.printf("vec2((-%s / %s), 0.5)", cName.c_str(), bVar.c_str());
1585 return true;
1586}
1587
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001588void gen2x2FS(int stageNum,
1589 ShaderCodeSegments* segments,
1590 GrGLProgram::StageUniLocations* locations,
1591 GrStringBuilder* sampleCoords,
1592 const char* samplerName,
1593 const char* texelSizeName,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001594 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001595 const char* fsOutColor,
1596 GrStringBuilder& texFunc,
1597 GrStringBuilder& modulate,
1598 bool complexCoord,
1599 int coordDims) {
1600 locations->fNormalizedTexelSizeUni = kUseUniform;
1601 if (complexCoord) {
1602 // assign the coord to a var rather than compute 4x.
1603 GrStringBuilder coordVar("tCoord");
1604 coordVar.appendS32(stageNum);
1605 segments->fFSCode.appendf("\t%s %s = %s;\n",
1606 float_vector_type_str(coordDims),
1607 coordVar.c_str(), sampleCoords->c_str());
1608 *sampleCoords = coordVar;
1609 }
1610 GrAssert(2 == coordDims);
1611 GrStringBuilder accumVar("accum");
1612 accumVar.appendS32(stageNum);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001613 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);
1614 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);
1615 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);
1616 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 +00001617 segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str());
1618
1619}
1620
1621void genConvolutionVS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001622 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001623 ShaderCodeSegments* segments,
1624 GrGLProgram::StageUniLocations* locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001625 GrGLShaderVar** kernel,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001626 const char** imageIncrementName,
1627 const char* varyingVSName) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001628 //GrGLShaderVar* kernel = &segments->fFSUnis.push_back();
1629 *kernel = &segments->fFSUnis.push_back();
tomhudson@google.com168e6342012-04-18 17:49:20 +00001630 (*kernel)->setType(kFloat_GrSLType);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001631 (*kernel)->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.comda668982011-12-07 15:06:29 +00001632 (*kernel)->setArrayCount(desc.fKernelWidth);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001633 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
tomhudson@google.com168e6342012-04-18 17:49:20 +00001634 imgInc->setType(kVec2f_GrSLType);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001635 imgInc->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001636
1637 convolve_param_names(stageNum,
tomhudson@google.comda668982011-12-07 15:06:29 +00001638 (*kernel)->accessName(),
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001639 imgInc->accessName());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001640 *imageIncrementName = imgInc->getName().c_str();
1641
1642 // need image increment in both VS and FS
1643 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1644
1645 locations->fKernelUni = kUseUniform;
1646 locations->fImageIncrementUni = kUseUniform;
1647 float scale = (desc.fKernelWidth - 1) * 0.5f;
1648 segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n",
1649 varyingVSName, scale, scale,
1650 *imageIncrementName);
1651}
1652
1653void genConvolutionFS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001654 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001655 ShaderCodeSegments* segments,
1656 const char* samplerName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001657 GrGLShaderVar* kernel,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001658 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001659 const char* imageIncrementName,
1660 const char* fsOutColor,
1661 GrStringBuilder& sampleCoords,
1662 GrStringBuilder& texFunc,
1663 GrStringBuilder& modulate) {
1664 GrStringBuilder sumVar("sum");
1665 sumVar.appendS32(stageNum);
1666 GrStringBuilder coordVar("coord");
1667 coordVar.appendS32(stageNum);
1668
tomhudson@google.comda668982011-12-07 15:06:29 +00001669 GrStringBuilder kernelIndex;
1670 kernel->appendArrayAccess("i", &kernelIndex);
1671
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001672 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1673 sumVar.c_str());
1674 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1675 coordVar.c_str(),
1676 sampleCoords.c_str());
1677 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1678 desc.fKernelWidth);
tomhudson@google.comda668982011-12-07 15:06:29 +00001679 segments->fFSCode.appendf("\t\t%s += %s(%s, %s)%s * %s;\n",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001680 sumVar.c_str(), texFunc.c_str(),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001681 samplerName, coordVar.c_str(), swizzle,
tomhudson@google.comda668982011-12-07 15:06:29 +00001682 kernelIndex.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001683 segments->fFSCode.appendf("\t\t%s += %s;\n",
1684 coordVar.c_str(),
1685 imageIncrementName);
vandebo@chromium.orgb9682d32012-02-21 18:53:39 +00001686 segments->fFSCode.append("\t}\n");
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001687 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1688 sumVar.c_str(), modulate.c_str());
1689}
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001690
1691void genMorphologyVS(int stageNum,
1692 const StageDesc& desc,
1693 ShaderCodeSegments* segments,
1694 GrGLProgram::StageUniLocations* locations,
1695 const char** imageIncrementName,
1696 const char* varyingVSName) {
1697 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
tomhudson@google.com168e6342012-04-18 17:49:20 +00001698 imgInc->setType(kVec2f_GrSLType);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001699 imgInc->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
1700
1701 image_increment_param_name(stageNum, imgInc->accessName());
1702 *imageIncrementName = imgInc->getName().c_str();
1703
1704 // need image increment in both VS and FS
1705 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1706
1707 locations->fImageIncrementUni = kUseUniform;
1708 segments->fVSCode.appendf("\t%s -= vec2(%d, %d) * %s;\n",
1709 varyingVSName, desc.fKernelWidth,
1710 desc.fKernelWidth, *imageIncrementName);
1711}
1712
1713void genMorphologyFS(int stageNum,
1714 const StageDesc& desc,
1715 ShaderCodeSegments* segments,
1716 const char* samplerName,
1717 const char* swizzle,
1718 const char* imageIncrementName,
1719 const char* fsOutColor,
1720 GrStringBuilder& sampleCoords,
1721 GrStringBuilder& texFunc,
1722 GrStringBuilder& modulate) {
1723 GrStringBuilder valueVar("value");
1724 valueVar.appendS32(stageNum);
1725 GrStringBuilder coordVar("coord");
1726 coordVar.appendS32(stageNum);
1727 bool isDilate = StageDesc::kDilate_FetchMode == desc.fFetchMode;
1728
1729 if (isDilate) {
1730 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1731 valueVar.c_str());
1732 } else {
1733 segments->fFSCode.appendf("\tvec4 %s = vec4(1, 1, 1, 1);\n",
1734 valueVar.c_str());
1735 }
1736 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1737 coordVar.c_str(),
1738 sampleCoords.c_str());
1739 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1740 desc.fKernelWidth * 2 + 1);
1741 segments->fFSCode.appendf("\t\t%s = %s(%s, %s(%s, %s)%s);\n",
1742 valueVar.c_str(), isDilate ? "max" : "min",
1743 valueVar.c_str(), texFunc.c_str(),
1744 samplerName, coordVar.c_str(), swizzle);
1745 segments->fFSCode.appendf("\t\t%s += %s;\n",
1746 coordVar.c_str(),
1747 imageIncrementName);
1748 segments->fFSCode.appendf("\t}\n");
1749 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1750 valueVar.c_str(), modulate.c_str());
1751}
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001752
1753}
1754
bsalomon@google.com96399942012-02-13 14:39:16 +00001755void GrGLProgram::genStageCode(const GrGLContextInfo& gl,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001756 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001757 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001758 const char* fsInColor, // NULL means no incoming color
1759 const char* fsOutColor,
1760 const char* vsInCoord,
1761 ShaderCodeSegments* segments,
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001762 StageUniLocations* locations,
1763 GrGLProgramStage* customStage) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001764
bsalomon@google.com74b98712011-11-11 19:46:16 +00001765 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
1766 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) ==
1767 desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001768
junov@google.comf93e7172011-03-31 21:26:24 +00001769 // First decide how many coords are needed to access the texture
1770 // Right now it's always 2 but we could start using 1D textures for
1771 // gradients.
1772 static const int coordDims = 2;
1773 int varyingDims;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001774
junov@google.comf93e7172011-03-31 21:26:24 +00001775 /// Vertex Shader Stuff
1776
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001777 if (NULL != customStage) {
1778 customStage->setupVSUnis(segments->fVSUnis, stageNum);
1779 }
1780
junov@google.comf93e7172011-03-31 21:26:24 +00001781 // decide whether we need a matrix to transform texture coords
1782 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001783 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001784 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001785 varyingDims = coordDims;
1786 } else {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001787 GrGLShaderVar* mat;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001788 #if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001789 mat = &segments->fVSAttrs.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001790 mat->setTypeModifier(GrGLShaderVar::kAttribute_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001791 locations->fTextureMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +00001792 #else
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001793 mat = &segments->fVSUnis.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001794 mat->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001795 locations->fTextureMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001796 #endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001797 tex_matrix_name(stageNum, mat->accessName());
tomhudson@google.com168e6342012-04-18 17:49:20 +00001798 mat->setType(kMat33f_GrSLType);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001799 matName = mat->getName().c_str();
1800
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001801 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001802 varyingDims = coordDims;
1803 } else {
1804 varyingDims = coordDims + 1;
1805 }
1806 }
1807
tomhudson@google.com168e6342012-04-18 17:49:20 +00001808 segments->fFSUnis.push_back().set(kSampler2D_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001809 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001810 sampler_name(stageNum, segments->fFSUnis.back().accessName());
bsalomon@google.com91961302011-05-09 18:39:58 +00001811 locations->fSamplerUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001812 const char* samplerName = segments->fFSUnis.back().getName().c_str();
junov@google.comf93e7172011-03-31 21:26:24 +00001813
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001814 const char* texelSizeName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001815 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
tomhudson@google.com168e6342012-04-18 17:49:20 +00001816 segments->fFSUnis.push_back().set(kVec2f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001817 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001818 normalized_texel_size_name(stageNum, segments->fFSUnis.back().accessName());
1819 texelSizeName = segments->fFSUnis.back().getName().c_str();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001820 }
1821
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001822 const char *varyingVSName, *varyingFSName;
tomhudson@google.com168e6342012-04-18 17:49:20 +00001823 append_varying(GrSLFloatVectorType(varyingDims),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001824 "Stage",
1825 stageNum,
1826 segments,
1827 &varyingVSName,
1828 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001829
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001830 if (!matName) {
junov@google.comf93e7172011-03-31 21:26:24 +00001831 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001832 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001833 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001834 // varying = texMatrix * texCoord
1835 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001836 varyingVSName, matName, vsInCoord,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001837 vector_all_coords(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001838 }
1839
tomhudson@google.comda668982011-12-07 15:06:29 +00001840 GrGLShaderVar* radial2Params = NULL;
1841 const char* radial2VaryingVSName = NULL;
1842 const char* radial2VaryingFSName = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001843
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001844 if (isRadialMapping((StageDesc::CoordMapping) desc.fCoordMapping)) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001845 radial2Params = genRadialVS(stageNum, segments,
1846 locations,
1847 &radial2VaryingVSName,
1848 &radial2VaryingFSName,
1849 varyingVSName,
1850 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001851 }
1852
tomhudson@google.comda668982011-12-07 15:06:29 +00001853 GrGLShaderVar* kernel = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001854 const char* imageIncrementName = NULL;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001855 if (StageDesc::kConvolution_FetchMode == desc.fFetchMode) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001856 genConvolutionVS(stageNum, desc, segments, locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001857 &kernel, &imageIncrementName, varyingVSName);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001858 } else if (StageDesc::kDilate_FetchMode == desc.fFetchMode ||
1859 StageDesc::kErode_FetchMode == desc.fFetchMode) {
1860 genMorphologyVS(stageNum, desc, segments, locations,
1861 &imageIncrementName, varyingVSName);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001862 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001863
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001864 if (NULL != customStage) {
1865 GrStringBuilder vertexShader;
1866 customStage->emitVS(&vertexShader, varyingVSName);
1867 segments->fVSCode.appendf("{\n");
1868 segments->fVSCode.append(vertexShader);
1869 segments->fVSCode.appendf("}\n");
1870 }
1871
junov@google.comf93e7172011-03-31 21:26:24 +00001872 /// Fragment Shader Stuff
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00001873
1874 if (NULL != customStage) {
1875 customStage->setupFSUnis(segments->fFSUnis, stageNum);
1876 }
1877
bsalomon@google.com91961302011-05-09 18:39:58 +00001878 GrStringBuilder fsCoordName;
junov@google.comf93e7172011-03-31 21:26:24 +00001879 // function used to access the shader, may be made projective
bsalomon@google.com91961302011-05-09 18:39:58 +00001880 GrStringBuilder texFunc("texture2D");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001881 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1882 StageDesc::kNoPerspective_OptFlagBit)) {
junov@google.comf93e7172011-03-31 21:26:24 +00001883 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001884 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001885 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001886 // if we have to do some special op on the varyings to get
junov@google.comf93e7172011-03-31 21:26:24 +00001887 // our final tex coords then when in perspective we have to
bsalomon@google.com91961302011-05-09 18:39:58 +00001888 // do an explicit divide. Otherwise, we can use a Proj func.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001889 if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
1890 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001891 texFunc.append("Proj");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001892 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001893 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001894 fsCoordName = "inCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +00001895 fsCoordName.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001896 segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n",
tomhudson@google.com168e6342012-04-18 17:49:20 +00001897 GrGLShaderVar::TypeString(GrSLFloatVectorType(coordDims)),
1898 fsCoordName.c_str(),
1899 varyingFSName,
1900 GrGLSLVectorNonhomogCoords(varyingDims),
1901 varyingFSName,
1902 GrGLSLVectorHomogCoord(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001903 }
1904 }
1905
bsalomon@google.comfc296292011-05-06 13:53:47 +00001906 GrStringBuilder sampleCoords;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001907 bool complexCoord = false;
junov@google.comf93e7172011-03-31 21:26:24 +00001908 switch (desc.fCoordMapping) {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001909 case StageDesc::kIdentity_CoordMapping:
junov@google.comf93e7172011-03-31 21:26:24 +00001910 sampleCoords = fsCoordName;
1911 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001912 case StageDesc::kSweepGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001913 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 +00001914 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001915 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001916 case StageDesc::kRadialGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001917 sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001918 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001919 break;
tomhudson@google.com5960d002011-10-19 20:21:48 +00001920 case StageDesc::kRadial2Gradient_CoordMapping:
1921 complexCoord = genRadial2GradientCoordMapping(
1922 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001923 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001924 sampleCoords, fsCoordName,
1925 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001926
tomhudson@google.com5960d002011-10-19 20:21:48 +00001927 break;
1928 case StageDesc::kRadial2GradientDegenerate_CoordMapping:
1929 complexCoord = genRadial2GradientDegenerateCoordMapping(
1930 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001931 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001932 sampleCoords, fsCoordName,
1933 varyingDims, coordDims);
1934 break;
junov@google.comf93e7172011-03-31 21:26:24 +00001935
junov@google.comf93e7172011-03-31 21:26:24 +00001936 };
1937
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001938 static const uint32_t kMulByAlphaMask =
1939 (StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag |
1940 StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag);
1941
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001942 const char* swizzle = "";
bsalomon@google.com74b98712011-11-11 19:46:16 +00001943 if (desc.fInConfigFlags & StageDesc::kSwapRAndB_InConfigFlag) {
1944 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag));
robertphillips@google.com69950682012-04-06 18:06:10 +00001945 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearRed_InConfigFlag));
bsalomon@google.com74b98712011-11-11 19:46:16 +00001946 swizzle = ".bgra";
1947 } else if (desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001948 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
robertphillips@google.com69950682012-04-06 18:06:10 +00001949 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearRed_InConfigFlag));
bsalomon@google.com74b98712011-11-11 19:46:16 +00001950 swizzle = ".aaaa";
robertphillips@google.com69950682012-04-06 18:06:10 +00001951 } else if (desc.fInConfigFlags & StageDesc::kSmearRed_InConfigFlag) {
1952 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
1953 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag));
1954 swizzle = ".rrrr";
1955 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001956
bsalomon@google.com91961302011-05-09 18:39:58 +00001957 GrStringBuilder modulate;
1958 if (NULL != fsInColor) {
1959 modulate.printf(" * %s", fsInColor);
1960 }
1961
tomhudson@google.com0d831722011-06-02 15:37:14 +00001962 if (desc.fOptFlags &
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001963 StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001964 GrStringBuilder texDomainName;
1965 tex_domain_name(stageNum, &texDomainName);
tomhudson@google.com168e6342012-04-18 17:49:20 +00001966 segments->fFSUnis.push_back().set(kVec4f_GrSLType,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001967 GrGLShaderVar::kUniform_TypeModifier, texDomainName);
junov@google.com6acc9b32011-05-16 18:32:07 +00001968 GrStringBuilder coordVar("clampCoord");
1969 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001970 float_vector_type_str(coordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001971 coordVar.c_str(),
1972 sampleCoords.c_str(),
1973 texDomainName.c_str(),
1974 texDomainName.c_str());
1975 sampleCoords = coordVar;
1976 locations->fTexDomUni = kUseUniform;
1977 }
1978
tomhudson@google.com5960d002011-10-19 20:21:48 +00001979 switch (desc.fFetchMode) {
1980 case StageDesc::k2x2_FetchMode:
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001981 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001982 gen2x2FS(stageNum, segments, locations, &sampleCoords,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001983 samplerName, texelSizeName, swizzle, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001984 texFunc, modulate, complexCoord, coordDims);
1985 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001986 case StageDesc::kConvolution_FetchMode:
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001987 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001988 genConvolutionFS(stageNum, desc, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001989 samplerName, kernel, swizzle, imageIncrementName, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001990 sampleCoords, texFunc, modulate);
1991 break;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001992 case StageDesc::kDilate_FetchMode:
1993 case StageDesc::kErode_FetchMode:
1994 GrAssert(!(desc.fInConfigFlags & kMulByAlphaMask));
1995 genMorphologyFS(stageNum, desc, segments,
1996 samplerName, swizzle, imageIncrementName, fsOutColor,
1997 sampleCoords, texFunc, modulate);
1998 break;
tomhudson@google.com5960d002011-10-19 20:21:48 +00001999 default:
bsalomon@google.coma91e9232012-02-23 15:39:54 +00002000 if (desc.fInConfigFlags & kMulByAlphaMask) {
2001 // only one of the mul by alpha flags should be set
2002 GrAssert(GrIsPow2(kMulByAlphaMask & desc.fInConfigFlags));
bsalomon@google.com74b98712011-11-11 19:46:16 +00002003 GrAssert(!(desc.fInConfigFlags &
2004 StageDesc::kSmearAlpha_InConfigFlag));
robertphillips@google.com69950682012-04-06 18:06:10 +00002005 GrAssert(!(desc.fInConfigFlags &
2006 StageDesc::kSmearRed_InConfigFlag));
bsalomon@google.com74b98712011-11-11 19:46:16 +00002007 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
2008 fsOutColor, texFunc.c_str(),
2009 samplerName, sampleCoords.c_str(),
2010 swizzle);
bsalomon@google.coma91e9232012-02-23 15:39:54 +00002011 if (desc.fInConfigFlags &
2012 StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag) {
2013 segments->fFSCode.appendf("\t%s = vec4(ceil(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
2014 fsOutColor, fsOutColor, fsOutColor,
2015 fsOutColor, modulate.c_str());
2016 } else {
2017 segments->fFSCode.appendf("\t%s = vec4(floor(%s.rgb*%s.a*255.0)/255.0,%s.a)%s;\n",
2018 fsOutColor, fsOutColor, fsOutColor,
2019 fsOutColor, modulate.c_str());
2020 }
bsalomon@google.com74b98712011-11-11 19:46:16 +00002021 } else {
2022 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n",
2023 fsOutColor, texFunc.c_str(),
2024 samplerName, sampleCoords.c_str(),
2025 swizzle, modulate.c_str());
2026 }
junov@google.comf93e7172011-03-31 21:26:24 +00002027 }
tomhudson@google.com07eecdc2012-04-20 18:35:38 +00002028
2029 if (NULL != customStage) {
2030 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
2031 StageDesc::kNoPerspective_OptFlagBit)) {
2032 customStage->setSamplerMode(GrGLProgramStage::kDefault_SamplerMode);
2033 } else if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
2034 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
2035 customStage->setSamplerMode(GrGLProgramStage::kProj_SamplerMode);
2036 } else {
2037 customStage->setSamplerMode(
2038 GrGLProgramStage::kExplicitDivide_SamplerMode);
2039 }
2040
2041 GrStringBuilder fragmentShader;
2042 fsCoordName = customStage->emitTextureSetup(
2043 &fragmentShader, varyingFSName,
2044 stageNum, coordDims, varyingDims);
2045 customStage->emitFS(&fragmentShader, fsOutColor, fsInColor,
2046 samplerName, fsCoordName.c_str());
2047
2048 // Enclose custom code in a block to avoid namespace conflicts
2049 segments->fFSCode.appendf("{\n");
2050 segments->fFSCode.append(fragmentShader);
2051 segments->fFSCode.appendf("}\n");
2052 }
junov@google.comf93e7172011-03-31 21:26:24 +00002053}
tomhudson@google.com5960d002011-10-19 20:21:48 +00002054
tomhudson@google.com5960d002011-10-19 20:21:48 +00002055