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