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

Similar documents
Introduction to OpenGL/GLSL and WebGL GLSL

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

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

Comp4422. Computer Graphics. Lab 02: WebGL API Prof. George Baciu. PQ838 x7272.

CPSC 436D Video Game Programming

Web-Based Visualization

CS452/552; EE465/505. Image Formation

Lecture 11 Shaders and WebGL. October 8, 2015

Programming with OpenGL Complete Programs Objectives Build a complete first program

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

CS452/552; EE465/505. Review & Examples

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

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

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

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

COMP371 COMPUTER GRAPHICS

2D graphics with WebGL

WebGL Game Development

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

BCA611 Video Oyunları için 3B Grafik

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

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

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

OPENGL RENDERING PIPELINE

Introduction to Computer Graphics with WebGL

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

Rasterization-based pipeline

Introduction to Computer Graphics. April 6, 2017 Kenshi Takayama

Introductory Seminar

CS 432 Interactive Computer Graphics

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

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

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

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

Programming with WebGL Part 1: Background. CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

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

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

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

CS452/552; EE465/505. Overview of Computer Graphics

Programming shaders & GPUs Christian Miller CS Fall 2011

OpenGL shaders and programming models that provide object persistence

Shaders. Slide credit to Prof. Zwicker

Game Graphics & Real-time Rendering

Lecture 13: OpenGL Shading Language (GLSL)

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

UberFlow: A GPU-Based Particle Engine

INTRODUCTION TO OPENGL PIPELINE

OPENGL AND GLSL. Computer Graphics

Mobile graphics API Overview

Rendering Grass with Instancing in DirectX* 10

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

Introduction to Computer Graphics with WebGL

JavaScript. Interpolated points

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

Models and Architectures

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

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

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

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

Sign up for crits! Announcments

Objectives. Coupling GLSL to Applications Example applications

Introduction to Computer Graphics with WebGL

Mobile Application Programing: Android. OpenGL Operation

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

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

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

object (say a cube) will be made up of triangles, each with three vertices, each with known object coordinates.

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

Programmable shader. Hanyang University

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

CSE 167: Introduction to Computer Graphics Lecture #19: Wrapping Up. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2017

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

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

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

CS 432 Interactive Computer Graphics

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 2: Introduction; GPU Architecture 1. Markus Hadwiger, KAUST

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

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

Water Simulation on WebGL and Three.js

Ciril Bohak. - INTRODUCTION TO WEBGL

Optimizing and Profiling Unity Games for Mobile Platforms. Angelo Theodorou Senior Software Engineer, MPG Gamelab 2014, 25 th -27 th June

DEFERRED RENDERING STEFAN MÜLLER ARISONA, ETH ZURICH SMA/

We assume that you are familiar with the following:

CS452/552; EE465/505. Shadow Mapping in WebGL

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

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

Mali Developer Resources. Kevin Ho ARM Taiwan FAE

Particle systems, collision detection, and ray tracing. Computer Graphics CSE 167 Lecture 17

Picking (In WebGL) Picking idea

Transformation, perspective projection, and LookAT in. Ming Ouhyoung, September 2018

Coding OpenGL ES 3.0 for Better Graphics Quality

CS452/552; EE465/505. Models & Viewing

Mobile Application Programing: Android. OpenGL Operation

Direct Rendering of Trimmed NURBS Surfaces

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

Introduction to Computer Graphics with WebGL

Transcription:

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

Today's Agenda Introduce WebGL and its programming model. Show code for a complete example. Demonstrate techniques for achieving best performance.

Introduction WebGL brings OpenGL ES 2.0 to the web. It is a proven rendering model, but very different than other web APIs. The triangle is the basic drawing primitive. Data for many triangles is prepared once, drawn many times. Vertex positions, colors, textures,... Shaders small programs that execute on the graphics card determine position of each triangle and color of each pixel.

Programming Model The GPU is a stream processor. Each point in 3D space has one or more streams of data associated with it. Position, surface normal, color, texture coordinate,... In OpenGL (and WebGL) these streams are called vertex attributes. These streams of data flow through the vertex and fragment shaders.

Programming Model

Vertex and Fragment Shaders Small, stateless programs which run on the GPU with a high degree of parallelism. The vertex shader is applied to each vertex of each triangle. It may output values to the fragment shader. The GPU figures out which pixels on the screen are covered by the triangle. The fragment shader is run at each pixel, automatically blending the outputs of the vertex shader.

Getting Data On To the GPU Vertex data is uploaded in to one or more buffer objects. The vertex attributes in the vertex shader are bound to the data in these buffer objects.

A Concrete Example Adapted from Giles Thomas' Learning WebGL Lesson 2 Code is checked in to http://webglsamples.googlecode.com/ under hello-webgl/

Vertex Shader attribute vec3 positionattr; attribute vec4 colorattr; varying vec4 vcolor; void main(void) { gl_position = vec4(positionattr, 1.0); vcolor = colorattr; }

Fragment Shader precision mediump float; varying vec4 vcolor; void main(void) { gl_fragcolor = vcolor; }

Initializing WebGL var gl; function initgl(canvas) { try { gl = canvas.getcontext("experimental-webgl"); gl.viewportwidth = canvas.width; gl.viewportheight = canvas.height; } catch (e) { } if (!gl) { alert("could not initialise WebGL, sorry :-("); } } Not the best detection and error reporting code. Link to http://get.webgl.org/ if initialization fails.

Shader Text The shader text for this sample is embedded in the web page using script elements. <script id="shader-vs" type="x-shader/x-vertex"> attribute vec3 positionattr; attribute vec4 colorattr;... </script> <script id="shader-fs" type="x-shader/x-fragment"> precision mediump float; varying vec4 vcolor;... </script>

Loading a Shader Create the shader object vertex or fragment. Specify its source code. Compile it. Check whether compilation succeeded. Complete code follows. Some error checking elided.

function getshader(gl, id) { var script = document.getelementbyid(id); var shader; if (script.type == "x-shader/x-vertex") { shader = gl.createshader(gl.vertex_shader); } else if (script.type == "x-shader/x-fragment") { shader = gl.createshader(gl.fragment_shader); } gl.shadersource(shader, script.text); gl.compileshader(shader); if (!gl.getshaderparameter( shader, gl.compile_status)) { alert(gl.getshaderinfolog(shader)); return null; } return shader; }

Loading the Shader Program A program object combines the vertex and fragment shaders. Load each shader separately. Attach each to the program. Link the program. Check whether linking succeeded. Prepare vertex attributes for later assignment. Complete code follows.

var program; function initshaders() { var fragmentshader = getshader(gl, "shader-fs"); var vertexshader = getshader(gl, "shader-vs"); program = gl.createprogram(); gl.attachshader(program, vertexshader); gl.attachshader(program, fragmentshader); gl.linkprogram(program); if (!gl.getprogramparameter(program, gl.link_status)) alert("could not initialise shaders"); gl.useprogram(program); program.positionattr = gl.getattriblocation(program, "positionattr"); gl.enablevertexattribarray(program.positionattr); program.colorattr = gl.getattriblocation(program, "colorattr"); gl.enablevertexattribarray(program.colorattr); }

Setting Up Geometry Allocate buffer object on the GPU. Upload geometric data containing all vertex streams. Many options: interleaved vs. non-interleaved data, using multiple buffer objects, etc. Generally, want to use as few buffer objects as possible. Switching is expensive.

var buffer; function initgeometry() { buffer = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, buffer); // Interleave vertex positions and colors var vertexdata = [ // Vertex 1 position 0.0, 0.8, 0.0, // Vertex 1 Color 1.0, 0.0, 0.0, 1.0, // Vertex 2 position -0.8, -0.8, 0.0, // Vertex 2 color 0.0, 1.0, 0.0, 1.0, // Vertex 3 position 0.8, -0.8, 0.0, // Vertex 3 color 0.0, 0.0, 1.0, 1.0 ]; gl.bufferdata(gl.array_buffer, new Float32Array(vertexData), gl.static_draw); }

Drawing the Scene Clear the viewing area. Set up vertex attribute streams. Issue the draw call.

function drawscene() { gl.viewport(0, 0, gl.viewportwidth, gl.viewportheight); gl.clear(gl.color_buffer_bit gl.depth_buffer_bit); gl.bindbuffer(gl.array_buffer, buffer); // There are 7 floating-point values per vertex var stride = 7 * Float32Array.BYTES_PER_ELEMENT; // Set up position stream gl.vertexattribpointer(program.positionattr, 3, gl.float, false, stride, 0); // Set up color stream gl.vertexattribpointer(program.colorattr, 4, gl.float, false, stride, 3 * Float32Array.BYTES_PER_ELEMENT); gl.drawarrays(gl.triangles, 0, 3); }

Libraries Now that we've dragged you through a complete example... Many libraries already exist to make it easier to use WebGL. A list (not comprehensive): http://www.khronos.org/webgl/wiki/user_contributions#frameworks A few suggestions: TDL (Aquarium, etc.) Three.js (http://ro.me, mr. doob's demos) CubicVR (Mozilla's WebGL demos like No Comply) CopperLicht (same developer as Irrlicht) PhiloGL (focus on data visualization) SpiderGL (lots of interesting visual effects) GLGE (used for early prototypes of Google Body) SceneJS (Interesting declarative syntax)

Achieving High Performance OpenGL "big rules" are to minimize: Draw calls Buffer, texture, and program binds Uniform variable changes State switches (enabling/disabling) WebGL "big rule": Offload as much JavaScript to the GPU as possible

Picking in Google Body (Thanks to Body team for this information) Highly detailed 3D model over a million triangles Selection is very fast

Picking in Google Body Could consider doing ray-casting in JavaScript Attempt to do quick discards if ray doesn't intersect bbox Still a lot of math to do in JavaScript

Picking in Google Body When model is loaded, assign different color to each organ Upon mouse click: Render body offscreen with different set of shaders Use threshold to determine whether to draw translucent layers Read back color of pixel under mouse pointer Same technique works at different levels of granularity

Particle Systems Particle demo from WebGL wiki (author: gman@) http://www.khronos.org/webgl/wiki/demo_repository Animates ~2000 particles at 60 FPS Does all the animation math on the GPU

Particle Systems Each particle's motion is defined by an equation Initial position, velocity, acceleration, spin, lifetime Set up motion parameters when particle is created Send down one parameter time each frame Vertex shader evaluates equation of motion, moves particle Absolute minimum amount of JavaScript work done per frame

Particle Systems Worlds of WebGL demo shows another particle-based effect http://www.nihilogic.dk/labs/worlds_of_webgl/ Uses point sprites rather than quads Vertex shader computes perspective projection and sets gl_pointsize for each point

Particle Systems Animation is done similarly to gman's particle demo For each scene, random positions are chosen for particles at setup time Time parameter interpolates between two position streams Particles in air / particles on floor Particles on floor / particle positions for next scene JavaScript does almost no computation

Physical Simulation WebGL supports floating point textures as an extension Store the state of a physical simulation on the GPU Any iterative computation where each step relies only on nearby neighbors is a good candidate for moving to GPU Several examples (waves, interference patterns): http://www.ibiblio.org/e-notes/webgl/gpu/contents.htm

More Resources Gregg Tavares' Google I/O 2011 talk on WebGL Techniques and Performance http://www.google.com/events/io/2011/sessions/webgl-techniques-and-performance.html Giles Thomas' WebGL blog http://learningwebgl.com/blog/ Mozilla's WebGL articles http://hacks.mozilla.org/category/webgl/ WebGL Chrome Experiments http://www.chromeexperiments.com/webgl/

More Resources WebGL wiki http://khronos.org/webgl/wiki Public WebGL mailing list (spec discussion only) https://www.khronos.org/webgl/public-mailing-list/ WebGL Developers' List https://groups.google.com/group/webgl-dev-list

Q&A