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.
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>
15 <div id=
"description"></div>
16 <div id=
"console"></div>
20 description("Tests that index validation verifies the correct number of indices");
22 function sizeInBytes(type
) {
25 case gl
.UNSIGNED_BYTE
:
28 case gl
.UNSIGNED_SHORT
:
39 var wtu
= WebGLTestUtils
;
40 var gl
= wtu
.create3DContext();
41 var program
= wtu
.loadStandardProgram(gl
);
43 // 3 vertices => 1 triangle, interleaved data
44 var dataComplete
= new Float32Array([0, 0, 0, 1,
50 var dataIncomplete
= new Float32Array([0, 0, 0, 1,
55 var indices
= new Uint16Array([0, 1, 2]);
57 debug("Testing with valid indices");
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
);
77 debug("Testing with out-of-range indices");
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
]);
96 debug("Test with enabled attribute that does not belong to current program");
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)');
114 var successfullyParsed
= true;
117 <script src=
"../../js/js-test-post.js"></script>