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)



  1. Of course, the freaking tabs are reported as one space.. what was the setting for the script for the setting for the script ?

    ..facking, give me back plain text !!!

  2. You are much smarter words can spell! Even I'm just wetting fingers into renderman shaders, there is still the chance I was right. I was just talking last night with my friend, about we have no chance to really understand, how your ancestors thought and, the real principles accupuncture works. I know how you feel about trimming, you see what I mean, because I'm a romanian. I've started doing in Carrara some shaders which I suppose they are renderman compliant, but I wish I knew more about it. I wish you the very best and, take care of yourself.
    (marius.iliescufeidyeu at gmail dot com)

  3. Hey Marius !

    Thanks for the support.. I checked your blog.. nice results indeed.. 3D models in swimsuits. I'll have to get me a test object like that someday ;)

  4. There is now on my blog a new wet cloth shader I did in Carrara. I knew nothing about it's rendering engine, I even didn't know if it's RM compatible - do not imagine I know RM but at least, I can read about it - and it proved it..., I'll better let you guess :).
    I wanted a fully generic shader, a wet look cloth shader. If you want to see the full image, don't left click on it, try right click, view image and zoom, otherwise left clicking will give you a 'deja-vu' :)
    I wish I could reference from Carrara 'formula' editor, the single place I can edit the shader, two meshes so as to be able to get the distance between them and, finally to get the shader depend absorption, scattering, bla, bla on that distance and, ideall I would like the shader displace - at render time - the mesh, to dynamically make it 'cling' to the body the 'wet' areas. I know this may sound stupid, especially when you are almost 50 yrs :)

  5. Interesting... I never used Carrara, so I'm not sure how shaders work there.

    Actually, I'm not even good with shaders in general (^^;)
    As a low level graphics/engine programmer, I mostly take care of making the shaders run.. while actual shader code is usually better written by artists/technical-artists/shader-programmers

    However, as I've been moving away from Cg and HLSL, I've been impressed with what is possible with more advanced systems such as RenderMan SL or Mental Ray ..and it makes me want to try and play with shader writing myself some day 8)

    I think that most shaders are probably generally built with GUI-based editors but the more complex ones are always going to require some programming 8)