blob: 970cbd4f0836d6151927314d684c81ce2f87c8dd [file] [log] [blame]
tomhudson@google.com7fab52d2012-05-31 19:40:13 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrGradientEffects.h"
9#include "gl/GrGLProgramStage.h"
10#include "GrProgramStageFactory.h"
11
12/////////////////////////////////////////////////////////////////////
13
14class GrGLRadialGradient : public GrGLProgramStage {
15
16public:
17
18 GrGLRadialGradient(const GrProgramStageFactory& factory,
19 const GrCustomStage&) : INHERITED (factory) { }
20 virtual ~GrGLRadialGradient() { }
21
22 virtual void emitVS(GrGLShaderBuilder* state,
23 const char* vertexCoords) SK_OVERRIDE { }
24 virtual void emitFS(GrGLShaderBuilder* state,
25 const char* outputColor,
26 const char* inputColor,
27 const char* samplerName) SK_OVERRIDE;
28
29 static StageKey GenKey(const GrCustomStage& s) { return 0; }
30
31private:
32
33 typedef GrGLProgramStage INHERITED;
34
35};
36
37void GrGLRadialGradient::emitFS(GrGLShaderBuilder* state,
rileya@google.com3e332582012-07-03 13:43:35 +000038 const char* outputColor,
39 const char* inputColor,
40 const char* samplerName) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000041 state->fSampleCoords.printf("vec2(length(%s.xy), 0.5)",
42 state->fSampleCoords.c_str());
43 state->fComplexCoord = true;
44
45 state->emitDefaultFetch(outputColor, samplerName);
46}
47
48
49/////////////////////////////////////////////////////////////////////
50
51
52GrRadialGradient::GrRadialGradient() {
53
54}
55
56GrRadialGradient::~GrRadialGradient() {
57
58}
59
60
61const GrProgramStageFactory& GrRadialGradient::getFactory() const {
62 return GrTProgramStageFactory<GrRadialGradient>::getInstance();
63}
64
65bool GrRadialGradient::isEqual(const GrCustomStage& sBase) const {
66 return true;
67}
68
69/////////////////////////////////////////////////////////////////////
70
71class GrGLRadial2Gradient : public GrGLProgramStage {
72
73public:
74
75 GrGLRadial2Gradient(const GrProgramStageFactory& factory,
76 const GrCustomStage&);
77 virtual ~GrGLRadial2Gradient() { }
78
79 virtual void setupVariables(GrGLShaderBuilder* state,
80 int stage) SK_OVERRIDE;
81 virtual void emitVS(GrGLShaderBuilder* state,
82 const char* vertexCoords) SK_OVERRIDE;
83 virtual void emitFS(GrGLShaderBuilder* state,
84 const char* outputColor,
85 const char* inputColor,
86 const char* samplerName) SK_OVERRIDE;
87 virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE;
88 virtual void setData(const GrGLInterface*,
89 const GrGLTexture&,
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +000090 const GrCustomStage&,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000091 int stageNum) SK_OVERRIDE;
92
93 static StageKey GenKey(const GrCustomStage& s) {
94 return (static_cast<const GrRadial2Gradient&>(s).isDegenerate());
95 }
96
97protected:
98
tomhudson@google.com761b37c2012-06-13 15:22:18 +000099 const GrGLShaderVar* fVSParamVar;
100 GrGLint fVSParamLocation;
101 const GrGLShaderVar* fFSParamVar;
102 GrGLint fFSParamLocation;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000103
104 const char* fVSVaryingName;
105 const char* fFSVaryingName;
106
107 bool fIsDegenerate;
108
109 // @{
110 /// Values last uploaded as uniforms
111
112 GrScalar fCachedCenter;
113 GrScalar fCachedRadius;
bsalomon@google.com0b323312012-06-01 18:50:01 +0000114 bool fCachedPosRoot;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000115
116 // @}
117
118private:
119
120 typedef GrGLProgramStage INHERITED;
121
122};
123
124GrGLRadial2Gradient::GrGLRadial2Gradient(
125 const GrProgramStageFactory& factory,
126 const GrCustomStage& baseData)
127 : INHERITED(factory)
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000128 , fVSParamVar(NULL)
129 , fFSParamVar(NULL)
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000130 , fVSVaryingName(NULL)
131 , fFSVaryingName(NULL)
132 , fCachedCenter(GR_ScalarMax)
133 , fCachedRadius(-GR_ScalarMax)
134 , fCachedPosRoot(0) {
135
136 const GrRadial2Gradient& data =
137 static_cast<const GrRadial2Gradient&>(baseData);
138 fIsDegenerate = data.isDegenerate();
139}
140
141void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) {
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000142 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
143 // to work around Xoom bug. Doesn't seem to cause performance decrease
144 // in test apps, but need to keep an eye on it.
145 fVSParamVar = &state->addUniform(
146 GrGLShaderBuilder::kVertex_VariableLifetime,
147 kFloat_GrSLType, "uRadial2VSParams", stage, 6);
148 fFSParamVar = &state->addUniform(
149 GrGLShaderBuilder::kFragment_VariableLifetime,
150 kFloat_GrSLType, "uRadial2FSParams", stage, 6);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000151
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000152 fVSParamLocation = GrGLProgramStage::kUseUniform;
153 fFSParamLocation = GrGLProgramStage::kUseUniform;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000154
155 // For radial gradients without perspective we can pass the linear
156 // part of the quadratic as a varying.
157 if (state->fVaryingDims == state->fCoordDims) {
158 state->addVarying(kFloat_GrSLType, "Radial2BCoeff", stage,
159 &fVSVaryingName, &fFSVaryingName);
160 }
161}
162
163void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* state,
rileya@google.com3e332582012-07-03 13:43:35 +0000164 const char* vertexCoords) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000165 GrStringBuilder* code = &state->fVSCode;
166 GrStringBuilder p2;
167 GrStringBuilder p3;
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000168 fVSParamVar->appendArrayAccess(2, &p2);
169 fVSParamVar->appendArrayAccess(3, &p3);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000170
171 // For radial gradients without perspective we can pass the linear
172 // part of the quadratic as a varying.
173 if (state->fVaryingDims == state->fCoordDims) {
174 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
175 code->appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
176 fVSVaryingName, p2.c_str(),
177 vertexCoords, p3.c_str());
178 }
179}
180
181void GrGLRadial2Gradient::emitFS(GrGLShaderBuilder* state,
rileya@google.com3e332582012-07-03 13:43:35 +0000182 const char* outputColor,
183 const char* inputColor,
184 const char* samplerName) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000185 GrStringBuilder* code = &state->fFSCode;
186 GrStringBuilder cName("c");
187 GrStringBuilder ac4Name("ac4");
188 GrStringBuilder rootName("root");
189 GrStringBuilder p0;
190 GrStringBuilder p1;
191 GrStringBuilder p2;
192 GrStringBuilder p3;
193 GrStringBuilder p4;
194 GrStringBuilder p5;
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000195 fFSParamVar->appendArrayAccess(0, &p0);
196 fFSParamVar->appendArrayAccess(1, &p1);
197 fFSParamVar->appendArrayAccess(2, &p2);
198 fFSParamVar->appendArrayAccess(3, &p3);
199 fFSParamVar->appendArrayAccess(4, &p4);
200 fFSParamVar->appendArrayAccess(5, &p5);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000201
202 // If we we're able to interpolate the linear component,
203 // bVar is the varying; otherwise compute it
204 GrStringBuilder bVar;
205 if (state->fCoordDims == state->fVaryingDims) {
206 bVar = fFSVaryingName;
207 GrAssert(2 == state->fVaryingDims);
208 } else {
209 GrAssert(3 == state->fVaryingDims);
210 bVar = "b";
211 //bVar.appendS32(stageNum);
212 code->appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
213 bVar.c_str(), p2.c_str(),
214 state->fSampleCoords.c_str(), p3.c_str());
215 }
216
217 // c = (x^2)+(y^2) - params[4]
218 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n",
219 cName.c_str(), state->fSampleCoords.c_str(),
220 state->fSampleCoords.c_str(),
221 p4.c_str());
222
223 // If we aren't degenerate, emit some extra code, and accept a slightly
224 // more complex coord.
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000225 if (!fIsDegenerate) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000226
227 // ac4 = 4.0 * params[0] * c
228 code->appendf("\tfloat %s = %s * 4.0 * %s;\n",
229 ac4Name.c_str(), p0.c_str(),
230 cName.c_str());
231
232 // root = sqrt(b^2-4ac)
233 // (abs to avoid exception due to fp precision)
234 code->appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
235 rootName.c_str(), bVar.c_str(), bVar.c_str(),
236 ac4Name.c_str());
237
238 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
239 // y coord is 0.5 (texture is effectively 1D)
240 state->fSampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)",
241 bVar.c_str(), p5.c_str(),
242 rootName.c_str(), p1.c_str());
243 } else {
244 // x coord is: -c/b
245 // y coord is 0.5 (texture is effectively 1D)
246 state->fSampleCoords.printf("vec2((-%s / %s), 0.5)",
247 cName.c_str(), bVar.c_str());
248 }
249 state->fComplexCoord = true;
250
251 state->emitDefaultFetch(outputColor, samplerName);
252}
253
254void GrGLRadial2Gradient::initUniforms(const GrGLInterface* gl, int programID) {
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000255 GR_GL_CALL_RET(gl, fVSParamLocation,
256 GetUniformLocation(programID, fVSParamVar->getName().c_str()));
257 GR_GL_CALL_RET(gl, fFSParamLocation,
258 GetUniformLocation(programID, fFSParamVar->getName().c_str()));
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000259}
260
261void GrGLRadial2Gradient::setData(const GrGLInterface* gl,
rileya@google.com3e332582012-07-03 13:43:35 +0000262 const GrGLTexture& texture,
263 const GrCustomStage& baseData,
264 int stageNum) {
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000265 const GrRadial2Gradient& data =
266 static_cast<const GrRadial2Gradient&>(baseData);
267 GrAssert(data.isDegenerate() == fIsDegenerate);
268 GrScalar centerX1 = data.center();
269 GrScalar radius0 = data.radius();
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000270 if (fCachedCenter != centerX1 ||
271 fCachedRadius != radius0 ||
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000272 fCachedPosRoot != data.isPosRoot()) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000273
274 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
275
276 // When we're in the degenerate (linear) case, the second
277 // value will be INF but the program doesn't read it. (We
278 // use the same 6 uniforms even though we don't need them
279 // all in the linear case just to keep the code complexity
280 // down).
281 float values[6] = {
282 GrScalarToFloat(a),
283 1 / (2.f * GrScalarToFloat(a)),
284 GrScalarToFloat(centerX1),
285 GrScalarToFloat(radius0),
286 GrScalarToFloat(GrMul(radius0, radius0)),
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000287 data.isPosRoot() ? 1.f : -1.f
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000288 };
289
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000290 GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
291 GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000292 fCachedCenter = centerX1;
293 fCachedRadius = radius0;
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000294 fCachedPosRoot = data.isPosRoot();
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000295 }
296}
297
298
299/////////////////////////////////////////////////////////////////////
300
301GrRadial2Gradient::GrRadial2Gradient(GrScalar center,
302 GrScalar radius,
303 bool posRoot)
304 : fCenterX1 (center)
305 , fRadius0 (radius)
306 , fPosRoot (posRoot) {
307
308}
309
310GrRadial2Gradient::~GrRadial2Gradient() {
311
312}
313
314
315const GrProgramStageFactory& GrRadial2Gradient::getFactory() const {
316 return GrTProgramStageFactory<GrRadial2Gradient>::getInstance();
317}
318
319bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const {
320 const GrRadial2Gradient& s = static_cast<const GrRadial2Gradient&>(sBase);
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000321 return (this->fCenterX1 == s.fCenterX1 &&
322 this->fRadius0 == s.fRadius0 &&
323 this->fPosRoot == s.fPosRoot);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000324}
325
326/////////////////////////////////////////////////////////////////////
327
rileya@google.com3e332582012-07-03 13:43:35 +0000328class GrGLConical2Gradient : public GrGLProgramStage {
329
330public:
331
332 GrGLConical2Gradient(const GrProgramStageFactory& factory,
333 const GrCustomStage&);
334 virtual ~GrGLConical2Gradient() { }
335
336 virtual void setupVariables(GrGLShaderBuilder* state,
337 int stage) SK_OVERRIDE;
338 virtual void emitVS(GrGLShaderBuilder* state,
339 const char* vertexCoords) SK_OVERRIDE;
340 virtual void emitFS(GrGLShaderBuilder* state,
341 const char* outputColor,
342 const char* inputColor,
343 const char* samplerName) SK_OVERRIDE;
344 virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE;
345 virtual void setData(const GrGLInterface*,
346 const GrGLTexture&,
347 const GrCustomStage&,
348 int stageNum) SK_OVERRIDE;
349
350 static StageKey GenKey(const GrCustomStage& s) {
351 return (static_cast<const GrConical2Gradient&>(s).isDegenerate());
352 }
353
354protected:
355
356 const GrGLShaderVar* fVSParamVar;
357 GrGLint fVSParamLocation;
358 const GrGLShaderVar* fFSParamVar;
359 GrGLint fFSParamLocation;
360
361 const char* fVSVaryingName;
362 const char* fFSVaryingName;
363
364 bool fIsDegenerate;
365
366 // @{
367 /// Values last uploaded as uniforms
368
369 GrScalar fCachedCenter;
370 GrScalar fCachedRadius;
371 GrScalar fCachedDiffRadius;
372
373 // @}
374
375private:
376
377 typedef GrGLProgramStage INHERITED;
378
379};
380
381GrGLConical2Gradient::GrGLConical2Gradient(
382 const GrProgramStageFactory& factory,
383 const GrCustomStage& baseData)
384 : INHERITED(factory)
385 , fVSParamVar(NULL)
386 , fFSParamVar(NULL)
387 , fVSVaryingName(NULL)
388 , fFSVaryingName(NULL)
389 , fCachedCenter(GR_ScalarMax)
390 , fCachedRadius(-GR_ScalarMax)
391 , fCachedDiffRadius(-GR_ScalarMax) {
392
393 const GrConical2Gradient& data =
394 static_cast<const GrConical2Gradient&>(baseData);
395 fIsDegenerate = data.isDegenerate();
396}
397
398void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) {
399 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
400 // to work around Xoom bug. Doesn't seem to cause performance decrease
401 // in test apps, but need to keep an eye on it.
402 fVSParamVar = &state->addUniform(
403 GrGLShaderBuilder::kVertex_VariableLifetime,
404 kFloat_GrSLType, "uConical2VSParams", stage, 6);
405 fFSParamVar = &state->addUniform(
406 GrGLShaderBuilder::kFragment_VariableLifetime,
407 kFloat_GrSLType, "uConical2FSParams", stage, 6);
408
409 fVSParamLocation = GrGLProgramStage::kUseUniform;
410 fFSParamLocation = GrGLProgramStage::kUseUniform;
411
412 // For radial gradients without perspective we can pass the linear
413 // part of the quadratic as a varying.
414 if (state->fVaryingDims == state->fCoordDims) {
415 state->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage,
416 &fVSVaryingName, &fFSVaryingName);
417 }
418}
419
420void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* state,
421 const char* vertexCoords) {
422 GrStringBuilder* code = &state->fVSCode;
423 GrStringBuilder p2; // distance between centers
424 GrStringBuilder p3; // start radius
425 GrStringBuilder p5; // difference in radii (r1 - r0)
426 fVSParamVar->appendArrayAccess(2, &p2);
427 fVSParamVar->appendArrayAccess(3, &p3);
428 fVSParamVar->appendArrayAccess(5, &p5);
429
430 // For radial gradients without perspective we can pass the linear
431 // part of the quadratic as a varying.
432 if (state->fVaryingDims == state->fCoordDims) {
433 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
434 code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
435 fVSVaryingName, p2.c_str(),
436 vertexCoords, p3.c_str(), p5.c_str());
437 }
438}
439
440void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* state,
441 const char* outputColor,
442 const char* inputColor,
443 const char* samplerName) {
444 GrStringBuilder* code = &state->fFSCode;
445
446 GrStringBuilder cName("c");
447 GrStringBuilder ac4Name("ac4");
448 GrStringBuilder dName("d");
449 GrStringBuilder qName("q");
450 GrStringBuilder r0Name("r0");
451 GrStringBuilder r1Name("r1");
452 GrStringBuilder tName("t");
453 GrStringBuilder p0; // 4a
454 GrStringBuilder p1; // 1/(2a)
455 GrStringBuilder p2; // distance between centers
456 GrStringBuilder p3; // start radius
457 GrStringBuilder p4; // start radius squared
458 GrStringBuilder p5; // difference in radii (r1 - r0)
459 fFSParamVar->appendArrayAccess(0, &p0);
460 fFSParamVar->appendArrayAccess(1, &p1);
461 fFSParamVar->appendArrayAccess(2, &p2);
462 fFSParamVar->appendArrayAccess(3, &p3);
463 fFSParamVar->appendArrayAccess(4, &p4);
464 fFSParamVar->appendArrayAccess(5, &p5);
465
466 // If we we're able to interpolate the linear component,
467 // bVar is the varying; otherwise compute it
468 GrStringBuilder bVar;
469 if (state->fCoordDims == state->fVaryingDims) {
470 bVar = fFSVaryingName;
471 GrAssert(2 == state->fVaryingDims);
472 } else {
473 GrAssert(3 == state->fVaryingDims);
474 bVar = "b";
475 code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
476 bVar.c_str(), p2.c_str(), state->fSampleCoords.c_str(),
477 p3.c_str(), p5.c_str());
478 }
479
rileya@google.come38160c2012-07-03 18:03:04 +0000480 // output will default to transparent black (we simply won't write anything
481 // else to it if invalid, instead of discarding or returning prematurely)
482 code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
483
rileya@google.com3e332582012-07-03 13:43:35 +0000484 // c = (x^2)+(y^2) - params[4]
485 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
486 state->fSampleCoords.c_str(), state->fSampleCoords.c_str(),
487 p4.c_str());
488
489 // Non-degenerate case (quadratic)
490 if (!fIsDegenerate) {
491
492 // ac4 = params[0] * c
493 code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
494 cName.c_str());
495
496 // d = b^2 - ac4
497 code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
498 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
499
rileya@google.come38160c2012-07-03 18:03:04 +0000500 // only proceed if discriminant is >= 0
501 code->appendf("\tif (%s >= 0.0) {\n", dName.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000502
503 // intermediate value we'll use to compute the roots
504 // q = -0.5 * (b +/- sqrt(d))
rileya@google.come38160c2012-07-03 18:03:04 +0000505 code->appendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
rileya@google.com3e332582012-07-03 13:43:35 +0000506 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
507 bVar.c_str(), dName.c_str());
508
509 // compute both roots
510 // r0 = q * params[1]
rileya@google.come38160c2012-07-03 18:03:04 +0000511 code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
512 qName.c_str(), p1.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000513 // r1 = c / q
rileya@google.come38160c2012-07-03 18:03:04 +0000514 code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
515 cName.c_str(), qName.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000516
517 // Note: If there are two roots that both generate radius(t) > 0, the
518 // Canvas spec says to choose the larger t.
519
520 // so we'll look at the larger one first:
rileya@google.come38160c2012-07-03 18:03:04 +0000521 code->appendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000522 r0Name.c_str(), r1Name.c_str());
523
rileya@google.come38160c2012-07-03 18:03:04 +0000524 // if r(t) > 0, then we're done; t will be our x coordinate
525 code->appendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000526 p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000527
528 // y coord is 0.5 (texture is effectively 1D)
529 code->appendf("\t\t");
530 state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
531 state->emitDefaultFetch(outputColor, samplerName);
532
533 // otherwise, if r(t) for the larger root was <= 0, try the other root
534 code->appendf("\t\t} else {\n");
535 code->appendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000536 r0Name.c_str(), r1Name.c_str());
537
rileya@google.come38160c2012-07-03 18:03:04 +0000538 // if r(t) > 0 for the smaller root, then t will be our x coordinate
539 code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
rileya@google.com3e332582012-07-03 13:43:35 +0000540 tName.c_str(), p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000541
542 // y coord is 0.5 (texture is effectively 1D)
543 code->appendf("\t\t\t");
544 state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
545 state->emitDefaultFetch(outputColor, samplerName);
546
547 // end if (r(t) > 0) for smaller root
548 code->appendf("\t\t\t}\n");
549 // end if (r(t) > 0), else, for larger root
550 code->appendf("\t\t}\n");
551 // end if (discriminant >= 0)
rileya@google.com3e332582012-07-03 13:43:35 +0000552 code->appendf("\t}\n");
553 } else {
554
555 // linear case: t = -c/b
556 code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
557 cName.c_str(), bVar.c_str());
558
rileya@google.come38160c2012-07-03 18:03:04 +0000559 // if r(t) > 0, then t will be the x coordinate
560 code->appendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000561 p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000562 code->appendf("\t");
563 state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
564 state->emitDefaultFetch(outputColor, samplerName);
565 code->appendf("\t}\n");
rileya@google.com3e332582012-07-03 13:43:35 +0000566 }
567 state->fComplexCoord = true;
rileya@google.com3e332582012-07-03 13:43:35 +0000568}
569
570void GrGLConical2Gradient::initUniforms(const GrGLInterface* gl, int programID) {
571 GR_GL_CALL_RET(gl, fVSParamLocation,
572 GetUniformLocation(programID, fVSParamVar->getName().c_str()));
573 GR_GL_CALL_RET(gl, fFSParamLocation,
574 GetUniformLocation(programID, fFSParamVar->getName().c_str()));
575}
576
577void GrGLConical2Gradient::setData(const GrGLInterface* gl,
578 const GrGLTexture& texture,
579 const GrCustomStage& baseData,
580 int stageNum) {
581 const GrConical2Gradient& data =
582 static_cast<const GrConical2Gradient&>(baseData);
583 GrAssert(data.isDegenerate() == fIsDegenerate);
584 GrScalar centerX1 = data.center();
585 GrScalar radius0 = data.radius();
586 GrScalar diffRadius = data.diffRadius();
587
588 if (fCachedCenter != centerX1 ||
589 fCachedRadius != radius0 ||
590 fCachedDiffRadius != diffRadius) {
591
592 GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius;
593
594 // When we're in the degenerate (linear) case, the second
595 // value will be INF but the program doesn't read it. (We
596 // use the same 6 uniforms even though we don't need them
597 // all in the linear case just to keep the code complexity
598 // down).
599 float values[6] = {
600 GrScalarToFloat(a * 4),
601 1.f / (GrScalarToFloat(a)),
602 GrScalarToFloat(centerX1),
603 GrScalarToFloat(radius0),
604 GrScalarToFloat(SkScalarMul(radius0, radius0)),
605 GrScalarToFloat(diffRadius)
606 };
607
608 GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
609 GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
610 fCachedCenter = centerX1;
611 fCachedRadius = radius0;
612 fCachedDiffRadius = diffRadius;
613 }
614}
615
616
617/////////////////////////////////////////////////////////////////////
618
619GrConical2Gradient::GrConical2Gradient(GrScalar center,
620 GrScalar radius,
621 GrScalar diffRadius)
622 : fCenterX1 (center)
623 , fRadius0 (radius)
624 , fDiffRadius (diffRadius) {
625
626}
627
628GrConical2Gradient::~GrConical2Gradient() {
629
630}
631
632
633const GrProgramStageFactory& GrConical2Gradient::getFactory() const {
634 return GrTProgramStageFactory<GrConical2Gradient>::getInstance();
635}
636
637bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const {
638 const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase);
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000639 return (this->fCenterX1 == s.fCenterX1 &&
640 this->fRadius0 == s.fRadius0 &&
641 this->fDiffRadius == s.fDiffRadius);
rileya@google.com3e332582012-07-03 13:43:35 +0000642}
643
644/////////////////////////////////////////////////////////////////////
645
646
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000647class GrGLSweepGradient : public GrGLProgramStage {
648
649public:
650
651 GrGLSweepGradient(const GrProgramStageFactory& factory,
652 const GrCustomStage&) : INHERITED (factory) { }
653 virtual ~GrGLSweepGradient() { }
654
655 virtual void emitVS(GrGLShaderBuilder* state,
656 const char* vertexCoords) SK_OVERRIDE { }
657 virtual void emitFS(GrGLShaderBuilder* state,
658 const char* outputColor,
659 const char* inputColor,
660 const char* samplerName) SK_OVERRIDE;
661
662 static StageKey GenKey(const GrCustomStage& s) { return 0; }
663
664private:
665
666 typedef GrGLProgramStage INHERITED;
667
668};
669
670void GrGLSweepGradient::emitFS(GrGLShaderBuilder* state,
671 const char* outputColor,
672 const char* inputColor,
673 const char* samplerName) {
674 state->fSampleCoords.printf(
675 "vec2(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5, 0.5)",
676 state->fSampleCoords.c_str(), state->fSampleCoords.c_str());
677 state->fComplexCoord = true;
678
679 state->emitDefaultFetch(outputColor, samplerName);
680}
681
682/////////////////////////////////////////////////////////////////////
683
684GrSweepGradient::GrSweepGradient() {
685
686}
687
688GrSweepGradient::~GrSweepGradient() {
689
690}
691
692const GrProgramStageFactory& GrSweepGradient::getFactory() const {
693 return GrTProgramStageFactory<GrSweepGradient>::getInstance();
694}
695
696bool GrSweepGradient::isEqual(const GrCustomStage& sBase) const {
697 return true;
698}
699