CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University.

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

©Zachary Wartell, UNCC9/28/ :30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights.
COMPUTER GRAPHICS CS 482 – FALL 2014 NOVEMBER 10, 2014 GRAPHICS HARDWARE GRAPHICS PROCESSING UNITS PARALLELISM.
Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
Status – Week 257 Victor Moya. Summary GPU interface. GPU interface. GPU state. GPU state. API/Driver State. API/Driver State. Driver/CPU Proxy. Driver/CPU.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
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.
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
A Crash Course on Programmable Graphics Hardware Li-Yi Wei 2005 at Tsinghua University, Beijing.
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Cg: C for Graphics Jon Moon Based on slides by Eugene Lee.
Status – Week 283 Victor Moya. 3D Graphics Pipeline Akeley & Hanrahan course. Akeley & Hanrahan course. Fixed vs Programmable. Fixed vs Programmable.
The Graphics Pipeline CS2150 Anthony Jones. Introduction What is this lecture about? – The graphics pipeline as a whole – With examples from the video.
The programmable pipeline Lecture 10 Slide Courtesy to Dr. Suresh Venkatasubramanian.
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.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.
REAL-TIME VOLUME GRAPHICS Christof Rezk Salama Computer Graphics and Multimedia Group, University of Siegen, Germany Eurographics 2006 Real-Time Volume.
CHAPTER 4 Window Creation and Control © 2008 Cengage Learning EMEA.
Enhancing GPU for Scientific Computing Some thoughts.
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.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
Programmable Pipelines. 2 Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
A Crash Course in HLSL Matt Christian.
CS 450: COMPUTER GRAPHICS REVIEW: INTRODUCTION TO COMPUTER GRAPHICS – PART 2 SPRING 2015 DR. MICHAEL J. REALE.
The programmable pipeline Lecture 3.
CS 480/680 Intro Dr. Frederick C Harris, Jr. Fall 2014.
CSE Real Time Rendering Week 2. Graphics Processing 2.
Stream Processing Main References: “Comparing Reyes and OpenGL on a Stream Architecture”, 2002 “Polygon Rendering on a Stream Architecture”, 2000 Department.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
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...
Lecture 2: Some definitions of terms 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
OpenGL-ES 3.0 And Beyond Boston Photo credit :Johnson Cameraface OpenGL Basics.
Computer Graphics 3 Lecture 6: Other Hardware-Based Extensions Benjamin Mora 1 University of Wales Swansea Dr. Benjamin Mora.
© David Kirk/NVIDIA and Wen-mei W. Hwu, ECE408, University of Illinois, Urbana-Champaign 1 Programming Massively Parallel Processors Lecture.
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.
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.
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.
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.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Programmable Pipelines
A Crash Course on Programmable Graphics Hardware
Graphics Processing Unit
Introduction to OpenGL
Chapter 6 GPU, Shaders, and Shading Languages
The Graphics Rendering Pipeline
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Introduction to Programmable Hardware
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
Introduction to OpenGL
OpenGL-Rendering Pipeline
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University

GPU Generations For the labs, 4th generation is desirable

Graphics Hardware Pipeline

Vertex Stage Vertex values:  position, color, texture coordinate(s), normal vector Operations (in a vertex program):  perform a sequence of math ops on each vertex  transform position into screen position for rasterizer  generate texture coordinates for texturing  perform vertex lighting to determine its color

Primitive Assembly and Rasterizer Operations:  assemble vertices into geometric primitives (triangles, lines, points)  clipping to the view frustrum and other clip planes  eliminate backward-facing polygons (culling)  rasterize geometric primitives into fragments

Primitive Assembly and Rasterizer Rasterization:  yields a set pixel locations and fragments What is a fragment?  potential pixel (still subject to fragment kill)  values: color, depth, location, texture coordinate sets Number of vertices and fragments are unrelated!

Fragment Stage Operations (fragment program):  interpolation, texturing, and coloring  math operations Output:  final color (RGBA), depth  output is either 1 or 0 fragments (may be discarded)

Raster Operations Final sequence of per-fragment operations before updating the framebuffer  fragment may be discarded here as well

Graphics Pipeline: Summary Vertices and fragments are vectors (up to dimension 4) Vertex and fragment stage are programmable

Cg Cg available to overcome need for assembler programming

Cg Developed by Nvidia Can work with OpenGL as well as Direct3D CG compiler produces OpenGL or Direct3D code  e.g., OpenGL’s ARB_fragment_program language OpenGL or Direct3D drivers perform final translation into hardware-executable code  core CG runtime library (CG prefix)  cgGL and cgD3D libraries

Simple Vertex Program Semantics connect Cg program with graphics pipeline  here: POSITION and COLOR float4, float2, float4x4, etc, are packed arrays  operations are most efficient semantics to rasterizer

Uniform vs. Varying Parameters Varying: values vary per vertex or fragment  interfaced via semantics Uniform: remain constant  interfaced via handles varying uniform

Compilation To interface Cg programs with application:  compile the program with appropriate profile dependent on underlying hardware  range of profiles will grow with GPU advances OpenGL: arbvp1 (basic), vp20, vp30 (advanced Nvidia) Link the program to the application program Can perform compilation at  compile time (static)  runtime (dynamic)

Cg Runtime Can take advantage of  latest profiles  optimization of existing profiles No dependency issues  register names, register allocations In the following, use OpenGL to illustrate  Direct3D similar methods

Preparing a Cg Program First, create a context:  context = cgCreateContext() Compile a program by adding it to the context:  program = cgCreateProgram(context, programString, profile, name, args) Loading a program (pass to the 3D API):  cgGLLoadProgram(program)

Running a Cg Program Executing the profile:  cgEnableProfile(CG_PROFILE_ARBVP1) Bind the program:  cgGLBindProgram(program) After binding, the program will execute in subsequent drawing calls  for every vertex (for vertex programs)  for every fragment (for fragment programs)  these programs are often called shaders

Running a Cg Program One one vertex / fragment program can be bound at a time  the same program will execute unless another program is bound Disable a profile by:  cgGLDisableProfile(CG_PROFILE_ARBVP1) Release resources:  cgDestroyProgram(program)  cgDestroyContext(context)  the latter destroys all programs as well

Error Handling There are core CG routines that retrieve global error variables:  error = cgGetError()  cgGetErrorString(error)  cgSet ErrorCallback(MyErrorCallback)

Passing Parameters into CG Programs Assume these shader variables:  float4 position : POSITION  float4 color : COLOR0 Get the handle for color by:  color = cgGetNamedParameter(program, "IN.color") Can set the value for color by:  cgGLSetParameter4f(color, 0.5f, 1.0f, 0.5f, 1.0f) Uniform variables are set infrequently:  example: modelViewMatrix

Passing Parameters into CG Programs Set other variables via OpenGL semantics:  glVertex, glColor, glTexCoord, glNormal,… Example: rendering a triangle with OpenGL: glBegin(GL_TRIANGLES); glVertex( 0.8, 0.8); glVertex(-0.8, 0.8); glVertex( 0.0, -0.8); glEnd(); glVertex affects POSITION semantics and updates/sets related parameter in vertex shader

Example 1 Vertex program  OUT parameter values are passed to fragment shader

Example 1 Result, assumimg:  a fragment shader that just passes values through  OpenGl program glBegin(GL_TRIANGLES); glVertex( 0.8, 0.8); glColor(dark); glVertex(-0.8, 0.8); glColor(dark); glVertex( 0.0, -0.8); glColor(light); glEnd();

Example 2 Fragment program, following example 1 vertex program Sampler2D is a texture object  other types exist: sampler3D, samplerCUBE, etc

Example 2 Tex2D(decal, texCoord) performs a texture- lookup  sampling, filtering, and interpolation depends on texture type and texture parameters  advanced fragment profiles allow sampling using texture coordinate sets from other texture units (dependent textures)

Example 2 Result

Math Support A rich set of math operators and library functions  +-/*, sin, cos, floor, etc….  no bit-wise operators yet, but operators reserved Latest hardware full floating point on framebuffer operations  half-floats are also available Function overloading frequent  for example, abs() function accepts float4, float2

Syntax IN keyword  call by value  parameter passing by value OUT keyword  indicates when the program returns

Example 3 2D Twisting

Example 3 Result finer meshes give better results

Example 4 Double Vision: vertex program OUT is defined via semantics in the prototype  optional

Example 4 Double Vision: fragment program #1  advanced fragment profiles  samples the same texture (named decal) twice lerp(a, b, weight)  result = (1-weight)  a + weight  b

Example 4 Result

Example 4 Double Vision: fragment program #2  basic fragment profiles  samples two different textures (decal0 and decal1)  textures must be bound to two texture units in advance

Further Resources Book  “The CG Tutorial: The Definite Guide to Programmable Real-Time Graphics” by R. Fernando and M. Kilgard, Addison Wesley, 2003 Web  developer.nvidia.com/Cg   many other resources on the web Try “hacking” some existing code to get what you want

First Programming Project Exercises and in the Cg book Exercises and in the Cg book Voronoi diagram example from lecture 5 Nearest neighbor example from lecture 5 This project’s goals are:  set up your machine with the Cg programming environment  get used to CG itself If you know Cg already well, then just skip the first two examples and do only the last two Finish by Feb 24