<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL OES_element_index_uint Conformance Tests</title>
<script src="resources/desktop-gl-constants.js" type="text/javascript"></script>
<script src="../../../resources/js-test.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>
<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">
#if defined(GL_ES)
precision mediump float;
#endif
varying vec4 color;
void main() {
  gl_FragColor = color;
}
</script>
<script>
description("This test verifies the functionality of the OES_element_index_uint extension, if it is available.");

debug("");

if (window.internals)
    window.internals.settings.setWebGLErrorsToConsoleEnabled(false);

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");

    // 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();

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

        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() {
    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.setupSimpleTextureProgram(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, gl.STATIC_DRAW);

        // 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]), gl.STATIC_DRAW);

        var opt_positionLocation = 0;
        gl.enableVertexAttribArray(opt_positionLocation);
        gl.vertexAttribPointer(opt_positionLocation, 3, 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(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;
                var source = readLocation(x, y);
                if (Math.abs(source[0] - expected) > 2) {
                    return false;
                }
            }
            return true;
        }
        return testList(blockList, 0) && testList(allowList, 255);
    };
    function verifyDraw(drawNumber, s) {
        gl.clearColor(1.0, 1.0, 1.0, 1.0);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
        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]);
            }
        }
        if (testPixel(blockList, allowList)) {
            testPassed("Draw " + drawNumber + " passed pixel test");
        } else {
            testFailed("Draw " + drawNumber + " failed pixel test");
        }
    };

    setupDraw(0.5);
    verifyDraw(0, 0.5);
}

function runIndexValidationTests() {
    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 = 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, gl.STATIC_DRAW);
    var elements = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elements);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
    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');
    glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    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, gl.STATIC_DRAW);
    gl.vertexAttribPointer(vertexLoc, 4, gl.FLOAT, false, 7 * sizeInBytes(gl.FLOAT), 0);
    gl.enableVertexAttribArray(vertexLoc);
    gl.disableVertexAttribArray(normalLoc);
    debug("Enable vertices, valid");
    glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    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);
    glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    glErrorShouldBe(gl, gl.INVALID_OPERATION);

    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");
    glErrorShouldBe(gl, gl.NO_ERROR);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    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));
    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));
    glErrorShouldBe(gl, gl.INVALID_VALUE);
    shouldBeUndefined('gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_INT, 0)');
    gl.disableVertexAttribArray(vertexLoc);
}

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

    var program = 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 ]), gl.STATIC_DRAW);
    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, gl.STATIC_DRAW);
    shouldGenerateGLError(gl, gl.NO_ERROR, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 4)");
    shouldGenerateGLError(gl, gl.INVALID_OPERATION, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 0)");
    shouldGenerateGLError(gl, gl.INVALID_OPERATION, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 8)");
    indices[0] = 2;
    indices[5] = 1;
    shouldGenerateGLError(gl, gl.NO_ERROR, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 4)");
    shouldGenerateGLError(gl, gl.INVALID_OPERATION, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 0)");
    shouldGenerateGLError(gl, gl.INVALID_OPERATION, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 8)");
}

function runResizedBufferTests() {
    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"]);
    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]), gl.STATIC_DRAW);
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
    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]), gl.STATIC_DRAW);
    gl.enableVertexAttribArray(1);
    gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
    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]), gl.STATIC_DRAW);
    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]), gl.STATIC_DRAW);
    gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, false, 0, 0);
    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, gl.STATIC_DRAW);
    glErrorShouldBe(gl, gl.NO_ERROR, "after setting up indices");
    gl.drawElements(gl.TRIANGLES, numQuads * 6, gl.UNSIGNED_INT, 0);
    glErrorShouldBe(gl, gl.NO_ERROR, "after drawing");
}

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

    var program = 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 ]), gl.STATIC_DRAW);
    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 ]), gl.STATIC_DRAW);
    shouldGenerateGLError(gl, gl.NO_ERROR, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 4)");
    shouldGenerateGLError(gl, gl.INVALID_OPERATION, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 0)");
    shouldGenerateGLError(gl, gl.INVALID_OPERATION, "gl.drawElements(gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_INT, 8)");
}

function runCrashWithBufferSubDataTests() {
    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, gl.STATIC_DRAW);
    var data = new Uint32Array(127);
    gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 64, data);
    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("");
</script>
</body>
</html>
