27 swap(this->gpu, tex.gpu);
28 swap(this->texture, tex.texture);
29 swap(this->width, tex.width);
30 swap(this->height, tex.height);
31 swap(this->freed, tex.freed);
42 std::vector<T>
flatten(std::vector<std::vector<T>> &v)
const {
43 std::vector<T> result;
44 for (
auto &inner : v) {
45 result.insert(result.end(), inner.begin(), inner.end());
61 std::vector<std::vector<T>> result(
height, std::vector<T>(
width));
62 for (
int i = 0; i <
height; i++) {
63 for (
int j = 0; j <
width; j++) {
64 result[i][j] = v[i*
width + j];
86 throw std::invalid_argument(
"Texture size too large, max size is 16384");
90 this->descriptor = MTL::TextureDescriptor::alloc()->init();
91 this->descriptor->setTextureType(MTL::TextureType2D);
92 this->descriptor->setPixelFormat(
static_cast<MTL::PixelFormat
>(tt));
95 throw std::invalid_argument(
"Texture type does not match template type");
97 this->descriptor->setWidth(
width);
98 this->descriptor->setHeight(
height);
99 this->texture = this->gpu->newTexture(this->descriptor);
115 throw std::invalid_argument(
"Texture size too large, max size is 16384");
119 this->descriptor = MTL::TextureDescriptor::alloc()->init();
120 this->descriptor->setTextureType(MTL::TextureType2D);
121 if (
typeid(uint8_t) ==
typeid(T)) {
122 this->descriptor->setPixelFormat(MTL::PixelFormatR8Uint);
123 }
else if (
typeid(uint16_t) ==
typeid(T)) {
124 this->descriptor->setPixelFormat(MTL::PixelFormatR16Uint);
125 }
else if (
typeid(uint32_t) ==
typeid(T)) {
126 this->descriptor->setPixelFormat(MTL::PixelFormatR32Uint);
127 }
else if (
typeid(
float) ==
typeid(T)) {
128 this->descriptor->setPixelFormat(MTL::PixelFormatR32Float);
129 }
else if (
typeid(int8_t) ==
typeid(T)) {
130 this->descriptor->setPixelFormat(MTL::PixelFormatR8Sint);
131 }
else if (
typeid(int16_t) ==
typeid(T)) {
132 this->descriptor->setPixelFormat(MTL::PixelFormatR16Sint);
133 }
else if (
typeid(int32_t) ==
typeid(T)) {
134 this->descriptor->setPixelFormat(MTL::PixelFormatR32Sint);
135 }
else if (
typeid(
float) ==
typeid(T)) {
136 this->descriptor->setPixelFormat(MTL::PixelFormatR32Float);
138 throw std::invalid_argument(
"Texture type not supported");
140 this->descriptor->setWidth(
width);
141 this->descriptor->setHeight(
height);
142 this->texture = this->gpu->newTexture(this->descriptor);
155 this->gpu = other.
gpu;
157 this->width = other.
width;
158 this->height = other.
height;
171 this->descriptor =
nullptr;
172 this->texture =
nullptr;
186 this->texture->autorelease();
187 this->descriptor->autorelease();
200 throw std::runtime_error(
"Texture already freed");
202 if (data.size() != this->height || data[0].size() != this->width) {
203 throw std::invalid_argument(
"Data size does not match texture size");
206 throw std::invalid_argument(
"Data size too large, max size is 16384");
208 std::vector<T> flat =
flatten(data);
209 this->texture->replaceRegion(MTL::Region::Make2D(0, 0, this->width, this->height), 0, flat.data(), this->width*
sizeof(T));
236 throw std::runtime_error(
"Texture already freed");
238 if (index >= (
long)this->width*(
long)this->height) {
239 throw std::out_of_range(
"Index out of bounds");
241 std::vector<T> flat((
long)this->width*(
long)this->height);
242 this->texture->getBytes(flat.data(), this->width*
sizeof(T), MTL::Region::Make2D(0, 0, this->width, this->height), 0);
243 return unflatten(flat, this->width, this->height)[index];
254 throw std::runtime_error(
"Buffer already freed");
256 if (this->width == -1 || this->height == -1) {
257 throw std::runtime_error(
"Buffer not initialized");
259 std::vector<T> flat((
long)this->width*(
long)this->height);
260 this->texture->getBytes(flat.data(), this->width*
sizeof(T), MTL::Region::Make2D(0, 0, this->width, this->height), 0);
261 return unflatten(flat, this->width, this->height);
Definition MTLComputeTexture.hpp:10
bool getFreed()
Get whether the texture has been freed.
Definition MTLComputeTexture.hpp:290
std::vector< std::vector< T > > unflatten(std::vector< T > &v, int width, int height) const
Unflatten a 1D vector into a 2D vector.
Definition MTLComputeTexture.hpp:60
std::vector< T > flatten(std::vector< std::vector< T > > &v) const
Flatten a 2D vector into a 1D vector.
Definition MTLComputeTexture.hpp:42
int height
The width and height of the texture.
Definition MTLComputeTexture.hpp:16
Texture & operator=(const Texture &other)
Overload the = operator to set texture contents from another texture.
Definition MTLComputeTexture.hpp:218
MTL::Device * getGPU()
Get the GPU device.
Definition MTLComputeTexture.hpp:280
MTL::Texture * getTexture()
Get the MTL::Texture object.
Definition MTLComputeTexture.hpp:270
int getWidth()
Get the width of the texture.
Definition MTLComputeTexture.hpp:310
MTL::TextureDescriptor * getDescriptor()
Get the texture descriptor.
Definition MTLComputeTexture.hpp:300
Texture()
Default constructor for the Texture class.
Definition MTLComputeTexture.hpp:169
void operator=(std::vector< std::vector< T > > data)
Overload the = operator to set texture contents from a vector.
Definition MTLComputeTexture.hpp:198
int width
The width and height of the texture.
Definition MTLComputeTexture.hpp:15
Texture(MTL::Device *gpu, int width, int height)
Constructor for the Texture class.
Definition MTLComputeTexture.hpp:112
std::vector< T > operator[](size_t index) const
Overload the [] operator to get a row from the texture.
Definition MTLComputeTexture.hpp:234
MTL::Device * gpu
The Metal device object.
Definition MTLComputeTexture.hpp:12
void swap(Texture &tex) noexcept
Swap the contents of two Textures.
Definition MTLComputeTexture.hpp:25
std::vector< std::vector< T > > getData()
Get the data from the texture as a vector.
Definition MTLComputeTexture.hpp:252
bool freed
Whether the texture has been freed.
Definition MTLComputeTexture.hpp:17
Texture(MTL::Device *gpu, int width, int height, TextureType tt)
Constructor for the Texture class.
Definition MTLComputeTexture.hpp:83
~Texture()
Destructor for the Texture class.
Definition MTLComputeTexture.hpp:184
Texture(const Texture &other)
Copy constructor for the Texture class.
Definition MTLComputeTexture.hpp:154
int getHeight()
Get the height of the texture.
Definition MTLComputeTexture.hpp:320
MTL::Texture * texture
The Metal texture object.
Definition MTLComputeTexture.hpp:13
MTL::TextureDescriptor * descriptor
The Metal texture descriptor object.
Definition MTLComputeTexture.hpp:14
Definition MTLComputeBuffer.hpp:7
constexpr long MAX_TEXTURE_SIZE
Definition MTLComputeGlobals.hpp:13
std::map< TextureType, size_t > TextureTypeSizes
Definition MTLComputeGlobals.hpp:34
TextureType
Definition MTLComputeGlobals.hpp:22