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