Cg (programming language)

C for Graphics (abbreviation: Cg) is a well-founded NVIDIA, so-called 'High Level Shader Language ' (not to be confused with HLSL ) for writing vertex shader, pixel shader, geometry shader and tessellation shaders - programs. The language is largely independent of the underlying graphics API (OpenGL and DirectX ) and the graphics card manufacturers (NVIDIA and ATI).

Standard language

The development of a high shader language was motivated by the fact that the programming of vertex and pixel shaders is quite complicated and confusing quickly in machine language. Other languages ​​are GLSL shader high, GLSL ES, HLSL and RenderMan. A different approach than Sh metalanguage.


Cg is similar in syntax, the C programming language components include simple data types, arrays, conditionals and loops. Because of the collaboration between NVIDIA and Microsoft, the syntax of Cg is very heavily based on HLSL.

Data types

  • Int (32bit integer)
  • Float ( 32bit floating point)
  • Half (16-bit floating point)
  • Fixed ( 12bit fixed point, 1-1-10 )
  • Stand-in
  • Bool
  • Sampler ( for texture objects )

It should be noted that on some video cards everything is expected to float.

Then there are the corresponding vectors and matrices: float2, float3, float4, float4x4.


Output parameters are marked with out:

Out float4 pos: POSITION; / / Vertex, which is passed from the vertex program out float4 color: COLOR; / / Color that is passed from the vertex program Uniform parameters are set outside the vertex program and do not change per vertex:

E.g. in an OpenGL program:

CGparameter timeParam = cgGetNamedParameter ( vertex program, "time" ); CGparameter modelviewParam = cgGetNamedParameter ( vertex program, "model view" ); cgGLSetParameter1f ( timeParam, 100); cgGLSetStateMatrixParameter ( modelviewParam, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY ); Then use the vertex program:

Uniform float time; uniform float4x4 modelview; Access to OpenGL state

Normally, access is through uniform parameters. This parameter must always be reset in the OpenGL program. In the case of ARB - profile access an OpenGL state is possible, for example,

Glstate.matrix.mvp / / * Projection Model View profiles

Another aspect is the ability to compile for various profiles. These are different versions of vertex and fragment programs, of which the best is automatically enabled for the existing hardware:

CGprofile vertex profile = cgGLGetLatestProfile ( CG_GL_VERTEX ); cgGLSetOptimalOptions ( vertex profile ); CGprofile fragment profile = cgGLGetLatestProfile ( CG_GL_FRAGMENT ); cgGLSetOptimalOptions (fragment profiles ); Can spend profile:

CgGetProfileString (fragment profile ) Integration and Programming

The created program code can be compiled separately and alternatively be translated at runtime as an external source into an executable high-level language program (eg C ) or integrated.

There must first be a context ( program memory ) is generated and the program to be compiled:

CGcontext context = cgCreateContext (); CGprogram program = cgCreateProgramFromFile (context, CG_SOURCE filename, profiles, "main", NULL); cgGLLoadProgram (program); / / Load the program Next follows the activation and selection of the program:

/ / Activate the profile ( and enable program ) cgGLEnableProfile ( CGprofile profile ); ... / / From here runs every glVertex () through its own Vertex Program ... / / Profile ( and program disable ) cgGLDisableProfile ( CGprofile profile ) ... / / From now running again every glVertex () by the standard OpenGL / / The pipeline ... / / Set the current vertex / fragment program cgGLBindProgram ( myVertexProgram ); Advantages and disadvantage


Since Cg both Platform - as well as graphical interfaces is independent, a shader must be written only once in Cg and can be used for near to each render system. Does a programmer with GLSL and HLSL shader must be written multiple effects.


While the Cg shaders largely optimized for graphics cards with NVIDIA GPU (eg the GeForce series ), the implementation for other graphics chips, such as those from AMD (eg the Radeon series) is usually rather poor. Then have the other chip manufacturers - such as AMD - but no direct access since the Cg toolkit is only developed by NVIDIA.