C for graphics

from Wikipedia, the free encyclopedia

C for Graphics (abbreviation: Cg ) is a High Level Shader Language (not to be confused with High Level Shading Language ) established by Nvidia for writing vertex shader , pixel shader , geometry shader and tessellation shader programs . The language is largely independent of the underlying graphics API ( OpenGL and DirectX ) and the graphics card manufacturer (Nvidia and ATI ).

High-level language

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

syntax

The syntax of Cg is similar to the C programming language . Components are u. a. simple data types, arrays, conditions and loops. Due to the collaboration between NVIDIA and Microsoft, the syntax of Cg is very much based on HLSL.

Data types

  • int (32bit integer)
  • float (32bit floating point)
  • half (16bit floating point)
  • fixed (12bit fixed point, 1-1-10)
  • double
  • bool
  • sampler (for texture objects)

It should be noted that on some graphics cards everything is calculated on float.

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

parameter

Output parameters are marked with out :

out float4 pos : POSITION; // Eckpunkt, der vom Vertex-Programm weitergegeben wird
out float4 color : COLOR; // Farbe, die vom Vertex-Programm weitergegeben wird

Uniform parameters are set outside of the vertex program and do not change per corner point:

z. B. in an OpenGL program:

CGparameter timeParam = cgGetNamedParameter(vertexProgram, "time");
CGparameter modelviewParam = cgGetNamedParameter(vertexProgram, "modelview");
cgGLSetParameter1f(timeParam, 100);
cgGLSetStateMatrixParameter(modelviewParam, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY);

Then use in the Vertex program:

uniform float time;
uniform float4x4 modelview;

Access to OpenGL State

Normally, access takes place via uniform parameters. These parameters must always be set anew in the OpenGL program. In the case of an ARB profile, access to an OpenGL state is possible, e.g. B.

glstate.matrix.mvp // Projection * Modelview

Profiles

Another aspect is the ability to compile for different profiles. These are different versions of vertex or fragment programs, of which the best possible one is automatically activated for the existing hardware:

CGprofile vertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
cgGLSetOptimalOptions(vertexProfile);
CGprofile fragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
cgGLSetOptimalOptions(fragmentProfile);

Output profile:

cgGetProfileString(fragmentProfile)

Integration and programming

The generated program codes can be compiled separately and integrated as an external source into an executable high-level language program (e.g. C ++ ) or, alternatively, only translated at runtime.

First a context (memory for programs) has to be created and then the program has to be compiled:

CGcontext context = cgCreateContext();
CGprogram program = cgCreateProgramFromFile(context, CG_SOURCE, filename, profile, "main", NULL);
cgGLLoadProgram(program);  // Programm laden

The program is then activated and selected:

// Profil aktivieren (und Programm aktivieren)
cgGLEnableProfile(CGProfile profile);

// Ab hier läuft jeder glVertex() durchs eigene Vertex Program

// Profil (und Programm deaktivieren)
cgGLDisableProfile(CGProfile profile)

// Ab jetzt läuft wieder jeder glVertex() durch die Standard OpenGL
// Pipeline

// Aktuelles Vertex/Fragment Program festlegen
cgGLBindProgram(myVertexProgram);

Advantage and disadvantage

advantage

Since Cg is independent of both platform and graphic interfaces, a shader only needs to be written once in Cg and can be used for almost any rendering system. If a programmer works with GLSL and HLSL, shader effects must be written several times.

disadvantage

While the Cg shaders on graphics cards with NVIDIA GPU (e.g. the GeForce series) run optimally for the most part, the implementation for other graphics chips, such as those from AMD (e.g. the Radeon series) is usually rather poor. The other chip manufacturers - like AMD - have no direct access to this, as the Cg Toolkit is only developed by NVIDIA.

Web links

  • Cg at NVIDIA (English)