﻿<!--
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 Draw Buffers Dirty State Bug Conformance Tests</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>
<canvas id="canvas" width="64" height="64"> </canvas>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">#version 300 es
in vec4 a_position;
void main() {
    gl_Position = a_position;
}
</script>
<script id="fshader" type="x-shader/x-fragment">#version 300 es
precision mediump float;

out vec4 my_FragColor;
void main() {
    my_FragColor = vec4(1, 0, 0, 1);
}
</script>
<script>
"use strict";
description("This test verifies a bug in draw buffers dirty state management in Chrome (crbug.com/678153).");

debug("");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, null, 2);

if (!gl) {
  testFailed("WebGL context does not exist");
} else {
  testPassed("WebGL context exists");

  runTest();

  debug("");
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
}

function runTest() {
  debug("");

  var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["a_position"]);
  wtu.setupUnitQuad(gl);

  var width = 2, height = 2;

  var colorTex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, colorTex);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

  var depthTex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, depthTex);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT24, width, height, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, null);

  var fb = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, colorTex, 0);

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Setup should cause no GL errors");

  wtu.clearAndDrawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Clear and draw should cause no GL errors");
  wtu.checkCanvasRect(gl, 0, 0, width, height, [255, 0, 0, 255], "should be red");

  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, depthTex, 0);
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, null, 0);

  wtu.clearAndDrawUnitQuad(gl);
  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Clear and draw should cause no GL errors");

  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, null, 0);
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, colorTex, 0);

  wtu.checkCanvasRect(gl, 0, 0, width, height, [255, 0, 0, 255], "should be red");

  gl.clearColor(0.0, 1.0, 0.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  // Previously in Chrome, switching around the attachments on a framebuffer
  // affected a DrawBuffers cache that was maintained properly during draw
  // calls, but not clears.
  wtu.checkCanvasRect(gl, 0, 0, width, height, [0, 255, 0, 255], "should be green");

  gl.deleteProgram(program);
  gl.deleteFramebuffer(fb);
  gl.deleteTexture(colorTex);
  gl.deleteTexture(depthTex);
}

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

</body>
</html>
