<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!--
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.
-->
<link rel="stylesheet" type="text/css" href="../unit.css" />
<script type="application/javascript" src="../unit.js"></script>
<script type="application/javascript" src="../util.js"></script>
<script type="application/javascript">

Tests.startUnit = function () {
  var canvas = document.getElementById('gl');
  var unwrappedGL = getGLContext(canvas);
  var gl = wrapGLContext(unwrappedGL);
  return [gl, unwrappedGL];
}

Tests.testUniformf = function(gl, unwrappedGL) {
  var sh = new Filter(gl, 'foobar-vert', 'foobar-frag');
  sh.apply(function(f){
    var fm4 = f.uniform('fm4');
    var fm2 = f.uniform('fm2');
    var fm3 = f.uniform('fm3');
    assertGLError(gl, gl.INVALID_VALUE, "bad transpose 4fv",
        function(){gl.uniformMatrix4fv(fm4, true, [1,0,0,0, 0,2,0,0, 0,0,3,0, 0,0,0,4]);});
    assertGLError(gl, gl.INVALID_VALUE, "bad transpose 3fv",
        function(){gl.uniformMatrix3fv(fm3, true, [1,0,0, 0,2,0, 0,0,3]);});
    assertGLError(gl, gl.INVALID_VALUE, "bad transpose 2fv",
        function(){gl.uniformMatrix2fv(fm2, true, [1,0, 0,2]);});
    assertThrowNoGLError(gl, "bad location",
        function(){gl.uniformMatrix4fv(588939, false, [1,0,0,0, 0,2,0,0, 0,0,3,0, 0,0,0,4]);});
    assertThrowNoGLError(gl, "bad location (negative)",
        function(){gl.uniformMatrix4fv(-588939, false, [1,0,0,0, 0,2,0,0, 0,0,3,0, 0,0,0,4]);});
    assertGLError(gl, gl.INVALID_VALUE, "17 values to 4fv",
        function(){gl.uniformMatrix4fv(fm4, false, [0,1,0,0,0, 0,2,0,0, 0,0,3,0, 0,0,0,4]);});
    assertGLError(gl, gl.INVALID_VALUE, "5 values to 2fv",
        function(){gl.uniformMatrix2fv(fm2, false, [0,2,1, 2,2]);});
    assertGLError(gl, gl.INVALID_VALUE, "10 values to 3fv",
        function(){gl.uniformMatrix3fv(fm3, false, [0,2,2,2, 2,2,2, 2,1,2]);});
    assertGLError(gl, gl.INVALID_VALUE, "too few values to 4fv",
        function(){gl.uniformMatrix4fv(fm4, false, [0,0,0, 0,2,0,0, 0,0,3,0, 0,0,0,4]);});
    assertGLError(gl, gl.INVALID_VALUE, "too few values to 2fv",
        function(){gl.uniformMatrix2fv(fm2, false, [1, 2,2]);});
    assertGLError(gl, gl.INVALID_VALUE, "too few values to 2fv",
        function(){gl.uniformMatrix2fv(fm2, false, []);});
    assertThrowNoGLError(gl, "string for data",
        function(){gl.uniformMatrix2fv(fm2, false, "fm4");});
    assertGLError(gl, gl.INVALID_VALUE, "too few values to 3fv",
        function(){gl.uniformMatrix3fv(fm3, false, [2,2, 2,2,2, 2,1,2]);});
    gl.uniformMatrix4fv(fm4, false, [1,0,0,0, 0,2,0,0, 0,0,3,0, 0,0,0,4]);
    gl.uniformMatrix2fv(fm2, false, [2,1, 2,2]);
    gl.uniformMatrix3fv(fm3, false, [2,2,2, 2,2,2, 2,1,2]);
    assertGLError(gl, gl.INVALID_OPERATION, "3fv on mat4",
        function(){gl.uniformMatrix3fv(fm4, false, [1,0,0, 0,2,0, 0,0,3]);});
    assertGLError(gl, gl.INVALID_OPERATION, "3fv on mat2",
        function(){gl.uniformMatrix3fv(fm2, false, [0,2,1, 0,2,2, 0,0,0]);});
    assertGLError(gl, gl.INVALID_OPERATION, "2fv om mat3",
        function(){gl.uniformMatrix2fv(fm3, false, [2,2, 2,2]);});
    for (var ii = 2; ii <= 4; ++ii) {
      var all = [];
      var mats = [[],[]];
      for (var jj = 0; jj < 2; ++jj) {
        for (var kk = 0; kk < ii * ii; ++kk) {
          mats[jj].push(jj + 1);
          all.push(jj + 1);
        }
      }
      var loc0Name = 'am' + ii + '[0]';
      var loc1Name = 'am' + ii + '[1]';
      var loc0 = f.uniform(loc0Name);
      var loc1 = f.uniform(loc1Name);
      var fname = "uniformMatrix" + ii + "fv";
      assert(loc0Name, loc0 != null);
      assert(loc1Name, loc1 != null);
      assertOk("set array of 2 matrices " + ii + "fv",
          function(){gl[fname].call(gl,loc0, false, all);});
      var actual = unwrappedGL.getUniform(sh.shader.program, loc0);
      assert("got value for loc0",
             gl.NO_ERROR == checkError(gl, "getUniform loc0"));
      assertArrayEquals(mats[0], actual);
      var actual = unwrappedGL.getUniform(sh.shader.program, loc1);
      assert("got value for loc1",
             gl.NO_ERROR == checkError(gl, "getUniform loc1"));
      assertArrayEquals(mats[1], actual);
      assertOk("set array of second array of 2 matrixes",
          function(){gl[fname].call(gl, loc1, false, mats[0]);});
      var actual = unwrappedGL.getUniform(sh.shader.program, loc1);
      assert("got value for loc1",
             gl.NO_ERROR == checkError(gl, "getUniform loc1"));
      assertArrayEquals(mats[0], actual);
      var big = mats[1].concat([3]);
      assertGLError(gl, gl.INVALID_VALUE, "set array of first array of 2 matrixes plus 1 value",
          function(){gl[fname].call(gl, loc0, false, big);});
    }
  });
  var d = new Uint8Array(4);
  gl.readPixels(0,0,1,1,gl.RGBA, gl.UNSIGNED_BYTE, d);
  assertArrayEqualsWithEpsilon([1,2,3,8], d, [1,1,1,1]);
  sh.destroy();
}

Tests.endUnit = function(gl) {
}

</script>
<script id="foobar-vert" type="x-shader/x-vertex">
attribute vec3 Vertex;
attribute vec2 Tex;

uniform mat2 fm2;
uniform mat3 fm3;
uniform mat2 am2[2];
uniform mat3 am3[2];
uniform mat4 am4[2];

varying vec4 texCoord0;
void main()
{
    texCoord0 = vec4(Tex.s, 1.0-Tex.t, fm2[0][1]+fm3[2][1], 0.0);
    float d = am2[0][1][1] + am3[0][2][2] + am4[0][3][3] +
              am2[1][1][1] + am3[1][2][2] + am4[1][3][3];
    gl_Position = vec4(Vertex, 1.0 + d * 0.0001);
}
</script>
<script id="foobar-frag" type="x-shader/x-fragment">
precision mediump float;

uniform mat4 fm4;

varying vec4 texCoord0;
void main()
{
    gl_FragColor = vec4(fm4[0][0]/256.0, fm4[1][1]/256.0, fm4[2][2]/256.0, fm4[3][3]*texCoord0.z/256.0);
}
</script>
<style>canvas{ position:absolute; }</style>
</head><body>
<canvas id="gl" width="16" height="16"></canvas>
</body></html>
