Skip to content

Commit ae4a4ec

Browse files
Test suite should test that depth test does not affect default framebuffer when context is created with stencil=true, depth=false KhronosGroup#3277 (KhronosGroup#3326)
Change conformance/context/context-attributes-alpha-depth-stencil-antialias.html to test all combinations of stencil, depth, antialias, alpha with the same test. Make the test test all the properties: - drawing with stencil test affects result: yes/no - drawing with depth test affects result: yes/no - drawing with has aliasing: yes/no - drawing with alpha affects result: yes/no Match the results of the draw function with the context creation configuration.
1 parent 61ec62b commit ae4a4ec

File tree

1 file changed

+128
-179
lines changed

1 file changed

+128
-179
lines changed

sdk/tests/conformance/context/context-attributes-alpha-depth-stencil-antialias.html

Lines changed: 128 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
var fbHasColor;
4848
var fbHasDepth;
4949
var fbHasStencil;
50+
var contextVersion = wtu.getDefault3DContextVersion();
5051

5152
function init()
5253
{
@@ -55,6 +56,23 @@
5556
runTest();
5657
}
5758

59+
var vertices = new Float32Array([
60+
1.0, 1.0, 0.0,
61+
-1.0, 1.0, 0.0,
62+
-1.0, -1.0, 0.0,
63+
1.0, 1.0, 0.0,
64+
-1.0, -1.0, 0.0,
65+
1.0, -1.0, 0.0]);
66+
67+
var colors = new Uint8Array([
68+
255, 0, 0, 255,
69+
255, 0, 0, 255,
70+
255, 0, 0, 255,
71+
255, 0, 0, 255,
72+
255, 0, 0, 255,
73+
255, 0, 0, 255]);
74+
75+
5876
function getWebGL(canvasWidth, canvasHeight, contextAttribs, clearColor, clearDepth, clearStencil)
5977
{
6078
var canvas = document.createElement("canvas");
@@ -63,7 +81,7 @@
6381
canvas.width = canvasWidth;
6482
canvas.height = canvasHeight;
6583

66-
gl = wtu.create3DContext(canvas, contextAttribs);
84+
gl = wtu.create3DContext(canvas, contextAttribs, contextVersion);
6785
if (!gl)
6886
return null;
6987

@@ -102,227 +120,158 @@
102120
}
103121
}
104122
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
105-
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
106-
107-
return gl;
108-
}
109123

110-
function drawAndReadPixel(gl, vertices, colors)
111-
{
112124
var colorOffset = vertices.byteLength;
113-
114125
var vbo = gl.createBuffer();
115126
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
116127
gl.bufferData(gl.ARRAY_BUFFER, colorOffset + colors.byteLength, gl.STATIC_DRAW);
117128
gl.bufferSubData(gl.ARRAY_BUFFER, 0, vertices);
118129
gl.bufferSubData(gl.ARRAY_BUFFER, colorOffset, colors);
119-
120130
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
121131
gl.enableVertexAttribArray(0);
122132
gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 0, colorOffset);
123133
gl.enableVertexAttribArray(1);
124134

125-
gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3);
135+
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
136+
return gl;
126137
}
127138

128-
function testDefault()
139+
function draw(gl, verticesCount)
129140
{
130-
debug("Testing default attributes: { stencil:false }");
131-
shouldBeNonNull("gl = getWebGL(1, 1, null, [ 0, 0, 0, 0 ], 1, 0)");
132-
shouldBeFalse("gl.getContextAttributes().stencil");
133-
shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
141+
verticesCount = verticesCount || vertices.length / 3;
142+
gl.drawArrays(gl.TRIANGLES, 0, verticesCount);
134143
}
135144

136-
function testAlpha(alpha)
145+
function checkDraw(hasAlpha, hasStencil, hasDepth, hasAntialias)
137146
{
138-
debug("Testing alpha = " + alpha);
139-
if (alpha) {
140-
shouldBeNonNull("gl = getWebGL(1, 1, { alpha: true, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
141-
shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
142-
} else {
143-
shouldBeNonNull("gl = getWebGL(1, 1, { alpha: false, depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 0 ], 1, 0)");
144-
shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) == 0");
145-
}
146-
shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
147-
shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
148-
shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
149-
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
150-
shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
151-
152-
shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
153-
shouldBeTrue("contextAttribs.alpha == " + alpha);
147+
let red = [255, 0, 0, 255 ];
148+
let black = [0, 0, 0, hasAlpha ? 0 : 255 ];
149+
debug(`Testing that stencil ${ hasStencil ? 'affects': 'does not affect'} the rendering.`);
150+
gl.stencilFunc(gl.NEVER, 1, 1);
151+
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
152+
draw(gl);
153+
correctColor = hasStencil ? black : red;
154+
wtu.checkCanvas(gl, correctColor)
155+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
156+
wtu.checkCanvas(gl, black);
157+
gl.stencilFunc(gl.ALWAYS, 1, 1);
154158

155-
var correctColor = (contextAttribs.alpha ? [0, 0, 0, 0] : [0, 0, 0, 255]);
156-
wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor);
159+
debug(`Testing that depth ${ hasDepth ? 'affects': 'does not affect'} the rendering.`);
160+
gl.depthFunc(gl.NEVER);
161+
draw(gl);
162+
correctColor = hasDepth ? black : red;
163+
wtu.checkCanvas(gl, correctColor);
164+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
165+
wtu.checkCanvas(gl, black);
166+
gl.depthFunc(gl.ALWAYS);
157167

158-
if (fbHasColor) {
159-
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
160-
gl.clearColor(0.5, 0.5, 0.5, 0.5);
161-
gl.clear(gl.COLOR_BUFFER_BIT);
162-
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [127, 127, 127, 127], undefined, 1);
163-
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
168+
debug(`Testing that rendering is ${hasAntialias ? 'antialiased' : 'aliased'}.`);
169+
draw(gl, 3);
170+
let N = 2;
171+
let buf = new Uint8Array(N * N * 4);
172+
gl.readPixels(0, 0, N, N, gl.RGBA, gl.UNSIGNED_BYTE, buf);
173+
redChannels[0] = buf[4 * (N + 1)]; // (1, 1)
174+
redChannels[1] = buf[4 * N * (N - 1)]; // left top
175+
redChannels[2] = buf[4 * (N - 1)]; // right bottom
176+
shouldBe("redChannels[1]", "255");
177+
shouldBe("redChannels[2]", "0");
178+
if (hasAntialias) {
179+
shouldNotBe("redChannels[0]", "255");
180+
shouldNotBe("redChannels[0]", "0");
181+
} else {
182+
shouldBeTrue("redChannels[0] == 255 || redChannels[0] == 0");
164183
}
184+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
185+
wtu.checkCanvas(gl, black);
186+
187+
debug("Testing that rendering works.");
188+
draw(gl);
189+
wtu.checkCanvas(gl, red);
190+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
191+
wtu.checkCanvas(gl, black);
165192
}
166193

167-
function testDepth(depth)
194+
function testDefault()
168195
{
169-
debug("Testing depth = " + depth);
170-
if (depth) {
171-
shouldBeNonNull("gl = getWebGL(1, 1, { stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
172-
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) >= 16");
173-
} else {
174-
shouldBeNonNull("gl = getWebGL(1, 1, { depth: false, stencil: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
175-
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
176-
}
177-
shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
178-
shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
179-
shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
180-
shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
196+
debug("Testing default attributes: { stencil:false }");
197+
shouldBeNonNull("gl = getWebGL(1, 1, null, [ 0, 0, 0, 0 ], 1, 0)");
198+
shouldBeFalse("gl.getContextAttributes().stencil");
199+
shouldBe("gl.getParameter(gl.STENCIL_BITS)", "0");
200+
}
181201

202+
function testAttributesAffectContext(alpha, stencil, depth, antialias)
203+
{
204+
shouldBeNonNull(`gl = getWebGL(2, 2, { depth: ${depth}, stencil: ${stencil}, antialias: ${antialias}, alpha: ${alpha} }, [ 0, 0, 0, 0 ], 1, 0)`);
182205
shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
183206

184-
gl.depthFunc(gl.NEVER);
185-
186-
var vertices = new Float32Array([
187-
1.0, 1.0, 0.0,
188-
-1.0, 1.0, 0.0,
189-
-1.0, -1.0, 0.0,
190-
1.0, 1.0, 0.0,
191-
-1.0, -1.0, 0.0,
192-
1.0, -1.0, 0.0]);
193-
var colors = new Uint8Array([
194-
255, 0, 0, 255,
195-
255, 0, 0, 255,
196-
255, 0, 0, 255,
197-
255, 0, 0, 255,
198-
255, 0, 0, 255,
199-
255, 0, 0, 255]);
200-
201-
drawAndReadPixel(gl, vertices, colors, 0, 0);
202-
correctColor = (contextAttribs.depth ? [0, 0, 0, 255] : [255, 0, 0, 255]);
203-
wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor);
204-
205-
if (fbHasDepth) {
206-
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
207-
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
208-
drawAndReadPixel(gl, vertices, colors, 0, 0);
209-
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255]);
210-
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
207+
shouldBeGreaterThanOrEqual("gl.getParameter(gl.RED_BITS)", "8");
208+
shouldBeGreaterThanOrEqual("gl.getParameter(gl.GREEN_BITS)", "8");
209+
shouldBeGreaterThanOrEqual("gl.getParameter(gl.BLUE_BITS)", "8");
210+
211+
shouldBe("contextAttribs.alpha", "" + alpha);
212+
if (contextVersion < 2) {
213+
if (!stencil)
214+
shouldBeFalse("contextAttribs.stencil");
215+
else
216+
stencil = contextAttribs.stencil;
217+
if (!depth)
218+
shouldBeFalse("contextAttribs.depth");
219+
else
220+
depth = contextAttribs.depth;
221+
if (!antialias)
222+
shouldBeFalse("contextAttribs.antialias");
223+
else
224+
antialias = contextAttribs.antialias;
225+
} else {
226+
shouldBe("contextAttribs.stencil", "" + stencil);
227+
shouldBe("contextAttribs.depth", "" + depth);
228+
shouldBe("contextAttribs.antialias", "" + antialias);
211229
}
212-
}
213230

214-
function testStencilAndDepth(stencil, depth)
215-
{
216-
debug("Testing stencil = " + stencil + ", depth = " + depth);
217-
var creationString =
218-
"gl = getWebGL(1, 1, { depth: " + depth + ", stencil: " + stencil + ", antialias: false }, [ 0, 0, 0, 1 ], 1, 0)";
219-
shouldBeNonNull(creationString);
220-
221-
shouldBeTrue("gl.getParameter(gl.RED_BITS) >= 8");
222-
shouldBeTrue("gl.getParameter(gl.GREEN_BITS) >= 8");
223-
shouldBeTrue("gl.getParameter(gl.BLUE_BITS) >= 8");
224-
shouldBeTrue("gl.getParameter(gl.ALPHA_BITS) >= 8");
225-
if (depth)
226-
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) >= 16");
231+
if (alpha)
232+
shouldBeGreaterThanOrEqual("gl.getParameter(gl.ALPHA_BITS)", "8");
227233
else
228-
shouldBeTrue("gl.getParameter(gl.DEPTH_BITS) == 0");
229-
234+
shouldBe("gl.getParameter(gl.ALPHA_BITS)", "0");
230235
if (stencil)
231-
shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) >= 8");
236+
shouldBeGreaterThanOrEqual("gl.getParameter(gl.STENCIL_BITS)", "8");
232237
else
233-
shouldBeTrue("gl.getParameter(gl.STENCIL_BITS) == 0");
234-
235-
shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
236-
if (!depth && contextAttribs.depth) {
237-
testFailed("WebGL implementation provided a depth buffer when it should not have");
238-
}
239-
if (!contextAttribs.depth)
240-
depth = false;
241-
if (!stencil && contextAttribs.stencil) {
242-
testFailed("WebGL implementation provided a stencil buffer when it should not have");
243-
}
244-
if (!contextAttribs.stencil)
245-
stencil = false;
238+
shouldBe("gl.getParameter(gl.STENCIL_BITS)", "0");
239+
if (depth)
240+
shouldBeGreaterThanOrEqual("gl.getParameter(gl.DEPTH_BITS)", "16");
241+
else
242+
shouldBe("gl.getParameter(gl.DEPTH_BITS)", "0");
246243

247-
gl.depthFunc(gl.ALWAYS);
244+
var correctColor = alpha ? [0, 0, 0, 0] : [0, 0, 0, 255];
245+
wtu.checkCanvas(gl, correctColor);
248246

249-
gl.stencilFunc(gl.NEVER, 1, 1);
250-
gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
247+
debug("Testing default framebuffer.");
248+
checkDraw(alpha, stencil, depth, antialias);
251249

252-
var vertices = new Float32Array([
253-
1.0, 1.0, 0.0,
254-
-1.0, 1.0, 0.0,
255-
-1.0, -1.0, 0.0,
256-
1.0, 1.0, 0.0,
257-
-1.0, -1.0, 0.0,
258-
1.0, -1.0, 0.0]);
259-
var colors = new Uint8Array([
260-
255, 0, 0, 255,
261-
255, 0, 0, 255,
262-
255, 0, 0, 255,
263-
255, 0, 0, 255,
264-
255, 0, 0, 255,
265-
255, 0, 0, 255]);
266-
267-
drawAndReadPixel(gl, vertices, colors, 0, 0);
268-
correctColor = (stencil ? [0, 0, 0, 255] : [255, 0, 0, 255]);
269-
wtu.checkCanvasRect(gl, 0, 0, 1, 1, correctColor)
270-
271-
if (fbHasStencil) {
272-
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
273-
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
274-
drawAndReadPixel(gl, vertices, colors, 0, 0);
275-
wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255]);
276-
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
250+
if (fbHasColor) {
251+
debug("Testing bound framebuffer object.");
252+
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
253+
gl.clearColor(0, 0, 0, 0);
254+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
255+
checkDraw(true, fbHasStencil, fbHasDepth, false);
256+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
277257
}
278258
}
279259

280-
function testAntialias(antialias)
281-
{
282-
debug("Testing antialias = " + antialias);
283-
// Both the width and height of canvas are N.
284-
var N = 2;
285-
if (antialias)
286-
shouldBeNonNull("gl = getWebGL(" + N + ", " + N + ", { depth: false, stencil: false, alpha: false, antialias: true }, [ 0, 0, 0, 1 ], 1, 0)");
287-
else
288-
shouldBeNonNull("gl = getWebGL(" + N + ", " + N + ", { depth: false, stencil: false, alpha: false, antialias: false }, [ 0, 0, 0, 1 ], 1, 0)");
289-
shouldBeNonNull("contextAttribs = gl.getContextAttributes()");
290-
291-
var vertices = new Float32Array([
292-
1.0, 1.0, 0.0,
293-
-1.0, 1.0, 0.0,
294-
-1.0, -1.0, 0.0]);
295-
var colors = new Uint8Array([
296-
255, 0, 0, 255,
297-
255, 0, 0, 255,
298-
255, 0, 0, 255]);
299-
drawAndReadPixel(gl, vertices, colors, 0, 0);
300-
var buf = new Uint8Array(N * N * 4);
301-
gl.readPixels(0, 0, N, N, gl.RGBA, gl.UNSIGNED_BYTE, buf);
302-
redChannels[0] = buf[4 * (N + 1)]; // (1, 1)
303-
redChannels[1] = buf[4 * N * (N - 1)]; // left top
304-
redChannels[2] = buf[4 * (N - 1)]; // right bottom
305-
shouldBeTrue("redChannels[1] == 255 && redChannels[2] == 0");
306-
shouldBe("redChannels[0] != 255 && redChannels[0] != 0", "contextAttribs.antialias");
307-
}
308-
309260
function runTest()
310261
{
311262
testDefault();
312-
testAlpha(true);
313-
testAlpha(false);
314-
testDepth(true);
315-
testDepth(false);
316-
testStencilAndDepth(true, false);
317-
testStencilAndDepth(false, false);
318-
testStencilAndDepth(true, true);
319-
testStencilAndDepth(false, true);
320-
testAntialias(true);
321-
testAntialias(false);
322-
263+
let cases = [false, true];
264+
for (let alpha of cases) {
265+
for (let stencil of cases) {
266+
for (let depth of cases) {
267+
for (let antialias of cases) {
268+
testAttributesAffectContext(alpha, stencil, depth, antialias);
269+
}
270+
}
271+
}
272+
}
323273
finishTest();
324274
}
325-
326275
</script>
327276
</head>
328277
<body onload="init()">

0 commit comments

Comments
 (0)