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" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 13 | #include "SkShader.h" |
| 14 | #include "SkUnPreMultiply.h" |
| 15 | #include "SkString.h" |
| 16 | |
| 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrContext.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 19 | #include "GrCoordTransform.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 20 | #include "gl/GrGLEffect.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 21 | #include "gl/builders/GrGLProgramBuilder.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 22 | #include "GrTBackendEffectFactory.h" |
| 23 | #include "SkGr.h" |
| 24 | #endif |
| 25 | |
| 26 | static const int kBlockSize = 256; |
| 27 | static const int kBlockMask = kBlockSize - 1; |
| 28 | static const int kPerlinNoise = 4096; |
| 29 | static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // noiseValue is the color component's value (or color) |
| 34 | // limitValue is the maximum perlin noise array index value allowed |
| 35 | // newValue is the current noise dimension (either width or height) |
| 36 | inline int checkNoise(int noiseValue, int limitValue, int newValue) { |
| 37 | // If the noise value would bring us out of bounds of the current noise array while we are |
| 38 | // stiching noise tiles together, wrap the noise around the current dimension of the noise to |
| 39 | // stay within the array bounds in a continuous fashion (so that tiling lines are not visible) |
| 40 | if (noiseValue >= limitValue) { |
| 41 | noiseValue -= newValue; |
| 42 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 43 | return noiseValue; |
| 44 | } |
| 45 | |
| 46 | inline SkScalar smoothCurve(SkScalar t) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 47 | static const SkScalar SK_Scalar3 = 3.0f; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 48 | |
| 49 | // returns t * t * (3 - 2 * t) |
| 50 | return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t); |
| 51 | } |
| 52 | |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 53 | bool perlin_noise_type_is_valid(SkPerlinNoiseShader::Type type) { |
| 54 | return (SkPerlinNoiseShader::kFractalNoise_Type == type) || |
| 55 | (SkPerlinNoiseShader::kTurbulence_Type == type); |
| 56 | } |
| 57 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 58 | } // end namespace |
| 59 | |
| 60 | struct SkPerlinNoiseShader::StitchData { |
| 61 | StitchData() |
| 62 | : fWidth(0) |
| 63 | , fWrapX(0) |
| 64 | , fHeight(0) |
| 65 | , fWrapY(0) |
| 66 | {} |
| 67 | |
| 68 | bool operator==(const StitchData& other) const { |
| 69 | return fWidth == other.fWidth && |
| 70 | fWrapX == other.fWrapX && |
| 71 | fHeight == other.fHeight && |
| 72 | fWrapY == other.fWrapY; |
| 73 | } |
| 74 | |
| 75 | int fWidth; // How much to subtract to wrap for stitching. |
| 76 | int fWrapX; // Minimum value to wrap. |
| 77 | int fHeight; |
| 78 | int fWrapY; |
| 79 | }; |
| 80 | |
| 81 | struct SkPerlinNoiseShader::PaintingData { |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 82 | PaintingData(const SkISize& tileSize, SkScalar seed, |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 83 | SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 84 | const SkMatrix& matrix) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 85 | { |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 86 | SkVector wavelength = SkVector::Make(SkScalarInvert(baseFrequencyX), |
| 87 | SkScalarInvert(baseFrequencyY)); |
| 88 | matrix.mapVectors(&wavelength, 1); |
| 89 | fBaseFrequency.fX = SkScalarInvert(wavelength.fX); |
| 90 | fBaseFrequency.fY = SkScalarInvert(wavelength.fY); |
| 91 | SkVector sizeVec = SkVector::Make(SkIntToScalar(tileSize.fWidth), |
| 92 | SkIntToScalar(tileSize.fHeight)); |
| 93 | matrix.mapVectors(&sizeVec, 1); |
| 94 | fTileSize.fWidth = SkScalarRoundToInt(sizeVec.fX); |
| 95 | fTileSize.fHeight = SkScalarRoundToInt(sizeVec.fY); |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 96 | this->init(seed); |
| 97 | if (!fTileSize.isEmpty()) { |
| 98 | this->stitch(); |
| 99 | } |
| 100 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 101 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 102 | fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1)); |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 103 | fPermutationsBitmap.setPixels(fLatticeSelector); |
| 104 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 105 | fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4)); |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 106 | fNoiseBitmap.setPixels(fNoise[0][0]); |
| 107 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int fSeed; |
| 111 | uint8_t fLatticeSelector[kBlockSize]; |
| 112 | uint16_t fNoise[4][kBlockSize][2]; |
| 113 | SkPoint fGradient[4][kBlockSize]; |
| 114 | SkISize fTileSize; |
| 115 | SkVector fBaseFrequency; |
| 116 | StitchData fStitchDataInit; |
| 117 | |
| 118 | private: |
| 119 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 120 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 121 | SkBitmap fPermutationsBitmap; |
| 122 | SkBitmap fNoiseBitmap; |
| 123 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 124 | |
| 125 | inline int random() { |
| 126 | static const int gRandAmplitude = 16807; // 7**5; primitive root of m |
| 127 | static const int gRandQ = 127773; // m / a |
| 128 | static const int gRandR = 2836; // m % a |
| 129 | |
| 130 | int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ); |
| 131 | if (result <= 0) |
| 132 | result += kRandMaximum; |
| 133 | fSeed = result; |
| 134 | return result; |
| 135 | } |
| 136 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 137 | // Only called once. Could be part of the constructor. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 138 | void init(SkScalar seed) |
| 139 | { |
| 140 | static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize)); |
| 141 | |
senorblanco@chromium.org | 857e320 | 2014-01-09 17:41:42 +0000 | [diff] [blame] | 142 | // According to the SVG spec, we must truncate (not round) the seed value. |
| 143 | fSeed = SkScalarTruncToInt(seed); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 144 | // The seed value clamp to the range [1, kRandMaximum - 1]. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 145 | if (fSeed <= 0) { |
| 146 | fSeed = -(fSeed % (kRandMaximum - 1)) + 1; |
| 147 | } |
| 148 | if (fSeed > kRandMaximum - 1) { |
| 149 | fSeed = kRandMaximum - 1; |
| 150 | } |
| 151 | for (int channel = 0; channel < 4; ++channel) { |
| 152 | for (int i = 0; i < kBlockSize; ++i) { |
| 153 | fLatticeSelector[i] = i; |
| 154 | fNoise[channel][i][0] = (random() % (2 * kBlockSize)); |
| 155 | fNoise[channel][i][1] = (random() % (2 * kBlockSize)); |
| 156 | } |
| 157 | } |
| 158 | for (int i = kBlockSize - 1; i > 0; --i) { |
| 159 | int k = fLatticeSelector[i]; |
| 160 | int j = random() % kBlockSize; |
| 161 | SkASSERT(j >= 0); |
| 162 | SkASSERT(j < kBlockSize); |
| 163 | fLatticeSelector[i] = fLatticeSelector[j]; |
| 164 | fLatticeSelector[j] = k; |
| 165 | } |
| 166 | |
| 167 | // Perform the permutations now |
| 168 | { |
| 169 | // Copy noise data |
| 170 | uint16_t noise[4][kBlockSize][2]; |
| 171 | for (int i = 0; i < kBlockSize; ++i) { |
| 172 | for (int channel = 0; channel < 4; ++channel) { |
| 173 | for (int j = 0; j < 2; ++j) { |
| 174 | noise[channel][i][j] = fNoise[channel][i][j]; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | // Do permutations on noise data |
| 179 | for (int i = 0; i < kBlockSize; ++i) { |
| 180 | for (int channel = 0; channel < 4; ++channel) { |
| 181 | for (int j = 0; j < 2; ++j) { |
| 182 | fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j]; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // 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] | 189 | static const SkScalar gHalfMax16bits = 32767.5f; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 190 | |
| 191 | // Compute gradients from permutated noise data |
| 192 | for (int channel = 0; channel < 4; ++channel) { |
| 193 | for (int i = 0; i < kBlockSize; ++i) { |
| 194 | fGradient[channel][i] = SkPoint::Make( |
| 195 | SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize), |
| 196 | gInvBlockSizef), |
skia.committer@gmail.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 197 | SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize), |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 198 | gInvBlockSizef)); |
| 199 | fGradient[channel][i].normalize(); |
| 200 | // Put the normalized gradient back into the noise data |
| 201 | fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 202 | fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 203 | fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 204 | fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 207 | } |
| 208 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 209 | // Only called once. Could be part of the constructor. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 210 | void stitch() { |
| 211 | SkScalar tileWidth = SkIntToScalar(fTileSize.width()); |
| 212 | SkScalar tileHeight = SkIntToScalar(fTileSize.height()); |
| 213 | SkASSERT(tileWidth > 0 && tileHeight > 0); |
| 214 | // When stitching tiled turbulence, the frequencies must be adjusted |
| 215 | // so that the tile borders will be continuous. |
| 216 | if (fBaseFrequency.fX) { |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 217 | SkScalar lowFrequencx = |
| 218 | SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
| 219 | SkScalar highFrequencx = |
| 220 | SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 221 | // BaseFrequency should be non-negative according to the standard. |
skia.committer@gmail.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 222 | if (SkScalarDiv(fBaseFrequency.fX, lowFrequencx) < |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 223 | SkScalarDiv(highFrequencx, fBaseFrequency.fX)) { |
| 224 | fBaseFrequency.fX = lowFrequencx; |
| 225 | } else { |
| 226 | fBaseFrequency.fX = highFrequencx; |
| 227 | } |
| 228 | } |
| 229 | if (fBaseFrequency.fY) { |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 230 | SkScalar lowFrequency = |
| 231 | SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
| 232 | SkScalar highFrequency = |
| 233 | SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
skia.committer@gmail.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 234 | if (SkScalarDiv(fBaseFrequency.fY, lowFrequency) < |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 235 | SkScalarDiv(highFrequency, fBaseFrequency.fY)) { |
| 236 | fBaseFrequency.fY = lowFrequency; |
| 237 | } else { |
| 238 | fBaseFrequency.fY = highFrequency; |
| 239 | } |
| 240 | } |
| 241 | // Set up TurbulenceInitial stitch values. |
| 242 | fStitchDataInit.fWidth = |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 243 | SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 244 | fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; |
| 245 | fStitchDataInit.fHeight = |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 246 | SkScalarRoundToInt(tileHeight * fBaseFrequency.fY); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 247 | fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight; |
| 248 | } |
| 249 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 250 | public: |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 251 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 252 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 253 | const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; } |
| 254 | |
| 255 | const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; } |
| 256 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 266 | SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 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) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 282 | , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 283 | , fSeed(seed) |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 284 | , fTileSize(NULL == tileSize ? SkISize::Make(0, 0) : *tileSize) |
| 285 | , fStitchTiles(!fTileSize.isEmpty()) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 286 | { |
| 287 | SkASSERT(numOctaves >= 0 && numOctaves < 256); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 288 | } |
| 289 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 290 | #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING |
| 291 | SkPerlinNoiseShader::SkPerlinNoiseShader(SkReadBuffer& buffer) : INHERITED(buffer) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 292 | fType = (SkPerlinNoiseShader::Type) buffer.readInt(); |
| 293 | fBaseFrequencyX = buffer.readScalar(); |
| 294 | fBaseFrequencyY = buffer.readScalar(); |
| 295 | fNumOctaves = buffer.readInt(); |
| 296 | fSeed = buffer.readScalar(); |
| 297 | fStitchTiles = buffer.readBool(); |
| 298 | fTileSize.fWidth = buffer.readInt(); |
| 299 | fTileSize.fHeight = buffer.readInt(); |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 300 | buffer.validate(perlin_noise_type_is_valid(fType) && |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 301 | (fNumOctaves >= 0) && (fNumOctaves <= 255) && |
| 302 | (fStitchTiles != fTileSize.isEmpty())); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 303 | } |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 304 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 305 | |
| 306 | SkPerlinNoiseShader::~SkPerlinNoiseShader() { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 307 | } |
| 308 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 309 | SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) { |
| 310 | Type type = (Type)buffer.readInt(); |
| 311 | SkScalar freqX = buffer.readScalar(); |
| 312 | SkScalar freqY = buffer.readScalar(); |
| 313 | int octaves = buffer.readInt(); |
| 314 | SkScalar seed = buffer.readScalar(); |
| 315 | SkISize tileSize; |
| 316 | tileSize.fWidth = buffer.readInt(); |
| 317 | tileSize.fHeight = buffer.readInt(); |
| 318 | |
| 319 | switch (type) { |
| 320 | case kFractalNoise_Type: |
| 321 | return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves, seed, &tileSize); |
| 322 | case kTurbulence_Type: |
| 323 | return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, seed, &tileSize); |
| 324 | default: |
| 325 | return NULL; |
| 326 | } |
| 327 | } |
| 328 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 329 | void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 330 | buffer.writeInt((int) fType); |
| 331 | buffer.writeScalar(fBaseFrequencyX); |
| 332 | buffer.writeScalar(fBaseFrequencyY); |
| 333 | buffer.writeInt(fNumOctaves); |
| 334 | buffer.writeScalar(fSeed); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 335 | buffer.writeInt(fTileSize.fWidth); |
| 336 | buffer.writeInt(fTileSize.fHeight); |
| 337 | } |
| 338 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 339 | SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 340 | int channel, const StitchData& stitchData, const SkPoint& noiseVector) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 341 | struct Noise { |
| 342 | int noisePositionIntegerValue; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 343 | int nextNoisePositionIntegerValue; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 344 | SkScalar noisePositionFractionValue; |
| 345 | Noise(SkScalar component) |
| 346 | { |
| 347 | SkScalar position = component + kPerlinNoise; |
| 348 | noisePositionIntegerValue = SkScalarFloorToInt(position); |
| 349 | noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 350 | nextNoisePositionIntegerValue = noisePositionIntegerValue + 1; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 351 | } |
| 352 | }; |
| 353 | Noise noiseX(noiseVector.x()); |
| 354 | Noise noiseY(noiseVector.y()); |
| 355 | SkScalar u, v; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 356 | const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 357 | // If stitching, adjust lattice points accordingly. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 358 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 359 | noiseX.noisePositionIntegerValue = |
| 360 | checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 361 | noiseY.noisePositionIntegerValue = |
| 362 | checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 363 | noiseX.nextNoisePositionIntegerValue = |
| 364 | checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 365 | noiseY.nextNoisePositionIntegerValue = |
| 366 | checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 367 | } |
| 368 | noiseX.noisePositionIntegerValue &= kBlockMask; |
| 369 | noiseY.noisePositionIntegerValue &= kBlockMask; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 370 | noiseX.nextNoisePositionIntegerValue &= kBlockMask; |
| 371 | noiseY.nextNoisePositionIntegerValue &= kBlockMask; |
| 372 | int i = |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 373 | fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue]; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 374 | int j = |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 375 | fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue]; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 376 | int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 377 | int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 378 | int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
| 379 | int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 380 | SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue); |
| 381 | SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue); |
| 382 | // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement |
| 383 | SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue, |
| 384 | noiseY.noisePositionFractionValue); // Offset (0,0) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 385 | u = fPaintingData->fGradient[channel][b00].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 386 | fractionValue.fX -= SK_Scalar1; // Offset (-1,0) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 387 | v = fPaintingData->fGradient[channel][b10].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 388 | SkScalar a = SkScalarInterp(u, v, sx); |
| 389 | fractionValue.fY -= SK_Scalar1; // Offset (-1,-1) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 390 | v = fPaintingData->fGradient[channel][b11].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 391 | fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 392 | u = fPaintingData->fGradient[channel][b01].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 393 | SkScalar b = SkScalarInterp(u, v, sx); |
| 394 | return SkScalarInterp(a, b, sy); |
| 395 | } |
| 396 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 397 | SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 398 | int channel, StitchData& stitchData, const SkPoint& point) const { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 399 | const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader); |
| 400 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 401 | // Set up TurbulenceInitial stitch values. |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 402 | stitchData = fPaintingData->fStitchDataInit; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 403 | } |
| 404 | SkScalar turbulenceFunctionResult = 0; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 405 | SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX), |
| 406 | SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY))); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 407 | SkScalar ratio = SK_Scalar1; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 408 | for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) { |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 409 | SkScalar noise = noise2D(channel, stitchData, noiseVector); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 410 | turbulenceFunctionResult += SkScalarDiv( |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 411 | (perlinNoiseShader.fType == kFractalNoise_Type) ? noise : SkScalarAbs(noise), ratio); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 412 | noiseVector.fX *= 2; |
| 413 | noiseVector.fY *= 2; |
| 414 | ratio *= 2; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 415 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 416 | // Update stitch values |
| 417 | stitchData.fWidth *= 2; |
| 418 | stitchData.fWrapX = stitchData.fWidth + kPerlinNoise; |
| 419 | stitchData.fHeight *= 2; |
| 420 | stitchData.fWrapY = stitchData.fHeight + kPerlinNoise; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 425 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 426 | if (perlinNoiseShader.fType == kFractalNoise_Type) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 427 | turbulenceFunctionResult = |
| 428 | SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf; |
| 429 | } |
| 430 | |
| 431 | if (channel == 3) { // Scale alpha by paint value |
| 432 | turbulenceFunctionResult = SkScalarMul(turbulenceFunctionResult, |
| 433 | SkScalarDiv(SkIntToScalar(getPaintAlpha()), SkIntToScalar(255))); |
| 434 | } |
| 435 | |
| 436 | // Clamp result |
| 437 | return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1); |
| 438 | } |
| 439 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 440 | SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade( |
| 441 | const SkPoint& point, StitchData& stitchData) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 442 | SkPoint newPoint; |
senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 443 | fMatrix.mapPoints(&newPoint, &point, 1); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 444 | newPoint.fX = SkScalarRoundToScalar(newPoint.fX); |
| 445 | newPoint.fY = SkScalarRoundToScalar(newPoint.fY); |
| 446 | |
| 447 | U8CPU rgba[4]; |
| 448 | for (int channel = 3; channel >= 0; --channel) { |
| 449 | rgba[channel] = SkScalarFloorToInt(255 * |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 450 | calculateTurbulenceValueForPoint(channel, stitchData, newPoint)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 451 | } |
| 452 | return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]); |
| 453 | } |
| 454 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 455 | SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec, |
| 456 | void* storage) const { |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 457 | return SkNEW_PLACEMENT_ARGS(storage, PerlinNoiseShaderContext, (*this, rec)); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | size_t SkPerlinNoiseShader::contextSize() const { |
| 461 | return sizeof(PerlinNoiseShaderContext); |
| 462 | } |
| 463 | |
| 464 | SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 465 | const SkPerlinNoiseShader& shader, const ContextRec& rec) |
| 466 | : INHERITED(shader, rec) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 467 | { |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 468 | SkMatrix newMatrix = *rec.fMatrix; |
reed@google.com | b67b8e6 | 2014-05-12 18:12:24 +0000 | [diff] [blame] | 469 | newMatrix.preConcat(shader.getLocalMatrix()); |
| 470 | if (rec.fLocalMatrix) { |
| 471 | newMatrix.preConcat(*rec.fLocalMatrix); |
| 472 | } |
senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 473 | // This (1,1) translation is due to WebKit's 1 based coordinates for the noise |
| 474 | // (as opposed to 0 based, usually). The same adjustment is in the setData() function. |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 475 | fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1); |
| 476 | fPaintingData = SkNEW_ARGS(PaintingData, (shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, shader.fBaseFrequencyY, newMatrix)); |
| 477 | } |
| 478 | |
| 479 | SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() { |
| 480 | SkDELETE(fPaintingData); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 481 | } |
| 482 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 483 | void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan( |
| 484 | int x, int y, SkPMColor result[], int count) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 485 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
| 486 | StitchData stitchData; |
| 487 | for (int i = 0; i < count; ++i) { |
| 488 | result[i] = shade(point, stitchData); |
| 489 | point.fX += SK_Scalar1; |
| 490 | } |
| 491 | } |
| 492 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 493 | void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan16( |
| 494 | int x, int y, uint16_t result[], int count) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 495 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
| 496 | StitchData stitchData; |
| 497 | DITHER_565_SCAN(y); |
| 498 | for (int i = 0; i < count; ++i) { |
| 499 | unsigned dither = DITHER_VALUE(x); |
| 500 | result[i] = SkDitherRGB32To565(shade(point, stitchData), dither); |
| 501 | DITHER_INC_X(x); |
| 502 | point.fX += SK_Scalar1; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | ///////////////////////////////////////////////////////////////////// |
| 507 | |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 508 | #if SK_SUPPORT_GPU |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 509 | |
| 510 | #include "GrTBackendEffectFactory.h" |
| 511 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 512 | class GrGLPerlinNoise : public GrGLEffect { |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 513 | public: |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 514 | GrGLPerlinNoise(const GrBackendEffectFactory& factory, |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 515 | const GrDrawEffect& drawEffect); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 516 | virtual ~GrGLPerlinNoise() {} |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 517 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 518 | virtual void emitCode(GrGLProgramBuilder*, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 519 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 520 | const GrEffectKey&, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 521 | const char* outputColor, |
| 522 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 523 | const TransformedCoordsArray&, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 524 | const TextureSamplerArray&) SK_OVERRIDE; |
| 525 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 526 | virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 527 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 528 | static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder* b); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 529 | |
| 530 | private: |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 531 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 532 | GrGLProgramDataManager::UniformHandle fStitchDataUni; |
| 533 | SkPerlinNoiseShader::Type fType; |
| 534 | bool fStitchTiles; |
| 535 | int fNumOctaves; |
| 536 | GrGLProgramDataManager::UniformHandle fBaseFrequencyUni; |
| 537 | GrGLProgramDataManager::UniformHandle fAlphaUni; |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 538 | |
| 539 | private: |
| 540 | typedef GrGLEffect INHERITED; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 541 | }; |
| 542 | |
| 543 | ///////////////////////////////////////////////////////////////////// |
| 544 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 545 | class GrPerlinNoiseEffect : public GrEffect { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 546 | public: |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 547 | static GrEffect* Create(SkPerlinNoiseShader::Type type, |
| 548 | int numOctaves, bool stitchTiles, |
| 549 | SkPerlinNoiseShader::PaintingData* paintingData, |
| 550 | GrTexture* permutationsTexture, GrTexture* noiseTexture, |
| 551 | const SkMatrix& matrix, uint8_t alpha) { |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 552 | return SkNEW_ARGS(GrPerlinNoiseEffect, (type, numOctaves, stitchTiles, paintingData, |
| 553 | permutationsTexture, noiseTexture, matrix, alpha)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 554 | } |
| 555 | |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 556 | virtual ~GrPerlinNoiseEffect() { |
| 557 | SkDELETE(fPaintingData); |
| 558 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 559 | |
| 560 | static const char* Name() { return "PerlinNoise"; } |
| 561 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 562 | return GrTBackendEffectFactory<GrPerlinNoiseEffect>::getInstance(); |
| 563 | } |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 564 | const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 565 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 566 | SkPerlinNoiseShader::Type type() const { return fType; } |
| 567 | bool stitchTiles() const { return fStitchTiles; } |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 568 | const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; } |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 569 | int numOctaves() const { return fNumOctaves; } |
| 570 | const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } |
| 571 | uint8_t alpha() const { return fAlpha; } |
| 572 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 573 | typedef GrGLPerlinNoise GLEffect; |
| 574 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 575 | private: |
| 576 | virtual bool onIsEqual(const GrEffect& sBase) const SK_OVERRIDE { |
| 577 | const GrPerlinNoiseEffect& s = CastEffect<GrPerlinNoiseEffect>(sBase); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 578 | return fType == s.fType && |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 579 | fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency && |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 580 | fNumOctaves == s.fNumOctaves && |
| 581 | fStitchTiles == s.fStitchTiles && |
| 582 | fCoordTransform.getMatrix() == s.fCoordTransform.getMatrix() && |
| 583 | fAlpha == s.fAlpha && |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 584 | fPermutationsAccess.getTexture() == s.fPermutationsAccess.getTexture() && |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 585 | fNoiseAccess.getTexture() == s.fNoiseAccess.getTexture() && |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 586 | fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 587 | } |
| 588 | |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 589 | GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 590 | int numOctaves, bool stitchTiles, |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 591 | SkPerlinNoiseShader::PaintingData* paintingData, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 592 | GrTexture* permutationsTexture, GrTexture* noiseTexture, |
| 593 | const SkMatrix& matrix, uint8_t alpha) |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 594 | : fType(type) |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 595 | , fNumOctaves(numOctaves) |
| 596 | , fStitchTiles(stitchTiles) |
| 597 | , fAlpha(alpha) |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 598 | , fPermutationsAccess(permutationsTexture) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 599 | , fNoiseAccess(noiseTexture) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 600 | , fPaintingData(paintingData) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 601 | this->addTextureAccess(&fPermutationsAccess); |
| 602 | this->addTextureAccess(&fNoiseAccess); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 603 | fCoordTransform.reset(kLocal_GrCoordSet, matrix); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 604 | this->addCoordTransform(&fCoordTransform); |
| 605 | this->setWillNotUseInputColor(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 606 | } |
| 607 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 608 | GR_DECLARE_EFFECT_TEST; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 609 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 610 | SkPerlinNoiseShader::Type fType; |
| 611 | GrCoordTransform fCoordTransform; |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 612 | int fNumOctaves; |
| 613 | bool fStitchTiles; |
| 614 | uint8_t fAlpha; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 615 | GrTextureAccess fPermutationsAccess; |
| 616 | GrTextureAccess fNoiseAccess; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 617 | SkPerlinNoiseShader::PaintingData *fPaintingData; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 618 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 619 | void getConstantColorComponents(GrColor*, uint32_t* validFlags) const SK_OVERRIDE { |
| 620 | *validFlags = 0; // This is noise. Nothing is constant. |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 621 | } |
| 622 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 623 | private: |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 624 | typedef GrEffect INHERITED; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | ///////////////////////////////////////////////////////////////////// |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 628 | GR_DEFINE_EFFECT_TEST(GrPerlinNoiseEffect); |
| 629 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 630 | GrEffect* GrPerlinNoiseEffect::TestCreate(SkRandom* random, |
| 631 | GrContext* context, |
| 632 | const GrDrawTargetCaps&, |
| 633 | GrTexture**) { |
sugoi@google.com | 423ac13 | 2013-04-18 14:04:57 +0000 | [diff] [blame] | 634 | int numOctaves = random->nextRangeU(2, 10); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 635 | bool stitchTiles = random->nextBool(); |
| 636 | SkScalar seed = SkIntToScalar(random->nextU()); |
| 637 | 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] | 638 | SkScalar baseFrequencyX = random->nextRangeScalar(0.01f, |
| 639 | 0.99f); |
| 640 | SkScalar baseFrequencyY = random->nextRangeScalar(0.01f, |
| 641 | 0.99f); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 642 | |
| 643 | SkShader* shader = random->nextBool() ? |
| 644 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| 645 | stitchTiles ? &tileSize : NULL) : |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 646 | SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 647 | stitchTiles ? &tileSize : NULL); |
| 648 | |
| 649 | SkPaint paint; |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 650 | GrColor paintColor; |
| 651 | GrEffect* effect; |
| 652 | SkAssertResult(shader->asNewEffect(context, paint, NULL, &paintColor, &effect)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 653 | |
| 654 | SkDELETE(shader); |
| 655 | |
| 656 | return effect; |
| 657 | } |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 658 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 659 | GrGLPerlinNoise::GrGLPerlinNoise(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect) |
| 660 | : INHERITED (factory) |
| 661 | , fType(drawEffect.castEffect<GrPerlinNoiseEffect>().type()) |
| 662 | , fStitchTiles(drawEffect.castEffect<GrPerlinNoiseEffect>().stitchTiles()) |
| 663 | , fNumOctaves(drawEffect.castEffect<GrPerlinNoiseEffect>().numOctaves()) { |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 664 | } |
| 665 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 666 | void GrGLPerlinNoise::emitCode(GrGLProgramBuilder* builder, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 667 | const GrDrawEffect&, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 668 | const GrEffectKey& key, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 669 | const char* outputColor, |
| 670 | const char* inputColor, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 671 | const TransformedCoordsArray& coords, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 672 | const TextureSamplerArray& samplers) { |
| 673 | sk_ignore_unused_variable(inputColor); |
| 674 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 675 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 676 | SkString vCoords = fsBuilder->ensureFSCoords2D(coords, 0); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 677 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 678 | fBaseFrequencyUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 679 | kVec2f_GrSLType, "baseFrequency"); |
| 680 | const char* baseFrequencyUni = builder->getUniformCStr(fBaseFrequencyUni); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 681 | fAlphaUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 682 | kFloat_GrSLType, "alpha"); |
| 683 | const char* alphaUni = builder->getUniformCStr(fAlphaUni); |
| 684 | |
| 685 | const char* stitchDataUni = NULL; |
| 686 | if (fStitchTiles) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 687 | fStitchDataUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 688 | kVec2f_GrSLType, "stitchData"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 689 | stitchDataUni = builder->getUniformCStr(fStitchDataUni); |
| 690 | } |
| 691 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 692 | // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 |
| 693 | const char* chanCoordR = "0.125"; |
| 694 | const char* chanCoordG = "0.375"; |
| 695 | const char* chanCoordB = "0.625"; |
| 696 | const char* chanCoordA = "0.875"; |
| 697 | const char* chanCoord = "chanCoord"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 698 | const char* stitchData = "stitchData"; |
| 699 | const char* ratio = "ratio"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 700 | const char* noiseVec = "noiseVec"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 701 | const char* noiseSmooth = "noiseSmooth"; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 702 | const char* floorVal = "floorVal"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 703 | const char* fractVal = "fractVal"; |
| 704 | const char* uv = "uv"; |
| 705 | const char* ab = "ab"; |
| 706 | const char* latticeIdx = "latticeIdx"; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 707 | const char* bcoords = "bcoords"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 708 | const char* lattice = "lattice"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 709 | const char* inc8bit = "0.00390625"; // 1.0 / 256.0 |
| 710 | // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a |
| 711 | // [-1,1] vector and perform a dot product between that vector and the provided vector. |
| 712 | const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);"; |
| 713 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 714 | // Add noise function |
| 715 | static const GrGLShaderVar gPerlinNoiseArgs[] = { |
| 716 | GrGLShaderVar(chanCoord, kFloat_GrSLType), |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 717 | GrGLShaderVar(noiseVec, kVec2f_GrSLType) |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 718 | }; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 719 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 720 | static const GrGLShaderVar gPerlinNoiseStitchArgs[] = { |
| 721 | GrGLShaderVar(chanCoord, kFloat_GrSLType), |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 722 | GrGLShaderVar(noiseVec, kVec2f_GrSLType), |
| 723 | GrGLShaderVar(stitchData, kVec2f_GrSLType) |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 724 | }; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 725 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 726 | SkString noiseCode; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 727 | |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 728 | noiseCode.appendf("\tvec4 %s;\n", floorVal); |
| 729 | noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec); |
| 730 | noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal); |
| 731 | noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 732 | |
| 733 | // smooth curve : t * t * (3 - 2 * t) |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 734 | noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);", |
| 735 | noiseSmooth, fractVal, fractVal, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 736 | |
| 737 | // Adjust frequencies if we're stitching tiles |
| 738 | if (fStitchTiles) { |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 739 | noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }", |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 740 | floorVal, stitchData, floorVal, stitchData); |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 741 | noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }", |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 742 | floorVal, stitchData, floorVal, stitchData); |
| 743 | noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }", |
| 744 | floorVal, stitchData, floorVal, stitchData); |
| 745 | noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }", |
| 746 | floorVal, stitchData, floorVal, stitchData); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | // Get texture coordinates and normalize |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 750 | noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n", |
| 751 | floorVal, floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 752 | |
| 753 | // Get permutation for x |
| 754 | { |
| 755 | SkString xCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 756 | xCoords.appendf("vec2(%s.x, 0.5)", floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 757 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 758 | noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 759 | fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 760 | noiseCode.append(".r;"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | // Get permutation for x + 1 |
| 764 | { |
| 765 | SkString xCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 766 | xCoords.appendf("vec2(%s.z, 0.5)", floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 767 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 768 | noiseCode.appendf("\n\t%s.y = ", latticeIdx); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 769 | fsBuilder->appendTextureLookup(&noiseCode, samplers[0], xCoords.c_str(), kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 770 | noiseCode.append(".r;"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 771 | } |
| 772 | |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 773 | #if defined(SK_BUILD_FOR_ANDROID) |
| 774 | // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3). |
| 775 | // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit |
| 776 | // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725 |
| 777 | // (or 0.484368 here). The following rounding operation prevents these precision issues from |
| 778 | // affecting the result of the noise by making sure that we only have multiples of 1/255. |
| 779 | // (Note that 1/255 is about 0.003921569, which is the value used here). |
| 780 | noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);", |
| 781 | latticeIdx, latticeIdx); |
| 782 | #endif |
| 783 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 784 | // Get (x,y) coordinates with the permutated x |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 785 | noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 786 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 787 | noiseCode.appendf("\n\n\tvec2 %s;", uv); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 788 | // Compute u, at offset (0,0) |
| 789 | { |
| 790 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 791 | latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 792 | noiseCode.appendf("\n\tvec4 %s = ", lattice); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 793 | fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(), |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 794 | kVec2f_GrSLType); |
| 795 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
| 796 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 797 | } |
| 798 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 799 | noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 800 | // Compute v, at offset (-1,0) |
| 801 | { |
| 802 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 803 | latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 804 | noiseCode.append("\n\tlattice = "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 805 | fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(), |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 806 | kVec2f_GrSLType); |
| 807 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
| 808 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // Compute 'a' as a linear interpolation of 'u' and 'v' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 812 | noiseCode.appendf("\n\tvec2 %s;", ab); |
| 813 | 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] | 814 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 815 | noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 816 | // Compute v, at offset (-1,-1) |
| 817 | { |
| 818 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 819 | latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 820 | noiseCode.append("\n\tlattice = "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 821 | fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(), |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 822 | kVec2f_GrSLType); |
| 823 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
| 824 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 825 | } |
| 826 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 827 | noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 828 | // Compute u, at offset (0,-1) |
| 829 | { |
| 830 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 831 | latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 832 | noiseCode.append("\n\tlattice = "); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 833 | fsBuilder->appendTextureLookup(&noiseCode, samplers[1], latticeCoords.c_str(), |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 834 | kVec2f_GrSLType); |
| 835 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
| 836 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | // Compute 'b' as a linear interpolation of 'u' and 'v' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 840 | 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] | 841 | // Compute the noise as a linear interpolation of 'a' and 'b' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 842 | 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] | 843 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 844 | SkString noiseFuncName; |
| 845 | if (fStitchTiles) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 846 | fsBuilder->emitFunction(kFloat_GrSLType, |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 847 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs), |
| 848 | gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 849 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 850 | fsBuilder->emitFunction(kFloat_GrSLType, |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 851 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs), |
| 852 | gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 853 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 854 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 855 | // There are rounding errors if the floor operation is not performed here |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 856 | fsBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;", |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 857 | noiseVec, vCoords.c_str(), baseFrequencyUni); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 858 | |
| 859 | // Clear the color accumulator |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 860 | fsBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", outputColor); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 861 | |
| 862 | if (fStitchTiles) { |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 863 | // Set up TurbulenceInitial stitch values. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 864 | fsBuilder->codeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 865 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 866 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 867 | fsBuilder->codeAppendf("\n\t\tfloat %s = 1.0;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 868 | |
| 869 | // Loop over all octaves |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 870 | fsBuilder->codeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 871 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 872 | fsBuilder->codeAppendf("\n\t\t\t%s += ", outputColor); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 873 | if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 874 | fsBuilder->codeAppend("abs("); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 875 | } |
| 876 | if (fStitchTiles) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 877 | fsBuilder->codeAppendf( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 878 | "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s)," |
| 879 | "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))", |
| 880 | noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData, |
| 881 | noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData, |
| 882 | noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData, |
| 883 | noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData); |
| 884 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 885 | fsBuilder->codeAppendf( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 886 | "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s)," |
| 887 | "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))", |
| 888 | noiseFuncName.c_str(), chanCoordR, noiseVec, |
| 889 | noiseFuncName.c_str(), chanCoordG, noiseVec, |
| 890 | noiseFuncName.c_str(), chanCoordB, noiseVec, |
| 891 | noiseFuncName.c_str(), chanCoordA, noiseVec); |
| 892 | } |
| 893 | if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 894 | fsBuilder->codeAppendf(")"); // end of "abs(" |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 895 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 896 | fsBuilder->codeAppendf(" * %s;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 897 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 898 | fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec); |
| 899 | fsBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 900 | |
| 901 | if (fStitchTiles) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 902 | fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 903 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 904 | fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 905 | |
| 906 | if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { |
| 907 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 908 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 909 | fsBuilder->codeAppendf("\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] | 910 | } |
| 911 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 912 | fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", outputColor, alphaUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 913 | |
| 914 | // Clamp values |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 915 | fsBuilder->codeAppendf("\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] | 916 | |
| 917 | // Pre-multiply the result |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 918 | fsBuilder->codeAppendf("\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] | 919 | outputColor, outputColor, outputColor, outputColor); |
| 920 | } |
| 921 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 922 | void GrGLPerlinNoise::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, GrEffectKeyBuilder* b) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 923 | const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>(); |
| 924 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 925 | uint32_t key = turbulence.numOctaves(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 926 | |
| 927 | key = key << 3; // Make room for next 3 bits |
| 928 | |
| 929 | switch (turbulence.type()) { |
| 930 | case SkPerlinNoiseShader::kFractalNoise_Type: |
| 931 | key |= 0x1; |
| 932 | break; |
| 933 | case SkPerlinNoiseShader::kTurbulence_Type: |
| 934 | key |= 0x2; |
| 935 | break; |
| 936 | default: |
| 937 | // leave key at 0 |
| 938 | break; |
| 939 | } |
| 940 | |
| 941 | if (turbulence.stitchTiles()) { |
| 942 | key |= 0x4; // Flip the 3rd bit if tile stitching is on |
| 943 | } |
| 944 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 945 | b->add32(key); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 946 | } |
| 947 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 948 | void GrGLPerlinNoise::setData(const GrGLProgramDataManager& pdman, const GrDrawEffect& drawEffect) { |
| 949 | INHERITED::setData(pdman, drawEffect); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 950 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 951 | const GrPerlinNoiseEffect& turbulence = drawEffect.castEffect<GrPerlinNoiseEffect>(); |
| 952 | |
| 953 | const SkVector& baseFrequency = turbulence.baseFrequency(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 954 | pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
| 955 | pdman.set1f(fAlphaUni, SkScalarDiv(SkIntToScalar(turbulence.alpha()), SkIntToScalar(255))); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 956 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 957 | if (turbulence.stitchTiles()) { |
| 958 | const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 959 | pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 960 | SkIntToScalar(stitchData.fHeight)); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 964 | ///////////////////////////////////////////////////////////////////// |
| 965 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 966 | bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 967 | const SkMatrix* externalLocalMatrix, GrColor* paintColor, |
| 968 | GrEffect** effect) const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 969 | SkASSERT(context); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 970 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 971 | *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 972 | |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 973 | SkMatrix localMatrix = this->getLocalMatrix(); |
| 974 | if (externalLocalMatrix) { |
| 975 | localMatrix.preConcat(*externalLocalMatrix); |
| 976 | } |
| 977 | |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 978 | SkMatrix matrix = context->getMatrix(); |
| 979 | matrix.preConcat(localMatrix); |
| 980 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 981 | if (0 == fNumOctaves) { |
| 982 | SkColor clearColor = 0; |
| 983 | if (kFractalNoise_Type == fType) { |
| 984 | clearColor = SkColorSetARGB(paint.getAlpha() / 2, 127, 127, 127); |
| 985 | } |
| 986 | SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter( |
| 987 | clearColor, SkXfermode::kSrc_Mode)); |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 988 | *effect = cf->asNewEffect(context); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 989 | return true; |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 990 | } |
| 991 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 992 | // Either we don't stitch tiles, either we have a valid tile size |
| 993 | SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
| 994 | |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 995 | SkPerlinNoiseShader::PaintingData* paintingData = SkNEW_ARGS(PaintingData, (fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 996 | GrTexture* permutationsTexture = GrLockAndRefCachedBitmapTexture( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 997 | context, paintingData->getPermutationsBitmap(), NULL); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 998 | GrTexture* noiseTexture = GrLockAndRefCachedBitmapTexture( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 999 | context, paintingData->getNoiseBitmap(), NULL); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1000 | |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 1001 | SkMatrix m = context->getMatrix(); |
| 1002 | m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); |
| 1003 | m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1004 | if ((permutationsTexture) && (noiseTexture)) { |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1005 | *effect = GrPerlinNoiseEffect::Create(fType, |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 1006 | fNumOctaves, |
| 1007 | fStitchTiles, |
| 1008 | paintingData, |
| 1009 | permutationsTexture, noiseTexture, |
| 1010 | m, paint.getAlpha()); |
| 1011 | } else { |
| 1012 | SkDELETE(paintingData); |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1013 | *effect = NULL; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 1014 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1015 | |
| 1016 | // Unlock immediately, this is not great, but we don't have a way of |
| 1017 | // knowing when else to unlock it currently. TODO: Remove this when |
| 1018 | // unref becomes the unlock replacement for all types of textures. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1019 | if (permutationsTexture) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1020 | GrUnlockAndUnrefCachedBitmapTexture(permutationsTexture); |
| 1021 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1022 | if (noiseTexture) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1023 | GrUnlockAndUnrefCachedBitmapTexture(noiseTexture); |
| 1024 | } |
| 1025 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 1026 | return true; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | #else |
| 1030 | |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 1031 | bool SkPerlinNoiseShader::asNewEffect(GrContext* context, const SkPaint& paint, |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 1032 | const SkMatrix* externalLocalMatrix, GrColor* paintColor, |
| 1033 | GrEffect** effect) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1034 | SkDEBUGFAIL("Should not call in GPU-less build"); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 1035 | return false; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | #endif |
| 1039 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 1040 | #ifndef SK_IGNORE_TO_STRING |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1041 | void SkPerlinNoiseShader::toString(SkString* str) const { |
| 1042 | str->append("SkPerlinNoiseShader: ("); |
| 1043 | |
| 1044 | str->append("type: "); |
| 1045 | switch (fType) { |
| 1046 | case kFractalNoise_Type: |
| 1047 | str->append("\"fractal noise\""); |
| 1048 | break; |
| 1049 | case kTurbulence_Type: |
| 1050 | str->append("\"turbulence\""); |
| 1051 | break; |
| 1052 | default: |
| 1053 | str->append("\"unknown\""); |
| 1054 | break; |
| 1055 | } |
| 1056 | str->append(" base frequency: ("); |
| 1057 | str->appendScalar(fBaseFrequencyX); |
| 1058 | str->append(", "); |
| 1059 | str->appendScalar(fBaseFrequencyY); |
| 1060 | str->append(") number of octaves: "); |
| 1061 | str->appendS32(fNumOctaves); |
| 1062 | str->append(" seed: "); |
| 1063 | str->appendScalar(fSeed); |
| 1064 | str->append(" stitch tiles: "); |
| 1065 | str->append(fStitchTiles ? "true " : "false "); |
| 1066 | |
| 1067 | this->INHERITED::toString(str); |
| 1068 | |
| 1069 | str->append(")"); |
| 1070 | } |
| 1071 | #endif |