webgl-draw-buffers-utils.js (2314B)
1 /* 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. 5 */ 6 7 // This file contains utilities shared between tests for the WEBGL_draw_buffers extension and multiple draw buffers functionality in WebGL 2.0. 8 9 'use strict'; 10 11 var WebGLDrawBuffersUtils = function(gl, ext) { 12 13 var getMaxUsableColorAttachments = function() { 14 var maxDrawingBuffers; 15 var maxColorAttachments; 16 if (ext) { 17 // EXT_draw_buffers 18 maxDrawingBuffers = gl.getParameter(ext.MAX_DRAW_BUFFERS_WEBGL); 19 maxColorAttachments = gl.getParameter(ext.MAX_COLOR_ATTACHMENTS_WEBGL); 20 } else { 21 // WebGL 2.0 22 maxDrawingBuffers = gl.getParameter(gl.MAX_DRAW_BUFFERS); 23 maxColorAttachments = gl.getParameter(gl.MAX_COLOR_ATTACHMENTS); 24 } 25 var maxUniformVectors = gl.getParameter(gl.MAX_FRAGMENT_UNIFORM_VECTORS); 26 return Math.min(maxDrawingBuffers, maxColorAttachments, maxUniformVectors); 27 }; 28 29 var makeColorAttachmentArray = function(size) { 30 var array = [] 31 for (var ii = 0; ii < size; ++ii) { 32 array.push(gl.COLOR_ATTACHMENT0 + ii); 33 } 34 return array; 35 } 36 37 var checkProgram = wtu.setupTexturedQuad(gl); 38 39 var checkAttachmentsForColorFn = function(attachments, colorFn) { 40 gl.bindFramebuffer(gl.FRAMEBUFFER, null); 41 gl.useProgram(checkProgram); 42 attachments.forEach(function(attachment, index) { 43 gl.bindTexture(gl.TEXTURE_2D, attachment.texture); 44 wtu.clearAndDrawUnitQuad(gl); 45 var expectedColor = colorFn(attachment, index); 46 var tolerance = 0; 47 expectedColor.forEach(function(v) { 48 if (v != 0 && v != 255) { 49 tolerance = 8; 50 } 51 }); 52 wtu.checkCanvas(gl, expectedColor, "attachment " + index + " should be " + expectedColor.toString(), tolerance); 53 }); 54 debug(""); 55 }; 56 57 var checkAttachmentsForColor = function(attachments, color) { 58 checkAttachmentsForColorFn(attachments, function(attachment, index) { 59 return color || attachment.color; 60 }); 61 }; 62 63 return { 64 getMaxUsableColorAttachments: getMaxUsableColorAttachments, 65 makeColorAttachmentArray: makeColorAttachmentArray, 66 checkAttachmentsForColorFn: checkAttachmentsForColorFn, 67 checkAttachmentsForColor: checkAttachmentsForColor 68 }; 69 };