| 1 |
DIRTY SPANS TECHNIQUE |
| 2 |
|
| 3 |
Please note: this is just an idea. |
| 4 |
|
| 5 |
I'll explain it in pseudo-code: |
| 6 |
|
| 7 |
/* in antic.c: */ |
| 8 |
|
| 9 |
int dirty_left[ATARI_HEIGHT]; |
| 10 |
int dirty_right[ATARI_HEIGHT]; |
| 11 |
|
| 12 |
static void draw_antic_*() |
| 13 |
{ |
| 14 |
int x; |
| 15 |
for (x = left_margin; x < right_margin; x++) { |
| 16 |
UBYTE new_pixel = generate_pixel(x); |
| 17 |
if (new_pixel != scrn_ptr[x]) { |
| 18 |
dirty_left[ypos - 8] = x; |
| 19 |
x = right_margin; |
| 20 |
do { |
| 21 |
x--; |
| 22 |
new_pixel = generate_pixel(x); |
| 23 |
} while (new_pixel == scrn_ptr[x]); |
| 24 |
dirty_right[ypos - 8] = x + 1; |
| 25 |
for (x = dirty_left[ypos - 8]; x < dirty_right[ypos - 8]; x++) { |
| 26 |
scrn_ptr[x] = generate_pixel(x); |
| 27 |
} |
| 28 |
return; |
| 29 |
} |
| 30 |
} |
| 31 |
/* no pixels changed in this line */ |
| 32 |
dirty_left[ypos - 8] = dirty_right[ypos - 8] = 0; |
| 33 |
} |
| 34 |
|
| 35 |
/* Note: (ypos - 8) is because ypos ranges from 8 to 247 |
| 36 |
for the visible scanlines */ |
| 37 |
|
| 38 |
|
| 39 |
/* port-specific Atari_DisplayScreen(): */ |
| 40 |
|
| 41 |
void Atari_DisplayScreen(UBYTE *screen) |
| 42 |
{ |
| 43 |
int y; |
| 44 |
for (y = 0; y < ATARI_HEIGHT; y++) { |
| 45 |
/* copy pixels dirty_left[y] <= x < dirty_right[y] |
| 46 |
from screen[] to the real screen */ |
| 47 |
blit_pixels(screen, dirty_left[y], dirty_right[y], y); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
Now some less formal explanation: |
| 52 |
|
| 53 |
The idea is to update only parts of scanlines that changed since last frame. |
| 54 |
We update one continuous sequence of pixels ("span") per scanline. |
| 55 |
dirty_left[y] and dirty_right[y] are the boundaries of the span in scanline y. |
| 56 |
In each scanline, we search for a first pixel that is different |
| 57 |
from the previous frame. If we don't find one, then the whole scanline |
| 58 |
is not dirty, which we mark with: |
| 59 |
dirty_left[y] = dirty_right[y] = 0; |
| 60 |
If we find left-most pixel that differs from the previous frame, |
| 61 |
we save its x coordinate in dirty_left[y]. Then we search starting |
| 62 |
from the right side for a pixel that changed since last frame. We know we'll |
| 63 |
find one. We save (x + 1) in dirty_right[y]. Now we normally generate pixels |
| 64 |
between dirty_left[y] and dirty_right[y]. |
| 65 |
|
| 66 |
The display routine doesn't need any additional comments, I think. |
| 67 |
|
| 68 |
Compared to the current approach (always display whole Atari screen), |
| 69 |
the DIRTY SPANS technique adds almost zero overhead in the worst case. |
| 70 |
In the best case we avoid screen updates completely. |
| 71 |
On average, we update just the areas that changed. |
| 72 |
|
| 73 |
There's one case when DIRTY SPANS perform worse than DIRTY RECT: |
| 74 |
if left and right sides of screen are changing while the center of screen |
| 75 |
remains static. This is however extremely rare (actually, I cannot recall |
| 76 |
any real Atari program that does such things). |
| 77 |
|
| 78 |
Compared to DIRTY RECT, DIRTY SPANS has nearly zero overhead in ANTIC code: |
| 79 |
while searching for boundaries there are comparisons but no writes |
| 80 |
to the screen; generating the changed part of the scanlines operates |
| 81 |
as fast as without DIRTY techniques. |
| 82 |
|
| 83 |
Displaying pixels is much faster: we immediately know which pixels to update, |
| 84 |
without stepping through 8K screen_dirty[]. It is also better for hardware |
| 85 |
architectures where continuous writes to the video memory are faster |
| 86 |
then random writes. |
| 87 |
|
| 88 |
It is possible to combine DIRTY SPANS with DIRTY RECT: in the first pass, |
| 89 |
find span boundaries using DIRTY SPANS. Then, in the display loop, |
| 90 |
update only the pixels that changed (compare current pixels to a backup copy |
| 91 |
of the screen). |
| 92 |
|
| 93 |
DIRTY SPANS should be straightforward to implement on most platforms: |
| 94 |
all we need is displaying 1-pixel-high bitmaps. |
| 95 |
|
| 96 |
The ANTIC code is what gets more complicated with DIRTY SPANS. |
| 97 |
Basically, we have to split each function that generates an Atari graphics |
| 98 |
mode into 3 parts: |
| 99 |
1. Find dirty_left (similar to normal code, but compare instead of write |
| 100 |
pixels). |
| 101 |
2. Find dirty_right (likewise, but *right-to-left*; it should be possible |
| 102 |
to implement it without a speed penalty). |
| 103 |
3. Write pixels (same as the current code). |
| 104 |
This probably means more and more macros (antic.c is already full of them). |
| 105 |
|
| 106 |
As noted at the beginning of this document, these are all plans |
| 107 |
for the future. |
| 108 |
|
| 109 |
Piotr Fusik |
| 110 |
2005-09-09 |