160 likes | 285 Vues
Cg Programming. Mikkel Adamsen Anders Vaaben Andersen Jacob Atzen Oliver Due Billing Peter Bruun-Rasmussen Micky Kelager Christensen Anders Fleron Dennis Franck Sune Gamsby Frederik Gottlieb. Adam Hasselbalch Hansen Anders Starcke Henriksen Joakim Hovard Stig Frank Irming-Pedersen
E N D
Cg Programming Mikkel Adamsen Anders Vaaben Andersen Jacob Atzen Oliver Due Billing Peter Bruun-Rasmussen Micky Kelager Christensen Anders Fleron Dennis Franck Sune Gamsby Frederik Gottlieb Adam Hasselbalch Hansen Anders Starcke Henriksen Joakim Hovard Stig Frank Irming-Pedersen Sune Gustav Nielsen Martin Lehnsbo Parm Emil Støvring Thomas Szymanski Erik Larsen Underbjerg Participants:
Plan for today • Administrative details • Overview of GPU pipeline • Programming model • Cg language overview • Examples • Intro to Image Lab
Seminar Details • Groups of 3. • 3 seminars each to pass. • Aim at 30 minutes presentation • Theory. • Implementation details. • Demonstration. • Schema at imagelab to assign presentation time and subject.
Available subjects • Natural Effects: • Water, skin, animation, noise, fire, grass, diffraction. • Lighting and shadows: • Lots of different shadowing techniques. • Materials: • Subsurface scattering, ambient occlusion, spatial BRDFs Image-based lighting, texture bombing. • Image Processing: • Glow, color, dof, filtering, hdr, ip. • Performance: • Occlusion culling, Renderman to realtime. • Beyond Triangles: • Computations on GPU, Fluid Dynamics, Stereograms, Volume Rendering, 3d ultrasound, Deformers.
GPU Pipeline Historical data: • Pre-GPU Graphics Acceleration • SGI, Evans & Sutherland. Introduced concepts like vertex transformation and texture mapping. Very expensive! • First-Generation GPU (-1998) • Nvidia TNT2, ATI Rage, Voodoo3. Vertex transformation on CPU, limited set of math operations. • Second-Generation GPU (1999-2000) • GeForce 256, Geforce2, Radeon 7500, Savage3D. Transformation & Lighting. More configurable, still not programmable. • Third-Generation GPU (2001) • Geforce3, Geforce4 Ti, Xbox, Radeon 8500. Vertex Programmability, pixel-level configurability. • Fourth-Generation GPU (2002-) • Geforce FX series, Radeon 9700 and on. Vertex-level and pixel-level programmability. CPU GPU Graphics State Application Transform Rasterizer Shade VideoMemory(Textures) Vertices(3D) Xformed,LitVertices(2D) Fragments(pre-pixels) Finalpixels(Color, Depth) Render-to-texture
GPU Pipeline: Transform • Vertex Processor (multiple operate in parallel) • Transform from “world space” to “image space” • Compute per-vertex lighting
GPU Pipeline: Rasterizer • Rasterizer • Convert geometric rep. (vertex) to image rep. (fragment) • Fragment = image fragment • Pixel + associated data: color, depth, stencil, etc. • Interpolate per-vertex quantities across pixels
GPU Pipeline: Shader • Fragment Processors (multiple in parallel) • Compute a color for each pixel • Optionally read colors from textures (images)
Switch to PDF • Cg language introduction coming up...
Datatypes • float 32-bit IEEE floating point • half 16-bit IEEE-like floating point • fixed 12-bit fixed [-2,2) clamping • bool Boolean • sampler Handle to a texture sampler • struct Structure as in C/C++ • No pointers... Yet.
Different kinds of variables • Uniform • same for each vertex (in a vertex program) • same for each fragment (in a fragment program) • examples: reflectivity, light, color • Varying • different for each vertex (in a vertex program) • different for each fragment (in a fragment program) • examples: position, normal, texture coordinates • Local • used for intermediate computations within a vertex or fragment program
Example } void introShaderFP(float2 texCoord : TEXCOORD0, float3 R : TEXCOORD1, float3 diffuse : TEXCOORD2, float3 specular : TEXCOORD3, out float3 color : COLOR, uniform float reflectivity, uniform sampler2D decalMap, uniform samplerCUBE environmentMap) { float3 lighting; lighting = diffuse + specular; color = lighting; } varying parameters } uniform parameters } local variable
Array/Vector/Matrix declarations • Native support for vectors (up to length 4) and matrices (up to size 4x4): float4 mycolor; float3x3 mymatrix; • Declare more general array exactly as in C: float lightpower[8]; • But, arrays are first-class types, not pointers: float v[4] != float4 v
Function Overloading • Examples: float myFuncA(float3 x); float myFuncA(half3 x); float myFuncA(float2 a, float2 b); float myFuncA(float3 a, float3 b); float myFuncA(float4 a, float4 b); Very useful with so many data types.
Change to Constant-Typing Rules • In C, it’s easy to accidently use high precision half x, y; x = y * 2.0; // multiply is at // float precision! • Not in Cg x = y * 2.0; // multiply is at half // precision (from y) • Unless you want to x = y * 2.0f; // multiply is at // float precision