<!--
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>The Color Types of Fragment Shader's Outputs Should Match The Data Types of Color Buffers</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>
</head>
<body>
<canvas id="example" width="8" height="8"></canvas>
<div id="description"></div>
<div id="console"></div>

<script id="vshader" type="x-shader/x-vertex">#version 300 es
in highp vec4 aPosition;
void main() {
    gl_Position = aPosition;
}
</script>

<script id="fshader" type="x-shader/x-fragment">#version 300 es
precision mediump float;
out vec4 oColor;
void main() {
    oColor = vec4(1.0, 0.0, 0.0, 0.0);
}
</script>

<script id="fshaderMRT" type="x-shader/x-fragment">#version 300 es
precision mediump float;
out vec4 oColor[2];
void main() {
    oColor[0] = vec4(1.0, 0.0, 0.0, 0.0);
}
</script>

<script id="fshaderRealMRT" type="x-shader/x-fragment">#version 300 es
precision mediump float;
out vec4 oColor[2];
void main() {
    oColor[0] = vec4(1.0, 0.0, 0.0, 0.0);
    oColor[1] = vec4(0.0, 1.0, 0.0, 0.0);
}
</script>

<script>
"use strict";

var wtu = WebGLTestUtils;
description("This test verifies that the color types of fragment shader's outputs should match color buffers' types.");

var gl = wtu.create3DContext("example", undefined, 2);

var width = 8;
var height = 8;
var tex0;
var tex1;
var rb0;
var rb1;
var fbo = gl.createFramebuffer();
var program0;
var program1;
var program2;

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

    init();

    // COLOR_ATTACHMENT0 is fixed-point data, which can be converted to float.
    // COLOR_ATTACHMENT1 is integer data. The fragment outputs are all float.
    allocate_textures();
    check_type_match();
    allocate_renderbuffers();
    check_type_match();
}

function check_type_match() {
    gl.useProgram(program0);
    rendering([gl.COLOR_ATTACHMENT0, gl.NONE], gl.NO_ERROR);
    rendering([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1], gl.INVALID_OPERATION);

    gl.useProgram(program1);
    rendering([gl.COLOR_ATTACHMENT0, gl.NONE], gl.NO_ERROR);
    rendering([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1], gl.INVALID_OPERATION);

    gl.useProgram(program2);
    rendering([gl.COLOR_ATTACHMENT0, gl.NONE], gl.NO_ERROR);
    rendering([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1], gl.INVALID_OPERATION);
}

function init() {
    program0 = wtu.setupProgram(gl, ['vshader', 'fshader'], ['aPosition'], [0]);
    program1 = wtu.setupProgram(gl, ['vshader', 'fshaderMRT'], ['aPosition'], [0]);
    program2 = wtu.setupProgram(gl, ['vshader', 'fshaderRealMRT'], ['aPosition'], [0]);
    if (!program0 || !program1 || !program2) {
        testFailed("Failed to set up program");
        return;
    }
    testPassed("Succeed to set up program");

    wtu.setupUnitQuad(gl, 0, 1);
    gl.viewport(0, 0, width, height);
}

function allocate_textures() {
    tex0 = gl.createTexture();
    tex1 = gl.createTexture();
    wtu.fillTexture(gl, tex0, width, height, [0xff, 0x0, 0x0, 0xff], 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.RGBA);
    wtu.fillTexture(gl, tex1, width, height, [0x0, 0xff, 0x0, 0xff], 0, gl.RGBA_INTEGER, gl.UNSIGNED_BYTE, gl.RGBA8UI);

    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex0, 0);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT1, gl.TEXTURE_2D, tex1, 0);
}

function allocate_renderbuffers() {
    rb0 = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, rb0);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA8, width, height);
    rb1 = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, rb1);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA8UI, width, height);

    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, rb0);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT1, gl.RENDERBUFFER, rb1);
}

function rendering(draw_buffers, error) {
    gl.drawBuffers(draw_buffers);

    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed("Framebuffer incomplete.");
        return;
    }

    wtu.drawUnitQuad(gl);
    wtu.glErrorShouldBe(gl, error, "If color buffers' type mismatch the type of fragment shader's outputs, geneate INVALID_OPERATION. Otherwise, it should be NO_ERROR");
}

gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.useProgram(null);
gl.deleteTexture(tex0);
gl.deleteTexture(tex1);
gl.deleteRenderbuffer(rb0);
gl.deleteRenderbuffer(rb1);
gl.deleteFramebuffer(fbo);
gl.deleteProgram(program0);
gl.deleteProgram(program1);
gl.deleteProgram(program2);

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

</body>
</html>
