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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/effects/SkPerlinNoiseShader.h" |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 9 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkColorFilter.h" |
| 12 | #include "include/core/SkShader.h" |
| 13 | #include "include/core/SkString.h" |
| 14 | #include "include/core/SkUnPreMultiply.h" |
Mike Klein | 8aa0edf | 2020-10-16 11:04:18 -0500 | [diff] [blame] | 15 | #include "include/private/SkTPin.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 16 | #include "src/core/SkArenaAlloc.h" |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 17 | #include "src/core/SkMatrixProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/core/SkReadBuffer.h" |
Mike Klein | e7541d9 | 2021-01-13 13:53:05 -0600 | [diff] [blame] | 19 | #include "src/core/SkVM.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/core/SkWriteBuffer.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 21 | |
| 22 | #if SK_SUPPORT_GPU |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 23 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrRecordingContextPriv.h" |
| 25 | #include "src/gpu/SkGr.h" |
Michael Ludwig | f2935c6 | 2020-06-26 11:07:23 -0400 | [diff] [blame] | 26 | #include "src/gpu/effects/GrMatrixEffect.h" |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 27 | #include "src/gpu/effects/GrTextureEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 29 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 30 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 31 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | static const int kBlockSize = 256; |
| 35 | static const int kBlockMask = kBlockSize - 1; |
| 36 | static const int kPerlinNoise = 4096; |
| 37 | static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
| 38 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 39 | class SkPerlinNoiseShaderImpl : public SkShaderBase { |
| 40 | public: |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 41 | struct StitchData { |
| 42 | StitchData() |
| 43 | : fWidth(0) |
| 44 | , fWrapX(0) |
| 45 | , fHeight(0) |
| 46 | , fWrapY(0) |
| 47 | {} |
| 48 | |
Florin Malita | 102c8cf | 2018-06-05 17:37:12 -0400 | [diff] [blame] | 49 | StitchData(SkScalar w, SkScalar h) |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 50 | : fWidth(std::min(SkScalarRoundToInt(w), SK_MaxS32 - kPerlinNoise)) |
Florin Malita | 102c8cf | 2018-06-05 17:37:12 -0400 | [diff] [blame] | 51 | , fWrapX(kPerlinNoise + fWidth) |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 52 | , fHeight(std::min(SkScalarRoundToInt(h), SK_MaxS32 - kPerlinNoise)) |
Florin Malita | 102c8cf | 2018-06-05 17:37:12 -0400 | [diff] [blame] | 53 | , fWrapY(kPerlinNoise + fHeight) {} |
| 54 | |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 55 | bool operator==(const StitchData& other) const { |
| 56 | return fWidth == other.fWidth && |
| 57 | fWrapX == other.fWrapX && |
| 58 | fHeight == other.fHeight && |
| 59 | fWrapY == other.fWrapY; |
| 60 | } |
| 61 | |
| 62 | int fWidth; // How much to subtract to wrap for stitching. |
| 63 | int fWrapX; // Minimum value to wrap. |
| 64 | int fHeight; |
| 65 | int fWrapY; |
| 66 | }; |
| 67 | |
| 68 | struct PaintingData { |
| 69 | PaintingData(const SkISize& tileSize, SkScalar seed, |
| 70 | SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 71 | const SkMatrix& matrix) |
| 72 | { |
Ethan Nicholas | 8294015 | 2019-01-10 13:58:14 -0500 | [diff] [blame] | 73 | SkVector tileVec; |
| 74 | matrix.mapVector(SkIntToScalar(tileSize.fWidth), SkIntToScalar(tileSize.fHeight), |
| 75 | &tileVec); |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 76 | |
Ethan Nicholas | 8294015 | 2019-01-10 13:58:14 -0500 | [diff] [blame] | 77 | SkSize scale; |
Ethan Nicholas | 2ee498c | 2019-01-11 15:32:05 -0500 | [diff] [blame] | 78 | if (!matrix.decomposeScale(&scale, nullptr)) { |
| 79 | scale.set(SK_ScalarNearlyZero, SK_ScalarNearlyZero); |
| 80 | } |
Ethan Nicholas | 8294015 | 2019-01-10 13:58:14 -0500 | [diff] [blame] | 81 | fBaseFrequency.set(baseFrequencyX * SkScalarInvert(scale.width()), |
| 82 | baseFrequencyY * SkScalarInvert(scale.height())); |
| 83 | fTileSize.set(SkScalarRoundToInt(tileVec.fX), SkScalarRoundToInt(tileVec.fY)); |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 84 | this->init(seed); |
| 85 | if (!fTileSize.isEmpty()) { |
| 86 | this->stitch(); |
| 87 | } |
| 88 | |
| 89 | #if SK_SUPPORT_GPU |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 90 | SkImageInfo info = SkImageInfo::MakeA8(kBlockSize, 1); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 91 | fPermutationsBitmap.installPixels(info, fLatticeSelector, info.minRowBytes()); |
| 92 | fPermutationsBitmap.setImmutable(); |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 93 | |
Brian Salomon | a3a9da7 | 2020-06-17 10:52:19 -0400 | [diff] [blame] | 94 | info = SkImageInfo::Make(kBlockSize, 4, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 95 | fNoiseBitmap.installPixels(info, fNoise[0][0], info.minRowBytes()); |
| 96 | fNoiseBitmap.setImmutable(); |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 97 | #endif |
| 98 | } |
| 99 | |
Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 100 | #if SK_SUPPORT_GPU |
| 101 | PaintingData(const PaintingData& that) |
| 102 | : fSeed(that.fSeed) |
| 103 | , fTileSize(that.fTileSize) |
| 104 | , fBaseFrequency(that.fBaseFrequency) |
| 105 | , fStitchDataInit(that.fStitchDataInit) |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 106 | , fPermutationsBitmap(that.fPermutationsBitmap) |
Brian Salomon | 4878b3e | 2021-01-08 18:11:33 -0500 | [diff] [blame] | 107 | , fNoiseBitmap(that.fNoiseBitmap) { |
Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 108 | memcpy(fLatticeSelector, that.fLatticeSelector, sizeof(fLatticeSelector)); |
| 109 | memcpy(fNoise, that.fNoise, sizeof(fNoise)); |
| 110 | memcpy(fGradient, that.fGradient, sizeof(fGradient)); |
| 111 | } |
| 112 | #endif |
| 113 | |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 114 | int fSeed; |
| 115 | uint8_t fLatticeSelector[kBlockSize]; |
| 116 | uint16_t fNoise[4][kBlockSize][2]; |
| 117 | SkPoint fGradient[4][kBlockSize]; |
| 118 | SkISize fTileSize; |
| 119 | SkVector fBaseFrequency; |
| 120 | StitchData fStitchDataInit; |
| 121 | |
| 122 | private: |
| 123 | |
| 124 | #if SK_SUPPORT_GPU |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 125 | SkBitmap fPermutationsBitmap; |
| 126 | SkBitmap fNoiseBitmap; |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 127 | #endif |
| 128 | |
| 129 | inline int random() { |
John Stiles | 039f681 | 2020-08-10 09:51:42 -0400 | [diff] [blame] | 130 | // See https://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement |
| 131 | // m = kRandMaximum, 2**31 - 1 (2147483647) |
| 132 | static constexpr int kRandAmplitude = 16807; // 7**5; primitive root of m |
| 133 | static constexpr int kRandQ = 127773; // m / a |
| 134 | static constexpr int kRandR = 2836; // m % a |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 135 | |
John Stiles | 039f681 | 2020-08-10 09:51:42 -0400 | [diff] [blame] | 136 | int result = kRandAmplitude * (fSeed % kRandQ) - kRandR * (fSeed / kRandQ); |
| 137 | if (result <= 0) { |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 138 | result += kRandMaximum; |
John Stiles | 039f681 | 2020-08-10 09:51:42 -0400 | [diff] [blame] | 139 | } |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 140 | fSeed = result; |
| 141 | return result; |
| 142 | } |
| 143 | |
| 144 | // Only called once. Could be part of the constructor. |
| 145 | void init(SkScalar seed) |
| 146 | { |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 147 | // According to the SVG spec, we must truncate (not round) the seed value. |
| 148 | fSeed = SkScalarTruncToInt(seed); |
| 149 | // The seed value clamp to the range [1, kRandMaximum - 1]. |
| 150 | if (fSeed <= 0) { |
| 151 | fSeed = -(fSeed % (kRandMaximum - 1)) + 1; |
| 152 | } |
| 153 | if (fSeed > kRandMaximum - 1) { |
| 154 | fSeed = kRandMaximum - 1; |
| 155 | } |
| 156 | for (int channel = 0; channel < 4; ++channel) { |
| 157 | for (int i = 0; i < kBlockSize; ++i) { |
| 158 | fLatticeSelector[i] = i; |
| 159 | fNoise[channel][i][0] = (random() % (2 * kBlockSize)); |
| 160 | fNoise[channel][i][1] = (random() % (2 * kBlockSize)); |
| 161 | } |
| 162 | } |
| 163 | for (int i = kBlockSize - 1; i > 0; --i) { |
| 164 | int k = fLatticeSelector[i]; |
| 165 | int j = random() % kBlockSize; |
| 166 | SkASSERT(j >= 0); |
| 167 | SkASSERT(j < kBlockSize); |
| 168 | fLatticeSelector[i] = fLatticeSelector[j]; |
| 169 | fLatticeSelector[j] = k; |
| 170 | } |
| 171 | |
| 172 | // Perform the permutations now |
| 173 | { |
| 174 | // Copy noise data |
| 175 | uint16_t noise[4][kBlockSize][2]; |
| 176 | for (int i = 0; i < kBlockSize; ++i) { |
| 177 | for (int channel = 0; channel < 4; ++channel) { |
| 178 | for (int j = 0; j < 2; ++j) { |
| 179 | noise[channel][i][j] = fNoise[channel][i][j]; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | // Do permutations on noise data |
| 184 | for (int i = 0; i < kBlockSize; ++i) { |
| 185 | for (int channel = 0; channel < 4; ++channel) { |
| 186 | for (int j = 0; j < 2; ++j) { |
| 187 | fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j]; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Half of the largest possible value for 16 bit unsigned int |
John Stiles | 039f681 | 2020-08-10 09:51:42 -0400 | [diff] [blame] | 194 | static constexpr SkScalar kHalfMax16bits = 32767.5f; |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 195 | |
| 196 | // Compute gradients from permutated noise data |
John Stiles | 039f681 | 2020-08-10 09:51:42 -0400 | [diff] [blame] | 197 | static constexpr SkScalar kInvBlockSizef = 1.0 / SkIntToScalar(kBlockSize); |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 198 | for (int channel = 0; channel < 4; ++channel) { |
| 199 | for (int i = 0; i < kBlockSize; ++i) { |
| 200 | fGradient[channel][i] = SkPoint::Make( |
John Stiles | 039f681 | 2020-08-10 09:51:42 -0400 | [diff] [blame] | 201 | (fNoise[channel][i][0] - kBlockSize) * kInvBlockSizef, |
| 202 | (fNoise[channel][i][1] - kBlockSize) * kInvBlockSizef); |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 203 | fGradient[channel][i].normalize(); |
| 204 | // Put the normalized gradient back into the noise data |
John Stiles | 039f681 | 2020-08-10 09:51:42 -0400 | [diff] [blame] | 205 | fNoise[channel][i][0] = |
| 206 | SkScalarRoundToInt((fGradient[channel][i].fX + 1) * kHalfMax16bits); |
| 207 | fNoise[channel][i][1] = |
| 208 | SkScalarRoundToInt((fGradient[channel][i].fY + 1) * kHalfMax16bits); |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Only called once. Could be part of the constructor. |
| 214 | void stitch() { |
| 215 | SkScalar tileWidth = SkIntToScalar(fTileSize.width()); |
| 216 | SkScalar tileHeight = SkIntToScalar(fTileSize.height()); |
| 217 | SkASSERT(tileWidth > 0 && tileHeight > 0); |
| 218 | // When stitching tiled turbulence, the frequencies must be adjusted |
| 219 | // so that the tile borders will be continuous. |
| 220 | if (fBaseFrequency.fX) { |
| 221 | SkScalar lowFrequencx = |
| 222 | SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
| 223 | SkScalar highFrequencx = |
| 224 | SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
| 225 | // BaseFrequency should be non-negative according to the standard. |
Kevin Lubick | 6ee268d | 2018-05-15 13:59:54 -0400 | [diff] [blame] | 226 | // lowFrequencx can be 0 if fBaseFrequency.fX is very small. |
| 227 | if (sk_ieee_float_divide(fBaseFrequency.fX, lowFrequencx) < highFrequencx / fBaseFrequency.fX) { |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 228 | fBaseFrequency.fX = lowFrequencx; |
| 229 | } else { |
| 230 | fBaseFrequency.fX = highFrequencx; |
| 231 | } |
| 232 | } |
| 233 | if (fBaseFrequency.fY) { |
| 234 | SkScalar lowFrequency = |
| 235 | SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
| 236 | SkScalar highFrequency = |
| 237 | SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
Kevin Lubick | 6ee268d | 2018-05-15 13:59:54 -0400 | [diff] [blame] | 238 | // lowFrequency can be 0 if fBaseFrequency.fY is very small. |
| 239 | if (sk_ieee_float_divide(fBaseFrequency.fY, lowFrequency) < highFrequency / fBaseFrequency.fY) { |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 240 | fBaseFrequency.fY = lowFrequency; |
| 241 | } else { |
| 242 | fBaseFrequency.fY = highFrequency; |
| 243 | } |
| 244 | } |
| 245 | // Set up TurbulenceInitial stitch values. |
Florin Malita | 102c8cf | 2018-06-05 17:37:12 -0400 | [diff] [blame] | 246 | fStitchDataInit = StitchData(tileWidth * fBaseFrequency.fX, |
| 247 | tileHeight * fBaseFrequency.fY); |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | public: |
| 251 | |
| 252 | #if SK_SUPPORT_GPU |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 253 | const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; } |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 254 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 255 | const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; } |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 256 | #endif |
| 257 | }; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * About the noise types : the difference between the first 2 is just minor tweaks to the |
| 261 | * algorithm, they're not 2 entirely different noises. The output looks different, but once the |
| 262 | * noise is generated in the [1, -1] range, the output is brought back in the [0, 1] range by |
| 263 | * doing : |
| 264 | * kFractalNoise_Type : noise * 0.5 + 0.5 |
| 265 | * kTurbulence_Type : abs(noise) |
| 266 | * Very little differences between the 2 types, although you can tell the difference visually. |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 267 | */ |
| 268 | enum Type { |
| 269 | kFractalNoise_Type, |
| 270 | kTurbulence_Type, |
Brian Salomon | 4878b3e | 2021-01-08 18:11:33 -0500 | [diff] [blame] | 271 | kLast_Type = kTurbulence_Type |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 272 | }; |
| 273 | |
Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 274 | static const int kMaxOctaves = 255; // numOctaves must be <= 0 and <= kMaxOctaves |
| 275 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 276 | SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::Type type, SkScalar baseFrequencyX, |
| 277 | SkScalar baseFrequencyY, int numOctaves, SkScalar seed, |
| 278 | const SkISize* tileSize); |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 279 | |
| 280 | class PerlinNoiseShaderContext : public Context { |
| 281 | public: |
| 282 | PerlinNoiseShaderContext(const SkPerlinNoiseShaderImpl& shader, const ContextRec&); |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 283 | |
| 284 | void shadeSpan(int x, int y, SkPMColor[], int count) override; |
| 285 | |
| 286 | private: |
| 287 | SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; |
| 288 | SkScalar calculateTurbulenceValueForPoint( |
| 289 | int channel, |
| 290 | StitchData& stitchData, const SkPoint& point) const; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 291 | SkScalar noise2D(int channel, |
| 292 | const StitchData& stitchData, const SkPoint& noiseVector) const; |
| 293 | |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 294 | SkMatrix fMatrix; |
| 295 | PaintingData fPaintingData; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 296 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 297 | using INHERITED = Context; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | #if SK_SUPPORT_GPU |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 301 | std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 302 | #endif |
| 303 | |
Mike Klein | e7541d9 | 2021-01-13 13:53:05 -0600 | [diff] [blame] | 304 | skvm::Color onProgram(skvm::Builder*, |
| 305 | skvm::Coord, skvm::Coord, skvm::Color, |
Mike Reed | 8046837 | 2021-03-19 14:05:19 -0400 | [diff] [blame] | 306 | const SkMatrixProvider&, const SkMatrix*, const SkColorInfo&, |
Mike Klein | e7541d9 | 2021-01-13 13:53:05 -0600 | [diff] [blame] | 307 | skvm::Uniforms*, SkArenaAlloc*) const override { |
| 308 | // TODO? |
| 309 | return {}; |
| 310 | } |
| 311 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 312 | protected: |
| 313 | void flatten(SkWriteBuffer&) const override; |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 314 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 315 | Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override; |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 316 | #endif |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 317 | |
| 318 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 319 | SK_FLATTENABLE_HOOKS(SkPerlinNoiseShaderImpl) |
| 320 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 321 | const SkPerlinNoiseShaderImpl::Type fType; |
| 322 | const SkScalar fBaseFrequencyX; |
| 323 | const SkScalar fBaseFrequencyY; |
| 324 | const int fNumOctaves; |
| 325 | const SkScalar fSeed; |
| 326 | const SkISize fTileSize; |
| 327 | const bool fStitchTiles; |
| 328 | |
| 329 | friend class ::SkPerlinNoiseShader; |
| 330 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 331 | using INHERITED = SkShaderBase; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 332 | }; |
| 333 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 334 | namespace { |
| 335 | |
| 336 | // noiseValue is the color component's value (or color) |
| 337 | // limitValue is the maximum perlin noise array index value allowed |
| 338 | // newValue is the current noise dimension (either width or height) |
| 339 | inline int checkNoise(int noiseValue, int limitValue, int newValue) { |
| 340 | // If the noise value would bring us out of bounds of the current noise array while we are |
| 341 | // stiching noise tiles together, wrap the noise around the current dimension of the noise to |
| 342 | // stay within the array bounds in a continuous fashion (so that tiling lines are not visible) |
| 343 | if (noiseValue >= limitValue) { |
| 344 | noiseValue -= newValue; |
| 345 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 346 | return noiseValue; |
| 347 | } |
| 348 | |
| 349 | inline SkScalar smoothCurve(SkScalar t) { |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 350 | return t * t * (3 - 2 * t); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | } // end namespace |
| 354 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 355 | SkPerlinNoiseShaderImpl::SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::Type type, |
Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 356 | SkScalar baseFrequencyX, |
| 357 | SkScalar baseFrequencyY, |
| 358 | int numOctaves, |
| 359 | SkScalar seed, |
| 360 | const SkISize* tileSize) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 361 | : fType(type) |
| 362 | , fBaseFrequencyX(baseFrequencyX) |
| 363 | , fBaseFrequencyY(baseFrequencyY) |
Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 364 | , fNumOctaves(numOctaves > kMaxOctaves ? kMaxOctaves : numOctaves/*[0,255] octaves allowed*/) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 365 | , fSeed(seed) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 366 | , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize) |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 367 | , fStitchTiles(!fTileSize.isEmpty()) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 368 | { |
Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 369 | SkASSERT(numOctaves >= 0 && numOctaves <= kMaxOctaves); |
Kevin Lubick | 6ee268d | 2018-05-15 13:59:54 -0400 | [diff] [blame] | 370 | SkASSERT(fBaseFrequencyX >= 0); |
| 371 | SkASSERT(fBaseFrequencyY >= 0); |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 374 | sk_sp<SkFlattenable> SkPerlinNoiseShaderImpl::CreateProc(SkReadBuffer& buffer) { |
Mike Reed | de5c502 | 2018-01-26 14:59:12 -0500 | [diff] [blame] | 375 | Type type = buffer.read32LE(kLast_Type); |
Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 376 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 377 | SkScalar freqX = buffer.readScalar(); |
| 378 | SkScalar freqY = buffer.readScalar(); |
Mike Reed | de5c502 | 2018-01-26 14:59:12 -0500 | [diff] [blame] | 379 | int octaves = buffer.read32LE<int>(kMaxOctaves); |
Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 380 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 381 | SkScalar seed = buffer.readScalar(); |
| 382 | SkISize tileSize; |
| 383 | tileSize.fWidth = buffer.readInt(); |
| 384 | tileSize.fHeight = buffer.readInt(); |
| 385 | |
| 386 | switch (type) { |
| 387 | case kFractalNoise_Type: |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 388 | return SkPerlinNoiseShader::MakeFractalNoise(freqX, freqY, octaves, seed, &tileSize); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 389 | case kTurbulence_Type: |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 390 | return SkPerlinNoiseShader::MakeTurbulence(freqX, freqY, octaves, seed, &tileSize); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 391 | default: |
Mike Reed | de5c502 | 2018-01-26 14:59:12 -0500 | [diff] [blame] | 392 | // Really shouldn't get here b.c. of earlier check on type |
Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 393 | buffer.validate(false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 394 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 398 | void SkPerlinNoiseShaderImpl::flatten(SkWriteBuffer& buffer) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 399 | buffer.writeInt((int) fType); |
| 400 | buffer.writeScalar(fBaseFrequencyX); |
| 401 | buffer.writeScalar(fBaseFrequencyY); |
| 402 | buffer.writeInt(fNumOctaves); |
| 403 | buffer.writeScalar(fSeed); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 404 | buffer.writeInt(fTileSize.fWidth); |
| 405 | buffer.writeInt(fTileSize.fHeight); |
| 406 | } |
| 407 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 408 | SkScalar SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::noise2D( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 409 | int channel, const StitchData& stitchData, const SkPoint& noiseVector) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 410 | struct Noise { |
| 411 | int noisePositionIntegerValue; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 412 | int nextNoisePositionIntegerValue; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 413 | SkScalar noisePositionFractionValue; |
| 414 | Noise(SkScalar component) |
| 415 | { |
| 416 | SkScalar position = component + kPerlinNoise; |
| 417 | noisePositionIntegerValue = SkScalarFloorToInt(position); |
| 418 | noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 419 | nextNoisePositionIntegerValue = noisePositionIntegerValue + 1; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 420 | } |
| 421 | }; |
| 422 | Noise noiseX(noiseVector.x()); |
| 423 | Noise noiseY(noiseVector.y()); |
| 424 | SkScalar u, v; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 425 | const SkPerlinNoiseShaderImpl& perlinNoiseShader = static_cast<const SkPerlinNoiseShaderImpl&>(fShader); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 426 | // If stitching, adjust lattice points accordingly. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 427 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 428 | noiseX.noisePositionIntegerValue = |
| 429 | checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 430 | noiseY.noisePositionIntegerValue = |
| 431 | checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 432 | noiseX.nextNoisePositionIntegerValue = |
| 433 | checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 434 | noiseY.nextNoisePositionIntegerValue = |
| 435 | checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 436 | } |
| 437 | noiseX.noisePositionIntegerValue &= kBlockMask; |
| 438 | noiseY.noisePositionIntegerValue &= kBlockMask; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 439 | noiseX.nextNoisePositionIntegerValue &= kBlockMask; |
| 440 | noiseY.nextNoisePositionIntegerValue &= kBlockMask; |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 441 | int i = fPaintingData.fLatticeSelector[noiseX.noisePositionIntegerValue]; |
| 442 | int j = fPaintingData.fLatticeSelector[noiseX.nextNoisePositionIntegerValue]; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 443 | int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 444 | int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 445 | int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
| 446 | int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 447 | SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue); |
| 448 | SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue); |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 449 | |
Hal Canary | fda4600 | 2017-05-08 17:17:47 -0400 | [diff] [blame] | 450 | if (sx < 0 || sy < 0 || sx > 1 || sy > 1) { |
| 451 | return 0; // Check for pathological inputs. |
| 452 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 453 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 454 | // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement |
| 455 | SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue, |
| 456 | noiseY.noisePositionFractionValue); // Offset (0,0) |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 457 | u = fPaintingData.fGradient[channel][b00].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 458 | fractionValue.fX -= SK_Scalar1; // Offset (-1,0) |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 459 | v = fPaintingData.fGradient[channel][b10].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 460 | SkScalar a = SkScalarInterp(u, v, sx); |
| 461 | fractionValue.fY -= SK_Scalar1; // Offset (-1,-1) |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 462 | v = fPaintingData.fGradient[channel][b11].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 463 | fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1) |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 464 | u = fPaintingData.fGradient[channel][b01].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 465 | SkScalar b = SkScalarInterp(u, v, sx); |
| 466 | return SkScalarInterp(a, b, sy); |
| 467 | } |
| 468 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 469 | SkScalar SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 470 | int channel, StitchData& stitchData, const SkPoint& point) const { |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 471 | const SkPerlinNoiseShaderImpl& perlinNoiseShader = static_cast<const SkPerlinNoiseShaderImpl&>(fShader); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 472 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 473 | // Set up TurbulenceInitial stitch values. |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 474 | stitchData = fPaintingData.fStitchDataInit; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 475 | } |
| 476 | SkScalar turbulenceFunctionResult = 0; |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 477 | SkPoint noiseVector(SkPoint::Make(point.x() * fPaintingData.fBaseFrequency.fX, |
| 478 | point.y() * fPaintingData.fBaseFrequency.fY)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 479 | SkScalar ratio = SK_Scalar1; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 480 | for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) { |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 481 | SkScalar noise = noise2D(channel, stitchData, noiseVector); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 482 | SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ? |
| 483 | noise : SkScalarAbs(noise); |
| 484 | turbulenceFunctionResult += numer / ratio; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 485 | noiseVector.fX *= 2; |
| 486 | noiseVector.fY *= 2; |
| 487 | ratio *= 2; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 488 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 489 | // Update stitch values |
John Stiles | 039f681 | 2020-08-10 09:51:42 -0400 | [diff] [blame] | 490 | stitchData = StitchData(SkIntToScalar(stitchData.fWidth) * 2, |
Florin Malita | 102c8cf | 2018-06-05 17:37:12 -0400 | [diff] [blame] | 491 | SkIntToScalar(stitchData.fHeight) * 2); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 496 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 497 | if (perlinNoiseShader.fType == kFractalNoise_Type) { |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 498 | turbulenceFunctionResult = SkScalarHalf(turbulenceFunctionResult + 1); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if (channel == 3) { // Scale alpha by paint value |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 502 | turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | // Clamp result |
Brian Osman | aba642c | 2020-02-06 12:52:25 -0500 | [diff] [blame] | 506 | return SkTPin(turbulenceFunctionResult, 0.0f, SK_Scalar1); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 509 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 510 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 511 | SkPMColor SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::shade( |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 512 | const SkPoint& point, StitchData& stitchData) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 513 | SkPoint newPoint; |
senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 514 | fMatrix.mapPoints(&newPoint, &point, 1); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 515 | newPoint.fX = SkScalarRoundToScalar(newPoint.fX); |
| 516 | newPoint.fY = SkScalarRoundToScalar(newPoint.fY); |
| 517 | |
| 518 | U8CPU rgba[4]; |
| 519 | for (int channel = 3; channel >= 0; --channel) { |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 520 | SkScalar value; |
Brian Salomon | 4878b3e | 2021-01-08 18:11:33 -0500 | [diff] [blame] | 521 | value = calculateTurbulenceValueForPoint(channel, stitchData, newPoint); |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 522 | rgba[channel] = SkScalarFloorToInt(255 * value); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 523 | } |
| 524 | return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]); |
| 525 | } |
| 526 | |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 527 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 528 | SkShaderBase::Context* SkPerlinNoiseShaderImpl::onMakeContext(const ContextRec& rec, |
Robert Phillips | f18c756 | 2018-06-13 09:01:36 -0400 | [diff] [blame] | 529 | SkArenaAlloc* alloc) const { |
Mike Reed | 011d166 | 2019-02-28 17:19:25 -0500 | [diff] [blame] | 530 | // should we pay attention to rec's device-colorspace? |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 531 | return alloc->make<PerlinNoiseShaderContext>(*this, rec); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 532 | } |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 533 | #endif |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 534 | |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 535 | static inline SkMatrix total_matrix(const SkShaderBase::ContextRec& rec, |
| 536 | const SkShaderBase& shader) { |
| 537 | SkMatrix matrix = SkMatrix::Concat(*rec.fMatrix, shader.getLocalMatrix()); |
| 538 | if (rec.fLocalMatrix) { |
| 539 | matrix.preConcat(*rec.fLocalMatrix); |
| 540 | } |
| 541 | |
| 542 | return matrix; |
| 543 | } |
| 544 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 545 | SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::PerlinNoiseShaderContext( |
| 546 | const SkPerlinNoiseShaderImpl& shader, const ContextRec& rec) |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 547 | : INHERITED(shader, rec) |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 548 | , fMatrix(total_matrix(rec, shader)) // used for temp storage, adjusted below |
| 549 | , fPaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, |
| 550 | shader.fBaseFrequencyY, fMatrix) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 551 | { |
senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 552 | // This (1,1) translation is due to WebKit's 1 based coordinates for the noise |
| 553 | // (as opposed to 0 based, usually). The same adjustment is in the setData() function. |
Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 554 | fMatrix.setTranslate(-fMatrix.getTranslateX() + SK_Scalar1, |
| 555 | -fMatrix.getTranslateY() + SK_Scalar1); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 558 | void SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::shadeSpan( |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 559 | int x, int y, SkPMColor result[], int count) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 560 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
| 561 | StitchData stitchData; |
| 562 | for (int i = 0; i < count; ++i) { |
| 563 | result[i] = shade(point, stitchData); |
| 564 | point.fX += SK_Scalar1; |
| 565 | } |
| 566 | } |
| 567 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 568 | ///////////////////////////////////////////////////////////////////// |
| 569 | |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 570 | #if SK_SUPPORT_GPU |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 571 | |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 572 | class GrGLPerlinNoise : public GrFragmentProcessor::ProgramImpl { |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 573 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 574 | void emitCode(EmitArgs&) override; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 575 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 576 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 577 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 578 | protected: |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 579 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 580 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 581 | private: |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 582 | GrGLSLProgramDataManager::UniformHandle fStitchDataUni; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 583 | GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni; |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 584 | |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 585 | using INHERITED = GrFragmentProcessor::ProgramImpl; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 586 | }; |
| 587 | |
| 588 | ///////////////////////////////////////////////////////////////////// |
| 589 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 590 | class GrPerlinNoise2Effect : public GrFragmentProcessor { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 591 | public: |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 592 | static std::unique_ptr<GrFragmentProcessor> Make( |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 593 | SkPerlinNoiseShaderImpl::Type type, |
| 594 | int numOctaves, |
| 595 | bool stitchTiles, |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 596 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData, |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 597 | GrSurfaceProxyView permutationsView, |
| 598 | GrSurfaceProxyView noiseView, |
| 599 | const SkMatrix& matrix, |
| 600 | const GrCaps& caps) { |
| 601 | static constexpr GrSamplerState kRepeatXSampler = {GrSamplerState::WrapMode::kRepeat, |
| 602 | GrSamplerState::WrapMode::kClamp, |
| 603 | GrSamplerState::Filter::kNearest}; |
| 604 | auto permutationsFP = |
| 605 | GrTextureEffect::Make(std::move(permutationsView), kPremul_SkAlphaType, |
| 606 | SkMatrix::I(), kRepeatXSampler, caps); |
| 607 | auto noiseFP = GrTextureEffect::Make(std::move(noiseView), kPremul_SkAlphaType, |
| 608 | SkMatrix::I(), kRepeatXSampler, caps); |
| 609 | |
Michael Ludwig | f2935c6 | 2020-06-26 11:07:23 -0400 | [diff] [blame] | 610 | return GrMatrixEffect::Make(matrix, std::unique_ptr<GrFragmentProcessor>( |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 611 | new GrPerlinNoise2Effect(type, numOctaves, stitchTiles, std::move(paintingData), |
Michael Ludwig | f2935c6 | 2020-06-26 11:07:23 -0400 | [diff] [blame] | 612 | std::move(permutationsFP), std::move(noiseFP)))); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 613 | } |
| 614 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 615 | const char* name() const override { return "PerlinNoise"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 616 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 617 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 618 | return std::unique_ptr<GrFragmentProcessor>(new GrPerlinNoise2Effect(*this)); |
Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 619 | } |
| 620 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 621 | const SkPerlinNoiseShaderImpl::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 622 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 623 | SkPerlinNoiseShaderImpl::Type type() const { return fType; } |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 624 | bool stitchTiles() const { return fStitchTiles; } |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 625 | const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; } |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 626 | int numOctaves() const { return fNumOctaves; } |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 627 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 628 | private: |
Brian Salomon | 3176e86 | 2021-08-09 11:23:04 -0400 | [diff] [blame] | 629 | std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override { |
Brian Salomon | 18ab203 | 2021-02-23 10:07:05 -0500 | [diff] [blame] | 630 | return std::make_unique<GrGLPerlinNoise>(); |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 633 | void onAddToKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 634 | GrGLPerlinNoise::GenKey(*this, caps, b); |
| 635 | } |
| 636 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 637 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 638 | const GrPerlinNoise2Effect& s = sBase.cast<GrPerlinNoise2Effect>(); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 639 | return fType == s.fType && |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 640 | fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency && |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 641 | fNumOctaves == s.fNumOctaves && |
| 642 | fStitchTiles == s.fStitchTiles && |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 643 | fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 646 | GrPerlinNoise2Effect(SkPerlinNoiseShaderImpl::Type type, |
| 647 | int numOctaves, |
| 648 | bool stitchTiles, |
Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 649 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData, |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 650 | std::unique_ptr<GrFragmentProcessor> permutationsFP, |
Michael Ludwig | f2935c6 | 2020-06-26 11:07:23 -0400 | [diff] [blame] | 651 | std::unique_ptr<GrFragmentProcessor> noiseFP) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 652 | : INHERITED(kGrPerlinNoise2Effect_ClassID, kNone_OptimizationFlags) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 653 | , fType(type) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 654 | , fNumOctaves(numOctaves) |
| 655 | , fStitchTiles(stitchTiles) |
Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 656 | , fPaintingData(std::move(paintingData)) { |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 657 | this->registerChild(std::move(permutationsFP), SkSL::SampleUsage::Explicit()); |
| 658 | this->registerChild(std::move(noiseFP), SkSL::SampleUsage::Explicit()); |
Michael Ludwig | f2935c6 | 2020-06-26 11:07:23 -0400 | [diff] [blame] | 659 | this->setUsesSampleCoordsDirectly(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 662 | GrPerlinNoise2Effect(const GrPerlinNoise2Effect& that) |
John Stiles | 307f8f5 | 2021-08-09 15:36:59 -0400 | [diff] [blame^] | 663 | : INHERITED(that) |
Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 664 | , fType(that.fType) |
Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 665 | , fNumOctaves(that.fNumOctaves) |
| 666 | , fStitchTiles(that.fStitchTiles) |
John Stiles | 307f8f5 | 2021-08-09 15:36:59 -0400 | [diff] [blame^] | 667 | , fPaintingData(new SkPerlinNoiseShaderImpl::PaintingData(*that.fPaintingData)) {} |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 668 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 669 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 670 | |
Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 671 | SkPerlinNoiseShaderImpl::Type fType; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 672 | int fNumOctaves; |
| 673 | bool fStitchTiles; |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 674 | |
Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 675 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> fPaintingData; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 676 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 677 | using INHERITED = GrFragmentProcessor; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 678 | }; |
| 679 | |
| 680 | ///////////////////////////////////////////////////////////////////// |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 681 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoise2Effect); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 682 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 683 | #if GR_TEST_UTILS |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 684 | std::unique_ptr<GrFragmentProcessor> GrPerlinNoise2Effect::TestCreate(GrProcessorTestData* d) { |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 685 | int numOctaves = d->fRandom->nextRangeU(2, 10); |
| 686 | bool stitchTiles = d->fRandom->nextBool(); |
| 687 | SkScalar seed = SkIntToScalar(d->fRandom->nextU()); |
John Stiles | 391a9f6 | 2021-04-29 23:17:41 -0400 | [diff] [blame] | 688 | SkISize tileSize; |
| 689 | tileSize.fWidth = d->fRandom->nextRangeU(4, 4096); |
| 690 | tileSize.fHeight = d->fRandom->nextRangeU(4, 4096); |
| 691 | SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, 0.99f); |
| 692 | SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, 0.99f); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 693 | |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 694 | sk_sp<SkShader> shader(d->fRandom->nextBool() ? |
| 695 | SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 696 | stitchTiles ? &tileSize : nullptr) : |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 697 | SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 698 | stitchTiles ? &tileSize : nullptr)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 699 | |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 700 | GrTest::TestAsFPArgs asFPArgs(d); |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 701 | return as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 702 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 703 | #endif |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 704 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 705 | void GrGLPerlinNoise::emitCode(EmitArgs& args) { |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 706 | const GrPerlinNoise2Effect& pne = args.fFp.cast<GrPerlinNoise2Effect>(); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 707 | |
Brian Salomon | ffd15ea | 2020-07-01 16:48:20 -0400 | [diff] [blame] | 708 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 709 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 710 | |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 711 | fBaseFrequencyUni = uniformHandler->addUniform(&pne, kFragment_GrShaderFlag, kHalf2_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 712 | "baseFrequency"); |
| 713 | const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 714 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 715 | const char* stitchDataUni = nullptr; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 716 | if (pne.stitchTiles()) { |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 717 | fStitchDataUni = uniformHandler->addUniform(&pne, kFragment_GrShaderFlag, kHalf2_GrSLType, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 718 | "stitchData"); |
| 719 | stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 720 | } |
| 721 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 722 | // Add noise function |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 723 | const GrShaderVar gPerlinNoiseArgs[] = {{"chanCoord", kHalf_GrSLType }, |
| 724 | {"noiseVec ", kHalf2_GrSLType}}; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 725 | |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 726 | const GrShaderVar gPerlinNoiseStitchArgs[] = {{"chanCoord" , kHalf_GrSLType }, |
| 727 | {"noiseVec" , kHalf2_GrSLType}, |
| 728 | {"stitchData", kHalf2_GrSLType}}; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 729 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 730 | SkString noiseCode; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 731 | |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 732 | noiseCode.append( |
| 733 | R"(half4 floorVal; |
| 734 | floorVal.xy = floor(noiseVec); |
| 735 | floorVal.zw = floorVal.xy + half2(1); |
| 736 | half2 fractVal = fract(noiseVec); |
| 737 | // smooth curve : t^2*(3 - 2*t) |
| 738 | half2 noiseSmooth = fractVal*fractVal*(half2(3) - 2*fractVal);)"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 739 | |
| 740 | // Adjust frequencies if we're stitching tiles |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 741 | if (pne.stitchTiles()) { |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 742 | noiseCode.append( |
| 743 | R"(if (floorVal.x >= stitchData.x) { floorVal.x -= stitchData.x; }; |
| 744 | if (floorVal.y >= stitchData.y) { floorVal.y -= stitchData.y; }; |
| 745 | if (floorVal.z >= stitchData.x) { floorVal.z -= stitchData.x; }; |
| 746 | if (floorVal.w >= stitchData.y) { floorVal.w -= stitchData.y; };)"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Brian Osman | 6b5dbb4 | 2020-07-15 15:31:05 -0400 | [diff] [blame] | 749 | // NOTE: We need to explicitly pass half4(1) as input color here, because the helper function |
| 750 | // can't see fInputColor (which is "_input" in the FP's outer function). skbug.com/10506 |
| 751 | SkString sampleX = this->invokeChild(0, "half4(1)", args, "half2(floorVal.x, 0.5)"); |
| 752 | SkString sampleY = this->invokeChild(0, "half4(1)", args, "half2(floorVal.z, 0.5)"); |
Brian Salomon | b43d699 | 2021-01-05 14:37:40 -0500 | [diff] [blame] | 753 | noiseCode.appendf("half2 latticeIdx = half2(%s.a, %s.a);", sampleX.c_str(), sampleY.c_str()); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 754 | |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 755 | #if defined(SK_BUILD_FOR_ANDROID) |
| 756 | // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3). |
| 757 | // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit |
| 758 | // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725 |
| 759 | // (or 0.484368 here). The following rounding operation prevents these precision issues from |
| 760 | // affecting the result of the noise by making sure that we only have multiples of 1/255. |
| 761 | // (Note that 1/255 is about 0.003921569, which is the value used here). |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 762 | noiseCode.append( |
| 763 | "latticeIdx = floor(latticeIdx * half2(255.0) + half2(0.5)) * half2(0.003921569);"); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 764 | #endif |
| 765 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 766 | // Get (x,y) coordinates with the permutated x |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 767 | noiseCode.append("half4 bcoords = 256*latticeIdx.xyxy + floorVal.yyww;"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 768 | |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 769 | noiseCode.append("half2 uv;"); |
| 770 | |
| 771 | // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a |
| 772 | // [-1,1] vector and perform a dot product between that vector and the provided vector. |
| 773 | // Save it as a string because we will repeat it 4x. |
| 774 | static constexpr const char* inc8bit = "0.00390625"; // 1.0 / 256.0 |
| 775 | SkString dotLattice = |
| 776 | SkStringPrintf("dot((lattice.ga + lattice.rb*%s)*2 - half2(1), fractVal)", inc8bit); |
| 777 | |
Brian Osman | 6b5dbb4 | 2020-07-15 15:31:05 -0400 | [diff] [blame] | 778 | SkString sampleA = this->invokeChild(1, "half4(1)", args, "half2(bcoords.x, chanCoord)"); |
| 779 | SkString sampleB = this->invokeChild(1, "half4(1)", args, "half2(bcoords.y, chanCoord)"); |
| 780 | SkString sampleC = this->invokeChild(1, "half4(1)", args, "half2(bcoords.w, chanCoord)"); |
| 781 | SkString sampleD = this->invokeChild(1, "half4(1)", args, "half2(bcoords.z, chanCoord)"); |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 782 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 783 | // Compute u, at offset (0,0) |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 784 | noiseCode.appendf("half4 lattice = %s;", sampleA.c_str()); |
| 785 | noiseCode.appendf("uv.x = %s;", dotLattice.c_str()); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 786 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 787 | // Compute v, at offset (-1,0) |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 788 | noiseCode.append("fractVal.x -= 1.0;"); |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 789 | noiseCode.appendf("lattice = %s;", sampleB.c_str()); |
| 790 | noiseCode.appendf("uv.y = %s;", dotLattice.c_str()); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 791 | |
| 792 | // Compute 'a' as a linear interpolation of 'u' and 'v' |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 793 | noiseCode.append("half2 ab;"); |
| 794 | noiseCode.append("ab.x = mix(uv.x, uv.y, noiseSmooth.x);"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 795 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 796 | // Compute v, at offset (-1,-1) |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 797 | noiseCode.append("fractVal.y -= 1.0;"); |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 798 | noiseCode.appendf("lattice = %s;", sampleC.c_str()); |
| 799 | noiseCode.appendf("uv.y = %s;", dotLattice.c_str()); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 800 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 801 | // Compute u, at offset (0,-1) |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 802 | noiseCode.append("fractVal.x += 1.0;"); |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 803 | noiseCode.appendf("lattice = %s;", sampleD.c_str()); |
| 804 | noiseCode.appendf("uv.x = %s;", dotLattice.c_str()); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 805 | |
| 806 | // Compute 'b' as a linear interpolation of 'u' and 'v' |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 807 | noiseCode.append("ab.y = mix(uv.x, uv.y, noiseSmooth.x);"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 808 | // Compute the noise as a linear interpolation of 'a' and 'b' |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 809 | noiseCode.append("return mix(ab.x, ab.y, noiseSmooth.y);"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 810 | |
John Stiles | 6b58a33 | 2020-10-26 17:53:06 -0400 | [diff] [blame] | 811 | SkString noiseFuncName = fragBuilder->getMangledFunctionName("noiseFuncName"); |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 812 | if (pne.stitchTiles()) { |
John Stiles | 6b58a33 | 2020-10-26 17:53:06 -0400 | [diff] [blame] | 813 | fragBuilder->emitFunction(kHalf_GrSLType, noiseFuncName.c_str(), |
John Stiles | fdf6148 | 2020-10-27 09:45:40 -0400 | [diff] [blame] | 814 | {gPerlinNoiseStitchArgs, SK_ARRAY_COUNT(gPerlinNoiseStitchArgs)}, |
John Stiles | 6b58a33 | 2020-10-26 17:53:06 -0400 | [diff] [blame] | 815 | noiseCode.c_str()); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 816 | } else { |
John Stiles | 6b58a33 | 2020-10-26 17:53:06 -0400 | [diff] [blame] | 817 | fragBuilder->emitFunction(kHalf_GrSLType, noiseFuncName.c_str(), |
John Stiles | fdf6148 | 2020-10-27 09:45:40 -0400 | [diff] [blame] | 818 | {gPerlinNoiseArgs, SK_ARRAY_COUNT(gPerlinNoiseArgs)}, |
John Stiles | 6b58a33 | 2020-10-26 17:53:06 -0400 | [diff] [blame] | 819 | noiseCode.c_str()); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 820 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 821 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 822 | // There are rounding errors if the floor operation is not performed here |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 823 | fragBuilder->codeAppendf("half2 noiseVec = half2(floor(%s.xy) * %s);", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 824 | args.fSampleCoord, baseFrequencyUni); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 825 | |
| 826 | // Clear the color accumulator |
John Stiles | d1eab8b | 2020-12-15 09:47:26 -0500 | [diff] [blame] | 827 | fragBuilder->codeAppendf("half4 color = half4(0);"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 828 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 829 | if (pne.stitchTiles()) { |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 830 | // Set up TurbulenceInitial stitch values. |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 831 | fragBuilder->codeAppendf("half2 stitchData = %s;", stitchDataUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 832 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 833 | |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 834 | fragBuilder->codeAppendf("half ratio = 1.0;"); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 835 | |
| 836 | // Loop over all octaves |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 837 | fragBuilder->codeAppendf("for (int octave = 0; octave < %d; ++octave) {", pne.numOctaves()); |
John Stiles | d1eab8b | 2020-12-15 09:47:26 -0500 | [diff] [blame] | 838 | fragBuilder->codeAppendf(" color += "); |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 839 | if (pne.type() != SkPerlinNoiseShaderImpl::kFractalNoise_Type) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 840 | fragBuilder->codeAppend("abs("); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 841 | } |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 842 | |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 843 | // There are 4 lines, put y coords at center of each. |
| 844 | static constexpr const char* chanCoordR = "0.5"; |
| 845 | static constexpr const char* chanCoordG = "1.5"; |
| 846 | static constexpr const char* chanCoordB = "2.5"; |
| 847 | static constexpr const char* chanCoordA = "3.5"; |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 848 | if (pne.stitchTiles()) { |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 849 | fragBuilder->codeAppendf(R"( |
John Stiles | 7cbb09c2 | 2021-01-07 16:07:00 -0500 | [diff] [blame] | 850 | half4(%s(%s, noiseVec, stitchData), %s(%s, noiseVec, stitchData), |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 851 | %s(%s, noiseVec, stitchData), %s(%s, noiseVec, stitchData)))", |
| 852 | noiseFuncName.c_str(), chanCoordR, |
| 853 | noiseFuncName.c_str(), chanCoordG, |
| 854 | noiseFuncName.c_str(), chanCoordB, |
| 855 | noiseFuncName.c_str(), chanCoordA); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 856 | } else { |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 857 | fragBuilder->codeAppendf(R"( |
| 858 | half4(%s(%s, noiseVec), %s(%s, noiseVec), |
| 859 | %s(%s, noiseVec), %s(%s, noiseVec)))", |
| 860 | noiseFuncName.c_str(), chanCoordR, |
| 861 | noiseFuncName.c_str(), chanCoordG, |
| 862 | noiseFuncName.c_str(), chanCoordB, |
| 863 | noiseFuncName.c_str(), chanCoordA); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 864 | } |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 865 | if (pne.type() != SkPerlinNoiseShaderImpl::kFractalNoise_Type) { |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 866 | fragBuilder->codeAppend(")"); // end of "abs(" |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 867 | } |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 868 | fragBuilder->codeAppend(" * ratio;"); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 869 | |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 870 | fragBuilder->codeAppend(R"(noiseVec *= half2(2.0); |
| 871 | ratio *= 0.5;)"); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 872 | |
robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 873 | if (pne.stitchTiles()) { |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 874 | fragBuilder->codeAppend("stitchData *= half2(2.0);"); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 875 | } |
Brian Salomon | e338c29 | 2020-06-16 16:52:17 -0400 | [diff] [blame] | 876 | fragBuilder->codeAppend("}"); // end of the for loop on octaves |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 877 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 878 | if (pne.type() == SkPerlinNoiseShaderImpl::kFractalNoise_Type) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 879 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 880 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
John Stiles | d1eab8b | 2020-12-15 09:47:26 -0500 | [diff] [blame] | 881 | fragBuilder->codeAppendf("color = color * half4(0.5) + half4(0.5);"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 882 | } |
| 883 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 884 | // Clamp values |
John Stiles | d1eab8b | 2020-12-15 09:47:26 -0500 | [diff] [blame] | 885 | fragBuilder->codeAppendf("color = saturate(color);"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 886 | |
| 887 | // Pre-multiply the result |
John Stiles | d1eab8b | 2020-12-15 09:47:26 -0500 | [diff] [blame] | 888 | fragBuilder->codeAppendf("return half4(color.rgb * color.aaa, color.a);"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 891 | void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 892 | GrProcessorKeyBuilder* b) { |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 893 | const GrPerlinNoise2Effect& turbulence = processor.cast<GrPerlinNoise2Effect>(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 894 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 895 | uint32_t key = turbulence.numOctaves(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 896 | |
| 897 | key = key << 3; // Make room for next 3 bits |
| 898 | |
| 899 | switch (turbulence.type()) { |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 900 | case SkPerlinNoiseShaderImpl::kFractalNoise_Type: |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 901 | key |= 0x1; |
| 902 | break; |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 903 | case SkPerlinNoiseShaderImpl::kTurbulence_Type: |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 904 | key |= 0x2; |
| 905 | break; |
| 906 | default: |
| 907 | // leave key at 0 |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | if (turbulence.stitchTiles()) { |
| 912 | key |= 0x4; // Flip the 3rd bit if tile stitching is on |
| 913 | } |
| 914 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 915 | b->add32(key); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 916 | } |
| 917 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 918 | void GrGLPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 919 | const GrFragmentProcessor& processor) { |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 920 | INHERITED::onSetData(pdman, processor); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 921 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 922 | const GrPerlinNoise2Effect& turbulence = processor.cast<GrPerlinNoise2Effect>(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 923 | |
| 924 | const SkVector& baseFrequency = turbulence.baseFrequency(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 925 | pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 926 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 927 | if (turbulence.stitchTiles()) { |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 928 | const SkPerlinNoiseShaderImpl::StitchData& stitchData = turbulence.stitchData(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 929 | pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
Brian Salomon | 81454df | 2020-06-17 10:59:28 -0400 | [diff] [blame] | 930 | SkIntToScalar(stitchData.fHeight)); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 934 | ///////////////////////////////////////////////////////////////////// |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 935 | |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 936 | std::unique_ptr<GrFragmentProcessor> SkPerlinNoiseShaderImpl::asFragmentProcessor( |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 937 | const GrFPArgs& args) const { |
brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 938 | SkASSERT(args.fContext); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 939 | |
Florin Malita | 52f0291 | 2020-03-09 16:33:17 -0400 | [diff] [blame] | 940 | const auto localMatrix = this->totalLocalMatrix(args.fPreLocalMatrix); |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 941 | const auto paintMatrix = SkMatrix::Concat(args.fMatrixProvider.localToDevice(), *localMatrix); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 942 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 943 | // Either we don't stitch tiles, either we have a valid tile size |
| 944 | SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
| 945 | |
Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 946 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData = |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 947 | std::make_unique<SkPerlinNoiseShaderImpl::PaintingData>(fTileSize, |
Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 948 | fSeed, |
| 949 | fBaseFrequencyX, |
| 950 | fBaseFrequencyY, |
Ethan Nicholas | 8294015 | 2019-01-10 13:58:14 -0500 | [diff] [blame] | 951 | paintMatrix); |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 952 | |
Brian Osman | 449b115 | 2020-04-15 16:43:00 -0400 | [diff] [blame] | 953 | SkMatrix m = args.fMatrixProvider.localToDevice(); |
Florin Malita | c6c5ead | 2018-04-11 15:33:40 -0400 | [diff] [blame] | 954 | m.setTranslateX(-localMatrix->getTranslateX() + SK_Scalar1); |
| 955 | m.setTranslateY(-localMatrix->getTranslateY() + SK_Scalar1); |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 956 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 957 | auto context = args.fContext; |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 958 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 959 | if (0 == fNumOctaves) { |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 960 | if (kFractalNoise_Type == fType) { |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 961 | // 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] | 962 | // TODO: Either treat the output of this shader as sRGB or allow client to specify a |
| 963 | // color space of the noise. Either way, this case (and the GLSL) need to convert to |
| 964 | // the destination. |
John Stiles | 8589430 | 2020-07-13 11:39:52 -0400 | [diff] [blame] | 965 | auto inner = GrFragmentProcessor::ModulateRGBA( |
| 966 | /*child=*/nullptr, SkPMColor4f::FromBytes_RGBA(0x80404040)); |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 967 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 968 | } |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 969 | // Emit zero. |
Brian Salomon | 354147a | 2021-04-14 11:15:05 -0400 | [diff] [blame] | 970 | return GrFragmentProcessor::MakeColor(SK_PMColor4fTRANSPARENT); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 973 | const SkBitmap& permutationsBitmap = paintingData->getPermutationsBitmap(); |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 974 | const SkBitmap& noiseBitmap = paintingData->getNoiseBitmap(); |
Greg Daniel | 7e1912a | 2018-02-08 09:15:33 -0500 | [diff] [blame] | 975 | |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 976 | auto permutationsView = std::get<0>(GrMakeCachedBitmapProxyView(context, permutationsBitmap)); |
| 977 | auto noiseView = std::get<0>(GrMakeCachedBitmapProxyView(context, noiseBitmap)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 978 | |
Brian Salomon | 27c4202 | 2021-04-28 12:39:21 -0400 | [diff] [blame] | 979 | if (permutationsView && noiseView) { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 980 | auto inner = GrPerlinNoise2Effect::Make(fType, |
| 981 | fNumOctaves, |
| 982 | fStitchTiles, |
| 983 | std::move(paintingData), |
Greg Daniel | c52db71 | 2020-01-28 17:03:46 -0500 | [diff] [blame] | 984 | std::move(permutationsView), |
| 985 | std::move(noiseView), |
Brian Salomon | 83c2d35 | 2020-06-17 11:46:21 -0400 | [diff] [blame] | 986 | m, |
| 987 | *context->priv().caps()); |
Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 988 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 989 | } |
bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 990 | return nullptr; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | #endif |
| 994 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 995 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 996 | |
Mike Reed | 832aa11 | 2018-05-18 11:48:50 -0400 | [diff] [blame] | 997 | static bool valid_input(SkScalar baseX, SkScalar baseY, int numOctaves, const SkISize* tileSize, |
| 998 | SkScalar seed) { |
| 999 | if (!(baseX >= 0 && baseY >= 0)) { |
| 1000 | return false; |
| 1001 | } |
| 1002 | if (!(numOctaves >= 0 && numOctaves <= SkPerlinNoiseShaderImpl::kMaxOctaves)) { |
| 1003 | return false; |
| 1004 | } |
| 1005 | if (tileSize && !(tileSize->width() >= 0 && tileSize->height() >= 0)) { |
| 1006 | return false; |
| 1007 | } |
| 1008 | if (!SkScalarIsFinite(seed)) { |
| 1009 | return false; |
| 1010 | } |
| 1011 | return true; |
| 1012 | } |
| 1013 | |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1014 | sk_sp<SkShader> SkPerlinNoiseShader::MakeFractalNoise(SkScalar baseFrequencyX, |
| 1015 | SkScalar baseFrequencyY, |
| 1016 | int numOctaves, SkScalar seed, |
| 1017 | const SkISize* tileSize) { |
Mike Reed | 832aa11 | 2018-05-18 11:48:50 -0400 | [diff] [blame] | 1018 | if (!valid_input(baseFrequencyX, baseFrequencyY, numOctaves, tileSize, seed)) { |
| 1019 | return nullptr; |
| 1020 | } |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1021 | return sk_sp<SkShader>(new SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::kFractalNoise_Type, |
| 1022 | baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| 1023 | tileSize)); |
| 1024 | } |
| 1025 | |
| 1026 | sk_sp<SkShader> SkPerlinNoiseShader::MakeTurbulence(SkScalar baseFrequencyX, |
| 1027 | SkScalar baseFrequencyY, |
| 1028 | int numOctaves, SkScalar seed, |
| 1029 | const SkISize* tileSize) { |
Mike Reed | 832aa11 | 2018-05-18 11:48:50 -0400 | [diff] [blame] | 1030 | if (!valid_input(baseFrequencyX, baseFrequencyY, numOctaves, tileSize, seed)) { |
| 1031 | return nullptr; |
| 1032 | } |
Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1033 | return sk_sp<SkShader>(new SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::kTurbulence_Type, |
| 1034 | baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| 1035 | tileSize)); |
| 1036 | } |
| 1037 | |
Mike Klein | fa5f6ce | 2018-10-20 08:21:31 -0400 | [diff] [blame] | 1038 | void SkPerlinNoiseShader::RegisterFlattenables() { |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 1039 | SK_REGISTER_FLATTENABLE(SkPerlinNoiseShaderImpl); |
Mike Klein | 1295672 | 2018-10-19 10:00:21 -0400 | [diff] [blame] | 1040 | } |