Shaders. Slide credit to Prof. Zwicker

Similar documents
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

Programming shaders & GPUs Christian Miller CS Fall 2011

COMP371 COMPUTER GRAPHICS

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

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

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

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

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

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

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

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

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

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

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

Introduction to Shaders.

Graphics Hardware. Instructor Stephen J. Guy

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

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

Lecture 2. Shaders, GLSL and GPGPU

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Programmable Graphics Hardware

The Rasterization Pipeline

CS 432 Interactive Computer Graphics

We assume that you are familiar with the following:

OPENGL RENDERING PIPELINE

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

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

Shaders (some slides taken from David M. course)

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

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

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

12.2 Programmable Graphics Hardware

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Graphics Hardware. Graphics Processing Unit (GPU) is a Subsidiary hardware. With massively multi-threaded many-core. Dedicated to 2D and 3D graphics

CS451Real-time Rendering Pipeline

CS427 Multicore Architecture and Parallel Computing

Supplement to Lecture 22

Rasterization Overview

CHAPTER 1 Graphics Systems and Models 3

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

2.11 Particle Systems

Computergrafik. Matthias Zwicker. Herbst 2010

Spring 2009 Prof. Hyesoon Kim

Mobile Application Programing: Android. OpenGL Operation

The Graphics Pipeline

Programmable Graphics Hardware

CS 4620 Program 3: Pipeline

Spring 2011 Prof. Hyesoon Kim

CMSC427 Transformations II: Viewing. Credit: some slides from Dr. Zwicker

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

Evolution of GPUs Chris Seitz

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

Programming Graphics Hardware

Today. Rendering algorithms. Rendering algorithms. Images. Images. Rendering Algorithms. Course overview Organization Introduction to ray tracing

Rendering Algorithms: Real-time indirect illumination. Spring 2010 Matthias Zwicker

Computer Graphics Coursework 1

Cg 2.0. Mark Kilgard

Introduction to Computer Graphics. Hardware Acceleration Review

The Graphics Pipeline and OpenGL I: Transformations!

Could you make the XNA functions yourself?

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

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

Programmable GPUS. Last Time? Reading for Today. Homework 4. Planar Shadows Projective Texture Shadows Shadow Maps Shadow Volumes

Zeyang Li Carnegie Mellon University

Lecture 13: OpenGL Shading Language (GLSL)

Deferred Rendering Due: Wednesday November 15 at 10pm

CS452/552; EE465/505. Image Processing Frame Buffer Objects

TSBK03 Screen-Space Ambient Occlusion

OPENGL AND GLSL. Computer Graphics

Rendering Objects. Need to transform all geometry then

Mattan Erez. The University of Texas at Austin

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Hardware Accelerated Volume Visualization. Leonid I. Dimitrov & Milos Sramek GMI Austrian Academy of Sciences

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

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

Models and Architectures

NVIDIA Parallel Nsight. Jeff Kiel

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

Today. Rendering algorithms. Rendering algorithms. Images. Images. Rendering Algorithms. Course overview Organization Introduction to ray tracing

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

Mobile Application Programing: Android. OpenGL Operation

CS559 Computer Graphics Fall 2015

INFOGR Computer Graphics

CS 179: GPU Programming

Ciril Bohak. - INTRODUCTION TO WEBGL

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

Computer Graphics (CS 543) Lecture 10: Soft Shadows (Maps and Volumes), Normal and Bump Mapping

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

CS GPU and GPGPU Programming Lecture 8+9: GPU Architecture 7+8. Markus Hadwiger, KAUST

BCA611 Video Oyunları için 3B Grafik

The Graphics Pipeline and OpenGL I: Transformations!

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

Transcription:

Shaders Slide credit to Prof. Zwicker

2 Today Shader programming

3 Complete model Blinn model with several light sources i diffuse specular ambient How is this implemented on the graphics processor (GPU)? Shader programming!

4 Programmable pipeline Functionality in parts (grey boxes) of the GPU pipeline specified by user programs Called shaders, or shader programs, executed on GPU Not all functionality in the pipeline is programmable GPU Scene data Vertex processing, modeling and viewing transformation Projection Rasterization, fragment processing, visibility Image

5 Shader programs Written in a shading language Examples Cg, early shading language by NVidia OpenGL s shading language GLSL http://en.wikipedia.org/wiki/glsl DirectX shading language HLSL (high level shading language) http://en.wikipedia.org/wiki/high_level_shader_language RenderMan shading language (film production) All similar to C, with specialties Recent, quickly changing technology Driven by more and more flexible GPUs

Programmable pipeline (2006) Two types of shader programs 1. Vertex program GPU Scene data Vertex processing, modeling and viewing transformation Projection 2. Fragment program (fragment: pixel location inside a triangle and interpolated data) Rasterization, fragment processing, visibility Image 6

7 GPU architecture (2006) Pipeline GPU Architecture NVidia NV80 (GeForce 8800 Series) 128 functional units, stream processors http://arstechnica.com/news.ars/post/20061108-8182.html

GPU architecture (2014) Similar, but more processors (2048 ) http://hexus.net/tech/reviews/graphics/74849-nvidia-geforce-gtx-980-28nm-maxwell/ 8

GPU architecture (2016) Similar, but even more processors (3840 ) https://devblogs.nvidia.com/parallelforall/inside-pascal/ 9

Parallelism Task parallelism http://en.wikipedia.org/wiki/task_parallelism Processor performs different threads (sequences of instructions) simultaneously Multi-core CPUs Data parallelism http://en.wikipedia.org/wiki/data_parallelism Processors performs same thread of instructions on different data elements Single Instruction Multiple Data (SIMD) GPUs 10

Parallelism GPUs up to now exploit mostly data parallelism Perform same thread of operations (same shader) on multiple vertices and pixels independently Massive parallelism (same operation on many vertices, pixels) enables massive number of operations per second Currently: hundreds of parallel operations at several hundred megahertz Detailed description of Nvidia Kepler architecture http://www.geforce.com/active/en_us/en_us/pdf/geforce-gtx-680-whitepaper-final.pdf 11

12 Still fixed functionality (2014) Hardcoded in hardware Projective division Rasterization I.e., determine which pixels lie inside triangle Vertex attribute interpolation (color, texture coords.) Access to framebuffer Z-buffering Texture filtering Framebuffer blending

13 Shader programming Each shader (vertex or fragment) is a separate piece of code in a shading language (e.g. GLSL) Vertex shader Executed automatically for each vertex and its attributes (color, normal, texture coordinates) flowing down the pipeline Type and number of output variables of vertex shader are user defined Every vertex produces same type of output Output interpolated automatically at each fragment and accessible as input to fragment shader Fragment shader Executed automatically for each fragment (pixel) being touched by rasterizer Output (fragment color) is written to framebuffer

14 Shader programming Shaders are activated/deactivated by host program (Java, C++, ) Can have different shaders to render different parts of a scene Shader programs can use additional variables set by user Modelview and projection matrices Light sources Textures Etc. Variables are passed by host (Java, C++) program to shader In jrtr via jogl, see class jrtr.glrendercontext Learn OpenGL details from example code, then (advanced) reference books, e.g. http://www.opengl.org/registry/doc/glspec40.core.20100311.pdf

Vertex programs Executed once for every vertex Or: every vertex is processed by same vertex program that is currently active Implements functionality for Modelview, projection transformation (required!) Per-vertex shading Vertex shader often used for animation Characters Particle systems 15

16 Fragment programs Executed once for every fragment Or: Every fragment is processed by same fragment program that is currently active Implements functionality for Output color to framebuffer Texturing Per-pixel shading Bump mapping Shadows Etc.

Creating shaders in OpenGL Sequence of OpenGL API calls to load, compile, link, activate shaders Mostly taken care of in Shader.java Input is a string that contains shader program String usually read from file Separate files for fragment and vertex shaders [OpenGL programming guide] 17

Creating shaders in OpenGL You can switch between different shaders during runtime of your application Setup several shaders as shown before Call gluseprogram(s) whenever you want to render using a certain shader s Shader is active until you call gluseprogram with a different shader In jrtr, this functionality is encapsulated in the Shader class 18

Vertex programs Vertices with attributes storage classifier in Coordinates in object space, additional vertex attributes Uniform parameters storage classifier uniform OpenGL state, application specified parameters Vertex program From application To rasterizer Output storage classifier out Transformed vertices, processed vertex attributes 19

20 Hello world vertex program main() function is executed for every vertex Three storage classifiers: in, out, uniform in vec4 position; // position, vertex attribute uniform mat4 projection; // projection matrix, set by host (Java) uniform mat4 modelview; // modelview matrix, set by host (Java) void main() { gl_position = projection * modelview * position; } // required, predefined output variable // apply projection matrix // apply modelview matrix // vertex position

21 Vertex attributes Data that flows down the pipeline with each vertex Per-vertex data that your application sends to rendering pipeline E.g., vertex position, color, normal, texture coordinates Declared using in storage classifier in your shader code Read-only

Vertex attributes Application needs to tell OpenGL which vertex attributes are mapped to which in variables In host (Java) program, sequence of calls glgenbuffers // Get reference to OpenGL buffer object glbindbuffer // Activate buffer object glbufferdata // Write data into buffer glgetattriblocation // Get reference of uniform variable // in shader glvertexattribpointer // Link buffer object with uniform // shader variable glenablevertexattribarray // Enable the link Details see GLRenderContext.draw No need to modify it 22

Uniform parameters Parameters that are set by the application, but do not change on a pervertex basis! Transformation matrices, parameters of light sources, textures Will be the same for each vertex until application changes it again Declared using uniform storage classifier in vertex shader Read-only 23

24 Uniform parameters To set parameters, use glgetuniformlocation, gluniform* in application After shader is active, before rendering Example In shader declare uniform float a; In application, set a using GLuint p; // initialize program p int i=glgetuniformlocation(p, a ); gluniform1f(i, 1.f);

25 Output variables Required, predefined output variable: homogeneous vertex coordinates vec4 gl_position Additional user defined outputs Mechanism to send data to the fragment shader Will be interpolated during rasterization Interpolated values accessible in fragment shader (using same variable names) Storage classifier out

Limitations (2014) Cannot write data to any memory accessible directly by application (Java, C++, etc.) Cannot pass data between vertices Each vertex is independent One vertex in, one vertex out Cannot generate new geometry Note: Geometry shaders (not discussed here) can do this 26

27 Examples Animation Offload as much as possible to the GPU Character skinning Particle systems Water Character skinning http://www.youtube.com/watch?v=on4h3s-w0ny

Fragment programs Uniform parameters storage classifier uniform OpenGL state, application specified parameters Fragment data storage classifier in Interpolated vertex attributes, additional fragment attributes Fragment program Output storage classifier out Fragment color, depth From rasterizer To fixed framebuffer access functionality (z-buffering, etc.) 28

29 Fragment data Change for each execution of the fragment program Interpolated from vertex output during rasterization Fragment color, texture coordinates, etc. Declared as in variables Need to have same variable name as output (declared as out) of vertex shader

30 Uniform parameters Work same as in vertex shader Typically transformation matrices, parameters of light sources, textures Pass from host application via glgetuniformlocation, gluniform*

31 Output variables Typically fragment color Declared as out Will be written to frame buffer (i.e., output image) automatically

32 Hello world fragment program main() function is executed for every fragment Draws everything in bluish color out vec4 fragcolor; void main() { fragcolor = vec4(0.4,0.4,0.8,1.0); }

Examples Per pixel shading as discussed in class Bump mapping Displacement mapping Realistic reflection models Cartoon shading Shadows Etc. Most often, vertex and fragment shader work together to achieve desired effect 33

Limitations (2014) Cannot read framebuffer Current pixel color, depth, etc. Can only write to framebuffer pixel that corresponds to fragment being processed No random write access to framebuffer Number of variables passed from vertex to fragment shader is limited Number of application defined uniform parameters is limited 34

35 GLSL built in functions and data types See OpenGL/GLSL quick reference card http://www.khronos.org/files/opengl-quick-reference-card.pdf Matrices, vectors, textures Matrix, vector operations Trigonometric functions Geometric functions on vectors Texture lookup

Summary Shader programs specify functionality of parts of the rendering pipeline Written in special shading language (GLSL in OpenGL) Sequence of OpenGL calls to compile/activate shaders Several types of shaders, discussed here: Vertex shaders Fragment shaders 36

37 GLSL main features Similar to C, with specialties Most important: in, out, uniform storage classifiers Parameters of shader (uniform variables) passed from host application via specific API calls Built in vector data types, vector operations No pointers, classes, inheritance, etc.

Debugging shaders No direct way to debug (setting breakpoints, inspecting values) Practical technique Render intermediate steps of your shader Color code information that you want to see (e.g, paint pixel a specific color if you reach certain part of shader code) Forum discussions http://stackoverflow.com/questions/2508818/how-to-debug-a-glsl-shader 38

39 Tutorials and documentation OpenGL and GLSL specifications http://www.opengl.org/documentation/specs/ OpenGL/GLSL quick reference card http://www.khronos.org/files/opengl-quick-reference-card.pdf Learn from example code and use the Ilias forum!

GPGPU programming General purpose GPU programming Special GPU programming languages CUDA http://en.wikipedia.org/wiki/cuda OpenCL http://en.wikipedia.org/wiki/opencl Exploit data parallelism SIMT (single instruction multiple threads) programming model Each thread has unique ID Each thread operates on single data item (as opposed to vector of data items in SIMD) Data items accessed via thread ID 40

41 A note on transforming normals If object-to-camera transformation includes shearing, transforming normals using does not work Transformed normals are not perpendicular to surface any more To avoid problem, need to transform normals by No derivation here, but remember for rotations