From 631d1e3a86fd3cfb32094951d311640418da68ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Mon, 8 Jan 2024 10:33:09 +0100 Subject: [PATCH 1/2] Let read thread guards allow access to out-of-tree nodes --- scene/main/node.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scene/main/node.h b/scene/main/node.h index cd1c31d784ce..8130c61a3419 100644 --- a/scene/main/node.h +++ b/scene/main/node.h @@ -558,7 +558,9 @@ class Node : public Object { _FORCE_INLINE_ bool is_readable_from_caller_thread() const { if (current_process_thread_group == nullptr) { // No thread processing. - return is_current_thread_safe_for_nodes(); + // Only accessible if node is outside the scene tree + // or access will happen from a node-safe thread. + return !data.inside_tree || is_current_thread_safe_for_nodes(); } else { // Thread processing. return true; From acac31ba5c4030d0d6f05097671759740462a1b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20J=2E=20Est=C3=A9banez?= Date: Tue, 9 Jan 2024 13:03:45 +0100 Subject: [PATCH 2/2] Allow EditorImportPlugin to override can_import_threaded() --- doc/classes/EditorImportPlugin.xml | 7 +++++++ editor/import/editor_import_plugin.cpp | 10 ++++++++++ editor/import/editor_import_plugin.h | 2 ++ 3 files changed, 19 insertions(+) diff --git a/doc/classes/EditorImportPlugin.xml b/doc/classes/EditorImportPlugin.xml index 82ba956151fc..88a6eeec268b 100644 --- a/doc/classes/EditorImportPlugin.xml +++ b/doc/classes/EditorImportPlugin.xml @@ -120,6 +120,13 @@ $DOCS_URL/tutorials/plugins/editor/import_plugins.html + + + + Tells whether this importer can be run in parallel on threads, or, on the contrary, it's only safe for the editor to call it from the main thread, for one file at a time. + If this method is not overridden, it will return [code]true[/code] by default (i.e., safe for parallel importing). + + diff --git a/editor/import/editor_import_plugin.cpp b/editor/import/editor_import_plugin.cpp index 7afce116b82b..3243dcf25695 100644 --- a/editor/import/editor_import_plugin.cpp +++ b/editor/import/editor_import_plugin.cpp @@ -186,6 +186,15 @@ Error EditorImportPlugin::import(const String &p_source_file, const String &p_sa ERR_FAIL_V_MSG(ERR_METHOD_NOT_FOUND, "Unimplemented _import in add-on."); } +bool EditorImportPlugin::can_import_threaded() const { + bool ret = false; + if (GDVIRTUAL_CALL(_can_import_threaded, ret)) { + return ret; + } else { + return ResourceImporter::can_import_threaded(); + } +} + Error EditorImportPlugin::_append_import_external_resource(const String &p_file, const Dictionary &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) { HashMap options; List keys; @@ -213,5 +222,6 @@ void EditorImportPlugin::_bind_methods() { GDVIRTUAL_BIND(_get_import_order) GDVIRTUAL_BIND(_get_option_visibility, "path", "option_name", "options") GDVIRTUAL_BIND(_import, "source_file", "save_path", "options", "platform_variants", "gen_files"); + GDVIRTUAL_BIND(_can_import_threaded); ClassDB::bind_method(D_METHOD("append_import_external_resource", "path", "custom_options", "custom_importer", "generator_parameters"), &EditorImportPlugin::_append_import_external_resource, DEFVAL(Dictionary()), DEFVAL(String()), DEFVAL(Variant())); } diff --git a/editor/import/editor_import_plugin.h b/editor/import/editor_import_plugin.h index fb164c7f15ff..ea5cfc2682dc 100644 --- a/editor/import/editor_import_plugin.h +++ b/editor/import/editor_import_plugin.h @@ -52,6 +52,7 @@ class EditorImportPlugin : public ResourceImporter { GDVIRTUAL0RC(int, _get_import_order) GDVIRTUAL3RC(bool, _get_option_visibility, String, StringName, Dictionary) GDVIRTUAL5RC(Error, _import, String, String, Dictionary, TypedArray, TypedArray) + GDVIRTUAL0RC(bool, _can_import_threaded) Error _append_import_external_resource(const String &p_file, const Dictionary &p_custom_options = Dictionary(), const String &p_custom_importer = String(), Variant p_generator_parameters = Variant()); @@ -69,6 +70,7 @@ class EditorImportPlugin : public ResourceImporter { virtual void get_import_options(const String &p_path, List *r_options, int p_preset) const override; virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap &p_options) const override; virtual Error import(const String &p_source_file, const String &p_save_path, const HashMap &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata = nullptr) override; + virtual bool can_import_threaded() const override; Error append_import_external_resource(const String &p_file, const HashMap &p_custom_options = HashMap(), const String &p_custom_importer = String(), Variant p_generator_parameters = Variant()); };