<!--

/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Enable Vertex Attrib Zero 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="50" height="50">
</canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
    attribute vec4 vPosition;
    void main()
    {
        gl_Position = vPosition;
    }
</script>

<script id="fshader" type="x-shader/x-fragment">
    void main()
    {
        gl_FragColor = vec4(0.0,1.0,0.0,1.0);
    }
</script>

<script>
"use strict";
description("Test some of the issues of the difference between attrib 0 on OpenGL vs WebGL");
debug("");
var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example");
var g_program;
var g_attribLocation;
function setup(attribIndex) {
  var program = wtu.setupProgram(
      gl, ['vshader', 'fshader'], ['vPosition'], [attribIndex]);
  g_program = program;
  g_attribLocation = attribIndex;
  shouldBe("g_attribLocation", "gl.getAttribLocation(g_program, 'vPosition')");
  return program;
}

function setupVerts(numVerts) {
  var verts = [
     1.0,  1.0, 0.0,
    -1.0,  1.0, 0.0,
    -1.0, -1.0, 0.0,
     1.0,  1.0, 0.0,
    -1.0, -1.0, 0.0,
     1.0, -1.0, 0.0
  ];
  var positions = new Float32Array(numVerts * 3);
  var indices = new Uint16Array(numVerts);
  for (var ii = 0; ii < numVerts; ++ii) {
    var ndx = ii % 6;
    var dst = ii * 3;
    var src = ndx * 3;
    for (var jj = 0; jj < 3; ++jj) {
      positions[dst + jj] = verts[src + jj];
    }
    indices[ii] = ii;
  }
  var vertexObject = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
  gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
  var indexBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
}

var p0 = setup(0);
var p3 = setup(3);
setupVerts(60000);

for (var ii = 0; ii < 5; ++ii) {
  // test drawing with attrib 0
  gl.useProgram(p0);
  gl.enableVertexAttribArray(0);
  gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.drawElements(gl.TRIANGLES, 60000, gl.UNSIGNED_SHORT, 0);
  wtu.glErrorShouldBe(
      gl, gl.NO_ERROR,
      "drawing using attrib 0 with 6 verts");
  wtu.checkCanvas(gl, [0, 255, 0, 255], "canvas should be green");
  gl.disableVertexAttribArray(0);

  // test drawing without attrib 0
  gl.useProgram(p3);
  gl.enableVertexAttribArray(3);
  gl.vertexAttribPointer(3, 3, gl.FLOAT, false, 0, 0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.drawArrays(gl.TRIANGLES, 0, 60000);
  wtu.glErrorShouldBe(
      gl, gl.NO_ERROR,
      "drawing using attrib 3 with 60000 verts");
  wtu.checkCanvas(gl, [0, 255, 0, 255], "canvas should be green");
  gl.disableVertexAttribArray(3);

  // This second test of drawing without attrib0 unconvered a bug in chrome
  // where after the draw without attrib0 the attrib 0 emulation code disabled
  // attrib 0 and it was never re-enabled so this next draw failed.
  gl.useProgram(p3);
  gl.enableVertexAttribArray(3);
  gl.clear(gl.COLOR_BUFFER_BIT);
  gl.drawElements(gl.TRIANGLES, 60000, gl.UNSIGNED_SHORT, 0);
  wtu.glErrorShouldBe(
      gl, gl.NO_ERROR,
      "drawing using attrib 3 with 60000 verts");
  wtu.checkCanvas(gl, [0, 255, 0, 255], "canvas should be green");
  gl.disableVertexAttribArray(3);
}


var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>

