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) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 165 | GrStringBuilder* code = &state->fVSCode; |
| 166 | GrStringBuilder p2; |
| 167 | GrStringBuilder 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) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 185 | GrStringBuilder* code = &state->fFSCode; |
| 186 | GrStringBuilder cName("c"); |
| 187 | GrStringBuilder ac4Name("ac4"); |
| 188 | GrStringBuilder rootName("root"); |
| 189 | GrStringBuilder p0; |
| 190 | GrStringBuilder p1; |
| 191 | GrStringBuilder p2; |
| 192 | GrStringBuilder p3; |
| 193 | GrStringBuilder p4; |
| 194 | GrStringBuilder p5; |
tomhudson@google.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 |
| 204 | GrStringBuilder bVar; |
| 205 | if (state->fCoordDims == state->fVaryingDims) { |
| 206 | bVar = fFSVaryingName; |
| 207 | GrAssert(2 == state->fVaryingDims); |
| 208 | } else { |
| 209 | GrAssert(3 == state->fVaryingDims); |
| 210 | bVar = "b"; |
| 211 | //bVar.appendS32(stageNum); |
| 212 | code->appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n", |
| 213 | bVar.c_str(), p2.c_str(), |
| 214 | state->fSampleCoords.c_str(), p3.c_str()); |
| 215 | } |
| 216 | |
| 217 | // c = (x^2)+(y^2) - params[4] |
| 218 | code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", |
| 219 | cName.c_str(), state->fSampleCoords.c_str(), |
| 220 | state->fSampleCoords.c_str(), |
| 221 | p4.c_str()); |
| 222 | |
| 223 | // If we aren't degenerate, emit some extra code, and accept a slightly |
| 224 | // more complex coord. |
tomhudson@google.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); |
| 321 | return (this->isDegenerate() == s.isDegenerate()); |
| 322 | } |
| 323 | |
| 324 | ///////////////////////////////////////////////////////////////////// |
| 325 | |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame^] | 326 | class GrGLConical2Gradient : public GrGLProgramStage { |
| 327 | |
| 328 | public: |
| 329 | |
| 330 | GrGLConical2Gradient(const GrProgramStageFactory& factory, |
| 331 | const GrCustomStage&); |
| 332 | virtual ~GrGLConical2Gradient() { } |
| 333 | |
| 334 | virtual void setupVariables(GrGLShaderBuilder* state, |
| 335 | int stage) SK_OVERRIDE; |
| 336 | virtual void emitVS(GrGLShaderBuilder* state, |
| 337 | const char* vertexCoords) SK_OVERRIDE; |
| 338 | virtual void emitFS(GrGLShaderBuilder* state, |
| 339 | const char* outputColor, |
| 340 | const char* inputColor, |
| 341 | const char* samplerName) SK_OVERRIDE; |
| 342 | virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE; |
| 343 | virtual void setData(const GrGLInterface*, |
| 344 | const GrGLTexture&, |
| 345 | const GrCustomStage&, |
| 346 | int stageNum) SK_OVERRIDE; |
| 347 | |
| 348 | static StageKey GenKey(const GrCustomStage& s) { |
| 349 | return (static_cast<const GrConical2Gradient&>(s).isDegenerate()); |
| 350 | } |
| 351 | |
| 352 | protected: |
| 353 | |
| 354 | const GrGLShaderVar* fVSParamVar; |
| 355 | GrGLint fVSParamLocation; |
| 356 | const GrGLShaderVar* fFSParamVar; |
| 357 | GrGLint fFSParamLocation; |
| 358 | |
| 359 | const char* fVSVaryingName; |
| 360 | const char* fFSVaryingName; |
| 361 | |
| 362 | bool fIsDegenerate; |
| 363 | |
| 364 | // @{ |
| 365 | /// Values last uploaded as uniforms |
| 366 | |
| 367 | GrScalar fCachedCenter; |
| 368 | GrScalar fCachedRadius; |
| 369 | GrScalar fCachedDiffRadius; |
| 370 | |
| 371 | // @} |
| 372 | |
| 373 | private: |
| 374 | |
| 375 | typedef GrGLProgramStage INHERITED; |
| 376 | |
| 377 | }; |
| 378 | |
| 379 | GrGLConical2Gradient::GrGLConical2Gradient( |
| 380 | const GrProgramStageFactory& factory, |
| 381 | const GrCustomStage& baseData) |
| 382 | : INHERITED(factory) |
| 383 | , fVSParamVar(NULL) |
| 384 | , fFSParamVar(NULL) |
| 385 | , fVSVaryingName(NULL) |
| 386 | , fFSVaryingName(NULL) |
| 387 | , fCachedCenter(GR_ScalarMax) |
| 388 | , fCachedRadius(-GR_ScalarMax) |
| 389 | , fCachedDiffRadius(-GR_ScalarMax) { |
| 390 | |
| 391 | const GrConical2Gradient& data = |
| 392 | static_cast<const GrConical2Gradient&>(baseData); |
| 393 | fIsDegenerate = data.isDegenerate(); |
| 394 | } |
| 395 | |
| 396 | void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) { |
| 397 | // 2 copies of uniform array, 1 for each of vertex & fragment shader, |
| 398 | // to work around Xoom bug. Doesn't seem to cause performance decrease |
| 399 | // in test apps, but need to keep an eye on it. |
| 400 | fVSParamVar = &state->addUniform( |
| 401 | GrGLShaderBuilder::kVertex_VariableLifetime, |
| 402 | kFloat_GrSLType, "uConical2VSParams", stage, 6); |
| 403 | fFSParamVar = &state->addUniform( |
| 404 | GrGLShaderBuilder::kFragment_VariableLifetime, |
| 405 | kFloat_GrSLType, "uConical2FSParams", stage, 6); |
| 406 | |
| 407 | fVSParamLocation = GrGLProgramStage::kUseUniform; |
| 408 | fFSParamLocation = GrGLProgramStage::kUseUniform; |
| 409 | |
| 410 | // For radial gradients without perspective we can pass the linear |
| 411 | // part of the quadratic as a varying. |
| 412 | if (state->fVaryingDims == state->fCoordDims) { |
| 413 | state->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage, |
| 414 | &fVSVaryingName, &fFSVaryingName); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* state, |
| 419 | const char* vertexCoords) { |
| 420 | GrStringBuilder* code = &state->fVSCode; |
| 421 | GrStringBuilder p2; // distance between centers |
| 422 | GrStringBuilder p3; // start radius |
| 423 | GrStringBuilder p5; // difference in radii (r1 - r0) |
| 424 | fVSParamVar->appendArrayAccess(2, &p2); |
| 425 | fVSParamVar->appendArrayAccess(3, &p3); |
| 426 | fVSParamVar->appendArrayAccess(5, &p5); |
| 427 | |
| 428 | // For radial gradients without perspective we can pass the linear |
| 429 | // part of the quadratic as a varying. |
| 430 | if (state->fVaryingDims == state->fCoordDims) { |
| 431 | // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5]) |
| 432 | code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n", |
| 433 | fVSVaryingName, p2.c_str(), |
| 434 | vertexCoords, p3.c_str(), p5.c_str()); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* state, |
| 439 | const char* outputColor, |
| 440 | const char* inputColor, |
| 441 | const char* samplerName) { |
| 442 | GrStringBuilder* code = &state->fFSCode; |
| 443 | |
| 444 | GrStringBuilder cName("c"); |
| 445 | GrStringBuilder ac4Name("ac4"); |
| 446 | GrStringBuilder dName("d"); |
| 447 | GrStringBuilder qName("q"); |
| 448 | GrStringBuilder r0Name("r0"); |
| 449 | GrStringBuilder r1Name("r1"); |
| 450 | GrStringBuilder tName("t"); |
| 451 | GrStringBuilder p0; // 4a |
| 452 | GrStringBuilder p1; // 1/(2a) |
| 453 | GrStringBuilder p2; // distance between centers |
| 454 | GrStringBuilder p3; // start radius |
| 455 | GrStringBuilder p4; // start radius squared |
| 456 | GrStringBuilder p5; // difference in radii (r1 - r0) |
| 457 | fFSParamVar->appendArrayAccess(0, &p0); |
| 458 | fFSParamVar->appendArrayAccess(1, &p1); |
| 459 | fFSParamVar->appendArrayAccess(2, &p2); |
| 460 | fFSParamVar->appendArrayAccess(3, &p3); |
| 461 | fFSParamVar->appendArrayAccess(4, &p4); |
| 462 | fFSParamVar->appendArrayAccess(5, &p5); |
| 463 | |
| 464 | // If we we're able to interpolate the linear component, |
| 465 | // bVar is the varying; otherwise compute it |
| 466 | GrStringBuilder bVar; |
| 467 | if (state->fCoordDims == state->fVaryingDims) { |
| 468 | bVar = fFSVaryingName; |
| 469 | GrAssert(2 == state->fVaryingDims); |
| 470 | } else { |
| 471 | GrAssert(3 == state->fVaryingDims); |
| 472 | bVar = "b"; |
| 473 | code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n", |
| 474 | bVar.c_str(), p2.c_str(), state->fSampleCoords.c_str(), |
| 475 | p3.c_str(), p5.c_str()); |
| 476 | } |
| 477 | |
| 478 | // c = (x^2)+(y^2) - params[4] |
| 479 | code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(), |
| 480 | state->fSampleCoords.c_str(), state->fSampleCoords.c_str(), |
| 481 | p4.c_str()); |
| 482 | |
| 483 | // Non-degenerate case (quadratic) |
| 484 | if (!fIsDegenerate) { |
| 485 | |
| 486 | // ac4 = params[0] * c |
| 487 | code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(), |
| 488 | cName.c_str()); |
| 489 | |
| 490 | // d = b^2 - ac4 |
| 491 | code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(), |
| 492 | bVar.c_str(), bVar.c_str(), ac4Name.c_str()); |
| 493 | |
| 494 | // if discriminant is < 0, set to transparent black and return |
| 495 | code->appendf("\tif (%s < 0.0) {\n\t\tgl_FragColor = vec4(0.0,0.0,0.0," |
| 496 | "0.0);\n\t\treturn;\n\t}\n", dName.c_str()); |
| 497 | |
| 498 | // intermediate value we'll use to compute the roots |
| 499 | // q = -0.5 * (b +/- sqrt(d)) |
| 500 | code->appendf("\tfloat %s = -0.5f * (%s + (%s < 0.0 ? -1.0 : 1.0)" |
| 501 | " * sqrt(%s));\n", qName.c_str(), bVar.c_str(), |
| 502 | bVar.c_str(), dName.c_str()); |
| 503 | |
| 504 | // compute both roots |
| 505 | // r0 = q * params[1] |
| 506 | code->appendf("\tfloat %s = %s * %s;\n", r0Name.c_str(), qName.c_str(), |
| 507 | p1.c_str()); |
| 508 | // r1 = c / q |
| 509 | code->appendf("\tfloat %s = %s / %s;\n", r1Name.c_str(), cName.c_str(), |
| 510 | qName.c_str()); |
| 511 | |
| 512 | // Note: If there are two roots that both generate radius(t) > 0, the |
| 513 | // Canvas spec says to choose the larger t. |
| 514 | |
| 515 | // so we'll look at the larger one first: |
| 516 | code->appendf("\tfloat %s = max(%s, %s);\n", tName.c_str(), |
| 517 | r0Name.c_str(), r1Name.c_str()); |
| 518 | |
| 519 | // if r(t) for the larger root is <= 0, try the other one |
| 520 | code->appendf("\tif (%s * %s + %s <= 0.0) {\n", tName.c_str(), |
| 521 | p5.c_str(), p3.c_str()); |
| 522 | code->appendf("\t\t%s = min(%s, %s);\n", tName.c_str(), |
| 523 | r0Name.c_str(), r1Name.c_str()); |
| 524 | |
| 525 | // if r(t) for the smaller root is also <= 0, set the fragment to |
| 526 | // transparent black and return |
| 527 | code->appendf("\t\tif (%s * %s + %s <= 0.0) {\n\t\t\tgl_FragColor = " |
| 528 | "vec4(0.0,0.0,0.0,0.0);\n\t\t\treturn;\n\t\t}\n", |
| 529 | tName.c_str(), p5.c_str(), p3.c_str()); |
| 530 | |
| 531 | code->appendf("\t}\n"); |
| 532 | } else { |
| 533 | |
| 534 | // linear case: t = -c/b |
| 535 | code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(), |
| 536 | cName.c_str(), bVar.c_str()); |
| 537 | |
| 538 | // set the fragment to transparent black and return if r(t) is <= 0 |
| 539 | code->appendf("\tif (%s * %s + %s <= 0.0) {\n\t\tgl_FragColor = vec4(" |
| 540 | "0.0,0.0,0.0,0.0);\n\t\treturn;\n\t}\n", tName.c_str(), |
| 541 | p5.c_str(), p3.c_str()); |
| 542 | } |
| 543 | state->fComplexCoord = true; |
| 544 | |
| 545 | // x coord is: t |
| 546 | // y coord is 0.5 (texture is effectively 1D) |
| 547 | state->fSampleCoords.printf("\tvec2(%s, 0.5)", tName.c_str()); |
| 548 | |
| 549 | state->emitDefaultFetch(outputColor, samplerName); |
| 550 | } |
| 551 | |
| 552 | void GrGLConical2Gradient::initUniforms(const GrGLInterface* gl, int programID) { |
| 553 | GR_GL_CALL_RET(gl, fVSParamLocation, |
| 554 | GetUniformLocation(programID, fVSParamVar->getName().c_str())); |
| 555 | GR_GL_CALL_RET(gl, fFSParamLocation, |
| 556 | GetUniformLocation(programID, fFSParamVar->getName().c_str())); |
| 557 | } |
| 558 | |
| 559 | void GrGLConical2Gradient::setData(const GrGLInterface* gl, |
| 560 | const GrGLTexture& texture, |
| 561 | const GrCustomStage& baseData, |
| 562 | int stageNum) { |
| 563 | const GrConical2Gradient& data = |
| 564 | static_cast<const GrConical2Gradient&>(baseData); |
| 565 | GrAssert(data.isDegenerate() == fIsDegenerate); |
| 566 | GrScalar centerX1 = data.center(); |
| 567 | GrScalar radius0 = data.radius(); |
| 568 | GrScalar diffRadius = data.diffRadius(); |
| 569 | |
| 570 | if (fCachedCenter != centerX1 || |
| 571 | fCachedRadius != radius0 || |
| 572 | fCachedDiffRadius != diffRadius) { |
| 573 | |
| 574 | GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius; |
| 575 | |
| 576 | // When we're in the degenerate (linear) case, the second |
| 577 | // value will be INF but the program doesn't read it. (We |
| 578 | // use the same 6 uniforms even though we don't need them |
| 579 | // all in the linear case just to keep the code complexity |
| 580 | // down). |
| 581 | float values[6] = { |
| 582 | GrScalarToFloat(a * 4), |
| 583 | 1.f / (GrScalarToFloat(a)), |
| 584 | GrScalarToFloat(centerX1), |
| 585 | GrScalarToFloat(radius0), |
| 586 | GrScalarToFloat(SkScalarMul(radius0, radius0)), |
| 587 | GrScalarToFloat(diffRadius) |
| 588 | }; |
| 589 | |
| 590 | GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values)); |
| 591 | GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values)); |
| 592 | fCachedCenter = centerX1; |
| 593 | fCachedRadius = radius0; |
| 594 | fCachedDiffRadius = diffRadius; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | |
| 599 | ///////////////////////////////////////////////////////////////////// |
| 600 | |
| 601 | GrConical2Gradient::GrConical2Gradient(GrScalar center, |
| 602 | GrScalar radius, |
| 603 | GrScalar diffRadius) |
| 604 | : fCenterX1 (center) |
| 605 | , fRadius0 (radius) |
| 606 | , fDiffRadius (diffRadius) { |
| 607 | |
| 608 | } |
| 609 | |
| 610 | GrConical2Gradient::~GrConical2Gradient() { |
| 611 | |
| 612 | } |
| 613 | |
| 614 | |
| 615 | const GrProgramStageFactory& GrConical2Gradient::getFactory() const { |
| 616 | return GrTProgramStageFactory<GrConical2Gradient>::getInstance(); |
| 617 | } |
| 618 | |
| 619 | bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const { |
| 620 | const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase); |
| 621 | return (this->isDegenerate() == s.isDegenerate()); |
| 622 | } |
| 623 | |
| 624 | ///////////////////////////////////////////////////////////////////// |
| 625 | |
| 626 | |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 627 | class GrGLSweepGradient : public GrGLProgramStage { |
| 628 | |
| 629 | public: |
| 630 | |
| 631 | GrGLSweepGradient(const GrProgramStageFactory& factory, |
| 632 | const GrCustomStage&) : INHERITED (factory) { } |
| 633 | virtual ~GrGLSweepGradient() { } |
| 634 | |
| 635 | virtual void emitVS(GrGLShaderBuilder* state, |
| 636 | const char* vertexCoords) SK_OVERRIDE { } |
| 637 | virtual void emitFS(GrGLShaderBuilder* state, |
| 638 | const char* outputColor, |
| 639 | const char* inputColor, |
| 640 | const char* samplerName) SK_OVERRIDE; |
| 641 | |
| 642 | static StageKey GenKey(const GrCustomStage& s) { return 0; } |
| 643 | |
| 644 | private: |
| 645 | |
| 646 | typedef GrGLProgramStage INHERITED; |
| 647 | |
| 648 | }; |
| 649 | |
| 650 | void GrGLSweepGradient::emitFS(GrGLShaderBuilder* state, |
| 651 | const char* outputColor, |
| 652 | const char* inputColor, |
| 653 | const char* samplerName) { |
| 654 | state->fSampleCoords.printf( |
| 655 | "vec2(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5, 0.5)", |
| 656 | state->fSampleCoords.c_str(), state->fSampleCoords.c_str()); |
| 657 | state->fComplexCoord = true; |
| 658 | |
| 659 | state->emitDefaultFetch(outputColor, samplerName); |
| 660 | } |
| 661 | |
| 662 | ///////////////////////////////////////////////////////////////////// |
| 663 | |
| 664 | GrSweepGradient::GrSweepGradient() { |
| 665 | |
| 666 | } |
| 667 | |
| 668 | GrSweepGradient::~GrSweepGradient() { |
| 669 | |
| 670 | } |
| 671 | |
| 672 | const GrProgramStageFactory& GrSweepGradient::getFactory() const { |
| 673 | return GrTProgramStageFactory<GrSweepGradient>::getInstance(); |
| 674 | } |
| 675 | |
| 676 | bool GrSweepGradient::isEqual(const GrCustomStage& sBase) const { |
| 677 | return true; |
| 678 | } |
| 679 | |