godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.77k stars 21.13k forks source link

Unable to modify/create compute shader code during runtime #96304

Closed thargor6 closed 1 month ago

thargor6 commented 2 months ago

Tested versions

System information

macOS Sonoma - Godot 4.3 stable - Vulkan (Forward+) - Apple M1 Max

Issue description

I'm working on project which needs to modify or create compute shader code. Here I mean not shader parameters, but whole functions (It would be impossible to include all possible combinations, these are affected by the users input).

So, I'm trying to use the RDShaderSource together with the with RenderingDevice::shader_compile_spirv_from_source to compile a compute shader during runtime. But this yields to a RID(0) when I then call RenderingDevice::shader_create_from_spirv

The only thing which works is to load/preload the shader code from the res:// folder. I also tried to load it from the user:// folder (using ResourceLoader::load), but this also does not work.

Steps to reproduce

var source = """
#[compute]
#version 450

<some shader code>

...
"""
var shader_source = RDShaderSource.new()
shader_source.language = RenderingDevice.SHADER_LANGUAGE_GLSL
shader_source.set_stage_source(RenderingDevice.SHADER_STAGE_COMPUTE, source)
# alternative way, seems to work the same):
# shader_source.source_compute = source 

var rd = RenderingServer.create_local_rendering_device()

# returns some RDShaderSPIRV, so seems to work:
var shader_spirv = rd.shader_compile_spirv_from_source(shader_source)

# returns RID(0):
var shader = rd.shader_create_from_spirv(shader_spirv)

# everything else fails because shader seems not be compiled

Minimal reproduction project (MRP)

Mandelbrot.zip

hippogyz commented 2 months ago

It seems you don't need to add the mark #[compute] for your shader code in gdscript.

I removed this mark from your project, and it works well.

Maybe this mark only works for raw glsl file, I guess?

thargor6 commented 1 month ago

Indeed, this works! Awesome, thanks a bunch! :-) Even if this resolves the problem completely for me, I will keep the issue open. Maybe the Godot team wants to add some hint to the docs or add some warning message somewhere.

jsjtxietian commented 1 month ago

There is some error in debugger, just not very helpful

image

Checking the code, it looks like shader_compile_spirv_from_source is called after strip all the stage info