summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/rendering/blitframebuffer-scissor-enabled.html
blob: d0e2dfaefa5fb968d62ae2fe4eaad0895ec1eb4f (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
<!--
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 when scissor test is enabled.");

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

// Define the src region and dst region for blitFramebuffer
var blit_src = [0, 0, 4, 4];
var blit_dst = [2, 2, 6, 6];

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

    var bounds = [
        [0, 0, 4, 4], // Partially intersects with blitFramebuffer's dst region
        [0, 0, 2, 2], // No intersection with blitFramebuffer's dst region
    ];

    // We can compute the real drawing area by intersecting the scissor bound with dst region of blitting.
    var intersections = [
        [2, 2, 4, 4],
        [0, 0, 0, 0],
    ];

    for (var ii = 0; ii < bounds.length; ++ii) {
        blitframebuffer_scissor(gl.RGBA8, gl.RGBA8, bounds[ii], intersections[ii]);
        blitframebuffer_scissor(gl.RGBA8, gl.SRGB8_ALPHA8, bounds[ii], intersections[ii]);
        blitframebuffer_scissor(gl.SRGB8_ALPHA8, gl.RGBA8, bounds[ii], intersections[ii]);
        blitframebuffer_scissor(gl.SRGB8_ALPHA8, gl.SRGB8_ALPHA8, bounds[ii], intersections[ii]);
    }
}

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_scissor(readbufferFormat, drawbufferFormat, bound, intersection) {
    debug("");
    debug("read buffer format is: " + wtu.glEnumToString(gl, readbufferFormat) + ", draw buffer format is: " + wtu.glEnumToString(gl, drawbufferFormat));


    // Initiate data to read framebuffer
    var size = 8;
    var data = new Uint8Array(size * size * 4);
    var color = [250, 100, 15, 255];
    for (var ii = 0; ii < size * size * 4; ii += 4) {
        for (var jj = 0; jj < 4; ++jj) {
          data[ii + jj] = color[jj];
        }
    }

    // Feed data to read buffer. Feed 0 to draw buffer.
    var tex_read = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex_read);
    gl.texImage2D(gl.TEXTURE_2D, 0, readbufferFormat, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);

    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);

    var tex_draw = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex_draw);
    gl.texImage2D(gl.TEXTURE_2D, 0, drawbufferFormat, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

    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 || gl.checkFramebufferStatus(gl.DRAW_FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed("Framebuffer incomplete.");
        return;
    }

    // Enable scissor test. Then blit framebuffer.
    gl.enable(gl.SCISSOR_TEST);
    gl.scissor(bound[0], bound[1], bound[2], bound[3]);
    gl.blitFramebuffer(blit_src[0], blit_src[1], blit_src[2], blit_src[3], blit_dst[0], blit_dst[1], blit_dst[2], blit_dst[3], gl.COLOR_BUFFER_BIT, gl.LINEAR);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "blitframebuffer should succeed");

    // Read pixels and Comparison
    var pixels = new Uint8Array(size * size * 4);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_draw);
    gl.readPixels(0, 0, size, size, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "readPixels should succeed");

    var blitColor;
    var expectedColor;
    var clearColor = [0, 0, 0, 0];

    if (readbufferFormat == drawbufferFormat) {
        blitColor = color;
    } else if (readbufferFormat == gl.SRGB8_ALPHA8) {
        blitColor = wtu.sRGBToLinear(color);
    } else {
        blitColor = wtu.linearToSRGB(color);
    }

    var failed = false;
    for (var ii = 0; ii < size; ++ii) {
        for (var jj = 0; jj < size; ++jj) {
            if (ii >= intersection[0] && jj >= intersection[1] && ii < intersection[2] && jj < intersection[3]) {
                expectedColor = blitColor;
            } else {
                expectedColor = clearColor;
            }
            var index = (ii * size + jj) * 4;
            var pixelColor = [pixels[index], pixels[index + 1], pixels[index + 2], pixels[index + 3]];
            if (checkPixel(pixelColor, expectedColor) == false) {
                failed = true;
                debug("Pixels comparison failed. Pixel at [" + jj + ", " + ii + "] should be (" + expectedColor + "), but the actual color is (" + pixelColor + ")");
            }
        }
    }
    if (failed == false) {
        testPassed("All pixels comparision passed!");
    }

    // Deinit
    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);
};

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

</body>
</html>