Force shader compilation

I’m trying to pre compile a bunch of shaders before creating my scene, but failing so far.

  • can the mesh provided to shaderMaterial.forceCompilation() be any old mesh?

  • how can I use “Parallel Shader Compilation”?

thanks!

Hello !

Are you able to reproduce in a Playground ? Or at least share code, and your error, so that we can help :slight_smile:

there is no error, forceCompilation just doesn’t seem to do anything. The shader is still compiled later, when creating the full scene.

shaderMaterial.onCompiled = () => console.log('shader compiled successfully');
shaderMaterial.forceCompilation(randomBox);

for the second question I have no code, I just saw this:

BJS - [17:14:07]: Babylon.js v7.13.1 - WebGL2 - Parallel shader compilation

The method forceCompilation is not synchronous. You have to wait for the function to complete either by passing a callback or use the async version. The onCompiled callback on the material will only trigger if the material is used while rendering. Even then, it still might not work if you use clip planes or instances. For these cases, you have to pass options to the forceCompilation functions to account for each permutation. If you can repro in a PG, we can diagnose further.

1 Like

ok good to know!

Before I do a repro I would like to check if what I have now works, so I would like to know

  • how can I be sure that the shader is precompiled (and not compiled again on first render)?
  • what kind of mesh does the forceCompilation call need in general? Can I throw a random mesh in this function and then dispose it again?

Thanks!

I’m not sure what you are looking for. Are you asking how to verify that shaders are not being compiled during render? I usually do this by adding some code to the engine, but obviously that’s not going to be easy to do if not building Babylon.js. Maybe @sebavan has some ideas.

You need a mesh that has the same shader defines as the real mesh being rendered (e.g., the mesh has the same properties configured as the real mesh.

I want to know when the work is being done. And yes, by extension, when it’s not being done.
I want to compile my shaders ahead of time, and I need a way to verify if I’m succeeding at that. I can add code to babylon at runtime if that’s what it takes, if you can tell me where to inject that would be great!

ok, thanks!!

You could check in the engine effect cache how many shaders you have: in engine._compiledEffects ?

1 Like