summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/rendering/blending.html
blob: b14705cd500de9137d23ec84da59e56fa9c37be2 (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
<!--
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">
<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>

<script id="eVsSrc" type="text/plain">
void main()
{
    gl_PointSize = 1.0;
    gl_Position = vec4(0, 0, 0, 1);
}
</script>

<script id="eFsSrc" type="text/plain">
precision mediump float;
uniform vec4 uColor;

void main()
{
    gl_FragColor = uColor;
}
</script>

</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description('Blending tests');

const wtu = WebGLTestUtils;

function CreateContext() {
    const gl = wtu.create3DContext();
    gl.viewport(0, 0, 1, 1);

    gl.prog = wtu.setupProgram(gl, [eVsSrc.innerHTML, eFsSrc.innerHTML]);
    gl.prog.uColor = (() => {
        const loc = gl.getUniformLocation(gl.prog, 'uColor');
        return x => gl.uniform4fv(loc, x);
    })();
    gl.useProgram(gl.prog);
    gl.prog.uColor([1 / 255, 2 / 255, 3 / 255, 4 / 255]);

    gl.drawAndRead = type => {
        gl.drawArrays(gl.POINTS, 0, 1);
        let ret;
        if (type == gl.UNSIGNED_BYTE) {
            ret = new Uint8Array(4);
        } else if (type == gl.FLOAT) {
            ret = new Float32Array(4);
        }
        gl.readPixels(0, 0, 1, 1, gl.RGBA, type, ret);
        return ret;
    };

    gl.enable(gl.BLEND);
    gl.blendFunc(gl.CONSTANT_COLOR, gl.ZERO);

    return gl;
}

function CreateValidFb(gl, formats) {
    const fb = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fb);

    for (let i in formats) {
        i = i|0; // Otherwise i is a string. :(
        const f = formats[i];
        if (!f)
            continue;
        if (f.length == 1) {
            const rb = gl.createRenderbuffer();
            gl.bindRenderbuffer(gl.RENDERBUFFER, rb);
            gl.renderbufferStorage(gl.RENDERBUFFER, f[0], 1, 1);
            gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0+i,
                                       gl.RENDERBUFFER, rb);
            continue;
        }
        if (f.length == 3) {
            let internalFormat = f[0];
            if (internalFormat === undefined) {
                internalFormat = f[1];
            }

            const tex = gl.createTexture();
            gl.bindTexture(gl.TEXTURE_2D, tex);
            gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, 1,1,0, f[1],f[2], null);
            gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0+i,
                                    gl.TEXTURE_2D, tex, 0);
            continue;
        }
        throw new Error('Invalid format length: ' + f);
    }

    const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
    if (status != gl.FRAMEBUFFER_COMPLETE) {
        gl.deleteFramebuffer(fb);
        return null;
    }
    return fb;
}

let was, fb;

const TESTS = [
    () => {
        debug('');
        debug('Clamping of blendColor args:');

        const gl = wtu.create3DContext();
        if (!gl.texImage3D) { // WebGL 1.0
            // WebGL 1.0 clamps without EXT_color_buffer_half_float or WEBGL_color_buffer_float.
            gl.blendColor(1000, 1, 1, 1);
            const was = gl.getParameter(gl.BLEND_COLOR);
            expectArray(was, [1, 1, 1, 1]);

            const ext = gl.getExtension('EXT_color_buffer_half_float') ||
                        gl.getExtension('WEBGL_color_buffer_float');
            if (!ext) return;
        }

        // WebGL 2.0 or extended WebGL 1.0 may still clamp the value on store
        // when the underlying platform does the same.
        gl.blendColor(1000, 1, 1, 1);
        const was = gl.getParameter(gl.BLEND_COLOR);
        if (was[0] == 1000) {
            expectArray(was, [1000, 1, 1, 1]);
        } else {
            debug("Platform does not support unclamped blend color.")
            expectArray(was, [1, 1, 1, 1]);
        }
    },
    () => {
        debug('');
        debug('Blending for RGBA8:');

        const gl = CreateContext();
        fb = CreateValidFb(gl, [[gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE]]);
        shouldBeNonNull('fb');

        // Regardless of the context version and enabled extensions,
        // the value will be clamped at draw time,
        gl.blendColor(10, 1, 1, 1);
        const was = gl.drawAndRead(gl.UNSIGNED_BYTE);
        expectArray(was, [1, 2, 3, 4]);

        if (gl.getExtension('EXT_color_buffer_half_float') ||
            gl.getExtension('WEBGL_color_buffer_float') ||
            gl.getExtension('EXT_color_buffer_float'))
        {
            debug('Enable floating-point color buffers and retest');
            gl.blendColor(1000, 1, 1, 1);
            const was = gl.drawAndRead(gl.UNSIGNED_BYTE);
            expectArray(was, [1, 2, 3, 4]);
        }
    },
    () => {
        debug('');
        debug('Blending for RGBA16F:');

        const gl = CreateContext();

        // Set the value before enabling the extension.
        // It must be clamped only on WebGL 1.0 contexts.
        gl.blendColor(10, 1, 1, 1);
        if (!gl.getExtension('EXT_color_buffer_half_float')) {
            testPassed('Missing ext EXT_color_buffer_half_float is optional, skipping.');
            return;
        }
        if (!gl.texImage3D) { // WebGL 1.0
            const ext = gl.getExtension('OES_texture_half_float');
            gl.HALF_FLOAT = ext.HALF_FLOAT_OES; // These aren't the same value, but this'll work.
        }

        fb = CreateValidFb(gl, [[gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT]]);
        shouldBeNonNull('fb');
        gl.prog.uColor([1, 2, 3, 4]);

        let was = gl.drawAndRead(gl.FLOAT);
        if (!gl.texImage3D) { // WebGL 1.0
            expectArray(was, [1, 2, 3, 4]);
        } else {
            // Some WebGL 2.0 implementations may clamp the blend color anyway.
            const r = gl.getParameter(gl.BLEND_COLOR)[0];
            expectArray(was, [r, 2, 3, 4]);
        }

        // Re-set the value after the extension was enabled.
        gl.blendColor(100, 1, 1, 1);
        const r = gl.getParameter(gl.BLEND_COLOR)[0];
        was = gl.drawAndRead(gl.FLOAT);
        expectArray(was, [r, 2, 3, 4]);
    },
    () => {
        debug('');
        debug('Blending for RGBA32F:');

        const gl = CreateContext();

        // Set the value before enabling the extension.
        // It must be clamped only on WebGL 1.0 contexts.
        gl.blendColor(10, 1, 1, 1);
        if (gl.texImage3D) { // WebGL 2.0
            if (!gl.getExtension('EXT_color_buffer_float')) {
                testPassed('Missing ext EXT_color_buffer_float is optional, skipping.');
                return;
            }
        } else {
            if (!gl.getExtension('WEBGL_color_buffer_float')) {
                testPassed('Missing ext WEBGL_color_buffer_float is optional, skipping.');
                return;
            }
            gl.getExtension('OES_texture_float');
        }
        fb = CreateValidFb(gl, [[gl.RGBA32F, gl.RGBA, gl.FLOAT]]);
        shouldBeNonNull('fb');
        gl.prog.uColor([1, 2, 3, 4]);

        let was = gl.drawAndRead(gl.FLOAT);
        if (!gl.texImage3D) { // WebGL 1.0
            expectArray(was, [1, 2, 3, 4]);
        } else {
            // Some WebGL 2.0 implementations may clamp the blend color anyway.
            const r = gl.getParameter(gl.BLEND_COLOR)[0];
            expectArray(was, [r, 2, 3, 4]);
        }

        // Re-set the value after the extension was enabled.
        gl.blendColor(100, 1, 1, 1);
        const r = gl.getParameter(gl.BLEND_COLOR)[0];
        was = gl.drawAndRead(gl.FLOAT);

        if (!gl.getExtension('EXT_float_blend')) {
            wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, 'Should not be able to blend 32F formats.');
            return;
        }
        wtu.glErrorShouldBe(gl, 0, 'Should be able to blend 32F formats.');
        expectArray(was, [r, 2, 3, 4]);
    },
];

async function Test() {
    for (const fn of TESTS) {
        await wtu.dispatchPromise(fn);
    }
    wtu.destroyAllContexts();
    finishTest();
}

Test();

var successfullyParsed = true;
</script>
</body>
</html>