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 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 8 | #include "SkPerlinNoiseShader.h" |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 9 | |
| 10 | #include "SkArenaAlloc.h" |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 12 | #include "SkReadBuffer.h" |
| 13 | #include "SkWriteBuffer.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 14 | #include "SkShader.h" |
| 15 | #include "SkUnPreMultiply.h" |
| 16 | #include "SkString.h" |
| 17 | |
| 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrContext.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 20 | #include "GrCoordTransform.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 21 | #include "SkGr.h" |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 22 | #include "SkGrPriv.h" |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 23 | #include "effects/GrConstColorProcessor.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 24 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 25 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 26 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 27 | #include "glsl/GrGLSLUniformHandler.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
| 30 | static const int kBlockSize = 256; |
| 31 | static const int kBlockMask = kBlockSize - 1; |
| 32 | static const int kPerlinNoise = 4096; |
| 33 | static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | // noiseValue is the color component's value (or color) |
| 38 | // limitValue is the maximum perlin noise array index value allowed |
| 39 | // newValue is the current noise dimension (either width or height) |
| 40 | inline int checkNoise(int noiseValue, int limitValue, int newValue) { |
| 41 | // If the noise value would bring us out of bounds of the current noise array while we are |
| 42 | // stiching noise tiles together, wrap the noise around the current dimension of the noise to |
| 43 | // stay within the array bounds in a continuous fashion (so that tiling lines are not visible) |
| 44 | if (noiseValue >= limitValue) { |
| 45 | noiseValue -= newValue; |
| 46 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 47 | return noiseValue; |
| 48 | } |
| 49 | |
| 50 | inline SkScalar smoothCurve(SkScalar t) { |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 51 | return t * t * (3 - 2 * t); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 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( |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 189 | (fNoise[channel][i][0] - kBlockSize) * gInvBlockSizef, |
| 190 | (fNoise[channel][i][1] - kBlockSize) * gInvBlockSizef); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 191 | fGradient[channel][i].normalize(); |
| 192 | // Put the normalized gradient back into the noise data |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 193 | fNoise[channel][i][0] = SkScalarRoundToInt( |
| 194 | (fGradient[channel][i].fX + 1) * gHalfMax16bits); |
| 195 | fNoise[channel][i][1] = SkScalarRoundToInt( |
| 196 | (fGradient[channel][i].fY + 1) * gHalfMax16bits); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 199 | } |
| 200 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 201 | // Only called once. Could be part of the constructor. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 202 | void stitch() { |
| 203 | SkScalar tileWidth = SkIntToScalar(fTileSize.width()); |
| 204 | SkScalar tileHeight = SkIntToScalar(fTileSize.height()); |
| 205 | SkASSERT(tileWidth > 0 && tileHeight > 0); |
| 206 | // When stitching tiled turbulence, the frequencies must be adjusted |
| 207 | // so that the tile borders will be continuous. |
| 208 | if (fBaseFrequency.fX) { |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 209 | SkScalar lowFrequencx = |
| 210 | SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
| 211 | SkScalar highFrequencx = |
| 212 | SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 213 | // BaseFrequency should be non-negative according to the standard. |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 214 | if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequency.fX) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 215 | fBaseFrequency.fX = lowFrequencx; |
| 216 | } else { |
| 217 | fBaseFrequency.fX = highFrequencx; |
| 218 | } |
| 219 | } |
| 220 | if (fBaseFrequency.fY) { |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 221 | SkScalar lowFrequency = |
| 222 | SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
| 223 | SkScalar highFrequency = |
| 224 | SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 225 | if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequency.fY) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 226 | fBaseFrequency.fY = lowFrequency; |
| 227 | } else { |
| 228 | fBaseFrequency.fY = highFrequency; |
| 229 | } |
| 230 | } |
| 231 | // Set up TurbulenceInitial stitch values. |
| 232 | fStitchDataInit.fWidth = |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 233 | SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 234 | fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; |
| 235 | fStitchDataInit.fHeight = |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 236 | SkScalarRoundToInt(tileHeight * fBaseFrequency.fY); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 237 | fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight; |
| 238 | } |
| 239 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 240 | public: |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 241 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 242 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 243 | const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; } |
| 244 | |
| 245 | const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; } |
| 246 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 249 | sk_sp<SkShader> SkPerlinNoiseShader::MakeFractalNoise(SkScalar baseFrequencyX, |
| 250 | SkScalar baseFrequencyY, |
| 251 | int numOctaves, SkScalar seed, |
| 252 | const SkISize* tileSize) { |
| 253 | return sk_sp<SkShader>(new SkPerlinNoiseShader(kFractalNoise_Type, baseFrequencyX, |
| 254 | baseFrequencyY, numOctaves, |
| 255 | seed, tileSize)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 256 | } |
| 257 | |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 258 | sk_sp<SkShader> SkPerlinNoiseShader::MakeTurbulence(SkScalar baseFrequencyX, |
| 259 | SkScalar baseFrequencyY, |
| 260 | int numOctaves, SkScalar seed, |
| 261 | const SkISize* tileSize) { |
| 262 | return sk_sp<SkShader>(new SkPerlinNoiseShader(kTurbulence_Type, baseFrequencyX, baseFrequencyY, |
| 263 | numOctaves, seed, tileSize)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, |
| 267 | SkScalar baseFrequencyX, |
| 268 | SkScalar baseFrequencyY, |
| 269 | int numOctaves, |
| 270 | SkScalar seed, |
| 271 | const SkISize* tileSize) |
| 272 | : fType(type) |
| 273 | , fBaseFrequencyX(baseFrequencyX) |
| 274 | , fBaseFrequencyY(baseFrequencyY) |
robertphillips | 6a16fd3 | 2016-06-27 12:26:29 -0700 | [diff] [blame] | 275 | , fNumOctaves(SkTPin<int>(numOctaves, 0, 255)) // [0,255] octaves allowed |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 276 | , fSeed(seed) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 277 | , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize) |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 278 | , fStitchTiles(!fTileSize.isEmpty()) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 279 | { |
robertphillips | 6a16fd3 | 2016-06-27 12:26:29 -0700 | [diff] [blame] | 280 | SkASSERT(fNumOctaves >= 0 && fNumOctaves < 256); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 281 | } |
| 282 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 283 | SkPerlinNoiseShader::~SkPerlinNoiseShader() { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 284 | } |
| 285 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 286 | sk_sp<SkFlattenable> SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 287 | Type type = (Type)buffer.readInt(); |
| 288 | SkScalar freqX = buffer.readScalar(); |
| 289 | SkScalar freqY = buffer.readScalar(); |
| 290 | int octaves = buffer.readInt(); |
| 291 | SkScalar seed = buffer.readScalar(); |
| 292 | SkISize tileSize; |
| 293 | tileSize.fWidth = buffer.readInt(); |
| 294 | tileSize.fHeight = buffer.readInt(); |
| 295 | |
| 296 | switch (type) { |
| 297 | case kFractalNoise_Type: |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 298 | return SkPerlinNoiseShader::MakeFractalNoise(freqX, freqY, octaves, seed, |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 299 | &tileSize); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 300 | case kTurbulence_Type: |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 301 | return SkPerlinNoiseShader::MakeTurbulence(freqX, freqY, octaves, seed, |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 302 | &tileSize); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 303 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 304 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 308 | void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 309 | buffer.writeInt((int) fType); |
| 310 | buffer.writeScalar(fBaseFrequencyX); |
| 311 | buffer.writeScalar(fBaseFrequencyY); |
| 312 | buffer.writeInt(fNumOctaves); |
| 313 | buffer.writeScalar(fSeed); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 314 | buffer.writeInt(fTileSize.fWidth); |
| 315 | buffer.writeInt(fTileSize.fHeight); |
| 316 | } |
| 317 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 318 | SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 319 | int channel, const StitchData& stitchData, const SkPoint& noiseVector) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 320 | struct Noise { |
| 321 | int noisePositionIntegerValue; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 322 | int nextNoisePositionIntegerValue; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 323 | SkScalar noisePositionFractionValue; |
| 324 | Noise(SkScalar component) |
| 325 | { |
| 326 | SkScalar position = component + kPerlinNoise; |
| 327 | noisePositionIntegerValue = SkScalarFloorToInt(position); |
| 328 | noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 329 | nextNoisePositionIntegerValue = noisePositionIntegerValue + 1; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 330 | } |
| 331 | }; |
| 332 | Noise noiseX(noiseVector.x()); |
| 333 | Noise noiseY(noiseVector.y()); |
| 334 | SkScalar u, v; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 335 | const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 336 | // If stitching, adjust lattice points accordingly. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 337 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 338 | noiseX.noisePositionIntegerValue = |
| 339 | checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 340 | noiseY.noisePositionIntegerValue = |
| 341 | checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 342 | noiseX.nextNoisePositionIntegerValue = |
| 343 | checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 344 | noiseY.nextNoisePositionIntegerValue = |
| 345 | checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 346 | } |
| 347 | noiseX.noisePositionIntegerValue &= kBlockMask; |
| 348 | noiseY.noisePositionIntegerValue &= kBlockMask; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 349 | noiseX.nextNoisePositionIntegerValue &= kBlockMask; |
| 350 | noiseY.nextNoisePositionIntegerValue &= kBlockMask; |
| 351 | int i = |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 352 | fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue]; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 353 | int j = |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 354 | fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue]; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 355 | int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 356 | int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 357 | int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
| 358 | int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 359 | SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue); |
| 360 | SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue); |
| 361 | // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement |
| 362 | SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue, |
| 363 | noiseY.noisePositionFractionValue); // Offset (0,0) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 364 | u = fPaintingData->fGradient[channel][b00].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 365 | fractionValue.fX -= SK_Scalar1; // Offset (-1,0) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 366 | v = fPaintingData->fGradient[channel][b10].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 367 | SkScalar a = SkScalarInterp(u, v, sx); |
| 368 | fractionValue.fY -= SK_Scalar1; // Offset (-1,-1) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 369 | v = fPaintingData->fGradient[channel][b11].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 370 | fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 371 | u = fPaintingData->fGradient[channel][b01].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 372 | SkScalar b = SkScalarInterp(u, v, sx); |
| 373 | return SkScalarInterp(a, b, sy); |
| 374 | } |
| 375 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 376 | SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 377 | int channel, StitchData& stitchData, const SkPoint& point) const { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 378 | const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader); |
| 379 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 380 | // Set up TurbulenceInitial stitch values. |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 381 | stitchData = fPaintingData->fStitchDataInit; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 382 | } |
| 383 | SkScalar turbulenceFunctionResult = 0; |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 384 | SkPoint noiseVector(SkPoint::Make(point.x() * fPaintingData->fBaseFrequency.fX, |
| 385 | point.y() * fPaintingData->fBaseFrequency.fY)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 386 | SkScalar ratio = SK_Scalar1; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 387 | for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) { |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 388 | SkScalar noise = noise2D(channel, stitchData, noiseVector); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 389 | SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ? |
| 390 | noise : SkScalarAbs(noise); |
| 391 | turbulenceFunctionResult += numer / ratio; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 392 | noiseVector.fX *= 2; |
| 393 | noiseVector.fY *= 2; |
| 394 | ratio *= 2; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 395 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 396 | // Update stitch values |
| 397 | stitchData.fWidth *= 2; |
| 398 | stitchData.fWrapX = stitchData.fWidth + kPerlinNoise; |
| 399 | stitchData.fHeight *= 2; |
| 400 | stitchData.fWrapY = stitchData.fHeight + kPerlinNoise; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 405 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 406 | if (perlinNoiseShader.fType == kFractalNoise_Type) { |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 407 | turbulenceFunctionResult = turbulenceFunctionResult * SK_ScalarHalf + SK_ScalarHalf; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 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 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 433 | SkShader::Context* SkPerlinNoiseShader::onMakeContext( |
| 434 | const ContextRec& rec, SkArenaAlloc* alloc) const { |
| 435 | return alloc->make<PerlinNoiseShaderContext>(*this, rec); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 439 | const SkPerlinNoiseShader& shader, const ContextRec& rec) |
| 440 | : INHERITED(shader, rec) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 441 | { |
Florin Malita | bbeb573 | 2017-01-26 16:23:06 -0500 | [diff] [blame] | 442 | SkMatrix newMatrix = SkMatrix::Concat(*rec.fMatrix, shader.getLocalMatrix()); |
reed@google.com | b67b8e6 | 2014-05-12 18:12:24 +0000 | [diff] [blame] | 443 | if (rec.fLocalMatrix) { |
| 444 | newMatrix.preConcat(*rec.fLocalMatrix); |
| 445 | } |
senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 446 | // This (1,1) translation is due to WebKit's 1 based coordinates for the noise |
| 447 | // (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] | 448 | fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 449 | fPaintingData = new PaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, |
| 450 | shader.fBaseFrequencyY, newMatrix); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 451 | } |
| 452 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 453 | SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() { delete fPaintingData; } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 454 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 455 | void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan( |
| 456 | int x, int y, SkPMColor result[], int count) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 457 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
| 458 | StitchData stitchData; |
| 459 | for (int i = 0; i < count; ++i) { |
| 460 | result[i] = shade(point, stitchData); |
| 461 | point.fX += SK_Scalar1; |
| 462 | } |
| 463 | } |
| 464 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 465 | ///////////////////////////////////////////////////////////////////// |
| 466 | |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 467 | #if SK_SUPPORT_GPU |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 468 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 469 | class GrGLPerlinNoise : public GrGLSLFragmentProcessor { |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 470 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 471 | void emitCode(EmitArgs&) override; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 472 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 473 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 474 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 475 | protected: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 476 | void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 477 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 478 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 479 | GrGLSLProgramDataManager::UniformHandle fStitchDataUni; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 480 | GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni; |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 481 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 482 | typedef GrGLSLFragmentProcessor INHERITED; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | ///////////////////////////////////////////////////////////////////// |
| 486 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 487 | class GrPerlinNoiseEffect : public GrFragmentProcessor { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 488 | public: |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 489 | static sk_sp<GrFragmentProcessor> Make(GrResourceProvider* resourceProvider, |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 490 | SkPerlinNoiseShader::Type type, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 491 | int numOctaves, bool stitchTiles, |
| 492 | SkPerlinNoiseShader::PaintingData* paintingData, |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 493 | sk_sp<GrTextureProxy> permutationsProxy, |
| 494 | sk_sp<GrTextureProxy> noiseProxy, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 495 | const SkMatrix& matrix) { |
| 496 | return sk_sp<GrFragmentProcessor>( |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 497 | new GrPerlinNoiseEffect(resourceProvider, type, numOctaves, stitchTiles, paintingData, |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 498 | std::move(permutationsProxy), std::move(noiseProxy), matrix)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 499 | } |
| 500 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 501 | virtual ~GrPerlinNoiseEffect() { delete fPaintingData; } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 502 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 503 | const char* name() const override { return "PerlinNoise"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 504 | |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 505 | const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 506 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 507 | SkPerlinNoiseShader::Type type() const { return fType; } |
| 508 | bool stitchTiles() const { return fStitchTiles; } |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 509 | const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; } |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 510 | int numOctaves() const { return fNumOctaves; } |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 511 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 512 | private: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 513 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 514 | return new GrGLPerlinNoise; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 517 | virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 518 | GrProcessorKeyBuilder* b) const override { |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 519 | GrGLPerlinNoise::GenKey(*this, caps, b); |
| 520 | } |
| 521 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 522 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 523 | const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>(); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 524 | return fType == s.fType && |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 525 | fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency && |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 526 | fNumOctaves == s.fNumOctaves && |
| 527 | fStitchTiles == s.fStitchTiles && |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 528 | fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 531 | GrPerlinNoiseEffect(GrResourceProvider* resourceProvider, |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 532 | SkPerlinNoiseShader::Type type, int numOctaves, bool stitchTiles, |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 533 | SkPerlinNoiseShader::PaintingData* paintingData, |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 534 | sk_sp<GrTextureProxy> permutationsProxy, sk_sp<GrTextureProxy> noiseProxy, |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 535 | const SkMatrix& matrix) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 536 | : INHERITED(kNone_OptimizationFlags) |
| 537 | , fType(type) |
| 538 | , fCoordTransform(matrix) |
| 539 | , fNumOctaves(numOctaves) |
| 540 | , fStitchTiles(stitchTiles) |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 541 | , fPermutationsSampler(resourceProvider, std::move(permutationsProxy)) |
| 542 | , fNoiseSampler(resourceProvider, std::move(noiseProxy)) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 543 | , fPaintingData(paintingData) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 544 | this->initClassID<GrPerlinNoiseEffect>(); |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 545 | this->addTextureSampler(&fPermutationsSampler); |
| 546 | this->addTextureSampler(&fNoiseSampler); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 547 | this->addCoordTransform(&fCoordTransform); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 548 | } |
| 549 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 550 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 551 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 552 | SkPerlinNoiseShader::Type fType; |
| 553 | GrCoordTransform fCoordTransform; |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 554 | int fNumOctaves; |
| 555 | bool fStitchTiles; |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 556 | TextureSampler fPermutationsSampler; |
| 557 | TextureSampler fNoiseSampler; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 558 | SkPerlinNoiseShader::PaintingData *fPaintingData; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 559 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 560 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 561 | typedef GrFragmentProcessor INHERITED; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 562 | }; |
| 563 | |
| 564 | ///////////////////////////////////////////////////////////////////// |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 565 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 566 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 567 | #if GR_TEST_UTILS |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 568 | sk_sp<GrFragmentProcessor> GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 569 | int numOctaves = d->fRandom->nextRangeU(2, 10); |
| 570 | bool stitchTiles = d->fRandom->nextBool(); |
| 571 | SkScalar seed = SkIntToScalar(d->fRandom->nextU()); |
| 572 | SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096), |
| 573 | d->fRandom->nextRangeU(4, 4096)); |
| 574 | SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, |
| 575 | 0.99f); |
| 576 | SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, |
| 577 | 0.99f); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 578 | |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 579 | sk_sp<SkShader> shader(d->fRandom->nextBool() ? |
| 580 | SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| 581 | stitchTiles ? &tileSize : nullptr) : |
| 582 | SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| 583 | stitchTiles ? &tileSize : nullptr)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 584 | |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 585 | GrTest::TestAsFPArgs asFPArgs(d); |
| 586 | return shader->asFragmentProcessor(asFPArgs.args()); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 587 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 588 | #endif |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 589 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 590 | void GrGLPerlinNoise::emitCode(EmitArgs& args) { |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 591 | const GrPerlinNoiseEffect& pne = args.fFp.cast<GrPerlinNoiseEffect>(); |
| 592 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 593 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 594 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 595 | SkString vCoords = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 596 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 597 | fBaseFrequencyUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 598 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 599 | "baseFrequency"); |
| 600 | const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 601 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 602 | const char* stitchDataUni = nullptr; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 603 | if (pne.stitchTiles()) { |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame] | 604 | fStitchDataUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 605 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 606 | "stitchData"); |
| 607 | stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 608 | } |
| 609 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 610 | // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 |
| 611 | const char* chanCoordR = "0.125"; |
| 612 | const char* chanCoordG = "0.375"; |
| 613 | const char* chanCoordB = "0.625"; |
| 614 | const char* chanCoordA = "0.875"; |
| 615 | const char* chanCoord = "chanCoord"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 616 | const char* stitchData = "stitchData"; |
| 617 | const char* ratio = "ratio"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 618 | const char* noiseVec = "noiseVec"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 619 | const char* noiseSmooth = "noiseSmooth"; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 620 | const char* floorVal = "floorVal"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 621 | const char* fractVal = "fractVal"; |
| 622 | const char* uv = "uv"; |
| 623 | const char* ab = "ab"; |
| 624 | const char* latticeIdx = "latticeIdx"; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 625 | const char* bcoords = "bcoords"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 626 | const char* lattice = "lattice"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 627 | const char* inc8bit = "0.00390625"; // 1.0 / 256.0 |
| 628 | // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a |
| 629 | // [-1,1] vector and perform a dot product between that vector and the provided vector. |
| 630 | const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);"; |
| 631 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 632 | // Add noise function |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 633 | static const GrShaderVar gPerlinNoiseArgs[] = { |
| 634 | GrShaderVar(chanCoord, kFloat_GrSLType), |
| 635 | GrShaderVar(noiseVec, kVec2f_GrSLType) |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 636 | }; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 637 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 638 | static const GrShaderVar gPerlinNoiseStitchArgs[] = { |
| 639 | GrShaderVar(chanCoord, kFloat_GrSLType), |
| 640 | GrShaderVar(noiseVec, kVec2f_GrSLType), |
| 641 | GrShaderVar(stitchData, kVec2f_GrSLType) |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 642 | }; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 643 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 644 | SkString noiseCode; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 645 | |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 646 | noiseCode.appendf("\tvec4 %s;\n", floorVal); |
| 647 | noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec); |
| 648 | noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal); |
| 649 | noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 650 | |
| 651 | // smooth curve : t * t * (3 - 2 * t) |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 652 | noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);", |
| 653 | noiseSmooth, fractVal, fractVal, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 654 | |
| 655 | // Adjust frequencies if we're stitching tiles |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 656 | if (pne.stitchTiles()) { |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 657 | noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 658 | floorVal, stitchData, floorVal, stitchData); |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 659 | noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 660 | floorVal, stitchData, floorVal, stitchData); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 661 | noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 662 | floorVal, stitchData, floorVal, stitchData); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 663 | noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }", |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 664 | floorVal, stitchData, floorVal, stitchData); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | // Get texture coordinates and normalize |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 668 | 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] | 669 | floorVal, floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 670 | |
| 671 | // Get permutation for x |
| 672 | { |
| 673 | SkString xCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 674 | xCoords.appendf("vec2(%s.x, 0.5)", floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 675 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 676 | noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 677 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 678 | kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 679 | noiseCode.append(".r;"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // Get permutation for x + 1 |
| 683 | { |
| 684 | SkString xCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 685 | xCoords.appendf("vec2(%s.z, 0.5)", floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 686 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 687 | noiseCode.appendf("\n\t%s.y = ", latticeIdx); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 688 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 689 | kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 690 | noiseCode.append(".r;"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 691 | } |
| 692 | |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 693 | #if defined(SK_BUILD_FOR_ANDROID) |
| 694 | // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3). |
| 695 | // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit |
| 696 | // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725 |
| 697 | // (or 0.484368 here). The following rounding operation prevents these precision issues from |
| 698 | // affecting the result of the noise by making sure that we only have multiples of 1/255. |
| 699 | // (Note that 1/255 is about 0.003921569, which is the value used here). |
| 700 | noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);", |
| 701 | latticeIdx, latticeIdx); |
| 702 | #endif |
| 703 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 704 | // Get (x,y) coordinates with the permutated x |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 705 | 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] | 706 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 707 | noiseCode.appendf("\n\n\tvec2 %s;", uv); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 708 | // Compute u, at offset (0,0) |
| 709 | { |
| 710 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 711 | latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 712 | noiseCode.appendf("\n\tvec4 %s = ", lattice); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 713 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 714 | kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 715 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
| 716 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 717 | } |
| 718 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 719 | noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 720 | // Compute v, at offset (-1,0) |
| 721 | { |
| 722 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 723 | latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 724 | noiseCode.append("\n\tlattice = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 725 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 726 | kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 727 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
| 728 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | // Compute 'a' as a linear interpolation of 'u' and 'v' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 732 | noiseCode.appendf("\n\tvec2 %s;", ab); |
| 733 | 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] | 734 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 735 | noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 736 | // Compute v, at offset (-1,-1) |
| 737 | { |
| 738 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 739 | latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 740 | noiseCode.append("\n\tlattice = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 741 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 742 | kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 743 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
| 744 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 745 | } |
| 746 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 747 | noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 748 | // Compute u, at offset (0,-1) |
| 749 | { |
| 750 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 751 | latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 752 | noiseCode.append("\n\tlattice = "); |
cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 753 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 754 | kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 755 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
| 756 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // Compute 'b' as a linear interpolation of 'u' and 'v' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 760 | 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] | 761 | // Compute the noise as a linear interpolation of 'a' and 'b' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 762 | 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] | 763 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 764 | SkString noiseFuncName; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 765 | if (pne.stitchTiles()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 766 | fragBuilder->emitFunction(kFloat_GrSLType, |
| 767 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs), |
| 768 | gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 769 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 770 | fragBuilder->emitFunction(kFloat_GrSLType, |
| 771 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs), |
| 772 | gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 773 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 774 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 775 | // There are rounding errors if the floor operation is not performed here |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 776 | fragBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;", |
| 777 | noiseVec, vCoords.c_str(), baseFrequencyUni); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 778 | |
| 779 | // Clear the color accumulator |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 780 | fragBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", args.fOutputColor); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 781 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 782 | if (pne.stitchTiles()) { |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 783 | // Set up TurbulenceInitial stitch values. |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 784 | fragBuilder->codeAppendf("vec2 %s = %s;", stitchData, stitchDataUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 785 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 786 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 787 | fragBuilder->codeAppendf("float %s = 1.0;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 788 | |
| 789 | // Loop over all octaves |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 790 | fragBuilder->codeAppendf("for (int octave = 0; octave < %d; ++octave) {", pne.numOctaves()); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 791 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 792 | fragBuilder->codeAppendf("%s += ", args.fOutputColor); |
| 793 | if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 794 | fragBuilder->codeAppend("abs("); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 795 | } |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 796 | if (pne.stitchTiles()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 797 | fragBuilder->codeAppendf( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 798 | "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s)," |
| 799 | "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))", |
| 800 | noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData, |
| 801 | noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData, |
| 802 | noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData, |
| 803 | noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData); |
| 804 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 805 | fragBuilder->codeAppendf( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 806 | "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s)," |
| 807 | "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))", |
| 808 | noiseFuncName.c_str(), chanCoordR, noiseVec, |
| 809 | noiseFuncName.c_str(), chanCoordG, noiseVec, |
| 810 | noiseFuncName.c_str(), chanCoordB, noiseVec, |
| 811 | noiseFuncName.c_str(), chanCoordA, noiseVec); |
| 812 | } |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 813 | if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 814 | fragBuilder->codeAppendf(")"); // end of "abs(" |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 815 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 816 | fragBuilder->codeAppendf(" * %s;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 817 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 818 | fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec); |
| 819 | fragBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 820 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 821 | if (pne.stitchTiles()) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 822 | fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 823 | } |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 824 | 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] | 825 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 826 | if (pne.type() == SkPerlinNoiseShader::kFractalNoise_Type) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 827 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 828 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 829 | fragBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", |
| 830 | args.fOutputColor,args.fOutputColor); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 831 | } |
| 832 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 833 | // Clamp values |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 834 | 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] | 835 | |
| 836 | // Pre-multiply the result |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 837 | fragBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", |
| 838 | args.fOutputColor, args.fOutputColor, |
| 839 | args.fOutputColor, args.fOutputColor); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 842 | void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 843 | GrProcessorKeyBuilder* b) { |
| 844 | const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 845 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 846 | uint32_t key = turbulence.numOctaves(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 847 | |
| 848 | key = key << 3; // Make room for next 3 bits |
| 849 | |
| 850 | switch (turbulence.type()) { |
| 851 | case SkPerlinNoiseShader::kFractalNoise_Type: |
| 852 | key |= 0x1; |
| 853 | break; |
| 854 | case SkPerlinNoiseShader::kTurbulence_Type: |
| 855 | key |= 0x2; |
| 856 | break; |
| 857 | default: |
| 858 | // leave key at 0 |
| 859 | break; |
| 860 | } |
| 861 | |
| 862 | if (turbulence.stitchTiles()) { |
| 863 | key |= 0x4; // Flip the 3rd bit if tile stitching is on |
| 864 | } |
| 865 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 866 | b->add32(key); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 867 | } |
| 868 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 869 | void GrGLPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman, |
| 870 | const GrProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 871 | INHERITED::onSetData(pdman, processor); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 872 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 873 | const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 874 | |
| 875 | const SkVector& baseFrequency = turbulence.baseFrequency(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 876 | pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 877 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 878 | if (turbulence.stitchTiles()) { |
| 879 | const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 880 | pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 881 | SkIntToScalar(stitchData.fHeight)); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 885 | ///////////////////////////////////////////////////////////////////// |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 886 | sk_sp<GrFragmentProcessor> SkPerlinNoiseShader::asFragmentProcessor(const AsFPArgs& args) const { |
| 887 | SkASSERT(args.fContext); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 888 | |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 889 | SkMatrix localMatrix = this->getLocalMatrix(); |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 890 | if (args.fLocalMatrix) { |
| 891 | localMatrix.preConcat(*args.fLocalMatrix); |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 892 | } |
| 893 | |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 894 | SkMatrix matrix = *args.fViewMatrix; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 895 | matrix.preConcat(localMatrix); |
| 896 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 897 | if (0 == fNumOctaves) { |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 898 | if (kFractalNoise_Type == fType) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 899 | // Extract the incoming alpha and emit rgba = (a/4, a/4, a/4, a/2) |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 900 | // TODO: Either treat the output of this shader as sRGB or allow client to specify a |
| 901 | // color space of the noise. Either way, this case (and the GLSL) need to convert to |
| 902 | // the destination. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 903 | sk_sp<GrFragmentProcessor> inner( |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 904 | GrConstColorProcessor::Make(GrColor4f::FromGrColor(0x80404040), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 905 | GrConstColorProcessor::kModulateRGBA_InputMode)); |
| 906 | return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 907 | } |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 908 | // Emit zero. |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 909 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
| 910 | GrConstColorProcessor::kIgnore_InputMode); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 911 | } |
| 912 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 913 | // Either we don't stitch tiles, either we have a valid tile size |
| 914 | SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
| 915 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 916 | SkPerlinNoiseShader::PaintingData* paintingData = |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 917 | new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix); |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 918 | sk_sp<GrTextureProxy> permutationsProxy(GrMakeCachedBitmapProxy( |
| 919 | args.fContext, |
| 920 | paintingData->getPermutationsBitmap())); |
| 921 | sk_sp<GrTextureProxy> noiseProxy(GrMakeCachedBitmapProxy(args.fContext, |
| 922 | paintingData->getNoiseBitmap())); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 923 | |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 924 | SkMatrix m = *args.fViewMatrix; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 925 | m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); |
| 926 | m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 927 | if (permutationsProxy && noiseProxy) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 928 | sk_sp<GrFragmentProcessor> inner( |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 929 | GrPerlinNoiseEffect::Make(args.fContext->resourceProvider(), |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 930 | fType, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 931 | fNumOctaves, |
| 932 | fStitchTiles, |
| 933 | paintingData, |
Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 934 | std::move(permutationsProxy), |
| 935 | std::move(noiseProxy), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 936 | m)); |
| 937 | return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner)); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 938 | } |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 939 | delete paintingData; |
| 940 | return nullptr; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | #endif |
| 944 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 945 | #ifndef SK_IGNORE_TO_STRING |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 946 | void SkPerlinNoiseShader::toString(SkString* str) const { |
| 947 | str->append("SkPerlinNoiseShader: ("); |
| 948 | |
| 949 | str->append("type: "); |
| 950 | switch (fType) { |
| 951 | case kFractalNoise_Type: |
| 952 | str->append("\"fractal noise\""); |
| 953 | break; |
| 954 | case kTurbulence_Type: |
| 955 | str->append("\"turbulence\""); |
| 956 | break; |
| 957 | default: |
| 958 | str->append("\"unknown\""); |
| 959 | break; |
| 960 | } |
| 961 | str->append(" base frequency: ("); |
| 962 | str->appendScalar(fBaseFrequencyX); |
| 963 | str->append(", "); |
| 964 | str->appendScalar(fBaseFrequencyY); |
| 965 | str->append(") number of octaves: "); |
| 966 | str->appendS32(fNumOctaves); |
| 967 | str->append(" seed: "); |
| 968 | str->appendScalar(fSeed); |
| 969 | str->append(" stitch tiles: "); |
| 970 | str->append(fStitchTiles ? "true " : "false "); |
| 971 | |
| 972 | this->INHERITED::toString(str); |
| 973 | |
| 974 | str->append(")"); |
| 975 | } |
| 976 | #endif |