summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/rendering/color-mask-preserved-during-implicit-clears.html
blob: 4aa8740cbe6267b1d3902e757ff1265440b0e9fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!--
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>ColorMask Must Be Preserved During Implicit Clears</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>
<div id="canvases"></div>
<div id="console"></div>
<script>
"use strict";
const wtu = WebGLTestUtils;
const sz = 128;
let frames;
let gl;
let tests = [
  { alpha: false, antialias: false, premultipliedAlpha: false },
  { alpha: false, antialias: false, premultipliedAlpha: true  },
  { alpha: false, antialias: true,  premultipliedAlpha: false },
  { alpha: false, antialias: true,  premultipliedAlpha: true  },
];
let currentTest;
let testIndex = 0;

function initTest() {
  description();
  debug('ColorMask must be preserved during implicit clears of textures.');
  debug('Regression test for <a href="http://crbug.com/911918">http://crbug.com/911918</a>');

  requestAnimationFrame(nextTest);
}

function nextTest() {
  if (testIndex >= tests.length) {
    finishTest();
    return;
  }

  frames = 20;
  let canvases = document.getElementById('canvases');
  let canvas = document.createElement('canvas');
  canvas.width = sz;
  canvas.height = sz;
  canvases.appendChild(canvas);
  currentTest = tests[testIndex];
  ++testIndex;
  gl = wtu.create3DContext(canvas, currentTest);
  requestAnimationFrame(runTest);
}

function runTest() {
  // Create a user-defined framebuffer which has an alpha channel.
  let fb = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
  // Set color mask to disable alpha writes. This is important; see below.
  gl.colorMask(true, true, true, false);
  let tex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, tex);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, sz, sz, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  // Upload a sub-rectangle. In Chrome, this was lazily clearing level 0 of the
  // texture using OpenGL, setting the driver's color mask to (true, true, true,
  // true) in the process. Because the user's color mask was set to (true, true,
  // true, false) above, incorrect caching code was failing to reset the
  // driver's color mask later. On macOS, Chrome implements alpha:false WebGL
  // contexts on top of RGBA textures whose alpha channel is cleared to 1.0 and
  // where the color mask is set to (true, true, true, false) during all writes.
  // This bug was allowing that texture's alpha channel to be destroyed.
  gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(4));
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
  // We have to issue one clear to this framebuffer to get the color mask
  // latched into the internal caching code.
  gl.clear(gl.COLOR_BUFFER_BIT);

  // Switch back to default framebuffer.
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  // Set clear color to transparent green.
  gl.clearColor(0, 1, 0, 0);
  // Clear. Result should be opaque green. Was transparent green when
  // bug was encountered.
  gl.clear(gl.COLOR_BUFFER_BIT);
  wtu.checkCanvasRect(gl, 0, 0, sz, sz,
                      [ 0, 255, 0, 255 ],
                      "default framebuffer should be opaque green for " + JSON.stringify(currentTest));

  gl.deleteFramebuffer(fb);
  gl.deleteTexture(tex);

  --frames;
  if (frames == 0) {
    requestAnimationFrame(nextTest);
  } else {
    requestAnimationFrame(runTest);
  }
}

requestAnimationFrame(initTest);
</script>
</body>
</html>