You are on page 1of 2

Cg (short for C for Graphics) is a high-level shading language developed by Nvidia in close

collaboration with Microsoft for programming vertex and pixel shaders. Cg is based on the C
programming language and although they share the same syntax, some features of C were
modified and new data types were added to make Cg more suitable for programming graphics
processing units. This language is only suitable for GPU programming and is not a general
programming language. The Cg compiler outputs DirectX or OpenGL shader programs.
Since 2012, Cg is deprecated, with no additional development or support available.[1]

Contents

1 Background

2 Details
o 2.1 Data types
o 2.2 Operators
o 2.3 Functions and control structures
o 2.4 The standard Cg library
o 2.5 The Cg runtime library
o 2.6 A sample Cg vertex shader

3 Applications and games that use Cg

4 See also

5 References

6 Further reading

7 External links

Background
Due to technical advances in graphics hardware, 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.
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. 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 maintain 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.

Details
Data types
Cg has six basic data types. Some of them are the same as in C, while others are especially
added for GPU programming. These types are:

float - a 32bit floating point number

half - a 16bit floating point number

int - a 32bit integer

fixed - a 12bit 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 and 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 their C
equivalents.

Operators

You might also like