Sommaire

Shaders




HLL Vertex and Pixel Programs

This implementation of vertex program and fragment program from openGL and vertex shaders 1.1 and pixel shaders 1.4 from directX, is developped to interpret and compile in assembly, pseudo code in a particular syntax.
The functionnalities of vertex program in the lib is designed to work on all cards even if graphic card driver doen't support it.(some divers doesn't have the open gl extension, some cards do not implement the ARB spefs even in software).
Here are some explanation on how to develop vertex shaders and pixel shaders and how to decline technique to fit destination graphic cards.
Note that there were some tricky stuff with writing "val -= u + v" that will only be expanded to "val = val - u + v" so that it may be written "val -= (u + v)"

       Functions:

       coord( TexCoordn )
       mul_x2 ( a , b )
       mul_x4 ( a , b )
       mul_sat ( a , b )
       dot ( a , b )
       dot_sat ( a , b )
       dotbx2 ( a , b ) ou dotofbx2 ( a , b )
       reciprocal ( x ) ou rcp ( x )
       rsq ( x )
       fractionnal ( x ) ou fract ( x ) ou frac ( x )
       cmp ( value , a , b ) ou compare ( value , a , b ) -> ( value > 0.0 ) ? a : b
       sge ( a , b )
       slt ( a , b )
       mix ( a , b , x )
       lerp ( a , b , x )
       lerp_sat ( a , b , x )
       smoothstep ( a , b , x )
       clamp ( x , min , max )
       max ( x , y )
       min ( x , y )
       normalise ( v ) ou normalize ( v )
       norme ( v ) ou norm ( v )
       inv ( v )
       mod ( x , modulo )
       pow ( x , e )
       log2 ( x )
       exp2 ( x )
       log ( x )
       exp ( x )
       abs ( x )
       floor ( x )
       round ( x )
       trunc ( x )
       degrees ( x )
       radians ( x )
       rsqrt ( x )
       sqrt ( x )
       atan ( x )
       atan2 ( x , y )
       tan ( x )
       asin ( x )
       sin ( x )
       acos ( x )
       cos ( x )
       tgt ( up , v )
       reflect ( u , n )
       refract ( u , n , eta )
       equation ( normal , point )
       plane ( v , eq )
       interpolate( v1 , v2 , t , M ) ou interpolation( v1 , v2 , t , M )
       normal_interpolate( v1 , v2 , t , M ) ou normal_interpolation( v1 , v2 , t , M )
       interpolate_base( v1 , v2 , t )
       normal_interpolate_base( v1 , v2 , t )
       iter( level )
       ValueToRGB( x )
       RGBToValue( rgb )
       SignedValueToRGB( x )
       RGBToSignedValue( rgba )
       Value16ToRGB( x )
       RGBToValue16( rg )
       SignedValue16ToRGB( x )
       RGBToSignedValue16( rg )
       sample[.xyzw] ( stage , uv )
       samplesecondary ( stage , uv )
       samplehalf[.xyzw] ( stage , uv )
       samplesecondaryhalf ( stage , uv )
       samplehalf16 ( stage , uv )
       samplesecondaryhalf16 ( stage , uv )
       sampleVS ( stage , uv )
       samplesecondaryVS ( stage , uv )
       sampleVShalf ( stage , uv )
       sampleVShalf16 ( stage , uv )
       samplesecondaryVShalf ( stage , uv )
       samplesecondaryVShalf16 ( stage , uv )
       sampleVSOffset ( stage , uv )
       sampleVSOffsetClamp ( stage , uv )
       sampleLOD ( stage , uv )
       halfValue16ToRGB( x , y )
       Quarter( x,z,raw0,raw1,raw2,raw3 )
       QuarterInterpolate( x,z,raw0,raw1,raw2,raw3 )
       QuarterInterpolateSin( x,z,koef,raw0,raw1,raw2,raw3 )
       GetPosition()
       GetNormal()
       GetPositionMatrix()
       GetNormalMatrix()
       SecondarySign(...)
       ScaleToSecondary(...)
       BoundsView(...)
       BoundsViewAffect(...)
       FallOffRange(...)
       FallOffRangeNorm(...)
       FallOffRangeValue(...)
       CompleteFallOffRange(...)
       CompleteFallOffRangeValue(...)
       ProcessShadowCoord(...)
       CalculateShadowMapping(...)
       CalculateShadowMappingIntegrate(...)
       NormalReflection(...)
       RelativeToNormal(...)
       HalfScale(...)
       HalfScaleNormalize(...)
       HalfScaleNorm(...)
       BoundsViewIntegrate(...)
       BoundsViewAffect(...)

Exemples of vertex program that works with lib3d vertex programs



Exemple1: Syntaxe

[VERTEXFORMAT]
       XYZ DIFFUSE TEX0-> vertex buffer format
[DEFINES]
       MATRIX WORLD;   -> link variables, constants
       MATRIX VIEWPROJ;
       CONST zero=;
       CONST one=1.0,1.0,1.0,1.0;
       TEXTURE tex;
       -> the words following [TECHNIQUE] can be the following :
       -> Shaders, Dot3, Multitexture, All Cards due to specification of the technique.
[TECHNIQUE]    All cards
[PASS0]
       [POSITION]      -> pseudo code
       u=iPos*4WORLD;
       oPos=u*4VIEWPROJ;
       [DIFFUSE]
       oDiffuse=one;
       [MAPPING0]
       oTexture0=iTexture0;
       [RENDER_STATE]  -> similar to directx
       Blend   False
       Texture[0]      tex
       ColorOp[0]      Modulate
       ColorArg1[0]    Texture
       ColorArg2[0]    Diffuse
       AlphaOp[0]      Modulate
       AlphaArg1[0]    Texture
       AlphaArg2[0]    Diffuse
       ColorOp[1]      Disable
       AlphaOp[1]      Disable
       Cull    None
       ZBuffer True
       ZBufferWrite    True
       TextureCoordinate[0]    TexCoo0
[END]



Exemple2: Environment mapping exemple

[VERTEXFORMAT]
       XYZ NORMAL TEX0
[DEFINES]
       CONST zero;
       CONST one=1.0,1.0,1.0,1.0;
       CONST half=0.5;
       VECTOR alpha;
       VECTOR color;
       MATRIX WORLD;
       MATRIX VIEWPROJ;
       TEXTURE tex;
       TEXTURE envmap;
[TECHNIQUE]    All cards
[PASS0]
       [POSITION]
       tPos=iPos*4WORLD;
       tNorm=iNorm*3WORLD;
       oPos=tPos*4VIEWPROJ;
       [DIFFUSE]
       c=color;
       oDiffuse=alpha*c;
       oDiffuse.w=one.x;
       [MAPPING0]
       oTexture1.x=iTexture0.x;
       oTexture1.y=iTexture0.y;
       [MAPPING1]
       oTexture1.x=half.x+half.x*tNorm.x;
       oTexture1.y=half.x+half.x*tNorm.y;
       [RENDER_STATE]
       ShadeMode       Smooth
       Texture[0]      tex
       Texture[1]      envmap
       ColorOp[0]      Modulate
       ColorArg1[0]    Texture
       ColorArg2[0]    Diffuse
       AlphaOp[0]      SelectArg2
       AlphaArg2[0]    Diffuse
       ColorOp[1]      Modulate
       ColorArg1[1]    Texture
       ColorArg2[1]    Current
       AlphaOp[1]      Disable
       Cull    Ccw
       ZBuffer True
       ZBufferWrite    True
       TextureCoordinate[0]    TexCoo0
[END]



Exemple3: Extrude silhouette volume of a degenerated edges vertex buffer

[VERTEXFORMAT]
       XYZ NORMAL TEX0
[DEFINES]
       MATRIX WORLD;
       MATRIX VIEWPROJ;
       VECTOR longueur;
       VECTOR Light;
       VECTOR Decal;
       CONST zero=;
       CONST one=1.0,1.0,1.0,1.0;
       TEXTURE tex;
       
[TECHNIQUE]    All cards
[PASS0]
       [POSITION]
       tPos=iPos*4WORLD;
       tNorm=iNorm*3WORLD;
       u=tPos-Light;
       s=sge((u|tNorm),zero);
       u1=tPos+longueur*s*u*rsq(u|u);
       oPos=u1*4VIEWPROJ;
       [DIFFUSE]
       oDiffuse.x=zero.x;
       oDiffuse.y=zero.y;
       oDiffuse.z=zero.z;
       oDiffuse.w=one.w-s.w;
       [MAPPING0]
       oTexture0=iTexture0+Decal;
       [RENDER_STATE]
       ShadeMode       Smooth
       Blend   True
       SrcBlendSrcAlpha
       DstBlendInvSrcAlpha
       Texture[0]      tex
       ColorOp[0]      SelectArg2
       ColorArg1[0]    Texture
       ColorArg2[0]    Diffuse
       AlphaOp[0]      SelectArg2
       AlphaArg2[0]    Diffuse
       ColorOp[1]      Disable
       AlphaOp[1]      Disable
       Cull    Ccw
       ZBuffer True
       ZBufferWrite    False
       TextureCoordinate[0]    TexCoo0
[PASS1]
       [POSITION]
       tPos=iPos*4WORLD;
       tNorm=iNorm*3WORLD;
       u=tPos-Light;
       s=sge((u|tNorm),zero);
       u1=tPos+longueur*s*u*rsq(u|u);
       oPos=u1*4VIEWPROJ;
       [DIFFUSE]
       oDiffuse.x=zero.x;
       oDiffuse.y=zero.y;
       oDiffuse.z=zero.z;
       oDiffuse.w=one.w-s.w;
       [MAPPING0]
       oTexture0=iTexture0+Decal;
       [RENDER_STATE]
       ShadeMode       Smooth
       Blend   True
       SrcBlendSrcAlpha
       DstBlendInvSrcAlpha
       Texture[0]      tex
       ColorOp[0]      SelectArg2
       ColorArg1[0]    Texture
       ColorArg2[0]    Diffuse
       AlphaOp[0]      SelectArg2
       AlphaArg2[0]    Diffuse
       ColorOp[1]      Disable
       AlphaOp[1]      Disable
       Cull    Cw
       ZBuffer True
       ZBufferWrite    False
       TextureCoordinate[0]    TexCoo0
[END]



Exemple4: Integrate owner shadow mapping shader to render pipeline

[VERTEXFORMAT]
       XYZ NORMAL TEX0
[DEFINES]
       TEXTURE Shd;      Shadow mapping texture
       VECTOR LIGHTPOS;      Position of light in world space (filled by class NED)
       VECTOR MULTI;      Factors and decals to calculate distance from light
       PSVECTOR Ambient;      ...
       VECTOR Light;       ...
       PSVECTOR Color;      ...
       MATRIX REF;      shadow mapping projection matrix(filled by class NED)
       ...
       oPos = pos *4 VIEWPROJ;
       ...
       ldir = pos - LIGHTPOS;
       dist.x = norme( ldir.xyz );
       posi = pos *4 REF;
[MAPPING0]
       oTexture0.xy = iTexture0.xy;      combine xy texture coordinate of the object
       oTexture0.z = (MULTI.x * dist.x) - MULTI.z;      with distance from light
[MAPPING1]
       oTexture1 = posi;
[SHADER]
       tmp = coord(Tex0);
       posi = coord(Tex1);
       posi = ProcessShadowCoord(posi);
       d.x = CalculateShadowMappingIntegrate(posi,tmp.z,2);
       ...
[RENDER_STATE]
       ...
       Texture[2] Shd
       Mip[2] Linear
       Mag[2] Linear
       Min[2] Linear
       ...
[PSFUNCTIONS]
       float calcshadow1(vec4 pos,float zval)
       {
          tmp = sample(2,pos.xy);
          tmp = RGBToValue(tmp);
          tmp.r = zval - tmp.x;
          tmp = cmp(tmp.r,psone,pszero);
          tmp.x = BoundsViewAffect(pos);
          tmp.x;
       };