device/buffer.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 <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include <dlpack/dlpack.h> | ||
| 12 | |||
| 13 | #include "chrono_batch_render/device/device.h" | ||
| 14 | #include "chrono_batch_render/device/resource.h" | ||
| 15 | #include "chrono_batch_render/export.h" | ||
| 16 | |||
| 17 | namespace chrono::batch_render::device { | ||
| 18 | |||
| 19 | class Device; | ||
| 20 | |||
| 21 | enum class Memory : uint8_t { | ||
| 22 | Device, | ||
| 23 | Upload, | ||
| 24 | Readback, | ||
| 25 | Shared, | ||
| 26 | Virtual, | ||
| 27 | }; | ||
| 28 | |||
| 29 | enum class BufferUsage : uint32_t { | ||
| 30 | None = 0, | ||
| 31 | Indirect = 1u << 0, | ||
| 32 | AccelBuildInput = 1u << 1, | ||
| 33 | AccelStorage = 1u << 2, | ||
| 34 | Interop = 1u << 3, | ||
| 35 | }; | ||
| 36 | |||
| 37 | constexpr BufferUsage operator|(BufferUsage a, BufferUsage b) { | ||
| 38 | return BufferUsage(uint32_t(a) | uint32_t(b)); | ||
| 39 | } | ||
| 40 | |||
| 41 | 437859 | constexpr bool Has(BufferUsage usage, BufferUsage bits) { | |
| 42 | 317876 | return (uint32_t(usage) & uint32_t(bits)) != 0; | |
| 43 | } | ||
| 44 | |||
| 45 | 390451 | struct BufferDesc { | |
| 46 | uint64_t bytes = 0; | ||
| 47 | Memory memory = Memory::Device; | ||
| 48 | BufferUsage usage = BufferUsage::None; | ||
| 49 | std::string label = "buffer"; | ||
| 50 | }; | ||
| 51 | |||
| 52 | constexpr uint64_t kBufferAlignment = 256; | ||
| 53 | |||
| 54 | constexpr uint64_t kWholeBuffer = ~uint64_t(0); | ||
| 55 | |||
| 56 | class BufferRange; | ||
| 57 | |||
| 58 | struct ExternalAddress { | ||
| 59 | DLDevice device{}; | ||
| 60 | uint64_t address = 0; | ||
| 61 | }; | ||
| 62 | |||
| 63 | class CHRONO_BATCH_RENDER_API Buffer : public Resource { | ||
| 64 | public: | ||
| 65 | virtual ExternalAddress External(DLDeviceType api); | ||
| 66 | |||
| 67 | ~Buffer() override; | ||
| 68 | |||
| 69 | 1686891 | uint64_t Bytes() const { | |
| 70 | 1648649 | return m_bytes; | |
| 71 | } | ||
| 72 | |||
| 73 | 675183 | uint64_t Address() const { | |
| 74 | 644721 | return m_address; | |
| 75 | } | ||
| 76 | |||
| 77 | 27968 | void* Mapped() const { | |
| 78 | 25178 | return m_mapped; | |
| 79 | } | ||
| 80 | |||
| 81 | 51252 | Memory GetMemory() const { | |
| 82 | 52311 | return m_memory; | |
| 83 | } | ||
| 84 | |||
| 85 | 197882 | BufferUsage Usage() const { | |
| 86 | 119985 | return m_usage; | |
| 87 | } | ||
| 88 | |||
| 89 | 479935 | bool Virtual() const { | |
| 90 | 479935 | return m_memory == Memory::Virtual; | |
| 91 | } | ||
| 92 | |||
| 93 | uint64_t PageBytes() const { | ||
| 94 | return m_pageBytes; | ||
| 95 | } | ||
| 96 | |||
| 97 | bool FullyBacked() const; | ||
| 98 | |||
| 99 | BufferRange Slice(uint64_t offset, uint64_t bytes = kWholeBuffer); | ||
| 100 | |||
| 101 | protected: | ||
| 102 | Buffer(Device& device, const BufferDesc& desc); | ||
| 103 | |||
| 104 | uint64_t m_bytes; | ||
| 105 | uint64_t m_address = 0; | ||
| 106 | void* m_mapped = nullptr; | ||
| 107 | uint64_t m_pageBytes = 0; | ||
| 108 | |||
| 109 | private: | ||
| 110 | friend class Device; | ||
| 111 | |||
| 112 | Memory m_memory; | ||
| 113 | BufferUsage m_usage; | ||
| 114 | bool m_counted = false; | ||
| 115 | }; | ||
| 116 | |||
| 117 | class BufferRange { | ||
| 118 | public: | ||
| 119 | 485512 | BufferRange(Buffer& buffer) : m_buffer(&buffer), m_bytes(buffer.Bytes()) {} | |
| 120 | |||
| 121 | 906127 | Buffer& GetBuffer() const { | |
| 122 | 876747 | return *m_buffer; | |
| 123 | } | ||
| 124 | |||
| 125 | 1098727 | uint64_t Offset() const { | |
| 126 | 1095937 | return m_offset; | |
| 127 | } | ||
| 128 | |||
| 129 | 1118409 | uint64_t Bytes() const { | |
| 130 | 1118409 | return m_bytes; | |
| 131 | } | ||
| 132 | |||
| 133 | 19904 | uint64_t Address() const { | |
| 134 | 19836 | return m_buffer->Address() + m_offset; | |
| 135 | } | ||
| 136 | |||
| 137 | CHRONO_BATCH_RENDER_API BufferRange Slice(uint64_t offset, uint64_t bytes = kWholeBuffer) const; | ||
| 138 | |||
| 139 | private: | ||
| 140 | friend class Buffer; | ||
| 141 | |||
| 142 | 566785 | BufferRange(Buffer& buffer, uint64_t offset, uint64_t bytes) | |
| 143 | 566785 | : m_buffer(&buffer), m_offset(offset), m_bytes(bytes) {} | |
| 144 | |||
| 145 | Buffer* m_buffer; | ||
| 146 | uint64_t m_offset = 0; | ||
| 147 | uint64_t m_bytes = 0; | ||
| 148 | }; | ||
| 149 | |||
| 150 | 14583 | template <class T> Ref<Buffer> Device::CreateBuffer(BufferDesc desc, Span<const T> values) { | |
| 151 | 29166 | return CreateBuffer(std::move(desc), AsBytes(values)); | |
| 152 | } | ||
| 153 | |||
| 154 | 5898 | template <class T> Ref<Buffer> Device::CreateBuffer(BufferDesc desc, std::span<const T> values) { | |
| 155 | 11796 | return CreateBuffer(std::move(desc), Span<const T>(values)); | |
| 156 | } | ||
| 157 | |||
| 158 | 9898 | template <class T> Ref<Buffer> Device::CreateBuffer(BufferDesc desc, const std::vector<T>& values) { | |
| 159 | 19796 | return CreateBuffer(std::move(desc), Span<const T>(values)); | |
| 160 | } | ||
| 161 | |||
| 162 | } | ||
| 163 |