What is a proper MTLRegion param here?

Viewed 86

I need to create a MTLTexture from the UnsafeRawPointer, I try to do it in such a way:

  private func createTexture(frame: UnsafeMutableRawPointer) -> MTLTexture? {
    let textureOjb: UnsafeMutableRawPointer = frame_get_texture_obj(frame)
    let ti: TextureInfo = texture_get_info(textureOjb)
    
    let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(
      pixelFormat: MTLPixelFormat.rgba8Unorm,
      width: Int(ti.width),
      height: Int(ti.height),
      mipmapped: false)
    
    textureDescriptor.usage = [.shaderWrite, .shaderRead]
    
    guard let texture: MTLTexture = device?.makeTexture(descriptor: textureDescriptor) else
    {
      logger?.log(severity: .error, msg: "create texture FAILED.")
      return nil
    }
    
    let data: UnsafeRawPointer = UnsafeRawPointer(ti.pixels)
    
    texture.replace(region: <#T##MTLRegion#>, mipmapLevel: 0, withBytes: data, bytesPerRow: Int(ti.stride))
    
    return texture
  }

According to the documentation, it is the correct approach, so, first of all, I created MTLTextureDescriptor, then I created a MTLTexture and finally I need to pass my pixels data in order to do it I use replace method.

The problem that I don't understand what is the first param here region? What should I pass there?

1 Answers

Use a MTLRegion structure to identify which part of the texture you want to update. This sample populates the entire texture with image data; so create a region that covers the entire texture.

MTLRegion region = {
    { 0, 0, 0 },                       // MTLOrigin
    {texture.width, texture.height, 1} // MTLSize
};

Swift:

let region = MTLRegion.init(origin: MTLOrigin.init(x: 0, y: 0, z: 0), size: MTLSize.init(width: texture.width, height: texture.height, depth: 1));
Related