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&, |
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 GrCustomStage& baseData, |
| 261 | int stageNum) { |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 262 | const GrRadial2Gradient& data = |
| 263 | static_cast<const GrRadial2Gradient&>(baseData); |
| 264 | GrAssert(data.isDegenerate() == fIsDegenerate); |
| 265 | GrScalar centerX1 = data.center(); |
| 266 | GrScalar radius0 = data.radius(); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 267 | if (fCachedCenter != centerX1 || |
| 268 | fCachedRadius != radius0 || |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 269 | fCachedPosRoot != data.isPosRoot()) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 270 | |
| 271 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
| 272 | |
| 273 | // When we're in the degenerate (linear) case, the second |
| 274 | // value will be INF but the program doesn't read it. (We |
| 275 | // use the same 6 uniforms even though we don't need them |
| 276 | // all in the linear case just to keep the code complexity |
| 277 | // down). |
| 278 | float values[6] = { |
| 279 | GrScalarToFloat(a), |
| 280 | 1 / (2.f * GrScalarToFloat(a)), |
| 281 | GrScalarToFloat(centerX1), |
| 282 | GrScalarToFloat(radius0), |
| 283 | GrScalarToFloat(GrMul(radius0, radius0)), |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 284 | data.isPosRoot() ? 1.f : -1.f |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 285 | }; |
| 286 | |
tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 287 | GR_GL_CALL(gl, Uniform1fv(fVSParamLocation, 6, values)); |
| 288 | GR_GL_CALL(gl, Uniform1fv(fFSParamLocation, 6, values)); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 289 | fCachedCenter = centerX1; |
| 290 | fCachedRadius = radius0; |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 291 | fCachedPosRoot = data.isPosRoot(); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | ///////////////////////////////////////////////////////////////////// |
| 297 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 298 | GrRadial2Gradient::GrRadial2Gradient(GrTexture* texture, |
| 299 | GrScalar center, |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 300 | GrScalar radius, |
| 301 | bool posRoot) |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 302 | : GrSingleTextureEffect(texture) |
| 303 | , fCenterX1 (center) |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 304 | , fRadius0 (radius) |
| 305 | , fPosRoot (posRoot) { |
| 306 | |
| 307 | } |
| 308 | |
| 309 | GrRadial2Gradient::~GrRadial2Gradient() { |
| 310 | |
| 311 | } |
| 312 | |
| 313 | |
| 314 | const GrProgramStageFactory& GrRadial2Gradient::getFactory() const { |
| 315 | return GrTProgramStageFactory<GrRadial2Gradient>::getInstance(); |
| 316 | } |
| 317 | |
| 318 | bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const { |
| 319 | const GrRadial2Gradient& s = static_cast<const GrRadial2Gradient&>(sBase); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 320 | return (INHERITED::isEqual(sBase) && |
| 321 | this->fCenterX1 == s.fCenterX1 && |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 322 | this->fRadius0 == s.fRadius0 && |
| 323 | this->fPosRoot == s.fPosRoot); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | ///////////////////////////////////////////////////////////////////// |
| 327 | |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 328 | class GrGLConical2Gradient : public GrGLProgramStage { |
| 329 | |
| 330 | public: |
| 331 | |
| 332 | GrGLConical2Gradient(const GrProgramStageFactory& factory, |
| 333 | const GrCustomStage&); |
| 334 | virtual ~GrGLConical2Gradient() { } |
| 335 | |
| 336 | virtual void setupVariables(GrGLShaderBuilder* state, |
| 337 | int stage) SK_OVERRIDE; |
| 338 | virtual void emitVS(GrGLShaderBuilder* state, |
| 339 | const char* vertexCoords) SK_OVERRIDE; |
| 340 | virtual void emitFS(GrGLShaderBuilder* state, |
| 341 | const char* outputColor, |
| 342 | const char* inputColor, |
| 343 | const char* samplerName) SK_OVERRIDE; |
| 344 | virtual void initUniforms(const GrGLInterface*, int programID) SK_OVERRIDE; |
| 345 | virtual void setData(const GrGLInterface*, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 346 | const GrCustomStage&, |
| 347 | int stageNum) SK_OVERRIDE; |
| 348 | |
| 349 | static StageKey GenKey(const GrCustomStage& s) { |
| 350 | return (static_cast<const GrConical2Gradient&>(s).isDegenerate()); |
| 351 | } |
| 352 | |
| 353 | protected: |
| 354 | |
| 355 | const GrGLShaderVar* fVSParamVar; |
| 356 | GrGLint fVSParamLocation; |
| 357 | const GrGLShaderVar* fFSParamVar; |
| 358 | GrGLint fFSParamLocation; |
| 359 | |
| 360 | const char* fVSVaryingName; |
| 361 | const char* fFSVaryingName; |
| 362 | |
| 363 | bool fIsDegenerate; |
| 364 | |
| 365 | // @{ |
| 366 | /// Values last uploaded as uniforms |
| 367 | |
| 368 | GrScalar fCachedCenter; |
| 369 | GrScalar fCachedRadius; |
| 370 | GrScalar fCachedDiffRadius; |
| 371 | |
| 372 | // @} |
| 373 | |
| 374 | private: |
| 375 | |
| 376 | typedef GrGLProgramStage INHERITED; |
| 377 | |
| 378 | }; |
| 379 | |
| 380 | GrGLConical2Gradient::GrGLConical2Gradient( |
| 381 | const GrProgramStageFactory& factory, |
| 382 | const GrCustomStage& baseData) |
| 383 | : INHERITED(factory) |
| 384 | , fVSParamVar(NULL) |
| 385 | , fFSParamVar(NULL) |
| 386 | , fVSVaryingName(NULL) |
| 387 | , fFSVaryingName(NULL) |
| 388 | , fCachedCenter(GR_ScalarMax) |
| 389 | , fCachedRadius(-GR_ScalarMax) |
| 390 | , fCachedDiffRadius(-GR_ScalarMax) { |
| 391 | |
| 392 | const GrConical2Gradient& data = |
| 393 | static_cast<const GrConical2Gradient&>(baseData); |
| 394 | fIsDegenerate = data.isDegenerate(); |
| 395 | } |
| 396 | |
| 397 | void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* state, int stage) { |
| 398 | // 2 copies of uniform array, 1 for each of vertex & fragment shader, |
| 399 | // to work around Xoom bug. Doesn't seem to cause performance decrease |
| 400 | // in test apps, but need to keep an eye on it. |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 401 | fVSParamVar = &state->addUniform(GrGLShaderBuilder::kVertex_ShaderType, |
| 402 | kFloat_GrSLType, "uConical2VSParams", stage, 6); |
| 403 | fFSParamVar = &state->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 404 | kFloat_GrSLType, "uConical2FSParams", stage, 6); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 405 | |
| 406 | fVSParamLocation = GrGLProgramStage::kUseUniform; |
| 407 | fFSParamLocation = GrGLProgramStage::kUseUniform; |
| 408 | |
| 409 | // For radial gradients without perspective we can pass the linear |
| 410 | // part of the quadratic as a varying. |
| 411 | if (state->fVaryingDims == state->fCoordDims) { |
| 412 | state->addVarying(kFloat_GrSLType, "Conical2BCoeff", stage, |
| 413 | &fVSVaryingName, &fFSVaryingName); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* state, |
| 418 | const char* vertexCoords) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 419 | SkString* code = &state->fVSCode; |
| 420 | SkString p2; // distance between centers |
| 421 | SkString p3; // start radius |
| 422 | SkString p5; // difference in radii (r1 - r0) |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 423 | fVSParamVar->appendArrayAccess(2, &p2); |
| 424 | fVSParamVar->appendArrayAccess(3, &p3); |
| 425 | fVSParamVar->appendArrayAccess(5, &p5); |
| 426 | |
| 427 | // For radial gradients without perspective we can pass the linear |
| 428 | // part of the quadratic as a varying. |
| 429 | if (state->fVaryingDims == state->fCoordDims) { |
| 430 | // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5]) |
| 431 | code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n", |
| 432 | fVSVaryingName, p2.c_str(), |
| 433 | vertexCoords, p3.c_str(), p5.c_str()); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* state, |
| 438 | const char* outputColor, |
| 439 | const char* inputColor, |
| 440 | const char* samplerName) { |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 441 | SkString* code = &state->fFSCode; |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 442 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 443 | SkString cName("c"); |
| 444 | SkString ac4Name("ac4"); |
| 445 | SkString dName("d"); |
| 446 | SkString qName("q"); |
| 447 | SkString r0Name("r0"); |
| 448 | SkString r1Name("r1"); |
| 449 | SkString tName("t"); |
| 450 | SkString p0; // 4a |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 451 | SkString p1; // 1/a |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 452 | SkString p2; // distance between centers |
| 453 | SkString p3; // start radius |
| 454 | SkString p4; // start radius squared |
| 455 | SkString p5; // difference in radii (r1 - r0) |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 456 | fFSParamVar->appendArrayAccess(0, &p0); |
| 457 | fFSParamVar->appendArrayAccess(1, &p1); |
| 458 | fFSParamVar->appendArrayAccess(2, &p2); |
| 459 | fFSParamVar->appendArrayAccess(3, &p3); |
| 460 | fFSParamVar->appendArrayAccess(4, &p4); |
| 461 | fFSParamVar->appendArrayAccess(5, &p5); |
| 462 | |
| 463 | // If we we're able to interpolate the linear component, |
| 464 | // bVar is the varying; otherwise compute it |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 465 | SkString bVar; |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 466 | if (state->fCoordDims == state->fVaryingDims) { |
| 467 | bVar = fFSVaryingName; |
| 468 | GrAssert(2 == state->fVaryingDims); |
| 469 | } else { |
| 470 | GrAssert(3 == state->fVaryingDims); |
| 471 | bVar = "b"; |
| 472 | code->appendf("\tfloat %s = -2.0 * (%s * %s.x + %s * %s);\n", |
| 473 | bVar.c_str(), p2.c_str(), state->fSampleCoords.c_str(), |
| 474 | p3.c_str(), p5.c_str()); |
| 475 | } |
| 476 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 477 | // output will default to transparent black (we simply won't write anything |
| 478 | // else to it if invalid, instead of discarding or returning prematurely) |
| 479 | code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor); |
| 480 | |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 481 | // c = (x^2)+(y^2) - params[4] |
| 482 | code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", cName.c_str(), |
| 483 | state->fSampleCoords.c_str(), state->fSampleCoords.c_str(), |
| 484 | p4.c_str()); |
| 485 | |
| 486 | // Non-degenerate case (quadratic) |
| 487 | if (!fIsDegenerate) { |
| 488 | |
| 489 | // ac4 = params[0] * c |
| 490 | code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(), |
| 491 | cName.c_str()); |
| 492 | |
| 493 | // d = b^2 - ac4 |
| 494 | code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(), |
| 495 | bVar.c_str(), bVar.c_str(), ac4Name.c_str()); |
| 496 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 497 | // only proceed if discriminant is >= 0 |
| 498 | code->appendf("\tif (%s >= 0.0) {\n", dName.c_str()); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 499 | |
| 500 | // intermediate value we'll use to compute the roots |
| 501 | // q = -0.5 * (b +/- sqrt(d)) |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 502 | 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] | 503 | " * sqrt(%s));\n", qName.c_str(), bVar.c_str(), |
| 504 | bVar.c_str(), dName.c_str()); |
| 505 | |
| 506 | // compute both roots |
| 507 | // r0 = q * params[1] |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 508 | code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(), |
| 509 | qName.c_str(), p1.c_str()); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 510 | // r1 = c / q |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 511 | code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(), |
| 512 | cName.c_str(), qName.c_str()); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 513 | |
| 514 | // Note: If there are two roots that both generate radius(t) > 0, the |
| 515 | // Canvas spec says to choose the larger t. |
| 516 | |
| 517 | // so we'll look at the larger one first: |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 518 | 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] | 519 | r0Name.c_str(), r1Name.c_str()); |
| 520 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 521 | // if r(t) > 0, then we're done; t will be our x coordinate |
| 522 | 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] | 523 | p5.c_str(), p3.c_str()); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 524 | |
| 525 | // y coord is 0.5 (texture is effectively 1D) |
| 526 | code->appendf("\t\t"); |
| 527 | state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str()); |
| 528 | state->emitDefaultFetch(outputColor, samplerName); |
| 529 | |
| 530 | // otherwise, if r(t) for the larger root was <= 0, try the other root |
| 531 | code->appendf("\t\t} else {\n"); |
| 532 | 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] | 533 | r0Name.c_str(), r1Name.c_str()); |
| 534 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 535 | // if r(t) > 0 for the smaller root, then t will be our x coordinate |
| 536 | code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n", |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 537 | tName.c_str(), p5.c_str(), p3.c_str()); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 538 | |
| 539 | // y coord is 0.5 (texture is effectively 1D) |
| 540 | code->appendf("\t\t\t"); |
| 541 | state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str()); |
| 542 | state->emitDefaultFetch(outputColor, samplerName); |
| 543 | |
| 544 | // end if (r(t) > 0) for smaller root |
| 545 | code->appendf("\t\t\t}\n"); |
| 546 | // end if (r(t) > 0), else, for larger root |
| 547 | code->appendf("\t\t}\n"); |
| 548 | // end if (discriminant >= 0) |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 549 | code->appendf("\t}\n"); |
| 550 | } else { |
| 551 | |
| 552 | // linear case: t = -c/b |
| 553 | code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(), |
| 554 | cName.c_str(), bVar.c_str()); |
| 555 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 556 | // if r(t) > 0, then t will be the x coordinate |
| 557 | 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] | 558 | p5.c_str(), p3.c_str()); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 559 | code->appendf("\t"); |
| 560 | state->fSampleCoords.printf("vec2(%s, 0.5)", tName.c_str()); |
| 561 | state->emitDefaultFetch(outputColor, samplerName); |
| 562 | code->appendf("\t}\n"); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 563 | } |
| 564 | state->fComplexCoord = true; |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void GrGLConical2Gradient::initUniforms(const GrGLInterface* gl, int programID) { |
| 568 | GR_GL_CALL_RET(gl, fVSParamLocation, |
| 569 | GetUniformLocation(programID, fVSParamVar->getName().c_str())); |
| 570 | GR_GL_CALL_RET(gl, fFSParamLocation, |
| 571 | GetUniformLocation(programID, fFSParamVar->getName().c_str())); |
| 572 | } |
| 573 | |
| 574 | void GrGLConical2Gradient::setData(const GrGLInterface* gl, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 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 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 615 | GrConical2Gradient::GrConical2Gradient(GrTexture* texture, |
| 616 | GrScalar center, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 617 | GrScalar radius, |
| 618 | GrScalar diffRadius) |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 619 | : GrSingleTextureEffect (texture) |
| 620 | , fCenterX1 (center) |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 621 | , fRadius0 (radius) |
| 622 | , fDiffRadius (diffRadius) { |
| 623 | |
| 624 | } |
| 625 | |
| 626 | GrConical2Gradient::~GrConical2Gradient() { |
| 627 | |
| 628 | } |
| 629 | |
| 630 | |
| 631 | const GrProgramStageFactory& GrConical2Gradient::getFactory() const { |
| 632 | return GrTProgramStageFactory<GrConical2Gradient>::getInstance(); |
| 633 | } |
| 634 | |
| 635 | bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const { |
| 636 | const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 637 | return (INHERITED::isEqual(sBase) && |
| 638 | this->fCenterX1 == s.fCenterX1 && |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 639 | this->fRadius0 == s.fRadius0 && |
| 640 | this->fDiffRadius == s.fDiffRadius); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | ///////////////////////////////////////////////////////////////////// |
| 644 | |
| 645 | |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 646 | class GrGLSweepGradient : public GrGLProgramStage { |
| 647 | |
| 648 | public: |
| 649 | |
| 650 | GrGLSweepGradient(const GrProgramStageFactory& factory, |
| 651 | const GrCustomStage&) : INHERITED (factory) { } |
| 652 | virtual ~GrGLSweepGradient() { } |
| 653 | |
| 654 | virtual void emitVS(GrGLShaderBuilder* state, |
| 655 | const char* vertexCoords) SK_OVERRIDE { } |
| 656 | virtual void emitFS(GrGLShaderBuilder* state, |
| 657 | const char* outputColor, |
| 658 | const char* inputColor, |
| 659 | const char* samplerName) SK_OVERRIDE; |
| 660 | |
| 661 | static StageKey GenKey(const GrCustomStage& s) { return 0; } |
| 662 | |
| 663 | private: |
| 664 | |
| 665 | typedef GrGLProgramStage INHERITED; |
| 666 | |
| 667 | }; |
| 668 | |
| 669 | void GrGLSweepGradient::emitFS(GrGLShaderBuilder* state, |
| 670 | const char* outputColor, |
| 671 | const char* inputColor, |
| 672 | const char* samplerName) { |
| 673 | state->fSampleCoords.printf( |
| 674 | "vec2(atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5, 0.5)", |
| 675 | state->fSampleCoords.c_str(), state->fSampleCoords.c_str()); |
| 676 | state->fComplexCoord = true; |
| 677 | |
| 678 | state->emitDefaultFetch(outputColor, samplerName); |
| 679 | } |
| 680 | |
| 681 | ///////////////////////////////////////////////////////////////////// |
| 682 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 683 | GrSweepGradient::GrSweepGradient(GrTexture* texture) |
| 684 | : GrSingleTextureEffect(texture) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 685 | |
| 686 | } |
| 687 | |
| 688 | GrSweepGradient::~GrSweepGradient() { |
| 689 | |
| 690 | } |
| 691 | |
| 692 | const GrProgramStageFactory& GrSweepGradient::getFactory() const { |
| 693 | return GrTProgramStageFactory<GrSweepGradient>::getInstance(); |
| 694 | } |
| 695 | |
| 696 | bool GrSweepGradient::isEqual(const GrCustomStage& sBase) const { |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame^] | 697 | return INHERITED::isEqual(sBase); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 698 | } |
| 699 | |