Tuesday, August 16, 2005

Optimizing for image performance, not size

Despite how the last one turned out, another computer-related post...

I was thinking about something just now. Generally, we optimize images for size, or at least for a quality-size tradeoff. Occasionally, games will save screenshots in a format optimized so that it takes a minimal amount of time to save to disk so performance isn't degraded. But, I wonder if there are any image formats optimized for drawing.

This would only really matter, it seems, for images with an alpha channel—images that couldn't be drawn simply by copying bits, or even ANDing a 1-bit mask and then XORing the bits. I know that some translucent bitmaps are stored with the RGB values already multiplied by the alpha value to save time when calculating; I thought that was quite clever when I first read that. But, I wonder if there'd be an effective way to do more—to define certain blocks of an image as all-opaque or all-transparent, so that the code that draws the image can be optimized and not have to do any multiplication for those blocks.

My hunch is that, while initially intriguing, it wouldn't end up being fruitful in the end, for several reasons. One, this is something that can be hardware-accelerated these days anyway. Two, you'd have to draw the image in multiple parts (which would not be aesthetically pleasing in some cases) or double-buffer (which would negate any possible performance benefit).

But, maybe this is something that a video card could do. The textures would be compressed when sent to the video card, but there would be metadata associated with the image that tells the video card which rectangular regions are empty, opaque, or need to be alpha-blended. It's all buffered anyway, so the concerns I listed above would be irrelevant. Maybe that would make it less expensive to do alpha blending. Or, maybe it would cause a branch prediction failure or some other hardware thing that I don't understand or care to know about. It's much more efficient to draw big blocks of things than to do it in smaller chunks because the math is easier.

But, it probably doesn't matter anymore anyway. It was interesting to think about for five minutes. It doesn't really matter if it was interesting to read about, I guess, since you're done...

No comments: