[comp.sys.ibm.pc] Why are bitmap save/restores so slo

mcdonald@uxe.cso.uiuc.edu (09/15/89)

>I am fiddling around with graphic objects in C++ on a PC (EGA mode)
>and am frustrated by the time taken to save/restore areas of the
>screen bit-map.


>I have had a peek at the assembly code doing the pixel copying and it
>seems to take around ten instructions per pixel.

It is because of poor programming. The EGA has a bizarre but efficient
architecture. The ega has a 350x640 pixels by 4 bits mode. That is
112 kbyte of data. So it won't fit, in the obvious way, two pixels 
to a byte, in one segment. They could have used two segments
(a000:0000 to roughly b000:d000) or could have switched portions of
map into one segment with an io register bit. But no, they went
to separate bitplanes for the four bits, all in one segment. That
way they get two whole screens in one segment. Or one 600x800
screen in a segment (on other vendors extended VGA cards).

There are extremely efficient ways of copying areas from region
to another of video memory. And, to copy to or from ordinary memory,
retaining the planar maps, is very fast indeed. So is drawing
lines or circles or text in a fixed color. The programmer just has
to understand how it works and choose an efficient way to do it.

Problem is, and this is evident most clearly in comp.lang.c,
is that a lot of programmers learned to program by taking classes
in a University somewhere, where they didn't stress enough (at all)
the importance of efficiency. And, the graphics textbooks I see
all seem to take some abstract model of a graphics device, rather
than a real, albeit odd or grubby, one. So people write  silly,
slow code, not thinking hard enough how to do it fast.

IF a programmer understands how the EGA works, it can go blazingly
fast. Don't write "modular" or "structured" code: write FAST code,
and it will go fast.

Doug MCDonald

bobmon@iuvax.cs.indiana.edu (RAMontante) (09/18/89)

mcdonald@uxe.cso.uiuc.edu <110200003@uxe.cso.uiuc.edu> :
-
-There are extremely efficient ways of copying areas from region
-to another of video memory. And, to copy to or from ordinary memory,
-retaining the planar maps, is very fast indeed. So is drawing
-lines or circles or text in a fixed color. The programmer just has
-to understand how it works and choose an efficient way to do it.

Okay.  Any tips on how to do this?

mcdonald@uxe.cso.uiuc.edu (09/18/89)

>mcdonald@uxe.cso.uiuc.edu <110200003@uxe.cso.uiuc.edu> :
>-
>-There are extremely efficient ways of copying areas from region
>-to another of video memory. And, to copy to or from ordinary memory,
>-retaining the planar maps, is very fast indeed. So is drawing
>-lines or circles or text in a fixed color. The programmer just has
>-to understand how it works and choose an efficient way to do it.

>Okay.  Any tips on how to do this?

To copy from one part of EGA/VGA memory to another you first realize that
it is stored (in C) as

unsigned char screen[480][640];       

that is first the upper left corner, then proceeding across the screen,
then down.

To copy a region from one place in screen memory to another, just
write code to copy the bytes. In C, you might want to use memcpy or
memmove. In assembler, use the byte string move instructions.
This is done with the control registers of the EGA/VGA in their
normal state as set by a bios call to set mode 16 or 18.

For example, to copy the top two rows to the 3rd and forth rows:

char far *p1;
char far *p2;
int i,j;

p1 = (char far *)0xa000000;
p2 = p1 + 1280;
for(i = 0; i < 1280; i++)*p2++ = *p1++;

(Now for the weird part!!!!! You can replace *p2++ = *p1++ with
*p2++ = - *p1++ or *p2++ = *p1++ +12345; and it will still work.
Each byte operation in C copies all four bit planes.)

To read the color bitplanes to or from ordinary memory, you copy each
individual plane one at a time.This requires diddling the IO registers.
It is complicated enough that you will need to read a book to see how
to set them. Once set, however, the graphics memory behaves just
like ordinary memory (no four bytes copied at once). Just be sure to
return the registers to normal.

Doug MCDonald