0 votes

I've noticed my game has a lag spike whenever an object using a unique material is shown on screen for the first time.

I've read up and learned that OpenGL is "lazily" compiling the shaders when the game is already running, so a when an object using a previously unused material appears, that frame needs to wait until the shader is compiled before it can be rendered - hence stopping the game dead in it's tracks until the shader is ready.

I think these lag spikes are unacceptable as they completely break the flow of the game. So far the only solutions I've heard is to render a frame with all the materials we'll be using in our game to force the shader compilation at the beginning, so they are ready for later.

That sounds like a very crude workaround.

I believe there has to be a better way to deal with this issue. So what is it?

asked Jun 3 in Engine by unfa (178 points)

1 Answer

+1 vote
Best answer

So far the only solutions I've heard is to render a frame with all the materials we'll be using in our game to force the shader compilation at the beginning, so they are ready for later.

No you are right, that's basically the simplest way you would do it in OpenGL. I believe improvements are coming with Vulkan since shaders can be handled differently.
For more information, this was discussed at length here: https://github.com/godotengine/godot/issues/13954

answered Jun 3 by Zylann (26,131 points)
selected Jun 3 by unfa

That's kinda... weird.

I've also noticed that GPU particle effects (which are driven by shaders too) cause big lag spikes. So should I spawn all the game assets when it loads fro one frame let it lag, and be done with it?

Well, looking forward to see the 4.0 I guess...

You need to use a quad and force the renderer to draw each material your game will use. For particle materials, use a particle node. You can do that even hiding it behind a loading screen or an off-screen 1x1 viewport (Godot won't cull it if it's still in front of the camera).

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.