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