summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/reading/read-pixels-into-pixel-pack-buffer.html
blob: 7d646a6ecdf20549657bdf96fd98f1558e6fbab4 (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
<!--
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 2 Conformance Test: ReadPixels Into Pixel Pack Buffer.</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="4" height="4"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";

function checkFormatAndType()
{
    debug("");
    debug("check format / type");
    var invalidFormat = [gl.DEPTH_COMPONENT, gl.DEPTH_STENCIL, gl.R8, gl.RGBA4, gl.LUMINANCE, gl.LUMINANCE_ALPHA];
    var invalidType = [gl.UNSIGNED_INT_24_8];
    for (var ff = 0; ff < invalidFormat.length; ++ff) {
        var format = invalidFormat[ff];
        gl.readPixels(0, 0, 1, 1, format, gl.UNSIGNED_BYTE, 0);
        wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "Format should not be able to read as " + wtu.glEnumToString(gl, format));
    }
    for (var tt = 0; tt < invalidType.length; ++tt) {
        var type = invalidType[tt];
        gl.readPixels(0, 0, 1, 1, gl.RGBA, type, 0);
        wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "Type should not be able to read as " + wtu.glEnumToString(gl, type));
    }

    debug("");
    debug("check combinations of format and type");
    var combinations = [
        {format: gl.RGBA,         type: gl.UNSIGNED_BYTE},
        {format: gl.RGB,          type: gl.UNSIGNED_BYTE},
        {format: gl.RGB,          type: gl.UNSIGNED_SHORT_5_6_5},
        {format: gl.RGBA,         type: gl.UNSIGNED_SHORT_5_5_5_1},
        {format: gl.RGBA,         type: gl.UNSIGNED_SHORT_4_4_4_4},
        {format: gl.ALPHA,        type: gl.UNSIGNED_BYTE},
        {format: gl.RED,          type: gl.UNSIGNED_BYTE},
        {format: gl.RGBA_INTEGER, type: gl.UNSIGNED_INT},
        {format: gl.RGBA_INTEGER, type: gl.INT}
    ];

    var implFormat = gl.getParameter(gl.IMPLEMENTATION_COLOR_READ_FORMAT);
    var implType = gl.getParameter(gl.IMPLEMENTATION_COLOR_READ_TYPE);
    for (var tt = 0; tt < combinations.length; ++ tt) {
        var info = combinations[tt];
        var format = info.format;
        var type = info.type;
        gl.readPixels(0, 0, 1, 1, format, type, 0);
        // Only two format/type parameter pairs are accepted. GL_RGBA/GL_UNSIGNED_BYTE is always
        // accepted on default readbuffer. The other acceptable pair can be discovered by querying
        // GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE.
        if ((format == gl.RGBA && type == gl.UNSIGNED_BYTE) || (format == implFormat && type == implType)) {
            wtu.glErrorShouldBe(gl, gl.NO_ERROR, "The combination of format/type should be able to read as " +
                                wtu.glEnumToString(gl, format) + " / " + wtu.glEnumToString(gl, type));
        } else {
            wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "The combination of format/type should not be able to read as " +
                                wtu.glEnumToString(gl, format) + " / " + wtu.glEnumToString(gl, type));
        }
    }
}

function validatePixelPackBufferAndParameters(canvasWidth, canvasHeight)
{
    debug("");
    debug("Validate PIXEL_PACK buffer and readPixels' parameters");
    gl.clearColor(0, 0, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.pixelStorei(gl.PACK_ALIGNMENT, 1);

    var size = canvasWidth * canvasHeight * 4;
    var buffer = gl.createBuffer();
    gl.bindBuffer(gl.PIXEL_PACK_BUFFER, buffer);
    gl.bufferData(gl.PIXEL_PACK_BUFFER, size, gl.STATIC_DRAW);
    var array = new Uint8Array(size);

    debug("");
    debug("PIXEL_PACK buffer is bound");
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, array);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "should generate INVALID_OPERATION if pixel pack buffer is bound");
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);

    debug("");
    debug("Validate the offset of PIXEL_PACK buffer and buffer size");
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, -1);
    wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "offset < 0");
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, size);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "offset > buffer size");
    gl.readPixels(0, 0, canvasWidth + 1, canvasHeight, gl.RGBA, gl.UNSIGNED_BYTE, 0);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "pixel pack buffer is not large enough");

    debug("");
    debug("Validate the reading area of framebuffer");
    gl.readPixels(-1, -2, canvasWidth, canvasHeight, gl.RGBA, gl.UNSIGNED_BYTE, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "reading pixels outside of the framebuffer should succeed.");
    gl.readPixels(2, 1, canvasWidth, canvasHeight, gl.RGBA, gl.UNSIGNED_BYTE, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "reading pixels outside of the framebuffer should succeed.");
    gl.readPixels(2, 1, -1, -1, gl.RGBA, gl.UNSIGNED_BYTE, 0);
    wtu.glErrorShouldBe(gl, gl.INVALID_VALUE,
                        "reading pixels with negative width / height should generate INVALID_VALUE.");

    checkFormatAndType();

    debug("");
    debug("no PIXEL_PACK buffer bound");
    gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null);
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, array);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR);
    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, 0);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "no pixel pack buffer bound");

    gl.deleteBuffer(buffer);
}

debug("");
debug("Canvas.getContext");

var wtu = WebGLTestUtils;
var pixel = [0, 0, 0, 0];
var expectedColor = [255, 102, 0, 255];

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

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

  debug("");
  description('ReadPixels into PIXEL_PACK buffer');
  validatePixelPackBufferAndParameters(4, 4);
}

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