blob: 1e02ced89b2dad18cd11eeb8644d38cfbff0f6db [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"
Herb Derby83e939b2017-02-07 14:25:11 -05009
10#include "SkArenaAlloc.h"
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000011#include "SkColorFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkReadBuffer.h"
13#include "SkWriteBuffer.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000014#include "SkShader.h"
15#include "SkUnPreMultiply.h"
16#include "SkString.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
bsalomon@google.com77af6802013-10-02 13:04:56 +000020#include "GrCoordTransform.h"
joshualitteb2a6762014-12-04 11:35:33 -080021#include "SkGr.h"
bsalomonc21b09e2015-08-28 18:46:56 -070022#include "effects/GrConstColorProcessor.h"
egdaniel64c47282015-11-13 06:54:19 -080023#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080024#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070025#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080026#include "glsl/GrGLSLUniformHandler.h"
sugoi@google.come3b4c502013-04-05 13:47:09 +000027#endif
28
29static const int kBlockSize = 256;
30static const int kBlockMask = kBlockSize - 1;
31static const int kPerlinNoise = 4096;
32static const int kRandMaximum = SK_MaxS32; // 2**31 - 1
33
34namespace {
35
36// noiseValue is the color component's value (or color)
37// limitValue is the maximum perlin noise array index value allowed
38// newValue is the current noise dimension (either width or height)
39inline int checkNoise(int noiseValue, int limitValue, int newValue) {
40 // If the noise value would bring us out of bounds of the current noise array while we are
41 // stiching noise tiles together, wrap the noise around the current dimension of the noise to
42 // stay within the array bounds in a continuous fashion (so that tiling lines are not visible)
43 if (noiseValue >= limitValue) {
44 noiseValue -= newValue;
45 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000046 return noiseValue;
47}
48
49inline SkScalar smoothCurve(SkScalar t) {
Mike Reed8be952a2017-02-13 20:44:33 -050050 return t * t * (3 - 2 * t);
sugoi@google.come3b4c502013-04-05 13:47:09 +000051}
52
53} // end namespace
54
55struct 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
76struct SkPerlinNoiseShader::PaintingData {
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000077 PaintingData(const SkISize& tileSize, SkScalar seed,
senorblancoca6a7c22014-06-27 13:35:52 -070078 SkScalar baseFrequencyX, SkScalar baseFrequencyY,
79 const SkMatrix& matrix)
sugoi@google.come3b4c502013-04-05 13:47:09 +000080 {
reed11fa2242015-03-13 06:08:28 -070081 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.orgfd5c9a62014-03-06 15:13:53 +000089 this->init(seed);
90 if (!fTileSize.isEmpty()) {
91 this->stitch();
92 }
93
senorblancof3b50272014-06-16 10:49:58 -070094#if SK_SUPPORT_GPU
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000095 fPermutationsBitmap.setInfo(SkImageInfo::MakeA8(kBlockSize, 1));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000096 fPermutationsBitmap.setPixels(fLatticeSelector);
97
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000098 fNoiseBitmap.setInfo(SkImageInfo::MakeN32Premul(kBlockSize, 4));
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +000099 fNoiseBitmap.setPixels(fNoise[0][0]);
100#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000101 }
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
111private:
112
senorblancof3b50272014-06-16 10:49:58 -0700113#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000114 SkBitmap fPermutationsBitmap;
115 SkBitmap fNoiseBitmap;
116#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000117
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.orgfd5c9a62014-03-06 15:13:53 +0000130 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000131 void init(SkScalar seed)
132 {
133 static const SkScalar gInvBlockSizef = SkScalarInvert(SkIntToScalar(kBlockSize));
134
senorblanco@chromium.org857e3202014-01-09 17:41:42 +0000135 // According to the SVG spec, we must truncate (not round) the seed value.
136 fSeed = SkScalarTruncToInt(seed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000137 // The seed value clamp to the range [1, kRandMaximum - 1].
sugoi@google.come3b4c502013-04-05 13:47:09 +0000138 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.org4b413c82013-11-25 19:44:07 +0000182 static const SkScalar gHalfMax16bits = 32767.5f;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000183
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(
Mike Reed8be952a2017-02-13 20:44:33 -0500188 (fNoise[channel][i][0] - kBlockSize) * gInvBlockSizef,
189 (fNoise[channel][i][1] - kBlockSize) * gInvBlockSizef);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000190 fGradient[channel][i].normalize();
191 // Put the normalized gradient back into the noise data
Mike Reed8be952a2017-02-13 20:44:33 -0500192 fNoise[channel][i][0] = SkScalarRoundToInt(
193 (fGradient[channel][i].fX + 1) * gHalfMax16bits);
194 fNoise[channel][i][1] = SkScalarRoundToInt(
195 (fGradient[channel][i].fY + 1) * gHalfMax16bits);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000196 }
197 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000198 }
199
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000200 // Only called once. Could be part of the constructor.
sugoi@google.come3b4c502013-04-05 13:47:09 +0000201 void stitch() {
202 SkScalar tileWidth = SkIntToScalar(fTileSize.width());
203 SkScalar tileHeight = SkIntToScalar(fTileSize.height());
204 SkASSERT(tileWidth > 0 && tileHeight > 0);
205 // When stitching tiled turbulence, the frequencies must be adjusted
206 // so that the tile borders will be continuous.
207 if (fBaseFrequency.fX) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000208 SkScalar lowFrequencx =
209 SkScalarFloorToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
210 SkScalar highFrequencx =
211 SkScalarCeilToScalar(tileWidth * fBaseFrequency.fX) / tileWidth;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000212 // BaseFrequency should be non-negative according to the standard.
reed80ea19c2015-05-12 10:37:34 -0700213 if (fBaseFrequency.fX / lowFrequencx < highFrequencx / fBaseFrequency.fX) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000214 fBaseFrequency.fX = lowFrequencx;
215 } else {
216 fBaseFrequency.fX = highFrequencx;
217 }
218 }
219 if (fBaseFrequency.fY) {
reed@google.com8015cdd2013-12-18 15:49:32 +0000220 SkScalar lowFrequency =
221 SkScalarFloorToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
222 SkScalar highFrequency =
223 SkScalarCeilToScalar(tileHeight * fBaseFrequency.fY) / tileHeight;
reed80ea19c2015-05-12 10:37:34 -0700224 if (fBaseFrequency.fY / lowFrequency < highFrequency / fBaseFrequency.fY) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000225 fBaseFrequency.fY = lowFrequency;
226 } else {
227 fBaseFrequency.fY = highFrequency;
228 }
229 }
230 // Set up TurbulenceInitial stitch values.
231 fStitchDataInit.fWidth =
reed@google.com8015cdd2013-12-18 15:49:32 +0000232 SkScalarRoundToInt(tileWidth * fBaseFrequency.fX);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000233 fStitchDataInit.fWrapX = kPerlinNoise + fStitchDataInit.fWidth;
234 fStitchDataInit.fHeight =
reed@google.com8015cdd2013-12-18 15:49:32 +0000235 SkScalarRoundToInt(tileHeight * fBaseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000236 fStitchDataInit.fWrapY = kPerlinNoise + fStitchDataInit.fHeight;
237 }
238
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000239public:
sugoi@google.come3b4c502013-04-05 13:47:09 +0000240
senorblancof3b50272014-06-16 10:49:58 -0700241#if SK_SUPPORT_GPU
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000242 const SkBitmap& getPermutationsBitmap() const { return fPermutationsBitmap; }
243
244 const SkBitmap& getNoiseBitmap() const { return fNoiseBitmap; }
245#endif
sugoi@google.come3b4c502013-04-05 13:47:09 +0000246};
247
reedfe630452016-03-25 09:08:00 -0700248sk_sp<SkShader> SkPerlinNoiseShader::MakeFractalNoise(SkScalar baseFrequencyX,
249 SkScalar baseFrequencyY,
250 int numOctaves, SkScalar seed,
251 const SkISize* tileSize) {
252 return sk_sp<SkShader>(new SkPerlinNoiseShader(kFractalNoise_Type, baseFrequencyX,
253 baseFrequencyY, numOctaves,
254 seed, tileSize));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000255}
256
reedfe630452016-03-25 09:08:00 -0700257sk_sp<SkShader> SkPerlinNoiseShader::MakeTurbulence(SkScalar baseFrequencyX,
258 SkScalar baseFrequencyY,
259 int numOctaves, SkScalar seed,
260 const SkISize* tileSize) {
261 return sk_sp<SkShader>(new SkPerlinNoiseShader(kTurbulence_Type, baseFrequencyX, baseFrequencyY,
262 numOctaves, seed, tileSize));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000263}
264
265SkPerlinNoiseShader::SkPerlinNoiseShader(SkPerlinNoiseShader::Type type,
266 SkScalar baseFrequencyX,
267 SkScalar baseFrequencyY,
268 int numOctaves,
269 SkScalar seed,
270 const SkISize* tileSize)
271 : fType(type)
272 , fBaseFrequencyX(baseFrequencyX)
273 , fBaseFrequencyY(baseFrequencyY)
robertphillips6a16fd32016-06-27 12:26:29 -0700274 , fNumOctaves(SkTPin<int>(numOctaves, 0, 255)) // [0,255] octaves allowed
sugoi@google.come3b4c502013-04-05 13:47:09 +0000275 , fSeed(seed)
halcanary96fcdcc2015-08-27 07:41:13 -0700276 , fTileSize(nullptr == tileSize ? SkISize::Make(0, 0) : *tileSize)
commit-bot@chromium.orgfd5c9a62014-03-06 15:13:53 +0000277 , fStitchTiles(!fTileSize.isEmpty())
sugoi@google.come3b4c502013-04-05 13:47:09 +0000278{
robertphillips6a16fd32016-06-27 12:26:29 -0700279 SkASSERT(fNumOctaves >= 0 && fNumOctaves < 256);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000280}
281
sugoi@google.come3b4c502013-04-05 13:47:09 +0000282SkPerlinNoiseShader::~SkPerlinNoiseShader() {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000283}
284
reed60c9b582016-04-03 09:11:13 -0700285sk_sp<SkFlattenable> SkPerlinNoiseShader::CreateProc(SkReadBuffer& buffer) {
reed9fa60da2014-08-21 07:59:51 -0700286 Type type = (Type)buffer.readInt();
287 SkScalar freqX = buffer.readScalar();
288 SkScalar freqY = buffer.readScalar();
289 int octaves = buffer.readInt();
290 SkScalar seed = buffer.readScalar();
291 SkISize tileSize;
292 tileSize.fWidth = buffer.readInt();
293 tileSize.fHeight = buffer.readInt();
294
295 switch (type) {
296 case kFractalNoise_Type:
reedfe630452016-03-25 09:08:00 -0700297 return SkPerlinNoiseShader::MakeFractalNoise(freqX, freqY, octaves, seed,
reed60c9b582016-04-03 09:11:13 -0700298 &tileSize);
reed9fa60da2014-08-21 07:59:51 -0700299 case kTurbulence_Type:
reedfe630452016-03-25 09:08:00 -0700300 return SkPerlinNoiseShader::MakeTurbulence(freqX, freqY, octaves, seed,
reed60c9b582016-04-03 09:11:13 -0700301 &tileSize);
reed9fa60da2014-08-21 07:59:51 -0700302 default:
halcanary96fcdcc2015-08-27 07:41:13 -0700303 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700304 }
305}
306
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000307void SkPerlinNoiseShader::flatten(SkWriteBuffer& buffer) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000308 buffer.writeInt((int) fType);
309 buffer.writeScalar(fBaseFrequencyX);
310 buffer.writeScalar(fBaseFrequencyY);
311 buffer.writeInt(fNumOctaves);
312 buffer.writeScalar(fSeed);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000313 buffer.writeInt(fTileSize.fWidth);
314 buffer.writeInt(fTileSize.fHeight);
315}
316
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000317SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::noise2D(
senorblancoca6a7c22014-06-27 13:35:52 -0700318 int channel, const StitchData& stitchData, const SkPoint& noiseVector) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000319 struct Noise {
320 int noisePositionIntegerValue;
senorblancoce6a3542014-06-12 11:24:19 -0700321 int nextNoisePositionIntegerValue;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000322 SkScalar noisePositionFractionValue;
323 Noise(SkScalar component)
324 {
325 SkScalar position = component + kPerlinNoise;
326 noisePositionIntegerValue = SkScalarFloorToInt(position);
327 noisePositionFractionValue = position - SkIntToScalar(noisePositionIntegerValue);
senorblancoce6a3542014-06-12 11:24:19 -0700328 nextNoisePositionIntegerValue = noisePositionIntegerValue + 1;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000329 }
330 };
331 Noise noiseX(noiseVector.x());
332 Noise noiseY(noiseVector.y());
333 SkScalar u, v;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000334 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000335 // If stitching, adjust lattice points accordingly.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000336 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000337 noiseX.noisePositionIntegerValue =
338 checkNoise(noiseX.noisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
339 noiseY.noisePositionIntegerValue =
340 checkNoise(noiseY.noisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
senorblancoce6a3542014-06-12 11:24:19 -0700341 noiseX.nextNoisePositionIntegerValue =
342 checkNoise(noiseX.nextNoisePositionIntegerValue, stitchData.fWrapX, stitchData.fWidth);
343 noiseY.nextNoisePositionIntegerValue =
344 checkNoise(noiseY.nextNoisePositionIntegerValue, stitchData.fWrapY, stitchData.fHeight);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000345 }
346 noiseX.noisePositionIntegerValue &= kBlockMask;
347 noiseY.noisePositionIntegerValue &= kBlockMask;
senorblancoce6a3542014-06-12 11:24:19 -0700348 noiseX.nextNoisePositionIntegerValue &= kBlockMask;
349 noiseY.nextNoisePositionIntegerValue &= kBlockMask;
350 int i =
senorblancoca6a7c22014-06-27 13:35:52 -0700351 fPaintingData->fLatticeSelector[noiseX.noisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700352 int j =
senorblancoca6a7c22014-06-27 13:35:52 -0700353 fPaintingData->fLatticeSelector[noiseX.nextNoisePositionIntegerValue];
senorblancoce6a3542014-06-12 11:24:19 -0700354 int b00 = (i + noiseY.noisePositionIntegerValue) & kBlockMask;
355 int b10 = (j + noiseY.noisePositionIntegerValue) & kBlockMask;
356 int b01 = (i + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
357 int b11 = (j + noiseY.nextNoisePositionIntegerValue) & kBlockMask;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000358 SkScalar sx = smoothCurve(noiseX.noisePositionFractionValue);
359 SkScalar sy = smoothCurve(noiseY.noisePositionFractionValue);
360 // This is taken 1:1 from SVG spec: http://www.w3.org/TR/SVG11/filters.html#feTurbulenceElement
361 SkPoint fractionValue = SkPoint::Make(noiseX.noisePositionFractionValue,
362 noiseY.noisePositionFractionValue); // Offset (0,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700363 u = fPaintingData->fGradient[channel][b00].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000364 fractionValue.fX -= SK_Scalar1; // Offset (-1,0)
senorblancoca6a7c22014-06-27 13:35:52 -0700365 v = fPaintingData->fGradient[channel][b10].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000366 SkScalar a = SkScalarInterp(u, v, sx);
367 fractionValue.fY -= SK_Scalar1; // Offset (-1,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700368 v = fPaintingData->fGradient[channel][b11].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000369 fractionValue.fX = noiseX.noisePositionFractionValue; // Offset (0,-1)
senorblancoca6a7c22014-06-27 13:35:52 -0700370 u = fPaintingData->fGradient[channel][b01].dot(fractionValue);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000371 SkScalar b = SkScalarInterp(u, v, sx);
372 return SkScalarInterp(a, b, sy);
373}
374
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000375SkScalar SkPerlinNoiseShader::PerlinNoiseShaderContext::calculateTurbulenceValueForPoint(
senorblancoca6a7c22014-06-27 13:35:52 -0700376 int channel, StitchData& stitchData, const SkPoint& point) const {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000377 const SkPerlinNoiseShader& perlinNoiseShader = static_cast<const SkPerlinNoiseShader&>(fShader);
378 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000379 // Set up TurbulenceInitial stitch values.
senorblancoca6a7c22014-06-27 13:35:52 -0700380 stitchData = fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000381 }
382 SkScalar turbulenceFunctionResult = 0;
Mike Reed8be952a2017-02-13 20:44:33 -0500383 SkPoint noiseVector(SkPoint::Make(point.x() * fPaintingData->fBaseFrequency.fX,
384 point.y() * fPaintingData->fBaseFrequency.fY));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000385 SkScalar ratio = SK_Scalar1;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000386 for (int octave = 0; octave < perlinNoiseShader.fNumOctaves; ++octave) {
senorblancoca6a7c22014-06-27 13:35:52 -0700387 SkScalar noise = noise2D(channel, stitchData, noiseVector);
reed80ea19c2015-05-12 10:37:34 -0700388 SkScalar numer = (perlinNoiseShader.fType == kFractalNoise_Type) ?
389 noise : SkScalarAbs(noise);
390 turbulenceFunctionResult += numer / ratio;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000391 noiseVector.fX *= 2;
392 noiseVector.fY *= 2;
393 ratio *= 2;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000394 if (perlinNoiseShader.fStitchTiles) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000395 // Update stitch values
396 stitchData.fWidth *= 2;
397 stitchData.fWrapX = stitchData.fWidth + kPerlinNoise;
398 stitchData.fHeight *= 2;
399 stitchData.fWrapY = stitchData.fHeight + kPerlinNoise;
400 }
401 }
402
403 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
404 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000405 if (perlinNoiseShader.fType == kFractalNoise_Type) {
Mike Reed8be952a2017-02-13 20:44:33 -0500406 turbulenceFunctionResult = turbulenceFunctionResult * SK_ScalarHalf + SK_ScalarHalf;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000407 }
408
409 if (channel == 3) { // Scale alpha by paint value
reed80ea19c2015-05-12 10:37:34 -0700410 turbulenceFunctionResult *= SkIntToScalar(getPaintAlpha()) / 255;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000411 }
412
413 // Clamp result
414 return SkScalarPin(turbulenceFunctionResult, 0, SK_Scalar1);
415}
416
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000417SkPMColor SkPerlinNoiseShader::PerlinNoiseShaderContext::shade(
418 const SkPoint& point, StitchData& stitchData) const {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000419 SkPoint newPoint;
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000420 fMatrix.mapPoints(&newPoint, &point, 1);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000421 newPoint.fX = SkScalarRoundToScalar(newPoint.fX);
422 newPoint.fY = SkScalarRoundToScalar(newPoint.fY);
423
424 U8CPU rgba[4];
425 for (int channel = 3; channel >= 0; --channel) {
426 rgba[channel] = SkScalarFloorToInt(255 *
senorblancoca6a7c22014-06-27 13:35:52 -0700427 calculateTurbulenceValueForPoint(channel, stitchData, newPoint));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000428 }
429 return SkPreMultiplyARGB(rgba[3], rgba[0], rgba[1], rgba[2]);
430}
431
Herb Derby83e939b2017-02-07 14:25:11 -0500432SkShader::Context* SkPerlinNoiseShader::onMakeContext(
433 const ContextRec& rec, SkArenaAlloc* alloc) const {
434 return alloc->make<PerlinNoiseShaderContext>(*this, rec);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000435}
436
437SkPerlinNoiseShader::PerlinNoiseShaderContext::PerlinNoiseShaderContext(
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000438 const SkPerlinNoiseShader& shader, const ContextRec& rec)
439 : INHERITED(shader, rec)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000440{
Florin Malitabbeb5732017-01-26 16:23:06 -0500441 SkMatrix newMatrix = SkMatrix::Concat(*rec.fMatrix, shader.getLocalMatrix());
reed@google.comb67b8e62014-05-12 18:12:24 +0000442 if (rec.fLocalMatrix) {
443 newMatrix.preConcat(*rec.fLocalMatrix);
444 }
senorblanco@chromium.orga8d95f82014-04-04 14:46:10 +0000445 // This (1,1) translation is due to WebKit's 1 based coordinates for the noise
446 // (as opposed to 0 based, usually). The same adjustment is in the setData() function.
senorblancoca6a7c22014-06-27 13:35:52 -0700447 fMatrix.setTranslate(-newMatrix.getTranslateX() + SK_Scalar1, -newMatrix.getTranslateY() + SK_Scalar1);
halcanary385fe4d2015-08-26 13:07:48 -0700448 fPaintingData = new PaintingData(shader.fTileSize, shader.fSeed, shader.fBaseFrequencyX,
449 shader.fBaseFrequencyY, newMatrix);
senorblancoca6a7c22014-06-27 13:35:52 -0700450}
451
halcanary385fe4d2015-08-26 13:07:48 -0700452SkPerlinNoiseShader::PerlinNoiseShaderContext::~PerlinNoiseShaderContext() { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000453
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000454void SkPerlinNoiseShader::PerlinNoiseShaderContext::shadeSpan(
455 int x, int y, SkPMColor result[], int count) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000456 SkPoint point = SkPoint::Make(SkIntToScalar(x), SkIntToScalar(y));
457 StitchData stitchData;
458 for (int i = 0; i < count; ++i) {
459 result[i] = shade(point, stitchData);
460 point.fX += SK_Scalar1;
461 }
462}
463
sugoi@google.come3b4c502013-04-05 13:47:09 +0000464/////////////////////////////////////////////////////////////////////
465
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000466#if SK_SUPPORT_GPU
sugoi@google.come3b4c502013-04-05 13:47:09 +0000467
egdaniel64c47282015-11-13 06:54:19 -0800468class GrGLPerlinNoise : public GrGLSLFragmentProcessor {
sugoi@google.com4775cba2013-04-17 13:46:56 +0000469public:
robertphillips9cdb9922016-02-03 12:25:40 -0800470 void emitCode(EmitArgs&) override;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000471
Brian Salomon94efbf52016-11-29 13:43:05 -0500472 static inline void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder*);
sugoi@google.com4775cba2013-04-17 13:46:56 +0000473
wangyixb1daa862015-08-18 11:29:31 -0700474protected:
egdaniel018fb622015-10-28 07:26:40 -0700475 void onSetData(const GrGLSLProgramDataManager&, const GrProcessor&) override;
wangyixb1daa862015-08-18 11:29:31 -0700476
sugoi@google.com4775cba2013-04-17 13:46:56 +0000477private:
egdaniel018fb622015-10-28 07:26:40 -0700478 GrGLSLProgramDataManager::UniformHandle fStitchDataUni;
egdaniel018fb622015-10-28 07:26:40 -0700479 GrGLSLProgramDataManager::UniformHandle fBaseFrequencyUni;
senorblancof3b50272014-06-16 10:49:58 -0700480
egdaniel64c47282015-11-13 06:54:19 -0800481 typedef GrGLSLFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000482};
483
484/////////////////////////////////////////////////////////////////////
485
joshualittb0a8a372014-09-23 09:50:21 -0700486class GrPerlinNoiseEffect : public GrFragmentProcessor {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000487public:
Brian Osman32342f02017-03-04 08:12:46 -0500488 static sk_sp<GrFragmentProcessor> Make(GrResourceProvider* resourceProvider,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500489 SkPerlinNoiseShader::Type type,
bungeman06ca8ec2016-06-09 08:01:03 -0700490 int numOctaves, bool stitchTiles,
491 SkPerlinNoiseShader::PaintingData* paintingData,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500492 sk_sp<GrTextureProxy> permutationsProxy,
493 sk_sp<GrTextureProxy> noiseProxy,
bungeman06ca8ec2016-06-09 08:01:03 -0700494 const SkMatrix& matrix) {
495 return sk_sp<GrFragmentProcessor>(
Brian Osman32342f02017-03-04 08:12:46 -0500496 new GrPerlinNoiseEffect(resourceProvider, type, numOctaves, stitchTiles, paintingData,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500497 std::move(permutationsProxy), std::move(noiseProxy), matrix));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000498 }
499
Brian Salomond3b65972017-03-22 12:05:03 -0400500 ~GrPerlinNoiseEffect() override { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000501
mtklein36352bf2015-03-25 18:17:31 -0700502 const char* name() const override { return "PerlinNoise"; }
joshualitteb2a6762014-12-04 11:35:33 -0800503
senorblancoca6a7c22014-06-27 13:35:52 -0700504 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000505
senorblancof3b50272014-06-16 10:49:58 -0700506 SkPerlinNoiseShader::Type type() const { return fType; }
507 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700508 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700509 int numOctaves() const { return fNumOctaves; }
senorblancof3b50272014-06-16 10:49:58 -0700510
sugoi@google.come3b4c502013-04-05 13:47:09 +0000511private:
egdaniel57d3b032015-11-13 11:57:27 -0800512 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillipsbf536af2016-02-04 06:11:53 -0800513 return new GrGLPerlinNoise;
wangyixb1daa862015-08-18 11:29:31 -0700514 }
515
Brian Salomon94efbf52016-11-29 13:43:05 -0500516 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800517 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -0700518 GrGLPerlinNoise::GenKey(*this, caps, b);
519 }
520
mtklein36352bf2015-03-25 18:17:31 -0700521 bool onIsEqual(const GrFragmentProcessor& sBase) const override {
joshualitt49586be2014-09-16 08:21:41 -0700522 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
senorblancof3b50272014-06-16 10:49:58 -0700523 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700524 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700525 fNumOctaves == s.fNumOctaves &&
526 fStitchTiles == s.fStitchTiles &&
senorblancoca6a7c22014-06-27 13:35:52 -0700527 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000528 }
529
Brian Osman32342f02017-03-04 08:12:46 -0500530 GrPerlinNoiseEffect(GrResourceProvider* resourceProvider,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500531 SkPerlinNoiseShader::Type type, int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700532 SkPerlinNoiseShader::PaintingData* paintingData,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500533 sk_sp<GrTextureProxy> permutationsProxy, sk_sp<GrTextureProxy> noiseProxy,
bsalomonc21b09e2015-08-28 18:46:56 -0700534 const SkMatrix& matrix)
Brian Salomon587e08f2017-01-27 10:59:27 -0500535 : INHERITED(kNone_OptimizationFlags)
536 , fType(type)
537 , fCoordTransform(matrix)
538 , fNumOctaves(numOctaves)
539 , fStitchTiles(stitchTiles)
Brian Osman32342f02017-03-04 08:12:46 -0500540 , fPermutationsSampler(resourceProvider, std::move(permutationsProxy))
541 , fNoiseSampler(resourceProvider, std::move(noiseProxy))
Brian Salomon587e08f2017-01-27 10:59:27 -0500542 , fPaintingData(paintingData) {
joshualitteb2a6762014-12-04 11:35:33 -0800543 this->initClassID<GrPerlinNoiseEffect>();
Brian Salomon0bbecb22016-11-17 11:38:22 -0500544 this->addTextureSampler(&fPermutationsSampler);
545 this->addTextureSampler(&fNoiseSampler);
senorblancof3b50272014-06-16 10:49:58 -0700546 this->addCoordTransform(&fCoordTransform);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000547 }
548
joshualittb0a8a372014-09-23 09:50:21 -0700549 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000550
senorblancof3b50272014-06-16 10:49:58 -0700551 SkPerlinNoiseShader::Type fType;
552 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700553 int fNumOctaves;
554 bool fStitchTiles;
Brian Salomon0bbecb22016-11-17 11:38:22 -0500555 TextureSampler fPermutationsSampler;
556 TextureSampler fNoiseSampler;
senorblancoca6a7c22014-06-27 13:35:52 -0700557 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000558
sugoi@google.com4775cba2013-04-17 13:46:56 +0000559private:
joshualittb0a8a372014-09-23 09:50:21 -0700560 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000561};
562
563/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700564GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000565
Hal Canary6f6961e2017-01-31 13:50:44 -0500566#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700567sk_sp<GrFragmentProcessor> GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700568 int numOctaves = d->fRandom->nextRangeU(2, 10);
569 bool stitchTiles = d->fRandom->nextBool();
570 SkScalar seed = SkIntToScalar(d->fRandom->nextU());
571 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096),
572 d->fRandom->nextRangeU(4, 4096));
573 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f,
574 0.99f);
575 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f,
576 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000577
reedfe630452016-03-25 09:08:00 -0700578 sk_sp<SkShader> shader(d->fRandom->nextBool() ?
579 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
580 stitchTiles ? &tileSize : nullptr) :
581 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
582 stitchTiles ? &tileSize : nullptr));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000583
Brian Osman9f532a32016-10-19 11:12:09 -0400584 GrTest::TestAsFPArgs asFPArgs(d);
585 return shader->asFragmentProcessor(asFPArgs.args());
sugoi@google.come3b4c502013-04-05 13:47:09 +0000586}
Hal Canary6f6961e2017-01-31 13:50:44 -0500587#endif
sugoi@google.com4775cba2013-04-17 13:46:56 +0000588
wangyix7c157a92015-07-22 15:08:53 -0700589void GrGLPerlinNoise::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800590 const GrPerlinNoiseEffect& pne = args.fFp.cast<GrPerlinNoiseEffect>();
591
cdalton85285412016-02-18 12:37:07 -0800592 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -0800593 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
bsalomon1a1aa932016-09-12 09:30:36 -0700594 SkString vCoords = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000595
cdalton5e58cee2016-02-11 12:49:47 -0800596 fBaseFrequencyUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800597 kVec2f_GrSLType, kDefault_GrSLPrecision,
598 "baseFrequency");
599 const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000600
halcanary96fcdcc2015-08-27 07:41:13 -0700601 const char* stitchDataUni = nullptr;
robertphillipsbf536af2016-02-04 06:11:53 -0800602 if (pne.stitchTiles()) {
cdalton5e58cee2016-02-11 12:49:47 -0800603 fStitchDataUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800604 kVec2f_GrSLType, kDefault_GrSLPrecision,
605 "stitchData");
606 stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000607 }
608
sugoi@google.comd537af52013-06-10 13:59:25 +0000609 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
610 const char* chanCoordR = "0.125";
611 const char* chanCoordG = "0.375";
612 const char* chanCoordB = "0.625";
613 const char* chanCoordA = "0.875";
614 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000615 const char* stitchData = "stitchData";
616 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000617 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000618 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700619 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000620 const char* fractVal = "fractVal";
621 const char* uv = "uv";
622 const char* ab = "ab";
623 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700624 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000625 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000626 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
627 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
628 // [-1,1] vector and perform a dot product between that vector and the provided vector.
629 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
630
sugoi@google.comd537af52013-06-10 13:59:25 +0000631 // Add noise function
Brian Salomon99938a82016-11-21 13:41:08 -0500632 static const GrShaderVar gPerlinNoiseArgs[] = {
633 GrShaderVar(chanCoord, kFloat_GrSLType),
634 GrShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000635 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000636
Brian Salomon99938a82016-11-21 13:41:08 -0500637 static const GrShaderVar gPerlinNoiseStitchArgs[] = {
638 GrShaderVar(chanCoord, kFloat_GrSLType),
639 GrShaderVar(noiseVec, kVec2f_GrSLType),
640 GrShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000641 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000642
sugoi@google.comd537af52013-06-10 13:59:25 +0000643 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000644
senorblancoce6a3542014-06-12 11:24:19 -0700645 noiseCode.appendf("\tvec4 %s;\n", floorVal);
646 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
647 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
648 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000649
650 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700651 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
652 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000653
654 // Adjust frequencies if we're stitching tiles
robertphillipsbf536af2016-02-04 06:11:53 -0800655 if (pne.stitchTiles()) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000656 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800657 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000658 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800659 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700660 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800661 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700662 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800663 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000664 }
665
666 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700667 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
egdaniel4ca2e602015-11-18 08:01:26 -0800668 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000669
670 // Get permutation for x
671 {
672 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700673 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000674
sugoi@google.comd537af52013-06-10 13:59:25 +0000675 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, latticeIdx);
cdalton3f6f76f2016-04-11 12:18:09 -0700676 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800677 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000678 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000679 }
680
681 // Get permutation for x + 1
682 {
683 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700684 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000685
sugoi@google.comd537af52013-06-10 13:59:25 +0000686 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
cdalton3f6f76f2016-04-11 12:18:09 -0700687 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800688 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000689 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000690 }
691
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000692#if defined(SK_BUILD_FOR_ANDROID)
693 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
694 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
695 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
696 // (or 0.484368 here). The following rounding operation prevents these precision issues from
697 // affecting the result of the noise by making sure that we only have multiples of 1/255.
698 // (Note that 1/255 is about 0.003921569, which is the value used here).
699 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
700 latticeIdx, latticeIdx);
701#endif
702
sugoi@google.come3b4c502013-04-05 13:47:09 +0000703 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700704 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000705
sugoi@google.comd537af52013-06-10 13:59:25 +0000706 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000707 // Compute u, at offset (0,0)
708 {
709 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700710 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000711 noiseCode.appendf("\n\tvec4 %s = ", lattice);
cdalton3f6f76f2016-04-11 12:18:09 -0700712 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800713 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000714 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
715 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000716 }
717
sugoi@google.comd537af52013-06-10 13:59:25 +0000718 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000719 // Compute v, at offset (-1,0)
720 {
721 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700722 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000723 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700724 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800725 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000726 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
727 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000728 }
729
730 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000731 noiseCode.appendf("\n\tvec2 %s;", ab);
732 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 +0000733
sugoi@google.comd537af52013-06-10 13:59:25 +0000734 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000735 // Compute v, at offset (-1,-1)
736 {
737 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700738 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000739 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700740 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800741 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000742 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
743 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000744 }
745
sugoi@google.comd537af52013-06-10 13:59:25 +0000746 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000747 // Compute u, at offset (0,-1)
748 {
749 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700750 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000751 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700752 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800753 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000754 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
755 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000756 }
757
758 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000759 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 +0000760 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000761 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000762
sugoi@google.comd537af52013-06-10 13:59:25 +0000763 SkString noiseFuncName;
robertphillipsbf536af2016-02-04 06:11:53 -0800764 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800765 fragBuilder->emitFunction(kFloat_GrSLType,
766 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
767 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000768 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800769 fragBuilder->emitFunction(kFloat_GrSLType,
770 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
771 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000772 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000773
sugoi@google.comd537af52013-06-10 13:59:25 +0000774 // There are rounding errors if the floor operation is not performed here
egdaniel4ca2e602015-11-18 08:01:26 -0800775 fragBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
776 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000777
778 // Clear the color accumulator
egdaniel4ca2e602015-11-18 08:01:26 -0800779 fragBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000780
robertphillipsbf536af2016-02-04 06:11:53 -0800781 if (pne.stitchTiles()) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000782 // Set up TurbulenceInitial stitch values.
robertphillipsbf536af2016-02-04 06:11:53 -0800783 fragBuilder->codeAppendf("vec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000784 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000785
robertphillipsbf536af2016-02-04 06:11:53 -0800786 fragBuilder->codeAppendf("float %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000787
788 // Loop over all octaves
robertphillipsbf536af2016-02-04 06:11:53 -0800789 fragBuilder->codeAppendf("for (int octave = 0; octave < %d; ++octave) {", pne.numOctaves());
sugoi@google.comd537af52013-06-10 13:59:25 +0000790
robertphillipsbf536af2016-02-04 06:11:53 -0800791 fragBuilder->codeAppendf("%s += ", args.fOutputColor);
792 if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800793 fragBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000794 }
robertphillipsbf536af2016-02-04 06:11:53 -0800795 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800796 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000797 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
798 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
799 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
800 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
801 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
802 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
803 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800804 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000805 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
806 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
807 noiseFuncName.c_str(), chanCoordR, noiseVec,
808 noiseFuncName.c_str(), chanCoordG, noiseVec,
809 noiseFuncName.c_str(), chanCoordB, noiseVec,
810 noiseFuncName.c_str(), chanCoordA, noiseVec);
811 }
robertphillipsbf536af2016-02-04 06:11:53 -0800812 if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800813 fragBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000814 }
egdaniel4ca2e602015-11-18 08:01:26 -0800815 fragBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000816
egdaniel4ca2e602015-11-18 08:01:26 -0800817 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
818 fragBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000819
robertphillipsbf536af2016-02-04 06:11:53 -0800820 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800821 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000822 }
egdaniel4ca2e602015-11-18 08:01:26 -0800823 fragBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000824
robertphillipsbf536af2016-02-04 06:11:53 -0800825 if (pne.type() == SkPerlinNoiseShader::kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000826 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
827 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
egdaniel4ca2e602015-11-18 08:01:26 -0800828 fragBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);",
829 args.fOutputColor,args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000830 }
831
sugoi@google.come3b4c502013-04-05 13:47:09 +0000832 // Clamp values
egdaniel4ca2e602015-11-18 08:01:26 -0800833 fragBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000834
835 // Pre-multiply the result
egdaniel4ca2e602015-11-18 08:01:26 -0800836 fragBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
837 args.fOutputColor, args.fOutputColor,
838 args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000839}
840
Brian Salomon94efbf52016-11-29 13:43:05 -0500841void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700842 GrProcessorKeyBuilder* b) {
843 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000844
bsalomon63e99f72014-07-21 08:03:14 -0700845 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000846
847 key = key << 3; // Make room for next 3 bits
848
849 switch (turbulence.type()) {
850 case SkPerlinNoiseShader::kFractalNoise_Type:
851 key |= 0x1;
852 break;
853 case SkPerlinNoiseShader::kTurbulence_Type:
854 key |= 0x2;
855 break;
856 default:
857 // leave key at 0
858 break;
859 }
860
861 if (turbulence.stitchTiles()) {
862 key |= 0x4; // Flip the 3rd bit if tile stitching is on
863 }
864
bsalomon63e99f72014-07-21 08:03:14 -0700865 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000866}
867
egdaniel018fb622015-10-28 07:26:40 -0700868void GrGLPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman,
869 const GrProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -0700870 INHERITED::onSetData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700871
joshualittb0a8a372014-09-23 09:50:21 -0700872 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000873
874 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700875 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000876
sugoi@google.com4775cba2013-04-17 13:46:56 +0000877 if (turbulence.stitchTiles()) {
878 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700879 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000880 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000881 }
882}
883
sugoi@google.come3b4c502013-04-05 13:47:09 +0000884/////////////////////////////////////////////////////////////////////
brianosman839345d2016-07-22 11:04:53 -0700885sk_sp<GrFragmentProcessor> SkPerlinNoiseShader::asFragmentProcessor(const AsFPArgs& args) const {
886 SkASSERT(args.fContext);
mtklein3f3b3d02014-12-01 11:47:08 -0800887
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000888 SkMatrix localMatrix = this->getLocalMatrix();
brianosman839345d2016-07-22 11:04:53 -0700889 if (args.fLocalMatrix) {
890 localMatrix.preConcat(*args.fLocalMatrix);
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000891 }
892
brianosman839345d2016-07-22 11:04:53 -0700893 SkMatrix matrix = *args.fViewMatrix;
senorblancoca6a7c22014-06-27 13:35:52 -0700894 matrix.preConcat(localMatrix);
895
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000896 if (0 == fNumOctaves) {
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000897 if (kFractalNoise_Type == fType) {
bsalomonc21b09e2015-08-28 18:46:56 -0700898 // Extract the incoming alpha and emit rgba = (a/4, a/4, a/4, a/2)
Brian Osman618d3042016-10-25 10:51:28 -0400899 // TODO: Either treat the output of this shader as sRGB or allow client to specify a
900 // color space of the noise. Either way, this case (and the GLSL) need to convert to
901 // the destination.
bungeman06ca8ec2016-06-09 08:01:03 -0700902 sk_sp<GrFragmentProcessor> inner(
Brian Osman618d3042016-10-25 10:51:28 -0400903 GrConstColorProcessor::Make(GrColor4f::FromGrColor(0x80404040),
bungeman06ca8ec2016-06-09 08:01:03 -0700904 GrConstColorProcessor::kModulateRGBA_InputMode));
905 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
reedcff10b22015-03-03 06:41:45 -0800906 }
bsalomonc21b09e2015-08-28 18:46:56 -0700907 // Emit zero.
Brian Osman618d3042016-10-25 10:51:28 -0400908 return GrConstColorProcessor::Make(GrColor4f::TransparentBlack(),
909 GrConstColorProcessor::kIgnore_InputMode);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000910 }
911
sugoi@google.come3b4c502013-04-05 13:47:09 +0000912 // Either we don't stitch tiles, either we have a valid tile size
913 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
914
joshualittb0a8a372014-09-23 09:50:21 -0700915 SkPerlinNoiseShader::PaintingData* paintingData =
halcanary385fe4d2015-08-26 13:07:48 -0700916 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix);
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500917 sk_sp<GrTextureProxy> permutationsProxy(GrMakeCachedBitmapProxy(
Robert Phillips26c90e02017-03-14 14:39:29 -0400918 args.fContext->resourceProvider(),
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500919 paintingData->getPermutationsBitmap()));
Robert Phillips26c90e02017-03-14 14:39:29 -0400920 sk_sp<GrTextureProxy> noiseProxy(GrMakeCachedBitmapProxy(args.fContext->resourceProvider(),
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500921 paintingData->getNoiseBitmap()));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000922
brianosman839345d2016-07-22 11:04:53 -0700923 SkMatrix m = *args.fViewMatrix;
senorblancoca6a7c22014-06-27 13:35:52 -0700924 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
925 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500926 if (permutationsProxy && noiseProxy) {
bungeman06ca8ec2016-06-09 08:01:03 -0700927 sk_sp<GrFragmentProcessor> inner(
Brian Osman32342f02017-03-04 08:12:46 -0500928 GrPerlinNoiseEffect::Make(args.fContext->resourceProvider(),
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500929 fType,
bungeman06ca8ec2016-06-09 08:01:03 -0700930 fNumOctaves,
931 fStitchTiles,
932 paintingData,
Robert Phillips6f9f7eb2017-02-18 15:15:51 -0500933 std::move(permutationsProxy),
934 std::move(noiseProxy),
bungeman06ca8ec2016-06-09 08:01:03 -0700935 m));
936 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
senorblancoca6a7c22014-06-27 13:35:52 -0700937 }
bsalomonc21b09e2015-08-28 18:46:56 -0700938 delete paintingData;
939 return nullptr;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000940}
941
942#endif
943
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000944#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +0000945void SkPerlinNoiseShader::toString(SkString* str) const {
946 str->append("SkPerlinNoiseShader: (");
947
948 str->append("type: ");
949 switch (fType) {
950 case kFractalNoise_Type:
951 str->append("\"fractal noise\"");
952 break;
953 case kTurbulence_Type:
954 str->append("\"turbulence\"");
955 break;
956 default:
957 str->append("\"unknown\"");
958 break;
959 }
960 str->append(" base frequency: (");
961 str->appendScalar(fBaseFrequencyX);
962 str->append(", ");
963 str->appendScalar(fBaseFrequencyY);
964 str->append(") number of octaves: ");
965 str->appendS32(fNumOctaves);
966 str->append(" seed: ");
967 str->appendScalar(fSeed);
968 str->append(" stitch tiles: ");
969 str->append(fStitchTiles ? "true " : "false ");
970
971 this->INHERITED::toString(str);
972
973 str->append(")");
974}
975#endif