Cg: C for Graphics Jon Moon Based on slides by Eugene Lee.

Slides:



Advertisements
Similar presentations
Perspective aperture ygyg yryr n zgzg y s = y g (n/z g ) ysys y s = y r (n/z r ) zrzr.
Advertisements

GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Graphics Pipeline.
CS-378: Game Technology Lecture #9: More Mapping Prof. Okan Arikan University of Texas, Austin Thanks to James O’Brien, Steve Chenney, Zoran Popovic, Jessica.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
The Programmable Graphics Hardware Pipeline Doug James Asst. Professor CS & Robotics.
Introduction to Shader Programming
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
The programmable pipeline Lecture 10 Slide Courtesy to Dr. Suresh Venkatasubramanian.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
Vertex & Pixel Shaders CPS124 – Computer Graphics Ferdinand Schober.
Cg Overview Cg is the High Level language from NVIDIA for programming GPUs, developed in close collaboration with Microsoft Cg is 100% compatible with.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
GPU Graphics Processing Unit. Graphics Pipeline Scene Transformations Lighting & Shading ViewingTransformations Rasterization GPUs evolved as hardware.
Cg Kevin Bjorke GDC NVIDIA CONFIDENTIAL A Whole New World with Cg Graphics Program Written in Cg “C” for Graphics Compiled & Optimized Low Level,
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.
Cg – C for Graphics Eric Vidal CS 280. History General graphics processing languages – Renderman shading language (1988) Assembly languages for graphics.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
Week 3 - Friday.  What did we talk about last time?  Vertex shaders  Geometry shaders  Pixel shaders.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Real-time Graphical Shader Programming with Cg (HLSL)
Geometric Objects and Transformations. Coordinate systems rial.html.
GPU Shading and Rendering Shading Technology 8:30 Introduction (:30–Olano) 9:00 Direct3D 10 (:45–Blythe) Languages, Systems and Demos 10:30 RapidMind.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
A Crash Course in HLSL Matt Christian.
Real-Time Shading Using Programmable Graphics Hardware Introduction, Setup and Examples Wan-Chun Ma National Taiwan University.
The programmable pipeline Lecture 3.
CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University.
GAM532 DPS932 – Week 2 Vertex Shaders. The Shader Pipeline Vertex Processing Primitive Assembly / Processing Rasterization Fragment Process Pixel Output.
GRAPHICS PIPELINE & SHADERS SET09115 Intro to Graphics Programming.
CS662 Computer Graphics Game Technologies Jim X. Chen, Ph.D. Computer Science Department George Mason University.
Programmable Pipelines Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University.
The Cg Runtime Cyril Zeller. Cg Pipeline Graphics programs are written in Cg and compiled to low-level assembly code... Cg Runtime API...
OpenGL Shader Language Vertex and Fragment Shading Programs.
David Luebke 1 11/24/2015 Programmable Graphics Hardware.
OpenGL Shading Language (GLSL)
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
OpenGL-ES 3.0 And Beyond Boston Photo credit :Johnson Cameraface OpenGL Basics.
OpenGL Shading Language (GLSL)
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
09/25/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Shadows Stage 2 outline.
Ray Tracing using Programmable Graphics Hardware
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
Background image by chromosphere.deviantart.com Fella in following slides by devart.deviantart.com DM2336 Programming hardware shaders Dioselin Gonzalez.
OpenGL Shading Language
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
OpenGl Shaders Lighthouse3d.com.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
GLSL Review Monday, Nov OpenGL pipeline Command Stream Vertex Processing Geometry processing Rasterization Fragment processing Fragment Ops/Blending.
Computer Science – Game DesignUC Santa Cruz Tile Engine.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Shader.
Chapter 6 GPU, Shaders, and Shading Languages
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Introduction to Computer Graphics with WebGL
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Lecture 13 Clipping & Scan Conversion
Computer Graphics Practical Lesson 10
Programming with OpenGL Part 3: Shaders
Computer Graphics Introduction to Shaders
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
CS 480/680 Computer Graphics GLSL Overview.
Language Definitions Chap. 3 of Orange Book.
CS 480/680 Part 1: Model Loading.
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

Cg: C for Graphics Jon Moon Based on slides by Eugene Lee

Overview  Cg Basics  Vertex Shaders  Fragment Shaders  Using Cg in OpenGL

What is Cg?  Cg is a high level shader language Made by NVIDIA, but supports other GPUs Programmer writes in a language similar to C Cg compiler produces hardware-specific optimized assembly code

Halflife 1 vs. Halflife 2

index.html

Overview  Motivation  Vertex Shaders  Fragment Shaders  Using Cg in OpenGL  Questions?

The Vertex Shader  Program executed once per vertex  Takes per-vertex input such as position, normal, color, light position, etc.  Determines per-vertex data such as position, color, depth, etc. But can do more!

The Vertex Shader

A Vertex Program struct VertexInput { float4 position : POSITION; float3 normal : NORMAL; } void NormalShade( VertexInput input, out float4 outPosition : POSITION, out float3 outColor : COLOR, const uniform float4x4 modelViewProjMatrix) { outPosition = mul(modelViewProjMatrix, input.position); outColor = abs(input.normal); }

Binding Semantics  Predefined “ names ”  POSITION, NORMAL, etc. struct VertexInput { float4 position : POSITION; float3 normal : NORMAL; } void NormalShade(VertexInput input, out float4 outPosition : POSITION, out float3 outColor : COLOR, const uniform float4x4 modelViewProjMatrix) { outPosition = mul(modelViewProjMatrix, input.position); outColor = abs(input.normal); }

Vertex Shader: Input  Varying parameters Color, normal, texture coordinates, etc. Data specified for each element glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glNormal3f(0.0f, 1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 1.0f); //... glEnd(); void Shade(float3 color : COLOR, float3 normal : NORMAL, float3 position : POSITION,...) { //... } OpenGLCG Vertex Program

Vertex Shader: Input  Uniform Parameters Data that remains constant over each element Parameter shadowing cgSetParameter3f(kdParam, 0.3f, 0.2f, 0.6f) glBegin(GL_TRIANGLES); //... glEnd(); void Shade(..., const uniform float3 kd) { //... } OpenGLCG Vertex Program

Vertex Shader: Output  Output serves as varying input to fragments  “ Custom data ” usually output as texcoords struct VertexInput { float4 position : POSITION; float3 normal : NORMAL; } void NormalShade(VertexInput input, out float4 outPosition : POSITION, out float3 outColor : COLOR, const uniform float4x4 modelViewProjMatrix) { outPosition = mul(modelViewProjMatrix, input.position); outColor = abs(input.normal); }

Types in Cg  Syntax similar to C  Basic types int, float, half, fixed, bool  Vector/Matrix types int4, float3, bool2, float4x4  Arrays int a[3], float4x4 matrices[4][4]

Playing with types  Vector initialization and swizzling float4 vec4 = float4(1, 1, 1, 0); float3 vec3 = vec4.yzw; float3 color = vec3.rgb; float3 position = 1.xxx;

Overview  Motivation  Vertex Shaders  Fragment Shaders  Using Cg in OpenGL  Questions?

The Fragment Shader  Program executed once per rasterized pixel.  Takes varying output from vertex program. Certain outputs, like TEXCOORD0 interpolated.  Determines final color, depth for pixel.

The Fragment Shader

A Fragment Program float3 SinWave(float3 tex : TEXCOORD0) : COLOR { return sin(tex.x); }

A Fragment Program void PerPixelLight(float3 position : TEXCOORD0, float3 normal : TEXCOORD1, const uniform float3 eye, const uniform float3 light) { float3 L = normalize(light – position); float3 V = normalize(eye – position); outColor = computeRd(L, N) + computeRs(L, V, N); }

Performance  Fragment Programs are Expensive! Try to do as much as possible in vertex programs. Try to keep fragment programs small.  Note: not all hardware supports fragments Rhodes 453 has Quadro FX GPU ’ s, which DO support fragment programs

Overview  Motivation  Vertex Shaders  Fragment Shaders  Using Cg in OpenGL  Questions?

Initializing Cg  Create a context CGcontext cgContext = CgGL.cgCreateContext();  Select and enable Profile int cgVertexProfile = CgGL.CG_PROFILE_VP20; CgGL.cgGLEnableProfile(cgVertexProfile);  Create & Load program CGprogram cgVertexProgram = CgGL.cgCreateProgramFromFile(cgContext, CgGL.CG_SOURCE, "Normal.cg", cgVertexProfile, "Normal", null); CgGL.cgGLLoadProgram(cgVertexProgram1);

Executing Cg  Get handles for parameters CGparameter mvpMatrix = CgGL.cgGetNamedParameter(cgVertexProgram, “ ModelViewProjMatrix ” );  Set parameters CgGL.cgGLSetStateMatrixParameter( mvpMatrix, CgGL.CG_GL_MODELVIEW_PROJECTION_MATRIX, CgGL.CG_GL_MATRIX_IDENTITY)  Bind program CgGL.CgGLBindProgram(cgVertexProgram);  Rendering glBegin() …

Cg in Action: Normal Shader  First demo: using 1 vertex shader to shade a cube based on its normal  Based on OpenGLDemo02 from last week  First look at Normal.cg, then at the Java code to actually use it

Normal.cg struct VertexInput { float4 position: POSITION; float3 normal: NORMAL; }; struct VertexOutput { float4 position: POSITION; float3 color: COLOR0; }; VertexOutput Normal(const VertexInput input, const uniform float4x4 ModelViewProjMatrix) { VertexOutput output; output.position = mul(ModelViewProjMatrix, input.position); output.color = (input.normal+1)/2; return output; }

Vertex.java: declarations protected CGcontext cgContext; protected int cgVertexProfile; protected static CGprogram cgVertexProgram; protected static CGparameter cgModelViewProjMatrix;

Vertex.java: initializations cgContext = CgGL.cgCreateContext(); cgVertexProfile = CgGL.CG_PROFILE_VP20; CgGL.cgGLEnableProfile( cgVertexProfile); cgVertexProgram = CgGL.cgCreateProgramFromFile( cgContext, CgGL.CG_SOURCE, "Normal.cg", cgVertexProfile, "Normal", null); CgGL.cgGLLoadProgram(cgVertexProgram); cgModelViewProjMatrix = CgGL.cgGetNamedParameter( cgVertexProgram, "ModelViewProjMatrix");

Vertex.java: Display loop CgGL.cgGLBindProgram(cgVertexProgram); CgGL.cgGLSetStateMatrixParameter( cgModelViewProjMatrix, CgGL.CG_GL_MODELVIEW_PROJECTION_MATRIX, CgGL.CG_GL_MATRIX_IDENTITY);

More than 1 Vertex Program  Can use different vertex (fragment) programs for different geometry!  Initialize programs as you would normally 1 context, 1 vertex profile, many programs, many loads, etc.  In display loop, bind appropriate VP before gl_Begin() … gl_End().  To use another VP, simply bind to it before the next gl_Begin() … gl_End().

TwoVertex.java  Code very similar to Vertex.java  We load the pastel normal shader for the front and back faces, then load the bright normal shader for the rest of the cube  Note that we needed to gl_End() after the first 2 faces, then bind our next VP, then gl_Begin(GL_QUADS) again

SinWave.cg float3 SinWave(float3 tex : TEXCOORD0) : COLOR { return (sin(sin(23*tex.x*tex.y)-10*tex.y)+1)/2; }  This shader is just using the texture coordinates, and some math.  Like position and normal, these are made available by OpenGL automatically But if SinWave needed other parameters, we would obtain and modify them as usual.

Fragment.java: declaration, init  Very similar to Vertex, except with all “vertex”es replaced with “fragment”s  We make sure to specify the texture coordinates for the corners of each face Texture coords are interpolated across polygons, so fragment programs can use them readily

Lambertian, a la PA1  Vertex program finds the normal and light vectors in world space, outputting those as COLOR0, COLOR1  Fragment program gets interpolated normal, light vector for each pixel and calculates lighting

Lambertian.java  Does standard declare, init: 1 context, 2 profiles (create, enable), 2 programs (create, load) Gets, sets all necessary variables for both programs In display loop, bind both VP and FP That’s it!

Some Tips  Check for errors! CgErrorException.checkCgError(); Debug output in shaders Normals, incident directions, different terms in BRDFs, etc.  Can compile.cg programs cgc – profile vp20 – entry (function name) Program.cg

PA1: Cg Shaders portion  Write a couple of shading models  Must set the input parameters to cg programs.  Reference Lambertian shader code provided

Resources  NVIDIA Cg:  Cg User Manual: html Also in Cg Toolkit folder 