summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/vertex_arrays/vertex-array-object.html
blob: 160a7f4ea607418ba9143659fa094ec3055fc698 (plain)
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
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL vertex_array_object Conformance Tests</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 a_position;
attribute vec4 a_color;
varying vec4 v_color;
void main(void) {
    gl_Position = a_position;
    v_color = a_color;
}
</script>
<script id="fshader" type="x-shader/x-fragment">
precision mediump float;
varying vec4 v_color;
void main(void) {
    gl_FragColor = v_color;
}
</script>
<script>
"use strict";
description("This test verifies the functionality of the Vertex Array Objects.");

debug("");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, null, 2);
var vao = null;

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");

    runBindingTest();
    runObjectTest();
    runAttributeTests();
    runAttributeValueTests();
    runDrawTests();
    runUnboundDeleteTests();
    runBoundDeleteTests();
    runArrayBufferBindTests();
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
}

function runBindingTest() {
    debug("Testing binding enum");

    shouldBe("gl.VERTEX_ARRAY_BINDING", "0x85B5");

    gl.getParameter(gl.VERTEX_ARRAY_BINDING);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "VERTEX_ARRAY_BINDING query should succeed");

    // Default value is null
    if (gl.getParameter(gl.VERTEX_ARRAY_BINDING) === null) {
        testPassed("Default value of VERTEX_ARRAY_BINDING is null");
    } else {
        testFailed("Default value of VERTEX_ARRAY_BINDING is not null");
    }

    debug("Testing binding a VAO");
    var vao0 = gl.createVertexArray();
    var vao1 = gl.createVertexArray();
    shouldBeNull("gl.getParameter(gl.VERTEX_ARRAY_BINDING)");
    gl.bindVertexArray(vao0);
    if (gl.getParameter(gl.VERTEX_ARRAY_BINDING) == vao0) {
        testPassed("gl.getParameter(gl.VERTEX_ARRAY_BINDING) is expected VAO");
    } else {
        testFailed("gl.getParameter(gl.VERTEX_ARRAY_BINDING) is not expected VAO")
    }
    gl.bindVertexArray(vao1);
    if (gl.getParameter(gl.VERTEX_ARRAY_BINDING) == vao1) {
        testPassed("gl.getParameter(gl.VERTEX_ARRAY_BINDING) is expected VAO");
    } else {
        testFailed("gl.getParameter(gl.VERTEX_ARRAY_BINDING) is not expected VAO")
    }
    gl.deleteVertexArray(vao1);
    shouldBeNull("gl.getParameter(gl.VERTEX_ARRAY_BINDING)");
    gl.bindVertexArray(vao1);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "binding a deleted vertex array object");
    gl.bindVertexArray(null);
    shouldBeNull("gl.getParameter(gl.VERTEX_ARRAY_BINDING)");
    gl.deleteVertexArray(vao1);
}

function runObjectTest() {
    debug("Testing object creation");

    vao = gl.createVertexArray();
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "createVertexArray should not set an error");
    shouldBeNonNull("vao");

    // Expect false if never bound
    shouldBeFalse("gl.isVertexArray(vao)");
    gl.bindVertexArray(vao);
    shouldBeTrue("gl.isVertexArray(vao)");
    gl.bindVertexArray(null);
    shouldBeTrue("gl.isVertexArray(vao)");

    shouldBeFalse("gl.isVertexArray(null)");

    gl.deleteVertexArray(vao);
    vao = null;
}

function runAttributeTests() {
    debug("Testing attributes work across bindings");

    var states = [];

    var attrCount = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
    for (var n = 0; n < attrCount; n++) {
        gl.bindBuffer(gl.ARRAY_BUFFER, null);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

        var state = {};
        states.push(state);

        var vao = state.vao = gl.createVertexArray();
        gl.bindVertexArray(vao);

        var enableArray = (n % 2 == 0);
        if (enableArray) {
            gl.enableVertexAttribArray(n);
        } else {
            gl.disableVertexAttribArray(n);
        }

        if (enableArray) {
            var buffer = state.buffer = gl.createBuffer();
            gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
            gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW);

            gl.vertexAttribPointer(n, 1 + n % 4, gl.FLOAT, true, n * 4, n * 4);
        }

        if (enableArray) {
            var elbuffer = state.elbuffer = gl.createBuffer();
            gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elbuffer);
            gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 1024, gl.STATIC_DRAW);
        }

        gl.bindVertexArray(null);
    }

    var anyMismatch = false;
    for (var n = 0; n < attrCount; n++) {
        var state = states[n];

        gl.bindVertexArray(state.vao);

        var shouldBeEnabled = (n % 2 == 0);
        var isEnabled = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED);
        if (shouldBeEnabled != isEnabled) {
            testFailed("VERTEX_ATTRIB_ARRAY_ENABLED not preserved");
            anyMismatch = true;
        }

        var buffer = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
        if (shouldBeEnabled) {
            if (buffer == state.buffer) {
                // Matched
                if ((gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_SIZE) == 1 + n % 4) &&
                    (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_TYPE) == gl.FLOAT) &&
                    (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED) == true) &&
                    (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_STRIDE) == n * 4) &&
                    (gl.getVertexAttribOffset(n, gl.VERTEX_ATTRIB_ARRAY_POINTER) == n * 4)) {
                    // Matched
                } else {
                    testFailed("VERTEX_ATTRIB_ARRAY_* not preserved");
                    anyMismatch = true;
                }
            } else {
                testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved");
                anyMismatch = true;
            }
        } else {
            // GL_CURRENT_VERTEX_ATTRIB is not preserved
            if (buffer) {
                testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved");
                anyMismatch = true;
            }
        }

        var elbuffer = gl.getParameter(gl.ELEMENT_ARRAY_BUFFER_BINDING);
        if (shouldBeEnabled) {
            if (elbuffer == state.elbuffer) {
                // Matched
            } else {
                testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved");
                anyMismatch = true;
            }
        } else {
            if (elbuffer == null) {
                // Matched
            } else {
                testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved");
                anyMismatch = true;
            }
        }
    }
    gl.bindVertexArray(null);
    if (!anyMismatch) {
        testPassed("All attributes preserved across bindings");
    }

    for (var n = 0; n < attrCount; n++) {
        var state = states[n];
        gl.deleteVertexArray(state.vao);
    }
}

function runAttributeValueTests() {
    debug("Testing that attribute values are not attached to bindings");

    var v;
    var vao0 = gl.createVertexArray();
    var anyFailed = false;

    gl.bindVertexArray(null);
    gl.vertexAttrib4f(0, 0, 1, 2, 3);

    v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
    if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) {
        testFailed("Vertex attrib value not round-tripped?");
        anyFailed = true;
    }

    gl.bindVertexArray(vao0);

    v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
    if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) {
        testFailed("Vertex attrib value reset across bindings");
        anyFailed = true;
    }

    gl.vertexAttrib4f(0, 4, 5, 6, 7);
    gl.bindVertexArray(null);

    v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
    if (!(v[0] == 4 && v[1] == 5 && v[2] == 6 && v[3] == 7)) {
        testFailed("Vertex attrib value bound to buffer");
        anyFailed = true;
    }

    if (!anyFailed) {
        testPassed("Vertex attribute values are not attached to bindings")
    }

    gl.bindVertexArray(null);
    gl.deleteVertexArray(vao0);
}

function runDrawTests() {
    debug("Testing draws with various VAO bindings");

    canvas.width = 50; canvas.height = 50;
    gl.viewport(0, 0, canvas.width, canvas.height);

    var vao0 = gl.createVertexArray();
    var vao1 = gl.createVertexArray();

    var opt_positionLocation = 0;
    var opt_texcoordLocation = 1;

    var program = wtu.setupSimpleTextureProgram(gl, opt_positionLocation, opt_texcoordLocation);

    function setupQuad(s) {
        var vertexObject = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
             1.0 * s,  1.0 * s, 0.0,
            -1.0 * s,  1.0 * s, 0.0,
            -1.0 * s, -1.0 * s, 0.0,
             1.0 * s,  1.0 * s, 0.0,
            -1.0 * s, -1.0 * s, 0.0,
             1.0 * s, -1.0 * s, 0.0]), gl.STATIC_DRAW);
        gl.enableVertexAttribArray(opt_positionLocation);
        gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0);

        var vertexObject = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
            1.0 * s, 1.0 * s,
            0.0 * s, 1.0 * s,
            0.0 * s, 0.0 * s,
            1.0 * s, 1.0 * s,
            0.0 * s, 0.0 * s,
            1.0 * s, 0.0 * s]), gl.STATIC_DRAW);
        gl.enableVertexAttribArray(opt_texcoordLocation);
        gl.vertexAttribPointer(opt_texcoordLocation, 2, gl.FLOAT, false, 0, 0);
    };

    function readLocation(x, y) {
        var pixels = new Uint8Array(1 * 1 * 4);
        gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
        return pixels;
    };
    function testPixel(blockList, allowList) {
        function testList(list, expected) {
            for (var n = 0; n < list.length; n++) {
                var l = list[n];
                var x = -Math.floor(l * canvas.width / 2) + canvas.width / 2;
                var y = -Math.floor(l * canvas.height / 2) + canvas.height / 2;
                var source = readLocation(x, y);
                if (Math.abs(source[0] - expected) > 2) {
                    return false;
                }
            }
            return true;
        }
        return testList(blockList, 0) && testList(allowList, 255);
    };
    function verifyDraw(drawNumber, s) {
        wtu.clearAndDrawUnitQuad(gl);
        var blockList = [];
        var allowList = [];
        var points = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0];
        for (var n = 0; n < points.length; n++) {
            if (points[n] <= s) {
                blockList.push(points[n]);
            } else {
                allowList.push(points[n]);
            }
        }
        if (testPixel(blockList, allowList)) {
            testPassed("Draw " + drawNumber + " passed pixel test");
        } else {
            testFailed("Draw " + drawNumber + " failed pixel test");
        }
    };

    // Setup all bindings
    setupQuad(1);
    gl.bindVertexArray(vao0);
    setupQuad(0.5);
    gl.bindVertexArray(vao1);
    setupQuad(0.25);

    // Verify drawing
    gl.bindVertexArray(null);
    verifyDraw(0, 1);
    gl.bindVertexArray(vao0);
    verifyDraw(1, 0.5);
    gl.bindVertexArray(vao1);
    verifyDraw(2, 0.25);

    gl.bindVertexArray(null);
    gl.deleteVertexArray(vao0);
    gl.deleteVertexArray(vao1);

    // Disable global vertex attrib array
    gl.disableVertexAttribArray(opt_positionLocation);
    gl.disableVertexAttribArray(opt_texcoordLocation);

    // Draw with values.
    var positionLoc = 0;
    var colorLoc = 1;
    var gridRes = 1;
    wtu.setupIndexedQuad(gl, gridRes, positionLoc);
    // Set the vertex color to red.
    gl.vertexAttrib4f(colorLoc, 1, 0, 0, 1);

    var vao0 = gl.createVertexArray();
    gl.bindVertexArray(vao0);
    var program = wtu.setupSimpleVertexColorProgram(gl, positionLoc, colorLoc);
    wtu.setupIndexedQuad(gl, gridRes, positionLoc);
    // Set the vertex color to green.
    gl.vertexAttrib4f(colorLoc, 0, 1, 0, 1);
    wtu.clearAndDrawIndexedQuad(gl, gridRes);
    wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green")
    gl.deleteVertexArray(vao0);
    wtu.clearAndDrawIndexedQuad(gl, gridRes);
    wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green")
}

function runUnboundDeleteTests() {
    debug("Testing using buffers that are deleted when attached to unbound VAOs");

    var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_position", "a_color"]);
    gl.useProgram(program);

    var positionBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
    gl.bufferData(
        gl.ARRAY_BUFFER,
        new Float32Array([
           1.0,  1.0,
          -1.0,  1.0,
          -1.0, -1.0,
           1.0, -1.0]),
        gl.STATIC_DRAW);

    var colors = [
      [255,   0,   0, 255],
      [  0, 255,   0, 255],
      [  0,   0, 255, 255],
      [  0, 255, 255, 255]
    ];
    var colorBuffers = [];
    var elementBuffers = [];
    var vaos = [];
    for (var ii = 0; ii < colors.length; ++ii) {
      var vao = gl.createVertexArray();
      vaos.push(vao);
      gl.bindVertexArray(vao);
      // Set the position buffer
      gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
      gl.enableVertexAttribArray(0);
      gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);

      var elementBuffer = gl.createBuffer();
      elementBuffers.push(elementBuffer);
      gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer);
      gl.bufferData(
          gl.ELEMENT_ARRAY_BUFFER,
          new Uint8Array([0, 1, 2, 0, 2, 3]),
          gl.STATIC_DRAW);

      // Setup the color attrib
      var color = colors[ii];
      if (ii < 3) {
        var colorBuffer = gl.createBuffer();
        colorBuffers.push(colorBuffer);
        gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(
          [ color[0], color[1], color[2], color[3],
            color[0], color[1], color[2], color[3],
            color[0], color[1], color[2], color[3],
            color[0], color[1], color[2], color[3]
          ]), gl.STATIC_DRAW);
        gl.enableVertexAttribArray(1);
        gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, 0);
      } else {
        gl.vertexAttrib4f(1, color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255);
      }
    }

    // delete the color buffers AND the position buffer.
    gl.bindVertexArray(null);
    for (var ii = 0; ii < colorBuffers.length; ++ii) {
      gl.deleteBuffer(colorBuffers[ii]);
      gl.deleteBuffer(elementBuffers[ii]);
      gl.bindVertexArray(vaos[ii]);
      var boundBuffer = gl.getVertexAttrib(1, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
      // The buffers should still be valid at this point, since it was attached to the VAO
      if(boundBuffer != colorBuffers[ii]) {
        testFailed("buffer removed too early");
      }
    }
    gl.bindVertexArray(null);
    gl.deleteBuffer(positionBuffer);

    // Render with the deleted buffers. As they are referenced by VAOs they
    // must still be around.
    for (var ii = 0; ii < colors.length; ++ii) {
      var color = colors[ii];
      gl.bindVertexArray(vaos[ii]);
      gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0);
      wtu.checkCanvas(gl, color, "should be " + color);
    }

    // Clean up.
    for (var ii = 0; ii < colorBuffers.length; ++ii) {
      gl.deleteVertexArray(vaos[ii]);
    }

    for (var ii = 0; ii < colorBuffers.length; ++ii) {
      // The buffers should no longer be valid now that the VAOs are deleted
      if(gl.isBuffer(colorBuffers[ii])) {
        testFailed("buffer not properly cleaned up after VAO deletion");
      }
    }
}

function runBoundDeleteTests() {
    debug("Testing using buffers that are deleted when attached to bound VAOs");

    var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_position", "a_color"]);
    gl.useProgram(program);

    var positionBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
    gl.bufferData(
        gl.ARRAY_BUFFER,
        new Float32Array([
           1.0,  1.0,
          -1.0,  1.0,
          -1.0, -1.0,
           1.0, -1.0]),
        gl.STATIC_DRAW);

    // Setup the color attrib
    var colorBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(
      [ 255,   0,   0, 255,
          0, 255,   0, 255,
          0,   0, 255, 255,
          0, 255, 255, 255
      ]), gl.STATIC_DRAW);

    var vaos = [];
    var elementBuffers = [];
    for (var ii = 0; ii < 4; ++ii) {
      var vao = gl.createVertexArray();
      vaos.push(vao);
      gl.bindVertexArray(vao);

      gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
      gl.enableVertexAttribArray(0);
      gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);

      var elementBuffer = gl.createBuffer();
      elementBuffers.push(elementBuffer);
      gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer);
      gl.bufferData(
          gl.ELEMENT_ARRAY_BUFFER,
          new Uint8Array([0, 1, 2, 0, 2, 3]),
          gl.STATIC_DRAW);

      gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
      gl.enableVertexAttribArray(1);
      gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, 0);
    }

    // delete the color buffers AND the position buffer, that are bound to the current VAO
    for (var ii = 0; ii < vaos.length; ++ii) {
      gl.bindVertexArray(vaos[ii]);

      gl.deleteBuffer(colorBuffer);
      gl.deleteBuffer(positionBuffer);

      // After the first iteration, deleteBuffer will be a no-op, and will not unbind its matching
      // bind points on the now-bound VAO like it did on the first iteration.
      var expectRetained = (ii != 0);
      var shouldBeStr = (expectRetained ? "retained" : "cleared");

      var boundPositionBuffer = gl.getVertexAttrib(0, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
      if (expectRetained != (boundPositionBuffer == positionBuffer)) {
        testFailed("Position attrib stored buffer should be " + shouldBeStr + ".");
      }

      var boundColorBuffer = gl.getVertexAttrib(1, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
      if (expectRetained != (boundColorBuffer == colorBuffer)) {
        testFailed("Color attrib stored buffer should be " + shouldBeStr + ".");
      }

      // If retained, everything should still work. If cleared, drawing should now fail.
      gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0);
      var expectedError = (expectRetained ? gl.NO_ERROR : gl.INVALID_OPERATION);
      wtu.glErrorShouldBe(gl, expectedError,
                          "Draw call should " + (expectRetained ? "not " : "") + "fail.");

      if (gl.isBuffer(positionBuffer)) {
        testFailed("References from unbound VAOs don't keep Position buffer alive.");
      }
      if (gl.isBuffer(colorBuffer)) {
        testFailed("References from unbound VAOs don't keep Color buffer alive");
      }
    }
}

function runArrayBufferBindTests() {
    debug("Testing that VAOs don't effect ARRAY_BUFFER binding.");

    gl.bindVertexArray(null);

    var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_color", "a_position"]);
    gl.useProgram(program);

    // create shared element buuffer
    var elementBuffer = gl.createBuffer();
    // bind to default
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer);
    gl.bufferData(
        gl.ELEMENT_ARRAY_BUFFER,
        new Uint8Array([0, 1, 2, 0, 2, 3]),
        gl.STATIC_DRAW);

    // first create the buffers for no vao draw.
    var nonVAOColorBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, nonVAOColorBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(
      [ 0, 255, 0, 255,
        0, 255, 0, 255,
        0, 255, 0, 255,
        0, 255, 0, 255,
      ]), gl.STATIC_DRAW);

    // shared position buffer.
    var positionBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
    gl.bufferData(
        gl.ARRAY_BUFFER,
        new Float32Array([
           1.0,  1.0,
          -1.0,  1.0,
          -1.0, -1.0,
           1.0, -1.0]),
        gl.STATIC_DRAW);

    // attach position buffer to default
    gl.enableVertexAttribArray(1);
    gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);

    // now create vao
    var vao = gl.createVertexArray();
    gl.bindVertexArray(vao);

    // attach the position buffer vao
    gl.enableVertexAttribArray(1);
    gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);

    var vaoColorBuffer = gl.createBuffer();
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0);
    gl.bindBuffer(gl.ARRAY_BUFFER, vaoColorBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array(
      [ 255, 0, 0, 255,
        255, 0, 0, 255,
        255, 0, 0, 255,
        255, 0, 0, 255,
      ]), gl.STATIC_DRAW);
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0);

    // now set the buffer back to the nonVAOColorBuffer
    gl.bindBuffer(gl.ARRAY_BUFFER, nonVAOColorBuffer);

    // bind to vao
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer);
    gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0);
    wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red");

    // unbind vao
    gl.bindVertexArray(null);

    // At this point the nonVAOColorBuffer should be still be bound.
    // If the WebGL impl is emulating VAOs it must make sure
    // it correctly restores this binding.
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0);
    gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0);
    wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green");
}

debug("");
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>