index-validation.html (4567B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 <!DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="utf-8"> 10 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 11 <script src="../../js/js-test-pre.js"></script> 12 <script src="../../js/webgl-test-utils.js"></script> 13 </head> 14 <body> 15 <div id="description"></div> 16 <div id="console"></div> 17 18 <script> 19 "use strict"; 20 description("Tests that index validation verifies the correct number of indices"); 21 22 function sizeInBytes(type) { 23 switch (type) { 24 case gl.BYTE: 25 case gl.UNSIGNED_BYTE: 26 return 1; 27 case gl.SHORT: 28 case gl.UNSIGNED_SHORT: 29 return 2; 30 case gl.INT: 31 case gl.UNSIGNED_INT: 32 case gl.FLOAT: 33 return 4; 34 default: 35 throw "unknown type"; 36 } 37 } 38 39 var wtu = WebGLTestUtils; 40 var gl = wtu.create3DContext(); 41 var program = wtu.loadStandardProgram(gl); 42 43 // 3 vertices => 1 triangle, interleaved data 44 var dataComplete = new Float32Array([0, 0, 0, 1, 45 0, 0, 1, 46 1, 0, 0, 1, 47 0, 0, 1, 48 1, 1, 1, 1, 49 0, 0, 1]); 50 var dataIncomplete = new Float32Array([0, 0, 0, 1, 51 0, 0, 1, 52 1, 0, 0, 1, 53 0, 0, 1, 54 1, 1, 1, 1]); 55 var indices = new Uint16Array([0, 1, 2]); 56 57 debug("Testing with valid indices"); 58 59 var bufferComplete = gl.createBuffer(); 60 gl.bindBuffer(gl.ARRAY_BUFFER, bufferComplete); 61 gl.bufferData(gl.ARRAY_BUFFER, dataComplete, gl.STATIC_DRAW); 62 var elements = gl.createBuffer(); 63 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elements); 64 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); 65 gl.useProgram(program); 66 var vertexLoc = gl.getAttribLocation(program, "a_vertex"); 67 var normalLoc = gl.getAttribLocation(program, "a_normal"); 68 gl.vertexAttribPointer(vertexLoc, 4, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 0); 69 gl.enableVertexAttribArray(vertexLoc); 70 gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 4 * sizeInBytes(gl.FLOAT)); 71 gl.enableVertexAttribArray(normalLoc); 72 shouldBe('gl.checkFramebufferStatus(gl.FRAMEBUFFER)', 'gl.FRAMEBUFFER_COMPLETE'); 73 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 74 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)'); 75 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 76 77 debug("Testing with out-of-range indices"); 78 79 var bufferIncomplete = gl.createBuffer(); 80 gl.bindBuffer(gl.ARRAY_BUFFER, bufferIncomplete); 81 gl.bufferData(gl.ARRAY_BUFFER, dataIncomplete, gl.STATIC_DRAW); 82 gl.vertexAttribPointer(vertexLoc, 4, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 0); 83 gl.enableVertexAttribArray(vertexLoc); 84 gl.disableVertexAttribArray(normalLoc); 85 debug("Enable vertices, valid"); 86 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 87 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)'); 88 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 89 debug("Enable normals, out-of-range"); 90 gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 4 * sizeInBytes(gl.FLOAT)); 91 gl.enableVertexAttribArray(normalLoc); 92 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 93 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)'); 94 wtu.glErrorShouldBe(gl, [gl.INVALID_OPERATION, gl.NO_ERROR]); 95 96 debug("Test with enabled attribute that does not belong to current program"); 97 98 gl.disableVertexAttribArray(normalLoc); 99 var extraLoc = Math.max(vertexLoc, normalLoc) + 1; 100 gl.enableVertexAttribArray(extraLoc); 101 debug("Enable an extra attribute with null"); 102 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 103 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)'); 104 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION); 105 debug("Enable an extra attribute with insufficient data buffer"); 106 gl.vertexAttribPointer(extraLoc, 3, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 4 * sizeInBytes(gl.FLOAT)); 107 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 108 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)'); 109 debug("Pass large negative index to vertexAttribPointer"); 110 gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), -2000000000 * sizeInBytes(gl.FLOAT)); 111 wtu.glErrorShouldBe(gl, gl.INVALID_VALUE); 112 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)'); 113 114 var successfullyParsed = true; 115 </script> 116 117 <script src="../../js/js-test-post.js"></script> 118 </body> 119 </html>