| 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 | |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 52 | GrRadialGradient::GrRadialGradient(GrTexture* texture) |
| 53 | : GrSingleTextureEffect(texture) { |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 54 | |
| 55 | } |
| 56 | |
| 57 | GrRadialGradient::~GrRadialGradient() { |
| 58 | |
| 59 | } |
| 60 | |
| 61 | |
| 62 | const GrProgramStageFactory& GrRadialGradient::getFactory() const { |
| 63 | return GrTProgramStageFactory<GrRadialGradient>::getInstance(); |
| 64 | } |
| 65 | |
| 66 | bool GrRadialGradient::isEqual(const GrCustomStage& sBase) const { |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 67 | return INHERITED::isEqual(sBase); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | ///////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | class GrGLRadial2Gradient : public GrGLProgramStage { |
| 73 | |
| 74 | public: |
| 75 | |
| 76 | GrGLRadial2Gradient(const GrProgramStageFactory& factory, |
| 77 | const GrCustomStage&); |
| 78 | virtual ~GrGLRadial2Gradient() { } |
| 79 | |
| 80 | virtual void setupVariables(GrGLShaderBuilder* state, |
| 81 | int stage) SK_OVERRIDE; |
| 82 | virtual void emitVS(GrGLShaderBuilder* state, |
| 83 | const char* vertexCoords) SK_OVERRIDE; |
| 84 | virtual void emitFS(GrGLShaderBuilder* state, |
| 85 | const char* outputColor, |
| 86 | const char* inputColor, |
| 87 | const char* samplerName) SK_OVERRIDE; |
| 88 | virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE; |
| 89 | virtual void setData(const GrGLInterface*, |
| tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 90 | const GrCustomStage&, |
| senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 91 | const GrRenderTarget*, |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 92 | int stageNum) SK_OVERRIDE; |
| 93 | |
| 94 | static StageKey GenKey(const GrCustomStage& s) { |
| 95 | return (static_cast<const GrRadial2Gradient&>(s).isDegenerate()); |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | |
| tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 100 | const GrGLShaderVar* fVSParamVar; |
| 101 | GrGLint fVSParamLocation; |
| 102 | const GrGLShaderVar* fFSParamVar; |
| 103 | GrGLint fFSParamLocation; |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 104 | |
| 105 | const char* fVSVaryingName; |
| 106 | const char* fFSVaryingName; |
| 107 | |
| 108 | bool fIsDegenerate; |
| 109 | |
| 110 | // @{ |
| 111 | /// Values last uploaded as uniforms |
| 112 | |
| 113 | GrScalar fCachedCenter; |
| 114 | GrScalar fCachedRadius; |
| bsalomon@google.com | 0b32331 | 2012-06-01 18:50:01 +0000 | [diff] [blame] | 115 | bool fCachedPosRoot; |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 116 | |
| 117 | // @} |
| 118 | |
| 119 | private: |
| 120 | |
| 121 | typedef GrGLProgramStage INHERITED; |
| 122 | |
| 123 | }; |
| 124 | |
| 125 | GrGLRadial2Gradient::GrGLRadial2Gradient( |
| 126 | const GrProgramStageFactory& factory, |
| 127 | const GrCustomStage& baseData) |
| 128 | : INHERITED(factory) |
| tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 129 | , fVSParamVar(NULL) |
| 130 | , fFSParamVar(NULL) |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 131 | , fVSVaryingName(NULL) |
| 132 | , fFSVaryingName(NULL) |
| 133 | , fCachedCenter(GR_ScalarMax) |
| 134 | , fCachedRadius(-GR_ScalarMax) |
| 135 | , fCachedPosRoot(0) { |
| 136 | |
| 137 | const GrRadial2Gradient& data = |
| 138 | static_cast<const GrRadial2Gradient&>(baseData); |
| 139 | fIsDegenerate = data.isDegenerate(); |
| 140 | } |
| 141 | |
| 142 | void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) { |
| tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 143 | // 2 copies of uniform array, 1 for each of vertex & fragment shader, |
| 144 | // to work around Xoom bug. Doesn't seem to cause performance decrease |
| 145 | // in test apps, but need to keep an eye on it. |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 146 | fVSParamVar = &state->addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 147 | kFloat_GrSLType, "uRadial2VSParams", stage, 6); |
| 148 | fFSParamVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 149 | kFloat_GrSLType, "uRadial2FSParams", stage, 6); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 150 | |
| tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 151 | fVSParamLocation = GrGLProgramStage::kUseUniform; |
| 152 | fFSParamLocation = GrGLProgramStage::kUseUniform; |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 153 | |
| 154 | // For radial gradients without perspective we can pass the linear |
| 155 | // part of the quadratic as a varying. |
| 156 | if (state->fVaryingDims == state->fCoordDims) { |
| 157 | state->addVarying(kFloat_GrSLType, "Radial2BCoeff", stage, |
| 158 | &fVSVaryingName, &fFSVaryingName); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* state, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 163 | const char* vertexCoords) { |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 164 | SkString* code = &state->fVSCode; |
| 165 | SkString p2; |
| 166 | SkString p3; |
| tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 167 | fVSParamVar->appendArrayAccess(2, &p2); |
| 168 | fVSParamVar->appendArrayAccess(3, &p3); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 169 | |
| 170 | // For radial gradients without perspective we can pass the linear |
| 171 | // part of the quadratic as a varying. |
| 172 | if (state->fVaryingDims == state->fCoordDims) { |
| 173 | // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3]) |
| 174 | code->appendf("\t%s = 2.0 *(%s * %s.x - %s);\n", |
| 175 | fVSVaryingName, p2.c_str(), |
| 176 | vertexCoords, p3.c_str()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void GrGLRadial2Gradient::emitFS(GrGLShaderBuilder* state, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 181 | const char* outputColor, |
| 182 | const char* inputColor, |
| 183 | const char* samplerName) { |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 184 | SkString* code = &state->fFSCode; |
| 185 | SkString cName("c"); |
| 186 | SkString ac4Name("ac4"); |
| 187 | SkString rootName("root"); |
| 188 | SkString p0; |
| 189 | SkString p1; |
| 190 | SkString p2; |
| 191 | SkString p3; |
| 192 | SkString p4; |
| 193 | SkString p5; |
| tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 194 | fFSParamVar->appendArrayAccess(0, &p0); |
| 195 | fFSParamVar->appendArrayAccess(1, &p1); |
| 196 | fFSParamVar->appendArrayAccess(2, &p2); |
| 197 | fFSParamVar->appendArrayAccess(3, &p3); |
| 198 | fFSParamVar->appendArrayAccess(4, &p4); |
| 199 | fFSParamVar->appendArrayAccess(5, &p5); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 200 | |
| 201 | // If we we're able to interpolate the linear component, |
| 202 | // bVar is the varying; otherwise compute it |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 203 | SkString bVar; |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 204 | if (state->fCoordDims == state->fVaryingDims) { |
| 205 | bVar = fFSVaryingName; |
| 206 | GrAssert(2 == state->fVaryingDims); |
| 207 | } else { |
| 208 | GrAssert(3 == state->fVaryingDims); |
| 209 | bVar = "b"; |
| 210 | //bVar.appendS32(stageNum); |
| 211 | code->appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n", |
| 212 | bVar.c_str(), p2.c_str(), |
| 213 | state->fSampleCoords.c_str(), p3.c_str()); |
| 214 | } |
| 215 | |
| 216 | // c = (x^2)+(y^2) - params[4] |
| 217 | code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", |
| 218 | cName.c_str(), state->fSampleCoords.c_str(), |
| 219 | state->fSampleCoords.c_str(), |
| 220 | p4.c_str()); |
| 221 | |
| 222 | // If we aren't degenerate, emit some extra code, and accept a slightly |
| 223 | // more complex coord. |
| tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 224 | if (!fIsDegenerate) { |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 225 | |
| 226 | // ac4 = 4.0 * params[0] * c |
| 227 | code->appendf("\tfloat %s = %s * 4.0 * %s;\n", |
| 228 | ac4Name.c_str(), p0.c_str(), |
| 229 | cName.c_str()); |
| 230 | |
| 231 | // root = sqrt(b^2-4ac) |
| 232 | // (abs to avoid exception due to fp precision) |
| 233 | code->appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n", |
| 234 | rootName.c_str(), bVar.c_str(), bVar.c_str(), |
| 235 | ac4Name.c_str()); |
| 236 | |
| 237 | // x coord is: (-b + params[5] * sqrt(b^2-4ac)) * params[1] |
| 238 | // y coord is 0.5 (texture is effectively 1D) |
| 239 | state->fSampleCoords.printf("vec2((-%s + %s * %s) * %s, 0.5)", |
| 240 | bVar.c_str(), p5.c_str(), |
| 241 | rootName.c_str(), p1.c_str()); |
| 242 | } else { |
| 243 | // x coord is: -c/b |
| 244 | // y coord is 0.5 (texture is effectively 1D) |
| 245 | state->fSampleCoords.printf("vec2((-%s / %s), 0.5)", |
| 246 | cName.c_str(), bVar.c_str()); |
| 247 | } |
| 248 | state->fComplexCoord = true; |
| 249 | |
| 250 | state->emitDefaultFetch(outputColor, samplerName); |
| 251 | } |
| 252 | |
| 253 | void GrGLRadial2Gradient::initUniforms(const GrGLInterface* gl, int programID) { |
| tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 254 | GR_GL_CALL_RET(gl, fVSParamLocation, |
| 255 | GetUniformLocation(programID, fVSParamVar->getName().c_str())); |
| 256 | GR_GL_CALL_RET(gl, fFSParamLocation, |
| 257 | GetUniformLocation(programID, fFSParamVar->getName().c_str())); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void GrGLRadial2Gradient::setData(const GrGLInterface* gl, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 261 | const GrCustomStage& baseData, |
| senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 262 | const GrRenderTarget*, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 263 | int stageNum) { |
| tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 264 | const GrRadial2Gradient& data = |
| 265 | static_cast<const GrRadial2Gradient&>(baseData); |
| 266 | GrAssert(data.isDegenerate() == fIsDegenerate); |
| 267 | GrScalar centerX1 = data.center(); |
| 268 | GrScalar radius0 = data.radius(); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 269 | if (fCachedCenter != centerX1 || |
| 270 | fCachedRadius != radius0 || |
| tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 271 | fCachedPosRoot != data.isPosRoot()) { |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 272 | |
| 273 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
| 274 | |
| 275 | // When we're in the degenerate (linear) case, the second |
| 276 | // value will be INF but the program doesn't read it. (We |
| 277 | // use the same 6 uniforms even though we don't need them |
| 278 | // all in the linear case just to keep the code complexity |
| 279 | // down). |
| 280 | float values[6] = { |
| 281 | GrScalarToFloat(a), |
| 282 | 1 / (2.f * GrScalarToFloat(a)), |
| 283 | GrScalarToFloat(centerX1), |
| 284 | GrScalarToFloat(radius0), |
| 285 | GrScalarToFloat(GrMul(radius0, radius0)), |
| tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 286 | data.isPosRoot() ? 1.f : -1.f |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 287 | }; |
| 288 | |
| tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 289 | GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values)); |
| 290 | GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values)); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 291 | fCachedCenter = centerX1; |
| 292 | fCachedRadius = radius0; |
| tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 293 | fCachedPosRoot = data.isPosRoot(); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | |
| 298 | ///////////////////////////////////////////////////////////////////// |
| 299 | |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 300 | GrRadial2Gradient::GrRadial2Gradient(GrTexture* texture, |
| 301 | GrScalar center, |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 302 | GrScalar radius, |
| 303 | bool posRoot) |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 304 | : GrSingleTextureEffect(texture) |
| 305 | , fCenterX1 (center) |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 306 | , fRadius0 (radius) |
| 307 | , fPosRoot (posRoot) { |
| 308 | |
| 309 | } |
| 310 | |
| 311 | GrRadial2Gradient::~GrRadial2Gradient() { |
| 312 | |
| 313 | } |
| 314 | |
| 315 | |
| 316 | const GrProgramStageFactory& GrRadial2Gradient::getFactory() const { |
| 317 | return GrTProgramStageFactory<GrRadial2Gradient>::getInstance(); |
| 318 | } |
| 319 | |
| 320 | bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const { |
| 321 | const GrRadial2Gradient& s = static_cast<const GrRadial2Gradient&>(sBase); |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 322 | return (INHERITED::isEqual(sBase) && |
| 323 | this->fCenterX1 == s.fCenterX1 && |
| tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 324 | this->fRadius0 == s.fRadius0 && |
| 325 | this->fPosRoot == s.fPosRoot); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | ///////////////////////////////////////////////////////////////////// |
| 329 | |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 330 | class GrGLConical2Gradient : public GrGLProgramStage { |
| 331 | |
| 332 | public: |
| 333 | |
| 334 | GrGLConical2Gradient(const GrProgramStageFactory& factory, |
| 335 | const GrCustomStage&); |
| 336 | virtual ~GrGLConical2Gradient() { } |
| 337 | |
| 338 | virtual void setupVariables(GrGLShaderBuilder* state, |
| 339 | int stage) SK_OVERRIDE; |
| 340 | virtual void emitVS(GrGLShaderBuilder* state, |
| 341 | const char* vertexCoords) SK_OVERRIDE; |
| 342 | virtual void emitFS(GrGLShaderBuilder* state, |
| 343 | const char* outputColor, |
| 344 | const char* inputColor, |
| 345 | const char* samplerName) SK_OVERRIDE; |
| 346 | virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE; |
| 347 | virtual void setData(const GrGLInterface*, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 348 | const GrCustomStage&, |
| senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 349 | const GrRenderTarget*, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 350 | int stageNum) SK_OVERRIDE; |
| 351 | |
| 352 | static StageKey GenKey(const GrCustomStage& s) { |
| 353 | return (static_cast<const GrConical2Gradient&>(s).isDegenerate()); |
| 354 | } |
| 355 | |
| 356 | protected: |
| 357 | |
| 358 | const GrGLShaderVar* fVSParamVar; |
| 359 | GrGLint fVSParamLocation; |
| 360 | const GrGLShaderVar* fFSParamVar; |
| 361 | GrGLint fFSParamLocation; |
| 362 | |
| 363 | const char* fVSVaryingName; |
| 364 | const char* fFSVaryingName; |
| 365 | |
| 366 | bool fIsDegenerate; |
| 367 | |
| 368 | // @{ |
| 369 | /// Values last uploaded as uniforms |
| 370 | |
| 371 | GrScalar fCachedCenter; |
| 372 | GrScalar fCachedRadius; |
| 373 | GrScalar fCachedDiffRadius; |
| 374 | |
| 375 | // @} |
| 376 | |
| 377 | private: |
| 378 | |
| 379 | typedef GrGLProgramStage INHERITED; |
| 380 | |
| 381 | }; |
| 382 | |
| 383 | GrGLConical2Gradient::GrGLConical2Gradient( |
| 384 | const GrProgramStageFactory& factory, |
| 385 | const GrCustomStage& baseData) |
| 386 | : INHERITED(factory) |
| 387 | , fVSParamVar(NULL) |
| 388 | , fFSParamVar(NULL) |
| 389 | , fVSVaryingName(NULL) |
| 390 | , fFSVaryingName(NULL) |
| 391 | , fCachedCenter(GR_ScalarMax) |
| 392 | , fCachedRadius(-GR_ScalarMax) |
| 393 | , fCachedDiffRadius(-GR_ScalarMax) { |
| 394 | |
| 395 | const GrConical2Gradient& data = |
| 396 | static_cast<const GrConical2Gradient&>(baseData); |
| 397 | fIsDegenerate = data.isDegenerate(); |
| 398 | } |
| 399 | |
| 400 | void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) { |
| 401 | // 2 copies of uniform array, 1 for each of vertex & fragment shader, |
| 402 | // to work around Xoom bug. Doesn't seem to cause performance decrease |
| 403 | // in test apps, but need to keep an eye on it. |
| bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 404 | fVSParamVar = &state->addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 405 | kFloat_GrSLType, "uConical2VSParams", stage, 6); |
| 406 | fFSParamVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 407 | kFloat_GrSLType, "uConical2FSParams", stage, 6); |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 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, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 578 | const GrCustomStage& baseData, |
| senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 579 | const GrRenderTarget*, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 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 | |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 619 | GrConical2Gradient::GrConical2Gradient(GrTexture* texture, |
| 620 | GrScalar center, |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 621 | GrScalar radius, |
| 622 | GrScalar diffRadius) |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 623 | : GrSingleTextureEffect (texture) |
| 624 | , fCenterX1 (center) |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 625 | , fRadius0 (radius) |
| 626 | , fDiffRadius (diffRadius) { |
| 627 | |
| 628 | } |
| 629 | |
| 630 | GrConical2Gradient::~GrConical2Gradient() { |
| 631 | |
| 632 | } |
| 633 | |
| 634 | |
| 635 | const GrProgramStageFactory& GrConical2Gradient::getFactory() const { |
| 636 | return GrTProgramStageFactory<GrConical2Gradient>::getInstance(); |
| 637 | } |
| 638 | |
| 639 | bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const { |
| 640 | const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase); |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 641 | return (INHERITED::isEqual(sBase) && |
| 642 | this->fCenterX1 == s.fCenterX1 && |
| tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 643 | this->fRadius0 == s.fRadius0 && |
| 644 | this->fDiffRadius == s.fDiffRadius); |
| rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | ///////////////////////////////////////////////////////////////////// |
| 648 | |
| 649 | |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 650 | class GrGLSweepGradient : public GrGLProgramStage { |
| 651 | |
| 652 | public: |
| 653 | |
| 654 | GrGLSweepGradient(const GrProgramStageFactory& factory, |
| 655 | const GrCustomStage&) : INHERITED (factory) { } |
| 656 | virtual ~GrGLSweepGradient() { } |
| 657 | |
| 658 | virtual void emitVS(GrGLShaderBuilder* state, |
| 659 | const char* vertexCoords) SK_OVERRIDE { } |
| 660 | virtual void emitFS(GrGLShaderBuilder* state, |
| 661 | const char* outputColor, |
| 662 | const char* inputColor, |
| 663 | const char* samplerName) SK_OVERRIDE; |
| 664 | |
| 665 | static StageKey GenKey(const GrCustomStage& s) { return 0; } |
| 666 | |
| 667 | private: |
| 668 | |
| 669 | typedef GrGLProgramStage INHERITED; |
| 670 | |
| 671 | }; |
| 672 | |
| 673 | void GrGLSweepGradient::emitFS(GrGLShaderBuilder* state, |
| 674 | const char* outputColor, |
| 675 | const char* inputColor, |
| 676 | const char* samplerName) { |
| 677 | state->fSampleCoords.printf( |
| 678 | "vec2(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5, 0.5)", |
| 679 | state->fSampleCoords.c_str(), state->fSampleCoords.c_str()); |
| 680 | state->fComplexCoord = true; |
| 681 | |
| 682 | state->emitDefaultFetch(outputColor, samplerName); |
| 683 | } |
| 684 | |
| 685 | ///////////////////////////////////////////////////////////////////// |
| 686 | |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 687 | GrSweepGradient::GrSweepGradient(GrTexture* texture) |
| 688 | : GrSingleTextureEffect(texture) { |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 689 | |
| 690 | } |
| 691 | |
| 692 | GrSweepGradient::~GrSweepGradient() { |
| 693 | |
| 694 | } |
| 695 | |
| 696 | const GrProgramStageFactory& GrSweepGradient::getFactory() const { |
| 697 | return GrTProgramStageFactory<GrSweepGradient>::getInstance(); |
| 698 | } |
| 699 | |
| 700 | bool GrSweepGradient::isEqual(const GrCustomStage& sBase) const { |
| tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 701 | return INHERITED::isEqual(sBase); |
| tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 702 | } |
| 703 | |