scene/tables/storage.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <algorithm> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <cstring> | ||
| 7 | #include <string> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "chrono_batch_render/device/buffer.h" | ||
| 11 | #include "chrono_batch_render/device/device.h" | ||
| 12 | #include "chrono_batch_render/device/queue.h" | ||
| 13 | #include "chrono_batch_render/device/ref.h" | ||
| 14 | |||
| 15 | namespace chrono::batch_render { | ||
| 16 | |||
| 17 | template <class T> class RecordTable { | ||
| 18 | public: | ||
| 19 | 4028 | explicit RecordTable(std::string label) : m_label(std::move(label)) {} | |
| 20 | |||
| 21 | 975 | void Assign(uint32_t rows, const T& value) { | |
| 22 | 975 | m_rows.assign(rows, value); | |
| 23 | 975 | m_dirty.clear(); | |
| 24 | 975 | m_grown = true; | |
| 25 | 975 | } | |
| 26 | |||
| 27 | 36510 | void Reserve(uint32_t rows) { | |
| 28 | 36510 | if (rows > m_rows.size()) { | |
| 29 | 4184 | m_rows.resize(std::max<size_t>(rows, m_rows.size() * 2), T{}); | |
| 30 | 3603 | m_grown = true; | |
| 31 | } | ||
| 32 | 36510 | } | |
| 33 | |||
| 34 | 26642 | bool Set(uint32_t row, const T& value) { | |
| 35 | 26642 | Reserve(row + 1); | |
| 36 | 26642 | if (std::memcmp(&m_rows[row], &value, sizeof(T)) == 0) { | |
| 37 | return false; | ||
| 38 | } | ||
| 39 | 7863 | m_rows[row] = value; | |
| 40 | 7863 | m_dirty.push_back(row); | |
| 41 | 7863 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 763 | const T& Row(uint32_t row) const { | |
| 45 | static const T kEmpty{}; | ||
| 46 | 762 | return row < m_rows.size() ? m_rows[row] : kEmpty; | |
| 47 | } | ||
| 48 | |||
| 49 | 4789 | uint32_t Rows() const { | |
| 50 | 4789 | return uint32_t(m_rows.size()); | |
| 51 | } | ||
| 52 | |||
| 53 | 5611 | bool Upload(device::Device& device) { | |
| 54 | 5611 | Reserve(1); | |
| 55 | 5611 | device::Queue& queue = device.GetQueue(); | |
| 56 | 5611 | const bool replaced = m_grown || !m_buffer; | |
| 57 | if (replaced) { | ||
| 58 | 3439 | m_buffer = device.CreateBuffer({.bytes = m_rows.size() * sizeof(T), .label = m_label}); | |
| 59 | 3310 | queue.Write(*m_buffer, m_rows.data()); | |
| 60 | 2301 | } else if (m_dirty.size() * 4 > m_rows.size()) { | |
| 61 | 11 | queue.Write(*m_buffer, m_rows.data()); | |
| 62 | } else { | ||
| 63 | 2290 | std::ranges::sort(m_dirty); | |
| 64 | 2290 | m_dirty.erase(std::ranges::unique(m_dirty).begin(), m_dirty.end()); | |
| 65 | 2653 | for (const uint32_t row : m_dirty) { | |
| 66 | 363 | queue.Write(m_buffer->Slice(uint64_t(row) * sizeof(T), sizeof(T)), &m_rows[row]); | |
| 67 | } | ||
| 68 | } | ||
| 69 | 5611 | m_dirty.clear(); | |
| 70 | 5611 | m_grown = false; | |
| 71 | 5611 | return replaced; | |
| 72 | 3310 | } | |
| 73 | |||
| 74 | const Ref<device::Buffer>& Buffer() const { | ||
| 75 | 14309 | return m_buffer; | |
| 76 | } | ||
| 77 | |||
| 78 | private: | ||
| 79 | std::string m_label; | ||
| 80 | std::vector<T> m_rows; | ||
| 81 | std::vector<uint32_t> m_dirty; | ||
| 82 | Ref<device::Buffer> m_buffer; | ||
| 83 | bool m_grown = false; | ||
| 84 | }; | ||
| 85 | |||
| 86 | template <class T> class DeviceTable { | ||
| 87 | public: | ||
| 88 | 8416 | explicit DeviceTable(std::string label, device::BufferUsage usage = device::BufferUsage::None) | |
| 89 | 8416 | : m_label(std::move(label)), m_usage(usage) {} | |
| 90 | |||
| 91 | 6528 | void Allocate(device::Device& device, uint32_t rows) { | |
| 92 | 6528 | m_rows = std::max(rows, 1u); | |
| 93 | 6722 | m_buffer = device.CreateBuffer( | |
| 94 | 6528 | {.bytes = uint64_t(m_rows) * sizeof(T), .usage = m_usage, .label = m_label}); | |
| 95 | 13056 | } | |
| 96 | |||
| 97 | 7692 | bool Reserve(device::Device& device, uint32_t rows) { | |
| 98 | 7692 | if (m_buffer && rows <= m_rows) { | |
| 99 | return false; | ||
| 100 | } | ||
| 101 | 4033 | Allocate(device, rows); | |
| 102 | 4033 | return true; | |
| 103 | } | ||
| 104 | |||
| 105 | 2695 | uint32_t Rows() const { | |
| 106 | 2695 | return m_buffer ? m_rows : 0; | |
| 107 | } | ||
| 108 | |||
| 109 | 26163 | uint64_t Address() const { | |
| 110 | 11463 | return m_buffer ? m_buffer->Address() : 0; | |
| 111 | } | ||
| 112 | |||
| 113 | 274 | const Ref<device::Buffer>& Buffer() const { | |
| 114 | 10901 | return m_buffer; | |
| 115 | } | ||
| 116 | |||
| 117 | private: | ||
| 118 | std::string m_label; | ||
| 119 | device::BufferUsage m_usage; | ||
| 120 | Ref<device::Buffer> m_buffer; | ||
| 121 | uint32_t m_rows = 0; | ||
| 122 | }; | ||
| 123 | |||
| 124 | } | ||
| 125 |