2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <title>Test readBuffer Against WebGL
2</title>
12 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
13 <script src=
"../../js/js-test-pre.js"></script>
14 <script src=
"../../js/webgl-test-utils.js"></script>
17 <div id=
"description"></div>
18 <div id=
"console"></div>
19 <canvas id=
"canvas" width=
"20" height=
"20"> </canvas>
22 description("This tests reading from fbo");
24 var clearDrawingbuffer = function(color
) {
25 gl
.clearColor(color
[0] / 255, color
[1] / 255, color
[2] / 255, color
[3] / 255);
26 gl
.clear(gl
.COLOR_BUFFER_BIT
);
29 var validateReadingFromFramebuffer = function(color
, expected
, msg
) {
30 var pixels
= new Uint8Array(1 * 1 * 4);
31 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixels
);
32 wtu
.glErrorShouldBe(gl
, expected
, msg
);
33 if (expected
== gl
.NO_ERROR
)
34 wtu
.checkCanvasRect(gl
, 0, 0, canvas
.width
, canvas
.height
, color
,
35 "the color should be [" + color
+ "]");
38 var setupRenderbuffer = function(attachment
) {
39 var renderbuffer
= gl
.createRenderbuffer();
40 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, renderbuffer
);
41 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, attachment
, gl
.RENDERBUFFER
, renderbuffer
);
42 gl
.renderbufferStorage(gl
.RENDERBUFFER
, gl
.RGBA8
, canvas
.width
, canvas
.height
);
46 var testReadBufferOnDefaultFB = function() {
47 gl
.readBuffer(gl
.NONE
);
48 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
49 "calling readBuffer with GL_NONE on the default framebuffer should succeed.");
50 var pixels
= new Uint8Array(1 * 1 * 4);
51 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixels
);
52 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
,
53 "should generate INVALID_OPERATION when reading from framebuffer and read buffer is GL_NONE.");
54 gl
.readBuffer(gl
.BACK
);
55 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
56 "calling readBuffer with GL_BACK on the default framebuffer should succeed.");
58 gl
.readBuffer(gl
.COLOR_ATTACHMENT0
);
59 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
,
60 "calling readBuffer with GL_COLOR_ATTACHMENT0 on the default framebuffer should generate INVALID_OPERATION.");
63 var testReadBufferOnFBO = function() {
64 gl
.readBuffer(gl
.BACK
);
65 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
,
66 "calling readBuffer with GL_BACK on fbo should generate INVALID_OPERATION.");
68 gl
.readBuffer(gl
.NONE
);
69 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
70 "calling readBuffer with GL_NONE on fbo should succeed.");
71 var pixels
= new Uint8Array(1 * 1 * 4);
72 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixels
);
73 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
,
74 "should generate INVALID_OPERATION when reading from framebuffer and read buffer is GL_NONE.");
75 gl
.readBuffer(gl
.COLOR_ATTACHMENT0
);
76 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
77 "calling readBuffer with GL_COLOR_ATTACHMENT0 on fbo should succeed.");
79 var maxColorAttachments
= gl
.getParameter(gl
.MAX_COLOR_ATTACHMENTS
);
80 gl
.readBuffer(gl
.COLOR_ATTACHMENT0
+ maxColorAttachments
);
81 wtu
.glErrorShouldBe(gl
, gl
.INVALID_OPERATION
,
82 "calling readBuffer with GL_COLOR_ATTACHMENTi that exceeds MAX_COLOR_ATTACHMENT on fbo should generate INVALID_OPERATION.");
83 gl
.readBuffer(gl
.COLOR_ATTACHMENT1
);
84 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
85 "calling readBuffer with GL_COLOR_ATTACHMENT1 on the fbo should succeed.");
86 shouldBe('gl.getParameter(gl.READ_BUFFER)', 'gl.COLOR_ATTACHMENT1');
90 debug("Canvas.getContext");
92 var wtu
= WebGLTestUtils
;
93 var canvas
= document
.getElementById("canvas");
94 var gl
= wtu
.create3DContext(canvas
, undefined, 2);
96 testFailed("context does not exist");
98 testPassed("context exists");
101 debug("Checking reading from framebuffer.");
103 // Test on the default framebuffer. Read buffer is GL_BACK by default.
104 var backColor
= [0, 0, 0, 255];
105 clearDrawingbuffer(backColor
);
106 validateReadingFromFramebuffer(backColor
, gl
.NO_ERROR
,
107 "should be no errors when reading from GL_BACK on the default framebuffer.");
109 shouldBe('gl.getParameter(gl.READ_BUFFER)', 'gl.BACK');
110 testReadBufferOnDefaultFB();
112 // Test on fbo. Read buffer is GL_COLOR_ATTACHMENT0 by default
113 var fb
= gl
.createFramebuffer();
114 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fb
);
115 var colorbuffer
= setupRenderbuffer(gl
.COLOR_ATTACHMENT0
);
116 var red
= [255, 0, 0, 255];
117 clearDrawingbuffer(red
);
118 validateReadingFromFramebuffer(red
, gl
.NO_ERROR
,
119 "should be no errors when reading from GL_COLOR_ATTACHMENT0 on fbo.");
121 shouldBe('gl.getParameter(gl.READ_BUFFER)', 'gl.COLOR_ATTACHMENT0');
122 testReadBufferOnFBO();
124 // Test on user defined read buffer (GL_COLOR_ATTACHMENT1) with or without corresponding image on fbo.
125 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fb
);
126 var colorbuffer1
= setupRenderbuffer(gl
.COLOR_ATTACHMENT1
);
127 var green
= [0, 255, 0, 255];
128 gl
.drawBuffers([gl
.NONE
, gl
.COLOR_ATTACHMENT1
]);
129 clearDrawingbuffer(green
);
130 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no error after setup and clear render buffer");
131 gl
.readBuffer(gl
.COLOR_ATTACHMENT1
);
132 validateReadingFromFramebuffer(green
, gl
.NO_ERROR
,
133 "should be no errors when reading from GL_COLOR_ATTACHMENT1 on fbo.");
134 shouldBe('gl.getParameter(gl.READ_BUFFER)', 'gl.COLOR_ATTACHMENT1');
135 // Need to reset draw buffers, otherwise it triggers a mac driver bug.
136 // We add a separate test for that bug: conformance2/rendering/framebuffer-completeness-unaffected.html
137 gl
.drawBuffers([gl
.COLOR_ATTACHMENT0
]);
139 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT1
, gl
.RENDERBUFFER
, null)
140 gl
.readBuffer(gl
.COLOR_ATTACHMENT1
);
141 validateReadingFromFramebuffer(null, gl
.INVALID_OPERATION
,
142 "should generate INVALID_OPERATION when reading from GL_COLOR_ATTACHMENT1 but this attachment has no image currently.");
144 // switch to another fbo, read buffer is GL_COLOR_ATTACHMENT0, not GL_COLOR_ATTACHMENT1
145 var fb1
= gl
.createFramebuffer();
146 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fb1
);
147 var buffer
= setupRenderbuffer(gl
.COLOR_ATTACHMENT0
);
148 shouldBe('gl.getParameter(gl.READ_BUFFER)', 'gl.COLOR_ATTACHMENT0');
149 var blue
= [0, 0, 255, 255];
150 clearDrawingbuffer(blue
);
151 validateReadingFromFramebuffer(blue
, gl
.NO_ERROR
,
152 "should be no errors when reading from GL_COLOR_ATTACHMENT0 on another fbo.");
154 // switch from fbo to default fb, read buffer will switch to GL_BACK from GL_COLOR_ATTACHMENT0
155 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, null);
156 shouldBe('gl.getParameter(gl.READ_BUFFER)', 'gl.BACK');
157 validateReadingFromFramebuffer(backColor
, gl
.NO_ERROR
,
158 "should be no errors when reading from GL_BACK on the default framebuffer.");
160 gl
.deleteFramebuffer(fb
);
161 gl
.deleteRenderbuffer(colorbuffer
);
162 gl
.deleteRenderbuffer(colorbuffer1
);
163 gl
.deleteFramebuffer(fb1
);
164 gl
.deleteRenderbuffer(buffer
);
168 var successfullyParsed
= true;
171 <script src=
"../../js/js-test-post.js"></script>