Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2013 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "SkDither.h" |
michael@0 | 9 | #include "SkPerlinNoiseShader.h" |
michael@0 | 10 | #include "SkColorFilter.h" |
michael@0 | 11 | #include "SkReadBuffer.h" |
michael@0 | 12 | #include "SkWriteBuffer.h" |
michael@0 | 13 | #include "SkShader.h" |
michael@0 | 14 | #include "SkUnPreMultiply.h" |
michael@0 | 15 | #include "SkString.h" |
michael@0 | 16 | |
michael@0 | 17 | #if SK_SUPPORT_GPU |
michael@0 | 18 | #include "GrContext.h" |
michael@0 | 19 | #include "GrCoordTransform.h" |
michael@0 | 20 | #include "gl/GrGLEffect.h" |
michael@0 | 21 | #include "GrTBackendEffectFactory.h" |
michael@0 | 22 | #include "SkGr.h" |
michael@0 | 23 | #endif |
michael@0 | 24 | |
michael@0 | 25 | static const int kBlockSize = 256; |
michael@0 | 26 | static const int kBlockMask = kBlockSize - 1; |
michael@0 | 27 | static const int kPerlinNoise = 4096; |
michael@0 | 28 | static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
michael@0 | 29 | |
michael@0 | 30 | namespace { |
michael@0 | 31 | |
michael@0 | 32 | // noiseValue is the color component's value (or color) |
michael@0 | 33 | // limitValue is the maximum perlin noise array index value allowed |
michael@0 | 34 | // newValue is the current noise dimension (either width or height) |
michael@0 | 35 | inline int checkNoise(int noiseValue, int limitValue, int newValue) { |
michael@0 | 36 | // If the noise value would bring us out of bounds of the current noise array while we are |
michael@0 | 37 | // stiching noise tiles together, wrap the noise around the current dimension of the noise to |
michael@0 | 38 | // stay within the array bounds in a continuous fashion (so that tiling lines are not visible) |
michael@0 | 39 | if (noiseValue >= limitValue) { |
michael@0 | 40 | noiseValue -= newValue; |
michael@0 | 41 | } |
michael@0 | 42 | if (noiseValue >= limitValue - 1) { |
michael@0 | 43 | noiseValue -= newValue - 1; |
michael@0 | 44 | } |
michael@0 | 45 | return noiseValue; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | inline SkScalar smoothCurve(SkScalar t) { |
michael@0 | 49 | static const SkScalar SK_Scalar3 = 3.0f; |
michael@0 | 50 | |
michael@0 | 51 | // returns t * t * (3 - 2 * t) |
michael@0 | 52 | return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | bool perlin_noise_type_is_valid(SkPerlinNoiseShader::Type type) { |
michael@0 | 56 | return (SkPerlinNoiseShader::kFractalNoise_Type == type) || |
michael@0 | 57 | (SkPerlinNoiseShader::kTurbulence_Type == type); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | } // end namespace |
michael@0 | 61 | |
michael@0 | 62 | struct SkPerlinNoiseShader::StitchData { |
michael@0 | 63 | StitchData() |
michael@0 | 64 | : fWidth(0) |
michael@0 | 65 | , fWrapX(0) |
michael@0 | 66 | , fHeight(0) |
michael@0 | 67 | , fWrapY(0) |
michael@0 | 68 | {} |
michael@0 | 69 | |
michael@0 | 70 | bool operator==(const StitchData& other) const { |
michael@0 | 71 | return fWidth == other.fWidth && |
michael@0 | 72 | fWrapX == other.fWrapX && |
michael@0 | 73 | fHeight == other.fHeight && |
michael@0 | 74 | fWrapY == other.fWrapY; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | int fWidth; // How much to subtract to wrap for stitching. |
michael@0 | 78 | int fWrapX; // Minimum value to wrap. |
michael@0 | 79 | int fHeight; |
michael@0 | 80 | int fWrapY; |
michael@0 | 81 | }; |
michael@0 | 82 | |
michael@0 | 83 | struct SkPerlinNoiseShader::PaintingData { |
michael@0 | 84 | PaintingData(const SkISize& tileSize, SkScalar seed, |
michael@0 | 85 | SkScalar baseFrequencyX, SkScalar baseFrequencyY) |
michael@0 | 86 | : fTileSize(tileSize) |
michael@0 | 87 | , fBaseFrequency(SkPoint::Make(baseFrequencyX, baseFrequencyY)) |
michael@0 | 88 | { |
michael@0 | 89 | this->init(seed); |
michael@0 | 90 | if (!fTileSize.isEmpty()) { |
michael@0 | 91 | this->stitch(); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | #if SK_SUPPORT_GPU && !defined(SK_USE_SIMPLEX_NOISE) |
michael@0 | 95 | fPermutationsBitmap.setConfig(SkImageInfo::MakeA8(kBlockSize, 1)); |
michael@0 | 96 | fPermutationsBitmap.setPixels(fLatticeSelector); |
michael@0 | 97 | |
michael@0 | 98 | fNoiseBitmap.setConfig(SkImageInfo::MakeN32Premul(kBlockSize, 4)); |
michael@0 | 99 | fNoiseBitmap.setPixels(fNoise[0][0]); |
michael@0 | 100 | #endif |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | int fSeed; |
michael@0 | 104 | uint8_t fLatticeSelector[kBlockSize]; |
michael@0 | 105 | uint16_t fNoise[4][kBlockSize][2]; |
michael@0 | 106 | SkPoint fGradient[4][kBlockSize]; |
michael@0 | 107 | SkISize fTileSize; |
michael@0 | 108 | SkVector fBaseFrequency; |
michael@0 | 109 | StitchData fStitchDataInit; |
michael@0 | 110 | |
michael@0 | 111 | private: |
michael@0 | 112 | |
michael@0 | 113 | #if SK_SUPPORT_GPU && !defined(SK_USE_SIMPLEX_NOISE) |
michael@0 | 114 | SkBitmap fPermutationsBitmap; |
michael@0 | 115 | SkBitmap fNoiseBitmap; |
michael@0 | 116 | #endif |
michael@0 | 117 | |
michael@0 | 118 | inline int random() { |
michael@0 | 119 | static const int gRandAmplitude = 16807; // 7**5; primitive root of m |
michael@0 | 120 | static const int gRandQ = 127773; // m / a |
michael@0 | 121 | static const int gRandR = 2836; // m % a |
michael@0 | 122 | |
michael@0 | 123 | int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ); |
michael@0 | 124 | if (result <= 0) |
michael@0 | 125 | result += kRandMaximum; |
michael@0 | 126 | fSeed = result; |
michael@0 | 127 | return result; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | // Only called once. Could be part of the constructor. |
michael@0 | 131 | void init(SkScalar seed) |
michael@0 | 132 | { |
michael@0 | 133 | static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize)); |
michael@0 | 134 | |
michael@0 | 135 | // According to the SVG spec, we must truncate (not round) the seed value. |
michael@0 | 136 | fSeed = SkScalarTruncToInt(seed); |
michael@0 | 137 | // The seed value clamp to the range [1, kRandMaximum - 1]. |
michael@0 | 138 | if (fSeed <= 0) { |
michael@0 | 139 | fSeed = -(fSeed % (kRandMaximum - 1)) + 1; |
michael@0 | 140 | } |
michael@0 | 141 | if (fSeed > kRandMaximum - 1) { |
michael@0 | 142 | fSeed = kRandMaximum - 1; |
michael@0 | 143 | } |
michael@0 | 144 | for (int channel = 0; channel < 4; ++channel) { |
michael@0 | 145 | for (int i = 0; i < kBlockSize; ++i) { |
michael@0 | 146 | fLatticeSelector[i] = i; |
michael@0 | 147 | fNoise[channel][i][0] = (random() % (2 * kBlockSize)); |
michael@0 | 148 | fNoise[channel][i][1] = (random() % (2 * kBlockSize)); |
michael@0 | 149 | } |
michael@0 | 150 | } |
michael@0 | 151 | for (int i = kBlockSize - 1; i > 0; --i) { |
michael@0 | 152 | int k = fLatticeSelector[i]; |
michael@0 | 153 | int j = random() % kBlockSize; |
michael@0 | 154 | SkASSERT(j >= 0); |
michael@0 | 155 | SkASSERT(j < kBlockSize); |
michael@0 | 156 | fLatticeSelector[i] = fLatticeSelector[j]; |
michael@0 | 157 | fLatticeSelector[j] = k; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // Perform the permutations now |
michael@0 | 161 | { |
michael@0 | 162 | // Copy noise data |
michael@0 | 163 | uint16_t noise[4][kBlockSize][2]; |
michael@0 | 164 | for (int i = 0; i < kBlockSize; ++i) { |
michael@0 | 165 | for (int channel = 0; channel < 4; ++channel) { |
michael@0 | 166 | for (int j = 0; j < 2; ++j) { |
michael@0 | 167 | noise[channel][i][j] = fNoise[channel][i][j]; |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | // Do permutations on noise data |
michael@0 | 172 | for (int i = 0; i < kBlockSize; ++i) { |
michael@0 | 173 | for (int channel = 0; channel < 4; ++channel) { |
michael@0 | 174 | for (int j = 0; j < 2; ++j) { |
michael@0 | 175 | fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j]; |
michael@0 | 176 | } |
michael@0 | 177 | } |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | // Half of the largest possible value for 16 bit unsigned int |
michael@0 | 182 | static const SkScalar gHalfMax16bits = 32767.5f; |
michael@0 | 183 | |
michael@0 | 184 | // Compute gradients from permutated noise data |
michael@0 | 185 | for (int channel = 0; channel < 4; ++channel) { |
michael@0 | 186 | for (int i = 0; i < kBlockSize; ++i) { |
michael@0 | 187 | fGradient[channel][i] = SkPoint::Make( |
michael@0 | 188 | SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize), |
michael@0 | 189 | gInvBlockSizef), |
michael@0 | 190 | SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize), |
michael@0 | 191 | gInvBlockSizef)); |
michael@0 | 192 | fGradient[channel][i].normalize(); |
michael@0 | 193 | // Put the normalized gradient back into the noise data |
michael@0 | 194 | fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul( |
michael@0 | 195 | fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits)); |
michael@0 | 196 | fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul( |
michael@0 | 197 | fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits)); |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | // Only called once. Could be part of the constructor. |
michael@0 | 203 | void stitch() { |
michael@0 | 204 | SkScalar tileWidth = SkIntToScalar(fTileSize.width()); |
michael@0 | 205 | SkScalar tileHeight = SkIntToScalar(fTileSize.height()); |
michael@0 | 206 | SkASSERT(tileWidth > 0 && tileHeight > 0); |
michael@0 | 207 | // When stitching tiled turbulence, the frequencies must be adjusted |
michael@0 | 208 | // so that the tile borders will be continuous. |
michael@0 | 209 | if (fBaseFrequency.fX) { |
michael@0 | 210 | SkScalar lowFrequencx = |
michael@0 | 211 | SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
michael@0 | 212 | SkScalar highFrequencx = |
michael@0 | 213 | SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
michael@0 | 214 | // BaseFrequency should be non-negative according to the standard. |
michael@0 | 215 | if (SkScalarDiv(fBaseFrequency.fX, lowFrequencx) < |
michael@0 | 216 | SkScalarDiv(highFrequencx, fBaseFrequency.fX)) { |
michael@0 | 217 | fBaseFrequency.fX = lowFrequencx; |
michael@0 | 218 | } else { |
michael@0 | 219 | fBaseFrequency.fX = highFrequencx; |
michael@0 | 220 | } |
michael@0 | 221 | } |
michael@0 | 222 | if (fBaseFrequency.fY) { |
michael@0 | 223 | SkScalar lowFrequency = |
michael@0 | 224 | SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
michael@0 | 225 | SkScalar highFrequency = |
michael@0 | 226 | SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
michael@0 | 227 | if (SkScalarDiv(fBaseFrequency.fY, lowFrequency) < |
michael@0 | 228 | SkScalarDiv(highFrequency, fBaseFrequency.fY)) { |
michael@0 | 229 | fBaseFrequency.fY = lowFrequency; |
michael@0 | 230 | } else { |
michael@0 | 231 | fBaseFrequency.fY = highFrequency; |
michael@0 | 232 | } |
michael@0 | 233 | } |
michael@0 | 234 | // Set up TurbulenceInitial stitch values. |
michael@0 | 235 | fStitchDataInit.fWidth = |
michael@0 | 236 | SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); |
michael@0 | 237 | fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; |
michael@0 | 238 | fStitchDataInit.fHeight = |
michael@0 | 239 | SkScalarRoundToInt(tileHeight * fBaseFrequency.fY); |
michael@0 | 240 | fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | public: |
michael@0 | 244 | |
michael@0 | 245 | #if SK_SUPPORT_GPU && !defined(SK_USE_SIMPLEX_NOISE) |
michael@0 | 246 | const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; } |
michael@0 | 247 | |
michael@0 | 248 | const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; } |
michael@0 | 249 | #endif |
michael@0 | 250 | }; |
michael@0 | 251 | |
michael@0 | 252 | SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
michael@0 | 253 | int numOctaves, SkScalar seed, |
michael@0 | 254 | const SkISize* tileSize) { |
michael@0 | 255 | return SkNEW_ARGS(SkPerlinNoiseShader, (kFractalNoise_Type, baseFrequencyX, baseFrequencyY, |
michael@0 | 256 | numOctaves, seed, tileSize)); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | SkShader* SkPerlinNoiseShader::CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
michael@0 | 260 | int numOctaves, SkScalar seed, |
michael@0 | 261 | const SkISize* tileSize) { |
michael@0 | 262 | return SkNEW_ARGS(SkPerlinNoiseShader, (kTurbulence_Type, baseFrequencyX, baseFrequencyY, |
michael@0 | 263 | numOctaves, seed, tileSize)); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, |
michael@0 | 267 | SkScalar baseFrequencyX, |
michael@0 | 268 | SkScalar baseFrequencyY, |
michael@0 | 269 | int numOctaves, |
michael@0 | 270 | SkScalar seed, |
michael@0 | 271 | const SkISize* tileSize) |
michael@0 | 272 | : fType(type) |
michael@0 | 273 | , fBaseFrequencyX(baseFrequencyX) |
michael@0 | 274 | , fBaseFrequencyY(baseFrequencyY) |
michael@0 | 275 | , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/) |
michael@0 | 276 | , fSeed(seed) |
michael@0 | 277 | , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize) |
michael@0 | 278 | , fStitchTiles(!fTileSize.isEmpty()) |
michael@0 | 279 | { |
michael@0 | 280 | SkASSERT(numOctaves >= 0 && numOctaves < 256); |
michael@0 | 281 | fMatrix.reset(); |
michael@0 | 282 | fPaintingData = SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY)); |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | SkPerlinNoiseShader::SkPerlinNoiseShader(SkReadBuffer& buffer) |
michael@0 | 286 | : INHERITED(buffer) |
michael@0 | 287 | { |
michael@0 | 288 | fType = (SkPerlinNoiseShader::Type) buffer.readInt(); |
michael@0 | 289 | fBaseFrequencyX = buffer.readScalar(); |
michael@0 | 290 | fBaseFrequencyY = buffer.readScalar(); |
michael@0 | 291 | fNumOctaves = buffer.readInt(); |
michael@0 | 292 | fSeed = buffer.readScalar(); |
michael@0 | 293 | fStitchTiles = buffer.readBool(); |
michael@0 | 294 | fTileSize.fWidth = buffer.readInt(); |
michael@0 | 295 | fTileSize.fHeight = buffer.readInt(); |
michael@0 | 296 | fMatrix.reset(); |
michael@0 | 297 | fPaintingData = SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY)); |
michael@0 | 298 | buffer.validate(perlin_noise_type_is_valid(fType) && |
michael@0 | 299 | (fNumOctaves >= 0) && (fNumOctaves <= 255) && |
michael@0 | 300 | (fStitchTiles != fTileSize.isEmpty())); |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | SkPerlinNoiseShader::~SkPerlinNoiseShader() { |
michael@0 | 304 | // Safety, should have been done in endContext() |
michael@0 | 305 | SkDELETE(fPaintingData); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const { |
michael@0 | 309 | this->INHERITED::flatten(buffer); |
michael@0 | 310 | buffer.writeInt((int) fType); |
michael@0 | 311 | buffer.writeScalar(fBaseFrequencyX); |
michael@0 | 312 | buffer.writeScalar(fBaseFrequencyY); |
michael@0 | 313 | buffer.writeInt(fNumOctaves); |
michael@0 | 314 | buffer.writeScalar(fSeed); |
michael@0 | 315 | buffer.writeBool(fStitchTiles); |
michael@0 | 316 | buffer.writeInt(fTileSize.fWidth); |
michael@0 | 317 | buffer.writeInt(fTileSize.fHeight); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | SkScalar SkPerlinNoiseShader::noise2D(int channel, const PaintingData& paintingData, |
michael@0 | 321 | const StitchData& stitchData, |
michael@0 | 322 | const SkPoint& noiseVector) const { |
michael@0 | 323 | struct Noise { |
michael@0 | 324 | int noisePositionIntegerValue; |
michael@0 | 325 | SkScalar noisePositionFractionValue; |
michael@0 | 326 | Noise(SkScalar component) |
michael@0 | 327 | { |
michael@0 | 328 | SkScalar position = component + kPerlinNoise; |
michael@0 | 329 | noisePositionIntegerValue = SkScalarFloorToInt(position); |
michael@0 | 330 | noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue); |
michael@0 | 331 | } |
michael@0 | 332 | }; |
michael@0 | 333 | Noise noiseX(noiseVector.x()); |
michael@0 | 334 | Noise noiseY(noiseVector.y()); |
michael@0 | 335 | SkScalar u, v; |
michael@0 | 336 | // If stitching, adjust lattice points accordingly. |
michael@0 | 337 | if (fStitchTiles) { |
michael@0 | 338 | noiseX.noisePositionIntegerValue = |
michael@0 | 339 | checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
michael@0 | 340 | noiseY.noisePositionIntegerValue = |
michael@0 | 341 | checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
michael@0 | 342 | } |
michael@0 | 343 | noiseX.noisePositionIntegerValue &= kBlockMask; |
michael@0 | 344 | noiseY.noisePositionIntegerValue &= kBlockMask; |
michael@0 | 345 | int latticeIndex = |
michael@0 | 346 | paintingData.fLatticeSelector[noiseX.noisePositionIntegerValue] + |
michael@0 | 347 | noiseY.noisePositionIntegerValue; |
michael@0 | 348 | int nextLatticeIndex = |
michael@0 | 349 | paintingData.fLatticeSelector[(noiseX.noisePositionIntegerValue + 1) & kBlockMask] + |
michael@0 | 350 | noiseY.noisePositionIntegerValue; |
michael@0 | 351 | SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue); |
michael@0 | 352 | SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue); |
michael@0 | 353 | // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement |
michael@0 | 354 | SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue, |
michael@0 | 355 | noiseY.noisePositionFractionValue); // Offset (0,0) |
michael@0 | 356 | u = paintingData.fGradient[channel][latticeIndex & kBlockMask].dot(fractionValue); |
michael@0 | 357 | fractionValue.fX -= SK_Scalar1; // Offset (-1,0) |
michael@0 | 358 | v = paintingData.fGradient[channel][nextLatticeIndex & kBlockMask].dot(fractionValue); |
michael@0 | 359 | SkScalar a = SkScalarInterp(u, v, sx); |
michael@0 | 360 | fractionValue.fY -= SK_Scalar1; // Offset (-1,-1) |
michael@0 | 361 | v = paintingData.fGradient[channel][(nextLatticeIndex + 1) & kBlockMask].dot(fractionValue); |
michael@0 | 362 | fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1) |
michael@0 | 363 | u = paintingData.fGradient[channel][(latticeIndex + 1) & kBlockMask].dot(fractionValue); |
michael@0 | 364 | SkScalar b = SkScalarInterp(u, v, sx); |
michael@0 | 365 | return SkScalarInterp(a, b, sy); |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | SkScalar SkPerlinNoiseShader::calculateTurbulenceValueForPoint(int channel, |
michael@0 | 369 | const PaintingData& paintingData, |
michael@0 | 370 | StitchData& stitchData, |
michael@0 | 371 | const SkPoint& point) const { |
michael@0 | 372 | if (fStitchTiles) { |
michael@0 | 373 | // Set up TurbulenceInitial stitch values. |
michael@0 | 374 | stitchData = paintingData.fStitchDataInit; |
michael@0 | 375 | } |
michael@0 | 376 | SkScalar turbulenceFunctionResult = 0; |
michael@0 | 377 | SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), paintingData.fBaseFrequency.fX), |
michael@0 | 378 | SkScalarMul(point.y(), paintingData.fBaseFrequency.fY))); |
michael@0 | 379 | SkScalar ratio = SK_Scalar1; |
michael@0 | 380 | for (int octave = 0; octave < fNumOctaves; ++octave) { |
michael@0 | 381 | SkScalar noise = noise2D(channel, paintingData, stitchData, noiseVector); |
michael@0 | 382 | turbulenceFunctionResult += SkScalarDiv( |
michael@0 | 383 | (fType == kFractalNoise_Type) ? noise : SkScalarAbs(noise), ratio); |
michael@0 | 384 | noiseVector.fX *= 2; |
michael@0 | 385 | noiseVector.fY *= 2; |
michael@0 | 386 | ratio *= 2; |
michael@0 | 387 | if (fStitchTiles) { |
michael@0 | 388 | // Update stitch values |
michael@0 | 389 | stitchData.fWidth *= 2; |
michael@0 | 390 | stitchData.fWrapX = stitchData.fWidth + kPerlinNoise; |
michael@0 | 391 | stitchData.fHeight *= 2; |
michael@0 | 392 | stitchData.fWrapY = stitchData.fHeight + kPerlinNoise; |
michael@0 | 393 | } |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
michael@0 | 397 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
michael@0 | 398 | if (fType == kFractalNoise_Type) { |
michael@0 | 399 | turbulenceFunctionResult = |
michael@0 | 400 | SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf; |
michael@0 | 401 | } |
michael@0 | 402 | |
michael@0 | 403 | if (channel == 3) { // Scale alpha by paint value |
michael@0 | 404 | turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult, |
michael@0 | 405 | SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255))); |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | // Clamp result |
michael@0 | 409 | return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1); |
michael@0 | 410 | } |
michael@0 | 411 | |
michael@0 | 412 | SkPMColor SkPerlinNoiseShader::shade(const SkPoint& point, StitchData& stitchData) const { |
michael@0 | 413 | SkMatrix matrix = fMatrix; |
michael@0 | 414 | matrix.postConcat(getLocalMatrix()); |
michael@0 | 415 | SkMatrix invMatrix; |
michael@0 | 416 | if (!matrix.invert(&invMatrix)) { |
michael@0 | 417 | invMatrix.reset(); |
michael@0 | 418 | } else { |
michael@0 | 419 | invMatrix.postConcat(invMatrix); // Square the matrix |
michael@0 | 420 | } |
michael@0 | 421 | // This (1,1) translation is due to WebKit's 1 based coordinates for the noise |
michael@0 | 422 | // (as opposed to 0 based, usually). The same adjustment is in the setData() function. |
michael@0 | 423 | matrix.postTranslate(SK_Scalar1, SK_Scalar1); |
michael@0 | 424 | SkPoint newPoint; |
michael@0 | 425 | matrix.mapPoints(&newPoint, &point, 1); |
michael@0 | 426 | invMatrix.mapPoints(&newPoint, &newPoint, 1); |
michael@0 | 427 | newPoint.fX = SkScalarRoundToScalar(newPoint.fX); |
michael@0 | 428 | newPoint.fY = SkScalarRoundToScalar(newPoint.fY); |
michael@0 | 429 | |
michael@0 | 430 | U8CPU rgba[4]; |
michael@0 | 431 | for (int channel = 3; channel >= 0; --channel) { |
michael@0 | 432 | rgba[channel] = SkScalarFloorToInt(255 * |
michael@0 | 433 | calculateTurbulenceValueForPoint(channel, *fPaintingData, stitchData, newPoint)); |
michael@0 | 434 | } |
michael@0 | 435 | return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]); |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | bool SkPerlinNoiseShader::setContext(const SkBitmap& device, const SkPaint& paint, |
michael@0 | 439 | const SkMatrix& matrix) { |
michael@0 | 440 | fMatrix = matrix; |
michael@0 | 441 | return INHERITED::setContext(device, paint, matrix); |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | void SkPerlinNoiseShader::shadeSpan(int x, int y, SkPMColor result[], int count) { |
michael@0 | 445 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
michael@0 | 446 | StitchData stitchData; |
michael@0 | 447 | for (int i = 0; i < count; ++i) { |
michael@0 | 448 | result[i] = shade(point, stitchData); |
michael@0 | 449 | point.fX += SK_Scalar1; |
michael@0 | 450 | } |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | void SkPerlinNoiseShader::shadeSpan16(int x, int y, uint16_t result[], int count) { |
michael@0 | 454 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
michael@0 | 455 | StitchData stitchData; |
michael@0 | 456 | DITHER_565_SCAN(y); |
michael@0 | 457 | for (int i = 0; i < count; ++i) { |
michael@0 | 458 | unsigned dither = DITHER_VALUE(x); |
michael@0 | 459 | result[i] = SkDitherRGB32To565(shade(point, stitchData), dither); |
michael@0 | 460 | DITHER_INC_X(x); |
michael@0 | 461 | point.fX += SK_Scalar1; |
michael@0 | 462 | } |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | ///////////////////////////////////////////////////////////////////// |
michael@0 | 466 | |
michael@0 | 467 | #if SK_SUPPORT_GPU |
michael@0 | 468 | |
michael@0 | 469 | #include "GrTBackendEffectFactory.h" |
michael@0 | 470 | |
michael@0 | 471 | class GrGLNoise : public GrGLEffect { |
michael@0 | 472 | public: |
michael@0 | 473 | GrGLNoise(const GrBackendEffectFactory& factory, |
michael@0 | 474 | const GrDrawEffect& drawEffect); |
michael@0 | 475 | virtual ~GrGLNoise() {} |
michael@0 | 476 | |
michael@0 | 477 | static inline EffectKey GenKey(const GrDrawEffect&, const GrGLCaps&); |
michael@0 | 478 | |
michael@0 | 479 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
michael@0 | 480 | |
michael@0 | 481 | protected: |
michael@0 | 482 | SkPerlinNoiseShader::Type fType; |
michael@0 | 483 | bool fStitchTiles; |
michael@0 | 484 | int fNumOctaves; |
michael@0 | 485 | GrGLUniformManager::UniformHandle fBaseFrequencyUni; |
michael@0 | 486 | GrGLUniformManager::UniformHandle fAlphaUni; |
michael@0 | 487 | GrGLUniformManager::UniformHandle fInvMatrixUni; |
michael@0 | 488 | |
michael@0 | 489 | private: |
michael@0 | 490 | typedef GrGLEffect INHERITED; |
michael@0 | 491 | }; |
michael@0 | 492 | |
michael@0 | 493 | class GrGLPerlinNoise : public GrGLNoise { |
michael@0 | 494 | public: |
michael@0 | 495 | GrGLPerlinNoise(const GrBackendEffectFactory& factory, |
michael@0 | 496 | const GrDrawEffect& drawEffect) |
michael@0 | 497 | : GrGLNoise(factory, drawEffect) {} |
michael@0 | 498 | virtual ~GrGLPerlinNoise() {} |
michael@0 | 499 | |
michael@0 | 500 | virtual void emitCode(GrGLShaderBuilder*, |
michael@0 | 501 | const GrDrawEffect&, |
michael@0 | 502 | EffectKey, |
michael@0 | 503 | const char* outputColor, |
michael@0 | 504 | const char* inputColor, |
michael@0 | 505 | const TransformedCoordsArray&, |
michael@0 | 506 | const TextureSamplerArray&) SK_OVERRIDE; |
michael@0 | 507 | |
michael@0 | 508 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
michael@0 | 509 | |
michael@0 | 510 | private: |
michael@0 | 511 | GrGLUniformManager::UniformHandle fStitchDataUni; |
michael@0 | 512 | |
michael@0 | 513 | typedef GrGLNoise INHERITED; |
michael@0 | 514 | }; |
michael@0 | 515 | |
michael@0 | 516 | class GrGLSimplexNoise : public GrGLNoise { |
michael@0 | 517 | // Note : This is for reference only. GrGLPerlinNoise is used for processing. |
michael@0 | 518 | public: |
michael@0 | 519 | GrGLSimplexNoise(const GrBackendEffectFactory& factory, |
michael@0 | 520 | const GrDrawEffect& drawEffect) |
michael@0 | 521 | : GrGLNoise(factory, drawEffect) {} |
michael@0 | 522 | |
michael@0 | 523 | virtual ~GrGLSimplexNoise() {} |
michael@0 | 524 | |
michael@0 | 525 | virtual void emitCode(GrGLShaderBuilder*, |
michael@0 | 526 | const GrDrawEffect&, |
michael@0 | 527 | EffectKey, |
michael@0 | 528 | const char* outputColor, |
michael@0 | 529 | const char* inputColor, |
michael@0 | 530 | const TransformedCoordsArray&, |
michael@0 | 531 | const TextureSamplerArray&) SK_OVERRIDE; |
michael@0 | 532 | |
michael@0 | 533 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE; |
michael@0 | 534 | |
michael@0 | 535 | private: |
michael@0 | 536 | GrGLUniformManager::UniformHandle fSeedUni; |
michael@0 | 537 | |
michael@0 | 538 | typedef GrGLNoise INHERITED; |
michael@0 | 539 | }; |
michael@0 | 540 | |
michael@0 | 541 | ///////////////////////////////////////////////////////////////////// |
michael@0 | 542 | |
michael@0 | 543 | class GrNoiseEffect : public GrEffect { |
michael@0 | 544 | public: |
michael@0 | 545 | virtual ~GrNoiseEffect() { } |
michael@0 | 546 | |
michael@0 | 547 | SkPerlinNoiseShader::Type type() const { return fType; } |
michael@0 | 548 | bool stitchTiles() const { return fStitchTiles; } |
michael@0 | 549 | const SkVector& baseFrequency() const { return fBaseFrequency; } |
michael@0 | 550 | int numOctaves() const { return fNumOctaves; } |
michael@0 | 551 | const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } |
michael@0 | 552 | uint8_t alpha() const { return fAlpha; } |
michael@0 | 553 | |
michael@0 | 554 | void getConstantColorComponents(GrColor*, uint32_t* validFlags) const SK_OVERRIDE { |
michael@0 | 555 | *validFlags = 0; // This is noise. Nothing is constant. |
michael@0 | 556 | } |
michael@0 | 557 | |
michael@0 | 558 | protected: |
michael@0 | 559 | virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { |
michael@0 | 560 | const GrNoiseEffect& s = CastEffect<GrNoiseEffect>(sBase); |
michael@0 | 561 | return fType == s.fType && |
michael@0 | 562 | fBaseFrequency == s.fBaseFrequency && |
michael@0 | 563 | fNumOctaves == s.fNumOctaves && |
michael@0 | 564 | fStitchTiles == s.fStitchTiles && |
michael@0 | 565 | fCoordTransform.getMatrix() == s.fCoordTransform.getMatrix() && |
michael@0 | 566 | fAlpha == s.fAlpha; |
michael@0 | 567 | } |
michael@0 | 568 | |
michael@0 | 569 | GrNoiseEffect(SkPerlinNoiseShader::Type type, const SkVector& baseFrequency, int numOctaves, |
michael@0 | 570 | bool stitchTiles, const SkMatrix& matrix, uint8_t alpha) |
michael@0 | 571 | : fType(type) |
michael@0 | 572 | , fBaseFrequency(baseFrequency) |
michael@0 | 573 | , fNumOctaves(numOctaves) |
michael@0 | 574 | , fStitchTiles(stitchTiles) |
michael@0 | 575 | , fMatrix(matrix) |
michael@0 | 576 | , fAlpha(alpha) { |
michael@0 | 577 | // This (1,1) translation is due to WebKit's 1 based coordinates for the noise |
michael@0 | 578 | // (as opposed to 0 based, usually). The same adjustment is in the shadeSpan() functions. |
michael@0 | 579 | SkMatrix m = matrix; |
michael@0 | 580 | m.postTranslate(SK_Scalar1, SK_Scalar1); |
michael@0 | 581 | fCoordTransform.reset(kLocal_GrCoordSet, m); |
michael@0 | 582 | this->addCoordTransform(&fCoordTransform); |
michael@0 | 583 | this->setWillNotUseInputColor(); |
michael@0 | 584 | } |
michael@0 | 585 | |
michael@0 | 586 | SkPerlinNoiseShader::Type fType; |
michael@0 | 587 | GrCoordTransform fCoordTransform; |
michael@0 | 588 | SkVector fBaseFrequency; |
michael@0 | 589 | int fNumOctaves; |
michael@0 | 590 | bool fStitchTiles; |
michael@0 | 591 | SkMatrix fMatrix; |
michael@0 | 592 | uint8_t fAlpha; |
michael@0 | 593 | |
michael@0 | 594 | private: |
michael@0 | 595 | typedef GrEffect INHERITED; |
michael@0 | 596 | }; |
michael@0 | 597 | |
michael@0 | 598 | class GrPerlinNoiseEffect : public GrNoiseEffect { |
michael@0 | 599 | public: |
michael@0 | 600 | static GrEffectRef* Create(SkPerlinNoiseShader::Type type, const SkVector& baseFrequency, |
michael@0 | 601 | int numOctaves, bool stitchTiles, |
michael@0 | 602 | const SkPerlinNoiseShader::StitchData& stitchData, |
michael@0 | 603 | GrTexture* permutationsTexture, GrTexture* noiseTexture, |
michael@0 | 604 | const SkMatrix& matrix, uint8_t alpha) { |
michael@0 | 605 | AutoEffectUnref effect(SkNEW_ARGS(GrPerlinNoiseEffect, (type, baseFrequency, numOctaves, |
michael@0 | 606 | stitchTiles, stitchData, permutationsTexture, noiseTexture, matrix, alpha))); |
michael@0 | 607 | return CreateEffectRef(effect); |
michael@0 | 608 | } |
michael@0 | 609 | |
michael@0 | 610 | virtual ~GrPerlinNoiseEffect() { } |
michael@0 | 611 | |
michael@0 | 612 | static const char* Name() { return "PerlinNoise"; } |
michael@0 | 613 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
michael@0 | 614 | return GrTBackendEffectFactory<GrPerlinNoiseEffect>::getInstance(); |
michael@0 | 615 | } |
michael@0 | 616 | const SkPerlinNoiseShader::StitchData& stitchData() const { return fStitchData; } |
michael@0 | 617 | |
michael@0 | 618 | typedef GrGLPerlinNoise GLEffect; |
michael@0 | 619 | |
michael@0 | 620 | private: |
michael@0 | 621 | virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { |
michael@0 | 622 | const GrPerlinNoiseEffect& s = CastEffect<GrPerlinNoiseEffect>(sBase); |
michael@0 | 623 | return INHERITED::onIsEqual(sBase) && |
michael@0 | 624 | fPermutationsAccess.getTexture() == s.fPermutationsAccess.getTexture() && |
michael@0 | 625 | fNoiseAccess.getTexture() == s.fNoiseAccess.getTexture() && |
michael@0 | 626 | fStitchData == s.fStitchData; |
michael@0 | 627 | } |
michael@0 | 628 | |
michael@0 | 629 | GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type, const SkVector& baseFrequency, |
michael@0 | 630 | int numOctaves, bool stitchTiles, |
michael@0 | 631 | const SkPerlinNoiseShader::StitchData& stitchData, |
michael@0 | 632 | GrTexture* permutationsTexture, GrTexture* noiseTexture, |
michael@0 | 633 | const SkMatrix& matrix, uint8_t alpha) |
michael@0 | 634 | : GrNoiseEffect(type, baseFrequency, numOctaves, stitchTiles, matrix, alpha) |
michael@0 | 635 | , fPermutationsAccess(permutationsTexture) |
michael@0 | 636 | , fNoiseAccess(noiseTexture) |
michael@0 | 637 | , fStitchData(stitchData) { |
michael@0 | 638 | this->addTextureAccess(&fPermutationsAccess); |
michael@0 | 639 | this->addTextureAccess(&fNoiseAccess); |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | GR_DECLARE_EFFECT_TEST; |
michael@0 | 643 | |
michael@0 | 644 | GrTextureAccess fPermutationsAccess; |
michael@0 | 645 | GrTextureAccess fNoiseAccess; |
michael@0 | 646 | SkPerlinNoiseShader::StitchData fStitchData; |
michael@0 | 647 | |
michael@0 | 648 | typedef GrNoiseEffect INHERITED; |
michael@0 | 649 | }; |
michael@0 | 650 | |
michael@0 | 651 | class GrSimplexNoiseEffect : public GrNoiseEffect { |
michael@0 | 652 | // Note : This is for reference only. GrPerlinNoiseEffect is used for processing. |
michael@0 | 653 | public: |
michael@0 | 654 | static GrEffectRef* Create(SkPerlinNoiseShader::Type type, const SkVector& baseFrequency, |
michael@0 | 655 | int numOctaves, bool stitchTiles, const SkScalar seed, |
michael@0 | 656 | const SkMatrix& matrix, uint8_t alpha) { |
michael@0 | 657 | AutoEffectUnref effect(SkNEW_ARGS(GrSimplexNoiseEffect, (type, baseFrequency, numOctaves, |
michael@0 | 658 | stitchTiles, seed, matrix, alpha))); |
michael@0 | 659 | return CreateEffectRef(effect); |
michael@0 | 660 | } |
michael@0 | 661 | |
michael@0 | 662 | virtual ~GrSimplexNoiseEffect() { } |
michael@0 | 663 | |
michael@0 | 664 | static const char* Name() { return "SimplexNoise"; } |
michael@0 | 665 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
michael@0 | 666 | return GrTBackendEffectFactory<GrSimplexNoiseEffect>::getInstance(); |
michael@0 | 667 | } |
michael@0 | 668 | const SkScalar& seed() const { return fSeed; } |
michael@0 | 669 | |
michael@0 | 670 | typedef GrGLSimplexNoise GLEffect; |
michael@0 | 671 | |
michael@0 | 672 | private: |
michael@0 | 673 | virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { |
michael@0 | 674 | const GrSimplexNoiseEffect& s = CastEffect<GrSimplexNoiseEffect>(sBase); |
michael@0 | 675 | return INHERITED::onIsEqual(sBase) && fSeed == s.fSeed; |
michael@0 | 676 | } |
michael@0 | 677 | |
michael@0 | 678 | GrSimplexNoiseEffect(SkPerlinNoiseShader::Type type, const SkVector& baseFrequency, |
michael@0 | 679 | int numOctaves, bool stitchTiles, const SkScalar seed, |
michael@0 | 680 | const SkMatrix& matrix, uint8_t alpha) |
michael@0 | 681 | : GrNoiseEffect(type, baseFrequency, numOctaves, stitchTiles, matrix, alpha) |
michael@0 | 682 | , fSeed(seed) { |
michael@0 | 683 | } |
michael@0 | 684 | |
michael@0 | 685 | SkScalar fSeed; |
michael@0 | 686 | |
michael@0 | 687 | typedef GrNoiseEffect INHERITED; |
michael@0 | 688 | }; |
michael@0 | 689 | |
michael@0 | 690 | ///////////////////////////////////////////////////////////////////// |
michael@0 | 691 | GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect); |
michael@0 | 692 | |
michael@0 | 693 | GrEffectRef* GrPerlinNoiseEffect::TestCreate(SkRandom* random, |
michael@0 | 694 | GrContext* context, |
michael@0 | 695 | const GrDrawTargetCaps&, |
michael@0 | 696 | GrTexture**) { |
michael@0 | 697 | int numOctaves = random->nextRangeU(2, 10); |
michael@0 | 698 | bool stitchTiles = random->nextBool(); |
michael@0 | 699 | SkScalar seed = SkIntToScalar(random->nextU()); |
michael@0 | 700 | SkISize tileSize = SkISize::Make(random->nextRangeU(4, 4096), random->nextRangeU(4, 4096)); |
michael@0 | 701 | SkScalar baseFrequencyX = random->nextRangeScalar(0.01f, |
michael@0 | 702 | 0.99f); |
michael@0 | 703 | SkScalar baseFrequencyY = random->nextRangeScalar(0.01f, |
michael@0 | 704 | 0.99f); |
michael@0 | 705 | |
michael@0 | 706 | SkShader* shader = random->nextBool() ? |
michael@0 | 707 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
michael@0 | 708 | stitchTiles ? &tileSize : NULL) : |
michael@0 | 709 | SkPerlinNoiseShader::CreateTubulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
michael@0 | 710 | stitchTiles ? &tileSize : NULL); |
michael@0 | 711 | |
michael@0 | 712 | SkPaint paint; |
michael@0 | 713 | GrEffectRef* effect = shader->asNewEffect(context, paint); |
michael@0 | 714 | |
michael@0 | 715 | SkDELETE(shader); |
michael@0 | 716 | |
michael@0 | 717 | return effect; |
michael@0 | 718 | } |
michael@0 | 719 | |
michael@0 | 720 | ///////////////////////////////////////////////////////////////////// |
michael@0 | 721 | |
michael@0 | 722 | void GrGLSimplexNoise::emitCode(GrGLShaderBuilder* builder, |
michael@0 | 723 | const GrDrawEffect&, |
michael@0 | 724 | EffectKey key, |
michael@0 | 725 | const char* outputColor, |
michael@0 | 726 | const char* inputColor, |
michael@0 | 727 | const TransformedCoordsArray& coords, |
michael@0 | 728 | const TextureSamplerArray&) { |
michael@0 | 729 | sk_ignore_unused_variable(inputColor); |
michael@0 | 730 | |
michael@0 | 731 | SkString vCoords = builder->ensureFSCoords2D(coords, 0); |
michael@0 | 732 | |
michael@0 | 733 | fSeedUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 734 | kFloat_GrSLType, "seed"); |
michael@0 | 735 | const char* seedUni = builder->getUniformCStr(fSeedUni); |
michael@0 | 736 | fInvMatrixUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 737 | kMat33f_GrSLType, "invMatrix"); |
michael@0 | 738 | const char* invMatrixUni = builder->getUniformCStr(fInvMatrixUni); |
michael@0 | 739 | fBaseFrequencyUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 740 | kVec2f_GrSLType, "baseFrequency"); |
michael@0 | 741 | const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni); |
michael@0 | 742 | fAlphaUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 743 | kFloat_GrSLType, "alpha"); |
michael@0 | 744 | const char* alphaUni = builder->getUniformCStr(fAlphaUni); |
michael@0 | 745 | |
michael@0 | 746 | // Add vec3 modulo 289 function |
michael@0 | 747 | static const GrGLShaderVar gVec3Args[] = { |
michael@0 | 748 | GrGLShaderVar("x", kVec3f_GrSLType) |
michael@0 | 749 | }; |
michael@0 | 750 | |
michael@0 | 751 | SkString mod289_3_funcName; |
michael@0 | 752 | builder->fsEmitFunction(kVec3f_GrSLType, |
michael@0 | 753 | "mod289", SK_ARRAY_COUNT(gVec3Args), gVec3Args, |
michael@0 | 754 | "const vec2 C = vec2(1.0 / 289.0, 289.0);\n" |
michael@0 | 755 | "return x - floor(x * C.xxx) * C.yyy;", &mod289_3_funcName); |
michael@0 | 756 | |
michael@0 | 757 | // Add vec4 modulo 289 function |
michael@0 | 758 | static const GrGLShaderVar gVec4Args[] = { |
michael@0 | 759 | GrGLShaderVar("x", kVec4f_GrSLType) |
michael@0 | 760 | }; |
michael@0 | 761 | |
michael@0 | 762 | SkString mod289_4_funcName; |
michael@0 | 763 | builder->fsEmitFunction(kVec4f_GrSLType, |
michael@0 | 764 | "mod289", SK_ARRAY_COUNT(gVec4Args), gVec4Args, |
michael@0 | 765 | "const vec2 C = vec2(1.0 / 289.0, 289.0);\n" |
michael@0 | 766 | "return x - floor(x * C.xxxx) * C.yyyy;", &mod289_4_funcName); |
michael@0 | 767 | |
michael@0 | 768 | // Add vec4 permute function |
michael@0 | 769 | SkString permuteCode; |
michael@0 | 770 | permuteCode.appendf("const vec2 C = vec2(34.0, 1.0);\n" |
michael@0 | 771 | "return %s(((x * C.xxxx) + C.yyyy) * x);", mod289_4_funcName.c_str()); |
michael@0 | 772 | SkString permuteFuncName; |
michael@0 | 773 | builder->fsEmitFunction(kVec4f_GrSLType, |
michael@0 | 774 | "permute", SK_ARRAY_COUNT(gVec4Args), gVec4Args, |
michael@0 | 775 | permuteCode.c_str(), &permuteFuncName); |
michael@0 | 776 | |
michael@0 | 777 | // Add vec4 taylorInvSqrt function |
michael@0 | 778 | SkString taylorInvSqrtFuncName; |
michael@0 | 779 | builder->fsEmitFunction(kVec4f_GrSLType, |
michael@0 | 780 | "taylorInvSqrt", SK_ARRAY_COUNT(gVec4Args), gVec4Args, |
michael@0 | 781 | "const vec2 C = vec2(-0.85373472095314, 1.79284291400159);\n" |
michael@0 | 782 | "return x * C.xxxx + C.yyyy;", &taylorInvSqrtFuncName); |
michael@0 | 783 | |
michael@0 | 784 | // Add vec3 noise function |
michael@0 | 785 | static const GrGLShaderVar gNoiseVec3Args[] = { |
michael@0 | 786 | GrGLShaderVar("v", kVec3f_GrSLType) |
michael@0 | 787 | }; |
michael@0 | 788 | |
michael@0 | 789 | SkString noiseCode; |
michael@0 | 790 | noiseCode.append( |
michael@0 | 791 | "const vec2 C = vec2(1.0/6.0, 1.0/3.0);\n" |
michael@0 | 792 | "const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);\n" |
michael@0 | 793 | |
michael@0 | 794 | // First corner |
michael@0 | 795 | "vec3 i = floor(v + dot(v, C.yyy));\n" |
michael@0 | 796 | "vec3 x0 = v - i + dot(i, C.xxx);\n" |
michael@0 | 797 | |
michael@0 | 798 | // Other corners |
michael@0 | 799 | "vec3 g = step(x0.yzx, x0.xyz);\n" |
michael@0 | 800 | "vec3 l = 1.0 - g;\n" |
michael@0 | 801 | "vec3 i1 = min(g.xyz, l.zxy);\n" |
michael@0 | 802 | "vec3 i2 = max(g.xyz, l.zxy);\n" |
michael@0 | 803 | |
michael@0 | 804 | "vec3 x1 = x0 - i1 + C.xxx;\n" |
michael@0 | 805 | "vec3 x2 = x0 - i2 + C.yyy;\n" // 2.0*C.x = 1/3 = C.y |
michael@0 | 806 | "vec3 x3 = x0 - D.yyy;\n" // -1.0+3.0*C.x = -0.5 = -D.y |
michael@0 | 807 | ); |
michael@0 | 808 | |
michael@0 | 809 | noiseCode.appendf( |
michael@0 | 810 | // Permutations |
michael@0 | 811 | "i = %s(i);\n" |
michael@0 | 812 | "vec4 p = %s(%s(%s(\n" |
michael@0 | 813 | " i.z + vec4(0.0, i1.z, i2.z, 1.0)) +\n" |
michael@0 | 814 | " i.y + vec4(0.0, i1.y, i2.y, 1.0)) +\n" |
michael@0 | 815 | " i.x + vec4(0.0, i1.x, i2.x, 1.0));\n", |
michael@0 | 816 | mod289_3_funcName.c_str(), permuteFuncName.c_str(), permuteFuncName.c_str(), |
michael@0 | 817 | permuteFuncName.c_str()); |
michael@0 | 818 | |
michael@0 | 819 | noiseCode.append( |
michael@0 | 820 | // Gradients: 7x7 points over a square, mapped onto an octahedron. |
michael@0 | 821 | // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294) |
michael@0 | 822 | "float n_ = 0.142857142857;\n" // 1.0/7.0 |
michael@0 | 823 | "vec3 ns = n_ * D.wyz - D.xzx;\n" |
michael@0 | 824 | |
michael@0 | 825 | "vec4 j = p - 49.0 * floor(p * ns.z * ns.z);\n" // mod(p,7*7) |
michael@0 | 826 | |
michael@0 | 827 | "vec4 x_ = floor(j * ns.z);\n" |
michael@0 | 828 | "vec4 y_ = floor(j - 7.0 * x_);" // mod(j,N) |
michael@0 | 829 | |
michael@0 | 830 | "vec4 x = x_ *ns.x + ns.yyyy;\n" |
michael@0 | 831 | "vec4 y = y_ *ns.x + ns.yyyy;\n" |
michael@0 | 832 | "vec4 h = 1.0 - abs(x) - abs(y);\n" |
michael@0 | 833 | |
michael@0 | 834 | "vec4 b0 = vec4(x.xy, y.xy);\n" |
michael@0 | 835 | "vec4 b1 = vec4(x.zw, y.zw);\n" |
michael@0 | 836 | ); |
michael@0 | 837 | |
michael@0 | 838 | noiseCode.append( |
michael@0 | 839 | "vec4 s0 = floor(b0) * 2.0 + 1.0;\n" |
michael@0 | 840 | "vec4 s1 = floor(b1) * 2.0 + 1.0;\n" |
michael@0 | 841 | "vec4 sh = -step(h, vec4(0.0));\n" |
michael@0 | 842 | |
michael@0 | 843 | "vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;\n" |
michael@0 | 844 | "vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;\n" |
michael@0 | 845 | |
michael@0 | 846 | "vec3 p0 = vec3(a0.xy, h.x);\n" |
michael@0 | 847 | "vec3 p1 = vec3(a0.zw, h.y);\n" |
michael@0 | 848 | "vec3 p2 = vec3(a1.xy, h.z);\n" |
michael@0 | 849 | "vec3 p3 = vec3(a1.zw, h.w);\n" |
michael@0 | 850 | ); |
michael@0 | 851 | |
michael@0 | 852 | noiseCode.appendf( |
michael@0 | 853 | // Normalise gradients |
michael@0 | 854 | "vec4 norm = %s(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));\n" |
michael@0 | 855 | "p0 *= norm.x;\n" |
michael@0 | 856 | "p1 *= norm.y;\n" |
michael@0 | 857 | "p2 *= norm.z;\n" |
michael@0 | 858 | "p3 *= norm.w;\n" |
michael@0 | 859 | |
michael@0 | 860 | // Mix final noise value |
michael@0 | 861 | "vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);\n" |
michael@0 | 862 | "m = m * m;\n" |
michael@0 | 863 | "return 42.0 * dot(m*m, vec4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3)));", |
michael@0 | 864 | taylorInvSqrtFuncName.c_str()); |
michael@0 | 865 | |
michael@0 | 866 | SkString noiseFuncName; |
michael@0 | 867 | builder->fsEmitFunction(kFloat_GrSLType, |
michael@0 | 868 | "snoise", SK_ARRAY_COUNT(gNoiseVec3Args), gNoiseVec3Args, |
michael@0 | 869 | noiseCode.c_str(), &noiseFuncName); |
michael@0 | 870 | |
michael@0 | 871 | const char* noiseVecIni = "noiseVecIni"; |
michael@0 | 872 | const char* factors = "factors"; |
michael@0 | 873 | const char* sum = "sum"; |
michael@0 | 874 | const char* xOffsets = "xOffsets"; |
michael@0 | 875 | const char* yOffsets = "yOffsets"; |
michael@0 | 876 | const char* channel = "channel"; |
michael@0 | 877 | |
michael@0 | 878 | // Fill with some prime numbers |
michael@0 | 879 | builder->fsCodeAppendf("\t\tconst vec4 %s = vec4(13.0, 53.0, 101.0, 151.0);\n", xOffsets); |
michael@0 | 880 | builder->fsCodeAppendf("\t\tconst vec4 %s = vec4(109.0, 167.0, 23.0, 67.0);\n", yOffsets); |
michael@0 | 881 | |
michael@0 | 882 | // There are rounding errors if the floor operation is not performed here |
michael@0 | 883 | builder->fsCodeAppendf( |
michael@0 | 884 | "\t\tvec3 %s = vec3(floor((%s*vec3(%s, 1.0)).xy) * vec2(0.66) * %s, 0.0);\n", |
michael@0 | 885 | noiseVecIni, invMatrixUni, vCoords.c_str(), baseFrequencyUni); |
michael@0 | 886 | |
michael@0 | 887 | // Perturb the texcoords with three components of noise |
michael@0 | 888 | builder->fsCodeAppendf("\t\t%s += 0.1 * vec3(%s(%s + vec3( 0.0, 0.0, %s))," |
michael@0 | 889 | "%s(%s + vec3( 43.0, 17.0, %s))," |
michael@0 | 890 | "%s(%s + vec3(-17.0, -43.0, %s)));\n", |
michael@0 | 891 | noiseVecIni, noiseFuncName.c_str(), noiseVecIni, seedUni, |
michael@0 | 892 | noiseFuncName.c_str(), noiseVecIni, seedUni, |
michael@0 | 893 | noiseFuncName.c_str(), noiseVecIni, seedUni); |
michael@0 | 894 | |
michael@0 | 895 | builder->fsCodeAppendf("\t\t%s = vec4(0.0);\n", outputColor); |
michael@0 | 896 | |
michael@0 | 897 | builder->fsCodeAppendf("\t\tvec3 %s = vec3(1.0);\n", factors); |
michael@0 | 898 | builder->fsCodeAppendf("\t\tfloat %s = 0.0;\n", sum); |
michael@0 | 899 | |
michael@0 | 900 | // Loop over all octaves |
michael@0 | 901 | builder->fsCodeAppendf("\t\tfor (int octave = 0; octave < %d; ++octave) {\n", fNumOctaves); |
michael@0 | 902 | |
michael@0 | 903 | // Loop over the 4 channels |
michael@0 | 904 | builder->fsCodeAppendf("\t\t\tfor (int %s = 3; %s >= 0; --%s) {\n", channel, channel, channel); |
michael@0 | 905 | |
michael@0 | 906 | builder->fsCodeAppendf( |
michael@0 | 907 | "\t\t\t\t%s[channel] += %s.x * %s(%s * %s.yyy - vec3(%s[%s], %s[%s], %s * %s.z));\n", |
michael@0 | 908 | outputColor, factors, noiseFuncName.c_str(), noiseVecIni, factors, xOffsets, channel, |
michael@0 | 909 | yOffsets, channel, seedUni, factors); |
michael@0 | 910 | |
michael@0 | 911 | builder->fsCodeAppend("\t\t\t}\n"); // end of the for loop on channels |
michael@0 | 912 | |
michael@0 | 913 | builder->fsCodeAppendf("\t\t\t%s += %s.x;\n", sum, factors); |
michael@0 | 914 | builder->fsCodeAppendf("\t\t\t%s *= vec3(0.5, 2.0, 0.75);\n", factors); |
michael@0 | 915 | |
michael@0 | 916 | builder->fsCodeAppend("\t\t}\n"); // end of the for loop on octaves |
michael@0 | 917 | |
michael@0 | 918 | if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { |
michael@0 | 919 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
michael@0 | 920 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
michael@0 | 921 | builder->fsCodeAppendf("\t\t%s = %s * vec4(0.5 / %s) + vec4(0.5);\n", |
michael@0 | 922 | outputColor, outputColor, sum); |
michael@0 | 923 | } else { |
michael@0 | 924 | builder->fsCodeAppendf("\t\t%s = abs(%s / vec4(%s));\n", |
michael@0 | 925 | outputColor, outputColor, sum); |
michael@0 | 926 | } |
michael@0 | 927 | |
michael@0 | 928 | builder->fsCodeAppendf("\t\t%s.a *= %s;\n", outputColor, alphaUni); |
michael@0 | 929 | |
michael@0 | 930 | // Clamp values |
michael@0 | 931 | builder->fsCodeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outputColor); |
michael@0 | 932 | |
michael@0 | 933 | // Pre-multiply the result |
michael@0 | 934 | builder->fsCodeAppendf("\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", |
michael@0 | 935 | outputColor, outputColor, outputColor, outputColor); |
michael@0 | 936 | } |
michael@0 | 937 | |
michael@0 | 938 | void GrGLPerlinNoise::emitCode(GrGLShaderBuilder* builder, |
michael@0 | 939 | const GrDrawEffect&, |
michael@0 | 940 | EffectKey key, |
michael@0 | 941 | const char* outputColor, |
michael@0 | 942 | const char* inputColor, |
michael@0 | 943 | const TransformedCoordsArray& coords, |
michael@0 | 944 | const TextureSamplerArray& samplers) { |
michael@0 | 945 | sk_ignore_unused_variable(inputColor); |
michael@0 | 946 | |
michael@0 | 947 | SkString vCoords = builder->ensureFSCoords2D(coords, 0); |
michael@0 | 948 | |
michael@0 | 949 | fInvMatrixUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 950 | kMat33f_GrSLType, "invMatrix"); |
michael@0 | 951 | const char* invMatrixUni = builder->getUniformCStr(fInvMatrixUni); |
michael@0 | 952 | fBaseFrequencyUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 953 | kVec2f_GrSLType, "baseFrequency"); |
michael@0 | 954 | const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni); |
michael@0 | 955 | fAlphaUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 956 | kFloat_GrSLType, "alpha"); |
michael@0 | 957 | const char* alphaUni = builder->getUniformCStr(fAlphaUni); |
michael@0 | 958 | |
michael@0 | 959 | const char* stitchDataUni = NULL; |
michael@0 | 960 | if (fStitchTiles) { |
michael@0 | 961 | fStitchDataUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, |
michael@0 | 962 | kVec2f_GrSLType, "stitchData"); |
michael@0 | 963 | stitchDataUni = builder->getUniformCStr(fStitchDataUni); |
michael@0 | 964 | } |
michael@0 | 965 | |
michael@0 | 966 | // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 |
michael@0 | 967 | const char* chanCoordR = "0.125"; |
michael@0 | 968 | const char* chanCoordG = "0.375"; |
michael@0 | 969 | const char* chanCoordB = "0.625"; |
michael@0 | 970 | const char* chanCoordA = "0.875"; |
michael@0 | 971 | const char* chanCoord = "chanCoord"; |
michael@0 | 972 | const char* stitchData = "stitchData"; |
michael@0 | 973 | const char* ratio = "ratio"; |
michael@0 | 974 | const char* noiseXY = "noiseXY"; |
michael@0 | 975 | const char* noiseVec = "noiseVec"; |
michael@0 | 976 | const char* noiseSmooth = "noiseSmooth"; |
michael@0 | 977 | const char* fractVal = "fractVal"; |
michael@0 | 978 | const char* uv = "uv"; |
michael@0 | 979 | const char* ab = "ab"; |
michael@0 | 980 | const char* latticeIdx = "latticeIdx"; |
michael@0 | 981 | const char* lattice = "lattice"; |
michael@0 | 982 | const char* inc8bit = "0.00390625"; // 1.0 / 256.0 |
michael@0 | 983 | // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a |
michael@0 | 984 | // [-1,1] vector and perform a dot product between that vector and the provided vector. |
michael@0 | 985 | const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);"; |
michael@0 | 986 | |
michael@0 | 987 | // Add noise function |
michael@0 | 988 | static const GrGLShaderVar gPerlinNoiseArgs[] = { |
michael@0 | 989 | GrGLShaderVar(chanCoord, kFloat_GrSLType), |
michael@0 | 990 | GrGLShaderVar(noiseVec, kVec2f_GrSLType) |
michael@0 | 991 | }; |
michael@0 | 992 | |
michael@0 | 993 | static const GrGLShaderVar gPerlinNoiseStitchArgs[] = { |
michael@0 | 994 | GrGLShaderVar(chanCoord, kFloat_GrSLType), |
michael@0 | 995 | GrGLShaderVar(noiseVec, kVec2f_GrSLType), |
michael@0 | 996 | GrGLShaderVar(stitchData, kVec2f_GrSLType) |
michael@0 | 997 | }; |
michael@0 | 998 | |
michael@0 | 999 | SkString noiseCode; |
michael@0 | 1000 | |
michael@0 | 1001 | noiseCode.appendf("\tvec4 %s = vec4(floor(%s), fract(%s));", noiseXY, noiseVec, noiseVec); |
michael@0 | 1002 | |
michael@0 | 1003 | // smooth curve : t * t * (3 - 2 * t) |
michael@0 | 1004 | noiseCode.appendf("\n\tvec2 %s = %s.zw * %s.zw * (vec2(3.0) - vec2(2.0) * %s.zw);", |
michael@0 | 1005 | noiseSmooth, noiseXY, noiseXY, noiseXY); |
michael@0 | 1006 | |
michael@0 | 1007 | // Adjust frequencies if we're stitching tiles |
michael@0 | 1008 | if (fStitchTiles) { |
michael@0 | 1009 | noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }", |
michael@0 | 1010 | noiseXY, stitchData, noiseXY, stitchData); |
michael@0 | 1011 | noiseCode.appendf("\n\tif(%s.x >= (%s.x - 1.0)) { %s.x -= (%s.x - 1.0); }", |
michael@0 | 1012 | noiseXY, stitchData, noiseXY, stitchData); |
michael@0 | 1013 | noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }", |
michael@0 | 1014 | noiseXY, stitchData, noiseXY, stitchData); |
michael@0 | 1015 | noiseCode.appendf("\n\tif(%s.y >= (%s.y - 1.0)) { %s.y -= (%s.y - 1.0); }", |
michael@0 | 1016 | noiseXY, stitchData, noiseXY, stitchData); |
michael@0 | 1017 | } |
michael@0 | 1018 | |
michael@0 | 1019 | // Get texture coordinates and normalize |
michael@0 | 1020 | noiseCode.appendf("\n\t%s.xy = fract(floor(mod(%s.xy, 256.0)) / vec2(256.0));\n", |
michael@0 | 1021 | noiseXY, noiseXY); |
michael@0 | 1022 | |
michael@0 | 1023 | // Get permutation for x |
michael@0 | 1024 | { |
michael@0 | 1025 | SkString xCoords(""); |
michael@0 | 1026 | xCoords.appendf("vec2(%s.x, 0.5)", noiseXY); |
michael@0 | 1027 | |
michael@0 | 1028 | noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); |
michael@0 | 1029 | builder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType); |
michael@0 | 1030 | noiseCode.append(".r;"); |
michael@0 | 1031 | } |
michael@0 | 1032 | |
michael@0 | 1033 | // Get permutation for x + 1 |
michael@0 | 1034 | { |
michael@0 | 1035 | SkString xCoords(""); |
michael@0 | 1036 | xCoords.appendf("vec2(fract(%s.x + %s), 0.5)", noiseXY, inc8bit); |
michael@0 | 1037 | |
michael@0 | 1038 | noiseCode.appendf("\n\t%s.y = ", latticeIdx); |
michael@0 | 1039 | builder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType); |
michael@0 | 1040 | noiseCode.append(".r;"); |
michael@0 | 1041 | } |
michael@0 | 1042 | |
michael@0 | 1043 | #if defined(SK_BUILD_FOR_ANDROID) |
michael@0 | 1044 | // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3). |
michael@0 | 1045 | // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit |
michael@0 | 1046 | // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725 |
michael@0 | 1047 | // (or 0.484368 here). The following rounding operation prevents these precision issues from |
michael@0 | 1048 | // affecting the result of the noise by making sure that we only have multiples of 1/255. |
michael@0 | 1049 | // (Note that 1/255 is about 0.003921569, which is the value used here). |
michael@0 | 1050 | noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);", |
michael@0 | 1051 | latticeIdx, latticeIdx); |
michael@0 | 1052 | #endif |
michael@0 | 1053 | |
michael@0 | 1054 | // Get (x,y) coordinates with the permutated x |
michael@0 | 1055 | noiseCode.appendf("\n\t%s = fract(%s + %s.yy);", latticeIdx, latticeIdx, noiseXY); |
michael@0 | 1056 | |
michael@0 | 1057 | noiseCode.appendf("\n\tvec2 %s = %s.zw;", fractVal, noiseXY); |
michael@0 | 1058 | |
michael@0 | 1059 | noiseCode.appendf("\n\n\tvec2 %s;", uv); |
michael@0 | 1060 | // Compute u, at offset (0,0) |
michael@0 | 1061 | { |
michael@0 | 1062 | SkString latticeCoords(""); |
michael@0 | 1063 | latticeCoords.appendf("vec2(%s.x, %s)", latticeIdx, chanCoord); |
michael@0 | 1064 | noiseCode.appendf("\n\tvec4 %s = ", lattice); |
michael@0 | 1065 | builder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(), |
michael@0 | 1066 | kVec2f_GrSLType); |
michael@0 | 1067 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
michael@0 | 1068 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
michael@0 | 1069 | } |
michael@0 | 1070 | |
michael@0 | 1071 | noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); |
michael@0 | 1072 | // Compute v, at offset (-1,0) |
michael@0 | 1073 | { |
michael@0 | 1074 | SkString latticeCoords(""); |
michael@0 | 1075 | latticeCoords.appendf("vec2(%s.y, %s)", latticeIdx, chanCoord); |
michael@0 | 1076 | noiseCode.append("\n\tlattice = "); |
michael@0 | 1077 | builder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(), |
michael@0 | 1078 | kVec2f_GrSLType); |
michael@0 | 1079 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
michael@0 | 1080 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
michael@0 | 1081 | } |
michael@0 | 1082 | |
michael@0 | 1083 | // Compute 'a' as a linear interpolation of 'u' and 'v' |
michael@0 | 1084 | noiseCode.appendf("\n\tvec2 %s;", ab); |
michael@0 | 1085 | noiseCode.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmooth); |
michael@0 | 1086 | |
michael@0 | 1087 | noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); |
michael@0 | 1088 | // Compute v, at offset (-1,-1) |
michael@0 | 1089 | { |
michael@0 | 1090 | SkString latticeCoords(""); |
michael@0 | 1091 | latticeCoords.appendf("vec2(fract(%s.y + %s), %s)", latticeIdx, inc8bit, chanCoord); |
michael@0 | 1092 | noiseCode.append("\n\tlattice = "); |
michael@0 | 1093 | builder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(), |
michael@0 | 1094 | kVec2f_GrSLType); |
michael@0 | 1095 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
michael@0 | 1096 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
michael@0 | 1097 | } |
michael@0 | 1098 | |
michael@0 | 1099 | noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); |
michael@0 | 1100 | // Compute u, at offset (0,-1) |
michael@0 | 1101 | { |
michael@0 | 1102 | SkString latticeCoords(""); |
michael@0 | 1103 | latticeCoords.appendf("vec2(fract(%s.x + %s), %s)", latticeIdx, inc8bit, chanCoord); |
michael@0 | 1104 | noiseCode.append("\n\tlattice = "); |
michael@0 | 1105 | builder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(), |
michael@0 | 1106 | kVec2f_GrSLType); |
michael@0 | 1107 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
michael@0 | 1108 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
michael@0 | 1109 | } |
michael@0 | 1110 | |
michael@0 | 1111 | // Compute 'b' as a linear interpolation of 'u' and 'v' |
michael@0 | 1112 | noiseCode.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmooth); |
michael@0 | 1113 | // Compute the noise as a linear interpolation of 'a' and 'b' |
michael@0 | 1114 | noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth); |
michael@0 | 1115 | |
michael@0 | 1116 | SkString noiseFuncName; |
michael@0 | 1117 | if (fStitchTiles) { |
michael@0 | 1118 | builder->fsEmitFunction(kFloat_GrSLType, |
michael@0 | 1119 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs), |
michael@0 | 1120 | gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName); |
michael@0 | 1121 | } else { |
michael@0 | 1122 | builder->fsEmitFunction(kFloat_GrSLType, |
michael@0 | 1123 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs), |
michael@0 | 1124 | gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName); |
michael@0 | 1125 | } |
michael@0 | 1126 | |
michael@0 | 1127 | // There are rounding errors if the floor operation is not performed here |
michael@0 | 1128 | builder->fsCodeAppendf("\n\t\tvec2 %s = floor((%s * vec3(%s, 1.0)).xy) * %s;", |
michael@0 | 1129 | noiseVec, invMatrixUni, vCoords.c_str(), baseFrequencyUni); |
michael@0 | 1130 | |
michael@0 | 1131 | // Clear the color accumulator |
michael@0 | 1132 | builder->fsCodeAppendf("\n\t\t%s = vec4(0.0);", outputColor); |
michael@0 | 1133 | |
michael@0 | 1134 | if (fStitchTiles) { |
michael@0 | 1135 | // Set up TurbulenceInitial stitch values. |
michael@0 | 1136 | builder->fsCodeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni); |
michael@0 | 1137 | } |
michael@0 | 1138 | |
michael@0 | 1139 | builder->fsCodeAppendf("\n\t\tfloat %s = 1.0;", ratio); |
michael@0 | 1140 | |
michael@0 | 1141 | // Loop over all octaves |
michael@0 | 1142 | builder->fsCodeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves); |
michael@0 | 1143 | |
michael@0 | 1144 | builder->fsCodeAppendf("\n\t\t\t%s += ", outputColor); |
michael@0 | 1145 | if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { |
michael@0 | 1146 | builder->fsCodeAppend("abs("); |
michael@0 | 1147 | } |
michael@0 | 1148 | if (fStitchTiles) { |
michael@0 | 1149 | builder->fsCodeAppendf( |
michael@0 | 1150 | "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s)," |
michael@0 | 1151 | "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))", |
michael@0 | 1152 | noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData, |
michael@0 | 1153 | noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData, |
michael@0 | 1154 | noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData, |
michael@0 | 1155 | noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData); |
michael@0 | 1156 | } else { |
michael@0 | 1157 | builder->fsCodeAppendf( |
michael@0 | 1158 | "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s)," |
michael@0 | 1159 | "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))", |
michael@0 | 1160 | noiseFuncName.c_str(), chanCoordR, noiseVec, |
michael@0 | 1161 | noiseFuncName.c_str(), chanCoordG, noiseVec, |
michael@0 | 1162 | noiseFuncName.c_str(), chanCoordB, noiseVec, |
michael@0 | 1163 | noiseFuncName.c_str(), chanCoordA, noiseVec); |
michael@0 | 1164 | } |
michael@0 | 1165 | if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { |
michael@0 | 1166 | builder->fsCodeAppendf(")"); // end of "abs(" |
michael@0 | 1167 | } |
michael@0 | 1168 | builder->fsCodeAppendf(" * %s;", ratio); |
michael@0 | 1169 | |
michael@0 | 1170 | builder->fsCodeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec); |
michael@0 | 1171 | builder->fsCodeAppendf("\n\t\t\t%s *= 0.5;", ratio); |
michael@0 | 1172 | |
michael@0 | 1173 | if (fStitchTiles) { |
michael@0 | 1174 | builder->fsCodeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData); |
michael@0 | 1175 | } |
michael@0 | 1176 | builder->fsCodeAppend("\n\t\t}"); // end of the for loop on octaves |
michael@0 | 1177 | |
michael@0 | 1178 | if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { |
michael@0 | 1179 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
michael@0 | 1180 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
michael@0 | 1181 | builder->fsCodeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", outputColor, outputColor); |
michael@0 | 1182 | } |
michael@0 | 1183 | |
michael@0 | 1184 | builder->fsCodeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni); |
michael@0 | 1185 | |
michael@0 | 1186 | // Clamp values |
michael@0 | 1187 | builder->fsCodeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", outputColor, outputColor); |
michael@0 | 1188 | |
michael@0 | 1189 | // Pre-multiply the result |
michael@0 | 1190 | builder->fsCodeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", |
michael@0 | 1191 | outputColor, outputColor, outputColor, outputColor); |
michael@0 | 1192 | } |
michael@0 | 1193 | |
michael@0 | 1194 | GrGLNoise::GrGLNoise(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect) |
michael@0 | 1195 | : INHERITED (factory) |
michael@0 | 1196 | , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type()) |
michael@0 | 1197 | , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles()) |
michael@0 | 1198 | , fNumOctaves(drawEffect.castEffect<GrPerlinNoiseEffect>().numOctaves()) { |
michael@0 | 1199 | } |
michael@0 | 1200 | |
michael@0 | 1201 | GrGLEffect::EffectKey GrGLNoise::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
michael@0 | 1202 | const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>(); |
michael@0 | 1203 | |
michael@0 | 1204 | EffectKey key = turbulence.numOctaves(); |
michael@0 | 1205 | |
michael@0 | 1206 | key = key << 3; // Make room for next 3 bits |
michael@0 | 1207 | |
michael@0 | 1208 | switch (turbulence.type()) { |
michael@0 | 1209 | case SkPerlinNoiseShader::kFractalNoise_Type: |
michael@0 | 1210 | key |= 0x1; |
michael@0 | 1211 | break; |
michael@0 | 1212 | case SkPerlinNoiseShader::kTurbulence_Type: |
michael@0 | 1213 | key |= 0x2; |
michael@0 | 1214 | break; |
michael@0 | 1215 | default: |
michael@0 | 1216 | // leave key at 0 |
michael@0 | 1217 | break; |
michael@0 | 1218 | } |
michael@0 | 1219 | |
michael@0 | 1220 | if (turbulence.stitchTiles()) { |
michael@0 | 1221 | key |= 0x4; // Flip the 3rd bit if tile stitching is on |
michael@0 | 1222 | } |
michael@0 | 1223 | |
michael@0 | 1224 | return key; |
michael@0 | 1225 | } |
michael@0 | 1226 | |
michael@0 | 1227 | void GrGLNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { |
michael@0 | 1228 | const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>(); |
michael@0 | 1229 | |
michael@0 | 1230 | const SkVector& baseFrequency = turbulence.baseFrequency(); |
michael@0 | 1231 | uman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
michael@0 | 1232 | uman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntToScalar(255))); |
michael@0 | 1233 | |
michael@0 | 1234 | SkMatrix m = turbulence.matrix(); |
michael@0 | 1235 | m.postTranslate(-SK_Scalar1, -SK_Scalar1); |
michael@0 | 1236 | SkMatrix invM; |
michael@0 | 1237 | if (!m.invert(&invM)) { |
michael@0 | 1238 | invM.reset(); |
michael@0 | 1239 | } else { |
michael@0 | 1240 | invM.postConcat(invM); // Square the matrix |
michael@0 | 1241 | } |
michael@0 | 1242 | uman.setSkMatrix(fInvMatrixUni, invM); |
michael@0 | 1243 | } |
michael@0 | 1244 | |
michael@0 | 1245 | void GrGLPerlinNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { |
michael@0 | 1246 | INHERITED::setData(uman, drawEffect); |
michael@0 | 1247 | |
michael@0 | 1248 | const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>(); |
michael@0 | 1249 | if (turbulence.stitchTiles()) { |
michael@0 | 1250 | const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData(); |
michael@0 | 1251 | uman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
michael@0 | 1252 | SkIntToScalar(stitchData.fHeight)); |
michael@0 | 1253 | } |
michael@0 | 1254 | } |
michael@0 | 1255 | |
michael@0 | 1256 | void GrGLSimplexNoise::setData(const GrGLUniformManager& uman, const GrDrawEffect& drawEffect) { |
michael@0 | 1257 | INHERITED::setData(uman, drawEffect); |
michael@0 | 1258 | |
michael@0 | 1259 | const GrSimplexNoiseEffect& turbulence = drawEffect.castEffect<GrSimplexNoiseEffect>(); |
michael@0 | 1260 | uman.set1f(fSeedUni, turbulence.seed()); |
michael@0 | 1261 | } |
michael@0 | 1262 | |
michael@0 | 1263 | ///////////////////////////////////////////////////////////////////// |
michael@0 | 1264 | |
michael@0 | 1265 | GrEffectRef* SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint) const { |
michael@0 | 1266 | SkASSERT(NULL != context); |
michael@0 | 1267 | |
michael@0 | 1268 | if (0 == fNumOctaves) { |
michael@0 | 1269 | SkColor clearColor = 0; |
michael@0 | 1270 | if (kFractalNoise_Type == fType) { |
michael@0 | 1271 | clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127); |
michael@0 | 1272 | } |
michael@0 | 1273 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter( |
michael@0 | 1274 | clearColor, SkXfermode::kSrc_Mode)); |
michael@0 | 1275 | return cf->asNewEffect(context); |
michael@0 | 1276 | } |
michael@0 | 1277 | |
michael@0 | 1278 | // Either we don't stitch tiles, either we have a valid tile size |
michael@0 | 1279 | SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
michael@0 | 1280 | |
michael@0 | 1281 | #ifdef SK_USE_SIMPLEX_NOISE |
michael@0 | 1282 | // Simplex noise is currently disabled but can be enabled by defining SK_USE_SIMPLEX_NOISE |
michael@0 | 1283 | sk_ignore_unused_variable(context); |
michael@0 | 1284 | GrEffectRef* effect = |
michael@0 | 1285 | GrSimplexNoiseEffect::Create(fType, fPaintingData->fBaseFrequency, |
michael@0 | 1286 | fNumOctaves, fStitchTiles, fSeed, |
michael@0 | 1287 | this->getLocalMatrix(), paint.getAlpha()); |
michael@0 | 1288 | #else |
michael@0 | 1289 | GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture( |
michael@0 | 1290 | context, fPaintingData->getPermutationsBitmap(), NULL); |
michael@0 | 1291 | GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture( |
michael@0 | 1292 | context, fPaintingData->getNoiseBitmap(), NULL); |
michael@0 | 1293 | |
michael@0 | 1294 | GrEffectRef* effect = (NULL != permutationsTexture) && (NULL != noiseTexture) ? |
michael@0 | 1295 | GrPerlinNoiseEffect::Create(fType, fPaintingData->fBaseFrequency, |
michael@0 | 1296 | fNumOctaves, fStitchTiles, |
michael@0 | 1297 | fPaintingData->fStitchDataInit, |
michael@0 | 1298 | permutationsTexture, noiseTexture, |
michael@0 | 1299 | this->getLocalMatrix(), paint.getAlpha()) : |
michael@0 | 1300 | NULL; |
michael@0 | 1301 | |
michael@0 | 1302 | // Unlock immediately, this is not great, but we don't have a way of |
michael@0 | 1303 | // knowing when else to unlock it currently. TODO: Remove this when |
michael@0 | 1304 | // unref becomes the unlock replacement for all types of textures. |
michael@0 | 1305 | if (NULL != permutationsTexture) { |
michael@0 | 1306 | GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture); |
michael@0 | 1307 | } |
michael@0 | 1308 | if (NULL != noiseTexture) { |
michael@0 | 1309 | GrUnlockAndUnrefCachedBitmapTexture(noiseTexture); |
michael@0 | 1310 | } |
michael@0 | 1311 | #endif |
michael@0 | 1312 | |
michael@0 | 1313 | return effect; |
michael@0 | 1314 | } |
michael@0 | 1315 | |
michael@0 | 1316 | #else |
michael@0 | 1317 | |
michael@0 | 1318 | GrEffectRef* SkPerlinNoiseShader::asNewEffect(GrContext*, const SkPaint&) const { |
michael@0 | 1319 | SkDEBUGFAIL("Should not call in GPU-less build"); |
michael@0 | 1320 | return NULL; |
michael@0 | 1321 | } |
michael@0 | 1322 | |
michael@0 | 1323 | #endif |
michael@0 | 1324 | |
michael@0 | 1325 | #ifndef SK_IGNORE_TO_STRING |
michael@0 | 1326 | void SkPerlinNoiseShader::toString(SkString* str) const { |
michael@0 | 1327 | str->append("SkPerlinNoiseShader: ("); |
michael@0 | 1328 | |
michael@0 | 1329 | str->append("type: "); |
michael@0 | 1330 | switch (fType) { |
michael@0 | 1331 | case kFractalNoise_Type: |
michael@0 | 1332 | str->append("\"fractal noise\""); |
michael@0 | 1333 | break; |
michael@0 | 1334 | case kTurbulence_Type: |
michael@0 | 1335 | str->append("\"turbulence\""); |
michael@0 | 1336 | break; |
michael@0 | 1337 | default: |
michael@0 | 1338 | str->append("\"unknown\""); |
michael@0 | 1339 | break; |
michael@0 | 1340 | } |
michael@0 | 1341 | str->append(" base frequency: ("); |
michael@0 | 1342 | str->appendScalar(fBaseFrequencyX); |
michael@0 | 1343 | str->append(", "); |
michael@0 | 1344 | str->appendScalar(fBaseFrequencyY); |
michael@0 | 1345 | str->append(") number of octaves: "); |
michael@0 | 1346 | str->appendS32(fNumOctaves); |
michael@0 | 1347 | str->append(" seed: "); |
michael@0 | 1348 | str->appendScalar(fSeed); |
michael@0 | 1349 | str->append(" stitch tiles: "); |
michael@0 | 1350 | str->append(fStitchTiles ? "true " : "false "); |
michael@0 | 1351 | |
michael@0 | 1352 | this->INHERITED::toString(str); |
michael@0 | 1353 | |
michael@0 | 1354 | str->append(")"); |
michael@0 | 1355 | } |
michael@0 | 1356 | #endif |