C for graphics

C for graphics

C for Graphics (Abkürzung: Cg) ist eine von NVIDIA begründete Shader-Hochsprache zum Schreiben von Vertex-Shader- und Pixel-Shader-Programmen. Die Sprache ist weitestgehend unabhängig von der zugrunde liegenden Grafik-API- (OpenGL und DirectX) und dem Grafikkartenhersteller (NVIDIA und ATI).

Inhaltsverzeichnis

Hochsprache

Die Entwicklung einer Shader-Hochsprache wurde dadurch motiviert, dass das Programmieren von Vertex- und Pixel-Shadern in Maschinensprache recht kompliziert und schnell unübersichtlich ist. Weitere Shader-Hochsprachen sind GLSL, HLSL und RenderMan. Einen anderen Ansatz verfolgt Sh als Metasprache.

Syntax

Cg ähnelt von der Syntax her der Programmiersprache C. Bestandteile sind u.a. einfache Datentypen, Arrays, Bedingungen und Schleifen.

Datentypen

  • int (32bit integer)
  • float (32bit floating point)
  • half (16bit floating point)
  • fixed (12bit fixed point, 1-1-10)
  • double
  • bool
  • sampler (für Texturobjekte)

Es sei darauf hingewiesen, dass auf manchen Grafikkarten alles auf float gerechnet wird.

Hinzu kommen die entsprechenden Vektoren und Matrizen: float2, float3, float4, float4x4.

Parameter

Ausgabe-Parameter werden mit out gekennzeichnet:

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

Uniform-Parameter werden außerhalb vom Vertex-Programm gesetzt und ändern sich nicht pro Eckpunkt:

z.B. in einem OpenGL-Programm:

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

Verwendung dann im Vertex-Programm:

 uniform float time;
 uniform float4x4 modelview;

Zugriff auf OpenGL State

Normalerweise erfolgt der Zugriff über Uniform-Parameter. Diese Parameter müssen im OpenGL-Programm immer neu gesetzt werden. Im Falle eines ARB-Profils ist der Zugriff auf einen OpenGL State möglich, z.B.

 glstate.matrix.mvp // Projection * Modelview

Profile

Ein weiterer Aspekt ist die Möglichkeit zum Kompilieren für verschiedene Profile. Das sind verschiedene Versionen von Vertex- bzw. Fragment-Programmen, von denen automatisch das bestmögliche für die vorhandene Hardware aktiviert wird:

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

Profil ausgeben lassen:

 cgGetProfileString(fragmentProfile)

Einbindung und Programmierung

Die erstellten Programm-Codes können separat kompiliert und als externe Quelle in ein lauffähiges Hochsprachen-Programm (z.B. C++) eingebunden oder alternativ erst zur Laufzeit übersetzt werden.

Es muss zunächst ein Kontext (Speicher für Programme) erzeugt und dann das Programm kompiliert werden:

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

Anschließend erfolgen Aktivierung und Auswahl des Programms:

 // 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);

Weblinks


Wikimedia Foundation.

Игры ⚽ Поможем решить контрольную работу

Schlagen Sie auch in anderen Wörterbüchern nach:

  • PCI Express for Graphics — PCI Express Logo PCI Express („Peripheral Component Interconnect Express“, abgekürzt: PCIe oder PCI E) ist ein Erweiterungsstandard zur Verbindung von Peripheriegeräten mit dem Chipsatz eines Hauptprozessors. PCIe ist der Nachfolger von PCI und… …   Deutsch Wikipedia

  • Graphics — (from Greek gr. ; see graphy) are visual presentations on some surface, such as a wall, canvas, computer screen, paper, or stone to brand, inform, illustrate, or entertain. Examples are photographs, drawings, Line Art, graphs, diagrams,… …   Wikipedia

  • Graphics processing unit — GPU redirects here. For other uses, see GPU (disambiguation). GeForce 6600GT (NV43) GPU A graphics processing unit or GPU (also occasionally called visual processing unit or VPU) is a specialized circuit designed to rapidly manipulate and alter… …   Wikipedia

  • Graphics tablet — This article is about the computer input device. For other tablet devices, see Tablet device (disambiguation). A graphics tablet (or digitizer, digitizing tablet, graphics pad, drawing tablet) is a computer input device that enables a user to… …   Wikipedia

  • Graphics Execution Manager — GEM (Graphics Execution Manager) is a modern memory manager for graphics chipsets for the Linux OS. It manages graphics memory, controls the execution context and manages the NUMA environment on modern Graphics chipsets. Multiple applications can …   Wikipedia

  • Graphics suite — A graphics suite is a software suite for graphics work that are distributed together. The programs are usually able to interact with each other on a higher level than the operating system would normally allow.There is no hard, fast rule regarding …   Wikipedia

  • Graphics BASIC — is a third party extension to the Commodore BASIC V2.0 programming language of the Commodore 64 computer. It was originally written in 1983 by Ron Gilbert and Tom McFarlane. The program was licensed to Hesware, who briefly sold the program in… …   Wikipedia

  • Graphics Layout Engine — (GLE) is a graphics scripting language designed for creating publication quality graphs, plots, diagrams, figures and slides. GLE supports various graph types such as function plots, histograms, bar graphs, scatter plots, contour lines, color… …   Wikipedia

  • Graphics Execution Manager — Graphics Execution Manager, ou GEM, est un gestionnaire de mémoire pour processeurs graphiques inclus dans le noyau Linux. Il est conçu pour décharger les pilotes graphiques de cette tâche. GEM a été développé initialement pour les architectures… …   Wikipédia en Français

  • For You, For Me Tour — Tour by Kylie Minogue Start date September 30, 2009 End date …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”