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