5 #include "ObjectHandle.hpp"
11 using gpu_addr =
void*;
16 virtual ~
Buffer() { rawFree(); }
18 virtual uint32_t getSize()
const {
return 0; }
19 virtual uint32_t vertices()
const {
return 0; }
21 void allocateBytes(std::size_t bytes) { rawResize(bytes); }
22 auto* rawData()
const {
return reinterpret_cast<std::byte*
>(_data); }
23 auto allocated()
const {
return _size; }
25 MN_SYMBOL gpu_addr getAddress()
const;
28 MN_SYMBOL
void rawResize(std::size_t newsize);
29 MN_SYMBOL
void rawFree();
30 MN_SYMBOL
auto rawSize()
const {
return _size; }
41 uint32_t getSize()
const override {
return sizeof(T); }
42 uint32_t vertices()
const override {
return size(); }
44 std::size_t size()
const
46 MIDNIGHT_ASSERT(rawSize() %
sizeof(T) == 0,
"Buffer incorrectly allocated");
47 return rawSize() /
sizeof(T);
50 T& at(std::size_t index)
52 MIDNIGHT_ASSERT(index < size(),
"Index out of bounds");
53 return *(
reinterpret_cast<T*
>(rawData()) + index);
56 const T& at(std::size_t index)
const
61 T& operator[](std::size_t index)
66 const T& operator[](std::size_t index)
const
71 void resize(std::size_t count)
73 this->rawResize(count *
sizeof(T));
92 Buffer::gpu_addr getAddress()
const
94 return memory.getAddress();
97 void reserve(
const uint32_t& count)
100 for (uint32_t i = count; i < this->count; i++)
101 reinterpret_cast<T*
>(memory.rawData() + i *
sizeof(T))->~T();
103 memory.allocateBytes(count *
sizeof(T));
106 void resize(
const uint32_t& count)
108 if (count == this->count)
return;
111 for (uint32_t i = count; i < this->count; i++)
112 reinterpret_cast<T*
>(memory.rawData() + i *
sizeof(T))->~T();
114 memory.allocateBytes(count *
sizeof(T));
117 for (uint32_t i = this->count; i < count; i++)
118 new(memory.rawData() + i *
sizeof(T)) T();
123 void push_back(
const T& value)
125 if (count == memory.allocated() /
sizeof(T))
126 reserve((memory.allocated() /
sizeof(T) + 5) * 1.75f *
sizeof(T));
127 new(memory.rawData() + count *
sizeof(T)) T(value);
130 template<
typename... Args>
131 void emplace_back(Args&&... args)
133 if (count == memory.allocated() /
sizeof(T))
134 reserve((memory.allocated() /
sizeof(T) + 5) * 1.75f *
sizeof(T));
135 new(memory.rawData() + count *
sizeof(T)) T(std::forward<Args>(args)...);
138 T& at(uint32_t index)
140 return *
reinterpret_cast<T*
>(memory.rawData() + index *
sizeof(T));
143 const T& at(uint32_t index)
const
145 return const_cast<Vector<T>*
>(
this)->at(index);
148 T& operator[](uint32_t index)
153 const T& operator[](uint32_t index)
const
Definition: Buffer.hpp:10
Definition: ObjectHandle.hpp:9
Definition: Buffer.hpp:40
Definition: Buffer.hpp:79
Definition: TypedHandle.hpp:9