-
Notifications
You must be signed in to change notification settings - Fork 4
/
webgl.html
1279 lines (1191 loc) · 44 KB
/
webgl.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
Google IO 2012 HTML5 Slide Template
Authors: Eric Bidelman <[email protected]>
Luke Mahé <[email protected]>
URL: https://code.google.com/p/io-2012-slides
Slightly modified to create SIGGRAPH 2012 slide template
Author: Kenneth Russell <[email protected]>
URL: https://github.com/kenrussell/siggraph2012course
-->
<!DOCTYPE html>
<html>
<head>
<!-- Title is populated with content in slide_config.js -->
<title>SIGGRAPH 2012</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">-->
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0">-->
<!--This one seems to work all the time, but really small on ipad-->
<!--<meta name="viewport" content="initial-scale=0.4">-->
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="theme/css/default.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="theme/css/phone.css">
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->
<script data-main="js/slides" src="js/require-1.0.8.min.js"></script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<slide class="fill nobackground" style="background-image: url(images/siggraph2012-title.jpg)">
</slide>
<slide class="title-slide segue nobackground">
<aside class="sglogobar"><img src="images/siggraph2012-logo-with-alpha.png"></aside>
<!-- The content of this hgroup is replaced programmatically through slide_config.js. -->
<hgroup class="auto-fadein">
<h1 data-config-title><!-- populated from slide_config.json --></h1>
<h2 data-config-subtitle><!-- populated from slide_config.json --></h2>
<p data-config-presenter><!-- populated from slide_config.json --></p>
</hgroup>
</slide>
<slide>
<hgroup>
<h2>Introduction</h2>
</hgroup>
<article>
<ul>
<li>WebGL brings 3D graphics to the HTML5 platform
<ul>
<li>Plugin free: never lose a user because they are afraid to download and install something from the web</li>
<li>Based on OpenGL ES 2.0: same for desktops, laptops, mobile devices, etc</li>
<li>Secure: ensure no out of bounds or uninitialized memory accesses</li>
</ul>
</li>
<li>WebGL is an alternative rendering context for the HTML5 Canvas element</li>
<li>WebGL = Javascript + Shaders
<ul>
<li>Shaders - small programs that execute on the GPU - determine position of each triangle and color of each pixel </li>
</ul>
</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Security</h2>
</hgroup>
<article>
<ul>
<li>Validate all input parameters/data in WebGL
<ul>
<li>Never leak driver functionality that's not supported in the WebGL spec</li>
<li>Out-of-bounds data access detection</li>
<li>Initialize all allocated objects</li>
</ul>
</li>
<li>Deal with driver bugs
<ul>
<li>Work around where possible</li>
<li>Browsers actively maintain a blacklist</li>
<li>Work with driver vendors to fix bugs</li>
<li>Comprehensive conformance test suite</li>
</ul>
</li>
<li>Terminate long-running content (accidental or malicious)</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Programming Model</h2>
</hgroup>
<article>
<p align="center"><img src="images/stream-processing-with-shaders.svg" height="200px" alt="Shader pipeline" title="Shader pipeline"></p>
<ul>
<li>The GPU is a stream processor</li>
<li>Vertex attributes: each point in 3D space has one or more streams of data associated with it
<ul>
<li>Position, surface normal, color, texture coordinate, ...</li>
</ul>
</li>
<li>These streams of data flow through the vertex and fragment shaders</li>
<li>Shaders are small, stateless programs which run on the GPU with a high degree of parallelism</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Vertex Shader</h2>
</hgroup>
<article>
<ul>
<li>Vertex shader is applied to each vertex of each triangle</li>
<li>Its primary goal is to output the location where the vertex should appear in the on-screen window</li>
<li>May also output one or more additional values — <b>varying variables</b> — to the fragment shader</li>
</ul>
<img style="position:absolute; right:20px; bottom:50px;" src="images/vertex-and-fragment-shader-diagram.svg" height="180px" alt="Description" title="Description">
</article>
</slide>
<slide>
<hgroup>
<h2>Vertex Shader -> Fragment Shader</h2>
</hgroup>
<article>
<ul>
<li>The outputs of vertex shader are the inputs of fragment shader, but not directly</li>
<li>For each triangle, the GPU figures out which pixels on the screen are covered by the triangle</li>
<li>At each pixel, GPU automatically blends outputs of the vertex shader based on where the pixel lies within the triangle</li>
</ul>
<img style="position:absolute; left:50px; bottom:180px;" src="images/stream-processing-with-shaders.svg" height="200px" alt="Shader pipeline" title="Shader pipeline">
<img style="position:absolute; left:330px; bottom:220px;" src="images/red_arrow.png" height="75px" alt="Arrow" title="Arrow">
<img style="position:absolute; right:20px; bottom:50px;" src="images/vertex-and-fragment-shader-diagram.svg" height="180px" alt="Description" title="Description">
</article>
</slide>
<slide>
<hgroup>
<h2>Fragment Shader</h2>
</hgroup>
<article>
<ul>
<li>GPU then runs the fragment shader on each of those pixels</li>
<li>Fragment shader then determines the color of the pixel based on those inputs</li>
</ul>
<img style="position:absolute; right:20px; bottom:50px;" src="images/vertex-and-fragment-shader-diagram.svg" height="180px" alt="Description" title="Description">
</article>
</slide>
<slide>
<hgroup>
<h2>Getting Data on to the GPU</h2>
</hgroup>
<article>
<ul>
<li>Vertex data is uploaded in to one or more buffer objects</li>
<li>The vertex attributes in the vertex shader are bound to the data in these buffer objects</li>
</ul>
<p align="center"><img src="images/buffer-objects-and-vertex-shaders.svg" height="360px" alt="Description" title="Description"></p>
</article>
</slide>
<slide>
<hgroup>
<h2>A Concrete Example</h2>
</hgroup>
<article>
<ul>
<li>Adapted from Giles Thomas' <a href="http://learningwebgl.com/blog/?p=134">Learning WebGL Lesson 2</a></li>
<li>Code is checked in to the <a href="http://code.google.com/p/webglsamples/">webglsamples project</a> under hello-webgl/</li>
<li>May be <a href="http://webglsamples.googlecode.com/hg/hello-webgl/hello-webgl.html">viewed directly</a> in a WebGL-enabled browser
<li>Goal of this example is to de-mystify WebGL by showing all of the steps necessary to draw a colored triangle on the screen</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>A Concrete Example</h2>
</hgroup>
<article>
<iframe data-src="hello-webgl.html"></iframe>
</article>
</slide>
<slide>
<hgroup>
<h2>The Big Picture</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="html">
<html><head>
<title>Hello, WebGL (adopted from Learning WebGL lesson 2)</title>
<script id="shader-fs" type="x-shader/x-fragment">
... // Vertex shader source code
</script>
<script id="shader-vs" type="x-shader/x-vertex">
... // Fragment shader source code
</script>
<script type="text/javascript">
... // WebGL source code
</script>
</head>
<body onload="webGLStart();">
<canvas id="lesson02-canvas" style="border: none;" width="400" height="400"></canvas>
</body>
</html>
</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Vertex Shader</h2>
</hgroup>
<article>
<pre class="prettyprint lang-glsl" data-lang="glsl">
attribute vec3 positionAttr;
attribute vec4 colorAttr;
varying vec4 vColor;
void main(void) {
gl_Position = vec4(positionAttr, 1.0);
vColor = colorAttr;
}
</pre>
<ul>
<li>In this example, the vertex shader will only execute three times (for one triangle)</li>
<li>In a typical application, thousands or tens of thousands of triangles are usually drawn together</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Fragment Shader</h2>
</hgroup>
<article>
<pre class="prettyprint lang-glsl" data-lang="glsl">
precision mediump float;
varying vec4 vColor;
void main(void) {
gl_FragColor = vColor;
}
</pre>
<ul>
<li>Value of vColor <b>varying variable</b> is a weighted combination of the colors specified at the three input vertices</li>
<li>Based on the location of the pixel within the triangle, GPU automatically blends colors that were specified at each vertex</li>
<li>Fragment shader executes between a dozen to tens of thousands of times, depending on how many pixels the triangle covers</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Embedding Shaders</h2>
</hgroup>
<article>
<ul>
<li>Shaders in this example are embedded in the web page using script elements</li>
<li>It is entirely up to the application how to manage the sources for its shaders</li>
<li>Script tags were chosen to hold the shaders for this example for simplicity</li>
<li>Real world application might download shaders using XMLHttpRequest, generate shaders in JavaScript, etc.</li>
</ul>
<pre class="prettyprint" data-lang="html">
<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></pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Initializing WebGL</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
var gl = null;
try {
gl = canvas.getContext("webgl");
if (!gl)
gl = canvas.getContext("experimental-webgl");
} catch (e) {}
if (!gl)
alert("Could not initialise WebGL, sorry :-(");</pre>
<ul>
<li>Not the best error detection logic; kept short for simplicity</li>
<li>Consult <a href="http://webglsamples.googlecode.com">WebGL samples</a> for better examples</li>
<li>Link to <a href="http://get.webgl.org/">http://get.webgl.org/</a> if initialization fails</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Loading a Shader</h2>
</hgroup>
<article>
<ul>
<li>Create the shader object – vertex or fragment</li>
<li>Specify its source code</li>
<li>Compile it</li>
<li>Check whether compilation succeeded</li>
<li>Complete code follows; some error checking elided</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Loading a Shader</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
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;
}</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Loading a Program</h2>
</hgroup>
<article>
<ul>
<li>A program object combines the vertex and fragment shaders</li>
<li>Load each shader separately</li>
<li>Attach each to the program</li>
<li>Link the program</li>
<li>Check whether linking succeeded</li>
<li>Prepare vertex attributes for later assignment</li>
<li>Complete code follows</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Loading a Program</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
var program;
function initShaders() {
var vertexShader = getShader(gl, "shader-vs");
var fragmentShader = getShader(gl, "shader-fs");
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);
}</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Setting up Geometry</h2>
</hgroup>
<article>
<ul>
<li>Independent step from initialization of shaders and program; could just as easily be done before</li>
<li>Allocate buffer object on the GPU</li>
<li>Upload geometric data containing all vertex streams</li>
<li>Many options: interleaved vs. non-interleaved data, using multiple buffer objects, etc.</li>
<li>Generally, want to use as few buffer objects as possible; switching is expensive</li>
<li>Complete code follows</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Setting up Geometry</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
var buffer;
function initGeometry() {
buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
// Interleave vertex positions and colors
var vertexData = [
// X Y Z R G B A
0.0, 0.8, 0.0, 1.0, 0.0, 0.0, 1.0,
// X Y Z R G B A
-0.8, -0.8, 0.0, 0.0, 1.0, 0.0, 1.0,
// X Y Z R G B A
0.8, -0.8, 0.0, 0.0, 0.0, 1.0, 1.0
];
gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(vertexData), gl.STATIC_DRAW);
}</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Drawing the Scene</h2>
</hgroup>
<article>
<ul>
<li>Ready to draw the scene at this point</li>
<li>Clear the viewing area</li>
<li>Set up vertex attribute streams</li>
<li>Issue the draw call</li>
<li>Complete code follows</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Drawing the Scene</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
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);
}</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Using Textures</h2>
</hgroup>
<article>
<iframe data-src="hello-webgl-texture.html"></iframe>
</article>
</slide>
<slide>
<hgroup>
<h2>Shaders Using a Texture</h2>
</hgroup>
<article>
<pre class="prettyprint lang-glsl" data-lang="vertex shader">
attribute vec3 positionAttr;
attribute vec2 texCoordAttr;
varying vec2 texCoord;
void main(void) {
gl_Position = vec4(positionAttr, 1.0);
texCoord = texCoordAttr;
}
</pre>
<pre class="prettyprint lang-glsl" data-lang="fragment shader">
precision mediump float;
uniform sampler2D tex;
varying vec2 texCoord;
void main(void) {
gl_FragColor = texture2D(tex, texCoord);
}
</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Setting up Geometry with Texture Coords</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
var buffer;
function initGeometry() {
buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
// Interleave vertex positions and texture coordinates
var vertexData = [
// X Y Z U V
0.0, 0.8, 0.0, 0.5, 1.0,
// X Y Z U V
-0.8, -0.8, 0.0, 0.0, 0.0,
// X Y Z U V
0.8, -0.8, 0.0, 1.0, 0.0,
];
gl.bufferData(gl.ARRAY_BUFFER,
new Float32Array(vertexData), gl.STATIC_DRAW);
}</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Setting up a Texture</h2>
</hgroup>
<article>
<ul>
<li>Create a texture object, and set up parameters</li>
<li>Download an image from web and wait</li>
<li>When an image is loaded, upload the image data to the texture</li>
<li>Draw with the texture</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Setting up a Texture</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
function loadTexture(src) {
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
var image = new Image();
image.onload = function() {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
drawScene();
};
image.src = src; // Start downloading the image by setting its source.
return texture;
}
</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>Draw with a Texture</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
function drawScene() {
... // Same as hello-webgl example
// Bind the texture to texture unit 0
gl.bindTexture(gl.TEXTURE_2D, texture);
// Point the uniform sampler to texture unit 0
// NOTE: you should fetch this uniform location once and cache it
// Only written this way for simplicity
var textureLoc = gl.getUniformLocation(program, "tex");
gl.uniform1i(textureLoc, 0);
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
</pre>
</article>
</slide>
<slide>
<hgroup>
<h2>What Else?</h2>
</hgroup>
<article>
<ul>
<li>WebGL specific: handling context lost and recovery</li>
<li>Regular graphics stuff:
<ul>
<li>Matrix: projection, transformation, ...</li>
<li>Lighting</li>
<li>Animation</li>
<li>Interaction</li>
<li>...</li>
</ul>
</li>
</ul>
<p align="center"><img src="images/aquarium.png" height="250px" alt="Aquarium" title="Aquarium"></p>
</article>
</slide>
<slide>
<hgroup>
<h2>Higher-Level Libraries</h2>
</hgroup>
<article>
<ul>
<li>Now that we've dragged you through a complete example...</li>
<li>Many libraries already exist to make it easier to use WebGL</li>
<li>A few suggestions:
<ul>
<li><a href="https://github.com/mrdoob/three.js">Three.js</a> (used in the <a href="http://ro.me">Rome</a> demo, <a href="http://mrdoob.github.com/three.js/">mr. doob's demos</a>, and more)</li>
<li><a href="http://www.cubicvr.org/">CubicVR</a> (used in Mozilla's WebGL demos such as <a href="https://developer.mozilla.org/en-US/demos/detail/no-comply/launch">No Comply</a>)</li>
<li><a href="http://threedlibrary.googlecode.com">TDL</a> (used in the WebGL Aquarium and most of the other webglsamples demos)</li>
<li><a href="http://www.ambiera.com/copperlicht/">CopperLicht</a> (same developer as Irrlicht)</li>
<li><a href="http://www.senchalabs.org/philogl/">PhiloGL</a> (focus on data visualization)</li>
<li><a href="http://www.glge.org/">GLGE</a> (used for early prototypes of Google Body)</li>
<li><a href="http://www.scenejs.com/">SceneJS</a> (unique and interesting declarative syntax)</li>
<li><a href="http://spidergl.org/">SpiderGL</a> (lots of interesting visual effects)</li>
</ul>
</li>
</ul>
</article>
</slide>
<slide class="segue dark nobackground">
<aside class="sglogobar"><img src="images/siggraph2012-logo-with-alpha.png"></aside>
<hgroup class="auto-fadein">
<h2>Break</h2>
</hgroup>
</slide>
<slide>
<hgroup>
<h2>Achieving High Performance</h2>
</hgroup>
<article>
<ul>
<li>"Big rule" associated with OpenGL programs:
<ul>
<li>Reduce the number of draw calls per frame
<ul>
<li>OpenGL's efficiency comes from sending large amounts of geometry to the GPU with very little overhead</li>
<li>Sending down small batches — or worse, one or two triangles per draw call — does not give the GPU opportunity to optimize</li>
</ul>
</li>
</ul>
</li>
<li>In order to draw many triangles at ones, it's usually necessary to sort objects in the scene by rendering state
<ul>
<li>For example, draw all objects using the same texture at once
</ul>
</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>State Sorting</h2>
</hgroup>
<article>
<img style="float: right;" src="images/relative-cost-of-operations.svg" width="450px" alt="Description" title="Description">
<ul>
<li>Objects should be sorted and drawn according to the following criteria, in decreasing order of importance:
<ul>
<li>Target framebuffer or context state
<ul>
<li>Blending, clipping, depth test, etc.</li>
</ul>
</li>
<li>Program, buffer, or texture
<ul>
<li>Switching these often requires a pipeline flush</li>
</ul>
</li>
<li>Uniforms and samplers
<ul>
<li>Switching these is relatively cheap, modulo JavaScript overhead</li>
</ul>
</li>
</ul>
</li>
</ul>
<footer class="source">source: Ben Vanik, Google</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>State Sorting</h2>
</hgroup>
<article>
<ul>
<li>If possible, sort scene ahead of time, maintain as a sorted list
<li>Walking the object hierarchy and re-sorting each frame can cancel gains from batching
<li>Generate content (models, etc.) so that they can be easily batched
<ul>
<li>Merge buffers
<li>Use texture atlases
<li>...
</ul>
</li>
</ul>
<footer class="source">source: Ben Vanik, Google</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>Example Structure of Drawing a Frame</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
gl.enable(gl.DEPTH_TEST);
gl.depthMask(true);
gl.disable(gl.BLEND);
// Draw opaque content
gl.depthMask(false);
gl.enable(gl.BLEND);
// Draw translucent content
gl.disable(gl.DEPTH_TEST);
// Draw UI</pre>
<img class="centered" src="images/frame-structure.svg" height="175px" alt="Description" title="Description">
<footer class="source">source: Ben Vanik, Google</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>JavaScript Performance</h2>
</hgroup>
<article>
<ul>
<li>JavaScript performance has improved dramatically over the past several years
<li>In particular, for 3D graphics use cases
<li>Already possible to generate many vertices from JavaScript every frame and send them to the graphics card
<ul>
<li><a href="https://www.khronos.org/registry/webgl/sdk/demos/google/nvidia-vertex-buffer-object/index.html">NVIDIA vertex buffer object demo</a> generates and uploads ~10 million vertices per second
</ul>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>WebGL Performance</h2>
</hgroup>
<article>
<ul>
<li>Still, in WebGL, all of the OpenGL "big rules" apply, along with another one:
<ul>
<li>Offload as much JavaScript to the GPU as possible, within reason</li>
</ul>
</li>
<li>Often, the GPU can be used to rephrase a computation that would otherwise need to be done on the CPU</li>
<li>Doing so can achieve not only better parallelism but also better performance</li>
<li>The following examples show how this rule was applied in some real-world scenarios</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Picking in Google Body</h2>
</hgroup>
<article>
<ul>
<li>Google Body is a browser for the human anatomy</li>
<li>Originally developed at Google Labs, it is now available
as <a href="http://www.zygotebody.com/">Zygote Body</a>, from
the company which developed the 3D models</li>
<li>Models in application are highly detailed — over a million triangles — yet selection is very fast</li>
<li>Click any body part to highlight it and see its name</li>
</ul>
<footer class="source">source: Google Body team</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>Picking in Google Body</h2>
</hgroup>
<article>
<ul>
<li>How to implement picking?</li>
<li>Could consider doing ray-casting in JavaScript
<ul>
<li>Attempt to do quick discards if ray doesn't intersect bounding box</li>
</ul>
<li>Still a lot of math to do in JavaScript</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Picking in Google Body</h2>
</hgroup>
<article>
<ul>
<li>Instead, Google Body uses the GPU to implement picking</li>
<li>When model is loaded, assign different color to each organ</li>
<li>Upon mouse click:
<ul>
<li>Render body offscreen with different set of shaders</li>
<li>Use threshold to determine whether to draw translucent layers</li>
<li>Read back color of pixel under mouse pointer</li>
</ul>
</li>
<li>Same technique works at different levels of granularity
<ul>
<li>Each triangle, rather than each object, could be assigned a different color to achieve finer detail when picking
</ul>
</li>
</ul>
<footer class="source">source: Google Body team</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>Picking in Google Body</h2>
</hgroup>
<article class="flexbox vcenter">
<img src="images/body-selection.png" height="550px" alt="Description" title="Description">
<footer class="source">source: Google Body team</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>Picking in Google Body</h2>
</hgroup>
<article>
<ul>
<li>Note that this technique uses the GPU for what it's best at: rendering</li>
<li>Essentially converts problem of picking into one of rendering</li>
<li>Despite readback from GPU to CPU at end of algorithm, performance gains are worth it</li>
</ul>
<footer class="source">source: Google Body team</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>Particle Systems</h2>
</hgroup>
<article>
<ul>
<li>Particle systems are a technique commonly used to draw graphical effects like explosions, smoke, clouds, and dust</li>
<li>Most obvious way to implement a particle system:
<ul>
<li>Compute positions of particles on the CPU</li>
<li>Upload the vertices to the GPU</li>
<li>Draw them in a single draw call</li>
</ul>
</li>
<li>This technique can work for small particle systems, but does not scale well because many vertices are uploaded to the GPU each frame</li>
<li>Additionally, mathematical operations are not yet as fast in JavaScript as they are in C or C++</li>
<li>A different technique is desired</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Particle Systems</h2>
</hgroup>
<article>
<ul>
<li><a href="http://games.greggman.com/game/">Gregg Tavares</a> has developed a <a href="https://cvs.khronos.org/svn/repos/registry/trunk/public/webgl/sdk/demos/google/particles/index.html">particle system demonstration</a> in the <a href="http://www.khronos.org/webgl/wiki/Demo_Repository">WebGL Demo Repository</a></li>
<li>Animates roughly 2000 particles at 60 frames per second</li>
<li>Does all animation math on the GPU</li>
</ul>
<img class="centered" height="350px" src="images/particles.png" alt="Description" title="Description">
<footer class="source">source: Gregg Tavares, Google</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>Particle Systems</h2>
</hgroup>
<article>
<ul>
<li>Each particle's motion is defined by an equation
<ul>
<li>Initial position, velocity, acceleration, spin, lifetime</li>
</ul>
</li>
<li>Set up motion parameters when particle is created</li>
<li>Send down one parameter — time — each frame</li>
<li>Vertex shader evaluates equation of motion, moves particle</li>
<li>Absolute minimum amount of JavaScript work done per frame</li>
</ul>
<footer class="source">source: Gregg Tavares, Google</footer>
</article>
</slide>
<slide>
<hgroup>
<h2>Particle Systems</h2>
</hgroup>
<article>
<ul>
<li><a href="http://blog.nihilogic.dk/">Jacob Seidelin's</a> <a href="http://www.nihilogic.dk/labs/worlds_of_webgl/">Worlds of WebGL</a> demo shows a similar particle system technique</li>
<li>Particles assemble to form various shapes, falling to the floor between scenes, animating smoothly between them</li>
<li>Animation is done similarly to Gregg Tavares’ particle system</li>
<li>For each scene, random positions for the particles are chosen at setup time</li>
<li>Time parameter interpolates between two vertex attribute streams at any given time
<ul>
<li>One stream contains the particle positions on the floor</li>
<li>The other contains the particle positions for the current shape</li>
</ul>
</li>
<li>Once the current shape has been assembled, next interpolation target becomes the particles on the floor again</li>
<li>JavaScript does almost no computation</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Sprite Engines</h2>
</hgroup>
<article>
<ul>
<li>In early 2011, Facebook released <a href="https://github.com/facebook/jsgamebench/">JSGameBench</a> sprite engine benchmark
<ul>
<li>"Sprites" terminology more commonly used when authoring certain kinds of 2D games; similar to particle system
</ul>
<li>Compared various techniques for rendering animated sprites within a web browser
<ul>
<li>CSS-transformed Images
<li>2D Canvas
<li>WebGL
</ul>
<li>JSGameBench doesn’t appear to be under active development any more, but some lessons can be learned about its structure and performance characteristics
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Sprite Engines</h2>
</hgroup>
<article>
<ul>
<li>At the time JSGameBench was released, several inefficiencies were identified in its WebGL backend
<ul>
<li>Did one draw call per sprite
<li>Set three uniform variables per sprite, including position
<li>On average, bound one texture per sprite
</ul>
<li>Seemed that drawing the entire sprite field with one draw call would be a big performance win
<li>Built <a href="http://webglsamples.googlecode.com/hg/sprites/index.html">prototype sprite engine</a> to test this hypothesis
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Alpha Blending and Draw Order</h2>
</hgroup>
<article>
<ul>
<li>Might seem impossible, because sprites require alpha blending and must be drawn in a particular order
<li>Little known fact: OpenGL's DrawArrays and DrawElements guarantee triangles are drawn in order
<ul>
<li>Apparently GPUs contain quite a bit of silicon in the Render Output Unit (ROP) to provide this guarantee
<li>Thanks to Nat Duca of Google for this information
</ul>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Batching Sprites</h2>
</hgroup>