5 <script src=
"../../../resources/js-test.js"></script>
6 <script src=
"resources/webgl-test.js"></script>
9 <div id=
"description"></div>
10 <div id=
"console"></div>
13 description("Tests that index validation verifies the correct number of indices");
16 window
.internals
.settings
.setWebGLErrorsToConsoleEnabled(false);
18 function sizeInBytes(type
) {
21 case gl
.UNSIGNED_BYTE
:
24 case gl
.UNSIGNED_SHORT
:
35 var gl
= create3DContext();
36 var program
= loadStandardProgram(gl
);
38 // 3 vertices => 1 triangle, interleaved data
39 var dataComplete
= new Float32Array([0, 0, 0, 1,
45 var dataIncomplete
= new Float32Array([0, 0, 0, 1,
50 var indices
= new Uint16Array([0, 1, 2]);
52 debug("Testing with valid indices");
54 var bufferComplete
= gl
.createBuffer();
55 gl
.bindBuffer(gl
.ARRAY_BUFFER
, bufferComplete
);
56 gl
.bufferData(gl
.ARRAY_BUFFER
, dataComplete
, gl
.STATIC_DRAW
);
57 var elements
= gl
.createBuffer();
58 gl
.bindBuffer(gl
.ELEMENT_ARRAY_BUFFER
, elements
);
59 gl
.bufferData(gl
.ELEMENT_ARRAY_BUFFER
, indices
, gl
.STATIC_DRAW
);
60 gl
.useProgram(program
);
61 var vertexLoc
= gl
.getAttribLocation(program
, "a_vertex");
62 var normalLoc
= gl
.getAttribLocation(program
, "a_normal");
63 gl
.vertexAttribPointer(vertexLoc
, 4, gl
.FLOAT
, false, 7 * sizeInBytes(gl
.FLOAT
), 0);
64 gl
.enableVertexAttribArray(vertexLoc
);
65 gl
.vertexAttribPointer(normalLoc
, 3, gl
.FLOAT
, false, 7 * sizeInBytes(gl
.FLOAT
), 4 * sizeInBytes(gl
.FLOAT
));
66 gl
.enableVertexAttribArray(normalLoc
);
67 shouldBe('gl.checkFramebufferStatus(gl.FRAMEBUFFER)', 'gl.FRAMEBUFFER_COMPLETE');
68 glErrorShouldBe(gl
, gl
.NO_ERROR
);
69 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)');
70 glErrorShouldBe(gl
, gl
.NO_ERROR
);
72 debug("Testing with out-of-range indices");
74 var bufferIncomplete
= gl
.createBuffer();
75 gl
.bindBuffer(gl
.ARRAY_BUFFER
, bufferIncomplete
);
76 gl
.bufferData(gl
.ARRAY_BUFFER
, dataIncomplete
, gl
.STATIC_DRAW
);
77 gl
.vertexAttribPointer(vertexLoc
, 4, gl
.FLOAT
, false, 7 * sizeInBytes(gl
.FLOAT
), 0);
78 gl
.enableVertexAttribArray(vertexLoc
);
79 gl
.disableVertexAttribArray(normalLoc
);
80 debug("Enable vertices, valid");
81 glErrorShouldBe(gl
, gl
.NO_ERROR
);
82 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)');
83 glErrorShouldBe(gl
, gl
.NO_ERROR
);
84 debug("Enable normals, out-of-range");
85 gl
.vertexAttribPointer(normalLoc
, 3, gl
.FLOAT
, false, 7 * sizeInBytes(gl
.FLOAT
), 4 * sizeInBytes(gl
.FLOAT
));
86 gl
.enableVertexAttribArray(normalLoc
);
87 glErrorShouldBe(gl
, gl
.NO_ERROR
);
88 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)');
89 glErrorShouldBe(gl
, gl
.INVALID_OPERATION
);
91 debug("Test with enabled attribute that does not belong to current program");
93 gl
.disableVertexAttribArray(normalLoc
);
94 var extraLoc
= Math
.max(vertexLoc
, normalLoc
) + 1;
95 gl
.enableVertexAttribArray(extraLoc
);
96 debug("Enable an extra attribute with null");
97 glErrorShouldBe(gl
, gl
.NO_ERROR
);
98 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)');
99 glErrorShouldBe(gl
, gl
.INVALID_OPERATION
);
100 debug("Enable an extra attribute with insufficient data buffer");
101 gl
.vertexAttribPointer(extraLoc
, 3, gl
.FLOAT
, false, 7 * sizeInBytes(gl
.FLOAT
), 4 * sizeInBytes(gl
.FLOAT
));
102 glErrorShouldBe(gl
, gl
.NO_ERROR
);
103 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)');
104 debug("Pass large negative index to vertexAttribPointer");
105 gl
.vertexAttribPointer(normalLoc
, 3, gl
.FLOAT
, false, 7 * sizeInBytes(gl
.FLOAT
), -2000000000 * sizeInBytes(gl
.FLOAT
));
106 glErrorShouldBe(gl
, gl
.INVALID_VALUE
);
107 shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0)');
109 successfullyParsed
= true;