buffer-gc-stress.html (4102B)
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 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>Float32Array garbage collection test</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> 15 </head> 16 <body> 17 <canvas id="canvas" width="40" height="40"></canvas> 18 <div id="description"></div> 19 <div id="console"></div> 20 <script id="vshader" type="x-shader/x-vertex"> 21 attribute vec2 inPosition; 22 attribute vec4 inColor; 23 24 varying vec4 color; 25 26 void main() 27 { 28 color = inColor; 29 30 gl_Position = vec4(inPosition, 0.0, 1.0); 31 } 32 </script> 33 <script id="fshader" type="x-shader/x-fragment"> 34 precision mediump float; 35 36 varying vec4 color; 37 38 void main() 39 { 40 if (color == vec4(0.0)) 41 discard; 42 43 gl_FragColor = color; 44 } 45 </script> 46 47 <script> 48 "use strict"; 49 50 description("Allocates a buffer object and then updates it repeatedly using throw-away Float32Array objects. " + 51 "Ideally, this should not result in a browser crash or instability, since GC should be able to collect all Float32Arrays."); 52 var wtu = WebGLTestUtils; 53 54 var vertices = []; 55 var w = 0.25; 56 for (var x = -1; x < 1; x += w) { 57 for (var y = -1; y < 1; y += w) { 58 vertices.push(x + w, y + w); 59 vertices.push(x, y + w); 60 vertices.push(x, y ); 61 62 vertices.push(x + w, y + w); 63 vertices.push(x, y ); 64 vertices.push(x + w, y ); 65 } 66 } 67 var numVertices = (vertices.length / 2); 68 69 var gl; 70 var squareBuffer; 71 var buffer; 72 var updateBufferData; 73 var drawIterationsPerTest = 100; 74 75 function initGL() { 76 gl = wtu.create3DContext("canvas"); 77 var attribs = ["inPosition", "inColor"]; 78 wtu.setupProgram(gl, ["vshader", "fshader"], attribs); 79 gl.disable(gl.DEPTH_TEST); 80 gl.disable(gl.BLEND); 81 82 squareBuffer = gl.createBuffer(); 83 gl.enableVertexAttribArray(0); 84 gl.bindBuffer(gl.ARRAY_BUFFER, squareBuffer); 85 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); 86 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); 87 88 buffer = gl.createBuffer(); 89 gl.enableVertexAttribArray(1); 90 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); 91 gl.vertexAttribPointer(1, 4, gl.FLOAT, false, 0, 0); 92 } 93 94 var testIndex = -1; 95 var drawIterations = 0; 96 var size = 0; 97 function runNextTest() { 98 ++testIndex; 99 var prevSize = size; 100 size = Math.pow(2, testIndex) * numVertices * 16; 101 102 if (size > 2 * 1024 * 1024 && prevSize <= 2 * 1024 * 1024) { 103 if (!confirm("The following tests can cause unresponsiveness or instability. Press OK to continue.")) { 104 testFailed("Tests aborted"); 105 return; 106 } 107 } 108 109 if (size > 64 * 1024 * 1024) { 110 gl.deleteBuffer(buffer); 111 testPassed("Tests finished"); 112 return; 113 } 114 115 debug('Initializing buffer with size: ' + size); 116 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); 117 gl.bufferData(gl.ARRAY_BUFFER, size, gl.DYNAMIC_DRAW); 118 updateBufferData = new Float32Array(size / 4); 119 120 debug("Drawing " + drawIterationsPerTest + " times, each time creating a new throw-away Float32Array of size " + size + " and using it to update the buffer"); 121 drawIterations = 0; 122 doDraw(); 123 }; 124 125 var doDraw = function() { 126 gl.clearColor(0, 255, 0, 255); 127 gl.clear(gl.COLOR_BUFFER_BIT); 128 129 // Update the array buffer with a throw-away Float32Array 130 gl.bindBuffer(gl.ARRAY_BUFFER, buffer); 131 gl.bufferSubData(gl.ARRAY_BUFFER, 0, new Float32Array(updateBufferData)); 132 133 gl.drawArrays(gl.TRIANGLES, 0, numVertices); 134 var error = gl.getError(); 135 if (error !== gl.NO_ERROR) { 136 testFailed("drawArrays failed with error " + wtu.glEnumToString(gl, error)); 137 return; 138 } 139 if (drawIterations < drawIterationsPerTest) { 140 ++drawIterations; 141 requestAnimationFrame(doDraw); 142 } else { 143 runNextTest(); 144 } 145 }; 146 147 initGL(); 148 runNextTest(); 149 150 var successfullyParsed = true; 151 </script> 152 153 </body> 154 </html>