Skip to content

Commit

Permalink
Moved main painting function to rendering thread
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Dec 30, 2024
1 parent 4e00866 commit 0b2ee2e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
36 changes: 19 additions & 17 deletions addons/material_maker/engine/pipeline/texture.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion material_maker/panels/paint/paint.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions material_maker/tools/painter/painter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0b2ee2e

Please sign in to comment.