Saturday, September 26, 2009

Shader compier. Not my cup of tea, but can't do without it !

I finally added support for default parameters values in shaders, in the shader compiler.

Default shader parameters values are not just constant values but pieces of shader code themselves.

So, each shader, when called needs to either run those default sub-routines to fill standard values or let the values come from the call from the RIB file.

Now the shader:
surface matte( float Ka = 1; float Kd = 1;)
 normal Nf = faceforward(normalize(N), I);
 Oi = Os;
 Ci = Os * Cs * (Ka*ambient() + Kd*diffuse(Nf));
 N            global varying normal
 I            global varying vector
 Oi           global varying color
 Os           global varying color
 Ci           global varying color
 Cs           global varying color

 _@K19        constant  uniform float    1.000000

 Ka           parameter uniform float   
 Kd           parameter uniform float   


surface ;== matte

 ;==== Begin Def Params
__defparam_Ka: Ka     _@K19 
__defparam_Kd: Kd     _@K19 
 ;==== End Def Params

 mov.vv $v2    N     
 normalize $v3    $v2   
 mov.vv $v2    $v3   
 mov.vv $v3    I     
 faceforward $v4    $v2    $v3   
 mov.vv $v2    $v4   
 mov.vv Oi     Os    
 mul.vvv $v4    Os     Cs    
 ambient $v5   
 mul.vsv $v5    Ka     $v5   
 mov.vv $v6    $v2   
 diffuse $v7    $v6   
 mul.vsv $v6    Kd     $v7   
 add.vvv $v5    $v5    $v6   
 mul.vvv $v4    $v4    $v5   
 mov.vv Ci     $v4   

The shader assembler will look for the labels starting with "__defparam_" and what's after is the name of the variable whose code that defines the default value follows (English ?).
So, __defparam_Ka is the label for the code for the default value of the variable Ka (which in this case is just a copy of the value 1.0).

The shader compiler is still not complete by any means.. but it's good to be on it, because I think that many people that are trying to do rendering aren't quite understanding that they can't write a renderer without proper support for programmable shaders.

It's a complex matter, but you can't keep pretending that your renderer doesn't need it 8)