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