summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/rendering/blitframebuffer-outside-readbuffer.html
blob: a2e87034eba2c679087e30c2b3cbba2a0dd92dc2 (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
<!--
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 BlitFramebuffer 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 id="example" width="8" height="8"></canvas>
<div id="description"></div>
<div id="console"></div>

<script>
"use strict";

var wtu = WebGLTestUtils;
description("This test verifies the functionality of blitFramebuffer.");

var gl = wtu.create3DContext("example", undefined, 2);

function checkPixel(color, expectedColor) {
  var tolerance = 3;
  return (Math.abs(color[0] - expectedColor[0]) <= tolerance &&
          Math.abs(color[1] - expectedColor[1]) <= tolerance &&
          Math.abs(color[2] - expectedColor[2]) <= tolerance &&
          Math.abs(color[3] - expectedColor[3]) <= tolerance);
}

function blitframebuffer_outside_readbuffer(readbufferFormat, drawbufferFormat) {
    debug("");
    debug("blitting outside of read framebuffer, read buffer format is: " + wtu.glEnumToString(gl, readbufferFormat) + ", draw buffer format is: " + wtu.glEnumToString(gl, drawbufferFormat));

    // Initiate data to read framebuffer
    var size_read = 3;
    var uint_read = new Uint8Array(size_read * size_read * 4);
    var start = 0x20;
    for (var ii = 0; ii < size_read * size_read * 4; ii += 4) {
        for (var jj = 0; jj < 3; ++jj) {
          uint_read[ii + jj] = start;
        }
        uint_read[ii + 3] = 0xff;
        start += 0x10;
    }

    // Create read framebuffer and feed data to read buffer
    // Read buffer may has srgb image
    var tex_read = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex_read);
    gl.texImage2D(gl.TEXTURE_2D, 0, readbufferFormat, size_read, size_read, 0, gl.RGBA, gl.UNSIGNED_BYTE, uint_read);

    var fbo_read = gl.createFramebuffer();
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.framebufferTexture2D(gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex_read, 0);

    // Initiate data to draw framebuffer
    var size_draw = 7;
    var uint_draw = new Uint8Array(size_draw * size_draw * 4);
    for (var ii = 0; ii < size_draw * size_draw * 4; ii += 4) {
        for (var jj = 0; jj < 3; ++jj) {
            uint_draw[ii + jj] = 0x10;
        }
        uint_draw[ii + 3] = 0xff;
    }

    // Create draw framebuffer and feed data to draw buffer
    // Draw buffer may has srgb image
    var tex_draw = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex_draw);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);

    gl.texImage2D(gl.TEXTURE_2D, 0, drawbufferFormat, size_draw, size_draw, 0, gl.RGBA, gl.UNSIGNED_BYTE, uint_draw);

    var fbo_draw = gl.createFramebuffer();
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex_draw, 0);

    if (gl.checkFramebufferStatus(gl.READ_FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
        var ref = [
            // The reference pixels of the 1st line: (0, 0) ~ (6, 0)
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],

            // The reference pixels of the 2nd line: (0, 1) ~ (6, 1)
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],

            // The reference pixels of the 3rd line: (0, 2) ~ (6, 2)
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x20, 0x20, 0x20, 0xff], [0x30, 0x30, 0x30, 0xff],
            [0x40, 0x40, 0x40, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],

            // The reference pixels of the 4th line: (0, 3) ~ (6, 3)
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x50, 0x50, 0x50, 0xff], [0x60, 0x60, 0x60, 0xff],
            [0x70, 0x70, 0x70, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],

            // The reference pixels of the 5th line: (0, 4) ~ (6, 4)
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x80, 0x80, 0x80, 0xff], [0x90, 0x90, 0x90, 0xff],
            [0xa0, 0xa0, 0xa0, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],

            // The reference pixels of the 6th line: (0, 5) ~ (6, 5)
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],

            // The reference pixels of the 7th line: (0, 6) ~ (6, 6)
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],
            [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff], [0x10, 0x10, 0x10, 0xff],
        ];

        // The 1st round test: blit read framebuffer to the image in draw framebuffer
        // All directions of the read region have pixels outside of the read buffer
        // The src region and/or dst region may be reversed during blitting.
        var test1 = [
             [-1, 4, 1, 6], // reverse neither src nor dst
             [4, -1, 1, 6], // reverse src only
             [-1, 4, 6, 1], // reverse dst only
             [4, -1, 6, 1]  // reverse both src and dst
        ];

        var readbufferHasSRGBImage = (readbufferFormat == gl.SRGB8_ALPHA8);
        var drawbufferHasSRGBImage = (drawbufferFormat == gl.SRGB8_ALPHA8);

        for (var i = 0; i < 4; ++i) {
            debug("");
            switch (i) {
                case 0: debug("reverse neither src region nor dst region"); break;
                case 1: debug("reverse src region only"); break;
                case 2: debug("reverse dst region only"); break;
                case 3: debug("reverse both src region and dst region"); break;
            }
            var srcStart = test1[i][0];
            var srcEnd = test1[i][1];
            var dstStart = test1[i][2];
            var dstEnd = test1[i][3];
            var realBlittedDstStart = 2;
            var realBlittedDstEnd = 5;
            gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
            gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
            gl.blitFramebuffer(srcStart, srcStart, srcEnd, srcEnd, dstStart, dstStart, dstEnd, dstEnd, gl.COLOR_BUFFER_BIT, gl.LINEAR);

            // Read pixels and check the correctness.
            var pixels = new Uint8Array(size_draw * size_draw * 4);
            gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_draw);
            gl.readPixels(0, 0, size_draw, size_draw, gl.RGBA, gl.UNSIGNED_BYTE, pixels);

            for (var ii = 0; ii < size_draw; ++ii) {
                for (var jj = 0; jj < size_draw; ++jj) {
                    var loc = ii * size_draw + jj;
                    var color = [pixels[loc * 4], pixels[loc * 4 + 1], pixels[loc * 4 + 2], pixels[loc * 4 + 3]];

                    // We may need to reverse the reference loc if necessary
                    var ref_loc = loc;
                    var reverse_src = (srcStart < srcEnd);
                    var reverse_dst = (dstStart < dstEnd);
                    var reversed = reverse_src ^ reverse_dst;
                    if (reversed) {
                        ref_loc = (size_draw - ii - 1) * size_draw + (size_draw - jj -1);
                    }
                    var expectedColor = ref[ref_loc];

                    // We may need to covert the color space for pixels in blit region
                    if ((readbufferHasSRGBImage ^ drawbufferHasSRGBImage) &&
                        (ii >= realBlittedDstStart && ii < realBlittedDstEnd && jj >= realBlittedDstStart && jj < realBlittedDstEnd)) {
                        if (drawbufferHasSRGBImage) {
                            expectedColor = wtu.linearToSRGB(expectedColor);
                        } else {
                            expectedColor = wtu.sRGBToLinear(expectedColor);
                        }
                    }
                    if (checkPixel(color, expectedColor) == true) {
                        testPassed("pixel at [" + jj + ", " + ii + "] is (" + color + "). It is correct!");
                    } else {
                        testFailed("pixel at [" + jj + ", " + ii + "] should be (" + expectedColor + "), but the actual color is (" + color + ")");
                    }
                }
            }
        }

        // The 2nd round test: blit read framebuffer to the image in draw framebuffer
        // Only one direction of the read region have pixels outside of the read buffer
        var tests = [
             [-1, 0], // pixels are outside the left edge of the read buffer
             [0, -1], // pixels are outside the bottom edge of the read buffer
             [1, 0],  // pixels are outside the right edge of the read buffer
             [0, 1]   // pixels are outside the top edge of the read buffer
        ];
        for (var i = 0; i < 4; ++i) {
            debug("");
            switch (i) {
                case 0: debug("verify that pixels lying outside the left edge of the read buffer should remain untouched"); break;
                case 1: debug("verify that pixels lying outside the bottom edge of the read buffer should remain untouched"); break;
                case 2: debug("verify that pixels lying outside the right edge of the read buffer should remain untouched"); break;
                case 3: debug("verify that pixels lying outside the top edge of the read buffer should remain untouched"); break;
            }
            gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
            gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
            var srcX = tests[i][0];
            var srcY = tests[i][1];
            var offset = dstStart - srcStart;
            gl.blitFramebuffer(srcX, srcY, srcX + size_read, srcY + size_read,
                               srcX + offset, srcY + offset, srcX + offset + size_read, srcY + offset + size_read,
                               gl.COLOR_BUFFER_BIT, gl.LINEAR);

            // Read pixels and check the correctness.
            var pixels = new Uint8Array(size_draw * size_draw * 4);
            gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_draw);
            gl.readPixels(0, 0, size_draw, size_draw, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
            for (var ii = srcY + offset; ii < srcY + offset + size_read; ++ii) {
                for (var jj = srcX + offset; jj < srcX + offset + size_read; ++jj) {
                    var loc = ii * size_draw + jj;
                    var color = [pixels[loc * 4], pixels[loc * 4 + 1], pixels[loc * 4 + 2], pixels[loc * 4 + 3]];
                    var expectedColor = ref[loc];
                    // We may need to covert the color space for pixels in blit region
                    if ((readbufferHasSRGBImage ^ drawbufferHasSRGBImage) &&
                        (ii >= realBlittedDstStart && ii < realBlittedDstEnd && jj >= realBlittedDstStart && jj < realBlittedDstEnd)) {
                        if (drawbufferHasSRGBImage) {
                            expectedColor = wtu.linearToSRGB(expectedColor);
                        } else {
                            expectedColor = wtu.sRGBToLinear(expectedColor);
                        }
                    }
                    if (checkPixel(color, expectedColor) == true) {
                        testPassed("pixel at [" + jj + ", " + ii + "] is (" + color + "). It is correct!");
                    } else {
                        testFailed("pixel at [" + jj + ", " + ii + "] should be (" + expectedColor + "), but the actual color is (" + color + ")");
                    }
                }
            }
        }
    } else {
        testFailed("framebuffer not complete");
    }

    gl.bindTexture(gl.TEXTURE_2D, null);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, null);
    gl.deleteFramebuffer(fbo_read);
    gl.deleteFramebuffer(fbo_draw);
    gl.deleteTexture(tex_read);
    gl.deleteTexture(tex_draw);
};

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");
    blitframebuffer_outside_readbuffer(gl.RGBA8, gl.RGBA8);
    blitframebuffer_outside_readbuffer(gl.RGBA8, gl.SRGB8_ALPHA8);
    blitframebuffer_outside_readbuffer(gl.SRGB8_ALPHA8, gl.RGBA8);
    blitframebuffer_outside_readbuffer(gl.SRGB8_ALPHA8, gl.SRGB8_ALPHA8);
}

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

</body>
</html>