1 Reply
      Latest reply on Apr 6, 2018 4:38 PM by Mikl
      hyperknot Level 1 Level 1 (0 points)

        MTKTextureLoader.newTexture fails to load 16-bit CGImages.


        let path = "test.scnassets/test16.png"
        let image = UIImage(named: path)!
        let textureLoader = MTKTextureLoader(device: defaultDevice)
        let texture = try! textureLoader.newTexture(cgImage: image.cgImage!, options: [:])


        Results in error:

        Error Domain=MTKTextureLoaderErrorDomain Code=0 "Image decoding failed" UserInfo={NSLocalizedDescription=Image decoding failed, MTKTextureLoaderErrorKey=Image decoding failed}


        Now my question is, if not like this, then how can I create a texture, manually, using a proper 16 bit pixel format.

        • Re: MTKTextureLoader fails for 16-big images
          Mikl Level 1 Level 1 (0 points)

          I got it working by looping over the raw UIImage data (data stored as rgb uint8 little endian) and creating a uint16 array to feed the texture with.


          let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: MTLPixelFormat.rgba16Uint, width: imageWidth, height: imageHeight, mipmapped: false) guard let texture: MTLTexture = device.makeTexture(descriptor: textureDescriptor) else { return nil }
          let region = MTLRegionMake2D(0, 0, Int(imageWidth), Int(imageHeight))
          texture.replace(region: region, mipmapLevel: 0, withBytes: &textureData, bytesPerRow: 8 * imageWidth)