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