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