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