summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/misc/webgl-specific-stencil-settings.html
blob: 770a107dc9e8422fcad66fa48b6786da4f9fc62c (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!--
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 stencil mask/func front-state-back-state equality test</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="console"></div>

<script>
"use strict";
var wtu = WebGLTestUtils;
description("Tests that stencil mask/func are validated correctly when the front state and back state differ.");

var gl;

function checkDrawError(errIfMismatch) {
    wtu.shouldGenerateGLError(gl, errIfMismatch, "wtu.dummySetProgramAndDrawNothing(gl)");
}

function setStencilMask(mask) {
    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.stencilMaskSeparate(gl.FRONT, " + mask[0] + ")");
    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.stencilMaskSeparate(gl.BACK,  " + mask[1] + ")");
}

function testStencilMaskCase(mask, error) {
    setStencilMask(mask);
    // If an error is generated, it should be at draw time.
    checkDrawError(error);
}

function testStencilMask(errIfMismatch) {
    testStencilMaskCase([0, 256], gl.NO_ERROR);
    testStencilMaskCase([1, 256], errIfMismatch);
    testStencilMaskCase([1, 257], gl.NO_ERROR);
    testStencilMaskCase([1, 258], errIfMismatch);
    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.stencilMask(1023)", "resetting stencilMask");
}

function setStencilFunc(ref, mask) {
    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.stencilFuncSeparate(gl.FRONT, gl.ALWAYS, " + ref[0] + ", " + mask[0] + ")");
    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.stencilFuncSeparate(gl.BACK,  gl.ALWAYS, " + ref[1] + ", " + mask[1] + ")");
}

function testStencilFuncCase(ref, mask, error) {
    setStencilFunc(ref, mask);
    // If an error is generated, it should be at draw time.
    checkDrawError(error);
}

function testStencilFunc(errIfMismatch) {
    testStencilFuncCase([ 256,  257], [1023, 1023], gl.NO_ERROR);
    testStencilFuncCase([ 256,  254], [1023, 1023], errIfMismatch);

    testStencilFuncCase([  -1,    0], [1023, 1023], gl.NO_ERROR);
    testStencilFuncCase([  -1,  254], [1023, 1023], errIfMismatch);

    testStencilFuncCase([   0,    0], [   1,  257], gl.NO_ERROR);
    testStencilFuncCase([   0,    0], [   1,  258], errIfMismatch);

    testStencilFuncCase([   1,    1], [1024, 2048], gl.NO_ERROR);
    testStencilFuncCase([   1,    1], [2048, 1024], gl.NO_ERROR);

    testStencilFuncCase([  -1,   -1], [1023, 1023], gl.NO_ERROR);
    testStencilFuncCase([  -1,    0], [1023, 1023], gl.NO_ERROR);
    testStencilFuncCase([   0,   -1], [1023, 1023], gl.NO_ERROR);
    testStencilFuncCase([   0,    0], [1023, 1023], gl.NO_ERROR);

    testStencilFuncCase([  -1,  255], [1023, 1023], errIfMismatch);
    testStencilFuncCase([   0,  256], [1023, 1023], errIfMismatch);
    testStencilFuncCase([   0, 1024], [1023, 1023], errIfMismatch);
    testStencilFuncCase([   1,  257], [1023, 1023], errIfMismatch);
    testStencilFuncCase([ 255,   -1], [1023, 1023], errIfMismatch);
    testStencilFuncCase([ 256,    0], [1023, 1023], errIfMismatch);
    testStencilFuncCase([1024,    0], [1023, 1023], errIfMismatch);
    testStencilFuncCase([ 257,    1], [1023, 1023], errIfMismatch);

    wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "gl.stencilFunc(gl.ALWAYS, 0, 1023)", "resetting stencilFunc");
}

//
// Tests of the default framebuffer
//

debug("");

debug("Testing default framebuffer with { stencil: true }");
gl = wtu.create3DContext(undefined, { stencil: true });
{
    gl.enable(gl.STENCIL_TEST);
    testStencilMaskCase([1, 256], gl.INVALID_OPERATION);
    testStencilFuncCase([256, 0], [1023, 1023], gl.INVALID_OPERATION);
}

debug("Testing default framebuffer with { stencil: false }");
gl = wtu.create3DContext(undefined, { stencil: false });
{
    // with { stencil: false }
    gl.enable(gl.STENCIL_TEST);
    testStencilMaskCase([1, 256], gl.NO_ERROR);
    testStencilFuncCase([256, 0], [1023, 1023], gl.NO_ERROR);
}
// (continue using this GL context for the other tests)

//
// Tests with a framebuffer object
//

const fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
const colorRB = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, colorRB);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, 1, 1);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, colorRB);

wtu.glErrorShouldBe(gl, gl.NO_ERROR, "initial framebuffer setup")

function runWithStencilSettings(haveDepthBuffer, haveStencilBuffer, enableStencilTest, fn) {
    let rbo = null;
    let attachment;
    if (haveDepthBuffer || haveStencilBuffer) {
        rbo = gl.createRenderbuffer();
        gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);

        let internalformat;
        if (haveDepthBuffer && haveStencilBuffer) {
            internalformat = gl.DEPTH_STENCIL;
            attachment = gl.DEPTH_STENCIL_ATTACHMENT;
        } else if (haveDepthBuffer) {
            internalformat = gl.DEPTH_COMPONENT16;
            attachment = gl.DEPTH_ATTACHMENT;
        } else if (haveStencilBuffer) {
            internalformat = gl.STENCIL_INDEX8;
            attachment = gl.STENCIL_ATTACHMENT;
        }
        gl.renderbufferStorage(gl.RENDERBUFFER, internalformat, 1, 1);
        gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, rbo);
    }
    shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "depth/stencil renderbuffer setup")

    if (enableStencilTest) {
        gl.enable(gl.STENCIL_TEST);
    } else {
        gl.disable(gl.STENCIL_TEST);
    }

    fn();

    if (rbo) {
        gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, null);
        gl.bindRenderbuffer(gl.RENDERBUFFER, null);
        gl.deleteRenderbuffer(rbo);
    }

    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "depth/stencil renderbuffer cleanup")
}

function testStencilSettings(haveDepthBuffer, haveStencilBuffer, enableStencilTest, errIfMismatch) {
    debug("");
    debug("With depthbuffer=" + haveDepthBuffer +
            ", stencilbuffer=" + haveStencilBuffer +
            ", stencilTest=" + enableStencilTest +
            ", expecting error=" + wtu.glEnumToString(gl, errIfMismatch) +
            " for mismatching mask or func settings.");

    runWithStencilSettings(haveDepthBuffer, haveStencilBuffer, enableStencilTest, () => {
        // Errors should be the same for both mask and func, because stencil test
        // and stencil write are always enabled/disabled in tandem.
        testStencilMask(errIfMismatch);
        testStencilFunc(errIfMismatch);
    });
}

debug("");
debug("Base case checks:");
testStencilMaskCase([0, 0], gl.NO_ERROR);
testStencilFuncCase([0, 0], [1023, 1023], gl.NO_ERROR);

//                  haveDepthBuffer
//                  |      haveStencilBuffer
//                  |      |      enableStencilTest
//                  |      |      |      errIfMismatch
testStencilSettings(false, false, false, gl.NO_ERROR);
testStencilSettings( true, false, false, gl.NO_ERROR);
testStencilSettings(false,  true, false, gl.NO_ERROR);
testStencilSettings( true,  true, false, gl.NO_ERROR);

testStencilSettings(false, false,  true, gl.NO_ERROR);
testStencilSettings( true, false,  true, gl.NO_ERROR);
testStencilSettings(false,  true,  true, gl.INVALID_OPERATION);
testStencilSettings( true,  true,  true, gl.INVALID_OPERATION);

//
// Tests to make sure the stencil validation check, if cached, is invalidated correctly.
//

debug("");

debug("Setup for stencil validation cache invalidation tests");
setStencilMask([1, 258]);
setStencilFunc([0, 256], [1023, 1023]);

debug("Test with enabling/disabling stencil test");
runWithStencilSettings(false, true, false, () => {
    checkDrawError(gl.NO_ERROR);
    gl.enable(gl.STENCIL_TEST);
    checkDrawError(gl.INVALID_OPERATION);
    gl.disable(gl.STENCIL_TEST);
    checkDrawError(gl.NO_ERROR);
});

debug("Test with swapping in a new FBO");
runWithStencilSettings(false, false, true, () => {
    // no error with no stencil buffer
    checkDrawError(gl.NO_ERROR);

    // swap in a new FBO with a stencil buffer
    const fb2 = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
    gl.bindRenderbuffer(gl.RENDERBUFFER, colorRB);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, colorRB);
    const rbo = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.STENCIL_INDEX8, 1, 1);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, rbo);
    shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    // this draw sholud detect the new fbo state
    checkDrawError(gl.INVALID_OPERATION);

    gl.deleteFramebuffer(fb2);
    gl.deleteRenderbuffer(rbo)
    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
});

debug("Test with adding a stencil attachment");
runWithStencilSettings(false, false, true, () => {
    // no error with no stencil buffer
    checkDrawError(gl.NO_ERROR);

    // add a stencil attachment
    const rbo = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.STENCIL_INDEX8, 1, 1);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, rbo);
    shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    // this draw sholud detect the new fbo state
    checkDrawError(gl.INVALID_OPERATION);

    gl.deleteRenderbuffer(rbo)
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
});

debug("Test with reallocating the DEPTH_STENCIL attachment from depth to depth+stencil");
runWithStencilSettings(false, false, true, () => {
    // attach a depth buffer to the DEPTH_STENCIL attachment
    const rbo = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, 1, 1);
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, rbo);
    shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    // this draw is invalid, but it still might trigger caching of the stencil validation
    checkDrawError(gl.INVALID_FRAMEBUFFER_OPERATION);

    gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, 1, 1);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    // this draw sholud detect the new fbo state
    checkDrawError(gl.INVALID_OPERATION);

    gl.deleteRenderbuffer(rbo)
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
});

gl.deleteFramebuffer(fb);
gl.deleteRenderbuffer(colorRB);

var successfullyParsed = true;
</script>

<script src="../../js/js-test-post.js"></script>
</body>
</html>