From 547197e78f6c0f5d5b250d84e99a0390d1aa2467 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 31 Oct 2021 12:37:51 +0100 Subject: [PATCH] Improved the threading logic of MMMaterial's render by quite a bit. Also updated ThreadPool to get the new cleanups / fixes / api improvements required. --- HEADS | 2 +- .../mat_maker_gd/editor/MatMakerGDEditor.gd | 9 +++++++ game/addons/mat_maker_gd/nodes/mm_material.gd | 24 ++++++++++++------- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/HEADS b/HEADS index 2dd95f84..3c2b067b 100644 --- a/HEADS +++ b/HEADS @@ -1 +1 @@ -{"engine": {"3.2": "94a0fc47f7b4e90f8973f9adbfd3312579ed2825", "master": "8c73e813134001e575b6f59e3b0100471c007410", "3.x": "7d516191c66250973e227e5f9963a98ced90acf6"}, "world_generator": {"master": "260c430f11b0b591eaf4714516419aa327d2842c"}, "entity_spell_system": {"master": "cfba9403cb150d0a1c802039a5b7a8822a4f4832"}, "ui_extensions": {"master": "f82273f54cb1ab87d458c91af9554acec5c10831"}, "voxelman": {"master": "9253cc53c6fbbd78b5e4268eb498ef55b4dd0181"}, "texture_packer": {"master": "a0786956813a85b5a82093a081b90c2f8a000e6c"}, "fastnoise": {"master": "d447fd5364e9ab5a6b14184483eab23cd3fe820b"}, "mesh_data_resource": {"master": "49b594130dd80e05ef29297055a31fe8ffa09ffb"}, "procedural_animations": {"master": "f8aae42bf06b3936cc6bd24cb18e1c3ec9f78f4f"}, "ess_data": {"master": "3bd637fdd3304b64a18287a49a6b7387acf2f5de"}, "props": {"master": "2afd6eff45f9a921bdf4090ff3029def86df5cb5"}, "mesh_utils": {"master": "ca1f54268be462414ee40b9ff96276a8112276a9"}, "broken_seals_module": {"master": "2afea56eba66beeb543934edf5a38a8aa63fc79b"}, "thread_pool": {"master": "b2e8c815392052947e7386f722913a12eea543a4"}, "terraman": {"master": "ae53ad5001a720ddb2eb52c46eea65dc2242ac9f"}} \ No newline at end of file +{"engine": {"3.2": "94a0fc47f7b4e90f8973f9adbfd3312579ed2825", "master": "8c73e813134001e575b6f59e3b0100471c007410", "3.x": "7d516191c66250973e227e5f9963a98ced90acf6"}, "world_generator": {"master": "260c430f11b0b591eaf4714516419aa327d2842c"}, "entity_spell_system": {"master": "cfba9403cb150d0a1c802039a5b7a8822a4f4832"}, "ui_extensions": {"master": "f82273f54cb1ab87d458c91af9554acec5c10831"}, "voxelman": {"master": "9253cc53c6fbbd78b5e4268eb498ef55b4dd0181"}, "texture_packer": {"master": "a0786956813a85b5a82093a081b90c2f8a000e6c"}, "fastnoise": {"master": "d447fd5364e9ab5a6b14184483eab23cd3fe820b"}, "mesh_data_resource": {"master": "49b594130dd80e05ef29297055a31fe8ffa09ffb"}, "procedural_animations": {"master": "f8aae42bf06b3936cc6bd24cb18e1c3ec9f78f4f"}, "ess_data": {"master": "3bd637fdd3304b64a18287a49a6b7387acf2f5de"}, "props": {"master": "2afd6eff45f9a921bdf4090ff3029def86df5cb5"}, "mesh_utils": {"master": "ca1f54268be462414ee40b9ff96276a8112276a9"}, "broken_seals_module": {"master": "2afea56eba66beeb543934edf5a38a8aa63fc79b"}, "thread_pool": {"master": "a02955e929fea4a4188d02850b187fa4048145a2"}, "terraman": {"master": "ae53ad5001a720ddb2eb52c46eea65dc2242ac9f"}} \ No newline at end of file diff --git a/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd b/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd index ecd51c08..d7fb0435 100644 --- a/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd +++ b/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd @@ -58,6 +58,8 @@ func recreate() -> void: if !_material: return + _material.cancel_render_and_wait() + for n in _material.nodes: var gn : GraphNode = MMGraphNode.new() gn.slot_colors = slot_colors @@ -98,6 +100,8 @@ func set_mmmaterial(object : MMMateial): func on_graph_edit_connection_request(from: String, from_slot: int, to: String, to_slot: int): var from_node : GraphNode = _graph_edit.get_node(from) var to_node : GraphNode = _graph_edit.get_node(to) + + _material.cancel_render_and_wait() if from_node.connect_slot(from_slot, to_node, to_slot): _graph_edit.connect_node(from, from_slot, to, to_slot) @@ -105,12 +109,15 @@ func on_graph_edit_connection_request(from: String, from_slot: int, to: String, func on_graph_edit_disconnection_request(from: String, from_slot: int, to: String, to_slot: int): var from_node : GraphNode = _graph_edit.get_node(from) var to_node : GraphNode = _graph_edit.get_node(to) + + _material.cancel_render_and_wait() if from_node.disconnect_slot(from_slot, to_node, to_slot): _graph_edit.disconnect_node(from, from_slot, to, to_slot) func on_graph_node_close_request(node : GraphNode) -> void: if _material: + _material.cancel_render_and_wait() _material.remove_node(node._node) recreate() @@ -122,6 +129,8 @@ func _on_AddPopup_ok_pressed(script_path : String): return ensure_objs() + + _material.cancel_render_and_wait() var sc = load(script_path) var nnode : MMNode = sc.new() diff --git a/game/addons/mat_maker_gd/nodes/mm_material.gd b/game/addons/mat_maker_gd/nodes/mm_material.gd index ac79dea5..d6eceeec 100644 --- a/game/addons/mat_maker_gd/nodes/mm_material.gd +++ b/game/addons/mat_maker_gd/nodes/mm_material.gd @@ -13,12 +13,14 @@ export(Array) var nodes : Array var initialized : bool = false var rendering : bool = false var queued_render : bool = false -var render_cancelled : bool = false +var job : ThreadPoolExecuteJob = ThreadPoolExecuteJob.new() func initialize(): if !initialized: initialized = true + job.setup(self, "_thread_func") + for n in nodes: n.connect("changed", self, "on_node_changed") @@ -66,6 +68,8 @@ func render_non_threaded() -> void: did_render = true func render_threaded() -> void: + job.cancelled = false + if rendering: queued_render = true return @@ -73,18 +77,16 @@ func render_threaded() -> void: if !initialized: initialize() - var j : ThreadPoolExecuteJob = ThreadPoolExecuteJob.new() - j.setup(self, "_thread_func") - - ThreadPool.add_job(j) + if !ThreadPool.has_job(job): + ThreadPool.add_job(job) func _thread_func() -> void: - if render_cancelled: + if job.cancelled: rendering = false return rendering = true - render_cancelled = false + job.cancelled = false var did_render : bool = true @@ -95,7 +97,7 @@ func _thread_func() -> void: if n && n.render(self): did_render = true - if render_cancelled: + if job.cancelled: rendering = false return @@ -105,5 +107,11 @@ func _thread_func() -> void: queued_render = false _thread_func() +func cancel_render_and_wait() -> void: + if rendering: + ThreadPool.cancel_task_wait(job) + + job.cancelled = false + func on_node_changed() -> void: call_deferred("render")