PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Optimizing colorkey blitter

Fellow developers,

I'm currently writing a game that uses a lot of transparent sprites, and am currently trying to optimize the colorkey blitter (it doesn't use any of the well-known commercial game engines).

I know there is a way to optimize this by taking two 16-bit pixels at once, but the results aren't satisfying enough yet. Maybe one of you can help with a specific part of the algorithm.

I need to quickly compare a 32-bit value (containing 2 pixels) if it contains no transparent pixels (so I can draw these 2 pixels immediatly). The code is something like this:

Code:

unsigned long d_colorkey = (colorkey << 16);
unsigned long d_pixel = src[x * 2] + src[x * 2 + 1];
if (((d_pixel & 0xFFFF) != colorkey) && ((d_pixel & 0xFFFF0000) != d_colorkey))
//draw 2 pixels


There must be a way to make the "checking for two non-transparent pixels" check faster, but I can't find of a way to do it. The rest of the code has been simplified to improve readability of the code.

Someone with a good tip?
Best way to draw quickly a sprite : Run Lenght Encoding and assembly language.
Yes, implementing RLE-sprites might be a good idea, but I'm currently seeking for a more short-term solution to this problem.

Assembly language will be my last resort, because the game needs to be able to run on a variety of different devices.
You can start with a C version for compatibility and hand optimise your function in ARM asm for the less powerful targets...
Your code is buggy. You mean:

Code:
unsigned long d_colorkey = (colorkey << 16);
unsigned long d_pixel = src[x * 2] | (src[x * 2 + 1] << 16);
if (((d_pixel & 0xFFFF) != colorkey) && ((d_pixel & 0xFFFF0000) != d_colorkey))


Anyway, off the top of my head, something like:

Code:
unsigned long d_colorkey = colorkey | (colorkey<<16);
unsigned long d_pixel = src[x * 2] | (src[x * 2 + 1] << 16);

unsigned long d_eor = d_pixel ^ d_colorkey;

if(d_eor)
{
// We have something to plot
if(d_eor>>16)
// we must plot the left

if(d_eor<<16)
// we must plot the right
}


I'll leave you to work out details like which side is which, etc (and whether it's faster).

StephC had the best answer though: you really want to use RLE to encode this sort of sprite. It doesn't have to be in assembler, but it's faster if it is.
Refractor:
Oops, the error just slipped in there :)
The routine you've posted looks similar to the one I had before the last optimization. Adding an extra check for 2 non-colorkeyed pixels actually increased the speed, but not quite enough. However, your code gave me an idea which might turn out really well.

Currently I'd like to speed up my surface oriented blitter first, before adding RLE sprites (surfaces require less overhead, and have more flexibility especially with clipped surfaces).

Thanks for the tips guys. More tips are always welcome though ;)
If you really want to use a colorkey you should transform your pixels so your colorkey is the pure black color (0 0 0) and if your sprite contains some pixels with pure black before, then simply add one to any of the components...
if you don't want to go for an RLE implementation (which really is not hat difficult to do) then there is another possibility to speedup the your blitting code:

considering that the color key does not change often (which is usually the case) you can create a 1-bit per pixel colorkey mask. doing so you can check 32-pixels at once. depending on your data most of these 32-pixels blocks will be fully transparent or fully opaque which allows quick copying or skipping of pixels. only if the mask is not zero and not 0xffffffff you have to do a per-pixel check.

anyway, the RLE method is still faster and even saves memory. the main issue with RLE is making it work correct at the left and right borders of the screen. still, i'd say a first C version of an RLE compiler and blitter should be doable in a few hours of coding.

just my cents,
Daniel