WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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

Drawing Fast The Graphics Pipeline

OPENGL RENDERING PIPELINE

Drawing Fast The Graphics Pipeline

Lecture 11 Shaders and WebGL. October 8, 2015

Practical Texturing (WebGL) CS559 Fall 2016 Lecture 20 November 7th 2016

Drawing Fast The Graphics Pipeline

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 Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

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

Programming shaders & GPUs Christian Miller CS Fall 2011

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

Rasterization and Graphics Hardware. Not just about fancy 3D! Rendering/Rasterization. The simplest case: Points. When do we care?

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

BCA611 Video Oyunları için 3B Grafik

COMP371 COMPUTER GRAPHICS

Converts geometric primitives into images Is split into several independent stages Those are usually executed concurrently

3D Drawing Visibility & Rasterization. CS559 Spring 2016 Lecture 9 February 23, 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

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

CS 432 Interactive Computer Graphics

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

12.2 Programmable Graphics Hardware

Introduction to Shaders.

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

The Graphics Pipeline

Getting fancy with texture mapping (Part 2) CS559 Spring Apr 2017

The Graphics Pipeline

Lecture 2. Shaders, GLSL and GPGPU

CS559 Computer Graphics Fall 2015

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

The Graphics Pipeline

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

Mobile Application Programing: Android. OpenGL Operation

Ciril Bohak. - INTRODUCTION TO WEBGL

Programmable Graphics Hardware

Rendering approaches. 1.image-oriented. 2.object-oriented. foreach pixel... 3D rendering pipeline. foreach object...

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

CS770/870 Fall 2015 Advanced GLSL

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

Shaders. Slide credit to Prof. Zwicker

Pipeline Operations. CS 4620 Lecture 14

Sign up for crits! Announcments

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

The Rasterization Pipeline

Lecture 13: OpenGL Shading Language (GLSL)

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

OpenGL shaders and programming models that provide object persistence

WebGL: Hands On. DevCon5 NYC Kenneth Russell Software Engineer, Google, Inc. Chair, WebGL Working Group

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

CS4620/5620: Lecture 14 Pipeline

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Drawing in 3D (viewing, projection, and the rest of the pipeline)

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

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

Mobile Application Programing: Android. OpenGL Operation

Drawing in 3D (viewing, projection, and the rest of the pipeline)

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

CPSC 436D Video Game Programming

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

HW-Tessellation Basics

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

CS 380 Introduction to Computer Graphics. LAB (1) : OpenGL Tutorial Reference : Foundations of 3D Computer Graphics, Steven J.

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

CS 4620 Midterm, October 23, 2018 SOLUTION

CS452/552; EE465/505. Image Formation

Dave Shreiner, ARM March 2009

Drawing in 3D (viewing, projection, and the rest of the pipeline)

Rendering Objects. Need to transform all geometry then

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

WebGL. Creating Interactive Content with WebGL. Media #WWDC14. Session 509 Dean Jackson and Brady Eidson WebKit Engineers

Rasterization-based pipeline

Game Graphics & Real-time Rendering

2D graphics with WebGL

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

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

Introduction to Shaders for Visualization. The Basic Computer Graphics Pipeline

Water Simulation on WebGL and Three.js

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

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

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

Introduction to the OpenGL Shading Language (GLSL)

Introduction to OpenGL/GLSL and WebGL GLSL

Could you make the XNA functions yourself?

Mobile Application Programming: Android. OpenGL Operation

Understanding Shaders and WebGL. Chris Dalton & Olli Etuaho

We assume that you are familiar with the following:

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

OpenGL Programmable Shaders

Introduction to Computer Graphics with WebGL

CS452/552; EE465/505. Review & Examples

Understanding M3G 2.0 and its Effect on Producing Exceptional 3D Java-Based Graphics. Sean Ellis Consultant Graphics Engineer ARM, Maidenhead

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

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

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

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

Game Graphics & Real-time Rendering

Transcription:

WebGL and GLSL Basics CS559 Fall 2016 Lecture 14 October 24 2016

Review

Hardware Rasterization For each point: Compute barycentric coords Decide if in or out.7,.7, -.4 1.1, 0, -.1.9,.05,.05.33,.33,.33

1988: The Personal Iris

A Pipeline 1 transf light project raster shade write

Applica>on Program The pipeline (1988) Graphics Driver Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

Applica>on Program The pipeline (2006-current) Graphics Driver Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

Applica>on Program Graphics Driver The parts you have to program 1988-2014 Now (in addition to above) Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

A Triangle s Journey

A Program to Draw a Triangle The complete WebGL thing we need Doing each necessary steps Just one triangle http://jsbin.com/fowoku/edit

Just a Triangle

HTML like you are used to

A Lot of Code

Look at the process inside-out We ll start with the end of the pipeline And work backwards

Applica>on Program Graphics Driver The parts you have to program 1988-2014 Now (in addition to above) Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

I warned you: Applica>on Program This step will get to be prexy exci>ng Graphics Driver Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

We have to program this step! Applica>on Program Graphics Driver The program is called the: Fragment Shader Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

Pixel in à Pixel out, each independent Applica>on Program All we do is change its values Graphics Driver Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

Coming axrac>ons Applica>on Program This step will get to be prexy exci>ng Graphics Driver Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

The Pixel Shader Given information about the pixel Compute color Optionally, compute other things

How to program it Use a Shading Language We ll use a language called GLSL Compiler built into WebGL Language specifics as we go

A (boring) Fragment Shader void main(void) { gl_fragcolor = vec4(0.0, 1.0, 1.0, 1.0); }

A (boring) Fragment Shader void main(void) { Shaders define a main func>on that take no arguments return no values gl_fragcolor = vec4(0.0, 1.0, 1.0, 1.0); }

A (boring) Fragment Shader void main(void) { GLSL Shaders operate by side effects on special variables (they look like globals) gl_fragcolor = vec4(0.0, 1.0, 1.0, 1.0); }

A (boring) Fragment Shader void main(void) GLSL has types useful in graphics Like 4 vectors { gl_fragcolor = vec4(0.0, 1.0, 1.0, 1.0); } This is opaque yellow (even colors are 4- vectors)

Processing Applica>on Program Graphics Driver We have to program this part too Also in GLSL Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

Process each vertex independently Applica>on Program Graphics Driver Transform compute x and n Clip Light compute c Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

What data about vertices? Inputs: Position Other Stuff A/ributes from applica>on Outputs: Position Other Stuff varying proper>es to framgment Shaders (remember interpola>on)

The simplest vertex shader attribute vec3 pos; void main(void) { } gl_position = vec4(pos, 1.0); }

The simplest vertex shader attribute vec3 pos; void main(void) { Shaders define a main func>on that take no arguments return no values gl_position = vec4(pos, 1.0); } }

The simplest vertex shader attribute vec3 pos; Shaders output by side effects: seang special variables void main(void) { } gl_position = vec4(pos, 1.0); }

The simplest vertex shader attribute vec3 pos; Shaders get input by reading special variables void main(void) { } gl_position = vec4(pos, 1.0); }

Special Variables Built in (magic) gl_position output of vertex shader gl_fragcolor output of frag shader User Defined attributes inputs to vertex shader varying output from vertex to fragment uniform constant over triangle group

The simplest vertex shader attribute vec3 pos; We are defining our own special variable void main(void) { } gl_position = vec4(pos, 1.0); }

The simplest vertex shader attribute vec3 pos; void main(void) { Cool GLSL feature: type conversions gl_position = vec4(pos, 1.0); } }

No Transformation? I will assume the position is already in the right coordinate system. The rasterizer (and everything else) works in Normalized Device Coordinates (NDC) -1 to 1 in each dimension

Applica>on Program Graphics Driver Start here Setup modes (window, ) Setup transform, lights Draw a triangle Posi>on, color, normal Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

In JavaScript using WebGL

The beginning

The beginning This should look like HTML5 Canvas You can have multiple contexts (draw canvas over WebGL)

Now about those shaders Get them into strings Use a library to read them from resources

Run the compiler!

Error Checking Here I checked for errors (since I often have syntax errors) You should check for errors everywhere

Run the compiler again! Need to compile both shaders

Link the shaders together Shaders always work in pairs Need to connect them

Setup the special variables Important to communicate with shaders

The simplest vertex shader attribute vec3 pos; Javascript needs to connect to the pos variable void main(void) { } gl_position = vec4(pos, 1.0); }

Communicating an attribute We give it an array of attributes Assign it to a position We have to ask which position

OK, Now for our triangle How do we get this data to the hardware? Need to do a block transfer

Applica>on Program Graphics Driver Need to get the ver>ces to the hardware fast! (normally more than 3) Command Buffer (Triangle Queue) Queue Processing (TCL) Cache Assembly Triangle Processing Rasterize Pixel Queue Pixel Processing Pixel Tests shading Geometry Shading Pixel Shading Texture Memory Render to texture Frame Buffer

Key Idea: Buffer Create a buffer buffer = a block of memory on the GPU Copy the data into the buffer Must be a special JavaScript object: Float32Array (array of fixed types)

Now to draw First we have to clear the screen Notice that color is a 4-vector I don t really need the z-buffer

Now we actually draw the triangle Notice that we use the shaders and the buffer

All that for a triangle!

Is it really 100 lines of code? Not really lots of comments Build wrappers to be more concise you do the same thing over and over But there are lots of steps and you should understand them

Two triangles Can you see where these triangles will go? (remember they are in NDC)

Change the array sizes

Two triangles

How do we color them differently?

Color per vertex Add an attribute for each vertex so we can pass a color for each Have the vertex shader output the color varying variable for fragment shader Have the fragment shader input the color

A (boring) Fragment Shader void main(void) { gl_fragcolor = vec4(0.0, 1.0, 1.0, 1.0); }

A (less boring) Fragment Shader precision highp float; varying vec3 outcolor; Our own magic variable! void main(void) { gl_fragcolor = vec4(outcolor, 1.0); }

A (less boring) Fragment Shader precision highp float; varying vec3 outcolor; Required so the shaders can talk void main(void) { gl_fragcolor = vec4(outcolor, 1.0); }

Connecting Shaders varying variables connect shaders the output of a vertex shader becomes the input to a fragment shader The 3 vertices of a triangle are interpolated

The simplest vertex shader attribute vec3 pos; void main(void) { } gl_position = vec4(pos, 1.0); }

The (almost) simplest vertex shader attribute vec3 pos; varying vec3 outcolor; void main(void) { gl_position = vec4(pos, 1.0); } outcolor = vec3(1.0,0.0,1.0); }

Two purple triangles hxp://jsbin.com/wecaci/edit?js,output

Make color an input as well attribute vec3 pos; attribute vec3 incolor; varying vec3 outcolor; void main(void) { gl_position = vec4(pos, 1.0); } outcolor = incolor; }

Remember We can t pass values directly to a fragment we don t even know what they will be! We pass attributes of vertices which can then pass them to fragments

Now to connect to JavaScript

Colors per vertex

Put them in a buffer

When we draw, use 2 buffers

Two triangles hxp://jsbin.com/digupi/edit?js,output

Apply a transformation One transformation for the triangle group It is constant over the drawarrays call This is a uniform variable hxp://jsbin.com/>rapu/19/edit?js,output

Simplifying the Code There is stuff you do over and over and Write it once and use it often Or let someone else write it once This is where twgl comes in

Compile two vertex programs For each run the compiler check for errors Link them together Attach to the attributes Set up to specify the uniforms

Do it by hand Do it with twgl var shaders = twgl.createprograminfo(gl, ["vs", "fs"]); Yes, one line And it grabs the string from script tags so they are separate from your JS program. But the documentation is terrible.

How about those shaders They do very specific things you need to understand the pipeline They have 3 kinds of weird variables you need to understand the model They are written in a cool language you ll pick it up quickly The language has a bunch of useful stuff look at the quick reference card

Learning Shader Programming Connecting your program to shaders is hard So, don t bother (yet) Use a Shader IDE that lets you focus on shaders Gives you an object, a program,

Some things about GLSL Very strongly typed float x = 1; // error! integer and float Cool sub-vector access: vec3 v; v.xy (a 2-vector) vec4(v,1) (a 4-vector) vec4(v.xy, v.zx)

More cool stuff about GLSL Lots of handy math functions They know it s for graphics! Limited control structures parallel execution means all the same Conditional functions step, softstep,