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

AnimatedTexture: Fix crash when loaded from a thread #93340

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion scene/resources/animated_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,18 @@ void AnimatedTexture::_bind_methods() {
BIND_CONSTANT(MAX_FRAMES);
}

void AnimatedTexture::_finish_non_thread_safe_setup() {
RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));
}

AnimatedTexture::AnimatedTexture() {
//proxy = RS::get_singleton()->texture_create();
proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);

RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);
RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));

MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &AnimatedTexture::_finish_non_thread_safe_setup));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't using the CONNECT_DEFERRED flag achieve something similar without having to introduce a new method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, but this is another case of the difference between the main call queue and the thread override call queue is relevant. CONNECT_DEFERRED would use MessageQueue::get_singleton(), which during threaded loading wouldn't be the good old default MessageQueue, but the override the ResourceLoader has set for the loading thread. We need to bypass it.

Maybe we could consider making that a bit more transparent. However, as I see things, we are in a de facto stage of colllection of information about the needs of resources dealing with a multi-threaded world and we're not ready yet to come up with solid API designs that are better than the manual approaches.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks for the details!

}

AnimatedTexture::~AnimatedTexture() {
Expand Down
1 change: 1 addition & 0 deletions scene/resources/animated_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class AnimatedTexture : public Texture2D {
uint64_t prev_ticks = 0;

void _update_proxy();
void _finish_non_thread_safe_setup();

protected:
static void _bind_methods();
Expand Down
Loading