blob: c8b04c997521e7f91772db542801ac28264c4fa0 [file] [log] [blame]
sugoi@google.come3b4c502013-04-05 13:47:09 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
sugoi@google.come3b4c502013-04-05 13:47:09 +00008#include "SkPerlinNoiseShader.h"
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +00009#include "SkColorFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000010#include "SkReadBuffer.h"
11#include "SkWriteBuffer.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000012#include "SkShader.h"
13#include "SkUnPreMultiply.h"
14#include "SkString.h"
15
16#if SK_SUPPORT_GPU
17#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000018#include "GrCoordTransform.h"
egdaniel605dd0f2014-11-12 08:35:25 -080019#include "GrInvariantOutput.h"
joshualitteb2a6762014-12-04 11:35:33 -080020#include "SkGr.h"
bsalomonc21b09e2015-08-28 18:46:56 -070021#include "effects/GrConstColorProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080022#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080023#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070024#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080025#include "glsl/GrGLSLUniformHandler.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000026#endif
27
28static const int kBlockSize = 256;
29static const int kBlockMask = kBlockSize - 1;
30static const int kPerlinNoise = 4096;
31static const int kRandMaximum = SK_MaxS32; // 2**31 - 1
32
33namespace {
34
35// noiseValue is the color component's value (or color)
36// limitValue is the maximum perlin noise array index value allowed
37// newValue is the current noise dimension (either width or height)
38inline int checkNoise(int noiseValue, int limitValue, int newValue) {
39 // If the noise value would bring us out of bounds of the current noise array while we are
40 // stiching noise tiles together, wrap the noise around the current dimension of the noise to
41 // stay within the array bounds in a continuous fashion (so that tiling lines are not visible)
42 if (noiseValue >= limitValue) {
43 noiseValue -= newValue;
44 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000045 return noiseValue;
46}
47
48inline SkScalar smoothCurve(SkScalar t) {
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000049 static const SkScalar SK_Scalar3 = 3.0f;
sugoi@google.come3b4c502013-04-05 13:47:09 +000050
51 // returns t * t * (3 - 2 * t)
52 return SkScalarMul(SkScalarSquare(t), SK_Scalar3 - 2 * t);
53}
54
55} // end namespace
56
57struct SkPerlinNoiseShader::StitchData {
58 StitchData()
59 : fWidth(0)
60 , fWrapX(0)
61 , fHeight(0)
62 , fWrapY(0)
63 {}
64
65 bool operator==(const StitchData& other) const {
66 return fWidth == other.fWidth &&
67 fWrapX == other.fWrapX &&
68 fHeight == other.fHeight &&
69 fWrapY == other.fWrapY;
70 }
71
72 int fWidth; // How much to subtract to wrap for stitching.
73 int fWrapX; // Minimum value to wrap.
74 int fHeight;
75 int fWrapY;
76};
77
78struct SkPerlinNoiseShader::PaintingData {
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000079 PaintingData(const SkISize& tileSize, SkScalar seed,
senorblancoca6a7c22014-06-27 13:35:52 -070080 SkScalar baseFrequencyX, SkScalar baseFrequencyY,
81 const SkMatrix& matrix)
sugoi@google.come3b4c502013-04-05 13:47:09 +000082 {
reed11fa2242015-03-13 06:08:28 -070083 SkVector vec[2] = {
84 { SkScalarInvert(baseFrequencyX), SkScalarInvert(baseFrequencyY) },
85 { SkIntToScalar(tileSize.fWidth), SkIntToScalar(tileSize.fHeight) },
86 };
87 matrix.mapVectors(vec, 2);
88
89 fBaseFrequency.set(SkScalarInvert(vec[0].fX), SkScalarInvert(vec[0].fY));
90 fTileSize.set(SkScalarRoundToInt(vec[1].fX), SkScalarRoundToInt(vec[1].fY));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000091 this->init(seed);
92 if (!fTileSize.isEmpty()) {
93 this->stitch();
94 }
95
senorblancof3b50272014-06-16 10:49:58 -070096#if SK_SUPPORT_GPU
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000097 fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000098 fPermutationsBitmap.setPixels(fLatticeSelector);
99
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000100 fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000101 fNoiseBitmap.setPixels(fNoise[0][0]);
102#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000103 }
104
105 int fSeed;
106 uint8_t fLatticeSelector[kBlockSize];
107 uint16_t fNoise[4][kBlockSize][2];
108 SkPoint fGradient[4][kBlockSize];
109 SkISize fTileSize;
110 SkVector fBaseFrequency;
111 StitchData fStitchDataInit;
112
113private:
114
senorblancof3b50272014-06-16 10:49:58 -0700115#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000116 SkBitmap fPermutationsBitmap;
117 SkBitmap fNoiseBitmap;
118#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000119
120 inline int random() {
121 static const int gRandAmplitude = 16807; // 7**5; primitive root of m
122 static const int gRandQ = 127773; // m / a
123 static const int gRandR = 2836; // m % a
124
125 int result = gRandAmplitude * (fSeed % gRandQ) - gRandR * (fSeed / gRandQ);
126 if (result <= 0)
127 result += kRandMaximum;
128 fSeed = result;
129 return result;
130 }
131
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000132 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000133 void init(SkScalar seed)
134 {
135 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
136
senorblanco@chromium.org857e3202014-01-09 17:41:42 +0000137 // According to the SVG spec, we must truncate (not round) the seed value.
138 fSeed = SkScalarTruncToInt(seed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000139 // The seed value clamp to the range [1, kRandMaximum - 1].
sugoi@google.come3b4c502013-04-05 13:47:09 +0000140 if (fSeed <= 0) {
141 fSeed = -(fSeed % (kRandMaximum - 1)) + 1;
142 }
143 if (fSeed > kRandMaximum - 1) {
144 fSeed = kRandMaximum - 1;
145 }
146 for (int channel = 0; channel < 4; ++channel) {
147 for (int i = 0; i < kBlockSize; ++i) {
148 fLatticeSelector[i] = i;
149 fNoise[channel][i][0] = (random() % (2 * kBlockSize));
150 fNoise[channel][i][1] = (random() % (2 * kBlockSize));
151 }
152 }
153 for (int i = kBlockSize - 1; i > 0; --i) {
154 int k = fLatticeSelector[i];
155 int j = random() % kBlockSize;
156 SkASSERT(j >= 0);
157 SkASSERT(j < kBlockSize);
158 fLatticeSelector[i] = fLatticeSelector[j];
159 fLatticeSelector[j] = k;
160 }
161
162 // Perform the permutations now
163 {
164 // Copy noise data
165 uint16_t noise[4][kBlockSize][2];
166 for (int i = 0; i < kBlockSize; ++i) {
167 for (int channel = 0; channel < 4; ++channel) {
168 for (int j = 0; j < 2; ++j) {
169 noise[channel][i][j] = fNoise[channel][i][j];
170 }
171 }
172 }
173 // Do permutations on noise data
174 for (int i = 0; i < kBlockSize; ++i) {
175 for (int channel = 0; channel < 4; ++channel) {
176 for (int j = 0; j < 2; ++j) {
177 fNoise[channel][i][j] = noise[channel][fLatticeSelector[i]][j];
178 }
179 }
180 }
181 }
182
183 // Half of the largest possible value for 16 bit unsigned int
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000184 static const SkScalar gHalfMax16bits = 32767.5f;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000185
186 // Compute gradients from permutated noise data
187 for (int channel = 0; channel < 4; ++channel) {
188 for (int i = 0; i < kBlockSize; ++i) {
189 fGradient[channel][i] = SkPoint::Make(
190 SkScalarMul(SkIntToScalar(fNoise[channel][i][0] - kBlockSize),
191 gInvBlockSizef),
skia.committer@gmail.comcff02432013-04-06 07:01:10 +0000192 SkScalarMul(SkIntToScalar(fNoise[channel][i][1] - kBlockSize),
sugoi@google.come3b4c502013-04-05 13:47:09 +0000193 gInvBlockSizef));
194 fGradient[channel][i].normalize();
195 // Put the normalized gradient back into the noise data
196 fNoise[channel][i][0] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000197 fGradient[channel][i].fX + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000198 fNoise[channel][i][1] = SkScalarRoundToInt(SkScalarMul(
sugoi@google.comd537af52013-06-10 13:59:25 +0000199 fGradient[channel][i].fY + SK_Scalar1, gHalfMax16bits));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000200 }
201 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000202 }
203
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000204 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000205 void stitch() {
206 SkScalar tileWidth = SkIntToScalar(fTileSize.width());
207 SkScalar tileHeight = SkIntToScalar(fTileSize.height());
208 SkASSERT(tileWidth > 0 && tileHeight > 0);
209 // When stitching tiled turbulence, the frequencies must be adjusted
210 // so that the tile borders will be continuous.
211 if (fBaseFrequency.fX) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000212 SkScalar lowFrequencx =
213 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
214 SkScalar highFrequencx =
215 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000216 // BaseFrequency should be non-negative according to the standard.
reed80ea19c2015-05-12 10:37:34 -0700217 if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequency.fX) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000218 fBaseFrequency.fX = lowFrequencx;
219 } else {
220 fBaseFrequency.fX = highFrequencx;
221 }
222 }
223 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000224 SkScalar lowFrequency =
225 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
226 SkScalar highFrequency =
227 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
reed80ea19c2015-05-12 10:37:34 -0700228 if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequency.fY) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000229 fBaseFrequency.fY = lowFrequency;
230 } else {
231 fBaseFrequency.fY = highFrequency;
232 }
233 }
234 // Set up TurbulenceInitial stitch values.
235 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000236 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000237 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
238 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000239 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000240 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
241 }
242
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000243public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000244
senorblancof3b50272014-06-16 10:49:58 -0700245#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000246 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
247
248 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
249#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000250};
251
reedfe630452016-03-25 09:08:00 -0700252sk_sp<SkShader> SkPerlinNoiseShader::MakeFractalNoise(SkScalar baseFrequencyX,
253 SkScalar baseFrequencyY,
254 int numOctaves, SkScalar seed,
255 const SkISize* tileSize) {
256 return sk_sp<SkShader>(new SkPerlinNoiseShader(kFractalNoise_Type, baseFrequencyX,
257 baseFrequencyY, numOctaves,
258 seed, tileSize));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000259}
260
reedfe630452016-03-25 09:08:00 -0700261sk_sp<SkShader> SkPerlinNoiseShader::MakeTurbulence(SkScalar baseFrequencyX,
262 SkScalar baseFrequencyY,
263 int numOctaves, SkScalar seed,
264 const SkISize* tileSize) {
265 return sk_sp<SkShader>(new SkPerlinNoiseShader(kTurbulence_Type, baseFrequencyX, baseFrequencyY,
266 numOctaves, seed, tileSize));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000267}
268
269SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
270 SkScalar baseFrequencyX,
271 SkScalar baseFrequencyY,
272 int numOctaves,
273 SkScalar seed,
274 const SkISize* tileSize)
275 : fType(type)
276 , fBaseFrequencyX(baseFrequencyX)
277 , fBaseFrequencyY(baseFrequencyY)
commit-bot@chromium.orgce33d602013-11-25 21:46:31 +0000278 , fNumOctaves(numOctaves > 255 ? 255 : numOctaves/*[0,255] octaves allowed*/)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000279 , fSeed(seed)
halcanary96fcdcc2015-08-27 07:41:13 -0700280 , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize)
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000281 , fStitchTiles(!fTileSize.isEmpty())
sugoi@google.come3b4c502013-04-05 13:47:09 +0000282{
283 SkASSERT(numOctaves >= 0 && numOctaves < 256);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000284}
285
sugoi@google.come3b4c502013-04-05 13:47:09 +0000286SkPerlinNoiseShader::~SkPerlinNoiseShader() {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000287}
288
reed60c9b582016-04-03 09:11:13 -0700289sk_sp<SkFlattenable> SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700290 Type type = (Type)buffer.readInt();
291 SkScalar freqX = buffer.readScalar();
292 SkScalar freqY = buffer.readScalar();
293 int octaves = buffer.readInt();
294 SkScalar seed = buffer.readScalar();
295 SkISize tileSize;
296 tileSize.fWidth = buffer.readInt();
297 tileSize.fHeight = buffer.readInt();
298
299 switch (type) {
300 case kFractalNoise_Type:
reedfe630452016-03-25 09:08:00 -0700301 return SkPerlinNoiseShader::MakeFractalNoise(freqX, freqY, octaves, seed,
reed60c9b582016-04-03 09:11:13 -0700302 &tileSize);
reed9fa60da2014-08-21 07:59:51 -0700303 case kTurbulence_Type:
reedfe630452016-03-25 09:08:00 -0700304 return SkPerlinNoiseShader::MakeTurbulence(freqX, freqY, octaves, seed,
reed60c9b582016-04-03 09:11:13 -0700305 &tileSize);
reed9fa60da2014-08-21 07:59:51 -0700306 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700307 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700308 }
309}
310
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000311void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000312 buffer.writeInt((int) fType);
313 buffer.writeScalar(fBaseFrequencyX);
314 buffer.writeScalar(fBaseFrequencyY);
315 buffer.writeInt(fNumOctaves);
316 buffer.writeScalar(fSeed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000317 buffer.writeInt(fTileSize.fWidth);
318 buffer.writeInt(fTileSize.fHeight);
319}
320
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000321SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700322 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000323 struct Noise {
324 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700325 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000326 SkScalar noisePositionFractionValue;
327 Noise(SkScalar component)
328 {
329 SkScalar position = component + kPerlinNoise;
330 noisePositionIntegerValue = SkScalarFloorToInt(position);
331 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700332 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000333 }
334 };
335 Noise noiseX(noiseVector.x());
336 Noise noiseY(noiseVector.y());
337 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000338 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000339 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000340 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000341 noiseX.noisePositionIntegerValue =
342 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
343 noiseY.noisePositionIntegerValue =
344 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700345 noiseX.nextNoisePositionIntegerValue =
346 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
347 noiseY.nextNoisePositionIntegerValue =
348 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000349 }
350 noiseX.noisePositionIntegerValue &= kBlockMask;
351 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700352 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
353 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
354 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700355 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700356 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700357 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700358 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
359 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
360 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
361 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000362 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
363 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
364 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
365 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
366 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700367 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000368 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700369 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000370 SkScalar a = SkScalarInterp(u, v, sx);
371 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700372 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000373 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700374 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000375 SkScalar b = SkScalarInterp(u, v, sx);
376 return SkScalarInterp(a, b, sy);
377}
378
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000379SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700380 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000381 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
382 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000383 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700384 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000385 }
386 SkScalar turbulenceFunctionResult = 0;
senorblancoca6a7c22014-06-27 13:35:52 -0700387 SkPoint noiseVector(SkPoint::Make(SkScalarMul(point.x(), fPaintingData->fBaseFrequency.fX),
388 SkScalarMul(point.y(), fPaintingData->fBaseFrequency.fY)));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000389 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000390 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700391 SkScalar noise = noise2D(channel, stitchData, noiseVector);
reed80ea19c2015-05-12 10:37:34 -0700392 SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ?
393 noise : SkScalarAbs(noise);
394 turbulenceFunctionResult += numer / ratio;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000395 noiseVector.fX *= 2;
396 noiseVector.fY *= 2;
397 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000398 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000399 // Update stitch values
400 stitchData.fWidth *= 2;
401 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
402 stitchData.fHeight *= 2;
403 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
404 }
405 }
406
407 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
408 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000409 if (perlinNoiseShader.fType == kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000410 turbulenceFunctionResult =
411 SkScalarMul(turbulenceFunctionResult, SK_ScalarHalf) + SK_ScalarHalf;
412 }
413
414 if (channel == 3) { // Scale alpha by paint value
reed80ea19c2015-05-12 10:37:34 -0700415 turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000416 }
417
418 // Clamp result
419 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
420}
421
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000422SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
423 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000424 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000425 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000426 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
427 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
428
429 U8CPU rgba[4];
430 for (int channel = 3; channel >= 0; --channel) {
431 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700432 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000433 }
434 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
435}
436
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000437SkShader::Context* SkPerlinNoiseShader::onCreateContext(const ContextRec& rec,
438 void* storage) const {
halcanary385fe4d2015-08-26 13:07:48 -0700439 return new (storage) PerlinNoiseShaderContext(*this, rec);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000440}
441
reed773ceda2016-03-03 18:18:25 -0800442size_t SkPerlinNoiseShader::onContextSize(const ContextRec&) const {
reeda0cee5f2016-03-04 07:38:11 -0800443 return sizeof(PerlinNoiseShaderContext);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000444}
445
446SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000447 const SkPerlinNoiseShader& shader, const ContextRec& rec)
448 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000449{
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000450 SkMatrix newMatrix = *rec.fMatrix;
reed@google.comb67b8e62014-05-12 18:12:24 +0000451 newMatrix.preConcat(shader.getLocalMatrix());
452 if (rec.fLocalMatrix) {
453 newMatrix.preConcat(*rec.fLocalMatrix);
454 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000455 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
456 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700457 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
halcanary385fe4d2015-08-26 13:07:48 -0700458 fPaintingData = new PaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX,
459 shader.fBaseFrequencyY, newMatrix);
senorblancoca6a7c22014-06-27 13:35:52 -0700460}
461
halcanary385fe4d2015-08-26 13:07:48 -0700462SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000463
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000464void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
465 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000466 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
467 StitchData stitchData;
468 for (int i = 0; i < count; ++i) {
469 result[i] = shade(point, stitchData);
470 point.fX += SK_Scalar1;
471 }
472}
473
sugoi@google.come3b4c502013-04-05 13:47:09 +0000474/////////////////////////////////////////////////////////////////////
475
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000476#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000477
egdaniel64c47282015-11-13 06:54:19 -0800478class GrGLPerlinNoise : public GrGLSLFragmentProcessor {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000479public:
robertphillips9cdb9922016-02-03 12:25:40 -0800480 void emitCode(EmitArgs&) override;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000481
robertphillipsbf536af2016-02-04 06:11:53 -0800482 static inline void GenKey(const GrProcessor&, const GrGLSLCaps&, GrProcessorKeyBuilder*);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000483
wangyixb1daa862015-08-18 11:29:31 -0700484protected:
egdaniel018fb622015-10-28 07:26:40 -0700485 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -0700486
sugoi@google.com4775cba2013-04-17 13:46:56 +0000487private:
egdaniel018fb622015-10-28 07:26:40 -0700488 GrGLSLProgramDataManager::UniformHandle fStitchDataUni;
egdaniel018fb622015-10-28 07:26:40 -0700489 GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni;
senorblancof3b50272014-06-16 10:49:58 -0700490
egdaniel64c47282015-11-13 06:54:19 -0800491 typedef GrGLSLFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000492};
493
494/////////////////////////////////////////////////////////////////////
495
joshualittb0a8a372014-09-23 09:50:21 -0700496class GrPerlinNoiseEffect : public GrFragmentProcessor {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000497public:
bsalomon4a339522015-10-06 08:40:50 -0700498 static GrFragmentProcessor* Create(SkPerlinNoiseShader::Type type,
joshualittb0a8a372014-09-23 09:50:21 -0700499 int numOctaves, bool stitchTiles,
500 SkPerlinNoiseShader::PaintingData* paintingData,
501 GrTexture* permutationsTexture, GrTexture* noiseTexture,
bsalomonc21b09e2015-08-28 18:46:56 -0700502 const SkMatrix& matrix) {
bsalomon4a339522015-10-06 08:40:50 -0700503 return new GrPerlinNoiseEffect(type, numOctaves, stitchTiles, paintingData,
bsalomonc21b09e2015-08-28 18:46:56 -0700504 permutationsTexture, noiseTexture, matrix);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000505 }
506
halcanary385fe4d2015-08-26 13:07:48 -0700507 virtual ~GrPerlinNoiseEffect() { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000508
mtklein36352bf2015-03-25 18:17:31 -0700509 const char* name() const override { return "PerlinNoise"; }
joshualitteb2a6762014-12-04 11:35:33 -0800510
senorblancoca6a7c22014-06-27 13:35:52 -0700511 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000512
senorblancof3b50272014-06-16 10:49:58 -0700513 SkPerlinNoiseShader::Type type() const { return fType; }
514 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700515 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700516 int numOctaves() const { return fNumOctaves; }
517 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); }
senorblancof3b50272014-06-16 10:49:58 -0700518
sugoi@google.come3b4c502013-04-05 13:47:09 +0000519private:
egdaniel57d3b032015-11-13 11:57:27 -0800520 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillipsbf536af2016-02-04 06:11:53 -0800521 return new GrGLPerlinNoise;
wangyixb1daa862015-08-18 11:29:31 -0700522 }
523
egdaniel57d3b032015-11-13 11:57:27 -0800524 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
525 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -0700526 GrGLPerlinNoise::GenKey(*this, caps, b);
527 }
528
mtklein36352bf2015-03-25 18:17:31 -0700529 bool onIsEqual(const GrFragmentProcessor& sBase) const override {
joshualitt49586be2014-09-16 08:21:41 -0700530 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
senorblancof3b50272014-06-16 10:49:58 -0700531 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700532 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700533 fNumOctaves == s.fNumOctaves &&
534 fStitchTiles == s.fStitchTiles &&
senorblancoca6a7c22014-06-27 13:35:52 -0700535 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000536 }
537
mtklein36352bf2015-03-25 18:17:31 -0700538 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel605dd0f2014-11-12 08:35:25 -0800539 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700540 }
541
bsalomon4a339522015-10-06 08:40:50 -0700542 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000543 int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700544 SkPerlinNoiseShader::PaintingData* paintingData,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000545 GrTexture* permutationsTexture, GrTexture* noiseTexture,
bsalomonc21b09e2015-08-28 18:46:56 -0700546 const SkMatrix& matrix)
senorblancof3b50272014-06-16 10:49:58 -0700547 : fType(type)
senorblancof3b50272014-06-16 10:49:58 -0700548 , fNumOctaves(numOctaves)
549 , fStitchTiles(stitchTiles)
sugoi@google.com4775cba2013-04-17 13:46:56 +0000550 , fPermutationsAccess(permutationsTexture)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000551 , fNoiseAccess(noiseTexture)
senorblancoca6a7c22014-06-27 13:35:52 -0700552 , fPaintingData(paintingData) {
joshualitteb2a6762014-12-04 11:35:33 -0800553 this->initClassID<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000554 this->addTextureAccess(&fPermutationsAccess);
555 this->addTextureAccess(&fNoiseAccess);
senorblancoca6a7c22014-06-27 13:35:52 -0700556 fCoordTransform.reset(kLocal_GrCoordSet, matrix);
senorblancof3b50272014-06-16 10:49:58 -0700557 this->addCoordTransform(&fCoordTransform);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000558 }
559
joshualittb0a8a372014-09-23 09:50:21 -0700560 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000561
senorblancof3b50272014-06-16 10:49:58 -0700562 SkPerlinNoiseShader::Type fType;
563 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700564 int fNumOctaves;
565 bool fStitchTiles;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000566 GrTextureAccess fPermutationsAccess;
567 GrTextureAccess fNoiseAccess;
senorblancoca6a7c22014-06-27 13:35:52 -0700568 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000569
sugoi@google.com4775cba2013-04-17 13:46:56 +0000570private:
joshualittb0a8a372014-09-23 09:50:21 -0700571 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000572};
573
574/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700575GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000576
bsalomonc21b09e2015-08-28 18:46:56 -0700577const GrFragmentProcessor* GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700578 int numOctaves = d->fRandom->nextRangeU(2, 10);
579 bool stitchTiles = d->fRandom->nextBool();
580 SkScalar seed = SkIntToScalar(d->fRandom->nextU());
581 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096),
582 d->fRandom->nextRangeU(4, 4096));
583 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f,
584 0.99f);
585 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f,
586 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000587
reedfe630452016-03-25 09:08:00 -0700588 sk_sp<SkShader> shader(d->fRandom->nextBool() ?
589 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
590 stitchTiles ? &tileSize : nullptr) :
591 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
592 stitchTiles ? &tileSize : nullptr));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000593
bsalomonc21b09e2015-08-28 18:46:56 -0700594 return shader->asFragmentProcessor(d->fContext,
595 GrTest::TestMatrix(d->fRandom), nullptr,
bsalomon4a339522015-10-06 08:40:50 -0700596 kNone_SkFilterQuality);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000597}
sugoi@google.com4775cba2013-04-17 13:46:56 +0000598
wangyix7c157a92015-07-22 15:08:53 -0700599void GrGLPerlinNoise::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800600 const GrPerlinNoiseEffect& pne = args.fFp.cast<GrPerlinNoiseEffect>();
601
cdalton85285412016-02-18 12:37:07 -0800602 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -0800603 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
egdaniel4ca2e602015-11-18 08:01:26 -0800604 SkString vCoords = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000605
cdalton5e58cee2016-02-11 12:49:47 -0800606 fBaseFrequencyUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800607 kVec2f_GrSLType, kDefault_GrSLPrecision,
608 "baseFrequency");
609 const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000610
halcanary96fcdcc2015-08-27 07:41:13 -0700611 const char* stitchDataUni = nullptr;
robertphillipsbf536af2016-02-04 06:11:53 -0800612 if (pne.stitchTiles()) {
cdalton5e58cee2016-02-11 12:49:47 -0800613 fStitchDataUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800614 kVec2f_GrSLType, kDefault_GrSLPrecision,
615 "stitchData");
616 stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000617 }
618
sugoi@google.comd537af52013-06-10 13:59:25 +0000619 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
620 const char* chanCoordR = "0.125";
621 const char* chanCoordG = "0.375";
622 const char* chanCoordB = "0.625";
623 const char* chanCoordA = "0.875";
624 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000625 const char* stitchData = "stitchData";
626 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000627 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000628 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700629 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000630 const char* fractVal = "fractVal";
631 const char* uv = "uv";
632 const char* ab = "ab";
633 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700634 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000635 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000636 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
637 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
638 // [-1,1] vector and perform a dot product between that vector and the provided vector.
639 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
640
sugoi@google.comd537af52013-06-10 13:59:25 +0000641 // Add noise function
egdaniel0d3f0612015-10-21 10:45:48 -0700642 static const GrGLSLShaderVar gPerlinNoiseArgs[] = {
643 GrGLSLShaderVar(chanCoord, kFloat_GrSLType),
644 GrGLSLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000645 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000646
egdaniel0d3f0612015-10-21 10:45:48 -0700647 static const GrGLSLShaderVar gPerlinNoiseStitchArgs[] = {
648 GrGLSLShaderVar(chanCoord, kFloat_GrSLType),
649 GrGLSLShaderVar(noiseVec, kVec2f_GrSLType),
650 GrGLSLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000651 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000652
sugoi@google.comd537af52013-06-10 13:59:25 +0000653 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000654
senorblancoce6a3542014-06-12 11:24:19 -0700655 noiseCode.appendf("\tvec4 %s;\n", floorVal);
656 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
657 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
658 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000659
660 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700661 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
662 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000663
664 // Adjust frequencies if we're stitching tiles
robertphillipsbf536af2016-02-04 06:11:53 -0800665 if (pne.stitchTiles()) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000666 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800667 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000668 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800669 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700670 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800671 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700672 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800673 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000674 }
675
676 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700677 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
egdaniel4ca2e602015-11-18 08:01:26 -0800678 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000679
680 // Get permutation for x
681 {
682 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700683 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000684
sugoi@google.comd537af52013-06-10 13:59:25 +0000685 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
egdaniel4ca2e602015-11-18 08:01:26 -0800686 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_str(),
687 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000688 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000689 }
690
691 // Get permutation for x + 1
692 {
693 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700694 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000695
sugoi@google.comd537af52013-06-10 13:59:25 +0000696 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
egdaniel4ca2e602015-11-18 08:01:26 -0800697 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[0], xCoords.c_str(),
698 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000699 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000700 }
701
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000702#if defined(SK_BUILD_FOR_ANDROID)
703 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
704 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
705 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
706 // (or 0.484368 here). The following rounding operation prevents these precision issues from
707 // affecting the result of the noise by making sure that we only have multiples of 1/255.
708 // (Note that 1/255 is about 0.003921569, which is the value used here).
709 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
710 latticeIdx, latticeIdx);
711#endif
712
sugoi@google.come3b4c502013-04-05 13:47:09 +0000713 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700714 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000715
sugoi@google.comd537af52013-06-10 13:59:25 +0000716 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000717 // Compute u, at offset (0,0)
718 {
719 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700720 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000721 noiseCode.appendf("\n\tvec4 %s = ", lattice);
egdaniel4ca2e602015-11-18 08:01:26 -0800722 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
723 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000724 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
725 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000726 }
727
sugoi@google.comd537af52013-06-10 13:59:25 +0000728 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000729 // Compute v, at offset (-1,0)
730 {
731 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700732 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000733 noiseCode.append("\n\tlattice = ");
egdaniel4ca2e602015-11-18 08:01:26 -0800734 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
735 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000736 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
737 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000738 }
739
740 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000741 noiseCode.appendf("\n\tvec2 %s;", ab);
742 noiseCode.appendf("\n\t%s.x = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000743
sugoi@google.comd537af52013-06-10 13:59:25 +0000744 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000745 // Compute v, at offset (-1,-1)
746 {
747 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700748 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000749 noiseCode.append("\n\tlattice = ");
egdaniel4ca2e602015-11-18 08:01:26 -0800750 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
751 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000752 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
753 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000754 }
755
sugoi@google.comd537af52013-06-10 13:59:25 +0000756 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000757 // Compute u, at offset (0,-1)
758 {
759 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700760 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000761 noiseCode.append("\n\tlattice = ");
egdaniel4ca2e602015-11-18 08:01:26 -0800762 fragBuilder->appendTextureLookup(&noiseCode, args.fSamplers[1], latticeCoords.c_str(),
763 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000764 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
765 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000766 }
767
768 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000769 noiseCode.appendf("\n\t%s.y = mix(%s.x, %s.y, %s.x);", ab, uv, uv, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000770 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000771 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000772
sugoi@google.comd537af52013-06-10 13:59:25 +0000773 SkString noiseFuncName;
robertphillipsbf536af2016-02-04 06:11:53 -0800774 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800775 fragBuilder->emitFunction(kFloat_GrSLType,
776 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
777 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000778 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800779 fragBuilder->emitFunction(kFloat_GrSLType,
780 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
781 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000782 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000783
sugoi@google.comd537af52013-06-10 13:59:25 +0000784 // There are rounding errors if the floor operation is not performed here
egdaniel4ca2e602015-11-18 08:01:26 -0800785 fragBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
786 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000787
788 // Clear the color accumulator
egdaniel4ca2e602015-11-18 08:01:26 -0800789 fragBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000790
robertphillipsbf536af2016-02-04 06:11:53 -0800791 if (pne.stitchTiles()) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000792 // Set up TurbulenceInitial stitch values.
robertphillipsbf536af2016-02-04 06:11:53 -0800793 fragBuilder->codeAppendf("vec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000794 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000795
robertphillipsbf536af2016-02-04 06:11:53 -0800796 fragBuilder->codeAppendf("float %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000797
798 // Loop over all octaves
robertphillipsbf536af2016-02-04 06:11:53 -0800799 fragBuilder->codeAppendf("for (int octave = 0; octave < %d; ++octave) {", pne.numOctaves());
sugoi@google.comd537af52013-06-10 13:59:25 +0000800
robertphillipsbf536af2016-02-04 06:11:53 -0800801 fragBuilder->codeAppendf("%s += ", args.fOutputColor);
802 if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800803 fragBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000804 }
robertphillipsbf536af2016-02-04 06:11:53 -0800805 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800806 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000807 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
808 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
809 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
810 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
811 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
812 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
813 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800814 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000815 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
816 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
817 noiseFuncName.c_str(), chanCoordR, noiseVec,
818 noiseFuncName.c_str(), chanCoordG, noiseVec,
819 noiseFuncName.c_str(), chanCoordB, noiseVec,
820 noiseFuncName.c_str(), chanCoordA, noiseVec);
821 }
robertphillipsbf536af2016-02-04 06:11:53 -0800822 if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800823 fragBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000824 }
egdaniel4ca2e602015-11-18 08:01:26 -0800825 fragBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000826
egdaniel4ca2e602015-11-18 08:01:26 -0800827 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
828 fragBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000829
robertphillipsbf536af2016-02-04 06:11:53 -0800830 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800831 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000832 }
egdaniel4ca2e602015-11-18 08:01:26 -0800833 fragBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000834
robertphillipsbf536af2016-02-04 06:11:53 -0800835 if (pne.type() == SkPerlinNoiseShader::kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000836 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
837 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
egdaniel4ca2e602015-11-18 08:01:26 -0800838 fragBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);",
839 args.fOutputColor,args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000840 }
841
sugoi@google.come3b4c502013-04-05 13:47:09 +0000842 // Clamp values
egdaniel4ca2e602015-11-18 08:01:26 -0800843 fragBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000844
845 // Pre-multiply the result
egdaniel4ca2e602015-11-18 08:01:26 -0800846 fragBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
847 args.fOutputColor, args.fOutputColor,
848 args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000849}
850
jvanverthcfc18862015-04-28 08:48:20 -0700851void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700852 GrProcessorKeyBuilder* b) {
853 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000854
bsalomon63e99f72014-07-21 08:03:14 -0700855 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000856
857 key = key << 3; // Make room for next 3 bits
858
859 switch (turbulence.type()) {
860 case SkPerlinNoiseShader::kFractalNoise_Type:
861 key |= 0x1;
862 break;
863 case SkPerlinNoiseShader::kTurbulence_Type:
864 key |= 0x2;
865 break;
866 default:
867 // leave key at 0
868 break;
869 }
870
871 if (turbulence.stitchTiles()) {
872 key |= 0x4; // Flip the 3rd bit if tile stitching is on
873 }
874
bsalomon63e99f72014-07-21 08:03:14 -0700875 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000876}
877
egdaniel018fb622015-10-28 07:26:40 -0700878void GrGLPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman,
879 const GrProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -0700880 INHERITED::onSetData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700881
joshualittb0a8a372014-09-23 09:50:21 -0700882 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000883
884 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700885 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000886
sugoi@google.com4775cba2013-04-17 13:46:56 +0000887 if (turbulence.stitchTiles()) {
888 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700889 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000890 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000891 }
892}
893
sugoi@google.come3b4c502013-04-05 13:47:09 +0000894/////////////////////////////////////////////////////////////////////
bsalomonc21b09e2015-08-28 18:46:56 -0700895const GrFragmentProcessor* SkPerlinNoiseShader::asFragmentProcessor(
896 GrContext* context,
897 const SkMatrix& viewM,
898 const SkMatrix* externalLocalMatrix,
bsalomon4a339522015-10-06 08:40:50 -0700899 SkFilterQuality) const {
bsalomon49f085d2014-09-05 13:34:00 -0700900 SkASSERT(context);
mtklein3f3b3d02014-12-01 11:47:08 -0800901
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000902 SkMatrix localMatrix = this->getLocalMatrix();
903 if (externalLocalMatrix) {
904 localMatrix.preConcat(*externalLocalMatrix);
905 }
906
joshualitt5531d512014-12-17 15:50:11 -0800907 SkMatrix matrix = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700908 matrix.preConcat(localMatrix);
909
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000910 if (0 == fNumOctaves) {
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000911 if (kFractalNoise_Type == fType) {
bsalomonc21b09e2015-08-28 18:46:56 -0700912 // Extract the incoming alpha and emit rgba = (a/4, a/4, a/4, a/2)
913 SkAutoTUnref<const GrFragmentProcessor> inner(
914 GrConstColorProcessor::Create(0x80404040,
915 GrConstColorProcessor::kModulateRGBA_InputMode));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700916 return GrFragmentProcessor::MulOutputByInputAlpha(inner);
reedcff10b22015-03-03 06:41:45 -0800917 }
bsalomonc21b09e2015-08-28 18:46:56 -0700918 // Emit zero.
919 return GrConstColorProcessor::Create(0x0, GrConstColorProcessor::kIgnore_InputMode);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000920 }
921
sugoi@google.come3b4c502013-04-05 13:47:09 +0000922 // Either we don't stitch tiles, either we have a valid tile size
923 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
924
joshualittb0a8a372014-09-23 09:50:21 -0700925 SkPerlinNoiseShader::PaintingData* paintingData =
halcanary385fe4d2015-08-26 13:07:48 -0700926 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix);
bsalomonbcf0a522014-10-08 08:40:09 -0700927 SkAutoTUnref<GrTexture> permutationsTexture(
bsalomonafa95e22015-10-12 10:39:46 -0700928 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(),
929 GrTextureParams::ClampNoFilter()));
bsalomonbcf0a522014-10-08 08:40:09 -0700930 SkAutoTUnref<GrTexture> noiseTexture(
bsalomonafa95e22015-10-12 10:39:46 -0700931 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(),
932 GrTextureParams::ClampNoFilter()));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000933
joshualitt5531d512014-12-17 15:50:11 -0800934 SkMatrix m = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700935 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
936 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
bsalomon49f085d2014-09-05 13:34:00 -0700937 if ((permutationsTexture) && (noiseTexture)) {
bsalomonc21b09e2015-08-28 18:46:56 -0700938 SkAutoTUnref<GrFragmentProcessor> inner(
bsalomon4a339522015-10-06 08:40:50 -0700939 GrPerlinNoiseEffect::Create(fType,
bsalomonc21b09e2015-08-28 18:46:56 -0700940 fNumOctaves,
941 fStitchTiles,
942 paintingData,
943 permutationsTexture, noiseTexture,
944 m));
bsalomonf1b7a1d2015-09-28 06:26:28 -0700945 return GrFragmentProcessor::MulOutputByInputAlpha(inner);
senorblancoca6a7c22014-06-27 13:35:52 -0700946 }
bsalomonc21b09e2015-08-28 18:46:56 -0700947 delete paintingData;
948 return nullptr;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000949}
950
951#endif
952
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000953#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +0000954void SkPerlinNoiseShader::toString(SkString* str) const {
955 str->append("SkPerlinNoiseShader: (");
956
957 str->append("type: ");
958 switch (fType) {
959 case kFractalNoise_Type:
960 str->append("\"fractal noise\"");
961 break;
962 case kTurbulence_Type:
963 str->append("\"turbulence\"");
964 break;
965 default:
966 str->append("\"unknown\"");
967 break;
968 }
969 str->append(" base frequency: (");
970 str->appendScalar(fBaseFrequencyX);
971 str->append(", ");
972 str->appendScalar(fBaseFrequencyY);
973 str->append(") number of octaves: ");
974 str->appendS32(fNumOctaves);
975 str->append(" seed: ");
976 str->appendScalar(fSeed);
977 str->append(" stitch tiles: ");
978 str->append(fStitchTiles ? "true " : "false ");
979
980 this->INHERITED::toString(str);
981
982 str->append(")");
983}
984#endif