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