This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
FastNoiseSIMD.h
365 lines (283 loc) · 15.5 KB
/
FastNoiseSIMD.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// FastNoiseSIMD.h
//
// MIT License
//
// Copyright(c) 2017 Jordan Peck
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// The developer's email is jorzixdan.me2@gzixmail.com (for great email, take
// off every 'zix'.)
//
// VERSION: 0.7.0
#ifndef FASTNOISE_SIMD_H
#define FASTNOISE_SIMD_H
#if defined(__arm__) || defined(__aarch64__)
#define FN_ARM
//#define FN_IOS
#define FN_COMPILE_NEON
#else
// Comment out lines to not compile for certain instruction sets
#define FN_COMPILE_SSE2
#define FN_COMPILE_SSE41
// To compile AVX2 set C++ code generation to use /arch:AVX(2) on FastNoiseSIMD_avx2.cpp
// Note: This does not break support for pre AVX CPUs, AVX code is only run if support is detected
#define FN_COMPILE_AVX2
// Only the latest compilers will support this
//#define FN_COMPILE_AVX512
// Using FMA instructions with AVX(51)2/NEON provides a small performance increase but can cause
// minute variations in noise output compared to other SIMD levels due to higher calculation precision
// Intel compiler will always generate FMA instructions, use /Qfma- or -no-fma to disable
#define FN_USE_FMA
#endif
// Using aligned sets of memory for float arrays allows faster storing of SIMD data
// Comment out to allow unaligned float arrays to be used as sets
#define FN_ALIGNED_SETS
// SSE2/NEON support is guaranteed on 64bit CPUs so no fallback is needed
#if !(defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__) || defined(__aarch64__) || defined(FN_IOS)) || defined(_DEBUG)
#define FN_COMPILE_NO_SIMD_FALLBACK
#endif
/*
Tested Compilers:
-MSVC v120/v140
-Intel 16.0
-GCC 4.7 Linux
-Clang MacOSX
CPU instruction support:
SSE2
Intel Pentium 4 - 2001
AMD Opteron/Athlon - 2003
SEE4.1
Intel Penryn - 2007
AMD Bulldozer - Q4 2011
AVX
Intel Sandy Bridge - Q1 2011
AMD Bulldozer - Q4 2011
AVX2
Intel Haswell - Q2 2013
AMD Carrizo - Q2 2015
FMA3
Intel Haswell - Q2 2013
AMD Piledriver - 2012
AVX-512F
Intel Skylake-X - Q2 2017
*/
struct FastNoiseVectorSet;
class FastNoiseSIMD
{
public:
enum NoiseType { Value, ValueFractal, Perlin, PerlinFractal, Simplex, SimplexFractal, WhiteNoise, Cellular, Cubic, CubicFractal };
enum FractalType { FBM, Billow, RigidMulti };
enum PerturbType { None, Gradient, GradientFractal, Normalise, Gradient_Normalise, GradientFractal_Normalise };
enum CellularDistanceFunction { Euclidean, Manhattan, Natural };
enum CellularReturnType { CellValue, Distance, Distance2, Distance2Add, Distance2Sub, Distance2Mul, Distance2Div, NoiseLookup, Distance2Cave };
// Creates new FastNoiseSIMD for the highest supported instuction set of the CPU
static FastNoiseSIMD* NewFastNoiseSIMD(int seed = 1337);
// Returns highest detected level of CPU support
// 5: ARM NEON
// 4: AVX-512F
// 3: AVX2 & FMA3
// 2: SSE4.1
// 1: SSE2
// 0: Fallback, no SIMD support
static int GetSIMDLevel(void);
// Sets the SIMD level for newly created FastNoiseSIMD objects
// 5: ARM NEON
// 4: AVX-512F
// 3: AVX2 & FMA3
// 2: SSE4.1
// 1: SSE2
// 0: Fallback, no SIMD support
// -1: Auto-detect fastest supported (Default)
// Caution: Setting this manually can cause crashes on CPUs that do not support that level
// Caution: Changing this after creating FastNoiseSIMD objects has undefined behaviour
static void SetSIMDLevel(int level) { s_currentSIMDLevel = level; }
// Free a noise set from memory
static void FreeNoiseSet(float* noiseSet);
// Create an empty (aligned) noise set for use with FillNoiseSet()
static float* GetEmptySet(int size);
// Create an empty (aligned) noise set for use with FillNoiseSet()
static float* GetEmptySet(int xSize, int ySize, int zSize) { return GetEmptySet(xSize*ySize*zSize); }
// Rounds the size up to the nearest aligned size for the current SIMD level
static int AlignedSize(int size);
// Returns seed used for all noise types
int GetSeed(void) const { return m_seed; }
// Sets seed used for all noise types
// Default: 1337
void SetSeed(int seed) { m_seed = seed; }
// Sets frequency for all noise types
// Default: 0.01
void SetFrequency(float frequency) { m_frequency = frequency; }
// Sets noise return type of (Get/Fill)NoiseSet()
// Default: Simplex
void SetNoiseType(NoiseType noiseType) { m_noiseType = noiseType; }
// Sets scaling factor for individual axis
// Defaults: 1.0
void SetAxisScales(float xScale, float yScale, float zScale) { m_xScale = xScale; m_yScale = yScale; m_zScale = zScale; }
// Sets octave count for all fractal noise types
// Default: 3
void SetFractalOctaves(int octaves) { m_octaves = octaves; m_fractalBounding = CalculateFractalBounding(m_octaves, m_gain); }
// Sets octave lacunarity for all fractal noise types
// Default: 2.0
void SetFractalLacunarity(float lacunarity) { m_lacunarity = lacunarity; }
// Sets octave gain for all fractal noise types
// Default: 0.5
void SetFractalGain(float gain) { m_gain = gain; m_fractalBounding = CalculateFractalBounding(m_octaves, m_gain); }
// Sets method for combining octaves in all fractal noise types
// Default: FBM
void SetFractalType(FractalType fractalType) { m_fractalType = fractalType; }
// Sets return type from cellular noise calculations
// Default: Distance
void SetCellularReturnType(CellularReturnType cellularReturnType) { m_cellularReturnType = cellularReturnType; }
// Sets distance function used in cellular noise calculations
// Default: Euclidean
void SetCellularDistanceFunction(CellularDistanceFunction cellularDistanceFunction) { m_cellularDistanceFunction = cellularDistanceFunction; }
// Sets the type of noise used if cellular return type is set the NoiseLookup
// Default: Simplex
void SetCellularNoiseLookupType(NoiseType cellularNoiseLookupType) { m_cellularNoiseLookupType = cellularNoiseLookupType; }
// Sets relative frequency on the cellular noise lookup return type
// Default: 0.2
void SetCellularNoiseLookupFrequency(float cellularNoiseLookupFrequency) { m_cellularNoiseLookupFrequency = cellularNoiseLookupFrequency; }
// Sets the 2 distance indicies used for distance2 return types
// Default: 0, 1
// Note: index0 should be lower than index1
// Both indicies must be >= 0, index1 must be < 4
void SetCellularDistance2Indicies(int cellularDistanceIndex0, int cellularDistanceIndex1);
// Sets the maximum distance a cellular point can move from it's grid position
// Setting this high will make artifacts more common
// Default: 0.45
void SetCellularJitter(float cellularJitter) { m_cellularJitter = cellularJitter; }
// Enables position perturbing for all noise types
// Default: None
void SetPerturbType(PerturbType perturbType) { m_perturbType = perturbType; }
// Sets the maximum distance the input position can be perturbed
// Default: 1.0
void SetPerturbAmp(float perturbAmp) { m_perturbAmp = perturbAmp / 511.5f; }
// Set the relative frequency for the perturb gradient
// Default: 0.5
void SetPerturbFrequency(float perturbFrequency) { m_perturbFrequency = perturbFrequency; }
// Sets octave count for perturb fractal types
// Default: 3
void SetPerturbFractalOctaves(int perturbOctaves) { m_perturbOctaves = perturbOctaves; m_perturbFractalBounding = CalculateFractalBounding(m_perturbOctaves, m_perturbGain); }
// Sets octave lacunarity for perturb fractal types
// Default: 2.0
void SetPerturbFractalLacunarity(float perturbLacunarity) { m_perturbLacunarity = perturbLacunarity; }
// Sets octave gain for perturb fractal types
// Default: 0.5
void SetPerturbFractalGain(float perturbGain) { m_perturbGain = perturbGain; m_perturbFractalBounding = CalculateFractalBounding(m_perturbOctaves, m_perturbGain); }
// Sets the length for vectors after perturb normalising
// Default: 1.0
void SetPerturbNormaliseLength(float perturbNormaliseLength) { m_perturbNormaliseLength = perturbNormaliseLength; }
static FastNoiseVectorSet* GetVectorSet(int xSize, int ySize, int zSize);
static FastNoiseVectorSet* GetSamplingVectorSet(int sampleScale, int xSize, int ySize, int zSize);
static void FillVectorSet(FastNoiseVectorSet* vectorSet, int xSize, int ySize, int zSize);
static void FillSamplingVectorSet(FastNoiseVectorSet* vectorSet, int sampleScale, int xSize, int ySize, int zSize);
float* GetNoiseSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
void FillNoiseSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
void FillNoiseSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f);
float* GetSampledNoiseSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, int sampleScale);
virtual void FillSampledNoiseSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, int sampleScale) = 0;
virtual void FillSampledNoiseSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
float* GetWhiteNoiseSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
virtual void FillWhiteNoiseSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillWhiteNoiseSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
float* GetValueSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
float* GetValueFractalSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
virtual void FillValueSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillValueFractalSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillValueSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
virtual void FillValueFractalSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
float* GetPerlinSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
float* GetPerlinFractalSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
virtual void FillPerlinSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillPerlinFractalSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillPerlinSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
virtual void FillPerlinFractalSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
float* GetSimplexSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
float* GetSimplexFractalSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
virtual void FillSimplexSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillSimplexFractalSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillSimplexSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
virtual void FillSimplexFractalSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
float* GetCellularSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
virtual void FillCellularSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillCellularSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
float* GetCubicSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
float* GetCubicFractalSet(int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f);
virtual void FillCubicSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillCubicFractalSet(float* noiseSet, int xStart, int yStart, int zStart, int xSize, int ySize, int zSize, float scaleModifier = 1.0f) = 0;
virtual void FillCubicSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
virtual void FillCubicFractalSet(float* noiseSet, FastNoiseVectorSet* vectorSet, float xOffset = 0.0f, float yOffset = 0.0f, float zOffset = 0.0f) = 0;
virtual ~FastNoiseSIMD() { }
protected:
int m_seed = 1337;
float m_frequency = 0.01f;
NoiseType m_noiseType = SimplexFractal;
float m_xScale = 1.0f;
float m_yScale = 1.0f;
float m_zScale = 1.0f;
int m_octaves = 3;
float m_lacunarity = 2.0f;
float m_gain = 0.5f;
FractalType m_fractalType = FBM;
float m_fractalBounding;
CellularDistanceFunction m_cellularDistanceFunction = Euclidean;
CellularReturnType m_cellularReturnType = Distance;
NoiseType m_cellularNoiseLookupType = Simplex;
float m_cellularNoiseLookupFrequency = 0.2f;
int m_cellularDistanceIndex0 = 0;
int m_cellularDistanceIndex1 = 1;
float m_cellularJitter = 0.45f;
PerturbType m_perturbType = None;
float m_perturbAmp = 1.0f;
float m_perturbFrequency = 0.5f;
int m_perturbOctaves = 3;
float m_perturbLacunarity = 2.0f;
float m_perturbGain = 0.5f;
float m_perturbFractalBounding;
float m_perturbNormaliseLength = 1.0f;
static int s_currentSIMDLevel;
static float CalculateFractalBounding(int octaves, float gain);
};
struct FastNoiseVectorSet
{
public:
int size = -1;
float* xSet = nullptr;
float* ySet = nullptr;
float* zSet = nullptr;
// Only used for sampled vector sets
int sampleScale = 0;
int sampleSizeX = -1;
int sampleSizeY = -1;
int sampleSizeZ = -1;
FastNoiseVectorSet() {}
FastNoiseVectorSet(int _size) { SetSize(_size); }
~FastNoiseVectorSet() { Free(); }
void Free();
void SetSize(int _size);
};
#define FN_CELLULAR_INDEX_MAX 3
#define FN_NO_SIMD_FALLBACK 0
#define FN_SSE2 1
#define FN_SSE41 2
#define FN_AVX2 3
#define FN_AVX512 4
#define FN_NEON 5
#endif