summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/rendering/clearbuffer-and-draw.html
blob: 051066c8ea475d3ac7d4719b80016c059c1342bf (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!--
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>Test clearBuffer with drawing</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 onload="runTest()">
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas" width="64" height="64" style="position:fixed;left:0;top:0"> </canvas>
<script>
"use strict";
description("This tests the operation of clearBuffer followed by a draw call.");

debug("Verifies that these combined with preserveDrawingBuffer's implicit clears work properly together.");
debug("Regression test for <a href='http://crbug.com/828262'>Chromium bug 828262</a>.");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl;
var testIndex = 0;
var iterations = 0;
var maxIterations = 10;
var prog;
var tests;
var stage = 0;
var blackUint8 = [0, 0, 0, 255];
var redFloat = [1.0, 0.0, 0.0, 0.0];
var redUint8 = [255, 0, 0, 255];
var greenFloat = [0.0, 1.0, 0.0, 0.0];
var greenUint8 = [0, 255, 0, 255];

function verifyOnePixel(kind, x, y, readFormat, readType, arrayType, expectedColor) {
  var buffer = new arrayType(4);
  gl.readPixels(Math.floor(x), Math.floor(y), 1, 1, readFormat, readType, buffer);
  if (buffer[0] == expectedColor[0] &&
      buffer[1] == expectedColor[1] &&
      buffer[2] == expectedColor[2] &&
      buffer[3] == expectedColor[3]) {
    testPassed(kind + " succeeded");
  } else {
    testFailed(kind + " failed. Expected: " + expectedColor + ", got: " + buffer);
  }
}

function testClearBufferAndDraw(test) {
  gl.stencilFunc(gl.EQUAL, 0, 0xFF);
  test['clear']();
  wtu.setFloatDrawColor(gl, greenFloat);
  wtu.drawUnitQuad(gl);
  // Back buffer has no alpha channel.
  let readFormat = gl.RGBA;
  let readType = gl.UNSIGNED_BYTE;
  if (stage == 2) {
    verifyOnePixel("Clearing outside scissor",63, 63, readFormat, readType, Uint8Array, blackUint8);
    verifyOnePixel("Drawing outside scissor", 40, 40, readFormat, readType, Uint8Array, blackUint8);
  }
  verifyOnePixel("Clearing", 0,  0, readFormat, readType, Uint8Array, test['bgColor']);
  verifyOnePixel("Drawing", 32, 32, readFormat, readType, Uint8Array, test['drawColor']);
}

function runNextTest() {
  if (testIndex >= tests.length) {
    // Restore after the last clearBufferiv test
    gl.enable(gl.DEPTH_TEST);
    if (stage == 0) {
      debug('');
      debug('Enabling full-canvas scissor');
      gl.enable(gl.SCISSOR_TEST);
    } else if (stage == 1) {
      debug('');
      debug('Limiting scissor rect');
      gl.scissor(0, 0, 33, 33);
    } else if (stage == 2) {
      finishTest();
      return;
    }
    testIndex = 0;
    stage++;
  }


  let test = tests[testIndex];
  if (iterations == 0) {
    debug('');
    debug('Testing: ' + test['desc'])
  }
  testClearBufferAndDraw(test);

  if (++iterations == maxIterations) {
    iterations = 0;
    ++testIndex;

    // Clear to yellow between the tests to ensure that
    // subsequent tests do not rely on past results.
    gl.clearColor(1.0, 1.0, 0.0, 1.0);
    gl.clear(gl.COLOR_BUFFER_BIT);
  }

  wtu.waitForComposite(runNextTest);
}

function runTest() {
  gl = wtu.create3DContext(canvas, { alpha: false, stencil: true }, 2);

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

  prog = wtu.setupColorQuad(gl, 0, { scale: 0.5 });

  tests = [
    {
      desc: 'Implicit clear',
      clear: function() {},
      bgColor: blackUint8,
      // The implicit clear clears depth to 1.0, and since the quad is
      // drawn at a depth of 0.0, it's always discarded.
      drawColor: blackUint8,
    },
    {
      desc: 'clearBufferfi only',
      clear: function() {
        gl.clearBufferfi(gl.DEPTH_STENCIL, 0, 0.0, 1);
        gl.stencilFunc(gl.EQUAL, 1, 0xFF);
      },
      bgColor: blackUint8,
      drawColor: greenUint8,
    },
    {
      desc: 'clearBufferfv only',
      clear: function() {
        gl.clearBufferfv(gl.DEPTH, 0, [0.0]);
      },
      bgColor: blackUint8,
      drawColor: greenUint8,
    },
    {
      desc: 'clearBufferfv and clear',
      clear: function() {
        gl.clearBufferfv(gl.COLOR, 0, redFloat);
        gl.clearDepth(0.0);
        gl.clear(gl.DEPTH_BUFFER_BIT);
      },
      bgColor: redUint8,
      drawColor: greenUint8,
    },
    {
      desc: 'clearBufferfv (no-op) and clear',
      clear: function() {
        gl.clearBufferfv(gl.COLOR, 1, greenFloat);
        gl.clearDepth(0.0);
        gl.clear(gl.DEPTH_BUFFER_BIT);
      },
      bgColor: blackUint8,
      drawColor: greenUint8,
    },
    {
      desc: 'clearBuffer{fv} and {fi}',
      clear: function() {
        gl.clearBufferfv(gl.COLOR, 0, redFloat);
        gl.clearBufferfi(gl.DEPTH_STENCIL, 0, 0.0, 2);
        gl.stencilFunc(gl.EQUAL, 2, 0xFF);
      },
      bgColor: redUint8,
      drawColor: greenUint8,
    },
    {
      desc: 'clearBufferiv only',
      clear: function() {
        gl.disable(gl.DEPTH_TEST);
        gl.clearBufferiv(gl.STENCIL, 0, [3]);
        gl.stencilFunc(gl.EQUAL, 3, 0xFF);
      },
      bgColor: blackUint8,
      drawColor: greenUint8,
    },
  ];

  // Clear canvas to something other than black to start.
  gl.clearColor(0.0, 0.0, 1.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);

  gl.enable(gl.DEPTH_TEST);
  // Unreal Engine's depth test is reversed from the
  // default. Including the clear of the depth buffer in this test
  // case highlights the rendering error more clearly, since neither
  // the background nor any rendered object show up.
  gl.depthFunc(gl.GEQUAL);

  gl.enable(gl.STENCIL_TEST);

  // Must run in a requestAnimationFrame loop to provoke implicit
  // clears of the canvas.
  wtu.waitForComposite(runNextTest);
}

debug("");
var successfullyParsed = true;

</script>

</body>
</html>