blob: 1400905bfe61d42c26839599bb8af4faa8d3939f [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:
bungeman06ca8ec2016-06-09 08:01:03 -0700498 static sk_sp<GrFragmentProcessor> Make(SkPerlinNoiseShader::Type type,
499 int numOctaves, bool stitchTiles,
500 SkPerlinNoiseShader::PaintingData* paintingData,
501 GrTexture* permutationsTexture, GrTexture* noiseTexture,
502 const SkMatrix& matrix) {
503 return sk_sp<GrFragmentProcessor>(
504 new GrPerlinNoiseEffect(type, numOctaves, stitchTiles, paintingData,
505 permutationsTexture, noiseTexture, matrix));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000506 }
507
halcanary385fe4d2015-08-26 13:07:48 -0700508 virtual ~GrPerlinNoiseEffect() { delete fPaintingData; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000509
mtklein36352bf2015-03-25 18:17:31 -0700510 const char* name() const override { return "PerlinNoise"; }
joshualitteb2a6762014-12-04 11:35:33 -0800511
senorblancoca6a7c22014-06-27 13:35:52 -0700512 const SkPerlinNoiseShader::StitchData& stitchData() const { return fPaintingData->fStitchDataInit; }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000513
senorblancof3b50272014-06-16 10:49:58 -0700514 SkPerlinNoiseShader::Type type() const { return fType; }
515 bool stitchTiles() const { return fStitchTiles; }
senorblancoca6a7c22014-06-27 13:35:52 -0700516 const SkVector& baseFrequency() const { return fPaintingData->fBaseFrequency; }
senorblancof3b50272014-06-16 10:49:58 -0700517 int numOctaves() const { return fNumOctaves; }
518 const SkMatrix& matrix() const { return fCoordTransform.getMatrix(); }
senorblancof3b50272014-06-16 10:49:58 -0700519
sugoi@google.come3b4c502013-04-05 13:47:09 +0000520private:
egdaniel57d3b032015-11-13 11:57:27 -0800521 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillipsbf536af2016-02-04 06:11:53 -0800522 return new GrGLPerlinNoise;
wangyixb1daa862015-08-18 11:29:31 -0700523 }
524
egdaniel57d3b032015-11-13 11:57:27 -0800525 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
526 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -0700527 GrGLPerlinNoise::GenKey(*this, caps, b);
528 }
529
mtklein36352bf2015-03-25 18:17:31 -0700530 bool onIsEqual(const GrFragmentProcessor& sBase) const override {
joshualitt49586be2014-09-16 08:21:41 -0700531 const GrPerlinNoiseEffect& s = sBase.cast<GrPerlinNoiseEffect>();
senorblancof3b50272014-06-16 10:49:58 -0700532 return fType == s.fType &&
senorblancoca6a7c22014-06-27 13:35:52 -0700533 fPaintingData->fBaseFrequency == s.fPaintingData->fBaseFrequency &&
senorblancof3b50272014-06-16 10:49:58 -0700534 fNumOctaves == s.fNumOctaves &&
535 fStitchTiles == s.fStitchTiles &&
senorblancoca6a7c22014-06-27 13:35:52 -0700536 fPaintingData->fStitchDataInit == s.fPaintingData->fStitchDataInit;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000537 }
538
mtklein36352bf2015-03-25 18:17:31 -0700539 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
egdaniel605dd0f2014-11-12 08:35:25 -0800540 inout->setToUnknown(GrInvariantOutput::kWillNot_ReadInput);
egdaniel1a8ecdf2014-10-03 06:24:12 -0700541 }
542
bsalomon4a339522015-10-06 08:40:50 -0700543 GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000544 int numOctaves, bool stitchTiles,
senorblancoca6a7c22014-06-27 13:35:52 -0700545 SkPerlinNoiseShader::PaintingData* paintingData,
sugoi@google.come3b4c502013-04-05 13:47:09 +0000546 GrTexture* permutationsTexture, GrTexture* noiseTexture,
bsalomonc21b09e2015-08-28 18:46:56 -0700547 const SkMatrix& matrix)
senorblancof3b50272014-06-16 10:49:58 -0700548 : fType(type)
senorblancof3b50272014-06-16 10:49:58 -0700549 , fNumOctaves(numOctaves)
550 , fStitchTiles(stitchTiles)
sugoi@google.com4775cba2013-04-17 13:46:56 +0000551 , fPermutationsAccess(permutationsTexture)
sugoi@google.come3b4c502013-04-05 13:47:09 +0000552 , fNoiseAccess(noiseTexture)
senorblancoca6a7c22014-06-27 13:35:52 -0700553 , fPaintingData(paintingData) {
joshualitteb2a6762014-12-04 11:35:33 -0800554 this->initClassID<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000555 this->addTextureAccess(&fPermutationsAccess);
556 this->addTextureAccess(&fNoiseAccess);
senorblancoca6a7c22014-06-27 13:35:52 -0700557 fCoordTransform.reset(kLocal_GrCoordSet, matrix);
senorblancof3b50272014-06-16 10:49:58 -0700558 this->addCoordTransform(&fCoordTransform);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000559 }
560
joshualittb0a8a372014-09-23 09:50:21 -0700561 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000562
senorblancof3b50272014-06-16 10:49:58 -0700563 SkPerlinNoiseShader::Type fType;
564 GrCoordTransform fCoordTransform;
senorblancof3b50272014-06-16 10:49:58 -0700565 int fNumOctaves;
566 bool fStitchTiles;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000567 GrTextureAccess fPermutationsAccess;
568 GrTextureAccess fNoiseAccess;
senorblancoca6a7c22014-06-27 13:35:52 -0700569 SkPerlinNoiseShader::PaintingData *fPaintingData;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000570
sugoi@google.com4775cba2013-04-17 13:46:56 +0000571private:
joshualittb0a8a372014-09-23 09:50:21 -0700572 typedef GrFragmentProcessor INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000573};
574
575/////////////////////////////////////////////////////////////////////
joshualittb0a8a372014-09-23 09:50:21 -0700576GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrPerlinNoiseEffect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000577
bungeman06ca8ec2016-06-09 08:01:03 -0700578sk_sp<GrFragmentProcessor> GrPerlinNoiseEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700579 int numOctaves = d->fRandom->nextRangeU(2, 10);
580 bool stitchTiles = d->fRandom->nextBool();
581 SkScalar seed = SkIntToScalar(d->fRandom->nextU());
582 SkISize tileSize = SkISize::Make(d->fRandom->nextRangeU(4, 4096),
583 d->fRandom->nextRangeU(4, 4096));
584 SkScalar baseFrequencyX = d->fRandom->nextRangeScalar(0.01f,
585 0.99f);
586 SkScalar baseFrequencyY = d->fRandom->nextRangeScalar(0.01f,
587 0.99f);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000588
reedfe630452016-03-25 09:08:00 -0700589 sk_sp<SkShader> shader(d->fRandom->nextBool() ?
590 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, seed,
591 stitchTiles ? &tileSize : nullptr) :
592 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, seed,
593 stitchTiles ? &tileSize : nullptr));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000594
bsalomonc21b09e2015-08-28 18:46:56 -0700595 return shader->asFragmentProcessor(d->fContext,
596 GrTest::TestMatrix(d->fRandom), nullptr,
brianosman982eb7f2016-06-06 13:10:58 -0700597 kNone_SkFilterQuality, SkSourceGammaTreatment::kRespect);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000598}
sugoi@google.com4775cba2013-04-17 13:46:56 +0000599
wangyix7c157a92015-07-22 15:08:53 -0700600void GrGLPerlinNoise::emitCode(EmitArgs& args) {
robertphillipsbf536af2016-02-04 06:11:53 -0800601 const GrPerlinNoiseEffect& pne = args.fFp.cast<GrPerlinNoiseEffect>();
602
cdalton85285412016-02-18 12:37:07 -0800603 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -0800604 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
egdaniel4ca2e602015-11-18 08:01:26 -0800605 SkString vCoords = fragBuilder->ensureFSCoords2D(args.fCoords, 0);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000606
cdalton5e58cee2016-02-11 12:49:47 -0800607 fBaseFrequencyUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800608 kVec2f_GrSLType, kDefault_GrSLPrecision,
609 "baseFrequency");
610 const char* baseFrequencyUni = uniformHandler->getUniformCStr(fBaseFrequencyUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000611
halcanary96fcdcc2015-08-27 07:41:13 -0700612 const char* stitchDataUni = nullptr;
robertphillipsbf536af2016-02-04 06:11:53 -0800613 if (pne.stitchTiles()) {
cdalton5e58cee2016-02-11 12:49:47 -0800614 fStitchDataUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -0800615 kVec2f_GrSLType, kDefault_GrSLPrecision,
616 "stitchData");
617 stitchDataUni = uniformHandler->getUniformCStr(fStitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000618 }
619
sugoi@google.comd537af52013-06-10 13:59:25 +0000620 // There are 4 lines, so the center of each line is 1/8, 3/8, 5/8 and 7/8
621 const char* chanCoordR = "0.125";
622 const char* chanCoordG = "0.375";
623 const char* chanCoordB = "0.625";
624 const char* chanCoordA = "0.875";
625 const char* chanCoord = "chanCoord";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000626 const char* stitchData = "stitchData";
627 const char* ratio = "ratio";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000628 const char* noiseVec = "noiseVec";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000629 const char* noiseSmooth = "noiseSmooth";
senorblancoce6a3542014-06-12 11:24:19 -0700630 const char* floorVal = "floorVal";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000631 const char* fractVal = "fractVal";
632 const char* uv = "uv";
633 const char* ab = "ab";
634 const char* latticeIdx = "latticeIdx";
senorblancoce6a3542014-06-12 11:24:19 -0700635 const char* bcoords = "bcoords";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000636 const char* lattice = "lattice";
sugoi@google.come3b4c502013-04-05 13:47:09 +0000637 const char* inc8bit = "0.00390625"; // 1.0 / 256.0
638 // This is the math to convert the two 16bit integer packed into rgba 8 bit input into a
639 // [-1,1] vector and perform a dot product between that vector and the provided vector.
640 const char* dotLattice = "dot(((%s.ga + %s.rb * vec2(%s)) * vec2(2.0) - vec2(1.0)), %s);";
641
sugoi@google.comd537af52013-06-10 13:59:25 +0000642 // Add noise function
egdaniel0d3f0612015-10-21 10:45:48 -0700643 static const GrGLSLShaderVar gPerlinNoiseArgs[] = {
644 GrGLSLShaderVar(chanCoord, kFloat_GrSLType),
645 GrGLSLShaderVar(noiseVec, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000646 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000647
egdaniel0d3f0612015-10-21 10:45:48 -0700648 static const GrGLSLShaderVar gPerlinNoiseStitchArgs[] = {
649 GrGLSLShaderVar(chanCoord, kFloat_GrSLType),
650 GrGLSLShaderVar(noiseVec, kVec2f_GrSLType),
651 GrGLSLShaderVar(stitchData, kVec2f_GrSLType)
sugoi@google.comd537af52013-06-10 13:59:25 +0000652 };
sugoi@google.come3b4c502013-04-05 13:47:09 +0000653
sugoi@google.comd537af52013-06-10 13:59:25 +0000654 SkString noiseCode;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000655
senorblancoce6a3542014-06-12 11:24:19 -0700656 noiseCode.appendf("\tvec4 %s;\n", floorVal);
657 noiseCode.appendf("\t%s.xy = floor(%s);\n", floorVal, noiseVec);
658 noiseCode.appendf("\t%s.zw = %s.xy + vec2(1.0);\n", floorVal, floorVal);
659 noiseCode.appendf("\tvec2 %s = fract(%s);\n", fractVal, noiseVec);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000660
661 // smooth curve : t * t * (3 - 2 * t)
senorblancoce6a3542014-06-12 11:24:19 -0700662 noiseCode.appendf("\n\tvec2 %s = %s * %s * (vec2(3.0) - vec2(2.0) * %s);",
663 noiseSmooth, fractVal, fractVal, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000664
665 // Adjust frequencies if we're stitching tiles
robertphillipsbf536af2016-02-04 06:11:53 -0800666 if (pne.stitchTiles()) {
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000667 noiseCode.appendf("\n\tif(%s.x >= %s.x) { %s.x -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800668 floorVal, stitchData, floorVal, stitchData);
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000669 noiseCode.appendf("\n\tif(%s.y >= %s.y) { %s.y -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800670 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700671 noiseCode.appendf("\n\tif(%s.z >= %s.x) { %s.z -= %s.x; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800672 floorVal, stitchData, floorVal, stitchData);
senorblancoce6a3542014-06-12 11:24:19 -0700673 noiseCode.appendf("\n\tif(%s.w >= %s.y) { %s.w -= %s.y; }",
egdaniel4ca2e602015-11-18 08:01:26 -0800674 floorVal, stitchData, floorVal, stitchData);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000675 }
676
677 // Get texture coordinates and normalize
senorblancoce6a3542014-06-12 11:24:19 -0700678 noiseCode.appendf("\n\t%s = fract(floor(mod(%s, 256.0)) / vec4(256.0));\n",
egdaniel4ca2e602015-11-18 08:01:26 -0800679 floorVal, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000680
681 // Get permutation for x
682 {
683 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700684 xCoords.appendf("vec2(%s.x, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000685
sugoi@google.comd537af52013-06-10 13:59:25 +0000686 noiseCode.appendf("\n\tvec2 %s;\n\t%s.x = ", latticeIdx, 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
692 // Get permutation for x + 1
693 {
694 SkString xCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700695 xCoords.appendf("vec2(%s.z, 0.5)", floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000696
sugoi@google.comd537af52013-06-10 13:59:25 +0000697 noiseCode.appendf("\n\t%s.y = ", latticeIdx);
cdalton3f6f76f2016-04-11 12:18:09 -0700698 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[0], xCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800699 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000700 noiseCode.append(".r;");
sugoi@google.come3b4c502013-04-05 13:47:09 +0000701 }
702
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000703#if defined(SK_BUILD_FOR_ANDROID)
704 // Android rounding for Tegra devices, like, for example: Xoom (Tegra 2), Nexus 7 (Tegra 3).
705 // The issue is that colors aren't accurate enough on Tegra devices. For example, if an 8 bit
706 // value of 124 (or 0.486275 here) is entered, we can get a texture value of 123.513725
707 // (or 0.484368 here). The following rounding operation prevents these precision issues from
708 // affecting the result of the noise by making sure that we only have multiples of 1/255.
709 // (Note that 1/255 is about 0.003921569, which is the value used here).
710 noiseCode.appendf("\n\t%s = floor(%s * vec2(255.0) + vec2(0.5)) * vec2(0.003921569);",
711 latticeIdx, latticeIdx);
712#endif
713
sugoi@google.come3b4c502013-04-05 13:47:09 +0000714 // Get (x,y) coordinates with the permutated x
senorblancoce6a3542014-06-12 11:24:19 -0700715 noiseCode.appendf("\n\tvec4 %s = fract(%s.xyxy + %s.yyww);", bcoords, latticeIdx, floorVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000716
sugoi@google.comd537af52013-06-10 13:59:25 +0000717 noiseCode.appendf("\n\n\tvec2 %s;", uv);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000718 // Compute u, at offset (0,0)
719 {
720 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700721 latticeCoords.appendf("vec2(%s.x, %s)", bcoords, chanCoord);
sugoi@google.comd537af52013-06-10 13:59:25 +0000722 noiseCode.appendf("\n\tvec4 %s = ", lattice);
cdalton3f6f76f2016-04-11 12:18:09 -0700723 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800724 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000725 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
726 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000727 }
728
sugoi@google.comd537af52013-06-10 13:59:25 +0000729 noiseCode.appendf("\n\t%s.x -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000730 // Compute v, at offset (-1,0)
731 {
732 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700733 latticeCoords.appendf("vec2(%s.y, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000734 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700735 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800736 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000737 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
738 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000739 }
740
741 // Compute 'a' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000742 noiseCode.appendf("\n\tvec2 %s;", ab);
743 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 +0000744
sugoi@google.comd537af52013-06-10 13:59:25 +0000745 noiseCode.appendf("\n\t%s.y -= 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000746 // Compute v, at offset (-1,-1)
747 {
748 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700749 latticeCoords.appendf("vec2(%s.w, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000750 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700751 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800752 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000753 noiseCode.appendf(".bgra;\n\t%s.y = ", uv);
754 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000755 }
756
sugoi@google.comd537af52013-06-10 13:59:25 +0000757 noiseCode.appendf("\n\t%s.x += 1.0;", fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000758 // Compute u, at offset (0,-1)
759 {
760 SkString latticeCoords("");
senorblancoce6a3542014-06-12 11:24:19 -0700761 latticeCoords.appendf("vec2(%s.z, %s)", bcoords, chanCoord);
commit-bot@chromium.org344cf452013-06-17 14:19:01 +0000762 noiseCode.append("\n\tlattice = ");
cdalton3f6f76f2016-04-11 12:18:09 -0700763 fragBuilder->appendTextureLookup(&noiseCode, args.fTexSamplers[1], latticeCoords.c_str(),
egdaniel4ca2e602015-11-18 08:01:26 -0800764 kVec2f_GrSLType);
sugoi@google.comd537af52013-06-10 13:59:25 +0000765 noiseCode.appendf(".bgra;\n\t%s.x = ", uv);
766 noiseCode.appendf(dotLattice, lattice, lattice, inc8bit, fractVal);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000767 }
768
769 // Compute 'b' as a linear interpolation of 'u' and 'v'
sugoi@google.comd537af52013-06-10 13:59:25 +0000770 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 +0000771 // Compute the noise as a linear interpolation of 'a' and 'b'
sugoi@google.comd537af52013-06-10 13:59:25 +0000772 noiseCode.appendf("\n\treturn mix(%s.x, %s.y, %s.y);\n", ab, ab, noiseSmooth);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000773
sugoi@google.comd537af52013-06-10 13:59:25 +0000774 SkString noiseFuncName;
robertphillipsbf536af2016-02-04 06:11:53 -0800775 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800776 fragBuilder->emitFunction(kFloat_GrSLType,
777 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseStitchArgs),
778 gPerlinNoiseStitchArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000779 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800780 fragBuilder->emitFunction(kFloat_GrSLType,
781 "perlinnoise", SK_ARRAY_COUNT(gPerlinNoiseArgs),
782 gPerlinNoiseArgs, noiseCode.c_str(), &noiseFuncName);
sugoi@google.comd537af52013-06-10 13:59:25 +0000783 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000784
sugoi@google.comd537af52013-06-10 13:59:25 +0000785 // There are rounding errors if the floor operation is not performed here
egdaniel4ca2e602015-11-18 08:01:26 -0800786 fragBuilder->codeAppendf("\n\t\tvec2 %s = floor(%s.xy) * %s;",
787 noiseVec, vCoords.c_str(), baseFrequencyUni);
sugoi@google.comd537af52013-06-10 13:59:25 +0000788
789 // Clear the color accumulator
egdaniel4ca2e602015-11-18 08:01:26 -0800790 fragBuilder->codeAppendf("\n\t\t%s = vec4(0.0);", args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000791
robertphillipsbf536af2016-02-04 06:11:53 -0800792 if (pne.stitchTiles()) {
sugoi@google.comd537af52013-06-10 13:59:25 +0000793 // Set up TurbulenceInitial stitch values.
robertphillipsbf536af2016-02-04 06:11:53 -0800794 fragBuilder->codeAppendf("vec2 %s = %s;", stitchData, stitchDataUni);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000795 }
sugoi@google.come3b4c502013-04-05 13:47:09 +0000796
robertphillipsbf536af2016-02-04 06:11:53 -0800797 fragBuilder->codeAppendf("float %s = 1.0;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000798
799 // Loop over all octaves
robertphillipsbf536af2016-02-04 06:11:53 -0800800 fragBuilder->codeAppendf("for (int octave = 0; octave < %d; ++octave) {", pne.numOctaves());
sugoi@google.comd537af52013-06-10 13:59:25 +0000801
robertphillipsbf536af2016-02-04 06:11:53 -0800802 fragBuilder->codeAppendf("%s += ", args.fOutputColor);
803 if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800804 fragBuilder->codeAppend("abs(");
sugoi@google.comd537af52013-06-10 13:59:25 +0000805 }
robertphillipsbf536af2016-02-04 06:11:53 -0800806 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800807 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000808 "vec4(\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s),"
809 "\n\t\t\t\t%s(%s, %s, %s),\n\t\t\t\t%s(%s, %s, %s))",
810 noiseFuncName.c_str(), chanCoordR, noiseVec, stitchData,
811 noiseFuncName.c_str(), chanCoordG, noiseVec, stitchData,
812 noiseFuncName.c_str(), chanCoordB, noiseVec, stitchData,
813 noiseFuncName.c_str(), chanCoordA, noiseVec, stitchData);
814 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800815 fragBuilder->codeAppendf(
sugoi@google.comd537af52013-06-10 13:59:25 +0000816 "vec4(\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s),"
817 "\n\t\t\t\t%s(%s, %s),\n\t\t\t\t%s(%s, %s))",
818 noiseFuncName.c_str(), chanCoordR, noiseVec,
819 noiseFuncName.c_str(), chanCoordG, noiseVec,
820 noiseFuncName.c_str(), chanCoordB, noiseVec,
821 noiseFuncName.c_str(), chanCoordA, noiseVec);
822 }
robertphillipsbf536af2016-02-04 06:11:53 -0800823 if (pne.type() != SkPerlinNoiseShader::kFractalNoise_Type) {
egdaniel4ca2e602015-11-18 08:01:26 -0800824 fragBuilder->codeAppendf(")"); // end of "abs("
sugoi@google.comd537af52013-06-10 13:59:25 +0000825 }
egdaniel4ca2e602015-11-18 08:01:26 -0800826 fragBuilder->codeAppendf(" * %s;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000827
egdaniel4ca2e602015-11-18 08:01:26 -0800828 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", noiseVec);
829 fragBuilder->codeAppendf("\n\t\t\t%s *= 0.5;", ratio);
sugoi@google.comd537af52013-06-10 13:59:25 +0000830
robertphillipsbf536af2016-02-04 06:11:53 -0800831 if (pne.stitchTiles()) {
egdaniel4ca2e602015-11-18 08:01:26 -0800832 fragBuilder->codeAppendf("\n\t\t\t%s *= vec2(2.0);", stitchData);
sugoi@google.comd537af52013-06-10 13:59:25 +0000833 }
egdaniel4ca2e602015-11-18 08:01:26 -0800834 fragBuilder->codeAppend("\n\t\t}"); // end of the for loop on octaves
sugoi@google.come3b4c502013-04-05 13:47:09 +0000835
robertphillipsbf536af2016-02-04 06:11:53 -0800836 if (pne.type() == SkPerlinNoiseShader::kFractalNoise_Type) {
sugoi@google.come3b4c502013-04-05 13:47:09 +0000837 // The value of turbulenceFunctionResult comes from ((turbulenceFunctionResult) + 1) / 2
838 // by fractalNoise and (turbulenceFunctionResult) by turbulence.
egdaniel4ca2e602015-11-18 08:01:26 -0800839 fragBuilder->codeAppendf("\n\t\t%s = %s * vec4(0.5) + vec4(0.5);",
840 args.fOutputColor,args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000841 }
842
sugoi@google.come3b4c502013-04-05 13:47:09 +0000843 // Clamp values
egdaniel4ca2e602015-11-18 08:01:26 -0800844 fragBuilder->codeAppendf("\n\t\t%s = clamp(%s, 0.0, 1.0);", args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000845
846 // Pre-multiply the result
egdaniel4ca2e602015-11-18 08:01:26 -0800847 fragBuilder->codeAppendf("\n\t\t%s = vec4(%s.rgb * %s.aaa, %s.a);\n",
848 args.fOutputColor, args.fOutputColor,
849 args.fOutputColor, args.fOutputColor);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000850}
851
jvanverthcfc18862015-04-28 08:48:20 -0700852void GrGLPerlinNoise::GenKey(const GrProcessor& processor, const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700853 GrProcessorKeyBuilder* b) {
854 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000855
bsalomon63e99f72014-07-21 08:03:14 -0700856 uint32_t key = turbulence.numOctaves();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000857
858 key = key << 3; // Make room for next 3 bits
859
860 switch (turbulence.type()) {
861 case SkPerlinNoiseShader::kFractalNoise_Type:
862 key |= 0x1;
863 break;
864 case SkPerlinNoiseShader::kTurbulence_Type:
865 key |= 0x2;
866 break;
867 default:
868 // leave key at 0
869 break;
870 }
871
872 if (turbulence.stitchTiles()) {
873 key |= 0x4; // Flip the 3rd bit if tile stitching is on
874 }
875
bsalomon63e99f72014-07-21 08:03:14 -0700876 b->add32(key);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000877}
878
egdaniel018fb622015-10-28 07:26:40 -0700879void GrGLPerlinNoise::onSetData(const GrGLSLProgramDataManager& pdman,
880 const GrProcessor& processor) {
wangyixb1daa862015-08-18 11:29:31 -0700881 INHERITED::onSetData(pdman, processor);
senorblancof3b50272014-06-16 10:49:58 -0700882
joshualittb0a8a372014-09-23 09:50:21 -0700883 const GrPerlinNoiseEffect& turbulence = processor.cast<GrPerlinNoiseEffect>();
sugoi@google.come3b4c502013-04-05 13:47:09 +0000884
885 const SkVector& baseFrequency = turbulence.baseFrequency();
kkinnunen7510b222014-07-30 00:04:16 -0700886 pdman.set2f(fBaseFrequencyUni, baseFrequency.fX, baseFrequency.fY);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000887
sugoi@google.com4775cba2013-04-17 13:46:56 +0000888 if (turbulence.stitchTiles()) {
889 const SkPerlinNoiseShader::StitchData& stitchData = turbulence.stitchData();
kkinnunen7510b222014-07-30 00:04:16 -0700890 pdman.set2f(fStitchDataUni, SkIntToScalar(stitchData.fWidth),
commit-bot@chromium.org98393202013-07-04 18:13:05 +0000891 SkIntToScalar(stitchData.fHeight));
sugoi@google.com4775cba2013-04-17 13:46:56 +0000892 }
893}
894
sugoi@google.come3b4c502013-04-05 13:47:09 +0000895/////////////////////////////////////////////////////////////////////
bungeman06ca8ec2016-06-09 08:01:03 -0700896sk_sp<GrFragmentProcessor> SkPerlinNoiseShader::asFragmentProcessor(
brianosman982eb7f2016-06-06 13:10:58 -0700897 GrContext* context,
898 const SkMatrix& viewM,
899 const SkMatrix* externalLocalMatrix,
900 SkFilterQuality,
901 SkSourceGammaTreatment gammaTreatment) const {
bsalomon49f085d2014-09-05 13:34:00 -0700902 SkASSERT(context);
mtklein3f3b3d02014-12-01 11:47:08 -0800903
commit-bot@chromium.org96fb7482014-05-09 20:28:11 +0000904 SkMatrix localMatrix = this->getLocalMatrix();
905 if (externalLocalMatrix) {
906 localMatrix.preConcat(*externalLocalMatrix);
907 }
908
joshualitt5531d512014-12-17 15:50:11 -0800909 SkMatrix matrix = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700910 matrix.preConcat(localMatrix);
911
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000912 if (0 == fNumOctaves) {
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000913 if (kFractalNoise_Type == fType) {
bsalomonc21b09e2015-08-28 18:46:56 -0700914 // Extract the incoming alpha and emit rgba = (a/4, a/4, a/4, a/2)
bungeman06ca8ec2016-06-09 08:01:03 -0700915 sk_sp<GrFragmentProcessor> inner(
916 GrConstColorProcessor::Make(0x80404040,
917 GrConstColorProcessor::kModulateRGBA_InputMode));
918 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
reedcff10b22015-03-03 06:41:45 -0800919 }
bsalomonc21b09e2015-08-28 18:46:56 -0700920 // Emit zero.
bungeman06ca8ec2016-06-09 08:01:03 -0700921 return GrConstColorProcessor::Make(0x0, GrConstColorProcessor::kIgnore_InputMode);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +0000922 }
923
sugoi@google.come3b4c502013-04-05 13:47:09 +0000924 // Either we don't stitch tiles, either we have a valid tile size
925 SkASSERT(!fStitchTiles || !fTileSize.isEmpty());
926
joshualittb0a8a372014-09-23 09:50:21 -0700927 SkPerlinNoiseShader::PaintingData* paintingData =
halcanary385fe4d2015-08-26 13:07:48 -0700928 new PaintingData(fTileSize, fSeed, fBaseFrequencyX, fBaseFrequencyY, matrix);
bsalomonbcf0a522014-10-08 08:40:09 -0700929 SkAutoTUnref<GrTexture> permutationsTexture(
bsalomonafa95e22015-10-12 10:39:46 -0700930 GrRefCachedBitmapTexture(context, paintingData->getPermutationsBitmap(),
brianosman982eb7f2016-06-06 13:10:58 -0700931 GrTextureParams::ClampNoFilter(), gammaTreatment));
bsalomonbcf0a522014-10-08 08:40:09 -0700932 SkAutoTUnref<GrTexture> noiseTexture(
bsalomonafa95e22015-10-12 10:39:46 -0700933 GrRefCachedBitmapTexture(context, paintingData->getNoiseBitmap(),
brianosman982eb7f2016-06-06 13:10:58 -0700934 GrTextureParams::ClampNoFilter(), gammaTreatment));
sugoi@google.come3b4c502013-04-05 13:47:09 +0000935
joshualitt5531d512014-12-17 15:50:11 -0800936 SkMatrix m = viewM;
senorblancoca6a7c22014-06-27 13:35:52 -0700937 m.setTranslateX(-localMatrix.getTranslateX() + SK_Scalar1);
938 m.setTranslateY(-localMatrix.getTranslateY() + SK_Scalar1);
bsalomon49f085d2014-09-05 13:34:00 -0700939 if ((permutationsTexture) && (noiseTexture)) {
bungeman06ca8ec2016-06-09 08:01:03 -0700940 sk_sp<GrFragmentProcessor> inner(
941 GrPerlinNoiseEffect::Make(fType,
942 fNumOctaves,
943 fStitchTiles,
944 paintingData,
945 permutationsTexture, noiseTexture,
946 m));
947 return GrFragmentProcessor::MulOutputByInputAlpha(std::move(inner));
senorblancoca6a7c22014-06-27 13:35:52 -0700948 }
bsalomonc21b09e2015-08-28 18:46:56 -0700949 delete paintingData;
950 return nullptr;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000951}
952
953#endif
954
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000955#ifndef SK_IGNORE_TO_STRING
sugoi@google.come3b4c502013-04-05 13:47:09 +0000956void SkPerlinNoiseShader::toString(SkString* str) const {
957 str->append("SkPerlinNoiseShader: (");
958
959 str->append("type: ");
960 switch (fType) {
961 case kFractalNoise_Type:
962 str->append("\"fractal noise\"");
963 break;
964 case kTurbulence_Type:
965 str->append("\"turbulence\"");
966 break;
967 default:
968 str->append("\"unknown\"");
969 break;
970 }
971 str->append(" base frequency: (");
972 str->appendScalar(fBaseFrequencyX);
973 str->append(", ");
974 str->appendScalar(fBaseFrequencyY);
975 str->append(") number of octaves: ");
976 str->appendS32(fNumOctaves);
977 str->append(" seed: ");
978 str->appendScalar(fSeed);
979 str->append(" stitch tiles: ");
980 str->append(fStitchTiles ? "true " : "false ");
981
982 this->INHERITED::toString(str);
983
984 str->append(")");
985}
986#endif