GLSL : nVidia specific features

From OpenGL.org
Jump to: navigation, search

Cg inside GLSL

nVidia's implementation offers some extra features for GLSL.
nVidia adds Cg features to GLSL.
That means you can use Cg language features such as built-in functions (lerp(), etc) and types (half, half2, etc) and your shader will compile.
Keep in mind that it will compile and yet it will give warning messages in the info log telling you that a certain line of your code has a non-standard GLSL code.
You must not define the version number at the top of your shader. Example for GLSL 1.10

 #version 110

If you do include the line above, instead of warning messages, it gives you error messages telling you your code is non-standard.

What can you do so that your shader compiles on nVidia and ATI/AMD?
Example :

 #ifndef __GLSL_CG_DATA_TYPES
 #define half float
 #define half2 vec2
 #define half3 vec3
 #define half4 vec4
 #endif
 //-------------
 half3 myNormal = normalize(myVector);
 half4 texel = texture2D(Texture0, TexCoord);
 float value = x + y;
 //and so on...

In the above code, if __GLSL_CG_DATA_TYPES is defined (yes it is defined if you are on a nVidia system) so your shader will use the half float precision type which may or may not give you speed improvements.
If you are on ATI/AMD, __GLSL_CG_DATA_TYPES is not defined, so we define half to be float, half2 to be vec2, etc... and your shader will compile just fine even on ATI/AMD.

noise

The noise function on nVidia implementations return always 0.0 since the GPU doesn't implement it. On ATI/AMD, since their GPUs doesn't support hardware noise either, they decided on a software solution. They probably perform perlin noise which takes quite a few instructions.