src/layers/prefetched_ground.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "layers/prefetched_ground.h" | ||
| 4 | |||
| 5 | #include "terraingen/util/bits.h" | ||
| 6 | |||
| 7 | #include <cstdint> | ||
| 8 | #include <map> | ||
| 9 | |||
| 10 | namespace terraingen { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | /// A table slot holding no read. | ||
| 15 | constexpr std::uint32_t kEmpty = 0xFFFFFFFFU; | ||
| 16 | |||
| 17 | } // namespace | ||
| 18 | |||
| 19 | 868231 | void RecordingGround::FillSamples(SampleBlock& samples, double spacing, | |
| 20 | unsigned /*threads*/) const { | ||
| 21 | 1747880 | for (std::size_t i = 0; i < samples.Size(); ++i) { | |
| 22 | 872486 | m_reads.push_back({samples.Position(i), spacing}); | |
| 23 | 875596 | samples.Height(i) = 0.0f; | |
| 24 | } | ||
| 25 | 875394 | } | |
| 26 | |||
| 27 | 263 | GroundReads::GroundReads(const Ground& ground, Span<const GroundRead> reads, unsigned threads) | |
| 28 | 263 | : m_ground(ground), m_heights(reads.size()) { | |
| 29 | 263 | std::size_t capacity = 16; | |
| 30 | 729 | while (capacity < 2 * reads.size()) { | |
| 31 | 466 | capacity *= 2; | |
| 32 | } | ||
| 33 | 263 | m_table.assign(capacity, kEmpty); | |
| 34 | 263 | std::vector<std::uint32_t> places(reads.size()); | |
| 35 | 263 | std::map<std::uint64_t, std::vector<std::uint32_t>> bySpacing; | |
| 36 | 888972 | for (std::size_t n = 0; n < reads.size(); ++n) { | |
| 37 | 888709 | const GroundRead& read = reads[n]; | |
| 38 | 888709 | const std::uint64_t x = BitCast<std::uint64_t>(read.point.x); | |
| 39 | 888709 | const std::uint64_t y = BitCast<std::uint64_t>(read.point.y); | |
| 40 | 888709 | const std::uint64_t spacing = BitCast<std::uint64_t>(read.spacing); | |
| 41 | 888709 | std::uint32_t& slot = m_table[Slot(x, y, spacing)]; | |
| 42 | 888709 | if (slot == kEmpty) { | |
| 43 | 823782 | slot = static_cast<std::uint32_t>(m_distinct.size()); | |
| 44 | 823782 | m_distinct.push_back({x, y, spacing, 0.0f}); | |
| 45 | 823782 | bySpacing[spacing].push_back(slot); | |
| 46 | } | ||
| 47 | 888709 | places[n] = slot; | |
| 48 | } | ||
| 49 | 263 | std::vector<Vec2d> points; | |
| 50 | 263 | std::vector<float> read; | |
| 51 | 526 | for (const auto& [spacing, indices] : bySpacing) { | |
| 52 | 263 | points.resize(indices.size()); | |
| 53 | 263 | read.assign(indices.size(), 0.0f); | |
| 54 | 824045 | for (std::size_t n = 0; n < indices.size(); ++n) { | |
| 55 | 823782 | const Held& held = m_distinct[indices[n]]; | |
| 56 | 823782 | points[n] = {BitCast<double>(held.x), BitCast<double>(held.y)}; | |
| 57 | } | ||
| 58 | 263 | m_ground.HeightsAt(points, read, BitCast<double>(spacing), threads); | |
| 59 | 824045 | for (std::size_t n = 0; n < indices.size(); ++n) { | |
| 60 | 823782 | m_distinct[indices[n]].height = read[n]; | |
| 61 | } | ||
| 62 | } | ||
| 63 | 888972 | for (std::size_t n = 0; n < reads.size(); ++n) { | |
| 64 | 888709 | m_heights[n] = m_distinct[places[n]].height; | |
| 65 | } | ||
| 66 | 263 | } | |
| 67 | |||
| 68 | 902650 | std::size_t GroundReads::Slot(std::uint64_t x, std::uint64_t y, std::uint64_t spacing) const { | |
| 69 | 902650 | const std::size_t mask = m_table.size() - 1; | |
| 70 | 902650 | std::uint64_t hash = x ^ (y * 0x9E3779B97F4A7C15ULL) ^ (spacing * 0xC2B2AE3D27D4EB4FULL); | |
| 71 | 902650 | hash = (hash ^ (hash >> 30U)) * 0xBF58476D1CE4E5B9ULL; | |
| 72 | 902650 | hash = (hash ^ (hash >> 27U)) * 0x94D049BB133111EBULL; | |
| 73 | 902650 | hash ^= hash >> 31U; | |
| 74 | 902650 | for (std::size_t slot = hash & mask;; slot = (slot + 1) & mask) { | |
| 75 | 1322061 | const std::uint32_t held = m_table[slot]; | |
| 76 | 1322061 | if (held == kEmpty) { | |
| 77 | return slot; | ||
| 78 | } | ||
| 79 | 484356 | const Held& entry = m_distinct[held]; | |
| 80 | 484356 | if (entry.x == x && entry.y == y && entry.spacing == spacing) { | |
| 81 | return slot; | ||
| 82 | } | ||
| 83 | 419411 | } | |
| 84 | } | ||
| 85 | |||
| 86 | 13941 | const float* GroundReads::Find(double x, double y, double spacing) const { | |
| 87 | 13941 | const std::uint32_t held = m_table[Slot(BitCast<std::uint64_t>(x), BitCast<std::uint64_t>(y), | |
| 88 | 13941 | BitCast<std::uint64_t>(spacing))]; | |
| 89 | 13937 | return held == kEmpty ? nullptr : &m_distinct[held].height; | |
| 90 | } | ||
| 91 | |||
| 92 | 895358 | const float* ReplayedGround::Held(double x, double y, double spacing) const { | |
| 93 | 895358 | if (m_next < m_reads.size()) { | |
| 94 | 895819 | const GroundRead& recorded = m_reads[m_next]; | |
| 95 | 1777807 | if (BitCast<std::uint64_t>(recorded.point.x) == BitCast<std::uint64_t>(x) && | |
| 96 | 895665 | BitCast<std::uint64_t>(recorded.point.y) == BitCast<std::uint64_t>(y) && | |
| 97 | 882260 | BitCast<std::uint64_t>(recorded.spacing) == BitCast<std::uint64_t>(spacing)) { | |
| 98 | 882981 | return &m_heights[m_next++]; | |
| 99 | } | ||
| 100 | } | ||
| 101 | 12223 | return m_held.Find(x, y, spacing); | |
| 102 | } | ||
| 103 | |||
| 104 | 893793 | void ReplayedGround::FillSamples(SampleBlock& samples, double spacing, unsigned threads) const { | |
| 105 | 893793 | std::vector<Vec2d> missing; | |
| 106 | 893793 | std::vector<std::size_t> where; | |
| 107 | 1790240 | for (std::size_t i = 0; i < samples.Size(); ++i) { | |
| 108 | 893641 | const Vec2d point = samples.Position(i); | |
| 109 | 894756 | if (const float* held = Held(point.x, point.y, spacing)) { | |
| 110 | 882299 | samples.Height(i) = *held; | |
| 111 | } else { | ||
| 112 | 13920 | missing.push_back(point); | |
| 113 | 13921 | where.push_back(i); | |
| 114 | } | ||
| 115 | } | ||
| 116 | 896599 | if (missing.empty()) { | |
| 117 | 882680 | return; | |
| 118 | } | ||
| 119 | 13919 | std::vector<float> read(missing.size()); | |
| 120 | 13921 | m_held.Read().HeightsAt(missing, read, spacing, threads); | |
| 121 | 27842 | for (std::size_t n = 0; n < missing.size(); ++n) { | |
| 122 | 13921 | samples.Height(where[n]) = read[n]; | |
| 123 | } | ||
| 124 | 896601 | } | |
| 125 | |||
| 126 | } // namespace terraingen | ||
| 127 |