How to import KTX2 compressed texture with UASTC or ETC1S format in Swift?

110 Views Asked by At

I'm trying to import KTX2 compressed textures with UASTC and ETC1S compression format into my iPad app, because at runtime I have to download numerous textures to apply to 3D models and simple jpg or png formats cause VRAM saturation on older devices like iPad seventh generation resulting in crashes.

This is my function to import the textures:

func loadKTX2Texture(device: MTLDevice, textureURL: URL) -> MTLTexture? {
   let textureLoader = MTKTextureLoader(device: device)
        
   do {
       let ktx2Texture = try textureLoader.newTexture(URL: textureURL, options: nil)
       return ktx2Texture
   } catch let e {
       print(e)
       return nil
   }
}

I'm trying also to import them with some options found somewhere:

let options: [MTKTextureLoader.Option : Any] = [
    MTKTextureLoader.Option.textureUsage: MTLTextureUsage.shaderRead,
    MTKTextureLoader.Option.SRGB: true
]

The result is that every texture I'm loading returns the error "Image decoding failed".

I'm also trying different compression formats, like ETC1S and ASCT, but it still happens. It seems that only PVRTC compression formats with .pvr extension work well but I wouldn't want to use it because we have other 3D engines that already use UASTC textures and I would like to maintain compatibility.

Do you have any ideas on how to solve this problem?

0

There are 0 best solutions below