44 this->buffer->retain();
57 this->gpu = other.
gpu;
61 this->buffer = other.
buffer;
76 this->buffer =
nullptr;
88 this->buffer->autorelease();
101 throw std::runtime_error(
"Buffer already freed");
103 return (T *)this->buffer->contents();
115 throw std::runtime_error(
"Buffer already freed");
117 if (index >= this->
length) {
118 throw std::out_of_range(
"Index out of bounds");
120 return ((T *)this->buffer->contents())[index];
132 throw std::runtime_error(
"Buffer already freed");
134 if (index >= this->
length) {
135 throw std::out_of_range(
"Index out of bounds");
137 return ((T *)this->buffer->contents())[index];
148 throw std::runtime_error(
"Buffer already freed");
150 if (data.size() != this->length) {
151 throw std::invalid_argument(
"Data size does not match buffer size");
153 memcpy(this->buffer->contents(), data.data(), this->length*this->itemsize);
156 this->buffer->didModifyRange(NS::Range(0, this->
length*this->
itemsize));
183 throw std::runtime_error(
"Buffer already freed");
186 throw std::runtime_error(
"Buffer not initialized");
188 std::vector<T> data(this->
length);
189 memcpy(data.data(), this->buffer->contents(), this->length*this->itemsize);
Definition MTLComputeBuffer.hpp:10
MTL::Device * gpu
The Metal device object.
Definition MTLComputeBuffer.hpp:12
Buffer< T > & operator=(const Buffer &other)
Overload the = operator to set buffer contents from another buffer.
Definition MTLComputeBuffer.hpp:166
T * contents()
Get the contents of the buffer.
Definition MTLComputeBuffer.hpp:99
void swap(Buffer &buffer) noexcept
Definition MTLComputeBuffer.hpp:17
bool getFreed()
Get whether the buffer has been freed.
Definition MTLComputeBuffer.hpp:219
T operator[](size_t index) const
Overload the [] operator to get the value at an index.
Definition MTLComputeBuffer.hpp:113
std::vector< T > getData()
Get the data from the buffer as a vector.
Definition MTLComputeBuffer.hpp:181
MTL::Device * getGPU()
Get the GPU device.
Definition MTLComputeBuffer.hpp:209
bool freed
Whether the buffer has been freed.
Definition MTLComputeBuffer.hpp:14
MTLCompute::ResourceStorage storageMode
The storage mode of the buffer.
Definition MTLComputeBuffer.hpp:15
size_t itemsize
The size of each item in the buffer.
Definition MTLComputeBuffer.hpp:234
Buffer(const Buffer &other)
Copy constructor for the Buffer class.
Definition MTLComputeBuffer.hpp:56
~Buffer()
Destructor for the Buffer class.
Definition MTLComputeBuffer.hpp:86
Buffer(MTL::Device *gpu, size_t length, ResourceStorage storageMode)
Constructor for the Buffer class.
Definition MTLComputeBuffer.hpp:38
MTLCompute::ResourceStorage getStorageMode()
Get the storage mode of the buffer.
Definition MTLComputeBuffer.hpp:229
MTL::Buffer * buffer
The Metal buffer object.
Definition MTLComputeBuffer.hpp:13
size_t length
The length of the buffer.
Definition MTLComputeBuffer.hpp:233
T & operator[](size_t index)
Overload the [] operator to set the value at an index.
Definition MTLComputeBuffer.hpp:130
MTL::Buffer * getBuffer()
Get the MTL::Buffer object.
Definition MTLComputeBuffer.hpp:199
Buffer()
Default constructor for the Buffer class.
Definition MTLComputeBuffer.hpp:71
void operator=(std::vector< T > data)
Overload the = operator to set buffer contents from a vector.
Definition MTLComputeBuffer.hpp:146
Definition MTLComputeBuffer.hpp:7
ResourceStorage
Definition MTLComputeGlobals.hpp:16