PocketMatrix forums

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

Bitmasking? xor or somthing?

Hi all, I think I've mentioned this before, but maybe someone can help he derive the fastest way to do the following:

I have 2 buffers, and I like to copy the contents of one to the other (a to b), ignoring a "trans" color (pattern) in a, and only copying the rest overwiping data in the second b, this is one way of doing it:

Code:

void place_screen_trans(PIXEL *src, PIXEL *dest, int transcolor)
{
int calc;
for (int y = 0; y < 320; y++)
{
for (int x = 0; x < 240; x++)
{
calc = x + (y * x);
if (src[calc] != transcolor)
dest[calc] = src[calc];
}
}
}


can it be done faster? (i.e without the if), otherwise thats 76800 if's for one screen copy ;-)

without the if, i.e ignoring the trans it's real easy
memcpy(a,b,153600); //16 bit

ideas?
If you have runs of several pixels that are transparent, you'll benefit by encoding the bitmap as a series of spans of pixels that are either transparent or opaque, prefixed by the length of the span. That way you can skip over entire spans of transparent pixels in the source bitmap. Windows supports this in what's referred to as run-length-encoded 8-bpp bitmaps (RLE). This bitmap format isn't supported in Windows CE though, but you can read the docs and get the general idea.

Jacco did something sort of similar with a span buffer (S-buffer) a year or so ago. Do a search in the archives and you'll find it, or ask him for details.

That being said, you can optimize the code you posted with a few changes:

Code:

PIXEL * pEnd = &dest;[320*240];

while (pEnd != dest)
{
if (*src != transcolor)
*dest = *src;
dest++;
src++;
}


If you have a lot of transparent pixels, it might benefit you to work on DWORD sized data in your loop. That would allow you to skip over two transparent pixels with a single test, but cost you an extra test for two opaque pixels.
given that my game is tile based, why not test the tile width? (since it's likly the whole tile may be trans)
8 DWORDS?
32 bit cpu?

that code does not quite work either.... ;-)
I concur with Digby - for this kind of thing I'd use an RLE method (especially for "sparse" tiles).

However, historically (when I wrote tile-routines for games using 8-bit graphics many, many years ago), I used to do it something like this (I think this is the EOR method you mention):

make a mask the same size as your bitmap - make set pixels 0s and transparent pixels 1s.

load the destination
load the mask
AND the destination with the mask
load the tile
ORR the destination with the tile

then plot.

When it was all 8-bit pixels (and masks) it wasn't a terrible way of doing things on the ARM because you could load a chunk into registers, munge them, and slam them straight out with an STMIA (doing it pixel by pixel would be ghastly).

However, with 16-bit pixels that's just too much data being thrown around for my liking (though you could use a 32-bit bit-mask per 32-pixel tile row and expand it into the mask on the fly). I'd investigate that approach if you have the time.

I'd still go for RLE unless most of the things you're plotting only have a few "holes" (I would think that a bitmask set would be faster to load and process than a heavily "fragmented" RLE set, IYSWIM).

Cheers,

Ref.
if you know exact dimensions of object (like tile which is repeated dozens of time) you will be served best with special function to draw it that knows how many pixels and where it has to "overjump" (and you store in your bitmap only visible pixels)

For everything else... digby showed you the way:)
brandan,

Code:

calc = x + (y * x);

should probably be...
Code:

calc = x + (y * 240);
brendan · 14 Oct 2002, 05:53 UTC — thansk
thanks refractor this is now what I have....

Code:

source_loc = 0;
dest_loc = 0;

for (y = 0; y < 320; y++)
{
for (x = 0; x < 240; x++)
{
//if (play_scr_p[source_loc + x] != trans_color) screen_buffer_p[dest_loc + x] = play_scr_p[source_loc + x];
screen_buffer_p[dest_loc + x] = screen_buffer_p[dest_loc + x] && trans_color;
screen_buffer_p[dest_loc + x] = screen_buffer_p[dest_loc + x] | play_scr_p[source_loc + x];
}
dest_loc += 240; // move down a row in dest
source_loc += 240; // move down a row in source
}


seems to work. have to see how much faster it makes the code! (if any)!

I wonder if I create a large trans color buffer (a screen worth) and do the same thing if it would be different? (faster)? to get rid of the loops?

-Brendan
I'm not entirely sure your code is doing what it should be doing, but if it works for you then what the hell. :)

I wonder if I create a large trans color buffer (a screen worth) and do the same thing if it would be different? (faster)? to get rid of the loops?


I'm not sure what you mean here. You're never reading a different trans colour are you?

Also, don't see loops as necessarily a bad thing. On a processor with a little cache and "cheap" branches (like the ARM), loops aren't that bad. Unrolling code too much is probably detrimental to speed at some point (and it's certainly detrimental to the instruction cache).

Your best bet at optimisation would be (no particular order):
  • Try the RLE method Digby mentioned above

  • Plot two pixels at the same time (unsigned long rather than unsigned short) - removes the 16-bit accesses and "munging"

  • See the other post about not moving memory around


(I'd also check the assembler output to make sure it wasn't storing twice into screen_buffer_p .. but I'm paranoid :mrgreen:).
brendan,

That last bit of code you posted contains a number of errors. For what it's worth, I took the code you posted and profiled it against the code I posted on an iPaq 3670 (StrongArm). I used a source buffer in system memory that had 50% transparent pixels and copied it to the screen using the two methods.

Your code executed in 36.117 milliseconds.
My code executed in 8.37 milliseconds.

Your code also doesn't handle transparent pixels correctly.

Good luck.
No refrector, i'm not, I was going to "implement" the trans feature (pic) later... but code wise it would be no diff (either a trans color, or nothing)... but it "seems" to be slower anyhows...

-Brendan