godotengine / godot

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

GLSL compute shader does not recompile when updated #72281

Open mlepage opened 1 year ago

mlepage commented 1 year ago

Godot version

4.0 beta 16

System information

Windows 11

Issue description

I follow the instructions as in the example https://docs.godotengine.org/en/latest/tutorials/shaders/compute_shaders.html

and it works and outputs the values doubled.

I edit the shader so instead of doubling the values (*= 2.0) it triples the values (*= 3.0).

I run the program but the values are still doubled (as before), not tripled (as updated).

In fact, if I type garbage into the glsl file, it still works. It's not updating the compiled version of the shader.

If I rename the .glsl resource file to .txt then back to .glsl, it recompiles the shader and then I see the updated values. But this renaming dance should not be necessary when updating the shader code.

Steps to reproduce

https://docs.godotengine.org/en/latest/tutorials/shaders/compute_shaders.html

Then edit it to triple instead of double, and run it again.

Minimal reproduction project

N/A

clayjohn commented 1 year ago

I think you need to reimport the GLSL file if it changes after you imported it originally.

mlepage commented 1 year ago

At one point I did see some kind of error in the output along the lines of "shader changed signal" so maybe it is somehow trying to update, but failing. I don't know enough about the pipeline to know how it is supposed to recompile.

I made the shader entirely in Godot 4 editor and am working on it with the corresponding gdscript at the same time. Is there an easy way to reimport the GLSL file?

clayjohn commented 1 year ago

At one point I did see some kind of error in the output along the lines of "shader changed signal" so maybe it is somehow trying to update, but failing. I don't know enough about the pipeline to know how it is supposed to recompile.

I made the shader entirely in Godot 4 editor and am working on it with the corresponding gdscript at the same time. Is there an easy way to reimport the GLSL file?

Yes, you click on the file in the asset explorer, then click import > reimport, just like you would with any other resource

mlepage commented 1 year ago

OK I found the import tab and that does help. Should it auto import when the underlying GLSL file (which is in the project) is edited in the editor? Or is that not expected?

clayjohn commented 1 year ago

OK I found the import tab and that does help. Should it auto import when the underlying GLSL file (which is in the project) is edited in the editor? Or is that not expected?

I don't think so. We don't have a built in editor for custom glsl files, they are meant to be edited in an external program. For Godot shaders we have a dedicated editor which allows the engine to automatically detect changes and recompile the shader, but we don't have anything like that for custom shaders.

mlepage commented 1 year ago

OK so this is working as expected and not a bug?

Calinou commented 1 year ago

We don't have a built in editor for custom glsl files, they are meant to be edited in an external program

Could something similar to https://github.com/godotengine/godot/pull/72259 be done to allow editing GLSL files in the script editor (not the shader editor)?

bitsawer commented 1 year ago

@mlepage I would still consider this a bug. I think if the Godot editor supports a certain file type it should automatically re-import it if the file changes in the filesytem, no matter if that change is made by an external or internal program (re-import should then happen when the editor scans the filesystem on focus gain, save file etc.). If the automatic re-import does not happen it sounds like a bug, unless I misunderstood something. Otherwise teams would have nightmarish source-control updates where they would have to go through every changed file and manually re-import them or risk using out-of-sync data.

edit: after some testing, this might not be a bug after all, although it's a bit of an usability improvement issue. I think the rename from .txt to .glsl does some strange stuff and there are some some temporary editor errors coming from somewhere (like "Attempt to disconnect a nonexistent connection from '<RDShaderFile#-9223370268771029076>'. Signal: 'changed', callable: 'ShaderFileEditor::_shader_changed'.") , but I think currently there is no support for editing .glsl files using the built-in Godot text editor as clayjohn told (although that would be a nice feature). Editing and saving using external text editor seems to work fine, the edits are noticed by Godot once it rescans the files.

mlepage commented 1 year ago

We don't have a built in editor for custom glsl files, they are meant to be edited in an external program

Could something similar to #72259 be done to allow editing GLSL files in the script editor (not the shader editor)?

Yes exactly, I've been editing them in the script editor.

clayjohn commented 1 year ago

I think right now editing in the script editor is kind of hacky and isn't a supported workflow. I agree it would be nice to edit them in the script editor and have Godot automatically reimport the shader when it changes. So I am marking this as an enhancement discussion

Calinou commented 1 year ago

Yes exactly, I've been editing them in the script editor.

I'm not referring to using the current hacky approach, but doing something similar to https://github.com/godotengine/godot/pull/72259 so that a resource file can be edited properly in the script editor. (Like GLSL, JSON is considered a resource in 4.0, and is imported by Godot.)

kakatoto-collab commented 7 months ago

I am having the same problem with Godot 4.2.beta5..

I include my custom math library witht the comma,nd #include "MathLibrary.glsl" but most of the time, when I change something in the math library, it takes some time before the changements are effective.

It makes the compute shaders very hard to use for now in Godot.

Go-Ba commented 2 months ago

Came from #72718 as that person was having a similar problem to me when I went searching for it in the discord history.

I had an issue where the .glsl file would not even show me what compilation errors it had when selecting it in the FileSystem dock and would show up as just Resource instead of RDShaderFile until I deleted the shader and made a new .glsl file with the same code. I was editing the files in VSCode and not the in-built script editor. I had no issues with the shader recompiling on changes made in VSCode after doing the fix stated above. v4.2.1.stable.mono.official [b09f793f5]

Flarkk commented 2 months ago

At one point I did see some kind of error in the output along the lines of "shader changed signal" so maybe it is somehow trying to update, but failing. I don't know enough about the pipeline to know how it is supposed to recompile. I made the shader entirely in Godot 4 editor and am working on it with the corresponding gdscript at the same time. Is there an easy way to reimport the GLSL file?

Yes, you click on the file in the asset explorer, then click import > reimport, just like you would with any other resource

Is reimporting supposed to trigger the changed signal on the resource instances ? I'm speaking here in the context of editor development, where one need to trigger some action (typically re-run some compute shaders to bake in textures) each time the user changes the GLSL shader code. I saw emit_changed() is called especially in RDShaderFile::set_bytecode() but from what I experimented with so far the signal seem not to be triggered on reimport. Is RDShaderFile::set_bytecode() called at all when a GLSL file is reimported ? Not sure how close the above is related to this specific ticket. Any view welcome.