content/canvas/test/webgl-conformance/conformance/extensions/oes-vertex-array-object.html

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 <!--
     2 Copyright (c) 2011 The Chromium Authors. All rights reserved.
     3 Use of this source code is governed by a BSD-style license that can be
     4 found in the LICENSE file.
     5  -->
     6 <!DOCTYPE html>
     7 <html>
     8 <head>
     9 <meta charset="utf-8">
    10 <title>WebGL OES_vertex_array_object Conformance Tests</title>
    11 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
    12 <script src="../../resources/desktop-gl-constants.js" type="text/javascript"></script>
    13 <script src="../../resources/js-test-pre.js"></script>
    14 <script src="../resources/webgl-test.js"></script>
    15 <script src="../resources/webgl-test-utils.js"></script>
    16 <!-- comment in the script tag below to test through JS emualation of the extension. -->
    17 <!--
    18 <script src="../../../demos/google/resources/OESVertexArrayObject.js"></script>
    19 -->
    20 </head>
    21 <body>
    22 <div id="description"></div>
    23 <canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
    24 <div id="console"></div>
    25 <!-- Shaders for testing standard derivatives -->
    27 <script>
    28 description("This test verifies the functionality of the OES_vertex_array_object extension, if it is available.");
    30 debug("");
    32 var wtu = WebGLTestUtils;
    33 var canvas = document.getElementById("canvas");
    34 var gl = create3DContext(canvas);
    35 var ext = null;
    36 var vao = null;
    38 if (!gl) {
    39     testFailed("WebGL context does not exist");
    40 } else {
    41     testPassed("WebGL context exists");
    43     // Setup emulated OESVertexArrayObject if it has been included.
    44     if (window.setupVertexArrayObject) {
    45         debug("using emuated OES_vertex_array_object");
    46         setupVertexArrayObject(gl);
    47     }
    49     // Run tests with extension disabled
    50     runBindingTestDisabled();
    52     // Query the extension and store globally so shouldBe can access it
    53     ext = gl.getExtension("OES_vertex_array_object");
    54     if (!ext) {
    55         testPassed("No OES_vertex_array_object support -- this is legal");
    57         runSupportedTest(false);
    58     } else {
    59         testPassed("Successfully enabled OES_vertex_array_object extension");
    61         runSupportedTest(true);
    62         runBindingTestEnabled();
    63         runObjectTest();
    64         runAttributeTests();
    65         runAttributeValueTests();
    66         runDrawTests();
    67     }
    68 }
    70 function runSupportedTest(extensionEnabled) {
    71     var supported = gl.getSupportedExtensions();
    72     if (supported.indexOf("OES_vertex_array_object") >= 0) {
    73         if (extensionEnabled) {
    74             testPassed("OES_vertex_array_object listed as supported and getExtension succeeded");
    75         } else {
    76             testFailed("OES_vertex_array_object listed as supported but getExtension failed");
    77         }
    78     } else {
    79         if (extensionEnabled) {
    80             testFailed("OES_vertex_array_object not listed as supported but getExtension succeeded");
    81         } else {
    82             testPassed("OES_vertex_array_object not listed as supported and getExtension failed -- this is legal");
    83         }
    84     }
    85 }
    87 function runBindingTestDisabled() {
    88     debug("Testing binding enum with extension disabled");
    90     // Use the constant directly as we don't have the extension
    91     var VERTEX_ARRAY_BINDING_OES = 0x85B5;
    93     gl.getParameter(VERTEX_ARRAY_BINDING_OES);
    94     glErrorShouldBe(gl, gl.INVALID_ENUM, "VERTEX_ARRAY_BINDING_OES should not be queryable if extension is disabled");
    95 }
    97 function runBindingTestEnabled() {
    98     debug("Testing binding enum with extension enabled");
   100     shouldBe("ext.VERTEX_ARRAY_BINDING_OES", "0x85B5");
   102     gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES);
   103     glErrorShouldBe(gl, gl.NO_ERROR, "VERTEX_ARRAY_BINDING_OES query should succeed if extension is enable");
   105     // Default value is null
   106     if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) === null) {
   107         testPassed("Default value of VERTEX_ARRAY_BINDING_OES is null");
   108     } else {
   109         testFailed("Default value of VERTEX_ARRAY_BINDING_OES is not null");
   110     }
   112     debug("Testing binding a VAO");
   113     var vao0 = ext.createVertexArrayOES();
   114     var vao1 = ext.createVertexArrayOES();
   115     shouldBeNull("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES)");
   116     ext.bindVertexArrayOES(vao0);
   117     if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) == vao0) {
   118         testPassed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is expected VAO");
   119     } else {
   120         testFailed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is not expected VAO")
   121     }
   122     ext.bindVertexArrayOES(vao1);
   123     if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) == vao1) {
   124         testPassed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is expected VAO");
   125     } else {
   126         testFailed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is not expected VAO")
   127     }
   128     ext.deleteVertexArrayOES(vao1);
   129     shouldBeNull("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES)");
   130     ext.bindVertexArrayOES(vao1);
   131     glErrorShouldBe(gl, gl.INVALID_OPERATION, "binding a deleted vertex array object");
   132     ext.bindVertexArrayOES(null);
   133     shouldBeNull("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES)");
   134     ext.deleteVertexArrayOES(vao1);
   135 }
   137 function runObjectTest() {
   138     debug("Testing object creation");
   140     vao = ext.createVertexArrayOES();
   141     glErrorShouldBe(gl, gl.NO_ERROR, "createVertexArrayOES should not set an error");
   142     shouldBeNonNull("vao");
   144     // Expect false if never bound
   145     shouldBeFalse("ext.isVertexArrayOES(vao)");
   146     ext.bindVertexArrayOES(vao);
   147     shouldBeTrue("ext.isVertexArrayOES(vao)");
   148     ext.bindVertexArrayOES(null);
   149     shouldBeTrue("ext.isVertexArrayOES(vao)");
   151     /*
   152      * Issue found in the conformance test. The public webgl mailing list has been notified about it.
   153      * The tests have already been fixed upstream.
   154      */
   155     //shouldBeFalse("ext.isVertexArrayOES()");
   156     shouldBeFalse("ext.isVertexArrayOES(null)");
   158     ext.deleteVertexArrayOES(vao);
   159     vao = null;
   160 }
   162 function runAttributeTests() {
   163     debug("Testing attributes work across bindings");
   165     var states = [];
   167     var attrCount = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
   168     for (var n = 0; n < attrCount; n++) {
   169         gl.bindBuffer(gl.ARRAY_BUFFER, null);
   170         gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
   172         var state = {};
   173         states.push(state);
   175         var vao = state.vao = ext.createVertexArrayOES();
   176         ext.bindVertexArrayOES(vao);
   178         if (n % 2 == 0) {
   179             gl.enableVertexAttribArray(n);
   180         } else {
   181             gl.disableVertexAttribArray(n);
   182         }
   184         if (n % 2 == 0) {
   185             var buffer = state.buffer = gl.createBuffer();
   186             gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
   187             gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW);
   189             gl.vertexAttribPointer(n, 1 + n % 4, gl.FLOAT, true, n * 4, n * 4);
   190         }
   192         if (n % 2 == 0) {
   193             var elbuffer = state.elbuffer = gl.createBuffer();
   194             gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elbuffer);
   195             gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 1024, gl.STATIC_DRAW);
   196         }
   198         ext.bindVertexArrayOES(null);
   199     }
   201     var anyMismatch = false;
   202     for (var n = 0; n < attrCount; n++) {
   203         var state = states[n];
   205         ext.bindVertexArrayOES(state.vao);
   207         var isEnabled = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED);
   208         if ((n % 2 == 1) || isEnabled) {
   209             // Valid
   210         } else {
   211             testFailed("VERTEX_ATTRIB_ARRAY_ENABLED not preserved");
   212             anyMismatch = true;
   213         }
   215         var buffer = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
   216         if (n % 2 == 0) {
   217             if (buffer == state.buffer) {
   218                 // Matched
   219                 if ((gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_SIZE) == 1 + n % 4) &&
   220                     (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_TYPE) == gl.FLOAT) &&
   221                     (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED) == true) &&
   222                     (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_STRIDE) == n * 4) &&
   223                     (gl.getVertexAttribOffset(n, gl.VERTEX_ATTRIB_ARRAY_POINTER) == n * 4)) {
   224                     // Matched
   225                 } else {
   226                     testFailed("VERTEX_ATTRIB_ARRAY_* not preserved");
   227                     anyMismatch = true;
   228                 }
   229             } else {
   230                 testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved");
   231                 anyMismatch = true;
   232             }
   233         } else {
   234             // GL_CURRENT_VERTEX_ATTRIB is not preserved
   235             if (buffer) {
   236                 testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved");
   237                 anyMismatch = true;
   238             }
   239         }
   241         var elbuffer = gl.getParameter(gl.ELEMENT_ARRAY_BUFFER_BINDING);
   242         if (n % 2 == 0) {
   243             if (elbuffer == state.elbuffer) {
   244                 // Matched
   245             } else {
   246                 testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved");
   247                 anyMismatch = true;
   248             }
   249         } else {
   250             if (elbuffer == null) {
   251                 // Matched
   252             } else {
   253                 testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved");
   254                 anyMismatch = true;
   255             }
   256         }
   257     }
   258     ext.bindVertexArrayOES(null);
   259     if (!anyMismatch) {
   260         testPassed("All attributes preserved across bindings");
   261     }
   263     for (var n = 0; n < attrCount; n++) {
   264         var state = states[n];
   265         ext.deleteVertexArrayOES(state.vao);
   266     }
   267 }
   269 function runAttributeValueTests() {
   270     debug("Testing that attribute values are not attached to bindings");
   272     var v;
   273     var vao0 = ext.createVertexArrayOES();
   274     var anyFailed = false;
   276     ext.bindVertexArrayOES(null);
   277     gl.vertexAttrib4f(0, 0, 1, 2, 3);
   279     v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
   280     if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) {
   281         testFailed("Vertex attrib value not round-tripped?");
   282         anyFailed = true;
   283     }
   285     ext.bindVertexArrayOES(vao0);
   287     v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
   288     if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) {
   289         testFailed("Vertex attrib value reset across bindings");
   290         anyFailed = true;
   291     }
   293     gl.vertexAttrib4f(0, 4, 5, 6, 7);
   294     ext.bindVertexArrayOES(null);
   296     v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
   297     if (!(v[0] == 4 && v[1] == 5 && v[2] == 6 && v[3] == 7)) {
   298         testFailed("Vertex attrib value bound to buffer");
   299         anyFailed = true;
   300     }
   302     if (!anyFailed) {
   303         testPassed("Vertex attribute values are not attached to bindings")
   304     }
   306     ext.bindVertexArrayOES(null);
   307     ext.deleteVertexArrayOES(vao0);
   308 }
   310 function runDrawTests() {
   311     debug("Testing draws with various VAO bindings");
   313     canvas.width = 50; canvas.height = 50;
   314     gl.viewport(0, 0, canvas.width, canvas.height);
   316     var vao0 = ext.createVertexArrayOES();
   317     var vao1 = ext.createVertexArrayOES();
   319     var program = wtu.setupSimpleTextureProgram(gl, 0, 1);
   321     function setupQuad(s) {
   322         var opt_positionLocation = 0;
   323         var opt_texcoordLocation = 1;
   324         var vertexObject = gl.createBuffer();
   325         gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
   326         gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
   327              1.0 * s,  1.0 * s, 0.0,
   328             -1.0 * s,  1.0 * s, 0.0,
   329             -1.0 * s, -1.0 * s, 0.0,
   330              1.0 * s,  1.0 * s, 0.0,
   331             -1.0 * s, -1.0 * s, 0.0,
   332              1.0 * s, -1.0 * s, 0.0]), gl.STATIC_DRAW);
   333         gl.enableVertexAttribArray(opt_positionLocation);
   334         gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0);
   336         var vertexObject = gl.createBuffer();
   337         gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
   338         gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
   339             1.0 * s, 1.0 * s,
   340             0.0 * s, 1.0 * s,
   341             0.0 * s, 0.0 * s,
   342             1.0 * s, 1.0 * s,
   343             0.0 * s, 0.0 * s,
   344             1.0 * s, 0.0 * s]), gl.STATIC_DRAW);
   345         gl.enableVertexAttribArray(opt_texcoordLocation);
   346         gl.vertexAttribPointer(opt_texcoordLocation, 2, gl.FLOAT, false, 0, 0);
   347     };
   349     function readLocation(x, y) {
   350         var pixels = new Uint8Array(1 * 1 * 4);
   351         gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
   352         return pixels;
   353     };
   354     function testPixel(blackList, whiteList) {
   355         function testList(list, expected) {
   356             for (var n = 0; n < list.length; n++) {
   357                 var l = list[n];
   358                 var x = -Math.floor(l * canvas.width / 2) + canvas.width / 2;
   359                 var y = -Math.floor(l * canvas.height / 2) + canvas.height / 2;
   360                 var source = readLocation(x, y);
   361                 if (Math.abs(source[0] - expected) > 2) {
   362                     return false;
   363                 }
   364             }
   365             return true;
   366         }
   367         return testList(blackList, 0) && testList(whiteList, 255);
   368     };
   369     function verifyDraw(drawNumber, s) {
   370         wtu.drawQuad(gl);
   371         var blackList = [];
   372         var whiteList = [];
   373         var points = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0];
   374         for (var n = 0; n < points.length; n++) {
   375             if (points[n] <= s) {
   376                 blackList.push(points[n]);
   377             } else {
   378                 whiteList.push(points[n]);
   379             }
   380         }
   381         if (testPixel(blackList, whiteList)) {
   382             testPassed("Draw " + drawNumber + " passed pixel test");
   383         } else {
   384             testFailed("Draw " + drawNumber + " failed pixel test");
   385         }
   386     };
   388     // Setup all bindings
   389     setupQuad(1);
   390     ext.bindVertexArrayOES(vao0);
   391     setupQuad(0.5);
   392     ext.bindVertexArrayOES(vao1);
   393     setupQuad(0.25);
   395     // Verify drawing
   396     ext.bindVertexArrayOES(null);
   397     verifyDraw(0, 1);
   398     ext.bindVertexArrayOES(vao0);
   399     verifyDraw(1, 0.5);
   400     ext.bindVertexArrayOES(vao1);
   401     verifyDraw(2, 0.25);
   403     ext.bindVertexArrayOES(null);
   404     ext.deleteVertexArrayOES(vao0);
   405     ext.deleteVertexArrayOES(vao1);
   406 }
   408 debug("");
   409 successfullyParsed = true;
   410 </script>
   411 <script>finishTest();</script>
   413 </body>
   414 </html>

mercurial