blob: d4719b3a2452e59ed05232f7020c19af63fa1e2b [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
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000052GrRadialGradient::GrRadialGradient(GrTexture* texture)
53 : GrSingleTextureEffect(texture) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000054
55}
56
57GrRadialGradient::~GrRadialGradient() {
58
59}
60
61
62const GrProgramStageFactory& GrRadialGradient::getFactory() const {
63 return GrTProgramStageFactory<GrRadialGradient>::getInstance();
64}
65
66bool GrRadialGradient::isEqual(const GrCustomStage& sBase) const {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +000067 return INHERITED::isEqual(sBase);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000068}
69
70/////////////////////////////////////////////////////////////////////
71
72class GrGLRadial2Gradient : public GrGLProgramStage {
73
74public:
75
76 GrGLRadial2Gradient(const GrProgramStageFactory& factory,
77 const GrCustomStage&);
78 virtual ~GrGLRadial2Gradient() { }
79
80 virtual void setupVariables(GrGLShaderBuilder* state,
81 int stage) SK_OVERRIDE;
82 virtual void emitVS(GrGLShaderBuilder* state,
83 const char* vertexCoords) SK_OVERRIDE;
84 virtual void emitFS(GrGLShaderBuilder* state,
85 const char* outputColor,
86 const char* inputColor,
87 const char* samplerName) SK_OVERRIDE;
88 virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE;
89 virtual void setData(const GrGLInterface*,
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.
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000145 fVSParamVar = &state->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
146 kFloat_GrSLType, "uRadial2VSParams", stage, 6);
147 fFSParamVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
148 kFloat_GrSLType, "uRadial2FSParams", stage, 6);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000149
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000150 fVSParamLocation = GrGLProgramStage::kUseUniform;
151 fFSParamLocation = GrGLProgramStage::kUseUniform;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000152
153 // For radial gradients without perspective we can pass the linear
154 // part of the quadratic as a varying.
155 if (state->fVaryingDims == state->fCoordDims) {
156 state->addVarying(kFloat_GrSLType, "Radial2BCoeff", stage,
157 &fVSVaryingName, &fFSVaryingName);
158 }
159}
160
161void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* state,
rileya@google.com3e332582012-07-03 13:43:35 +0000162 const char* vertexCoords) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000163 SkString* code = &state->fVSCode;
164 SkString p2;
165 SkString p3;
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000166 fVSParamVar->appendArrayAccess(2, &p2);
167 fVSParamVar->appendArrayAccess(3, &p3);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000168
169 // For radial gradients without perspective we can pass the linear
170 // part of the quadratic as a varying.
171 if (state->fVaryingDims == state->fCoordDims) {
172 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
173 code->appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
174 fVSVaryingName, p2.c_str(),
175 vertexCoords, p3.c_str());
176 }
177}
178
179void GrGLRadial2Gradient::emitFS(GrGLShaderBuilder* state,
rileya@google.com3e332582012-07-03 13:43:35 +0000180 const char* outputColor,
181 const char* inputColor,
182 const char* samplerName) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000183 SkString* code = &state->fFSCode;
184 SkString cName("c");
185 SkString ac4Name("ac4");
186 SkString rootName("root");
187 SkString p0;
188 SkString p1;
189 SkString p2;
190 SkString p3;
191 SkString p4;
192 SkString p5;
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000193 fFSParamVar->appendArrayAccess(0, &p0);
194 fFSParamVar->appendArrayAccess(1, &p1);
195 fFSParamVar->appendArrayAccess(2, &p2);
196 fFSParamVar->appendArrayAccess(3, &p3);
197 fFSParamVar->appendArrayAccess(4, &p4);
198 fFSParamVar->appendArrayAccess(5, &p5);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000199
200 // If we we're able to interpolate the linear component,
201 // bVar is the varying; otherwise compute it
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000202 SkString bVar;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000203 if (state->fCoordDims == state->fVaryingDims) {
204 bVar = fFSVaryingName;
205 GrAssert(2 == state->fVaryingDims);
206 } else {
207 GrAssert(3 == state->fVaryingDims);
208 bVar = "b";
209 //bVar.appendS32(stageNum);
210 code->appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
211 bVar.c_str(), p2.c_str(),
212 state->fSampleCoords.c_str(), p3.c_str());
213 }
214
215 // c = (x^2)+(y^2) - params[4]
216 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n",
217 cName.c_str(), state->fSampleCoords.c_str(),
218 state->fSampleCoords.c_str(),
219 p4.c_str());
220
221 // If we aren't degenerate, emit some extra code, and accept a slightly
222 // more complex coord.
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000223 if (!fIsDegenerate) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000224
225 // ac4 = 4.0 * params[0] * c
226 code->appendf("\tfloat %s = %s * 4.0 * %s;\n",
227 ac4Name.c_str(), p0.c_str(),
228 cName.c_str());
229
230 // root = sqrt(b^2-4ac)
231 // (abs to avoid exception due to fp precision)
232 code->appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
233 rootName.c_str(), bVar.c_str(), bVar.c_str(),
234 ac4Name.c_str());
235
236 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
237 // y coord is 0.5 (texture is effectively 1D)
238 state->fSampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)",
239 bVar.c_str(), p5.c_str(),
240 rootName.c_str(), p1.c_str());
241 } else {
242 // x coord is: -c/b
243 // y coord is 0.5 (texture is effectively 1D)
244 state->fSampleCoords.printf("vec2((-%s / %s), 0.5)",
245 cName.c_str(), bVar.c_str());
246 }
247 state->fComplexCoord = true;
248
249 state->emitDefaultFetch(outputColor, samplerName);
250}
251
252void GrGLRadial2Gradient::initUniforms(const GrGLInterface* gl, int programID) {
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000253 GR_GL_CALL_RET(gl, fVSParamLocation,
254 GetUniformLocation(programID, fVSParamVar->getName().c_str()));
255 GR_GL_CALL_RET(gl, fFSParamLocation,
256 GetUniformLocation(programID, fFSParamVar->getName().c_str()));
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000257}
258
259void GrGLRadial2Gradient::setData(const GrGLInterface* gl,
rileya@google.com3e332582012-07-03 13:43:35 +0000260 const GrCustomStage& baseData,
261 int stageNum) {
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000262 const GrRadial2Gradient& data =
263 static_cast<const GrRadial2Gradient&>(baseData);
264 GrAssert(data.isDegenerate() == fIsDegenerate);
265 GrScalar centerX1 = data.center();
266 GrScalar radius0 = data.radius();
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000267 if (fCachedCenter != centerX1 ||
268 fCachedRadius != radius0 ||
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000269 fCachedPosRoot != data.isPosRoot()) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000270
271 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
272
273 // When we're in the degenerate (linear) case, the second
274 // value will be INF but the program doesn't read it. (We
275 // use the same 6 uniforms even though we don't need them
276 // all in the linear case just to keep the code complexity
277 // down).
278 float values[6] = {
279 GrScalarToFloat(a),
280 1 / (2.f * GrScalarToFloat(a)),
281 GrScalarToFloat(centerX1),
282 GrScalarToFloat(radius0),
283 GrScalarToFloat(GrMul(radius0, radius0)),
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000284 data.isPosRoot() ? 1.f : -1.f
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000285 };
286
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000287 GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
288 GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000289 fCachedCenter = centerX1;
290 fCachedRadius = radius0;
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000291 fCachedPosRoot = data.isPosRoot();
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000292 }
293}
294
295
296/////////////////////////////////////////////////////////////////////
297
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000298GrRadial2Gradient::GrRadial2Gradient(GrTexture* texture,
299 GrScalar center,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000300 GrScalar radius,
301 bool posRoot)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000302 : GrSingleTextureEffect(texture)
303 , fCenterX1 (center)
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000304 , fRadius0 (radius)
305 , fPosRoot (posRoot) {
306
307}
308
309GrRadial2Gradient::~GrRadial2Gradient() {
310
311}
312
313
314const GrProgramStageFactory& GrRadial2Gradient::getFactory() const {
315 return GrTProgramStageFactory<GrRadial2Gradient>::getInstance();
316}
317
318bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const {
319 const GrRadial2Gradient& s = static_cast<const GrRadial2Gradient&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000320 return (INHERITED::isEqual(sBase) &&
321 this->fCenterX1 == s.fCenterX1 &&
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000322 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*,
rileya@google.com3e332582012-07-03 13:43:35 +0000346 const GrCustomStage&,
347 int stageNum) SK_OVERRIDE;
348
349 static StageKey GenKey(const GrCustomStage& s) {
350 return (static_cast<const GrConical2Gradient&>(s).isDegenerate());
351 }
352
353protected:
354
355 const GrGLShaderVar* fVSParamVar;
356 GrGLint fVSParamLocation;
357 const GrGLShaderVar* fFSParamVar;
358 GrGLint fFSParamLocation;
359
360 const char* fVSVaryingName;
361 const char* fFSVaryingName;
362
363 bool fIsDegenerate;
364
365 // @{
366 /// Values last uploaded as uniforms
367
368 GrScalar fCachedCenter;
369 GrScalar fCachedRadius;
370 GrScalar fCachedDiffRadius;
371
372 // @}
373
374private:
375
376 typedef GrGLProgramStage INHERITED;
377
378};
379
380GrGLConical2Gradient::GrGLConical2Gradient(
381 const GrProgramStageFactory& factory,
382 const GrCustomStage& baseData)
383 : INHERITED(factory)
384 , fVSParamVar(NULL)
385 , fFSParamVar(NULL)
386 , fVSVaryingName(NULL)
387 , fFSVaryingName(NULL)
388 , fCachedCenter(GR_ScalarMax)
389 , fCachedRadius(-GR_ScalarMax)
390 , fCachedDiffRadius(-GR_ScalarMax) {
391
392 const GrConical2Gradient& data =
393 static_cast<const GrConical2Gradient&>(baseData);
394 fIsDegenerate = data.isDegenerate();
395}
396
397void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) {
398 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
399 // to work around Xoom bug. Doesn't seem to cause performance decrease
400 // in test apps, but need to keep an eye on it.
bsalomon@google.comeb715c82012-07-11 15:03:31 +0000401 fVSParamVar = &state->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
402 kFloat_GrSLType, "uConical2VSParams", stage, 6);
403 fFSParamVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
404 kFloat_GrSLType, "uConical2FSParams", stage, 6);
rileya@google.com3e332582012-07-03 13:43:35 +0000405
406 fVSParamLocation = GrGLProgramStage::kUseUniform;
407 fFSParamLocation = GrGLProgramStage::kUseUniform;
408
409 // For radial gradients without perspective we can pass the linear
410 // part of the quadratic as a varying.
411 if (state->fVaryingDims == state->fCoordDims) {
412 state->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage,
413 &fVSVaryingName, &fFSVaryingName);
414 }
415}
416
417void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* state,
418 const char* vertexCoords) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000419 SkString* code = &state->fVSCode;
420 SkString p2; // distance between centers
421 SkString p3; // start radius
422 SkString p5; // difference in radii (r1 - r0)
rileya@google.com3e332582012-07-03 13:43:35 +0000423 fVSParamVar->appendArrayAccess(2, &p2);
424 fVSParamVar->appendArrayAccess(3, &p3);
425 fVSParamVar->appendArrayAccess(5, &p5);
426
427 // For radial gradients without perspective we can pass the linear
428 // part of the quadratic as a varying.
429 if (state->fVaryingDims == state->fCoordDims) {
430 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
431 code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
432 fVSVaryingName, p2.c_str(),
433 vertexCoords, p3.c_str(), p5.c_str());
434 }
435}
436
437void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* state,
438 const char* outputColor,
439 const char* inputColor,
440 const char* samplerName) {
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000441 SkString* code = &state->fFSCode;
rileya@google.com3e332582012-07-03 13:43:35 +0000442
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000443 SkString cName("c");
444 SkString ac4Name("ac4");
445 SkString dName("d");
446 SkString qName("q");
447 SkString r0Name("r0");
448 SkString r1Name("r1");
449 SkString tName("t");
450 SkString p0; // 4a
rileya@google.com62197282012-07-10 20:05:23 +0000451 SkString p1; // 1/a
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000452 SkString p2; // distance between centers
453 SkString p3; // start radius
454 SkString p4; // start radius squared
455 SkString p5; // difference in radii (r1 - r0)
rileya@google.com3e332582012-07-03 13:43:35 +0000456 fFSParamVar->appendArrayAccess(0, &p0);
457 fFSParamVar->appendArrayAccess(1, &p1);
458 fFSParamVar->appendArrayAccess(2, &p2);
459 fFSParamVar->appendArrayAccess(3, &p3);
460 fFSParamVar->appendArrayAccess(4, &p4);
461 fFSParamVar->appendArrayAccess(5, &p5);
462
463 // If we we're able to interpolate the linear component,
464 // bVar is the varying; otherwise compute it
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000465 SkString bVar;
rileya@google.com3e332582012-07-03 13:43:35 +0000466 if (state->fCoordDims == state->fVaryingDims) {
467 bVar = fFSVaryingName;
468 GrAssert(2 == state->fVaryingDims);
469 } else {
470 GrAssert(3 == state->fVaryingDims);
471 bVar = "b";
472 code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
473 bVar.c_str(), p2.c_str(), state->fSampleCoords.c_str(),
474 p3.c_str(), p5.c_str());
475 }
476
rileya@google.come38160c2012-07-03 18:03:04 +0000477 // output will default to transparent black (we simply won't write anything
478 // else to it if invalid, instead of discarding or returning prematurely)
479 code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
480
rileya@google.com3e332582012-07-03 13:43:35 +0000481 // c = (x^2)+(y^2) - params[4]
482 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
483 state->fSampleCoords.c_str(), state->fSampleCoords.c_str(),
484 p4.c_str());
485
486 // Non-degenerate case (quadratic)
487 if (!fIsDegenerate) {
488
489 // ac4 = params[0] * c
490 code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
491 cName.c_str());
492
493 // d = b^2 - ac4
494 code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
495 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
496
rileya@google.come38160c2012-07-03 18:03:04 +0000497 // only proceed if discriminant is >= 0
498 code->appendf("\tif (%s >= 0.0) {\n", dName.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000499
500 // intermediate value we'll use to compute the roots
501 // q = -0.5 * (b +/- sqrt(d))
rileya@google.come38160c2012-07-03 18:03:04 +0000502 code->appendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
rileya@google.com3e332582012-07-03 13:43:35 +0000503 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
504 bVar.c_str(), dName.c_str());
505
506 // compute both roots
507 // r0 = q * params[1]
rileya@google.come38160c2012-07-03 18:03:04 +0000508 code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
509 qName.c_str(), p1.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000510 // r1 = c / q
rileya@google.come38160c2012-07-03 18:03:04 +0000511 code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
512 cName.c_str(), qName.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000513
514 // Note: If there are two roots that both generate radius(t) > 0, the
515 // Canvas spec says to choose the larger t.
516
517 // so we'll look at the larger one first:
rileya@google.come38160c2012-07-03 18:03:04 +0000518 code->appendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000519 r0Name.c_str(), r1Name.c_str());
520
rileya@google.come38160c2012-07-03 18:03:04 +0000521 // if r(t) > 0, then we're done; t will be our x coordinate
522 code->appendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000523 p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000524
525 // y coord is 0.5 (texture is effectively 1D)
526 code->appendf("\t\t");
527 state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
528 state->emitDefaultFetch(outputColor, samplerName);
529
530 // otherwise, if r(t) for the larger root was <= 0, try the other root
531 code->appendf("\t\t} else {\n");
532 code->appendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000533 r0Name.c_str(), r1Name.c_str());
534
rileya@google.come38160c2012-07-03 18:03:04 +0000535 // if r(t) > 0 for the smaller root, then t will be our x coordinate
536 code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
rileya@google.com3e332582012-07-03 13:43:35 +0000537 tName.c_str(), p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000538
539 // y coord is 0.5 (texture is effectively 1D)
540 code->appendf("\t\t\t");
541 state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
542 state->emitDefaultFetch(outputColor, samplerName);
543
544 // end if (r(t) > 0) for smaller root
545 code->appendf("\t\t\t}\n");
546 // end if (r(t) > 0), else, for larger root
547 code->appendf("\t\t}\n");
548 // end if (discriminant >= 0)
rileya@google.com3e332582012-07-03 13:43:35 +0000549 code->appendf("\t}\n");
550 } else {
551
552 // linear case: t = -c/b
553 code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
554 cName.c_str(), bVar.c_str());
555
rileya@google.come38160c2012-07-03 18:03:04 +0000556 // if r(t) > 0, then t will be the x coordinate
557 code->appendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000558 p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000559 code->appendf("\t");
560 state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
561 state->emitDefaultFetch(outputColor, samplerName);
562 code->appendf("\t}\n");
rileya@google.com3e332582012-07-03 13:43:35 +0000563 }
564 state->fComplexCoord = true;
rileya@google.com3e332582012-07-03 13:43:35 +0000565}
566
567void GrGLConical2Gradient::initUniforms(const GrGLInterface* gl, int programID) {
568 GR_GL_CALL_RET(gl, fVSParamLocation,
569 GetUniformLocation(programID, fVSParamVar->getName().c_str()));
570 GR_GL_CALL_RET(gl, fFSParamLocation,
571 GetUniformLocation(programID, fFSParamVar->getName().c_str()));
572}
573
574void GrGLConical2Gradient::setData(const GrGLInterface* gl,
rileya@google.com3e332582012-07-03 13:43:35 +0000575 const GrCustomStage& baseData,
576 int stageNum) {
577 const GrConical2Gradient& data =
578 static_cast<const GrConical2Gradient&>(baseData);
579 GrAssert(data.isDegenerate() == fIsDegenerate);
580 GrScalar centerX1 = data.center();
581 GrScalar radius0 = data.radius();
582 GrScalar diffRadius = data.diffRadius();
583
584 if (fCachedCenter != centerX1 ||
585 fCachedRadius != radius0 ||
586 fCachedDiffRadius != diffRadius) {
587
588 GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius;
589
590 // When we're in the degenerate (linear) case, the second
591 // value will be INF but the program doesn't read it. (We
592 // use the same 6 uniforms even though we don't need them
593 // all in the linear case just to keep the code complexity
594 // down).
595 float values[6] = {
596 GrScalarToFloat(a * 4),
597 1.f / (GrScalarToFloat(a)),
598 GrScalarToFloat(centerX1),
599 GrScalarToFloat(radius0),
600 GrScalarToFloat(SkScalarMul(radius0, radius0)),
601 GrScalarToFloat(diffRadius)
602 };
603
604 GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
605 GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
606 fCachedCenter = centerX1;
607 fCachedRadius = radius0;
608 fCachedDiffRadius = diffRadius;
609 }
610}
611
612
613/////////////////////////////////////////////////////////////////////
614
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000615GrConical2Gradient::GrConical2Gradient(GrTexture* texture,
616 GrScalar center,
rileya@google.com3e332582012-07-03 13:43:35 +0000617 GrScalar radius,
618 GrScalar diffRadius)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000619 : GrSingleTextureEffect (texture)
620 , fCenterX1 (center)
rileya@google.com3e332582012-07-03 13:43:35 +0000621 , fRadius0 (radius)
622 , fDiffRadius (diffRadius) {
623
624}
625
626GrConical2Gradient::~GrConical2Gradient() {
627
628}
629
630
631const GrProgramStageFactory& GrConical2Gradient::getFactory() const {
632 return GrTProgramStageFactory<GrConical2Gradient>::getInstance();
633}
634
635bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const {
636 const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000637 return (INHERITED::isEqual(sBase) &&
638 this->fCenterX1 == s.fCenterX1 &&
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000639 this->fRadius0 == s.fRadius0 &&
640 this->fDiffRadius == s.fDiffRadius);
rileya@google.com3e332582012-07-03 13:43:35 +0000641}
642
643/////////////////////////////////////////////////////////////////////
644
645
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000646class GrGLSweepGradient : public GrGLProgramStage {
647
648public:
649
650 GrGLSweepGradient(const GrProgramStageFactory& factory,
651 const GrCustomStage&) : INHERITED (factory) { }
652 virtual ~GrGLSweepGradient() { }
653
654 virtual void emitVS(GrGLShaderBuilder* state,
655 const char* vertexCoords) SK_OVERRIDE { }
656 virtual void emitFS(GrGLShaderBuilder* state,
657 const char* outputColor,
658 const char* inputColor,
659 const char* samplerName) SK_OVERRIDE;
660
661 static StageKey GenKey(const GrCustomStage& s) { return 0; }
662
663private:
664
665 typedef GrGLProgramStage INHERITED;
666
667};
668
669void GrGLSweepGradient::emitFS(GrGLShaderBuilder* state,
670 const char* outputColor,
671 const char* inputColor,
672 const char* samplerName) {
673 state->fSampleCoords.printf(
674 "vec2(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5, 0.5)",
675 state->fSampleCoords.c_str(), state->fSampleCoords.c_str());
676 state->fComplexCoord = true;
677
678 state->emitDefaultFetch(outputColor, samplerName);
679}
680
681/////////////////////////////////////////////////////////////////////
682
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000683GrSweepGradient::GrSweepGradient(GrTexture* texture)
684 : GrSingleTextureEffect(texture) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000685
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 {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000697 return INHERITED::isEqual(sBase);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000698}
699