PocketMatrix forums

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

fastest sprite routine

Hello,

I've spent some time to write a fast sprite routine in ARM assembly with RLE encoding and alpha blending.

Using the 32x32 animated sprite found in gapidraw and warmi's demos it seems that I get more than 1000 sprites displayed at 30FPS, same results on my ipaq 3600 and my Zaurus SL-5500.

I've also a sprite routine optimised for XScale (with PLD instruction to preload cache lines), but no XScale to test it for now...


My bench only consist of :

- blit a background image on the whole screen (memcpy)
- blit all the sprites
- update their coordinates
- update screen (rotate blit on my ipaq)

I don't know if this bench is acceptable for comparison with others demos.

If it's OK, what I want to know is : is there a faster sprite routine out there ?

--
StephC / int13 production
Ha , 1000 of them ?

I think I got up to 700 color keyed sprites ( the ZSurface demo is capped at 500) on my Z so getting aditional 300 with RLE would make perfect sense.
But if you were able to get 1000+ alpha-blended sprites at 30 fps then , hell, I don't know what kind of secrets you found about ARM CPU but that would be a huge number ( even considering RLE - after all you are paying penalty for blending process itself and 32x32 sprites used in GapiDraw demos doesn't have that many "empty" pixels anyway.)
BTW . what kind of alpha blending is that – 50/50 or arbitrary ?
I'am using arbitrary alpha (0-32) and I think the speed is mainly due to my RLE encoding, source data is always 32 bits aligned when it's possible, and I can blit as much as 16 pixels at a time if there is no empty pixels.

For the bench process, I remember that you got differents results on ipaq and Zaurus, I think it's due to a flaw in your bench on Zaurus...
I got different results on 5500 and ipaq 3835 meaning that Zaurus was simply faster about 30% than 3835.

Don't look at ZSurface Demo app - I wasn't using this particular piece of QTimer based code for comparing performance of these devices ( it was a demo not a accurate benchmark anyway.)
I simply measured time in milliseconds for 100 blits on both devices (running in a tight for() loop)and 5500 was simply faster at transferring memory.
This is understandable considering that 5500 has a faster bus than 3835.
I don't know if 3600 is different in this regard but could very well be.

As far as RLE, yeah I agree that you can blit as much as 16 pixels at a time using ldmias etc - I was considering using that approach as well. In the best case ( when entire sprite line is either empty or has not transparent pixels) it will result in either no blit or simply memcpy style stuff which will be about 30%-40% faster than non RLE approach.
But that's for the best case, - there are cases where RLE can backfire and actually slow down entire blit.
Overall , using RLE for most sprites will generally result in a nice speedup ( I was planning to introduce RLE into Zsurface but never got around to do it.)

As far as Alpha, it would be very hard to get 16 pixel blits since you have to sacrifice quite a few registers for alpha calculations but if you pulled it out, hey more power to you.

PS.

BTW, are you using the same alpha (0-32) method as I used on ZSurface - meaning one mult per pixel ?

What I am trying to figure out is if you managed to come up with faster alpha blending algorithm or if this speedup is related to RLE encoding.
Worst case for RLE encoding is a grid of alternate opaque and transparents pixels, which is highy improbable.

Most of the sprites have empty pixels at their borders, and that is the case where RLE encoding is fast.

my RLE encoding translates the image data to a stream of 32bits aligned segments of the form :

<pixel|skip|runcode><data>

where skip is the number of transparent pixels to skip, runcode is the number of pixels to blit,
pixel is the first pixel in case of odd segment
and data are the pixels themselves.

runcode also contains special markers for end of line, end of sprite and alpha level.

I even think that this approache is as fast as compiled sprites on this architecture.
I have 3 mult by pixels for alpha blending, maybe this code is not optimal...

[code]

;// r6 = pixel 2
;// r4 = pixel 1
;// r5 = alpha

and r7, r6, #0xF800 ;// p1M = p1 & REDMASK
and r8, r4, #0xF800 ;// p2M = p2 & REDMASK
sub r8, r8, r7 ;// p2M - p1M
mul r9, r8, r5 ;// * alpha
and r11, r6, #0x07E0 ;// p1M = p1 & GREENMASK - precalc to avoid pipeline stall
and r8, r4, #0x07E0 ;// p2M = p2 & GREENMASK - precalc to avoid pipeline stall
add r7, r7, r9, lsr #5 ;// >>5 + p1M
and r10, r7, #0xF800 ;// & REDMASK r10 = temp result

sub r8, r8, r11 ;// p2M - p1M
mul r9, r8, r5 ;// * alpha
and r8, r4, #0x001F ;// p2M = p2 & BLUEMASK - precalc to avoid pipeline stall
and r7, r6, #0x001F ;// p1M = p1 & BLUEMASK - precalc to avoid pipeline stall
add r11, r11, r9, lsr #5 ;// >>5 + p1M
and r11, r11, #0x07E0 ;// & GREENMASK
orr r10, r10, r11 ;// | temp result

sub r8, r8, r7 ;// p2M - p1M
mul r9, r8, r5 ;// * alpha
add r7, r7, r9, lsr #5 ;// >>5 + p1M
and r7, r7, #0x001F ;// & BLUEMASK
orr r10, r10, r7 ;// | temp result

;// r10 = blended pixel





[/code]
This code does alpha blending of 2 pixels at a time using 1 mult per pixel
Code:

blitNormalBlendAligned_wordcopy:

ldr r7, [r1],#4 // load source pixel
ldr r5, [r0] // load dest pixel

ldr r3,=0x8020040
and r6,r7,r9
add r6,r6,r3

and r13,r5,r9 // x part
sub r4,r6,r13 // (sx+64) - dx
mul r4,r12,r4 // alpha * ((sx+64) - dx)
add r4,r13,r4,lsr #5 // (((alpha * ((sx+64) -dx )) >>5) + dx)
sub r4,r4,r8 // (((alpha * ((sx+64) -dx) >> 5) +dx )) - alpha*2
and r4,r4,r9

ldr r3,=0x10020040
and r7,r7,r10
add r7,r3,r7,lsr #5


and r13,r5,r10 // y part
sub r3,r7,r13,lsr #5 // (sy + 64) - dx
mul r3,r12,r3 // alpha * ((sy+64)-dy)
mov r13,r13,lsr #5
add r3,r13,r3,lsr #5 // (((alpha * ((sy+64) -dy )) >>5) + dx
sub r3,r3,lr
and r3,r10,r3,lsl #5

orr r5,r3,r4

str r5, [r0],#4
subs r11,r11, #1
bne blitNormalBlendAligned_wordcopy


It requires 2 aditional registers with preprocessed alpha value for GRB and RBG component.
This means if you are doing alpha blending with a static alpha then you can precalculate these values for the duration of entire blit, but if you are loading alpha from memory ( each pixel has its own alpha value) then you have to do preprocessing for these values at each itteration.

Check this
[url]http://forums.pocketmatrix.com/viewtopic.php?t=10743&start=30&highlight=zsurface
[/url] (look for my post at the botom of the page ) for explanation how this works.

PS.
Try to use this approach and see if it will speed up your code even more.
This alpha optimization combined with your RLE approach should result in truly ass-kicking performance.
I've tried this technique when I was still at non-asm optimisations, and the result was not better because of the preloading of big constants in memory and mul is not really slow on ARM.

And for my test case alpha level is hardly constant.

But this approache is very good for blending a color rectangle and I use it for that purpose.

Anyway this is a bit of speculation as I haven't tried any hand optimised version of this blending technique.