./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
150 / 150
100.0%
Branches:
58 / 59
98.3%
Functions:
14 / 14, 3 excl
100.0%

include/terraingen/noise/fractals.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief Stacks of fractals with domain warps and blend modes.
5 #pragma once
6
7 #include "terraingen/noise/fractal.h"
8
9 namespace terraingen {
10
11 /// How a fractal combines with the height accumulated from the fractals before it.
12 enum class FractalBlend : uint32_t {
13 Add = 0, ///< height + fractal
14 Max = 1, ///< max(height, fractal)
15 Min = 2, ///< min(height, fractal)
16 Mask =
17 3, ///< height * (fractal / amplitude + 1) / 2: the fractal modulates roughness by region.
18 };
19
20 /// Number of `FractalBlend` values.
21 static const uint32_t kFractalBlendCount = 4;
22
23 /// Most fractals a stack may hold; a fixed bound keeps stacks uploadable to GPUs.
24 static const uint32_t kMaxFractals = 8;
25
26 /// One fractal of a stack: the `FractalParams` fields, an optional domain warp and a blend mode.
27 /// The fields keep the std430 layout GPU kernels read: `enabled`, then the `FractalParams` fields
28 /// in their order, then the blend and warp.
29 21 struct Fractal {
30 bool enabled = true; ///< Whether the fractal contributes.
31 uint32_t seed = 0; ///< Seed; each octave derives its own seed and lattice offset.
32 float wavelength = 1000.0f; ///< Wavelength of the first octave, in world units (meters).
33 float amplitude = 100.0f; ///< Maximum absolute height, in world units (meters).
34 int octaves = 8; ///< Number of octaves; at least 1.
35 float lacunarity = 2.0f; ///< Frequency ratio between successive octaves; greater than 1.
36 float gain = 0.5f; ///< Weight ratio between successive octaves; greater than 0.
37 NoiseBasis basis = NoiseBasis::Perlin; ///< Single-octave noise.
38 FractalType type = FractalType::Fbm; ///< How octaves combine.
39 float offset = 1.0f; ///< Ridge offset for `Ridged` and `HybridMultifractal`; > 0.
40 FractalBlend blend = FractalBlend::Add; ///< How it combines with the fractals before it.
41 float warpAmplitude = 0.0f; ///< Domain warp displacement in meters; 0 disables warping.
42 float warpWavelength = 500.0f; ///< Wavelength of the warp field in meters.
43
44 #ifndef __SLANG__
45 /// An fBm fractal of `seed` whose broadest octave is `wavelength` meters across and whose
46 /// height reaches `amplitude` meters, the rest at its defaults: `Fractal::Fbm(1, 800.0, 40.0)`.
47 30 static Fractal Fbm(uint32_t seed, double wavelength, double amplitude) {
48 30 Fractal fractal;
49 30 fractal.seed = seed;
50 30 fractal.wavelength = static_cast<float>(wavelength);
51 30 fractal.amplitude = static_cast<float>(amplitude);
52
5/5
✓ Branch 1 taken 5 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 5 times.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
28 return fractal;
53 }
54
55 /// Memberwise equality.
56 29 bool operator==(const Fractal& other) const {
57
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 1 time.
28 return enabled == other.enabled && seed == other.seed && wavelength == other.wavelength &&
58
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 1 time.
25 amplitude == other.amplitude && octaves == other.octaves &&
59
6/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 1 time.
23 lacunarity == other.lacunarity && gain == other.gain && basis == other.basis &&
60
6/6
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 17 times.
✓ Branch 5 taken 1 time.
20 type == other.type && offset == other.offset && blend == other.blend &&
61
6/6
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 15 times.
46 warpAmplitude == other.warpAmplitude && warpWavelength == other.warpWavelength;
62 }
63 8 bool operator!=(const Fractal& other) const {
64
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 return !(*this == other);
65 }
66 #endif
67 };
68
69 /// The `FractalParams` part of `fractal`, which `Fractal2` and `Fractal3` evaluate.
70 3786312 inline FractalParams FractalParamsFrom(Fractal fractal) {
71 3786312 FractalParams params = {};
72 3786312 params.seed = fractal.seed;
73 3786312 params.wavelength = fractal.wavelength;
74 3786312 params.amplitude = fractal.amplitude;
75 3786312 params.octaves = fractal.octaves;
76 3786312 params.lacunarity = fractal.lacunarity;
77 3786312 params.gain = fractal.gain;
78 3786312 params.basis = fractal.basis;
79 3786312 params.type = fractal.type;
80 3786312 params.offset = fractal.offset;
81
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 2243340 times.
3785000 return params;
82 }
83
84 /// Up to `kMaxFractals` fractals in a fixed-size block, the form GPU kernels receive.
85 struct FractalStack {
86 Fractal fractals[kMaxFractals]; ///< The fractals; only the first `count` are used.
87 uint32_t count = 0; ///< Number of fractals used, at most `kMaxFractals`.
88 };
89
90 /// A 2D domain warp offset.
91 struct WarpOffset2 {
92 float dx = 0.0f; ///< Offset in x, in meters.
93 float dy = 0.0f; ///< Offset in y, in meters.
94 };
95
96 namespace detail {
97
98 /// Mixed into a fractal's seed to seed its warp fields apart from its own noise. Frozen for
99 /// reproducibility.
100 static const uint32_t kWarpSeedSalt = 0x5bd1e995u;
101
102 } // namespace detail
103
104 /// The fractal of the first warp field of `layer`: 4-octave Perlin fBm of the fractal's warp
105 /// wavelength and amplitude. Each further field is seeded with `PcgHash` of the one before.
106 10346 inline FractalParams WarpParams(Fractal layer) {
107 10346 FractalParams warp = {};
108 10346 warp.wavelength = layer.warpWavelength;
109 10346 warp.amplitude = layer.warpAmplitude;
110 10346 warp.octaves = 4;
111 10346 warp.seed = PcgHash(layer.seed ^ detail::kWarpSeedSalt);
112 10346 return warp;
113 }
114
115 /// A fractal's domain warp offset from two independent warp fields (`WarpParams`).
116 /// @param x, y World coordinates in meters.
117 /// @return The offset, each component bounded by `layer.warpAmplitude` meters.
118 9301 inline WarpOffset2 DomainWarp2(float x, float y, Fractal layer) {
119 9301 FractalParams warp = WarpParams(layer);
120 9301 WarpOffset2 offset = {};
121 9301 offset.dx = Fractal2(x, y, warp);
122 9301 warp.seed = PcgHash(warp.seed);
123 9301 offset.dy = Fractal2(x, y, warp);
124 9301 return offset;
125 }
126
127 /// A fractal's normalized value, warped when `warpAmplitude` > 0, before amplitude and
128 /// blending.
129 /// @param x, y World coordinates in meters.
130 /// @return The value in [-1, 1].
131 1038028 inline float EvaluateFractal(float x, float y, Fractal layer) {
132 1038028 FractalParams unit = FractalParamsFrom(layer);
133 1038028 unit.amplitude = 1.0f;
134
2/2
✓ Branch 0 taken 9299 times.
✓ Branch 1 taken 1028729 times.
1038028 if (layer.warpAmplitude > 0.0f) {
135 9299 const WarpOffset2 offset = DomainWarp2(x, y, layer);
136 9299 return Fractal2(x + offset.dx, y + offset.dy, unit);
137 }
138 1028729 return Fractal2(x, y, unit);
139 }
140
141 namespace detail {
142
143 /// The height of a stack of fractals so far.
144 9906105 struct NoiseStack {
145 float height = 0.0f; ///< Height so far, in meters.
146 bool started = false; ///< Whether any fractal has set the height yet.
147 };
148
149 /// Combines an enabled fractal with a stack; the first fractal sets the height.
150 /// @param value The fractal's normalized value, in [-1, 1].
151 11473331 inline NoiseStack BlendFractal(NoiseStack stack, Fractal layer, float value) {
152 11473331 const float scaled = layer.amplitude * value;
153
2/2
✓ Branch 0 taken 11240718 times.
✓ Branch 1 taken 232613 times.
11473331 if (!stack.started) {
154 11240718 stack.height = scaled;
155 11240718 stack.started = true;
156 11240718 return stack;
157 }
158
4/4
✓ Branch 0 taken 5498 times.
✓ Branch 1 taken 9594 times.
✓ Branch 2 taken 41538 times.
✓ Branch 3 taken 175983 times.
232613 switch (layer.blend) {
159 5498 case FractalBlend::Max:
160
2/2
✓ Branch 0 taken 844 times.
✓ Branch 1 taken 4654 times.
5498 stack.height = max(stack.height, scaled);
161 5498 break;
162 9594 case FractalBlend::Min:
163
2/2
✓ Branch 0 taken 5544 times.
✓ Branch 1 taken 4050 times.
9594 stack.height = min(stack.height, scaled);
164 9594 break;
165 41538 case FractalBlend::Mask:
166 41538 stack.height *= 0.5f * (value + 1.0f);
167 41538 break;
168 175983 default:
169 175983 stack.height += scaled;
170 175983 break;
171 }
172 232613 return stack;
173 }
174
175 /// Adds a fractal at a 2D point to a stack, if the fractal is enabled.
176 /// @param x, y World coordinates in meters.
177 826364 inline NoiseStack AddFractal2(NoiseStack stack, float x, float y, Fractal layer) {
178
2/2
✓ Branch 0 taken 5517 times.
✓ Branch 1 taken 820847 times.
826364 if (!layer.enabled) {
179 5517 return stack;
180 }
181 820847 return BlendFractal(stack, layer, EvaluateFractal(x, y, layer));
182 }
183
184 } // namespace detail
185
186 /// Height of stacked fractals: the first enabled fractal sets it (`amplitude * value`), each
187 /// later enabled fractal combines by its `blend` mode.
188 /// @param x, y World coordinates in meters.
189 /// @return Height in meters; 0 if no fractal is enabled.
190 4117 inline float EvaluateFractals(float x, float y, FractalStack stack) {
191 4117 detail::NoiseStack height = {};
192
2/2
✓ Branch 0 taken 32828 times.
✓ Branch 1 taken 4117 times.
36945 for (uint32_t i = 0; i < stack.count; ++i) {
193 32828 height = detail::AddFractal2(height, x, y, stack.fractals[i]);
194 }
195 4117 return height.height;
196 }
197
198 #ifndef __SLANG__
199 /// `EvaluateFractals` of any number of fractals, including more than a GPU block holds.
200 /// @param x, y World coordinates in meters.
201 /// @param fractals The first of `count` fractals.
202 /// @return Height in meters; 0 if no fractal is enabled.
203 789167 inline float EvaluateFractals(float x, float y, const Fractal* fractals, std::size_t count) {
204 789167 detail::NoiseStack stack;
205
2/2
✓ Branch 0 taken 791209 times.
✓ Branch 1 taken 828710 times.
1619919 for (std::size_t i = 0; i < count; ++i) {
206 791209 stack = detail::AddFractal2(stack, x, y, fractals[i]);
207 }
208 828710 return stack.height;
209 }
210
211 /// A 3D domain warp offset.
212 struct WarpOffset3 {
213 double dx = 0.0; ///< Offset in x, in meters.
214 double dy = 0.0; ///< Offset in y, in meters.
215 double dz = 0.0; ///< Offset in z, in meters.
216 };
217
218 /// `DomainWarp2` in 3D: three warp fields (`WarpParams`) evaluated with `Fractal3`.
219 /// @param x, y, z World coordinates in meters.
220 /// @return The offset, each component bounded by `layer.warpAmplitude` meters.
221 860 inline WarpOffset3 DomainWarp3(double x, double y, double z, Fractal layer) {
222 860 FractalParams warp = WarpParams(layer);
223 860 WarpOffset3 offset;
224 860 offset.dx = static_cast<double>(Fractal3(x, y, z, warp));
225 860 warp.seed = PcgHash(warp.seed);
226 860 offset.dy = static_cast<double>(Fractal3(x, y, z, warp));
227 860 warp.seed = PcgHash(warp.seed);
228 860 offset.dz = static_cast<double>(Fractal3(x, y, z, warp));
229 860 return offset;
230 }
231
232 /// `EvaluateFractal` in 3D, with `Fractal3` and `DomainWarp3`.
233 /// @param x, y, z World coordinates in meters.
234 /// @return The fractal's normalized value in [-1, 1].
235 504751 inline float EvaluateFractal3(double x, double y, double z, Fractal layer) {
236 504751 FractalParams unit = FractalParamsFrom(layer);
237 504751 unit.amplitude = 1.0f;
238
2/2
✓ Branch 0 taken 859 times.
✓ Branch 1 taken 503892 times.
504751 if (layer.warpAmplitude > 0.0f) {
239 859 const WarpOffset3 offset = DomainWarp3(x, y, z, layer);
240 859 return Fractal3(x + offset.dx, y + offset.dy, z + offset.dz, unit);
241 }
242 503892 return Fractal3(x, y, z, unit);
243 }
244
245 namespace detail {
246
247 /// Adds a fractal at a 3D point to a stack, if the fractal is enabled.
248 /// @param x, y, z World coordinates in meters.
249 504752 inline NoiseStack AddFractal3(NoiseStack stack, double x, double y, double z, Fractal layer) {
250
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 504750 times.
504752 if (!layer.enabled) {
251 2 return stack;
252 }
253 504750 return BlendFractal(stack, layer, EvaluateFractal3(x, y, z, layer));
254 }
255
256 } // namespace detail
257
258 /// `EvaluateFractals` in 3D with `EvaluateFractal3`, for noise on a sphere.
259 /// @param x, y, z World coordinates in meters.
260 /// @param fractals The first of `count` fractals.
261 /// @return Height in meters; 0 if no fractal is enabled.
262 503637 inline float EvaluateFractals3(double x, double y, double z, const Fractal* fractals,
263 std::size_t count) {
264 503637 detail::NoiseStack stack;
265
2/2
✓ Branch 0 taken 504757 times.
✓ Branch 1 taken 503670 times.
1008427 for (std::size_t i = 0; i < count; ++i) {
266 504757 stack = detail::AddFractal3(stack, x, y, z, fractals[i]);
267 }
268 503670 return stack.height;
269 }
270 #endif
271
272 } // namespace terraingen
273