4 ** Copyright (c) 2013 The Khronos Group Inc.
6 ** Permission is hereby granted, free of charge, to any person obtaining a
7 ** copy of this software and/or associated documentation files (the
8 ** "Materials"), to deal in the Materials without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Materials, and to
11 ** permit persons to whom the Materials are furnished to do so, subject to
12 ** the following conditions:
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
31 <meta charset="utf-8">
32 <title>WebGL WEBGL_draw_buffers Conformance Tests</title>
33 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
34 <script src="../../resources/js-test-pre.js"></script>
35 <script src="../resources/webgl-test-utils.js"></script>
38 <div id="description"></div>
39 <canvas id="canvas" width="64" height="64"> </canvas>
40 <div id="console"></div>
41 <script id="vshader" type="x-shader/x-vertex">
42 attribute vec4 a_position;
44 gl_Position = a_position;
47 <script id="fshader" type="x-shader/x-fragment">
48 #extension GL_EXT_draw_buffers : require
49 precision mediump float;
50 uniform vec4 u_colors[$(numDrawingBuffers)];
52 for (int i = 0; i < $(numDrawingBuffers); ++i) {
53 gl_FragData[i] = u_colors[i];
57 <script id="fshaderRed" type="x-shader/x-fragment">
58 precision mediump float;
60 gl_FragColor = vec4(1,0,0,1);
63 <script id="fshaderMacroDisabled" type="x-shader/x-fragment">
64 #ifdef GL_EXT_draw_buffers
67 precision mediump float;
69 gl_FragColor = vec4(0,0,0,0);
72 <script id="fshaderMacroEnabled" type="x-shader/x-fragment">
73 #ifdef GL_EXT_draw_buffers
74 #if GL_EXT_draw_buffers == 1
77 #define CODE this_code_is_bad_it_should_have_compiled
80 #define CODE this_code_is_bad_it_should_have_compiled
83 precision mediump float;
85 gl_FragColor = vec4(0,0,0,0);
88 <script id="fshaderBuiltInConstEnabled" type="x-shader/x-fragment">
89 precision mediump float;
91 gl_FragColor = (gl_MaxDrawBuffers == $(numDrawingBuffers)) ? vec4(0,1,0,1) : vec4(1,0,0,1);
96 description("This test verifies the functionality of the WEBGL_draw_buffers extension, if it is available.");
100 var wtu = WebGLTestUtils;
101 var canvas = document.getElementById("canvas");
102 var output = document.getElementById("console");
103 var gl = wtu.create3DContext(canvas);
107 var extensionConstants = [
108 { name: "MAX_COLOR_ATTACHMENTS_WEBGL", enum: 0x8CDF, expectedFn: function(v) { return v > 0; }, passMsg: " should be > 0"},
109 { name: "MAX_DRAW_BUFFERS_WEBGL", enum: 0x8824, expectedFn: function(v) { return v > 0; }, passMsg: " should be > 0"},
111 { name: "COLOR_ATTACHMENT0_WEBGL", enum: 0x8CE0, },
112 { name: "COLOR_ATTACHMENT1_WEBGL", enum: 0x8CE1, },
113 { name: "COLOR_ATTACHMENT2_WEBGL", enum: 0x8CE2, },
114 { name: "COLOR_ATTACHMENT3_WEBGL", enum: 0x8CE3, },
115 { name: "COLOR_ATTACHMENT4_WEBGL", enum: 0x8CE4, },
116 { name: "COLOR_ATTACHMENT5_WEBGL", enum: 0x8CE5, },
117 { name: "COLOR_ATTACHMENT6_WEBGL", enum: 0x8CE6, },
118 { name: "COLOR_ATTACHMENT7_WEBGL", enum: 0x8CE7, },
119 { name: "COLOR_ATTACHMENT8_WEBGL", enum: 0x8CE8, },
120 { name: "COLOR_ATTACHMENT9_WEBGL", enum: 0x8CE9, },
121 { name: "COLOR_ATTACHMENT10_WEBGL", enum: 0x8CEA, },
122 { name: "COLOR_ATTACHMENT11_WEBGL", enum: 0x8CEB, },
123 { name: "COLOR_ATTACHMENT12_WEBGL", enum: 0x8CEC, },
124 { name: "COLOR_ATTACHMENT13_WEBGL", enum: 0x8CED, },
125 { name: "COLOR_ATTACHMENT14_WEBGL", enum: 0x8CEE, },
126 { name: "COLOR_ATTACHMENT15_WEBGL", enum: 0x8CEF, },
128 { name: "DRAW_BUFFER0_WEBGL", enum: 0x8825, },
129 { name: "DRAW_BUFFER1_WEBGL", enum: 0x8826, },
130 { name: "DRAW_BUFFER2_WEBGL", enum: 0x8827, },
131 { name: "DRAW_BUFFER3_WEBGL", enum: 0x8828, },
132 { name: "DRAW_BUFFER4_WEBGL", enum: 0x8829, },
133 { name: "DRAW_BUFFER5_WEBGL", enum: 0x882A, },
134 { name: "DRAW_BUFFER6_WEBGL", enum: 0x882B, },
135 { name: "DRAW_BUFFER7_WEBGL", enum: 0x882C, },
136 { name: "DRAW_BUFFER8_WEBGL", enum: 0x882D, },
137 { name: "DRAW_BUFFER9_WEBGL", enum: 0x882E, },
138 { name: "DRAW_BUFFER10_WEBGL", enum: 0x882F, },
139 { name: "DRAW_BUFFER11_WEBGL", enum: 0x8830, },
140 { name: "DRAW_BUFFER12_WEBGL", enum: 0x8831, },
141 { name: "DRAW_BUFFER13_WEBGL", enum: 0x8832, },
142 { name: "DRAW_BUFFER14_WEBGL", enum: 0x8833, },
143 { name: "DRAW_BUFFER15_WEBGL", enum: 0x8834, },
147 testFailed("WebGL context does not exist");
149 testPassed("WebGL context exists");
151 // Run tests with extension disabled
152 runEnumTestDisabled();
153 runShadersTestDisabled();
154 runAttachmentTestDisabled();
158 // Query the extension and store globally so shouldBe can access it
159 ext = gl.getExtension("WEBGL_draw_buffers");
161 testPassed("No WEBGL_draw_buffers support -- this is legal");
163 runSupportedTest(false);
165 testPassed("Successfully enabled WEBGL_draw_buffers extension");
167 runSupportedTest(true);
168 runEnumTestEnabled();
169 runShadersTestEnabled();
170 runAttachmentTestEnabled();
173 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
177 function createExtDrawBuffersProgram(scriptId, sub) {
178 var fsource = wtu.getScript(scriptId);
179 fsource = wtu.replaceParams(fsource, sub);
180 wtu.addShaderSource(output, "fragement shader", fsource);
181 return wtu.setupProgram(gl, ["vshader", fsource], ["a_position"]);
184 function runSupportedTest(extensionEnabled) {
185 var supported = gl.getSupportedExtensions();
186 if (supported.indexOf("WEBGL_draw_buffers") >= 0) {
187 if (extensionEnabled) {
188 testPassed("WEBGL_draw_buffers listed as supported and getExtension succeeded");
190 testFailed("WEBGL_draw_buffers listed as supported but getExtension failed");
193 if (extensionEnabled) {
194 testFailed("WEBGL_draw_buffers not listed as supported but getExtension succeeded");
196 testPassed("WEBGL_draw_buffers not listed as supported and getExtension failed -- this is legal");
201 function runEnumTestDisabled() {
203 debug("Testing binding enum with extension disabled");
205 // Use the constant directly as we don't have the extension
206 extensionConstants.forEach(function(c) {
208 gl.getParameter(c.enum);
209 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, c.name + " should not be queryable if extension is disabled");
212 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
215 function runEnumTestEnabled() {
217 debug("Testing enums with extension enabled");
219 extensionConstants.forEach(function(c) {
220 shouldBe("ext." + c.name, "0x" + c.enum.toString(16));
222 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "foo");
223 debug(c.name + ":" + ext[c.name].toString(16));
224 expectTrue(c.expectedFn(gl.getParameter(ext[c.name])), "gl.getParemter(ext." + c.name + ")" + c.passMsg);
225 wtu.glErrorShouldBe(gl, gl.NO_ERROR, c.name + " query should succeed if extension is enabled");
229 debug("Testing drawBuffersWEBGL with default drawing buffer");
230 shouldBe("gl.getParameter(ext.DRAW_BUFFER0_WEBGL)", "gl.BACK");
231 wtu.shouldGenerateGLError(gl, gl.INVALID_VALUE, "ext.drawBuffersWEBGL([])");
232 wtu.shouldGenerateGLError(gl, gl.INVALID_VALUE, "ext.drawBuffersWEBGL([gl.NONE, gl.NONE])");
233 wtu.shouldGenerateGLError(gl, gl.INVALID_OPERATION, "ext.drawBuffersWEBGL([ext.COLOR_ATTACHMENT0_WEBGL])");
234 shouldBe("gl.getParameter(ext.DRAW_BUFFER0_WEBGL)", "gl.BACK");
235 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "ext.drawBuffersWEBGL([gl.NONE])");
236 shouldBe("gl.getParameter(ext.DRAW_BUFFER0_WEBGL)", "gl.NONE");
237 wtu.shouldGenerateGLError(gl, gl.NO_ERROR, "ext.drawBuffersWEBGL([gl.BACK])");
238 shouldBe("gl.getParameter(ext.DRAW_BUFFER0_WEBGL)", "gl.BACK");
239 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
242 function testShaders(tests, sub) {
243 tests.forEach(function(test) {
244 var shaders = [wtu.getScript(test.shaders[0]), wtu.replaceParams(wtu.getScript(test.shaders[1]), sub)];
245 wtu.addShaderSource(output, "vertex shader", shaders[0]);
246 wtu.addShaderSource(output, "fragement shader", shaders[1]);
247 var program = wtu.setupProgram(gl, shaders, ["a_position"]);
248 var programLinkedSuccessfully = (program != null);
249 var expectedProgramToLinkSuccessfully = (test.expectFailure == true);
250 expectTrue(programLinkedSuccessfully != expectedProgramToLinkSuccessfully, test.msg);
251 gl.deleteProgram(program);
255 function runShadersTestDisabled() {
257 debug("test shaders disabled");
260 { shaders: ["vshader", "fshaderMacroDisabled"],
261 msg: "GL_EXT_draw_buffers should not be defined in GLSL",
263 { shaders: ["vshader", "fshader"],
264 msg: "#extension GL_EXT_draw_buffers should not be allowed in GLSL",
267 ], {numDrawingBuffers: 1});
268 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
271 function runShadersTestEnabled() {
273 debug("test shaders enabled");
275 var sub = {numDrawingBuffers: gl.getParameter(ext.MAX_DRAW_BUFFERS_WEBGL)};
277 { shaders: ["vshader", "fshaderMacroEnabled"],
278 msg: "GL_EXT_draw_buffers should be defined as 1 in GLSL",
282 var program = createExtDrawBuffersProgram("fshaderBuiltInConstEnabled", sub);
283 wtu.setupUnitQuad(gl);
284 wtu.clearAndDrawUnitQuad(gl);
285 wtu.checkCanvas(gl, [0, 255, 0, 255], "should be green");
286 gl.deleteProgram(program);
287 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
290 function runAttachmentTestDisabled() {
292 debug("test attachment disabled");
293 var tex = gl.createTexture();
294 var fb = gl.createFramebuffer();
295 gl.bindTexture(gl.TEXTURE_2D, tex);
296 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
297 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + 1, gl.TEXTURE_2D, tex, 0);
298 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "should not be able to attach to gl.COLOR_ATTACHMENT1");
299 gl.deleteFramebuffer(fb);
300 gl.deleteTexture(tex);
301 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
304 function makeArray(size, value) {
306 for (var ii = 0; ii < size; ++ii) {
312 function makeColorAttachmentArray(size) {
314 for (var ii = 0; ii < size; ++ii) {
315 array.push(gl.COLOR_ATTACHMENT0 + ii);
320 function runAttachmentTestEnabled() {
322 debug("test attachment enabled");
324 var maxDrawingBuffers = gl.getParameter(ext.MAX_DRAW_BUFFERS_WEBGL);
325 var maxColorAttachments = gl.getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL);
327 var tex = gl.createTexture();
328 var fb = gl.createFramebuffer();
329 gl.bindTexture(gl.TEXTURE_2D, tex);
330 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
331 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + maxColorAttachments, gl.TEXTURE_2D, tex, 0);
332 wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, "should not be able to attach pass the max attachment point: gl.COLOR_ATTACHMENT0 + " + maxColorAttachments);
333 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + maxColorAttachments - 1, gl.TEXTURE_2D, tex, 0);
334 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be able to attach to the max attachment point: gl.COLOR_ATTACHMENT0 + " + (maxColorAttachments - 1));
335 ext.drawBuffersWEBGL(makeArray(maxDrawingBuffers, gl.NONE));
336 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be able to call drawBuffersWEBGL with array NONE of size " + maxColorAttachments);
337 var bufs = makeColorAttachmentArray(maxDrawingBuffers);
338 ext.drawBuffersWEBGL(bufs);
339 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be able to call drawBuffersWEBGL with array attachments of size " + maxColorAttachments);
341 ext.drawBuffersWEBGL(bufs);
342 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be able to call drawBuffersWEBGL with mixed array attachments of size " + maxColorAttachments);
343 if (maxDrawingBuffers > 1) {
344 bufs[0] = ext.COLOR_ATTACHMENT1_WEBGL;
345 bufs[1] = ext.COLOR_ATTACHMENT0_WEBGL;
346 ext.drawBuffersWEBGL(bufs);
347 wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "should not be able to call drawBuffersWEBGL with out of order attachments of size " + maxColorAttachments);
348 var bufs = makeColorAttachmentArray(Math.floor(maxDrawingBuffers / 2));
349 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should not be able to call drawBuffersWEBGL with short array of attachments of size " + maxColorAttachments);
352 gl.deleteFramebuffer(fb);
353 gl.deleteTexture(tex);
354 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
357 function makeColorByIndex(index) {
358 var low = (index - 1) % 15 + 1;
359 var high = (index - 1) / 15;
361 var zeroOrOne = function(v) {
365 var oneOrTwo = function(v) {
369 var makeComponent = function(b0, b1, b2) {
370 return Math.floor(255 * zeroOrOne(b0) / oneOrTwo(b1) / oneOrTwo(b2));
373 makeComponent(low & (1 << 0), high & (1 << 0), high & (1 << 4)),
374 makeComponent(low & (1 << 1), high & (1 << 1), high & (1 << 5)),
375 makeComponent(low & (1 << 2), high & (1 << 2), high & (1 << 6)),
376 makeComponent(low & (1 << 3), high & (1 << 3), high & (1 << 7)),
380 function runDrawTests() {
382 debug("--------- draw tests -----------");
383 var fb = gl.createFramebuffer();
384 var fb2 = gl.createFramebuffer();
385 var halfFB1 = gl.createFramebuffer();
386 var halfFB2 = gl.createFramebuffer();
387 var endsFB = gl.createFramebuffer();
388 var middleFB = gl.createFramebuffer();
390 var maxDrawingBuffers = gl.getParameter(ext.MAX_DRAW_BUFFERS_WEBGL);
391 var maxColorAttachments = gl.getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL);
392 var maxUniformVectors = gl.getParameter(gl.MAX_FRAGMENT_UNIFORM_VECTORS);
393 var maxUsable = Math.min(maxDrawingBuffers, maxColorAttachments, maxUniformVectors);
394 var half = Math.floor(maxUsable / 2);
395 var bufs = makeColorAttachmentArray(maxUsable);
396 var nones = makeArray(maxUsable, gl.NONE);
398 [fb, fb2, halfFB1, halfFB2, endsFB, middleFB].forEach(function(fbo) {
399 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
400 ext.drawBuffersWEBGL(bufs);
403 var checkProgram = wtu.setupTexturedQuad(gl);
404 var redProgram = wtu.setupProgram(gl, ["vshader", "fshaderRed"], ["a_position"]);
405 var drawProgram = createExtDrawBuffersProgram("fshader", {numDrawingBuffers: maxDrawingBuffers});
408 var attachments = [];
409 // Makes 6 framebuffers.
410 // fb and fb2 have all the attachments.
411 // halfFB1 has the first half of the attachments
412 // halfFB2 has the second half of the attachments
413 // endsFB has the first and last attachments
414 // middleFB has all but the first and last attachments
415 for (var ii = 0; ii < maxUsable; ++ii) {
416 var tex = gl.createTexture();
417 gl.bindTexture(gl.TEXTURE_2D, tex);
418 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
419 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
420 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
421 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
422 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
423 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
424 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + ii, gl.TEXTURE_2D, tex, 0);
425 gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
426 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + ii, gl.TEXTURE_2D, tex, 0);
427 gl.bindFramebuffer(gl.FRAMEBUFFER, ii < half ? halfFB1 : halfFB2);
428 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + ii, gl.TEXTURE_2D, tex, 0);
429 gl.bindFramebuffer(gl.FRAMEBUFFER, (ii == 0 || ii == (maxUsable - 1)) ? endsFB : middleFB);
430 gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + ii, gl.TEXTURE_2D, tex, 0);
431 var location = gl.getUniformLocation(drawProgram, "u_colors[" + ii + "]");
432 var color = makeColorByIndex(ii + 1);
433 var floatColor = [color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255];
434 gl.uniform4fv(location, floatColor);
441 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
442 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
443 gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
444 shouldBe("gl.checkFramebufferStatus(gl.FRAMEBUFFER)", "gl.FRAMEBUFFER_COMPLETE");
446 var checkAttachmentsForColorFn = function(attachments, colorFn) {
447 gl.bindFramebuffer(gl.FRAMEBUFFER, null);
448 gl.useProgram(checkProgram);
449 attachments.forEach(function(attachment, index) {
450 gl.bindTexture(gl.TEXTURE_2D, attachment.texture);
451 wtu.clearAndDrawUnitQuad(gl);
452 var expectedColor = colorFn(attachment, index);
454 expectedColor.forEach(function(v) {
455 if (v != 0 && v != 255) {
459 wtu.checkCanvas(gl, expectedColor, "attachment " + index + " should be " + expectedColor.toString(), tolerance);
464 var checkAttachmentsForColor = function(attachments, color) {
465 checkAttachmentsForColorFn(attachments, function(attachment, index) {
466 return color || attachment.color;
470 var drawAndCheckAttachments = function(testFB, msg, testFn) {
471 debug("test clearing " + msg);
473 gl.bindFramebuffer(gl.FRAMEBUFFER, testFB);
475 attachments.forEach(function(attachment, index) {
476 debug("attachment: " + index + " = " + wtu.glEnumToString(gl, gl.getParameter(ext.DRAW_BUFFER0_WEBGL + index)) +
477 ", " + wtu.glEnumToString(gl, gl.getFramebufferAttachmentParameter(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + index, gl.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE)));
480 if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
481 debug("framebuffer not complete");
486 // Clear all the attachments
487 gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
488 gl.clearColor(0, 0, 0, 0);
489 gl.clear(gl.COLOR_BUFFER_BIT);
490 //checkAttachmentsForColorFn(attachments, function(attachment, index) {
491 // return [0, 0, 0, 0];
495 // Clear some attachments using testFB
496 gl.bindFramebuffer(gl.FRAMEBUFFER, testFB);
498 gl.clearColor(0, 1, 0, 1);
499 gl.clear(gl.COLOR_BUFFER_BIT);
500 checkAttachmentsForColorFn(attachments, function(attachment, index) {
501 return testFn(attachment, index) ? [0, 255, 0, 255] : [0, 0, 0, 0];
504 debug("test drawing to " + msg);
506 // Draw to some attachments using testFB
507 gl.useProgram(drawProgram);
508 gl.bindFramebuffer(gl.FRAMEBUFFER, testFB);
509 wtu.drawUnitQuad(gl);
511 checkAttachmentsForColorFn(attachments, function(attachment, index) {
512 return testFn(attachment, index) ? attachment.color : [0, 0, 0, 0];
516 gl.useProgram(drawProgram);
517 gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
518 ext.drawBuffersWEBGL(bufs);
519 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
520 ext.drawBuffersWEBGL(bufs);
522 wtu.drawUnitQuad(gl);
524 debug("test that each texture got the correct color.");
526 checkAttachmentsForColor(attachments);
528 debug("test clearing clears all the textures");
529 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
530 gl.clearColor(0, 1, 0, 1);
531 gl.clear(gl.COLOR_BUFFER_BIT);
533 checkAttachmentsForColor(attachments, [0, 255, 0, 255]);
535 debug("test that NONE draws nothing");
536 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
537 ext.drawBuffersWEBGL(nones);
538 gl.useProgram(redProgram);
539 wtu.clearAndDrawUnitQuad(gl);
541 checkAttachmentsForColor(attachments, [0, 255, 0, 255]);
543 debug("test that gl_FragColor broadcasts");
544 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
545 ext.drawBuffersWEBGL(bufs);
546 gl.useProgram(redProgram);
547 wtu.drawUnitQuad(gl);
549 checkAttachmentsForColor(attachments, [255, 0, 0, 255]);
552 var bufs1 = makeColorAttachmentArray(maxUsable);
553 var bufs2 = makeColorAttachmentArray(maxUsable);
554 for (var ii = 0; ii < maxUsable; ++ii) {
562 debug("test setting first half to NONE and clearing");
564 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
565 ext.drawBuffersWEBGL(bufs1);
566 gl.clearColor(0, 1, 0, 1);
567 gl.clear(gl.COLOR_BUFFER_BIT);
569 checkAttachmentsForColorFn(attachments, function(attachment, index) {
570 return index < half ? [255, 0, 0, 255] : [0, 255, 0, 255];
573 debug("test setting first half to NONE and drawing");
575 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
576 gl.useProgram(drawProgram);
577 wtu.drawUnitQuad(gl);
579 checkAttachmentsForColorFn(attachments, function(attachment, index) {
580 return index < half ? [255, 0, 0, 255] : attachment.color;
583 debug("test setting second half to NONE and clearing");
585 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
586 ext.drawBuffersWEBGL(bufs);
587 gl.clearColor(1, 0, 0, 1);
588 gl.clear(gl.COLOR_BUFFER_BIT);
589 ext.drawBuffersWEBGL(bufs2);
590 gl.clearColor(0, 0, 1, 1);
591 gl.clear(gl.COLOR_BUFFER_BIT);
592 checkAttachmentsForColorFn(attachments, function(attachment, index) {
593 return index < half ? [0, 0, 255, 255] : [255, 0, 0, 255];
596 debug("test setting second half to NONE and drawing");
598 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
599 gl.useProgram(drawProgram);
600 wtu.drawUnitQuad(gl);
602 checkAttachmentsForColorFn(attachments, function(attachment, index) {
603 return index < half ? attachment.color : [255, 0, 0, 255];
606 gl.bindFramebuffer(gl.FRAMEBUFFER, halfFB1);
607 ext.drawBuffersWEBGL(bufs);
608 drawAndCheckAttachments(
609 halfFB1, "framebuffer that only has first half of attachments",
610 function(attachment, index) {
614 gl.bindFramebuffer(gl.FRAMEBUFFER, halfFB2);
615 ext.drawBuffersWEBGL(bufs);
616 drawAndCheckAttachments(
617 halfFB2, "framebuffer that only has second half of attachments",
618 function(attachment, index) {
619 return index >= half;
623 gl.bindFramebuffer(gl.FRAMEBUFFER, endsFB);
624 ext.drawBuffersWEBGL(bufs);
625 drawAndCheckAttachments(
626 endsFB, "framebuffer that only has first and last attachments",
627 function(attachment, index) {
628 return index == 0 || index == (maxUsable - 1);
631 gl.bindFramebuffer(gl.FRAMEBUFFER, middleFB);
632 ext.drawBuffersWEBGL(bufs);
633 drawAndCheckAttachments(
635 "framebuffer that has all but the first and last attachments",
636 function(attachment, index) {
637 return index != 0 && index != (maxUsable - 1);
642 debug("test switching between fbos keeps drawbuffer state");
643 gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
644 ext.drawBuffersWEBGL(nones);
646 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
647 ext.drawBuffersWEBGL(bufs);
648 gl.clearColor(1, 0, 0, 1);
649 gl.clear(gl.COLOR_BUFFER_BIT);
650 checkAttachmentsForColor(attachments, [255, 0, 0, 255]);
652 gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
653 gl.useProgram(drawProgram);
654 wtu.drawUnitQuad(gl);
655 checkAttachmentsForColor(attachments, [255, 0, 0, 255]);
657 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
658 gl.useProgram(drawProgram);
659 wtu.drawUnitQuad(gl);
660 checkAttachmentsForColor(attachments);
662 debug("test queries");
663 debug("check framebuffer with all attachments on");
664 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
665 for (var ii = 0; ii < maxUsable; ++ii) {
666 shouldBe("gl.getParameter(ext.DRAW_BUFFER0_WEBGL + " + ii + ")", "gl.COLOR_ATTACHMENT0 + " + ii);
669 debug("check framebuffer with all attachments off");
670 gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
671 for (var ii = 0; ii < maxUsable; ++ii) {
672 shouldBe("gl.getParameter(ext.DRAW_BUFFER0_WEBGL + " + ii + ")", "gl.NONE");
675 debug("test attachment size mis-match");
676 gl.bindTexture(gl.TEXTURE_2D, attachments[0].texture);
677 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width * 2, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
678 gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
679 shouldBeTrue("gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE");
680 gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
681 shouldBeTrue("gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE");
683 gl.deleteFramebuffer(fb);
684 gl.deleteFramebuffer(fb2);
685 gl.deleteFramebuffer(halfFB1);
686 gl.deleteFramebuffer(halfFB2);
687 attachments.forEach(function(attachment) {
688 gl.deleteTexture(attachment.texture);
690 gl.deleteProgram(checkProgram);
691 gl.deleteProgram(redProgram);
692 gl.deleteProgram(drawProgram);
696 var successfullyParsed = true;
698 <script src="../../resources/js-test-post.js"></script>