| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 8 | #include "SkPerlinNoiseShader.h" |
| Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 9 | |
| 10 | #include "SkArenaAlloc.h" |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 11 | #include "SkDither.h" |
| commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 12 | #include "SkColorFilter.h" |
| Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 13 | #include "SkMakeUnique.h" |
| commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 14 | #include "SkReadBuffer.h" |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 15 | #include "SkWriteBuffer.h" |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 16 | #include "SkShader.h" |
| 17 | #include "SkUnPreMultiply.h" |
| 18 | #include "SkString.h" |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 19 | |
| 20 | #if SK_SUPPORT_GPU |
| 21 | #include "GrContext.h" |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 22 | #include "GrContextPriv.h" |
| bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 23 | #include "GrCoordTransform.h" |
| joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 24 | #include "SkGr.h" |
| bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 25 | #include "effects/GrConstColorProcessor.h" |
| egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 26 | #include "glsl/GrGLSLFragmentProcessor.h" |
| egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 27 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 28 | #include "glsl/GrGLSLProgramDataManager.h" |
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 29 | #include "glsl/GrGLSLUniformHandler.h" |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | static const int kBlockSize = 256; |
| 33 | static const int kBlockMask = kBlockSize - 1; |
| 34 | static const int kPerlinNoise = 4096; |
| 35 | static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
| 36 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 37 | static uint8_t improved_noise_permutations[] = { |
| 38 | 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, |
| 39 | 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, |
| 40 | 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, |
| 41 | 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, |
| 42 | 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, |
| 43 | 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, |
| 44 | 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, |
| 45 | 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, |
| 46 | 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, |
| 47 | 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, |
| 48 | 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, |
| 49 | 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, |
| 50 | 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, |
| 51 | 141, 128, 195, 78, 66, 215, 61, 156, 180, |
| 52 | 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, |
| 53 | 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, |
| 54 | 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, |
| 55 | 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, |
| 56 | 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, |
| 57 | 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, |
| 58 | 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, |
| 59 | 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, |
| 60 | 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, |
| 61 | 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, |
| 62 | 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, |
| 63 | 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, |
| 64 | 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, |
| 65 | 141, 128, 195, 78, 66, 215, 61, 156, 180 |
| 66 | }; |
| 67 | |
| 68 | class SkPerlinNoiseShaderImpl : public SkShaderBase { |
| 69 | public: |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 70 | struct StitchData { |
| 71 | StitchData() |
| 72 | : fWidth(0) |
| 73 | , fWrapX(0) |
| 74 | , fHeight(0) |
| 75 | , fWrapY(0) |
| 76 | {} |
| 77 | |
| 78 | bool operator==(const StitchData& other) const { |
| 79 | return fWidth == other.fWidth && |
| 80 | fWrapX == other.fWrapX && |
| 81 | fHeight == other.fHeight && |
| 82 | fWrapY == other.fWrapY; |
| 83 | } |
| 84 | |
| 85 | int fWidth; // How much to subtract to wrap for stitching. |
| 86 | int fWrapX; // Minimum value to wrap. |
| 87 | int fHeight; |
| 88 | int fWrapY; |
| 89 | }; |
| 90 | |
| 91 | struct PaintingData { |
| 92 | PaintingData(const SkISize& tileSize, SkScalar seed, |
| 93 | SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 94 | const SkMatrix& matrix) |
| 95 | { |
| 96 | SkVector vec[2] = { |
| 97 | { SkScalarInvert(baseFrequencyX), SkScalarInvert(baseFrequencyY) }, |
| 98 | { SkIntToScalar(tileSize.fWidth), SkIntToScalar(tileSize.fHeight) }, |
| 99 | }; |
| 100 | matrix.mapVectors(vec, 2); |
| 101 | |
| 102 | fBaseFrequency.set(SkScalarInvert(vec[0].fX), SkScalarInvert(vec[0].fY)); |
| 103 | fTileSize.set(SkScalarRoundToInt(vec[1].fX), SkScalarRoundToInt(vec[1].fY)); |
| 104 | this->init(seed); |
| 105 | if (!fTileSize.isEmpty()) { |
| 106 | this->stitch(); |
| 107 | } |
| 108 | |
| 109 | #if SK_SUPPORT_GPU |
| 110 | fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1)); |
| 111 | fPermutationsBitmap.setPixels(fLatticeSelector); |
| 112 | |
| 113 | fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4)); |
| 114 | fNoiseBitmap.setPixels(fNoise[0][0]); |
| 115 | |
| 116 | fImprovedPermutationsBitmap.setInfo(SkImageInfo::MakeA8(256, 1)); |
| 117 | fImprovedPermutationsBitmap.setPixels(improved_noise_permutations); |
| 118 | |
| 119 | fGradientBitmap.setInfo(SkImageInfo::MakeN32Premul(16, 1)); |
| 120 | static uint8_t gradients[] = { 2, 2, 1, 0, |
| 121 | 0, 2, 1, 0, |
| 122 | 2, 0, 1, 0, |
| 123 | 0, 0, 1, 0, |
| 124 | 2, 1, 2, 0, |
| 125 | 0, 1, 2, 0, |
| 126 | 2, 1, 0, 0, |
| 127 | 0, 1, 0, 0, |
| 128 | 1, 2, 2, 0, |
| 129 | 1, 0, 2, 0, |
| 130 | 1, 2, 0, 0, |
| 131 | 1, 0, 0, 0, |
| 132 | 2, 2, 1, 0, |
| 133 | 1, 0, 2, 0, |
| 134 | 0, 2, 1, 0, |
| 135 | 1, 0, 0, 0 }; |
| 136 | fGradientBitmap.setPixels(gradients); |
| 137 | #endif |
| 138 | } |
| 139 | |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 140 | #if SK_SUPPORT_GPU |
| 141 | PaintingData(const PaintingData& that) |
| 142 | : fSeed(that.fSeed) |
| 143 | , fTileSize(that.fTileSize) |
| 144 | , fBaseFrequency(that.fBaseFrequency) |
| 145 | , fStitchDataInit(that.fStitchDataInit) |
| 146 | , fPermutationsBitmap(that.fPermutationsBitmap) |
| 147 | , fNoiseBitmap(that.fNoiseBitmap) |
| 148 | , fImprovedPermutationsBitmap(that.fImprovedPermutationsBitmap) |
| 149 | , fGradientBitmap(that.fGradientBitmap) { |
| 150 | memcpy(fLatticeSelector, that.fLatticeSelector, sizeof(fLatticeSelector)); |
| 151 | memcpy(fNoise, that.fNoise, sizeof(fNoise)); |
| 152 | memcpy(fGradient, that.fGradient, sizeof(fGradient)); |
| 153 | } |
| 154 | #endif |
| 155 | |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 156 | int fSeed; |
| 157 | uint8_t fLatticeSelector[kBlockSize]; |
| 158 | uint16_t fNoise[4][kBlockSize][2]; |
| 159 | SkPoint fGradient[4][kBlockSize]; |
| 160 | SkISize fTileSize; |
| 161 | SkVector fBaseFrequency; |
| 162 | StitchData fStitchDataInit; |
| 163 | |
| 164 | private: |
| 165 | |
| 166 | #if SK_SUPPORT_GPU |
| 167 | SkBitmap fPermutationsBitmap; |
| 168 | SkBitmap fNoiseBitmap; |
| 169 | SkBitmap fImprovedPermutationsBitmap; |
| 170 | SkBitmap fGradientBitmap; |
| 171 | #endif |
| 172 | |
| 173 | inline int random() { |
| 174 | static const int gRandAmplitude = 16807; // 7**5; primitive root of m |
| 175 | static const int gRandQ = 127773; // m / a |
| 176 | static const int gRandR = 2836; // m % a |
| 177 | |
| 178 | int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ); |
| 179 | if (result <= 0) |
| 180 | result += kRandMaximum; |
| 181 | fSeed = result; |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | // Only called once. Could be part of the constructor. |
| 186 | void init(SkScalar seed) |
| 187 | { |
| 188 | static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize)); |
| 189 | |
| 190 | // According to the SVG spec, we must truncate (not round) the seed value. |
| 191 | fSeed = SkScalarTruncToInt(seed); |
| 192 | // The seed value clamp to the range [1, kRandMaximum - 1]. |
| 193 | if (fSeed <= 0) { |
| 194 | fSeed = -(fSeed % (kRandMaximum - 1)) + 1; |
| 195 | } |
| 196 | if (fSeed > kRandMaximum - 1) { |
| 197 | fSeed = kRandMaximum - 1; |
| 198 | } |
| 199 | for (int channel = 0; channel < 4; ++channel) { |
| 200 | for (int i = 0; i < kBlockSize; ++i) { |
| 201 | fLatticeSelector[i] = i; |
| 202 | fNoise[channel][i][0] = (random() % (2 * kBlockSize)); |
| 203 | fNoise[channel][i][1] = (random() % (2 * kBlockSize)); |
| 204 | } |
| 205 | } |
| 206 | for (int i = kBlockSize - 1; i > 0; --i) { |
| 207 | int k = fLatticeSelector[i]; |
| 208 | int j = random() % kBlockSize; |
| 209 | SkASSERT(j >= 0); |
| 210 | SkASSERT(j < kBlockSize); |
| 211 | fLatticeSelector[i] = fLatticeSelector[j]; |
| 212 | fLatticeSelector[j] = k; |
| 213 | } |
| 214 | |
| 215 | // Perform the permutations now |
| 216 | { |
| 217 | // Copy noise data |
| 218 | uint16_t noise[4][kBlockSize][2]; |
| 219 | for (int i = 0; i < kBlockSize; ++i) { |
| 220 | for (int channel = 0; channel < 4; ++channel) { |
| 221 | for (int j = 0; j < 2; ++j) { |
| 222 | noise[channel][i][j] = fNoise[channel][i][j]; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | // Do permutations on noise data |
| 227 | for (int i = 0; i < kBlockSize; ++i) { |
| 228 | for (int channel = 0; channel < 4; ++channel) { |
| 229 | for (int j = 0; j < 2; ++j) { |
| 230 | fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j]; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Half of the largest possible value for 16 bit unsigned int |
| 237 | static const SkScalar gHalfMax16bits = 32767.5f; |
| 238 | |
| 239 | // Compute gradients from permutated noise data |
| 240 | for (int channel = 0; channel < 4; ++channel) { |
| 241 | for (int i = 0; i < kBlockSize; ++i) { |
| 242 | fGradient[channel][i] = SkPoint::Make( |
| 243 | (fNoise[channel][i][0] - kBlockSize) * gInvBlockSizef, |
| 244 | (fNoise[channel][i][1] - kBlockSize) * gInvBlockSizef); |
| 245 | fGradient[channel][i].normalize(); |
| 246 | // Put the normalized gradient back into the noise data |
| 247 | fNoise[channel][i][0] = SkScalarRoundToInt( |
| 248 | (fGradient[channel][i].fX + 1) * gHalfMax16bits); |
| 249 | fNoise[channel][i][1] = SkScalarRoundToInt( |
| 250 | (fGradient[channel][i].fY + 1) * gHalfMax16bits); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Only called once. Could be part of the constructor. |
| 256 | void stitch() { |
| 257 | SkScalar tileWidth = SkIntToScalar(fTileSize.width()); |
| 258 | SkScalar tileHeight = SkIntToScalar(fTileSize.height()); |
| 259 | SkASSERT(tileWidth > 0 && tileHeight > 0); |
| 260 | // When stitching tiled turbulence, the frequencies must be adjusted |
| 261 | // so that the tile borders will be continuous. |
| 262 | if (fBaseFrequency.fX) { |
| 263 | SkScalar lowFrequencx = |
| 264 | SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
| 265 | SkScalar highFrequencx = |
| 266 | SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
| 267 | // BaseFrequency should be non-negative according to the standard. |
| 268 | if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequency.fX) { |
| 269 | fBaseFrequency.fX = lowFrequencx; |
| 270 | } else { |
| 271 | fBaseFrequency.fX = highFrequencx; |
| 272 | } |
| 273 | } |
| 274 | if (fBaseFrequency.fY) { |
| 275 | SkScalar lowFrequency = |
| 276 | SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
| 277 | SkScalar highFrequency = |
| 278 | SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
| 279 | if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequency.fY) { |
| 280 | fBaseFrequency.fY = lowFrequency; |
| 281 | } else { |
| 282 | fBaseFrequency.fY = highFrequency; |
| 283 | } |
| 284 | } |
| 285 | // Set up TurbulenceInitial stitch values. |
| 286 | fStitchDataInit.fWidth = |
| 287 | SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); |
| 288 | fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; |
| 289 | fStitchDataInit.fHeight = |
| 290 | SkScalarRoundToInt(tileHeight * fBaseFrequency.fY); |
| 291 | fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight; |
| 292 | } |
| 293 | |
| 294 | public: |
| 295 | |
| 296 | #if SK_SUPPORT_GPU |
| 297 | const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; } |
| 298 | |
| 299 | const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; } |
| 300 | |
| 301 | const SkBitmap& getImprovedPermutationsBitmap() const { return fImprovedPermutationsBitmap; } |
| 302 | |
| 303 | const SkBitmap& getGradientBitmap() const { return fGradientBitmap; } |
| 304 | #endif |
| 305 | }; |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 306 | |
| 307 | /** |
| 308 | * About the noise types : the difference between the first 2 is just minor tweaks to the |
| 309 | * algorithm, they're not 2 entirely different noises. The output looks different, but once the |
| 310 | * noise is generated in the [1, -1] range, the output is brought back in the [0, 1] range by |
| 311 | * doing : |
| 312 | * kFractalNoise_Type : noise * 0.5 + 0.5 |
| 313 | * kTurbulence_Type : abs(noise) |
| 314 | * Very little differences between the 2 types, although you can tell the difference visually. |
| 315 | * "Improved" is based on the Improved Perlin Noise algorithm described at |
| 316 | * http://mrl.nyu.edu/~perlin/noise/. It is quite distinct from the other two, and the noise is |
| 317 | * a 2D slice of a 3D noise texture. Minor changes to the Z coordinate will result in minor |
| 318 | * changes to the noise, making it suitable for animated noise. |
| 319 | */ |
| 320 | enum Type { |
| 321 | kFractalNoise_Type, |
| 322 | kTurbulence_Type, |
| 323 | kImprovedNoise_Type, |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 324 | kLast_Type = kImprovedNoise_Type |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 325 | }; |
| 326 | |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 327 | static const int kMaxOctaves = 255; // numOctaves must be <= 0 and <= kMaxOctaves |
| 328 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 329 | SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::Type type, SkScalar baseFrequencyX, |
| 330 | SkScalar baseFrequencyY, int numOctaves, SkScalar seed, |
| 331 | const SkISize* tileSize); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 332 | |
| 333 | class PerlinNoiseShaderContext : public Context { |
| 334 | public: |
| 335 | PerlinNoiseShaderContext(const SkPerlinNoiseShaderImpl& shader, const ContextRec&); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 336 | |
| 337 | void shadeSpan(int x, int y, SkPMColor[], int count) override; |
| 338 | |
| 339 | private: |
| 340 | SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; |
| 341 | SkScalar calculateTurbulenceValueForPoint( |
| 342 | int channel, |
| 343 | StitchData& stitchData, const SkPoint& point) const; |
| 344 | SkScalar calculateImprovedNoiseValueForPoint(int channel, const SkPoint& point) const; |
| 345 | SkScalar noise2D(int channel, |
| 346 | const StitchData& stitchData, const SkPoint& noiseVector) const; |
| 347 | |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 348 | SkMatrix fMatrix; |
| 349 | PaintingData fPaintingData; |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 350 | |
| 351 | typedef Context INHERITED; |
| 352 | }; |
| 353 | |
| 354 | #if SK_SUPPORT_GPU |
| Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 355 | std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 356 | #endif |
| 357 | |
| 358 | SK_TO_STRING_OVERRIDE() |
| 359 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShaderImpl) |
| 360 | |
| 361 | protected: |
| 362 | void flatten(SkWriteBuffer&) const override; |
| 363 | Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override; |
| 364 | |
| 365 | private: |
| 366 | const SkPerlinNoiseShaderImpl::Type fType; |
| 367 | const SkScalar fBaseFrequencyX; |
| 368 | const SkScalar fBaseFrequencyY; |
| 369 | const int fNumOctaves; |
| 370 | const SkScalar fSeed; |
| 371 | const SkISize fTileSize; |
| 372 | const bool fStitchTiles; |
| 373 | |
| 374 | friend class ::SkPerlinNoiseShader; |
| 375 | |
| 376 | typedef SkShaderBase INHERITED; |
| 377 | }; |
| 378 | |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 379 | namespace { |
| 380 | |
| 381 | // noiseValue is the color component's value (or color) |
| 382 | // limitValue is the maximum perlin noise array index value allowed |
| 383 | // newValue is the current noise dimension (either width or height) |
| 384 | inline int checkNoise(int noiseValue, int limitValue, int newValue) { |
| 385 | // If the noise value would bring us out of bounds of the current noise array while we are |
| 386 | // stiching noise tiles together, wrap the noise around the current dimension of the noise to |
| 387 | // stay within the array bounds in a continuous fashion (so that tiling lines are not visible) |
| 388 | if (noiseValue >= limitValue) { |
| 389 | noiseValue -= newValue; |
| 390 | } |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 391 | return noiseValue; |
| 392 | } |
| 393 | |
| 394 | inline SkScalar smoothCurve(SkScalar t) { |
| Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 395 | return t * t * (3 - 2 * t); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | } // end namespace |
| 399 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 400 | SkPerlinNoiseShaderImpl::SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::Type type, |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 401 | SkScalar baseFrequencyX, |
| 402 | SkScalar baseFrequencyY, |
| 403 | int numOctaves, |
| 404 | SkScalar seed, |
| 405 | const SkISize* tileSize) |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 406 | : fType(type) |
| 407 | , fBaseFrequencyX(baseFrequencyX) |
| 408 | , fBaseFrequencyY(baseFrequencyY) |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 409 | , fNumOctaves(numOctaves > kMaxOctaves ? kMaxOctaves : numOctaves/*[0,255] octaves allowed*/) |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 410 | , fSeed(seed) |
| halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 411 | , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize) |
| commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 412 | , fStitchTiles(!fTileSize.isEmpty()) |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 413 | { |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 414 | SkASSERT(numOctaves >= 0 && numOctaves <= kMaxOctaves); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 415 | } |
| 416 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 417 | sk_sp<SkFlattenable> SkPerlinNoiseShaderImpl::CreateProc(SkReadBuffer& buffer) { |
| Mike Reed | de5c502 | 2018-01-26 14:59:12 -0500 | [diff] [blame] | 418 | Type type = buffer.read32LE(kLast_Type); |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 419 | |
| reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 420 | SkScalar freqX = buffer.readScalar(); |
| 421 | SkScalar freqY = buffer.readScalar(); |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 422 | |
| Mike Reed | de5c502 | 2018-01-26 14:59:12 -0500 | [diff] [blame] | 423 | int octaves = buffer.read32LE<int>(kMaxOctaves); |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 424 | |
| reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 425 | SkScalar seed = buffer.readScalar(); |
| 426 | SkISize tileSize; |
| 427 | tileSize.fWidth = buffer.readInt(); |
| 428 | tileSize.fHeight = buffer.readInt(); |
| 429 | |
| 430 | switch (type) { |
| 431 | case kFractalNoise_Type: |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 432 | return SkPerlinNoiseShader::MakeFractalNoise(freqX, freqY, octaves, seed, &tileSize); |
| reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 433 | case kTurbulence_Type: |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 434 | return SkPerlinNoiseShader::MakeTurbulence(freqX, freqY, octaves, seed, &tileSize); |
| 435 | case kImprovedNoise_Type: |
| 436 | return SkPerlinNoiseShader::MakeImprovedNoise(freqX, freqY, octaves, seed); |
| reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 437 | default: |
| Mike Reed | de5c502 | 2018-01-26 14:59:12 -0500 | [diff] [blame] | 438 | // Really shouldn't get here b.c. of earlier check on type |
| Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 439 | buffer.validate(false); |
| halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 440 | return nullptr; |
| reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 444 | void SkPerlinNoiseShaderImpl::flatten(SkWriteBuffer& buffer) const { |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 445 | buffer.writeInt((int) fType); |
| 446 | buffer.writeScalar(fBaseFrequencyX); |
| 447 | buffer.writeScalar(fBaseFrequencyY); |
| 448 | buffer.writeInt(fNumOctaves); |
| 449 | buffer.writeScalar(fSeed); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 450 | buffer.writeInt(fTileSize.fWidth); |
| 451 | buffer.writeInt(fTileSize.fHeight); |
| 452 | } |
| 453 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 454 | SkScalar SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::noise2D( |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 455 | int channel, const StitchData& stitchData, const SkPoint& noiseVector) const { |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 456 | struct Noise { |
| 457 | int noisePositionIntegerValue; |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 458 | int nextNoisePositionIntegerValue; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 459 | SkScalar noisePositionFractionValue; |
| 460 | Noise(SkScalar component) |
| 461 | { |
| 462 | SkScalar position = component + kPerlinNoise; |
| 463 | noisePositionIntegerValue = SkScalarFloorToInt(position); |
| 464 | noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue); |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 465 | nextNoisePositionIntegerValue = noisePositionIntegerValue + 1; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 466 | } |
| 467 | }; |
| 468 | Noise noiseX(noiseVector.x()); |
| 469 | Noise noiseY(noiseVector.y()); |
| 470 | SkScalar u, v; |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 471 | const SkPerlinNoiseShaderImpl& perlinNoiseShader = static_cast<const SkPerlinNoiseShaderImpl&>(fShader); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 472 | // If stitching, adjust lattice points accordingly. |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 473 | if (perlinNoiseShader.fStitchTiles) { |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 474 | noiseX.noisePositionIntegerValue = |
| 475 | checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 476 | noiseY.noisePositionIntegerValue = |
| 477 | checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 478 | noiseX.nextNoisePositionIntegerValue = |
| 479 | checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 480 | noiseY.nextNoisePositionIntegerValue = |
| 481 | checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 482 | } |
| 483 | noiseX.noisePositionIntegerValue &= kBlockMask; |
| 484 | noiseY.noisePositionIntegerValue &= kBlockMask; |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 485 | noiseX.nextNoisePositionIntegerValue &= kBlockMask; |
| 486 | noiseY.nextNoisePositionIntegerValue &= kBlockMask; |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 487 | int i = fPaintingData.fLatticeSelector[noiseX.noisePositionIntegerValue]; |
| 488 | int j = fPaintingData.fLatticeSelector[noiseX.nextNoisePositionIntegerValue]; |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 489 | int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 490 | int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 491 | int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
| 492 | int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 493 | SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue); |
| 494 | SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 495 | |
| Hal Canary | fda4600 | 2017-05-08 17:17:47 -0400 | [diff] [blame] | 496 | if (sx < 0 || sy < 0 || sx > 1 || sy > 1) { |
| 497 | return 0; // Check for pathological inputs. |
| 498 | } |
| Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 499 | |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 500 | // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement |
| 501 | SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue, |
| 502 | noiseY.noisePositionFractionValue); // Offset (0,0) |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 503 | u = fPaintingData.fGradient[channel][b00].dot(fractionValue); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 504 | fractionValue.fX -= SK_Scalar1; // Offset (-1,0) |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 505 | v = fPaintingData.fGradient[channel][b10].dot(fractionValue); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 506 | SkScalar a = SkScalarInterp(u, v, sx); |
| 507 | fractionValue.fY -= SK_Scalar1; // Offset (-1,-1) |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 508 | v = fPaintingData.fGradient[channel][b11].dot(fractionValue); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 509 | fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1) |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 510 | u = fPaintingData.fGradient[channel][b01].dot(fractionValue); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 511 | SkScalar b = SkScalarInterp(u, v, sx); |
| 512 | return SkScalarInterp(a, b, sy); |
| 513 | } |
| 514 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 515 | SkScalar SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint( |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 516 | int channel, StitchData& stitchData, const SkPoint& point) const { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 517 | const SkPerlinNoiseShaderImpl& perlinNoiseShader = static_cast<const SkPerlinNoiseShaderImpl&>(fShader); |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 518 | if (perlinNoiseShader.fStitchTiles) { |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 519 | // Set up TurbulenceInitial stitch values. |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 520 | stitchData = fPaintingData.fStitchDataInit; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 521 | } |
| 522 | SkScalar turbulenceFunctionResult = 0; |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 523 | SkPoint noiseVector(SkPoint::Make(point.x() * fPaintingData.fBaseFrequency.fX, |
| 524 | point.y() * fPaintingData.fBaseFrequency.fY)); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 525 | SkScalar ratio = SK_Scalar1; |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 526 | for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) { |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 527 | SkScalar noise = noise2D(channel, stitchData, noiseVector); |
| reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 528 | SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ? |
| 529 | noise : SkScalarAbs(noise); |
| 530 | turbulenceFunctionResult += numer / ratio; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 531 | noiseVector.fX *= 2; |
| 532 | noiseVector.fY *= 2; |
| 533 | ratio *= 2; |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 534 | if (perlinNoiseShader.fStitchTiles) { |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 535 | // Update stitch values |
| 536 | stitchData.fWidth *= 2; |
| 537 | stitchData.fWrapX = stitchData.fWidth + kPerlinNoise; |
| 538 | stitchData.fHeight *= 2; |
| 539 | stitchData.fWrapY = stitchData.fHeight + kPerlinNoise; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 544 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 545 | if (perlinNoiseShader.fType == kFractalNoise_Type) { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 546 | turbulenceFunctionResult = SkScalarHalf(turbulenceFunctionResult + 1); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | if (channel == 3) { // Scale alpha by paint value |
| reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 550 | turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | // Clamp result |
| 554 | return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1); |
| 555 | } |
| 556 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 557 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 558 | // Improved Perlin Noise based on Java implementation found at http://mrl.nyu.edu/~perlin/noise/ |
| 559 | static SkScalar fade(SkScalar t) { |
| 560 | return t * t * t * (t * (t * 6 - 15) + 10); |
| 561 | } |
| 562 | |
| 563 | static SkScalar lerp(SkScalar t, SkScalar a, SkScalar b) { |
| 564 | return a + t * (b - a); |
| 565 | } |
| 566 | |
| 567 | static SkScalar grad(int hash, SkScalar x, SkScalar y, SkScalar z) { |
| 568 | int h = hash & 15; |
| 569 | SkScalar u = h < 8 ? x : y; |
| 570 | SkScalar v = h < 4 ? y : h == 12 || h == 14 ? x : z; |
| 571 | return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); |
| 572 | } |
| 573 | |
| 574 | SkScalar SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::calculateImprovedNoiseValueForPoint( |
| 575 | int channel, const SkPoint& point) const { |
| 576 | const SkPerlinNoiseShaderImpl& perlinNoiseShader = static_cast<const SkPerlinNoiseShaderImpl&>(fShader); |
| 577 | SkScalar x = point.fX * perlinNoiseShader.fBaseFrequencyX; |
| 578 | SkScalar y = point.fY * perlinNoiseShader.fBaseFrequencyY; |
| 579 | // z offset between different channels, chosen arbitrarily |
| 580 | static const SkScalar CHANNEL_DELTA = 1000.0f; |
| 581 | SkScalar z = channel * CHANNEL_DELTA + perlinNoiseShader.fSeed; |
| 582 | SkScalar result = 0; |
| 583 | SkScalar ratio = SK_Scalar1; |
| 584 | for (int i = 0; i < perlinNoiseShader.fNumOctaves; i++) { |
| 585 | int X = SkScalarFloorToInt(x) & 255; |
| 586 | int Y = SkScalarFloorToInt(y) & 255; |
| 587 | int Z = SkScalarFloorToInt(z) & 255; |
| 588 | SkScalar px = x - SkScalarFloorToScalar(x); |
| 589 | SkScalar py = y - SkScalarFloorToScalar(y); |
| 590 | SkScalar pz = z - SkScalarFloorToScalar(z); |
| 591 | SkScalar u = fade(px); |
| 592 | SkScalar v = fade(py); |
| 593 | SkScalar w = fade(pz); |
| 594 | uint8_t* permutations = improved_noise_permutations; |
| 595 | int A = permutations[X] + Y; |
| 596 | int AA = permutations[A] + Z; |
| 597 | int AB = permutations[A + 1] + Z; |
| 598 | int B = permutations[X + 1] + Y; |
| 599 | int BA = permutations[B] + Z; |
| 600 | int BB = permutations[B + 1] + Z; |
| 601 | result += lerp(w, lerp(v, lerp(u, grad(permutations[AA ], px , py , pz ), |
| 602 | grad(permutations[BA ], px - 1, py , pz )), |
| 603 | lerp(u, grad(permutations[AB ], px , py - 1, pz ), |
| 604 | grad(permutations[BB ], px - 1, py - 1, pz ))), |
| 605 | lerp(v, lerp(u, grad(permutations[AA + 1], px , py , pz - 1), |
| 606 | grad(permutations[BA + 1], px - 1, py , pz - 1)), |
| 607 | lerp(u, grad(permutations[AB + 1], px , py - 1, pz - 1), |
| 608 | grad(permutations[BB + 1], px - 1, py - 1, pz - 1)))) / |
| 609 | ratio; |
| 610 | x *= 2; |
| 611 | y *= 2; |
| 612 | ratio *= 2; |
| 613 | } |
| 614 | result = SkScalarClampMax((result + 1.0f) / 2.0f, 1.0f); |
| 615 | return result; |
| 616 | } |
| 617 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 618 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 619 | SkPMColor SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::shade( |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 620 | const SkPoint& point, StitchData& stitchData) const { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 621 | const SkPerlinNoiseShaderImpl& perlinNoiseShader = static_cast<const SkPerlinNoiseShaderImpl&>(fShader); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 622 | SkPoint newPoint; |
| senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 623 | fMatrix.mapPoints(&newPoint, &point, 1); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 624 | newPoint.fX = SkScalarRoundToScalar(newPoint.fX); |
| 625 | newPoint.fY = SkScalarRoundToScalar(newPoint.fY); |
| 626 | |
| 627 | U8CPU rgba[4]; |
| 628 | for (int channel = 3; channel >= 0; --channel) { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 629 | SkScalar value; |
| 630 | if (perlinNoiseShader.fType == kImprovedNoise_Type) { |
| 631 | value = calculateImprovedNoiseValueForPoint(channel, newPoint); |
| 632 | } |
| 633 | else { |
| 634 | value = calculateTurbulenceValueForPoint(channel, stitchData, newPoint); |
| 635 | } |
| 636 | rgba[channel] = SkScalarFloorToInt(255 * value); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 637 | } |
| 638 | return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]); |
| 639 | } |
| 640 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 641 | SkShaderBase::Context* SkPerlinNoiseShaderImpl::onMakeContext(const ContextRec& rec, |
| 642 | SkArenaAlloc* alloc) const { |
| Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 643 | return alloc->make<PerlinNoiseShaderContext>(*this, rec); |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 646 | static inline SkMatrix total_matrix(const SkShaderBase::ContextRec& rec, |
| 647 | const SkShaderBase& shader) { |
| 648 | SkMatrix matrix = SkMatrix::Concat(*rec.fMatrix, shader.getLocalMatrix()); |
| 649 | if (rec.fLocalMatrix) { |
| 650 | matrix.preConcat(*rec.fLocalMatrix); |
| 651 | } |
| 652 | |
| 653 | return matrix; |
| 654 | } |
| 655 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 656 | SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::PerlinNoiseShaderContext( |
| 657 | const SkPerlinNoiseShaderImpl& shader, const ContextRec& rec) |
| commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 658 | : INHERITED(shader, rec) |
| Florin Malita | 83223bc | 2017-05-31 14:14:05 -0400 | [diff] [blame] | 659 | , fMatrix(total_matrix(rec, shader)) // used for temp storage, adjusted below |
| 660 | , fPaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, |
| 661 | shader.fBaseFrequencyY, fMatrix) |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 662 | { |
| senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 663 | // This (1,1) translation is due to WebKit's 1 based coordinates for the noise |
| 664 | // (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] | 665 | fMatrix.setTranslate(-fMatrix.getTranslateX() + SK_Scalar1, |
| 666 | -fMatrix.getTranslateY() + SK_Scalar1); |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 669 | void SkPerlinNoiseShaderImpl::PerlinNoiseShaderContext::shadeSpan( |
| commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 670 | int x, int y, SkPMColor result[], int count) { |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 671 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
| 672 | StitchData stitchData; |
| 673 | for (int i = 0; i < count; ++i) { |
| 674 | result[i] = shade(point, stitchData); |
| 675 | point.fX += SK_Scalar1; |
| 676 | } |
| 677 | } |
| 678 | |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 679 | ///////////////////////////////////////////////////////////////////// |
| 680 | |
| commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 681 | #if SK_SUPPORT_GPU |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 682 | |
| egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 683 | class GrGLPerlinNoise : public GrGLSLFragmentProcessor { |
| sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 684 | public: |
| robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 685 | void emitCode(EmitArgs&) override; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 686 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 687 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b); |
| sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 688 | |
| wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 689 | protected: |
| Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 690 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
| wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 691 | |
| sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 692 | private: |
| egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 693 | GrGLSLProgramDataManager::UniformHandle fStitchDataUni; |
| egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 694 | GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni; |
| senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 695 | |
| egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 696 | typedef GrGLSLFragmentProcessor INHERITED; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | ///////////////////////////////////////////////////////////////////// |
| 700 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 701 | class GrPerlinNoise2Effect : public GrFragmentProcessor { |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 702 | public: |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 703 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 704 | SkPerlinNoiseShaderImpl::Type type, int numOctaves, bool stitchTiles, |
| 705 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData, |
| 706 | sk_sp<GrTextureProxy> permutationsProxy, sk_sp<GrTextureProxy> noiseProxy, |
| 707 | const SkMatrix& matrix) { |
| 708 | return std::unique_ptr<GrFragmentProcessor>(new GrPerlinNoise2Effect( |
| 709 | type, numOctaves, stitchTiles, std::move(paintingData), |
| 710 | std::move(permutationsProxy), std::move(noiseProxy), matrix)); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 713 | const char* name() const override { return "PerlinNoise"; } |
| joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 714 | |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 715 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 716 | return std::unique_ptr<GrFragmentProcessor>(new GrPerlinNoise2Effect(*this)); |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 717 | } |
| 718 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 719 | const SkPerlinNoiseShaderImpl::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; } |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 720 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 721 | SkPerlinNoiseShaderImpl::Type type() const { return fType; } |
| senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 722 | bool stitchTiles() const { return fStitchTiles; } |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 723 | const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; } |
| senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 724 | int numOctaves() const { return fNumOctaves; } |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 725 | const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } |
| senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 726 | |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 727 | private: |
| egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 728 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 729 | return new GrGLPerlinNoise; |
| wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 732 | virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 733 | GrProcessorKeyBuilder* b) const override { |
| wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 734 | GrGLPerlinNoise::GenKey(*this, caps, b); |
| 735 | } |
| 736 | |
| mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 737 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 738 | const GrPerlinNoise2Effect& s = sBase.cast<GrPerlinNoise2Effect>(); |
| senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 739 | return fType == s.fType && |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 740 | fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency && |
| senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 741 | fNumOctaves == s.fNumOctaves && |
| 742 | fStitchTiles == s.fStitchTiles && |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 743 | fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 746 | GrPerlinNoise2Effect(SkPerlinNoiseShaderImpl::Type type, int numOctaves, bool stitchTiles, |
| Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 747 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 748 | sk_sp<GrTextureProxy> permutationsProxy, |
| 749 | sk_sp<GrTextureProxy> noiseProxy, |
| 750 | const SkMatrix& matrix) |
| Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 751 | : INHERITED(kGrPerlinNoise2Effect_ClassID, kNone_OptimizationFlags) |
| Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 752 | , fType(type) |
| Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 753 | , fNumOctaves(numOctaves) |
| 754 | , fStitchTiles(stitchTiles) |
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 755 | , fPermutationsSampler(std::move(permutationsProxy)) |
| 756 | , fNoiseSampler(std::move(noiseProxy)) |
| Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 757 | , fPaintingData(std::move(paintingData)) { |
| Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 758 | this->addTextureSampler(&fPermutationsSampler); |
| 759 | this->addTextureSampler(&fNoiseSampler); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 760 | fCoordTransform.reset(matrix); |
| senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 761 | this->addCoordTransform(&fCoordTransform); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 764 | GrPerlinNoise2Effect(const GrPerlinNoise2Effect& that) |
| Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 765 | : INHERITED(kGrPerlinNoise2Effect_ClassID, kNone_OptimizationFlags) |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 766 | , fType(that.fType) |
| 767 | , fCoordTransform(that.fCoordTransform) |
| 768 | , fNumOctaves(that.fNumOctaves) |
| 769 | , fStitchTiles(that.fStitchTiles) |
| 770 | , fPermutationsSampler(that.fPermutationsSampler) |
| 771 | , fNoiseSampler(that.fNoiseSampler) |
| 772 | , fPaintingData(new SkPerlinNoiseShaderImpl::PaintingData(*that.fPaintingData)) { |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 773 | this->addTextureSampler(&fPermutationsSampler); |
| 774 | this->addTextureSampler(&fNoiseSampler); |
| 775 | this->addCoordTransform(&fCoordTransform); |
| 776 | } |
| 777 | |
| Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 778 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 779 | |
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 780 | SkPerlinNoiseShaderImpl::Type fType; |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 781 | GrCoordTransform fCoordTransform; |
| 782 | int fNumOctaves; |
| 783 | bool fStitchTiles; |
| 784 | TextureSampler fPermutationsSampler; |
| 785 | TextureSampler fNoiseSampler; |
| Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 786 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> fPaintingData; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 787 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 788 | typedef GrFragmentProcessor INHERITED; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 789 | }; |
| 790 | |
| 791 | ///////////////////////////////////////////////////////////////////// |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 792 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoise2Effect); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 793 | |
| Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 794 | #if GR_TEST_UTILS |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 795 | std::unique_ptr<GrFragmentProcessor> GrPerlinNoise2Effect::TestCreate(GrProcessorTestData* d) { |
| joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 796 | int numOctaves = d->fRandom->nextRangeU(2, 10); |
| 797 | bool stitchTiles = d->fRandom->nextBool(); |
| 798 | SkScalar seed = SkIntToScalar(d->fRandom->nextU()); |
| 799 | SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096), |
| 800 | d->fRandom->nextRangeU(4, 4096)); |
| 801 | SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, |
| 802 | 0.99f); |
| 803 | SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, |
| 804 | 0.99f); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 805 | |
| reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 806 | sk_sp<SkShader> shader(d->fRandom->nextBool() ? |
| 807 | SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 808 | stitchTiles ? &tileSize : nullptr) : |
| reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 809 | SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 810 | stitchTiles ? &tileSize : nullptr)); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 811 | |
| Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 812 | GrTest::TestAsFPArgs asFPArgs(d); |
| Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 813 | return as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 814 | } |
| Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 815 | #endif |
| sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 816 | |
| wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 817 | void GrGLPerlinNoise::emitCode(EmitArgs& args) { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 818 | const GrPerlinNoise2Effect& pne = args.fFp.cast<GrPerlinNoise2Effect>(); |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 819 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 820 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 821 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 822 | SkString vCoords = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 823 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 824 | fBaseFrequencyUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, |
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 825 | "baseFrequency"); |
| 826 | const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 827 | |
| halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 828 | const char* stitchDataUni = nullptr; |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 829 | if (pne.stitchTiles()) { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 830 | fStitchDataUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, |
| egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 831 | "stitchData"); |
| 832 | stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 835 | // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 |
| 836 | const char* chanCoordR = "0.125"; |
| 837 | const char* chanCoordG = "0.375"; |
| 838 | const char* chanCoordB = "0.625"; |
| 839 | const char* chanCoordA = "0.875"; |
| 840 | const char* chanCoord = "chanCoord"; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 841 | const char* stitchData = "stitchData"; |
| 842 | const char* ratio = "ratio"; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 843 | const char* noiseVec = "noiseVec"; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 844 | const char* noiseSmooth = "noiseSmooth"; |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 845 | const char* floorVal = "floorVal"; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 846 | const char* fractVal = "fractVal"; |
| 847 | const char* uv = "uv"; |
| 848 | const char* ab = "ab"; |
| 849 | const char* latticeIdx = "latticeIdx"; |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 850 | const char* bcoords = "bcoords"; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 851 | const char* lattice = "lattice"; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 852 | const char* inc8bit = "0.00390625"; // 1.0 / 256.0 |
| 853 | // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a |
| 854 | // [-1,1] vector and perform a dot product between that vector and the provided vector. |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 855 | const char* dotLattice = "dot(((%s.ga + %s.rb * half2(%s)) * half2(2.0) - half2(1.0)), %s);"; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 856 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 857 | // Add noise function |
| Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 858 | static const GrShaderVar gPerlinNoiseArgs[] = { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 859 | GrShaderVar(chanCoord, kHalf_GrSLType), |
| 860 | GrShaderVar(noiseVec, kHalf2_GrSLType) |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 861 | }; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 862 | |
| Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 863 | static const GrShaderVar gPerlinNoiseStitchArgs[] = { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 864 | GrShaderVar(chanCoord, kHalf_GrSLType), |
| 865 | GrShaderVar(noiseVec, kHalf2_GrSLType), |
| 866 | GrShaderVar(stitchData, kHalf2_GrSLType) |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 867 | }; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 868 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 869 | SkString noiseCode; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 870 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 871 | noiseCode.appendf("\thalf4 %s;\n", floorVal); |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 872 | noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 873 | noiseCode.appendf("\t%s.zw = %s.xy + half2(1.0);\n", floorVal, floorVal); |
| 874 | noiseCode.appendf("\thalf2 %s = fract(%s);\n", fractVal, noiseVec); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 875 | |
| 876 | // smooth curve : t * t * (3 - 2 * t) |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 877 | noiseCode.appendf("\n\thalf2 %s = %s * %s * (half2(3.0) - half2(2.0) * %s);", |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 878 | noiseSmooth, fractVal, fractVal, fractVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 879 | |
| 880 | // Adjust frequencies if we're stitching tiles |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 881 | if (pne.stitchTiles()) { |
| commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 882 | noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 883 | floorVal, stitchData, floorVal, stitchData); |
| commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 884 | noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 885 | floorVal, stitchData, floorVal, stitchData); |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 886 | noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 887 | floorVal, stitchData, floorVal, stitchData); |
| senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 888 | noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 889 | floorVal, stitchData, floorVal, stitchData); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | // Get texture coordinates and normalize |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 893 | noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / half4(256.0));\n", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 894 | floorVal, floorVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 895 | |
| 896 | // Get permutation for x |
| 897 | { |
| 898 | SkString xCoords(""); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 899 | xCoords.appendf("half2(%s.x, 0.5)", floorVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 900 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 901 | noiseCode.appendf("\n\thalf2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); |
| cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 902 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(), |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 903 | kHalf2_GrSLType); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 904 | noiseCode.append(".r;"); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | // Get permutation for x + 1 |
| 908 | { |
| 909 | SkString xCoords(""); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 910 | xCoords.appendf("half2(%s.z, 0.5)", floorVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 911 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 912 | noiseCode.appendf("\n\t%s.y = ", latticeIdx); |
| cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 913 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(), |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 914 | kHalf2_GrSLType); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 915 | noiseCode.append(".r;"); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 918 | #if defined(SK_BUILD_FOR_ANDROID) |
| 919 | // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3). |
| 920 | // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit |
| 921 | // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725 |
| 922 | // (or 0.484368 here). The following rounding operation prevents these precision issues from |
| 923 | // affecting the result of the noise by making sure that we only have multiples of 1/255. |
| 924 | // (Note that 1/255 is about 0.003921569, which is the value used here). |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 925 | noiseCode.appendf("\n\t%s = floor(%s * half2(255.0) + half2(0.5)) * half2(0.003921569);", |
| commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 926 | latticeIdx, latticeIdx); |
| 927 | #endif |
| 928 | |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 929 | // Get (x,y) coordinates with the permutated x |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 930 | noiseCode.appendf("\n\thalf4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 931 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 932 | noiseCode.appendf("\n\n\thalf2 %s;", uv); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 933 | // Compute u, at offset (0,0) |
| 934 | { |
| 935 | SkString latticeCoords(""); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 936 | latticeCoords.appendf("half2(%s.x, %s)", bcoords, chanCoord); |
| 937 | noiseCode.appendf("\n\thalf4 %s = ", lattice); |
| cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 938 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(), |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 939 | kHalf2_GrSLType); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 940 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
| 941 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 944 | noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 945 | // Compute v, at offset (-1,0) |
| 946 | { |
| 947 | SkString latticeCoords(""); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 948 | latticeCoords.appendf("half2(%s.y, %s)", bcoords, chanCoord); |
| commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 949 | noiseCode.append("\n\tlattice = "); |
| cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 950 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(), |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 951 | kHalf2_GrSLType); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 952 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
| 953 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | // Compute 'a' as a linear interpolation of 'u' and 'v' |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 957 | noiseCode.appendf("\n\thalf2 %s;", ab); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 958 | 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] | 959 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 960 | noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 961 | // Compute v, at offset (-1,-1) |
| 962 | { |
| 963 | SkString latticeCoords(""); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 964 | latticeCoords.appendf("half2(%s.w, %s)", bcoords, chanCoord); |
| commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 965 | noiseCode.append("\n\tlattice = "); |
| cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 966 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(), |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 967 | kHalf2_GrSLType); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 968 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
| 969 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 972 | noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 973 | // Compute u, at offset (0,-1) |
| 974 | { |
| 975 | SkString latticeCoords(""); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 976 | latticeCoords.appendf("half2(%s.z, %s)", bcoords, chanCoord); |
| commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 977 | noiseCode.append("\n\tlattice = "); |
| cdalton | 3f6f76f | 2016-04-11 12:18:09 -0700 | [diff] [blame] | 978 | fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(), |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 979 | kHalf2_GrSLType); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 980 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
| 981 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | // Compute 'b' as a linear interpolation of 'u' and 'v' |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 985 | 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] | 986 | // Compute the noise as a linear interpolation of 'a' and 'b' |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 987 | 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] | 988 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 989 | SkString noiseFuncName; |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 990 | if (pne.stitchTiles()) { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 991 | fragBuilder->emitFunction(kHalf_GrSLType, |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 992 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs), |
| 993 | gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 994 | } else { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 995 | fragBuilder->emitFunction(kHalf_GrSLType, |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 996 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs), |
| 997 | gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 998 | } |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 999 | |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1000 | // There are rounding errors if the floor operation is not performed here |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1001 | fragBuilder->codeAppendf("\n\t\thalf2 %s = floor(%s.xy) * %s;", |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1002 | noiseVec, vCoords.c_str(), baseFrequencyUni); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1003 | |
| 1004 | // Clear the color accumulator |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1005 | fragBuilder->codeAppendf("\n\t\t%s = half4(0.0);", args.fOutputColor); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1006 | |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 1007 | if (pne.stitchTiles()) { |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1008 | // Set up TurbulenceInitial stitch values. |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1009 | fragBuilder->codeAppendf("\n\t\thalf2 %s = %s;", stitchData, stitchDataUni); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1010 | } |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1011 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1012 | fragBuilder->codeAppendf("\n\t\thalf %s = 1.0;", ratio); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Loop over all octaves |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 1015 | fragBuilder->codeAppendf("for (int octave = 0; octave < %d; ++octave) {", pne.numOctaves()); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1016 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1017 | fragBuilder->codeAppendf("\n\t\t\t%s += ", args.fOutputColor); |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1018 | if (pne.type() != SkPerlinNoiseShaderImpl::kFractalNoise_Type) { |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1019 | fragBuilder->codeAppend("abs("); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1020 | } |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 1021 | if (pne.stitchTiles()) { |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1022 | fragBuilder->codeAppendf( |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1023 | "half4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s)," |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1024 | "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))", |
| 1025 | noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData, |
| 1026 | noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData, |
| 1027 | noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData, |
| 1028 | noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData); |
| 1029 | } else { |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1030 | fragBuilder->codeAppendf( |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1031 | "half4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s)," |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1032 | "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))", |
| 1033 | noiseFuncName.c_str(), chanCoordR, noiseVec, |
| 1034 | noiseFuncName.c_str(), chanCoordG, noiseVec, |
| 1035 | noiseFuncName.c_str(), chanCoordB, noiseVec, |
| 1036 | noiseFuncName.c_str(), chanCoordA, noiseVec); |
| 1037 | } |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1038 | if (pne.type() != SkPerlinNoiseShaderImpl::kFractalNoise_Type) { |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1039 | fragBuilder->codeAppendf(")"); // end of "abs(" |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1040 | } |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1041 | fragBuilder->codeAppendf(" * %s;", ratio); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1042 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1043 | fragBuilder->codeAppendf("\n\t\t\t%s *= half2(2.0);", noiseVec); |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1044 | fragBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1045 | |
| robertphillips | bf536af | 2016-02-04 06:11:53 -0800 | [diff] [blame] | 1046 | if (pne.stitchTiles()) { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1047 | fragBuilder->codeAppendf("\n\t\t\t%s *= half2(2.0);", stitchData); |
| sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 1048 | } |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1049 | 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] | 1050 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1051 | if (pne.type() == SkPerlinNoiseShaderImpl::kFractalNoise_Type) { |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1052 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 1053 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1054 | fragBuilder->codeAppendf("\n\t\t%s = %s * half4(0.5) + half4(0.5);", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1055 | args.fOutputColor,args.fOutputColor); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1058 | // Clamp values |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1059 | 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] | 1060 | |
| 1061 | // Pre-multiply the result |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1062 | fragBuilder->codeAppendf("\n\t\t%s = half4(%s.rgb * %s.aaa, %s.a);\n", |
| egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 1063 | args.fOutputColor, args.fOutputColor, |
| 1064 | args.fOutputColor, args.fOutputColor); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 1067 | void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1068 | GrProcessorKeyBuilder* b) { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1069 | const GrPerlinNoise2Effect& turbulence = processor.cast<GrPerlinNoise2Effect>(); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1070 | |
| bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1071 | uint32_t key = turbulence.numOctaves(); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1072 | |
| 1073 | key = key << 3; // Make room for next 3 bits |
| 1074 | |
| 1075 | switch (turbulence.type()) { |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1076 | case SkPerlinNoiseShaderImpl::kFractalNoise_Type: |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1077 | key |= 0x1; |
| 1078 | break; |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1079 | case SkPerlinNoiseShaderImpl::kTurbulence_Type: |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1080 | key |= 0x2; |
| 1081 | break; |
| 1082 | default: |
| 1083 | // leave key at 0 |
| 1084 | break; |
| 1085 | } |
| 1086 | |
| 1087 | if (turbulence.stitchTiles()) { |
| 1088 | key |= 0x4; // Flip the 3rd bit if tile stitching is on |
| 1089 | } |
| 1090 | |
| bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 1091 | b->add32(key); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 1094 | void GrGLPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman, |
| Brian Salomon | ab015ef | 2017-04-04 10:15:51 -0400 | [diff] [blame] | 1095 | const GrFragmentProcessor& processor) { |
| wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 1096 | INHERITED::onSetData(pdman, processor); |
| senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 1097 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1098 | const GrPerlinNoise2Effect& turbulence = processor.cast<GrPerlinNoise2Effect>(); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1099 | |
| 1100 | const SkVector& baseFrequency = turbulence.baseFrequency(); |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1101 | pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1102 | |
| sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 1103 | if (turbulence.stitchTiles()) { |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1104 | const SkPerlinNoiseShaderImpl::StitchData& stitchData = turbulence.stitchData(); |
| kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 1105 | pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
| commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 1106 | SkIntToScalar(stitchData.fHeight)); |
| sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1110 | ///////////////////////////////////////////////////////////////////// |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1111 | |
| 1112 | class GrGLImprovedPerlinNoise : public GrGLSLFragmentProcessor { |
| 1113 | public: |
| 1114 | void emitCode(EmitArgs&) override; |
| 1115 | |
| 1116 | static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*); |
| 1117 | |
| 1118 | protected: |
| 1119 | void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override; |
| 1120 | |
| 1121 | private: |
| 1122 | GrGLSLProgramDataManager::UniformHandle fZUni; |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1123 | GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni; |
| 1124 | |
| 1125 | typedef GrGLSLFragmentProcessor INHERITED; |
| 1126 | }; |
| 1127 | |
| 1128 | ///////////////////////////////////////////////////////////////////// |
| 1129 | |
| 1130 | class GrImprovedPerlinNoiseEffect : public GrFragmentProcessor { |
| 1131 | public: |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1132 | static std::unique_ptr<GrFragmentProcessor> Make( |
| 1133 | int octaves, SkScalar z, |
| 1134 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData, |
| 1135 | sk_sp<GrTextureProxy> permutationsProxy, sk_sp<GrTextureProxy> gradientProxy, |
| 1136 | const SkMatrix& matrix) { |
| 1137 | return std::unique_ptr<GrFragmentProcessor>(new GrImprovedPerlinNoiseEffect( |
| 1138 | octaves, z, std::move(paintingData), std::move(permutationsProxy), |
| 1139 | std::move(gradientProxy), matrix)); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1140 | } |
| 1141 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1142 | const char* name() const override { return "ImprovedPerlinNoise"; } |
| 1143 | |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1144 | std::unique_ptr<GrFragmentProcessor> clone() const override { |
| 1145 | return std::unique_ptr<GrFragmentProcessor>(new GrImprovedPerlinNoiseEffect(*this)); |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 1146 | } |
| 1147 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1148 | const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; } |
| 1149 | SkScalar z() const { return fZ; } |
| 1150 | int octaves() const { return fOctaves; } |
| 1151 | const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } |
| 1152 | |
| 1153 | private: |
| 1154 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 1155 | return new GrGLImprovedPerlinNoise; |
| 1156 | } |
| 1157 | |
| 1158 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
| 1159 | GrGLImprovedPerlinNoise::GenKey(*this, caps, b); |
| 1160 | } |
| 1161 | |
| 1162 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
| 1163 | const GrImprovedPerlinNoiseEffect& s = sBase.cast<GrImprovedPerlinNoiseEffect>(); |
| 1164 | return fZ == fZ && |
| 1165 | fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency; |
| 1166 | } |
| 1167 | |
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 1168 | GrImprovedPerlinNoiseEffect(int octaves, SkScalar z, |
| Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 1169 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1170 | sk_sp<GrTextureProxy> permutationsProxy, |
| 1171 | sk_sp<GrTextureProxy> gradientProxy, |
| 1172 | const SkMatrix& matrix) |
| Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 1173 | : INHERITED(kGrImprovedPerlinNoiseEffect_ClassID, kNone_OptimizationFlags) |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1174 | , fOctaves(octaves) |
| 1175 | , fZ(z) |
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 1176 | , fPermutationsSampler(std::move(permutationsProxy)) |
| 1177 | , fGradientSampler(std::move(gradientProxy)) |
| Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 1178 | , fPaintingData(std::move(paintingData)) { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1179 | this->addTextureSampler(&fPermutationsSampler); |
| 1180 | this->addTextureSampler(&fGradientSampler); |
| 1181 | fCoordTransform.reset(matrix); |
| 1182 | this->addCoordTransform(&fCoordTransform); |
| 1183 | } |
| 1184 | |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 1185 | GrImprovedPerlinNoiseEffect(const GrImprovedPerlinNoiseEffect& that) |
| Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 1186 | : INHERITED(kGrImprovedPerlinNoiseEffect_ClassID, kNone_OptimizationFlags) |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 1187 | , fCoordTransform(that.fCoordTransform) |
| 1188 | , fOctaves(that.fOctaves) |
| 1189 | , fZ(that.fZ) |
| 1190 | , fPermutationsSampler(that.fPermutationsSampler) |
| 1191 | , fGradientSampler(that.fGradientSampler) |
| 1192 | , fPaintingData(new SkPerlinNoiseShaderImpl::PaintingData(*that.fPaintingData)) { |
| Brian Salomon | 4331e46 | 2017-07-26 14:58:11 -0400 | [diff] [blame] | 1193 | this->addTextureSampler(&fPermutationsSampler); |
| 1194 | this->addTextureSampler(&fGradientSampler); |
| 1195 | this->addCoordTransform(&fCoordTransform); |
| 1196 | } |
| 1197 | |
| Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 1198 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1199 | |
| 1200 | GrCoordTransform fCoordTransform; |
| 1201 | int fOctaves; |
| 1202 | SkScalar fZ; |
| 1203 | TextureSampler fPermutationsSampler; |
| 1204 | TextureSampler fGradientSampler; |
| Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 1205 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> fPaintingData; |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1206 | |
| 1207 | typedef GrFragmentProcessor INHERITED; |
| 1208 | }; |
| 1209 | |
| 1210 | ///////////////////////////////////////////////////////////////////// |
| 1211 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrImprovedPerlinNoiseEffect); |
| 1212 | |
| 1213 | #if GR_TEST_UTILS |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1214 | std::unique_ptr<GrFragmentProcessor> GrImprovedPerlinNoiseEffect::TestCreate( |
| 1215 | GrProcessorTestData* d) { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1216 | SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, |
| 1217 | 0.99f); |
| 1218 | SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, |
| 1219 | 0.99f); |
| 1220 | int numOctaves = d->fRandom->nextRangeU(2, 10); |
| 1221 | SkScalar z = SkIntToScalar(d->fRandom->nextU()); |
| 1222 | |
| 1223 | sk_sp<SkShader> shader(SkPerlinNoiseShader::MakeImprovedNoise(baseFrequencyX, |
| 1224 | baseFrequencyY, |
| 1225 | numOctaves, |
| 1226 | z)); |
| 1227 | |
| 1228 | GrTest::TestAsFPArgs asFPArgs(d); |
| 1229 | return as_SB(shader)->asFragmentProcessor(asFPArgs.args()); |
| 1230 | } |
| 1231 | #endif |
| 1232 | |
| 1233 | void GrGLImprovedPerlinNoise::emitCode(EmitArgs& args) { |
| Ethan Nicholas | c352f3b | 2017-06-19 11:25:00 -0400 | [diff] [blame] | 1234 | const GrImprovedPerlinNoiseEffect& pne = args.fFp.cast<GrImprovedPerlinNoiseEffect>(); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1235 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 1236 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 1237 | SkString vCoords = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]); |
| 1238 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1239 | fBaseFrequencyUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1240 | "baseFrequency"); |
| 1241 | const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni); |
| 1242 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1243 | fZUni = uniformHandler->addUniform(kFragment_GrShaderFlag, kHalf_GrSLType, "z"); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1244 | const char* zUni = uniformHandler->getUniformCStr(fZUni); |
| 1245 | |
| 1246 | // fade function |
| 1247 | static const GrShaderVar fadeArgs[] = { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1248 | GrShaderVar("t", kHalf3_GrSLType) |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1249 | }; |
| 1250 | SkString fadeFuncName; |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1251 | fragBuilder->emitFunction(kHalf3_GrSLType, "fade", SK_ARRAY_COUNT(fadeArgs), |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1252 | fadeArgs, |
| 1253 | "return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);", |
| 1254 | &fadeFuncName); |
| 1255 | |
| 1256 | // perm function |
| 1257 | static const GrShaderVar permArgs[] = { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1258 | GrShaderVar("x", kHalf_GrSLType) |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1259 | }; |
| 1260 | SkString permFuncName; |
| 1261 | SkString permCode("return "); |
| 1262 | // FIXME even though I'm creating these textures with kRepeat_TileMode, they're clamped. Not |
| 1263 | // sure why. Using fract() (here and the next texture lookup) as a workaround. |
| Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1264 | fragBuilder->appendTextureLookup(&permCode, args.fTexSamplers[0], "float2(fract(x / 256.0), 0.0)", |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1265 | kHalf2_GrSLType); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1266 | permCode.append(".r * 255.0;"); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1267 | fragBuilder->emitFunction(kHalf_GrSLType, "perm", SK_ARRAY_COUNT(permArgs), permArgs, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1268 | permCode.c_str(), &permFuncName); |
| 1269 | |
| 1270 | // grad function |
| 1271 | static const GrShaderVar gradArgs[] = { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1272 | GrShaderVar("x", kHalf_GrSLType), |
| 1273 | GrShaderVar("p", kHalf3_GrSLType) |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1274 | }; |
| 1275 | SkString gradFuncName; |
| 1276 | SkString gradCode("return dot("); |
| Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1277 | fragBuilder->appendTextureLookup(&gradCode, args.fTexSamplers[1], "float2(fract(x / 16.0), 0.0)", |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1278 | kHalf2_GrSLType); |
| Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1279 | gradCode.append(".rgb * 255.0 - float3(1.0), p);"); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1280 | fragBuilder->emitFunction(kHalf_GrSLType, "grad", SK_ARRAY_COUNT(gradArgs), gradArgs, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1281 | gradCode.c_str(), &gradFuncName); |
| 1282 | |
| 1283 | // lerp function |
| 1284 | static const GrShaderVar lerpArgs[] = { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1285 | GrShaderVar("a", kHalf_GrSLType), |
| 1286 | GrShaderVar("b", kHalf_GrSLType), |
| 1287 | GrShaderVar("w", kHalf_GrSLType) |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1288 | }; |
| 1289 | SkString lerpFuncName; |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1290 | fragBuilder->emitFunction(kHalf_GrSLType, "lerp", SK_ARRAY_COUNT(lerpArgs), lerpArgs, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1291 | "return a + w * (b - a);", &lerpFuncName); |
| 1292 | |
| 1293 | // noise function |
| 1294 | static const GrShaderVar noiseArgs[] = { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1295 | GrShaderVar("p", kHalf3_GrSLType), |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1296 | }; |
| 1297 | SkString noiseFuncName; |
| 1298 | SkString noiseCode; |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1299 | noiseCode.append("half3 P = mod(floor(p), 256.0);"); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1300 | noiseCode.append("p -= floor(p);"); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1301 | noiseCode.appendf("half3 f = %s(p);", fadeFuncName.c_str()); |
| 1302 | noiseCode.appendf("half A = %s(P.x) + P.y;", permFuncName.c_str()); |
| 1303 | noiseCode.appendf("half AA = %s(A) + P.z;", permFuncName.c_str()); |
| 1304 | noiseCode.appendf("half AB = %s(A + 1.0) + P.z;", permFuncName.c_str()); |
| 1305 | noiseCode.appendf("half B = %s(P.x + 1.0) + P.y;", permFuncName.c_str()); |
| 1306 | noiseCode.appendf("half BA = %s(B) + P.z;", permFuncName.c_str()); |
| 1307 | noiseCode.appendf("half BB = %s(B + 1.0) + P.z;", permFuncName.c_str()); |
| 1308 | noiseCode.appendf("half result = %s(", lerpFuncName.c_str()); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1309 | noiseCode.appendf("%s(%s(%s(%s(AA), p),", lerpFuncName.c_str(), lerpFuncName.c_str(), |
| 1310 | gradFuncName.c_str(), permFuncName.c_str()); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1311 | noiseCode.appendf("%s(%s(BA), p + half3(-1.0, 0.0, 0.0)), f.x),", gradFuncName.c_str(), |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1312 | permFuncName.c_str()); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1313 | noiseCode.appendf("%s(%s(%s(AB), p + half3(0.0, -1.0, 0.0)),", lerpFuncName.c_str(), |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1314 | gradFuncName.c_str(), permFuncName.c_str()); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1315 | noiseCode.appendf("%s(%s(BB), p + half3(-1.0, -1.0, 0.0)), f.x), f.y),", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1316 | gradFuncName.c_str(), permFuncName.c_str()); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1317 | noiseCode.appendf("%s(%s(%s(%s(AA + 1.0), p + half3(0.0, 0.0, -1.0)),", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1318 | lerpFuncName.c_str(), lerpFuncName.c_str(), gradFuncName.c_str(), |
| 1319 | permFuncName.c_str()); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1320 | noiseCode.appendf("%s(%s(BA + 1.0), p + half3(-1.0, 0.0, -1.0)), f.x),", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1321 | gradFuncName.c_str(), permFuncName.c_str()); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1322 | noiseCode.appendf("%s(%s(%s(AB + 1.0), p + half3(0.0, -1.0, -1.0)),", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1323 | lerpFuncName.c_str(), gradFuncName.c_str(), permFuncName.c_str()); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1324 | noiseCode.appendf("%s(%s(BB + 1.0), p + half3(-1.0, -1.0, -1.0)), f.x), f.y), f.z);", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1325 | gradFuncName.c_str(), permFuncName.c_str()); |
| 1326 | noiseCode.append("return result;"); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1327 | fragBuilder->emitFunction(kHalf_GrSLType, "noise", SK_ARRAY_COUNT(noiseArgs), noiseArgs, |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1328 | noiseCode.c_str(), &noiseFuncName); |
| 1329 | |
| 1330 | // noiseOctaves function |
| 1331 | static const GrShaderVar noiseOctavesArgs[] = { |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1332 | GrShaderVar("p", kHalf3_GrSLType) |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1333 | }; |
| 1334 | SkString noiseOctavesFuncName; |
| 1335 | SkString noiseOctavesCode; |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1336 | noiseOctavesCode.append("half result = 0.0;"); |
| 1337 | noiseOctavesCode.append("half ratio = 1.0;"); |
| 1338 | noiseOctavesCode.appendf("for (half i = 0.0; i < %d; i++) {", pne.octaves()); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1339 | noiseOctavesCode.appendf("result += %s(p) / ratio;", noiseFuncName.c_str()); |
| 1340 | noiseOctavesCode.append("p *= 2.0;"); |
| 1341 | noiseOctavesCode.append("ratio *= 2.0;"); |
| 1342 | noiseOctavesCode.append("}"); |
| 1343 | noiseOctavesCode.append("return (result + 1.0) / 2.0;"); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1344 | fragBuilder->emitFunction(kHalf_GrSLType, "noiseOctaves", SK_ARRAY_COUNT(noiseOctavesArgs), |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1345 | noiseOctavesArgs, noiseOctavesCode.c_str(), &noiseOctavesFuncName); |
| 1346 | |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1347 | fragBuilder->codeAppendf("half2 coords = %s * %s;", vCoords.c_str(), baseFrequencyUni); |
| 1348 | fragBuilder->codeAppendf("half r = %s(half3(coords, %s));", noiseOctavesFuncName.c_str(), |
| Ethan Nicholas | c352f3b | 2017-06-19 11:25:00 -0400 | [diff] [blame] | 1349 | zUni); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1350 | fragBuilder->codeAppendf("half g = %s(half3(coords, %s + 0000.0));", |
| Ethan Nicholas | c352f3b | 2017-06-19 11:25:00 -0400 | [diff] [blame] | 1351 | noiseOctavesFuncName.c_str(), zUni); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1352 | fragBuilder->codeAppendf("half b = %s(half3(coords, %s + 0000.0));", |
| Ethan Nicholas | c352f3b | 2017-06-19 11:25:00 -0400 | [diff] [blame] | 1353 | noiseOctavesFuncName.c_str(), zUni); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1354 | fragBuilder->codeAppendf("half a = %s(half3(coords, %s + 0000.0));", |
| Ethan Nicholas | c352f3b | 2017-06-19 11:25:00 -0400 | [diff] [blame] | 1355 | noiseOctavesFuncName.c_str(), zUni); |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1356 | fragBuilder->codeAppendf("%s = half4(r, g, b, a);", args.fOutputColor); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1357 | |
| 1358 | // Clamp values |
| 1359 | fragBuilder->codeAppendf("%s = clamp(%s, 0.0, 1.0);", args.fOutputColor, args.fOutputColor); |
| 1360 | |
| 1361 | // Pre-multiply the result |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1362 | fragBuilder->codeAppendf("\n\t\t%s = half4(%s.rgb * %s.aaa, %s.a);\n", |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1363 | args.fOutputColor, args.fOutputColor, |
| 1364 | args.fOutputColor, args.fOutputColor); |
| 1365 | } |
| 1366 | |
| 1367 | void GrGLImprovedPerlinNoise::GenKey(const GrProcessor& processor, const GrShaderCaps&, |
| 1368 | GrProcessorKeyBuilder* b) { |
| Ethan Nicholas | c352f3b | 2017-06-19 11:25:00 -0400 | [diff] [blame] | 1369 | const GrImprovedPerlinNoiseEffect& pne = processor.cast<GrImprovedPerlinNoiseEffect>(); |
| 1370 | b->add32(pne.octaves()); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | void GrGLImprovedPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman, |
| 1374 | const GrFragmentProcessor& processor) { |
| 1375 | INHERITED::onSetData(pdman, processor); |
| 1376 | |
| 1377 | const GrImprovedPerlinNoiseEffect& noise = processor.cast<GrImprovedPerlinNoiseEffect>(); |
| 1378 | |
| 1379 | const SkVector& baseFrequency = noise.baseFrequency(); |
| 1380 | pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
| 1381 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1382 | pdman.set1f(fZUni, noise.z()); |
| 1383 | } |
| 1384 | |
| 1385 | ///////////////////////////////////////////////////////////////////// |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1386 | std::unique_ptr<GrFragmentProcessor> SkPerlinNoiseShaderImpl::asFragmentProcessor( |
| Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 1387 | const GrFPArgs& args) const { |
| brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 1388 | SkASSERT(args.fContext); |
| mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 1389 | |
| commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 1390 | SkMatrix localMatrix = this->getLocalMatrix(); |
| brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 1391 | if (args.fLocalMatrix) { |
| 1392 | localMatrix.preConcat(*args.fLocalMatrix); |
| commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| brianosman | 839345d | 2016-07-22 11:04:53 -0700 | [diff] [blame] | 1395 | SkMatrix matrix = *args.fViewMatrix; |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 1396 | matrix.preConcat(localMatrix); |
| 1397 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1398 | // Either we don't stitch tiles, either we have a valid tile size |
| 1399 | SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
| 1400 | |
| Florin Malita | b365cf5 | 2017-05-30 17:18:01 -0400 | [diff] [blame] | 1401 | std::unique_ptr<SkPerlinNoiseShaderImpl::PaintingData> paintingData = |
| 1402 | skstd::make_unique<SkPerlinNoiseShaderImpl::PaintingData>(fTileSize, |
| 1403 | fSeed, |
| 1404 | fBaseFrequencyX, |
| 1405 | fBaseFrequencyY, |
| 1406 | matrix); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1407 | |
| 1408 | SkMatrix m = *args.fViewMatrix; |
| 1409 | m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); |
| 1410 | m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); |
| 1411 | |
| 1412 | if (fType == kImprovedNoise_Type) { |
| Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 1413 | GrSamplerState textureParams(GrSamplerState::WrapMode::kRepeat, |
| 1414 | GrSamplerState::Filter::kNearest); |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1415 | sk_sp<GrTextureProxy> permutationsTexture( |
| 1416 | GrRefCachedBitmapTextureProxy(args.fContext, |
| 1417 | paintingData->getImprovedPermutationsBitmap(), |
| 1418 | textureParams, nullptr)); |
| 1419 | sk_sp<GrTextureProxy> gradientTexture( |
| 1420 | GrRefCachedBitmapTextureProxy(args.fContext, |
| 1421 | paintingData->getGradientBitmap(), |
| 1422 | textureParams, nullptr)); |
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 1423 | return GrImprovedPerlinNoiseEffect::Make(fNumOctaves, fSeed, std::move(paintingData), |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1424 | std::move(permutationsTexture), |
| 1425 | std::move(gradientTexture), m); |
| 1426 | } |
| 1427 | |
| commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 1428 | if (0 == fNumOctaves) { |
| commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 1429 | if (kFractalNoise_Type == fType) { |
| bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 1430 | // 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] | 1431 | // TODO: Either treat the output of this shader as sRGB or allow client to specify a |
| 1432 | // color space of the noise. Either way, this case (and the GLSL) need to convert to |
| 1433 | // the destination. |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1434 | auto inner = |
| 1435 | GrConstColorProcessor::Make(GrColor4f::FromGrColor(0x80404040), |
| Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 1436 | GrConstColorProcessor::InputMode::kModulateRGBA); |
| Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 1437 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
| reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 1438 | } |
| bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 1439 | // Emit zero. |
| Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 1440 | return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(), |
| Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 1441 | GrConstColorProcessor::InputMode::kIgnore); |
| commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1444 | sk_sp<GrTextureProxy> permutationsProxy = GrMakeCachedBitmapProxy( |
| Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 1445 | args.fContext->contextPriv().proxyProvider(), |
| 1446 | paintingData->getPermutationsBitmap()); |
| 1447 | sk_sp<GrTextureProxy> noiseProxy = GrMakeCachedBitmapProxy( |
| 1448 | args.fContext->contextPriv().proxyProvider(), |
| 1449 | paintingData->getNoiseBitmap()); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1450 | |
| Robert Phillips | 6f9f7eb | 2017-02-18 15:15:51 -0500 | [diff] [blame] | 1451 | if (permutationsProxy && noiseProxy) { |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 1452 | auto inner = GrPerlinNoise2Effect::Make(fType, |
| 1453 | fNumOctaves, |
| 1454 | fStitchTiles, |
| 1455 | std::move(paintingData), |
| 1456 | std::move(permutationsProxy), |
| 1457 | std::move(noiseProxy), |
| 1458 | m); |
| Mike Reed | 28eaed2 | 2018-02-01 11:24:53 -0500 | [diff] [blame] | 1459 | return GrFragmentProcessor::MulChildByInputAlpha(std::move(inner)); |
| senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 1460 | } |
| bsalomon | c21b09e | 2015-08-28 18:46:56 -0700 | [diff] [blame] | 1461 | return nullptr; |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | #endif |
| 1465 | |
| commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 1466 | #ifndef SK_IGNORE_TO_STRING |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1467 | void SkPerlinNoiseShaderImpl::toString(SkString* str) const { |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1468 | str->append("SkPerlinNoiseShaderImpl: ("); |
| sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1469 | |
| 1470 | str->append("type: "); |
| 1471 | switch (fType) { |
| 1472 | case kFractalNoise_Type: |
| 1473 | str->append("\"fractal noise\""); |
| 1474 | break; |
| 1475 | case kTurbulence_Type: |
| 1476 | str->append("\"turbulence\""); |
| 1477 | break; |
| 1478 | default: |
| 1479 | str->append("\"unknown\""); |
| 1480 | break; |
| 1481 | } |
| 1482 | str->append(" base frequency: ("); |
| 1483 | str->appendScalar(fBaseFrequencyX); |
| 1484 | str->append(", "); |
| 1485 | str->appendScalar(fBaseFrequencyY); |
| 1486 | str->append(") number of octaves: "); |
| 1487 | str->appendS32(fNumOctaves); |
| 1488 | str->append(" seed: "); |
| 1489 | str->appendScalar(fSeed); |
| 1490 | str->append(" stitch tiles: "); |
| 1491 | str->append(fStitchTiles ? "true " : "false "); |
| 1492 | |
| 1493 | this->INHERITED::toString(str); |
| 1494 | |
| 1495 | str->append(")"); |
| 1496 | } |
| 1497 | #endif |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1498 | |
| Mike Reed | f2ae2b2 | 2017-05-30 15:22:54 -0400 | [diff] [blame] | 1499 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1500 | |
| 1501 | sk_sp<SkShader> SkPerlinNoiseShader::MakeFractalNoise(SkScalar baseFrequencyX, |
| 1502 | SkScalar baseFrequencyY, |
| 1503 | int numOctaves, SkScalar seed, |
| 1504 | const SkISize* tileSize) { |
| 1505 | return sk_sp<SkShader>(new SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::kFractalNoise_Type, |
| 1506 | baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| 1507 | tileSize)); |
| 1508 | } |
| 1509 | |
| 1510 | sk_sp<SkShader> SkPerlinNoiseShader::MakeTurbulence(SkScalar baseFrequencyX, |
| 1511 | SkScalar baseFrequencyY, |
| 1512 | int numOctaves, SkScalar seed, |
| 1513 | const SkISize* tileSize) { |
| 1514 | return sk_sp<SkShader>(new SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::kTurbulence_Type, |
| 1515 | baseFrequencyX, baseFrequencyY, numOctaves, seed, |
| 1516 | tileSize)); |
| 1517 | } |
| 1518 | |
| 1519 | sk_sp<SkShader> SkPerlinNoiseShader::MakeImprovedNoise(SkScalar baseFrequencyX, |
| 1520 | SkScalar baseFrequencyY, |
| 1521 | int numOctaves, SkScalar z) { |
| 1522 | return sk_sp<SkShader>(new SkPerlinNoiseShaderImpl(SkPerlinNoiseShaderImpl::kImprovedNoise_Type, |
| 1523 | baseFrequencyX, baseFrequencyY, numOctaves, z, |
| 1524 | nullptr)); |
| 1525 | } |
| 1526 | |
| Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 1527 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkPerlinNoiseShader) |
| 1528 | SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkPerlinNoiseShaderImpl) |
| 1529 | SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |