From 4476879bdea63976ae1ae34f32a18466ba87b187 Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Tue, 1 Mar 2022 19:12:06 +0500 Subject: [PATCH 1/8] draw optimizer --- src/Tools/Draw.gd | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Tools/Draw.gd b/src/Tools/Draw.gd index f2ede06f0d93..3b1589700303 100644 --- a/src/Tools/Draw.gd +++ b/src/Tools/Draw.gd @@ -20,6 +20,8 @@ var _line_end := Vector2.ZERO var _indicator := BitMap.new() var _polylines := [] var _line_polylines := [] +var _draw_cache: PoolVector2Array = [] +var _can_cache: bool = false func _ready() -> void: @@ -202,6 +204,7 @@ func draw_tool(position: Vector2) -> void: # Bresenham's Algorithm # Thanks to https://godotengine.org/qa/35276/tile-based-line-drawing-algorithm-efficiency func draw_fill_gap(start: Vector2, end: Vector2) -> void: + _draw_cache = [] # Clear cache (cause new points have arrived) var dx := int(abs(end.x - start.x)) var dy := int(-abs(end.y - start.y)) var err := dx + dy @@ -218,7 +221,9 @@ func draw_fill_gap(start: Vector2, end: Vector2) -> void: if e2 <= dx: err += dx y += sy + _can_cache = true # Used to make sure that _set_pixel() is comming straight from THIS function draw_tool(Vector2(x, y)) + _can_cache = false func draw_tool_pixel(position: Vector2) -> void: @@ -345,6 +350,11 @@ func draw_indicator_at(position: Vector2, offset: Vector2, color: Color) -> void func _set_pixel(position: Vector2, ignore_mirroring := false) -> void: + if _can_cache: # Means that the points are comming from "draw_fill_gap()" function + if position in _draw_cache: + return + _draw_cache.append(position) # Store the position of pixel + var project: Project = Global.current_project if project.tile_mode and project.get_tile_mode_rect().has_point(position): position = position.posmodv(project.size) From f74cef055020cb42be4ce3b8acfc99ed16f1f465 Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Tue, 1 Mar 2022 22:03:35 +0500 Subject: [PATCH 2/8] improvements --- src/Tools/BaseTool.gd | 4 ++++ src/Tools/Draw.gd | 14 +++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index 67ef9feee6c9..564a43b318d5 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -8,6 +8,9 @@ var cursor_text := "" var _cursor := Vector2.INF +var _draw_cache: PoolVector2Array = [] # for storing already drawn pixels +var _cache_limit :int = 2000 #To prevent memory bloating + func _ready() -> void: kname = name.replace(" ", "_").to_lower() @@ -40,6 +43,7 @@ func update_config() -> void: func draw_start(_position: Vector2) -> void: + _draw_cache = [] is_moving = true diff --git a/src/Tools/Draw.gd b/src/Tools/Draw.gd index 3b1589700303..7da5cdfcb2ef 100644 --- a/src/Tools/Draw.gd +++ b/src/Tools/Draw.gd @@ -20,8 +20,6 @@ var _line_end := Vector2.ZERO var _indicator := BitMap.new() var _polylines := [] var _line_polylines := [] -var _draw_cache: PoolVector2Array = [] -var _can_cache: bool = false func _ready() -> void: @@ -204,7 +202,6 @@ func draw_tool(position: Vector2) -> void: # Bresenham's Algorithm # Thanks to https://godotengine.org/qa/35276/tile-based-line-drawing-algorithm-efficiency func draw_fill_gap(start: Vector2, end: Vector2) -> void: - _draw_cache = [] # Clear cache (cause new points have arrived) var dx := int(abs(end.x - start.x)) var dy := int(-abs(end.y - start.y)) var err := dx + dy @@ -221,9 +218,7 @@ func draw_fill_gap(start: Vector2, end: Vector2) -> void: if e2 <= dx: err += dx y += sy - _can_cache = true # Used to make sure that _set_pixel() is comming straight from THIS function draw_tool(Vector2(x, y)) - _can_cache = false func draw_tool_pixel(position: Vector2) -> void: @@ -350,10 +345,11 @@ func draw_indicator_at(position: Vector2, offset: Vector2, color: Color) -> void func _set_pixel(position: Vector2, ignore_mirroring := false) -> void: - if _can_cache: # Means that the points are comming from "draw_fill_gap()" function - if position in _draw_cache: - return - _draw_cache.append(position) # Store the position of pixel + if position in _draw_cache: + return + if _draw_cache.size() > _cache_limit: + _draw_cache = [] + _draw_cache.append(position) # Store the position of pixel var project: Project = Global.current_project if project.tile_mode and project.get_tile_mode_rect().has_point(position): From 1c9493b46760ab7ce7c03c0aed85c860ea8adca3 Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Tue, 1 Mar 2022 22:08:21 +0500 Subject: [PATCH 3/8] some memory improvements don't think this one's needed but doing it just in case... --- src/Tools/BaseTool.gd | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index 564a43b318d5..be83f6fd9ea2 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -56,6 +56,7 @@ func draw_move(position: Vector2) -> void: func draw_end(_position: Vector2) -> void: is_moving = false + _draw_cache = [] func cursor_move(position: Vector2) -> void: From 84b3d407a681de3cf45f59463d630fe0399c5e83 Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Tue, 1 Mar 2022 22:11:01 +0500 Subject: [PATCH 4/8] formatting --- src/Tools/BaseTool.gd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index be83f6fd9ea2..8d8723bf24fc 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -9,7 +9,7 @@ var cursor_text := "" var _cursor := Vector2.INF var _draw_cache: PoolVector2Array = [] # for storing already drawn pixels -var _cache_limit :int = 2000 #To prevent memory bloating +var _cache_limit: int = 2000 #To prevent memory bloating func _ready() -> void: From 573bd4e5e6ec7ebbbf8e43a6727f5b1e02daff34 Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Tue, 1 Mar 2022 23:02:36 +0500 Subject: [PATCH 5/8] add limit change with brush --- src/Tools/BaseTool.gd | 1 - src/Tools/Draw.gd | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index 8d8723bf24fc..825c0aa953b0 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -9,7 +9,6 @@ var cursor_text := "" var _cursor := Vector2.INF var _draw_cache: PoolVector2Array = [] # for storing already drawn pixels -var _cache_limit: int = 2000 #To prevent memory bloating func _ready() -> void: diff --git a/src/Tools/Draw.gd b/src/Tools/Draw.gd index 7da5cdfcb2ef..6bca917e21d4 100644 --- a/src/Tools/Draw.gd +++ b/src/Tools/Draw.gd @@ -347,7 +347,8 @@ func draw_indicator_at(position: Vector2, offset: Vector2, color: Color) -> void func _set_pixel(position: Vector2, ignore_mirroring := false) -> void: if position in _draw_cache: return - if _draw_cache.size() > _cache_limit: + var cache_limit: int = (_brush_size * _brush_size) * 3 # This equation seems the best match + if _draw_cache.size() > cache_limit: _draw_cache = [] _draw_cache.append(position) # Store the position of pixel From 7f29607b7aa2b7a0c2ef177c4171f491444c469e Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Tue, 1 Mar 2022 23:38:49 +0500 Subject: [PATCH 6/8] fixed a small bug cache was being kept when frames were changed --- src/Tools/BaseTool.gd | 1 + src/Tools/Draw.gd | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Tools/BaseTool.gd b/src/Tools/BaseTool.gd index 825c0aa953b0..cd0aaf04cbed 100644 --- a/src/Tools/BaseTool.gd +++ b/src/Tools/BaseTool.gd @@ -9,6 +9,7 @@ var cursor_text := "" var _cursor := Vector2.INF var _draw_cache: PoolVector2Array = [] # for storing already drawn pixels +var _for_frame := 0 # cache for which frame? func _ready() -> void: diff --git a/src/Tools/Draw.gd b/src/Tools/Draw.gd index 6bca917e21d4..9a9d687f927b 100644 --- a/src/Tools/Draw.gd +++ b/src/Tools/Draw.gd @@ -345,11 +345,18 @@ func draw_indicator_at(position: Vector2, offset: Vector2, color: Color) -> void func _set_pixel(position: Vector2, ignore_mirroring := false) -> void: - if position in _draw_cache: + if ( + position in _draw_cache + and _for_frame == Global.current_project.current_frame + ): return var cache_limit: int = (_brush_size * _brush_size) * 3 # This equation seems the best match - if _draw_cache.size() > cache_limit: + if ( + _draw_cache.size() > cache_limit + or _for_frame != Global.current_project.current_frame + ): _draw_cache = [] + _for_frame = Global.current_project.current_frame _draw_cache.append(position) # Store the position of pixel var project: Project = Global.current_project From c0be1a0e0d871bdd0923a2db8b8d96f7313d0167 Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Tue, 1 Mar 2022 23:41:27 +0500 Subject: [PATCH 7/8] formatting --- src/Tools/Draw.gd | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Tools/Draw.gd b/src/Tools/Draw.gd index 9a9d687f927b..0f63dfd1119b 100644 --- a/src/Tools/Draw.gd +++ b/src/Tools/Draw.gd @@ -345,16 +345,10 @@ func draw_indicator_at(position: Vector2, offset: Vector2, color: Color) -> void func _set_pixel(position: Vector2, ignore_mirroring := false) -> void: - if ( - position in _draw_cache - and _for_frame == Global.current_project.current_frame - ): + if position in _draw_cache and _for_frame == Global.current_project.current_frame: return var cache_limit: int = (_brush_size * _brush_size) * 3 # This equation seems the best match - if ( - _draw_cache.size() > cache_limit - or _for_frame != Global.current_project.current_frame - ): + if _draw_cache.size() > cache_limit or _for_frame != Global.current_project.current_frame: _draw_cache = [] _for_frame = Global.current_project.current_frame _draw_cache.append(position) # Store the position of pixel From 1d044cec0947c02d4066a233ded06fde96c46376 Mon Sep 17 00:00:00 2001 From: Variable <77773850+Variable-ind@users.noreply.github.com> Date: Thu, 3 Mar 2022 12:17:09 +0500 Subject: [PATCH 8/8] change `_cache_limit` on changing `_brush_size` --- src/Tools/Draw.gd | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Tools/Draw.gd b/src/Tools/Draw.gd index 0f63dfd1119b..c332c146556d 100644 --- a/src/Tools/Draw.gd +++ b/src/Tools/Draw.gd @@ -2,6 +2,7 @@ extends BaseTool var _brush := Brushes.get_default_brush() var _brush_size := 1 +var _cache_limit: int = 3 var _brush_interpolate := 0 var _brush_image := Image.new() var _brush_texture := ImageTexture.new() @@ -43,6 +44,7 @@ func _on_Brush_selected(brush: Brushes.Brush) -> void: func _on_BrushSize_value_changed(value: float) -> void: _brush_size = int(value) + _cache_limit = (_brush_size * _brush_size) * 3 # This equation seems the best match update_config() save_config() @@ -347,8 +349,7 @@ func draw_indicator_at(position: Vector2, offset: Vector2, color: Color) -> void func _set_pixel(position: Vector2, ignore_mirroring := false) -> void: if position in _draw_cache and _for_frame == Global.current_project.current_frame: return - var cache_limit: int = (_brush_size * _brush_size) * 3 # This equation seems the best match - if _draw_cache.size() > cache_limit or _for_frame != Global.current_project.current_frame: + if _draw_cache.size() > _cache_limit or _for_frame != Global.current_project.current_frame: _draw_cache = [] _for_frame = Global.current_project.current_frame _draw_cache.append(position) # Store the position of pixel