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