./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
43 / 43
100.0%
Branches:
42 / 44
95.5%
Functions:
4 / 4, 1 excl
100.0%

src/layers/craters/shaping/prepared.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/layers/craters/shaping/prepared.h"
4
5 #include "layers/craters/shaping/resolved_state.h"
6 #include "terraingen/layers/craters/shaping/apply.h"
7 #include "terraingen/util/bits.h"
8 #include "terraingen/util/parallel.h"
9
10 #include <algorithm>
11 #include <array>
12 #include <optional>
13 #include <utility>
14
15 namespace terraingen {
16
17 namespace {
18
19 /// Craters each thread keeps prepared for reuse.
20 constexpr std::size_t kRecentCraters = 512;
21
22 /// A crater a thread prepared: whose it is, the crater, and its outline and state.
23 2518 struct RecentCrater {
24 PreparedReuse reuse;
25 Crater crater;
26 CraterOutline outline;
27 CraterState state;
28 };
29
30 /// The outline and state of `crater`, reshaped by `reshape`, reused from this thread's last
31 /// preparation of it for `reuse` when there is one.
32 509512 std::pair<CraterOutline, CraterState> OutlineAndState(
33 const Crater& crater, const CraterApplyOptions& options, const CraterShapeModels& models,
34 const std::function<void(const Crater&, CraterOutline&)>& reshape, PreparedReuse reuse) {
35 818883 const auto prepare = [&] {
36 309371 CraterOutline outline = IrregularOutline(crater, options.irregularity);
37
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 309362 times.
309370 if (reshape) {
38
1/1
✓ Branch 1 taken 8 times.
8 reshape(crater, outline);
39 }
40
1/1
✓ Branch 1 taken 309380 times.
618663 return std::pair{std::move(outline), ResolvedCraterState(models, crater)};
41 309137 };
42
2/2
✓ Branch 0 taken 32793 times.
✓ Branch 1 taken 476719 times.
509512 if (reuse.cache == 0) {
43 32793 return prepare();
44 }
45
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 476648 times.
476719 thread_local std::array<std::optional<RecentCrater>, kRecentCraters> recent;
46 476719 std::optional<RecentCrater>& slot =
47
2/2
✓ Branch 0 taken 468946 times.
✓ Branch 1 taken 7773 times.
476719 recent[static_cast<std::size_t>((crater.id * 0x9E3779B97F4A7C15ULL) >> 55U)];
48
5/6
✓ Branch 0 taken 468946 times.
✓ Branch 1 taken 7773 times.
✓ Branch 2 taken 463316 times.
✓ Branch 3 taken 5630 times.
✓ Branch 4 taken 463321 times.
✗ Branch 5 not taken.
476719 if (slot && slot->reuse.cache == reuse.cache && slot->reuse.owner == reuse.owner &&
49
2/2
✓ Branch 0 taken 200162 times.
✓ Branch 1 taken 263159 times.
463321 slot->crater == crater) {
50 200162 return {slot->outline, slot->state};
51 }
52 276557 std::pair<CraterOutline, CraterState> prepared = prepare();
53
1/1
✓ Branch 1 taken 276539 times.
276563 slot = RecentCrater{reuse, crater, prepared.first, prepared.second};
54 276508 return prepared;
55 276525 }
56
57 } // namespace
58
59 std::vector<PreparedCrater>
60 77497 PrepareCraters(const CraterList& craters, const CraterApplyOptions& options, double spacing,
61 const CraterShapeModels& models, unsigned threads,
62 const std::function<void(const Crater&, CraterOutline&)>& reshape,
63 PreparedReuse reuse) {
64 77497 constexpr std::size_t kChunk = 64;
65 77497 const std::vector<std::size_t> order = CraterApplyOrder(craters, options.order);
66
1/1
✓ Branch 1 taken 77456 times.
77520 std::vector<PreparedCrater> prepared(order.size());
67
2/2
✓ Branch 1 taken 77505 times.
✓ Branch 4 taken 77567 times.
77457 ParallelFor(CeilDiv(order.size(), kChunk), threads, [&](std::size_t chunk) {
68
2/2
✓ Branch 0 taken 4942 times.
✓ Branch 1 taken 77368 times.
82310 const std::size_t end = std::min(order.size(), (chunk + 1) * kChunk);
69
2/2
✓ Branch 0 taken 582095 times.
✓ Branch 1 taken 82520 times.
664615 for (std::size_t n = chunk * kChunk; n < end; ++n) {
70 582095 PreparedCrater& entry = prepared[n];
71 582095 entry.crater = craters.At(order[n]);
72 582190 entry.resolution = ResolutionWeight(entry.crater.diameter, spacing, options.fade);
73
2/2
✓ Branch 0 taken 72990 times.
✓ Branch 1 taken 509520 times.
582510 if (entry.resolution == 0.0) {
74 72990 continue;
75 }
76 509520 auto [outline, state] = OutlineAndState(entry.crater, options, models, reshape, reuse);
77 509247 entry.outline = std::move(outline);
78 509297 entry.state = state;
79 509315 }
80 82520 });
81 77549 const auto kept =
82 77549 std::remove_if(prepared.begin(), prepared.end(),
83
15/16
✓ Branch 0 taken 409 times.
✓ Branch 1 taken 101760 times.
✓ Branch 2 taken 407 times.
✓ Branch 3 taken 101353 times.
✓ Branch 4 taken 342 times.
✓ Branch 5 taken 101011 times.
✓ Branch 6 taken 448 times.
✓ Branch 7 taken 100563 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3580 times.
✓ Branch 10 taken 11 times.
✓ Branch 11 taken 19472 times.
✓ Branch 12 taken 55 times.
✓ Branch 13 taken 74466 times.
✓ Branch 14 taken 7336 times.
✓ Branch 15 taken 71318 times.
278407 [](const PreparedCrater& entry) { return entry.resolution == 0.0; });
84 77540 prepared.erase(kept, prepared.end());
85 77524 return prepared;
86 77555 }
87
88 } // namespace terraingen
89