KTX decode fails (most of the time) when loading many GLTF files

I’m working on a WebXR AR project that loads a bunch of GLTF furniture models into a room. All of these models are utilizing KTX textures with a combination of ETC1S and UASTC (for normal maps).

KTX, along with DRACO geometry compression, has worked really well to decrease the GLTF file sizes. I typically see a 60-80% reduction in file size.

However, once I started trying to load a bunch of these GLTF files with KTX textures together, it will often fail during the KTX decode process with the following error printed to the console:

BJS - [17:26:21]: Failed to load KTX2 texture data: Error: KTX2 container - could not transcode the data. TypeError: Cannot convert "undefined" to unsigned long
TypeError: Cannot convert "undefined" to unsigned long
    at Object.toWireType (https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.js:9:49255)
    at __emval_as (https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.js:9:54139)
    at <anonymous>:wasm-function[196]:0x255a5
    at <anonymous>:wasm-function[190]:0x24ff0
    at BasisLzEtc1sImageTranscoder$decodeTables [as decodeTables] (eval at new_ (https://preview.babylonjs.com/ktx2Transcoders/msc_basis_transcoder.js:9:34844), <anonymous>:9:10)
    at https://preview.babylonjs.com/babylon.ktx2Decoder.js:1:16817
    at async Promise.all (index 7)

This error occurs after it has already successfully loaded a few GLTF models with KTX textures.
I’m not sure exactly what is going on, my instinct says some kind of memory overflow with the decoder(s) if its failing after successfully decoding so many textures from the GLTF files.

It’s also worth noting that all the models load just fine in isolation. Every single GLTF model loads perfectly in the Babylon Sandbox. It is only when trying to load them in succession that this error occurs.

I have tried throwing in an artificial wait time after each model loads to see if that would work around any kind of memory garbage collection issue but it still occurs even with the artificial wait time.

The frustrating part is that this decode error does not occur consistently. In fact it usually occurs 50-70% of the time. The rest of the time, all the models will load in successfully. Even more frustrating is that it appears to occur only 10-20% of the time when the browser tab is being actively inspected by the Chrome DevTools, it occurs most frequently with the device unhooked from USB and DevTools.

This KTX decode error occurs on both my testing Android devices, both fully up-to-date:

  • Google Pixel 4a 5G
  • Samsung Galaxy Note 9

I have so far been unable to replicate this decoding error on Desktop. The scene always loads in on Desktop without issue.

Im currently using Babylon 5.0.0 alpha 29

Also adding @bghgary as it might be related to the compressed format on andriod having some issues ?

It seems a bit like this issue:

Maybe you can try to create an issue in their repo and see if it rings some bells on their side?

Also, you can try to set BABYLON.KhronosTextureContainer2.DefaultNumWorkers = 1; in your program and see if that helps (could be a concurrent problem happening when several worker threads are working in parallel).

2 Likes

Ok, I tried setting KhronosTextureContainer2.DefaultNumWorkers = 1 but unfortunately the same errors occur.

Posting on their repo might definitely be a good idea in this case

1 Like