2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <title>Test clearBuffer with drawing
</title>
12 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
13 <script src=
"../../js/js-test-pre.js"></script>
14 <script src=
"../../js/webgl-test-utils.js"></script>
16 <body onload=
"runTest()">
17 <div id=
"description"></div>
18 <div id=
"console"></div>
19 <canvas id=
"canvas" width=
"64" height=
"64" style=
"position:fixed;left:0;top:0"> </canvas>
22 description("This tests the operation of clearBuffer followed by a draw call.");
24 debug("Verifies that these combined with preserveDrawingBuffer's implicit clears work properly together.");
25 debug("Regression test for <a href='http://crbug.com/828262'>Chromium bug 828262</a>.");
27 var wtu
= WebGLTestUtils
;
28 var canvas
= document
.getElementById("canvas");
32 var maxIterations
= 10;
36 var blackUint8
= [0, 0, 0, 255];
37 var redFloat
= [1.0, 0.0, 0.0, 0.0];
38 var redUint8
= [255, 0, 0, 255];
39 var greenFloat
= [0.0, 1.0, 0.0, 0.0];
40 var greenUint8
= [0, 255, 0, 255];
42 function verifyOnePixel(kind
, x
, y
, readFormat
, readType
, arrayType
, expectedColor
) {
43 var buffer
= new arrayType(4);
44 gl
.readPixels(Math
.floor(x
), Math
.floor(y
), 1, 1, readFormat
, readType
, buffer
);
45 if (buffer
[0] == expectedColor
[0] &&
46 buffer
[1] == expectedColor
[1] &&
47 buffer
[2] == expectedColor
[2] &&
48 buffer
[3] == expectedColor
[3]) {
49 testPassed(kind
+ " succeeded");
51 testFailed(kind
+ " failed. Expected: " + expectedColor
+ ", got: " + buffer
);
55 function testClearBufferAndDraw(test
) {
56 gl
.stencilFunc(gl
.EQUAL
, 0, 0xFF);
58 wtu
.setFloatDrawColor(gl
, greenFloat
);
60 // Back buffer has no alpha channel.
61 let readFormat
= gl
.RGBA
;
62 let readType
= gl
.UNSIGNED_BYTE
;
64 verifyOnePixel("Clearing outside scissor",63, 63, readFormat
, readType
, Uint8Array
, blackUint8
);
65 verifyOnePixel("Drawing outside scissor", 40, 40, readFormat
, readType
, Uint8Array
, blackUint8
);
67 verifyOnePixel("Clearing", 0, 0, readFormat
, readType
, Uint8Array
, test
['bgColor']);
68 verifyOnePixel("Drawing", 32, 32, readFormat
, readType
, Uint8Array
, test
['drawColor']);
71 function runNextTest() {
72 if (testIndex
>= tests
.length
) {
73 // Restore after the last clearBufferiv test
74 gl
.enable(gl
.DEPTH_TEST
);
77 debug('Enabling full-canvas scissor');
78 gl
.enable(gl
.SCISSOR_TEST
);
79 } else if (stage
== 1) {
81 debug('Limiting scissor rect');
82 gl
.scissor(0, 0, 33, 33);
83 } else if (stage
== 2) {
92 let test
= tests
[testIndex
];
93 if (iterations
== 0) {
95 debug('Testing: ' + test
['desc'])
97 testClearBufferAndDraw(test
);
99 if (++iterations
== maxIterations
) {
103 // Clear to yellow between the tests to ensure that
104 // subsequent tests do not rely on past results.
105 gl
.clearColor(1.0, 1.0, 0.0, 1.0);
106 gl
.clear(gl
.COLOR_BUFFER_BIT
);
109 wtu
.waitForComposite(runNextTest
);
113 gl
= wtu
.create3DContext(canvas
, { alpha
: false, stencil
: true }, 2);
116 testFailed("context does not exist");
119 testPassed("context exists");
122 prog
= wtu
.setupColorQuad(gl
, 0, { scale
: 0.5 });
126 desc
: 'Implicit clear',
127 clear: function() {},
129 // The implicit clear clears depth to 1.0, and since the quad is
130 // drawn at a depth of 0.0, it's always discarded.
131 drawColor
: blackUint8
,
134 desc
: 'clearBufferfi only',
136 gl
.clearBufferfi(gl
.DEPTH_STENCIL
, 0, 0.0, 1);
137 gl
.stencilFunc(gl
.EQUAL
, 1, 0xFF);
140 drawColor
: greenUint8
,
143 desc
: 'clearBufferfv only',
145 gl
.clearBufferfv(gl
.DEPTH
, 0, [0.0]);
148 drawColor
: greenUint8
,
151 desc
: 'clearBufferfv and clear',
153 gl
.clearBufferfv(gl
.COLOR
, 0, redFloat
);
155 gl
.clear(gl
.DEPTH_BUFFER_BIT
);
158 drawColor
: greenUint8
,
161 desc
: 'clearBufferfv (no-op) and clear',
163 gl
.clearBufferfv(gl
.COLOR
, 1, greenFloat
);
165 gl
.clear(gl
.DEPTH_BUFFER_BIT
);
168 drawColor
: greenUint8
,
171 desc
: 'clearBuffer{fv} and {fi}',
173 gl
.clearBufferfv(gl
.COLOR
, 0, redFloat
);
174 gl
.clearBufferfi(gl
.DEPTH_STENCIL
, 0, 0.0, 2);
175 gl
.stencilFunc(gl
.EQUAL
, 2, 0xFF);
178 drawColor
: greenUint8
,
181 desc
: 'clearBufferiv only',
183 gl
.disable(gl
.DEPTH_TEST
);
184 gl
.clearBufferiv(gl
.STENCIL
, 0, [3]);
185 gl
.stencilFunc(gl
.EQUAL
, 3, 0xFF);
188 drawColor
: greenUint8
,
192 // Clear canvas to something other than black to start.
193 gl
.clearColor(0.0, 0.0, 1.0, 1.0);
194 gl
.clear(gl
.COLOR_BUFFER_BIT
);
196 gl
.enable(gl
.DEPTH_TEST
);
197 // Unreal Engine's depth test is reversed from the
198 // default. Including the clear of the depth buffer in this test
199 // case highlights the rendering error more clearly, since neither
200 // the background nor any rendered object show up.
201 gl
.depthFunc(gl
.GEQUAL
);
203 gl
.enable(gl
.STENCIL_TEST
);
205 // Must run in a requestAnimationFrame loop to provoke implicit
206 // clears of the canvas.
207 wtu
.waitForComposite(runNextTest
);
211 var successfullyParsed
= true;