I was wondering why our website is rather slow to launch, often taking multiple seconds until it is responsive. After some investigating, here’s what I found
We’re using the Babylonjs WebGPU backend and are writing our shaders in WGSL.
However, a few of the Babylonjs built-in shaders are currently written in GLSL, and Babylon converts them at runtime.
This means
shipping GLSL shaders
then starting up shaderc/glslc that has been compiled to WASM
turning the GLSL into optimized spirv <== takes over 3 seconds
then starting up some other WASM binary
turning the spirv into WGSL
and only then does the browser get a WGSL shader
I pointed the browser developer tools at a page running in development mode, since that’s what’s affecting our workflow the most. This can also be reproduced by going to the Babylon.js Playground and using the browser’s profiler.
Yes:) this is a pain. We need to support backward compat so shaders need to be in webgl for now. That being said I’m super open to all good ideas and one that you mentioned and that I like a lot: caching them in local storage.
We also discussed internally about precompiling some shaders but the permutations are so high that it is not doable.
But caching could be a way: it would be expensive only the first time (and we could cache using the engine version so if a new version is used we could wipe the cache)
I love the caching idea and I think for targetted scenari only maybe there could be a cache download/share option ? like run the full game once, share the cache so all the useful (current permutations) should be warmed up ?
Not saying this is the way to go, but it might be a nice intermediate workaround ?
@noname0310 It won’t be possible until we switch to WGSL as our main shader language, because we can’t / don’t want to maintain two different shader code base (GLSL and WGSL).
If we could transpile WGSL to GLSL or vice versa at build time, it would be possible to support two backends (GLSL, WebGPU) with just one code and there’s no runtime transpilation overheads
And for babylon.js to be more sustainable, it seems important to base it on WGSL rather than converting it from GLSL to WGSL.
Because once a system is built, it’s hard to fundamentally change it for backwards compatibility.
and a potentially more involved thing to experiment with would be using naga. I’m not entirely sure how good Naga’s translation capabilities are though. Naga should be faster at translating though.