From 0b2ee2eab88b202f9f405dda8ef39912423874a0 Mon Sep 17 00:00:00 2001 From: Rodz Labs Date: Mon, 30 Dec 2024 13:22:43 +0100 Subject: [PATCH] Moved main painting function to rendering thread --- .../material_maker/engine/pipeline/texture.gd | 36 ++++++++++--------- material_maker/panels/paint/paint.gd | 2 +- material_maker/tools/painter/painter.gd | 17 +++++---- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/addons/material_maker/engine/pipeline/texture.gd b/addons/material_maker/engine/pipeline/texture.gd index 02e462b84..03456641b 100644 --- a/addons/material_maker/engine/pipeline/texture.gd +++ b/addons/material_maker/engine/pipeline/texture.gd @@ -55,28 +55,30 @@ func set_texture_rid(new_rid : RID, size : Vector2i, format : RenderingDevice.Da texture_format = format texture_needs_update = true -func in_thread_get_texture() -> void: - var byte_data : PackedByteArray = rd.texture_get_data(rid, 0) - var image_format : Image.Format - match texture_format: - RenderingDevice.DATA_FORMAT_R32_SFLOAT: - image_format = Image.FORMAT_RF - RenderingDevice.DATA_FORMAT_R32G32B32A32_SFLOAT: - image_format = Image.FORMAT_RGBAF - RenderingDevice.DATA_FORMAT_R16_SFLOAT: - image_format = Image.FORMAT_RH - RenderingDevice.DATA_FORMAT_R16G16B16A16_SFLOAT: - image_format = Image.FORMAT_RGBAH - RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM: - image_format = Image.FORMAT_RGBA8 - var image : Image = Image.create_from_data(texture_size.x, texture_size.y, false, image_format, byte_data) - texture.set_image(image) +func in_thread_get_texture() -> Texture2D: + if texture_needs_update: + var byte_data : PackedByteArray = rd.texture_get_data(rid, 0) + var image_format : Image.Format + match texture_format: + RenderingDevice.DATA_FORMAT_R32_SFLOAT: + image_format = Image.FORMAT_RF + RenderingDevice.DATA_FORMAT_R32G32B32A32_SFLOAT: + image_format = Image.FORMAT_RGBAF + RenderingDevice.DATA_FORMAT_R16_SFLOAT: + image_format = Image.FORMAT_RH + RenderingDevice.DATA_FORMAT_R16G16B16A16_SFLOAT: + image_format = Image.FORMAT_RGBAH + RenderingDevice.DATA_FORMAT_R8G8B8A8_UNORM: + image_format = Image.FORMAT_RGBA8 + var image : Image = Image.create_from_data(texture_size.x, texture_size.y, false, image_format, byte_data) + texture.set_image(image) + texture_needs_update = false + return texture func get_texture() -> Texture2D: if texture_needs_update: if rd and rid.is_valid(): await mm_renderer.thread_run(in_thread_get_texture) - texture_needs_update = false return texture func set_texture(new_texture : ImageTexture) -> void: diff --git a/material_maker/panels/paint/paint.gd b/material_maker/panels/paint/paint.gd index d9fb32546..fa9302b69 100644 --- a/material_maker/panels/paint/paint.gd +++ b/material_maker/panels/paint/paint.gd @@ -1009,7 +1009,7 @@ func _on_Painter_end_of_stroke(stroke_state): for c in stroke_state.keys(): if c in layer.get_channels(): var channel_image : Image = Image.new() - channel_image.copy_from(stroke_state[c].get_texture().get_image()) + channel_image.copy_from((await stroke_state[c].get_texture()).get_image()) if false: channel_image.save_png("d:/undo_%d_%s.png" % [ undo_index, c ]) new_history_item[c] = channel_image diff --git a/material_maker/tools/painter/painter.gd b/material_maker/tools/painter/painter.gd index d19f813f1..9e0b9ba4a 100644 --- a/material_maker/tools/painter/painter.gd +++ b/material_maker/tools/painter/painter.gd @@ -575,6 +575,9 @@ func on_dep_update_value(buffer_name : String, parameter_name : String, value) - return false func paint(shader_params : Dictionary, end_of_stroke : bool = false, emit_end_of_stroke : bool = true, on_mask : bool = false) -> void: + await mm_renderer.thread_run(self.in_thread_paint, [shader_params, end_of_stroke, emit_end_of_stroke, on_mask]) + +func in_thread_paint(shader_params : Dictionary, end_of_stroke : bool = false, emit_end_of_stroke : bool = true, on_mask : bool = false) -> void: var channels_infos : Array[Dictionary] = PAINT_CHANNELS_MASK if on_mask else PAINT_CHANNELS for p in shader_params.keys(): var n : String @@ -583,23 +586,23 @@ func paint(shader_params : Dictionary, end_of_stroke : bool = false, emit_end_of else: n = p paint_shader.set_parameter(n, shader_params[p], true) - await paint_shader.render_ext(paint_textures, Vector2i(texture_size, texture_size)) + paint_shader.in_thread_render_ext(paint_textures, Vector2i(texture_size, texture_size)) for i in range(paint_textures.size()/3): if false and OS.is_debug_build(): - paint_textures[i*3+1].get_texture() # Update stroke texture - await paint_textures[i*3+2].get_texture() # Update painted texture + paint_textures[i*3+1].in_thread_get_texture() # Update stroke texture + paint_textures[i*3+2].in_thread_get_texture() # Update painted texture emit_signal("painted") if end_of_stroke and emit_end_of_stroke: for i in range(paint_textures.size()/3): init_shader.set_parameter("modulate", Color(1.0, 1.0, 1.0, 1.0)) init_shader.set_parameter("use_input_image", true) init_shader.set_parameter("input_image", paint_textures[i*3+2]) - await init_shader.render_ext([ paint_textures[i*3] ], Vector2i(texture_size, texture_size)) - paint_textures[i*3].get_texture() + init_shader.in_thread_render_ext([ paint_textures[i*3] ], Vector2i(texture_size, texture_size)) + paint_textures[i*3].in_thread_get_texture() init_shader.set_parameter("modulate", Color(0.0, 0.0, 0.0, 0.0)) init_shader.set_parameter("use_input_image", false) - await init_shader.render_ext([ paint_textures[i*3+1] ], Vector2i(texture_size, texture_size)) - paint_textures[i*3+1].get_texture() + init_shader.in_thread_render_ext([ paint_textures[i*3+1] ], Vector2i(texture_size, texture_size)) + paint_textures[i*3+1].in_thread_get_texture() var stroke_state = {} for c in channels_infos.size(): var channel_name : String = channels_infos[c].name