src/layers/craters/generation/cell_cache.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | #include "layers/craters/generation/cell_cache.h" | ||
| 4 | |||
| 5 | #include "terraingen/noise/hash.h" | ||
| 6 | #include "terraingen/util/log.h" | ||
| 7 | |||
| 8 | #include <array> | ||
| 9 | #include <atomic> | ||
| 10 | |||
| 11 | namespace terraingen { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | /// Cells each thread keeps of the caches it last read. | ||
| 16 | constexpr std::size_t kRecentCells = 256; | ||
| 17 | |||
| 18 | /// The source of each cache's `m_id`, from 1 so 0 marks an empty slot. | ||
| 19 | std::atomic<std::uint64_t> nextCacheId{1}; | ||
| 20 | |||
| 21 | /// A cell a thread read: its cache, band and index, and its craters. | ||
| 22 | 398384 | struct RecentCell { | |
| 23 | std::uint64_t cache = 0; | ||
| 24 | int exponent = 0; | ||
| 25 | std::int32_t i = 0; | ||
| 26 | std::int32_t j = 0; | ||
| 27 | std::shared_ptr<const CellCraters> craters; | ||
| 28 | }; | ||
| 29 | |||
| 30 | } // namespace | ||
| 31 | |||
| 32 | std::shared_ptr<const CellCraters> | ||
| 33 | 1685559 | CraterCellCache::Get(int exponent, const CellIndex& cell, | |
| 34 | const std::function<CellCraters()>& generate) { | ||
| 35 | 1685559 | thread_local std::array<RecentCell, kRecentCells> recent; | |
| 36 | 1685560 | const std::uint32_t mixed = (static_cast<std::uint32_t>(cell.i) * 0x9E3779B1u) ^ | |
| 37 | 1685560 | (static_cast<std::uint32_t>(cell.j) * 0x85EBCA77u) ^ | |
| 38 | 1685560 | (static_cast<std::uint32_t>(exponent) * 0xC2B2AE3Du); | |
| 39 | 1685560 | RecentCell& slot = recent[(mixed >> 16U) % kRecentCells]; | |
| 40 | 1685560 | if (slot.cache == m_id && slot.exponent == exponent && slot.i == cell.i && slot.j == cell.j) { | |
| 41 | 1486868 | return slot.craters; | |
| 42 | } | ||
| 43 | 198692 | std::shared_ptr<const CellCraters> craters = m_cache.Get({exponent, cell.i, cell.j}, generate); | |
| 44 | 199192 | slot = {m_id, exponent, cell.i, cell.j, craters}; | |
| 45 | 199192 | return craters; | |
| 46 | 597576 | } | |
| 47 | |||
| 48 | 605678 | std::size_t CraterCellCache::KeyHash::operator()(const Key& key) const { | |
| 49 | 605678 | return HashWords({static_cast<std::uint64_t>(key.exponent), static_cast<std::uint64_t>(key.i), | |
| 50 | 605678 | static_cast<std::uint64_t>(key.j)}); | |
| 51 | } | ||
| 52 | |||
| 53 | 1309 | CraterCellCache::CraterCellCache(std::size_t maxCraters) | |
| 54 | 1309 | : m_cache( | |
| 55 | 109663 | maxCraters, [](const CellCraters& cell) { return cell.craters.Size(); }, | |
| 56 | 1309 | [maxCraters](std::size_t evicted) { | |
| 57 | 738 | LogDebug("craters", "Crater cache evicted ", evicted, " cells to stay within ", | |
| 58 | 369 | maxCraters, " craters"); | |
| 59 | 369 | }), | |
| 60 | 1308 | m_id(nextCacheId.fetch_add(1)) {} | |
| 61 | |||
| 62 | 262738 | CellCraterSource CachedCellCraters(CraterCellCache& cache, const SurfaceCells& cells, | |
| 63 | const SizeFrequencyDistribution& density, | ||
| 64 | const CraterAgeModel& ages, std::uint32_t seed) { | ||
| 65 | 2211879 | return [&cache, &cells, &density, &ages, seed](const CraterBand& band, const CellIndex& cell) { | |
| 66 | 5165283 | return cache.Get(band.exponent, cell, [&] { | |
| 67 | 109262 | return CellCraters(GenerateCellCraters(cells, band, cell, density, ages, seed)); | |
| 68 | 3370895 | }); | |
| 69 | 262738 | }; | |
| 70 | } | ||
| 71 | |||
| 72 | 263038 | CraterList GenerateCachedCraters(CraterCellCache* cache, const SurfaceCells& cells, | |
| 73 | const Bounds2d& region, const SizeFrequencyDistribution& density, | ||
| 74 | const CraterAgeModel& ages, std::uint32_t seed, | ||
| 75 | CraterGenerationOptions options) { | ||
| 76 | 263038 | if (cache != nullptr) { | |
| 77 | 263034 | options.cellCraters = CachedCellCraters(*cache, cells, density, ages, seed); | |
| 78 | } | ||
| 79 | 263584 | return GenerateCraters(cells, region, density, ages, seed, options); | |
| 80 | } | ||
| 81 | |||
| 82 | } // namespace terraingen | ||
| 83 |