./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
72 / 72
100.0%
Branches:
47 / 47
100.0%
Functions:
4 / 4
100.0%

src/noise/fractal_rows.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "noise/fractal_rows.h"
4
5 #include <vector>
6
7 namespace terraingen {
8
9 namespace {
10
11 /// Points evaluated together, so the octave's arrays stay in the first-level cache.
12 constexpr std::size_t kRun = 256;
13
14 /// One octave's basis noise at `count` lattice positions.
15 15325855 void BasisRun(NoiseBasis basis, const float* lx, const float* ly, std::size_t count,
16 std::uint32_t seed, float* noise) {
17
2/2
✓ Branch 0 taken 15322270 times.
✓ Branch 1 taken 3585 times.
15325855 if (basis == NoiseBasis::Perlin) {
18
2/2
✓ Branch 0 taken 62056819 times.
✓ Branch 1 taken 15322270 times.
77379089 for (std::size_t i = 0; i < count; ++i) {
19 62056819 noise[i] = PerlinNoise2(lx[i], ly[i], seed);
20 }
21 return;
22 }
23
2/2
✓ Branch 0 taken 1840 times.
✓ Branch 1 taken 1745 times.
3585 if (basis == NoiseBasis::Simplex) {
24
2/2
✓ Branch 0 taken 287728 times.
✓ Branch 1 taken 1840 times.
289568 for (std::size_t i = 0; i < count; ++i) {
25
2/2
✓ Branch 0 taken 140340 times.
✓ Branch 1 taken 147388 times.
428068 noise[i] = SimplexNoise2(lx[i], ly[i], seed);
26 }
27 return;
28 }
29
2/2
✓ Branch 0 taken 338276 times.
✓ Branch 1 taken 1745 times.
340021 for (std::size_t i = 0; i < count; ++i) {
30 338276 noise[i] = BasisNoise2(basis, lx[i], ly[i], seed);
31 }
32 }
33
34 /// `Fractal2` over one run of at most `kRun` points.
35 2267347 void FractalRun(const float* xs, const float* ys, std::size_t count, FractalParams params,
36 float* heights) {
37 2267347 float lx[kRun];
38 2267347 float ly[kRun];
39 2267347 float noise[kRun];
40
2/2
✓ Branch 0 taken 416105175 times.
✓ Branch 1 taken 2267347 times.
418372522 detail::FractalSums sums[kRun];
41
2/2
✓ Branch 0 taken 10263639 times.
✓ Branch 1 taken 2267347 times.
12530986 for (std::size_t i = 0; i < count; ++i) {
42 10263639 sums[i] = detail::FractalSums{};
43 }
44 2267347 float frequency = 1.0f / params.wavelength;
45 2267347 float turnCos = 1.0f;
46 2267347 float turnSin = 0.0f;
47
2/2
✓ Branch 0 taken 15341485 times.
✓ Branch 1 taken 2273426 times.
17614911 for (int octave = 0; octave < params.octaves; ++octave) {
48 15341485 const UVec3 h = Pcg3dHash(detail::OctaveKey(params.seed, uint32_t(octave), 0u));
49 15341485 const float offsetX = HashToUnitFloat(h.y);
50 15341485 const float offsetY = HashToUnitFloat(h.z);
51
2/2
✓ Branch 0 taken 64693797 times.
✓ Branch 1 taken 15341485 times.
80035282 for (std::size_t i = 0; i < count; ++i) {
52 64693797 const float rx = xs[i] * turnCos - ys[i] * turnSin;
53 64693797 const float ry = xs[i] * turnSin + ys[i] * turnCos;
54 64693797 lx[i] = rx * frequency + offsetX;
55 64693797 ly[i] = ry * frequency + offsetY;
56 }
57 15341485 BasisRun(params.basis, lx, ly, count, h.x, noise);
58
2/2
✓ Branch 1 taken 62591223 times.
✓ Branch 2 taken 15347564 times.
77932708 for (std::size_t i = 0; i < count; ++i) {
59 62591223 sums[i] = detail::AddFractalOctave(sums[i], params, octave, noise[i]);
60 }
61 15347564 frequency *= params.lacunarity;
62 15347564 const float nextCos = turnCos * detail::kOctaveTurnCos - turnSin * detail::kOctaveTurnSin;
63 15347564 turnSin = turnSin * detail::kOctaveTurnCos + turnCos * detail::kOctaveTurnSin;
64 15347564 turnCos = nextCos;
65 }
66
2/2
✓ Branch 0 taken 9991750 times.
✓ Branch 1 taken 2273426 times.
12265176 for (std::size_t i = 0; i < count; ++i) {
67 9991750 heights[i] = detail::FinishFractal(sums[i], params);
68 }
69 2273426 }
70
71 } // namespace
72
73 2244286 void Fractal2Rows(Span<const float> xs, Span<const float> ys, FractalParams params,
74 Span<float> heights) {
75
2/2
✓ Branch 0 taken 2264997 times.
✓ Branch 1 taken 2252362 times.
4517359 for (std::size_t begin = 0; begin < xs.size(); begin += kRun) {
76
2/2
✓ Branch 0 taken 2247741 times.
✓ Branch 1 taken 17256 times.
2264997 const std::size_t count = std::min(kRun, xs.size() - begin);
77 2264997 FractalRun(xs.data() + begin, ys.data() + begin, count, params, heights.data() + begin);
78 }
79 2252362 }
80
81 2242539 void EvaluateFractalsRows(Span<const float> xs, Span<const float> ys, const Fractal* fractals,
82 std::size_t count, Span<float> heights) {
83 2242539 const std::size_t points = xs.size();
84 2242539 std::vector<detail::NoiseStack> stacks(points);
85
1/1
✓ Branch 1 taken 2244060 times.
2244759 std::vector<float> values(points);
86
1/1
✓ Branch 1 taken 2243620 times.
2244060 std::vector<float> dx(points);
87
1/1
✓ Branch 1 taken 2251708 times.
2243620 std::vector<float> dy(points);
88
1/1
✓ Branch 1 taken 2238369 times.
2251708 std::vector<float> wx(points);
89
1/1
✓ Branch 1 taken 2242401 times.
2238369 std::vector<float> wy(points);
90
2/2
✓ Branch 0 taken 2243552 times.
✓ Branch 1 taken 2249037 times.
4492589 for (std::size_t f = 0; f < count; ++f) {
91 2243552 const Fractal& layer = fractals[f];
92
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2243524 times.
2243552 if (!layer.enabled) {
93 28 continue;
94 }
95
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 2243340 times.
2243524 FractalParams unit = FractalParamsFrom(layer);
96 2243524 unit.amplitude = 1.0f;
97
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 2243340 times.
2243524 if (layer.warpAmplitude > 0.0f) {
98 184 FractalParams warp = WarpParams(layer);
99 184 Fractal2Rows(xs, ys, warp, dx);
100 184 warp.seed = PcgHash(warp.seed);
101 184 Fractal2Rows(xs, ys, warp, dy);
102
2/2
✓ Branch 0 taken 32588 times.
✓ Branch 1 taken 184 times.
32772 for (std::size_t i = 0; i < points; ++i) {
103 32588 wx[i] = xs[i] + dx[i];
104 32588 wy[i] = ys[i] + dy[i];
105 }
106 184 Fractal2Rows(wx, wy, unit, values);
107 } else {
108 2243340 Fractal2Rows(xs, ys, unit, values);
109 }
110
2/2
✓ Branch 0 taken 10110985 times.
✓ Branch 1 taken 2250160 times.
12361145 for (std::size_t i = 0; i < points; ++i) {
111 10110985 stacks[i] = detail::BlendFractal(stacks[i], layer, values[i]);
112 }
113 }
114
2/2
✓ Branch 0 taken 9981940 times.
✓ Branch 1 taken 2246601 times.
12228541 for (std::size_t i = 0; i < points; ++i) {
115 9981940 heights[i] = stacks[i].height;
116 }
117 2246601 }
118
119 } // namespace terraingen
120