<!--
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>Passing a struct containing a sampler to a function.</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="output" style="border: none;" width="64" height="64"></canvas>
<div id="description"></div>
<div id="console"></div>

<script id="shader-fs" type="x-shader/x-fragment">
    precision mediump float;

    struct SomeStruct{
        sampler2D source;
    };

    vec4 fun(SomeStruct s){
        return texture2D(s.source, vec2(0.5));
    }

    uniform SomeStruct green;
    void main(){
        gl_FragColor = fun(green);
    }
</script>

<script id="shader-fs-array" type="x-shader/x-fragment">
    precision mediump float;

    struct SomeStruct{
        sampler2D source;
    };

    vec4 fun(SomeStruct s[2]){
        return texture2D(s[0].source, vec2(0.5));
    }

    uniform SomeStruct green[2];
    void main(){
        gl_FragColor = fun(green);
    }
</script>

<script>
"use strict";

description();
debug("If the test passes correctly the viewport will be green.");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("output");
var gl = wtu.create3DContext(canvas);

var textureGreen;

var createGreenTexture = function() {
    var texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
    wtu.fillTexture(gl, texture, 1, 1, [0, 255, 0, 255]);
    gl.bindTexture(gl.TEXTURE_2D, null);
    return texture;
};

var test = function(fragShaderId, texUniformName) {
    var program = wtu.setupProgram(gl, [wtu.simpleVertexShader, fragShaderId], ["a_position"], [0], true);

    if (!program) {
        testFailed("Shader compilation/link failed");
    } else {
        // Bind texture
        var uniformMap = wtu.getUniformMap(gl, program);
        gl.activeTexture(gl.TEXTURE0);
        gl.bindTexture(gl.TEXTURE_2D, textureGreen);
        gl.uniform1i(uniformMap[texUniformName].location, 0);

        // Draw
        wtu.clearAndDrawUnitQuad(gl);

        // Verify output
        wtu.checkCanvasRect(gl, 0, 128, 256, 128, [0, 255,0, 255], "should be green",  1);
    }
};

if (!gl) {
    testFailed("context does not exist");
} else {
    wtu.setupUnitQuad(gl, 0, 1);
    textureGreen = createGreenTexture();
    test("shader-fs", "green.source");
    test("shader-fs-array", "green[0].source");
}
var successfullyParsed = true;
</script>
<script src="../../../js/js-test-post.js"></script>
</body>
</html>
