summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/extensions/oes-element-index-uint.html
blob: f30acaba28109ccba21dfb37b46fa44b758636b6 (plain)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL OES_element_index_uint Conformance Tests</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>

<script id="vs" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec4 vColor;
varying vec4 color;
void main() {
    gl_Position = vPosition;
    color = vColor;
}
</script>
<script id="fs" type="x-shader/x-fragment">
precision mediump float;
varying vec4 color;
void main() {
  gl_FragColor = color;
}
</script>

</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("This test verifies the functionality of the OES_element_index_uint extension, if it is available.");

debug("");

var wtu = WebGLTestUtils;
var gl = null;
var ext = null;
var canvas = null;

// Test both STATIC_DRAW and DYNAMIC_DRAW as a regression test
// for a bug in ANGLE which has since been fixed.
for (var ii = 0; ii < 2; ++ii) {
    canvas = document.createElement("canvas");
    canvas.width = 50;
    canvas.height = 50;

    gl = wtu.create3DContext(canvas);

    if (!gl) {
        testFailed("WebGL context does not exist");
    } else {
        testPassed("WebGL context exists");

        var drawType = (ii == 0) ? gl.STATIC_DRAW : gl.DYNAMIC_DRAW;
        debug("Testing " + ((ii == 0) ? "STATIC_DRAW" : "DYNAMIC_DRAW"));


        // Query the extension and store globally so shouldBe can access it
        ext = gl.getExtension("OES_element_index_uint");
        if (!ext) {
            testPassed("No OES_element_index_uint support -- this is legal");

            runSupportedTest(false);
        } else {
            testPassed("Successfully enabled OES_element_index_uint extension");

            runSupportedTest(true);

            runDrawTests(drawType);

            // These tests are tweaked duplicates of the buffers/index-validation* tests
            // using unsigned int indices to ensure that behavior remains consistent
            runIndexValidationTests(drawType);
            runCopiesIndicesTests(drawType);
            runResizedBufferTests(drawType);
            runVerifiesTooManyIndicesTests(drawType);
            runCrashWithBufferSubDataTests(drawType);

            wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
        }
    }
}

function runSupportedTest(extensionEnabled) {
    var supported = gl.getSupportedExtensions();
    if (supported.indexOf("OES_element_index_uint") >= 0) {
        if (extensionEnabled) {
            testPassed("OES_element_index_uint listed as supported and getExtension succeeded");
        } else {
            testFailed("OES_element_index_uint listed as supported but getExtension failed");
        }
    } else {
        if (extensionEnabled) {
            testFailed("OES_element_index_uint not listed as supported but getExtension succeeded");
        } else {
            testPassed("OES_element_index_uint not listed as supported and getExtension failed -- this is legal");
        }
    }
}

function runDrawTests(drawType) {
    debug("Test that draws with unsigned integer indices produce the expected results");

    canvas.width = 50; canvas.height = 50;
    gl.viewport(0, 0, canvas.width, canvas.height);

    var program = wtu.setupSimpleColorProgram(gl);

    function setupDraw(s) {
        // Create a vertex buffer that cannot be fully indexed via shorts
        var quadArrayLen = 65537 * 3;
        var quadArray = new Float32Array(quadArrayLen);

        // Leave all but the last 4 values zero-ed out
        var idx = quadArrayLen - 12;

        // Initialized the last 4 values to a quad
        quadArray[idx++] = 1.0 * s;
        quadArray[idx++] = 1.0 * s;
        quadArray[idx++] = 0.0;

        quadArray[idx++] = -1.0 * s;
        quadArray[idx++] = 1.0 * s;
        quadArray[idx++] = 0.0;

        quadArray[idx++] = -1.0 * s;
        quadArray[idx++] = -1.0 * s;
        quadArray[idx++] = 0.0;

        quadArray[idx++] = 1.0 * s;
        quadArray[idx++] = -1.0 * s;
        quadArray[idx++] = 0.0;

        var vertexObject = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
        gl.bufferData(gl.ARRAY_BUFFER, quadArray, drawType);

        // Create an unsigned int index buffer that indexes the last 4 vertices
        var baseIndex = (quadArrayLen / 3) - 4;

        var indexObject = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexObject);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array([
            baseIndex + 0,
            baseIndex + 1,
            baseIndex + 2,
            baseIndex + 2,
            baseIndex + 3,
            baseIndex + 0]), drawType);

        var opt_positionLocation = 0;
        gl.enableVertexAttribArray(opt_positionLocation);
        gl.vertexAttribPointer(opt_positionLocation, 3, gl.FLOAT, false, 0, 0);
    };
    function testPixel(blockList, allowList) {
        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;
                wtu.checkCanvasRect(gl, x, y, 1, 1, [expected, expected, expected],
                    "Draw should pass", 2);
            }
        }
        testList(blockList, 0);
        testList(allowList, 255);
    };
    function verifyDraw(s) {
        gl.clearColor(1.0, 1.0, 1.0, 1.0);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
        wtu.setFloatDrawColor(gl, [0.0, 0.0, 0.0, 1.0]);
        gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, 0);

        var blockList = [];
        var allowList = [];
        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) {
                blockList.push(points[n]);
            } else {
                allowList.push(points[n]);
            }
        }
        testPixel(blockList, allowList);
    };

    setupDraw(0.5);
    verifyDraw(0.5);
}

function runIndexValidationTests(drawType) {
    description("Tests that index validation verifies the correct number of indices");

    function sizeInBytes(type) {
      switch (type) {
      case gl.BYTE:
      case gl.UNSIGNED_BYTE:
        return 1;
      case gl.SHORT:
      case gl.UNSIGNED_SHORT:
        return 2;
      case gl.INT:
      case gl.UNSIGNED_INT:
      case gl.FLOAT:
        return 4;
      default:
        throw "unknown type";
      }
    }

    var program = wtu.loadStandardProgram(gl);

    // 3 vertices => 1 triangle, interleaved data
    var dataComplete = new Float32Array([0, 0, 0, 1,
                                         0, 0, 1,
                                         1, 0, 0, 1,
                                         0, 0, 1,
                                         1, 1, 1, 1,
                                         0, 0, 1]);
    var dataIncomplete = new Float32Array([0, 0, 0, 1,
                                           0, 0, 1,
                                           1, 0, 0, 1,
                                           0, 0, 1,
                                           1, 1, 1, 1]);
    var indices = new Uint32Array([0, 1, 2]);

    debug("Testing with valid indices");

    var bufferComplete = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, bufferComplete);
    gl.bufferData(gl.ARRAY_BUFFER, dataComplete, drawType);
    var elements = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elements);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, drawType);
    gl.useProgram(program);
    var vertexLoc = gl.getAttribLocation(program, "a_vertex");
    var normalLoc = gl.getAttribLocation(program, "a_normal");
    gl.vertexAttribPointer(vertexLoc, 4, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 0);
    gl.enableVertexAttribArray(vertexLoc);
    gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 4 * sizeInBytes(gl.FLOAT));
    gl.enableVertexAttribArray(normalLoc);
    shouldBe('gl.checkFramebufferStatus(gl.FRAMEBUFFER)', 'gl.FRAMEBUFFER_COMPLETE');
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("Testing with out-of-range indices");

    var bufferIncomplete = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, bufferIncomplete);
    gl.bufferData(gl.ARRAY_BUFFER, dataIncomplete, drawType);
    gl.vertexAttribPointer(vertexLoc, 4, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 0);
    gl.enableVertexAttribArray(vertexLoc);
    gl.disableVertexAttribArray(normalLoc);
    debug("Enable vertices, valid");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    debug("Enable normals, out-of-range");
    gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 4 * sizeInBytes(gl.FLOAT));
    gl.enableVertexAttribArray(normalLoc);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    wtu.shouldGenerateGLError(gl, [gl.INVALID_OPERATION, gl.NO_ERROR],
                              'gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');

    debug("Test with enabled attribute that does not belong to current program");

    gl.disableVertexAttribArray(normalLoc);
    var extraLoc = Math.max(vertexLoc, normalLoc) + 1;
    gl.enableVertexAttribArray(extraLoc);
    debug("Enable an extra attribute with null");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION);
    debug("Enable an extra attribute with insufficient data buffer");
    gl.vertexAttribPointer(extraLoc, 3, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 4 * sizeInBytes(gl.FLOAT));
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    debug("Pass large negative index to vertexAttribPointer");
    gl.vertexAttribPointer(normalLoc, 3, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), -2000000000 * sizeInBytes(gl.FLOAT));
    wtu.glErrorShouldBe(gl, gl.INVALID_VALUE);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
}

function runCopiesIndicesTests(drawType) {
    debug("Test that client data is always copied during bufferData and bufferSubData calls");

    var program = wtu.loadStandardProgram(gl);

    gl.useProgram(program);
    var vertexObject = gl.createBuffer();
    gl.enableVertexAttribArray(0);
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
    // 4 vertices -> 2 triangles
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0,0, 0,1,0, 1,0,0, 1,1,0 ]), drawType);
    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);

    var indexObject = gl.createBuffer();

    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexObject);
    var indices = new Uint32Array([ 10000, 0, 1, 2, 3, 10000 ]);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, drawType);
    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 4)");
    var indexValidationError = wtu.shouldGenerateGLError(gl, [gl.INVALID_OPERATION, gl.NO_ERROR],
                                                         "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 0)");
    wtu.shouldGenerateGLError(gl, indexValidationError, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 8)");
    indices[0] = 2;
    indices[5] = 1;
    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 4)");
    wtu.shouldGenerateGLError(gl, indexValidationError, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 0)");
    wtu.shouldGenerateGLError(gl, indexValidationError, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 8)");
}

function runResizedBufferTests(drawType) {
    debug("Test that updating the size of a vertex buffer is properly noticed by the WebGL implementation.");

    var program = wtu.setupProgram(gl, ["vs", "fs"], ["vPosition", "vColor"]);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after initialization");

    var vertexObject = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(
        [-1,1,0, 1,1,0, -1,-1,0,
         -1,-1,0, 1,1,0, 1,-1,0]), drawType);
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after vertex setup");

    var texCoordObject = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, texCoordObject);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(
        [0,0, 1,0, 0,1,
         0,1, 1,0, 1,1]), drawType);
    gl.enableVertexAttribArray(1);
    gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after texture coord setup");

    // Now resize these buffers because we want to change what we're drawing.
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
        -1,1,0, 1,1,0, -1,-1,0, 1,-1,0,
        -1,1,0, 1,1,0, -1,-1,0, 1,-1,0]), drawType);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after vertex redefinition");
    gl.bindBuffer(gl.ARRAY_BUFFER, texCoordObject);
    gl.bufferData(gl.ARRAY_BUFFER, new Uint8Array([
        255, 0, 0, 255,
        255, 0, 0, 255,
        255, 0, 0, 255,
        255, 0, 0, 255,
        0, 255, 0, 255,
        0, 255, 0, 255,
        0, 255, 0, 255,
        0, 255, 0, 255]), drawType);
    gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, false, 0, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after texture coordinate / color redefinition");

    var numQuads = 2;
    var indices = new Uint32Array(numQuads * 6);
    for (var ii = 0; ii < numQuads; ++ii) {
        var offset = ii * 6;
        var quad = (ii == (numQuads - 1)) ? 4 : 0;
        indices[offset + 0] = quad + 0;
        indices[offset + 1] = quad + 1;
        indices[offset + 2] = quad + 2;
        indices[offset + 3] = quad + 2;
        indices[offset + 4] = quad + 1;
        indices[offset + 5] = quad + 3;
    }
    var indexObject = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexObject);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, drawType);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after setting up indices");
    gl.drawElements(gl.TRIANGLES, numQuads * 6, gl.UNSIGNED_INT, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after drawing");
}

function runVerifiesTooManyIndicesTests(drawType) {
    description("Tests that index validation for drawElements does not examine too many indices");

    var program = wtu.loadStandardProgram(gl);

    gl.useProgram(program);
    var vertexObject = gl.createBuffer();
    gl.enableVertexAttribArray(0);
    gl.disableVertexAttribArray(1);
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
    // 4 vertices -> 2 triangles
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,0,0, 0,1,0, 1,0,0, 1,1,0 ]), drawType);
    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);

    var indexObject = gl.createBuffer();

    debug("Test out of range indices")
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexObject);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint32Array([ 10000, 0, 1, 2, 3, 10000 ]), drawType);
    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 4)");
    var indexValidationError = wtu.shouldGenerateGLError(gl, [gl.INVALID_OPERATION, gl.NO_ERROR],
                                                         "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 0)");
    wtu.shouldGenerateGLError(gl, indexValidationError, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 8)");
}

function runCrashWithBufferSubDataTests(drawType) {
    debug('Verifies that the index validation code which is within bufferSubData does not crash.')

    var elementBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementBuffer);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 256, drawType);
    var data = new Uint32Array(127);
    gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 64, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "after attempting to update a buffer outside of the allocated bounds");
    testPassed("bufferSubData, when buffer object was initialized with null, did not crash");
}

debug("");
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>