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 | |
| 8 | #include "SkDither.h" |
| 9 | #include "SkPerlinNoiseShader.h" |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 10 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 11 | #include "SkReadBuffer.h" |
| 12 | #include "SkWriteBuffer.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 13 | #include "SkShader.h" |
| 14 | #include "SkUnPreMultiply.h" |
| 15 | #include "SkString.h" |
| 16 | |
| 17 | #if SK_SUPPORT_GPU |
| 18 | #include "GrContext.h" |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 19 | #include "GrCoordTransform.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 20 | #include "GrInvariantOutput.h" |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 21 | #include "SkGr.h" |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 22 | #include "gl/GrGLFragmentProcessor.h" |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 23 | #include "gl/builders/GrGLProgramBuilder.h" |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | static const int kBlockSize = 256; |
| 27 | static const int kBlockMask = kBlockSize - 1; |
| 28 | static const int kPerlinNoise = 4096; |
| 29 | static const int kRandMaximum = SK_MaxS32; // 2**31 - 1 |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // noiseValue is the color component's value (or color) |
| 34 | // limitValue is the maximum perlin noise array index value allowed |
| 35 | // newValue is the current noise dimension (either width or height) |
| 36 | inline int checkNoise(int noiseValue, int limitValue, int newValue) { |
| 37 | // If the noise value would bring us out of bounds of the current noise array while we are |
| 38 | // stiching noise tiles together, wrap the noise around the current dimension of the noise to |
| 39 | // stay within the array bounds in a continuous fashion (so that tiling lines are not visible) |
| 40 | if (noiseValue >= limitValue) { |
| 41 | noiseValue -= newValue; |
| 42 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 43 | return noiseValue; |
| 44 | } |
| 45 | |
| 46 | inline SkScalar smoothCurve(SkScalar t) { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 47 | static const SkScalar SK_Scalar3 = 3.0f; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 48 | |
| 49 | // returns t * t * (3 - 2 * t) |
| 50 | return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t); |
| 51 | } |
| 52 | |
| 53 | } // end namespace |
| 54 | |
| 55 | struct SkPerlinNoiseShader::StitchData { |
| 56 | StitchData() |
| 57 | : fWidth(0) |
| 58 | , fWrapX(0) |
| 59 | , fHeight(0) |
| 60 | , fWrapY(0) |
| 61 | {} |
| 62 | |
| 63 | bool operator==(const StitchData& other) const { |
| 64 | return fWidth == other.fWidth && |
| 65 | fWrapX == other.fWrapX && |
| 66 | fHeight == other.fHeight && |
| 67 | fWrapY == other.fWrapY; |
| 68 | } |
| 69 | |
| 70 | int fWidth; // How much to subtract to wrap for stitching. |
| 71 | int fWrapX; // Minimum value to wrap. |
| 72 | int fHeight; |
| 73 | int fWrapY; |
| 74 | }; |
| 75 | |
| 76 | struct SkPerlinNoiseShader::PaintingData { |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 77 | PaintingData(const SkISize& tileSize, SkScalar seed, |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 78 | SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 79 | const SkMatrix& matrix) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 80 | { |
reed | 11fa224 | 2015-03-13 06:08:28 -0700 | [diff] [blame] | 81 | SkVector vec[2] = { |
| 82 | { SkScalarInvert(baseFrequencyX), SkScalarInvert(baseFrequencyY) }, |
| 83 | { SkIntToScalar(tileSize.fWidth), SkIntToScalar(tileSize.fHeight) }, |
| 84 | }; |
| 85 | matrix.mapVectors(vec, 2); |
| 86 | |
| 87 | fBaseFrequency.set(SkScalarInvert(vec[0].fX), SkScalarInvert(vec[0].fY)); |
| 88 | fTileSize.set(SkScalarRoundToInt(vec[1].fX), SkScalarRoundToInt(vec[1].fY)); |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 89 | this->init(seed); |
| 90 | if (!fTileSize.isEmpty()) { |
| 91 | this->stitch(); |
| 92 | } |
| 93 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 94 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 95 | fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1)); |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 96 | fPermutationsBitmap.setPixels(fLatticeSelector); |
| 97 | |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 98 | fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4)); |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 99 | fNoiseBitmap.setPixels(fNoise[0][0]); |
| 100 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int fSeed; |
| 104 | uint8_t fLatticeSelector[kBlockSize]; |
| 105 | uint16_t fNoise[4][kBlockSize][2]; |
| 106 | SkPoint fGradient[4][kBlockSize]; |
| 107 | SkISize fTileSize; |
| 108 | SkVector fBaseFrequency; |
| 109 | StitchData fStitchDataInit; |
| 110 | |
| 111 | private: |
| 112 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 113 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 114 | SkBitmap fPermutationsBitmap; |
| 115 | SkBitmap fNoiseBitmap; |
| 116 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 117 | |
| 118 | inline int random() { |
| 119 | static const int gRandAmplitude = 16807; // 7**5; primitive root of m |
| 120 | static const int gRandQ = 127773; // m / a |
| 121 | static const int gRandR = 2836; // m % a |
| 122 | |
| 123 | int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ); |
| 124 | if (result <= 0) |
| 125 | result += kRandMaximum; |
| 126 | fSeed = result; |
| 127 | return result; |
| 128 | } |
| 129 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 130 | // Only called once. Could be part of the constructor. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 131 | void init(SkScalar seed) |
| 132 | { |
| 133 | static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize)); |
| 134 | |
senorblanco@chromium.org | 857e320 | 2014-01-09 17:41:42 +0000 | [diff] [blame] | 135 | // According to the SVG spec, we must truncate (not round) the seed value. |
| 136 | fSeed = SkScalarTruncToInt(seed); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 137 | // The seed value clamp to the range [1, kRandMaximum - 1]. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 138 | if (fSeed <= 0) { |
| 139 | fSeed = -(fSeed % (kRandMaximum - 1)) + 1; |
| 140 | } |
| 141 | if (fSeed > kRandMaximum - 1) { |
| 142 | fSeed = kRandMaximum - 1; |
| 143 | } |
| 144 | for (int channel = 0; channel < 4; ++channel) { |
| 145 | for (int i = 0; i < kBlockSize; ++i) { |
| 146 | fLatticeSelector[i] = i; |
| 147 | fNoise[channel][i][0] = (random() % (2 * kBlockSize)); |
| 148 | fNoise[channel][i][1] = (random() % (2 * kBlockSize)); |
| 149 | } |
| 150 | } |
| 151 | for (int i = kBlockSize - 1; i > 0; --i) { |
| 152 | int k = fLatticeSelector[i]; |
| 153 | int j = random() % kBlockSize; |
| 154 | SkASSERT(j >= 0); |
| 155 | SkASSERT(j < kBlockSize); |
| 156 | fLatticeSelector[i] = fLatticeSelector[j]; |
| 157 | fLatticeSelector[j] = k; |
| 158 | } |
| 159 | |
| 160 | // Perform the permutations now |
| 161 | { |
| 162 | // Copy noise data |
| 163 | uint16_t noise[4][kBlockSize][2]; |
| 164 | for (int i = 0; i < kBlockSize; ++i) { |
| 165 | for (int channel = 0; channel < 4; ++channel) { |
| 166 | for (int j = 0; j < 2; ++j) { |
| 167 | noise[channel][i][j] = fNoise[channel][i][j]; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | // Do permutations on noise data |
| 172 | for (int i = 0; i < kBlockSize; ++i) { |
| 173 | for (int channel = 0; channel < 4; ++channel) { |
| 174 | for (int j = 0; j < 2; ++j) { |
| 175 | fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j]; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Half of the largest possible value for 16 bit unsigned int |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 182 | static const SkScalar gHalfMax16bits = 32767.5f; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 183 | |
| 184 | // Compute gradients from permutated noise data |
| 185 | for (int channel = 0; channel < 4; ++channel) { |
| 186 | for (int i = 0; i < kBlockSize; ++i) { |
| 187 | fGradient[channel][i] = SkPoint::Make( |
| 188 | SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize), |
| 189 | gInvBlockSizef), |
skia.committer@gmail.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 190 | SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize), |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 191 | gInvBlockSizef)); |
| 192 | fGradient[channel][i].normalize(); |
| 193 | // Put the normalized gradient back into the noise data |
| 194 | fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 195 | fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 196 | fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 197 | fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 200 | } |
| 201 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 202 | // Only called once. Could be part of the constructor. |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 203 | void stitch() { |
| 204 | SkScalar tileWidth = SkIntToScalar(fTileSize.width()); |
| 205 | SkScalar tileHeight = SkIntToScalar(fTileSize.height()); |
| 206 | SkASSERT(tileWidth > 0 && tileHeight > 0); |
| 207 | // When stitching tiled turbulence, the frequencies must be adjusted |
| 208 | // so that the tile borders will be continuous. |
| 209 | if (fBaseFrequency.fX) { |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 210 | SkScalar lowFrequencx = |
| 211 | SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
| 212 | SkScalar highFrequencx = |
| 213 | SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 214 | // BaseFrequency should be non-negative according to the standard. |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 215 | if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequency.fX) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 216 | fBaseFrequency.fX = lowFrequencx; |
| 217 | } else { |
| 218 | fBaseFrequency.fX = highFrequencx; |
| 219 | } |
| 220 | } |
| 221 | if (fBaseFrequency.fY) { |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 222 | SkScalar lowFrequency = |
| 223 | SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
| 224 | SkScalar highFrequency = |
| 225 | SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 226 | if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequency.fY) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 227 | fBaseFrequency.fY = lowFrequency; |
| 228 | } else { |
| 229 | fBaseFrequency.fY = highFrequency; |
| 230 | } |
| 231 | } |
| 232 | // Set up TurbulenceInitial stitch values. |
| 233 | fStitchDataInit.fWidth = |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 234 | SkScalarRoundToInt(tileWidth * fBaseFrequency.fX); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 235 | fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth; |
| 236 | fStitchDataInit.fHeight = |
reed@google.com | 8015cdd | 2013-12-18 15:49:32 +0000 | [diff] [blame] | 237 | SkScalarRoundToInt(tileHeight * fBaseFrequency.fY); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 238 | fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight; |
| 239 | } |
| 240 | |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 241 | public: |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 242 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 243 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 244 | const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; } |
| 245 | |
| 246 | const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; } |
| 247 | #endif |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | SkShader* SkPerlinNoiseShader::CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
| 251 | int numOctaves, SkScalar seed, |
| 252 | const SkISize* tileSize) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 253 | return new SkPerlinNoiseShader(kFractalNoise_Type, baseFrequencyX, baseFrequencyY, numOctaves, |
| 254 | seed, tileSize); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 255 | } |
| 256 | |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 257 | SkShader* SkPerlinNoiseShader::CreateTurbulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 258 | int numOctaves, SkScalar seed, |
| 259 | const SkISize* tileSize) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 260 | return new SkPerlinNoiseShader(kTurbulence_Type, baseFrequencyX, baseFrequencyY, numOctaves, |
| 261 | seed, tileSize); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, |
| 265 | SkScalar baseFrequencyX, |
| 266 | SkScalar baseFrequencyY, |
| 267 | int numOctaves, |
| 268 | SkScalar seed, |
| 269 | const SkISize* tileSize) |
| 270 | : fType(type) |
| 271 | , fBaseFrequencyX(baseFrequencyX) |
| 272 | , fBaseFrequencyY(baseFrequencyY) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 273 | , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 274 | , fSeed(seed) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 275 | , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize) |
commit-bot@chromium.org | fd5c9a6 | 2014-03-06 15:13:53 +0000 | [diff] [blame] | 276 | , fStitchTiles(!fTileSize.isEmpty()) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 277 | { |
| 278 | SkASSERT(numOctaves >= 0 && numOctaves < 256); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 279 | } |
| 280 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 281 | SkPerlinNoiseShader::~SkPerlinNoiseShader() { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 282 | } |
| 283 | |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 284 | SkFlattenable* SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) { |
| 285 | Type type = (Type)buffer.readInt(); |
| 286 | SkScalar freqX = buffer.readScalar(); |
| 287 | SkScalar freqY = buffer.readScalar(); |
| 288 | int octaves = buffer.readInt(); |
| 289 | SkScalar seed = buffer.readScalar(); |
| 290 | SkISize tileSize; |
| 291 | tileSize.fWidth = buffer.readInt(); |
| 292 | tileSize.fHeight = buffer.readInt(); |
| 293 | |
| 294 | switch (type) { |
| 295 | case kFractalNoise_Type: |
| 296 | return SkPerlinNoiseShader::CreateFractalNoise(freqX, freqY, octaves, seed, &tileSize); |
| 297 | case kTurbulence_Type: |
| 298 | return SkPerlinNoiseShader::CreateTubulence(freqX, freqY, octaves, seed, &tileSize); |
| 299 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 300 | return nullptr; |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 304 | void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 305 | buffer.writeInt((int) fType); |
| 306 | buffer.writeScalar(fBaseFrequencyX); |
| 307 | buffer.writeScalar(fBaseFrequencyY); |
| 308 | buffer.writeInt(fNumOctaves); |
| 309 | buffer.writeScalar(fSeed); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 310 | buffer.writeInt(fTileSize.fWidth); |
| 311 | buffer.writeInt(fTileSize.fHeight); |
| 312 | } |
| 313 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 314 | SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 315 | int channel, const StitchData& stitchData, const SkPoint& noiseVector) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 316 | struct Noise { |
| 317 | int noisePositionIntegerValue; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 318 | int nextNoisePositionIntegerValue; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 319 | SkScalar noisePositionFractionValue; |
| 320 | Noise(SkScalar component) |
| 321 | { |
| 322 | SkScalar position = component + kPerlinNoise; |
| 323 | noisePositionIntegerValue = SkScalarFloorToInt(position); |
| 324 | noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 325 | nextNoisePositionIntegerValue = noisePositionIntegerValue + 1; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 326 | } |
| 327 | }; |
| 328 | Noise noiseX(noiseVector.x()); |
| 329 | Noise noiseY(noiseVector.y()); |
| 330 | SkScalar u, v; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 331 | const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 332 | // If stitching, adjust lattice points accordingly. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 333 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 334 | noiseX.noisePositionIntegerValue = |
| 335 | checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 336 | noiseY.noisePositionIntegerValue = |
| 337 | checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 338 | noiseX.nextNoisePositionIntegerValue = |
| 339 | checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth); |
| 340 | noiseY.nextNoisePositionIntegerValue = |
| 341 | checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 342 | } |
| 343 | noiseX.noisePositionIntegerValue &= kBlockMask; |
| 344 | noiseY.noisePositionIntegerValue &= kBlockMask; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 345 | noiseX.nextNoisePositionIntegerValue &= kBlockMask; |
| 346 | noiseY.nextNoisePositionIntegerValue &= kBlockMask; |
| 347 | int i = |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 348 | fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue]; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 349 | int j = |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 350 | fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue]; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 351 | int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 352 | int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask; |
| 353 | int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
| 354 | int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 355 | SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue); |
| 356 | SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue); |
| 357 | // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement |
| 358 | SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue, |
| 359 | noiseY.noisePositionFractionValue); // Offset (0,0) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 360 | u = fPaintingData->fGradient[channel][b00].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 361 | fractionValue.fX -= SK_Scalar1; // Offset (-1,0) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 362 | v = fPaintingData->fGradient[channel][b10].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 363 | SkScalar a = SkScalarInterp(u, v, sx); |
| 364 | fractionValue.fY -= SK_Scalar1; // Offset (-1,-1) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 365 | v = fPaintingData->fGradient[channel][b11].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 366 | fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 367 | u = fPaintingData->fGradient[channel][b01].dot(fractionValue); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 368 | SkScalar b = SkScalarInterp(u, v, sx); |
| 369 | return SkScalarInterp(a, b, sy); |
| 370 | } |
| 371 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 372 | SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint( |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 373 | int channel, StitchData& stitchData, const SkPoint& point) const { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 374 | const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader); |
| 375 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 376 | // Set up TurbulenceInitial stitch values. |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 377 | stitchData = fPaintingData->fStitchDataInit; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 378 | } |
| 379 | SkScalar turbulenceFunctionResult = 0; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 380 | SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX), |
| 381 | SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY))); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 382 | SkScalar ratio = SK_Scalar1; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 383 | for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) { |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 384 | SkScalar noise = noise2D(channel, stitchData, noiseVector); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 385 | SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ? |
| 386 | noise : SkScalarAbs(noise); |
| 387 | turbulenceFunctionResult += numer / ratio; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 388 | noiseVector.fX *= 2; |
| 389 | noiseVector.fY *= 2; |
| 390 | ratio *= 2; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 391 | if (perlinNoiseShader.fStitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 392 | // Update stitch values |
| 393 | stitchData.fWidth *= 2; |
| 394 | stitchData.fWrapX = stitchData.fWidth + kPerlinNoise; |
| 395 | stitchData.fHeight *= 2; |
| 396 | stitchData.fWrapY = stitchData.fHeight + kPerlinNoise; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 401 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 402 | if (perlinNoiseShader.fType == kFractalNoise_Type) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 403 | turbulenceFunctionResult = |
| 404 | SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf; |
| 405 | } |
| 406 | |
| 407 | if (channel == 3) { // Scale alpha by paint value |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 408 | turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Clamp result |
| 412 | return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1); |
| 413 | } |
| 414 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 415 | SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade( |
| 416 | const SkPoint& point, StitchData& stitchData) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 417 | SkPoint newPoint; |
senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 418 | fMatrix.mapPoints(&newPoint, &point, 1); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 419 | newPoint.fX = SkScalarRoundToScalar(newPoint.fX); |
| 420 | newPoint.fY = SkScalarRoundToScalar(newPoint.fY); |
| 421 | |
| 422 | U8CPU rgba[4]; |
| 423 | for (int channel = 3; channel >= 0; --channel) { |
| 424 | rgba[channel] = SkScalarFloorToInt(255 * |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 425 | calculateTurbulenceValueForPoint(channel, stitchData, newPoint)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 426 | } |
| 427 | return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]); |
| 428 | } |
| 429 | |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 430 | SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec, |
| 431 | void* storage) const { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 432 | return new (storage) PerlinNoiseShaderContext(*this, rec); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | size_t SkPerlinNoiseShader::contextSize() const { |
| 436 | return sizeof(PerlinNoiseShaderContext); |
| 437 | } |
| 438 | |
| 439 | SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext( |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 440 | const SkPerlinNoiseShader& shader, const ContextRec& rec) |
| 441 | : INHERITED(shader, rec) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 442 | { |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 443 | SkMatrix newMatrix = *rec.fMatrix; |
reed@google.com | b67b8e6 | 2014-05-12 18:12:24 +0000 | [diff] [blame] | 444 | newMatrix.preConcat(shader.getLocalMatrix()); |
| 445 | if (rec.fLocalMatrix) { |
| 446 | newMatrix.preConcat(*rec.fLocalMatrix); |
| 447 | } |
senorblanco@chromium.org | a8d95f8 | 2014-04-04 14:46:10 +0000 | [diff] [blame] | 448 | // This (1,1) translation is due to WebKit's 1 based coordinates for the noise |
| 449 | // (as opposed to 0 based, usually). The same adjustment is in the setData() function. |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 450 | fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 451 | fPaintingData = new PaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX, |
| 452 | shader.fBaseFrequencyY, newMatrix); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 453 | } |
| 454 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 455 | SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() { delete fPaintingData; } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 456 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 457 | void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan( |
| 458 | int x, int y, SkPMColor result[], int count) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 459 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
| 460 | StitchData stitchData; |
| 461 | for (int i = 0; i < count; ++i) { |
| 462 | result[i] = shade(point, stitchData); |
| 463 | point.fX += SK_Scalar1; |
| 464 | } |
| 465 | } |
| 466 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 467 | void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan16( |
| 468 | int x, int y, uint16_t result[], int count) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 469 | SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y)); |
| 470 | StitchData stitchData; |
| 471 | DITHER_565_SCAN(y); |
| 472 | for (int i = 0; i < count; ++i) { |
| 473 | unsigned dither = DITHER_VALUE(x); |
| 474 | result[i] = SkDitherRGB32To565(shade(point, stitchData), dither); |
| 475 | DITHER_INC_X(x); |
| 476 | point.fX += SK_Scalar1; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | ///////////////////////////////////////////////////////////////////// |
| 481 | |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 482 | #if SK_SUPPORT_GPU |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 483 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 484 | class GrGLPerlinNoise : public GrGLFragmentProcessor { |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 485 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 486 | GrGLPerlinNoise(const GrProcessor&); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 487 | virtual ~GrGLPerlinNoise() {} |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 488 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 489 | virtual void emitCode(EmitArgs&) override; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 490 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 491 | static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder* b); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 492 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 493 | protected: |
| 494 | void onSetData(const GrGLProgramDataManager&, const GrProcessor&) override; |
| 495 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 496 | private: |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 497 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 498 | GrGLProgramDataManager::UniformHandle fStitchDataUni; |
| 499 | SkPerlinNoiseShader::Type fType; |
| 500 | bool fStitchTiles; |
| 501 | int fNumOctaves; |
| 502 | GrGLProgramDataManager::UniformHandle fBaseFrequencyUni; |
| 503 | GrGLProgramDataManager::UniformHandle fAlphaUni; |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 504 | |
| 505 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 506 | typedef GrGLFragmentProcessor INHERITED; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | ///////////////////////////////////////////////////////////////////// |
| 510 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 511 | class GrPerlinNoiseEffect : public GrFragmentProcessor { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 512 | public: |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 513 | static GrFragmentProcessor* Create(GrProcessorDataManager* procDataManager, |
joshualitt | b245605 | 2015-07-08 09:36:59 -0700 | [diff] [blame] | 514 | SkPerlinNoiseShader::Type type, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 515 | int numOctaves, bool stitchTiles, |
| 516 | SkPerlinNoiseShader::PaintingData* paintingData, |
| 517 | GrTexture* permutationsTexture, GrTexture* noiseTexture, |
| 518 | const SkMatrix& matrix, uint8_t alpha) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 519 | return new GrPerlinNoiseEffect(procDataManager, type, numOctaves, stitchTiles, paintingData, |
| 520 | permutationsTexture, noiseTexture, matrix, alpha); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 521 | } |
| 522 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 523 | virtual ~GrPerlinNoiseEffect() { delete fPaintingData; } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 524 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 525 | const char* name() const override { return "PerlinNoise"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 526 | |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 527 | const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 528 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 529 | SkPerlinNoiseShader::Type type() const { return fType; } |
| 530 | bool stitchTiles() const { return fStitchTiles; } |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 531 | const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; } |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 532 | int numOctaves() const { return fNumOctaves; } |
| 533 | const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); } |
| 534 | uint8_t alpha() const { return fAlpha; } |
| 535 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 536 | private: |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 537 | GrGLFragmentProcessor* onCreateGLInstance() const override { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 538 | return new GrGLPerlinNoise(*this); |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 539 | } |
| 540 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 541 | virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, |
| 542 | GrProcessorKeyBuilder* b) const override { |
| 543 | GrGLPerlinNoise::GenKey(*this, caps, b); |
| 544 | } |
| 545 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 546 | bool onIsEqual(const GrFragmentProcessor& sBase) const override { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 547 | const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>(); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 548 | return fType == s.fType && |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 549 | fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency && |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 550 | fNumOctaves == s.fNumOctaves && |
| 551 | fStitchTiles == s.fStitchTiles && |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 552 | fAlpha == s.fAlpha && |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 553 | fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 554 | } |
| 555 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 556 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 557 | inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput); |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 558 | } |
| 559 | |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 560 | GrPerlinNoiseEffect(GrProcessorDataManager*, SkPerlinNoiseShader::Type type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 561 | int numOctaves, bool stitchTiles, |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 562 | SkPerlinNoiseShader::PaintingData* paintingData, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 563 | GrTexture* permutationsTexture, GrTexture* noiseTexture, |
| 564 | const SkMatrix& matrix, uint8_t alpha) |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 565 | : fType(type) |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 566 | , fNumOctaves(numOctaves) |
| 567 | , fStitchTiles(stitchTiles) |
| 568 | , fAlpha(alpha) |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 569 | , fPermutationsAccess(permutationsTexture) |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 570 | , fNoiseAccess(noiseTexture) |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 571 | , fPaintingData(paintingData) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 572 | this->initClassID<GrPerlinNoiseEffect>(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 573 | this->addTextureAccess(&fPermutationsAccess); |
| 574 | this->addTextureAccess(&fNoiseAccess); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 575 | fCoordTransform.reset(kLocal_GrCoordSet, matrix); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 576 | this->addCoordTransform(&fCoordTransform); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 577 | } |
| 578 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 579 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 580 | |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 581 | SkPerlinNoiseShader::Type fType; |
| 582 | GrCoordTransform fCoordTransform; |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 583 | int fNumOctaves; |
| 584 | bool fStitchTiles; |
| 585 | uint8_t fAlpha; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 586 | GrTextureAccess fPermutationsAccess; |
| 587 | GrTextureAccess fNoiseAccess; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 588 | SkPerlinNoiseShader::PaintingData *fPaintingData; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 589 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 590 | private: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 591 | typedef GrFragmentProcessor INHERITED; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
| 594 | ///////////////////////////////////////////////////////////////////// |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 595 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 596 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 597 | GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) { |
| 598 | int numOctaves = d->fRandom->nextRangeU(2, 10); |
| 599 | bool stitchTiles = d->fRandom->nextBool(); |
| 600 | SkScalar seed = SkIntToScalar(d->fRandom->nextU()); |
| 601 | SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096), |
| 602 | d->fRandom->nextRangeU(4, 4096)); |
| 603 | SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f, |
| 604 | 0.99f); |
| 605 | SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f, |
| 606 | 0.99f); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 607 | |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 608 | SkShader* shader = d->fRandom->nextBool() ? |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 609 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 610 | stitchTiles ? &tileSize : nullptr) : |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 611 | SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 612 | stitchTiles ? &tileSize : nullptr); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 613 | |
| 614 | SkPaint paint; |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 615 | GrColor paintColor; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 616 | GrFragmentProcessor* effect; |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 617 | GrPaint grPaint; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 618 | SkAssertResult(shader->asFragmentProcessor(d->fContext, paint, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 619 | GrTest::TestMatrix(d->fRandom), nullptr, |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 620 | &paintColor, grPaint.getProcessorDataManager(), |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 621 | &effect)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 622 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 623 | delete shader; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 624 | |
| 625 | return effect; |
| 626 | } |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 627 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 628 | GrGLPerlinNoise::GrGLPerlinNoise(const GrProcessor& processor) |
| 629 | : fType(processor.cast<GrPerlinNoiseEffect>().type()) |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 630 | , fStitchTiles(processor.cast<GrPerlinNoiseEffect>().stitchTiles()) |
| 631 | , fNumOctaves(processor.cast<GrPerlinNoiseEffect>().numOctaves()) { |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 632 | } |
| 633 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 634 | void GrGLPerlinNoise::emitCode(EmitArgs& args) { |
| 635 | GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder(); |
| 636 | SkString vCoords = fsBuilder->ensureFSCoords2D(args.fCoords, 0); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 637 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 638 | fBaseFrequencyUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 639 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 640 | "baseFrequency"); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 641 | const char* baseFrequencyUni = args.fBuilder->getUniformCStr(fBaseFrequencyUni); |
| 642 | fAlphaUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 643 | kFloat_GrSLType, kDefault_GrSLPrecision, |
| 644 | "alpha"); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 645 | const char* alphaUni = args.fBuilder->getUniformCStr(fAlphaUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 646 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 647 | const char* stitchDataUni = nullptr; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 648 | if (fStitchTiles) { |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 649 | fStitchDataUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 650 | kVec2f_GrSLType, kDefault_GrSLPrecision, |
| 651 | "stitchData"); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 652 | stitchDataUni = args.fBuilder->getUniformCStr(fStitchDataUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 653 | } |
| 654 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 655 | // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8 |
| 656 | const char* chanCoordR = "0.125"; |
| 657 | const char* chanCoordG = "0.375"; |
| 658 | const char* chanCoordB = "0.625"; |
| 659 | const char* chanCoordA = "0.875"; |
| 660 | const char* chanCoord = "chanCoord"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 661 | const char* stitchData = "stitchData"; |
| 662 | const char* ratio = "ratio"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 663 | const char* noiseVec = "noiseVec"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 664 | const char* noiseSmooth = "noiseSmooth"; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 665 | const char* floorVal = "floorVal"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 666 | const char* fractVal = "fractVal"; |
| 667 | const char* uv = "uv"; |
| 668 | const char* ab = "ab"; |
| 669 | const char* latticeIdx = "latticeIdx"; |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 670 | const char* bcoords = "bcoords"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 671 | const char* lattice = "lattice"; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 672 | const char* inc8bit = "0.00390625"; // 1.0 / 256.0 |
| 673 | // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a |
| 674 | // [-1,1] vector and perform a dot product between that vector and the provided vector. |
| 675 | const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);"; |
| 676 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 677 | // Add noise function |
| 678 | static const GrGLShaderVar gPerlinNoiseArgs[] = { |
| 679 | GrGLShaderVar(chanCoord, kFloat_GrSLType), |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 680 | GrGLShaderVar(noiseVec, kVec2f_GrSLType) |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 681 | }; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 682 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 683 | static const GrGLShaderVar gPerlinNoiseStitchArgs[] = { |
| 684 | GrGLShaderVar(chanCoord, kFloat_GrSLType), |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 685 | GrGLShaderVar(noiseVec, kVec2f_GrSLType), |
| 686 | GrGLShaderVar(stitchData, kVec2f_GrSLType) |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 687 | }; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 688 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 689 | SkString noiseCode; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 690 | |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 691 | noiseCode.appendf("\tvec4 %s;\n", floorVal); |
| 692 | noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec); |
| 693 | noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal); |
| 694 | noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 695 | |
| 696 | // smooth curve : t * t * (3 - 2 * t) |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 697 | noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);", |
| 698 | noiseSmooth, fractVal, fractVal, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 699 | |
| 700 | // Adjust frequencies if we're stitching tiles |
| 701 | if (fStitchTiles) { |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 702 | noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }", |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 703 | floorVal, stitchData, floorVal, stitchData); |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 704 | noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }", |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 705 | floorVal, stitchData, floorVal, stitchData); |
| 706 | noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }", |
| 707 | floorVal, stitchData, floorVal, stitchData); |
| 708 | noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }", |
| 709 | floorVal, stitchData, floorVal, stitchData); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | // Get texture coordinates and normalize |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 713 | noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n", |
| 714 | floorVal, floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 715 | |
| 716 | // Get permutation for x |
| 717 | { |
| 718 | SkString xCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 719 | xCoords.appendf("vec2(%s.x, 0.5)", floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 720 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 721 | noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 722 | fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_str(), |
| 723 | kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 724 | noiseCode.append(".r;"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // Get permutation for x + 1 |
| 728 | { |
| 729 | SkString xCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 730 | xCoords.appendf("vec2(%s.z, 0.5)", floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 731 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 732 | noiseCode.appendf("\n\t%s.y = ", latticeIdx); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 733 | fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_str(), |
| 734 | kVec2f_GrSLType); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 735 | noiseCode.append(".r;"); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 736 | } |
| 737 | |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 738 | #if defined(SK_BUILD_FOR_ANDROID) |
| 739 | // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3). |
| 740 | // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit |
| 741 | // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725 |
| 742 | // (or 0.484368 here). The following rounding operation prevents these precision issues from |
| 743 | // affecting the result of the noise by making sure that we only have multiples of 1/255. |
| 744 | // (Note that 1/255 is about 0.003921569, which is the value used here). |
| 745 | noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);", |
| 746 | latticeIdx, latticeIdx); |
| 747 | #endif |
| 748 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 749 | // Get (x,y) coordinates with the permutated x |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 750 | noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 751 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 752 | noiseCode.appendf("\n\n\tvec2 %s;", uv); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 753 | // Compute u, at offset (0,0) |
| 754 | { |
| 755 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 756 | latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 757 | noiseCode.appendf("\n\tvec4 %s = ", lattice); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 758 | fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(), |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 759 | kVec2f_GrSLType); |
| 760 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
| 761 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 762 | } |
| 763 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 764 | noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 765 | // Compute v, at offset (-1,0) |
| 766 | { |
| 767 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 768 | latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 769 | noiseCode.append("\n\tlattice = "); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 770 | fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(), |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 771 | kVec2f_GrSLType); |
| 772 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
| 773 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | // Compute 'a' as a linear interpolation of 'u' and 'v' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 777 | noiseCode.appendf("\n\tvec2 %s;", ab); |
| 778 | noiseCode.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmooth); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 779 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 780 | noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 781 | // Compute v, at offset (-1,-1) |
| 782 | { |
| 783 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 784 | latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 785 | noiseCode.append("\n\tlattice = "); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 786 | fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(), |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 787 | kVec2f_GrSLType); |
| 788 | noiseCode.appendf(".bgra;\n\t%s.y = ", uv); |
| 789 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 790 | } |
| 791 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 792 | noiseCode.appendf("\n\t%s.x += 1.0;", fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 793 | // Compute u, at offset (0,-1) |
| 794 | { |
| 795 | SkString latticeCoords(""); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 796 | latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord); |
commit-bot@chromium.org | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 797 | noiseCode.append("\n\tlattice = "); |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 798 | fsBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(), |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 799 | kVec2f_GrSLType); |
| 800 | noiseCode.appendf(".bgra;\n\t%s.x = ", uv); |
| 801 | noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | // Compute 'b' as a linear interpolation of 'u' and 'v' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 805 | noiseCode.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmooth); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 806 | // Compute the noise as a linear interpolation of 'a' and 'b' |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 807 | noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 808 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 809 | SkString noiseFuncName; |
| 810 | if (fStitchTiles) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 811 | fsBuilder->emitFunction(kFloat_GrSLType, |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 812 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs), |
| 813 | gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 814 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 815 | fsBuilder->emitFunction(kFloat_GrSLType, |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 816 | "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs), |
| 817 | gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 818 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 819 | |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 820 | // There are rounding errors if the floor operation is not performed here |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 821 | fsBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;", |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 822 | noiseVec, vCoords.c_str(), baseFrequencyUni); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 823 | |
| 824 | // Clear the color accumulator |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 825 | fsBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", args.fOutputColor); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 826 | |
| 827 | if (fStitchTiles) { |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 828 | // Set up TurbulenceInitial stitch values. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 829 | fsBuilder->codeAppendf("\n\t\tvec2 %s = %s;", stitchData, stitchDataUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 830 | } |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 831 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 832 | fsBuilder->codeAppendf("\n\t\tfloat %s = 1.0;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 833 | |
| 834 | // Loop over all octaves |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 835 | fsBuilder->codeAppendf("\n\t\tfor (int octave = 0; octave < %d; ++octave) {", fNumOctaves); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 836 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 837 | fsBuilder->codeAppendf("\n\t\t\t%s += ", args.fOutputColor); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 838 | if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 839 | fsBuilder->codeAppend("abs("); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 840 | } |
| 841 | if (fStitchTiles) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 842 | fsBuilder->codeAppendf( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 843 | "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s)," |
| 844 | "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))", |
| 845 | noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData, |
| 846 | noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData, |
| 847 | noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData, |
| 848 | noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData); |
| 849 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 850 | fsBuilder->codeAppendf( |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 851 | "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s)," |
| 852 | "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))", |
| 853 | noiseFuncName.c_str(), chanCoordR, noiseVec, |
| 854 | noiseFuncName.c_str(), chanCoordG, noiseVec, |
| 855 | noiseFuncName.c_str(), chanCoordB, noiseVec, |
| 856 | noiseFuncName.c_str(), chanCoordA, noiseVec); |
| 857 | } |
| 858 | if (fType != SkPerlinNoiseShader::kFractalNoise_Type) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 859 | fsBuilder->codeAppendf(")"); // end of "abs(" |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 860 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 861 | fsBuilder->codeAppendf(" * %s;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 862 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 863 | fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec); |
| 864 | fsBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 865 | |
| 866 | if (fStitchTiles) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 867 | fsBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData); |
sugoi@google.com | d537af5 | 2013-06-10 13:59:25 +0000 | [diff] [blame] | 868 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 869 | fsBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 870 | |
| 871 | if (fType == SkPerlinNoiseShader::kFractalNoise_Type) { |
| 872 | // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2 |
| 873 | // by fractalNoise and (turbulenceFunctionResult) by turbulence. |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 874 | fsBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);", |
| 875 | args.fOutputColor,args.fOutputColor); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 876 | } |
| 877 | |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 878 | fsBuilder->codeAppendf("\n\t\t%s.a *= %s;", args.fOutputColor, alphaUni); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 879 | |
| 880 | // Clamp values |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 881 | fsBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor, args.fOutputColor); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 882 | |
| 883 | // Pre-multiply the result |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 884 | fsBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n", |
wangyix | 7c157a9 | 2015-07-22 15:08:53 -0700 | [diff] [blame] | 885 | args.fOutputColor, args.fOutputColor, |
| 886 | args.fOutputColor, args.fOutputColor); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 887 | } |
| 888 | |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 889 | void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLSLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 890 | GrProcessorKeyBuilder* b) { |
| 891 | const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 892 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 893 | uint32_t key = turbulence.numOctaves(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 894 | |
| 895 | key = key << 3; // Make room for next 3 bits |
| 896 | |
| 897 | switch (turbulence.type()) { |
| 898 | case SkPerlinNoiseShader::kFractalNoise_Type: |
| 899 | key |= 0x1; |
| 900 | break; |
| 901 | case SkPerlinNoiseShader::kTurbulence_Type: |
| 902 | key |= 0x2; |
| 903 | break; |
| 904 | default: |
| 905 | // leave key at 0 |
| 906 | break; |
| 907 | } |
| 908 | |
| 909 | if (turbulence.stitchTiles()) { |
| 910 | key |= 0x4; // Flip the 3rd bit if tile stitching is on |
| 911 | } |
| 912 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 913 | b->add32(key); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 914 | } |
| 915 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 916 | void GrGLPerlinNoise::onSetData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) { |
| 917 | INHERITED::onSetData(pdman, processor); |
senorblanco | f3b5027 | 2014-06-16 10:49:58 -0700 | [diff] [blame] | 918 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 919 | const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>(); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 920 | |
| 921 | const SkVector& baseFrequency = turbulence.baseFrequency(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 922 | pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY); |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 923 | pdman.set1f(fAlphaUni, SkIntToScalar(turbulence.alpha()) / 255); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 924 | |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 925 | if (turbulence.stitchTiles()) { |
| 926 | const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData(); |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 927 | pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth), |
commit-bot@chromium.org | 9839320 | 2013-07-04 18:13:05 +0000 | [diff] [blame] | 928 | SkIntToScalar(stitchData.fHeight)); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 932 | ///////////////////////////////////////////////////////////////////// |
| 933 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 934 | bool SkPerlinNoiseShader::asFragmentProcessor(GrContext* context, const SkPaint& paint, |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 935 | const SkMatrix& viewM, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 936 | const SkMatrix* externalLocalMatrix, |
joshualitt | b245605 | 2015-07-08 09:36:59 -0700 | [diff] [blame] | 937 | GrColor* paintColor, |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 938 | GrProcessorDataManager* procDataManager, |
joshualitt | 8ca93e7 | 2015-07-08 06:51:43 -0700 | [diff] [blame] | 939 | GrFragmentProcessor** fp) const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 940 | SkASSERT(context); |
mtklein | 3f3b3d0 | 2014-12-01 11:47:08 -0800 | [diff] [blame] | 941 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 942 | *paintColor = SkColor2GrColorJustAlpha(paint.getColor()); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 943 | |
commit-bot@chromium.org | 96fb748 | 2014-05-09 20:28:11 +0000 | [diff] [blame] | 944 | SkMatrix localMatrix = this->getLocalMatrix(); |
| 945 | if (externalLocalMatrix) { |
| 946 | localMatrix.preConcat(*externalLocalMatrix); |
| 947 | } |
| 948 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 949 | SkMatrix matrix = viewM; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 950 | matrix.preConcat(localMatrix); |
| 951 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 952 | if (0 == fNumOctaves) { |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 953 | if (kFractalNoise_Type == fType) { |
bsalomon | ea8b55d | 2015-03-04 11:03:52 -0800 | [diff] [blame] | 954 | uint32_t alpha = paint.getAlpha() >> 1; |
| 955 | uint32_t rgb = alpha >> 1; |
| 956 | *paintColor = GrColorPackRGBA(rgb, rgb, rgb, alpha); |
| 957 | } else { |
| 958 | *paintColor = 0; |
reed | cff10b2 | 2015-03-03 06:41:45 -0800 | [diff] [blame] | 959 | } |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 960 | return true; |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 961 | } |
| 962 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 963 | // Either we don't stitch tiles, either we have a valid tile size |
| 964 | SkASSERT(!fStitchTiles || !fTileSize.isEmpty()); |
| 965 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 966 | SkPerlinNoiseShader::PaintingData* paintingData = |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 967 | new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 968 | SkAutoTUnref<GrTexture> permutationsTexture( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 969 | GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(), nullptr)); |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 970 | SkAutoTUnref<GrTexture> noiseTexture( |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 971 | GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(), nullptr)); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 972 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 973 | SkMatrix m = viewM; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 974 | m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1); |
| 975 | m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 976 | if ((permutationsTexture) && (noiseTexture)) { |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 977 | *fp = GrPerlinNoiseEffect::Create(procDataManager, |
joshualitt | b245605 | 2015-07-08 09:36:59 -0700 | [diff] [blame] | 978 | fType, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 979 | fNumOctaves, |
| 980 | fStitchTiles, |
| 981 | paintingData, |
| 982 | permutationsTexture, noiseTexture, |
| 983 | m, paint.getAlpha()); |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 984 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 985 | delete paintingData; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 986 | *fp = nullptr; |
senorblanco | ca6a7c2 | 2014-06-27 13:35:52 -0700 | [diff] [blame] | 987 | } |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 988 | return true; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | #else |
| 992 | |
joshualitt | 5531d51 | 2014-12-17 15:50:11 -0800 | [diff] [blame] | 993 | bool SkPerlinNoiseShader::asFragmentProcessor(GrContext*, const SkPaint&, const SkMatrix&, |
joshualitt | 9cc1775 | 2015-07-09 06:28:14 -0700 | [diff] [blame] | 994 | const SkMatrix*, GrColor*, GrProcessorDataManager*, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 995 | GrFragmentProcessor**) const { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 996 | SkDEBUGFAIL("Should not call in GPU-less build"); |
dandov | 9de5b51 | 2014-06-10 14:38:28 -0700 | [diff] [blame] | 997 | return false; |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | #endif |
| 1001 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 1002 | #ifndef SK_IGNORE_TO_STRING |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1003 | void SkPerlinNoiseShader::toString(SkString* str) const { |
| 1004 | str->append("SkPerlinNoiseShader: ("); |
| 1005 | |
| 1006 | str->append("type: "); |
| 1007 | switch (fType) { |
| 1008 | case kFractalNoise_Type: |
| 1009 | str->append("\"fractal noise\""); |
| 1010 | break; |
| 1011 | case kTurbulence_Type: |
| 1012 | str->append("\"turbulence\""); |
| 1013 | break; |
| 1014 | default: |
| 1015 | str->append("\"unknown\""); |
| 1016 | break; |
| 1017 | } |
| 1018 | str->append(" base frequency: ("); |
| 1019 | str->appendScalar(fBaseFrequencyX); |
| 1020 | str->append(", "); |
| 1021 | str->appendScalar(fBaseFrequencyY); |
| 1022 | str->append(") number of octaves: "); |
| 1023 | str->appendS32(fNumOctaves); |
| 1024 | str->append(" seed: "); |
| 1025 | str->appendScalar(fSeed); |
| 1026 | str->append(" stitch tiles: "); |
| 1027 | str->append(fStitchTiles ? "true " : "false "); |
| 1028 | |
| 1029 | this->INHERITED::toString(str); |
| 1030 | |
| 1031 | str->append(")"); |
| 1032 | } |
| 1033 | #endif |