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