SlideShare une entreprise Scribd logo
1  sur  2
Télécharger pour lire hors ligne
Cg in Two Pages
                                                              Mark J. Kilgard
                                                             NVIDIA Corporation
                                                                Austin, Texas
                                                              January 16, 2003

                                                                             The out modifier indicates that clipPosition, oColor,
1. Cg by Example                                                             oDecalCoord, and oLightMapCoord parameters are output by the
Cg is a language for programming GPUs. Cg programs look a lot
                                                                             program. The semantics that follow these parameters are therefore
like C programs. Here is a Cg vertex program:
                                                                             output semantics. The respective semantics indicate the program
void simpleTransform(float4 objectPosition : POSITION,                       outputs a transformed clip-space position and a scaled color. Also,
                     float4 color          : COLOR,                          two sets of texture coordinates are passed through. The resulting
                     float4 decalCoord     : TEXCOORD0,                      vertex is feed to primitive assembly to eventually generate a
                     float4 lightMapCoord : TEXCOORD1,                       primitive for rasterization.
                 out float4 clipPosition : POSITION,
                 out float4 oColor         : COLOR,                          Compiling the program requires the program source code, the
                 out float4 oDecalCoord    : TEXCOORD0,                      name of the entry function to compile (simpleTransform), and a
                 out float4 oLightMapCoord : TEXCOORD1,                      profile name (vs_1_1).
             uniform float brightness,                                       The Cg compiler can then compile the above Cg program into the
             uniform float4x4 modelViewProjection)                           following DirectX 8 vertex shader:
{                                                                              vs.1.1
  clipPosition = mul(modelViewProjection, objectPosition);                     mov oT0, v7
  oColor = brightness * color;                                                 mov oT1, v8
  oDecalCoord = decalCoord;
                                                                               dp4 oPos.x, c1, v0
  oLightMapCoord = lightMapCoord;
}                                                                              dp4 oPos.y, c2, v0
                                                                               dp4 oPos.z, c3, v0
1.1 Vertex Program Explanation                                                 dp4 oPos.w, c4, v0
The program transforms an object-space position for a vertex by a              mul oD0, c0.x, v5
4x4 matrix containing the concatenation of the modeling, viewing,            The profile indicates for what API execution environment the
and projection transforms. The resulting vector is output as the             program should be compiled. This same program can be compiled
clip-space position of the vertex. The per-vertex color is scaled by         for the DirectX 9 vertex shader profile (vs_2_0), the multi-vendor
a floating-point parameter prior to output. Also, two texture                OpenGL vertex program extension (arbvp1), or NVIDIA-
coordinate sets are passed through unperturbed.                              proprietary OpenGL extensions (vp20 & vp30).
Cg supports scalar data types such as float but also has first-class         The process of compiling Cg programs can take place during the
support for vector data types. float4 represents a vector of four            initialization of your application using Cg. The Cg runtime
floats. float4x4 represents a matrix. mul is a standard library              contains the Cg compiler as well as API-dependent routines that
routine that performs matrix by vector multiplication. Cg provides           greatly simplify the process of configuring your compiled program
function overloading like C++; mul is an overloaded function so it           for use with either OpenGL or Direct3D.
can be used to multiply all combinations of vectors and matrices.
                                                                             1.2 Fragment Program Explanation
Cg provides the same operators as C. Unlike C however, Cg
                                                                             In addition to writing programs to process vertices, you can write
operators accept and return vectors as well as scalars. For example,
                                                                             programs to process fragments. Here is a Cg fragment program:
the scalar, brightness, scales the vector, color, as you would
expect.                                                                      float4 brightLightMapDecal(float4 color              : COLOR,
                                                                                                           float4 decalCoord      : TEXCOORD0,
In Cg, declaring a parameter with the uniform modifier indicates                                           float4 lightMapCoord : TEXCOORD1,
that its value is initialized by an external source that will not vary                           uniform sampler2D decal,
over a given batch of vertices. In this respect, the uniform modifier                            uniform sampler2D lightMap) : COLOR
in Cg is different from the uniform modifier in RenderMan but                {
used in similar contexts. In practice, the external source is some             float4 d = tex2Dproj(decal, decalCoord);
OpenGL or Direct3D state that your application takes care to load              float4 lm = tex2Dproj(lightMap, lightMapCoord);
appropriately. For example, your application must supply the                   return 2.0 * color * d * lm;
modelViewProjection matrix and the brightness scalar. The Cg                 }
runtime library provides an API for loading your application state           The input parameters correspond to the interpolated color and two
into the appropriate API state required by the compiled program.             texture coordinate sets as designated by their input semantics.
The POSITION, COLOR, TEXCOORD0, and TEXCOORD1 identifiers                    The sampler2D type corresponds to a 2D texture unit. The Cg
following the objectPosition, color, decalCoord, and                         standard library routine tex2Dproj performs a projective 2D
lightMapCoord parameters are input semantics. They indicate how              texture lookup. The two tex2Dproj calls sample a decal and light
their parameters are initialized by per-vertex varying data. In              map texture and assign the result to the local variables, d and lm,
OpenGL, glVertex commands feed the POSITION input semantic;                  respectively.
glColor commands feed the COLOR semantic; glMultiTexCoord
commands feed the TEXCOORDn semantics.


                                                                         1
The program multiplies the two textures results, the interpolated            vec1.xw = vec3; // vec1 = (3.0, -2.0, 5.0, 3.0)
color, and the constant 2.0 together and returns this RGBA color.           Use either .xyzw or .rgba suffixes swizzling and write masking.
The program returns a float4 and the semantic for the return value
                                                                            The Cg standard library includes a large set of built-in functions
is COLOR, the final color of the fragment.
                                                                            for mathematics (abs, dot, log2, reflect, rsqrt, etc.) and texture
The Cg compiler generates the following code for                            access (texCUBE, tex3Dproj, etc.). The standard library makes
brightLightMapDecal when compiled with the arbfp1 multi-                    extensive use of function overloading (similar to C++) to support
vendor OpenGL fragment profile:                                             different vector lengths and data types. There is no need to use
  !!ARBfp1.0                                                                #include to obtain prototypes for standard library routines as in C;
  PARAM c0 = {2, 2, 2, 2}; TEMP R0; TEMP R1; TEMP R2;                       Cg standard library routines are automatically prototyped.
  TXP R0, fragment.texcoord[0], texture[0], 2D;                             In addition to the out modifier for call-by-result parameter
  TXP R1, fragment.texcoord[1], texture[1], 2D;                             passing, the inout modifier treats a parameter as both a call-by-
  MUL R2, c0.x, fragment.color.primary;
                                                                            value input parameter and a call-by-result output parameter.
  MUL R0, R2, R0;
  MUL result.color, R0, R1;                                                 The discard keyword is similar to return but aborts the
  END                                                                       processing without returning a transformed fragment.
This same program also compiles for the DirectX 8 and 9 profiles
(ps_1_3 & ps_2_x) and NVIDIA-proprietary OpenGL extensions
                                                                            2.3 Features Not Supported
                                                                            Cg has no support currently for pointers or bitwise operations
(fp20 & fp30).
                                                                            (however, the necessary C operators and keywords are reserved for
2. Other Cg Functionality                                                   this purpose). Cg does not (currently) support unions and function
                                                                            variables.
2.1 Features from C
                                                                            Cg lacks C++ features for “programming in the large” such as
Cg provides structures and arrays, including multi-dimensional
                                                                            classes, templates, operator overloading, exception handling, and
arrays. Cg provides all of C’s arithmetic operators (+, *, /, etc.).
                                                                            namespaces.
Cg provides a boolean type and boolean and relational operators
(||, &&, !, etc.). Cg provides increment/decrement (++/--) operators,       The Cg standard library lacks routines for functionality such as
the conditional expression operator (?:), assignment expressions            string processing, file input/output, and memory allocation, which
(+=, etc.), and even the C comma operator.                                  is beyond the specialized scope of Cg.
Cg provides user-defined functions (in addition to pre-defined              However, Cg reserves all C and C++ keywords so that features
standard library functions), but recursive functions are not allowed.       from these languages could be incorporated into future
Cg provides a subset of C’s control flow constructs (do, while,             implementations of Cg as warranted.
for, if, break, continue); other constructs such as goto and
switch are not supported in current the current Cg implementation           3. Profile Dependencies
but the necessary keywords are reserved.                                    When you compile a C or C++ program, you expect it to compile
                                                                            without regard to how big (within reason) the program is or what
Like C, Cg does not mandate the precision and range of its data             the program does. With Cg, a syntactically and semantically
types. In practice, the profile chosen for compilation determines           correct program may still not compile due to limitations of the
the concrete representation for each data type. float, half, and            profile for which you are compiling the program.
double are meant to represent continuous values, ideally in
                                                                            For example, it is currently an error to access a texture when
floating-point, but this can depend on the profile. half is intended
                                                                            compiling with a vertex profile. Future vertex profiles may well
for a 16-bit half-precision floating-point data type. (NVIDIA’s
                                                                            allow texture accesses, but existing vertex profiles do not. Other
CineFX architecture provides such a data type.) int is an integer
                                                                            errors are more inherent. For example, a fragment profile should
data type, usually used for looping and indexing. fixed is an
                                                                            not output a parameter with a TEXCOORD0 semantic. Other errors
additional data type intended to represent a fixed-point continuous
                                                                            may be due to exceeding a capacity limit of current GPUs such as
data type that may not be floating-point.
                                                                            the maximum number of instructions or the number of texture units
Cg provides #include, #define, #ifdef, etc. matching the C                  available.
preprocessor. Cg supports C and C++ comments.
                                                                            Understand that these profile dependent errors do not reflect
2.2 Additional Features Not in C                                            limitations of the Cg language, but rather limitations of the current
Cg provides built-in constructors (similar to C++ but not user-             implementation of Cg or the underlying hardware limitations of
defined) for vector data types:                                             your target GPU.
  float4 vec1 = float4(4.0, -2.0, 5.0, 3.0);                                4. Compatibility and Portability
Swizzling is a way of rearranging components of vector values and           NVIDIA's Cg implementation and Microsoft's High Level Shader
constructing shorter or longer vectors. Example:                            Language (HLSL) are very similar as they were co-developed.
  float2 vec2 = vec1.yx;          // vec2 = (-2.0, 4.0)                     HLSL is integrated with DirectX 9 and the Windows operating
  float scalar = vec1.w;          // scalar = 3.0                           system. Cg provides support for multiple APIs (OpenGL, Direct X
  float3 vec3 = scalar.xxx;       // vec3 = (3.0, 3.0, 3.0)                 8, and Direct X 9) and multiple operating systems (Windows,
More complicated swizzling syntax is available for matrices.                Linux, and Mac OS X). Because Cg interfaces to multi-vendor
Vector and matrix elements can also be accessed with standard               APIs, Cg runs on GPUs from multiple vendors.
array indexing syntax as well.
                                                                            5. More Information
Write masking restricts       vector   assignments    to   indicated        In March 2003, look for The Cg Tutorial: The Definitive Guide to
components. Example:                                                        Programmable Real-Time Graphics (ISBN 0321194969) published
                                                                            by Addison-Wesley.
                                                                        2

Contenu connexe

Tendances

Couverture erts2012
Couverture erts2012Couverture erts2012
Couverture erts2012AdaCore
 
Migrating from OpenGL to Vulkan
Migrating from OpenGL to VulkanMigrating from OpenGL to Vulkan
Migrating from OpenGL to VulkanMark Kilgard
 
Gallium3D - Mesa's New Driver Model
Gallium3D - Mesa's New Driver ModelGallium3D - Mesa's New Driver Model
Gallium3D - Mesa's New Driver ModelChia-I Wu
 
High Speed Decoding of Non-Binary Irregular LDPC Codes Using GPUs (Paper)
High Speed Decoding of Non-Binary Irregular LDPC Codes Using GPUs (Paper)High Speed Decoding of Non-Binary Irregular LDPC Codes Using GPUs (Paper)
High Speed Decoding of Non-Binary Irregular LDPC Codes Using GPUs (Paper)Enrique Monzo Solves
 
Lpc2020 the-light weight-jit-compiler-slides
Lpc2020 the-light weight-jit-compiler-slidesLpc2020 the-light weight-jit-compiler-slides
Lpc2020 the-light weight-jit-compiler-slidesVladimir Makarov
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handoutSuraj Kumar
 
Yet Another Three QVT Languages
Yet Another Three QVT LanguagesYet Another Three QVT Languages
Yet Another Three QVT LanguagesEdward Willink
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDBDavid Khosid
 
C++ Interface Versioning
C++ Interface VersioningC++ Interface Versioning
C++ Interface VersioningSkills Matter
 
Web technology slideshare
Web technology slideshareWeb technology slideshare
Web technology slideshareGuruAbirami2
 
Open Standards for ADAS: Andrew Richards, Codeplay, at AutoSens 2016
Open Standards for ADAS: Andrew Richards, Codeplay, at AutoSens 2016Open Standards for ADAS: Andrew Richards, Codeplay, at AutoSens 2016
Open Standards for ADAS: Andrew Richards, Codeplay, at AutoSens 2016Andrew Richards
 
On Context-Orientation in Aggregate Programming
On Context-Orientation in Aggregate ProgrammingOn Context-Orientation in Aggregate Programming
On Context-Orientation in Aggregate ProgrammingRoberto Casadei
 
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineTwo-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineYusuke Izawa
 

Tendances (20)

Couverture erts2012
Couverture erts2012Couverture erts2012
Couverture erts2012
 
Gnu debugger
Gnu debuggerGnu debugger
Gnu debugger
 
Part 1
Part 1Part 1
Part 1
 
Migrating from OpenGL to Vulkan
Migrating from OpenGL to VulkanMigrating from OpenGL to Vulkan
Migrating from OpenGL to Vulkan
 
Gallium3D - Mesa's New Driver Model
Gallium3D - Mesa's New Driver ModelGallium3D - Mesa's New Driver Model
Gallium3D - Mesa's New Driver Model
 
GCC, GNU compiler collection
GCC, GNU compiler collectionGCC, GNU compiler collection
GCC, GNU compiler collection
 
High Speed Decoding of Non-Binary Irregular LDPC Codes Using GPUs (Paper)
High Speed Decoding of Non-Binary Irregular LDPC Codes Using GPUs (Paper)High Speed Decoding of Non-Binary Irregular LDPC Codes Using GPUs (Paper)
High Speed Decoding of Non-Binary Irregular LDPC Codes Using GPUs (Paper)
 
Typing Concerns
Typing ConcernsTyping Concerns
Typing Concerns
 
Lpc2020 the-light weight-jit-compiler-slides
Lpc2020 the-light weight-jit-compiler-slidesLpc2020 the-light weight-jit-compiler-slides
Lpc2020 the-light weight-jit-compiler-slides
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handout
 
Yet Another Three QVT Languages
Yet Another Three QVT LanguagesYet Another Three QVT Languages
Yet Another Three QVT Languages
 
Advanced Debugging with GDB
Advanced Debugging with GDBAdvanced Debugging with GDB
Advanced Debugging with GDB
 
C++ Interface Versioning
C++ Interface VersioningC++ Interface Versioning
C++ Interface Versioning
 
Web technology slideshare
Web technology slideshareWeb technology slideshare
Web technology slideshare
 
Usage of GDB
Usage of GDBUsage of GDB
Usage of GDB
 
Open Standards for ADAS: Andrew Richards, Codeplay, at AutoSens 2016
Open Standards for ADAS: Andrew Richards, Codeplay, at AutoSens 2016Open Standards for ADAS: Andrew Richards, Codeplay, at AutoSens 2016
Open Standards for ADAS: Andrew Richards, Codeplay, at AutoSens 2016
 
On Context-Orientation in Aggregate Programming
On Context-Orientation in Aggregate ProgrammingOn Context-Orientation in Aggregate Programming
On Context-Orientation in Aggregate Programming
 
Unit i
Unit iUnit i
Unit i
 
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineTwo-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One Engine
 
I965g
I965gI965g
I965g
 

En vedette

SOFAVR_jesnault_19-09-11_presentation1
SOFAVR_jesnault_19-09-11_presentation1SOFAVR_jesnault_19-09-11_presentation1
SOFAVR_jesnault_19-09-11_presentation1Jérôme Esnault
 
Celine Deswarte EU #MWC14 #mHealth
Celine Deswarte EU #MWC14 #mHealthCeline Deswarte EU #MWC14 #mHealth
Celine Deswarte EU #MWC14 #mHealth3GDR
 
M health 2.0 161111_maison_chimie
M health 2.0 161111_maison_chimieM health 2.0 161111_maison_chimie
M health 2.0 161111_maison_chimiePelhammedia
 
POLYCLINIQUE LES JASMINS - Batimaghreb Expo
POLYCLINIQUE LES JASMINS - Batimaghreb ExpoPOLYCLINIQUE LES JASMINS - Batimaghreb Expo
POLYCLINIQUE LES JASMINS - Batimaghreb ExpoMade in Tunisia
 
Urgences au cabinet médical - trousse d'urgence
Urgences au cabinet médical - trousse d'urgenceUrgences au cabinet médical - trousse d'urgence
Urgences au cabinet médical - trousse d'urgenceArnaud Depil-Duval
 
Le cabinet médical
Le cabinet médicalLe cabinet médical
Le cabinet médicalguestd5d8f
 
Centre international carthage médical batimaghreb expo
Centre international carthage médical   batimaghreb expoCentre international carthage médical   batimaghreb expo
Centre international carthage médical batimaghreb expoMade in Tunisia
 
Samuel Gaillard, Logival pour la journée e-health 2013
Samuel Gaillard, Logival pour la journée e-health 2013Samuel Gaillard, Logival pour la journée e-health 2013
Samuel Gaillard, Logival pour la journée e-health 2013Thearkvalais
 
Accelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
Accelerating Vector Graphics Rendering using the Graphics Hardware PipelineAccelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
Accelerating Vector Graphics Rendering using the Graphics Hardware PipelineMark Kilgard
 

En vedette (11)

gts madical green(fRENCH)
gts madical green(fRENCH)gts madical green(fRENCH)
gts madical green(fRENCH)
 
SOFAVR_jesnault_19-09-11_presentation1
SOFAVR_jesnault_19-09-11_presentation1SOFAVR_jesnault_19-09-11_presentation1
SOFAVR_jesnault_19-09-11_presentation1
 
Celine Deswarte EU #MWC14 #mHealth
Celine Deswarte EU #MWC14 #mHealthCeline Deswarte EU #MWC14 #mHealth
Celine Deswarte EU #MWC14 #mHealth
 
M health 2.0 161111_maison_chimie
M health 2.0 161111_maison_chimieM health 2.0 161111_maison_chimie
M health 2.0 161111_maison_chimie
 
POLYCLINIQUE LES JASMINS - Batimaghreb Expo
POLYCLINIQUE LES JASMINS - Batimaghreb ExpoPOLYCLINIQUE LES JASMINS - Batimaghreb Expo
POLYCLINIQUE LES JASMINS - Batimaghreb Expo
 
Urgences au cabinet médical - trousse d'urgence
Urgences au cabinet médical - trousse d'urgenceUrgences au cabinet médical - trousse d'urgence
Urgences au cabinet médical - trousse d'urgence
 
Le cabinet médical
Le cabinet médicalLe cabinet médical
Le cabinet médical
 
Centre international carthage médical batimaghreb expo
Centre international carthage médical   batimaghreb expoCentre international carthage médical   batimaghreb expo
Centre international carthage médical batimaghreb expo
 
Samuel Gaillard, Logival pour la journée e-health 2013
Samuel Gaillard, Logival pour la journée e-health 2013Samuel Gaillard, Logival pour la journée e-health 2013
Samuel Gaillard, Logival pour la journée e-health 2013
 
Accelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
Accelerating Vector Graphics Rendering using the Graphics Hardware PipelineAccelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
Accelerating Vector Graphics Rendering using the Graphics Hardware Pipeline
 
Virtual reality x brands
Virtual reality x brandsVirtual reality x brands
Virtual reality x brands
 

Similaire à Cg in Two Pages

OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...ICS
 
Opengl lec 3
Opengl lec 3Opengl lec 3
Opengl lec 3elnaqah
 
Open cl programming using python syntax
Open cl programming using python syntaxOpen cl programming using python syntax
Open cl programming using python syntaxcsandit
 
OpenCL programming using Python syntax
OpenCL programming using Python syntax OpenCL programming using Python syntax
OpenCL programming using Python syntax cscpconf
 
CS 354 Programmable Shading
CS 354 Programmable ShadingCS 354 Programmable Shading
CS 354 Programmable ShadingMark Kilgard
 
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksBeginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksJinTaek Seo
 
Gdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glGdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glchangehee lee
 
고급컴파일러구성론_개레_230303.pptx
고급컴파일러구성론_개레_230303.pptx고급컴파일러구성론_개레_230303.pptx
고급컴파일러구성론_개레_230303.pptxssuser1e7611
 
openGL basics for sample program (1).ppt
openGL basics for sample program (1).pptopenGL basics for sample program (1).ppt
openGL basics for sample program (1).pptHIMANKMISHRA2
 
openGL basics for sample program.ppt
openGL basics for sample program.pptopenGL basics for sample program.ppt
openGL basics for sample program.pptHIMANKMISHRA2
 
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeksBeginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeksJinTaek Seo
 
426 lecture 4: AR Developer Tools
426 lecture 4: AR Developer Tools426 lecture 4: AR Developer Tools
426 lecture 4: AR Developer ToolsMark Billinghurst
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkDatabricks
 
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...Sourour Kanzari
 
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...Sourour Kanzari
 

Similaire à Cg in Two Pages (20)

Pixel shaders
Pixel shadersPixel shaders
Pixel shaders
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
Android native gl
Android native glAndroid native gl
Android native gl
 
Opengl lec 3
Opengl lec 3Opengl lec 3
Opengl lec 3
 
Open cl programming using python syntax
Open cl programming using python syntaxOpen cl programming using python syntax
Open cl programming using python syntax
 
OpenCL programming using Python syntax
OpenCL programming using Python syntax OpenCL programming using Python syntax
OpenCL programming using Python syntax
 
CS 354 Programmable Shading
CS 354 Programmable ShadingCS 354 Programmable Shading
CS 354 Programmable Shading
 
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksBeginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
 
Gdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_glGdc 14 bringing unreal engine 4 to open_gl
Gdc 14 bringing unreal engine 4 to open_gl
 
고급컴파일러구성론_개레_230303.pptx
고급컴파일러구성론_개레_230303.pptx고급컴파일러구성론_개레_230303.pptx
고급컴파일러구성론_개레_230303.pptx
 
Pixel shaders
Pixel shadersPixel shaders
Pixel shaders
 
openGL basics for sample program (1).ppt
openGL basics for sample program (1).pptopenGL basics for sample program (1).ppt
openGL basics for sample program (1).ppt
 
openGL basics for sample program.ppt
openGL basics for sample program.pptopenGL basics for sample program.ppt
openGL basics for sample program.ppt
 
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeksBeginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
Beginning direct3d gameprogramming03_programmingconventions_20160414_jintaeks
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Opengl basics
Opengl basicsOpengl basics
Opengl basics
 
426 lecture 4: AR Developer Tools
426 lecture 4: AR Developer Tools426 lecture 4: AR Developer Tools
426 lecture 4: AR Developer Tools
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache Spark
 
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
 
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
Andrade sep15 fromlowarchitecturalexpertiseuptohighthroughputnonbinaryldpcdec...
 

Plus de Mark Kilgard

D11: a high-performance, protocol-optional, transport-optional, window system...
D11: a high-performance, protocol-optional, transport-optional, window system...D11: a high-performance, protocol-optional, transport-optional, window system...
D11: a high-performance, protocol-optional, transport-optional, window system...Mark Kilgard
 
Computers, Graphics, Engineering, Math, and Video Games for High School Students
Computers, Graphics, Engineering, Math, and Video Games for High School StudentsComputers, Graphics, Engineering, Math, and Video Games for High School Students
Computers, Graphics, Engineering, Math, and Video Games for High School StudentsMark Kilgard
 
NVIDIA OpenGL and Vulkan Support for 2017
NVIDIA OpenGL and Vulkan Support for 2017NVIDIA OpenGL and Vulkan Support for 2017
NVIDIA OpenGL and Vulkan Support for 2017Mark Kilgard
 
NVIDIA OpenGL 4.6 in 2017
NVIDIA OpenGL 4.6 in 2017NVIDIA OpenGL 4.6 in 2017
NVIDIA OpenGL 4.6 in 2017Mark Kilgard
 
NVIDIA OpenGL in 2016
NVIDIA OpenGL in 2016NVIDIA OpenGL in 2016
NVIDIA OpenGL in 2016Mark Kilgard
 
Virtual Reality Features of NVIDIA GPUs
Virtual Reality Features of NVIDIA GPUsVirtual Reality Features of NVIDIA GPUs
Virtual Reality Features of NVIDIA GPUsMark Kilgard
 
EXT_window_rectangles
EXT_window_rectanglesEXT_window_rectangles
EXT_window_rectanglesMark Kilgard
 
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...Mark Kilgard
 
NV_path rendering Functional Improvements
NV_path rendering Functional ImprovementsNV_path rendering Functional Improvements
NV_path rendering Functional ImprovementsMark Kilgard
 
OpenGL 4.5 Update for NVIDIA GPUs
OpenGL 4.5 Update for NVIDIA GPUsOpenGL 4.5 Update for NVIDIA GPUs
OpenGL 4.5 Update for NVIDIA GPUsMark Kilgard
 
SIGGRAPH Asia 2012: GPU-accelerated Path Rendering
SIGGRAPH Asia 2012: GPU-accelerated Path RenderingSIGGRAPH Asia 2012: GPU-accelerated Path Rendering
SIGGRAPH Asia 2012: GPU-accelerated Path RenderingMark Kilgard
 
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondSIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondMark Kilgard
 
GPU accelerated path rendering fastforward
GPU accelerated path rendering fastforwardGPU accelerated path rendering fastforward
GPU accelerated path rendering fastforwardMark Kilgard
 
GPU-accelerated Path Rendering
GPU-accelerated Path RenderingGPU-accelerated Path Rendering
GPU-accelerated Path RenderingMark Kilgard
 
SIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
SIGGRAPH 2012: GPU-Accelerated 2D and Web RenderingSIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
SIGGRAPH 2012: GPU-Accelerated 2D and Web RenderingMark Kilgard
 
SIGGRAPH 2012: NVIDIA OpenGL for 2012
SIGGRAPH 2012: NVIDIA OpenGL for 2012SIGGRAPH 2012: NVIDIA OpenGL for 2012
SIGGRAPH 2012: NVIDIA OpenGL for 2012Mark Kilgard
 
GTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path RenderingGTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path Rendering Mark Kilgard
 
GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012Mark Kilgard
 
CS 354 Final Exam Review
CS 354 Final Exam ReviewCS 354 Final Exam Review
CS 354 Final Exam ReviewMark Kilgard
 

Plus de Mark Kilgard (20)

D11: a high-performance, protocol-optional, transport-optional, window system...
D11: a high-performance, protocol-optional, transport-optional, window system...D11: a high-performance, protocol-optional, transport-optional, window system...
D11: a high-performance, protocol-optional, transport-optional, window system...
 
Computers, Graphics, Engineering, Math, and Video Games for High School Students
Computers, Graphics, Engineering, Math, and Video Games for High School StudentsComputers, Graphics, Engineering, Math, and Video Games for High School Students
Computers, Graphics, Engineering, Math, and Video Games for High School Students
 
NVIDIA OpenGL and Vulkan Support for 2017
NVIDIA OpenGL and Vulkan Support for 2017NVIDIA OpenGL and Vulkan Support for 2017
NVIDIA OpenGL and Vulkan Support for 2017
 
NVIDIA OpenGL 4.6 in 2017
NVIDIA OpenGL 4.6 in 2017NVIDIA OpenGL 4.6 in 2017
NVIDIA OpenGL 4.6 in 2017
 
NVIDIA OpenGL in 2016
NVIDIA OpenGL in 2016NVIDIA OpenGL in 2016
NVIDIA OpenGL in 2016
 
Virtual Reality Features of NVIDIA GPUs
Virtual Reality Features of NVIDIA GPUsVirtual Reality Features of NVIDIA GPUs
Virtual Reality Features of NVIDIA GPUs
 
EXT_window_rectangles
EXT_window_rectanglesEXT_window_rectangles
EXT_window_rectangles
 
OpenGL for 2015
OpenGL for 2015OpenGL for 2015
OpenGL for 2015
 
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
Slides: Accelerating Vector Graphics Rendering using the Graphics Hardware Pi...
 
NV_path rendering Functional Improvements
NV_path rendering Functional ImprovementsNV_path rendering Functional Improvements
NV_path rendering Functional Improvements
 
OpenGL 4.5 Update for NVIDIA GPUs
OpenGL 4.5 Update for NVIDIA GPUsOpenGL 4.5 Update for NVIDIA GPUs
OpenGL 4.5 Update for NVIDIA GPUs
 
SIGGRAPH Asia 2012: GPU-accelerated Path Rendering
SIGGRAPH Asia 2012: GPU-accelerated Path RenderingSIGGRAPH Asia 2012: GPU-accelerated Path Rendering
SIGGRAPH Asia 2012: GPU-accelerated Path Rendering
 
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and BeyondSIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
SIGGRAPH Asia 2012 Exhibitor Talk: OpenGL 4.3 and Beyond
 
GPU accelerated path rendering fastforward
GPU accelerated path rendering fastforwardGPU accelerated path rendering fastforward
GPU accelerated path rendering fastforward
 
GPU-accelerated Path Rendering
GPU-accelerated Path RenderingGPU-accelerated Path Rendering
GPU-accelerated Path Rendering
 
SIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
SIGGRAPH 2012: GPU-Accelerated 2D and Web RenderingSIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
SIGGRAPH 2012: GPU-Accelerated 2D and Web Rendering
 
SIGGRAPH 2012: NVIDIA OpenGL for 2012
SIGGRAPH 2012: NVIDIA OpenGL for 2012SIGGRAPH 2012: NVIDIA OpenGL for 2012
SIGGRAPH 2012: NVIDIA OpenGL for 2012
 
GTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path RenderingGTC 2012: GPU-Accelerated Path Rendering
GTC 2012: GPU-Accelerated Path Rendering
 
GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012GTC 2012: NVIDIA OpenGL in 2012
GTC 2012: NVIDIA OpenGL in 2012
 
CS 354 Final Exam Review
CS 354 Final Exam ReviewCS 354 Final Exam Review
CS 354 Final Exam Review
 

Dernier

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 

Dernier (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 

Cg in Two Pages

  • 1. Cg in Two Pages Mark J. Kilgard NVIDIA Corporation Austin, Texas January 16, 2003 The out modifier indicates that clipPosition, oColor, 1. Cg by Example oDecalCoord, and oLightMapCoord parameters are output by the Cg is a language for programming GPUs. Cg programs look a lot program. The semantics that follow these parameters are therefore like C programs. Here is a Cg vertex program: output semantics. The respective semantics indicate the program void simpleTransform(float4 objectPosition : POSITION, outputs a transformed clip-space position and a scaled color. Also, float4 color : COLOR, two sets of texture coordinates are passed through. The resulting float4 decalCoord : TEXCOORD0, vertex is feed to primitive assembly to eventually generate a float4 lightMapCoord : TEXCOORD1, primitive for rasterization. out float4 clipPosition : POSITION, out float4 oColor : COLOR, Compiling the program requires the program source code, the out float4 oDecalCoord : TEXCOORD0, name of the entry function to compile (simpleTransform), and a out float4 oLightMapCoord : TEXCOORD1, profile name (vs_1_1). uniform float brightness, The Cg compiler can then compile the above Cg program into the uniform float4x4 modelViewProjection) following DirectX 8 vertex shader: { vs.1.1 clipPosition = mul(modelViewProjection, objectPosition); mov oT0, v7 oColor = brightness * color; mov oT1, v8 oDecalCoord = decalCoord; dp4 oPos.x, c1, v0 oLightMapCoord = lightMapCoord; } dp4 oPos.y, c2, v0 dp4 oPos.z, c3, v0 1.1 Vertex Program Explanation dp4 oPos.w, c4, v0 The program transforms an object-space position for a vertex by a mul oD0, c0.x, v5 4x4 matrix containing the concatenation of the modeling, viewing, The profile indicates for what API execution environment the and projection transforms. The resulting vector is output as the program should be compiled. This same program can be compiled clip-space position of the vertex. The per-vertex color is scaled by for the DirectX 9 vertex shader profile (vs_2_0), the multi-vendor a floating-point parameter prior to output. Also, two texture OpenGL vertex program extension (arbvp1), or NVIDIA- coordinate sets are passed through unperturbed. proprietary OpenGL extensions (vp20 & vp30). Cg supports scalar data types such as float but also has first-class The process of compiling Cg programs can take place during the support for vector data types. float4 represents a vector of four initialization of your application using Cg. The Cg runtime floats. float4x4 represents a matrix. mul is a standard library contains the Cg compiler as well as API-dependent routines that routine that performs matrix by vector multiplication. Cg provides greatly simplify the process of configuring your compiled program function overloading like C++; mul is an overloaded function so it for use with either OpenGL or Direct3D. can be used to multiply all combinations of vectors and matrices. 1.2 Fragment Program Explanation Cg provides the same operators as C. Unlike C however, Cg In addition to writing programs to process vertices, you can write operators accept and return vectors as well as scalars. For example, programs to process fragments. Here is a Cg fragment program: the scalar, brightness, scales the vector, color, as you would expect. float4 brightLightMapDecal(float4 color : COLOR, float4 decalCoord : TEXCOORD0, In Cg, declaring a parameter with the uniform modifier indicates float4 lightMapCoord : TEXCOORD1, that its value is initialized by an external source that will not vary uniform sampler2D decal, over a given batch of vertices. In this respect, the uniform modifier uniform sampler2D lightMap) : COLOR in Cg is different from the uniform modifier in RenderMan but { used in similar contexts. In practice, the external source is some float4 d = tex2Dproj(decal, decalCoord); OpenGL or Direct3D state that your application takes care to load float4 lm = tex2Dproj(lightMap, lightMapCoord); appropriately. For example, your application must supply the return 2.0 * color * d * lm; modelViewProjection matrix and the brightness scalar. The Cg } runtime library provides an API for loading your application state The input parameters correspond to the interpolated color and two into the appropriate API state required by the compiled program. texture coordinate sets as designated by their input semantics. The POSITION, COLOR, TEXCOORD0, and TEXCOORD1 identifiers The sampler2D type corresponds to a 2D texture unit. The Cg following the objectPosition, color, decalCoord, and standard library routine tex2Dproj performs a projective 2D lightMapCoord parameters are input semantics. They indicate how texture lookup. The two tex2Dproj calls sample a decal and light their parameters are initialized by per-vertex varying data. In map texture and assign the result to the local variables, d and lm, OpenGL, glVertex commands feed the POSITION input semantic; respectively. glColor commands feed the COLOR semantic; glMultiTexCoord commands feed the TEXCOORDn semantics. 1
  • 2. The program multiplies the two textures results, the interpolated vec1.xw = vec3; // vec1 = (3.0, -2.0, 5.0, 3.0) color, and the constant 2.0 together and returns this RGBA color. Use either .xyzw or .rgba suffixes swizzling and write masking. The program returns a float4 and the semantic for the return value The Cg standard library includes a large set of built-in functions is COLOR, the final color of the fragment. for mathematics (abs, dot, log2, reflect, rsqrt, etc.) and texture The Cg compiler generates the following code for access (texCUBE, tex3Dproj, etc.). The standard library makes brightLightMapDecal when compiled with the arbfp1 multi- extensive use of function overloading (similar to C++) to support vendor OpenGL fragment profile: different vector lengths and data types. There is no need to use !!ARBfp1.0 #include to obtain prototypes for standard library routines as in C; PARAM c0 = {2, 2, 2, 2}; TEMP R0; TEMP R1; TEMP R2; Cg standard library routines are automatically prototyped. TXP R0, fragment.texcoord[0], texture[0], 2D; In addition to the out modifier for call-by-result parameter TXP R1, fragment.texcoord[1], texture[1], 2D; passing, the inout modifier treats a parameter as both a call-by- MUL R2, c0.x, fragment.color.primary; value input parameter and a call-by-result output parameter. MUL R0, R2, R0; MUL result.color, R0, R1; The discard keyword is similar to return but aborts the END processing without returning a transformed fragment. This same program also compiles for the DirectX 8 and 9 profiles (ps_1_3 & ps_2_x) and NVIDIA-proprietary OpenGL extensions 2.3 Features Not Supported Cg has no support currently for pointers or bitwise operations (fp20 & fp30). (however, the necessary C operators and keywords are reserved for 2. Other Cg Functionality this purpose). Cg does not (currently) support unions and function variables. 2.1 Features from C Cg lacks C++ features for “programming in the large” such as Cg provides structures and arrays, including multi-dimensional classes, templates, operator overloading, exception handling, and arrays. Cg provides all of C’s arithmetic operators (+, *, /, etc.). namespaces. Cg provides a boolean type and boolean and relational operators (||, &&, !, etc.). Cg provides increment/decrement (++/--) operators, The Cg standard library lacks routines for functionality such as the conditional expression operator (?:), assignment expressions string processing, file input/output, and memory allocation, which (+=, etc.), and even the C comma operator. is beyond the specialized scope of Cg. Cg provides user-defined functions (in addition to pre-defined However, Cg reserves all C and C++ keywords so that features standard library functions), but recursive functions are not allowed. from these languages could be incorporated into future Cg provides a subset of C’s control flow constructs (do, while, implementations of Cg as warranted. for, if, break, continue); other constructs such as goto and switch are not supported in current the current Cg implementation 3. Profile Dependencies but the necessary keywords are reserved. When you compile a C or C++ program, you expect it to compile without regard to how big (within reason) the program is or what Like C, Cg does not mandate the precision and range of its data the program does. With Cg, a syntactically and semantically types. In practice, the profile chosen for compilation determines correct program may still not compile due to limitations of the the concrete representation for each data type. float, half, and profile for which you are compiling the program. double are meant to represent continuous values, ideally in For example, it is currently an error to access a texture when floating-point, but this can depend on the profile. half is intended compiling with a vertex profile. Future vertex profiles may well for a 16-bit half-precision floating-point data type. (NVIDIA’s allow texture accesses, but existing vertex profiles do not. Other CineFX architecture provides such a data type.) int is an integer errors are more inherent. For example, a fragment profile should data type, usually used for looping and indexing. fixed is an not output a parameter with a TEXCOORD0 semantic. Other errors additional data type intended to represent a fixed-point continuous may be due to exceeding a capacity limit of current GPUs such as data type that may not be floating-point. the maximum number of instructions or the number of texture units Cg provides #include, #define, #ifdef, etc. matching the C available. preprocessor. Cg supports C and C++ comments. Understand that these profile dependent errors do not reflect 2.2 Additional Features Not in C limitations of the Cg language, but rather limitations of the current Cg provides built-in constructors (similar to C++ but not user- implementation of Cg or the underlying hardware limitations of defined) for vector data types: your target GPU. float4 vec1 = float4(4.0, -2.0, 5.0, 3.0); 4. Compatibility and Portability Swizzling is a way of rearranging components of vector values and NVIDIA's Cg implementation and Microsoft's High Level Shader constructing shorter or longer vectors. Example: Language (HLSL) are very similar as they were co-developed. float2 vec2 = vec1.yx; // vec2 = (-2.0, 4.0) HLSL is integrated with DirectX 9 and the Windows operating float scalar = vec1.w; // scalar = 3.0 system. Cg provides support for multiple APIs (OpenGL, Direct X float3 vec3 = scalar.xxx; // vec3 = (3.0, 3.0, 3.0) 8, and Direct X 9) and multiple operating systems (Windows, More complicated swizzling syntax is available for matrices. Linux, and Mac OS X). Because Cg interfaces to multi-vendor Vector and matrix elements can also be accessed with standard APIs, Cg runs on GPUs from multiple vendors. array indexing syntax as well. 5. More Information Write masking restricts vector assignments to indicated In March 2003, look for The Cg Tutorial: The Definitive Guide to components. Example: Programmable Real-Time Graphics (ISBN 0321194969) published by Addison-Wesley. 2