Lecture 13: OpenGL Shading Language (GLSL)

Similar documents
CS450/550. Pipeline Architecture. Adapted From: Angel and Shreiner: Interactive Computer Graphics6E Addison-Wesley 2012

Programming shaders & GPUs Christian Miller CS Fall 2011

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

Shaders. Slide credit to Prof. Zwicker

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

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

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

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

Ciril Bohak. - INTRODUCTION TO WEBGL

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

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

CS427 Multicore Architecture and Parallel Computing

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

12.2 Programmable Graphics Hardware

Supplement to Lecture 22

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

Introduction to Shaders.

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

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

Dave Shreiner, ARM March 2009

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

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

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

Graphics Hardware. Instructor Stephen J. Guy

BCA611 Video Oyunları için 3B Grafik

COMP371 COMPUTER GRAPHICS

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

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

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

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

An Overview GLUT GLSL GLEW

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

GPGPU Applications. for Hydrological and Atmospheric Simulations. and Visualizations on the Web. Ibrahim Demir

OpenGL BOF Siggraph 2011

Copyright Khronos Group, Page Graphic Remedy. All Rights Reserved

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

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

CS 179: GPU Programming

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, designed for rendering 2D graphics and interactive 3D graphics.

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

ECE 571 Advanced Microprocessor-Based Design Lecture 20

The Graphics Pipeline

Programmable Graphics Hardware

Programmable GPUs Outline

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

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

Motivation Hardware Overview Programming model. GPU computing. Part 1: General introduction. Ch. Hoelbling. Wuppertal University

printf Debugging Examples

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

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

Vulkan: Architecture positive How Vulkan maps to PowerVR GPUs Kevin sun Lead Developer Support Engineer, APAC PowerVR Graphics.

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

CS 220: Introduction to Parallel Computing. Introduction to CUDA. Lecture 28

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

OpenGL Programmable Shaders

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

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

Lecture 2. Shaders, GLSL and GPGPU

Mobile graphics API Overview

CS475/CS675 - Computer Graphics. OpenGL Drawing

CSE 591: GPU Programming. Introduction. Entertainment Graphics: Virtual Realism for the Masses. Computer games need to have: Klaus Mueller

ECE 571 Advanced Microprocessor-Based Design Lecture 18

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

Graphics Processing Unit Architecture (GPU Arch)

Next Generation OpenGL Neil Trevett Khronos President NVIDIA VP Mobile Copyright Khronos Group Page 1

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

CS179 GPU Programming Introduction to CUDA. Lecture originally by Luke Durant and Tamas Szalay

GPU Memory Model. Adapted from:

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

We assume that you are familiar with the following:

GPU Memory Model Overview

CS 450: COMPUTER GRAPHICS REVIEW: INTRODUCTION TO COMPUTER GRAPHICS SPRING 2016 DR. MICHAEL J. REALE

Copyright Khronos Group Page 1

WebGL. Announcements. WebGL for Graphics Developers. WebGL for Web Developers. Homework 5 due Monday, 04/16. Final on Tuesday, 05/01

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

Rendering Objects. Need to transform all geometry then

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Performance OpenGL Programming (for whatever reason)

LECTURE 02 OPENGL API

Mobile Graphics Ecosystem. Tom Olson OpenGL ES working group chair

Mention driver developers in the room. Because of time this will be fairly high level, feel free to come talk to us afterwards

Shading Language Update

Sign up for crits! Announcments

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

NVIDIA Parallel Nsight. Jeff Kiel

CS179: GPU Programming

Achieving High-performance Graphics on Mobile With the Vulkan API

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

OpenGL Essentials Training

GPGPU. Peter Laurens 1st-year PhD Student, NSC

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

2: Introducing image synthesis. Some orientation how did we get here? Graphics system architecture Overview of OpenGL / GLU / GLUT

PERFORMANCE OPTIMIZATIONS FOR AUTOMOTIVE SOFTWARE

Mining the Rendering Power in Web Browsers. Jianxia Xue Jan. 28, 2014

Lab 2-3D Transformations and Vertex Shaders

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

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. Daniel Wesslén, Stefan Seipel, Examples

WebGL. WebGL. Bring 3D to the Masses. WebGL. The web has text, images, and video. We want to support. Put it in on a webpage

Transcription:

Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 18, 2018 1/56

Motivation } Last week, we discussed the many of the new tricks in Graphics require low-level access to the Graphics pipeline } For example, using textures, which is a pain in regular OpenGL. 2/56

Graphics History } Software Rendering } Old-fashioned way (Use ray casting) } Fixed-Function } Immediate Mode (What we have used) } Programmable Pipeline (~2001) } GL Shading Language (What we will learn) } CUDA/OpenCL (GPGPU) (~2008) } General Purpose GPU Programming 3/56

COMP 175 COMPUTER GRAPHICS Ray Casting (Software Rendering) } } Wolfenstein Quake 1 Remco Chang 13 GLSL 4/56

COMP 175 COMPUTER GRAPHICS Fixed-Function } } Quake 3 Half-Life Remco Chang 13 GLSL 5/56

COMP 175 COMPUTER GRAPHICS Programmable Pipeline (Shaders) } } Doom 3 Far Cry Remco Chang 13 GLSL 6/56

GPGPU } General Purpose Graphics Processing Unit } Utilize graphics card for computation } Do work other than graphics 7/56

Old Fixed-Function 8/56

Programmable Pipeline 9/56

Why GPU? } Having two (CPU and GPU) is better than one } GPU is dedicated to floating point operations (a traditionally expensive operation on a CPU) } It is especially optimized for linear algebra (e.g. matrix manipulation)! } GPU is multi-core (multi-processor) } Imagine that you have one processor per pixel, how would you program what the processor should do? 10/56

11/56

Floating Point Operations (FLOPs) 12/56

13 4/15/2014 Accessing Programmable Pipeline Introducing Shaders! 13/56

CPU + GPU } CPU: Typical C++ code } GPU: OpenGL Shading Language (GLSL) } CPU passes data to GPU } Some processing happens on the CPU: } Get mouse movement } Load and parse data from disk } Some processing happens on the GPU: } Transform point from Object to World space } Solve lighting equation } Render 14/56

CPU + GPU (Continued) } Think of this as two separate processors. What we ll learn today are: 1. What the GPU should do (and what the CPU should do). } In particular different shaders to fit into the different parts of the graphics pipeline 2. The language used by the GPU (i.e. OpenGL Shading Language, or OpenGL SL, or GLSL). 3. How to communicate between CPU and GPU } Sharing chunks of memory } Passing variable names and values 15/56

Types of Shaders } Most common: } Vertex } Fragment (Pixel) } Newer: } Geometry } Tessellation } Compute 16/56

Two Things to Learn: } OpenGL Shading Language (GLSL) } Syntax } Sequence and processes } Vertex Shader } Pixel Shader } Communication between CPU and GPU } What type of information can be passed } How to pass information } Note that you also need to know how to load a shader program, but that s magic incantation that (mostly) stays the same every time. 17/56

Interfacing with the GPU } Modern OpenGL } Mobile/Web Programming } Direct3D } Compute Languages } Stream, OpenCL, CUDA } Future 18/56

Client/Server Architecture } Think of like a network } Client = the CPU } Server = the GPU } The less they talk, the better } Less waiting for memory to transfer } Bandwidth is almost always the bottleneck } Send data over once, and have it ready to go 19/56

Immediate Mode (Fixed-Function pipeline) } Every gl command is being sent to the GPU every iteration. } Very inefficient } Think of it like having an interpreter versus a compiler for C++ Michael Shah 20/56

Fixed Function (Left) vs. Programmable (Right) 21/56

Optimizations Available } Vertex Array } Display Lists } Vertex Buffer Objects } Pixel Buffers } Frame Buffer Objects Michael Shah 22/56

Vertex Array } No more glbegin/glend } Store Vertices, indices, normals, colors, texture coordinates in an array } Minimizes OpenGL calls on Client (CPU) } Data still being passed from Client to Server every frame } A little better because it s all at once } Stylistically, much cleaner code as well Michael Shah 23/56

Display List 24 4/22/2014 } Compiled geometry } Send it to the Server(GPU) once, and it is stored in memory on Server } Geometry cannot be updated, without deleting display list. somegeometrydisplaylist = glgenlists(1); glnewlist(somegeometrydisplaylist,gl_compile); drawshape(); glendlist(); Michael Shah 24/56

Vertex Buffer Object (VBO) 25 4/22/2014 } Improves upon Vertex Array } We can store data on the Server (GPU) } Just like display lists! } We get a pointer into the Server (GPU) so that we can update it! } As good as display lists! } Because we have a pointer to the data, multiple clients can access data. } Multiple instances of a program can access data Michael Shah 25/56

Modifiers to Variables Uniform/Varying/Attribute } Uniform Read only data passed from CPU into GPU (vertex/fragment shaders) } Attributes Input value which change every vertex. } Read only Used only in vertex shader } Varying Variables passed from one shader to another } Depending on GLSL version: } GLSL <3.0: Varying Pass data from vertex to fragment shader. } GLSL >=3.0: in / out in is the variable being passed in, out is the variable being passed out } Read-only in fragment shader. } Read/Write in Vertex Shader 26/56

} Order matters Info is only passed in one direction (starting with vertex shader) } Note that we can run this loop multiple times (e.g. for shadow, or achieve specific shading effects (such as toon-shading)) 27/56

OpenGL Shading Language 28/56

GLSL } C-Like Language } Made for GPU Architecture } Because GPU s are different than CPU s } Still Cross Platform } Debugging Still as difficult } Domain Specific Language } Compiled at runtime!!!!! 29/56

GLSL Primitives } float, int, bool Example: float f = 10.0; or float f = float(10); (Casting not allowed, choose your type wisely!) 30/56

GLSL Types } Vector: vec2, vec3, vec4 } Overloaded to take in each type } Example: vec4 temp= vec4(1.0, 1.0, 1.0, 1.0) } Matrix: mat2, mat3, mat4 } Example: glmodelviewmatrix is a mat4 type } Access first column with: mat[0] } Access second column with: mat[1] } Example: mat[1] = vec3(1.0, 1.0, 1.0); } Access individual element: mat[2][1]; 31/56

Some more examples } Pass in vec3 into a vec4 vec3 color = vec3(1.0,0.5,0.25) vec4 temp = vec4(color, 1.0) } Get components of vector } color.x = 1.0; or color[0] = 1.0 } color.y = 1.0; or color[1] = 1.0; 32/56

Swizzling [1] } Swizzling is the ability to rearrange a vectors components } vec2 pos = temp.xy; // retrieve x and y component, or any of xyzw } vec2 pos = temp.yz; } vec4 = color.agbr; // opposite of rgba 33/56

More GLSL Keywords [1][2] } Note that some of these default keywords might not be there depending on GLSL version number (for example: gl_modelviewmatrix isn t there any more) 34/56

Write a Simple Shader Basic Vertex and Fragment Shader 35/56

Writing a Shader } Writing a shader, compiling a shader, and loading a shader } Compile at runtime } So we do not need to recompile entire source, if we only modify shader. 36/56

37/56

Simple Vertex Shader 38/56

Simple Fragment Shader 39/56

The cpp file 40/56

Drawing with immediate mode for now } Ideally we ll use a Vertex Buffer Object (VBO). But to keep it simple in this example: 41/56

Final Image 42/56

Communications Passing Vertex Buffer Objects from CPU -> Shaders Passing variables from CPU -> Shaders Passing variables between Shaders 43/56

Modifiers to Variables Uniform/Varying/Attribute } Uniform Read only data passed from CPU into GPU (vertex/fragment shaders) } Attributes Input value which change every vertex. } Read only Used only in vertex shader } Varying Variables passed from one shader to another } Depending on GLSL version: } GLSL <3.0: Varying Pass data from vertex to fragment shader. } GLSL >=3.0: in / out in is the variable being passed in, out is the variable being passed out } Read-only in fragment shader. } Read/Write in Vertex Shader 44/56

Communications (Attrib) https://www.khronos.org/opengl/wiki/tutorial2:_vaos,_vbos,_vertex_and_fragment_shaders_(c_/_sdl) 45/56

Communications (Attrib) 46/56

Vertex Shader Fragment Shader 47/56

Magic!! Vertex to Fragment Shader } If you are wondering how a vertex shader passes data to a fragment shader } Because vertex shaders operate at a per vertex level and fragments operate at a per pixel level } What gets sent from one to another?? } The magic process is blending } Defaults to linear blend. Can be changed to different blend funcs 48/56

Communications (Uniform) } Sometimes you want to pass just a single number (not per vertex (i.e. attribute) information via vertex buffer objects). } These are called uniform variables. These can include: } Rendering information (e.g. projection matrix) } Rendering resource (e.g. a texture ID) } Control variable (e.g. rendermode) }. In the vertex shader below, the value of Scale 49/56

Communications (Uniform) } ProgramObject is the bound shader program } gluniform has many forms: } gluniform1f, gluniform2f, 4f (float) } gluniform1i 4i (int) } gluniform1ui 4ui (unsigned int) } gluniform1fv 4fv (floating array) } gluniformmatrix2fv 4fv 50/56

Versions of OpenGL Confusions galore 51/56

Many versions of OpenGL } OpenGL 2.0 (2004) introduces the idea of shaders } OpenGL SL 1.0 (or 1.1) is a part of OpenGL 2.0 } Currently, OpenGL is at 4.6 (2017). GLSL is also at 4.6 } When OpenGL 3.1 was introduced, GLSL changed versioning to match (to 3.1), which was previous 1.3.1 } 3.0 -> 3.1 (2009) is said to be the big move where fixed pipeline is considered deprecated. 52/56

Open GL ES } OpenGL ES (ES = Embedded Systems) is meant for smart phones, devices, etc. } OpenGL ES 1.0 (2003) implemented OpenGL 1.3 } OpenGL ES 2.0 (2007) is a reduced set of OpenGL 2.0 } But ES 2.0 jumped ahead and eliminated almost all fixed-pipeline operations (e.g. glbegin, glend) } OpenGL ES 2.0 is not backward compatible with previous versions } Current version is ES 3.2 (2015) 53/56

WebGL } WebGL is OpenGL for browsers, in particular HTML5 canvas element. } WebGL 1.0 is based on OpenGL ES 2.0 (2011) } Current version is WebGL 2.0 (2017) } based on OpenGL ES 3.0 } Largely backward compatible 54/56

CUDA / OpenCL } Made for general purpose graphics programming (GPGPU) } Popular known use is bitcoin mining } CUDA is a language specific to nvidia (2007) } Obviously different from GLSL in that there are no graphics related keywords or concepts. But the general idea (of parallel computing) is similar } OpenCL is an open standard version (2009) of CUDA led by the Khronos Group } The Khronos Group is a non-profit consortium founded by major graphics companies } Currently Khronos is in charge of the OpenGL, OpenCL, OpenGL ES, WebGL, WebCL, and a bunch of related APIs. 55/56

Moving Forward in this Class } The C++ platform of GLUT only supports up to OpenGL 1.2 for Macs, but supports OpenGL 2.0 (or higher) in Windows. } As it stands, I am not aware of an easy C++ cross-platform environment that supports advanced OpenGL } Complicated platforms like Qt, wx, GTK exist, but the learning curve is high. One possible path forward is FLTK } Side-stepping all the platform, hardware, GL version, issues, we switch to WebGL for the rest of the class } PROS: modern, latest OpenGL concepts. Easy to show off } CONS: uh, toss out all of your work in C++ so far and switch to Javascript 56/56