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 | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 217 | virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 218 | virtual void emitVS(GrGLShaderBuilder* builder, |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 219 | const char* vertexCoords) SK_OVERRIDE; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 220 | virtual void emitFS(GrGLShaderBuilder* builder, |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 221 | const char* outputColor, |
| 222 | const char* inputColor, |
| 223 | const char* samplerName) SK_OVERRIDE; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 224 | virtual void setData(const GrGLUniformManager&, |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 225 | const GrCustomStage&, |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 226 | const GrRenderTarget*, |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 227 | int stageNum) SK_OVERRIDE; |
| 228 | |
| 229 | static StageKey GenKey(const GrCustomStage& s) { |
| 230 | return (static_cast<const GrRadial2Gradient&>(s).isDegenerate()); |
| 231 | } |
| 232 | |
| 233 | protected: |
| 234 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 235 | UniformHandle fVSParamUni; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 236 | UniformHandle fFSParamUni; |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 237 | |
| 238 | const char* fVSVaryingName; |
| 239 | const char* fFSVaryingName; |
| 240 | |
| 241 | bool fIsDegenerate; |
| 242 | |
| 243 | // @{ |
| 244 | /// Values last uploaded as uniforms |
| 245 | |
| 246 | GrScalar fCachedCenter; |
| 247 | GrScalar fCachedRadius; |
bsalomon@google.com | 0b32331 | 2012-06-01 18:50:01 +0000 | [diff] [blame] | 248 | bool fCachedPosRoot; |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 249 | |
| 250 | // @} |
| 251 | |
| 252 | private: |
| 253 | |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 254 | typedef GrGLGradientStage INHERITED; |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 255 | |
| 256 | }; |
| 257 | |
| 258 | GrGLRadial2Gradient::GrGLRadial2Gradient( |
| 259 | const GrProgramStageFactory& factory, |
| 260 | const GrCustomStage& baseData) |
| 261 | : INHERITED(factory) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 262 | , fVSParamUni(kInvalidUniformHandle) |
| 263 | , fFSParamUni(kInvalidUniformHandle) |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 264 | , fVSVaryingName(NULL) |
| 265 | , fFSVaryingName(NULL) |
| 266 | , fCachedCenter(GR_ScalarMax) |
| 267 | , fCachedRadius(-GR_ScalarMax) |
| 268 | , fCachedPosRoot(0) { |
| 269 | |
| 270 | const GrRadial2Gradient& data = |
| 271 | static_cast<const GrRadial2Gradient&>(baseData); |
| 272 | fIsDegenerate = data.isDegenerate(); |
| 273 | } |
| 274 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 275 | void GrGLRadial2Gradient::setupVariables(GrGLShaderBuilder* builder) { |
tomhudson@google.com | 761b37c | 2012-06-13 15:22:18 +0000 | [diff] [blame] | 276 | // 2 copies of uniform array, 1 for each of vertex & fragment shader, |
| 277 | // to work around Xoom bug. Doesn't seem to cause performance decrease |
| 278 | // in test apps, but need to keep an eye on it. |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 279 | fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType, |
| 280 | kFloat_GrSLType, "Radial2VSParams", 6); |
| 281 | fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType, |
| 282 | kFloat_GrSLType, "Radial2FSParams", 6); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 283 | |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 284 | // For radial gradients without perspective we can pass the linear |
| 285 | // part of the quadratic as a varying. |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 286 | if (builder->fVaryingDims == builder->fCoordDims) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 287 | builder->addVarying(kFloat_GrSLType, "Radial2BCoeff", |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 288 | &fVSVaryingName, &fFSVaryingName); |
| 289 | } |
| 290 | } |
| 291 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 292 | void GrGLRadial2Gradient::emitVS(GrGLShaderBuilder* builder, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 293 | const char* vertexCoords) { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 294 | SkString* code = &builder->fVSCode; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 295 | SkString p2; |
| 296 | SkString p3; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 297 | builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2); |
| 298 | builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 299 | |
| 300 | // For radial gradients without perspective we can pass the linear |
| 301 | // part of the quadratic as a varying. |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 302 | if (builder->fVaryingDims == builder->fCoordDims) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 303 | // r2Var = 2 * (r2Parm[2] * varCoord.x - r2Param[3]) |
| 304 | code->appendf("\t%s = 2.0 *(%s * %s.x - %s);\n", |
| 305 | fVSVaryingName, p2.c_str(), |
| 306 | vertexCoords, p3.c_str()); |
| 307 | } |
| 308 | } |
| 309 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 310 | void GrGLRadial2Gradient::emitFS(GrGLShaderBuilder* builder, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 311 | const char* outputColor, |
| 312 | const char* inputColor, |
| 313 | const char* samplerName) { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 314 | SkString* code = &builder->fFSCode; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 315 | SkString cName("c"); |
| 316 | SkString ac4Name("ac4"); |
| 317 | SkString rootName("root"); |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 318 | SkString t; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 319 | SkString p0; |
| 320 | SkString p1; |
| 321 | SkString p2; |
| 322 | SkString p3; |
| 323 | SkString p4; |
| 324 | SkString p5; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 325 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0); |
| 326 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1); |
| 327 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2); |
| 328 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3); |
| 329 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4); |
| 330 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 331 | |
| 332 | // If we we're able to interpolate the linear component, |
| 333 | // bVar is the varying; otherwise compute it |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 334 | SkString bVar; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 335 | if (builder->fCoordDims == builder->fVaryingDims) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 336 | bVar = fFSVaryingName; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 337 | GrAssert(2 == builder->fVaryingDims); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 338 | } else { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 339 | GrAssert(3 == builder->fVaryingDims); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 340 | bVar = "b"; |
| 341 | //bVar.appendS32(stageNum); |
| 342 | code->appendf("\tfloat %s = 2.0 * (%s * %s.x - %s);\n", |
| 343 | bVar.c_str(), p2.c_str(), |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 344 | builder->fSampleCoords.c_str(), p3.c_str()); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // c = (x^2)+(y^2) - params[4] |
| 348 | code->appendf("\tfloat %s = dot(%s, %s) - %s;\n", |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 349 | cName.c_str(), builder->fSampleCoords.c_str(), |
| 350 | builder->fSampleCoords.c_str(), |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 351 | p4.c_str()); |
| 352 | |
| 353 | // If we aren't degenerate, emit some extra code, and accept a slightly |
| 354 | // more complex coord. |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 355 | if (!fIsDegenerate) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 356 | |
| 357 | // ac4 = 4.0 * params[0] * c |
| 358 | code->appendf("\tfloat %s = %s * 4.0 * %s;\n", |
| 359 | ac4Name.c_str(), p0.c_str(), |
| 360 | cName.c_str()); |
| 361 | |
| 362 | // root = sqrt(b^2-4ac) |
| 363 | // (abs to avoid exception due to fp precision) |
| 364 | code->appendf("\tfloat %s = sqrt(abs(%s*%s - %s));\n", |
| 365 | rootName.c_str(), bVar.c_str(), bVar.c_str(), |
| 366 | ac4Name.c_str()); |
| 367 | |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 368 | // t is: (-b + params[5] * sqrt(b^2-4ac)) * params[1] |
| 369 | t.printf("(-%s + %s * %s) * %s", bVar.c_str(), p5.c_str(), |
| 370 | rootName.c_str(), p1.c_str()); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 371 | } else { |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 372 | // t is: -c/b |
| 373 | t.printf("-%s / %s", cName.c_str(), bVar.c_str()); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 374 | } |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 375 | |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 376 | this->emitColorLookup(builder, t.c_str(), outputColor, samplerName); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 377 | } |
| 378 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 379 | void GrGLRadial2Gradient::setData(const GrGLUniformManager& uman, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 380 | const GrCustomStage& baseData, |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 381 | const GrRenderTarget*, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 382 | int stageNum) { |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 383 | const GrRadial2Gradient& data = |
| 384 | static_cast<const GrRadial2Gradient&>(baseData); |
| 385 | GrAssert(data.isDegenerate() == fIsDegenerate); |
| 386 | GrScalar centerX1 = data.center(); |
| 387 | GrScalar radius0 = data.radius(); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 388 | if (fCachedCenter != centerX1 || |
| 389 | fCachedRadius != radius0 || |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 390 | fCachedPosRoot != data.isPosRoot()) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 391 | |
| 392 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
| 393 | |
| 394 | // When we're in the degenerate (linear) case, the second |
| 395 | // value will be INF but the program doesn't read it. (We |
| 396 | // use the same 6 uniforms even though we don't need them |
| 397 | // all in the linear case just to keep the code complexity |
| 398 | // down). |
| 399 | float values[6] = { |
| 400 | GrScalarToFloat(a), |
| 401 | 1 / (2.f * GrScalarToFloat(a)), |
| 402 | GrScalarToFloat(centerX1), |
| 403 | GrScalarToFloat(radius0), |
| 404 | GrScalarToFloat(GrMul(radius0, radius0)), |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 405 | data.isPosRoot() ? 1.f : -1.f |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 406 | }; |
| 407 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 408 | uman.set1fv(fVSParamUni, 0, 6, values); |
| 409 | uman.set1fv(fFSParamUni, 0, 6, values); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 410 | fCachedCenter = centerX1; |
| 411 | fCachedRadius = radius0; |
tomhudson@google.com | dcdc1fc | 2012-05-31 19:53:37 +0000 | [diff] [blame] | 412 | fCachedPosRoot = data.isPosRoot(); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | |
| 417 | ///////////////////////////////////////////////////////////////////// |
| 418 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 419 | GrRadial2Gradient::GrRadial2Gradient(GrTexture* texture, |
| 420 | GrScalar center, |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 421 | GrScalar radius, |
| 422 | bool posRoot) |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 423 | : INHERITED(texture) |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 424 | , fCenterX1 (center) |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 425 | , fRadius0 (radius) |
| 426 | , fPosRoot (posRoot) { |
| 427 | |
| 428 | } |
| 429 | |
rileya@google.com | 91f319c | 2012-07-25 17:18:31 +0000 | [diff] [blame] | 430 | GrRadial2Gradient::GrRadial2Gradient(GrContext* ctx, |
| 431 | const SkShader& shader, |
| 432 | GrSamplerState* sampler, |
| 433 | SkScalar center, |
| 434 | SkScalar startRadius, |
| 435 | SkScalar diffRadius) |
| 436 | : INHERITED(ctx, shader, sampler) |
| 437 | , fCenterX1(center) |
| 438 | , fRadius0(startRadius) |
| 439 | , fPosRoot(diffRadius < 0) { |
rileya@google.com | 03c1c35 | 2012-07-20 20:02:43 +0000 | [diff] [blame] | 440 | } |
| 441 | |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 442 | GrRadial2Gradient::~GrRadial2Gradient() { |
| 443 | |
| 444 | } |
| 445 | |
| 446 | |
| 447 | const GrProgramStageFactory& GrRadial2Gradient::getFactory() const { |
| 448 | return GrTProgramStageFactory<GrRadial2Gradient>::getInstance(); |
| 449 | } |
| 450 | |
| 451 | bool GrRadial2Gradient::isEqual(const GrCustomStage& sBase) const { |
| 452 | const GrRadial2Gradient& s = static_cast<const GrRadial2Gradient&>(sBase); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 453 | return (INHERITED::isEqual(sBase) && |
| 454 | this->fCenterX1 == s.fCenterX1 && |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 455 | this->fRadius0 == s.fRadius0 && |
| 456 | this->fPosRoot == s.fPosRoot); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | ///////////////////////////////////////////////////////////////////// |
| 460 | |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 461 | class GrGLConical2Gradient : public GrGLGradientStage { |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 462 | |
| 463 | public: |
| 464 | |
| 465 | GrGLConical2Gradient(const GrProgramStageFactory& factory, |
| 466 | const GrCustomStage&); |
| 467 | virtual ~GrGLConical2Gradient() { } |
| 468 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 469 | virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 470 | virtual void emitVS(GrGLShaderBuilder* builder, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 471 | const char* vertexCoords) SK_OVERRIDE; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 472 | virtual void emitFS(GrGLShaderBuilder* builder, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 473 | const char* outputColor, |
| 474 | const char* inputColor, |
| 475 | const char* samplerName) SK_OVERRIDE; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 476 | virtual void setData(const GrGLUniformManager&, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 477 | const GrCustomStage&, |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 478 | const GrRenderTarget*, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 479 | int stageNum) SK_OVERRIDE; |
| 480 | |
| 481 | static StageKey GenKey(const GrCustomStage& s) { |
| 482 | return (static_cast<const GrConical2Gradient&>(s).isDegenerate()); |
| 483 | } |
| 484 | |
| 485 | protected: |
| 486 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 487 | UniformHandle fVSParamUni; |
| 488 | GrGLint fVSParamLocation; |
| 489 | UniformHandle fFSParamUni; |
| 490 | GrGLint fFSParamLocation; |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 491 | |
| 492 | const char* fVSVaryingName; |
| 493 | const char* fFSVaryingName; |
| 494 | |
| 495 | bool fIsDegenerate; |
| 496 | |
| 497 | // @{ |
| 498 | /// Values last uploaded as uniforms |
| 499 | |
| 500 | GrScalar fCachedCenter; |
| 501 | GrScalar fCachedRadius; |
| 502 | GrScalar fCachedDiffRadius; |
| 503 | |
| 504 | // @} |
| 505 | |
| 506 | private: |
| 507 | |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 508 | typedef GrGLGradientStage INHERITED; |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 509 | |
| 510 | }; |
| 511 | |
| 512 | GrGLConical2Gradient::GrGLConical2Gradient( |
| 513 | const GrProgramStageFactory& factory, |
| 514 | const GrCustomStage& baseData) |
| 515 | : INHERITED(factory) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 516 | , fVSParamUni(kInvalidUniformHandle) |
| 517 | , fFSParamUni(kInvalidUniformHandle) |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 518 | , fVSVaryingName(NULL) |
| 519 | , fFSVaryingName(NULL) |
| 520 | , fCachedCenter(GR_ScalarMax) |
| 521 | , fCachedRadius(-GR_ScalarMax) |
| 522 | , fCachedDiffRadius(-GR_ScalarMax) { |
| 523 | |
| 524 | const GrConical2Gradient& data = |
| 525 | static_cast<const GrConical2Gradient&>(baseData); |
| 526 | fIsDegenerate = data.isDegenerate(); |
| 527 | } |
| 528 | |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 529 | void GrGLConical2Gradient::setupVariables(GrGLShaderBuilder* builder) { |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 530 | // 2 copies of uniform array, 1 for each of vertex & fragment shader, |
| 531 | // to work around Xoom bug. Doesn't seem to cause performance decrease |
| 532 | // in test apps, but need to keep an eye on it. |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 533 | fVSParamUni = builder->addUniformArray(GrGLShaderBuilder::kVertex_ShaderType, |
| 534 | kFloat_GrSLType, "Conical2VSParams", 6); |
| 535 | fFSParamUni = builder->addUniformArray(GrGLShaderBuilder::kFragment_ShaderType, |
| 536 | kFloat_GrSLType, "Conical2FSParams", 6); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 537 | |
| 538 | fVSParamLocation = GrGLProgramStage::kUseUniform; |
| 539 | fFSParamLocation = GrGLProgramStage::kUseUniform; |
| 540 | |
| 541 | // For radial gradients without perspective we can pass the linear |
| 542 | // part of the quadratic as a varying. |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 543 | if (builder->fVaryingDims == builder->fCoordDims) { |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 544 | builder->addVarying(kFloat_GrSLType, "Conical2BCoeff", |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 545 | &fVSVaryingName, &fFSVaryingName); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 549 | void GrGLConical2Gradient::emitVS(GrGLShaderBuilder* builder, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 550 | const char* vertexCoords) { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 551 | SkString* code = &builder->fVSCode; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 552 | SkString p2; // distance between centers |
| 553 | SkString p3; // start radius |
| 554 | SkString p5; // difference in radii (r1 - r0) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 555 | builder->getUniformVariable(fVSParamUni).appendArrayAccess(2, &p2); |
| 556 | builder->getUniformVariable(fVSParamUni).appendArrayAccess(3, &p3); |
| 557 | builder->getUniformVariable(fVSParamUni).appendArrayAccess(5, &p5); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 558 | |
| 559 | // For radial gradients without perspective we can pass the linear |
| 560 | // part of the quadratic as a varying. |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 561 | if (builder->fVaryingDims == builder->fCoordDims) { |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 562 | // r2Var = -2 * (r2Parm[2] * varCoord.x - r2Param[3] * r2Param[5]) |
| 563 | code->appendf("\t%s = -2.0 * (%s * %s.x + %s * %s);\n", |
| 564 | fVSVaryingName, p2.c_str(), |
| 565 | vertexCoords, p3.c_str(), p5.c_str()); |
| 566 | } |
| 567 | } |
| 568 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 569 | void GrGLConical2Gradient::emitFS(GrGLShaderBuilder* builder, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 570 | const char* outputColor, |
| 571 | const char* inputColor, |
| 572 | const char* samplerName) { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 573 | SkString* code = &builder->fFSCode; |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 574 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 575 | SkString cName("c"); |
| 576 | SkString ac4Name("ac4"); |
| 577 | SkString dName("d"); |
| 578 | SkString qName("q"); |
| 579 | SkString r0Name("r0"); |
| 580 | SkString r1Name("r1"); |
| 581 | SkString tName("t"); |
| 582 | SkString p0; // 4a |
rileya@google.com | 6219728 | 2012-07-10 20:05:23 +0000 | [diff] [blame] | 583 | SkString p1; // 1/a |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 584 | SkString p2; // distance between centers |
| 585 | SkString p3; // start radius |
| 586 | SkString p4; // start radius squared |
| 587 | SkString p5; // difference in radii (r1 - r0) |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 588 | |
bsalomon@google.com | ec4037f | 2012-07-16 13:46:39 +0000 | [diff] [blame] | 589 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(0, &p0); |
| 590 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(1, &p1); |
| 591 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(2, &p2); |
| 592 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(3, &p3); |
| 593 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(4, &p4); |
| 594 | builder->getUniformVariable(fFSParamUni).appendArrayAccess(5, &p5); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 595 | |
| 596 | // If we we're able to interpolate the linear component, |
| 597 | // bVar is the varying; otherwise compute it |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 598 | SkString bVar; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 599 | if (builder->fCoordDims == builder->fVaryingDims) { |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 600 | bVar = fFSVaryingName; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 601 | GrAssert(2 == builder->fVaryingDims); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 602 | } else { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 603 | GrAssert(3 == builder->fVaryingDims); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 604 | bVar = "b"; |
| 605 | 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] | 606 | bVar.c_str(), p2.c_str(), builder->fSampleCoords.c_str(), |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 607 | p3.c_str(), p5.c_str()); |
| 608 | } |
| 609 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 610 | // output will default to transparent black (we simply won't write anything |
| 611 | // else to it if invalid, instead of discarding or returning prematurely) |
| 612 | code->appendf("\t%s = vec4(0.0,0.0,0.0,0.0);\n", outputColor); |
| 613 | |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 614 | // c = (x^2)+(y^2) - params[4] |
| 615 | 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] | 616 | builder->fSampleCoords.c_str(), builder->fSampleCoords.c_str(), |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 617 | p4.c_str()); |
| 618 | |
| 619 | // Non-degenerate case (quadratic) |
| 620 | if (!fIsDegenerate) { |
| 621 | |
| 622 | // ac4 = params[0] * c |
| 623 | code->appendf("\tfloat %s = %s * %s;\n", ac4Name.c_str(), p0.c_str(), |
| 624 | cName.c_str()); |
| 625 | |
| 626 | // d = b^2 - ac4 |
| 627 | code->appendf("\tfloat %s = %s * %s - %s;\n", dName.c_str(), |
| 628 | bVar.c_str(), bVar.c_str(), ac4Name.c_str()); |
| 629 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 630 | // only proceed if discriminant is >= 0 |
| 631 | code->appendf("\tif (%s >= 0.0) {\n", dName.c_str()); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 632 | |
| 633 | // intermediate value we'll use to compute the roots |
| 634 | // q = -0.5 * (b +/- sqrt(d)) |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 635 | 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] | 636 | " * sqrt(%s));\n", qName.c_str(), bVar.c_str(), |
| 637 | bVar.c_str(), dName.c_str()); |
| 638 | |
| 639 | // compute both roots |
| 640 | // r0 = q * params[1] |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 641 | code->appendf("\t\tfloat %s = %s * %s;\n", r0Name.c_str(), |
| 642 | qName.c_str(), p1.c_str()); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 643 | // r1 = c / q |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 644 | code->appendf("\t\tfloat %s = %s / %s;\n", r1Name.c_str(), |
| 645 | cName.c_str(), qName.c_str()); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 646 | |
| 647 | // Note: If there are two roots that both generate radius(t) > 0, the |
| 648 | // Canvas spec says to choose the larger t. |
| 649 | |
| 650 | // so we'll look at the larger one first: |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 651 | 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] | 652 | r0Name.c_str(), r1Name.c_str()); |
| 653 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 654 | // if r(t) > 0, then we're done; t will be our x coordinate |
| 655 | 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] | 656 | p5.c_str(), p3.c_str()); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 657 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 658 | code->appendf("\t\t"); |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 659 | this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 660 | |
| 661 | // otherwise, if r(t) for the larger root was <= 0, try the other root |
| 662 | code->appendf("\t\t} else {\n"); |
| 663 | 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] | 664 | r0Name.c_str(), r1Name.c_str()); |
| 665 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 666 | // if r(t) > 0 for the smaller root, then t will be our x coordinate |
| 667 | code->appendf("\t\t\tif (%s * %s + %s > 0.0) {\n", |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 668 | tName.c_str(), p5.c_str(), p3.c_str()); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 669 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 670 | code->appendf("\t\t\t"); |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 671 | this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 672 | |
| 673 | // end if (r(t) > 0) for smaller root |
| 674 | code->appendf("\t\t\t}\n"); |
| 675 | // end if (r(t) > 0), else, for larger root |
| 676 | code->appendf("\t\t}\n"); |
| 677 | // end if (discriminant >= 0) |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 678 | code->appendf("\t}\n"); |
| 679 | } else { |
| 680 | |
| 681 | // linear case: t = -c/b |
| 682 | code->appendf("\tfloat %s = -(%s / %s);\n", tName.c_str(), |
| 683 | cName.c_str(), bVar.c_str()); |
| 684 | |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 685 | // if r(t) > 0, then t will be the x coordinate |
| 686 | 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] | 687 | p5.c_str(), p3.c_str()); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 688 | code->appendf("\t"); |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 689 | this->emitColorLookup(builder, tName.c_str(), outputColor, samplerName); |
rileya@google.com | e38160c | 2012-07-03 18:03:04 +0000 | [diff] [blame] | 690 | code->appendf("\t}\n"); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 691 | } |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 692 | } |
| 693 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 694 | void GrGLConical2Gradient::setData(const GrGLUniformManager& uman, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 695 | const GrCustomStage& baseData, |
senorblanco@chromium.org | f4770d7 | 2012-07-13 18:25:06 +0000 | [diff] [blame] | 696 | const GrRenderTarget*, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 697 | int stageNum) { |
| 698 | const GrConical2Gradient& data = |
| 699 | static_cast<const GrConical2Gradient&>(baseData); |
| 700 | GrAssert(data.isDegenerate() == fIsDegenerate); |
| 701 | GrScalar centerX1 = data.center(); |
| 702 | GrScalar radius0 = data.radius(); |
| 703 | GrScalar diffRadius = data.diffRadius(); |
| 704 | |
| 705 | if (fCachedCenter != centerX1 || |
| 706 | fCachedRadius != radius0 || |
| 707 | fCachedDiffRadius != diffRadius) { |
| 708 | |
| 709 | GrScalar a = GrMul(centerX1, centerX1) - diffRadius * diffRadius; |
| 710 | |
| 711 | // When we're in the degenerate (linear) case, the second |
| 712 | // value will be INF but the program doesn't read it. (We |
| 713 | // use the same 6 uniforms even though we don't need them |
| 714 | // all in the linear case just to keep the code complexity |
| 715 | // down). |
| 716 | float values[6] = { |
| 717 | GrScalarToFloat(a * 4), |
| 718 | 1.f / (GrScalarToFloat(a)), |
| 719 | GrScalarToFloat(centerX1), |
| 720 | GrScalarToFloat(radius0), |
| 721 | GrScalarToFloat(SkScalarMul(radius0, radius0)), |
| 722 | GrScalarToFloat(diffRadius) |
| 723 | }; |
| 724 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 725 | uman.set1fv(fVSParamUni, 0, 6, values); |
| 726 | uman.set1fv(fFSParamUni, 0, 6, values); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 727 | fCachedCenter = centerX1; |
| 728 | fCachedRadius = radius0; |
| 729 | fCachedDiffRadius = diffRadius; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | |
| 734 | ///////////////////////////////////////////////////////////////////// |
| 735 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 736 | GrConical2Gradient::GrConical2Gradient(GrTexture* texture, |
| 737 | GrScalar center, |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 738 | GrScalar radius, |
| 739 | GrScalar diffRadius) |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 740 | : INHERITED (texture) |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 741 | , fCenterX1 (center) |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 742 | , fRadius0 (radius) |
| 743 | , fDiffRadius (diffRadius) { |
| 744 | |
| 745 | } |
| 746 | |
rileya@google.com | 91f319c | 2012-07-25 17:18:31 +0000 | [diff] [blame] | 747 | GrConical2Gradient::GrConical2Gradient(GrContext* ctx, |
| 748 | const SkShader& shader, |
| 749 | GrSamplerState* sampler, |
| 750 | SkScalar center, |
| 751 | SkScalar startRadius, |
| 752 | SkScalar diffRadius) |
| 753 | : INHERITED(ctx, shader, sampler) |
| 754 | , fCenterX1(center) |
| 755 | , fRadius0(startRadius) |
| 756 | , fDiffRadius(diffRadius) { |
rileya@google.com | 03c1c35 | 2012-07-20 20:02:43 +0000 | [diff] [blame] | 757 | } |
| 758 | |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 759 | GrConical2Gradient::~GrConical2Gradient() { |
| 760 | |
| 761 | } |
| 762 | |
| 763 | |
| 764 | const GrProgramStageFactory& GrConical2Gradient::getFactory() const { |
| 765 | return GrTProgramStageFactory<GrConical2Gradient>::getInstance(); |
| 766 | } |
| 767 | |
| 768 | bool GrConical2Gradient::isEqual(const GrCustomStage& sBase) const { |
| 769 | const GrConical2Gradient& s = static_cast<const GrConical2Gradient&>(sBase); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 770 | return (INHERITED::isEqual(sBase) && |
| 771 | this->fCenterX1 == s.fCenterX1 && |
tomhudson@google.com | 1dcfa1f | 2012-07-09 18:21:28 +0000 | [diff] [blame] | 772 | this->fRadius0 == s.fRadius0 && |
| 773 | this->fDiffRadius == s.fDiffRadius); |
rileya@google.com | 3e33258 | 2012-07-03 13:43:35 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | ///////////////////////////////////////////////////////////////////// |
| 777 | |
| 778 | |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 779 | class GrGLSweepGradient : public GrGLGradientStage { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 780 | |
| 781 | public: |
| 782 | |
| 783 | GrGLSweepGradient(const GrProgramStageFactory& factory, |
| 784 | const GrCustomStage&) : INHERITED (factory) { } |
| 785 | virtual ~GrGLSweepGradient() { } |
| 786 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 787 | virtual void emitVS(GrGLShaderBuilder* builder, |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 788 | const char* vertexCoords) SK_OVERRIDE { } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 789 | virtual void emitFS(GrGLShaderBuilder* builder, |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 790 | const char* outputColor, |
| 791 | const char* inputColor, |
| 792 | const char* samplerName) SK_OVERRIDE; |
| 793 | |
| 794 | static StageKey GenKey(const GrCustomStage& s) { return 0; } |
| 795 | |
| 796 | private: |
| 797 | |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 798 | typedef GrGLGradientStage INHERITED; |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 799 | |
| 800 | }; |
| 801 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 802 | void GrGLSweepGradient::emitFS(GrGLShaderBuilder* builder, |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 803 | const char* outputColor, |
| 804 | const char* inputColor, |
| 805 | const char* samplerName) { |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 806 | SkString t; |
| 807 | t.printf("atan(- %s.y, - %s.x) * 0.1591549430918 + 0.5", |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 808 | builder->fSampleCoords.c_str(), builder->fSampleCoords.c_str()); |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 809 | this->emitColorLookup(builder, t.c_str(), outputColor, samplerName); |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | ///////////////////////////////////////////////////////////////////// |
| 813 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 814 | GrSweepGradient::GrSweepGradient(GrTexture* texture) |
rileya@google.com | 22e57f9 | 2012-07-19 15:16:19 +0000 | [diff] [blame] | 815 | : INHERITED(texture) { |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 816 | |
| 817 | } |
| 818 | |
rileya@google.com | 91f319c | 2012-07-25 17:18:31 +0000 | [diff] [blame] | 819 | GrSweepGradient::GrSweepGradient(GrContext* ctx, const SkShader& shader, |
| 820 | GrSamplerState* sampler) |
| 821 | : INHERITED(ctx, shader, sampler) { |
rileya@google.com | 03c1c35 | 2012-07-20 20:02:43 +0000 | [diff] [blame] | 822 | } |
| 823 | |
tomhudson@google.com | 7fab52d | 2012-05-31 19:40:13 +0000 | [diff] [blame] | 824 | GrSweepGradient::~GrSweepGradient() { |
| 825 | |
| 826 | } |
| 827 | |
| 828 | const GrProgramStageFactory& GrSweepGradient::getFactory() const { |
| 829 | return GrTProgramStageFactory<GrSweepGradient>::getInstance(); |
| 830 | } |
| 831 | |