Cg

From OpenGL.org
Jump to: navigation, search

Cg or C for Graphics is a high level shading language created by NVIDIA to simplify vertex and fragment shader programming. Though it now supports geometry shaders as well.

Although Cg shares many syntactical similarities with C/C++, some features were modified to accommodate the inherent differences between CPU programming and GPU programming.

Background

As a result of technical advancements in graphics cards, some areas of 3D graphics programming have become quite complex. To simplify the process, new features were added to graphics cards, including the ability to modify their rendering pipelines using vertex and pixel shaders (and more recently geometry shaders).

In the beginning, vertex and pixel shaders were programmed at a very low level with only the assembly language of the graphics processing unit. Although using the assembly language gave the programmer complete control over code and flexibility, it was fairly hard to use (just as with CPU assembly languages). A portable, higher level language for programming the GPU was needed, so Cg was created to overcome these problems and make shader development easier.

Some of the benefits of using Cg over assembly are:

  • High level code is easier to learn, program, read, and understand than assembly code.
  • Cg code is portable to a wide range of hardware and platforms, unlike assembly code, which usually depends on hardware and the platforms it's written for.
  • The Cg compiler can optimize code and do lower level tasks automatically, which are hard to do and error prone in assembly.

Benefits: Why use Cg

Initially Cg was the only high level language for OpenGL programmers to program graphics hardware. With the arrival of GLSL on the scene and with HLSL being more popular on DirectX, it was thought that Cg would soon die. However, due to its "platform independent" nature, and with the upcoming Playstation 3 from Sony using Cg as the primary shading language, there has actually been an increasing trend towards using Cg in many engines. There are many reasons as listed below:

  • Cg shaders are more portable and many platforms are supported. Initially there were compatibility problems on many hardware (particularly from IHVs other than NVIDIA), but most seem to have been resolved now.
  • Cg has a syntax similar to HLSL (DirectX) and that makes porting Cg shaders to HLSL very easy. Sometimes Cg shaders can be compiled on DirectX without any modification.
  • Many games support multiple platforms and writing shaders in "native" shading languages can get extremely complicated, especially with engines providing many thousand built-in shaders. This is where Cg can come handy, providing the user with the "Write Once Run Everywhere" convenience. Information on whether Cg supports XBox-360 is not available.

Details

Data types

Cg has six basic data types, some of them are the same as in C, others are especially added for GPU programming, these types are:

  • float - a 32-bit floating point number
  • half - a 16-bit floating point number
  • int - a 32-bit integer
  • fixed - a 12-bit fixed point number
  • bool - a boolean variable
  • sampler* - represents a texture object

Cg also features vector and matrix data types that are based on the basic data types, such as float3, float4x4, such data types are quite common when dealing with 3D graphics programming, Cg also has struct and array data types, which work in a similar way to C equivalents.

Operators

Cg supports a wide range of operators, including the common arithmetic operators from C, the equivalent arithmetic operators for vector and matrix data types, and the common logical operators.

Functions and control structures

Cg shares the basic control structures with C, like if/else, while, and for. It also has a similar way of defining functions.

The standard Cg library

As in C, Cg features a set of functions for common tasks in GPU programming. Some of the functions have equivalents in C, like the mathematical functions abs and sin, while others are specialized in GPU programming tasks, like the texture mapping functions tex1D and tex2D.

The Cg runtime library

Cg programs are merely vertex, geometry, and pixel shaders, and they need supporting programs that handle the rest of the rendering process, Cg can be used with two APIs: OpenGL or DirectX, each has its own set of Cg functions to communicate with the Cg program, like setting the current Cg shader, passing parameters, and such tasks.

A sample Cg vertex shader

// input vertex
struct VertIn {
    float4 pos   : POSITION;
    float4 color : COLOR0;
};
 
// output vertex
struct VertOut {
    float4 pos   : POSITION;
    float4 color : COLOR0;
};
 
// vertex shader main entry
VertOut main(VertIn IN, uniform float4x4 modelViewProj) {
    VertOut OUT;
    OUT.pos     = mul(modelViewProj, IN.pos); // calculate output coords
    OUT.color   = IN.color; // copy input color to output
    OUT.color.z = 1.0f; // blue component of color = 1.0f
    return OUT;
}

Applications and games that use Cg

Further reading

See also

External links