<!--
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>WebGL uniform unused array elements get truncated Conformance 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>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<canvas id="example" width="2" height="2"> </canvas>
<script id="vshader" type="x-shader/x-vertex">
    attribute vec4 a_position;
    void main()
    {
        gl_Position = a_position;
    }
</script>
<script id="fshader-max" type="x-shader/x-fragment">
    precision mediump float;
    uniform vec4 colora[$(numUniformVectors)];
    void main()
    {
        gl_FragColor = vec4(colora[$(usedUniformVector)]);
    }
</script>
<script>
"use strict";
description();
debug("");
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example");
var vSrc = wtu.getScript("vshader");
var uniforms;
// This test is to test drivers the have bugs related to optimizing
// an array of uniforms when only 1 of those uniforms is used.
debug("");
var maxUniformVectors = gl.getParameter(gl.MAX_FRAGMENT_UNIFORM_VECTORS);
var tests = [
 { desc: "using 5th element",
   maxUniformVectors: maxUniformVectors,
   numUniformVectors: maxUniformVectors * 2,
   usedUniformVector: 5,
   shader: "fshader-max",
   color: [0, 1, 0, 1],
   arrayName: "colora",
 },
];

// According to the spec unused array elements must be truncated.
var requiredUniformLocationsExist;
function testUniformIssues(testIndex) {
  var test = tests[testIndex];
  debug("");
  debug(test.desc);
  var fSrc = test.source;
  if (!fSrc) {
    fSrc = wtu.replaceParams(wtu.getScript(test.shader), test);
  }

  var consoleElem = document.getElementById("console");
  wtu.addShaderSource(
      consoleElem, "vertex shader", vSrc);
  wtu.addShaderSource(
      consoleElem, "fragment shader", fSrc);

  var program = wtu.loadProgram(gl, vSrc, fSrc);
  gl.useProgram(program);
  uniforms = wtu.getUniformMap(gl, program);
  shouldBe('uniforms["' + test.arrayName + '[0]"].size', (test.usedUniformVector + 1).toString());

  requiredUniformLocationsExist = true;
  for (var ii = 0; ii <= test.usedUniformVector + 1; ++ii) {
    var name = test.arrayName + "[" + ii + "]";
    var colorLocation = gl.getUniformLocation(program, name);
    if (ii <= test.usedUniformVector) {
      if (!colorLocation) {
        requiredUniformLocationsExist = false
      }
    } else {
      if (colorLocation) {
        testFailed("uniform array was not truncated as specified in OpenGL ES 2.0.25 section 2.10.4");
      }
    }
  }
  shouldBeTrue("requiredUniformLocationsExist");
}

var testIndex = 0;
function runNextTest() {
  testUniformIssues(testIndex++);
  if (testIndex < tests.length) {
    setTimeout(runNextTest, 0);
  } else {
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
    debug("");
    finishTest();
  }
}
runNextTest();

var successfullyParsed = true;

</script>
</body>
</html>
