BabylonJS / BabylonNative

Build cross-platform native applications with the power of the Babylon.js JavaScript framework
MIT License
761 stars 131 forks source link

GLTF Loader non-cached HTTP requests #538

Open CedricGuillemet opened 3 years ago

CedricGuillemet commented 3 years ago

Context

While debugging the Linux Dead lock, I logged all the xmlHTTPRequest and found the same requests for some .gltf. Textures are loaded multiple times (requests with related allocations). Loaded but not uploaded to VRAM as there is a cache mecanism but too late in the loading pipeline.

Pipeline

Textures found in the .gltf are loaded async in a buffer: https://github.com/BabylonJS/Babylon.js/blob/af28ff68fcf6f782f8dec7f0494d5fced8a2948f/loaders/src/glTF/2.0/glTFLoader.ts#L2020 and https://github.com/BabylonJS/Babylon.js/blob/af28ff68fcf6f782f8dec7f0494d5fced8a2948f/loaders/src/glTF/2.0/glTFLoader.ts#L2096

Then, this buffer is sent to the engine to create a texture with. But a texture cache discards that buffer if the URL is already present: https://github.com/BabylonJS/Babylon.js/blob/b8238023e50655c58361136a88fcd7e88f06772d/src/Materials/Textures/texture.ts#L371

So, some buffers are discarded but the request and loading is done in NativeEngine. I guess there is a cache mecanism in the browser to overcome this kind of issue. Maybe also the OS. But memory allocation and latency is a performance issue. Let alone bandwidth quota on mobile.

Fix proposal

ping @ryantrem

CedricGuillemet commented 3 years ago

You can observe this behavior with GLTF Node NegativeScale (0) test scene : https://www.babylonjs-playground.com/#DS8AA7#19