150 likes | 411 Vues
Cg – C for Graphics. Eric Vidal CS 280. History. General graphics processing languages Renderman shading language (1988) Assembly languages for graphics card processing DirectX 8/9 vertex shaders and pixel shaders (2000, 2002) OpenGL ARB_vertex_program/ARB_fragment_program (2002)
E N D
Cg – C for Graphics Eric Vidal CS 280
History • General graphics processing languages • Renderman shading language (1988) • Assembly languages for graphics card processing • DirectX 8/9 vertex shaders and pixel shaders (2000, 2002) • OpenGL ARB_vertex_program/ARB_fragment_program (2002) • High-level languages for graphics card processing • DirectX High Level Shading Language (HLSL) (2002) • OpenGL Shading Language (glslang) (2003) • Cg – C for Graphics (2002)
History • Cg – C for Graphics • Developed in 2002 by NVIDIA Corporation in close collaboration with Microsoft • Supports OpenGL and DirectX (does not replace them) • Supported in Windows, Mac OS X and Linux • Proprietary (but language specification is open) • Cg compiler technology is open-source
Rationale • Do graphics-related processing (“shading”) in graphics card • Offloads work from the main processor • Higher performance • Cg is a high-level language • Easier to understand than graphics card “assembly language” (DirectX vertex/pixel shaders, OpenGL vertex/fragment programs) • Code portability across graphics cards
Features • Two types of Cg programs: • Vertex programs • Fragment programs • On-the-fly compilation supports several target graphics assembly language formats: • DirectX 8 vertex/pixel shaders (vs_1_1, ps_1_3) • DirectX 9 vertex/pixel shaders (vs_2_0, ps_2_x) • OpenGL vertex/fragment program extensions (arbvp1, arbfp1) • NVIDIA proprietary OpenGL extensions (vp20, fp20, vp30, fp30)
Features • First class support for vector data types • float4 – vector of four floats • float4x4 – square matrix with 16 elements • Same operators as C • Works with vectors as well as with scalars float scalar = 4.0; float4 vector = float4(1.0, 2.0, -1.0, 3.0), result; /* this properly scales “vector” by “scalar” */ result = scalar * vector;
Features • uniform parameters • input semantics (for non-uniform parameters) • out, inout parameters void simpleTransform(float4 objectPosition : POSITION, float4 color : COLOR, float4 decalCoord : TEXCOORD0, float4 lightMapCoord : TEXCOORD1, out float4 clipPosition : POSITION, out float4 oColor : COLOR, out float4 oDecalCoord : TEXCOORD0, out float4 oLightMapCoord : TEXCOORD1, uniform float brightness, uniform float4x4 modelViewProjection)
Features • Structures and arrays (including multidimensional arrays) • All of C’s arithmetic operators • boolean type • Boolean/relational operators (||, &&, !, etc.) • Increment/decrement operators (++, --) • Conditional expressions (?, :) • Assignment expressions (=, +=, etc.)
Features • User-defined functions • But no recursive functions! • Control flow constructs • do, while, for, if, break, continue • No switch and goto! • Data types • float, half, double • int • fixed
Features • #include, #define, #ifdef • C and C++-style comments • Swizzling and write-masking float4 vec1 = float4(1.0, 2.0, 3.0, 4.0); float2 vec2 = vec1.yx; // vec2 = (2.0, 1.0); float scalar = vec1.w; // scalar = 4.0; float3 vec3 = scalar.xxx; // vec3 = (4.0, 4.0, 4.0); vec1.xw = vec3; // vec1 = (4.0, 2.0, 3.0, 4.0); • discard keyword (similar to return, but no return value)
Features • Built-in library functions for graphics math • abs() • log2() • mul() • dot() • normalize() • lerp() • specular() • reflect() • ... and more! (No need to #include)
(Non-)Features • Pointers • Bitwise operations • Unions • Function variables • String processing • File I/O • Memory allocation/deallocation • C++ classes, templates, overloading, exceptions
Applications Using Cg • Unreal engine (Epic Games, Inc.) • Aurora engine (Bioware Inc.) • Storm engine (Blizzard Entertainment) • Other game companies (Electronic Arts, Ensemble Studios, Ion Storm, LithTech, Maxis, Microsoft Games) • Third party content creation tools • Alias Wavefront Maya 4.5 • Discreet 3ds max 5 • SOFTIMAGE|XSI v.3.0
Where to Get Cg • Main Cg Website: • http://developer.nvidia.com/page/cg_main.html • Cg Language Specification: • http://developer.nvidia.com/object/cg_specification.html • Cg Compiler and Toolkit: • http://developer.nvidia.com/object/cg_toolkit.html