<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="resources/webgl-test.js"></script>
<script src="resources/webgl-test-utils.js"></script>
<script id="vshader" type="x-shader/x-vertex">
attribute vec3 g_Position;
attribute vec2 g_TexCoord0;

varying vec2 texCoord;

void main()
{
    gl_Position = vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);
    texCoord = g_TexCoord0;
}
</script>

<script id="fshader" type="x-shader/x-fragment">
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D tex;
varying vec2 texCoord;
void main()
{
    gl_FragColor = texture2D(tex, texCoord);
}
</script>

<script>

function init()
{
    if (window.initNonKhronosFramework) {
        window.initNonKhronosFramework(true);
    }

    description('Verify copyTexImage2D and copyTexSubImage2D');

    runTest();
}

// These two declarations need to be global for "shouldBe" to see them
var pixel = [0, 0, 0];
var correctColor = null;
var gl = null;
var wtu = WebGLTestUtils;

function runTestIteration(antialias, version2)
{
    let canvasName = `antialias${antialias ? "On" : "Off"}${version2 ? "2" : ""}`;
    gl = initWebGL(canvasName, "vshader", "fshader", [ "g_Position", "g_TexCoord0" ], [ 0, 0, 0, 1 ], 1, { antialias: !!antialias}, !!version2);
    if (!gl) {
        testFailed(`Could not create context for antialias: ${antialias} version2:${version2}`);
        return false;
    }
    var textureLoc = gl.getUniformLocation(gl.program, "tex");

    var vertices = new Float32Array([
         1.0,  1.0, 0.0,
        -1.0,  1.0, 0.0,
        -1.0, -1.0, 0.0,
         1.0,  1.0, 0.0,
        -1.0, -1.0, 0.0,
         1.0, -1.0, 0.0]);
    var texCoords = new Float32Array([
        1.0, 1.0,
        0.0, 1.0,
        0.0, 0.0,
        1.0, 1.0,
        0.0, 0.0,
        1.0, 0.0]);
    var texCoordOffset = vertices.byteLength;

    var vbo = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
    gl.bufferData(gl.ARRAY_BUFFER,
                  texCoordOffset + texCoords.byteLength,
                  gl.STATIC_DRAW);
    gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
    gl.bufferSubData(gl.ARRAY_BUFFER, texCoordOffset, texCoords);

    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(1);
    gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, texCoordOffset);

    gl.colorMask(1, 1, 1, 0);
    gl.disable(gl.BLEND);
    gl.uniform1i(textureLoc, 0);
    var texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 2, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
    glErrorShouldBe(gl, gl.NO_ERROR);

    debug('Testing copyTexImage2D');
    // Copy red to the texture.
    gl.clearColor(1, 0, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    glErrorShouldBe(gl, gl.NO_ERROR);
    gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, 2, 2, 0);
    glErrorShouldBe(gl, gl.NO_ERROR);

    gl.clearColor(0, 1, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, 6);
    wtu.checkCanvasRect(gl, 0, 0, 2, 2, [255, 0, 0, 0], "copyTexImage2D is correct (drawn texture was red)", 2);

    debug('Testing copyTexSubImage2D');
    // Copy green to the texture.
    gl.clearColor(0, 1, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    glErrorShouldBe(gl, gl.NO_ERROR);
    gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, 2, 2);
    glErrorShouldBe(gl, gl.NO_ERROR);

    gl.clearColor(0, 0, 1, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, 6);
    wtu.checkCanvasRect(gl, 0, 0, 2, 2, [0, 255, 0, 0], "copyTexImage2D is correct (drawn texture was green)", 2);

    debug('Testing copyTexImage2D from partial FBO rectangle');
    gl.clearColor(0.5, 0.1, 0.1, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 1, 1, 0);
    gl.clearColor(0, 1, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, 6);
    wtu.checkCanvasRect(gl, 0, 0, 2, 2, [128, 25, 25, 0], "copyTexImage2D from partial FBO rectangle is correct (drawn texture was redish)", 2);
    glErrorShouldBe(gl, gl.NO_ERROR);

    debug('Testing copyTexSubImage2D from partial FBO rectangle');
    gl.clearColor(0.1, 0.5, 0.1, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 1, 1, 1);
    gl.clearColor(0, 0, 1, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, 6);
    wtu.checkCanvasRect(gl, 0, 0, 2, 2, [25, 128, 25, 0], "copyTexSubImage2D from partial FBO rectangle is correct (drawn texture greenish)", 2);
    glErrorShouldBe(gl, gl.NO_ERROR);
}

function runTest(antialias)
{
    debug("Testing with antialias on");
    runTestIteration(true);
    debug("Testing with antialias off");
    runTestIteration(false);
    debug("Testing with antialias on, WebGL2");
    runTestIteration(true, true);
    debug("Testing with antialias off, WebGL2");
    runTestIteration(false, true);
    var epilogue = document.createElement("script");
    epilogue.onload = finish;
    epilogue.src = "../../../resources/js-test-post.js";
    document.body.appendChild(epilogue);
}

function finish() {
    if (window.nonKhronosFrameworkNotifyDone) {
        window.nonKhronosFrameworkNotifyDone();
    }
}
</script>
</head>
<body onload="init()">
<canvas id="antialiasOn" width="2px" height="2px"></canvas>
<canvas id="antialiasOff" width="2px" height="2px"></canvas>
<canvas id="antialiasOn2" width="2px" height="2px"></canvas>
<canvas id="antialiasOff2" width="2px" height="2px"></canvas>
<div id="description"></div>
<div id="console"></div>
</body>
</html>
