An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.

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

COMPUTER GRAPHICS CS 482 – FALL 2014 NOVEMBER 10, 2014 GRAPHICS HARDWARE GRAPHICS PROCESSING UNITS PARALLELISM.
The Programmable Graphics Hardware Pipeline Doug James Asst. Professor CS & Robotics.
CS5500 Computer Graphics © Chun-Fa Chang, Spring 2007 CS5500 Computer Graphics April 19, 2007.
Control Flow Virtualization for General-Purpose Computation on Graphics Hardware Ghulam Lashari Ondrej Lhotak University of Waterloo.
Introduction to Shader Programming
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.
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.
Shading Languages By Markus Kummerer. Markus Kummerer 2 / 19 State of the Art Shading.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
GPU Tutorial 이윤진 Computer Game 2007 가을 2007 년 11 월 다섯째 주, 12 월 첫째 주.
GPU Graphics Processing Unit. Graphics Pipeline Scene Transformations Lighting & Shading ViewingTransformations Rasterization GPUs evolved as hardware.
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.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
Technology and Historical Overview. Introduction to 3d Computer Graphics  3D computer graphics is the science, study, and method of projecting a mathematical.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
May 8, 2007Farid Harhad and Alaa Shams CS7080 Over View of the GPU Architecture CS7080 Class Project Supervised by: Dr. Elias Khalaf By: Farid Harhad &
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.
Chris Kerkhoff Matthew Sullivan 10/16/2009.  Shaders are simple programs that describe the traits of either a vertex or a pixel.  Shaders replace a.
CS 450: COMPUTER GRAPHICS REVIEW: INTRODUCTION TO COMPUTER GRAPHICS – PART 2 SPRING 2015 DR. MICHAEL J. REALE.
1 Dr. Scott Schaefer Programmable Shaders. 2/30 Graphics Cards Performance Nvidia Geforce 6800 GTX 1  6.4 billion pixels/sec Nvidia Geforce 7900 GTX.
The programmable pipeline Lecture 3.
CSE 690: GPGPU Lecture 6: Cg Tutorial Klaus Mueller Computer Science, Stony Brook University.
Stream Processing Main References: “Comparing Reyes and OpenGL on a Stream Architecture”, 2002 “Polygon Rendering on a Stream Architecture”, 2000 Department.
Shadow Mapping Chun-Fa Chang National Taiwan Normal University.
The Rendering Pipeline CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
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.
Image Synthesis Rabie A. Ramadan, PhD 4. 2 Review Questions Q1: What are the two principal tasks required to create an image of a three-dimensional scene?
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
May 8, 2007Farid Harhad and Alaa Shams CS7080 Overview of the GPU Architecture CS7080 Final Class Project Supervised by: Dr. Elias Khalaf By: Farid Harhad.
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.
From Turing Machine to Global Illumination Chun-Fa Chang National Taiwan Normal University.
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.
Mesh Skinning Sébastien Dominé. Agenda Introduction to Mesh Skinning 2 matrix skinning 4 matrix skinning with lighting Complex skinning for character.
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.
GPU Computing for GIS James Mower Department of Geography and Planning University at Albany.
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.
Our Graphics Environment Landscape Rendering. Hardware  CPU  Modern CPUs are multicore processors  User programs can run at the same time as other.
- Introduction - Graphics Pipeline
Programmable Pipelines
Graphics Processing Unit
Chapter 6 GPU, Shaders, and Shading Languages
CS451Real-time 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.
Graphics Processing Unit
CS5500 Computer Graphics April 17, 2006 CS5500 Computer Graphics
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 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department

Topics Background definitions Shading Language Basics Cg specifics Simple sample Cg programs How to call Cg programs from C/C++ Performance tips Frame-buffer Objects (if time permits) References: Learn more!

Motivation Languages like Cg allow programmers to have more control over the graphics processor. Code is easier to produce, comprehend and maintain. Can take more advantage of the graphics acceleration technology available today. Produce better-looking, faster graphics.

GPU Graphics Processing Unit (GPU). Processor specialized in transforming, rasterizing, and rendering 3D models into 2D images. CPU for general computation, GPU specialized in graphics computation. CPU can do anything GPU can do, but GPU is faster for parallel floating point calculations.

Shading Language Instructions are executed in GPU. Cg, OpenGL Shading Language, and High-level Shading Language (HLSL) are examples of shading languages. Programs are called shaders, can be compiled and executed at run-time from inside a C++ application. Shaders are run as a result of the graphics library’s call to “draw” or render polygons.

Why Use Shaders? Speed. GPUs specialize in performing many float operations in parallel, making them faster than CPUs for most 3D graphics operations. Programmability. Before high-level shaders, GPUs could only be programmed using assembly code.

Vertex Shaders Program that is executed for every vertex in a 3D model sent through the pipeline. Changes vertex positions for animation Performs transformations from object space to eye space or world space. Can do lighting calculations.

Pixel Shaders Also known as fragment shaders. Executed for every fragment (potential pixel) that is rasterized from the polygons created from the vertices. Usually perform lighting, shading, and texturing calculations.

Programmable Graphics Pipeline

Data Types Vectors and Matrices supported as packed arrays. Operations between these types are processed in parallel (more efficient use of hardware). Vector types are float2, float3, float4, half2, half3, half4 (half the precision of a float). E.g., “float3 vertexPos;” has three values that can be accessed individually by vertexPos.x, vertexPos.y, vertexPos.z. Matrices have similar types. A 4x4 matrix can be defined as “float4x4 matrixName;”

Data Types Semantics are special words preceded by a colon that indicate an input or output that is varying, i.e. different for each vertex/fragment processed by a shader. A uniform parameter to a function is a set of data that is the same for all vertices/fragments. These values are accessible to every vertex/fragment and are loaded to memory only once for all vertices/fragments processed.

Textures Textures: common uniform input type for large quantities of data in shaders. They are one, two, or three-dimensional arrays of packed arrays. Typically, a texture would be a two- dimensional array of float3 values that contain the RGB values of an image. In practice, a texture can contain any kind of data.

Homogeneous Coordinates 3D positions expressed as 4D vectors All transformations (rotation, translation, scaling, etc.) are 4x4 matrices Matrix/vector multiplication is the only operator needed in homogeneous coordinate transforms All transformations can be multiplied and compounded into a single transform to multiply by each vertex

Transformations

Cg Language Many math expressions supported by language: lerp (linear interpolation), reflect (reflection vector calculation), dot (dot product) and cross (cross product), tex2D (2D texture lookup), etc. Structures, and arrays, if size is known at compile-time (pointers not supported) Functions and return statement If-else For, while, and do-while

A Simple Vertex Program Void translateXby1(float3 position : POSITION, float2 texCoord : TEXCOORD0, out float4 oPosition : POSITION, outfloat2 oTexCoord : TEXCOORD0) { oPosition = float4(position + float3(1,0,0), 1); oTexCoord = texCoord; }

A Simple Fragment Program Void modulate(float4 color : COLOR, float2 texCoord : TEXCOORD0, outfloat4 oColor : COLOR, uniformsampler2D decal) { float4 decalColor = tex2D(decal, texCoord); oColor = lerp(color, decalColor, 0.5); }

Cg Runtime (for C/C++) #include CGcontext context = cgCreateContext(); CGprogram program = cgCreateProgram(context, CG_SOURCE, // or object programString, // text/data profile, // depends on GPU “main”, // entry function name args ); // compiler options

Cg Runtime (for C/C++) cgGLLoadProgram(program); Cgparameter myParameter = cgGetNamedParameter(program, “myParameter”); cgGLSetParameter4fv(myParameter, value); cgGLEnableProfile(CG_PROFILE_ARBVP1); cgGLBindProgram(program); cgDestroyProgram(program); cgDestroyContext(context); Cgerror error = cgGetError(); Const char* errorString = cgGetErrorString(error);

Performance Tips Use the Cg Standard Library Take advantage of Uniform parameters (by computing unchanging calculations outside of shader if possible) Using Vertex Programs vs. Fragment Programs Use lower precision data types if possible Take advantage of Vectorization (use parallel computations by performing operations on one float4 instead of doing it four times for a float)

Performance Tips Use textures to encode functions (texture lookups are usually faster than math) Use swizzling and negation freely, no performance penalty. E.g., float4(myScalar.xxx, 1) Shade only pixels that you must (perform depth test before fragment program runs) Shorter assembly is not necessarily faster

GPGPU Shaders normally help render a 2D image of a 3D world on the window. GPGPU (General-purpose GPU) techniques use GPU for any task involving intensive, parallel floating point computation. Only way to output data computed in a shader is to render a texture, using the color channels to store floating point data. Texture is not displayed on the window, but rendered off-screen to be accessed by C++ application.

Frame-buffer Objects (FBOs) Most recent technique to render-to-texture or to Renderbuffers, better performance than P- buffer and ARB render texture Applications: dynamic textures, multi-pass techniques, image processing effects, GPGPU Uses OpenGL extension called EXT_framebuffer_object

FBO Advantages Requires only a single OpenGL context No need for complicated pixel format selection Avoids performing copy from framebuffer to texture Uses less memory, only one copy of image

References Cg Toolkit User’s Manual. ftp://download.nvidia.com/developer/cg/Cg_1.4/ Docs/CG_UserManual_1-4.pdf ftp://download.nvidia.com/developer/cg/Cg_1.4/ Docs/CG_UserManual_1-4.pdf The OpenGL Framebuffer Object Extension. ons/2005/GDC/OpenGL_Day/OpenGL_FrameB uffer_Object.pdf ons/2005/GDC/OpenGL_Day/OpenGL_FrameB uffer_Object.pdf Fernando, R. and Kilgard, M. The Cg Tutorial NVIDIA Corporation.