OLD | NEW |
(Empty) | |
| 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"></s
cript> |
| 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 extens
ion. --> |
| 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 getExten
sion succeeded"); |
| 115 } else { |
| 116 testFailed("OES_vertex_array_object listed as supported but getExten
sion failed"); |
| 117 } |
| 118 } else { |
| 119 if (extensionEnabled) { |
| 120 testFailed("OES_vertex_array_object not listed as supported but getE
xtension succeeded"); |
| 121 } else { |
| 122 testPassed("OES_vertex_array_object not listed as supported and getE
xtension 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 succ
eed 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 VA
O"); |
| 159 } else { |
| 160 testFailed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is not expecte
d 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 VA
O"); |
| 165 } else { |
| 166 testFailed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is not expecte
d 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 ob
ject"); |
| 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 err
or"); |
| 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(null)"); |
| 192 |
| 193 ext.deleteVertexArrayOES(vao); |
| 194 vao = null; |
| 195 } |
| 196 |
| 197 function runAttributeTests() { |
| 198 debug("Testing attributes work across bindings"); |
| 199 |
| 200 var states = []; |
| 201 |
| 202 var attrCount = gl.getParameter(gl.MAX_VERTEX_ATTRIBS); |
| 203 for (var n = 0; n < attrCount; n++) { |
| 204 gl.bindBuffer(gl.ARRAY_BUFFER, null); |
| 205 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null); |
| 206 |
| 207 var state = {}; |
| 208 states.push(state); |
| 209 |
| 210 var vao = state.vao = ext.createVertexArrayOES(); |
| 211 ext.bindVertexArrayOES(vao); |
| 212 |
| 213 var enableArray = (n % 2 == 0); |
| 214 if (enableArray) { |
| 215 gl.enableVertexAttribArray(n); |
| 216 } else { |
| 217 gl.disableVertexAttribArray(n); |
| 218 } |
| 219 |
| 220 if (enableArray) { |
| 221 var buffer = state.buffer = gl.createBuffer(); |
| 222 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); |
| 223 gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW); |
| 224 |
| 225 gl.vertexAttribPointer(n, 1 + n % 4, gl.FLOAT, true, n * 4, n * 4); |
| 226 } |
| 227 |
| 228 if (enableArray) { |
| 229 var elbuffer = state.elbuffer = gl.createBuffer(); |
| 230 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elbuffer); |
| 231 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 1024, gl.STATIC_DRAW); |
| 232 } |
| 233 |
| 234 ext.bindVertexArrayOES(null); |
| 235 } |
| 236 |
| 237 var anyMismatch = false; |
| 238 for (var n = 0; n < attrCount; n++) { |
| 239 var state = states[n]; |
| 240 |
| 241 ext.bindVertexArrayOES(state.vao); |
| 242 |
| 243 var shouldBeEnabled = (n % 2 == 0); |
| 244 var isEnabled = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED); |
| 245 if (shouldBeEnabled != isEnabled) { |
| 246 testFailed("VERTEX_ATTRIB_ARRAY_ENABLED not preserved"); |
| 247 anyMismatch = true; |
| 248 } |
| 249 |
| 250 var buffer = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING
); |
| 251 if (shouldBeEnabled) { |
| 252 if (buffer == state.buffer) { |
| 253 // Matched |
| 254 if ((gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_SIZE) == 1 + n
% 4) && |
| 255 (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_TYPE) == gl.FL
OAT) && |
| 256 (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED) ==
true) && |
| 257 (gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_STRIDE) == n *
4) && |
| 258 (gl.getVertexAttribOffset(n, gl.VERTEX_ATTRIB_ARRAY_POINTER)
== n * 4)) { |
| 259 // Matched |
| 260 } else { |
| 261 testFailed("VERTEX_ATTRIB_ARRAY_* not preserved"); |
| 262 anyMismatch = true; |
| 263 } |
| 264 } else { |
| 265 testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved"); |
| 266 anyMismatch = true; |
| 267 } |
| 268 } else { |
| 269 // GL_CURRENT_VERTEX_ATTRIB is not preserved |
| 270 if (buffer) { |
| 271 testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved"); |
| 272 anyMismatch = true; |
| 273 } |
| 274 } |
| 275 |
| 276 var elbuffer = gl.getParameter(gl.ELEMENT_ARRAY_BUFFER_BINDING); |
| 277 if (shouldBeEnabled) { |
| 278 if (elbuffer == state.elbuffer) { |
| 279 // Matched |
| 280 } else { |
| 281 testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved"); |
| 282 anyMismatch = true; |
| 283 } |
| 284 } else { |
| 285 if (elbuffer == null) { |
| 286 // Matched |
| 287 } else { |
| 288 testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved"); |
| 289 anyMismatch = true; |
| 290 } |
| 291 } |
| 292 } |
| 293 ext.bindVertexArrayOES(null); |
| 294 if (!anyMismatch) { |
| 295 testPassed("All attributes preserved across bindings"); |
| 296 } |
| 297 |
| 298 for (var n = 0; n < attrCount; n++) { |
| 299 var state = states[n]; |
| 300 ext.deleteVertexArrayOES(state.vao); |
| 301 } |
| 302 } |
| 303 |
| 304 function runAttributeValueTests() { |
| 305 debug("Testing that attribute values are not attached to bindings"); |
| 306 |
| 307 var v; |
| 308 var vao0 = ext.createVertexArrayOES(); |
| 309 var anyFailed = false; |
| 310 |
| 311 ext.bindVertexArrayOES(null); |
| 312 gl.vertexAttrib4f(0, 0, 1, 2, 3); |
| 313 |
| 314 v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB); |
| 315 if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) { |
| 316 testFailed("Vertex attrib value not round-tripped?"); |
| 317 anyFailed = true; |
| 318 } |
| 319 |
| 320 ext.bindVertexArrayOES(vao0); |
| 321 |
| 322 v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB); |
| 323 if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) { |
| 324 testFailed("Vertex attrib value reset across bindings"); |
| 325 anyFailed = true; |
| 326 } |
| 327 |
| 328 gl.vertexAttrib4f(0, 4, 5, 6, 7); |
| 329 ext.bindVertexArrayOES(null); |
| 330 |
| 331 v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB); |
| 332 if (!(v[0] == 4 && v[1] == 5 && v[2] == 6 && v[3] == 7)) { |
| 333 testFailed("Vertex attrib value bound to buffer"); |
| 334 anyFailed = true; |
| 335 } |
| 336 |
| 337 if (!anyFailed) { |
| 338 testPassed("Vertex attribute values are not attached to bindings") |
| 339 } |
| 340 |
| 341 ext.bindVertexArrayOES(null); |
| 342 ext.deleteVertexArrayOES(vao0); |
| 343 } |
| 344 |
| 345 function runDrawTests() { |
| 346 debug("Testing draws with various VAO bindings"); |
| 347 |
| 348 canvas.width = 50; canvas.height = 50; |
| 349 gl.viewport(0, 0, canvas.width, canvas.height); |
| 350 |
| 351 var vao0 = ext.createVertexArrayOES(); |
| 352 var vao1 = ext.createVertexArrayOES(); |
| 353 |
| 354 var opt_positionLocation = 0; |
| 355 var opt_texcoordLocation = 1; |
| 356 |
| 357 var program = wtu.setupSimpleTextureProgram(gl, opt_positionLocation, opt_te
xcoordLocation); |
| 358 |
| 359 function setupQuad(s) { |
| 360 var vertexObject = gl.createBuffer(); |
| 361 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
| 362 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
| 363 1.0 * s, 1.0 * s, 0.0, |
| 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]), gl.STATIC_DRAW); |
| 369 gl.enableVertexAttribArray(opt_positionLocation); |
| 370 gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0); |
| 371 |
| 372 var vertexObject = gl.createBuffer(); |
| 373 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject); |
| 374 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ |
| 375 1.0 * s, 1.0 * s, |
| 376 0.0 * s, 1.0 * s, |
| 377 0.0 * s, 0.0 * s, |
| 378 1.0 * s, 1.0 * s, |
| 379 0.0 * s, 0.0 * s, |
| 380 1.0 * s, 0.0 * s]), gl.STATIC_DRAW); |
| 381 gl.enableVertexAttribArray(opt_texcoordLocation); |
| 382 gl.vertexAttribPointer(opt_texcoordLocation, 2, gl.FLOAT, false, 0, 0); |
| 383 }; |
| 384 |
| 385 function readLocation(x, y) { |
| 386 var pixels = new Uint8Array(1 * 1 * 4); |
| 387 gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels); |
| 388 return pixels; |
| 389 }; |
| 390 function testPixel(blackList, whiteList) { |
| 391 function testList(list, expected) { |
| 392 for (var n = 0; n < list.length; n++) { |
| 393 var l = list[n]; |
| 394 var x = -Math.floor(l * canvas.width / 2) + canvas.width / 2; |
| 395 var y = -Math.floor(l * canvas.height / 2) + canvas.height / 2; |
| 396 var source = readLocation(x, y); |
| 397 if (Math.abs(source[0] - expected) > 2) { |
| 398 return false; |
| 399 } |
| 400 } |
| 401 return true; |
| 402 } |
| 403 return testList(blackList, 0) && testList(whiteList, 255); |
| 404 }; |
| 405 function verifyDraw(drawNumber, s) { |
| 406 wtu.clearAndDrawUnitQuad(gl); |
| 407 var blackList = []; |
| 408 var whiteList = []; |
| 409 var points = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]; |
| 410 for (var n = 0; n < points.length; n++) { |
| 411 if (points[n] <= s) { |
| 412 blackList.push(points[n]); |
| 413 } else { |
| 414 whiteList.push(points[n]); |
| 415 } |
| 416 } |
| 417 if (testPixel(blackList, whiteList)) { |
| 418 testPassed("Draw " + drawNumber + " passed pixel test"); |
| 419 } else { |
| 420 testFailed("Draw " + drawNumber + " failed pixel test"); |
| 421 } |
| 422 }; |
| 423 |
| 424 // Setup all bindings |
| 425 setupQuad(1); |
| 426 ext.bindVertexArrayOES(vao0); |
| 427 setupQuad(0.5); |
| 428 ext.bindVertexArrayOES(vao1); |
| 429 setupQuad(0.25); |
| 430 |
| 431 // Verify drawing |
| 432 ext.bindVertexArrayOES(null); |
| 433 verifyDraw(0, 1); |
| 434 ext.bindVertexArrayOES(vao0); |
| 435 verifyDraw(1, 0.5); |
| 436 ext.bindVertexArrayOES(vao1); |
| 437 verifyDraw(2, 0.25); |
| 438 |
| 439 ext.bindVertexArrayOES(null); |
| 440 ext.deleteVertexArrayOES(vao0); |
| 441 ext.deleteVertexArrayOES(vao1); |
| 442 |
| 443 // Disable global vertex attrib array |
| 444 gl.disableVertexAttribArray(opt_positionLocation); |
| 445 gl.disableVertexAttribArray(opt_texcoordLocation); |
| 446 |
| 447 // Draw with values. |
| 448 var positionLoc = 0; |
| 449 var colorLoc = 1; |
| 450 var gridRes = 1; |
| 451 wtu.setupIndexedQuad(gl, gridRes, positionLoc); |
| 452 // Set the vertex color to red. |
| 453 gl.vertexAttrib4f(colorLoc, 1, 0, 0, 1); |
| 454 |
| 455 var vao0 = ext.createVertexArrayOES(); |
| 456 ext.bindVertexArrayOES(vao0); |
| 457 var program = wtu.setupSimpleVertexColorProgram(gl, positionLoc, colorLoc); |
| 458 wtu.setupIndexedQuad(gl, gridRes, positionLoc); |
| 459 // Set the vertex color to green. |
| 460 gl.vertexAttrib4f(colorLoc, 0, 1, 0, 1); |
| 461 wtu.clearAndDrawIndexedQuad(gl, gridRes); |
| 462 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green") |
| 463 ext.deleteVertexArrayOES(vao0); |
| 464 wtu.clearAndDrawIndexedQuad(gl, gridRes); |
| 465 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green") |
| 466 } |
| 467 |
| 468 function runDeleteTests() { |
| 469 debug("Testing using deleted buffers referenced by VAOs"); |
| 470 |
| 471 var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_position", "a
_color"]); |
| 472 gl.useProgram(program); |
| 473 |
| 474 var positionBuffer = gl.createBuffer(); |
| 475 gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 476 gl.bufferData( |
| 477 gl.ARRAY_BUFFER, |
| 478 new Float32Array([ |
| 479 1.0, 1.0, |
| 480 -1.0, 1.0, |
| 481 -1.0, -1.0, |
| 482 1.0, -1.0]), |
| 483 gl.STATIC_DRAW); |
| 484 |
| 485 var colors = [ |
| 486 [255, 0, 0, 255], |
| 487 [ 0, 255, 0, 255], |
| 488 [ 0, 0, 255, 255], |
| 489 [ 0, 255, 255, 255] |
| 490 ]; |
| 491 var colorBuffers = []; |
| 492 var elementBuffers = []; |
| 493 var vaos = []; |
| 494 for (var ii = 0; ii < colors.length; ++ii) { |
| 495 var vao = ext.createVertexArrayOES(); |
| 496 vaos.push(vao); |
| 497 ext.bindVertexArrayOES(vao); |
| 498 // Set the position buffer |
| 499 gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 500 gl.enableVertexAttribArray(0); |
| 501 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 502 |
| 503 var elementBuffer = gl.createBuffer(); |
| 504 elementBuffers.push(elementBuffer); |
| 505 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer); |
| 506 gl.bufferData( |
| 507 gl.ELEMENT_ARRAY_BUFFER, |
| 508 new Uint8Array([0, 1, 2, 0, 2, 3]), |
| 509 gl.STATIC_DRAW); |
| 510 |
| 511 // Setup the color attrib |
| 512 var color = colors[ii]; |
| 513 if (ii < 3) { |
| 514 var colorBuffer = gl.createBuffer(); |
| 515 colorBuffers.push(colorBuffer); |
| 516 gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); |
| 517 gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array( |
| 518 [ color[0], color[1], color[2], color[3], |
| 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 ]), gl.STATIC_DRAW); |
| 523 gl.enableVertexAttribArray(1); |
| 524 gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, 0); |
| 525 } else { |
| 526 gl.vertexAttrib4f(1, color[0] / 255, color[1] / 255, color[2] / 255, col
or[3] / 255); |
| 527 } |
| 528 } |
| 529 |
| 530 // delete the color buffers AND the position buffer. |
| 531 ext.bindVertexArrayOES(null); |
| 532 for (var ii = 0; ii < colorBuffers.length; ++ii) { |
| 533 gl.deleteBuffer(colorBuffers[ii]); |
| 534 gl.deleteBuffer(elementBuffers[ii]); |
| 535 // The buffers should still be valid at this point, since it was attached
to the VAO |
| 536 if(!gl.isBuffer(colorBuffers[ii])) { |
| 537 testFailed("buffer removed too early"); |
| 538 } |
| 539 } |
| 540 gl.deleteBuffer(positionBuffer); |
| 541 |
| 542 // Render with the deleted buffers. As they are referenced by VAOs they |
| 543 // must still be around. |
| 544 for (var ii = 0; ii < colors.length; ++ii) { |
| 545 var color = colors[ii]; |
| 546 ext.bindVertexArrayOES(vaos[ii]); |
| 547 gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0); |
| 548 wtu.checkCanvas(gl, color, "should be " + color); |
| 549 } |
| 550 |
| 551 // Clean up. |
| 552 for (var ii = 0; ii < colorBuffers.length; ++ii) { |
| 553 ext.deleteVertexArrayOES(vaos[ii]); |
| 554 } |
| 555 |
| 556 for (var ii = 0; ii < colorBuffers.length; ++ii) { |
| 557 // The buffers should no longer be valid now that the VAOs are deleted |
| 558 if(gl.isBuffer(colorBuffers[ii])) { |
| 559 testFailed("buffer not properly cleaned up after VAO deletion"); |
| 560 } |
| 561 } |
| 562 } |
| 563 |
| 564 function runArrayBufferBindTests() { |
| 565 debug("Testing that VAOs don't effect ARRAY_BUFFER binding."); |
| 566 |
| 567 ext.bindVertexArrayOES(null); |
| 568 |
| 569 var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_color", "a_po
sition"]); |
| 570 gl.useProgram(program); |
| 571 |
| 572 // create shared element buuffer |
| 573 var elementBuffer = gl.createBuffer(); |
| 574 // bind to default |
| 575 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer); |
| 576 gl.bufferData( |
| 577 gl.ELEMENT_ARRAY_BUFFER, |
| 578 new Uint8Array([0, 1, 2, 0, 2, 3]), |
| 579 gl.STATIC_DRAW); |
| 580 |
| 581 // first create the buffers for no vao draw. |
| 582 var nonVAOColorBuffer = gl.createBuffer(); |
| 583 gl.bindBuffer(gl.ARRAY_BUFFER, nonVAOColorBuffer); |
| 584 gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array( |
| 585 [ 0, 255, 0, 255, |
| 586 0, 255, 0, 255, |
| 587 0, 255, 0, 255, |
| 588 0, 255, 0, 255, |
| 589 ]), gl.STATIC_DRAW); |
| 590 |
| 591 // shared position buffer. |
| 592 var positionBuffer = gl.createBuffer(); |
| 593 gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); |
| 594 gl.bufferData( |
| 595 gl.ARRAY_BUFFER, |
| 596 new Float32Array([ |
| 597 1.0, 1.0, |
| 598 -1.0, 1.0, |
| 599 -1.0, -1.0, |
| 600 1.0, -1.0]), |
| 601 gl.STATIC_DRAW); |
| 602 |
| 603 // attach position buffer to default |
| 604 gl.enableVertexAttribArray(1); |
| 605 gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0); |
| 606 |
| 607 // now create vao |
| 608 var vao = ext.createVertexArrayOES(); |
| 609 ext.bindVertexArrayOES(vao); |
| 610 |
| 611 // attach the position buffer vao |
| 612 gl.enableVertexAttribArray(1); |
| 613 gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0); |
| 614 |
| 615 var vaoColorBuffer = gl.createBuffer(); |
| 616 gl.enableVertexAttribArray(0); |
| 617 gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0); |
| 618 gl.bindBuffer(gl.ARRAY_BUFFER, vaoColorBuffer); |
| 619 gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array( |
| 620 [ 255, 0, 0, 255, |
| 621 255, 0, 0, 255, |
| 622 255, 0, 0, 255, |
| 623 255, 0, 0, 255, |
| 624 ]), gl.STATIC_DRAW); |
| 625 gl.enableVertexAttribArray(0); |
| 626 gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0); |
| 627 |
| 628 // now set the buffer back to the nonVAOColorBuffer |
| 629 gl.bindBuffer(gl.ARRAY_BUFFER, nonVAOColorBuffer); |
| 630 |
| 631 // bind to vao |
| 632 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer); |
| 633 gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0); |
| 634 wtu.checkCanvas(gl, [255, 0, 0, 255], "should be red"); |
| 635 |
| 636 // unbind vao |
| 637 ext.bindVertexArrayOES(null); |
| 638 |
| 639 // At this point the nonVAOColorBuffer should be still be bound. |
| 640 // If the WebGL impl is emulating VAOs it must make sure |
| 641 // it correctly restores this binding. |
| 642 gl.enableVertexAttribArray(0); |
| 643 gl.vertexAttribPointer(0, 4, gl.UNSIGNED_BYTE, true, 0, 0); |
| 644 gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_BYTE, 0); |
| 645 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green"); |
| 646 } |
| 647 |
| 648 debug(""); |
| 649 var successfullyParsed = true; |
| 650 </script> |
| 651 <script src="../../resources/js-test-post.js"></script> |
| 652 |
| 653 </body> |
| 654 </html> |
OLD | NEW |