summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/textures/misc/copy-texture-image-webgl-specific.html
blob: ae969e0795fafe86f0caa06ddb6fc43276eb19a6 (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
<!--
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 CopyTexImage 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="2" height="2"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";

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

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

function copytexsubimage3d_invalid_operation_feedbackloops() {
    debug("");
    debug("Testing copytexsubimage3d_invalid_operation_feedbackloops");
    var texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_3D, texture);
    var uint8 = new Uint8Array(32);
    var layer = 0;
    var width = 2;
    var height = 2;
    var depth = 2;
    gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA, width, height, depth, 0, gl.RGBA, gl.UNSIGNED_BYTE, uint8);

    var fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, texture, 0, layer);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
        gl.copyTexSubImage3D(gl.TEXTURE_3D, 0, 0, 0, layer, 0, 0, width, height);
        wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "gl.INVALID_OPERATION is generated");
    } else {
        testFailed("framebuffer not complete");
    }

    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    gl.deleteFramebuffer(fbo);
    gl.deleteTexture(texture);
};

function copytexsubimage3d_valid_operation_diff_level() {
    debug("");
    debug("Testing copytexsubimage3d_valid_operation_diff_level");
    var texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_3D, texture);
    var uint8 = new Uint8Array(32);
    var level1 = 0;
    var level2 = 1;
    var width = 2;
    var height = 2;
    var depth = 2;
    gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA, width, height, depth, 0, gl.RGBA, gl.UNSIGNED_BYTE, uint8);
    gl.generateMipmap(gl.TEXTURE_3D);

    var fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, texture, level1, 0);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
        gl.copyTexSubImage3D(gl.TEXTURE_3D, level2, 0, 0, 0, 0, 0, width/2, height/2);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texImage3D should succeed.");
    } else {
        testFailed("framebuffer not complete");
    }

    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    gl.deleteFramebuffer(fbo);
    gl.deleteTexture(texture);
};

function copytexsubimage3d_valid_operation_diff_layer() {
    debug("");
    debug("Testing copytexsubimage3d_valid_operation_diff_layer");
    var texture = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_3D, texture);
    var uint8 = new Uint8Array(32);
    var layer1 = 0;
    var layer2 = 1;
    var width = 2;
    var height = 2;
    var depth = 2;
    gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA, width, height, depth, 0, gl.RGBA, gl.UNSIGNED_BYTE, uint8);

    var fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, texture, 0, layer1);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
        gl.copyTexSubImage3D(gl.TEXTURE_3D, 0, 0, 0, layer2, 0, 0, width, height);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texImage3D should succeed.");
    } else {
        testFailed("framebuffer not complete");
    }

    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    gl.deleteFramebuffer(fbo);
    gl.deleteTexture(texture);
}

function copytexsubimage3d_texture_wrongly_initialized() {
    debug("");
    debug("Testing copytexsubimage3d_texture_wrongly_initialized");
    var texture = [];
    texture[0] = gl.createTexture();
    texture[1] = gl.createTexture();
    var layer = 0;
    var width = 2;
    var height = 2;
    var depth = 2;
    gl.bindTexture(gl.TEXTURE_2D, texture[0]);
    var uint = new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, uint);

    var fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture[0], 0);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
        gl.bindTexture(gl.TEXTURE_3D, texture[1]);
        gl.texStorage3D(gl.TEXTURE_3D, 1, gl.RGBA8, width, height, depth);
        gl.copyTexSubImage3D(gl.TEXTURE_3D, 0, 0, 0, layer, 0, 0, width, height);
        gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, texture[1], 0, layer);
        var bytes = new Uint8Array(width * height * 4);
        gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, bytes);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "readpixel should succeed.");
        for (var x = 0; x < width * height * 4; x++) {
            if (bytes[x] != uint[x]) {
                testFailed("byte comparison failure, byte at "+ x + " was " + bytes[x] +
                ", should be " + uint[x]);
                break;
            }
        }
    } else {
        testFailed("framebuffer not complete");
    }

    gl.bindTexture(gl.TEXTURE_2D, null);
    gl.bindTexture(gl.TEXTURE_3D, null);
    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    gl.deleteFramebuffer(fbo);
    gl.deleteTexture(texture[0]);
    gl.deleteTexture(texture[1]);
};

function copytexsubimage3d_out_of_bounds_test_helper(xx, yy, copyWidth, copyHeight) {
    var texture = [];
    texture[0] = gl.createTexture();
    texture[1] = gl.createTexture();
    var layer = 0;
    var width = 2;
    var height = 2;
    var depth = 2;
    var width2 = 4;
    var height2 = 4;
    var xoffset = 0;
    var yoffset = 0;
    var uint = new Uint8Array(width * height * 4);
    for (var i = 0; i < uint.length; i++) {
        uint[i] = 0x01;
    }
    var uint2 = new Uint8Array(width2 * height2 * depth * 4);
    for (var i = 0; i < uint2.length; i++) {
        uint2[i] = 0xFF;
    }

    var fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);

    gl.bindTexture(gl.TEXTURE_2D, texture[0]);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, uint);
    gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture[0], 0);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) == gl.FRAMEBUFFER_COMPLETE) {
        gl.bindTexture(gl.TEXTURE_3D, texture[1]);
        gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA, width2, height2, depth, 0, gl.RGBA, gl.UNSIGNED_BYTE, uint2);
        gl.copyTexSubImage3D(gl.TEXTURE_3D, 0, xoffset, yoffset, layer, xx, yy, copyWidth, copyHeight);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "using copyTexSubImage3D: x = " + xx + ", y = " + yy + " width = " + copyWidth + ", height = " + copyHeight);

        gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, texture[1], 0, layer);
        var bytes = new Uint8Array(width2 * height2 * 4);
        gl.readPixels(0, 0, width2, height2, gl.RGBA, gl.UNSIGNED_BYTE, bytes);
        wtu.glErrorShouldBe(gl, gl.NO_ERROR, "readpixel should succeed.");

        var sourceX = new Object();
        var sourceY = new Object();
        Clip(xx, copyWidth, width, sourceX);
        Clip(yy, copyHeight, height, sourceY);
        var destX = sourceX.start - xx + xoffset;
        var rangeX = sourceX.range;
        var destY = sourceY.start - yy + yoffset;
        var rangeY = sourceY.range;
        for (var y = 0; y < height2; y++) {
            for (var x = 0; x < width2 * 4; x++) {
                var current = y * height2 * 4 + x;
                // pixels copied from read framebuffer should be 0x01
                if (x >= destX * 4 && x < (destX + rangeX) * 4 && y >= destY && y < destY + rangeY) {
                    if (bytes[current] != 0x01) {
                        testFailed("byte comparison failure, byte at "+ (current) + " was " +
                        bytes[current] +", should be 1");
                        break;
                     }
                // pixels out-of-bounds should be untouched
                } else {
                    if (bytes[current] != 0xFF) {
                        testFailed("byte comparison failure, byte at "+ (current) + " was " +
                        bytes[current] + ", should be 255");
                        break;
                    }
                }
            }
            // Test failed; abort
            if (x < width2 * 4) {
                break;
            }
        }
    } else {
        testFailed("framebuffer not complete");
    }

    gl.bindTexture(gl.TEXTURE_2D, null);
    gl.bindTexture(gl.TEXTURE_3D, null);
    gl.bindFramebuffer(gl.FRAMEBUFFER, null);
    gl.deleteFramebuffer(fbo);
    gl.deleteTexture(texture[0]);
    gl.deleteTexture(texture[1]);
}

function copytexsubimage3d_out_of_bounds() {
    debug("");
    debug("Test pixels outside of read framebuffer for CopyTexSubImage3D");

    for(var i=0; i < testlist.length; i++) {
        copytexsubimage3d_out_of_bounds_test_helper(testlist[i][0], testlist[i][1], testlist[i][2], testlist[i][3]);
    }
};

/**
 * This array defines some copy areas for CopyTexSubImage3D.
 * A copy area is defined by x coordinate, y coordinate, copyWidth and copyHeight.
 * the source read framebuffer is (0, 0, 2, 2).
 */
var testlist = [
    [-1, -1, 4, 4],

    [0, 0, 3, 3],
    [-1, -1, 3, 3],
    [-1, 0, 3, 3],
    [0, -1, 3, 3],

    [0, 0, 2, 3],
    [0, 0, 3, 2],
    [-1, 0, 3, 2],
    [0, -1, 2, 3],

    [-2, -2, 3, 3],
    [-2, 1, 3, 3],
    [1, -2, 3, 3],
    [1, 1, 3, 3],

    [2 , 2 ,3, 3]
];


function Clip(start, range, sourceRange, target) {
    if (start < 0) {
        range += start;
        start = 0;
    }
    var end = start + range;
    if(end > sourceRange) {
        range -= end - sourceRange;
    }
    target.start = start;
    target.range = range;
}

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");
    copytexsubimage3d_invalid_operation_feedbackloops();
    copytexsubimage3d_valid_operation_diff_level();
    copytexsubimage3d_valid_operation_diff_layer();
    copytexsubimage3d_texture_wrongly_initialized();
    copytexsubimage3d_out_of_bounds();
}

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

</body>
</html>