From abbcec3be9caf38e383b40406ef6d8b9bd19abd6 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 3 Jul 2024 18:46:09 -0400 Subject: [PATCH 1/6] Optimize draw_rect_outline by reducing dot access * Reduce dot access by fetching values once * Add inline diagram of the rectangle points --- arcade/draw/rect.py | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/arcade/draw/rect.py b/arcade/draw/rect.py index fa4367a68a..13c550383a 100644 --- a/arcade/draw/rect.py +++ b/arcade/draw/rect.py @@ -247,14 +247,38 @@ def draw_rect_outline( HALF_BORDER = border_width / 2 # fmt: off - i_lb = rect.bottom_left.x + HALF_BORDER, rect.bottom_left.y + HALF_BORDER - i_rb = rect.bottom_right.x - HALF_BORDER, rect.bottom_right.y + HALF_BORDER - i_rt = rect.top_right.x - HALF_BORDER, rect.top_right.y - HALF_BORDER - i_lt = rect.top_left.x + HALF_BORDER, rect.top_left.y - HALF_BORDER - o_lb = rect.bottom_left.x - HALF_BORDER, rect.bottom_left.y - HALF_BORDER - o_rb = rect.bottom_right.x + HALF_BORDER, rect.bottom_right.y - HALF_BORDER - o_rt = rect.top_right.x + HALF_BORDER, rect.top_right.y + HALF_BORDER - o_lt = rect.top_left.x - HALF_BORDER, rect.top_right.y + HALF_BORDER + left = rect.left + right = rect.right + bottom = rect.bottom + top = rect.top + x = rect.x + y = rect.y + + # o = outer, i = inner + # + # o_lt o_rt + # +-------------------------------+ + # | i_lt i_rt | + # | +---------------------+ | + # | | | | + # | | | | + # | | | | + # | +---------------------+ | + # | i_lb i_rb | + # +-------------------------------+ + # o_lb o_rb + + # Inner vertices + i_lb = left + HALF_BORDER, bottom + HALF_BORDER + i_rb = right - HALF_BORDER, bottom + HALF_BORDER + i_rt = right - HALF_BORDER, top - HALF_BORDER + i_lt = left + HALF_BORDER, top - HALF_BORDER + + # Outer vertices + o_lb = left - HALF_BORDER, bottom - HALF_BORDER + o_rb = right + HALF_BORDER, bottom - HALF_BORDER + o_rt = right + HALF_BORDER, top + HALF_BORDER + o_lt = left - HALF_BORDER, top + HALF_BORDER # fmt: on point_list: Point2List = (o_lt, i_lt, o_rt, i_rt, o_rb, i_rb, o_lb, i_lb, o_lt, i_lt) @@ -262,7 +286,7 @@ def draw_rect_outline( if tilt_angle != 0: point_list_2 = [] for point in point_list: - new_point = rotate_point(point[0], point[1], rect.x, rect.y, tilt_angle) + new_point = rotate_point(point[0], point[1], x, y, tilt_angle) point_list_2.append(new_point) point_list = point_list_2 From 891926b82f7b0f23d2a1041a3fa8f285808bd517 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 3 Jul 2024 19:15:37 -0400 Subject: [PATCH 2/6] Unroll rotated portion of draw_rect_outline --- arcade/draw/rect.py | 83 +++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/arcade/draw/rect.py b/arcade/draw/rect.py index 13c550383a..7ca82182db 100644 --- a/arcade/draw/rect.py +++ b/arcade/draw/rect.py @@ -245,6 +245,7 @@ def draw_rect_outline( """ HALF_BORDER = border_width / 2 + # Extremely unrolled code below # fmt: off left = rect.left @@ -256,40 +257,56 @@ def draw_rect_outline( # o = outer, i = inner # - # o_lt o_rt - # +-------------------------------+ - # | i_lt i_rt | - # | +---------------------+ | - # | | | | - # | | | | - # | | | | - # | +---------------------+ | - # | i_lb i_rb | - # +-------------------------------+ - # o_lb o_rb - - # Inner vertices - i_lb = left + HALF_BORDER, bottom + HALF_BORDER - i_rb = right - HALF_BORDER, bottom + HALF_BORDER - i_rt = right - HALF_BORDER, top - HALF_BORDER - i_lt = left + HALF_BORDER, top - HALF_BORDER - - # Outer vertices - o_lb = left - HALF_BORDER, bottom - HALF_BORDER - o_rb = right + HALF_BORDER, bottom - HALF_BORDER - o_rt = right + HALF_BORDER, top + HALF_BORDER - o_lt = left - HALF_BORDER, top + HALF_BORDER + # o_left, o_top o_right, o_top + # +----------------------------------------+ + # | i_left, i_top i_right, i_top | + # | +----------------------------------+ | + # | | | | + # | | | | + # | | | | + # | +----------------------------------+ | + # | i_left, i_bottom i_right , i_bottom | + # +----------------------------------------+ + # o_left, o_bottom o_right, o_bottom + + i_left = left + HALF_BORDER + i_bottom = bottom + HALF_BORDER + i_right = right - HALF_BORDER + i_top = top - HALF_BORDER + + o_left = left - HALF_BORDER + o_bottom = bottom - HALF_BORDER + o_right = right + HALF_BORDER + o_top = top + HALF_BORDER + + # This is intentionally unrolled to minimize repacking tuples + if tilt_angle == 0: + point_list: PointList = ( + (o_left , o_top ), + (i_left , i_top ), + (o_right , o_top ), + (i_right , i_top ), + (o_right , o_bottom), + (i_right , i_bottom), + (o_left , o_bottom), + (i_left , i_bottom), + (o_left , i_top ), + (i_left , i_top ) + ) + else: + point_list: PointList = ( + rotate_point(o_left , o_top , x, y, tilt_angle), + rotate_point(i_left , i_top , x, y, tilt_angle), + rotate_point(o_right , o_top , x, y, tilt_angle), + rotate_point(i_right , i_top , x, y, tilt_angle), + rotate_point(o_right , o_bottom, x, y, tilt_angle), + rotate_point(i_right , i_bottom, x, y, tilt_angle), + rotate_point(o_left , o_bottom, x, y, tilt_angle), + rotate_point(i_left , i_bottom, x, y, tilt_angle), + rotate_point(o_left , i_top , x, y, tilt_angle), + rotate_point(i_left , i_top , x, y, tilt_angle) + ) # fmt: on - - point_list: Point2List = (o_lt, i_lt, o_rt, i_rt, o_rb, i_rb, o_lb, i_lb, o_lt, i_lt) - - if tilt_angle != 0: - point_list_2 = [] - for point in point_list: - new_point = rotate_point(point[0], point[1], x, y, tilt_angle) - point_list_2.append(new_point) - point_list = point_list_2 - _generic_draw_line_strip(point_list, color, gl.TRIANGLE_STRIP) From 822e25e9b0bba62eb61f0c4c5f57be8c5ef185c7 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 3 Jul 2024 19:21:20 -0400 Subject: [PATCH 3/6] Use direct indexed access instead of slower properties in texture drawing --- arcade/draw/rect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/draw/rect.py b/arcade/draw/rect.py index 7ca82182db..fd64b14701 100644 --- a/arcade/draw/rect.py +++ b/arcade/draw/rect.py @@ -60,7 +60,7 @@ def draw_texture_rect( geometry = ctx.geometry_empty program = ctx.sprite_program_single - program["pos"] = rect.center_x, rect.center_y, 0 + program["pos"] = rect.x, rect.y, 0 program["color"] = color.normalized program["size"] = rect.width, rect.height program["angle"] = angle From f9477fdfe2ff2e970813f5f548882f7d47415e8c Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Wed, 3 Jul 2024 19:34:16 -0400 Subject: [PATCH 4/6] Attempt to work around broken mypy --- arcade/draw/rect.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arcade/draw/rect.py b/arcade/draw/rect.py index fd64b14701..95462a7dc4 100644 --- a/arcade/draw/rect.py +++ b/arcade/draw/rect.py @@ -279,9 +279,12 @@ def draw_rect_outline( o_right = right + HALF_BORDER o_top = top + HALF_BORDER + # Declared separately because the code below seems to break mypy + point_list: PointList + # This is intentionally unrolled to minimize repacking tuples if tilt_angle == 0: - point_list: PointList = ( + point_list = ( (o_left , o_top ), (i_left , i_top ), (o_right , o_top ), @@ -294,7 +297,7 @@ def draw_rect_outline( (i_left , i_top ) ) else: - point_list: PointList = ( + point_list = ( rotate_point(o_left , o_top , x, y, tilt_angle), rotate_point(i_left , i_top , x, y, tilt_angle), rotate_point(o_right , o_top , x, y, tilt_angle), From 6c6fce4a3a759ee07e7b6eece3d9d48bc8ec80b6 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Thu, 18 Jul 2024 20:58:44 -0400 Subject: [PATCH 5/6] Use Point2List --- arcade/draw/rect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/draw/rect.py b/arcade/draw/rect.py index 95462a7dc4..1579444a41 100644 --- a/arcade/draw/rect.py +++ b/arcade/draw/rect.py @@ -280,7 +280,7 @@ def draw_rect_outline( o_top = top + HALF_BORDER # Declared separately because the code below seems to break mypy - point_list: PointList + point_list: Point2List # This is intentionally unrolled to minimize repacking tuples if tilt_angle == 0: From 381f8e8bd920b6f3d6b0ba0a270b91549068f334 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Thu, 18 Jul 2024 21:05:11 -0400 Subject: [PATCH 6/6] Fix bug in closing rectangle corner --- arcade/draw/rect.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arcade/draw/rect.py b/arcade/draw/rect.py index 1579444a41..31f611fa2f 100644 --- a/arcade/draw/rect.py +++ b/arcade/draw/rect.py @@ -293,7 +293,7 @@ def draw_rect_outline( (i_right , i_bottom), (o_left , o_bottom), (i_left , i_bottom), - (o_left , i_top ), + (o_left , o_top ), (i_left , i_top ) ) else: @@ -306,7 +306,7 @@ def draw_rect_outline( rotate_point(i_right , i_bottom, x, y, tilt_angle), rotate_point(o_left , o_bottom, x, y, tilt_angle), rotate_point(i_left , i_bottom, x, y, tilt_angle), - rotate_point(o_left , i_top , x, y, tilt_angle), + rotate_point(o_left , o_top , x, y, tilt_angle), rotate_point(i_left , i_top , x, y, tilt_angle) ) # fmt: on