blob: ec465b815bc0a782509fd9d05dc7716262f8b0c1 [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
rileya@google.com22e57f92012-07-19 15:16:19 +000012// Base class for GL gradient custom stages
13class GrGLGradientStage : public GrGLProgramStage {
14public:
15
16 GrGLGradientStage(const GrProgramStageFactory& factory);
17 virtual ~GrGLGradientStage();
18
19 // emit code that gets a fragment's color from an expression for t; for now
20 // this always uses the texture, but for simpler cases we'll be able to lerp
21 void emitColorLookup(GrGLShaderBuilder* builder, const char* t,
22 const char* outputColor, const char* samplerName);
23
24private:
25
26 typedef GrGLProgramStage INHERITED;
27};
28
29GrGLGradientStage::GrGLGradientStage(const GrProgramStageFactory& factory)
30 : INHERITED(factory) { }
31
32GrGLGradientStage::~GrGLGradientStage() { }
33
34void GrGLGradientStage::emitColorLookup(GrGLShaderBuilder* builder,
35 const char* tName,
36 const char* outputColor,
37 const char* samplerName) {
38 // Texture is effectively 1D so the y coordinate is 0.5, if we pack multiple
39 // gradients into a texture, we could instead pick the appropriate row here
40 builder->fSampleCoords.printf("vec2(%s, 0.5)", tName);
41 builder->fComplexCoord = true;
42 builder->emitDefaultFetch(outputColor, samplerName);
43}
44
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000045/////////////////////////////////////////////////////////////////////
46
rileya@google.com22e57f92012-07-19 15:16:19 +000047GrGradientEffect::GrGradientEffect(GrTexture* texture)
48 : fTexture (texture)
49 , fUseTexture(true) {
50 SkSafeRef(fTexture);
51}
52
53GrGradientEffect::~GrGradientEffect() {
54 if (fTexture) {
55 SkSafeUnref(fTexture);
56 }
57}
58
59unsigned int GrGradientEffect::numTextures() const {
60 return fUseTexture ? 1 : 0;
61}
62
63GrTexture* GrGradientEffect::texture(unsigned int index)
64 const {
65 GrAssert(fUseTexture && 0 == index);
66 return fTexture;
67}
68
69/////////////////////////////////////////////////////////////////////
70
71class GrGLLinearGradient : public GrGLGradientStage {
72public:
73
74 GrGLLinearGradient(const GrProgramStageFactory& factory,
75 const GrCustomStage&)
76 : INHERITED (factory) { }
77
78 virtual ~GrGLLinearGradient() { }
79
80 virtual void emitVS(GrGLShaderBuilder* builder,
81 const char* vertexCoords) SK_OVERRIDE { }
82 virtual void emitFS(GrGLShaderBuilder* builder,
83 const char* outputColor,
84 const char* inputColor,
85 const char* samplerName) SK_OVERRIDE;
86 static StageKey GenKey(const GrCustomStage& s) { return 0; }
87
88private:
89
90 typedef GrGLGradientStage INHERITED;
91};
92
93void GrGLLinearGradient::emitFS(GrGLShaderBuilder* builder,
94 const char* outputColor,
95 const char* inputColor,
96 const char* samplerName) {
97 SkString t;
98 t.printf("%s.x", builder->fSampleCoords.c_str());
99 this->emitColorLookup(builder, t.c_str(), outputColor, samplerName);
100}
101
102/////////////////////////////////////////////////////////////////////
103
104GrLinearGradient::GrLinearGradient(GrTexture* texture)
105 : INHERITED(texture) {
106}
107
108GrLinearGradient::~GrLinearGradient() {
109
110}
111
112const GrProgramStageFactory& GrLinearGradient::getFactory() const {
113 return GrTProgramStageFactory<GrLinearGradient>::getInstance();
114}
115
116bool GrLinearGradient::isEqual(const GrCustomStage& sBase) const {
117 return INHERITED::isEqual(sBase);
118}
119
120/////////////////////////////////////////////////////////////////////
121
122class GrGLRadialGradient : public GrGLGradientStage {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000123
124public:
125
126 GrGLRadialGradient(const GrProgramStageFactory& factory,
127 const GrCustomStage&) : INHERITED (factory) { }
128 virtual ~GrGLRadialGradient() { }
129
bsalomon@google.com032b2212012-07-16 13:36:18 +0000130 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000131 const char* vertexCoords) SK_OVERRIDE { }
bsalomon@google.com032b2212012-07-16 13:36:18 +0000132 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000133 const char* outputColor,
134 const char* inputColor,
135 const char* samplerName) SK_OVERRIDE;
136
137 static StageKey GenKey(const GrCustomStage& s) { return 0; }
138
139private:
140
rileya@google.com22e57f92012-07-19 15:16:19 +0000141 typedef GrGLGradientStage INHERITED;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000142
143};
144
bsalomon@google.com032b2212012-07-16 13:36:18 +0000145void GrGLRadialGradient::emitFS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000146 const char* outputColor,
147 const char* inputColor,
148 const char* samplerName) {
rileya@google.com22e57f92012-07-19 15:16:19 +0000149 SkString t;
150 t.printf("length(%s.xy)", builder->fSampleCoords.c_str());
151 this->emitColorLookup(builder, t.c_str(), outputColor, samplerName);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000152}
153
154
155/////////////////////////////////////////////////////////////////////
156
157
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000158GrRadialGradient::GrRadialGradient(GrTexture* texture)
rileya@google.com22e57f92012-07-19 15:16:19 +0000159 : INHERITED(texture) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000160
161}
162
163GrRadialGradient::~GrRadialGradient() {
164
165}
166
167
168const GrProgramStageFactory& GrRadialGradient::getFactory() const {
169 return GrTProgramStageFactory<GrRadialGradient>::getInstance();
170}
171
172bool GrRadialGradient::isEqual(const GrCustomStage& sBase) const {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000173 return INHERITED::isEqual(sBase);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000174}
175
176/////////////////////////////////////////////////////////////////////
177
bsalomon@google.com032b2212012-07-16 13:36:18 +0000178// For brevity, and these definitions are likely to move to a different class soon.
179typedef GrGLShaderBuilder::UniformHandle UniformHandle;
180static const UniformHandle kInvalidUniformHandle = GrGLShaderBuilder::kInvalidUniformHandle;
181
rileya@google.com22e57f92012-07-19 15:16:19 +0000182class GrGLRadial2Gradient : public GrGLGradientStage {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000183
184public:
185
186 GrGLRadial2Gradient(const GrProgramStageFactory& factory,
187 const GrCustomStage&);
188 virtual ~GrGLRadial2Gradient() { }
189
bsalomon@google.com032b2212012-07-16 13:36:18 +0000190 virtual void setupVariables(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000191 int stage) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000192 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000193 const char* vertexCoords) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000194 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000195 const char* outputColor,
196 const char* inputColor,
197 const char* samplerName) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000198 virtual void initUniforms(const GrGLShaderBuilder* builder,
199 const GrGLInterface*,
200 int programID) SK_OVERRIDE;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000201 virtual void setData(const GrGLInterface*,
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000202 const GrCustomStage&,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000203 const GrRenderTarget*,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000204 int stageNum) SK_OVERRIDE;
205
206 static StageKey GenKey(const GrCustomStage& s) {
207 return (static_cast<const GrRadial2Gradient&>(s).isDegenerate());
208 }
209
210protected:
211
bsalomon@google.com032b2212012-07-16 13:36:18 +0000212 UniformHandle fVSParamUni;
213 GrGLint fVSParamLocation;
214 UniformHandle fFSParamUni;
215 GrGLint fFSParamLocation;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000216
217 const char* fVSVaryingName;
218 const char* fFSVaryingName;
219
220 bool fIsDegenerate;
221
222 // @{
223 /// Values last uploaded as uniforms
224
225 GrScalar fCachedCenter;
226 GrScalar fCachedRadius;
bsalomon@google.com0b323312012-06-01 18:50:01 +0000227 bool fCachedPosRoot;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000228
229 // @}
230
231private:
232
rileya@google.com22e57f92012-07-19 15:16:19 +0000233 typedef GrGLGradientStage INHERITED;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000234
235};
236
237GrGLRadial2Gradient::GrGLRadial2Gradient(
238 const GrProgramStageFactory& factory,
239 const GrCustomStage& baseData)
240 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000241 , fVSParamUni(kInvalidUniformHandle)
242 , fFSParamUni(kInvalidUniformHandle)
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000243 , fVSVaryingName(NULL)
244 , fFSVaryingName(NULL)
245 , fCachedCenter(GR_ScalarMax)
246 , fCachedRadius(-GR_ScalarMax)
247 , fCachedPosRoot(0) {
248
249 const GrRadial2Gradient& data =
250 static_cast<const GrRadial2Gradient&>(baseData);
251 fIsDegenerate = data.isDegenerate();
252}
253
bsalomon@google.com032b2212012-07-16 13:36:18 +0000254void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* builder, int stage) {
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000255 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
256 // to work around Xoom bug. Doesn't seem to cause performance decrease
257 // in test apps, but need to keep an eye on it.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000258 fVSParamUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
259 kFloat_GrSLType, "uRadial2VSParams", stage, 6);
260 fFSParamUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
261 kFloat_GrSLType, "uRadial2FSParams", stage, 6);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000262
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000263 fVSParamLocation = GrGLProgramStage::kUseUniform;
264 fFSParamLocation = GrGLProgramStage::kUseUniform;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000265
266 // For radial gradients without perspective we can pass the linear
267 // part of the quadratic as a varying.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000268 if (builder->fVaryingDims == builder->fCoordDims) {
269 builder->addVarying(kFloat_GrSLType, "Radial2BCoeff", stage,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000270 &fVSVaryingName, &fFSVaryingName);
271 }
272}
273
bsalomon@google.com032b2212012-07-16 13:36:18 +0000274void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000275 const char* vertexCoords) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000276 SkString* code = &builder->fVSCode;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000277 SkString p2;
278 SkString p3;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000279 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
280 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000281
282 // For radial gradients without perspective we can pass the linear
283 // part of the quadratic as a varying.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000284 if (builder->fVaryingDims == builder->fCoordDims) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000285 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
286 code->appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
287 fVSVaryingName, p2.c_str(),
288 vertexCoords, p3.c_str());
289 }
290}
291
bsalomon@google.com032b2212012-07-16 13:36:18 +0000292void GrGLRadial2Gradient::emitFS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000293 const char* outputColor,
294 const char* inputColor,
295 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000296 SkString* code = &builder->fFSCode;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000297 SkString cName("c");
298 SkString ac4Name("ac4");
299 SkString rootName("root");
rileya@google.com22e57f92012-07-19 15:16:19 +0000300 SkString t;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000301 SkString p0;
302 SkString p1;
303 SkString p2;
304 SkString p3;
305 SkString p4;
306 SkString p5;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000307 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
308 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
309 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
310 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
311 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
312 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000313
314 // If we we're able to interpolate the linear component,
315 // bVar is the varying; otherwise compute it
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000316 SkString bVar;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000317 if (builder->fCoordDims == builder->fVaryingDims) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000318 bVar = fFSVaryingName;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000319 GrAssert(2 == builder->fVaryingDims);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000320 } else {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000321 GrAssert(3 == builder->fVaryingDims);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000322 bVar = "b";
323 //bVar.appendS32(stageNum);
324 code->appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
325 bVar.c_str(), p2.c_str(),
bsalomon@google.com032b2212012-07-16 13:36:18 +0000326 builder->fSampleCoords.c_str(), p3.c_str());
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000327 }
328
329 // c = (x^2)+(y^2) - params[4]
330 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n",
bsalomon@google.com032b2212012-07-16 13:36:18 +0000331 cName.c_str(), builder->fSampleCoords.c_str(),
332 builder->fSampleCoords.c_str(),
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000333 p4.c_str());
334
335 // If we aren't degenerate, emit some extra code, and accept a slightly
336 // more complex coord.
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000337 if (!fIsDegenerate) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000338
339 // ac4 = 4.0 * params[0] * c
340 code->appendf("\tfloat %s = %s * 4.0 * %s;\n",
341 ac4Name.c_str(), p0.c_str(),
342 cName.c_str());
343
344 // root = sqrt(b^2-4ac)
345 // (abs to avoid exception due to fp precision)
346 code->appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
347 rootName.c_str(), bVar.c_str(), bVar.c_str(),
348 ac4Name.c_str());
349
rileya@google.com22e57f92012-07-19 15:16:19 +0000350 // t is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
351 t.printf("(-%s + %s * %s) * %s", bVar.c_str(), p5.c_str(),
352 rootName.c_str(), p1.c_str());
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000353 } else {
rileya@google.com22e57f92012-07-19 15:16:19 +0000354 // t is: -c/b
355 t.printf("-%s / %s", cName.c_str(), bVar.c_str());
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000356 }
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000357
rileya@google.com22e57f92012-07-19 15:16:19 +0000358 this->emitColorLookup(builder, t.c_str(), outputColor, samplerName);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000359}
360
bsalomon@google.com032b2212012-07-16 13:36:18 +0000361void GrGLRadial2Gradient::initUniforms(const GrGLShaderBuilder* builder,
362 const GrGLInterface* gl,
363 int programID) {
364 const char* vsParam = builder->getUniformCStr(fVSParamUni);
365 const char* fsParam = builder->getUniformCStr(fFSParamUni);
366 GR_GL_CALL_RET(gl, fVSParamLocation, GetUniformLocation(programID, vsParam));
367 GR_GL_CALL_RET(gl, fFSParamLocation, GetUniformLocation(programID, fsParam));
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000368}
369
370void GrGLRadial2Gradient::setData(const GrGLInterface* gl,
rileya@google.com3e332582012-07-03 13:43:35 +0000371 const GrCustomStage& baseData,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000372 const GrRenderTarget*,
rileya@google.com3e332582012-07-03 13:43:35 +0000373 int stageNum) {
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000374 const GrRadial2Gradient& data =
375 static_cast<const GrRadial2Gradient&>(baseData);
376 GrAssert(data.isDegenerate() == fIsDegenerate);
377 GrScalar centerX1 = data.center();
378 GrScalar radius0 = data.radius();
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000379 if (fCachedCenter != centerX1 ||
380 fCachedRadius != radius0 ||
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000381 fCachedPosRoot != data.isPosRoot()) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000382
383 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
384
385 // When we're in the degenerate (linear) case, the second
386 // value will be INF but the program doesn't read it. (We
387 // use the same 6 uniforms even though we don't need them
388 // all in the linear case just to keep the code complexity
389 // down).
390 float values[6] = {
391 GrScalarToFloat(a),
392 1 / (2.f * GrScalarToFloat(a)),
393 GrScalarToFloat(centerX1),
394 GrScalarToFloat(radius0),
395 GrScalarToFloat(GrMul(radius0, radius0)),
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000396 data.isPosRoot() ? 1.f : -1.f
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000397 };
398
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000399 GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
400 GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000401 fCachedCenter = centerX1;
402 fCachedRadius = radius0;
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000403 fCachedPosRoot = data.isPosRoot();
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000404 }
405}
406
407
408/////////////////////////////////////////////////////////////////////
409
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000410GrRadial2Gradient::GrRadial2Gradient(GrTexture* texture,
411 GrScalar center,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000412 GrScalar radius,
413 bool posRoot)
rileya@google.com22e57f92012-07-19 15:16:19 +0000414 : INHERITED(texture)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000415 , fCenterX1 (center)
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000416 , fRadius0 (radius)
417 , fPosRoot (posRoot) {
418
419}
420
421GrRadial2Gradient::~GrRadial2Gradient() {
422
423}
424
425
426const GrProgramStageFactory& GrRadial2Gradient::getFactory() const {
427 return GrTProgramStageFactory<GrRadial2Gradient>::getInstance();
428}
429
430bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const {
431 const GrRadial2Gradient& s = static_cast<const GrRadial2Gradient&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000432 return (INHERITED::isEqual(sBase) &&
433 this->fCenterX1 == s.fCenterX1 &&
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000434 this->fRadius0 == s.fRadius0 &&
435 this->fPosRoot == s.fPosRoot);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000436}
437
438/////////////////////////////////////////////////////////////////////
439
rileya@google.com22e57f92012-07-19 15:16:19 +0000440class GrGLConical2Gradient : public GrGLGradientStage {
rileya@google.com3e332582012-07-03 13:43:35 +0000441
442public:
443
444 GrGLConical2Gradient(const GrProgramStageFactory& factory,
445 const GrCustomStage&);
446 virtual ~GrGLConical2Gradient() { }
447
bsalomon@google.com032b2212012-07-16 13:36:18 +0000448 virtual void setupVariables(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000449 int stage) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000450 virtual void emitVS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000451 const char* vertexCoords) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000452 virtual void emitFS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000453 const char* outputColor,
454 const char* inputColor,
455 const char* samplerName) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000456 virtual void initUniforms(const GrGLShaderBuilder* builder,
457 const GrGLInterface*,
458 int programID) SK_OVERRIDE;
rileya@google.com3e332582012-07-03 13:43:35 +0000459 virtual void setData(const GrGLInterface*,
rileya@google.com3e332582012-07-03 13:43:35 +0000460 const GrCustomStage&,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000461 const GrRenderTarget*,
rileya@google.com3e332582012-07-03 13:43:35 +0000462 int stageNum) SK_OVERRIDE;
463
464 static StageKey GenKey(const GrCustomStage& s) {
465 return (static_cast<const GrConical2Gradient&>(s).isDegenerate());
466 }
467
468protected:
469
bsalomon@google.com032b2212012-07-16 13:36:18 +0000470 UniformHandle fVSParamUni;
471 GrGLint fVSParamLocation;
472 UniformHandle fFSParamUni;
473 GrGLint fFSParamLocation;
rileya@google.com3e332582012-07-03 13:43:35 +0000474
475 const char* fVSVaryingName;
476 const char* fFSVaryingName;
477
478 bool fIsDegenerate;
479
480 // @{
481 /// Values last uploaded as uniforms
482
483 GrScalar fCachedCenter;
484 GrScalar fCachedRadius;
485 GrScalar fCachedDiffRadius;
486
487 // @}
488
489private:
490
rileya@google.com22e57f92012-07-19 15:16:19 +0000491 typedef GrGLGradientStage INHERITED;
rileya@google.com3e332582012-07-03 13:43:35 +0000492
493};
494
495GrGLConical2Gradient::GrGLConical2Gradient(
496 const GrProgramStageFactory& factory,
497 const GrCustomStage& baseData)
498 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000499 , fVSParamUni(kInvalidUniformHandle)
500 , fFSParamUni(kInvalidUniformHandle)
rileya@google.com3e332582012-07-03 13:43:35 +0000501 , fVSVaryingName(NULL)
502 , fFSVaryingName(NULL)
503 , fCachedCenter(GR_ScalarMax)
504 , fCachedRadius(-GR_ScalarMax)
505 , fCachedDiffRadius(-GR_ScalarMax) {
506
507 const GrConical2Gradient& data =
508 static_cast<const GrConical2Gradient&>(baseData);
509 fIsDegenerate = data.isDegenerate();
510}
511
bsalomon@google.com032b2212012-07-16 13:36:18 +0000512void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* builder, int stage) {
rileya@google.com3e332582012-07-03 13:43:35 +0000513 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
514 // to work around Xoom bug. Doesn't seem to cause performance decrease
515 // in test apps, but need to keep an eye on it.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000516 fVSParamUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
517 kFloat_GrSLType, "uConical2VSParams", stage, 6);
518 fFSParamUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
519 kFloat_GrSLType, "uConical2FSParams", stage, 6);
rileya@google.com3e332582012-07-03 13:43:35 +0000520
521 fVSParamLocation = GrGLProgramStage::kUseUniform;
522 fFSParamLocation = GrGLProgramStage::kUseUniform;
523
524 // For radial gradients without perspective we can pass the linear
525 // part of the quadratic as a varying.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000526 if (builder->fVaryingDims == builder->fCoordDims) {
527 builder->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage,
528 &fVSVaryingName, &fFSVaryingName);
rileya@google.com3e332582012-07-03 13:43:35 +0000529 }
530}
531
bsalomon@google.com032b2212012-07-16 13:36:18 +0000532void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000533 const char* vertexCoords) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000534 SkString* code = &builder->fVSCode;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000535 SkString p2; // distance between centers
536 SkString p3; // start radius
537 SkString p5; // difference in radii (r1 - r0)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000538 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
539 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
540 builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5);
rileya@google.com3e332582012-07-03 13:43:35 +0000541
542 // For radial gradients without perspective we can pass the linear
543 // part of the quadratic as a varying.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000544 if (builder->fVaryingDims == builder->fCoordDims) {
rileya@google.com3e332582012-07-03 13:43:35 +0000545 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
546 code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
547 fVSVaryingName, p2.c_str(),
548 vertexCoords, p3.c_str(), p5.c_str());
549 }
550}
551
bsalomon@google.com032b2212012-07-16 13:36:18 +0000552void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000553 const char* outputColor,
554 const char* inputColor,
555 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000556 SkString* code = &builder->fFSCode;
rileya@google.com3e332582012-07-03 13:43:35 +0000557
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000558 SkString cName("c");
559 SkString ac4Name("ac4");
560 SkString dName("d");
561 SkString qName("q");
562 SkString r0Name("r0");
563 SkString r1Name("r1");
564 SkString tName("t");
565 SkString p0; // 4a
rileya@google.com62197282012-07-10 20:05:23 +0000566 SkString p1; // 1/a
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000567 SkString p2; // distance between centers
568 SkString p3; // start radius
569 SkString p4; // start radius squared
570 SkString p5; // difference in radii (r1 - r0)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000571
bsalomon@google.comec4037f2012-07-16 13:46:39 +0000572 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
573 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
574 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
575 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
576 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
577 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
rileya@google.com3e332582012-07-03 13:43:35 +0000578
579 // If we we're able to interpolate the linear component,
580 // bVar is the varying; otherwise compute it
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000581 SkString bVar;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000582 if (builder->fCoordDims == builder->fVaryingDims) {
rileya@google.com3e332582012-07-03 13:43:35 +0000583 bVar = fFSVaryingName;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000584 GrAssert(2 == builder->fVaryingDims);
rileya@google.com3e332582012-07-03 13:43:35 +0000585 } else {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000586 GrAssert(3 == builder->fVaryingDims);
rileya@google.com3e332582012-07-03 13:43:35 +0000587 bVar = "b";
588 code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
bsalomon@google.com032b2212012-07-16 13:36:18 +0000589 bVar.c_str(), p2.c_str(), builder->fSampleCoords.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000590 p3.c_str(), p5.c_str());
591 }
592
rileya@google.come38160c2012-07-03 18:03:04 +0000593 // output will default to transparent black (we simply won't write anything
594 // else to it if invalid, instead of discarding or returning prematurely)
595 code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
596
rileya@google.com3e332582012-07-03 13:43:35 +0000597 // c = (x^2)+(y^2) - params[4]
598 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
bsalomon@google.com032b2212012-07-16 13:36:18 +0000599 builder->fSampleCoords.c_str(), builder->fSampleCoords.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000600 p4.c_str());
601
602 // Non-degenerate case (quadratic)
603 if (!fIsDegenerate) {
604
605 // ac4 = params[0] * c
606 code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
607 cName.c_str());
608
609 // d = b^2 - ac4
610 code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
611 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
612
rileya@google.come38160c2012-07-03 18:03:04 +0000613 // only proceed if discriminant is >= 0
614 code->appendf("\tif (%s >= 0.0) {\n", dName.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000615
616 // intermediate value we'll use to compute the roots
617 // q = -0.5 * (b +/- sqrt(d))
rileya@google.come38160c2012-07-03 18:03:04 +0000618 code->appendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
rileya@google.com3e332582012-07-03 13:43:35 +0000619 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
620 bVar.c_str(), dName.c_str());
621
622 // compute both roots
623 // r0 = q * params[1]
rileya@google.come38160c2012-07-03 18:03:04 +0000624 code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
625 qName.c_str(), p1.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000626 // r1 = c / q
rileya@google.come38160c2012-07-03 18:03:04 +0000627 code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
628 cName.c_str(), qName.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000629
630 // Note: If there are two roots that both generate radius(t) > 0, the
631 // Canvas spec says to choose the larger t.
632
633 // so we'll look at the larger one first:
rileya@google.come38160c2012-07-03 18:03:04 +0000634 code->appendf("\t\tfloat %s = max(%s, %s);\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000635 r0Name.c_str(), r1Name.c_str());
636
rileya@google.come38160c2012-07-03 18:03:04 +0000637 // if r(t) > 0, then we're done; t will be our x coordinate
638 code->appendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000639 p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000640
rileya@google.come38160c2012-07-03 18:03:04 +0000641 code->appendf("\t\t");
rileya@google.com22e57f92012-07-19 15:16:19 +0000642 this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName);
rileya@google.come38160c2012-07-03 18:03:04 +0000643
644 // otherwise, if r(t) for the larger root was <= 0, try the other root
645 code->appendf("\t\t} else {\n");
646 code->appendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000647 r0Name.c_str(), r1Name.c_str());
648
rileya@google.come38160c2012-07-03 18:03:04 +0000649 // if r(t) > 0 for the smaller root, then t will be our x coordinate
650 code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
rileya@google.com3e332582012-07-03 13:43:35 +0000651 tName.c_str(), p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000652
rileya@google.come38160c2012-07-03 18:03:04 +0000653 code->appendf("\t\t\t");
rileya@google.com22e57f92012-07-19 15:16:19 +0000654 this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName);
rileya@google.come38160c2012-07-03 18:03:04 +0000655
656 // end if (r(t) > 0) for smaller root
657 code->appendf("\t\t\t}\n");
658 // end if (r(t) > 0), else, for larger root
659 code->appendf("\t\t}\n");
660 // end if (discriminant >= 0)
rileya@google.com3e332582012-07-03 13:43:35 +0000661 code->appendf("\t}\n");
662 } else {
663
664 // linear case: t = -c/b
665 code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
666 cName.c_str(), bVar.c_str());
667
rileya@google.come38160c2012-07-03 18:03:04 +0000668 // if r(t) > 0, then t will be the x coordinate
669 code->appendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000670 p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000671 code->appendf("\t");
rileya@google.com22e57f92012-07-19 15:16:19 +0000672 this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName);
rileya@google.come38160c2012-07-03 18:03:04 +0000673 code->appendf("\t}\n");
rileya@google.com3e332582012-07-03 13:43:35 +0000674 }
rileya@google.com3e332582012-07-03 13:43:35 +0000675}
676
bsalomon@google.com032b2212012-07-16 13:36:18 +0000677void GrGLConical2Gradient::initUniforms(const GrGLShaderBuilder* builder,
678 const GrGLInterface* gl,
679 int programID) {
680 const char* vsParam = builder->getUniformCStr(fVSParamUni);
681 const char* fsParam = builder->getUniformCStr(fFSParamUni);
682 GR_GL_CALL_RET(gl, fVSParamLocation, GetUniformLocation(programID, vsParam));
683 GR_GL_CALL_RET(gl, fFSParamLocation, GetUniformLocation(programID, fsParam));
rileya@google.com3e332582012-07-03 13:43:35 +0000684}
685
686void GrGLConical2Gradient::setData(const GrGLInterface* gl,
rileya@google.com3e332582012-07-03 13:43:35 +0000687 const GrCustomStage& baseData,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000688 const GrRenderTarget*,
rileya@google.com3e332582012-07-03 13:43:35 +0000689 int stageNum) {
690 const GrConical2Gradient& data =
691 static_cast<const GrConical2Gradient&>(baseData);
692 GrAssert(data.isDegenerate() == fIsDegenerate);
693 GrScalar centerX1 = data.center();
694 GrScalar radius0 = data.radius();
695 GrScalar diffRadius = data.diffRadius();
696
697 if (fCachedCenter != centerX1 ||
698 fCachedRadius != radius0 ||
699 fCachedDiffRadius != diffRadius) {
700
701 GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius;
702
703 // When we're in the degenerate (linear) case, the second
704 // value will be INF but the program doesn't read it. (We
705 // use the same 6 uniforms even though we don't need them
706 // all in the linear case just to keep the code complexity
707 // down).
708 float values[6] = {
709 GrScalarToFloat(a * 4),
710 1.f / (GrScalarToFloat(a)),
711 GrScalarToFloat(centerX1),
712 GrScalarToFloat(radius0),
713 GrScalarToFloat(SkScalarMul(radius0, radius0)),
714 GrScalarToFloat(diffRadius)
715 };
716
717 GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
718 GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
719 fCachedCenter = centerX1;
720 fCachedRadius = radius0;
721 fCachedDiffRadius = diffRadius;
722 }
723}
724
725
726/////////////////////////////////////////////////////////////////////
727
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000728GrConical2Gradient::GrConical2Gradient(GrTexture* texture,
729 GrScalar center,
rileya@google.com3e332582012-07-03 13:43:35 +0000730 GrScalar radius,
731 GrScalar diffRadius)
rileya@google.com22e57f92012-07-19 15:16:19 +0000732 : INHERITED (texture)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000733 , fCenterX1 (center)
rileya@google.com3e332582012-07-03 13:43:35 +0000734 , fRadius0 (radius)
735 , fDiffRadius (diffRadius) {
736
737}
738
739GrConical2Gradient::~GrConical2Gradient() {
740
741}
742
743
744const GrProgramStageFactory& GrConical2Gradient::getFactory() const {
745 return GrTProgramStageFactory<GrConical2Gradient>::getInstance();
746}
747
748bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const {
749 const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000750 return (INHERITED::isEqual(sBase) &&
751 this->fCenterX1 == s.fCenterX1 &&
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000752 this->fRadius0 == s.fRadius0 &&
753 this->fDiffRadius == s.fDiffRadius);
rileya@google.com3e332582012-07-03 13:43:35 +0000754}
755
756/////////////////////////////////////////////////////////////////////
757
758
rileya@google.com22e57f92012-07-19 15:16:19 +0000759class GrGLSweepGradient : public GrGLGradientStage {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000760
761public:
762
763 GrGLSweepGradient(const GrProgramStageFactory& factory,
764 const GrCustomStage&) : INHERITED (factory) { }
765 virtual ~GrGLSweepGradient() { }
766
bsalomon@google.com032b2212012-07-16 13:36:18 +0000767 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000768 const char* vertexCoords) SK_OVERRIDE { }
bsalomon@google.com032b2212012-07-16 13:36:18 +0000769 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000770 const char* outputColor,
771 const char* inputColor,
772 const char* samplerName) SK_OVERRIDE;
773
774 static StageKey GenKey(const GrCustomStage& s) { return 0; }
775
776private:
777
rileya@google.com22e57f92012-07-19 15:16:19 +0000778 typedef GrGLGradientStage INHERITED;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000779
780};
781
bsalomon@google.com032b2212012-07-16 13:36:18 +0000782void GrGLSweepGradient::emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000783 const char* outputColor,
784 const char* inputColor,
785 const char* samplerName) {
rileya@google.com22e57f92012-07-19 15:16:19 +0000786 SkString t;
787 t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5",
bsalomon@google.com032b2212012-07-16 13:36:18 +0000788 builder->fSampleCoords.c_str(), builder->fSampleCoords.c_str());
rileya@google.com22e57f92012-07-19 15:16:19 +0000789 this->emitColorLookup(builder, t.c_str(), outputColor, samplerName);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000790}
791
792/////////////////////////////////////////////////////////////////////
793
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000794GrSweepGradient::GrSweepGradient(GrTexture* texture)
rileya@google.com22e57f92012-07-19 15:16:19 +0000795 : INHERITED(texture) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000796
797}
798
799GrSweepGradient::~GrSweepGradient() {
800
801}
802
803const GrProgramStageFactory& GrSweepGradient::getFactory() const {
804 return GrTProgramStageFactory<GrSweepGradient>::getInstance();
805}
806
807bool GrSweepGradient::isEqual(const GrCustomStage& sBase) const {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000808 return INHERITED::isEqual(sBase);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000809}
810