summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/extensions/webgl-stencil-texturing.html
blob: 729a5bcf8a72586faa67c52481522803945dce1b (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
<!--
Copyright (c) 2023 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 WEBGL_stencil_texturing Conformance Tests</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";
description("This test verifies the functionality of the WEBGL_stencil_texturing extension, if it is available.");

debug("");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext(null, null, 2);
var ext;

function runTestNoExtension() {
    debug("");
    debug("Check the texture parameter without the extension");

    const tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);

    shouldBeNull("gl.getTexParameter(gl.TEXTURE_2D, 0x90EA /* DEPTH_STENCIL_TEXTURE_MODE_WEBGL */)");
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no more errors");

    gl.texParameteri(gl.TEXTURE_2D, 0x90EA /* DEPTH_STENCIL_TEXTURE_MODE_WEBGL */, gl.DEPTH_COMPONENT);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown for texParameteri without enabling the extension");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no more errors");

    gl.texParameterf(gl.TEXTURE_2D, 0x90EA /* DEPTH_STENCIL_TEXTURE_MODE_WEBGL */, gl.DEPTH_COMPONENT);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown for texParameterf without enabling the extension");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no more errors");

    const sampler = gl.createSampler();
    gl.samplerParameteri(sampler, 0x90EA /* DEPTH_STENCIL_TEXTURE_MODE_WEBGL */, gl.DEPTH_COMPONENT);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown for samplerParameteri");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no more errors");
    gl.samplerParameterf(sampler, 0x90EA /* DEPTH_STENCIL_TEXTURE_MODE_WEBGL */, gl.DEPTH_COMPONENT);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown for samplerParameterf");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no more errors");
}

function checkEnums() {
    debug("");
    debug("Check enums");
    shouldBe("ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL", "0x90EA");
    shouldBe("ext.STENCIL_INDEX_WEBGL",              "0x1901");
}

function checkQueries() {
    const tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);

    debug("");
    debug("Check default texture state");
    shouldBe('gl.getTexParameter(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL)', 'gl.DEPTH_COMPONENT');
    debug("");
    debug("Check texture state updates using texParameteri");
    gl.texParameteri(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, ext.STENCIL_INDEX_WEBGL);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
    shouldBe('gl.getTexParameter(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL)', 'ext.STENCIL_INDEX_WEBGL');
    gl.texParameteri(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, gl.DEPTH_COMPONENT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
    shouldBe('gl.getTexParameter(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL)', 'gl.DEPTH_COMPONENT');
    gl.texParameteri(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, 0);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "invalid depth stencil mode value rejected by texParameteri");
    shouldBe('gl.getTexParameter(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL)', 'gl.DEPTH_COMPONENT');
    debug("");
    debug("Check texture state updates using texParameterf");
    gl.texParameterf(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, ext.STENCIL_INDEX_WEBGL);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
    shouldBe('gl.getTexParameter(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL)', 'ext.STENCIL_INDEX_WEBGL');
    gl.texParameterf(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, gl.DEPTH_COMPONENT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
    shouldBe('gl.getTexParameter(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL)', 'gl.DEPTH_COMPONENT');
    gl.texParameterf(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, 0);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "invalid depth stencil mode value rejected by texParameterf");
    shouldBe('gl.getTexParameter(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL)', 'gl.DEPTH_COMPONENT');

    debug("");
    debug("Check that depth stencil texture mode is not accepted as a sampler state");
    const sampler = gl.createSampler();
    gl.samplerParameteri(sampler, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, gl.DEPTH_COMPONENT);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown for samplerParameteri");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no more errors");
    gl.samplerParameterf(sampler, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, gl.DEPTH_COMPONENT);
    wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown for samplerParameterf");
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no more errors");
}

function checkSampling() {
    const formats = [
        {name: "DEPTH_COMPONENT16", internalFormat: gl.DEPTH_COMPONENT16,
         format: gl.DEPTH_COMPONENT, type: gl.UNSIGNED_SHORT},
        {name: "DEPTH_COMPONENT24", internalFormat: gl.DEPTH_COMPONENT24,
         format: gl.DEPTH_COMPONENT, type: gl.UNSIGNED_INT},
        {name: "DEPTH_COMPONENT32F", internalFormat: gl.DEPTH_COMPONENT32F,
         format: gl.DEPTH_COMPONENT, type: gl.FLOAT},
        {name: "DEPTH24_STENCIL8", internalFormat: gl.DEPTH24_STENCIL8,
         format: gl.DEPTH_STENCIL, type: gl.UNSIGNED_INT_24_8},
        {name: "DEPTH32F_STENCIL8", internalFormat: gl.DEPTH32F_STENCIL8,
         format: gl.DEPTH_STENCIL, type: gl.FLOAT_32_UNSIGNED_INT_24_8_REV}
    ];

    gl.enable(gl.DEPTH_TEST);
    gl.enable(gl.STENCIL_TEST);
    gl.stencilFunc(gl.ALWAYS, 170, 0xFF);
    gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);

    wtu.setupUnitQuad(gl);

    const drawProgram = wtu.setupProgram(gl, [wtu.simpleVertexShader,
                                              wtu.simpleColorFragmentShader]);

    const readDepthProgram = wtu.setupProgram(gl, [wtu.simpleTextureVertexShaderESSL300,
                                                   wtu.simpleTextureFragmentShaderESSL300]);

    const readStencilShader = `#version 300 es
        precision highp float;
        uniform highp usampler2D tex;
        in vec2 texCoord;
        out vec4 out_color;
        void main() {
            out_color = vec4(texture(tex, texCoord)) / 255.0;
        }`;
    const readStencilProgram = wtu.setupProgram(gl, [wtu.simpleTextureVertexShaderESSL300,
                                                     readStencilShader]);

    for (const format of formats) {
        debug("");
        debug(`Testing depth stencil texture modes with ${format.name}`);
        const fbo = gl.createFramebuffer();
        gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);

        const rbo = gl.createRenderbuffer();
        gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
        gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA8, 1, 1);
        gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, rbo);

        const tex = gl.createTexture();
        gl.bindTexture(gl.TEXTURE_2D, tex);
        gl.texImage2D(gl.TEXTURE_2D, 0, format.internalFormat, 1, 1, 0, format.format, format.type, null);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texture created");

        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, tex, 0);
        if (format.format == gl.DEPTH_STENCIL) {
            gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.TEXTURE_2D, tex, 0);
        }
        wtu.framebufferStatusShouldBe(gl, gl.FRAMEBUFFER, gl.FRAMEBUFFER_COMPLETE);

        gl.clear(gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
        gl.useProgram(drawProgram);
        wtu.drawUnitQuad(gl);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "no errors after drawing to the depth or depth stencil texture");

        // Detach the depth or depth stencil texture to avoid feedback loop
        gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, null);
        wtu.framebufferStatusShouldBe(gl, gl.FRAMEBUFFER, gl.FRAMEBUFFER_COMPLETE);

        const magFilters = ['NEAREST', 'LINEAR'];

        const minFilters = [
            'NEAREST',
            'LINEAR',
            'NEAREST_MIPMAP_NEAREST',
            'LINEAR_MIPMAP_NEAREST',
            'NEAREST_MIPMAP_LINEAR',
            'LINEAR_MIPMAP_LINEAR'
        ];

        const modes = [
            [gl.DEPTH_COMPONENT, 'DEPTH_COMPONENT'],
            [ext.STENCIL_INDEX_WEBGL, 'STENCIL_INDEX_WEBGL']
        ];

        const programs = [
            [readDepthProgram, 'depth'],
            [readStencilProgram, 'stencil']
        ];

        function validFilters(magFilter, minFilter) {
            return magFilter == gl.NEAREST &&
                   (minFilter == gl.NEAREST || minFilter == gl.NEAREST_MIPMAP_NEAREST);
        }

        for (const program of programs) {
            gl.useProgram(program[0]);
            for (const mode of modes) {
                gl.texParameteri(gl.TEXTURE_2D, ext.DEPTH_STENCIL_TEXTURE_MODE_WEBGL, mode[0]);
                for (const magFilter of magFilters) {
                    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl[magFilter]);
                    for (const minFilter of minFilters) {
                        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl[minFilter]);
                        debug(`Program: ${program[1]}, mode: ${mode[1]}, mag: ${magFilter}, min: ${minFilter}`);

                        gl.clear(gl.COLOR_BUFFER_BIT);
                        wtu.drawUnitQuad(gl);

                        if (format.format == gl.DEPTH_COMPONENT || mode[0] == gl.DEPTH_COMPONENT) {
                            if (program[1] == 'depth') {
                                wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
                                if (validFilters(gl[magFilter], gl[minFilter])) {
                                    wtu.checkCanvasRect(gl, 0, 0, 1, 1, [128, 0, 0, 255], "sampling depth from complete texture", 1);
                                } else {
                                    wtu.checkCanvasRect(gl, 0, 0, 1, 1, [0, 0, 0, 255], "sampling depth from incomplete texture", 1);
                                }
                            } else {
                                wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "sampling depth using incompatible program");
                            }
                        } else {
                            if (program[1] == 'stencil') {
                                wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
                                if (validFilters(gl[magFilter], gl[minFilter])) {
                                    wtu.checkCanvasRect(gl, 0, 0, 1, 1, [170, 0, 0, 1], "sampling stencil from complete texture", 1);
                                } else {
                                    // Incomplete textures may produce [0, 0, 0, 1] or [0, 0, 0, 255].
                                    const value = new Uint8Array(4);
                                    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, value);
                                    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
                                    const msg = "sampling stencil from incomplete texture";
                                    if (value[0] == 0 && value[1] == 0 && value[2] == 0 && (value[3] == 1 || value[3] == 255)) {
                                        testPassed(msg);
                                    } else {
                                        testFailed(`${msg}: ${value}`);
                                    }
                                }
                            } else {
                                wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "sampling stencil using incompatible program");
                            }
                        }
                    }
                }
            }
        }
    }
}

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

    runTestNoExtension();

    ext = gl.getExtension("WEBGL_stencil_texturing");
    wtu.runExtensionSupportedTest(gl, "WEBGL_stencil_texturing", ext !== null);

    if (ext !== null) {
        checkEnums();
        checkQueries();
        checkSampling();
    } else {
        testPassed("No WEBGL_stencil_texturing support -- this is legal");
    }
}

runTest();

var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>
</body>
</html>