How to detect shader compilation errors at runtime?

Godot Version

4.2.1

Question

Hi there, I am creating Shader resources (from .gdshader files or by setting the code property) at runtime, and would like to be able to detect whether there are errors (i.e. if shader compilation failed).

In the docs and forums, I’ve not found any way to query a Shader (or its internal RID using RenderServer) in a way that reports its “validity”. I do see compile errors reported in the Output window, but I can’t find any method to check for success, or any signal for which to register.

Any suggestions on how I can detect shader compilation at runtime?

Thanks,
Glen.

The hack way is to always have at least one uniform in the shader. Then check if there are any valid uniforms:

var shader = Shader.new()
shader.code = "shader_type canvas_item;\nuniform vec4 hello;\nvoid wrong() { return 1; }"
if shader.get_shader_uniform_list().is_empty():
	print('Invalid shader probably.')
else:
	print('Valid shader most likely.')

The real way is to expose relevant methods and compile your own custom build of godot. I am surprised that getting an error isn’t exposed. I wonder why that is?

Here’s a relevant line which shows how to get the error from the shader preprocessor at least: godot/scene/resources/shader.cpp at master · godotengine/godot · GitHub

Didn’t dig deeper to find how to compile the shader.

1 Like

Thanks @indicainkwell, I’d also thought about using uniforms as a check (and it does indeed work on the laptop I’m using now, even if I inject a dummy unreferenced uniform). The problem is that you probably can’t rely on that, because (at least on some drivers) the shader compiler may optimize out unused uniforms, so even if they appear in the shader code, they may not appear as uniforms when you query the shader. At least, I’ve seen that occur in OpenGL with NVIDIA hardware which seems to have a pretty aggressive optimizer; I assume it could be the same with Godot’s Vulkan or OGL ES shaders – but maybe not, maybe the Godot preprocessor actually “extracts” the uniforms regardless.

Anyhow, thanks for your answer, if I know the shader has uniforms (most do) then I’ll use the existence of uniforms as a sanity check, as you suggest; I’m not going to get into making my own patched version of Godot for now. :wink:

1 Like

Somewhat related to this issue.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.