Programmable Graphics Hardware

Similar documents
12.2 Programmable Graphics Hardware

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

Intro to GPU Programming (OpenGL Shading Language) Cliff Lindsay Ph.D. Student CS WPI

Supplement to Lecture 22

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Shader Programming. Daniel Wesslén, Stefan Seipel, Examples

Programming shaders & GPUs Christian Miller CS Fall 2011

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Objectives. Open GL Shading Language (GLSL)

Introduction to Shaders.

Introduction to the OpenGL Shading Language (GLSL)

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

Lecture 09: Shaders (Part 1)

GLSL 1: Basics. J.Tumblin-Modified SLIDES from:

CSE 167: Introduction to Computer Graphics Lecture #13: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Shader Programming 1. Examples. Vertex displacement mapping. Daniel Wesslén 1. Post-processing, animated procedural textures

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Programmable Graphics Hardware

Some advantages come from the limited environment! No classes. Stranight ans simple code. Remarkably. Avoids most of the bad things with C/C++.

COMP371 COMPUTER GRAPHICS

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

CS GPU and GPGPU Programming Lecture 7: Shading and Compute APIs 1. Markus Hadwiger, KAUST

Shaders. Slide credit to Prof. Zwicker

CSE 167: Lecture #8: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

OpenGL Programmable Shaders

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

CMPS160 Shader-based OpenGL Programming. All slides originally from Prabath Gunawardane, et al. unless noted otherwise

OPENGL RENDERING PIPELINE

2D graphics with WebGL

Shading Languages. Ari Silvennoinen Apri 12, 2004

Introduction to the OpenGL Shading Language

Lecture 2. Shaders, GLSL and GPGPU

Sign up for crits! Announcments

Shaders (some slides taken from David M. course)

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

GLSL v1.20. Scott MacHaffie Schrödinger, Inc.

Real - Time Rendering. Graphics pipeline. Michal Červeňanský Juraj Starinský

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

CMPS160 Shader-based OpenGL Programming

CS559 Computer Graphics Fall 2015

Tuesday, 23 March OpenGL Shading Language

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

CS 432 Interactive Computer Graphics

Vertex & Fragment shaders

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Programmable shader. Hanyang University

The OpenGL Shading Language

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

Mobile Application Programing: Android. OpenGL Operation

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Programming Graphics Hardware

Basics of GPU-Based Programming

Lecture 11 Shaders and WebGL. October 8, 2015

Shadows. Prof. George Wolberg Dept. of Computer Science City College of New York

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

GLSL Language Details

An Overview GLUT GLSL GLEW

Design Focus. GLSL Language Details. Additions from C++ Additions for Graphics

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Programmable GPUs Outline

MXwendler Fragment Shader Development Reference Version 1.0

Pipeline Operations. CS 4620 Lecture 14

0.Features of the OpenGL Shading Language

GPU Programming EE Midterm Examination

0.Features of the OpenGL Shading Language

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

Programmable Graphics Hardware

OpenGL shaders and programming models that provide object persistence

Methodology for Lecture. Importance of Lighting. Outline. Shading Models. Brief primer on Color. Foundations of Computer Graphics (Spring 2010)

GPU Programming EE Final Examination

1 of 5 9/10/ :11 PM

Technical Game Development II. Reference: Rost, OpenGL Shading Language, 2nd Ed., AW, 2006 The Orange Book Also take CS 4731 Computer Graphics

Advanced Graphics. The Shader knows. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

Mobile Application Programing: Android. OpenGL Operation

Computer Graphics with OpenGL ES (J. Han) Chapter 6 Fragment shader

OpenGL Rendering Pipeline and Programmable Shaders

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

GPU Programming EE Final Examination

GPU Programming EE Final Examination

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Lecture 13: OpenGL Shading Language (GLSL)

Computer Graphics Coursework 1

Transcription:

CSCI 480 Computer Graphics Lecture 14 Programmable Graphics Hardware [Ch. 9] March 2, 2011 Jernej Barbic University of Southern California OpenGL Extensions Shading Languages Vertex Program Fragment Program http://www-bcf.usc.edu/~jbarbic/cs480-s11/ 1

Introduction Recent major advance in real time graphics is the programmable pipeline: First introduced by NVIDIA GeForce 3 (in 2001) Supported by all modern high-end commodity cards NVIDIA, ATI Software Support Direct X 8, 9, 10 OpenGL This lecture: programmable pipeline and shaders 2

OpenGL Extensions Initial OpenGL version was 1.0 Current OpenGL version is 4.1 As graphics hardware improved, new capabilities were added to OpenGL multitexturing multisampling non-power-of-two textures shaders and many more 3

OpenGL Grows via Extensions Phase 1: vendor-specific: GL_NV_multisample Phase 2: multi-vendor: GL_EXT_multisample Phase 3: approved by OpenGL s review board GL_ARB_multisample Phase 4: incorporated into OpenGL (v1.3) 4

OpenGL 2.0 Added Shaders Shaders are customized programs that replace a part of the OpenGL pipeline They enable many effects not possible by the fixed OpenGL pipeline Motivated by Pixar s Renderman (offline shader) 5

Shaders Enable Many New Effects Complex materials Shadowing Lighting environments Advanced mapping 6

The Rendering Pipeline vertices vertices fragments fragments CPU Vertex Processor Rasterizer Fragment Processor Frame Buffer 7

Shaders Replace Part of the Pipeline vertices vertices fragments fragments CPU Vertex Processor Rasterizer Fragment Processor Frame Buffer customizable by a vertex program customizable by a fragment program 8

Shaders Vertex shader (= vertex program) Fragment shader (= fragment program) Geometry shader (recent addition) Default shaders are provided by OpenGL (fixed-function pipeline) Programmer can install her own shaders as needed 9

Shaders Are Written in Shading Languages Early shaders: assembly language Since ~2004: high-level shading languages OpenGL Shading Language (GLSL) highly integrated with OpenGL Cg (NVIDIA and Microsoft), very similar to GLSL HLSL (Microsoft), almost identical to Cg All of these are simplified versions of C/C++ 10

Vertex Program Input: vertices, and per-vertex attributes: color normal texture coordinates many more Output: vertex location in clip coordinates vertex color vertex normal many more are possible 11

Simple Vertex Program in GLSL /* pass-through vertex shader */ void main() { } gl_position = gl_projectionmatrix * (gl_modelviewmatrix * gl_vertex); 12

Fragment Program Input: pixels, and per-pixel attributes: color normal texture coordinates many more are possible Inputs are outputs from vertex program, interpolated (by the GPU) to the pixel location! Output: pixel color depth value 13

Simple Fragment Program /* pass-through fragment shader */ void main() { gl_fragcolor = gl_color; } 14

Simple Fragment Program #2 /* all-red fragment shader */ void main() { gl_fragcolor = vec4(1.0, 0.0, 0.0, 1.0); } 15

GLSL: Data Types Scalar Types float - 32 bit, very nearly IEEE-754 compatible int - at least 16 bit bool - like in C++ Vector Types vec[2 3 4] - floating-point vector ivec[2 3 4] - integer vector bvec[2 3 4] - boolean vector Matrix Types mat[2 3 4] - for 2x2, 3x3, and 4x4 floating-point matrices Sampler Types sampler[1 2 3]D - to access texture images 16

GLSL: Operations Operators behave like in C++ Component-wise for vector & matrix Multiplication on vectors and matrices Examples: Vec3 t = u * v; float f = v[2]; v.x = u.x + f; 17

GLSL: Swizzling Swizzling is a convenient way to access individual vector components vec4 myvector; myvector.rgba; // is the same as myvector myvector.xy; // is a vec2 myvector.b; // is a float myvector[2]; // is the same as myvector.b myvector.xb; // illegal myvector.xxx; // is a vec3 18

GLSL: Global Qualifiers Attribute Information specific to each vertex/pixel passed to vertex/fragment shader No integers, bools, structs, or arrays Uniform Constant information passed to vertex/fragment shader Cannot be written to in a shader Example: Vertex Color Example: Light Position Eye Position Varying Info passed from vertex shader to fragment shader Interpolated from vertices to pixels Write in vertex shader, but only read in fragment shader Example: Vertex Color Texture Coords Const To declare non-writable, constant variables Example: pi, e, 0.480 19

GLSL: Flow Control Loops C++ style if-else C++ style for, while, and do Functions Much like C++ Entry point into a shader is void main() No support for recursion Call by value-return calling convention Parameter Qualifiers in - copy in, but don t copy out out - only copy out inout - copy in and copy out Example function: void ComputeTangent( in vec3 N, out vec3 T, inout vec3 coord) { if((dot(n, coord)>0) T = vec3(1,0,0); else T = vec3(0,0,0); coord = 2 * T; } 20

GLSL: Built-in Functions Wide Assortment Trigonometry (cos, sin, tan, etc.) Exponential (pow, log, sqrt, etc.) Common (abs, floor, min, clamp, etc.) Geometry (length, dot, normalize, reflect, etc.) Relational (less than, equal, etc.) Need to watch out for common reserved keywords Always use built-in functions, don t implement your own Some functions aren t implemented on some cards 21

GLSL: Accessing OpenGL State Built-in Variables Always prefaced with gl_ Accessible to both vertex and fragment shaders Uniform Variables Matrices (ModelViewMatrix, ProjectionMatrix, inverses, transposes) Materials (in MaterialParameters struct, ambient, diffuse, etc.) Lights (in LightSourceParameters struct, specular, position, etc.) Varying Variables FrontColor for colors TexCoord[] for texture coordinates 22

GLSL: Accessing OpenGL State Vertex Shader: Have access to several vertex attributes: gl_color, gl_normal, gl_vertex, etc. Also write to special output variables: gl_position, gl_pointsize, etc. Fragment Shader: Have access to special input variables: gl_fragcoord, gl_frontfacing, etc. Also write to special output variables: gl_fragcolor, gl_fragdepth, etc. 23

Example: Phong Shader ( per-pixel lighting ) Questions? Goals: C/C++ Application Setup Vertex Shader Fragment Shader Debugging 24

Phong Shading Review l n θ v φ r 25

Phong Shader: Setup Steps Step 1: Create Shaders Create handles to shaders Step 2: Specify Shaders load strings that contain shader source Step 3: Compiling Shaders Actually compile source (check for errors) Step 4: Creating Program Objects Program object controls the shaders Step 5: Attach Shaders to Programs Attach shaders to program objects via handle Step 6: Link Shaders to Programs Another step similar to attach Step 7: Enable Shaders Finally, let OpenGL and GPU know that shaders are ready 26

Phong Shader: Vertex Program varying vec3 n; varying vec3 vtx; void main(void) { } // transform vertex position to eye coordinates: vtx = vec3(gl_modelviewmatrix * gl_vertex); // transform normal: these will be passed to fragment program (interpolated by hardware) n = normalize(gl_normalmatrix * gl_normal); // transform vertex position to clip coordinates: gl_position = gl_modelviewprojectionmatrix * gl_vertex; 27

Phong Shader: Fragment Program varying vec3 n; varying vec3 vtx; void main (void) { interpolated from vertex program outputs // we are in eye coordinates, so eye pos is (0,0,0) vec3 l = normalize(gl_lightsource[0].position.xyz - vtx); vec3 v = normalize(-vtx); vec3 r = normalize(-reflect(l,n)); //calculate ambient, diffuse, specular terms: vec4 Iamb = gl_frontlightproduct[0].ambient; vec4 Idiff = gl_frontlightproduct[0].diffuse * max(dot(n,l), 0.0); vec4 Ispec = gl_frontlightproduct[0].specular * pow(max(dot(r,v),0.0), gl_frontmaterial.shininess); // write total color: gl_fragcolor = gl_frontlightmodelproduct.scenecolor + Iamb + Idiff + Ispec; } l n θ v φ r 28

Debugging Shaders No good automatic debugging tools exist Common show-stoppers: Typos in shader source Assuming implicit type conversion Attempting to pass data to undeclared varying/uniform variables Extremely important to check error codes, use status functions like: glgetobjectparameter{i f}varb (GLhandleARB shader, GLenum whattocheck, GLfloat * statusvals) Subtle Problems Type overflow Shader too long Use too many registers 29

Summary OpenGL Extensions Shanding Languages Vertex Programs Fragment Programs Phong Shading in GLSL 30