PocketMatrix forums

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

Rotated Blit Optimization Tips?

Hi,

I've been trying to optimize a system-to-video memory rotated blit loop by prefetching 32 byte areas as described in this excellent post (see page 3):
http://www.pocketmatrix.com/forums/view ... he&start=0

However I'm seeing zero difference on a PXA270 (Dell Axim 51v) between using prefetching or not. My current inner loop is:

Code:


uint width16 = width / (16 * 4);
rgb565* preload;
rgb565 dummy0=0, dummy1=0, dummy2=0, dummy3=0;

while(height--){ // for each row

// preload row into cache by reading each 32 bytes:
x=width16;
preload = src;
while ( x-- ){
dummy0 = *preload;
preload += 16;
dummy1 = *preload;
preload += 16;
dummy2 = *preload;
preload += 16;
dummy3 = *preload;
preload += 16;
}

// now copy:
x=width;
while(x--){ // for each pixel
*dest=*src++;
dest+=destXPitch;
}

dest+=destLineAdvance;
src+=srcLineAdvance;
}



Any ideas on why prefetching is not helping?
As far as I can see, since the destination is not cacheable (video memory), copying in 32x32 byte tiles should not improve it.

FPS is at about 11.7 with the above loop, while with PHAL it jumps to about 40 FPS, so there must be a way of improving it, even without going to assembly code.

Thanks for any help,

Jorge Diogo
http://Ludimate.com
Is your app running at 240x320 or 480x640? You will experience a major performance hit with the lower resolution, because it requires real-time backwards compatibility to convert to the native VGA device.

Dan East
The game is using DirectDraw and with the HI_RES_AWARE flag set, therefore running in 640x480.

After further research it seems the video memory is indeed doing some kind of caching! A test that sets all pixels in the video memory to a certain value (so that no read cache is involved) runs at double speed if writes are sequential, as in a memset.
So the solution seems to be tile blitting: rotate-copy 16x16 pixels blocks one at the time, so that the involved read/write cache misses are minimized. And it works very well: frame rate jumped to about 20 FPS (from 11.7) which is much more acceptable.

It is weird that video memory is being cached: maybe it's not the real video memory and DirectDraw is doing some magic mapping(?) there...

The only improvement I can think of is reading 2 pixels/32 bits at the time, maybe that's a little faster...

Thanks and Best Regards,

Jorge Diogo
http://Ludimate.com
Hi,

I guess that code came from me, but it was without the rotation (which is the problem here). The problem is that you'll have many, many cache misses this way (look into the other thread that Nomad started.

The pre-caching only helps putting a complete source line into the cache, but it won't help with the cache misses you'll get when you jump one line per target pixel.

The solution is to rotate in macro blocks which should give a speedup of ~500%.

The code to do that is quite short and simple but if required I can post mine...

Daniel
This is the code I'm currently using for the inner 16x16 pixel block blitting:
Code:

//----------- for each 16x16 block

height=16;
while(height--){ // for each line in 16x16 block

x=16;
while(x--){ // for each pixel in a block line
*dest = *src++;
dest += destXPitch;
}

// adjust src and dest to start of block line below
dest+=destLineAdvance16;
src+=srcLineAdvance16;
}

// adjust src and dest to the start of next 16x16 block in this line
dest+=destLineAdvance4Block16;
src+=srcLineAdvance4Block16;

//---------------------------------


This loop is inside some other code that loops to blit each 16x16 tile (and is quite ugly so I didn't post it).
Each 16x16 block involves 32*16*2 = 1024 bytes of memory read/writes, which should fit most ARM CPU caches. Do you think prefetching would speed up the above loop?

- It would certainly be interesting to see your code.

Best Regards,

Jorge Diogo
http://Ludimate.com
Ok, here it is.
It should be easy for you to write the rotateBlockCCW() function too...

Code:

static void
rotateBlockCW(unsigned short* nDst, unsigned int nDstPitch, const unsigned short* nSrc, unsigned int nSrcPitch, unsigned int nW, unsigned int nH)
{
nDstPitch >>= 1; // pitch bytes to short
nSrcPitch >>= 1;

int x,y=nH;
nSrc += (nH-1)*nSrcPitch;

while(y--)
{
x = nW;
unsigned short* dstT = nDst;
const unsigned short* srcT = nSrc;

while(x--)
{
*dstT = *srcT;
srcT++;
dstT+=nDstPitch;
}

nDst ++;
nSrc -= nSrcPitch;
}
}

void
ImageTool::rotateImage(const unsigned short* nSrc, unsigned short *nDst, int nSrcWidth, int nSrcHeight, int nAngle, unsigned int nBlockW, unsigned int nBlockH)
{
if(nAngle==90)
{
unsigned int dstWidth = nSrcHeight;

unsigned int x=0,y=0,
numX=nSrcWidth/nBlockW,numY=nSrcHeight/nBlockH;

for(y=0; y<numY; y++)
for(x=0; x<numX; x++)
rotateBlockCW((nDst+(numY-y-1)*nBlockW+x*nBlockH*dstWidth), dstWidth,
nSrc+(x*nBlockW+y*nBlockH*nSrcWidth), nSrcWidth,
nBlockW,nBlockH);
}
}
Does it help to preload the next macro block? Do you know how much cache these devices tend to have?
Could not find any improvement in preloading pixels/blocks. But doing rotated block blits certainly improves performance *a lot* due to its cache-friendliness.

Best Regards,

Jorge Diogo
Ludimate wrote:Could not find any improvement in preloading pixels/blocks. But doing rotated block blits certainly improves performance *a lot* due to its cache-friendliness.

Best Regards,

Jorge Diogo


I means still rotating in blocks, but also preloading the next block as well. I think the key will be how big the cache size is, if you're working in 32x32 blocks, then thats 1k for source and destination, so if the cache was 4k large, then you would have enough space to preload the next block.
Actually a 32x32 block rotation requires 4k of cache (in the optimal case): 16 bits per pixel (RGB565) and read plus write...

The size of the cache depends on the actual CPU. The XScale has 1024 lines of 32 bytes each, which results in 32KByte cache. I guess other ARM CPUs might have smaller caches.

I'm not sure but I think I played around with preloading the source block and it didn't make a noticeable difference.

Daniel
You'd have to preload the destination block as well tho else you'd still get a stall. (I meant 32x32 byte blocks rather than pixels btw :) ).