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

**URL:** <https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037>\
**Category:** Bugs\
**Created:** [June 30, 2021, 9:54pm UTC](https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037 "2021-06-30T21:54:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blitzy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blitzy/32/17126_2.png) [@Blitzy](https://forum.babylonjs.com/u/Blitzy)\
**Post date:** [June 30, 2021, 9:54pm UTC](https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037/1 "2021-06-30T21:54:35Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 30, 2021, 10:08pm UTC](https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037/2 "2021-06-30T22:08:31Z")

</div>

Assigned @Evgeni\_Popov on June 30, 2021, 10:08pm UTC



---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 30, 2021, 10:09pm UTC](https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037/3 "2021-06-30T22:09:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [June 30, 2021, 11:20pm UTC](https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037/4 "2021-06-30T23:20:35Z")

</div>

It seems a bit like this issue:

> <https://github.com/KhronosGroup/KTX-Software/issues/371>
>
> One of our user has occasional problems when loading a .glb file containing .ktx…2 pictures:
> 
> https://playground.babylonjs.com/#CF7E5Q#17
> 
> Open the browser console to see the exception. Sometimes it works, so just hit F5 or the Play button to start it again until it fails. On my computer using Chrome, it fails fairly quickly, either the first time I browse the page or after 2 or 3 F5 refreshes. Note that you won't see the full stack traces I dumped below because I used locally a modified version of our ktx2Decoder component to get the stack traces (I will update the production site so that we can see the full traces in the Playground too).
> 
> I tried to dig a bit and the errors are thrown from inside the \`msc\_basis\_transcoder\` component, either from the \`transcodeImage\` or \`decodePalettes\` call:
> 
> (transcodeImage):
> \`\`\`
> TypeError: Cannot perform Construct on a detached ArrayBuffer
> at new Uint8Array (\<anonymous\>)
> at emval\_allocator\_3 (eval at craftEmvalAllocator (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:1:1), \<anonymous\>:13:11)
> at \_\_emval\_new (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:8:58552)
> at \<anonymous\>:wasm-function\[246\]:0x1d2c0
> at \<anonymous\>:wasm-function\[488\]:0x3585e
> at ha (\<anonymous\>:wasm-function\[296\]:0x2cc1e)
> at Module.dynCall\_iiiiiiii (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:8:63561)
> at dynCall\_iiiiiiii\_1 (eval at makeDynCaller (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:1:1), \<anonymous\>:4:12)
> at BasisLzEtc1sImageTranscoder$transcodeImage \[as transcodeImage\] (eval at new\_ (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:1:1), \<anonymous\>:13:10)
> at http://localhost:1338/dist/preview%20release/babylon.ktx2Decoder.js:1:16911
> \`\`\`
> or (decodePalettes):
> \`\`\`
> TypeError: Cannot perform Construct on a detached ArrayBuffer
> at new Uint8Array (\<anonymous\>)
> at emval\_allocator\_3 (eval at craftEmvalAllocator (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:1:1), \<anonymous\>:13:11)
> at \_\_emval\_new (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:8:58552)
> at \<anonymous\>:wasm-function\[261\]:0x2a776
> at \<anonymous\>:wasm-function\[503\]:0x36857
> at ga (\<anonymous\>:wasm-function\[297\]:0x2cc39)
> at Module.dynCall\_iiiiiii (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:8:63452)
> at dynCall\_iiiiiii\_1 (eval at makeDynCaller (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:1:1), \<anonymous\>:4:12)
> at BasisLzEtc1sImageTranscoder$decodePalettes \[as decodePalettes\] (eval at new\_ (http://localhost:1338/dist/preview%20release/ktx2Transcoders/msc\_basis\_transcoder.js:1:1), \<anonymous\>:12:10)
> at http://localhost:1338/dist/preview%20release/babylon.ktx2Decoder.js:1:16615
> \`\`\`
> Note that those stack traces have been produced using the latest version of the msc component (https://github.com/KhronosGroup/KTX-Software/releases/tag/v4.0.0-beta6). The version used in production in the Playground above is a little older but the exceptions we get are the same.
> 
> Also, in the playground above I have set the number of worker threads to 1 to rule out a possible problem coming from multiple threads instancing the msc component.
> 
> So, maybe we are not doing the right things when using the component(?), even if I think the implementation is quite straightforward:
> https://github.com/BabylonJS/Babylon.js/blob/master/ktx2Decoder/src/Transcoders/mscTranscoder.ts
> 
> Note that the .glb file is using only the ETC1S path.
> 
> Could you have a look on your side and tell us what you think? One thing that would help I think would be to know what is this buffer that is detached and not valid anymore: is it a buffer from our side (I don't think so, though, as sometimes the error comes from a call to \`decodePalettes\` and we don't pass any buffer to this function)?
> 
> Thanks in advance!

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).

---

<div class="post-metadata">

**Author:** ![Blitzy](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/blitzy/32/17126_2.png) [@Blitzy](https://forum.babylonjs.com/u/Blitzy)\
**Post date:** [July 1, 2021, 2:37pm UTC](https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037/5 "2021-07-01T14:37:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [July 1, 2021, 4:03pm UTC](https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037/6 "2021-07-01T16:03:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [July 4, 2021, 2:04pm UTC](https://forum.babylonjs.com/t/ktx-decode-fails-most-of-the-time-when-loading-many-gltf-files/22037/7 "2021-07-04T14:04:11Z")

</div>

Unassigned @Evgeni\_Popov on July 4, 2021, 2:04pm UTC


