PocketMatrix forums

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

Optimize z-buffered flat shading

i'm currently optimizing my rasterization code. until now i used 16-bits writes everywhere. changing non-z-buffered shading to 32-bits writes gave quite a speedup. for z-buffered shading things are more difficult. i posted below my flat shading inner loop.

32-bit alignment has been checked already.
c,z,cn,zn are all unsigned long.
dstIC & dstIZ are unsigned long*.
runZ & ZStep are fixed point.

Code:

while(w--)
{
int curZ = sgl_cast_int(runZ);
c = *dstIC;
z = *dstIZ;

if(curZ < (z>>16))
{
cn = col<<16;
zn = curZ<<16;
}
else
{
cn = (c&0xffff0000);
zn = (z&0xffff0000);
}

runZ += ZStep;
curZ = sgl_cast_int(runZ);

if(curZ < (z&0xffff))
{
cn |= col;
zn |= curZ;
}
else
{
cn |= (c&0x0000ffff);
zn |= (z&0x0000ffff);
}

*(dstIC++) = cn;
*(dstIZ++) = zn;

runZ += ZStep;
}


i get a fillrate of ~8MPixel/sec with this code. my 16-bits-writes code performs exactly at the same speed. has anybody an idea how to speed up this code?

i thought about using the Intel GPP rasterization routines, but they use 32-bits z-buffer which is a lot slower as i experienced...

thanks,
DANIEL
The purpose of a z-test is to determine if it's possible to do zero work for that pixel. Look at your code and note all of the work you're still doing even though the pixel is occluded. There is no use in doing the color read unless you've passed the z-test and you certainly shouldn't be doing a write to the color and z buffer each pixel (pixel pair in your case) if the z-test has failed.

As far as why things are running the same speed, the dumb question first: did you halve the value of w for your 32 bit case?

The compiler might be doing something dumb. You can have it generate an assembly listing by adding /FAcs to your compiler settings. There are plenty of us here that are well-versed in ARM assembly language if you have questions about the quality of the code generated by the compiler. Just ask.
yes i did half the w value :).

the purpose of always reading and writing was the try to use only 32-bit memory operations. of course one could optimize for the case where nothing has to be written at all.

maybe it makes even sense to optimize for cases where only 16 bits need to be updated.

i did test it with a setup where every pixel is drawn. so saving time by not processing anything for invisible pixels would not have increased performance in this test. i just wanted to see how this version compares to one that does no z-testing/writing: it is ~4x slower.

maybe i should just use the intel gpp and not bother about optimizing ppc code...

DANIEL
as mentioned above, get the compiler to generate an ASM listing and look at the variable usage. Offhand it looks like all your variables should fit in registers... I am willing to be that the compiler didn't do this. I have found that you need to state 'register' in front of local variables and copy global (or parameter) values into local variables (declared with 'register') in order to get the compiler to put everything in registers. Really, the best approach is to get the C++ version working as good as you can and then rewrite in ASM.
i now tried the corresponding GPP routine. sadly it is only exactly as fast as my 32-bits-zbuffer routine (which should be seen as unoptimized): 5 MPix/sec

since i can get 8 MPix/sec with 16-bits-zbuffer routine this means for me that the GPP rasterization is pretty uninteresting. at least for flat/z-buffered scanlines. maybe the more powerful routines (texturing with shading) are better...

DANIEL
Post the assembler code and let us hack it ;) As Digby said, plenty of us can "do" ARM :) ..and enjoy doing ARM (now there's a euphemism).
i took the advice of Digby and optimized the loop into all possible cases. the code now looks like this:

Code:

while(w--)
{
curZ = sgl_cast_int(runZ);

if(curZ < *dstZ) // first pixel visible
{
curZ2 = sgl_cast_int(runZ+ZStep);

if(curZ < *(dstZ+1)) // both pixels visible
{
*(unsigned int*)(dstC) = colI;
*(unsigned int*)(dstZ) = (curZ<<16) | curZ2;
}
else // only first pixel visible
{
*dstC = col;
*dstZ = curZ;
}
}
else // first pixel not visible
{
curZ2 = sgl_cast_int(runZ+ZStep);

if(curZ < *(dstZ+1)) // only second pixel visible
{
*(dstC+1) = col;
*(dstZ+1) = curZ2;
}
}

dstC += 2;
dstZ += 2;
runZ += ZStep2;
}


the result is a 13% speed-up (9.4 instead of 8.3MPix/sec) which means that i could draw 122fps full screen (no overdraw and no other overhead included) which should be fast enough anyhow.

nethertheless i'll be glad to bother you ASM pros when i start optimizing my gouraud shading or texture mapping routine (which will really need optimization).

thanks for now,
DANIEL
When you test the second pixel, in both cases, shouldn't you be testing against curZ2?

As you use it in both cases:

curZ2 = sgl_cast_int(runZ+ZStep);

can be moved up with

curZ = sgl_cast_int(runZ);
you are right.

in fact, if i do it correctly the speed of this "optimized" version is exactly the same as if i use the simple one-pixel-per-loop way. which is far less code:

Code:

register int curZ;
while(Width--) {
curZ = sgl_cast_uint(runZ);

if(curZ < *dstZ)
{
*(dstZ++) = curZ;
*(dstC++) = col;
}
else
{
dstC++;
dstZ++;
}

runZ += ZStep;
}


seems like the speed up was only because the compiler optimized away the second z-buffer read...

btw: i also tested this loop with the intel compiler and it produces code that is more than 2x slower. strange, strange...

DANIEL
[/code]