From d274fe957c4f9e3c5d19b9c8b24d465ba7b27559 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 23 Aug 2024 19:00:12 +0200 Subject: [PATCH] preparing to optimize monogfx.fill() --- compiler/res/prog8lib/cx16/monogfx.p8 | 48 ++++++++++++++++----------- docs/source/todo.rst | 3 ++ examples/test.p8 | 22 ++++++------ 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/compiler/res/prog8lib/cx16/monogfx.p8 b/compiler/res/prog8lib/cx16/monogfx.p8 index 5b84c44b6..67c9a2966 100644 --- a/compiler/res/prog8lib/cx16/monogfx.p8 +++ b/compiler/res/prog8lib/cx16/monogfx.p8 @@ -789,31 +789,14 @@ invert: while cx16.r12L!=0 { pop_stack() xx = x1 - while xx >= 0 { - if pget(xx as uword, yy as uword) as ubyte != cx16.r11L - break - xx-- - } - if x1!=xx - horizontal_line(xx as uword+1, yy as uword, x1-xx as uword, cx16.r10L as bool) - else - goto skip - + if fill_scanline_left() goto skip left = xx + 1 if left < x1 push_stack(left, x1 - 1, yy, -dy) xx = x1 + 1 do { - cx16.r9s = xx - while xx <= width-1 { - if pget(xx as uword, yy as uword) as ubyte != cx16.r11L - break - xx++ - } - if cx16.r9s!=xx - horizontal_line(cx16.r9, yy as uword, xx-cx16.r9s as uword, cx16.r10L as bool) - + fill_scanline_right() push_stack(left, xx - 1, yy, dy) if xx > x2 + 1 push_stack(x2 + 1, xx - 1, yy, -dy) @@ -827,6 +810,33 @@ skip: left = xx } until xx>x2 } + + sub fill_scanline_left() -> bool { + ; TODO optimize this to use vera auto-decrements, but requires masking etc because of 8 pixels per byte... + cx16.r9s = xx + while xx >= 0 { + if pget(xx as uword, yy as uword) as ubyte != cx16.r11L + break + xx-- + } + if xx!=cx16.r9s { + horizontal_line(xx+1 as uword, yy as uword, cx16.r9s-xx as uword, cx16.r10L as bool) + return false + } + return true + } + + sub fill_scanline_right() { + ; TODO optimize this to use vera auto-increments, but requires masking etc because of 8 pixels per byte... + cx16.r9s = xx + while xx <= width-1 { + if pget(xx as uword, yy as uword) as ubyte != cx16.r11L + break + xx++ + } + if xx!=cx16.r9s + horizontal_line(cx16.r9, yy as uword, xx-cx16.r9s as uword, cx16.r10L as bool) + } } sub position(uword @zp xx, uword yy) { diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 6970397ea..19e795d11 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,6 +1,9 @@ TODO ==== +Optimize monogfx fill_scanline_left() and fill_scanline_right(). + + Improve register load order in subroutine call args assignments: in certain situations, the "wrong" order of evaluation of function call arguments is done which results in overwriting registers that already got their value, which requires a lot of stack juggling (especially on plain 6502 cpu!) diff --git a/examples/test.p8 b/examples/test.p8 index 121cfee84..9ffd45632 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,4 +1,4 @@ -%import gfx2 +%import monogfx %import textio %import math @@ -9,31 +9,31 @@ main { sub start() { - gfx2.screen_mode(2) + monogfx.lores() demofill() } sub demofill() { - gfx2.circle(160, 120, 110, 1) - gfx2.rect(180, 5, 25, 190, 2) - gfx2.line(100, 150, 240, 10, 2) - gfx2.rect(150, 130, 10, 100, 3) + monogfx.circle(160, 120, 110, true) + monogfx.rect(180, 5, 25, 190, true) + monogfx.line(100, 150, 240, 10, true) + monogfx.line(101, 150, 241, 10, true) + monogfx.rect(150, 130, 10, 100, true) sys.wait(30) cbm.SETTIM(0,0,0) - gfx2.fill(100,100,3) - gfx2.fill(100,100,2) - gfx2.fill(100,100,0) + monogfx.fill(100,100,true) + monogfx.fill(100,100,false) uword duration = cbm.RDTIM16() sys.wait(30) - gfx2.screen_mode(0) + monogfx.textmode() txt.nl() txt.print_uw(duration) txt.print(" jiffies\n") - ; hires 4c before optimizations: ~345 jiffies + ; before optimizations: ~166 jiffies } }