MetalCompute 1.0
An API to make GPU compute calls easier
Loading...
Searching...
No Matches
MTLComputeBuffer.hpp
Go to the documentation of this file.
1#include <iostream>
2#include <vector>
4
5#pragma once
6
7namespace MTLCompute {
8
9 template<typename T>
10 class Buffer {
11 private:
12 MTL::Device *gpu;
13 MTL::Buffer *buffer;
14 bool freed = false;
16
17 void swap(Buffer &buffer) noexcept {
18 using std::swap;
19 swap(this->gpu, buffer.gpu);
20 swap(this->buffer, buffer.buffer);
21 swap(this->length, buffer.length);
22 swap(this->itemsize, buffer.itemsize);
23 swap(this->storageMode, buffer.storageMode);
24 }
25
26 public:
38 Buffer(MTL::Device *gpu, size_t length, ResourceStorage storageMode) {
39 this->gpu = gpu;
40 this->length = length;
41 this->itemsize = sizeof(T);
42 this->storageMode = storageMode;
43 this->buffer = gpu->newBuffer(length*itemsize, static_cast<MTL::ResourceOptions>(storageMode));
44 this->buffer->retain();
45 }
46
47
56 Buffer(const Buffer &other) {
57 this->gpu = other.gpu;
58 this->length = other.length;
59 this->itemsize = other.itemsize;
60 this->storageMode = other.storageMode;
61 this->buffer = other.buffer;
62 }
63
64
72 this->gpu = nullptr;
73 this->length = -1;
74 this->itemsize = -1;
75 this->storageMode = MTLCompute::ResourceStorage::Shared;
76 this->buffer = nullptr;
77 }
78
87 if (!this->freed) {
88 this->buffer->autorelease();
89 this->freed = true;
90 }
91 }
92
99 T *contents() {
100 if (this->freed) {
101 throw std::runtime_error("Buffer already freed");
102 }
103 return (T *)this->buffer->contents();
104 }
105
113 T operator[](size_t index) const {
114 if (this->freed) {
115 throw std::runtime_error("Buffer already freed");
116 }
117 if (index >= this->length) {
118 throw std::out_of_range("Index out of bounds");
119 }
120 return ((T *)this->buffer->contents())[index];
121 }
122
130 T& operator[](size_t index) {
131 if (this->freed) {
132 throw std::runtime_error("Buffer already freed");
133 }
134 if (index >= this->length) {
135 throw std::out_of_range("Index out of bounds");
136 }
137 return ((T *)this->buffer->contents())[index];
138 }
139
146 void operator=(std::vector<T> data) {
147 if (this->freed) {
148 throw std::runtime_error("Buffer already freed");
149 }
150 if (data.size() != this->length) {
151 throw std::invalid_argument("Data size does not match buffer size");
152 }
153 memcpy(this->buffer->contents(), data.data(), this->length*this->itemsize);
154
155 if (this->storageMode == MTLCompute::ResourceStorage::Managed) {
156 this->buffer->didModifyRange(NS::Range(0, this->length*this->itemsize));
157 }
158 }
159
166 Buffer<T> & operator=(const Buffer &other) {
167 // copy and swap because apparently it's good
168 // i don't know what this is
169 Buffer temp(other);
170 swap(temp);
171
172 return *this;
173 }
174
181 std::vector<T> getData() {
182 if (this->freed) {
183 throw std::runtime_error("Buffer already freed");
184 }
185 if (this->length == -1) {
186 throw std::runtime_error("Buffer not initialized");
187 }
188 std::vector<T> data(this->length);
189 memcpy(data.data(), this->buffer->contents(), this->length*this->itemsize);
190 return data;
191 }
192
199 MTL::Buffer *getBuffer() {
200 return this->buffer;
201 }
202
209 MTL::Device *getGPU() {
210 return this->gpu;
211 }
212
219 bool getFreed() {
220 return this->freed;
221 }
222
232
233 size_t length;
234 size_t itemsize;
235 };
236
237}
238
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