Implemented threaded generation for mat_maker_gd using ThreadPool.

This commit is contained in:
Relintai 2021-10-31 11:29:36 +01:00
parent d63dbbbf68
commit ee03da9a85

View File

@ -2,10 +2,17 @@ tool
class_name MMMateial class_name MMMateial
extends Resource extends Resource
#threads are implemented using my thread pool engine module.
#if you want to use this without that module in your engine set this to false,
#and comment out the lines that give errors
const USE_THREADS = true
export(Vector2) var image_size : Vector2 = Vector2(128, 128) export(Vector2) var image_size : Vector2 = Vector2(128, 128)
export(Array) var nodes : Array export(Array) var nodes : Array
var initialized : bool = false var initialized : bool = false
var rendering : bool = false
var queued_render : bool = false
func initialize(): func initialize():
if !initialized: if !initialized:
@ -39,6 +46,12 @@ func remove_node(node : MMNode) -> void:
emit_changed() emit_changed()
func render() -> void: func render() -> void:
if USE_THREADS:
render_threaded()
else:
render_non_threaded()
func render_non_threaded() -> void:
if !initialized: if !initialized:
initialize() initialize()
@ -51,5 +64,36 @@ func render() -> void:
if n && n.render(self): if n && n.render(self):
did_render = true did_render = true
func render_threaded() -> void:
if rendering:
queued_render = true
return
if !initialized:
initialize()
var j : ThreadPoolExecuteJob = ThreadPoolExecuteJob.new()
j.setup(self, "_thread_func")
ThreadPool.add_job(j)
func _thread_func() -> void:
rendering = true
var did_render : bool = true
while did_render:
did_render = false
for n in nodes:
if n && n.render(self):
did_render = true
rendering = false
if queued_render:
queued_render = false
_thread_func()
func on_node_changed() -> void: func on_node_changed() -> void:
call_deferred("render") call_deferred("render")