From fb2345238378160ae358ce41b3a6fa286f6ee681 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 23 Aug 2024 19:00:12 +0200 Subject: [PATCH] optimize monogfx.fill() --- compiler/res/prog8lib/cx16/monogfx.p8 | 85 +++++++++++++++++++++------ examples/test.p8 | 57 +++++++++++------- 2 files changed, 103 insertions(+), 39 deletions(-) diff --git a/compiler/res/prog8lib/cx16/monogfx.p8 b/compiler/res/prog8lib/cx16/monogfx.p8 index 52d675e90..1c66bfbe9 100644 --- a/compiler/res/prog8lib/cx16/monogfx.p8 +++ b/compiler/res/prog8lib/cx16/monogfx.p8 @@ -819,31 +819,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) @@ -857,6 +840,70 @@ skip: left = xx } until xx>x2 } + + sub fill_scanline_left() -> bool { + ; TODO maybe this could use vera auto decrement, but that would require some clever masking calculations + cx16.r9s = xx + while xx >= 0 { + if pgetset() + break + xx-- + } + return xx==cx16.r9s + } + + sub fill_scanline_right() { + ; TODO maybe this could use vera auto increment, but that would require some clever masking calculations + cx16.r9s = xx + while xx <= width-1 { + if pgetset() + break + xx++ + } + } + + sub pgetset() -> bool { + ; test and optionally set a pixel + word @zp xpos = xx + %asm {{ + lda p8v_xpos + and #7 + pha ; xbits + }} + xpos /= 8 + if width==320 + xpos += yy*(320/8) as uword + else + xpos += yy*(640/8) as uword + + %asm {{ + stz cx16.VERA_CTRL + stz cx16.VERA_ADDR_H + lda p8v_xpos+1 + sta cx16.VERA_ADDR_M + lda p8v_xpos + sta cx16.VERA_ADDR_L + ply ; xbits + lda p8s_plot.p8v_maskbits,y + and cx16.VERA_DATA0 + beq + + lda #1 ++ + ; cx16.r11L = seed color to check against + eor cx16.r11L + beq + + rts ++ ; cx16.r10L = new color to set + lda p8s_plot.p8v_maskbits,y + ldx cx16.r10L + beq + + tsb cx16.VERA_DATA0 + bra ++ ++ trb cx16.VERA_DATA0 ++ lda #0 + rts + }} + } } sub position(uword @zp xx, uword yy) { diff --git a/examples/test.p8 b/examples/test.p8 index ca6a7ddd9..af54f1ce6 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,25 +1,42 @@ -%option no_sysinit, enable_floats +%import monogfx +%import textio +%import math + +%option no_sysinit %zeropage basicsafe -main { - sub start() { - ubyte[] array = [1,2,3] - ubyte[3] array2 - float[] flarray = [1.1, 2.2, 3.3] - float[3] flarray2 - word[] warray = [-2222,42,3333] - word[3] warray2 - str[] names = ["apple", "banana", "tomato"] - str[3] names2 - ; 8 array assignments -> 8 arraycopies: - array = [8,7,6] - array = array2 - flarray = [99.9, 88.8, 77.7] - flarray = flarray2 - warray = [4444,5555,6666] - warray = warray2 - names = ["x1", "x2", "x3"] - names = names2 +main { + + sub start() { + monogfx.lores() + demofill() + } + + sub demofill() { + const uword offsetx = 0 + const uword offsety = 0 + + monogfx.circle(offsetx+160, offsety+120, 110, true) + monogfx.rect(offsetx+180, offsety+5, 25, 190, true) + monogfx.line(offsetx+100, offsety+150, offsetx+240, offsety+10, true) + monogfx.line(offsetx+101, offsety+150, offsetx+241, offsety+10, true) + monogfx.rect(offsetx+150, offsety+130, 10, 100, true) + + sys.wait(30) + + cbm.SETTIM(0,0,0) + monogfx.fill(offsetx+100,offsety+100,true) + monogfx.fill(offsetx+100,offsety+100,false) + uword duration = cbm.RDTIM16() + sys.wait(30) + + monogfx.textmode() + txt.nl() + txt.print_uw(duration) + txt.print(" jiffies\n") + + ; before optimizations: ~166 jiffies + } }