summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/rendering/read-draw-when-missing-image.html
blob: a9d8c74b9d4d1d922329f9835249fe8d5f891dd3 (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
<!--
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>Read or Draw when Attachment(s) Miss Image</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 reading/drawing when color attachment(s) miss image.");

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

var tex_read = gl.createTexture();
var tex_draw = gl.createTexture();
var tex_depth = gl.createTexture();
var tex_stencil = gl.createTexture();
var fbo_read = gl.createFramebuffer();
var fbo_draw = gl.createFramebuffer();
var size = 8;

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

    // READ_FRAMEBUFFER has image at COLOR_ATTACHMENT0. READ_FRAMEBUFFER is framebuffer complete.
    // Read from COLOR_ATTACHMENT1, which has no image attached.
    init_read_fbo();
    read();

    // DRAW_FRAMEBUFFER has image at COLOR_ATTACHMENT0. DRAW_FRAMEBUFFER is framebuffer complete.
    // Clear and draw COLOR_ATTACHMENT1 or COLOR_ATTACHMENT0 + COLOR_ATTACHMENT1 attaching point(s).
    init_draw_fbo();
    clear();
    draw();

    blit();
}

function init_read_fbo() {
    gl.bindTexture(gl.TEXTURE_2D, tex_read);
    wtu.fillTexture(gl, tex_read, size, size, [0x0, 0xff, 0xff, 0xff], 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.RGBA8);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.framebufferTexture2D(gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex_read, 0);
    if (gl.checkFramebufferStatus(gl.READ_FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed("Framebuffer incomplete.");
        return;
    } else {
        testPassed("framebuffer complete!");
    }
}

function init_draw_fbo() {
    gl.bindTexture(gl.TEXTURE_2D, tex_draw);
    wtu.fillTexture(gl, tex_draw, size, size, [0x0, 0xff, 0xff, 0xff], 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.RGBA8);
    wtu.fillTexture(gl, tex_depth, size, size, [0x80], 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, gl.DEPTH_COMPONENT16);
    wtu.fillTexture(gl, tex_stencil, size, size, [0x40], 0, gl.DEPTH_STENCIL, gl.UNSIGNED_INT_24_8, gl.DEPTH24_STENCIL8);
    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.DRAW_FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed("Framebuffer incomplete.");
        return;
    } else {
        testPassed("framebuffer complete!");
    }
}

function read() {
    debug("");
    debug("read from a color buffer which has no image attached");

    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo_read);
    gl.readBuffer(gl.COLOR_ATTACHMENT1);

    var data = new Uint8Array(size * size * 4);
    gl.readPixels(0, 0, size, size, gl.RGBA, gl.UNSIGNED_BYTE, data);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should generate INVALID_OPERATION when reading from a color buffer without image.");

    var copy_2d = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, copy_2d);
    gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 0, 0, size, size, 0);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should generate INVALID_OPERATION when reading from a color buffer without image.");

    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, size / 2, size / 2, 0, 0, size / 2, size / 2);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should generate INVALID_OPERATION when reading from a color buffer without image.");

    var copy_3d = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_3D, copy_3d);
    gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA8, size, size, 2, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
    gl.copyTexSubImage3D(gl.TEXTURE_3D, 0, size / 2, size / 2, 0, 0, 0, size / 2, size / 2);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should generate INVALID_OPERATION when reading from a color buffer without image.");

    gl.bindTexture(gl.TEXTURE_2D, null);
    gl.deleteTexture(copy_2d);
    gl.bindTexture(gl.TEXTURE_3D, null);
    gl.deleteTexture(copy_3d);
}

function checkTextureValue(fbo, buffer, value) {
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo);
    gl.readBuffer(buffer);
    wtu.checkCanvas(gl, value);
    gl.readBuffer(gl.COLOR_ATTACHMENT0);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
}

function clear() {
    debug("");
    debug("clear a color buffer which has no image attached");

    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    var color = [0.0, 1.0, 0.0, 1.0];
    gl.clearColor(color[0], color[1], color[2], color[3]);
    gl.drawBuffers([gl.NONE, gl.COLOR_ATTACHMENT1]);
    gl.clear(gl.COLOR_BUFFER_BIT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 should not be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [0, 255, 255, 255]);

    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.drawBuffers([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1]);
    gl.clear(gl.COLOR_BUFFER_BIT);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 should be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [0, 255, 0, 255]);

    var data = new Float32Array(color);
    gl.clearBufferfv(gl.COLOR, 1, data);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
}

function draw() {
    debug("");
    debug("draw to a color buffer which has no image attached");

    var program = wtu.setupSimpleColorProgram(gl, 0);
    gl.uniform4f(gl.getUniformLocation(program, "u_color"), 1, 0, 0, 1);
    wtu.setupUnitQuad(gl, 0);

    // Call to drawArrays
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.drawBuffers([gl.NONE, gl.COLOR_ATTACHMENT1]);
    wtu.drawUnitQuad(gl);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 should not be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [0, 255, 0, 255]);

    gl.drawBuffers([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1]);
    wtu.drawUnitQuad(gl);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 should be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [255, 0, 0, 255]);

    // Call to drawElements
    gl.uniform4f(gl.getUniformLocation(program, "u_color"), 1, 1, 0, 1);
    wtu.setupIndexedQuad(gl, 1);
    gl.drawBuffers([gl.NONE, gl.COLOR_ATTACHMENT1]);
    wtu.drawIndexedQuad(gl, 1);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 should not be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [255, 0, 0, 255]);

    gl.drawBuffers([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1]);
    wtu.drawIndexedQuad(gl, 1);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 should be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [255, 255, 0, 255]);
}

function blit() {
    debug("");
    debug("blit color buffer(s) which have no image attached");
    // Some or all draw buffers have no image. Read buffer have image. It should be OK.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.readBuffer(gl.COLOR_ATTACHMENT0);
    gl.drawBuffers([gl.NONE, gl.COLOR_ATTACHMENT1]);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.COLOR_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 in draw fbo should not be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [255, 255, 0, 255]);

    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.readBuffer(gl.COLOR_ATTACHMENT0);
    gl.drawBuffers([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1]);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.COLOR_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 in draw fbo should be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [0, 255, 255, 255]);

    // Draw buffer(s) have no image. Read buffer have no image. It should be OK.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.readBuffer(gl.COLOR_ATTACHMENT1);
    gl.drawBuffers([gl.NONE, gl.COLOR_ATTACHMENT1]);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.COLOR_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // The image color at COLOR_ATTACHMENT0 in draw fbo should not be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [0, 255, 255, 255]);

    // Read buffer have no image. Some or all draw buffers have image. It should generate INVALID_OPERATION.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.readBuffer(gl.COLOR_ATTACHMENT1);
    gl.drawBuffers([gl.COLOR_ATTACHMENT0]);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.COLOR_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should generate INVALID_OPERATION when read buffer misses image.");
    // The image color at COLOR_ATTACHMENT0 in draw fbo should not be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [0, 255, 255, 255]);

    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.readBuffer(gl.COLOR_ATTACHMENT1);
    gl.drawBuffers([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1]);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.COLOR_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should generate INVALID_OPERATION when read buffer misses image.");
    // The image color at COLOR_ATTACHMENT0 in draw fbo should not be changed.
    checkTextureValue(fbo_draw, gl.COLOR_ATTACHMENT0, [0, 255, 255, 255]);

    // Depth buffer in read fbo has no image. It should generate INVALID_OPERATION if depth buffer in draw fbo has image.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, tex_depth, 0);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.DEPTH_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should generate INVALID_OPERATION when depth buffer misses image.");

    // Depth buffer in read fbo has no image. It should be OK if depth buffer in draw fbo has no image too.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, null, 0);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.DEPTH_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // Validate some other parameters as usual
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.DEPTH_BUFFER_BIT, gl.LINEAR);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid filter");

    // Stencil buffer in read fbo has no image. It should generate INVALID_OPERATION if stencil buffer in draw fbo has image.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.TEXTURE_2D, tex_stencil, 0);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.STENCIL_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "Should generate INVALID_OPERATION when stencil buffer misses image.");

    // Stencil buffer in read fbo has no image. It should be OK if stencil buffer in draw fbo has no image too.
    gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, fbo_draw);
    gl.bindFramebuffer(gl.READ_FRAMEBUFFER, fbo_read);
    gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.TEXTURE_2D, null, 0);
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.STENCIL_BUFFER_BIT, gl.NEAREST);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no error.");
    // Validate some other parameters as usual
    gl.blitFramebuffer(0, 0, size, size, 0, 0, size, size, gl.STENCIL_BUFFER_BIT, gl.LINEAR);
    wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "invalid filter");
}

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

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

</body>
</html>