godotengine / godot

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

RDShaderFile resource sometimes being loaded as TextFile #86188

Open blackears opened 6 months ago

blackears commented 6 months ago

Tested versions

4.2 stable

System information

Windows 10 Mobile

Issue description

For some reason, RDShaderFile .glsl file resources are sometimes being loaded as TextFile. The Import window lists them as GLSL Shader Files and .import files list them as being RDShaderFile. And everything is working fine in the test project I assembled too. However, in my main project I keep getting the error

res://scripts/glsl/glsl_shader_program.gd:51 - Trying to assign value of type 'TextFile' to a variable of type 'RDShaderFile'.

Steps to reproduce

Unfortunately I cannot consistently reproduce this and my MRP doesn't reproduce it at all, but I'm doing exactly the same things as my main project. I'm selecting my "my_glsl_program.tres" resource, clicking on the Shader Source parameter in the Inspector, browsing to one of my GLSL resource files and pressing Enter. In the MRP this works, but in my main program half the time this returns a RDShaderFile and half the time it returns a TextFile with the same file sometimes loading as one and sometimes as the other.

Minimal reproduction project (MRP)

load_shader_bug.zip

blackears commented 6 months ago

This happens in other contexts too when I try to load glsl shaders as @tool and use them in the editor. Sometimes they're imported as TextFile and break the logic. This error does not happen when I run the same code as a program.

jsjtxietian commented 6 months ago

In the MRP this works

Same for me, can't reproduce it. Maybe there are other conditions affect it as well.

clayjohn commented 6 months ago

Do you by chance have the .GLSL file open in the script editor in your main project, but not in your MRP?

blackears commented 6 months ago

I think you're right. The error seems to occur when the glsl shader is open in the text editor.

jsjtxietian commented 6 months ago

@blackears you can try add ResourceLoader.CACHE_MODE_IGNORE to your ResourceLoader.load and see if it fixes the problem.

After some quich check I think maybe it's because the default flag CACHE_MODE_REUSE, when the glsl file is loaded by script editor it's recognized as a text file and get cached, so if you load again it uses the cache one, which is a textFile.