gman@chromium.org | d9b90ed | 2013-02-14 07:36:05 +0000 | [diff] [blame^] | 1 | <!-- |
| 2 | |
| 3 | /* |
| 4 | ** Copyright (c) 2012 The Khronos Group Inc. |
| 5 | ** |
| 6 | ** Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | ** copy of this software and/or associated documentation files (the |
| 8 | ** "Materials"), to deal in the Materials without restriction, including |
| 9 | ** without limitation the rights to use, copy, modify, merge, publish, |
| 10 | ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 11 | ** permit persons to whom the Materials are furnished to do so, subject to |
| 12 | ** the following conditions: |
| 13 | ** |
| 14 | ** The above copyright notice and this permission notice shall be included |
| 15 | ** in all copies or substantial portions of the Materials. |
| 16 | ** |
| 17 | ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 | ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 | */ |
| 25 | |
| 26 | --> |
| 27 | |
| 28 | <!DOCTYPE html> |
| 29 | <html> |
| 30 | <head> |
| 31 | <meta charset="utf-8"> |
| 32 | <title>WebGL OES_vertex_array_object Conformance Tests</title> |
| 33 | <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 34 | <script src="../../resources/desktop-gl-constants.js" type="text/javascript"></script> |
| 35 | <script src="../../resources/js-test-pre.js"></script> |
| 36 | <script src="../resources/webgl-test.js"></script> |
| 37 | <script src="../resources/webgl-test-utils.js"></script> |
| 38 | <!-- comment in the script tag below to test through JS emualation of the extension. --> |
| 39 | <!-- |
| 40 | <script src="../../../demos/google/resources/OESVertexArrayObject.js"></script> |
| 41 | --> |
| 42 | </head> |
| 43 | <body> |
| 44 | <div id="description"></div> |
| 45 | <canvas id="canvas" style="width: 50px; height: 50px;"> </canvas> |
| 46 | <div id="console"></div> |
| 47 | <script id="vshader", type="x-shader/x-vertex"> |
| 48 | attribute vec4 a_position; |
| 49 | attribute vec4 a_color; |
| 50 | varying vec4 v_color; |
| 51 | void main(void) { |
| 52 | gl_Position = a_position; |
| 53 | v_color = a_color; |
| 54 | } |
| 55 | </script> |
| 56 | <script id="fshader", type="x-shader/x-fragment"> |
| 57 | precision mediump float; |
| 58 | varying vec4 v_color; |
| 59 | void main(void) { |
| 60 | gl_FragColor = v_color; |
| 61 | } |
| 62 | </script> |
| 63 | <script> |
| 64 | "use strict"; |
| 65 | description("This test verifies the functionality of the OES_vertex_array_object extension, if it is available."); |
| 66 | |
| 67 | debug(""); |
| 68 | |
| 69 | var wtu = WebGLTestUtils; |
| 70 | var canvas = document.getElementById("canvas"); |
| 71 | var gl = wtu.create3DContext(canvas); |
| 72 | var ext = null; |
| 73 | var vao = null; |
| 74 | |
| 75 | if (!gl) { |
| 76 | testFailed("WebGL context does not exist"); |
| 77 | } else { |
| 78 | testPassed("WebGL context exists"); |
| 79 | |
| 80 | // Setup emulated OESVertexArrayObject if it has been included. |
| 81 | if (window.setupVertexArrayObject) { |
| 82 | debug("using emuated OES_vertex_array_object"); |
| 83 | setupVertexArrayObject(gl); |
| 84 | } |
| 85 | |
| 86 | // Run tests with extension disabled |
| 87 | runBindingTestDisabled(); |
| 88 | |
| 89 | // Query the extension and store globally so shouldBe can access it |
| 90 | ext = gl.getExtension("OES_vertex_array_object"); |
| 91 | if (!ext) { |
| 92 | testPassed("No OES_vertex_array_object support -- this is legal"); |
| 93 | |
| 94 | runSupportedTest(false); |
| 95 | } else { |
| 96 | testPassed("Successfully enabled OES_vertex_array_object extension"); |
| 97 | |
| 98 | runSupportedTest(true); |
| 99 | runBindingTestEnabled(); |
| 100 | runObjectTest(); |
| 101 | runAttributeTests(); |
| 102 | runAttributeValueTests(); |
| 103 | runDrawTests(); |
| 104 | runDeleteTests(); |
| 105 | runArrayBufferBindTests(); |
| 106 | glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors"); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | function runSupportedTest(extensionEnabled) { |
| 111 | var supported = gl.getSupportedExtensions(); |
| 112 | if (supported.indexOf("OES_vertex_array_object") >= 0) { |
| 113 | if (extensionEnabled) { |
| 114 | testPassed("OES_vertex_array_object listed as supported and getExtension succeeded"); |
| 115 | } else { |
| 116 | testFailed("OES_vertex_array_object listed as supported but getExtension failed"); |
| 117 | } |
| 118 | } else { |
| 119 | if (extensionEnabled) { |
| 120 | testFailed("OES_vertex_array_object not listed as supported but getExtension succeeded"); |
| 121 | } else { |
| 122 | testPassed("OES_vertex_array_object not listed as supported and getExtension failed -- this is legal"); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | function runBindingTestDisabled() { |
| 128 | debug("Testing binding enum with extension disabled"); |
| 129 | |
| 130 | // Use the constant directly as we don't have the extension |
| 131 | var VERTEX_ARRAY_BINDING_OES = 0x85B5; |
| 132 | |
| 133 | gl.getParameter(VERTEX_ARRAY_BINDING_OES); |
| 134 | glErrorShouldBe(gl, gl.INVALID_ENUM, "VERTEX_ARRAY_BINDING_OES should not be queryable if extension is disabled"); |
| 135 | } |
| 136 | |
| 137 | function runBindingTestEnabled() { |
| 138 | debug("Testing binding enum with extension enabled"); |
| 139 | |
| 140 | shouldBe("ext.VERTEX_ARRAY_BINDING_OES", "0x85B5"); |
| 141 | |
| 142 | gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES); |
| 143 | glErrorShouldBe(gl, gl.NO_ERROR, "VERTEX_ARRAY_BINDING_OES query should succeed if extension is enable"); |
| 144 | |
| 145 | // Default value is null |
| 146 | if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) === null) { |
| 147 | testPassed("Default value of VERTEX_ARRAY_BINDING_OES is null"); |
| 148 | } else { |
| 149 | testFailed("Default value of VERTEX_ARRAY_BINDING_OES is not null"); |
| 150 | } |
| 151 | |
| 152 | debug("Testing binding a VAO"); |
| 153 | var vao0 = ext.createVertexArrayOES(); |
| 154 | var vao1 = ext.createVertexArrayOES(); |
| 155 | shouldBeNull("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES)"); |
| 156 | ext.bindVertexArrayOES(vao0); |
| 157 | if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) == vao0) { |
| 158 | testPassed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is expected VAO"); |
| 159 | } else { |
| 160 | testFailed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is not expected VAO") |
| 161 | } |
| 162 | ext.bindVertexArrayOES(vao1); |
| 163 | if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) == vao1) { |
| 164 | testPassed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is expected VAO"); |
| 165 | } else { |
| 166 | testFailed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is not expected VAO") |
| 167 | } |
| 168 | ext.deleteVertexArrayOES(vao1); |
| 169 | shouldBeNull("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES)"); |
| 170 | ext.bindVertexArrayOES(vao1); |
| 171 | glErrorShouldBe(gl, gl.INVALID_OPERATION, "binding a deleted vertex array object"); |
| 172 | ext.bindVertexArrayOES(null); |
| 173 | shouldBeNull("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES)"); |
| 174 | ext.deleteVertexArrayOES(vao1); |
| 175 | } |
| 176 | |
| 177 | function runObjectTest() { |
| 178 | debug("Testing object creation"); |
| 179 | |
| 180 | vao = ext.createVertexArrayOES(); |
| 181 | glErrorShouldBe(gl, gl.NO_ERROR, "createVertexArrayOES should not set an error"); |
| 182 | shouldBeNonNull("vao"); |
| 183 | |
| 184 | // Expect false if never bound |
| 185 | shouldBeFalse("ext.isVertexArrayOES(vao)"); |
| 186 | ext.bindVertexArrayOES(vao); |
| 187 | shouldBeTrue("ext.isVertexArrayOES(vao)"); |
| 188 | ext.bindVertexArrayOES(null); |
| 189 | shouldBeTrue("ext.isVertexArrayOES(vao)"); |
| 190 | |
| 191 | shouldBeFalse("ext.isVertexArrayOES()"); |
| 192 | shouldBeFalse("ext.isVertexArrayOES(null)"); |
| 193 | |
| 194 | ext.deleteVertexArrayOES(vao); |
| 195 | vao = null; |
| 196 | } |
| 197 | |
| 198 | function runAttributeTests() { |
| 199 | debug("Testing attributes work across bindings"); |
| 200 | |
| 201 | var states = []; |
| 202 | |
| 203 | var attrCount = gl.getParameter(gl.MAX_VERTEX_ATTRIBS); |
| 204 | for (var n = 0; n < attrCount; n++) { |
| 205 | gl.bindBuffer(gl.ARRAY_BUFFER, null); |
| 206 | gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null); |
| 207 | |
| 208 | var state = {}; |
| 209 | states.push(state); |
| 210 | |
| 211 | var vao = state.vao = ext.createVertexArrayOES(); |
| 212 | ext.bindVertexArrayOES(vao); |
| 213 | |
| 214 | var enableArray = (n % 2 == 0); |
| 215 | if (enableArray) { |
| 216 | gl.enableVertexAttribArray(n); |
| 217 | } else { |
| 218 | gl.disableVertexAttribArray(n); |
| 219 | } |
| 220 | |
| 221 | if (enableArray) { |
| 222 | var buffer = state.buffer = gl.createBuffer(); |
| 223 | gl.bindBuffer(gl.ARRAY_BUFFER, buffer); |
| 224 | gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW); |
| 225 | |
| 226 | gl.vertexAttribPointer(n, 1 + n % 4, gl.FLOAT, true, n * 4, n * 4); |
| 227 | } |
| 228 | |
| 229 | if (enableArray) { |
| 230 | var elbuffer = state.elbuffer = gl.createBuffer(); |
| 231 | gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elbuffer); |
| 232 | gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 1024, gl.STATIC_DRAW); |
| 233 | } |
| 234 | |
| 235 | ext.bindVertexArrayOES(null); |
| 236 | } |
| 237 | |
| 238 | var anyMismatch = false; |
| 239 | for (var n = 0; n < attrCount; n++) { |
| 240 | var state = states[n]; |
| 241 | |
| 242 | ext.bindVertexArrayOES(state.vao); |
| 243 | |
| 244 | var shouldBeEnabled = (n % 2 == 0); |
| 245 | var isEnabled = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED); |
| 246 | if (shouldBeEnabled != isEnabled) { |
| 247 | testFailed("VERTEX_ATTRIB_ARRAY_ENABLED not preserved"); |
| 248 | anyMismatch = true; |
| 249 | } |
| 250 | |
| 251 | var buffer = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING); |
| 252 | if (shouldBeEnabled) { |
| 253 | if (buffer == state.buffer) { |
| 254 | // Matched |
| 255 | if ((gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_SIZE) == 1 + n % 4) && |
| 256 | (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_TYPE) == gl.FLOAT) && |
| 257 | (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED) == true) && |
| 258 | (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_STRIDE) == n * 4) && |
| 259 | (gl.getVertexAttribOffset(n, gl.VERTEX_ATTRIB_ARRAY_POINTER) == n * 4)) { |
| 260 | // Matched |
| 261 | } else { |
| 262 | testFailed("VERTEX_ATTRIB_ARRAY_* not preserved"); |
| 263 | anyMismatch = true; |
| 264 | } |
| 265 | } else { |
| 266 | testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved"); |
| 267 | anyMismatch = true; |
| 268 | } |
| 269 | } else { |
| 270 | // GL_CURRENT_VERTEX_ATTRIB is not preserved |
| 271 | if (buffer) { |
| 272 | testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved"); |
| 273 | anyMismatch = true; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | var elbuffer = gl.getParameter(gl.ELEMENT_ARRAY_BUFFER_BINDING); |
| 278 | if (shouldBeEnabled) { |
| 279 | if (elbuffer == state.elbuffer) { |
| 280 | // Matched |
| 281 | } else { |
| 282 | testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved"); |
| 283 | anyMismatch = true; |
| 284 | } |
| 285 | } else { |
| 286 | if (elbuffer == null) { |
| 287 | // Matched |
| 288 | } else { |
| 289 | testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved"); |
| 290 | anyMismatch = true; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | ext.bindVertexArrayOES(null); |
| 295 | if (!anyMismatch) { |
| 296 | testPassed("All attributes preserved across bindings"); |
| 297 | } |
| 298 | |
| 299 | for (var n = 0; n < attrCount; n++) { |
| 300 | var state = states[n]; |
| 301 | ext.deleteVertexArrayOES(state.vao); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | function runAttributeValueTests() { |
| 306 | debug("Testing that attribute values are not attached to bindings"); |
| 307 | |
| 308 | var v; |
| 309 | var vao0 = ext.createVertexArrayOES(); |
| 310 | var anyFailed = false; |
| 311 | |
| 312 | ext.bindVertexArrayOES(null); |
| 313 | gl.vertexAttrib4f(0, 0, 1, 2, 3); |
| 314 | |
| 315 | v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB); |
| 316 | if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) { |
| 317 | testFailed("Vertex attrib value not round-tripped?"); |
| 318 | anyFailed = true; |
| 319 | } |
| 320 | |
| 321 | ext.bindVertexArrayOES(vao0); |
| 322 | |
| 323 | v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB); |
| 324 | if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) { |
| 325 | testFailed("Vertex attrib value reset across bindings"); |
| 326 | anyFailed = true; |
| 327 | } |
| 328 | |
| 329 | gl.vertexAttrib4f(0, 4, 5, 6, 7); |
| 330 | ext.bindVertexArrayOES(null); |
| 331 | |
| 332 | v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB); |
| 333 | if (!(v[0] == 4 && v[1] == 5 && v[2] == 6 && v[3] == 7)) { |
| 334 | testFailed("Vertex attrib value bound to buffer"); |
| 335 | anyFailed = true; |
| 336 | } |
| 337 | |
| 338 | if (!anyFailed) { |
| 339 | testPassed("Vertex attribute values are not attached to bindings") |
| 340 | } |
| 341 | |
| 342 | ext.bindVertexArrayOES(null); |
| 343 | ext.deleteVertexArrayOES(vao0); |
| 344 | } |
| 345 | |
| 346 | function runDrawTests() { |
| 347 | debug("Testing draws with various VAO bindings"); |
| 348 | |
| 349 | canvas.width = 50; canvas.height = 50; |
| 350 | gl.viewport(0, 0, canvas.width, canvas.height); |
| 351 | |
| 352 | var vao0 = ext.createVertexArrayOES(); |
| 353 | var vao1 = ext.createVertexArrayOES(); |
| 354 | |
| 355 | var opt_positionLocation = 0; |
| 356 | var opt_texcoordLocation = 1; |
| 357 | |
| 358 | var program = wtu.setupSimpleTextureProgram(gl, opt_positionLocation, opt_texcoordLocation); |
| 359 | |
| 360 | function setupQuad(s) { |
| 361 | var vertexObject = gl.createBuffer(); |
| 362 | gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
| 363 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
| 364 | 1.0 * s, 1.0 * s, 0.0, |
| 365 | -1.0 * s, 1.0 * s, 0.0, |
| 366 | -1.0 * s, -1.0 * s, 0.0, |
| 367 | 1.0 * s, 1.0 * s, 0.0, |
| 368 | -1.0 * s, -1.0 * s, 0.0, |
| 369 | 1.0 * s, -1.0 * s, 0.0]), gl.STATIC_DRAW); |
| 370 | gl.enableVertexAttribArray(opt_positionLocation); |
| 371 | gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0); |
| 372 | |
| 373 | var vertexObject = gl.createBuffer(); |
| 374 | gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
| 375 | gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
| 376 | 1.0 * s, 1.0 * s, |
| 377 | 0.0 * s, 1.0 * s, |
| 378 | 0.0 * s, 0.0 * s, |
| 379 | 1.0 * s, 1.0 * s, |
| 380 | 0.0 * s, 0.0 * s, |
| 381 | 1.0 * s, 0.0 * s]), gl.STATIC_DRAW); |
| 382 | gl.enableVertexAttribArray(opt_texcoordLocation); |
| 383 | gl.vertexAttribPointer(opt_texcoordLocation, 2, gl.FLOAT, false, 0, 0); |
| 384 | }; |
| 385 | |
| 386 | function readLocation(x, y) { |
| 387 | var pixels = new Uint8Array(1 * 1 * 4); |
| 388 | gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels); |
| 389 | return pixels; |
| 390 | }; |
| 391 | function testPixel(blackList, whiteList) { |
| 392 | function testList(list, expected) { |
| 393 | for (var n = 0; n < list.length; n++) { |
| 394 | var l = list[n]; |
| 395 | var x = -Math.floor(l * canvas.width / 2) + canvas.width / 2; |
| 396 | var y = -Math.floor(l * canvas.height / 2) + canvas.height / 2; |
| 397 | var source = readLocation(x, y); |
| 398 | if (Math.abs(source[0] - expected) > 2) { |
| 399 | return false; |
| 400 | } |
| 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | return testList(blackList, 0) && testList(whiteList, 255); |
| 405 | }; |
| 406 | function verifyDraw(drawNumber, s) { |
| 407 | wtu.drawQuad(gl); |
| 408 | var blackList = []; |
| 409 | var whiteList = []; |
| 410 | var points = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]; |
| 411 | for (var n = 0; n < points.length; n++) { |
| 412 | if (points[n] <= s) { |
| 413 | blackList.push(points[n]); |
| 414 | } else { |
| 415 | whiteList.push(points[n]); |
| 416 | } |
| 417 | } |
| 418 | if (testPixel(blackList, whiteList)) { |
| 419 | testPassed("Draw " + drawNumber + " passed pixel test"); |
| 420 | } else { |
| 421 | testFailed("Draw " + drawNumber + " failed pixel test"); |
| 422 | } |
| 423 | }; |
| 424 | |
| 425 | // Setup all bindings |
| 426 | setupQuad(1); |
| 427 | ext.bindVertexArrayOES(vao0); |
| 428 | setupQuad(0.5); |
| 429 | ext.bindVertexArrayOES(vao1); |
| 430 | setupQuad(0.25); |
| 431 | |
| 432 | // Verify drawing |
| 433 | ext.bindVertexArrayOES(null); |
| 434 | verifyDraw(0, 1); |
| 435 | ext.bindVertexArrayOES(vao0); |
| 436 | verifyDraw(1, 0.5); |
| 437 | ext.bindVertexArrayOES(vao1); |
| 438 | verifyDraw(2, 0.25); |
| 439 | |
| 440 | ext.bindVertexArrayOES(null); |
| 441 | ext.deleteVertexArrayOES(vao0); |
| 442 | ext.deleteVertexArrayOES(vao1); |
| 443 | |
| 444 | // Disable global vertex attrib array |
| 445 | gl.disableVertexAttribArray(opt_positionLocation); |
| 446 | gl.disableVertexAttribArray(opt_texcoordLocation); |
| 447 | |
| 448 | // Draw with values. |
| 449 | var positionLoc = 0; |
| 450 | var colorLoc = 1; |
| 451 | var gridRes = 1; |
| 452 | wtu.setupQuad(gl, gridRes, positionLoc); |
| 453 | // Set the vertex color to red. |
| 454 | gl.vertexAttrib4f(colorLoc, 1, 0, 0, 1); |
| 455 | |
| 456 | var vao0 = ext.createVertexArrayOES(); |
| 457 | ext.bindVertexArrayOES(vao0); |
| 458 | var program = wtu.setupSimpleVertexColorProgram(gl, positionLoc, colorLoc); |
| 459 | wtu.setupQuad(gl, gridRes, positionLoc); |
| 460 | // Set the vertex color to green. |
| 461 | gl.vertexAttrib4f(colorLoc, 0, 1, 0, 1); |
| 462 | wtu.drawIndexedQuad(gl, gridRes); |
| 463 | wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green") |
| 464 | ext.deleteVertexArrayOES(vao0); |
| 465 | wtu.drawIndexedQuad(gl, gridRes); |
| 466 | wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green") |
| 467 | } |
| 468 | |
| 469 | function runDeleteTests() { |
| 470 | debug("Testing using deleted buffers referenced by VAOs"); |
| 471 | |
| 472 | var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_position", "a_color"]); |
| 473 | gl.useProgram(program); |
| 474 | |
| 475 | var positionBuffer = gl.createBuffer(); |
| 476 | gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 477 | gl.bufferData( |
| 478 | gl.ARRAY_BUFFER, |
| 479 | new Float32Array([ |
| 480 | 1.0, 1.0, |
| 481 | -1.0, 1.0, |
| 482 | -1.0, -1.0, |
| 483 | 1.0, -1.0]), |
| 484 | gl.STATIC_DRAW); |
| 485 | |
| 486 | var colors = [ |
| 487 | [255, 0, 0, 255], |
| 488 | [ 0, 255, 0, 255], |
| 489 | [ 0, 0, 255, 255], |
| 490 | [ 0, 255, 255, 255] |
| 491 | ]; |
| 492 | var colorBuffers = []; |
| 493 | var elementBuffers = []; |
| 494 | var vaos = []; |
| 495 | for (var ii = 0; ii < colors.length; ++ii) { |
| 496 | var vao = ext.createVertexArrayOES(); |
| 497 | vaos.push(vao); |
| 498 | ext.bindVertexArrayOES(vao); |
| 499 | // Set the position buffer |
| 500 | gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 501 | gl.enableVertexAttribArray(0); |
| 502 | gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 503 | |
| 504 | var elementBuffer = gl.createBuffer(); |
| 505 | elementBuffers.push(elementBuffer); |
| 506 | gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer); |
| 507 | gl.bufferData( |
| 508 | gl.ELEMENT_ARRAY_BUFFER, |
| 509 | new Uint8Array([0, 1, 2, 0, 2, 3]), |
| 510 | gl.STATIC_DRAW); |
| 511 | |
| 512 | // Setup the color attrib |
| 513 | var color = colors[ii]; |
| 514 | if (ii < 3) { |
| 515 | var colorBuffer = gl.createBuffer(); |
| 516 | colorBuffers.push(colorBuffer); |
| 517 | gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); |
| 518 | gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array( |
| 519 | [ color[0], color[1], color[2], color[3], |
| 520 | color[0], color[1], color[2], color[3], |
| 521 | color[0], color[1], color[2], color[3], |
| 522 | color[0], color[1], color[2], color[3] |
| 523 | ]), gl.STATIC_DRAW); |
| 524 | gl.enableVertexAttribArray(1); |
| 525 | gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, 0); |
| 526 | } else { |
| 527 | gl.vertexAttrib4f(1, color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // delete the color buffers AND the position buffer. |
| 532 | ext.bindVertexArrayOES(null); |
| 533 | for (var ii = 0; ii < colorBuffers.length; ++ii) { |
| 534 | gl.deleteBuffer(colorBuffers[ii]); |
| 535 | gl.deleteBuffer(elementBuffers[ii]); |
| 536 | // The buffers should still be valid at this point, since it was attached to the VAO |
| 537 | if(!gl.isBuffer(colorBuffers[ii])) { |
| 538 | testFailed("buffer removed too early"); |
| 539 | } |
| 540 | } |
| 541 | gl.deleteBuffer(positionBuffer); |
| 542 | |
| 543 | // Render with the deleted buffers. As they are referenced by VAOs they |
| 544 | // must still be around. |
| 545 | for (var ii = 0; ii < colors.length; ++ii) { |
| 546 | var color = colors[ii]; |
| 547 | ext.bindVertexArrayOES(vaos[ii]); |
| 548 | gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0); |
| 549 | wtu.checkCanvas(gl, color, "should be " + color); |
| 550 | } |
| 551 | |
| 552 | // Clean up. |
| 553 | for (var ii = 0; ii < colorBuffers.length; ++ii) { |
| 554 | ext.deleteVertexArrayOES(vaos[ii]); |
| 555 | } |
| 556 | |
| 557 | for (var ii = 0; ii < colorBuffers.length; ++ii) { |
| 558 | // The buffers should no longer be valid now that the VAOs are deleted |
| 559 | if(gl.isBuffer(colorBuffers[ii])) { |
| 560 | testFailed("buffer not properly cleaned up after VAO deletion"); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | function runArrayBufferBindTests() { |
| 566 | debug("Testing that VAOs don't effect ARRAY_BUFFER binding."); |
| 567 | |
| 568 | ext.bindVertexArrayOES(null); |
| 569 | |
| 570 | var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_color", "a_position"]); |
| 571 | gl.useProgram(program); |
| 572 | |
| 573 | // create shared element buuffer |
| 574 | var elementBuffer = gl.createBuffer(); |
| 575 | // bind to default |
| 576 | gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer); |
| 577 | gl.bufferData( |
| 578 | gl.ELEMENT_ARRAY_BUFFER, |
| 579 | new Uint8Array([0, 1, 2, 0, 2, 3]), |
| 580 | gl.STATIC_DRAW); |
| 581 | |
| 582 | // first create the buffers for no vao draw. |
| 583 | var nonVAOColorBuffer = gl.createBuffer(); |
| 584 | gl.bindBuffer(gl.ARRAY_BUFFER, nonVAOColorBuffer); |
| 585 | gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array( |
| 586 | [ 0, 255, 0, 255, |
| 587 | 0, 255, 0, 255, |
| 588 | 0, 255, 0, 255, |
| 589 | 0, 255, 0, 255, |
| 590 | ]), gl.STATIC_DRAW); |
| 591 | |
| 592 | // shared position buffer. |
| 593 | var positionBuffer = gl.createBuffer(); |
| 594 | gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 595 | gl.bufferData( |
| 596 | gl.ARRAY_BUFFER, |
| 597 | new Float32Array([ |
| 598 | 1.0, 1.0, |
| 599 | -1.0, 1.0, |
| 600 | -1.0, -1.0, |
| 601 | 1.0, -1.0]), |
| 602 | gl.STATIC_DRAW); |
| 603 | |
| 604 | // attach position buffer to default |
| 605 | gl.enableVertexAttribArray(1); |
| 606 | gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0); |
| 607 | |
| 608 | // now create vao |
| 609 | var vao = ext.createVertexArrayOES(); |
| 610 | ext.bindVertexArrayOES(vao); |
| 611 | |
| 612 | // attach the position buffer vao |
| 613 | gl.enableVertexAttribArray(1); |
| 614 | gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0); |
| 615 | |
| 616 | var vaoColorBuffer = gl.createBuffer(); |
| 617 | gl.enableVertexAttribArray(0); |
| 618 | gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0); |
| 619 | gl.bindBuffer(gl.ARRAY_BUFFER, vaoColorBuffer); |
| 620 | gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array( |
| 621 | [ 255, 0, 0, 255, |
| 622 | 255, 0, 0, 255, |
| 623 | 255, 0, 0, 255, |
| 624 | 255, 0, 0, 255, |
| 625 | ]), gl.STATIC_DRAW); |
| 626 | gl.enableVertexAttribArray(0); |
| 627 | gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0); |
| 628 | |
| 629 | // now set the buffer back to the nonVAOColorBuffer |
| 630 | gl.bindBuffer(gl.ARRAY_BUFFER, nonVAOColorBuffer); |
| 631 | |
| 632 | // bind to vao |
| 633 | gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer); |
| 634 | gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0); |
| 635 | wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red"); |
| 636 | |
| 637 | // unbind vao |
| 638 | ext.bindVertexArrayOES(null); |
| 639 | |
| 640 | // At this point the nonVAOColorBuffer should be still be bound. |
| 641 | // If the WebGL impl is emulating VAOs it must make sure |
| 642 | // it correctly restores this binding. |
| 643 | gl.enableVertexAttribArray(0); |
| 644 | gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0); |
| 645 | gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0); |
| 646 | wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green"); |
| 647 | } |
| 648 | |
| 649 | debug(""); |
| 650 | var successfullyParsed = true; |
| 651 | </script> |
| 652 | <script src="../../resources/js-test-post.js"></script> |
| 653 | |
| 654 | </body> |
| 655 | </html> |