forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
oes-vertex-array-object.html
400 lines (340 loc) · 13.3 KB
/
oes-vertex-array-object.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<!--
Copyright (c) 2011 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>WebGL OES_vertex_array_object Conformance Tests</title>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../resources/desktop-gl-constants.js" type="text/javascript"></script>
<script src="../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"></script>
<script src="resources/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
<div id="console"></div>
<!-- Shaders for testing standard derivatives -->
<script>
description("This test verifies the functionality of the OES_vertex_array_object extension, if it is available.");
debug("");
var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = create3DContext(canvas);
var ext = null;
var vao = null;
if (!gl) {
testFailed("WebGL context does not exist");
} else {
testPassed("WebGL context exists");
// Run tests with extension disabled
runBindingTestDisabled();
// Query the extension and store globally so shouldBe can access it
ext = gl.getExtension("OES_vertex_array_object");
if (!ext) {
testPassed("No OES_vertex_array_object support -- this is legal");
runSupportedTest(false);
} else {
testPassed("Successfully enabled OES_vertex_array_object extension");
runSupportedTest(true);
runBindingTestEnabled();
runObjectTest();
runAttributeTests();
runAttributeValueTests();
runDrawTests();
}
}
function runSupportedTest(extensionEnabled) {
var supported = gl.getSupportedExtensions();
if (supported.indexOf("OES_vertex_array_object") >= 0) {
if (extensionEnabled) {
testPassed("OES_vertex_array_object listed as supported and getExtension succeeded");
} else {
testFailed("OES_vertex_array_object listed as supported but getExtension failed");
}
} else {
if (extensionEnabled) {
testFailed("OES_vertex_array_object not listed as supported but getExtension succeeded");
} else {
testPassed("OES_vertex_array_object not listed as supported and getExtension failed -- this is legal");
}
}
}
function runBindingTestDisabled() {
debug("Testing binding enum with extension disabled");
// Use the constant directly as we don't have the extension
var VERTEX_ARRAY_BINDING_OES = 0x85B5;
gl.getParameter(VERTEX_ARRAY_BINDING_OES);
glErrorShouldBe(gl, gl.INVALID_ENUM, "VERTEX_ARRAY_BINDING_OES should not be queryable if extension is disabled");
}
function runBindingTestEnabled() {
debug("Testing binding enum with extension enabled");
shouldBe("ext.VERTEX_ARRAY_BINDING_OES", "0x85B5");
gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES);
glErrorShouldBe(gl, gl.NO_ERROR, "VERTEX_ARRAY_BINDING_OES query should succeed if extension is enable");
// Default value is null
if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) === null) {
testPassed("Default value of VERTEX_ARRAY_BINDING_OES is null");
} else {
testFailed("Default value of VERTEX_ARRAY_BINDING_OES is not null");
}
debug("Testing binding a VAO");
var vao0 = ext.createVertexArrayOES();
var vao1 = ext.createVertexArrayOES();
shouldBeNull("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES)");
ext.bindVertexArrayOES(vao0);
if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) == vao0) {
testPassed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is expected VAO");
} else {
testFailed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is not expected VAO")
}
ext.bindVertexArrayOES(vao1);
if (gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) == vao1) {
testPassed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is expected VAO");
} else {
testFailed("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES) is not expected VAO")
}
ext.bindVertexArrayOES(null);
shouldBeNull("gl.getParameter(ext.VERTEX_ARRAY_BINDING_OES)");
ext.deleteVertexArrayOES(vao0);
ext.deleteVertexArrayOES(vao1);
}
function runObjectTest() {
debug("Testing object creation");
vao = ext.createVertexArrayOES();
glErrorShouldBe(gl, gl.NO_ERROR, "createVertexArrayOES should not set an error");
shouldBeNonNull("vao");
// Expect false if never bound
shouldBeFalse("ext.isVertexArrayOES(vao)");
ext.bindVertexArrayOES(vao);
shouldBeTrue("ext.isVertexArrayOES(vao)");
ext.bindVertexArrayOES(null);
shouldBeTrue("ext.isVertexArrayOES(vao)");
shouldBeFalse("ext.isVertexArrayOES()");
shouldBeFalse("ext.isVertexArrayOES(null)");
ext.deleteVertexArrayOES(vao);
vao = null;
}
function runAttributeTests() {
debug("Testing attributes work across bindings");
var states = [];
var attrCount = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
for (var n = 0; n < attrCount; n++) {
gl.bindBuffer(gl.ARRAY_BUFFER, null);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
var state = {};
states.push(state);
var vao = state.vao = ext.createVertexArrayOES();
ext.bindVertexArrayOES(vao);
if (n % 2 == 0) {
gl.enableVertexAttribArray(n);
} else {
gl.disableVertexAttribArray(n);
}
if (n % 2 == 0) {
var buffer = state.buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW);
gl.vertexAttribPointer(n, 1 + n % 4, gl.FLOAT, true, n * 4, n * 4);
}
if (n % 2 == 0) {
var elbuffer = state.elbuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elbuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 1024, gl.STATIC_DRAW);
}
ext.bindVertexArrayOES(null);
}
var anyMismatch = false;
for (var n = 0; n < attrCount; n++) {
var state = states[n];
ext.bindVertexArrayOES(state.vao);
var isEnabled = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_ENABLED);
if ((n % 2 == 1) || isEnabled) {
// Valid
} else {
testFailed("VERTEX_ATTRIB_ARRAY_ENABLED not preserved");
anyMismatch = true;
}
var buffer = gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING);
if (n % 2 == 0) {
if (buffer == state.buffer) {
// Matched
if ((gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_SIZE) == 1 + n % 4) &&
(gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_TYPE) == gl.FLOAT) &&
(gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_NORMALIZED) == true) &&
(gl.getVertexAttrib(n, gl.VERTEX_ATTRIB_ARRAY_STRIDE) == n * 4) &&
(gl.getVertexAttribOffset(n, gl.VERTEX_ATTRIB_ARRAY_POINTER) == n * 4)) {
// Matched
} else {
testFailed("VERTEX_ATTRIB_ARRAY_* not preserved");
anyMismatch = true;
}
} else {
testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved");
anyMismatch = true;
}
} else {
// GL_CURRENT_VERTEX_ATTRIB is not preserved
if (buffer) {
testFailed("VERTEX_ATTRIB_ARRAY_BUFFER_BINDING not preserved");
anyMismatch = true;
}
}
var elbuffer = gl.getParameter(gl.ELEMENT_ARRAY_BUFFER_BINDING);
if (n % 2 == 0) {
if (elbuffer == state.elbuffer) {
// Matched
} else {
testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved");
anyMismatch = true;
}
} else {
if (elbuffer == null) {
// Matched
} else {
testFailed("ELEMENT_ARRAY_BUFFER_BINDING not preserved");
anyMismatch = true;
}
}
}
ext.bindVertexArrayOES(null);
if (!anyMismatch) {
testPassed("All attributes preserved across bindings");
}
for (var n = 0; n < attrCount; n++) {
var state = states[n];
ext.deleteVertexArrayOES(state.vao);
}
}
function runAttributeValueTests() {
debug("Testing that attribute values are not attached to bindings");
var v;
var vao0 = ext.createVertexArrayOES();
var anyFailed = false;
ext.bindVertexArrayOES(null);
gl.vertexAttrib4f(0, 0, 1, 2, 3);
v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) {
testFailed("Vertex attrib value not round-tripped?");
anyFailed = true;
}
ext.bindVertexArrayOES(vao0);
v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
if (!(v[0] == 0 && v[1] == 1 && v[2] == 2 && v[3] == 3)) {
testFailed("Vertex attrib value reset across bindings");
anyFailed = true;
}
gl.vertexAttrib4f(0, 4, 5, 6, 7);
ext.bindVertexArrayOES(null);
v = gl.getVertexAttrib(0, gl.CURRENT_VERTEX_ATTRIB);
if (!(v[0] == 4 && v[1] == 5 && v[2] == 6 && v[3] == 7)) {
testFailed("Vertex attrib value bound to buffer");
anyFailed = true;
}
if (!anyFailed) {
testPassed("Vertex attribute values are not attached to bindings")
}
ext.bindVertexArrayOES(null);
ext.deleteVertexArrayOES(vao0);
}
function runDrawTests() {
debug("Testing draws with various VAO bindings");
canvas.width = 50; canvas.height = 50;
gl.viewport(0, 0, canvas.width, canvas.height);
var vao0 = ext.createVertexArrayOES();
var vao1 = ext.createVertexArrayOES();
var program = wtu.setupSimpleTextureProgram(gl, 0, 1);
function setupQuad(s) {
var opt_positionLocation = 0;
var opt_texcoordLocation = 1;
var vertexObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
1.0 * s, 1.0 * s, 0.0,
-1.0 * s, 1.0 * s, 0.0,
-1.0 * s, -1.0 * s, 0.0,
1.0 * s, 1.0 * s, 0.0,
-1.0 * s, -1.0 * s, 0.0,
1.0 * s, -1.0 * s, 0.0]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(opt_positionLocation);
gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0);
var vertexObject = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
1.0 * s, 1.0 * s,
0.0 * s, 1.0 * s,
0.0 * s, 0.0 * s,
1.0 * s, 1.0 * s,
0.0 * s, 0.0 * s,
1.0 * s, 0.0 * s]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(opt_texcoordLocation);
gl.vertexAttribPointer(opt_texcoordLocation, 2, gl.FLOAT, false, 0, 0);
};
function readLocation(x, y) {
var pixels = new Uint8Array(1 * 1 * 4);
gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
return pixels;
};
function testPixel(blackList, whiteList) {
function testList(list, expected) {
for (var n = 0; n < list.length; n++) {
var l = list[n];
var x = -Math.floor(l * canvas.width / 2) + canvas.width / 2;
var y = -Math.floor(l * canvas.height / 2) + canvas.height / 2;
var source = readLocation(x, y);
if (Math.abs(source[0] - expected) > 2) {
return false;
}
}
return true;
}
return testList(blackList, 0) && testList(whiteList, 255);
};
function verifyDraw(drawNumber, s) {
wtu.drawQuad(gl);
var blackList = [];
var whiteList = [];
var points = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0];
for (var n = 0; n < points.length; n++) {
if (points[n] <= s) {
blackList.push(points[n]);
} else {
whiteList.push(points[n]);
}
}
if (testPixel(blackList, whiteList)) {
testPassed("Draw " + drawNumber + " passed pixel test");
} else {
testFailed("Draw " + drawNumber + " failed pixel test");
}
};
// Setup all bindings
setupQuad(1);
ext.bindVertexArrayOES(vao0);
setupQuad(0.5);
ext.bindVertexArrayOES(vao1);
setupQuad(0.25);
// Verify drawing
ext.bindVertexArrayOES(null);
verifyDraw(0, 1);
ext.bindVertexArrayOES(vao0);
verifyDraw(1, 0.5);
ext.bindVertexArrayOES(vao1);
verifyDraw(2, 0.25);
ext.bindVertexArrayOES(null);
ext.deleteVertexArrayOES(vao0);
ext.deleteVertexArrayOES(vao1);
}
debug("");
successfullyParsed = true;
</script>
<script src="../resources/js-test-post.js"></script>
<script>
</script>
</body>
</html>