stage/publish/change_set.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/stage/publish/change_set.h" | ||
| 3 | |||
| 4 | #include <algorithm> | ||
| 5 | |||
| 6 | #include "chrono_batch_render/device/queue.h" | ||
| 7 | #include "chrono_batch_render/stage/columns/kernels/changes.h" | ||
| 8 | |||
| 9 | namespace chrono::batch_render { | ||
| 10 | |||
| 11 | 1768 | device::BufferRange ChangedRows::Count() const { | |
| 12 | 1768 | return m_buffer->Slice(0, sizeof(uint32_t)); | |
| 13 | } | ||
| 14 | |||
| 15 | ✗ | device::BufferRange ChangedRows::Rows() const { | |
| 16 | ✗ | return m_buffer->Slice(kChangedRowsOffset * sizeof(uint32_t), | |
| 17 | ✗ | std::max<uint64_t>(m_capacity, 1) * sizeof(uint32_t)); | |
| 18 | } | ||
| 19 | |||
| 20 | 1768 | std::vector<uint32_t> ChangedRows::ToHost() const { | |
| 21 | 1768 | if (!m_buffer) { | |
| 22 | ✗ | return {}; | |
| 23 | } | ||
| 24 | 1768 | device::Queue& queue = m_buffer->GetDevice().GetQueue(); | |
| 25 | 1768 | uint32_t count = 0; | |
| 26 | 1768 | queue.Read(Count(), &count); | |
| 27 | 1768 | std::vector<uint32_t> rows(count); | |
| 28 | 1768 | if (count > 0) { | |
| 29 | 33 | queue.Read(m_buffer->Slice(kChangedRowsOffset * sizeof(uint32_t), count * sizeof(uint32_t)), | |
| 30 | 33 | rows.data()); | |
| 31 | } | ||
| 32 | 1768 | std::ranges::sort(rows); | |
| 33 | 1768 | return rows; | |
| 34 | 1768 | } | |
| 35 | |||
| 36 | 1774 | const ChangedRows* ChangeSet::Rows(ColumnRef column) const { | |
| 37 | 1774 | const auto found = std::ranges::find_if( | |
| 38 | 9707 | columns, [&](const ChangedRows& rows) { return rows.Column().column == column.column; }); | |
| 39 | 1774 | return found == columns.end() ? nullptr : &*found; | |
| 40 | } | ||
| 41 | |||
| 42 | 8149 | std::span<const uint32_t> ChangeSet::ArrayRows(ColumnRef column) const { | |
| 43 | 25412 | for (const auto& [at, rows] : arrays) { | |
| 44 | 25412 | if (at.column == column.column) { | |
| 45 | 8149 | return rows; | |
| 46 | } | ||
| 47 | } | ||
| 48 | ✗ | return {}; | |
| 49 | } | ||
| 50 | |||
| 51 | 2583 | bool ChangeSet::Wrote(TableId table) const { | |
| 52 | 2583 | return std::ranges::find(wrote, table) != wrote.end(); | |
| 53 | } | ||
| 54 | |||
| 55 | 3 | bool ChangeSet::World(uint32_t world) const { | |
| 56 | 3 | return Global() || std::ranges::binary_search(worlds, world); | |
| 57 | } | ||
| 58 | |||
| 59 | 1349 | bool ChangeSet::Global() const { | |
| 60 | 1349 | return !worlds.empty() && worlds.back() == kGlobalWorld; | |
| 61 | } | ||
| 62 | |||
| 63 | 4948 | void TableVersions::Record(TableId table, std::span<const uint32_t> worlds) { | |
| 64 | 4948 | ++m_sequence; | |
| 65 | 4948 | if (uint32_t(table) >= m_versions.size()) { | |
| 66 | 2818 | m_versions.resize(uint32_t(table) + 1); | |
| 67 | } | ||
| 68 | 4948 | m_versions[uint32_t(table)] = m_sequence; | |
| 69 | 4948 | Entry& entry = m_entries.emplace_back(Entry{m_sequence, table, {worlds.begin(), worlds.end()}}); | |
| 70 | 4948 | std::ranges::sort(entry.worlds); | |
| 71 | 4948 | entry.worlds.erase(std::ranges::unique(entry.worlds).begin(), entry.worlds.end()); | |
| 72 | 4948 | } | |
| 73 | |||
| 74 | 8763 | void TableVersions::Collect(TableId table, uint64_t since, std::vector<uint32_t>& worlds) const { | |
| 75 | 107113 | for (const Entry& entry : m_entries) { | |
| 76 | 98350 | if (entry.table != table || entry.sequence <= since) { | |
| 77 | 89507 | continue; | |
| 78 | } | ||
| 79 | 8843 | worlds.insert(worlds.end(), entry.worlds.begin(), entry.worlds.end()); | |
| 80 | } | ||
| 81 | 8763 | std::ranges::sort(worlds); | |
| 82 | 8763 | worlds.erase(std::ranges::unique(worlds).begin(), worlds.end()); | |
| 83 | 8763 | } | |
| 84 | |||
| 85 | 1167 | void TableVersions::Trim(uint64_t seen) { | |
| 86 | 8733 | std::erase_if(m_entries, [&](const Entry& entry) { return entry.sequence <= seen; }); | |
| 87 | 1167 | } | |
| 88 | |||
| 89 | } | ||
| 90 |