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

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

Programmable Graphics Hardware

12.2 Programmable Graphics Hardware

OPENGL RENDERING PIPELINE

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

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

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

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

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

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

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

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

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

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

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

2D graphics with WebGL

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

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

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

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

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

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

Mobile Application Programing: Android. OpenGL Operation

Programming Graphics Hardware

The Graphics Pipeline and OpenGL I: Transformations!

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

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

The Graphics Pipeline and OpenGL IV: Stereo Rendering, Depth of Field Rendering, Multi-pass Rendering!

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

The Graphics Pipeline and OpenGL I: Transformations!

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

Sign up for crits! Announcments

Lecture 09: Shaders (Part 1)

Computer Graphics Coursework 1

Introduction to Shaders.

Lecture 11 Shaders and WebGL. October 8, 2015

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

Supplement to Lecture 22

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Shaders (some slides taken from David M. course)

Shaders. Slide credit to Prof. Zwicker

Programming shaders & GPUs Christian Miller CS Fall 2011

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

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Mobile Application Programing: Android. OpenGL Operation

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

GLSL Programming. Nicolas Holzschuch

The Graphics Pipeline

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

BCA611 Video Oyunları için 3B Grafik

Programmable Graphics Hardware

Introduction to GLSL. Announcements. Agenda. Course Contents. Patrick Cozzi University of Pennsylvania CIS Fall 2012

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

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

OpenGL Programmable Shaders

The Graphics Pipeline

CS 432 Interactive Computer Graphics

OpenGL shaders and programming models that provide object persistence

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

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

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

The OpenGL Shading Language

Introduction to Programmable GPUs CPSC 314. Real Time Graphics

Basics of GPU-Based Programming

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

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

Mobile Application Programming: Android. OpenGL Operation

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

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

INFOGR Computer Graphics

Introduction to the OpenGL Shading Language (GLSL)

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

Shading Language Update

Lecture 2. Shaders, GLSL and GPGPU

COMP371 COMPUTER GRAPHICS

Introduction to Computer Graphics. Hardware Acceleration Review

Introduction to Programmable GPUs CPSC 314. Introduction to GPU Programming CS314 Gordon Wetzstein, 09/03/09

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

CS179: GPU Programming

To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Computer Graphics

CS559 Computer Graphics Fall 2015

Mobile graphics API Overview

1 of 5 9/10/ :11 PM

Lecture 13: OpenGL Shading Language (GLSL)

INFOGR Computer Graphics

Lighting and Shading II. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

CS 130 Final. Fall 2015

SUMMARY. CS380: Introduction to Computer Graphics Texture Mapping Chapter 15. Min H. Kim KAIST School of Computing 18/05/03.

Introduction to the OpenGL Shading Language

Computer Graphics (CS 543) Lecture 8a: Per-Vertex lighting, Shading and Per-Fragment lighting

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

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

CS452/552; EE465/505. Intro to Lighting

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

To Do. Demo for mytest3. Methodology for Lecture. Importance of Lighting. Brief primer on Color. Foundations of Computer Graphics (Spring 2012)

CS475/CS675 - Computer Graphics. OpenGL Drawing

Computergraphics Exercise 15/ Shading & Texturing

Lighting and Texturing

Understanding Shaders and WebGL. Chris Dalton & Olli Etuaho

Transcription:

! The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)! Gordon Wetzstein! Stanford University! EE 267 Virtual Reality! Lecture 4! stanford.edu/class/ee267/!

Lecture Overview! Review of graphics pipeline! vertex and fragment shaders! OpenGL Shading Language (GLSL 1.10)! Implementing lighting & shading with GLSL vertex and fragment shaders!!

Reminder: The Graphics Pipeline! https://www.ntu.edu.sg/home/ehchua/programming/opengl/cg_basicstheory.html!

Reminder: The Graphics Pipeline! vertex shader! fragment shader! transforms! (per-vertex) lighting!! texturing! (per-fragment) lighting!! https://www.ntu.edu.sg/home/ehchua/programming/opengl/cg_basicstheory.html!

Reminder: The Graphics Pipeline! The Rasterizer! Two goals:! 1. determine which fragments are inside the primitives (triangles) and which ones aren t! 2. interpolate per-vertex attributes (color, texture coordinates, normals, ) to each fragment in the primitive!! https://www.ntu.edu.sg/home/ehchua/programming/opengl/cg_basicstheory.html!

Vertex Shaders! input! vertex position, normal, color, material, texture coordinates! modelview matrix, projection matrix, normal matrix!! vertex shader (executed for each vertex in parallel)! void main () { // do something here https://www.ntu.edu.sg/home/ehchua/programming/opengl/cg_basicstheory.html! output! transformed vertex position (in clip! coords), texture coordinates!

Fragment Shaders! input! vertex position in window coords,! texture coordinates! fragment shader (executed for each fragment in parallel)! void main () { // do something here output! fragment color! fragment depth!! https://www.ntu.edu.sg/home/ehchua/programming/opengl/cg_basicstheory.html!

Why Do We Need Shaders?! massively parallel computing! single instruction multiple data (SIMD) paradigm à GPUs are designed to be parallel processors! vertex shaders are independently executed for each vertex on GPU (in parallel)! fragment shaders are independently executed for each fragment on GPU (in parallel)!

! Why Do We Need Shaders?! most important: vertex transforms and lighting & shading calculations! shading: how to compute color of each fragment (e.g. interpolate colors)! 1. Flat shading! 2. Gouraud shading (per-vertex shading)! 3. Phong shading (per-fragment shading)! other: render motion blur, depth of field, physical simulation,! courtesy: Intergraph Computer Systems!

!!!! Shading Languages! Cg (C for Graphics NVIDIA, deprecated)! GLSL (GL Shading Language OpenGL)! EE 267! HLSL (High Level Shading Language - MS Direct3D)!

Demo Simple Vertex Shader! // variable passed in from JavaScript / three.js uniform float deformation; attribute vec3 position; attribute vec3 normal; uniform mat4 projectionmat; uniform mat4 modelviewmat; void main () // vertex shader { // deform vertex position in object coordinates vec3 pos = position + deformation * normal; // convert to clip space gl_position = projectionmat*modelviewmat*vec4(pos,1.0); // do lighting calculations here (in view space)

Demo Simple Fragment Shader! // variables passed in from JavaScript / three.js varying vec2 texturecoords; uniform sampler2d texture; uniform float gamma; void main () // fragment shader { // texture lookup vec3 texturecolor = texture2d(texture, texturecoords).rgb; // set output color by applying gamma gl_fragcolor.rgb = pow(texturecolor.rgb, gamma);

Demo Vertex+Fragment Shader! // variable to be passed from vertex to fragment shader varying vec4 mycolor; // variable passed in from JavaScript / three.js uniform mat4 projectionmat; uniform mat4 modelviewmat; uniform mat3 normalmat; vertex shader! attribute vec3 position; attribute vec3 normal; void main () // vertex shader Gouraud shading { // transform position to clip space gl_position = projectionmat * modelviewmat * vec4(position,1.0); // transform position to view space vec4 positionview = modelviewmat * vec4(position,1.0); // transform normal into view space vec3 normalview = normalmat * normal; // do lighting calculations here (in view space) mycolor = fragment! shader! // variable to be passed from vertex to fragment shader varying vec4 mycolor; void main () // fragment shader - Gouraud shading { gl_fragcolor = mycolor;

Demo Vertex+Fragment Shader! // variable to be passed from vertex to fragment shader varying vec4 mypos; varying vec3 mynormal; // variable passed in from JavaScript / three.js uniform mat4 projectionmat; uniform mat4 modelviewmat; uniform mat3 normalmat; vertex shader! attribute vec3 position; attribute vec3 normal; void main () // vertex shader Phong shading { // transform position to clip space gl_position = projectionmat * modelviewmat * vec4(position,1.0); // transform position to view space mypos = modelviewmat * vec4(position,1.0); // transform normal into view space mynormal = normalmat * normal; fragment! shader! // variable to be passed from vertex to fragment shader varying vec4 mypos; varying vec3 mynormal; void main () // fragment shader - Phong shading { // do lighting calculations here... gl_fragcolor = ;

Demo General Purpose Computation Shader! varying vec2 texturecoords; // variables passed in from JavaScript / three.js uniform sampler2d tex; const float timestep = 1.0; void main () // fragment shader { // texture lookups float u = texture2d(tex, texturecoords).r; float u_xp1 = texture2d(tex, float2(texturecoords.x+1,texturecoords.y)).r; float u_xm1 = texture2d(tex, float2(texturecoords.x-1,texturecoords.y)).r; float u_yp1 = texture2d(tex, float2(texturecoords.x,texturecoords.y+1)).r; float u_ym1 = texture2d(tex, float2(texturecoords.x,texturecoords.y-1)).r; glfragcolor.r = u + timestep*(u_xp1+u_xm1+u_yp1+u_ym1-4*u); u heat equation:! t = α 2 u u ( t+1) = Δ t α 2 u + u ( t)

! OpenGL Shading Language (GLSL)! high-level programming language for shaders! syntax similar to C (i.e. has main function and many other similarities)! usually very short programs that are executed in parallel on GPU! good introduction / tutorial:! https://www.opengl.org/sdk/docs/tutorials/typhoonlabs/!

OpenGL Shading Language (GLSL)! versions of OpenGL, WebGL, GLSL can get confusing! here s what we use:! WebGL 1.0 - based on OpenGL ES 2.0; cheat sheet:! https://www.khronos.org/files/webgl/webgl-reference-card-1_0.pdf! GLSL 1.10 - shader preprocessor: #version 110 reason: three.js doesn t support WebGL 2.0 yet!

GLSL Vertex Shader Input/Output!! input! input variables are either uniform (passed in from JavaScript, e.g. matrices) or attribute (values associated with each vertex, e.g. position, normal, uv, ) built-in output! vec4 gl_position vertex position in clip coordinates

GLSL Fragment Shader Input/Output! input! input variables are either uniform (passed in from JavaScript) or varying (passed in from vertex shader through rasterizer) built-in output! vec4 gl_fragcolor fragment color! float gl_fragdepth value written to depth buffer, if not specified: gl_fragcoord.z (only available in OpenGL but not WebGL)!

GLSL Shader! Uniforms! Uniforms! Samplers! Attribute 0! Varying 0! Varying 0! Attribute 1! Attribute 2! Attribute 3! Attribute 4! Attribute 5! Attribute 6! Attribute 7! Vertex Shader! Temporary Variables! Varying 1! Varying 2! Varying 3! Varying 4! Varying 5! Varying 6! Varying 7! gl_position!! rasterizer! Varying 1! Varying 2! Varying 3! Varying 4! Varying 5! Varying 6! Varying 7! gl_fragcoord!! Fragment Shader! Temporary Variables! gl_fragcolor!

GLSL Shader! Uniforms! all of these values will be interpolated over the primitive by rasterizer! Uniforms! Samplers! Attribute 0! Varying 0! Attribute 1! Attribute 2! Varying 1! Varying 2! Attribute 3! Attribute 4! Attribute 5! Attribute 6! Vertex Shader! Varying 3! Varying 4! Varying 5! Varying 6! Fragment Shader! gl_fragcolor! Attribute 7! Varying 7! Temporary Variables! gl_position!! gl_fragcoord!! Temporary Variables!

! GLSL Data Types! bool int float ivec2, ivec3, ivec4 vec2, vec3, vec4 mat2, mat3, mat4 sampler2d attribute! boolean (true or false)! signed integer! 32 bit floating point! integer vector with 2, 3, or 4 elements! floating point vector with 2, 3, or 4 elements! floating point matrix with 2x2, 3x3, or 4x4 elements! handle to a 2D texture! per-vertex attribute, such as position, normal, uv,!

! GLSL Data Types! uniform type read-only values passed in from JavaScript, e.g. uniform float or uniform sampler2d vertex shader! fragment shader! uniform mat4 modelviewprojectionmatrix; varying texturecoords; attribute vec3 position; attribute vec2 uv; void main () { gl_position = modelviewprojectionmatrix * vec4(position, 1.0); texturecoords = uv; uniform sampler2d texture; varying texturecoords; void main () { gl_fragcolor = texture2d(texture, texturecoords);

! GLSL Data Types! varying type variables that are passed from vertex to fragment shader (i.e. write-only in vertex shader, read-only in fragment shader)! rasterizer interpolates these values in between shaders! vertex shader! fragment shader! varying float myvalue; uniform mat4 modelviewprojectionmatrix; attribute vec3 position; varying float myvalue; void main () { void main () { gl_position = modelviewprojectionmatrix * vec4(position,1.0); myvalue = 3.14159 / 10.0; gl_fragcolor = vec4(myvalue, myvalue, myvalue, 1.0);

GLSL Simplest (pass-through) Vertex Shader! // variable passed in from JavaScript / three.js uniform mat4 modelviewprojectionmatrix; // vertex positions are parsed as attributes attribute vec3 position; void main () // vertex shader { // transform position to clip space // this is similar to gl_position = ftransform(); gl_position = modelviewprojectionmatrix * vec4(position,1.0);

GLSL Simplest Fragment Shader! void main () // fragment shader { // set same color for each fragment gl_fragcolor = vec4(1.0,0.0,0.0,1.0);

GLSL built-in functions! dot dot product between two vectors! cross cross product between two vectors! texture2d texture lookup (get color value of texture at some tex coords)! normalize normalize a vector! clamp clamp a scalar to some range (e.g., 0 to 1)! radians, degrees, sin, cos, tan, asin, acos, atan, pow, exp, log, exp2, log2, sqrt, abs, sign, floor, ceil, mod, min, max, length, good summary of OpenGL ES (WebGL) shader functions:! https://www.khronos.org/files/webgl/webgl-reference-card-1_0.pdf!

Gouraud Shading with GLSL (only diffuse part) Vertex Shader! uniform vec3 lightpositionview; uniform vec3 lightcolor; uniform vec3 diffusematerial; user-defined light & material properties! uniform mat4 projectionmat; uniform mat4 modelviewmat; uniform mat3 normalmat; attribute vec3 position; attribute vec3 normal; user-defined transformation matrices! per-vertex attributes! varying vec3 vcolor; void main () // vertex shader { // transform position to clip space gl_position = projectionmat * modelviewmat * vec4(position,1.0); // transform vertex position, normal, and light position to view space vec3 P = vec3 L = vec3 N = // compute the diffuse term here float diffusefactor = // set output color vcolor = diffusefactor * diffusematerial * lightcolor; color computed by Phong lighting model to be interpolated by rasterizer!

Gouraud Shading with GLSL (only diffuse part) Fragment Shader! varying vec3 vcolor; void main () // fragment shader { // set output color gl_fragcolor = vec4(vcolor,1.0);

Phong Shading with GLSL (only diffuse part) Vertex Shader! uniform mat4 modelviewmat; uniform mat4 projectionmat; uniform mat3 normalmat; user-defined transformation matrices! attribute vec3 position; attribute vec3 normal; per-vertex attributes! varying vec3 vposition; varying vec3 vnormal; void main () // vertex shader { // transform position to clip space gl_position = projectionmat * modelviewmat * vec3(position,1.0); // transform vertex position, normal, and light position to view space vec3 P = vec3 N = // set output texture coordinate to vertex position in world coords vposition = P; // set output color to vertex normal direction vnormal = N; vertex position & normal to be interpolated by rasterizer!

Phong Shading with GLSL (only diffuse part) Fragment Shader! uniform vec3 lightcolor; uniform vec3 diffusematerial; uniform vec3 lightpositionworld; user-defined light & material properties! varying vec3 vposition; varying vec3 vnormal; vertex & normal positions interpolated to each fragment by rasterizer! void main () // fragment shader { // incoming color is interpolated by rasterizer over primitives! vec3 N = vnormal; // vector pointing to light source vec3 L = // compute the diffuse term float diffusefactor // set output color gl_fragcolor.rgb = diffusefactor * diffusematerial * lightcolor;

! GLSL - Misc! swizzling:! vec4 myvector1; vec4 myvector2; vec3 myvector1.xxy + myvector2.zxy; matrices are column-major ordering! initialize vectors in any of the following ways:! vec4 myvector = vec4(1.0, 2.0, 3.0, 4.0); vec4 myvector2 = vec4(vec2(1.0, 2.0), 3.0, 4.0); vec4 myvector3 = vec4(vec3(1.0, 2.0, 3.0), 4.0); these are equivalent:! myvector.xyzw = myvector.rgba we omitted a lot of details

JavaScript & GLSL! goals:! loading, compiling, and linking GLSL shaders (from a file) using JavaScript! activating and deactivate GLSL shaders in JavaScript! accessing uniforms from JavaScript! our approach (for labs and homeworks):! use three.js to handle all of the above! can do manually, but more work we will shield this from you!

Summary! GLSL is your language for writing vertex and fragment shaders! each shader is independently executed for each vertex/fragment on the GPU! usually require both vertex and fragment shader, but can pass-through data!

Further Reading! GLSL tutorial: https://www.opengl.org/sdk/docs/tutorials/typhoonlabs/! summary of built-in GLSL functions: http://www.shaderific.com/glsl-functions/! GLSL and WebGL: https://webglfundamentals.org/webgl/lessons/webgl-shadersand-glsl.html!