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