Cg programming language
Encyclopedia
Cg is a high-level shading language
Shading language
A shading language is a special programming language adapted to map on shader programming. Those kind of languages usually have special data types like color and normal...

 developed by Nvidia
NVIDIA
Nvidia is an American global technology company based in Santa Clara, California. Nvidia is best known for its graphics processors . Nvidia and chief rival AMD Graphics Techonologies have dominated the high performance GPU market, pushing other manufacturers to smaller, niche roles...

 in close collaboration with Microsoft
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...

 for programming vertex and pixel shaders
Shader
In the field of computer graphics, a shader is a computer program that is used primarily to calculate rendering effects on graphics hardware with a high degree of flexibility...

. It is very similar to Microsoft
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...

's HLSL
High Level Shader Language
The High Level Shader Language or High Level Shading Language is a proprietary shading language developed by Microsoft for use with the Microsoft Direct3D API. It is analogous to the GLSL shading language used with the OpenGL standard...

.

Cg is based on the C programming language
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 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 unit
Graphics processing unit
A graphics processing unit or GPU is a specialized circuit designed to rapidly manipulate and alter memory in such a way so as to accelerate the building of images in a frame buffer intended for output to a display...

s.

This language is only suitable for GPU programming and is not a general programming language.

The Cg compiler outputs DirectX
DirectX
Microsoft DirectX is a collection of application programming interfaces for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. Originally, the names of these APIs all began with Direct, such as Direct3D, DirectDraw, DirectMusic, DirectPlay,...

 or OpenGL
OpenGL
OpenGL is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL...

 shader programs.

Background

As a result of technical advancements 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 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.

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

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 and pixel shaders, and they need supporting programs that handle the rest of the rendering process. Cg can be used with two APIs: OpenGL
OpenGL
OpenGL is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL...

 or DirectX
DirectX
Microsoft DirectX is a collection of application programming interfaces for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. Originally, the names of these APIs all began with Direct, such as Direct3D, DirectDraw, DirectMusic, DirectPlay,...

. 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.

In addition to being able to compile Cg source to assembly code, the Cg runtime also has the ability to compile shaders during execution of the supporting program. This allows the runtime to compile the shader using the latest optimizations available for hardware that the program is currently executing on. However, this technique requires that the source code for the shader be available in plain text to the compiler, allowing the user of the program to access the source-code for the shader. Some developers view this as a major drawback of this technique.

To avoid exposing the source code of the shader, and still maintain some of the hardware specific optimizations, the concept of profiles was developed. Shaders can be compiled to suit different graphics hardware platforms (according to profiles). When executing the supporting program, the best/most optimized shader is loaded according to its profile. For instance there might be a profile for a graphics card that supports complex pixel shaders, and another profile for one that supports only minimal pixel shaders. By creating a pixel shader for each of these profiles a supporting program enlarges the number of supported hardware platforms without sacrificing picture quality on powerful systems.

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

  • Cafu Engine
    Cafu Engine
    The Cafu Engine is a game engine developed by Carsten Fuchs. It is portable across platforms and currently runs under Windows and Linux, with plans to be adapted to Mac OS...

  • Crystal Space
    Crystal Space
    Crystal Space is a framework for developing 3D applications written in C++ by Jorrit Tyberghein and others. The first public release was on August 26, 1997. It is typically used as a game engine but the framework is more general and can be used for any kind of 3D visualization. It is very...

  • EON Professional/Ultra
  • Far Cry
    Far Cry
    Far Cry is a first-person shooter video game developed by Crytek Studios from Germany and published by Ubisoft on March 23, 2004, for Microsoft Windows. Far Cry sold 730,000 units within four months of release. It received positive reviews upon release...

  • Lightfeather
  • OGRE
    OGRE Engine
    OGRE is a scene-oriented, flexible 3D rendering engine written in C++ designed to make it easier and intuitive for developers to produce applications utilizing hardware-accelerated 3D graphics...

  • Panda3d
    Panda3D
    Panda3D is a game engine which includes graphics, audio, I/O, collision detection, and other abilities relevant to the creation of 3D games.Panda3D is open source and, as of May 28, 2008, free software under the revised BSD license. Releases prior to May 28, 2008 are not considered Free Software...

  • Unity game engine
    Unity (game engine)
    Unity is an integrated authoring tool for creating 3D video games or other interactive content such as architectural visualizations or real-time 3D animations. Unity's development environment runs on Microsoft Windows and Mac OS X, and the games it produces can be run on Windows, Mac, Xbox 360,...

  • Virtools Dev
  • Battlefield 2
    Battlefield 2
    Battlefield 2 is a computer game by the Swedish developer Digital Illusions CE . During development, Trauma Studios contributed to the development of the game after it was acquired by DICE...

  • Enemy Territory: Quake Wars
    Enemy Territory: Quake Wars
    Enemy Territory: Quake Wars is a first-person shooter video game, and is the follow-up to the 2005 title Quake 4. It is also the first game in the series to be rated T by the ESRB...

  • Earths Special Forces - A Half-Life Mod
  • Hitman: Blood Money
    Hitman: Blood Money
    Hitman: Blood Money is a stealth game developed by IO Interactive and published by Eidos Interactive. It is the fourth entry in the Hitman game series....

  • R.U.S.E
  • Racer
    Racer (simulator)
    Racer, fully named Racer Free Car Simulation, is a free gaming simulator that runs on Microsoft Windows, Linux, and Mac OS X.Although Racer started out as a driving simulator, it also has features that are usually seen in racing games, such as racing against AI cars, or against human opponents in...

  • dolphin-emu
    Dolphin (emulator)
    Dolphin is an open-source Nintendo GameCube, Wii, and Triforce emulator for Microsoft Windows, Linux, and Mac OS X . It is the first emulator to successfully run commercial Nintendo GameCube and Wii games and is the only emulator capable of running commercial Wii games...

  • muvee Reveal
  • Eyeon Fusion
    Eyeon Fusion
    Fusion is an image compositing software program created by eyeon Software Inc. It is typically used to create visual effects and digital compositing for film, HD and commercials...

  • Garshasp

See also

  • Computer programming
    Computer programming
    Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

  • Computer graphics
    Computer graphics
    Computer graphics are graphics created using computers and, more generally, the representation and manipulation of image data by a computer with help from specialized software and hardware....

  • Vertex and pixel shaders
  • High level shader language
  • OpenGL shading language
    GLSL
    OpenGL Shading Language , is a high-level shading language based on the syntax of the C programming language...

  • Shader Model
  • OpenGL
    OpenGL
    OpenGL is a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL...

  • DirectX
    DirectX
    Microsoft DirectX is a collection of application programming interfaces for handling tasks related to multimedia, especially game programming and video, on Microsoft platforms. Originally, the names of these APIs all began with Direct, such as Direct3D, DirectDraw, DirectMusic, DirectPlay,...


Further reading

  • Randima Fernando, Mark J. Kilgard
    Mark Kilgard
    Mark J. Kilgard is a graphics software engineer working at Nvidia.Prior to joining Nvidia, Mark Kilgard worked at Compaq and Silicon Graphics. While at Silicon Graphics, he authored the OpenGL Utility Toolkit, better known as GLUT, to make it easy to write OpenGL-based 3D examples and demos. The...

    , The Cg Tutorial: The Definitive Guide to Programmable Real-Time Graphics, Addison-Wesley Professional, ISBN 0-321-19496-9
  • Randima Fernando, GPU Gems: Programming Techniques, Tips, and Tricks for Real-Time Graphics, Addison-Wesley Professional, ISBN 0-321-22832-4
  • William R. Mark, R. Steven Glanville, Kurt Akeley
    Kurt Akeley
    Kurt Akeley is a computer graphics engineer.-Biography:Kurt Akeley received a B.E.E. from the University of Delaware in 1980, and an M.S.E.E. from Stanford University in 1982...

    , Mark J. Kilgard
    Mark Kilgard
    Mark J. Kilgard is a graphics software engineer working at Nvidia.Prior to joining Nvidia, Mark Kilgard worked at Compaq and Silicon Graphics. While at Silicon Graphics, he authored the OpenGL Utility Toolkit, better known as GLUT, to make it easy to write OpenGL-based 3D examples and demos. The...

    , Cg: A System for Programming Graphics Hardware in a C-like Language, Proceedings of SIGGRAPH 2003.
  • Mark J. Kilgard
    Mark Kilgard
    Mark J. Kilgard is a graphics software engineer working at Nvidia.Prior to joining Nvidia, Mark Kilgard worked at Compaq and Silicon Graphics. While at Silicon Graphics, he authored the OpenGL Utility Toolkit, better known as GLUT, to make it easy to write OpenGL-based 3D examples and demos. The...

    , Cg in Two Pages, 2003.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK