godotengine / godot

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

Editor crash when setting shader parameter for type uniform sampler2D array ("Attempting to initialize the wrong RID") #84966

Open BrixsterPlanet opened 11 months ago

BrixsterPlanet commented 11 months ago

Godot version

v4.2.beta6.mono.official [64150060f]

System information

Godot v4.2.beta5.mono - Windows 10.0.22621 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 2080 SUPER (NVIDIA; 31.0.15.3742) - Intel(R) Core(TM) i9-9900 CPU @ 3.10GHz (16 Threads)

Godot v4.2.beta6 - Linux Mint 21 (Vanessa) - X11 - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1080 (nvidia; 530.41.03) - Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz (24 Threads)

This was also successfully reproduced on macOS (ARM) and Linux Mint; i.e., this issue is not OS agnostic.

Issue description

Sampler2D arrays are a new feature exclusive to Godot 4, but they appear to have not been tested at all.

While they work without issue when solely utilizing them in code, attempting to set a uniform sampler2D[] in the Godot property inspector will cause a crash on the next editor startup with the error "Attempting to initialize the wrong RID."

For relevant logs, please refer to the attached message in the reproduction project, courtesy of @johnnyninjutsu on Discord.

Steps to reproduce

uniform sampler2D[23] bad_var;


- Assign the shader to a ShaderMaterial in a relevant node, and populate the sampler array. This has been provided in the repro project:

![image](https://github.com/godotengine/godot/assets/58347007/7a5da4f7-6622-4379-8815-e1f2db7d89f1)

- Save and restart Godot. If done correctly, the editor will immediately crash upon reopening, bricking the project until the shader cache and imports are removed.

The crash only occurs after restarting the editor, so ensure you entirely save the project with Ctrl/Cmd + S and restart Godot for a successful repro.

### Minimal reproduction project

[Sampler2dArrayRepro.zip](https://github.com/godotengine/godot/files/13373132/Sampler2dArrayRepro.zip)

Crash log:

ERROR: Attempting to initialize the wrong RID at: get_or_null (./core/templates/rid_owner.h:189) ERROR: Parameter "mem" is null. at: initialize_rid (./core/templates/rid_owner.h:219) ERROR: Parameter "shader" is null. at: shader_set_code (servers/rendering/renderer_rd/storage_rd/material_storage.cpp:1853) ERROR: Parameter "shader" is null. at: shader_set_path_hint (servers/rendering/renderer_rd/storage_rd/material_storage.cpp:1930) ERROR: Initializing already initialized RID at: get_or_null (./core/templates/rid_owner.h:182) ERROR: Parameter "mem" is null. at: initialize_rid (./core/templates/rid_owner.h:214) ERROR: Parameter "shader" is null. at: material_set_shader (servers/rendering/renderer_rd/storage_rd/material_storage.cpp:2108) ERROR: Parameter "_fp" is null. at: _ref (core/variant/array.cpp:55) ERROR: Parameter "_fp" is null. at: _ref (core/variant/array.cpp:55)

================================================================ handle_crash: Program crashed with signal 11 Engine version: Godot Engine v4.2.beta6.official (64150060f89677eaf11229813ae6c5cf8a873802) Dumping the backtrace. Please include this when reporting the bug to the project developer. [1] /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7f002927e520] (??:0) [2] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x326742a] (??:0) [3] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x328f86d] (??:0) [4] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x313c5a7] (??:0) [5] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x46fa0b] (??:0) [6] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x313e49c] (??:0) [7] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x2901875] (??:0) [8] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x4e5488] (??:0) [9] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x39a8c14] (??:0) [10] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x467f68] (??:0) [11] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x3267450] (??:0) [12] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x328f86d] (??:0) [13] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x313c5a7] (??:0) [14] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x46fa0b] (??:0) [15] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x30cd8ac] (??:0) [16] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x18bdf06] (??:0) [17] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x1d70e69] (??:0) [18] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x122f314] (??:0) [19] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x122fd18] (??:0) [20] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x3fd8cfd] (??:0) [21] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x1240bc7] (??:0) [22] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x3b8baa0] (??:0) [23] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0xadb403] (??:0) [24] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x11681dd] (??:0) [25] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x3b264c8] (??:0) [26] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x2087a44] (??:0) [27] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x2088e4b] (??:0) [28] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x208c5d1] (??:0) [29] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x4f932d] (??:0) [30] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x41ed16] (??:0) [31] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7f0029265d90] (??:0) [32] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7f0029265e40] (??:0) [33] /home/john/builds/games/Godot_v4.2-beta6_linux.x86_64() [0x42a96a] (??:0) -- END OF BACKTRACE --



NOTE: The sample production project and crash log were provided by @johnnyninjutsu on the [Godot Engine Discord](https://discord.gg/4JBkykG), who collaborated with me in developing a working repro and writing up this issue. Talk can be found [here](https://discord.com/channels/212250894228652034/1082369329976315964/1082369329976315964).
joined72 commented 11 months ago

Can you test with the latest beta 6, please? Download it here: https://godotengine.org/download/archive/4.2-beta6/

BrixsterPlanet commented 11 months ago

Can you test with the latest beta 6, please? Download it here: https://godotengine.org/download/archive/4.2-beta6/

Successfully reproduced on v4.2.beta6.mono.official [64150060f] - Windows 10.0.22621. Should be applicable on macOS and Linux as well.

joined72 commented 11 months ago

Why you referenced [ 6415006 ] ?

akien-mga commented 11 months ago

Why you referenced [ 6415006 ] ?

That's the commit hash for beta 6.

jsjtxietian commented 11 months ago

Confirmed, but does godot support sampler arrays? #49485 mentioned that "Support for texture samplers will be later" and looks like #77740 "removes sampler arrays in shaders (which are not supported by all devices)" . If it's not supported, we should probably give some error when compile the shader.

But it should not crash, the crash call stack on my machine looks like this:

image

bitsawer commented 11 months ago

I get the same error messages, but I can't repro the crash in any way either with official 4.2 beta 6 or current master build (ce9901ef54537eb81e77667f82a2adc68d1d913c). Windows 10, NVIDIA GTX 970. Rendering seems to work correctly too despite those error messages. Seems to also work with Compatibility renderer with no errors.

Removing .godot folder and opening the project also runs without error messages until next open. Interestingly, only removing .godot/editor/editor_layout.cfg file before every project open seems to be enough to make everything work without issues. The first error ("Attempting to initialize the wrong RID") happens here, so maybe something to do with how the editor is starting up and interacting with the RenderingServer? The stack trace with EditorNode load stuff (including call to EditorNode::_load_editor_layout()) could also hint towards that, but it could also be a false lead.

https://github.com/godotengine/godot/blob/ce9901ef54537eb81e77667f82a2adc68d1d913c/core/templates/rid_owner.h#L185-L190

Full stack trace of that first error ("Attempting to initialize the wrong RID"):

_err_print_error(char const*, char const*, int, char const*, char const*, bool, ErrorHandlerType) (e:\Repositories\godot\core\error\error_macros.cpp:86)
RID_Alloc<RendererRD::MaterialStorage::Shader, true>::get_or_null(RID const&, bool) (e:\Repositories\godot\core\templates\rid_owner.h:189)
RID_Alloc<RendererRD::MaterialStorage::Shader, true>::initialize_rid(RID, RendererRD::MaterialStorage::Shader const&) (e:\Repositories\godot\core\templates\rid_owner.h:218)
RID_Owner<RendererRD::MaterialStorage::Shader, true>::initialize_rid(RID, RendererRD::MaterialStorage::Shader const&) (e:\Repositories\godot\core\templates\rid_owner.h:447)
RendererRD::MaterialStorage::shader_initialize(RID) (e:\Repositories\godot\servers\rendering\renderer_rd\storage_rd\material_storage.cpp:1832)
CommandQueueMT::Command1<RendererMaterialStorage, void (RendererMaterialStorage::*)(RID), RID>::call() (e:\Repositories\godot\core\templates\command_queue_mt.h:322)
CommandQueueMT::_flush() (e:\Repositories\godot\core\templates\command_queue_mt.h:373)
CommandQueueMT::flush_if_pending() (e:\Repositories\godot\core\templates\command_queue_mt.h:404)
RenderingServerDefault::free(RID) (e:\Repositories\godot\servers\rendering\rendering_server_default.h:988)
CompressedTexture2D::~CompressedTexture2D() (e:\Repositories\godot\scene\resources\compressed_texture.cpp:465)
void memdelete<RefCounted>(RefCounted*) (e:\Repositories\godot\core\os\memory.h:109)
Variant::_clear_internal() (e:\Repositories\godot\core\variant\variant.cpp:1360)
Variant::clear() (e:\Repositories\godot\core\variant\variant.h:302)
Variant::~Variant() (e:\Repositories\godot\core\variant\variant.h:785)
CowData<Variant>::_unref(void*) (e:\Repositories\godot\core\templates\cowdata.h:216)
Error CowData<Variant>::resize<false>(int) (e:\Repositories\godot\core\templates\cowdata.h:275)
Vector<Variant>::resize(int) (e:\Repositories\godot\core\templates\vector.h:94)
Vector<Variant>::clear() (e:\Repositories\godot\core\templates\vector.h:87)
Array::clear() (e:\Repositories\godot\core\variant\array.cpp:110)
RendererRD::MaterialStorage::material_free(RID) (e:\Repositories\godot\servers\rendering\renderer_rd\storage_rd\material_storage.cpp:2076)
RendererRD::MaterialStorage::free(RID) (e:\Repositories\godot\servers\rendering\renderer_rd\storage_rd\material_storage.cpp:1192)
RendererRD::Utilities::free(RID) (e:\Repositories\godot\servers\rendering\renderer_rd\storage_rd\utilities.cpp:94)
RenderingServerDefault::_free(RID) (e:\Repositories\godot\servers\rendering\rendering_server_default.cpp:51)
CommandQueueMT::Command1<RenderingServerDefault, void (RenderingServerDefault::*)(RID), RID>::call() (e:\Repositories\godot\core\templates\command_queue_mt.h:322)
CommandQueueMT::_flush() (e:\Repositories\godot\core\templates\command_queue_mt.h:373)
CommandQueueMT::flush_if_pending() (e:\Repositories\godot\core\templates\command_queue_mt.h:404)
RenderingServerDefault::instance_set_visible(RID, bool) (e:\Repositories\godot\servers\rendering\rendering_server_default.h:785)
Node3DEditor::clear() (e:\Repositories\godot\editor\plugins\node_3d_editor_plugin.cpp:8032)
Node3DEditorPlugin::clear() (e:\Repositories\godot\editor\plugins\node_3d_editor_plugin.h:949)
EditorData::set_editor_plugin_states(Dictionary const&) (e:\Repositories\godot\editor\editor_data.cpp:325)
EditorData::restore_edited_scene_state(EditorSelection*, EditorSelectionHistory*) (e:\Repositories\godot\editor\editor_data.cpp:928)
EditorNode::_set_current_scene_nocheck(int) (e:\Repositories\godot\editor\editor_node.cpp:3673)
EditorNode::_set_current_scene(int) (e:\Repositories\godot\editor\editor_node.cpp:3627)
EditorNode::_remove_edited_scene(bool) (e:\Repositories\godot\editor\editor_node.cpp:3493)
EditorNode::load_scene(String const&, bool, bool, bool, bool, bool) (e:\Repositories\godot\editor\editor_node.cpp:3765)
EditorNode::_load_open_scenes_from_config(Ref<ConfigFile>) (e:\Repositories\godot\editor\editor_node.cpp:5481)
EditorNode::_load_editor_layout() (e:\Repositories\godot\editor\editor_node.cpp:5105)
EditorNode::_sources_changed(bool) (e:\Repositories\godot\editor\editor_node.cpp:1055)
void call_with_variant_args_helper<EditorNode, bool, 0ull>(EditorNode*, void (EditorNode::*)(bool), Variant const**, Callable::CallError&, IndexSequence<0ull>) (e:\Repositories\godot\core\variant\binder_common.h:303)
void call_with_variant_args<EditorNode, bool>(EditorNode*, void (EditorNode::*)(bool), Variant const**, int, Callable::CallError&) (e:\Repositories\godot\core\variant\binder_common.h:417)
CallableCustomMethodPointer<EditorNode, bool>::call(Variant const**, int, Variant&, Callable::CallError&) const (e:\Repositories\godot\core\object\callable_method_pointer.h:104)
Callable::callp(Variant const**, int, Variant&, Callable::CallError&) const (e:\Repositories\godot\core\variant\callable.cpp:57)
Object::emit_signalp(StringName const&, Variant const**, int) (e:\Repositories\godot\core\object\object.cpp:1127)
Node::emit_signalp(StringName const&, Variant const**, int) (e:\Repositories\godot\scene\main\node.cpp:3606)
Error Object::emit_signal<bool>(StringName const&, bool) (e:\Repositories\godot\core\object\object.h:922)
EditorFileSystem::_notification(int) (e:\Repositories\godot\editor\editor_file_system.cpp:1285)
EditorFileSystem::_notificationv(int, bool) (e:\Repositories\godot\editor\editor_file_system.h:146)
Object::notification(int, bool) (e:\Repositories\godot\core\object\object.cpp:837)
SceneTree::_process_group(SceneTree::ProcessGroup*, bool) (e:\Repositories\godot\scene\main\scene_tree.cpp:951)
SceneTree::_process(bool) (e:\Repositories\godot\scene\main\scene_tree.cpp:1028)
SceneTree::process(double) (e:\Repositories\godot\scene\main\scene_tree.cpp:508)
Main::iteration() (e:\Repositories\godot\main\main.cpp:3636)
OS_Windows::run() (e:\Repositories\godot\platform\windows\os_windows.cpp:1474)
widechar_main(int, wchar_t**) (e:\Repositories\godot\platform\windows\godot_windows.cpp:180)
_main() (e:\Repositories\godot\platform\windows\godot_windows.cpp:204)
main (e:\Repositories\godot\platform\windows\godot_windows.cpp:223)
__tmainCRTStartup (@__tmainCRTStartup:108)
WinMainCRTStartup (@.l_startw:6)
BaseThreadInitThunk (@BaseThreadInitThunk:9)
RtlUserThreadStart (@RtlUserThreadStart:12)
BrixsterPlanet commented 11 months ago

Confirmed, but does godot support sampler arrays? #49485 mentioned that "Support for texture samplers will be later" and looks like #77740 "removes sampler arrays in shaders (which are not supported by all devices)" . If it's not supported, we should probably give some error when compile the shader.

But it should not crash, the crash call stack on my machine looks like this:

image

Yes, Godot 4 supports sampler arrays.

If you go to Shader Globals in the project settings and attempt to add a new property, you can explicitly see sampler2D array in the type dropdown.

Also sampler arrays work perfectly fine with no issue. The crash isn't related to shader support necessarily, but rather the editor itself.

BrixsterPlanet commented 11 months ago

I get the same error messages, but I can't repro the crash in any way either with official 4.2 beta 6 or current master build (ce9901e). Windows 10, NVIDIA GTX 970. Rendering seems to work correctly too despite those error messages. Seems to also work with Compatibility renderer with no errors.

I recommend trying to reproduce on Forward+ to see if it's an issue with that pipeline exclusively. The reproduction project provided should crash if: You open it, view the shader material in the property inspector, explicitly save the project and then relaunch Godot.