Fading In 16 / 32-bit Color Modes
Question submitted by (10 December 1999)
|Return to The Archives|
|Back in the old days of mode 13h, fading to black was relatively easy - just decrement the pallette once each frame (or something similar). With 16 bit or 32 bit color things are a little different as, obviously, there is no palette. If I wanted to fade the screen in either of these modes I could just cycle through every pixel on the screen and subtract 1 from each of the RGB values of the pixels, but this proves to be incredibly slow, even using MMX. My question is: how do you go about performing fades in both of these modes. (Assuming 5, 6, 5 RGB for 16 bit, and ARGB for 32 bit).|
This should not be incredibly slow (even without MMX). So here are a few
obscure thoughts as I try to figure out what you're doing wrong.
If you're only subtracting one from each frame, at a frame rate of 30Hz, it will take 8.5 seconds to darken a 32-bit image. Maybe you just need to subtract more each frame? Ideally, you want to darken based on time, not frame number.
Since writes to video display are often memory bound (i.e. the processor can feed memory to the video RAM MUCH faster than the bus can carry the data) you should have plenty of time to darken each pixel before you write it to the video memory.
Whatever you do, don't darken the image in the video buffer. Keep a local copy in RAM and darken it as you write it to the video buffer. By darkening an image in the video buffer, you're doing a read/modify/write to a memory source that is on the other side of a bus (on the video card). This is slow for two main reasons. First, reading back memory from video RAM is almost always slow by nature. And second, because the bi-directional reads/writes going across the bus can cause collisions, slowing things down even more. This will certainly be "too slow for prime time."
One note on your darkening. Though, you can get by with a subtract, it's not the right way to darken an image. Consider the greenish-blue color (r=0, g=128, b=255). By subtracting 128, you end up with (r=0, g=0, b=128) where the color is now a shade of pure blue, not greenish-blue. Grayscale images do not suffer from this effect. The proper way to darken an image is to scale each pixel (i.e. multiply by a value 0...1). With MMX, this can be done rather easily.
Response provided by Paul Nettle
This article was originally an entry in flipCode's Fountain of Knowledge, an open Question and Answer column that no longer exists.