stage/columns/array_pool.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #pragma once | ||
| 3 | |||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <span> | ||
| 7 | #include <string> | ||
| 8 | #include <unordered_map> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "chrono_batch_render/stage/columns/device_heap.h" | ||
| 12 | #include "chrono_batch_render/stage/schema/types.h" | ||
| 13 | |||
| 14 | namespace chrono::batch_render { | ||
| 15 | |||
| 16 | class ArrayPool { | ||
| 17 | public: | ||
| 18 | struct StagedCopy { | ||
| 19 | uint64_t dst; | ||
| 20 | uint64_t source; | ||
| 21 | uint32_t words; | ||
| 22 | }; | ||
| 23 | |||
| 24 | ArrayPool(DeviceHeap& heap, std::string label, uint32_t itemBytes); | ||
| 25 | |||
| 26 | 1976 | uint32_t ItemBytes() const { | |
| 27 | 1976 | return m_itemBytes; | |
| 28 | } | ||
| 29 | |||
| 30 | 5 | uint32_t ArrayCount() const { | |
| 31 | 5 | return uint32_t(m_arrays.size()); | |
| 32 | } | ||
| 33 | |||
| 34 | ArrayRef Allocate(uint32_t count, uint32_t capacity = 0); | ||
| 35 | std::vector<ArrayRef> AllocateRun(std::span<const uint32_t> counts); | ||
| 36 | std::vector<ArrayRef> Borrow(Ref<device::Buffer> buffer, uint32_t arrays, uint32_t count); | ||
| 37 | bool Borrowed(const ArrayRef& array) const; | ||
| 38 | ArrayRef Share(const ArrayRef& array); | ||
| 39 | void Free(const ArrayRef& array); | ||
| 40 | uint32_t References(const ArrayRef& array) const; | ||
| 41 | |||
| 42 | device::BufferRange Range(const ArrayRef& array) const; | ||
| 43 | void Write(const ArrayRef& array, std::span<const std::byte> bytes); | ||
| 44 | void Stage(const ArrayRef& array, std::span<const std::byte> bytes); | ||
| 45 | |||
| 46 | 144241 | std::span<const std::byte> StagedBytes() const { | |
| 47 | 144241 | return m_stagedBytes; | |
| 48 | } | ||
| 49 | |||
| 50 | std::vector<StagedCopy> TakeStaged(uint64_t base); | ||
| 51 | std::vector<std::byte> Read(const ArrayRef& array); | ||
| 52 | std::vector<std::vector<std::byte>> Read(std::span<const ArrayRef> arrays); | ||
| 53 | |||
| 54 | private: | ||
| 55 | 12125 | struct Run { | |
| 56 | HeapBlock block; | ||
| 57 | uint32_t live = 0; | ||
| 58 | Ref<device::Buffer> borrowed; | ||
| 59 | }; | ||
| 60 | |||
| 61 | struct Entry { | ||
| 62 | uint32_t run = 0; | ||
| 63 | uint32_t references = 0; | ||
| 64 | }; | ||
| 65 | |||
| 66 | struct Staged { | ||
| 67 | ArrayRef array; | ||
| 68 | uint64_t offset = 0; | ||
| 69 | uint64_t bytes = 0; | ||
| 70 | }; | ||
| 71 | |||
| 72 | void FlushStaged(); | ||
| 73 | const Entry& Lookup(const ArrayRef& array) const; | ||
| 74 | uint32_t AddRun(HeapBlock block, uint32_t live); | ||
| 75 | |||
| 76 | DeviceHeap& m_heap; | ||
| 77 | std::string m_label; | ||
| 78 | uint32_t m_itemBytes; | ||
| 79 | std::vector<Run> m_runs; | ||
| 80 | std::vector<uint32_t> m_freeRuns; | ||
| 81 | std::unordered_map<uint64_t, Entry> m_arrays; | ||
| 82 | std::vector<Staged> m_staged; | ||
| 83 | std::vector<std::byte> m_stagedBytes; | ||
| 84 | std::unordered_map<uint64_t, size_t> m_stagedIndices; | ||
| 85 | }; | ||
| 86 | |||
| 87 | } | ||
| 88 |