<!--
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>WebGL2 Non-Power of 2 texture 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>
<canvas id="example" width="5" height="3" style="width: 40px; height: 30px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
attribute vec4 vPosition;
attribute vec2 texCoord0;
varying vec2 texCoord;
void main()
{
    gl_Position = vPosition;
    texCoord = texCoord0;
}
</script>

<script id="fshader" type="x-shader/x-fragment">
precision mediump float;
uniform samplerCube tex;
varying vec2 texCoord;
void main()
{
    gl_FragColor = textureCube(tex, normalize(vec3(texCoord, 1)));
}
</script>
<script>
"use strict";
description(document.title);
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example", undefined, 2);
var program = wtu.setupTexturedQuad(gl);

wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

var tests = [
  { format: gl.RGBA,
    type: gl.UNSIGNED_BYTE,
    color: [192, 0, 128, 64],
    expected: [192, 0, 128, 64],
    tolerance: 0,
  },
  { format: gl.RGB,
    type: gl.UNSIGNED_BYTE,
    color: [192, 0, 128],
    expected: [192, 0, 128, 255],
    tolerance: 0,
  },
  { format: gl.LUMINANCE,
    type: gl.UNSIGNED_BYTE,
    color: [192],
    expected: [192, 192, 192, 255],
    tolerance: 0,
  },
  { format: gl.ALPHA,
    type: gl.UNSIGNED_BYTE,
    color: [64],
    expected: [0, 0, 0, 64],
    tolerance: 0,
  },
  { format: gl.LUMINANCE_ALPHA,
    type: gl.UNSIGNED_BYTE,
    color: [192, 64],
    expected: [192, 192, 192, 64],
    tolerance: 0,
  },
];

tests.forEach(function(test) {
  debug("");
  debug("test " + wtu.glEnumToString(gl, test.format) + "/" + wtu.glEnumToString(gl, test.type));
  var tex = gl.createTexture();

  // Check that an NPOT texture not on level 0 does not generate INVALID_VALUE
  wtu.fillTexture(gl, tex, 5, 3, test.color, 1, test.format, test.type);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR,
      "gl.texImage2D with NPOT texture with level > 0 should succeed");

  // Check that an NPOT texture on level 0 succeeds
  wtu.fillTexture(gl, tex, 5, 3, test.color, 0, test.format, test.type);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR,
      "gl.texImage2D with NPOT texture at level 0 should succeed");

  // Check that generateMipmap succeeds on NPOT
  gl.generateMipmap(gl.TEXTURE_2D);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR,
      "gl.generateMipmap with NPOT texture should succeed");

  // Check that nothing is drawn if filtering is not correct for NPOT
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);

  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(
      gl, test.expected,
      "NPOT texture with TEXTURE_WRAP set to REPEAT should draw");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

  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);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR);

  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(
      gl, test.expected,
      "NPOT texture with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);

  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(
      gl, test.expected,
      "NPOT texture with TEXTURE_MIN_FILTER set to LINEAR should draw.");

  gl.copyTexImage2D(gl.TEXTURE_2D, 1, test.format, 0, 0, 5, 3, 0);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR,
      "copyTexImage2D with NPOT texture with level > 0 should succeed.");

  // Check that generateMipmap for an POT texture succeeds
  wtu.fillTexture(gl, tex, 2, 2, test.color, 0, test.format);
  gl.generateMipmap(gl.TEXTURE_2D);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR,
      "gl.texImage2D and gl.generateMipmap with POT texture at level 0 should succeed");

  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);

  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(
      gl, test.expected,
      "POT texture with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw.");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
});

var successfullyParsed = true;

</script>
<script src="../../../js/js-test-post.js"></script>

</body>
</html>

