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