The idea is here is to create a texture of two black and two grey pixels
let TextureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: View.colorPixelFormat, width: 2, height: 2, mipmapped: Settings.Mipmaps > 0)
Texture = Device.makeBuffer(bytes: [UInt8](arrayLiteral: 127, 127, 127, 255, 0, 0, 0, 255, 127, 127, 127, 255, 0, 0, 0, 255), length: 16)?.makeTexture(descriptor: TextureDescriptor, offset: 0, bytesPerRow: 8)
However, first of all, it's complaining that bytesPerRow isn't aligned to 256, and if I change that and the buffer it gives other issues such as resourceOptions (0x10) must match backing buffer resource options (0x0)
, so how can I initialize an MTL texture from an array of numbers, that represent red, green, blue, alpha, and given a texture width and height?
I had to use MTLTexture.replace()
, but seriously, you don't want to store textures like this!
Texture = Device.makeTexture(descriptor: TextureDescriptor)
Texture?.replace(region: MTLRegionMake2D(0, 0, 16, 16), mipmapLevel: 0, slice: 0, withBytes: [UInt8]([127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 127, 127, 127, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 251, 206, 119, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 254, 231, 199, 255, 251, 206, 119, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 104, 104, 104, 255, 127, 127, 127, 255, 254, 231, 199, 255, 245, 236, 93, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 116, 116, 116, 255, 127, 127, 127, 255, 104, 104, 104, 255, 255, 255, 255, 255, 254, 231, 199, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 251, 206, 119, 255, 251, 206, 119, 255, 245, 236, 93, 255, 127, 127, 127, 255, 127, 127, 127, 255, 251, 206, 119, 255, 245, 236, 93, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 254, 231, 199, 255, 251, 206, 119, 255, 104, 104, 104, 255, 143, 143, 143, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 255, 255, 255, 255, 254, 231, 199, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 254, 231, 199, 255, 245, 236, 93, 255, 245, 236, 93, 255, 245, 236, 93, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 116, 116, 116, 255, 251, 206, 119, 255, 143, 143, 143, 255, 143, 143, 143, 255, 116, 116, 116, 255, 104, 104, 104, 255, 116, 116, 116, 255, 143, 143, 143, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 104, 104, 104, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 254, 231, 199, 255, 245, 236, 93, 255, 143, 143, 143, 255, 143, 143, 143, 255, 104, 104, 104, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 254, 231, 199, 255, 255, 255, 255, 255, 254, 231, 199, 255, 251, 206, 119, 255, 251, 206, 119, 255, 245, 236, 93, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 116, 116, 116, 255, 254, 231, 199, 255, 245, 236, 93, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 245, 236, 93, 255, 245, 236, 93, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 104, 104, 104, 255, 143, 143, 143, 255, 143, 143, 143, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 127, 127, 127, 255, 116, 116, 116, 255, 116, 116, 116, 255, 116, 116, 116, 255, 127, 127, 127, 255, 127, 127, 127, 255]), bytesPerRow: 16*4, bytesPerImage: 1024)