summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/extensions/oes-shader-multisample-interpolation.html
blob: dcb272c2f0843966905e7c49cf3ab2a5833464be (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!--
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 OES_shader_multisample_interpolation 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>
<canvas width="32" height="32" id="c"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("This test verifies the functionality of the OES_shader_multisample_interpolation extension, if it is available.");

debug("");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("c", { antialias: false }, 2);
var ext;

function runShaderTests(extensionEnabled) {
    debug("");
    debug("Testing various shader compiles with extension " + (extensionEnabled ? "enabled" : "disabled"));

    const macroVertex = `#version 300 es
        in vec4 vPosition;
        void main() {
        #ifdef GL_OES_shader_multisample_interpolation
            gl_Position = vPosition;
        #else
            #error no GL_OES_shader_multisample_interpolation;
        #endif
        }`;

    const macroFragment = `#version 300 es
        precision highp float;
        out vec4 my_FragColor;
        void main() {
        #ifdef GL_OES_shader_multisample_interpolation
            my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
        #else
            #error no GL_OES_shader_multisample_interpolation;
        #endif
        }`;

    for (const shaders of [[wtu.simpleVertexShaderESSL300, macroFragment],
                           [macroVertex, wtu.simpleColorFragmentShaderESSL300]]) {
        // Expect the macro shader to succeed ONLY if enabled
        if (wtu.setupProgram(gl, shaders)) {
            if (extensionEnabled) {
                testPassed("Macro defined in shaders when extension is enabled");
            } else {
                testFailed("Macro defined in shaders when extension is disabled");
            }
        } else {
            if (extensionEnabled) {
                testFailed("Macro not defined in shaders when extension is enabled");
            } else {
                testPassed("Macro not defined in shaders when extension is disabled");
            }
        }
    }

    const missingVertex = `#version 300 es
        sample out float interpolant;
        in vec4 vPosition;
        void main() {
            gl_Position = vPosition;
        }`;

    const missingFragment = `#version 300 es
        precision highp float;
        sample in float interpolant;
        out vec4 my_FragColor;
        void main() {
            my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
        }`;

    // Always expect the shader missing the #extension pragma to fail (whether enabled or not)
    for (const shaders of [[missingVertex, wtu.simpleColorFragmentShaderESSL300],
                           [wtu.simpleVertexShaderESSL300, missingFragment],
                           [missingVertex, missingFragment]]) {
        if (wtu.setupProgram(gl, shaders)) {
            testFailed("Sample interpolation qualifier allowed without #extension pragma");
        } else {
            testPassed("Sample interpolation qualifier disallowed without #extension pragma");
        }
    }

    const validVertex = `#version 300 es
        #extension GL_OES_shader_multisample_interpolation : enable
        sample out float interpolant;
        in vec4 vPosition;
        void main() {
            gl_Position = vPosition;
        }`;

    const validFragment = `#version 300 es
        #extension GL_OES_shader_multisample_interpolation : enable
        precision highp float;
        sample in float interpolant;
        out vec4 my_FragColor;
        void main() {
            my_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
        }`;

    // Try to compile a shader using a sample qualifier that should only succeed if enabled
    if (wtu.setupProgram(gl, [validVertex, validFragment])) {
        if (extensionEnabled) {
            testPassed("Sample interpolation qualifier compiled successfully when extension enabled");
        } else {
            testFailed("Sample interpolation qualifier compiled successfully when extension disabled");
        }
    } else {
        if (extensionEnabled) {
            testFailed("Sample interpolation qualifier failed to compile when extension enabled");
        } else {
            testPassed("Sample interpolation qualifier failed to compile when extension disabled");
        }
    }
}

function runQueryTests(extensionEnabled) {
    debug("");
    debug("Testing parameters with extension " + (extensionEnabled ? "enabled" : "disabled"));
    if (extensionEnabled) {
        shouldBeGreaterThanOrEqual("gl.getParameter(ext.FRAGMENT_INTERPOLATION_OFFSET_BITS_OES)", "4");
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

        const limit = 0.5 - Math.pow(2, -gl.getParameter(ext.FRAGMENT_INTERPOLATION_OFFSET_BITS_OES));
        shouldBeLessThanOrEqual("gl.getParameter(ext.MIN_FRAGMENT_INTERPOLATION_OFFSET_OES)", `-${limit}`);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
        shouldBeGreaterThanOrEqual("gl.getParameter(ext.MAX_FRAGMENT_INTERPOLATION_OFFSET_OES)", `${limit}`);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
    } else {
        shouldBeNull("gl.getParameter(0x8E5B /* MIN_FRAGMENT_INTERPOLATION_OFFSET_OES */)");
        wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
        shouldBeNull("gl.getParameter(0x8E5C /* MAX_FRAGMENT_INTERPOLATION_OFFSET_OES */)");
        wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
        shouldBeNull("gl.getParameter(0x8E5D /* FRAGMENT_INTERPOLATION_OFFSET_BITS_OES */)");
        wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "parameter unknown without enabling the extension");
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
    }
}

function checkEnums() {
    debug("");
    debug("Check enums");
    shouldBe("ext.MIN_FRAGMENT_INTERPOLATION_OFFSET_OES", "0x8E5B");
    shouldBe("ext.MAX_FRAGMENT_INTERPOLATION_OFFSET_OES", "0x8E5C");
    shouldBe("ext.FRAGMENT_INTERPOLATION_OFFSET_BITS_OES", "0x8E5D");
}

/*
 * This test renders a triangle using MSAAx4 and 1x1 viewport
 * with the following vertex colors.
 *
 * | Position |   Color   |
 * |==========|===========|
 * | (-1, -1) | (0, 0, 0) |
 * | (-1, +1) | (0, 1, 0) |
 * | (+1, -1) | (1, 0, 0) |
 *
 * This triangle cannot cover all four samples.
 *
 * When default interpolation is used, the vertex color is interpolated
 * once, most likely in the pixel center.
 *
 * When per-sample interpolation is used, the vertex color is interpolated
 * several times, producing a distinct value for each covered sample.
 * Due to the asymmetry of sample positions, the resolved pixel color must
 * not match the color produced by default interpolation.
 *
 * OpenGL specs do not guarantee specific sample positions, so the test
 * checks only that the resolved colors are different.
 */
function runInterpolationTest() {
    debug("");
    debug("Testing multisample interpolation");

    function draw(program) {
        gl.viewport(0, 0, 1, 1);
        gl.useProgram(program);

        const posLoc = gl.getAttribLocation(program, "position");
        const buf = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, buf);
        gl.bufferData(
            gl.ARRAY_BUFFER,
            new Float32Array([
                -1.0, -1.0,
                -1.0, +1.0,
                +1.0, -1.0]),
            gl.STATIC_DRAW);

        gl.vertexAttribPointer(posLoc, 2, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(posLoc);

        const rbo = gl.createRenderbuffer();
        gl.bindRenderbuffer(gl.RENDERBUFFER, rbo);
        gl.renderbufferStorageMultisample(gl.RENDERBUFFER, 4, gl.RGBA8, 1, 1);

        const fbo = gl.createFramebuffer();
        gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
        gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, rbo);

        wtu.framebufferStatusShouldBe(gl, gl.FRAMEBUFFER, gl.FRAMEBUFFER_COMPLETE);

        gl.clear(gl.COLOR_BUFFER_BIT);
        gl.drawArrays(gl.TRIANGLES, 0, 3);

        gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo);
        gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
        gl.blitFramebuffer(0, 0, 1, 1, 0, 0, 1, 1, gl.COLOR_BUFFER_BIT, gl.NEAREST);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");

        gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
    }

    const vertexCenter = `#version 300 es
        in vec4 position;
        out vec4 interp_color;
        void main() {
            gl_Position = position;
            interp_color = vec4(position.xy * 0.5 + 0.5, 0.0, 1.0);
        }`;

    const fragmentCenter = `#version 300 es
        precision highp float;
        in vec4 interp_color;
        out vec4 fragColor;
        void main() {
            fragColor = interp_color;
        }`;
    const programCenter = wtu.setupProgram(gl, [vertexCenter, fragmentCenter]);

    draw(programCenter);
    const centerColor = new Uint8Array(4);
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, centerColor);

    const vertexSample = `#version 300 es
        #extension GL_OES_shader_multisample_interpolation : require
        in vec4 position;
        sample out vec4 interp_color;
        void main() {
            gl_Position = position;
            interp_color = vec4(position.xy * 0.5 + 0.5, 0.0, 1.0);
        }`;

    const fragmentSample = `#version 300 es
        #extension GL_OES_shader_multisample_interpolation : require
        precision highp float;
        sample in vec4 interp_color;
        out vec4 fragColor;
        void main() {
            fragColor = interp_color;
        }`;
    const programSample = wtu.setupProgram(gl, [vertexSample, fragmentSample]);

    draw(programSample);
    const sampleColor = new Uint8Array(4);
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, sampleColor);

    const message = `Pixel-center value: ${centerColor}, sample-average value: ${sampleColor}`;
    if (centerColor[0] == sampleColor[0] && centerColor[1] == sampleColor[1]) {
        testFailed(message);
    } else {
        testPassed(message);
    }
}

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

    runQueryTests(false);
    runShaderTests(false);

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

    if (!ext) {
        testPassed("No OES_shader_multisample_interpolation support -- this is legal");
    } else {
        testPassed("Successfully enabled OES_shader_multisample_interpolation extension");
        runQueryTests(true);
        runShaderTests(true);
        runInterpolationTest();
    }
}

runTest();

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