Lecture 09: Shaders (Part 1)

Similar documents
Programming shaders & GPUs Christian Miller CS Fall 2011

Supplement to Lecture 22

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

GLSL: Creating GLSL Programs - Overview

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

An Introduction to 3D Computer Graphics, Stereoscopic Image, and Animation in OpenGL and C/C++ Fore June

12.2 Programmable Graphics Hardware

Programmable Graphics Hardware

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

- Surface Rendering -

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

Programmable shader. Hanyang University

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

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

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

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

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

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

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

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

Objectives. Open GL Shading Language (GLSL)

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

The GLSL API. Mike Bailey. Oregon State University. Geometry Shader. Program. The GLSL Shader-creation Process. create. compile. Vertex.

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

GLSL Programming. Nicolas Holzschuch

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

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

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

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

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

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

Introduction to the OpenGL Shading Language (GLSL)

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

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

Introduction to Computer Graphics. Hardware Acceleration Review

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

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

Vertex & Fragment shaders

Programmable Shading. University of Texas at Austin CS354 - Computer Graphics

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

CS 432 Interactive Computer Graphics

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

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

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

Tutorial 1: Your First Triangle!

Introduction to Shaders.

COMP371 COMPUTER GRAPHICS

Shaders. Slide credit to Prof. Zwicker

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

CMPS160 Shader-based OpenGL Programming

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

OpenGL shaders and programming models that provide object persistence

INTRODUCTION TO OPENGL PIPELINE

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

An Overview GLUT GLSL GLEW

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

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

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

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

Programmable Graphics Hardware

EECS 487: Interactive Computer Graphics

Lecture 31 Sections 9.1, 9.2, 9.3. Mon, Nov 23, 2009

The OpenGL Shading Language

Philip Calderon CSE 520 Lab 3 Color Shader

Mobile Application Programing: Android. OpenGL Operation

OpenGL. Shading Language. Third Edition

OpenGL Programmable Shaders

Programmable Graphics Hardware

Introduction to the OpenGL Shading Language

OpenGL: What s Coming Down the Graphics Pipeline. Dave Shreiner ARM

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

3d Programming I. Dr Anton Gerdelan

GLSL Language Details

Computer Graphics (CS 4731) & 2D Graphics Systems

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

Programmable GPUs Outline

Shaders (some slides taken from David M. course)

2D graphics with WebGL

Lecture 13: OpenGL Shading Language (GLSL)

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

Sign up for crits! Announcments

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

OPENGL RENDERING PIPELINE

gvirtualxray Tutorial 01: Creating a Window and an OpenGL Context Using GLUT

Mobile Application Programing: Android. OpenGL Operation

1)Write a shader program that renders a regular pentagon with textured image like the one shown below.

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

GPU Programming EE Final Examination

last time put back pipeline figure today will be very codey OpenGL API library of routines to control graphics calls to compile and load shaders

Lighting and Texturing

Tuesday, 23 March OpenGL Shading Language

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

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

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

Objectives. Coupling GLSL to Applications Example applications

Stripes, Rings, and Dots!

Transcription:

Lecture 09: Shaders (Part 1) CSE 40166 Computer Graphics Peter Bui University of Notre Dame, IN, USA November 9, 2010

OpenGL Rendering Pipeline

OpenGL Rendering Pipeline (Pseudo-Code) 1 f o r gl_vertex in GL_VERTICES : 2 // P r o c e s s v e r t i c e s 3 gl_position = gl_modelviewprojectionmatrix * gl_vertex 4 gl_frontcolor = gl_color ; 5 6 // C l i p and a s s e m b l e 7 primitive = Assemble ( gl_position, gl_frontcolor ) 8 i f not Clipped ( primitive ): 9 PRIMITIVES. append ( primitive ) 10 11 // R a s t e r i z e p r i m i t i v e s i n t o f r a g m e n t s 12 FRAGMENTS = Rasterize ( PRIMITIVES ) 13 14 f o r fragment in FRAGMENTS : 15 // P r o c e s s f r a g m e n t s 16 gl_fragcolor = ApplyLighting ( gl_color )

OpenGL Rendering Pipeline (Pros and Cons) Pros Has the following features: Concurrency: Each vertex and fragment can be processed in parallel. Fast: Hardware implements of common functions. Good Enough: Modified Phong lighting model yields adequate images. Cons Hard to achieve the following: Photorealism: Skin, fabrics, fluids, translucent materials, refraction. Non-photorealism: Simulate brush strokes or cartoonlike shading effects.

Difficult to Render

From Fixed to Programmable (Problem) Problem Although OpenGL pipeline is fast it is limited due to its fixed functionality.

From Fixed to Programmable (Solution) Solution Allow developers to replace fixed functions with their own programmable shaders. Vertex Shader: Replace for vertex transformation stage. Fragment Shader: Replace fragment texturing and coloring stage.

From Fixed to Programmable (Responsibilities) Vertex Shader Vertex position using ModelView and Projection matrices. Lighting per vertex or per pixel. Color and texture computation. Must at least set gl Position variable. Fragment Shader Computing colors, texture coordinates per pixel. Texture application. Fog computation. Output result by setting gl FragColor. If we use a shader we must re-program ALL functionality.

From Fixed to Programmable (Architecture) Observation Vertex and Fragment processor is a parallel computing unit. Data Parallelism: Each data item can be processed independently. SPMD: Single Program Multiple Data. Stream Processors: Many simple processors attached to fixed computational units.

GLSL: OpenGL Shading Language Based on the C programming language (with some C++isms). Shader application is run per vertex or per fragment with results set in particular global variables. Example: Pass through vertex shader 1 v o i d main () 2 { 3 gl_position = gl_modelviewprojectionmatrix * gl_vertex ; 4 } Example: Pass through fragment shader 1 v o i d main () 2 { 3 gl_fragcolor = gl_color ; 4 }

GLSL: Data Types Data Types Scalar: Floating point (float), integer (int), boolean bool). Vectors: One dimensional arrays with swizzling operator. Matrices: Square two-dimensional arrays with overloaded operators. Arrays and Structs: Same as in C. Example: Data types 1 v o i d main () 2 { 3 f l o a t f [16]; 4 i n t i = 0; 5 vec4 red = vec4 (1.0, 0.0, 0.0, 1.0); 6 mat4 m = gl_projectionmatrix ; 7 f l o a t r, g, b; 8 9 r = red.r; 10 g = red.g; 11 b = red.b; 12 red. rgb = vec3 (0.5, 0.0, 0.0); 13 }

GLSL: Data Qualifiers const: Variable is unchangeable by the shader. attribute: Variable changes at most once per vertex in vertex shader. uniform: Variable set in application program for an entire batch of primitives. varying: Provide mechanism for conveying data from a vertex shader to fragment shader. Example: scaling vertex shader 1 uniform f l o a t ElapsedTime ; 2 3 v o i d main () 4 { 5 f l o a t s; 6 7 s = 1.0 + 0.5* sin (0.005* ElapsedTime ); 8 9 gl_position = gl_modelviewprojectionmatrix *( vec4 (s, s, s, 1.0)* gl_vertex ); 10 gl_frontcolor = gl_color ; 11 }

GLSL: Operators and Functions Matrix-vector operations behave as expected. Swizzling operator allows for convenient access to elements of a vector: x,y,z,w; r,g,b,a; s,t,p,q. Can access built-in functions: Trigonometric: asin, acos, atan. Mathematical: pow, log2, sqrt, abs, max, min. Geometric: length, distance, dot, normalize, reflect. Fixed: ftransform. Can make your own functions, but must qualify variables as in, out, inout. Example: Pass through vertex shader (ftransform) 1 v o i d main () 2 { 3 gl_position = ftransform (); 4 }

GLSL: Attaching, Compiling, and Linking Shaders To use shaders in an OpenGL program, we need to load them into memory, attach them, compile them and link the program.

GLSL: GLEW In order to load, compile, and use shaders, we must use OpenGL extensions. To manage the use of these extensions, we use the GLEW library. 1 // I n c l u d e GLEW b e f o r e GLUT 2 #i n c l u d e <GL/ glew.h> 3 #i n c l u d e <GL/ glut.h> 4 5 initialize () 6 { 7 GLenum result ; 8 // Note : Must have OpenGL c o n t e x t f i r s t! 9 10 // I n i t i a l i z e and check GLEW 11 result = glewinit (); 12 i f ( result!= GLEW_OK ) { 13 fprintf (stderr, " unable to initialize glew : %s\n", 14 glewgeterrorstring ( result )); 15 exit ( EXIT_FAILURE ); 16 } 17 // GLSL r e q u i r e a t l e a s t OpenGL 2. 0 18 i f (! glewissupported (" GL_VERSION_2_0 ")) { 19 fprintf (stderr, " OpenGL 2.0 is not supported \n"); 20 exit ( EXIT_FAILURE ); 21 } 22 }

GLSL: Attaching, Compiling, and Linking Shaders (Code) 1 GLuint vert_id, frag_id, prog_id ; 2 v o i d load_shaders ( c o n s t char * vert_path, c o n s t char * frag_path ) 3 { 4 char *vert_src, * frag_src ; 5 6 // Create program, vertex and fragment shaders IDs 7 prog_id = glcreateprogram (); 8 vert_id = glcreateshader ( GL_VERTEX_SHADER ); 9 frag_id = glcreateshader ( GL_FRAGMENT_SHADER ); 10 11 // Read s h a d e r s o u r c e i n t o memory b u f f e r s 12 vert_src = read_shader ( vert_path ); 13 frag_src = read_shader ( frag_path ); 14 15 // Bind s h a d e r s o u r c e 16 glshadersource (vert_id, 1, ( c o n s t char **)& vert_src, NULL ); 17 glshadersource (frag_id, 1, ( c o n s t char **)& frag_src, NULL ); 18 19 // Compile s h a d e r s 20 glcompileshader ( vert_id ); 21 glcompileshader ( frag_id ); 22 23 // Attach s h a d e r s to program 24 glattachshader ( prog_id, vert_id ); 25 glattachshader ( prog_id, frag_id ); 26 27 // L i n k program 28 gllinkprogram ( prog_id ); 29 }

GLSL: Using Shaders and Cleaning up 1 v o i d display () 2 { 3 gluseprogram ( prog_id ); // Use s h a d e r program 4 // Render code 5 gluseprogram (0); // D i s a b l e s h a d e r program 6 } 7 8 v o i d cleanup () 9 { 10 // Detach s h a d e r s from program 11 gldetachshader ( prog_id, vert_id ); 12 gldetachshader ( prog_id, frag_id ); 13 14 // D e l e t e s h a d e r ( must be d e t a c h e d ) 15 gldeleteshader ( vert_id ); 16 gldeleteshader ( frag_id ); 17 18 // D e l e t e program 19 gldeleteprogram ( prog_id ); 20 }

GLSL: Debugging 1 v o i d print_shader_log ( GLuint id) { // P r i n t s h a d e r l o g 2 char * buffer ; 3 GLint buffer_written ; 4 GLint buffer_size ; 5 6 glgetshaderiv (id, GL_INFO_LOG_LENGTH, & buffer_size ); 7 i f ( buffer_size > 0) { 8 buffer = malloc ( s i z e o f ( char ) * buffer_size ); 9 glgetshaderinfolog (id, buffer_size, & buffer_written, buffer ); 10 fprintf (stderr, " Shader %u Log : %s\n", id, buffer ); 11 free ( buffer ); 12 } 13 } 14 v o i d print_program_log ( GLuint id) { // P r i n t program l o g 15 char * buffer ; 16 GLint buffer_written ; 17 GLint buffer_size ; 18 19 glgetprogramiv (id, GL_INFO_LOG_LENGTH, & buffer_size ); 20 i f ( buffer_size > 0) { 21 buffer = malloc ( s i z e o f ( char ) * buffer_size ); 22 glgetprograminfolog (id, buffer_size, & buffer_written, buffer ); 23 fprintf (stderr, " Program %u Log : %s\n", id, buffer ); 24 free ( buffer ); 25 } 26 } 27 28 print_shader_log ( vert_id ); 29 print_shader_log ( frag_id ); 30 print_program_log ( prog_id );

Example: Red vertex shading Vertex Shader 1 // C o l o r a l l v e r t i c e s r e d 2 c o n s t vec4 RedColor = vec4 (1.0, 0.0, 0.0, 1.0); 3 4 v o i d main () 5 { 6 gl_position = ftransform (); 7 gl_frontcolor = RedColor ; 8 }

Example: Color based on vertex Vertex Shader 1 // C o l o r a l l v e r t i c e s based on n o r m a l i z e d v e r t e x c o o r d i n a t e s 2 c o n s t vec4 RedColor = vec4 (1.0, 0.0, 0.0, 1.0); 3 4 v o i d main () 5 { 6 gl_position = ftransform (); 7 gl_frontcolor = normalize ( gl_vertex ); 8 }

Example: Scaling over time Vertex Shader 1 // S c a l e v e r t i c e s o v e r time 2 uniform f l o a t ElapsedTime ; 3 4 v o i d main () 5 { 6 f l o a t s; 7 8 s = 1.0 + 0.5* sin (0.005* ElapsedTime ); 9 10 gl_position = gl_modelviewprojectionmatrix *( vec4 (s, s, s, 1.0)* gl_vertex ); 11 gl_frontcolor = gl_color ; 12 } OpenGL 1 v o i d set_elapsed_time () 2 { 3 GLint etloc ; 4 5 / Set s h a d e r u n i f o r m v a r i a b l e / 6 etloc = glgetuniformlocation (prog_id, " ElapsedTime "); 7 gluniform1f (etloc, glutget ( GLUT_ELAPSED_TIME )); 8 }

Example: Flatten and Wave Vertex Shader 1 uniform f l o a t ElapsedTime ; 2 3 v o i d main () 4 { 5 vec4 v = vec4 ( gl_vertex ); 6 7 v.y = sin (5.0 * v.x + ElapsedTime *0.01)*0.25; 8 9 gl_position = gl_modelviewprojectionmatrix * v; 10 gl_frontcolor = gl_color ; 11 }

Example: Toon Shading Vertex Shader 1 varying vec3 Normal ; 2 3 v o i d main () 4 { 5 Normal = gl_normalmatrix * gl_normal ; 6 gl_position = ftransform (); 7 } Fragment Shader 1 varying vec3 Normal ; 2 3 v o i d main () 4 { 5 f l o a t intensity ; 6 vec4 color ; 7 vec3 n = normalize ( Normal ); 8 9 intensity = dot (vec3 ( gl_lightsource [0]. position ), n); 10 11 i f ( intensity > 0.95) color = vec4 (1.0, 0.5, 0.5, 1.0); 12 e l s e i f ( intensity > 0.50) color = vec4 (0.6, 0.3, 0.3, 1.0); 13 e l s e i f ( intensity > 0.25) color = vec4 (0.4, 0.2, 0.2, 1.0); 14 e l s e color = vec4 (0.2, 0.1, 0.1, 1.0); 15 16 gl_fragcolor = color ; 17 }

Resources OpenGL Shading Language Specs: http://www.opengl.org/documentation/glsl/ Lighthouse 3D GLSL Tutorial: http://www.lighthouse3d.com/opengl/glsl/index.php?intro Neon Helium GLSL Tutorial: http://nehe.gamedev.net/data/articles/article.asp?article=21 Clockwork Coders GLSL Tutorial: http://www.clockworkcoders.com/oglsl/tutorials.html Swiftless Tutorials: http://www.swiftless.com/glsltuts.html