66f691ca752133f7b34b2a3c4f32a73ff6d8b875
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / extensions / webgl-draw-buffers.html
1 <!--
2
3 /*
4 ** Copyright (c) 2013 The Khronos Group Inc.
5 **
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:
13 **
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
16 **
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.
24 */
25
26 -->
27
28 <!DOCTYPE html>
29 <html>
30 <head>
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>
36 </head>
37 <body>
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;
43 void main() {
44     gl_Position = a_position;
45 }
46 </script>
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)];
51 void main() {
52     for (int i = 0; i < $(numDrawingBuffers); ++i) {
53         gl_FragData[i] = u_colors[i];
54     }
55 }
56 </script>
57 <script id="fshaderRed" type="x-shader/x-fragment">
58 precision mediump float;
59 void main() {
60     gl_FragColor = vec4(1,0,0,1);
61 }
62 </script>
63 <script id="fshaderMacroDisabled" type="x-shader/x-fragment">
64 #ifdef GL_EXT_draw_buffers
65   bad code here
66 #endif
67 precision mediump float;
68 void main() {
69     gl_FragColor = vec4(0,0,0,0);
70 }
71 </script>
72 <script id="fshaderMacroEnabled" type="x-shader/x-fragment">
73 #ifdef GL_EXT_draw_buffers
74   #if GL_EXT_draw_buffers == 1
75     #define CODE
76   #else
77     #define CODE this_code_is_bad_it_should_have_compiled
78   #endif
79 #else
80    #define CODE this_code_is_bad_it_should_have_compiled
81 #endif
82 CODE
83 precision mediump float;
84 void main() {
85     gl_FragColor = vec4(0,0,0,0);
86 }
87 </script>
88 <script id="fshaderBuiltInConstEnabled" type="x-shader/x-fragment">
89 precision mediump float;
90 void main() {
91     gl_FragColor = (gl_MaxDrawBuffers == $(numDrawingBuffers)) ? vec4(0,1,0,1) : vec4(1,0,0,1);
92 }
93 </script>
94 <script>
95 "use strict";
96 description("This test verifies the functionality of the WEBGL_draw_buffers extension, if it is available.");
97
98 debug("");
99
100 var wtu = WebGLTestUtils;
101 var canvas = document.getElementById("canvas");
102 var output = document.getElementById("console");
103 var gl = wtu.create3DContext(canvas);
104 var ext = null;
105 var vao = null;
106
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"},
110
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, },
127
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, },
144 ];
145
146 if (!gl) {
147   testFailed("WebGL context does not exist");
148 } else {
149   testPassed("WebGL context exists");
150
151   // Run tests with extension disabled
152   runEnumTestDisabled();
153   runShadersTestDisabled();
154   runAttachmentTestDisabled();
155
156   debug("");
157
158   // Query the extension and store globally so shouldBe can access it
159   ext = gl.getExtension("WEBGL_draw_buffers");
160   if (!ext) {
161     testPassed("No WEBGL_draw_buffers support -- this is legal");
162
163     runSupportedTest(false);
164   } else {
165     testPassed("Successfully enabled WEBGL_draw_buffers extension");
166
167     runSupportedTest(true);
168     runEnumTestEnabled();
169     runShadersTestEnabled();
170     runAttachmentTestEnabled();
171     runDrawTests();
172
173     wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
174   }
175 }
176
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"]);
182 }
183
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");
189     } else {
190       testFailed("WEBGL_draw_buffers listed as supported but getExtension failed");
191     }
192   } else {
193     if (extensionEnabled) {
194       testFailed("WEBGL_draw_buffers not listed as supported but getExtension succeeded");
195     } else {
196       testPassed("WEBGL_draw_buffers not listed as supported and getExtension failed -- this is legal");
197     }
198   }
199 }
200
201 function runEnumTestDisabled() {
202   debug("");
203   debug("Testing binding enum with extension disabled");
204
205   // Use the constant directly as we don't have the extension
206   extensionConstants.forEach(function(c) {
207     if (c.expectedFn) {
208       gl.getParameter(c.enum);
209       wtu.glErrorShouldBe(gl, gl.INVALID_ENUM, c.name + " should not be queryable if extension is disabled");
210     }
211   });
212   wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
213 }
214
215 function runEnumTestEnabled() {
216   debug("");
217   debug("Testing enums with extension enabled");
218
219   extensionConstants.forEach(function(c) {
220     shouldBe("ext." + c.name, "0x" + c.enum.toString(16));
221     if (c.expectedFn) {
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");
226     }
227   });
228
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");
240 }
241
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);
252   });
253 }
254
255 function runShadersTestDisabled() {
256   debug("");
257   debug("test shaders disabled");
258
259   testShaders([
260     { shaders: ["vshader", "fshaderMacroDisabled"],
261       msg: "GL_EXT_draw_buffers should not be defined in GLSL",
262     },
263     { shaders: ["vshader", "fshader"],
264       msg: "#extension GL_EXT_draw_buffers should not be allowed in GLSL",
265       expectFailure: true,
266     },
267   ], {numDrawingBuffers: 1});
268   wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
269 }
270
271 function runShadersTestEnabled() {
272   debug("");
273   debug("test shaders enabled");
274
275   var sub = {numDrawingBuffers: gl.getParameter(ext.MAX_DRAW_BUFFERS_WEBGL)};
276   testShaders([
277     { shaders: ["vshader", "fshaderMacroEnabled"],
278     msg: "GL_EXT_draw_buffers should be defined as 1 in GLSL",
279     },
280   ], sub);
281
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");
288 }
289
290 function runAttachmentTestDisabled() {
291   debug("");
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");
302 }
303
304 function makeArray(size, value) {
305   var array = []
306   for (var ii = 0; ii < size; ++ii) {
307     array.push(value);
308   }
309   return array;
310 }
311
312 function makeColorAttachmentArray(size) {
313   var array = []
314   for (var ii = 0; ii < size; ++ii) {
315     array.push(gl.COLOR_ATTACHMENT0 + ii);
316   }
317   return array;
318 }
319
320 function runAttachmentTestEnabled() {
321   debug("");
322   debug("test attachment enabled");
323
324   var maxDrawingBuffers = gl.getParameter(ext.MAX_DRAW_BUFFERS_WEBGL);
325   var maxColorAttachments = gl.getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL);
326
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);
340   bufs[0] = gl.NONE;
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);
350   }
351
352   gl.deleteFramebuffer(fb);
353   gl.deleteTexture(tex);
354   wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");
355 }
356
357 function makeColorByIndex(index) {
358   var low = (index - 1) % 15 + 1;
359   var high = (index - 1) / 15;
360
361   var zeroOrOne = function(v) {
362     return v ? 1 : 0;
363   };
364
365   var oneOrTwo = function(v) {
366     return v ? 2 : 1;
367   }
368
369   var makeComponent = function(b0, b1, b2) {
370     return Math.floor(255 * zeroOrOne(b0) / oneOrTwo(b1) / oneOrTwo(b2));
371   };
372   return [
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)),
377   ];
378 }
379
380 function runDrawTests() {
381   debug("");
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();
389
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);
397
398   [fb, fb2, halfFB1, halfFB2, endsFB, middleFB].forEach(function(fbo) {
399     gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
400     ext.drawBuffersWEBGL(bufs);
401   });
402
403   var checkProgram = wtu.setupTexturedQuad(gl);
404   var redProgram = wtu.setupProgram(gl, ["vshader", "fshaderRed"], ["a_position"]);
405   var drawProgram = createExtDrawBuffersProgram("fshader", {numDrawingBuffers: maxDrawingBuffers});
406   var width = 64;
407   var height = 64;
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);
435     attachments.push({
436       texture: tex,
437       location: location,
438       color: color
439     });
440   }
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");
445
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);
453       var tolerance = 0;
454       expectedColor.forEach(function(v) {
455         if (v != 0 && v != 255) {
456           tolerance = 8;
457         }
458       });
459       wtu.checkCanvas(gl, expectedColor, "attachment " + index + " should be " + expectedColor.toString(), tolerance);
460     });
461     debug("");
462   };
463
464   var checkAttachmentsForColor = function(attachments, color) {
465     checkAttachmentsForColorFn(attachments, function(attachment, index) {
466       return color || attachment.color;
467     });
468   };
469
470   var drawAndCheckAttachments = function(testFB, msg, testFn) {
471     debug("test clearing " + msg);
472
473     gl.bindFramebuffer(gl.FRAMEBUFFER, testFB);
474
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)));
478     });
479
480     if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
481       debug("framebuffer not complete");
482       debug("");
483       return;
484     }
485
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];
492     //});
493     //debug("--");
494
495     // Clear some attachments using testFB
496     gl.bindFramebuffer(gl.FRAMEBUFFER, testFB);
497
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];
502     });
503
504     debug("test drawing to " + msg);
505
506     // Draw to some attachments using testFB
507     gl.useProgram(drawProgram);
508     gl.bindFramebuffer(gl.FRAMEBUFFER, testFB);
509     wtu.drawUnitQuad(gl);
510
511     checkAttachmentsForColorFn(attachments, function(attachment, index) {
512       return testFn(attachment, index) ? attachment.color : [0, 0, 0, 0];
513     });
514   };
515
516   gl.useProgram(drawProgram);
517   gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
518   ext.drawBuffersWEBGL(bufs);
519   gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
520   ext.drawBuffersWEBGL(bufs);
521
522   wtu.drawUnitQuad(gl);
523
524   debug("test that each texture got the correct color.");
525
526   checkAttachmentsForColor(attachments);
527
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);
532
533   checkAttachmentsForColor(attachments, [0, 255, 0, 255]);
534
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);
540
541   checkAttachmentsForColor(attachments, [0, 255, 0, 255]);
542
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);
548
549   checkAttachmentsForColor(attachments, [255, 0, 0, 255]);
550
551   if (maxUsable > 1) {
552     var bufs1 = makeColorAttachmentArray(maxUsable);
553     var bufs2 = makeColorAttachmentArray(maxUsable);
554     for (var ii = 0; ii < maxUsable; ++ii) {
555       if (ii < half) {
556         bufs1[ii] = gl.NONE;
557       } else {
558         bufs2[ii] = gl.NONE;
559       }
560     }
561
562     debug("test setting first half to NONE and clearing");
563
564     gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
565     ext.drawBuffersWEBGL(bufs1);
566     gl.clearColor(0, 1, 0, 1);
567     gl.clear(gl.COLOR_BUFFER_BIT);
568
569     checkAttachmentsForColorFn(attachments, function(attachment, index) {
570       return index < half ? [255, 0, 0, 255] : [0, 255, 0, 255];
571     });
572
573     debug("test setting first half to NONE and drawing");
574
575     gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
576     gl.useProgram(drawProgram);
577     wtu.drawUnitQuad(gl);
578
579     checkAttachmentsForColorFn(attachments, function(attachment, index) {
580       return index < half ? [255, 0, 0, 255] : attachment.color;
581     });
582
583     debug("test setting second half to NONE and clearing");
584
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];
594     });
595
596     debug("test setting second half to NONE and drawing");
597
598     gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
599     gl.useProgram(drawProgram);
600     wtu.drawUnitQuad(gl);
601
602     checkAttachmentsForColorFn(attachments, function(attachment, index) {
603       return index < half ? attachment.color : [255, 0, 0, 255];
604     });
605
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) {
611         return index < half;
612       });
613
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;
620       });
621
622     if (maxUsable > 2) {
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);
629         });
630
631       gl.bindFramebuffer(gl.FRAMEBUFFER, middleFB);
632       ext.drawBuffersWEBGL(bufs);
633       drawAndCheckAttachments(
634         middleFB,
635         "framebuffer that has all but the first and last attachments",
636         function(attachment, index) {
637           return index != 0 && index != (maxUsable - 1);
638         });
639     }
640   }
641
642   debug("test switching between fbos keeps drawbuffer state");
643   gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
644   ext.drawBuffersWEBGL(nones);
645
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]);
651
652   gl.bindFramebuffer(gl.FRAMEBUFFER, fb2);
653   gl.useProgram(drawProgram);
654   wtu.drawUnitQuad(gl);
655   checkAttachmentsForColor(attachments, [255, 0, 0, 255]);
656
657   gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
658   gl.useProgram(drawProgram);
659   wtu.drawUnitQuad(gl);
660   checkAttachmentsForColor(attachments);
661
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);
667   }
668
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");
673   }
674
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");
682
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);
689   });
690   gl.deleteProgram(checkProgram);
691   gl.deleteProgram(redProgram);
692   gl.deleteProgram(drawProgram);
693 }
694
695 debug("");
696 var successfullyParsed = true;
697 </script>
698 <script src="../../resources/js-test-post.js"></script>
699
700 </body>
701 </html>