| 1 |
USING DIRTY UPDATE SCHEME |
| 2 |
|
| 3 |
1. Why dirty updates? |
| 4 |
Most Atari 800 games and applications update only very small part of |
| 5 |
the screen every frame. Typical numbers range from 1% to 5%. On the |
| 6 |
other hand many ports have to convert screen image from internal |
| 7 |
emulator format to port native format and copy it to platform video |
| 8 |
memory. Both conversion and copy (or blit) are typically slow. |
| 9 |
|
| 10 |
2. How to enable it? |
| 11 |
To enable dirty update tracking the symbol DIRTYRECT must be #defined |
| 12 |
globally (in CONFIG.H or in project settings). When it is defined the |
| 13 |
array screen_dirty[] gets allocated. This is UBYTE array of the size |
| 14 |
ATARI_HEIGHT*ATARI_WIDTH/8. In current implementation every element |
| 15 |
is either 0 or 1. Initially the entire array is initialized with 0. |
| 16 |
Each element in this array corresponds to eight consequtive pixels in |
| 17 |
atari_screen[]. When any of this pixels is being changed the element |
| 18 |
in screen_dirty[] is getting set to 1. Port's implementation of |
| 19 |
Atari_DisplayScreen() can use this information and draw only those |
| 20 |
pixels belonging to "changed" octets. Port must reset screen_dirty[] |
| 21 |
at the end of its Atari_DisplayScreen(). Emulator core _never_ resets |
| 22 |
screen_dirty[] elements. |
| 23 |
|
| 24 |
3. How to use it? |
| 25 |
The simplest implementation would be this: every time your code is |
| 26 |
attempting to process pixel at pointer src (which must point somewhere |
| 27 |
_inside_ atari_screen[]) do a check: |
| 28 |
|
| 29 |
if(screen_dirty[((ULONG)src-(ULONG)atari_screen)/8]) ... |
| 30 |
|
| 31 |
If the condition succeeds, update the pixel. Otherwise, skip it. More |
| 32 |
advanced implementation would do a check only once per eight pixels |
| 33 |
but the check basically remains the same. The best use is to redesign |
| 34 |
the entire update loop so it moves through the array screen_dirty[] |
| 35 |
instead of typical loop through atari_screen[] or double loop through |
| 36 |
screen coordinates x and y. Essentially, if screen_dirty[m]==1 then |
| 37 |
eight pixels starting at atari_screen[m*8] needs to be refreshed. Or, |
| 38 |
if you want these in screen coordinates: y=m/ATARI_WIDTH, eight pixels |
| 39 |
starting at m%ATARI_WIDTH. Note that pixels are guaranteed to be at |
| 40 |
the same scanline. Properly written loop would not even use division |
| 41 |
there: |
| 42 |
|
| 43 |
for(m=0, x=0, y=0; m<ATARI_HEIGHT*ATARI_WIDTH/8; m++) |
| 44 |
{ |
| 45 |
if(screen_dirty[m]) |
| 46 |
{ |
| 47 |
/* Draw eight pixels (x,y), (x+1,y),...,(x+7,y) here */ |
| 48 |
... |
| 49 |
screen_dirty[m] = 0; |
| 50 |
} |
| 51 |
x += 8; |
| 52 |
if(x >= ATARI_WIDTH) |
| 53 |
{ |
| 54 |
x = 0; |
| 55 |
y ++; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
The code in that last example also resets screen_dirty[] as it goes |
| 60 |
and does it in very efficient way. If your code does not do that you |
| 61 |
can just use: |
| 62 |
|
| 63 |
memset(screen_dirty, 0, ATARI_HEIGHT * ATARI_WIDTH/8); |
| 64 |
|
| 65 |
at the end of Atari_DisplayScreen(). |
| 66 |
|
| 67 |
4. Is it worth a trouble? |
| 68 |
Believe me, yes, unless your target platform is so fast that you don't |
| 69 |
care about the performance. I tested this implementation on PocketPC |
| 70 |
(WinCE) port on iPaq. Old implementation was too slow even with |
| 71 |
frameskip of 3 (anything higher tends to break PM graphics). With the |
| 72 |
new implementation I am getting better speed at frameskip 1 than I had |
| 73 |
with the old implementation at frameskip 3, and I finally got 100% |
| 74 |
speed at frameskip 2! At the same time I am using linear filtering |
| 75 |
with the new implemenation, something that was too expensive with the |
| 76 |
old one. On the top of this, I am not using the most efficient |
| 77 |
(described above) way to use the scheme. |
| 78 |
There are only a few little catches there. First, you should use |
| 79 |
compiler with at least semi-decent optimizer. Otherwise you will get |
| 80 |
a few extra memory moves every time you access video memory. It's not |
| 81 |
a big problem but it is something to be aware of. Second, there is |
| 82 |
slight performance hit in ANTIC.C: changed pixels are slightly more |
| 83 |
expensive (unchanged pixels stay the same, may be even cheaper than |
| 84 |
before depending on platform memory controller). That only means that |
| 85 |
if you #define DIRTYRECT you should definitely implement proper dirty |
| 86 |
update support in your port or you'll burn CPU cycles. Then again, the |
| 87 |
impact is not that big. |
| 88 |
|
| 89 |
5. Changing core emulator code. |
| 90 |
If you need to draw anything on the screen from the emulator core you |
| 91 |
need to follow certain protocol. First of all, you cannot use |
| 92 |
atari_screen[] directly anymore. If your code resides in ANTIC.C you |
| 93 |
should use macros WRITE_VIDEO_BYTE(), WRITE_VIDEO() (writes a word), |
| 94 |
WRITE_VIDEO_LONG(), and FILL_VIDEO(). There is one case when code |
| 95 |
reads from atari_screen[], that one uses macro READ_VIDEO_LONG() but |
| 96 |
I would strongly discourage everybody from doing tricks like that. |
| 97 |
For code outside of ANTIC.C there are two simple functions: |
| 98 |
video_putbyte() and video_memset(). Those should suffice in most |
| 99 |
situations. |
| 100 |
|
| 101 |
6. Other notes. |
| 102 |
With current set of macros the emulator core is shielded from actual |
| 103 |
atari_screen[] implementation. By all means, atari_screen can be fake |
| 104 |
pointer now. It is possible to create set of macros that will cause |
| 105 |
port code (outside of emulator core) to draw pixels directly to the |
| 106 |
screen as emulator generates them, potentially saving some CPU clocks |
| 107 |
(just replace screen_dirty[] changes with calls to client functions). |
| 108 |
It is also possible to move atari_screen[] allocation completely to |
| 109 |
the client side. Not sure if anybody needs these features, but they |
| 110 |
come free. Another interesting use of dirty update implementation |
| 111 |
would be very efficient way to record movies straight from the |
| 112 |
emulator: no need to calculate diff frames anymore. This is rather |
| 113 |
fancy feature but you never know. |
| 114 |
|
| 115 |
Vasyl |
| 116 |
04/07/2002 |