OpenGL and WebGL Patrick Cozzi University of Pennsylvania CIS 565 - Fall 2013.

Slides:



Advertisements
Similar presentations
POST-PROCESSING SET09115 Intro Graphics Programming.
Advertisements

COMPUTER GRAPHICS CS 482 – FALL 2014 NOVEMBER 10, 2014 GRAPHICS HARDWARE GRAPHICS PROCESSING UNITS PARALLELISM.
GlTF and rest3d Patrick Cozzi University of Pennsylvania CIS Fall 2013.
GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Status – Week 257 Victor Moya. Summary GPU interface. GPU interface. GPU state. GPU state. API/Driver State. API/Driver State. Driver/CPU Proxy. Driver/CPU.
An introduction to WebGL Viktor Kovács. Content A little 3D modeling. What is WebGL? Introducing Three.js. Visualizing GDL objects.
WebGL: GPU Acceleration for the open web Guest Lecture: Patrick Cozzi Analytical Graphics, Inc. University of Pennsylvania.
HTML5 Haptics Standardization
 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
Introduction to OpenGL Joseph Kider University of Pennsylvania CIS 565 – Fall 2011 (Source: Patrick Cozzi)
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Introduction to Computer Graphics Ed Angel Professor of Computer Science, Electrical and.
GPU Graphics Processing Unit. Graphics Pipeline Scene Transformations Lighting & Shading ViewingTransformations Rasterization GPUs evolved as hardware.
A closer look Dynamic Webpages Jessica Meyerson March 1, 2011.
GAM532 DPS932 – Week 1 Rendering Pipeline and Shaders.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
WebGL Patrick Cozzi University of Pennsylvania CIS Spring 2012.
CSU0021 Computer Graphics © Chun-Fa Chang CSU0021 Computer Graphics September 10, 2014.
OpenGL 3.0 Texture Arrays Presentation: Olivia Terrell, Dec. 4, 2008.
Teaching Intro and Advanced Graphics with WebGL
WebGL: Hands On Zhenyao Mo Software Engineer, Google, Inc. Chrome GPU Team.
Mobile App Support Jacob Poirier Geri Hengesbach Andrea Menke Erin Rossell.
CS 480/680 Computer Graphics Course Overview Dr. Frederick C Harris, Jr. Fall 2012.
Java Mobile Apps with GWT & PhoneGap Josh Marinacci, webOS Developer Advocate.
Copyright© Jeffrey Jongko, Ateneo de Manila University Android.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
1 Angel and Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Introduction to Computer Graphics Ed Angel Professor Emeritus of Computer.
1 Introduction to Computer Graphics SEN Introduction to OpenGL Graphics Applications.
Computer Graphics I, Fall 2008 Introduction to Computer Graphics.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 CS4610/7610: Introduction to Computer Graphics.
Real-Time High Quality Rendering CSE 291 [Winter 2015], Lecture 4 Brief Intro to Programmable Shaders
WebGL Patrick Cozzi University of Pennsylvania CIS Fall 2012.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
CS 4363/6353 OPENGL BACKGROUND. WHY IS THIS CLASS SO HARD TO TEACH? (I’LL STOP WHINING SOON) Hardware (GPUs) double in processing power ever 6 months!
CS 480/680 Intro Dr. Frederick C Harris, Jr. Fall 2014.
به نام خدا تنظیم کننده : فرانه حدادی استاد : مهندس زمانیان تابستان 92.
Tone Mapping on GPUs Cliff Woolley University of Virginia Slides courtesy Nolan Goodnight.
OpenGL Shader Language Vertex and Fragment Shading Programs.
Final Project Ideas Patrick Cozzi University of Pennsylvania CIS Fall 2014.
Final Project Ideas Patrick Cozzi University of Pennsylvania CIS Fall 2013.
Shaders in OpenGL Marshall Hahn. Introduction to Shaders in OpenGL In this talk, the basics of OpenGL Shading Language will be covered. This includes.
Patrick Cozzi University of Pennsylvania CIS Fall 2014
OpenGL Shading Language (GLSL)
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
OpenGL Shading Language (GLSL)
WebGL Presentation by: Viet Nguyen.  Final Project Outline  Introduction  History  Support  Implementation  Advantages  Disadvantages  Conclusion.
The OpenGL API Patrick Cozzi University of Pennsylvania CIS Fall 2012.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
OpenGl Shaders Lighthouse3d.com.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
GPU Computing for GIS James Mower Department of Geography and Planning University at Albany.
NativeScript – Open source platform to build Native iOS/Android Apps.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
WebGL The HTML5/JavaScript 3D Computer Graphics API
The Basics: HTML5, Drawing, and Source Code Organization
Web3D Consortium X3DOM: Next-Generation Web3D Applications on Open Standards and Open Source Web3D Consortium
Introduction to OpenGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
UMBC Graphics for Games
Chapter VI OpenGL ES and Shader
Introduction to Shaders
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
Introduction to Computer Graphics with WebGL
Introduction to OpenGL
CS 480/680 Computer Graphics GLSL Overview.
OpenGL-Rendering Pipeline
Presentation transcript:

OpenGL and WebGL Patrick Cozzi University of Pennsylvania CIS Fall 2013

Announcements LinkedIn Group Academic Integrity Project 4 demos next Wednesday 2

Announcements Projects  4. Due tomorrow  5. Released Friday. Due Thursday 11/07  6. Released Friday 11/08. Due Friday 11/15 Hackathon  Monday 11/11 Final Project  Kickoff Wednesday 11/13  Pitches on Monday 11/18 3

OpenGL Is a C-based API Is cross platform Is run by the ARB: Architecture Review Board Hides the device driver details OpenGL vs. Direct3D

OpenGL We are core profile  No fixed function vertex and fragment shading  No legacy API calls: glBegin() glRotatef() glTexEnvf() AlphaFunc() … Why was the alpha test remove?Recall the fixed function light map

OpenGL GPU Device Driver OpenGL API Application Software stack:

OpenGL Major objects: Shader Programs Textures Framebuffers Shader Objects Array Buffers Fixed Function State Element Buffers Pixel Buffers Renderbuffers We are not covering everything. Just surveying the most relevant parts for writing GLSL shaders

Shaders Shader object: an individual vertex, fragment, etc. shader  Are provided shader source code as a string  Are compiled Shader program: Multiple shader objects linked together

Shader Objects const char *source = //... GLint sourceLength = //... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE //... glDeleteShader(v); Compile a shader object:

Shader Objects const char *source = //... GLint sourceLength = //... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE //... glDeleteShader(v); Compile a shader object: v is an opaque object What is it under the hood? How would you design this in C++? OpenGL functions start with gl. Why? How would you design this in C++?

Shader Objects const char *source = //... GLint sourceLength = //... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE //... glDeleteShader(v); Compile a shader object: Provide the shader’s source code Where should the source come from? Why can we pass more than one string?

Shader Objects const char *source = //... GLint sourceLength = //... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE //... glDeleteShader(v); Compile a shader object: Compile, but what does the driver really do?

Shader Objects const char *source = //... GLint sourceLength = //... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE //... glDeleteShader(v); Compile a shader object: Good developers check for error. Again, how would you design this in C++? Calling glGet* has performance implications. Why?

Shader Objects const char *source = //... GLint sourceLength = //... GLuint v = glCreateShader(GL_VERTEX_SHADER); glShaderSource(v, 1, &source, &sourceLength); glCompileShader(v); GLint compiled; glGetShaderiv(v, GL_COMPILE_STATUS, &compiled); // success: compiled == GL_TRUE //... glDeleteShader(v); Compile a shader object: Good developers also cleanup resources

Shader Programs GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); //... GLuint p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); GLint linked; glGetShaderiv(p, GL_LINK_STATUS, &linked); // success: linked == GL_TRUE //... glDeleteProgram(v); Link a shader program:

Shader Programs GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); //... GLuint p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); GLint linked; glGetShaderiv(p, GL_LINK_STATUS, &linked); // success: linked == GL_TRUE //... glDeleteProgram(v); Link a shader program: A program needs a vertex and fragment shader

Shader Programs GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); //... GLuint p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); GLint linked; glGetShaderiv(p, GL_LINK_STATUS, &linked); // success: linked == GL_TRUE //... glDeleteProgram(v); Link a shader program:

Shader Programs GLuint v = glCreateShader(GL_VERTEX_SHADER); GLuint f = glCreateShader(GL_FRAGMENT_SHADER); //... GLuint p = glCreateProgram(); glAttachShader(p, v); glAttachShader(p, f); glLinkProgram(p); GLint linked; glGetShaderiv(p, GL_LINK_STATUS, &linked); // success: linked == GL_TRUE //... glDeleteProgram(v); Link a shader program: Be a good developer again

Using Shader Programs GLuint p = glCreateProgram(); //... glUseProgram(p); glDraw*(); // * because there are lots of draw functions Part of the current state How do you draw different objects with different shaders? What is the cost of using multiple shaders? How do we reduce the cost? Hint: write more CPU code – really.

Uniforms GLuint p = glCreateProgram(); //... glLinkProgram(p); GLuint m = glGetUniformLocation(p, “u_modelViewMatrix”); GLuint l = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = //... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0);

Uniforms GLuint p = glCreateProgram(); //... glLinkProgram(p); GLuint m = glGetUniformLocation(p, “u_modelViewMatrix”); GLuint l = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = //... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0); Each active uniform has an integer index location.

Uniforms GLuint p = glCreateProgram(); //... glLinkProgram(p); GLuint m = glGetUniformLocation(p, “u_modelViewMatrix”); GLuint l = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = //... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0); mat4 is part of the C++ GLM library GLM:

Uniforms GLuint p = glCreateProgram(); //... glLinkProgram(p); GLuint m = glGetUniformLocation(p, “u_modelViewMatrix”); GLuint l = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = //... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0); Uniforms can be changed as often as needed, but are constant during a draw call Not transposing the matrix glUniform* for all sorts of datatypes

Uniforms GLuint p = glCreateProgram(); //... glLinkProgram(p); GLuint m = glGetUniformLocation(p, “u_modelViewMatrix”); GLuint l = glGetUniformLocation(p, “u_lightMap”); glUseProgram(p); mat4 matrix = //... glUniformMatrix4fv(m, 1, GL_FALSE, &matrix[0][0]); glUniform1i(l, 0); Why not glUniform*(p, …) ?

The web has text, images, and video  What is the next media-type? We want to support  Windows, Linux, Mac  Desktop and mobile WebGL 25

Bring 3D to the Masses Put it in on a webpage  Does not require a plugin or install  Does not require administrator rights Make it run on most GPUs 26

WebGL Image from Ecosystem_Aug-11.pdfhttp:// Ecosystem_Aug-11.pdf OpenGL ES 2.0 for JavaScript  Seriously, JavaScript 27

WebGL Includes  Vertex shaders  Fragment shaders  Vertex buffers  Textures  Framebuffers  Render states  … Does not include  Geometry shaders  Tessellation shaders  Vertex Array Objects  Multiple render targets  Floating-point textures  Compressed textures  FS depth writes  … See 28

WebGL If you know OpenGL, you already know WebGL If you know C++, the real learning curve is JavaScript 29

WebGL Alternatives? Flash Silverlight Java Applets Unity 30

WebGL Creating a context is easy: // HTML: // JavaScript: var gl = document.getElementById("glCanvas").getContext("experimental-webgl"); 31

WebGL The rest is similar to desktop OpenGL: //... gl.bindBuffer(/*... */); gl.vertexAttribPointer(/*... */); gl.useProgram(/*... */); gl.drawArrays(/*... */); Checkout 32

WebGL Create an animation loop: (function tick(){ //... GL calls to draw scene window.requestAnimationFrame(tick); })(); You want this to work cross-browser. See 33

WebGL Performance Performance can be very good. Why? 34

WebGL Performance Performance can be very good. Why?  The GPU is still doing the rendering  Batch! Draw multiple objects with one draw call Sort by texture Push work into shaders Push work into web workers See 35

WebGL Performance Image from 32x3264x64128x128 C++1.9 ms6.25 ms58.82 ms Chrome ms ms ms x slowdown x3264x64128x128 C ms9.43 ms37.03 ms Chrome ms22.72 ms41.66 ms x slowdown CPU-intensive GPU-intensive (256 draws per frame) 36

WebGL and other APIs Take advantage of other web APIs:  HTML5  2D  CSS transforms  Composite UI elements  Web workers  Typed Arrays 37

HTML5 on Mobile Touch events Geolocation Device orientation and motion The future of HTML5 and WebGL on mobile is very promising 38

WebGL support is good, and it is getting better… 39

WebGL Stats October

WebGL Support Desktop: Android: iOS:

WebGL on Your System 42

Desktop WebGL Support Windows  No OpenGL driver installed? Old driver? Only 35% of Windows XP machines have GL 2 drivers  Buggy driver?  No problem: ANGLE – Almost Native Graphics Layer Engine OpenGL ES 2.0 Direct3D 9 See 43

N-Body Simulation Prototypes for Firefox and WebKit are available WebCL OpenCL bindings for JavaScript are coming. 44

Browser Architecture Single Process See 45

Browser Architecture Chrome’s Multi-process See 46

Browser Architecture Chrome’s Multi-process See 47

Browser Architecture Chrome’s Multi-process See 48

Questions In a multi-process is gl.Get* slow? Why? What about security? 49

Cross-Origin Resource Sharing Images can’t always be used as texture sources. Why? 50

Cross-Origin Resource Sharing var img = new Image(); img.onload = function() { gl.texImage2D(/*... */, img); }; img.src = "image.png"; Same domain is OK: 51

Cross-Origin Resource Sharing var img = new Image(); img.onload = function() { gl.texImage2D(/*... */, img); }; img.crossOrigin = "anonymous"; img.src = " Another domain requires CORS if supported: 52

Cross-Origin Resource Sharing Not all servers support CORS: Browser html/js/css files Images files used for textures 53

Cross-Origin Resource Sharing Use a proxy server: Browser html/js/css files Images files used for textures Images files used for textures “proxy.php? domain.com/image.png" See 54

Denial of Service Attacks Long draw calls  Complicated shaders  Big vertex buffers Solutions  Kill long draw calls  Forbid further rendering Lots of WebGL security info: 55

WebGL Libraries Three.js: Cesium: Many more:

WebGL Resources WebGL Camps: Learning WebGL:

The Joys of JavaScript Skip the next 30 slides if you already know JavaScript 58

JavaScript is weakly typed… 59

JavaScript Type System short, int, float, double. Who needs them? var n = 1; 60

JavaScript Type System JavaScript has numbers, strings, and booleans: var n = 1; var s = “WebGL”; var b = true; 61

JavaScript Type System This compiles: var n = 1; var s = “WebGL”; var b = true; var sum = n + s + b; 62

JavaScript is a functional language… 63

JavaScript Functions Looks familiar: Functions are first-class objects, so… function add(x, y) { return x + y; } var sum = add(1, 2); 64

JavaScript Functions Functions are objects: var add = function(x, y) { return x + y; }; var sum = add(1, 2); 65

JavaScript Functions Pass functions to functions: var add = function //... function execute(op, x, y) { return op(x, y); } var sum = execute(add, 1, 2); 66

JavaScript Anonymous Functions Why name functions? function execute(op, x, y) //... var sum = execute(function(x, y) { return x + y; }, 1, 2); 67

JavaScript Closures Why limit scope? var z = 3; var sum = execute(function(x, y) { return x + y + z; }, 1, 2); 68

JavaScript is a dynamic language… 69

JavaScript Object Literals Who needs struct ? Create objects on the fly: var position = { x : 1.0, y : 2.0 }; 70

JavaScript Object Literals Why not add fields on the fly too? var position = { x : 1.0, y : 2.0 }; position.z = 3.0; 71

JavaScript Object Literals Who needs class ? 72

JavaScript Object Literals Who needs class ? Create functions too: var position = { x : 1.0, y : 2.0, min : function() { return Math.min(this.x, this.y); } }; 73

JavaScript Object Literals Why not change min() ? position.z = 3.0; position.min = function() { return Math.min(this.x, this.y, this.z); }; 74

JavaScript Object Literals Useful for passing to functions. Why? 75

JavaScript Object Literals Useful for passing to functions. Why? What do these arguments mean? pick(322, 40, 5, 4); 76

JavaScript Object Literals Useful for passing to functions. Why? What do these arguments mean? pick({ x : 322, y : 40, width : 5, height : 4 }); 77

JavaScript does object-oriented… 78

JavaScript Constructor Functions function Vector(x, y) { this.x = x; this.y = y; } var v = new Vector(1, 2); 79

JavaScript Constructor Functions function Vector(x, y) { this.x = x; this.y = y; this.min = function() { return Math.min(this.x, this.y); }; } Objects can have functions: 80

JavaScript Constructor Functions function Vector(x, y) { this.x = x; this.y = y; } Vector.prototype.min = function() { return Math.min(this.x, this.y); }; Objects have prototypes: 81

JavaScript Polymorphism No need for virtual functions function draw(model) { model.setRenderState(); model.render(); } 82

JavaScript Polymorphism No need for virtual functions var level = { setRenderState : function() //... render : function() //... }; draw(level); // Just works 83

JavaScript Build Pipeline See ConcatenateMinify Different than C++ Goal: fast downloads Common: Alternative: fine-grain modules How do you deploy shaders?.js files One.js file “Compressed”.js file 84

JavaScript Advice Use JSHint Have excellent test coverage Use the Chrome and Firefox debuggers 85

JavaScript Resources I promise I do not work for O'Reilly or Yahoo 86