viernes, 21 de junio de 2013

Avoid transparent sorting with discard() function

One of the most expensive tasks when rendering, is transparent sorting.... having to sort all transparent meshes from back to front to render them that way is very consuming... and with Ogre in Iphone there is a 300 or something limit to the number of transparent meshes that can be sorted.... I do not know why this limit exists, or if its really necessary, but it exists.... One solution to this 300 limit, is to make clusters of transparent meshes, joined into one mesh, but then you have certain artifacts....


I never really had an issue with this problem, i could sort everythin fine, and we didnt have a lot of renderiing performance issues, until a few days ago, with a level we could not optimize enough... this wasnt in IOS but in a console... but the problem and solution is the same...

I discovered a VERY SIMPLE solution to say, Alpha plant textures....alpha plants, are more an STENCIL than an actual Alpha... there is no real need to have grades of transparency...


There is a special function in CG, and probably in any language....

discard()

with this function in a shader you force a pixel not to be rendered.

This way an alpha plant mesh, or any other kind of stencil mesh, where you do not need grades of transparency needs not to be sorted, and in fact, it is not an alpha material.... you simply do not render pixles that have transparency not equal to 1.0.


so, the sample would be something on the lines of:


if(color.a < 0.9) //not 1.0 just to make life easier for desingners
{
      discard();
}


This is probably very common, but i didnt know about it :P