PocketMatrix forums

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

What's the fastest way to do a 90° rotated blit?

i need to do a 90 degrees rotated blit of a camera image (so i have no possibility to generate it rotated).

blitting non-rotating is very fast but it seems when blitting rotating due to cache misses (i have no other explanation) thigs slow down very much: from 11 fps to 7fps.

i currently do the following:

Code:

dst += BUFFERTYPE::OFFSET;
const unsigned char *srcI = (const unsigned int*)src;
register a;

h = backBuffer.getHeight();
while(h--)
{
BUFFERTYPE::PIXELTYPE *dst2 = dst;
const unsigned int *srcI2 = srcI;

w = backBuffer.getWidth();
while(w--)
{
a = *srcI2;

*dst2 = ((a>>8)&0xF800) | ((a>>5)&0x07E0) | ((a>>3)&0x1F);

srcI2++;
dst2 += BUFFERTYPE::PITCHX;
}

srcI += BUFFERTYPE::WIDTH;
dst += BUFFERTYPE::PITCHY;
}


BUFFERTYPE::PIXELTYPE is unsigned short (16-bits) and the PITCHX and PITCHY are set to walk correctly across the destination buffer. the source is in 32-bits.
it should be pretty clear what this code does.

the question is: is there a way to get it a lot faster (removing the cache misses)?

interestingly the code is even slower if i try to reverse the run (let the destination pointer do the small steps and write to 2 pixels at once into 32bit memory addresses).

any suggestions?

thanks,
DANIEL
angedelamort · 03 Apr 2003, 21:01 UTC
interestingly the code is even slower if i try to reverse the run (let the destination pointer do the small steps and write to 2 pixels at once into 32bit memory addresses).


I've experimented exactly the same thing... and I don't see any good reason why it's slower.

For the blitting, I use almost the same algorithm... If there a way to do it faster, I don't know it, but I will check tonight in my books :P Maybe there a way that I don't know. Really interesting problem... When we will be able to use hardware acceleration? ;)
interestingly the code is even slower if i try to reverse the run (let the destination pointer do the small steps and write to 2 pixels at once into 32bit memory addresses).


Sounds reasonable. Each time you're looking up a pixel in the source you're then loading a 32-byte cache line from memory. So to gain you your 32-bit plot you're potentially loading 32-bytes...

I think that a "fast" (read: cache-coherant) approach to this is to rotate and blit small sections/"tiles" of the image at a time to increase cache coherency (i.e. rotate and plot a 16x16 pixel square lots of times to cover the entire surface). I'm pretty sure rotated blits have been covered in PocketMatrix before, though maybe not in this particular forum (the Overloaded forum perhaps).
You could try some unrolling in the inner loop, are all your image's widths even? If so unroll the inner loop once, that should show the most effect, if any. (I doubt the compiler could optimize this since it doesn't know the bounds of your images.)

Very minor, but potentially to some effect maybe get a source generated listing to be sure, if your w assignment uses an indirect register load, if so try setting w once where you have set h, and then reset a loop width from that w instead of your backbuffer method. What I am saying is that a register to register copy is faster than a indirect memory load. So h, w and loopw would likely get a register. But to be honest I don't even know how many registers are available for the pda chips. (This may already be a possible optimization that is done.) Things like this may be suspect, since you can be assured your image is in some memory, and the backbuffer is? So you may not be able to avoid it, just because of that.


refractor,
Maybe I'm confused but I'm not sure how that would help at all, you would then have to reorder those 16x16 segments right? If thats true you would run into the same cache problems. I guess the actual rotation process would be faster, but would the eventual cost of reordering the segments spend just as much time?
I guess you could write some blit functions that work in this mode, (ie reorder when blitting). Would be interesting to see this in action, have you done this before?
refractor wrote:

I think that a "fast" (read: cache-coherant) approach to this is to rotate and blit small sections/"tiles" of the image at a time to increase cache coherency (i.e. rotate and plot a 16x16 pixel square lots of times to cover the entire surface). I'm pretty sure rotated blits have been covered in PocketMatrix before, though maybe not in this particular forum (the Overloaded forum perhaps).


This is exactly the way to go although you don't have to go as small as 16x16 pixels to see a speed up. In Iraq Atak I used 64x64 tiles and can rotate and zoom a massive bitmap to the screen at 30fps so it is possible. 8)
Maybe I'm confused but I'm not sure how that would help at all, you would then have to reorder those 16x16 segments right? If thats true you would run into the same cache problems.


It's all about locality.

On a "normal" rotation routine, you'd be stepping down an entire vertical line of the source to plot it horizontally. Say the image is 200x200. As you go down column 199, you're forcing the cache to load 32 bytes per pixel you pick up (thus incurring 200 cache-line-load penalties - you're dumping 32x200 bytes through the cache = 6400 bytes).

Ok, now when you've done that and get back up to start <i style="text-decoration: line-through;">row</i> column 198, chances are that none of the cache-lines you want are in the cache any more. So you have to reload them and once again incur a cache-line-load penalty per pixel. Nasty.

If you do exactly the same routine (i.e. a pixel at a time) over a 16x16 pixel area (or larger as pacomac suggested), then it's far far more likely that the data will be in the cache for all but the first column you process.

Essentially you're using what's in the cache effectively rather than using one part of it and then throwing it all away repeatedly. The "computations" around it will be effectively "free" for a lot of it because they should be covered by load latencies if you arrange it right :)

Side note:

The ARM is a register based processor with ~13 registers freely available (couple more if you're careful).
hi,

i implemented that block-rotator which rotates small blocks instead of the whole image. the speed-up is tremendous.

what i do is to rotate and convert a 320x240x32bits image into a 240x320x16bits image.

with the old, dumb routine (which also pocketfrog uses - without the bits reduction of course) i was able to do 16 such conversions per second. with the new routine i can do ~60 such operations per second which is almost 4 times of the original speed.

loop unrolling did not help to improve speed any further, although it is still around 50% faster without the rotation (90fps).

what i find quite interesting is that i can use block sizes up to 80x80 (didn't try any larger) without any speed loss...

i can definitely live with that speed and thank all who helped me on that.

DANIEL
If you do exactly the same routine (i.e. a pixel at a time) over a 16x16 pixel area (or larger as pacomac suggested), then it's far far more likely that the data will be in the cache for all but the first column you process.

Would this mean that when the bytes are read into the cache, they are not read in a linear form (say from x1..x64), but rather read "around" the data you are accessing (say x-4,y-4 to x+4,y+4)?

Also, Is this on a per-device or per-processor implementation?

Thanks!

--Eric
Would this mean that when the bytes are read into the cache, they are not read in a linear form (say from x1..x64), but rather read "around" the data you are accessing (say x-4,y-4 to x+4,y+4)?


Yes. Sort of. Cache-lines are usually aligned on a cache-line-length boundary. So, if you have a cache-line-length of four words:

0123 4567

and you want word 3, then 0123 will be read into the cache. However, some processors (I'm pretty sure the ARMs do, but it might be implementation specific) will load word 3 so it's available first (the first word you wanted), and then load 012 (the rest) afterwards.

It should be a per processor implementation... but the memory setup on the device may affect it too.
drgoldie: I am removing the Lock() feature from GapiDraw since it was commonly misused. I still have plans to add a tutorial to www.gapidraw.com explaining how to subclass a surface and add a Lock() function to it (rotating the internal buffer to match xpitch=1, ypitch=width).

Would you be interested in publishing your implementation? If so, I would like to include it in this tutorial (with full credits to you of course). Right now I am too busy with optimizing GapiDraw to do any experimentation on rotations...

Please contact me on the forum or at johan.sanneblad@home.se if you are interested.