blob: 988717a367e4d90c38ebfcedc7ffe5954373c1d6 [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
bsalomon@google.com032b2212012-07-16 13:36:18 +000022 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000023 const char* vertexCoords) SK_OVERRIDE { }
bsalomon@google.com032b2212012-07-16 13:36:18 +000024 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000025 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
bsalomon@google.com032b2212012-07-16 13:36:18 +000037void GrGLRadialGradient::emitFS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +000038 const char* outputColor,
39 const char* inputColor,
40 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +000041 builder->fSampleCoords.printf("vec2(length(%s.xy), 0.5)",
42 builder->fSampleCoords.c_str());
43 builder->fComplexCoord = true;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000044
bsalomon@google.com032b2212012-07-16 13:36:18 +000045 builder->emitDefaultFetch(outputColor, samplerName);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000046}
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
bsalomon@google.com032b2212012-07-16 13:36:18 +000072// For brevity, and these definitions are likely to move to a different class soon.
73typedef GrGLShaderBuilder::UniformHandle UniformHandle;
74static const UniformHandle kInvalidUniformHandle = GrGLShaderBuilder::kInvalidUniformHandle;
75
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000076class GrGLRadial2Gradient : public GrGLProgramStage {
77
78public:
79
80 GrGLRadial2Gradient(const GrProgramStageFactory& factory,
81 const GrCustomStage&);
82 virtual ~GrGLRadial2Gradient() { }
83
bsalomon@google.com032b2212012-07-16 13:36:18 +000084 virtual void setupVariables(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000085 int stage) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +000086 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000087 const char* vertexCoords) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +000088 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000089 const char* outputColor,
90 const char* inputColor,
91 const char* samplerName) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +000092 virtual void initUniforms(const GrGLShaderBuilder* builder,
93 const GrGLInterface*,
94 int programID) SK_OVERRIDE;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000095 virtual void setData(const GrGLInterface*,
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +000096 const GrCustomStage&,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +000097 const GrRenderTarget*,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +000098 int stageNum) SK_OVERRIDE;
99
100 static StageKey GenKey(const GrCustomStage& s) {
101 return (static_cast<const GrRadial2Gradient&>(s).isDegenerate());
102 }
103
104protected:
105
bsalomon@google.com032b2212012-07-16 13:36:18 +0000106 UniformHandle fVSParamUni;
107 GrGLint fVSParamLocation;
108 UniformHandle fFSParamUni;
109 GrGLint fFSParamLocation;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000110
111 const char* fVSVaryingName;
112 const char* fFSVaryingName;
113
114 bool fIsDegenerate;
115
116 // @{
117 /// Values last uploaded as uniforms
118
119 GrScalar fCachedCenter;
120 GrScalar fCachedRadius;
bsalomon@google.com0b323312012-06-01 18:50:01 +0000121 bool fCachedPosRoot;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000122
123 // @}
124
125private:
126
127 typedef GrGLProgramStage INHERITED;
128
129};
130
131GrGLRadial2Gradient::GrGLRadial2Gradient(
132 const GrProgramStageFactory& factory,
133 const GrCustomStage& baseData)
134 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000135 , fVSParamUni(kInvalidUniformHandle)
136 , fFSParamUni(kInvalidUniformHandle)
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000137 , fVSVaryingName(NULL)
138 , fFSVaryingName(NULL)
139 , fCachedCenter(GR_ScalarMax)
140 , fCachedRadius(-GR_ScalarMax)
141 , fCachedPosRoot(0) {
142
143 const GrRadial2Gradient& data =
144 static_cast<const GrRadial2Gradient&>(baseData);
145 fIsDegenerate = data.isDegenerate();
146}
147
bsalomon@google.com032b2212012-07-16 13:36:18 +0000148void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* builder, int stage) {
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000149 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
150 // to work around Xoom bug. Doesn't seem to cause performance decrease
151 // in test apps, but need to keep an eye on it.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000152 fVSParamUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
153 kFloat_GrSLType, "uRadial2VSParams", stage, 6);
154 fFSParamUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
155 kFloat_GrSLType, "uRadial2FSParams", stage, 6);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000156
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000157 fVSParamLocation = GrGLProgramStage::kUseUniform;
158 fFSParamLocation = GrGLProgramStage::kUseUniform;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000159
160 // For radial gradients without perspective we can pass the linear
161 // part of the quadratic as a varying.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000162 if (builder->fVaryingDims == builder->fCoordDims) {
163 builder->addVarying(kFloat_GrSLType, "Radial2BCoeff", stage,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000164 &fVSVaryingName, &fFSVaryingName);
165 }
166}
167
bsalomon@google.com032b2212012-07-16 13:36:18 +0000168void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000169 const char* vertexCoords) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000170 SkString* code = &builder->fVSCode;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000171 SkString p2;
172 SkString p3;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000173 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
174 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000175
176 // For radial gradients without perspective we can pass the linear
177 // part of the quadratic as a varying.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000178 if (builder->fVaryingDims == builder->fCoordDims) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000179 // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3])
180 code->appendf("\t%s = 2.0 *(%s * %s.x - %s);\n",
181 fVSVaryingName, p2.c_str(),
182 vertexCoords, p3.c_str());
183 }
184}
185
bsalomon@google.com032b2212012-07-16 13:36:18 +0000186void GrGLRadial2Gradient::emitFS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000187 const char* outputColor,
188 const char* inputColor,
189 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000190 SkString* code = &builder->fFSCode;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000191 SkString cName("c");
192 SkString ac4Name("ac4");
193 SkString rootName("root");
194 SkString p0;
195 SkString p1;
196 SkString p2;
197 SkString p3;
198 SkString p4;
199 SkString p5;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000200 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
201 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
202 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
203 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
204 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
205 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000206
207 // If we we're able to interpolate the linear component,
208 // bVar is the varying; otherwise compute it
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000209 SkString bVar;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000210 if (builder->fCoordDims == builder->fVaryingDims) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000211 bVar = fFSVaryingName;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000212 GrAssert(2 == builder->fVaryingDims);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000213 } else {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000214 GrAssert(3 == builder->fVaryingDims);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000215 bVar = "b";
216 //bVar.appendS32(stageNum);
217 code->appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n",
218 bVar.c_str(), p2.c_str(),
bsalomon@google.com032b2212012-07-16 13:36:18 +0000219 builder->fSampleCoords.c_str(), p3.c_str());
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000220 }
221
222 // c = (x^2)+(y^2) - params[4]
223 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n",
bsalomon@google.com032b2212012-07-16 13:36:18 +0000224 cName.c_str(), builder->fSampleCoords.c_str(),
225 builder->fSampleCoords.c_str(),
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000226 p4.c_str());
227
228 // If we aren't degenerate, emit some extra code, and accept a slightly
229 // more complex coord.
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000230 if (!fIsDegenerate) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000231
232 // ac4 = 4.0 * params[0] * c
233 code->appendf("\tfloat %s = %s * 4.0 * %s;\n",
234 ac4Name.c_str(), p0.c_str(),
235 cName.c_str());
236
237 // root = sqrt(b^2-4ac)
238 // (abs to avoid exception due to fp precision)
239 code->appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n",
240 rootName.c_str(), bVar.c_str(), bVar.c_str(),
241 ac4Name.c_str());
242
243 // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1]
244 // y coord is 0.5 (texture is effectively 1D)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000245 builder->fSampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)",
246 bVar.c_str(), p5.c_str(),
247 rootName.c_str(), p1.c_str());
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000248 } else {
249 // x coord is: -c/b
250 // y coord is 0.5 (texture is effectively 1D)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000251 builder->fSampleCoords.printf("vec2((-%s / %s), 0.5)",
252 cName.c_str(), bVar.c_str());
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000253 }
bsalomon@google.com032b2212012-07-16 13:36:18 +0000254 builder->fComplexCoord = true;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000255
bsalomon@google.com032b2212012-07-16 13:36:18 +0000256 builder->emitDefaultFetch(outputColor, samplerName);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000257}
258
bsalomon@google.com032b2212012-07-16 13:36:18 +0000259void GrGLRadial2Gradient::initUniforms(const GrGLShaderBuilder* builder,
260 const GrGLInterface* gl,
261 int programID) {
262 const char* vsParam = builder->getUniformCStr(fVSParamUni);
263 const char* fsParam = builder->getUniformCStr(fFSParamUni);
264 GR_GL_CALL_RET(gl, fVSParamLocation, GetUniformLocation(programID, vsParam));
265 GR_GL_CALL_RET(gl, fFSParamLocation, GetUniformLocation(programID, fsParam));
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000266}
267
268void GrGLRadial2Gradient::setData(const GrGLInterface* gl,
rileya@google.com3e332582012-07-03 13:43:35 +0000269 const GrCustomStage& baseData,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000270 const GrRenderTarget*,
rileya@google.com3e332582012-07-03 13:43:35 +0000271 int stageNum) {
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000272 const GrRadial2Gradient& data =
273 static_cast<const GrRadial2Gradient&>(baseData);
274 GrAssert(data.isDegenerate() == fIsDegenerate);
275 GrScalar centerX1 = data.center();
276 GrScalar radius0 = data.radius();
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000277 if (fCachedCenter != centerX1 ||
278 fCachedRadius != radius0 ||
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000279 fCachedPosRoot != data.isPosRoot()) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000280
281 GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1;
282
283 // When we're in the degenerate (linear) case, the second
284 // value will be INF but the program doesn't read it. (We
285 // use the same 6 uniforms even though we don't need them
286 // all in the linear case just to keep the code complexity
287 // down).
288 float values[6] = {
289 GrScalarToFloat(a),
290 1 / (2.f * GrScalarToFloat(a)),
291 GrScalarToFloat(centerX1),
292 GrScalarToFloat(radius0),
293 GrScalarToFloat(GrMul(radius0, radius0)),
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000294 data.isPosRoot() ? 1.f : -1.f
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000295 };
296
tomhudson@google.com761b37c2012-06-13 15:22:18 +0000297 GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
298 GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000299 fCachedCenter = centerX1;
300 fCachedRadius = radius0;
tomhudson@google.comdcdc1fc2012-05-31 19:53:37 +0000301 fCachedPosRoot = data.isPosRoot();
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000302 }
303}
304
305
306/////////////////////////////////////////////////////////////////////
307
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000308GrRadial2Gradient::GrRadial2Gradient(GrTexture* texture,
309 GrScalar center,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000310 GrScalar radius,
311 bool posRoot)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000312 : GrSingleTextureEffect(texture)
313 , fCenterX1 (center)
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000314 , fRadius0 (radius)
315 , fPosRoot (posRoot) {
316
317}
318
319GrRadial2Gradient::~GrRadial2Gradient() {
320
321}
322
323
324const GrProgramStageFactory& GrRadial2Gradient::getFactory() const {
325 return GrTProgramStageFactory<GrRadial2Gradient>::getInstance();
326}
327
328bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const {
329 const GrRadial2Gradient& s = static_cast<const GrRadial2Gradient&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000330 return (INHERITED::isEqual(sBase) &&
331 this->fCenterX1 == s.fCenterX1 &&
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000332 this->fRadius0 == s.fRadius0 &&
333 this->fPosRoot == s.fPosRoot);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000334}
335
336/////////////////////////////////////////////////////////////////////
337
rileya@google.com3e332582012-07-03 13:43:35 +0000338class GrGLConical2Gradient : public GrGLProgramStage {
339
340public:
341
342 GrGLConical2Gradient(const GrProgramStageFactory& factory,
343 const GrCustomStage&);
344 virtual ~GrGLConical2Gradient() { }
345
bsalomon@google.com032b2212012-07-16 13:36:18 +0000346 virtual void setupVariables(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000347 int stage) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000348 virtual void emitVS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000349 const char* vertexCoords) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000350 virtual void emitFS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000351 const char* outputColor,
352 const char* inputColor,
353 const char* samplerName) SK_OVERRIDE;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000354 virtual void initUniforms(const GrGLShaderBuilder* builder,
355 const GrGLInterface*,
356 int programID) SK_OVERRIDE;
rileya@google.com3e332582012-07-03 13:43:35 +0000357 virtual void setData(const GrGLInterface*,
rileya@google.com3e332582012-07-03 13:43:35 +0000358 const GrCustomStage&,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000359 const GrRenderTarget*,
rileya@google.com3e332582012-07-03 13:43:35 +0000360 int stageNum) SK_OVERRIDE;
361
362 static StageKey GenKey(const GrCustomStage& s) {
363 return (static_cast<const GrConical2Gradient&>(s).isDegenerate());
364 }
365
366protected:
367
bsalomon@google.com032b2212012-07-16 13:36:18 +0000368 UniformHandle fVSParamUni;
369 GrGLint fVSParamLocation;
370 UniformHandle fFSParamUni;
371 GrGLint fFSParamLocation;
rileya@google.com3e332582012-07-03 13:43:35 +0000372
373 const char* fVSVaryingName;
374 const char* fFSVaryingName;
375
376 bool fIsDegenerate;
377
378 // @{
379 /// Values last uploaded as uniforms
380
381 GrScalar fCachedCenter;
382 GrScalar fCachedRadius;
383 GrScalar fCachedDiffRadius;
384
385 // @}
386
387private:
388
389 typedef GrGLProgramStage INHERITED;
390
391};
392
393GrGLConical2Gradient::GrGLConical2Gradient(
394 const GrProgramStageFactory& factory,
395 const GrCustomStage& baseData)
396 : INHERITED(factory)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000397 , fVSParamUni(kInvalidUniformHandle)
398 , fFSParamUni(kInvalidUniformHandle)
rileya@google.com3e332582012-07-03 13:43:35 +0000399 , fVSVaryingName(NULL)
400 , fFSVaryingName(NULL)
401 , fCachedCenter(GR_ScalarMax)
402 , fCachedRadius(-GR_ScalarMax)
403 , fCachedDiffRadius(-GR_ScalarMax) {
404
405 const GrConical2Gradient& data =
406 static_cast<const GrConical2Gradient&>(baseData);
407 fIsDegenerate = data.isDegenerate();
408}
409
bsalomon@google.com032b2212012-07-16 13:36:18 +0000410void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* builder, int stage) {
rileya@google.com3e332582012-07-03 13:43:35 +0000411 // 2 copies of uniform array, 1 for each of vertex & fragment shader,
412 // to work around Xoom bug. Doesn't seem to cause performance decrease
413 // in test apps, but need to keep an eye on it.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000414 fVSParamUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
415 kFloat_GrSLType, "uConical2VSParams", stage, 6);
416 fFSParamUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
417 kFloat_GrSLType, "uConical2FSParams", stage, 6);
rileya@google.com3e332582012-07-03 13:43:35 +0000418
419 fVSParamLocation = GrGLProgramStage::kUseUniform;
420 fFSParamLocation = GrGLProgramStage::kUseUniform;
421
422 // For radial gradients without perspective we can pass the linear
423 // part of the quadratic as a varying.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000424 if (builder->fVaryingDims == builder->fCoordDims) {
425 builder->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage,
426 &fVSVaryingName, &fFSVaryingName);
rileya@google.com3e332582012-07-03 13:43:35 +0000427 }
428}
429
bsalomon@google.com032b2212012-07-16 13:36:18 +0000430void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000431 const char* vertexCoords) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000432 SkString* code = &builder->fVSCode;
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000433 SkString p2; // distance between centers
434 SkString p3; // start radius
435 SkString p5; // difference in radii (r1 - r0)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000436 builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2);
437 builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3);
438 builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5);
rileya@google.com3e332582012-07-03 13:43:35 +0000439
440 // For radial gradients without perspective we can pass the linear
441 // part of the quadratic as a varying.
bsalomon@google.com032b2212012-07-16 13:36:18 +0000442 if (builder->fVaryingDims == builder->fCoordDims) {
rileya@google.com3e332582012-07-03 13:43:35 +0000443 // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5])
444 code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n",
445 fVSVaryingName, p2.c_str(),
446 vertexCoords, p3.c_str(), p5.c_str());
447 }
448}
449
bsalomon@google.com032b2212012-07-16 13:36:18 +0000450void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* builder,
rileya@google.com3e332582012-07-03 13:43:35 +0000451 const char* outputColor,
452 const char* inputColor,
453 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000454 SkString* code = &builder->fFSCode;
rileya@google.com3e332582012-07-03 13:43:35 +0000455
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000456 SkString cName("c");
457 SkString ac4Name("ac4");
458 SkString dName("d");
459 SkString qName("q");
460 SkString r0Name("r0");
461 SkString r1Name("r1");
462 SkString tName("t");
463 SkString p0; // 4a
rileya@google.com62197282012-07-10 20:05:23 +0000464 SkString p1; // 1/a
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000465 SkString p2; // distance between centers
466 SkString p3; // start radius
467 SkString p4; // start radius squared
468 SkString p5; // difference in radii (r1 - r0)
bsalomon@google.com032b2212012-07-16 13:36:18 +0000469
bsalomon@google.comec4037f2012-07-16 13:46:39 +0000470 builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0);
471 builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1);
472 builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2);
473 builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3);
474 builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4);
475 builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5);
rileya@google.com3e332582012-07-03 13:43:35 +0000476
477 // If we we're able to interpolate the linear component,
478 // bVar is the varying; otherwise compute it
bsalomon@google.comf0a104e2012-07-10 17:51:07 +0000479 SkString bVar;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000480 if (builder->fCoordDims == builder->fVaryingDims) {
rileya@google.com3e332582012-07-03 13:43:35 +0000481 bVar = fFSVaryingName;
bsalomon@google.com032b2212012-07-16 13:36:18 +0000482 GrAssert(2 == builder->fVaryingDims);
rileya@google.com3e332582012-07-03 13:43:35 +0000483 } else {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000484 GrAssert(3 == builder->fVaryingDims);
rileya@google.com3e332582012-07-03 13:43:35 +0000485 bVar = "b";
486 code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n",
bsalomon@google.com032b2212012-07-16 13:36:18 +0000487 bVar.c_str(), p2.c_str(), builder->fSampleCoords.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000488 p3.c_str(), p5.c_str());
489 }
490
rileya@google.come38160c2012-07-03 18:03:04 +0000491 // output will default to transparent black (we simply won't write anything
492 // else to it if invalid, instead of discarding or returning prematurely)
493 code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor);
494
rileya@google.com3e332582012-07-03 13:43:35 +0000495 // c = (x^2)+(y^2) - params[4]
496 code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(),
bsalomon@google.com032b2212012-07-16 13:36:18 +0000497 builder->fSampleCoords.c_str(), builder->fSampleCoords.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000498 p4.c_str());
499
500 // Non-degenerate case (quadratic)
501 if (!fIsDegenerate) {
502
503 // ac4 = params[0] * c
504 code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(),
505 cName.c_str());
506
507 // d = b^2 - ac4
508 code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(),
509 bVar.c_str(), bVar.c_str(), ac4Name.c_str());
510
rileya@google.come38160c2012-07-03 18:03:04 +0000511 // only proceed if discriminant is >= 0
512 code->appendf("\tif (%s >= 0.0) {\n", dName.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000513
514 // intermediate value we'll use to compute the roots
515 // q = -0.5 * (b +/- sqrt(d))
rileya@google.come38160c2012-07-03 18:03:04 +0000516 code->appendf("\t\tfloat %s = -0.5 * (%s + (%s < 0.0 ? -1.0 : 1.0)"
rileya@google.com3e332582012-07-03 13:43:35 +0000517 " * sqrt(%s));\n", qName.c_str(), bVar.c_str(),
518 bVar.c_str(), dName.c_str());
519
520 // compute both roots
521 // r0 = q * params[1]
rileya@google.come38160c2012-07-03 18:03:04 +0000522 code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(),
523 qName.c_str(), p1.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000524 // r1 = c / q
rileya@google.come38160c2012-07-03 18:03:04 +0000525 code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(),
526 cName.c_str(), qName.c_str());
rileya@google.com3e332582012-07-03 13:43:35 +0000527
528 // Note: If there are two roots that both generate radius(t) > 0, the
529 // Canvas spec says to choose the larger t.
530
531 // so we'll look at the larger one first:
rileya@google.come38160c2012-07-03 18:03:04 +0000532 code->appendf("\t\tfloat %s = max(%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, then we're done; t will be our x coordinate
536 code->appendf("\t\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000537 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");
bsalomon@google.com032b2212012-07-16 13:36:18 +0000541 builder->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
542 builder->emitDefaultFetch(outputColor, samplerName);
rileya@google.come38160c2012-07-03 18:03:04 +0000543
544 // otherwise, if r(t) for the larger root was <= 0, try the other root
545 code->appendf("\t\t} else {\n");
546 code->appendf("\t\t\t%s = min(%s, %s);\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000547 r0Name.c_str(), r1Name.c_str());
548
rileya@google.come38160c2012-07-03 18:03:04 +0000549 // if r(t) > 0 for the smaller root, then t will be our x coordinate
550 code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n",
rileya@google.com3e332582012-07-03 13:43:35 +0000551 tName.c_str(), p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000552
553 // y coord is 0.5 (texture is effectively 1D)
554 code->appendf("\t\t\t");
bsalomon@google.com032b2212012-07-16 13:36:18 +0000555 builder->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
556 builder->emitDefaultFetch(outputColor, samplerName);
rileya@google.come38160c2012-07-03 18:03:04 +0000557
558 // end if (r(t) > 0) for smaller root
559 code->appendf("\t\t\t}\n");
560 // end if (r(t) > 0), else, for larger root
561 code->appendf("\t\t}\n");
562 // end if (discriminant >= 0)
rileya@google.com3e332582012-07-03 13:43:35 +0000563 code->appendf("\t}\n");
564 } else {
565
566 // linear case: t = -c/b
567 code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(),
568 cName.c_str(), bVar.c_str());
569
rileya@google.come38160c2012-07-03 18:03:04 +0000570 // if r(t) > 0, then t will be the x coordinate
571 code->appendf("\tif (%s * %s + %s > 0.0) {\n", tName.c_str(),
rileya@google.com3e332582012-07-03 13:43:35 +0000572 p5.c_str(), p3.c_str());
rileya@google.come38160c2012-07-03 18:03:04 +0000573 code->appendf("\t");
bsalomon@google.com032b2212012-07-16 13:36:18 +0000574 builder->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str());
575 builder->emitDefaultFetch(outputColor, samplerName);
rileya@google.come38160c2012-07-03 18:03:04 +0000576 code->appendf("\t}\n");
rileya@google.com3e332582012-07-03 13:43:35 +0000577 }
bsalomon@google.com032b2212012-07-16 13:36:18 +0000578 builder->fComplexCoord = true;
rileya@google.com3e332582012-07-03 13:43:35 +0000579}
580
bsalomon@google.com032b2212012-07-16 13:36:18 +0000581void GrGLConical2Gradient::initUniforms(const GrGLShaderBuilder* builder,
582 const GrGLInterface* gl,
583 int programID) {
584 const char* vsParam = builder->getUniformCStr(fVSParamUni);
585 const char* fsParam = builder->getUniformCStr(fFSParamUni);
586 GR_GL_CALL_RET(gl, fVSParamLocation, GetUniformLocation(programID, vsParam));
587 GR_GL_CALL_RET(gl, fFSParamLocation, GetUniformLocation(programID, fsParam));
rileya@google.com3e332582012-07-03 13:43:35 +0000588}
589
590void GrGLConical2Gradient::setData(const GrGLInterface* gl,
rileya@google.com3e332582012-07-03 13:43:35 +0000591 const GrCustomStage& baseData,
senorblanco@chromium.orgf4770d72012-07-13 18:25:06 +0000592 const GrRenderTarget*,
rileya@google.com3e332582012-07-03 13:43:35 +0000593 int stageNum) {
594 const GrConical2Gradient& data =
595 static_cast<const GrConical2Gradient&>(baseData);
596 GrAssert(data.isDegenerate() == fIsDegenerate);
597 GrScalar centerX1 = data.center();
598 GrScalar radius0 = data.radius();
599 GrScalar diffRadius = data.diffRadius();
600
601 if (fCachedCenter != centerX1 ||
602 fCachedRadius != radius0 ||
603 fCachedDiffRadius != diffRadius) {
604
605 GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius;
606
607 // When we're in the degenerate (linear) case, the second
608 // value will be INF but the program doesn't read it. (We
609 // use the same 6 uniforms even though we don't need them
610 // all in the linear case just to keep the code complexity
611 // down).
612 float values[6] = {
613 GrScalarToFloat(a * 4),
614 1.f / (GrScalarToFloat(a)),
615 GrScalarToFloat(centerX1),
616 GrScalarToFloat(radius0),
617 GrScalarToFloat(SkScalarMul(radius0, radius0)),
618 GrScalarToFloat(diffRadius)
619 };
620
621 GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values));
622 GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values));
623 fCachedCenter = centerX1;
624 fCachedRadius = radius0;
625 fCachedDiffRadius = diffRadius;
626 }
627}
628
629
630/////////////////////////////////////////////////////////////////////
631
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000632GrConical2Gradient::GrConical2Gradient(GrTexture* texture,
633 GrScalar center,
rileya@google.com3e332582012-07-03 13:43:35 +0000634 GrScalar radius,
635 GrScalar diffRadius)
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000636 : GrSingleTextureEffect (texture)
637 , fCenterX1 (center)
rileya@google.com3e332582012-07-03 13:43:35 +0000638 , fRadius0 (radius)
639 , fDiffRadius (diffRadius) {
640
641}
642
643GrConical2Gradient::~GrConical2Gradient() {
644
645}
646
647
648const GrProgramStageFactory& GrConical2Gradient::getFactory() const {
649 return GrTProgramStageFactory<GrConical2Gradient>::getInstance();
650}
651
652bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const {
653 const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000654 return (INHERITED::isEqual(sBase) &&
655 this->fCenterX1 == s.fCenterX1 &&
tomhudson@google.com1dcfa1f2012-07-09 18:21:28 +0000656 this->fRadius0 == s.fRadius0 &&
657 this->fDiffRadius == s.fDiffRadius);
rileya@google.com3e332582012-07-03 13:43:35 +0000658}
659
660/////////////////////////////////////////////////////////////////////
661
662
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000663class GrGLSweepGradient : public GrGLProgramStage {
664
665public:
666
667 GrGLSweepGradient(const GrProgramStageFactory& factory,
668 const GrCustomStage&) : INHERITED (factory) { }
669 virtual ~GrGLSweepGradient() { }
670
bsalomon@google.com032b2212012-07-16 13:36:18 +0000671 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000672 const char* vertexCoords) SK_OVERRIDE { }
bsalomon@google.com032b2212012-07-16 13:36:18 +0000673 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000674 const char* outputColor,
675 const char* inputColor,
676 const char* samplerName) SK_OVERRIDE;
677
678 static StageKey GenKey(const GrCustomStage& s) { return 0; }
679
680private:
681
682 typedef GrGLProgramStage INHERITED;
683
684};
685
bsalomon@google.com032b2212012-07-16 13:36:18 +0000686void GrGLSweepGradient::emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000687 const char* outputColor,
688 const char* inputColor,
689 const char* samplerName) {
bsalomon@google.com032b2212012-07-16 13:36:18 +0000690 builder->fSampleCoords.printf(
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000691 "vec2(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5, 0.5)",
bsalomon@google.com032b2212012-07-16 13:36:18 +0000692 builder->fSampleCoords.c_str(), builder->fSampleCoords.c_str());
693 builder->fComplexCoord = true;
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000694
bsalomon@google.com032b2212012-07-16 13:36:18 +0000695 builder->emitDefaultFetch(outputColor, samplerName);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000696}
697
698/////////////////////////////////////////////////////////////////////
699
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000700GrSweepGradient::GrSweepGradient(GrTexture* texture)
701 : GrSingleTextureEffect(texture) {
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000702
703}
704
705GrSweepGradient::~GrSweepGradient() {
706
707}
708
709const GrProgramStageFactory& GrSweepGradient::getFactory() const {
710 return GrTProgramStageFactory<GrSweepGradient>::getInstance();
711}
712
713bool GrSweepGradient::isEqual(const GrCustomStage& sBase) const {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000714 return INHERITED::isEqual(sBase);
tomhudson@google.com7fab52d2012-05-31 19:40:13 +0000715}
716