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

Similar documents
Programmable Graphics Hardware

12.2 Programmable Graphics Hardware

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

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

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

CS 432 Interactive Computer Graphics

Supplement to Lecture 22

Programming with OpenGL Shaders I. Adapted From: 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

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

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

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

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

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

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

Lecture 09: Shaders (Part 1)

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

Programming shaders & GPUs Christian Miller CS Fall 2011

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

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

Shaders. Slide credit to Prof. Zwicker

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Introduction to the OpenGL Shading Language (GLSL)

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

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

Introduction to Shaders.

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

Lecture 11 Shaders and WebGL. October 8, 2015

Programmable Graphics Hardware

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

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

Starting out with OpenGL ES 3.0. Jon Kirkham, Senior Software Engineer, ARM

Sign up for crits! Announcments

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

EECS 487: Interactive Computer Graphics

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

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

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

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

GLSL Overview: Creating a Program

Shading Languages. Ari Silvennoinen Apri 12, 2004

2D graphics with WebGL

OPENGL RENDERING PIPELINE

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

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

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Rendering Objects. Need to transform all geometry then

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

An Overview GLUT GLSL GLEW

Vertex & Fragment shaders

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Interaction. CSCI 420 Computer Graphics Lecture 3

Cg 2.0. Mark Kilgard

OpenGL Programmable Shaders

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

Lecture 2. Shaders, GLSL and GPGPU

CSE 167. Discussion 03 ft. Glynn 10/16/2017

Texture Mapping. Texture Mapping. Map textures to surfaces. Trompe L Oeil ( Deceive the Eye ) The texture. Texture map

Introduction to the OpenGL Shading Language

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

CS 432 Interactive Computer Graphics

COURSE 17: STATE-OF-THE-ART IN SHADING HARDWARE1 CHAPTER 6: THE OPENGL SHADING LANGUAGE 2. Randi J. Rost 3Dlabs, Inc.

Mobile Application Programing: Android. OpenGL Operation

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

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

Computer Graphics (CS 4731) & 2D Graphics Systems

Lighting and Texturing

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

CPSC 436D Video Game Programming

Computer Graphics Seminar

Shaders (some slides taken from David M. course)

Computergraphics Exercise 15/ Shading & Texturing

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

Shading System Immediate-Mode API v2.2

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

Objectives. Open GL Shading Language (GLSL)

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

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

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

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

CS559 Computer Graphics Fall 2015

Particle Systems with Compute & Geometry Shader. Institute of Computer Graphics and Algorithms Vienna University of Technology

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

Could you make the XNA functions yourself?

Programming Graphics Hardware

CS GPU and GPGPU Programming Lecture 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

Tutorial 04. Harshavardhan Kode. September 14, 2015

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

Lecture 13: OpenGL Shading Language (GLSL)

The OpenGL Shading Language

CGT520 Lighting. Lighting. T-vertices. Normal vector. Color of an object can be specified 1) Explicitly as a color buffer

Pipeline Operations. CS 4620 Lecture 14

Transcription:

CSCI 420 Computer Graphics Lecture 4 Shaders Jernej Barbic University of Southern California Shading Languages GLSL Vertex Array Objects Vertex Shader Fragment Shader [Angel Ch. 1, 2, A] Introduction The major advance in real time graphics has been the programmable pipeline: First introduced by NVIDIA GeForce 3 (in 2001) Supported by all modern high-end commodity cards NVIDIA, AMD, Intel Software Support Direct3D OpenGL This lecture: programmable pipeline and shaders 1 2 OpenGL Extensions Initial OpenGL version was 1.0 Current OpenGL version is 4.5 As graphics hardware improved, new capabilities were added to OpenGL multitexturing multisampling non-power-of-two textures shaders and many more 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) 3 4 OpenGL 2.0 Added Shaders Shaders Enable Many New Effects 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) Complex materials Shadowing 5 Lighting environments Advanced mapping 6 1

Vertex Shader Vertex Shader 5x5 terrain (as in hw1) 5x5 = 25 vertices 4x4 = 16 quads 7 96 vertex shaders User must tessellate into execute in parallel triangles (in the VBO) 4 x 4 x 2 = 32 triangles 32 x 3 = 96 vertices (assuming GL_TRIANGLES) 8 Fragment Shader Rasterization This triangle rasterizes into 13 pixels Triangles (now in 2D) cover m pixels Some pixels may repeat in multiple triangles m fragment shaders execute in parallel 9 The Rendering Pipeline CPU vertices gldrawarrays() Discard off-screen triangles Convert triangles to pixels Position vertices (and project to 2D) Vertex Shader triangles Clipping vertices triangles fragments Rasterizer Fragment Shader Subdivide geometry Tessellation Shader Geometry Shader pixels Color each fragment GPU triangles Process geometry Frame Buffer 10 Shaders Vertex shader (= vertex program) Tessellation control and evaluation shader (OpenGL 4.0; subdivide the geometry) Geometry shader (OpenGL 3.2; process, generate, replace or delete geometry) Fragment shader (= fragment program) Shaders Compatibility profile: Default shaders are provided by OpenGL (fixed-function pipeline) Core profile: no default vertex or fragment shader; must be provided by the programmer Tessellation shaders, geometry shaders and compute shaders are optional Compute shader (OpenGL 4.3; general purpose) 11 12 2

Shader Variables Classification Attribute Information specific to each vertex/pixel passed to vertex/fragment shader Uniform Constant information passed to vertex/fragment shader Cannot be written to in a shader Out/in Info passed from vertex shader to fragment shader Interpolated from vertices to pixels Write in vertex shader, but only read in fragment shader Const To declare non-writable, constant variables Example: Vertex Color Example: Light Position Eye Position Example: pi, e, 0.480 Example: Vertex Color Texture Coords 13 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), the shading language of Direct3D All of these are simplified versions of C/C++ 14 GLSL The shading language of OpenGL Managed by OpenGL Architecture Review Board Introduced in OpenGL 2.0 We use shader version 1.50: #version 150 (a good version supporting the core profile features) Current shader version: 4.50 Vertex Shader Input: vertices, in object coordinates, and per-vertex attributes: color normal texture coordinates many more Output: vertex location in clip coordinates vertex color vertex normal many more are possible 15 16 Basic Vertex Shader in GLSL #version 150 position; // input position, in object coordinates in vec4 color; // input color out vec4 col; // output color uniform mat4 modelviewmatrix; // uniform variable to store the modelview mtx uniform mat4 projectionmatrix; // uniform variable to store the projection mtx Fragment Shader Input: pixels, and per-pixel attributes: color normal texture coordinates many more are possible Inputs are outputs from the vertex shader, interpolated (by the GPU) to the pixel location! void main() { // compute the transformed and projected vertex position (into gl_position) gl_position = projectionmatrix * modelviewmatrix * vec4(position, 1.0f); Output: // compute the vertex color (into col) pixel color col = color; depth value } 17 18 3

Basic Fragment Shader #version 150 in vec4 col; // input color (computed by the interpolator) out vec4 c; // output color (the final fragment color) void main() { // compute the final fragment color c = col; } Another Fragment Shader #version 150 in vec4 col; // input color (computed by the interpolator) out vec4 c; // output color (the final fragment color) void main() { // compute the final fragment color c = vec4(1.0, 0.0, 0.0, 1.0); } 19 20 Pipeline program Container for all the shaders Vertex, fragment, geometry, tessellation, compute Can have several pipeline programs (for example, one for each rendering style) Must have at least one (core profile) At any moment of time, exactly one pipeline program is bound (active) 21 Installing Pipeline Programs 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 22 Our helper library: PipelineProgram Our helper library: PipelineProgram // load shaders from a file int BuildShadersFromFiles(const char * filenamebasepath, const char * vertexshaderfilename, const char * fragmentshaderfilename, const char * geometryshaderfilename = NULL, const char * tessellationcontrolshaderfilename = NULL, const char * tessellationevaluationshaderfilename = NULL); // load shaders from a C text string int BuildShadersFromStrings(const char * vertexshadercode, const char * fragmentshadercode, const char * geometryshadercode = NULL, const char * tessellationcontrolshadercode = NULL, const char * tessellationevaluationshadercode = NULL); 23 24 4

Our helper library: PipelineProgram // global variable BasicPipelineProgram * pipelineprogram; // during initialization: pipelineprogram = new BasicPipelineProgram(); pipelineprogram->init(../openglhelper-startercode ); // before rendering, bind (activate) the pipeline program: pipelineprogram->bind(); If you want to change the pipeline program, call Bind on the new pipeline program 25 Setting up the Uniform Variables Uploading the modelview matrix transformation to the GPU (in the display function) // get a handle to the program GLuint program = pipelineprogram->getprogramhandle(); // get a handle to the modelviewmatrix shader variable GLint h_modelviewmatrix = glgetuniformlocation(program, modelviewmatrix ); float m[16]; // column-major // here, must fill m (use our OpenGLMatrix class) // (missing code to fill m) // upload m to the GPU GLboolean isrowmajor = GL_FALSE; gluniformmatrix4fv(h_modelviewmatrix, 1, isrowmajor, m); 26 Setting up the Uniform Variables Repeat the same process also for the projection matrix: // get a handle to the program GLuint program = pipelineprogram->getprogramhandle(); // get a handle to the projectionmatrix shader variable GLint h_projectionmatrix = glgetuniformlocation(program, projectionmatrix ); float p[16]; // column-major // here, must fill p (use our OpenGLMatrix class) // (missing code to fill p) Vertex Array Objects (VAOs) A container to collect the VBOs of each object VBO 1 VBO 2 position color VBO 3 VBO 4 position color shader variables // upload p to the GPU GLboolean isrowmajor = GL_FALSE; gluniformmatrix4fv(h_projectionmatrix, 1, isrowmajor, p); VAO 1 VAO 2 27 28 Vertex Array Objects (VAOs) A container to collect the VBOs of each object Usage is mandatory During initialization: create VBOs (one or more per object), create VAOs (one per object), and place the VBOs into the proper VAOs At render time: bind the VAO, then call gldrawarrays(), then unbind 29 Step 1: Create the VAO GLuint vao; glgenvertexarrays(1, &vao); glbindvertexarray(vao); // bind the VAO VAO currently empty VBO 1 gg5 53vsff&$#422424d^^3d VBO 2 x*reqrr$$fd02r2ccd%w position; in vec4 color; currently unorganized bytes shader variables currently unconnected to data 30 5

Step 2: Connect VBO to VAO and the shader variable, and interpret VBO VAO code ( position shader variable) During initialization: VAO VBO 1 gg5 53vs ff&$ #422 424d ^^3d vtx1 x vtx1 y vtx1 z vtx2 x vtx2 y vtx2 z position GLuint loc = glgetattriblocation(program, position ); glvertexattribpointer(loc, 3, GL_FLOAT, GL_FALSE, stride, offset); 31 GLuint vao; glgenvertexarrays(1, &vao); glbindvertexarray(vao); // bind the VAO // bind the VBO buffer (must be previously created) glbindbuffer(gl_array_buffer, buffer); // get location index of the position shader variable GLuint loc = glgetattriblocation(program, position ); glenablevertexattribarray(loc); // enable the position attribute const void * offset = (const void*) 0; GLsizei stride = 0; GLboolean normalized = GL_FALSE; // set the layout of the position attribute data glvertexattribpointer(loc, 3, GL_FLOAT, normalized, stride, offset); 32 VAO code ( color shader variable) Use the VAO // get the location index of the color shader variable loc = glgetattriblocation(program, color ); glenablevertexattribarray(loc); // enable the color attribute offset = (const void*) sizeof(positions); GLsizei stride = 0; Glboolean normalized = GL_FALSE; // set the layout of the color attribute data glvertexattribpointer(loc, 4, GL_FLOAT, normalized, stride, offset); glbindvertexarray(0); // unbind the VAO In the display function: pipelineprogram->bind(); // bind the pipeline program glbindvertexarray(vao); // bind the VAO GLint first = 0; GLsizei count = numvertices; gldrawarrays(gl_triangles, first, count); glbindvertexarray(0); // unbind the VAO 33 34 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 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; 35 36 6

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 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( 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; } 37 38 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 (lessthan, equal, etc.) Need to watch out for common reserved keywords Always use built-in functions, do not implement your own Some functions are not implemented on some cards GLSL: Built-in Variables Always prefaced with gl_ Accessible to both vertex and fragment shaders Examples: (input) gl_vertexid: index of currently processed vertex (input) gl_frontfacing: whether pixel is front facing or not (input) gl_fragcoord : x,y: coordinate of pixel, z: depth (output) gl_fragdepth: pixel depth 39 40 GLSL: Accessing OpenGL State (Compatibility Profile Only) Vertex shader: Have access to several vertex attributes: gl_color, gl_normal, gl_vertex, etc. Fragment shader: Write to special output variable: gl_fragcolor Uniform Variables Matrices (ModelViewMatrix, ProjectionMatrix, inverses, transposes) Materials (in MaterialParameters struct, ambient, diffuse, etc.) Lights (in LightSourceParameters struct, specular, position, etc.) Debugging Shaders More difficult than debugging C programs Common show-stoppers: Typos in shader source Assuming implicit type conversion Attempting to pass data to undeclared variables Very important to check error codes; use status functions like: glgetshaderiv(gluint shader, GLenum pname, GLint * params) Varying Variables FrontColor for colors TexCoord[] for texture coordinates These do not work in the core profile! 41 42 7

Summary Shading Languages Program Pipeline Vertex Array Objects GLSL Vertex Shader Fragment Shader 43 8