<!DOCTYPE html>
<html>
<head>
<title>WebGL Non-Power of 2 texture conformance test.</title>
<script src="../../../resources/js-test.js"></script>
<script src="resources/webgl-test.js"> </script>
<script src="resources/webgl-test-utils.js"> </script>
</head>
<body>
<canvas id="example" width="4" height="4" 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">
#ifdef GL_ES
precision mediump float;
#endif
uniform samplerCube tex;
varying vec2 texCoord;
void main()
{
    gl_FragColor = textureCube(tex, normalize(vec3(texCoord, 1)));
}
</script>
<script>
if (window.internals)
    window.internals.settings.setWebGLErrorsToConsoleEnabled(false);

var wtu = WebGLTestUtils;
var canvas = document.getElementById("example");
var gl = wtu.create3DContext(canvas);
var program = wtu.setupTexturedQuad(gl);

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

var tex = gl.createTexture();

// Check that an NPOT texture not on level 0 generates INVALID_VALUE
wtu.fillTexture(gl, tex, 5, 3, [0, 192, 128, 255], 1);
glErrorShouldBe(gl, gl.INVALID_VALUE,
    "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE");

// Check that an NPOT texture on level 0 succeeds
wtu.fillTexture(gl, tex, 5, 3, [0, 192, 128, 255]);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.texImage2D with NPOT texture at level 0 should succeed");

// Check that generateMipmap fails on NPOT
gl.generateMipmap(gl.TEXTURE_2D);
glErrorShouldBe(gl, gl.INVALID_OPERATION,
    "gl.generateMipmap with NPOT texture should return INVALID_OPERATION");

var loc = gl.getUniformLocation(program, "tex");
gl.uniform1i(loc, 0);

// 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.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "NPOT texture with TEXTURE_WRAP set to REPEAT should draw with 0,0,0,255");
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.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "NPOT texture with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw with 0,0,0,255");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

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

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 192, 128, 255],
    "NPOT texture with TEXTURE_MIN_FILTER set to LINEAR should draw.");

gl.copyTexImage2D(gl.TEXTURE_2D, 1, gl.RGBA, 0, 0, 5, 3, 0);
glErrorShouldBe(gl, gl.INVALID_VALUE,
    "copyTexImage2D with NPOT texture with level > 0 should return INVALID_VALUE.");

// Check that generateMipmap for an POT texture succeeds
wtu.fillTexture(gl, tex, 4, 4, [0, 192, 128, 255]);
gl.generateMipmap(gl.TEXTURE_2D);
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.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 192, 128, 255],
    "POT texture with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw.");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

debug("");
debug("check using cubemap");
var program = wtu.setupProgram(
    gl,
    [wtu.loadShaderFromScript(gl, 'vshader', gl.VERTEX_SHADER),
     wtu.loadShaderFromScript(gl, 'fshader', gl.FRAGMENT_SHADER)],
    ['vPosition', 'texCoord0'], [0, 1]);
var tex = gl.createTexture();

// Check that an NPOT texture not on level 0 generates INVALID_VALUE
fillCubeTexture(gl, tex, 5, 3, [0, 192, 128, 255], 1);
glErrorShouldBe(gl, gl.INVALID_VALUE,
    "gl.texImage2D with NPOT texture with level > 0 should return INVALID_VALUE");

// Check that an NPOT texture on level 0 succeeds
fillCubeTexture(gl, tex, 5, 5, [0, 192, 128, 255]);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.texImage2D with NPOT texture at level 0 should succeed");

// Check that generateMipmap fails on NPOT
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
glErrorShouldBe(gl, gl.INVALID_OPERATION,
    "gl.generateMipmap with NPOT texture should return INVALID_OPERATION");

var loc = gl.getUniformLocation(program, "tex");
gl.uniform1i(loc, 0);

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

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "NPOT cubemap with TEXTURE_WRAP set to REPEAT should draw with 0,0,0,255");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR);

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "NPOT cubemap with TEXTURE_MIN_FILTER not NEAREST or LINEAR should draw with 0,0,0,255");
glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

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

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 192, 128, 255],
    "NPOT cubemap with TEXTURE_MIN_FILTER set to LINEAR should draw.");

// Check that an POT texture on level 0 succeeds
fillCubeTexture(gl, tex, 4, 4, [0, 192, 128, 255]);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.texImage2D with POT texture at level 0 should succeed");

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

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 0, 0, 255],
    "POT cubemap with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR but no mips draw with 0,0,0,255");

// Check that generateMipmap succeeds on POT
gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
glErrorShouldBe(gl, gl.NO_ERROR,
    "gl.generateMipmap with POT texture should return succeed");

wtu.drawQuad(gl);
wtu.checkCanvas(
    gl, [0, 192, 128, 255],
    "POT cubemap with TEXTURE_MIN_FILTER set to LINEAR_MIPMAP_LINEAR should draw.");

function fillCubeTexture(gl, tex, width, height, color, opt_level) {
  opt_level = opt_level || 0;
  var canvas = document.createElement('canvas');
  canvas.width = width;
  canvas.height = height;
  var ctx2d = canvas.getContext('2d');
  ctx2d.fillStyle = "rgba(" + color[0] + "," + color[1] + "," + color[2] + "," + color[3] + ")";
  ctx2d.fillRect(0, 0, width, height);
  gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex);
  var targets = [
    gl.TEXTURE_CUBE_MAP_POSITIVE_X,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
    gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
    gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_Z];
  for (var tt = 0; tt < targets.length; ++tt) {
    gl.texImage2D(
        targets[tt], opt_level, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
  }
};

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

