blob: 610446d615977ae55416faf2e31b220ca0a3e199 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
junov@google.comf93e7172011-03-31 21:26:24 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
junov@google.comf93e7172011-03-31 21:26:24 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
junov@google.comf93e7172011-03-31 21:26:24 +000010#include "GrGLProgram.h"
11
bsalomon@google.com4fa66942011-09-20 19:06:12 +000012#include "GrAllocator.h"
13#include "GrGLShaderVar.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000014#include "SkTrace.h"
Scroggo97c88c22011-05-11 14:05:25 +000015#include "SkXfermode.h"
16
junov@google.comf93e7172011-03-31 21:26:24 +000017namespace {
18
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +000019enum {
20 /// Used to mark a StageUniLocation field that should be bound
21 /// to a uniform during getUniformLocationsAndInitCache().
22 kUseUniform = 2000
23};
24
25
bsalomon@google.com0b77d682011-08-19 13:28:54 +000026const char* GrPrecision(const GrGLInterface* gl) {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +000027 if (gl->supportsES2()) {
junov@google.comf93e7172011-03-31 21:26:24 +000028 return "mediump";
29 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +000030 return " ";
junov@google.comf93e7172011-03-31 21:26:24 +000031 }
32}
33
bsalomon@google.com0b77d682011-08-19 13:28:54 +000034const char* GrShaderPrecision(const GrGLInterface* gl) {
bsalomon@google.com1dcf5062011-11-14 19:29:53 +000035 if (gl->supportsES2()) {
junov@google.comf93e7172011-03-31 21:26:24 +000036 return "precision mediump float;\n";
37 } else {
38 return "";
39 }
40}
41
42} // namespace
43
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000044#define PRINT_SHADERS 0
45
bsalomon@google.com4fa66942011-09-20 19:06:12 +000046typedef GrTAllocator<GrGLShaderVar> VarArray;
47
48// number of each input/output type in a single allocation block
49static const int gVarsPerBlock = 8;
50// except FS outputs where we expect 2 at most.
51static const int gMaxFSOutputs = 2;
52
53struct ShaderCodeSegments {
54 ShaderCodeSegments()
55 : fVSUnis(gVarsPerBlock)
56 , fVSAttrs(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000057 , fVSOutputs(gVarsPerBlock)
58 , fGSInputs(gVarsPerBlock)
59 , fGSOutputs(gVarsPerBlock)
60 , fFSInputs(gVarsPerBlock)
bsalomon@google.com4fa66942011-09-20 19:06:12 +000061 , fFSUnis(gVarsPerBlock)
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000062 , fFSOutputs(gMaxFSOutputs)
63 , fUsesGS(false) {}
bsalomon@google.com4fa66942011-09-20 19:06:12 +000064 GrStringBuilder fHeader; // VS+FS, GLSL version, etc
65 VarArray fVSUnis;
66 VarArray fVSAttrs;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000067 VarArray fVSOutputs;
68 VarArray fGSInputs;
69 VarArray fGSOutputs;
70 VarArray fFSInputs;
71 GrStringBuilder fGSHeader; // layout qualifiers specific to GS
bsalomon@google.com4fa66942011-09-20 19:06:12 +000072 VarArray fFSUnis;
73 VarArray fFSOutputs;
74 GrStringBuilder fFSFunctions;
75 GrStringBuilder fVSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000076 GrStringBuilder fGSCode;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000077 GrStringBuilder fFSCode;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +000078
79 bool fUsesGS;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000080};
81
bsalomon@google.com85b505b2011-11-07 14:56:51 +000082typedef GrGLProgram::ProgramDesc::StageDesc StageDesc;
bsalomon@google.com4fa66942011-09-20 19:06:12 +000083
bsalomon@google.com4be283f2011-04-19 21:15:09 +000084#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +000085 #define VIEW_MATRIX_NAME "aViewM"
86#else
87 #define VIEW_MATRIX_NAME "uViewM"
88#endif
89
90#define POS_ATTR_NAME "aPosition"
91#define COL_ATTR_NAME "aColor"
bsalomon@google.coma3108262011-10-10 14:08:47 +000092#define COV_ATTR_NAME "aCoverage"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000093#define EDGE_ATTR_NAME "aEdge"
bsalomon@google.com4be283f2011-04-19 21:15:09 +000094#define COL_UNI_NAME "uColor"
bsalomon@google.com2401ae82012-01-17 21:03:05 +000095#define COV_UNI_NAME "uCoverage"
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +000096#define EDGES_UNI_NAME "uEdges"
Scroggo97c88c22011-05-11 14:05:25 +000097#define COL_FILTER_UNI_NAME "uColorFilter"
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +000098#define COL_MATRIX_UNI_NAME "uColorMatrix"
99#define COL_MATRIX_VEC_UNI_NAME "uColorMatrixVec"
junov@google.comf93e7172011-03-31 21:26:24 +0000100
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000101namespace {
102inline void tex_attr_name(int coordIdx, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000103 *s = "aTexCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000104 s->appendS32(coordIdx);
junov@google.comf93e7172011-03-31 21:26:24 +0000105}
106
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000107inline GrGLShaderVar::Type float_vector_type(int count) {
108 GR_STATIC_ASSERT(GrGLShaderVar::kFloat_Type == 0);
109 GR_STATIC_ASSERT(GrGLShaderVar::kVec2f_Type == 1);
110 GR_STATIC_ASSERT(GrGLShaderVar::kVec3f_Type == 2);
111 GR_STATIC_ASSERT(GrGLShaderVar::kVec4f_Type == 3);
112 GrAssert(count > 0 && count <= 4);
113 return (GrGLShaderVar::Type)(count - 1);
junov@google.comf93e7172011-03-31 21:26:24 +0000114}
115
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000116inline const char* float_vector_type_str(int count) {
117 return GrGLShaderVar::TypeString(float_vector_type(count));
118}
119
120inline const char* vector_homog_coord(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000121 static const char* HOMOGS[] = {"ERROR", "", ".y", ".z", ".w"};
122 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(HOMOGS));
123 return HOMOGS[count];
124}
125
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000126inline const char* vector_nonhomog_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000127 static const char* NONHOMOGS[] = {"ERROR", "", ".x", ".xy", ".xyz"};
128 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(NONHOMOGS));
129 return NONHOMOGS[count];
130}
131
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000132inline const char* vector_all_coords(int count) {
junov@google.comf93e7172011-03-31 21:26:24 +0000133 static const char* ALL[] = {"ERROR", "", ".xy", ".xyz", ".xyzw"};
134 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ALL));
135 return ALL[count];
136}
137
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000138inline const char* all_ones_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000139 static const char* ONESVEC[] = {"ERROR", "1.0", "vec2(1,1)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000140 "vec3(1,1,1)", "vec4(1,1,1,1)"};
141 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ONESVEC));
142 return ONESVEC[count];
143}
144
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000145inline const char* all_zeros_vec(int count) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000146 static const char* ZEROSVEC[] = {"ERROR", "0.0", "vec2(0,0)",
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000147 "vec3(0,0,0)", "vec4(0,0,0,0)"};
148 GrAssert(count >= 1 && count < (int)GR_ARRAY_COUNT(ZEROSVEC));
149 return ZEROSVEC[count];
150}
151
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000152inline const char* declared_color_output_name() { return "fsColorOut"; }
153inline const char* dual_source_output_name() { return "dualSourceOut"; }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000154
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000155inline void tex_matrix_name(int stage, GrStringBuilder* s) {
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000156#if GR_GL_ATTRIBUTE_MATRICES
junov@google.comf93e7172011-03-31 21:26:24 +0000157 *s = "aTexM";
158#else
159 *s = "uTexM";
160#endif
bsalomon@google.comfc296292011-05-06 13:53:47 +0000161 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000162}
163
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000164inline void normalized_texel_size_name(int stage, GrStringBuilder* s) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000165 *s = "uTexelSize";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000166 s->appendS32(stage);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000167}
168
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000169inline void sampler_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000170 *s = "uSampler";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000171 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000172}
173
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000174inline void radial2_param_name(int stage, GrStringBuilder* s) {
junov@google.comf93e7172011-03-31 21:26:24 +0000175 *s = "uRadial2Params";
bsalomon@google.comfc296292011-05-06 13:53:47 +0000176 s->appendS32(stage);
junov@google.comf93e7172011-03-31 21:26:24 +0000177}
178
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000179inline void convolve_param_names(int stage, GrStringBuilder* k, GrStringBuilder* i) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000180 *k = "uKernel";
181 k->appendS32(stage);
182 *i = "uImageIncrement";
183 i->appendS32(stage);
184}
185
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000186inline void tex_domain_name(int stage, GrStringBuilder* s) {
junov@google.com6acc9b32011-05-16 18:32:07 +0000187 *s = "uTexDom";
188 s->appendS32(stage);
189}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000190}
junov@google.com6acc9b32011-05-16 18:32:07 +0000191
junov@google.comf93e7172011-03-31 21:26:24 +0000192GrGLProgram::GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000193}
194
195GrGLProgram::~GrGLProgram() {
junov@google.comf93e7172011-03-31 21:26:24 +0000196}
197
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000198void GrGLProgram::overrideBlend(GrBlendCoeff* srcCoeff,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000199 GrBlendCoeff* dstCoeff) const {
200 switch (fProgramDesc.fDualSrcOutput) {
201 case ProgramDesc::kNone_DualSrcOutput:
202 break;
203 // the prog will write a coverage value to the secondary
204 // output and the dst is blended by one minus that value.
205 case ProgramDesc::kCoverage_DualSrcOutput:
206 case ProgramDesc::kCoverageISA_DualSrcOutput:
207 case ProgramDesc::kCoverageISC_DualSrcOutput:
208 *dstCoeff = (GrBlendCoeff)GrGpu::kIS2C_BlendCoeff;
209 break;
210 default:
211 GrCrash("Unexpected dual source blend output");
212 break;
213 }
214}
215
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000216// assigns modulation of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000217// vars can be vec4s or floats (or one of each)
218// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000219// if either var is "" then assign to the other var
220// if both are "" then assign all ones
221static inline void modulate_helper(const char* outputVar,
222 const char* var0,
223 const char* var1,
224 GrStringBuilder* code) {
225 GrAssert(NULL != outputVar);
226 GrAssert(NULL != var0);
227 GrAssert(NULL != var1);
228 GrAssert(NULL != code);
229
230 bool has0 = '\0' != *var0;
231 bool has1 = '\0' != *var1;
232
233 if (!has0 && !has1) {
234 code->appendf("\t%s = %s;\n", outputVar, all_ones_vec(4));
235 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000236 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000237 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000238 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000239 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000240 code->appendf("\t%s = vec4(%s * %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000241 }
242}
243
244// assigns addition of two vars to an output var
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000245// vars can be vec4s or floats (or one of each)
246// result is always vec4
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000247// if either var is "" then assign to the other var
248// if both are "" then assign all zeros
249static inline void add_helper(const char* outputVar,
250 const char* var0,
251 const char* var1,
252 GrStringBuilder* code) {
253 GrAssert(NULL != outputVar);
254 GrAssert(NULL != var0);
255 GrAssert(NULL != var1);
256 GrAssert(NULL != code);
257
258 bool has0 = '\0' != *var0;
259 bool has1 = '\0' != *var1;
260
261 if (!has0 && !has1) {
262 code->appendf("\t%s = %s;\n", outputVar, all_zeros_vec(4));
263 } else if (!has0) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000264 code->appendf("\t%s = vec4(%s);\n", outputVar, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000265 } else if (!has1) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000266 code->appendf("\t%s = vec4(%s);\n", outputVar, var0);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000267 } else {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000268 code->appendf("\t%s = vec4(%s + %s);\n", outputVar, var0, var1);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000269 }
270}
271
272// given two blend coeffecients determine whether the src
273// and/or dst computation can be omitted.
274static inline void needBlendInputs(SkXfermode::Coeff srcCoeff,
275 SkXfermode::Coeff dstCoeff,
276 bool* needSrcValue,
277 bool* needDstValue) {
278 if (SkXfermode::kZero_Coeff == srcCoeff) {
279 switch (dstCoeff) {
280 // these all read the src
281 case SkXfermode::kSC_Coeff:
282 case SkXfermode::kISC_Coeff:
283 case SkXfermode::kSA_Coeff:
284 case SkXfermode::kISA_Coeff:
285 *needSrcValue = true;
286 break;
287 default:
288 *needSrcValue = false;
289 break;
290 }
291 } else {
292 *needSrcValue = true;
293 }
294 if (SkXfermode::kZero_Coeff == dstCoeff) {
295 switch (srcCoeff) {
296 // these all read the dst
297 case SkXfermode::kDC_Coeff:
298 case SkXfermode::kIDC_Coeff:
299 case SkXfermode::kDA_Coeff:
300 case SkXfermode::kIDA_Coeff:
301 *needDstValue = true;
302 break;
303 default:
304 *needDstValue = false;
305 break;
306 }
307 } else {
308 *needDstValue = true;
Scroggo97c88c22011-05-11 14:05:25 +0000309 }
310}
311
312/**
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000313 * Create a blend_coeff * value string to be used in shader code. Sets empty
314 * string if result is trivially zero.
315 */
316static void blendTermString(GrStringBuilder* str, SkXfermode::Coeff coeff,
317 const char* src, const char* dst,
318 const char* value) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000319 switch (coeff) {
320 case SkXfermode::kZero_Coeff: /** 0 */
321 *str = "";
322 break;
323 case SkXfermode::kOne_Coeff: /** 1 */
324 *str = value;
325 break;
326 case SkXfermode::kSC_Coeff:
327 str->printf("(%s * %s)", src, value);
328 break;
329 case SkXfermode::kISC_Coeff:
330 str->printf("((%s - %s) * %s)", all_ones_vec(4), src, value);
331 break;
332 case SkXfermode::kDC_Coeff:
333 str->printf("(%s * %s)", dst, value);
334 break;
335 case SkXfermode::kIDC_Coeff:
336 str->printf("((%s - %s) * %s)", all_ones_vec(4), dst, value);
337 break;
338 case SkXfermode::kSA_Coeff: /** src alpha */
339 str->printf("(%s.a * %s)", src, value);
340 break;
341 case SkXfermode::kISA_Coeff: /** inverse src alpha (i.e. 1 - sa) */
342 str->printf("((1.0 - %s.a) * %s)", src, value);
343 break;
344 case SkXfermode::kDA_Coeff: /** dst alpha */
345 str->printf("(%s.a * %s)", dst, value);
346 break;
347 case SkXfermode::kIDA_Coeff: /** inverse dst alpha (i.e. 1 - da) */
348 str->printf("((1.0 - %s.a) * %s)", dst, value);
349 break;
350 default:
351 GrCrash("Unexpected xfer coeff.");
352 break;
353 }
354}
355/**
Scroggo97c88c22011-05-11 14:05:25 +0000356 * Adds a line to the fragment shader code which modifies the color by
357 * the specified color filter.
358 */
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000359static void addColorFilter(GrStringBuilder* fsCode, const char * outputVar,
tomhudson@google.com0d3f1fb2011-06-01 19:27:31 +0000360 SkXfermode::Coeff uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000361 SkXfermode::Coeff colorCoeff,
362 const char* inColor) {
363 GrStringBuilder colorStr, constStr;
364 blendTermString(&colorStr, colorCoeff, COL_FILTER_UNI_NAME,
365 inColor, inColor);
366 blendTermString(&constStr, uniformCoeff, COL_FILTER_UNI_NAME,
367 inColor, COL_FILTER_UNI_NAME);
368
369 add_helper(outputVar, colorStr.c_str(), constStr.c_str(), fsCode);
Scroggo97c88c22011-05-11 14:05:25 +0000370}
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000371/**
372 * Adds code to the fragment shader code which modifies the color by
373 * the specified color matrix.
374 */
375static void addColorMatrix(GrStringBuilder* fsCode, const char * outputVar,
376 const char* inColor) {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000377 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);
378 fsCode->appendf("\t%s.rgb *= %s.a;\n", outputVar, outputVar);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000379}
Scroggo97c88c22011-05-11 14:05:25 +0000380
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000381namespace {
382
383const char* glsl_version_string(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000384 GrGLSLGeneration v) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000385 switch (v) {
tomhudson@google.com086e5352011-12-08 14:44:10 +0000386 case k110_GLSLGeneration:
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000387 if (gl->supportsES2()) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000388 // ES2s shader language is based on version 1.20 but is version
389 // 1.00 of the ES language.
390 return "#version 100\n";
391 } else {
bsalomon@google.com373a6632011-10-19 20:43:20 +0000392 return "#version 110\n";
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000393 }
tomhudson@google.com086e5352011-12-08 14:44:10 +0000394 case k130_GLSLGeneration:
bsalomon@google.com4c5de582011-11-14 19:35:08 +0000395 GrAssert(!gl->supportsES2());
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000396 return "#version 130\n";
tomhudson@google.com086e5352011-12-08 14:44:10 +0000397 case k150_GLSLGeneration:
bsalomon@google.com4c5de582011-11-14 19:35:08 +0000398 GrAssert(!gl->supportsES2());
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000399 return "#version 150\n";
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000400 default:
401 GrCrash("Unknown GL version.");
402 return ""; // suppress warning
403 }
404}
405
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000406// Adds a var that is computed in the VS and read in FS.
407// If there is a GS it will just pass it through.
408void append_varying(GrGLShaderVar::Type type,
409 const char* name,
410 ShaderCodeSegments* segments,
411 const char** vsOutName = NULL,
412 const char** fsInName = NULL) {
413 segments->fVSOutputs.push_back();
414 segments->fVSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000415 segments->fVSOutputs.back().setTypeModifier(
416 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000417 segments->fVSOutputs.back().accessName()->printf("v%s", name);
418 if (vsOutName) {
419 *vsOutName = segments->fVSOutputs.back().getName().c_str();
420 }
421 // input to FS comes either from VS or GS
422 const GrStringBuilder* fsName;
423 if (segments->fUsesGS) {
424 // if we have a GS take each varying in as an array
425 // and output as non-array.
426 segments->fGSInputs.push_back();
427 segments->fGSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000428 segments->fGSInputs.back().setTypeModifier(
429 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000430 segments->fGSInputs.back().setUnsizedArray();
431 *segments->fGSInputs.back().accessName() =
432 segments->fVSOutputs.back().getName();
433 segments->fGSOutputs.push_back();
434 segments->fGSOutputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000435 segments->fGSOutputs.back().setTypeModifier(
436 GrGLShaderVar::kOut_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000437 segments->fGSOutputs.back().accessName()->printf("g%s", name);
438 fsName = segments->fGSOutputs.back().accessName();
439 } else {
440 fsName = segments->fVSOutputs.back().accessName();
441 }
442 segments->fFSInputs.push_back();
443 segments->fFSInputs.back().setType(type);
tomhudson@google.com086e5352011-12-08 14:44:10 +0000444 segments->fFSInputs.back().setTypeModifier(
445 GrGLShaderVar::kIn_TypeModifier);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000446 segments->fFSInputs.back().setName(*fsName);
447 if (fsInName) {
448 *fsInName = fsName->c_str();
449 }
450}
451
452// version of above that adds a stage number to the
453// the var name (for uniqueness)
454void append_varying(GrGLShaderVar::Type type,
455 const char* name,
456 int stageNum,
457 ShaderCodeSegments* segments,
458 const char** vsOutName = NULL,
459 const char** fsInName = NULL) {
460 GrStringBuilder nameWithStage(name);
461 nameWithStage.appendS32(stageNum);
462 append_varying(type, nameWithStage.c_str(), segments, vsOutName, fsInName);
463}
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000464}
465
bsalomon@google.com66105672011-09-15 15:12:00 +0000466void GrGLProgram::genEdgeCoverage(const GrGLInterface* gl,
467 GrVertexLayout layout,
468 CachedData* programData,
469 GrStringBuilder* coverageVar,
470 ShaderCodeSegments* segments) const {
471 if (fProgramDesc.fEdgeAANumEdges > 0) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000472 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec3f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000473 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000474 EDGES_UNI_NAME,
475 fProgramDesc.fEdgeAANumEdges);
bsalomon@google.com66105672011-09-15 15:12:00 +0000476 programData->fUniLocations.fEdgesUni = kUseUniform;
477 int count = fProgramDesc.fEdgeAANumEdges;
478 segments->fFSCode.append(
479 "\tvec3 pos = vec3(gl_FragCoord.xy, 1);\n");
480 for (int i = 0; i < count; i++) {
481 segments->fFSCode.append("\tfloat a");
482 segments->fFSCode.appendS32(i);
483 segments->fFSCode.append(" = clamp(dot(" EDGES_UNI_NAME "[");
484 segments->fFSCode.appendS32(i);
485 segments->fFSCode.append("], pos), 0.0, 1.0);\n");
486 }
487 if (fProgramDesc.fEdgeAAConcave && (count & 0x01) == 0) {
488 // For concave polys, we consider the edges in pairs.
489 segments->fFSFunctions.append("float cross2(vec2 a, vec2 b) {\n");
490 segments->fFSFunctions.append("\treturn dot(a, vec2(b.y, -b.x));\n");
491 segments->fFSFunctions.append("}\n");
492 for (int i = 0; i < count; i += 2) {
493 segments->fFSCode.appendf("\tfloat eb%d;\n", i / 2);
494 segments->fFSCode.appendf("\tif (cross2(" EDGES_UNI_NAME "[%d].xy, " EDGES_UNI_NAME "[%d].xy) < 0.0) {\n", i, i + 1);
495 segments->fFSCode.appendf("\t\teb%d = a%d * a%d;\n", i / 2, i, i + 1);
496 segments->fFSCode.append("\t} else {\n");
497 segments->fFSCode.appendf("\t\teb%d = a%d + a%d - a%d * a%d;\n", i / 2, i, i + 1, i, i + 1);
498 segments->fFSCode.append("\t}\n");
499 }
500 segments->fFSCode.append("\tfloat edgeAlpha = ");
501 for (int i = 0; i < count / 2 - 1; i++) {
502 segments->fFSCode.appendf("min(eb%d, ", i);
503 }
504 segments->fFSCode.appendf("eb%d", count / 2 - 1);
505 for (int i = 0; i < count / 2 - 1; i++) {
506 segments->fFSCode.append(")");
507 }
508 segments->fFSCode.append(";\n");
509 } else {
510 segments->fFSCode.append("\tfloat edgeAlpha = ");
511 for (int i = 0; i < count - 1; i++) {
512 segments->fFSCode.appendf("min(a%d * a%d, ", i, i + 1);
513 }
514 segments->fFSCode.appendf("a%d * a0", count - 1);
515 for (int i = 0; i < count - 1; i++) {
516 segments->fFSCode.append(")");
517 }
518 segments->fFSCode.append(";\n");
519 }
520 *coverageVar = "edgeAlpha";
521 } else if (layout & GrDrawTarget::kEdge_VertexLayoutBit) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000522 const char *vsName, *fsName;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000523 append_varying(GrGLShaderVar::kVec4f_Type, "Edge", segments,
524 &vsName, &fsName);
525 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
526 GrGLShaderVar::kAttribute_TypeModifier, EDGE_ATTR_NAME);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000527 segments->fVSCode.appendf("\t%s = " EDGE_ATTR_NAME ";\n", vsName);
tomhudson@google.com93813632011-10-27 20:21:16 +0000528 if (GrDrawState::kHairLine_EdgeType == fProgramDesc.fVertexEdgeType) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000529 segments->fFSCode.appendf("\tfloat edgeAlpha = abs(dot(vec3(gl_FragCoord.xy,1), %s.xyz));\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000530 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000531 } else if (GrDrawState::kQuad_EdgeType == fProgramDesc.fVertexEdgeType) {
532 segments->fFSCode.appendf("\tfloat edgeAlpha;\n");
533 // keep the derivative instructions outside the conditional
534 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
535 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
536 segments->fFSCode.appendf("\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
537 // today we know z and w are in device space. We could use derivatives
538 segments->fFSCode.appendf("\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName, fsName);
539 segments->fFSCode.append ("\t} else {\n");
540 segments->fFSCode.appendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
541 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
542 fsName, fsName);
543 segments->fFSCode.appendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
544 segments->fFSCode.appendf("\t\tedgeAlpha = clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n"
545 "\t}\n");
546 if (gl->supportsES2()) {
547 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
548 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000549 } else {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000550 GrAssert(GrDrawState::kHairQuad_EdgeType == fProgramDesc.fVertexEdgeType);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000551 segments->fFSCode.appendf("\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
552 segments->fFSCode.appendf("\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000553 segments->fFSCode.appendf("\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
554 "\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
555 fsName, fsName);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000556 segments->fFSCode.appendf("\tfloat edgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, fsName);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000557 segments->fFSCode.append("\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
558 segments->fFSCode.append("\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
bsalomon@google.com1dcf5062011-11-14 19:29:53 +0000559 if (gl->supportsES2()) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000560 segments->fHeader.printf("#extension GL_OES_standard_derivatives: enable\n");
561 }
562 }
bsalomon@google.com66105672011-09-15 15:12:00 +0000563 *coverageVar = "edgeAlpha";
564 } else {
565 coverageVar->reset();
566 }
567}
568
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000569namespace {
570
571// returns true if the color output was explicitly declared or not.
tomhudson@google.com086e5352011-12-08 14:44:10 +0000572bool decl_and_get_fs_color_output(GrGLSLGeneration v,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000573 VarArray* fsOutputs,
574 const char** name) {
575 switch (v) {
tomhudson@google.com086e5352011-12-08 14:44:10 +0000576 case k110_GLSLGeneration:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000577 *name = "gl_FragColor";
578 return false;
579 break;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000580 case k130_GLSLGeneration: // fallthru
581 case k150_GLSLGeneration:
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000582 *name = declared_color_output_name();
583 fsOutputs->push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000584 GrGLShaderVar::kOut_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000585 declared_color_output_name());
586 return true;
587 break;
588 default:
589 GrCrash("Unknown GLSL version.");
590 return false; // suppress warning
591 }
592}
593
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000594void genInputColor(GrGLProgram::ProgramDesc::ColorInput colorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000595 GrGLProgram::CachedData* programData,
596 ShaderCodeSegments* segments,
597 GrStringBuilder* inColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000598 switch (colorInput) {
599 case GrGLProgram::ProgramDesc::kAttribute_ColorInput: {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000600 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000601 GrGLShaderVar::kAttribute_TypeModifier,
602 COL_ATTR_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000603 const char *vsName, *fsName;
604 append_varying(GrGLShaderVar::kVec4f_Type, "Color", segments, &vsName, &fsName);
605 segments->fVSCode.appendf("\t%s = " COL_ATTR_NAME ";\n", vsName);
606 *inColor = fsName;
607 } break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000608 case GrGLProgram::ProgramDesc::kUniform_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000609 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000610 GrGLShaderVar::kUniform_TypeModifier,
611 COL_UNI_NAME);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000612 programData->fUniLocations.fColorUni = kUseUniform;
613 *inColor = COL_UNI_NAME;
614 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000615 case GrGLProgram::ProgramDesc::kTransBlack_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000616 GrAssert(!"needComputedColor should be false.");
617 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000618 case GrGLProgram::ProgramDesc::kSolidWhite_ColorInput:
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000619 break;
620 default:
621 GrCrash("Unknown color type.");
622 break;
623 }
624}
625
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000626void genAttributeCoverage(ShaderCodeSegments* segments,
627 GrStringBuilder* inOutCoverage) {
628 segments->fVSAttrs.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000629 GrGLShaderVar::kAttribute_TypeModifier,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000630 COV_ATTR_NAME);
631 const char *vsName, *fsName;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000632 append_varying(GrGLShaderVar::kVec4f_Type, "Coverage",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000633 segments, &vsName, &fsName);
634 segments->fVSCode.appendf("\t%s = " COV_ATTR_NAME ";\n", vsName);
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000635 if (inOutCoverage->size()) {
636 segments->fFSCode.appendf("\tvec4 attrCoverage = %s * %s;\n",
637 fsName, inOutCoverage->c_str());
638 *inOutCoverage = "attrCoverage";
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000639 } else {
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000640 *inOutCoverage = fsName;
641 }
642}
643
644void genUniformCoverage(ShaderCodeSegments* segments,
645 GrGLProgram::CachedData* programData,
646 GrStringBuilder* inOutCoverage) {
647 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
648 GrGLShaderVar::kUniform_TypeModifier,
649 COV_UNI_NAME);
650 programData->fUniLocations.fCoverageUni = kUseUniform;
651 if (inOutCoverage->size()) {
652 segments->fFSCode.appendf("\tvec4 uniCoverage = %s * %s;\n",
653 COV_UNI_NAME, inOutCoverage->c_str());
654 *inOutCoverage = "uniCoverage";
655 } else {
656 *inOutCoverage = COV_UNI_NAME;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000657 }
658}
659
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000660}
661
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000662void GrGLProgram::genGeometryShader(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000663 GrGLSLGeneration glslGeneration,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000664 ShaderCodeSegments* segments) const {
665#if GR_GL_EXPERIMENTAL_GS
666 if (fProgramDesc.fExperimentalGS) {
tomhudson@google.com086e5352011-12-08 14:44:10 +0000667 GrAssert(glslGeneration >= k150_GLSLGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000668 segments->fGSHeader.append("layout(triangles) in;\n"
669 "layout(triangle_strip, max_vertices = 6) out;\n");
670 segments->fGSCode.append("void main() {\n"
671 "\tfor (int i = 0; i < 3; ++i) {\n"
672 "\t\tgl_Position = gl_in[i].gl_Position;\n");
673 if (this->fProgramDesc.fEmitsPointSize) {
674 segments->fGSCode.append("\t\tgl_PointSize = 1.0;\n");
675 }
676 GrAssert(segments->fGSInputs.count() == segments->fGSOutputs.count());
677 int count = segments->fGSInputs.count();
678 for (int i = 0; i < count; ++i) {
679 segments->fGSCode.appendf("\t\t%s = %s[i];\n",
680 segments->fGSOutputs[i].getName().c_str(),
681 segments->fGSInputs[i].getName().c_str());
682 }
683 segments->fGSCode.append("\t\tEmitVertex();\n"
684 "\t}\n"
685 "\tEndPrimitive();\n"
686 "}\n");
687 }
688#endif
689}
690
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000691const char* GrGLProgram::adjustInColor(const GrStringBuilder& inColor) const {
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000692 if (inColor.size()) {
693 return inColor.c_str();
694 } else {
695 if (ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput) {
696 return all_ones_vec(4);
697 } else {
698 return all_zeros_vec(4);
699 }
700 }
701}
702
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000703
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000704bool GrGLProgram::genProgram(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000705 GrGLSLGeneration glslGeneration,
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000706 GrGLProgram::CachedData* programData) const {
junov@google.comf93e7172011-03-31 21:26:24 +0000707
708 ShaderCodeSegments segments;
709 const uint32_t& layout = fProgramDesc.fVertexLayout;
710
bsalomon@google.com91961302011-05-09 18:39:58 +0000711 programData->fUniLocations.reset();
junov@google.comf93e7172011-03-31 21:26:24 +0000712
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000713#if GR_GL_EXPERIMENTAL_GS
714 segments.fUsesGS = fProgramDesc.fExperimentalGS;
715#endif
716
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000717 SkXfermode::Coeff colorCoeff, uniformCoeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000718 bool applyColorMatrix = SkToBool(fProgramDesc.fColorMatrixEnabled);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000719 // The rest of transfer mode color filters have not been implemented
720 if (fProgramDesc.fColorFilterXfermode < SkXfermode::kCoeffModesCnt) {
721 GR_DEBUGCODE(bool success =)
tomhudson@google.com0d831722011-06-02 15:37:14 +0000722 SkXfermode::ModeAsCoeff(static_cast<SkXfermode::Mode>
723 (fProgramDesc.fColorFilterXfermode),
724 &uniformCoeff, &colorCoeff);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000725 GR_DEBUGASSERT(success);
726 } else {
727 colorCoeff = SkXfermode::kOne_Coeff;
728 uniformCoeff = SkXfermode::kZero_Coeff;
729 }
730
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000731 // no need to do the color filter / matrix at all if coverage is 0. The
732 // output color is scaled by the coverage. All the dual source outputs are
733 // scaled by the coverage as well.
734 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fCoverageInput) {
735 colorCoeff = SkXfermode::kZero_Coeff;
736 uniformCoeff = SkXfermode::kZero_Coeff;
737 applyColorMatrix = false;
738 }
739
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000740 // If we know the final color is going to be all zeros then we can
741 // simplify the color filter coeffecients. needComputedColor will then
742 // come out false below.
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000743 if (ProgramDesc::kTransBlack_ColorInput == fProgramDesc.fColorInput) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000744 colorCoeff = SkXfermode::kZero_Coeff;
745 if (SkXfermode::kDC_Coeff == uniformCoeff ||
746 SkXfermode::kDA_Coeff == uniformCoeff) {
747 uniformCoeff = SkXfermode::kZero_Coeff;
748 } else if (SkXfermode::kIDC_Coeff == uniformCoeff ||
749 SkXfermode::kIDA_Coeff == uniformCoeff) {
750 uniformCoeff = SkXfermode::kOne_Coeff;
751 }
752 }
753
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000754 bool needColorFilterUniform;
755 bool needComputedColor;
756 needBlendInputs(uniformCoeff, colorCoeff,
757 &needColorFilterUniform, &needComputedColor);
758
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000759 // the dual source output has no canonical var name, have to
760 // declare an output, which is incompatible with gl_FragColor/gl_FragData.
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000761 const char* fsColorOutput = NULL;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000762 bool dualSourceOutputWritten = false;
tomhudson@google.com086e5352011-12-08 14:44:10 +0000763 segments.fHeader.printf(glsl_version_string(gl, glslGeneration));
764 bool isColorDeclared = decl_and_get_fs_color_output(glslGeneration,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000765 &segments.fFSOutputs,
766 &fsColorOutput);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000767
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000768#if GR_GL_ATTRIBUTE_MATRICES
tomhudson@google.com086e5352011-12-08 14:44:10 +0000769 segments.fVSAttrs.push_back().set(GrGLShaderVar::kMat33f_Type,
770 GrGLShaderVar::kAttribute_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000771 programData->fUniLocations.fViewMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +0000772#else
tomhudson@google.com086e5352011-12-08 14:44:10 +0000773 segments.fVSUnis.push_back().set(GrGLShaderVar::kMat33f_Type,
774 GrGLShaderVar::kUniform_TypeModifier, VIEW_MATRIX_NAME);
bsalomon@google.com91961302011-05-09 18:39:58 +0000775 programData->fUniLocations.fViewMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +0000776#endif
tomhudson@google.com086e5352011-12-08 14:44:10 +0000777 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type,
778 GrGLShaderVar::kAttribute_TypeModifier, POS_ATTR_NAME);
junov@google.comf93e7172011-03-31 21:26:24 +0000779
bsalomon@google.com91961302011-05-09 18:39:58 +0000780 segments.fVSCode.append(
781 "void main() {\n"
782 "\tvec3 pos3 = " VIEW_MATRIX_NAME " * vec3("POS_ATTR_NAME", 1);\n"
783 "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n");
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000784
bsalomon@google.com91961302011-05-09 18:39:58 +0000785 // incoming color to current stage being processed.
786 GrStringBuilder inColor;
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000787
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000788 if (needComputedColor) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000789 genInputColor((ProgramDesc::ColorInput) fProgramDesc.fColorInput,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000790 programData, &segments, &inColor);
junov@google.comf93e7172011-03-31 21:26:24 +0000791 }
792
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +0000793 // we output point size in the GS if present
794 if (fProgramDesc.fEmitsPointSize && !segments.fUsesGS){
bsalomon@google.com91961302011-05-09 18:39:58 +0000795 segments.fVSCode.append("\tgl_PointSize = 1.0;\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000796 }
bsalomon@google.com4be283f2011-04-19 21:15:09 +0000797
bsalomon@google.com91961302011-05-09 18:39:58 +0000798 segments.fFSCode.append("void main() {\n");
junov@google.comf93e7172011-03-31 21:26:24 +0000799
800 // add texture coordinates that are used to the list of vertex attr decls
tomhudson@google.com93813632011-10-27 20:21:16 +0000801 GrStringBuilder texCoordAttrs[GrDrawState::kMaxTexCoords];
802 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000803 if (GrDrawTarget::VertexUsesTexCoordIdx(t, layout)) {
junov@google.comf93e7172011-03-31 21:26:24 +0000804 tex_attr_name(t, texCoordAttrs + t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000805 segments.fVSAttrs.push_back().set(GrGLShaderVar::kVec2f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000806 GrGLShaderVar::kAttribute_TypeModifier,
807 texCoordAttrs[t].c_str());
junov@google.comf93e7172011-03-31 21:26:24 +0000808 }
809 }
810
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000811 ///////////////////////////////////////////////////////////////////////////
812 // compute the final color
Scroggo97c88c22011-05-11 14:05:25 +0000813
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000814 // if we have color stages string them together, feeding the output color
815 // of each to the next and generating code for each stage.
816 if (needComputedColor) {
817 GrStringBuilder outColor;
818 for (int s = 0; s < fProgramDesc.fFirstCoverageStage; ++s) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000819 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000820 // create var to hold stage result
821 outColor = "color";
822 outColor.appendS32(s);
823 segments.fFSCode.appendf("\tvec4 %s;\n", outColor.c_str());
824
825 const char* inCoords;
826 // figure out what our input coords are
827 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
828 layout) {
829 inCoords = POS_ATTR_NAME;
Scroggo97c88c22011-05-11 14:05:25 +0000830 } else {
831 int tcIdx = GrDrawTarget::VertexTexCoordsForStage(s, layout);
832 // we better have input tex coordinates if stage is enabled.
833 GrAssert(tcIdx >= 0);
834 GrAssert(texCoordAttrs[tcIdx].size());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000835 inCoords = texCoordAttrs[tcIdx].c_str();
junov@google.comf93e7172011-03-31 21:26:24 +0000836 }
837
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +0000838 this->genStageCode(gl,
839 s,
840 fProgramDesc.fStages[s],
841 inColor.size() ? inColor.c_str() : NULL,
842 outColor.c_str(),
843 inCoords,
844 &segments,
845 &programData->fUniLocations.fStages[s]);
junov@google.comf93e7172011-03-31 21:26:24 +0000846 inColor = outColor;
junov@google.comf93e7172011-03-31 21:26:24 +0000847 }
848 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000849 }
Scroggo97c88c22011-05-11 14:05:25 +0000850
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000851 // if have all ones or zeros for the "dst" input to the color filter then we
852 // may be able to make additional optimizations.
853 if (needColorFilterUniform && needComputedColor && !inColor.size()) {
bsalomon@google.com85b505b2011-11-07 14:56:51 +0000854 GrAssert(ProgramDesc::kSolidWhite_ColorInput == fProgramDesc.fColorInput);
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000855 bool uniformCoeffIsZero = SkXfermode::kIDC_Coeff == uniformCoeff ||
856 SkXfermode::kIDA_Coeff == uniformCoeff;
857 if (uniformCoeffIsZero) {
858 uniformCoeff = SkXfermode::kZero_Coeff;
859 bool bogus;
860 needBlendInputs(SkXfermode::kZero_Coeff, colorCoeff,
861 &needColorFilterUniform, &bogus);
862 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000863 }
864 if (needColorFilterUniform) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000865 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000866 GrGLShaderVar::kUniform_TypeModifier,
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000867 COL_FILTER_UNI_NAME);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000868 programData->fUniLocations.fColorFilterUni = kUseUniform;
869 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000870 bool wroteFragColorZero = false;
tomhudson@google.com0d831722011-06-02 15:37:14 +0000871 if (SkXfermode::kZero_Coeff == uniformCoeff &&
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000872 SkXfermode::kZero_Coeff == colorCoeff &&
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000873 !applyColorMatrix) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000874 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000875 fsColorOutput,
876 all_zeros_vec(4));
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000877 wroteFragColorZero = true;
878 } else if (SkXfermode::kDst_Mode != fProgramDesc.fColorFilterXfermode) {
879 segments.fFSCode.appendf("\tvec4 filteredColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000880 const char* color = adjustInColor(inColor);
tomhudson@google.com0d831722011-06-02 15:37:14 +0000881 addColorFilter(&segments.fFSCode, "filteredColor", uniformCoeff,
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000882 colorCoeff, color);
883 inColor = "filteredColor";
884 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000885 if (applyColorMatrix) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000886 segments.fFSUnis.push_back().set(GrGLShaderVar::kMat44f_Type,
887 GrGLShaderVar::kUniform_TypeModifier,
888 COL_MATRIX_UNI_NAME);
889 segments.fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
890 GrGLShaderVar::kUniform_TypeModifier,
891 COL_MATRIX_VEC_UNI_NAME);
892 programData->fUniLocations.fColorMatrixUni = kUseUniform;
893 programData->fUniLocations.fColorMatrixVecUni = kUseUniform;
894 segments.fFSCode.appendf("\tvec4 matrixedColor;\n");
senorblanco@chromium.orgb3a39b52012-01-05 18:28:56 +0000895 const char* color = adjustInColor(inColor);
896 addColorMatrix(&segments.fFSCode, "matrixedColor", color);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000897 inColor = "matrixedColor";
898 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000899
900 ///////////////////////////////////////////////////////////////////////////
901 // compute the partial coverage (coverage stages and edge aa)
902
903 GrStringBuilder inCoverage;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000904 bool coverageIsZero = ProgramDesc::kTransBlack_ColorInput ==
905 fProgramDesc.fCoverageInput;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000906 // we don't need to compute coverage at all if we know the final shader
907 // output will be zero and we don't have a dual src blend output.
908 if (!wroteFragColorZero ||
909 ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com66105672011-09-15 15:12:00 +0000910
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000911 if (!coverageIsZero) {
912 this->genEdgeCoverage(gl,
913 layout,
914 programData,
915 &inCoverage,
916 &segments);
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000917
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000918 switch (fProgramDesc.fCoverageInput) {
919 case ProgramDesc::kSolidWhite_ColorInput:
920 // empty string implies solid white
921 break;
922 case ProgramDesc::kAttribute_ColorInput:
923 genAttributeCoverage(&segments, &inCoverage);
924 break;
925 case ProgramDesc::kUniform_ColorInput:
926 genUniformCoverage(&segments, programData, &inCoverage);
927 break;
928 default:
929 GrCrash("Unexpected input coverage.");
930 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000931
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000932 GrStringBuilder outCoverage;
933 const int& startStage = fProgramDesc.fFirstCoverageStage;
934 for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
935 if (fProgramDesc.fStages[s].isEnabled()) {
936 // create var to hold stage output
937 outCoverage = "coverage";
938 outCoverage.appendS32(s);
939 segments.fFSCode.appendf("\tvec4 %s;\n",
940 outCoverage.c_str());
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000941
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000942 const char* inCoords;
943 // figure out what our input coords are
944 if (GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s) &
945 layout) {
946 inCoords = POS_ATTR_NAME;
947 } else {
948 int tcIdx =
949 GrDrawTarget::VertexTexCoordsForStage(s, layout);
950 // we better have input tex coordinates if stage is
951 // enabled.
952 GrAssert(tcIdx >= 0);
953 GrAssert(texCoordAttrs[tcIdx].size());
954 inCoords = texCoordAttrs[tcIdx].c_str();
955 }
956
957 genStageCode(gl, s,
958 fProgramDesc.fStages[s],
959 inCoverage.size() ? inCoverage.c_str() : NULL,
960 outCoverage.c_str(),
961 inCoords,
962 &segments,
963 &programData->fUniLocations.fStages[s]);
964 inCoverage = outCoverage;
bsalomon@google.comf2d91552011-05-16 20:56:06 +0000965 }
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +0000966 }
967 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000968 if (ProgramDesc::kNone_DualSrcOutput != fProgramDesc.fDualSrcOutput) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000969 segments.fFSOutputs.push_back().set(GrGLShaderVar::kVec4f_Type,
tomhudson@google.com086e5352011-12-08 14:44:10 +0000970 GrGLShaderVar::kOut_TypeModifier,
971 dual_source_output_name());
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000972 bool outputIsZero = coverageIsZero;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000973 GrStringBuilder coeff;
bsalomon@google.com2401ae82012-01-17 21:03:05 +0000974 if (!outputIsZero &&
975 ProgramDesc::kCoverage_DualSrcOutput !=
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000976 fProgramDesc.fDualSrcOutput && !wroteFragColorZero) {
977 if (!inColor.size()) {
978 outputIsZero = true;
979 } else {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000980 if (fProgramDesc.fDualSrcOutput ==
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000981 ProgramDesc::kCoverageISA_DualSrcOutput) {
982 coeff.printf("(1 - %s.a)", inColor.c_str());
983 } else {
984 coeff.printf("(vec4(1,1,1,1) - %s)", inColor.c_str());
985 }
986 }
987 }
988 if (outputIsZero) {
tomhudson@google.com0d831722011-06-02 15:37:14 +0000989 segments.fFSCode.appendf("\t%s = %s;\n",
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000990 dual_source_output_name(),
991 all_zeros_vec(4));
992 } else {
993 modulate_helper(dual_source_output_name(),
994 coeff.c_str(),
995 inCoverage.c_str(),
996 &segments.fFSCode);
997 }
998 dualSourceOutputWritten = true;
999 }
junov@google.comf93e7172011-03-31 21:26:24 +00001000 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001001
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001002 ///////////////////////////////////////////////////////////////////////////
1003 // combine color and coverage as frag color
1004
1005 if (!wroteFragColorZero) {
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001006 if (coverageIsZero) {
1007 segments.fFSCode.appendf("\t%s = %s;\n",
1008 fsColorOutput,
1009 all_zeros_vec(4));
1010 } else {
1011 modulate_helper(fsColorOutput,
1012 inColor.c_str(),
1013 inCoverage.c_str(),
1014 &segments.fFSCode);
1015 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001016 if (ProgramDesc::kNo_OutputPM == fProgramDesc.fOutputPM) {
bsalomon@google.come63799d2011-11-09 14:14:38 +00001017 segments.fFSCode.appendf("\t%s = %s.a <= 0.0 ? vec4(0,0,0,0) : vec4(%s.rgb / %s.a, %s.a);\n",
1018 fsColorOutput,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001019 fsColorOutput,
1020 fsColorOutput,
1021 fsColorOutput,
1022 fsColorOutput);
1023 }
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001024 }
1025
bsalomon@google.com91961302011-05-09 18:39:58 +00001026 segments.fVSCode.append("}\n");
1027 segments.fFSCode.append("}\n");
1028
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001029 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001030 // insert GS
1031#if GR_DEBUG
tomhudson@google.com086e5352011-12-08 14:44:10 +00001032 this->genGeometryShader(gl, glslGeneration, &segments);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001033#endif
1034
1035 ///////////////////////////////////////////////////////////////////////////
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001036 // compile and setup attribs and unis
1037
tomhudson@google.com086e5352011-12-08 14:44:10 +00001038 if (!CompileShaders(gl, glslGeneration, segments, programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001039 return false;
1040 }
1041
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001042 if (!this->bindOutputsAttribsAndLinkProgram(gl, texCoordAttrs,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001043 isColorDeclared,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001044 dualSourceOutputWritten,
1045 programData)) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001046 return false;
1047 }
1048
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001049 this->getUniformLocationsAndInitCache(gl, programData);
bsalomon@google.com91961302011-05-09 18:39:58 +00001050
1051 return true;
1052}
1053
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001054namespace {
1055
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001056inline void expand_decls(const VarArray& vars,
1057 const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001058 GrStringBuilder* string,
1059 GrGLSLGeneration gen) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001060 const int count = vars.count();
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001061 for (int i = 0; i < count; ++i) {
tomhudson@google.com086e5352011-12-08 14:44:10 +00001062 vars[i].appendDecl(gl, string, gen);
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001063 }
1064}
1065
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001066inline void print_shader(int stringCnt,
1067 const char** strings,
1068 int* stringLengths) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001069 for (int i = 0; i < stringCnt; ++i) {
1070 if (NULL == stringLengths || stringLengths[i] < 0) {
1071 GrPrintf(strings[i]);
1072 } else {
1073 GrPrintf("%.*s", stringLengths[i], strings[i]);
1074 }
1075 }
1076}
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001077
1078typedef SkTArray<const char*, true> StrArray;
1079#define PREALLOC_STR_ARRAY(N) SkSTArray<(N), const char*, true>
1080
1081typedef SkTArray<int, true> LengthArray;
1082#define PREALLOC_LENGTH_ARRAY(N) SkSTArray<(N), int, true>
1083
1084// these shouldn't relocate
1085typedef GrTAllocator<GrStringBuilder> TempArray;
1086#define PREALLOC_TEMP_ARRAY(N) GrSTAllocator<(N), GrStringBuilder>
1087
1088inline void append_string(const GrStringBuilder& str,
1089 StrArray* strings,
1090 LengthArray* lengths) {
1091 int length = (int) str.size();
1092 if (length) {
1093 strings->push_back(str.c_str());
1094 lengths->push_back(length);
1095 }
1096 GrAssert(strings->count() == lengths->count());
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001097}
1098
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001099inline void append_decls(const VarArray& vars,
1100 const GrGLInterface* gl,
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001101 StrArray* strings,
1102 LengthArray* lengths,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001103 TempArray* temp,
1104 GrGLSLGeneration gen) {
1105 expand_decls(vars, gl, &temp->push_back(), gen);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001106 append_string(temp->back(), strings, lengths);
1107}
1108
1109}
1110
1111bool GrGLProgram::CompileShaders(const GrGLInterface* gl,
tomhudson@google.com086e5352011-12-08 14:44:10 +00001112 GrGLSLGeneration glslGeneration,
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001113 const ShaderCodeSegments& segments,
bsalomon@google.com91961302011-05-09 18:39:58 +00001114 CachedData* programData) {
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001115 enum { kPreAllocStringCnt = 8 };
junov@google.comf93e7172011-03-31 21:26:24 +00001116
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001117 PREALLOC_STR_ARRAY(kPreAllocStringCnt) strs;
1118 PREALLOC_LENGTH_ARRAY(kPreAllocStringCnt) lengths;
1119 PREALLOC_TEMP_ARRAY(kPreAllocStringCnt) temps;
1120
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001121 GrStringBuilder unis;
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001122 GrStringBuilder inputs;
1123 GrStringBuilder outputs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001124
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001125 append_string(segments.fHeader, &strs, &lengths);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001126 append_decls(segments.fVSUnis, gl, &strs, &lengths, &temps, glslGeneration);
1127 append_decls(segments.fVSAttrs, gl, &strs, &lengths,
1128 &temps, glslGeneration);
1129 append_decls(segments.fVSOutputs, gl, &strs, &lengths,
1130 &temps, glslGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001131 append_string(segments.fVSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001132
1133#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001134 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001135 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001136#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001137
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001138 programData->fVShaderID =
1139 CompileShader(gl, GR_GL_VERTEX_SHADER, strs.count(),
1140 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001141
bsalomon@google.com91961302011-05-09 18:39:58 +00001142 if (!programData->fVShaderID) {
1143 return false;
1144 }
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001145 if (segments.fUsesGS) {
1146 strs.reset();
1147 lengths.reset();
1148 temps.reset();
1149 append_string(segments.fHeader, &strs, &lengths);
1150 append_string(segments.fGSHeader, &strs, &lengths);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001151 append_decls(segments.fGSInputs, gl, &strs, &lengths,
1152 &temps, glslGeneration);
1153 append_decls(segments.fGSOutputs, gl, &strs, &lengths,
1154 &temps, glslGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001155 append_string(segments.fGSCode, &strs, &lengths);
1156#if PRINT_SHADERS
1157 print_shader(strs.count(), &strs[0], &lengths[0]);
1158 GrPrintf("\n");
1159#endif
1160 programData->fGShaderID =
1161 CompileShader(gl, GR_GL_GEOMETRY_SHADER, strs.count(),
1162 &strs[0], &lengths[0]);
1163 } else {
1164 programData->fGShaderID = 0;
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +00001165 }
junov@google.comf93e7172011-03-31 21:26:24 +00001166
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001167 strs.reset();
1168 lengths.reset();
1169 temps.reset();
1170
1171 append_string(segments.fHeader, &strs, &lengths);
1172 GrStringBuilder precisionStr(GrShaderPrecision(gl));
1173 append_string(precisionStr, &strs, &lengths);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001174 append_decls(segments.fFSUnis, gl, &strs, &lengths, &temps, glslGeneration);
1175 append_decls(segments.fFSInputs, gl, &strs, &lengths,
1176 &temps, glslGeneration);
bsalomon@google.com373a6632011-10-19 20:43:20 +00001177 // We shouldn't have declared outputs on 1.10
tomhudson@google.com086e5352011-12-08 14:44:10 +00001178 GrAssert(k110_GLSLGeneration != glslGeneration ||
1179 segments.fFSOutputs.empty());
1180 append_decls(segments.fFSOutputs, gl, &strs, &lengths,
1181 &temps, glslGeneration);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001182 append_string(segments.fFSFunctions, &strs, &lengths);
1183 append_string(segments.fFSCode, &strs, &lengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001184
1185#if PRINT_SHADERS
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001186 print_shader(strs.count(), &strs[0], &lengths[0]);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001187 GrPrintf("\n");
junov@google.comf93e7172011-03-31 21:26:24 +00001188#endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001189
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001190 programData->fFShaderID =
1191 CompileShader(gl, GR_GL_FRAGMENT_SHADER, strs.count(),
1192 &strs[0], &lengths[0]);
junov@google.comf93e7172011-03-31 21:26:24 +00001193
bsalomon@google.com91961302011-05-09 18:39:58 +00001194 if (!programData->fFShaderID) {
1195 return false;
junov@google.comf93e7172011-03-31 21:26:24 +00001196 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001197
bsalomon@google.com91961302011-05-09 18:39:58 +00001198 return true;
junov@google.comf93e7172011-03-31 21:26:24 +00001199}
1200
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001201GrGLuint GrGLProgram::CompileShader(const GrGLInterface* gl,
1202 GrGLenum type,
1203 int stringCnt,
1204 const char** strings,
1205 int* stringLengths) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001206 SK_TRACE_EVENT1("GrGLProgram::CompileShader",
1207 "stringCount", SkStringPrintf("%i", stringCnt).c_str());
1208
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001209 GrGLuint shader;
1210 GR_GL_CALL_RET(gl, shader, CreateShader(type));
junov@google.comf93e7172011-03-31 21:26:24 +00001211 if (0 == shader) {
1212 return 0;
1213 }
1214
1215 GrGLint compiled = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001216 GR_GL_CALL(gl, ShaderSource(shader, stringCnt, strings, stringLengths));
1217 GR_GL_CALL(gl, CompileShader(shader));
1218 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_COMPILE_STATUS, &compiled));
junov@google.comf93e7172011-03-31 21:26:24 +00001219
1220 if (!compiled) {
1221 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001222 GR_GL_CALL(gl, GetShaderiv(shader, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001223 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
junov@google.comf93e7172011-03-31 21:26:24 +00001224 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001225 // retrieve length even though we don't need it to workaround
1226 // bug in chrome cmd buffer param validation.
1227 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001228 GR_GL_CALL(gl, GetShaderInfoLog(shader, infoLen+1,
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001229 &length, (char*)log.get()));
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001230 print_shader(stringCnt, strings, stringLengths);
junov@google.comf93e7172011-03-31 21:26:24 +00001231 GrPrintf("\n%s", log.get());
1232 }
1233 GrAssert(!"Shader compilation failed!");
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001234 GR_GL_CALL(gl, DeleteShader(shader));
junov@google.comf93e7172011-03-31 21:26:24 +00001235 return 0;
1236 }
1237 return shader;
1238}
1239
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001240bool GrGLProgram::bindOutputsAttribsAndLinkProgram(
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001241 const GrGLInterface* gl,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001242 GrStringBuilder texCoordAttrNames[],
1243 bool bindColorOut,
tomhudson@google.com0d831722011-06-02 15:37:14 +00001244 bool bindDualSrcOut,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001245 CachedData* programData) const {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001246 GR_GL_CALL_RET(gl, programData->fProgramID, CreateProgram());
bsalomon@google.com91961302011-05-09 18:39:58 +00001247 if (!programData->fProgramID) {
1248 return false;
1249 }
1250 const GrGLint& progID = programData->fProgramID;
1251
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001252 GR_GL_CALL(gl, AttachShader(progID, programData->fVShaderID));
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001253 if (programData->fGShaderID) {
1254 GR_GL_CALL(gl, AttachShader(progID, programData->fGShaderID));
1255 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001256 GR_GL_CALL(gl, AttachShader(progID, programData->fFShaderID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001257
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001258 if (bindColorOut) {
bsalomon@google.combc5cf512011-09-21 16:21:07 +00001259 GR_GL_CALL(gl, BindFragDataLocation(programData->fProgramID,
1260 0, declared_color_output_name()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001261 }
1262 if (bindDualSrcOut) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001263 GR_GL_CALL(gl, BindFragDataLocationIndexed(programData->fProgramID,
bsalomon@google.com271cffc2011-05-20 14:13:56 +00001264 0, 1, dual_source_output_name()));
1265 }
1266
bsalomon@google.com91961302011-05-09 18:39:58 +00001267 // Bind the attrib locations to same values for all shaders
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001268 GR_GL_CALL(gl, BindAttribLocation(progID, PositionAttributeIdx(),
1269 POS_ATTR_NAME));
tomhudson@google.com93813632011-10-27 20:21:16 +00001270 for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001271 if (texCoordAttrNames[t].size()) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001272 GR_GL_CALL(gl, BindAttribLocation(progID,
1273 TexCoordAttributeIdx(t),
1274 texCoordAttrNames[t].c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001275 }
1276 }
1277
bsalomon@google.com91961302011-05-09 18:39:58 +00001278 if (kSetAsAttribute == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001279 GR_GL_CALL(gl, BindAttribLocation(progID,
1280 ViewMatrixAttributeIdx(),
1281 VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001282 }
1283
tomhudson@google.com93813632011-10-27 20:21:16 +00001284 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com72b4fcb2011-05-09 20:47:34 +00001285 const StageUniLocations& unis = programData->fUniLocations.fStages[s];
1286 if (kSetAsAttribute == unis.fTextureMatrixUni) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001287 GrStringBuilder matName;
1288 tex_matrix_name(s, &matName);
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001289 GR_GL_CALL(gl, BindAttribLocation(progID,
1290 TextureMatrixAttributeIdx(s),
1291 matName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001292 }
1293 }
1294
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001295 GR_GL_CALL(gl, BindAttribLocation(progID, ColorAttributeIdx(),
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001296 COL_ATTR_NAME));
bsalomon@google.coma3108262011-10-10 14:08:47 +00001297 GR_GL_CALL(gl, BindAttribLocation(progID, CoverageAttributeIdx(),
1298 COV_ATTR_NAME));
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001299 GR_GL_CALL(gl, BindAttribLocation(progID, EdgeAttributeIdx(),
1300 EDGE_ATTR_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001301
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001302 GR_GL_CALL(gl, LinkProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001303
1304 GrGLint linked = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001305 GR_GL_CALL(gl, GetProgramiv(progID, GR_GL_LINK_STATUS, &linked));
bsalomon@google.com91961302011-05-09 18:39:58 +00001306 if (!linked) {
1307 GrGLint infoLen = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001308 GR_GL_CALL(gl, GetProgramiv(progID, GR_GL_INFO_LOG_LENGTH, &infoLen));
bsalomon@google.com3582bf92011-06-30 21:32:31 +00001309 SkAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger
bsalomon@google.com91961302011-05-09 18:39:58 +00001310 if (infoLen > 0) {
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001311 // retrieve length even though we don't need it to workaround
1312 // bug in chrome cmd buffer param validation.
1313 GrGLsizei length = GR_GL_INIT_ZERO;
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001314 GR_GL_CALL(gl, GetProgramInfoLog(progID, infoLen+1,
bsalomon@google.com79afcaa2011-09-14 14:29:18 +00001315 &length, (char*)log.get()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001316 GrPrintf((char*)log.get());
1317 }
1318 GrAssert(!"Error linking program");
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001319 GR_GL_CALL(gl, DeleteProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001320 programData->fProgramID = 0;
1321 return false;
1322 }
1323 return true;
1324}
1325
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001326void GrGLProgram::getUniformLocationsAndInitCache(const GrGLInterface* gl,
1327 CachedData* programData) const {
bsalomon@google.com91961302011-05-09 18:39:58 +00001328 const GrGLint& progID = programData->fProgramID;
1329
1330 if (kUseUniform == programData->fUniLocations.fViewMatrixUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001331 GR_GL_CALL_RET(gl, programData->fUniLocations.fViewMatrixUni,
1332 GetUniformLocation(progID, VIEW_MATRIX_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001333 GrAssert(kUnusedUniform != programData->fUniLocations.fViewMatrixUni);
1334 }
1335 if (kUseUniform == programData->fUniLocations.fColorUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001336 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorUni,
1337 GetUniformLocation(progID, COL_UNI_NAME));
bsalomon@google.com91961302011-05-09 18:39:58 +00001338 GrAssert(kUnusedUniform != programData->fUniLocations.fColorUni);
1339 }
Scroggo97c88c22011-05-11 14:05:25 +00001340 if (kUseUniform == programData->fUniLocations.fColorFilterUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001341 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorFilterUni,
1342 GetUniformLocation(progID, COL_FILTER_UNI_NAME));
Scroggo97c88c22011-05-11 14:05:25 +00001343 GrAssert(kUnusedUniform != programData->fUniLocations.fColorFilterUni);
1344 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001345
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001346 if (kUseUniform == programData->fUniLocations.fColorMatrixUni) {
1347 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorMatrixUni,
1348 GetUniformLocation(progID, COL_MATRIX_UNI_NAME));
1349 }
1350
1351 if (kUseUniform == programData->fUniLocations.fColorMatrixVecUni) {
1352 GR_GL_CALL_RET(gl, programData->fUniLocations.fColorMatrixVecUni,
1353 GetUniformLocation(progID, COL_MATRIX_VEC_UNI_NAME));
1354 }
bsalomon@google.com2401ae82012-01-17 21:03:05 +00001355 if (kUseUniform == programData->fUniLocations.fCoverageUni) {
1356 GR_GL_CALL_RET(gl, programData->fUniLocations.fCoverageUni,
1357 GetUniformLocation(progID, COV_UNI_NAME));
1358 GrAssert(kUnusedUniform != programData->fUniLocations.fCoverageUni);
1359 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001360
bsalomon@google.comf2d91552011-05-16 20:56:06 +00001361 if (kUseUniform == programData->fUniLocations.fEdgesUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001362 GR_GL_CALL_RET(gl, programData->fUniLocations.fEdgesUni,
1363 GetUniformLocation(progID, EDGES_UNI_NAME));
senorblanco@chromium.org92e0f222011-05-12 15:49:15 +00001364 GrAssert(kUnusedUniform != programData->fUniLocations.fEdgesUni);
1365 } else {
1366 programData->fUniLocations.fEdgesUni = kUnusedUniform;
1367 }
1368
tomhudson@google.com93813632011-10-27 20:21:16 +00001369 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001370 StageUniLocations& locations = programData->fUniLocations.fStages[s];
tomhudson@google.com0d831722011-06-02 15:37:14 +00001371 if (fProgramDesc.fStages[s].isEnabled()) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001372 if (kUseUniform == locations.fTextureMatrixUni) {
1373 GrStringBuilder texMName;
1374 tex_matrix_name(s, &texMName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001375 GR_GL_CALL_RET(gl, locations.fTextureMatrixUni,
1376 GetUniformLocation(progID, texMName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001377 GrAssert(kUnusedUniform != locations.fTextureMatrixUni);
1378 }
1379
1380 if (kUseUniform == locations.fSamplerUni) {
1381 GrStringBuilder samplerName;
1382 sampler_name(s, &samplerName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001383 GR_GL_CALL_RET(gl, locations.fSamplerUni,
1384 GetUniformLocation(progID,samplerName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001385 GrAssert(kUnusedUniform != locations.fSamplerUni);
1386 }
1387
1388 if (kUseUniform == locations.fNormalizedTexelSizeUni) {
1389 GrStringBuilder texelSizeName;
1390 normalized_texel_size_name(s, &texelSizeName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001391 GR_GL_CALL_RET(gl, locations.fNormalizedTexelSizeUni,
1392 GetUniformLocation(progID, texelSizeName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001393 GrAssert(kUnusedUniform != locations.fNormalizedTexelSizeUni);
1394 }
1395
1396 if (kUseUniform == locations.fRadial2Uni) {
1397 GrStringBuilder radial2ParamName;
1398 radial2_param_name(s, &radial2ParamName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001399 GR_GL_CALL_RET(gl, locations.fRadial2Uni,
1400 GetUniformLocation(progID, radial2ParamName.c_str()));
bsalomon@google.com91961302011-05-09 18:39:58 +00001401 GrAssert(kUnusedUniform != locations.fRadial2Uni);
1402 }
junov@google.com6acc9b32011-05-16 18:32:07 +00001403
1404 if (kUseUniform == locations.fTexDomUni) {
1405 GrStringBuilder texDomName;
1406 tex_domain_name(s, &texDomName);
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001407 GR_GL_CALL_RET(gl, locations.fTexDomUni,
1408 GetUniformLocation(progID, texDomName.c_str()));
junov@google.com6acc9b32011-05-16 18:32:07 +00001409 GrAssert(kUnusedUniform != locations.fTexDomUni);
1410 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001411
1412 GrStringBuilder kernelName, imageIncrementName;
1413 convolve_param_names(s, &kernelName, &imageIncrementName);
1414 if (kUseUniform == locations.fKernelUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001415 GR_GL_CALL_RET(gl, locations.fKernelUni,
1416 GetUniformLocation(progID, kernelName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001417 GrAssert(kUnusedUniform != locations.fKernelUni);
1418 }
1419
1420 if (kUseUniform == locations.fImageIncrementUni) {
bsalomon@google.com56bfc5a2011-09-01 13:28:16 +00001421 GR_GL_CALL_RET(gl, locations.fImageIncrementUni,
1422 GetUniformLocation(progID,
1423 imageIncrementName.c_str()));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001424 GrAssert(kUnusedUniform != locations.fImageIncrementUni);
1425 }
bsalomon@google.com91961302011-05-09 18:39:58 +00001426 }
1427 }
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001428 GR_GL_CALL(gl, UseProgram(progID));
bsalomon@google.com91961302011-05-09 18:39:58 +00001429
1430 // init sampler unis and set bogus values for state tracking
tomhudson@google.com93813632011-10-27 20:21:16 +00001431 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001432 if (kUnusedUniform != programData->fUniLocations.fStages[s].fSamplerUni) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001433 GR_GL_CALL(gl, Uniform1i(programData->fUniLocations.fStages[s].fSamplerUni, s));
bsalomon@google.com91961302011-05-09 18:39:58 +00001434 }
1435 programData->fTextureMatrices[s] = GrMatrix::InvalidMatrix();
1436 programData->fRadial2CenterX1[s] = GR_ScalarMax;
1437 programData->fRadial2Radius0[s] = -GR_ScalarMax;
1438 programData->fTextureWidth[s] = -1;
1439 programData->fTextureHeight[s] = -1;
1440 }
1441 programData->fViewMatrix = GrMatrix::InvalidMatrix();
1442 programData->fColor = GrColor_ILLEGAL;
Scroggo97c88c22011-05-11 14:05:25 +00001443 programData->fColorFilterColor = GrColor_ILLEGAL;
bsalomon@google.com91961302011-05-09 18:39:58 +00001444}
1445
junov@google.comf93e7172011-03-31 21:26:24 +00001446//============================================================================
1447// Stage code generation
1448//============================================================================
1449
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001450namespace {
1451
1452bool isRadialMapping(GrGLProgram::StageDesc::CoordMapping mapping) {
1453 return
1454 (GrGLProgram::StageDesc::kRadial2Gradient_CoordMapping == mapping ||
1455 GrGLProgram::StageDesc::kRadial2GradientDegenerate_CoordMapping == mapping);
1456}
1457
tomhudson@google.comda668982011-12-07 15:06:29 +00001458GrGLShaderVar* genRadialVS(int stageNum,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001459 ShaderCodeSegments* segments,
1460 GrGLProgram::StageUniLocations* locations,
1461 const char** radial2VaryingVSName,
1462 const char** radial2VaryingFSName,
1463 const char* varyingVSName,
1464 int varyingDims, int coordDims) {
1465
1466 GrGLShaderVar* radial2FSParams = &segments->fFSUnis.push_back();
1467 radial2FSParams->setType(GrGLShaderVar::kFloat_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001468 radial2FSParams->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001469 radial2FSParams->setArrayCount(6);
1470 radial2_param_name(stageNum, radial2FSParams->accessName());
1471 segments->fVSUnis.push_back(*radial2FSParams).setEmitPrecision(true);
1472
1473 locations->fRadial2Uni = kUseUniform;
1474
1475 // for radial grads without perspective we can pass the linear
1476 // part of the quadratic as a varying.
1477 if (varyingDims == coordDims) {
1478 GrAssert(2 == coordDims);
1479 append_varying(GrGLShaderVar::kFloat_Type,
1480 "Radial2BCoeff",
1481 stageNum,
1482 segments,
1483 radial2VaryingVSName,
1484 radial2VaryingFSName);
1485
tomhudson@google.comda668982011-12-07 15:06:29 +00001486 GrStringBuilder radial2p2;
1487 GrStringBuilder radial2p3;
1488 radial2FSParams->appendArrayAccess(2, &radial2p2);
1489 radial2FSParams->appendArrayAccess(3, &radial2p3);
1490
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001491 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
1492 const char* r2ParamName = radial2FSParams->getName().c_str();
tomhudson@google.comda668982011-12-07 15:06:29 +00001493 segments->fVSCode.appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
1494 *radial2VaryingVSName, radial2p2.c_str(),
1495 varyingVSName, radial2p3.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001496 }
1497
tomhudson@google.comda668982011-12-07 15:06:29 +00001498 return radial2FSParams;
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001499}
1500
1501bool genRadial2GradientCoordMapping(int stageNum,
1502 ShaderCodeSegments* segments,
1503 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001504 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001505 GrStringBuilder& sampleCoords,
1506 GrStringBuilder& fsCoordName,
1507 int varyingDims,
1508 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001509 GrStringBuilder cName("c");
1510 GrStringBuilder ac4Name("ac4");
1511 GrStringBuilder rootName("root");
1512
1513 cName.appendS32(stageNum);
1514 ac4Name.appendS32(stageNum);
1515 rootName.appendS32(stageNum);
1516
tomhudson@google.comda668982011-12-07 15:06:29 +00001517 GrStringBuilder radial2p0;
1518 GrStringBuilder radial2p1;
1519 GrStringBuilder radial2p2;
1520 GrStringBuilder radial2p3;
1521 GrStringBuilder radial2p4;
1522 GrStringBuilder radial2p5;
1523 radial2Params->appendArrayAccess(0, &radial2p0);
1524 radial2Params->appendArrayAccess(1, &radial2p1);
1525 radial2Params->appendArrayAccess(2, &radial2p2);
1526 radial2Params->appendArrayAccess(3, &radial2p3);
1527 radial2Params->appendArrayAccess(4, &radial2p4);
1528 radial2Params->appendArrayAccess(5, &radial2p5);
1529
tomhudson@google.com5960d002011-10-19 20:21:48 +00001530 // if we were able to interpolate the linear component bVar is the varying
1531 // otherwise compute it
1532 GrStringBuilder bVar;
1533 if (coordDims == varyingDims) {
1534 bVar = radial2VaryingFSName;
1535 GrAssert(2 == varyingDims);
1536 } else {
1537 GrAssert(3 == varyingDims);
1538 bVar = "b";
1539 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001540 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1541 bVar.c_str(), radial2p2.c_str(),
1542 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001543 }
1544
1545 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001546 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001547 cName.c_str(), fsCoordName.c_str(),
1548 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001549 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001550 // ac4 = 4.0 * params[0] * c
tomhudson@google.comda668982011-12-07 15:06:29 +00001551 segments->fFSCode.appendf("\tfloat %s = %s * 4.0 * %s;\n",
1552 ac4Name.c_str(), radial2p0.c_str(),
tomhudson@google.com5960d002011-10-19 20:21:48 +00001553 cName.c_str());
1554
1555 // root = sqrt(b^2-4ac)
1556 // (abs to avoid exception due to fp precision)
1557 segments->fFSCode.appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
1558 rootName.c_str(), bVar.c_str(), bVar.c_str(),
1559 ac4Name.c_str());
1560
1561 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
1562 // y coord is 0.5 (texture is effectively 1D)
tomhudson@google.comda668982011-12-07 15:06:29 +00001563 sampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)",
1564 bVar.c_str(), radial2p5.c_str(),
1565 rootName.c_str(), radial2p1.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001566 return true;
1567}
1568
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001569bool genRadial2GradientDegenerateCoordMapping(int stageNum,
1570 ShaderCodeSegments* segments,
1571 const char* radial2VaryingFSName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001572 GrGLShaderVar* radial2Params,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001573 GrStringBuilder& sampleCoords,
1574 GrStringBuilder& fsCoordName,
1575 int varyingDims,
1576 int coordDims) {
tomhudson@google.com5960d002011-10-19 20:21:48 +00001577 GrStringBuilder cName("c");
1578
1579 cName.appendS32(stageNum);
1580
tomhudson@google.comda668982011-12-07 15:06:29 +00001581 GrStringBuilder radial2p2;
1582 GrStringBuilder radial2p3;
1583 GrStringBuilder radial2p4;
1584 radial2Params->appendArrayAccess(2, &radial2p2);
1585 radial2Params->appendArrayAccess(3, &radial2p3);
1586 radial2Params->appendArrayAccess(4, &radial2p4);
1587
tomhudson@google.com5960d002011-10-19 20:21:48 +00001588 // if we were able to interpolate the linear component bVar is the varying
1589 // otherwise compute it
1590 GrStringBuilder bVar;
1591 if (coordDims == varyingDims) {
1592 bVar = radial2VaryingFSName;
1593 GrAssert(2 == varyingDims);
1594 } else {
1595 GrAssert(3 == varyingDims);
1596 bVar = "b";
1597 bVar.appendS32(stageNum);
tomhudson@google.comda668982011-12-07 15:06:29 +00001598 segments->fFSCode.appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
1599 bVar.c_str(), radial2p2.c_str(),
1600 fsCoordName.c_str(), radial2p3.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001601 }
1602
1603 // c = (x^2)+(y^2) - params[4]
tomhudson@google.comda668982011-12-07 15:06:29 +00001604 segments->fFSCode.appendf("\tfloat %s = dot(%s, %s) - %s;\n",
tomhudson@google.com5960d002011-10-19 20:21:48 +00001605 cName.c_str(), fsCoordName.c_str(),
1606 fsCoordName.c_str(),
tomhudson@google.comda668982011-12-07 15:06:29 +00001607 radial2p4.c_str());
tomhudson@google.com5960d002011-10-19 20:21:48 +00001608
1609 // x coord is: -c/b
1610 // y coord is 0.5 (texture is effectively 1D)
1611 sampleCoords.printf("vec2((-%s / %s), 0.5)", cName.c_str(), bVar.c_str());
1612 return true;
1613}
1614
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001615void gen2x2FS(int stageNum,
1616 ShaderCodeSegments* segments,
1617 GrGLProgram::StageUniLocations* locations,
1618 GrStringBuilder* sampleCoords,
1619 const char* samplerName,
1620 const char* texelSizeName,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001621 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001622 const char* fsOutColor,
1623 GrStringBuilder& texFunc,
1624 GrStringBuilder& modulate,
1625 bool complexCoord,
1626 int coordDims) {
1627 locations->fNormalizedTexelSizeUni = kUseUniform;
1628 if (complexCoord) {
1629 // assign the coord to a var rather than compute 4x.
1630 GrStringBuilder coordVar("tCoord");
1631 coordVar.appendS32(stageNum);
1632 segments->fFSCode.appendf("\t%s %s = %s;\n",
1633 float_vector_type_str(coordDims),
1634 coordVar.c_str(), sampleCoords->c_str());
1635 *sampleCoords = coordVar;
1636 }
1637 GrAssert(2 == coordDims);
1638 GrStringBuilder accumVar("accum");
1639 accumVar.appendS32(stageNum);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001640 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);
1641 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);
1642 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);
1643 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 +00001644 segments->fFSCode.appendf("\t%s = .25 * %s%s;\n", fsOutColor, accumVar.c_str(), modulate.c_str());
1645
1646}
1647
1648void genConvolutionVS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001649 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001650 ShaderCodeSegments* segments,
1651 GrGLProgram::StageUniLocations* locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001652 GrGLShaderVar** kernel,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001653 const char** imageIncrementName,
1654 const char* varyingVSName) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001655 //GrGLShaderVar* kernel = &segments->fFSUnis.push_back();
1656 *kernel = &segments->fFSUnis.push_back();
1657 (*kernel)->setType(GrGLShaderVar::kFloat_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001658 (*kernel)->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.comda668982011-12-07 15:06:29 +00001659 (*kernel)->setArrayCount(desc.fKernelWidth);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001660 GrGLShaderVar* imgInc = &segments->fFSUnis.push_back();
1661 imgInc->setType(GrGLShaderVar::kVec2f_Type);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001662 imgInc->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001663
1664 convolve_param_names(stageNum,
tomhudson@google.comda668982011-12-07 15:06:29 +00001665 (*kernel)->accessName(),
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001666 imgInc->accessName());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001667 *imageIncrementName = imgInc->getName().c_str();
1668
1669 // need image increment in both VS and FS
1670 segments->fVSUnis.push_back(*imgInc).setEmitPrecision(true);
1671
1672 locations->fKernelUni = kUseUniform;
1673 locations->fImageIncrementUni = kUseUniform;
1674 float scale = (desc.fKernelWidth - 1) * 0.5f;
1675 segments->fVSCode.appendf("\t%s -= vec2(%g, %g) * %s;\n",
1676 varyingVSName, scale, scale,
1677 *imageIncrementName);
1678}
1679
1680void genConvolutionFS(int stageNum,
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001681 const StageDesc& desc,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001682 ShaderCodeSegments* segments,
1683 const char* samplerName,
tomhudson@google.comda668982011-12-07 15:06:29 +00001684 GrGLShaderVar* kernel,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001685 const char* swizzle,
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001686 const char* imageIncrementName,
1687 const char* fsOutColor,
1688 GrStringBuilder& sampleCoords,
1689 GrStringBuilder& texFunc,
1690 GrStringBuilder& modulate) {
1691 GrStringBuilder sumVar("sum");
1692 sumVar.appendS32(stageNum);
1693 GrStringBuilder coordVar("coord");
1694 coordVar.appendS32(stageNum);
1695
tomhudson@google.comda668982011-12-07 15:06:29 +00001696 GrStringBuilder kernelIndex;
1697 kernel->appendArrayAccess("i", &kernelIndex);
1698
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001699 segments->fFSCode.appendf("\tvec4 %s = vec4(0, 0, 0, 0);\n",
1700 sumVar.c_str());
1701 segments->fFSCode.appendf("\tvec2 %s = %s;\n",
1702 coordVar.c_str(),
1703 sampleCoords.c_str());
1704 segments->fFSCode.appendf("\tfor (int i = 0; i < %d; i++) {\n",
1705 desc.fKernelWidth);
tomhudson@google.comda668982011-12-07 15:06:29 +00001706 segments->fFSCode.appendf("\t\t%s += %s(%s, %s)%s * %s;\n",
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001707 sumVar.c_str(), texFunc.c_str(),
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001708 samplerName, coordVar.c_str(), swizzle,
tomhudson@google.comda668982011-12-07 15:06:29 +00001709 kernelIndex.c_str());
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001710 segments->fFSCode.appendf("\t\t%s += %s;\n",
1711 coordVar.c_str(),
1712 imageIncrementName);
1713 segments->fFSCode.appendf("\t}\n");
1714 segments->fFSCode.appendf("\t%s = %s%s;\n", fsOutColor,
1715 sumVar.c_str(), modulate.c_str());
1716}
1717
1718}
1719
bsalomon@google.com0b77d682011-08-19 13:28:54 +00001720void GrGLProgram::genStageCode(const GrGLInterface* gl,
1721 int stageNum,
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001722 const GrGLProgram::StageDesc& desc,
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001723 const char* fsInColor, // NULL means no incoming color
1724 const char* fsOutColor,
1725 const char* vsInCoord,
1726 ShaderCodeSegments* segments,
1727 StageUniLocations* locations) const {
junov@google.comf93e7172011-03-31 21:26:24 +00001728
bsalomon@google.com74b98712011-11-11 19:46:16 +00001729 GrAssert(stageNum >= 0 && stageNum <= GrDrawState::kNumStages);
1730 GrAssert((desc.fInConfigFlags & StageDesc::kInConfigBitMask) ==
1731 desc.fInConfigFlags);
junov@google.comf93e7172011-03-31 21:26:24 +00001732
junov@google.comf93e7172011-03-31 21:26:24 +00001733 // First decide how many coords are needed to access the texture
1734 // Right now it's always 2 but we could start using 1D textures for
1735 // gradients.
1736 static const int coordDims = 2;
1737 int varyingDims;
1738 /// Vertex Shader Stuff
1739
1740 // decide whether we need a matrix to transform texture coords
1741 // and whether the varying needs a perspective coord.
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001742 const char* matName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001743 if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001744 varyingDims = coordDims;
1745 } else {
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001746 GrGLShaderVar* mat;
bsalomon@google.com4be283f2011-04-19 21:15:09 +00001747 #if GR_GL_ATTRIBUTE_MATRICES
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001748 mat = &segments->fVSAttrs.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001749 mat->setTypeModifier(GrGLShaderVar::kAttribute_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001750 locations->fTextureMatrixUni = kSetAsAttribute;
junov@google.comf93e7172011-03-31 21:26:24 +00001751 #else
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001752 mat = &segments->fVSUnis.push_back();
tomhudson@google.com086e5352011-12-08 14:44:10 +00001753 mat->setTypeModifier(GrGLShaderVar::kUniform_TypeModifier);
bsalomon@google.com91961302011-05-09 18:39:58 +00001754 locations->fTextureMatrixUni = kUseUniform;
junov@google.comf93e7172011-03-31 21:26:24 +00001755 #endif
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001756 tex_matrix_name(stageNum, mat->accessName());
1757 mat->setType(GrGLShaderVar::kMat33f_Type);
1758 matName = mat->getName().c_str();
1759
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001760 if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
junov@google.comf93e7172011-03-31 21:26:24 +00001761 varyingDims = coordDims;
1762 } else {
1763 varyingDims = coordDims + 1;
1764 }
1765 }
1766
tomhudson@google.com086e5352011-12-08 14:44:10 +00001767 segments->fFSUnis.push_back().set(GrGLShaderVar::kSampler2D_Type,
1768 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001769 sampler_name(stageNum, segments->fFSUnis.back().accessName());
bsalomon@google.com91961302011-05-09 18:39:58 +00001770 locations->fSamplerUni = kUseUniform;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001771 const char* samplerName = segments->fFSUnis.back().getName().c_str();
junov@google.comf93e7172011-03-31 21:26:24 +00001772
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001773 const char* texelSizeName = NULL;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001774 if (StageDesc::k2x2_FetchMode == desc.fFetchMode) {
tomhudson@google.com086e5352011-12-08 14:44:10 +00001775 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec2f_Type,
1776 GrGLShaderVar::kUniform_TypeModifier, "");
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001777 normalized_texel_size_name(stageNum, segments->fFSUnis.back().accessName());
1778 texelSizeName = segments->fFSUnis.back().getName().c_str();
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001779 }
1780
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001781 const char *varyingVSName, *varyingFSName;
1782 append_varying(float_vector_type(varyingDims),
1783 "Stage",
1784 stageNum,
1785 segments,
1786 &varyingVSName,
1787 &varyingFSName);
junov@google.comf93e7172011-03-31 21:26:24 +00001788
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001789 if (!matName) {
junov@google.comf93e7172011-03-31 21:26:24 +00001790 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001791 segments->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
junov@google.comf93e7172011-03-31 21:26:24 +00001792 } else {
bsalomon@google.com91961302011-05-09 18:39:58 +00001793 // varying = texMatrix * texCoord
1794 segments->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001795 varyingVSName, matName, vsInCoord,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001796 vector_all_coords(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001797 }
1798
tomhudson@google.comda668982011-12-07 15:06:29 +00001799 GrGLShaderVar* radial2Params = NULL;
1800 const char* radial2VaryingVSName = NULL;
1801 const char* radial2VaryingFSName = NULL;
junov@google.comf93e7172011-03-31 21:26:24 +00001802
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001803 if (isRadialMapping((StageDesc::CoordMapping) desc.fCoordMapping)) {
tomhudson@google.comda668982011-12-07 15:06:29 +00001804 radial2Params = genRadialVS(stageNum, segments,
1805 locations,
1806 &radial2VaryingVSName,
1807 &radial2VaryingFSName,
1808 varyingVSName,
1809 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001810 }
1811
tomhudson@google.comda668982011-12-07 15:06:29 +00001812 GrGLShaderVar* kernel = NULL;
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001813 const char* imageIncrementName = NULL;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001814 if (StageDesc::kConvolution_FetchMode == desc.fFetchMode) {
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001815 genConvolutionVS(stageNum, desc, segments, locations,
tomhudson@google.comda668982011-12-07 15:06:29 +00001816 &kernel, &imageIncrementName, varyingVSName);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001817 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001818
junov@google.comf93e7172011-03-31 21:26:24 +00001819 /// Fragment Shader Stuff
bsalomon@google.com91961302011-05-09 18:39:58 +00001820 GrStringBuilder fsCoordName;
junov@google.comf93e7172011-03-31 21:26:24 +00001821 // function used to access the shader, may be made projective
bsalomon@google.com91961302011-05-09 18:39:58 +00001822 GrStringBuilder texFunc("texture2D");
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001823 if (desc.fOptFlags & (StageDesc::kIdentityMatrix_OptFlagBit |
1824 StageDesc::kNoPerspective_OptFlagBit)) {
junov@google.comf93e7172011-03-31 21:26:24 +00001825 GrAssert(varyingDims == coordDims);
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001826 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001827 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001828 // if we have to do some special op on the varyings to get
junov@google.comf93e7172011-03-31 21:26:24 +00001829 // our final tex coords then when in perspective we have to
bsalomon@google.com91961302011-05-09 18:39:58 +00001830 // do an explicit divide. Otherwise, we can use a Proj func.
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001831 if (StageDesc::kIdentity_CoordMapping == desc.fCoordMapping &&
1832 StageDesc::kSingle_FetchMode == desc.fFetchMode) {
bsalomon@google.com91961302011-05-09 18:39:58 +00001833 texFunc.append("Proj");
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001834 fsCoordName = varyingFSName;
junov@google.comf93e7172011-03-31 21:26:24 +00001835 } else {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001836 fsCoordName = "inCoord";
bsalomon@google.comfc296292011-05-06 13:53:47 +00001837 fsCoordName.appendS32(stageNum);
bsalomon@google.com91961302011-05-09 18:39:58 +00001838 segments->fFSCode.appendf("\t%s %s = %s%s / %s%s;\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001839 GrGLShaderVar::TypeString(float_vector_type(coordDims)),
1840 fsCoordName.c_str(),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001841 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001842 vector_nonhomog_coords(varyingDims),
bsalomon@google.comedfe1aa2011-09-29 14:40:26 +00001843 varyingFSName,
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001844 vector_homog_coord(varyingDims));
junov@google.comf93e7172011-03-31 21:26:24 +00001845 }
1846 }
1847
bsalomon@google.comfc296292011-05-06 13:53:47 +00001848 GrStringBuilder sampleCoords;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001849 bool complexCoord = false;
junov@google.comf93e7172011-03-31 21:26:24 +00001850 switch (desc.fCoordMapping) {
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001851 case StageDesc::kIdentity_CoordMapping:
junov@google.comf93e7172011-03-31 21:26:24 +00001852 sampleCoords = fsCoordName;
1853 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001854 case StageDesc::kSweepGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001855 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 +00001856 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001857 break;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001858 case StageDesc::kRadialGradient_CoordMapping:
bsalomon@google.com91961302011-05-09 18:39:58 +00001859 sampleCoords.printf("vec2(length(%s.xy), 0.5)", fsCoordName.c_str());
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001860 complexCoord = true;
junov@google.comf93e7172011-03-31 21:26:24 +00001861 break;
tomhudson@google.com5960d002011-10-19 20:21:48 +00001862 case StageDesc::kRadial2Gradient_CoordMapping:
1863 complexCoord = genRadial2GradientCoordMapping(
1864 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001865 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001866 sampleCoords, fsCoordName,
1867 varyingDims, coordDims);
junov@google.comf93e7172011-03-31 21:26:24 +00001868
tomhudson@google.com5960d002011-10-19 20:21:48 +00001869 break;
1870 case StageDesc::kRadial2GradientDegenerate_CoordMapping:
1871 complexCoord = genRadial2GradientDegenerateCoordMapping(
1872 stageNum, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001873 radial2VaryingFSName, radial2Params,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001874 sampleCoords, fsCoordName,
1875 varyingDims, coordDims);
1876 break;
junov@google.comf93e7172011-03-31 21:26:24 +00001877
junov@google.comf93e7172011-03-31 21:26:24 +00001878 };
1879
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001880 const char* swizzle = "";
bsalomon@google.com74b98712011-11-11 19:46:16 +00001881 if (desc.fInConfigFlags & StageDesc::kSwapRAndB_InConfigFlag) {
1882 GrAssert(!(desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag));
1883 swizzle = ".bgra";
1884 } else if (desc.fInConfigFlags & StageDesc::kSmearAlpha_InConfigFlag) {
1885 GrAssert(!(desc.fInConfigFlags &
1886 StageDesc::kMulRGBByAlpha_InConfigFlag));
1887 swizzle = ".aaaa";
1888 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001889
bsalomon@google.com91961302011-05-09 18:39:58 +00001890 GrStringBuilder modulate;
1891 if (NULL != fsInColor) {
1892 modulate.printf(" * %s", fsInColor);
1893 }
1894
tomhudson@google.com0d831722011-06-02 15:37:14 +00001895 if (desc.fOptFlags &
bsalomon@google.com22c5dea2011-07-07 14:38:03 +00001896 StageDesc::kCustomTextureDomain_OptFlagBit) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001897 GrStringBuilder texDomainName;
1898 tex_domain_name(stageNum, &texDomainName);
tomhudson@google.com086e5352011-12-08 14:44:10 +00001899 segments->fFSUnis.push_back().set(GrGLShaderVar::kVec4f_Type,
1900 GrGLShaderVar::kUniform_TypeModifier, texDomainName);
junov@google.com6acc9b32011-05-16 18:32:07 +00001901 GrStringBuilder coordVar("clampCoord");
1902 segments->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com4fa66942011-09-20 19:06:12 +00001903 float_vector_type_str(coordDims),
junov@google.com6acc9b32011-05-16 18:32:07 +00001904 coordVar.c_str(),
1905 sampleCoords.c_str(),
1906 texDomainName.c_str(),
1907 texDomainName.c_str());
1908 sampleCoords = coordVar;
1909 locations->fTexDomUni = kUseUniform;
1910 }
1911
tomhudson@google.com5960d002011-10-19 20:21:48 +00001912 switch (desc.fFetchMode) {
1913 case StageDesc::k2x2_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001914 GrAssert(!(desc.fInConfigFlags &
1915 StageDesc::kMulRGBByAlpha_InConfigFlag));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001916 gen2x2FS(stageNum, segments, locations, &sampleCoords,
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001917 samplerName, texelSizeName, swizzle, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001918 texFunc, modulate, complexCoord, coordDims);
1919 break;
bsalomon@google.com85b505b2011-11-07 14:56:51 +00001920 case StageDesc::kConvolution_FetchMode:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001921 GrAssert(!(desc.fInConfigFlags &
1922 StageDesc::kMulRGBByAlpha_InConfigFlag));
tomhudson@google.com2a2e3ef2011-10-25 19:51:09 +00001923 genConvolutionFS(stageNum, desc, segments,
tomhudson@google.comda668982011-12-07 15:06:29 +00001924 samplerName, kernel, swizzle, imageIncrementName, fsOutColor,
tomhudson@google.com5960d002011-10-19 20:21:48 +00001925 sampleCoords, texFunc, modulate);
1926 break;
1927 default:
bsalomon@google.com74b98712011-11-11 19:46:16 +00001928 if (desc.fInConfigFlags & StageDesc::kMulRGBByAlpha_InConfigFlag) {
1929 GrAssert(!(desc.fInConfigFlags &
1930 StageDesc::kSmearAlpha_InConfigFlag));
1931 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s;\n",
1932 fsOutColor, texFunc.c_str(),
1933 samplerName, sampleCoords.c_str(),
1934 swizzle);
1935 segments->fFSCode.appendf("\t%s = vec4(%s.rgb*%s.a,%s.a)%s;\n",
1936 fsOutColor, fsOutColor, fsOutColor,
1937 fsOutColor, modulate.c_str());
1938 } else {
1939 segments->fFSCode.appendf("\t%s = %s(%s, %s)%s%s;\n",
1940 fsOutColor, texFunc.c_str(),
1941 samplerName, sampleCoords.c_str(),
1942 swizzle, modulate.c_str());
1943 }
junov@google.comf93e7172011-03-31 21:26:24 +00001944 }
junov@google.comf93e7172011-03-31 21:26:24 +00001945}
tomhudson@google.com5960d002011-10-19 20:21:48 +00001946
tomhudson@google.com5960d002011-10-19 20:21:48 +00001947