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