<!--
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>GLSL out-of-range texture offset test</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 src="../../js/glsl-conformance-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script id="vshaderInvalidOffset" type="x-shader/x-vertex">#version 300 es
in vec4 a_position;
in vec2 a_in0;
out vec2 v_texCoord;

void main()
{
    gl_Position = a_position;
    v_texCoord = a_in0;
}
</script>
<script id="fshaderInvalidOffset" type="x-shader/x-fragment">#version 300 es
precision mediump float;

in vec2 v_texCoord;
out vec4 my_FragColor;
uniform sampler2D u_sampler;
uniform int x;

void main() {
    my_FragColor = textureOffset(u_sampler, v_texCoord, ivec2(0, $(yoffset)));
}
</script>
<script type="application/javascript">
"use strict";
description("Out-of-range texture offset should not compile.");

var wtu = WebGLTestUtils;

var vshader = wtu.getScript('vshaderInvalidOffset');
var fshaderTemplate = wtu.getScript('fshaderInvalidOffset');

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

if (!gl) {
    testFailed("Unable to initialize WebGL 2.0 context.");
} else {
    var minOffset = gl.getParameter(gl.MIN_PROGRAM_TEXEL_OFFSET);
    var maxOffset = gl.getParameter(gl.MAX_PROGRAM_TEXEL_OFFSET);

    var shaderSrcValidMin = wtu.replaceParams(fshaderTemplate, {'yoffset': minOffset});
    var shaderSrcValidMax = wtu.replaceParams(fshaderTemplate, {'yoffset': maxOffset});
    var shaderSrcBelowMin = wtu.replaceParams(fshaderTemplate, {'yoffset': (minOffset - 1)});
    var shaderSrcAboveMax = wtu.replaceParams(fshaderTemplate, {'yoffset': (maxOffset + 1)});
    var shaderSrcDynamic = wtu.replaceParams(fshaderTemplate, {'yoffset': 'x'});

    GLSLConformanceTester.runTests([
    {
        vShaderSource: vshader,
        fShaderSource: shaderSrcValidMin,
        fShaderSuccess: true,
        linkSuccess: true,
        passMsg: 'Minimum in-range texture offset should compile'
    },
    {
        vShaderSource: vshader,
        fShaderSource: shaderSrcValidMax,
        fShaderSuccess: true,
        linkSuccess: true,
        passMsg: 'Maximum in-range texture offset should compile'
    },
    {
        vShaderSource: vshader,
        fShaderSource: shaderSrcBelowMin,
        fShaderSuccess: false,
        linkSuccess: false,
        passMsg: 'Texture offset below minimum valid value should not compile'
    },
    {
        vShaderSource: vshader,
        fShaderSource: shaderSrcAboveMax,
        fShaderSuccess: false,
        linkSuccess: false,
        passMsg: 'Texture offset above maximum valid value should not compile'
    },
    {
        vShaderSource: vshader,
        fShaderSource: shaderSrcDynamic,
        fShaderSuccess: false,
        linkSuccess: false,
        passMsg: 'Dynamic texture offset should not compile'
    }
    ], 2);
}
</script>
</body>
</html>
