OpenGL Programmable Shaders

Similar documents
Programmable GPUs Outline

OpenGL Rendering Pipeline and Programmable Shaders

OpenGL Rendering Pipeline and Programmable Shaders

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

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

Programming shaders & GPUs Christian Miller CS Fall 2011

Supplement to Lecture 22

Programmable Graphics Hardware

12.2 Programmable Graphics Hardware

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

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

Introduction to Shaders.

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

Programmable Graphics Hardware

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

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

Grafica Computazionale: Lezione 30. Grafica Computazionale. Hiding complexity... ;) Introduction to OpenGL. lezione30 Introduction to OpenGL

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

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

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

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

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

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

Could you make the XNA functions yourself?

Pipeline Operations. CS 4620 Lecture 14

Lecture 2. Shaders, GLSL and GPGPU

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

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

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

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

CS451Real-time Rendering Pipeline

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Rasterization Overview

CSE4030 Introduction to Computer Graphics

The Application Stage. The Game Loop, Resource Management and Renderer Design

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

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

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

Why modern versions of OpenGL should be used Some useful API commands and extensions

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

Graphics Processing Unit Architecture (GPU Arch)

Shaders. Slide credit to Prof. Zwicker

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

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

Tutorial on GPU Programming #2. Joong-Youn Lee Supercomputing Center, KISTI

GPU Programming EE Final Examination

GPU Programming EE Final Examination

Cornell University CS 569: Interactive Computer Graphics. Introduction. Lecture 1. [John C. Stone, UIUC] NASA. University of Calgary

Copyright Khronos Group, Page Graphic Remedy. All Rights Reserved

GPU Programming EE Final Examination

The Graphics Pipeline

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

Next-Generation Graphics on Larrabee. Tim Foley Intel Corp

Spring 2009 Prof. Hyesoon Kim

Models and Architectures

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

GPU Programming EE Midterm Examination

GPU Memory Model. Adapted from:

Lecture 13: OpenGL Shading Language (GLSL)

Introduction to Computer Graphics with WebGL

CS 498 VR. Lecture 19-4/9/18. go.illinois.edu/vrlect19

Rendering Objects. Need to transform all geometry then

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

CS 4620 Program 3: Pipeline

Shading Language Update

GPU Programming EE Final Examination

COMP371 COMPUTER GRAPHICS

Spring 2011 Prof. Hyesoon Kim

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

CS 432 Interactive Computer Graphics

GPU Programming EE Final Examination

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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 GPU and GPGPU Programming Lecture 3: GPU Architecture 2. Markus Hadwiger, KAUST

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

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

Computer Graphics Coursework 1

EE 4702 GPU Programming

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak

- Rasterization. Geometry. Scan Conversion. Rasterization

Shaders (some slides taken from David M. course)

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

Introduction to the OpenGL Shading Language

Advanced Rendering Techniques

Lecture 09: Shaders (Part 1)

Drawing Fast The Graphics Pipeline

CS 130 Final. Fall 2015

1 of 5 9/10/ :11 PM

Comparing Reyes and OpenGL on a Stream Architecture

2.11 Particle Systems

Graphics Hardware. Instructor Stephen J. Guy

CS130 : Computer Graphics. Tamar Shinar Computer Science & Engineering UC Riverside

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

General Purpose Computation (CAD/CAM/CAE) on the GPU (a.k.a. Topics in Manufacturing)

Shader Series Primer: Fundamentals of the Programmable Pipeline in XNA Game Studio Express

Transcription:

h gpup 1 Topics Rendering Pipeline Shader Types OpenGL Programmable Shaders sh gpup 1 OpenGL Shader Language Basics h gpup 1 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

h gpup 2 References OpenGL Shading Language References sh gpup 2 John Kessenich, The OpenGL Shading Language, OpenGL Language Version 4.20, Document Revision 6, August 2011. OpenGL Commands for Shader Language Control Mark Segal, Kurt Akeley, The OpenGL Graphics System: A Specification (Version 4.3), OpenGL, August 2012. h gpup 2 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 2

h gpup 3 sh gpup 3 Pipeline: An organization for software and hardware in which a fixed sequence of operations is carried out on data items... Rendering Pipeline: An organization for the set of steps needed to convert a set of vertices into a frame buffer image. h gpup 3 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 3

h gpup 4 sh gpup 4 Simplified OpenGL Rendering Pipeline 1 Vtx v Vertex Shader 1 Vtx v 1 Tri v 1 v 2 v 3 Geometry Shader >= 0 Tri v 1 v 2 v 3 v 1 v 2 v 3 1 Tri v 1 v 2 v 3 Rasterizer >> 1 Frag f f f f f f f f f f f f f f f f 1 Frag f Fragment Shader 0 or 1 Frag f 1 Frag f Frame Buffer Update h gpup 4 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 4

h gpup 5 OpenGL Shaders sh gpup 5 Shader: A program that performs certain rendering pipeline steps. h gpup 5 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 5

h gpup 6 Preparation Major Rendering Pipeline Steps sh gpup 6 These activities are performed before invoking pipeline. CPU specifies transforms, material properties, etc. Calling, say, gltranslatef, helps set up pipeline...... but does not start it running or feed it data. Feed Data to Pipeline Data enters in a unit including a vertex and its attributes. This initiates the steps. h gpup 6 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 6

h gpup 7 sh gpup 7 Vertex Processing Steps (By GPU for each vertex.) Apply modelview transform to vertex. Main result is vertex coordinate in eye space. Compute lighted color of vertex. Main result is lighted color. Apply projection transform to eye-space vertex. Result is vertex coordinate in clip space. h gpup 7 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 7

h gpup 8 sh gpup 8 Primitive Assembly Steps These steps operate on a primitive (a group of primitives). Primitive Assembly (Group vertices into a primitive). Result is, say, a group of 3 describing a triangle. Clip (remove) off-screen parts of primitive. Result is fewer and maybe different primitives. Rasterize Result is the set of fragments (fb locations) covered by primitive. h gpup 8 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 8

h gpup 9 sh gpup 9 Fragment Processing Steps These steps operate on a fragment. Fetch texels, filter and blend. Result is a frame-buffer ready color. Frame Buffer Update If fragment passes depth and other tests, write or blend. h gpup 9 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 9

gpup 10 Programmable Units sh gpup 1 Programmable Unit: Part of the pipeline that can be programmed (as defined by some API). Choice of what is and isn t programmable constrained by: Need to allow for parallel (multithreaded, SIMD, MIMD) execution. Simple memory access. gpup 10 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 11 sh gpup 1 Major OpenGL Programmable Units Vertex Processor: Transform vertex and texture coordinates, compute lighting. Geometry Processor: Using a transformed primitive and its neighbors generates new primitives. For example, replace one triangle with many triangles to more closely match a curved surface. Fragment Processor: Using interpolated coordinates, read filtered texels and combine with colors. gpup 11 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 12 Languages sh gpup 1 Shader: A programmable part of a GPU. Name is now misleading but is still in common use. Shader Language: An language for programming shaders. gpup 12 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 13 High-Level Shader Languages sh gpup 1 High-Level Shader Languages OpenGL Shader Language OpenGL standard. Syntax very similar to C. Language designed for vertex and fragment shaders. Cg Originated with ATI, adopted in Direct3D. Syntax very similar to C. Language designed for stream programs...... geometry, vertex, and fragment programs can be in stream form. gpup 13 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 14 OpenGL Shader Language (OGSL) sh gpup 1 OpenGL Shader Language Important Features C-like CPP-like preprocessor directives. Library of useful geometry functions. Includes vector and matrix types and operators. gpup 14 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 15 Example OGSL Data Types Example sh gpup 1 vec4 vertex_e = gl_modelviewmatrix * o_point; vec3 norm_e = gl_normalmatrix * gl_normal; vec4 light_pos = gl_lightsource[1].position; float phase_light = dot(norm_e, normalize(light_pos - vertex_e).xyz); float phase_user = dot(norm_e, -vertex_e.xyz); float phase = sign(phase_light) == sign(phase_user)? abs(phase_light) : 0.0; gpup 15 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 16 Storage Qualifiers OGSL Storage Qualifiers sh gpup 1 Used in a variable declaration, specifies where data stored. Below, in, uniform, constant, and out are storage qualifiers. in vec4 force; uniform float x; const int sides = 5; out vec2 nudge; // Input to this shader, different for each primitive. // Input to shader, value rarely changed. // Can never be changed. // Output of this shader (input to some other). gpup 16 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 17 sh gpup 1 Storage Qualifier Types uniform: Read-only by shader. Written by client, change is time consuming. Typical use: transformation matrices. in: Input to shader. Read-only by shader that made the in declaration. Value is set either by client (using glvertexattrib and friends) or by a prior stage shader (by writing an out variable. Typical uses: vertex material properties (color), normal. out: Output of shader. Value is written by shader in which out declaration appears and read by shader in subsequent stage. sampler: Read-only by vertex and fragment shader. A handle to a texture unit, used by texture access functions. gpup 17 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 18 sh gpup 1 Interpolation Qualifiers Used for fragment shader inputs. Specify how value should be interpolated. flat: No interpolation. smooth: Perspective-correct interpolation. noperspective: Linear interpolation. gpup 18 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 19 sh gpup 1 Deprecated Storage Qualifiers. These were used in earlier versions of OGSL. They have been replaced by in and out. attribute: Deprecated. Like an in but only can be used for vertex shader. varying: Deprecated. When used in a vertex shader is the same as out, when used in a fragment shader is the same as in. gpup 19 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 1

gpup 20 sh gpup 2 Storage Qualifier Example // For vertex and fragment shaders: uniform vec3 gravity_force; uniform float gs_constant; uniform vec2 ball_size; // Vertex Shader Only in float step_last_time; in vec4 position_left, position_right, position_above, position_below; in vec3 ball_speed; out vec4 out_position; out vec3 out_velocity; // Fragment Shader Only // in vec4 out_position; in vec3 out_velocity; gpup 20 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 2

gpup 21 Function Parameters OGSL Functions sh gpup 2 OpenGL Shading Language 1.30 Section 4.4 Call by value. Parameter Qualifiers: in (default) out inout Built In Functions See OpenGL Shading Language 1.30 Section 8 gpup 21 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 2

gpup 22 OpenGL Shading Language Use sh gpup 2 Steps for adding a typical shader to existing OpenGL code: Define what the shader is supposed to do. Identify appropriate programmable units (vertex, geometry, fragment, etc). Identify data that shaders will use. If data from client (CPU) determine whether attribute or uniform. For attributes and uniforms, determine if pre-defined or user-defined. Write shader code. In CPU code follow steps for installing shader. (E.g., use pshader). Get names of any new uniforms and attributes. As necessary, initialize uniforms and attributes. Turn shader on and off as necessary. gpup 22 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 2

gpup 23 Example Phong Shader sh gpup 2 Phong Shader: A lighting model in which the lighted color is computed at each fragment. (Otherwise the lighted color is computed at each vertex of a primitive and those lighted colors are interpolated across the fragments.) Phong Shader Steps Define what shader does. Computes lighting at fragment using interpolated normal... Identify appropriate units. For computing lighting: fragment shader. For passing along normal and color info, vertex shader. Identify data that shaders use. VS: Lighting data, normal. (All pre-defined.) FS: Normal (interpolated), eye-space vertex coordinates. User def. gpup 23 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 2

gpup 24 sh gpup 2 OpenGL Calls, from Initialization to Use (See OGL 4.3 Chapter 7) Create Program Object (Once) pobject = glcreateprogram() For Each Shader (Vertex, Geometry, Fragment, etc.): Create Shader Object sobject = glcreateshader(gl VERTEX SHADER) Provide Source Code to Shader Object and Compile glshadersource(sobject,1,&shader text lines,null); glcompileshader(sobject); Attach glattachshader(pobject,sobject); Link (Once) gllinkprogram(pobject); Use (Many Times, e.g., once per frame.) gluseprogram(pobject); gpup 24 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 2

gpup 25 OGSL Use sh gpup 2 Obtaining and Using Variable References At run time variables identified by number. At Initialization get location (index) of attributes and uniforms: vsal pinnacle = glgetattriblocation(pobject,name); sun ball size = glgetuniformlocation(pobject,name); During Render (Infrequently) Change Uniform Value (Using location) gluniform2f(sun ball size,ball size,ball size sq); During Render (Per Vertex Okay) Change Attribute Value (Using location) glvertexattrib4f(vsal pinnacle,pinnacle.x,pinnacle.y,pinnacle.z,radius); Done before each glvertex. Same options as vertex, such as client and buffer object arrays. gpup 25 EE 4702-X Lecture Transparency. Formatted 9:03, 20 October 2014 from shaders2. sh gpup 2