Cg Overview Cg is the High Level language


























- Slides: 26


Cg Overview Cg is the High Level language from NVIDIA for programming GPUs, developed in close collaboration with Microsoft Cg is 100% compatible with the HLSL in Direct. X 9 Cg has been offered to the Open. GL ARB as a proposal for the High Level Shading Language for future versions of Open. GL Cg enables a dramatic productivity increase for graphics development for: Game developers DCC tool developers Artists & Shader writers CAD and Visualization application developers “This is C for Graphics”

Assembly or High-level…. Which do you prefer? Phong Shader Assembly … DP 3 R 0, c[11]. xyzx; RSQ R 0, R 0. x; MUL R 0, R 0. x, c[11]. xyzx; MOV R 1, c[3]; MUL R 1, R 1. x, c[0]. xyzx; DP 3 R 2, R 1. xyzx; RSQ R 2, R 2. x; MUL R 1, R 2. x, R 1. xyzx; ADD R 2, R 0. xyzx, R 1. xyzx; DP 3 R 3, R 2. xyzx; RSQ R 3, R 3. x; MUL R 2, R 3. x, R 2. xyzx; DP 3 R 2, R 1. xyzx, R 2. xyzx; MAX R 2, c[3]. z, R 2. x; MOV R 2. z, c[3]. y; MOV R 2. w, c[3]. y; LIT R 2, R 2; . . . or Cg COLOR c. Plastic = Ca + Cd * dot(Nf, L) + Cs * pow(max(0, dot(Nf, H)), phong. Exp);

NVIDIA’s Cg Compiler Industry standard high level language from NVIDIA and Microsoft Developers can choose either API as a target Direct. X 8 & 9 : access millions of installed GPUs Open. GL: (Windows, Mac, Linux) Unified Compiler Architecture: One language Several optimizations

Cg is a C-like language, modified for GPUs Syntax, operators, functions from C Conditionals and flow control Particularly suitable for GPUs: Expresses data flow of the pipeline/stream architecture of GPUs (e. g. vertex-to-pixel) Vector and matrix operations Supports hardware data types for maximum performance Exposes GPU functions for convenience and speed: Intrinsic: (mul, dot, sqrt…) Built-in: extremely useful and GPU optimized math, utility and geometric functions (noise, mix, reflect, sin…) Language reserves keywords to support future hardware implementations (e. g. , pointers, switch, case…) Compiler uses hardware profiles to subset Cg as required for particular hardware capabilities (or lack thereof)

Cg and Cg. FX One Cg vertex & pixel program together describe a single rendering pass Cg. FX shaders can describe multiple passes Although Cine. FX architecture supports 1024 pixel instructions in a single pass! Cg. FX also contains multiple implementations For different APIs For various HW For Shader LOD

Cg. FX Overview Cg. FX files contain shaders and the supplementary data required to use them Unlimited multiple implementations API (D 3 D vs. Open. GL) Platform (Xbox, PC, …) Shader Level of Detail NVIDIA’s Compiler includes a Cg. FX parser for easy integration Full DCC Integration

Key DCC Applications integrating Cg

NVIDIA’s Cg Toolkit Beta 2 Available now at developer. nvidia. com/Cg Includes Cine. FX support Cg Compiler Vertex (ARB_vertex_program, Direct. X 8, Open. GL, Cine. FX) Pixel (Direct. X 8, Cine. FX) Robust built-in Library Cg Browser 4. 0 & Shaders Cine. FX sample shaders Cg User’s Manual Publicly released in August Leather … float spec. Roll. Off. Edge = pow( (1. 0 - Vd. H), 3. 0); float roll. Off = spec. Roll. Off. Edge + (1. 0 - spec. Roll. Off. Edge) * specular. Roll. Off; color C = Light. Color * Ecc * Gain * roll. Off; …

NVIDIA’s Cg Toolkit Final Release Will be available this fall Cg Compiler Vertex (Direct. X 9, Cine. FX support) Pixel (Direct. X 9, Cine. FX support) Cg. FX Parsing Cg Browser 5. 0 (Cg. FX enabled) More Shaders Max Cg. FX plugin Maya Cg. FX plugin XSI Cg support Cg Courseware

And now for the details. . .

Data types float half = 32 -bit IEEE floating point = 16 -bit IEEE-like floating point fixed = 12 -bit fixed [-2, 2) clamping (Open. GL only) bool = Boolean sampler* = Handle to a texture sampler

Array / vector / matrix declarations Declare vectors (up to length 4) and matrices (up to size 4 x 4) using built-in data types: float 4 mycolor; float 3 x 3 mymatrix; Not the same as arrays : float mycolor[4]; float mymatrix[3][3]; Arrays are first-class types, not pointers

Can define overloaded functions Examples: float myfunc. A(float 3 x); float myfunc. A(half 3 x); float myfunc. B(float 2 a, float 2 b); float myfunc. B(float 3 a, float 3 b); float myfunc. B(float 4 a, float 4 b); Very useful with so many data types.

Extend standard arithmetic to vectors and matrices Component-wise + - * / > < == ? : for vectors Dot product dot(v 1, v 2); // returns a scalar Matrix multiplications: assuming float 4 x 4 M and float 4 v matrix-vector: mul(M, v); // returns a vector-matrix: mul(v, M); // returns a vector matrix-matrix: mul(M, N); // returns a matrix

New vector operators Swizzle operator extracts elements from vector a = b. xxyy; Vector constructor builds vector a = float 4(1. 0, 0. 0, 1. 0); Masking operator specifies which elements are overwritten a. wx = b. xy;

The Big Picture Application Vertex Program Connector Fragment Program Connector Cg or asm Framebuffer Connector Cg or asm

What exactly is a connector? Describes shader inputs and outputs Defines an interface that allows mixing-andmatching of programs It’s a struct, but with optional extra information

Connecting Application to Vertex Program name of connector (struct tag) struct myappdata { float 4 pos; float 4 color; float 2 uv; float w 1; float w 2; }; // skinning weight #1 // skinning weight #2 Connectors define an interface that allows mixing-and-matching of programs.

Defining a Vertex Program output connector Input connector v 2 f skinning(myappdata vin, uniform float 4 x 4 m 1, uniform float 4 x 4 m 2) { v 2 f vout; // skinning vout. pos = vin. w 1*(mul(m 1, vin. pos)) + vin. w 2*(mul(m 2, vin. pos)); vout. color = vin. color; vout. uv = vin. uv; return vout; } Note that “uniform” variables don’t come in via a connector.

Connecting Vertex Program to Fragment Program name of connector struct v 2 f { float 4 pos : POSITION; float 4 color; float 2 uv; }; Must have POSITION

Defining a Fragment Program predefined output connector input connector f 2 fb do_texture(v 2 f fragin, uniform sampler 2 D tex) { f 2 fb fragout; fragout. COL = fragin. color * f 4 tex 2 D(tex, fragin. uv); return fragout; }

Optional: Specify Connector Registers struct v 2 f { float 4 pos : POSITION; float 4 color : TEXCOORD 0; float 2 uv : TEXCOORD 1; }; These semantics allow mix & match with manually-written assembly code.

“Profiles” Define Specific HW Behavior Vertex profiles: No “half” or “fixed” data type No texture-mapping functions Fragment/pixel profiles: No “for” or “while” loops (unless they’re unrollable)

Cg Summary C-like language – expressive and efficient HW data types Vector and matrix operations Write separate vertex and fragment programs Connectors enable mix & match of programs by defining data flows Supports any DX 8 hardware Will support future HW (beyond Cine. FX/DX 9)

Questions?