mlx_image.swift
1 2 3 import Metal 4 5 6 public class MlxImg 7 { 8 public var texture: MTLTexture 9 /// var texture_buff: MTLBuffer 10 11 public var texture_sizeline: Int 12 public var texture_data: UnsafeMutablePointer<UInt32> 13 public var texture_width: Int 14 public var texture_height: Int 15 16 public var onGPU = 0 17 18 convenience public init(d device:MTLDevice, w width:Int, h height:Int) 19 { 20 self.init(d:device, w:width, h:height, t:0) 21 } 22 23 public init(d device:MTLDevice, w width:Int, h height:Int, t target:Int) 24 { 25 texture_width = width 26 texture_height = height 27 texture_sizeline = width * 4 28 texture_sizeline = 256 * (texture_sizeline / 256 + (texture_sizeline%256 >= 1 ? 1 : 0) ) 29 30 let textureDesc = MTLTextureDescriptor() 31 textureDesc.width = texture_width 32 textureDesc.height = texture_height 33 textureDesc.usage = .shaderRead 34 if (target == 1) 35 { 36 textureDesc.usage = .renderTarget 37 textureDesc.storageMode = .private 38 } 39 textureDesc.pixelFormat = MTLPixelFormat.bgra8Unorm 40 let texture_buff = device.makeBuffer(length: texture_sizeline * height)! 41 texture = texture_buff.makeTexture(descriptor:textureDesc, offset:0, bytesPerRow:texture_sizeline)! 42 43 let tmpptr = texture_buff.contents() 44 texture_data = tmpptr.assumingMemoryBound(to:UInt32.self) 45 } 46 47 48 }