Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Godot 4 GLES2 2D renderer + linux display manager #44399

Closed
wants to merge 5 commits into from

Conversation

lawnjelly
Copy link
Member

@lawnjelly lawnjelly commented Dec 15, 2020

First implementation with linux display manager

Co-authored-by:clayjohn claynjohn@gmail.com
Co-authored-by:Fabio Alessandrelli fabio.alessandrelli@gmail.com

Explanation

The GLES backends consist of 3 modules:

  1. Middle layer (must be written for each platform). Handles GL contexts and windows. Common to both APIs.
  2. GLES2 renderer.
  3. GLES3 renderer.

The middle layers are rewritten compared to 3.2, notably they should handle multiple windows and context sharing, although they share some code with 3.2. They act as a middle man between the display module (x11, windows etc) and the renderer, and get the API into the correct state for everything to work.

As such there will be a certain amount of writing / debugging / alpha testing middle layers. The main priority at first is getting some usable middle layers for linux and windows, the other platforms can be added at leisure.

The 2D is bare bones to get the basics working, and batching is commented out for now. 3D is just stubs so will not display.

Notes

  • Loads still not working yet
  • I've just done the linux middle layer, clayjohn is working on windows and fales HTML5
  • This requires periodic resyncing with changes reduz is making to the renderer base classes (which is causing some pushes to not compile).

@Riteo

This comment has been minimized.

@lawnjelly

This comment has been minimized.

@Riteo

This comment has been minimized.

@qarmin
Copy link
Contributor

qarmin commented Dec 16, 2020

Crash when opening - fixed

I can't open Godot with Vulkan video driver when compiled with scons p=linuxbsd -j8 use_asan=yes use_ubsan=yes

requested video driver : Vulkan
	0 vulkan
	1 GLES2
	2 GLES3
Godot Engine v4.0.dev.custom_build.ea8fcc158 - https://godotengine.org
Using ICU / HarfBuzz / Graphite text server...
creating video driver : vulkan
rendering_driver vulkan
rendering_driver vulkan
platform/linuxbsd/display_server_x11.cpp:3842:41: runtime error: member call on misaligned address 0xbebebebebebebebe for type 'struct GLManager_X11', which requires 8 byte alignment
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>
window_create window id 0
platform/linuxbsd/gl_manager_x11.cpp:217:26: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct GLManager_X11', which requires 8 byte alignment
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>
platform/linuxbsd/gl_manager_x11.cpp:217:39: runtime error: member call on misaligned address 0xbebebebebebebece for type 'struct LocalVector', which requires 8 byte alignment
0xbebebebebebebece: note: pointer points here
<memory cannot be printed>
core/templates/local_vector.h:117:41: runtime error: member access within misaligned address 0xbebebebebebebece for type 'const struct LocalVector', which requires 8 byte alignment
0xbebebebebebebece: note: pointer points here
<memory cannot be printed>
handle_crash: Program crashed with signal 11
Dumping the backtrace. Please include this when reporting the bug on https://github.com/godotengine/godot/issues
[1] bin/godot.linuxbsd.tools.64s() [0x1ec6770] (/mnt/Miecz/mojgodot/platform/linuxbsd/crash_handler_linuxbsd.cpp:54)
[2] /lib/x86_64-linux-gnu/libc.so.6(+0x46210) [0x7f50b3a7f210] (??:0)
[3] LocalVector<GLManager_X11::GLWindow, unsigned int, false>::size() const (/mnt/Miecz/mojgodot/./core/templates/local_vector.h:117)
[4] GLManager_X11::window_create(int, unsigned long, _XDisplay*, int, int) (/mnt/Miecz/mojgodot/platform/linuxbsd/gl_manager_x11.cpp:217 (discriminator 4))
[5] DisplayServerX11::_create_window(DisplayServer::WindowMode, unsigned int, Rect2i const&) (/mnt/Miecz/mojgodot/platform/linuxbsd/display_server_x11.cpp:3842)
[6] DisplayServerX11::DisplayServerX11(String const&, DisplayServer::WindowMode, unsigned int, Vector2i const&, Error&) (/mnt/Miecz/mojgodot/platform/linuxbsd/display_server_x11.cpp:4122 (discriminator 2))
[7] DisplayServerX11::create_func(String const&, DisplayServer::WindowMode, unsigned int, Vector2i const&, Error&) (/mnt/Miecz/mojgodot/platform/linuxbsd/display_server_x11.cpp:3662)
[8] DisplayServer::create(int, String const&, DisplayServer::WindowMode, unsigned int, Vector2i const&, Error&) (/mnt/Miecz/mojgodot/servers/display_server.cpp:612)
[9] Main::setup2(unsigned long) (/mnt/Miecz/mojgodot/main/main.cpp:1549 (discriminator 3))
[10] Main::setup(char const*, int, char**, bool) (/mnt/Miecz/mojgodot/main/main.cpp:1409)
[11] bin/godot.linuxbsd.tools.64s(main+0x310) [0x1ec4426] (/mnt/Miecz/mojgodot/platform/linuxbsd/godot_linuxbsd.cpp:51)
[12] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0x7f50b3a600b3] (??:0)
[13] bin/godot.linuxbsd.tools.64s(_start+0x2e) [0x1ec405e] (??:?)

GLES 2 and 3 also crashes with similar errors

Godot Engine v4.0.dev.custom_build.ea8fcc158 - https://godotengine.org
Using ICU / HarfBuzz / Graphite text server...
creating video driver : GLES2
rendering_driver GLES2
platform/linuxbsd/display_server_x11.cpp:3835:45: runtime error: member call on misaligned address 0xbebebebebebebebe for type 'struct VulkanContextX11', which requires 8 byte alignment
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>
platform/linuxbsd/display_server_x11.cpp:3835:45: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct VulkanContextX11', which requires 8 byte alignment
0xbebebebebebebebe: note: pointer points here
<memory cannot be printed>
handle_crash: Program crashed with signal 11
Dumping the backtrace. Please include this when reporting the bug on https://github.com/godotengine/godot/issues
[1] bin/godot.linuxbsd.tools.64s() [0x1ec6770] (/mnt/Miecz/mojgodot/platform/linuxbsd/crash_handler_linuxbsd.cpp:54)
[2] /lib/x86_64-linux-gnu/libc.so.6(+0x46210) [0x7f6281acd210] (??:0)
[3] DisplayServerX11::_create_window(DisplayServer::WindowMode, unsigned int, Rect2i const&) (/mnt/Miecz/mojgodot/platform/linuxbsd/display_server_x11.cpp:3835)
[4] DisplayServerX11::DisplayServerX11(String const&, DisplayServer::WindowMode, unsigned int, Vector2i const&, Error&) (/mnt/Miecz/mojgodot/platform/linuxbsd/display_server_x11.cpp:4122 (discriminator 2))
[5] DisplayServerX11::create_func(String const&, DisplayServer::WindowMode, unsigned int, Vector2i const&, Error&) (/mnt/Miecz/mojgodot/platform/linuxbsd/display_server_x11.cpp:3662)
[6] DisplayServer::create(int, String const&, DisplayServer::WindowMode, unsigned int, Vector2i const&, Error&) (/mnt/Miecz/mojgodot/servers/display_server.cpp:612)
[7] Main::setup2(unsigned long) (/mnt/Miecz/mojgodot/main/main.cpp:1549 (discriminator 3))
[8] Main::setup(char const*, int, char**, bool) (/mnt/Miecz/mojgodot/main/main.cpp:1409)
[9] bin/godot.linuxbsd.tools.64s(main+0x310) [0x1ec4426] (/mnt/Miecz/mojgodot/platform/linuxbsd/godot_linuxbsd.cpp:51)
[10] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0x7f6281aae0b3] (??:0)
[11] bin/godot.linuxbsd.tools.64s(_start+0x2e) [0x1ec405e] (??:?)
-- END OF BACKTRACE --
Przerwane (zrzut pamięci)

@lawnjelly
Copy link
Member Author

lawnjelly commented Dec 16, 2020

Fixed @qarmin : Yes this is probably due to something akien let me know earlier that I'd been confusing the display driver string with the renderer driver string. It is not clear in the existing source (and no comments) and it had some left over code from 3.2 which was using this string for gles2, gles3 selection, where now it may be used for wayland, x etc.

I'll have a look at that soon, it should not be a big deal. For now run it without specifying driver in the command line.

It will run the project manager in vulkan. You can only select vulkan for new project (as reduz doesn't want to be able to select gles2) but once in the project there is an API selection box in the top right, and you can change it to GLES2.

Note that currently it can only work in x11, in windows I had to compile it out with a #define, because there are some compile errors and I don't have windows so can't fix / debug that platform. I'll be able to do middle layers for linux and probably android, clayjohn should be able to do windows. We'll need someone else to do iOS / mac, but that can wait as we can get onto GLES3 first, as GLES3 is the main target for Godot 4, the middle layers will work with both APIs, and GLES3 may be easier for others to debug as it works with renderdoc.

@qarmin
Copy link
Contributor

qarmin commented Dec 19, 2020

Allocation memory fails when opening - fixed

Opening project in editor -RegressionTestProject.zip with GLES 2 cause crash due to too big allocation size
EDIT: It happens with all, even new projects, example CI - https://github.com/qarmin/RegressionTestProject/pull/9/checks?check_run_id=1584212594

==327557==ERROR: AddressSanitizer: requested allocation size 0xfffffffefafafb08 (0xfffffffefafb0b08 after adjustments for alignment, red zones etc.) exceeds maximum supported size of 0x10000000000 (thread T0)
    #0 0x7f9673024517 in malloc (/lib/x86_64-linux-gnu/libasan.so.6+0xb0517)
    #1 0x127d7463 in Memory::alloc_static(unsigned long, bool) core/os/memory.cpp:76
    #2 0x2988757 in int* memnew_arr_template<int>(unsigned long, char const*) core/os/memory.h:151
    #3 0x7ee906e in ShaderGLES2::get_current_version() drivers/gles2/shader_gles2.cpp:164
    #4 0x7ee6f07 in ShaderGLES2::bind() drivers/gles2/shader_gles2.cpp:92
    #5 0x7e19e82 in RasterizerStorageGLES2::_update_shader(RasterizerStorageGLES2::Shader*) const drivers/gles2/rasterizer_storage_gles2.cpp:1659
    #6 0x7e1b851 in RasterizerStorageGLES2::shader_get_param_list(RID, List<PropertyInfo, DefaultAllocator>*) const drivers/gles2/rasterizer_storage_gles2.cpp:1682
    #7 0x10e37cea in RenderingServerDefault::shader_get_param_list(RID, List<PropertyInfo, DefaultAllocator>*) const servers/rendering/rendering_server_default.h:214
    #8 0x10e977a7 in RenderingServerWrapMT::shader_get_param_list(RID, List<PropertyInfo, DefaultAllocator>*) const servers/rendering/rendering_server_wrap_mt.h:126
    #9 0xf89fa55 in Shader::get_param_list(List<PropertyInfo, DefaultAllocator>*) const scene/resources/shader.cpp:71
    #10 0xf418dc4 in ShaderMaterial::_get_property_list(List<PropertyInfo, DefaultAllocator>*) const scene/resources/material.cpp:153
    #11 0xf4c2f45 in ShaderMaterial::_get_property_listv(List<PropertyInfo, DefaultAllocator>*, bool) const scene/resources/material.h:76
    #12 0x13884696 in Object::get_property_list(List<PropertyInfo, DefaultAllocator>*, bool) const core/object/object.cpp:597
    #13 0x12d101db in Resource::duplicate(bool) const core/io/resource.cpp:208
    #14 0xa7f3235 in Node3DEditor::_init_indicators() editor/plugins/node_3d_editor_plugin.cpp:5432
    #15 0xa81a813 in Node3DEditor::_notification(int) editor/plugins/node_3d_editor_plugin.cpp:5999
    #16 0xa89ab51 in Node3DEditor::_notificationv(int, bool) editor/plugins/node_3d_editor_plugin.h:546
    #17 0x1388b720 in Object::notification(int, bool) core/object/object.cpp:793
    #18 0xc9c18ba in Node::_propagate_enter_tree() scene/main/node.cpp:214
    #19 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #20 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #21 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #22 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #23 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #24 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #25 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #26 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #27 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #28 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229
    #29 0xc9c2c0a in Node::_propagate_enter_tree() scene/main/node.cpp:229

==327557==HINT: if you don't care about these errors you may set allocator_may_return_null=1
SUMMARY: AddressSanitizer: allocation-size-too-big (/lib/x86_64-linux-gnu/libasan.so.6+0xb0517) in malloc
==327557==ABORTING

@lawnjelly
Copy link
Member Author

lawnjelly commented Dec 22, 2020

Fixed > Opening project in editor -[RegressionTestProject.zip](https://github.com/godotengine/godot/files/5719816/RegressionTestProject.zip) with GLES 2 cause crash due to too big allocation size > EDIT: It happens with all, even new projects, example CI - https://github.com/godotengine/regression-test-project/pull/9/checks?check_run_id=1584212594

I'll take a look. This may be because some new Godot 4 shaders for 3d gizmos created by the editor do not compile in GLES2. It could be that because they don't compile this is causing errors down the line. I haven't thought about how to deal with these shaders yet, as they are for 3d they haven't been high priority as I'm only getting 2d working for now. But yeah having them cause errors is not good.

EDIT: This is now fixed. It does seem to be as I hypothesized above. Some ShaderGLES2 objects are not getting properly created with ShaderGLES2::setup, I'm presuming it's the shaders that are not compiling, and these are causing the memory errors when they are accessed later. I've added a valid bool to the class so it will reject calls in such circumstances.

The correct fix later on will take a little more thought: maybe to get these shaders to compile, or prevent them being attempted, not sure yet.

@qarmin
Copy link
Contributor

qarmin commented Dec 22, 2020

Now I have when opening and closing empty project(with only empty project.godot file) in editor this memory leaks -
memory_leaks.txt

e.g.

Direct leak of 480 byte(s) in 4 object(s) allocated from:
    #0 0x7fabd6ba0517 in malloc (/lib/x86_64-linux-gnu/libasan.so.6+0xb0517)
    #1 0x12802b89 in Memory::alloc_static(unsigned long, bool) core/os/memory.cpp:76
    #2 0x12802a88 in operator new(unsigned long, char const*) core/os/memory.cpp:41
    #3 0x11d88799 in RendererSceneCull::camera_create() servers/rendering/renderer_scene_cull.cpp:42
    #4 0x10e69cff in RenderingServerDefault::camera_create() servers/rendering/rendering_server_default.h:490
    #5 0x10f31dc3 in RenderingServerWrapMT::camera_create() (/mnt/Miecz/mojgodot/bin/godot.linuxbsd.tools.64s+0x10f31dc3)
    #6 0xdddb897 in Camera3D::Camera3D() scene/3d/camera_3d.cpp:655
    #7 0xa76eee7 in Node3DEditorViewport::Node3DEditorViewport(Node3DEditor*, EditorNode*, int) editor/plugins/node_3d_editor_plugin.cpp:3903
    #8 0xa866835 in Node3DEditor::Node3DEditor(EditorNode*) editor/plugins/node_3d_editor_plugin.cpp:6433
    #9 0xa880d40 in Node3DEditorPlugin::Node3DEditorPlugin(EditorNode*) editor/plugins/node_3d_editor_plugin.cpp:6697
    #10 0x8a1773f in EditorNode::EditorNode() editor/editor_node.cpp:6646
    #11 0x20083c1 in Main::start() main/main.cpp:2211
    #12 0x1eadff4 in main platform/linuxbsd/godot_linuxbsd.cpp:57
    #13 0x7fabd56db0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

@qarmin
Copy link
Contributor

qarmin commented Dec 22, 2020

When opening this project in editor - THE_WORST_WITHOUT_SCRIPTS.zip - (just a few scenes with almost all available nodes without any scripts), then I have crash almost immediately:

drivers/gles2/rasterizer_canvas_base_gles2.cpp:209:12: runtime error: load of value 190, which is not a valid value for type 'bool'
drivers/gles2/rasterizer_canvas_base_gles2.cpp:214:12: runtime error: load of value 190, which is not a valid value for type 'bool'
drivers/gles2/rasterizer_canvas_base_gles2.cpp:219:12: runtime error: load of value 190, which is not a valid value for type 'bool'
servers/rendering/renderer_canvas_cull.cpp:73:2: runtime error: load of value 64, which is not a valid value for type 'bool'
handle_crash: Program crashed with signal 11
Dumping the backtrace. Please include this when reporting the bug on https://github.com/godotengine/godot/issues
[1] bin/godot.linuxbsd.tools.64s() [0x1eb0300] (/mnt/Miecz/mojgodot/platform/linuxbsd/crash_handler_linuxbsd.cpp:54)
[2] /lib/x86_64-linux-gnu/libc.so.6(+0x46210) [0x7faeef6b0210] (??:0)
[3] /lib/x86_64-linux-gnu/libGLdispatch.so.0(+0x49989) [0x7faeef4f4989] (??:0)
-- END OF BACKTRACE --
Przerwane (zrzut pamięci)

@lawnjelly
Copy link
Member Author

lawnjelly commented Dec 22, 2020

When opening this project in editor - THE_WORST_WITHOUT_SCRIPTS.zip - (just a few scenes with almost all available nodes without any scripts), then I have crash almost immediately:

There is an error in the project file, to select GLES2, the project setting has changed. The line should be:
quality/driver/rendering_driver="GLES2"

The old driver_name is deprecated, because in Godot 4.0 there are 2 drivers, a display driver (x11, wayland etc), and a rendering driver (vulkan, gles2, gles3 etc). These are changed in this PR (I talked this over with the other rendering guys beforehand). So your project crashes when running with vulkan (which it does by default because that project setting is not recognised), but it actually seems to run ok in GLES2 (albeit showing a blank window for 3d, but there is no 3d in GLES2 as yet).

@lawnjelly
Copy link
Member Author

Now I have when opening and closing empty project(with only empty project.godot file) in editor this memory leaks -
memory_leaks.txt

These leaks seem to be in vulkan + 3d. (see above). There probably are some leaks in 2d however! 😁

@qarmin
Copy link
Contributor

qarmin commented Dec 22, 2020

This lines in project.godot are remnants of the project update from version 3.2, but from my knowledge they shouldn't do anything(both good or bad) - but to check I removed this line, and there was no difference.

To run project in editor I used this two commands:

/mnt/Miecz/mojgodot/bin/godot.linuxbsd.tools.64s --rendering-driver vulkan -e --path ~/test/ -q
/mnt/Miecz/mojgodot/bin/godot.linuxbsd.tools.64s --rendering-driver GLES2 -e --path ~/test/ -q

so I'm sure that I opened project with correct renderer.

I wrote about memory leaks, because I checked that they happens only with GLES2.
Vulkan have only one small memory leak, visible in this issue - #44045

Also when opening THE_WORST_WITHOUT_SCRIPTS.zip, crash log with GLES2 is different than in vulkan.
If you can run this project without crash, then seems that my hardware is cause of problems

Ubuntu 20.04
Nvidia GTX 970 - 440.95.01

@lawnjelly
Copy link
Member Author

lawnjelly commented Dec 22, 2020

Not quite sure what is happening for you at the moment. EDIT Detecting prime with multiple GPUs is in after all, I just didn't realise it worked by setting an environmental variable, so hopefully that isn't a problem.

I did just try the -q argument, I had to ask on IRC as I hadn't seen it before, it sounds like it quits after the first iteration. For me this gets stuck in a deadlock in EditorResourcePreview. Perhaps due to some API function that is not implemented yet (there are a couple of EditorTexture previews that are not working yet, maybe it is related).

Unfortunately I had rebased and pushed since your original post and the line numbers don't line up any more so it's difficult to pinpoint what is happening.

Leaks in RendererSceneCull are scene stuff, and scene isn't implemented yet in GLES2. So it could be they are a result of the dummy function stubs, or something Juan is working on (he's currently working in that area).

There can also be a shed load of warnings in the log:

 drivers/gles2/shader_gles2.cpp:98 - Condition "!version" is true. Returning: false

I think this is the 3d gizmo shaders not compiling.

 servers/rendering/renderer_scene_cull.cpp:482 - Condition "instance->base_type == RenderingServer::INSTANCE_NONE" is true.

This happens when you have a 3d scene open (as there is no 3d in GLES2 yet, and presumably the dummy stub functions return INSTANCE_NONE).

@lawnjelly
Copy link
Member Author

lawnjelly commented Dec 23, 2020

At this point anyone running it successfully reporting info would be useful too, you can use the artifacts from the CI builds I think. (This is linux only at the moment). For me it runs fine in normal (multi window) mode and also with the --single-window command line. --single-window is more likely to work if you have problems.

@Faless has also reported a threading issue in some of the new Godot 4.0 APIs which can cause a crash in some circumstances. This may be because OpenGL calls should be made on the main thread (or rather the GL context can only be current in 1 thread, but I think we are just using main thread), and the APIs can be called from other threads. Reduz is aware and says he will work on a fix.

This may be responsible for the deadlock I saw when running with the -q command line option.

@qarmin
Copy link
Contributor

qarmin commented Jan 14, 2021

Now I can't open any project(even empty) in editor due crashes

[1] /lib/x86_64-linux-gnu/libc.so.6(+0x46210) [0x7f66f4192210] (??:0)
[2] /lib/x86_64-linux-gnu/libGLdispatch.so.0(+0x49989) [0x7f66f3fd6989] (??:0)
-- END OF BACKTRACE --

Looks that this is issue with Nvidia, because in CI everything works fine without any crashes - https://github.com/qarmin/RegressionTestProject/runs/1686662513

Godot crash inside glGenTextures(1, &texture->tex_id); in RasterizerStorageGLES2::texture_create
Zrzut ekranu z 2021-01-14 15-38-54

@lawnjelly
Copy link
Member Author

lawnjelly commented Jan 14, 2021

I haven't changed much, only added the bootscreen and vsync. The place where it crashes isn't very informative, it suggests maybe GL is in a bad state, something has caused an error earlier on, or it is a threading problem. In fact it most likely is calling GL from the wrong thread looking at the call stack. This may be related to why some of the previews don't work on mine, maybe it has better error fallback on some drivers than others.

The threading problem which @Faless reported has still to be fixed - there were some changes to the API in 4.0, which reduz said he was going to fix (although he has a long todo list). There may be some assumptions that have been made in 4.0 about multithreaded rendering which just don't work with GLES, so will either need rewriting in the calling code, or workarounds in the renderer (like in this case I can perhaps defer generating the GL id until the texture is used from the main thread - a similar problem may occur with upload to the texture).

I will do what I can, but at the moment we are partly waiting for reduz to stabilize the API with the vulkan stuff so we can merge GLES2 and iron out these problems and start on GLES3. He said he will need another month or so to finish up.

EDIT : I would double check your project is set to rendering->threads->single_safe. Mine crashes on multi-threaded too, as it tries to initialize Glad from a non main thread. I'm going to see if I can add some deferring for glCalls if I can detect it is not being run on the main thread.

@lawnjelly
Copy link
Member Author

Yes I'm now pretty convinced the crash is occurring on some drivers because the GL functions are being called from non-main thread. It is only 'working' on mine because the driver is gracefully handling the error, and this finally seems to be the reason for the black texture for the small preview for the default icon in the IDE.

I've put in some basic protection for this situation - it records the main thread ID at initialization, and ERR_FAILS if the current thread is not the main thread for the texture create and set calls.

So the threaded resource previews is broken, but hopefully we can get it not to crash.

I had a look at deferring these calls but it is quite messy, it would be easier if we could just call generate editor previews single threaded when using GLES renderers, so I'll enquire as to whether this is possible.

First implementation with linux display manager
Co-authored-by:clayjohn <claynjohn@gmail.com>
Co-authored-by:Fabio Alessandrelli <fabio.alessandrelli@gmail.com>
@lawnjelly
Copy link
Member Author

The threading issue causing crashes / bugs from EditorResourcePreview should now be fixed.

  • I've added an enum to OS to store whether the renderer is a main thread only renderer (e.g. GLES).
  • This is read by the EditorResourcePreview singleton, and if set, instead of creating a new thread, the EditorResourcePreview uses a new mode where it updates every _process, and iterates anything in the EditorResourcePreview queue.
  • This means the renderer is run from the main thread, and the icon previews etc are now working! 😁

It is still possible that there might be somewhere else in the engine which tries to use the renderer from a non-main thread, let me know if there are any more problems / crashes and I will try to fix them too.

@qarmin
Copy link
Contributor

qarmin commented Jan 15, 2021

This really broken project(must be run from CLI not editor to avoid spam about using invalid functions etc.) - TheWorstGodotTestProject.zip - after importing and running crash with this backtrace:

[1] bin/godot.linuxbsd.tools.64s() [0x1e4b1e0] (/mnt/Miecz/mojgodot/platform/linuxbsd/crash_handler_linuxbsd.cpp:54)
[2] /lib/x86_64-linux-gnu/libc.so.6(+0x46210) [0x7f534b56f210] (??:0)
[3] CowData<unsigned char>::get(int) const (/mnt/Miecz/mojgodot/./core/templates/cowdata.h:153 (discriminator 7))
[4] Vector<unsigned char>::operator[](int) const (/mnt/Miecz/mojgodot/./core/templates/vector.h:89)
[5] RasterizerStorageGLES2::texture_set_data(RID, Ref<Image> const&, int) (/mnt/Miecz/mojgodot/drivers/gles2/rasterizer_storage_gles2.cpp:870)
[6] RasterizerStorageGLES2::texture_2d_create(Ref<Image> const&) (/mnt/Miecz/mojgodot/drivers/gles2/rasterizer_storage_gles2.cpp:500)
[7] RenderingServerDefault::texture_2d_create(Ref<Image> const&) (/mnt/Miecz/mojgodot/servers/rendering/rendering_server_default.h:170)
[8] RenderingServerWrapMT::texture_2d_create(Ref<Image> const&) (/mnt/Miecz/mojgodot/servers/rendering/rendering_server_wrap_mt.h:80)
[9] GradientTexture::_update() (/mnt/Miecz/mojgodot/scene/resources/texture.cpp:1793 (discriminator 1))
[10] void call_with_variant_args_helper<__UnexistingClass>(__UnexistingClass*, void (__UnexistingClass::*)(), Variant const**, Callable::CallError&, IndexSequence<>) (/mnt/Miecz/mojgodot/./core/variant/binder_common.h:201 (discriminator 4))
[11] void call_with_variant_args_dv<__UnexistingClass>(__UnexistingClass*, void (__UnexistingClass::*)(), Variant const**, int, Callable::CallError&, Vector<Variant> const&) (/mnt/Miecz/mojgodot/./core/variant/binder_common.h:284)
[12] MethodBindT<>::call(Object*, Variant const**, int, Callable::CallError&) (/mnt/Miecz/mojgodot/./core/object/method_bind.h:282)
[13] Object::call(StringName const&, Variant const**, int, Callable::CallError&) (/mnt/Miecz/mojgodot/core/object/object.cpp:784 (discriminator 1))
[14] Callable::call(Variant const**, int, Variant&, Callable::CallError&) const (/mnt/Miecz/mojgodot/core/variant/callable.cpp:53)
[15] MessageQueue::_call_function(Callable const&, Variant const*, int, bool) (/mnt/Miecz/mojgodot/core/object/message_queue.cpp:259)
[16] MessageQueue::flush() (/mnt/Miecz/mojgodot/core/object/message_queue.cpp:306)
[17] SceneTree::process(float) (/mnt/Miecz/mojgodot/scene/main/scene_tree.cpp:447 (discriminator 2))
[18] Main::iteration() (/mnt/Miecz/mojgodot/main/main.cpp:2575)
[19] OS_LinuxBSD::run() (/mnt/Miecz/mojgodot/platform/linuxbsd/os_linuxbsd.cpp:261)
[20] bin/godot.linuxbsd.tools.64s(main+0x3fc) [0x1e48fa2] (/mnt/Miecz/mojgodot/platform/linuxbsd/godot_linuxbsd.cpp:60)
[21] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0x7f534b5500b3] (??:0)
[22] bin/godot.linuxbsd.tools.64s(_start+0x2e) [0x1e48aee] (??:?)

@lawnjelly
Copy link
Member Author

lawnjelly commented Jan 15, 2021

This really broken project(must be run from CLI not editor to avoid spam about using invalid functions etc.) - TheWorstGodotTestProject.zip - after importing and running crash with this backtrace:

Fixed. And another similar bug in texture->set_data. It seems to run now ok for me (although I only ran it for 30 seconds or so).

It does seems to be continually using up memory though, I don't know if that is expected with your project. So it will probably eventually fail from lack of memory.

@Calinou
Copy link
Member

Calinou commented May 15, 2022

Superseded by #54307. Thanks again for doing the initial work 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants