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 | 309370 | if (reshape) { | |
| 38 | 8 | reshape(crater, outline); | |
| 39 | } | ||
| 40 | 618663 | return std::pair{std::move(outline), ResolvedCraterState(models, crater)}; | |
| 41 | 309137 | }; | |
| 42 | 509512 | if (reuse.cache == 0) { | |
| 43 | 32793 | return prepare(); | |
| 44 | } | ||
| 45 | 476719 | thread_local std::array<std::optional<RecentCrater>, kRecentCraters> recent; | |
| 46 | 476719 | std::optional<RecentCrater>& slot = | |
| 47 | 476719 | recent[static_cast<std::size_t>((crater.id * 0x9E3779B97F4A7C15ULL) >> 55U)]; | |
| 48 | 476719 | if (slot && slot->reuse.cache == reuse.cache && slot->reuse.owner == reuse.owner && | |
| 49 | 463321 | slot->crater == crater) { | |
| 50 | 200162 | return {slot->outline, slot->state}; | |
| 51 | } | ||
| 52 | 276557 | std::pair<CraterOutline, CraterState> prepared = prepare(); | |
| 53 | 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 | 77520 | std::vector<PreparedCrater> prepared(order.size()); | |
| 67 | 77457 | ParallelFor(CeilDiv(order.size(), kChunk), threads, [&](std::size_t chunk) { | |
| 68 | 82310 | const std::size_t end = std::min(order.size(), (chunk + 1) * kChunk); | |
| 69 | 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 | 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 | 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 |