Double buffering screen bitmap

Basic and Machine Language

Moderator: Moderators

User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Double buffering screen bitmap

Post by MrSterlingBS »

Hello,

I need support to implement a double buffering routine that I would like to use in my frames per second counter / monitor program.
Please see my other post.

My thought on this routine is this.

Main loop:
3D calculations with plotting to a memory area for example $4000-$4FFF
Copy data from memory $4000-$4FFF to screen bitmap $1000-$1FFF
Delete memory area $4000-$4FFF
Jump to the beginning

Trying out the process was unsuccessful. See attached Video (~560kB)
3D-Cube.zip
(548.32 KiB) Downloaded 55 times
Could it be because the screen arrangement is not regular? My screen has a resolution of x=168 and y=192 pixels.

Code: Select all

On:								; Set the characters on the right place
		CLC						; CLC
		LDA #$04					; LDA #$10	= 16
		TAY						; TAY
On_00:
		STA $01FC,Y					; STA $0FF0,Y	= 4080
		ADC #$0C					; ADC #$0C	= 12
		BCC On_01					; BCC On_01
		SBC #$FB					; SBC #$EF	= 239
On_01:
		INY						; INY
		BNE On_00					; BNE On_00
Thanks for your help.

Best regards
Sven
User avatar
thegg
Vic 20 Amateur
Posts: 69
Joined: Mon Aug 30, 2021 4:49 am
Location: England
Occupation: retired

Re: Double buffering screen bitmap

Post by thegg »

Double buffering is a technique for creating smoother graphical animations. The general principle is that updates to an animation frame are written to an off-screen buffer. When the update is complete, the content of the off-screen buffer are displayed.

For your application, I suggest you modify your graphics rendering routine to use two bit-maps, one displayed and one being constructed. When the frame under construction is complete, just switch the VIC character definition location to display it.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Double buffering screen bitmap

Post by MrSterlingBS »

Hello Thegg,

you mean I should use 128 (0-127) chars for BitMap 1 and 128 (128-255) chars for BitMap 2?
User avatar
javierglez
Vic 20 Hobbyist
Posts: 107
Joined: Sat Jun 03, 2017 3:33 pm

Re: Double buffering screen bitmap

Post by javierglez »

This snippet iterates reg.Y from $4 to $FF and fills $200 to $2FB with a pattern (which I don't know).

Anyway, double buffering would be to write to $1000-$17FF while $1800-$1FFF is on display, and then swap the roles. I doubt you can double buffer pictures larger than 2kB on a regular VIC20 (without an internal expansion).

A VIC20 picture takes around 1kB, it is possible to (better or worse) update the screen using the chase the raster method. But in this case it's a pseudo hires picture and you want to update the full charset which is 4kB.

I once did a similar animation but it was on a commodore 64/16.
User avatar
beamrider
Vic 20 Scientist
Posts: 1452
Joined: Sun Oct 17, 2010 2:28 pm
Location: UK

Re: Double buffering screen bitmap

Post by beamrider »

thegg wrote: Tue Dec 05, 2023 6:12 am Double buffering is a technique for creating smoother graphical animations. The general principle is that updates to an animation frame are written to an off-screen buffer. When the update is complete, the content of the off-screen buffer are displayed.

For your application, I suggest you modify your graphics rendering routine to use two bit-maps, one displayed and one being constructed. When the frame under construction is complete, just switch the VIC character definition location to display it.
..and if you don't want flicker, set up an interrupt to swap the screens over when the raster has gone off the bottom of the screen.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Double buffering screen bitmap

Post by MrSterlingBS »

How?

IRQ:
LDA $9004
BPL Return
Code for SWAP Screens
Return:
JMP $EB15

Could this work?
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Double buffering screen bitmap

Post by Mike »

javierglez wrote:pseudo hires
Even if that bitmap needs a previously set up text screen that serves as address generator for the VIC (and writing certain values to VIC registers), there is nothing "pseudo" about that high resolution mode! There is a 1:1 correspondence between pixels on screen and bits in memory and that is exactly what defines a screen bitmap.
MrSterlingBS wrote:Could this work?
In general, if you poll for register values inside an interrupt routine (for an essentially undetermined duration), you are doing something wrong.

How an interrupt is set up so it triggers on a certain raster line (just below the bottom of the display area, for example) is shown here:

https://sleepingelephant.com/ipw-web/bu ... 3&start=73

Note for that kind of application it is not necessary to set up a stable raster interrupt.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Double buffering screen bitmap

Post by MrSterlingBS »

Thanks again Mike! :)
User avatar
thegg
Vic 20 Amateur
Posts: 69
Joined: Mon Aug 30, 2021 4:49 am
Location: England
Occupation: retired

Re: Double buffering screen bitmap

Post by thegg »

MrSterlingBS wrote: Tue Dec 05, 2023 6:58 am Hello Thegg,

you mean I should use 128 (0-127) chars for BitMap 1 and 128 (128-255) chars for BitMap 2?
I was thinking of you using two separate character sets. However, if your animation needs less than 128 character definitions, you could consider 0-127 as one bit-map and 128-255 as the other. You would need two screen definitions, one for each set of characters forming your bit-map. Then animating your object would involve switching the VIC screen matrix when the graphic frame finishes its update. If you want to do the switch in an interrupt, you will need a flag to signal that the frame is ready for rendering.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Double buffering screen bitmap

Post by MrSterlingBS »

Hello,

now i draw to "screen 2" @ $1080 - $10FF, $1180 - $11FF,... wait for the raster beam.
Then I copy the result to "screen 1" @ $1000 - $107f, 1100 - 117F,...
Clear screen 2 and so on ...

The result is okay for me.
Attachments
3D-Cube-DB.zip
(2.88 KiB) Downloaded 59 times
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Double buffering screen bitmap

Post by Mike »

MrSterlingBS wrote:now i draw to "screen 2" @ $1080 - $10FF, $1180 - $11FF,... wait for the raster beam.
Then I copy the result to "screen 1" @ $1000 - $107f, 1100 - 117F,...
Clear screen 2 and so on ...
The point of using two small 2K bitmaps at $1000 and $1800 is so you can easily switch between them for display by simply writing to $9005.

That entirely saves you the copying step.
thegg wrote:You would need two screen definitions, [...]
No. One screen with all characters $00..$7F (at $0200) is entirely sufficient when the character base is switched between $1000 and $1800.
User avatar
thegg
Vic 20 Amateur
Posts: 69
Joined: Mon Aug 30, 2021 4:49 am
Location: England
Occupation: retired

Re: Double buffering screen bitmap

Post by thegg »

Mike wrote: Wed Dec 06, 2023 6:25 am
thegg wrote:You would need two screen definitions, [...]
No. One screen with all characters $00..$7F (at $0200) is entirely sufficient when the character base is switched between $1000 and $1800.
My comment was in response to the possibility of using the two bit-maps in the same character set definitions. In which case two matrix definitions, one using characters 0-127, and one using characters 128-255 could effect the display with a simple switch of the matrix in $9005. That too would avoid any data copying. :wink:
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Double buffering screen bitmap

Post by MrSterlingBS »

THX for your replies.
That saves the LDA $XXYY,x

I will try it tomorrorw.
User avatar
MrSterlingBS
Vic 20 Enthusiast
Posts: 174
Joined: Tue Jan 31, 2023 2:56 am
Location: Germany,Braunschweig

Re: Double buffering screen bitmap

Post by MrSterlingBS »

Good morning,

The task is giving me a bit of a headache. :|
Are my following assumptions correct?

Charset @ $0200 to $02FF
BitMap one @ $1000 - $17FF
BitMap one @ $1800 - $1FFF

Flip bit @ $9005
LDA $9005
EOR #%01000000
STA $9005

But how can I cleverly rewrite the routine for plotting?

;subroutine: plot a point (codebase64.org)
lda highb,x
sta ZP_Plot_H
lda lowb,x
sta ZP_Plot_L
lda (ZP_Plot_L),y
ora xtableset,x
sta (ZP_Plot_L),y

The low byte is the same for both cases.
But whats about the high byte?
Does it have to be queried for each plot run which bitmap I am currently in?
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Double buffering screen bitmap

Post by Mike »

You obviously need two different tables for the column high bytes of each bitmap, that's right.

Since you likely want to keep the given pixel plot routine and minimize the impact, that calls for self-modifying code that queries the bitmap number once per page flip and writes the corresponding table base address into the operand field of "lda highb,x".
Post Reply