Backed out changeset 8fc3326bce7f (bug 1943032) for causing failures at browser_tab_g...
[gecko.git] / dom / canvas / test / webgl-conf / checkout / extra / buffer-gc-stress.html
blob86056747e2137bf93be59edeabf5eb71d8ca460a
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 -->
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;
24 varying vec4 color;
26 void main()
28 color = inColor;
30 gl_Position = vec4(inPosition, 0.0, 1.0);
32 </script>
33 <script id="fshader" type="x-shader/x-fragment">
34 precision mediump float;
36 varying vec4 color;
38 void main()
40 if (color == vec4(0.0))
41 discard;
43 gl_FragColor = color;
45 </script>
47 <script>
48 "use strict";
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;
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 );
62 vertices.push(x + w, y + w);
63 vertices.push(x, y );
64 vertices.push(x + w, y );
67 var numVertices = (vertices.length / 2);
69 var gl;
70 var squareBuffer;
71 var buffer;
72 var updateBufferData;
73 var drawIterationsPerTest = 100;
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);
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);
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);
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;
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;
109 if (size > 64 * 1024 * 1024) {
110 gl.deleteBuffer(buffer);
111 testPassed("Tests finished");
112 return;
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);
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();
125 var doDraw = function() {
126 gl.clearColor(0, 255, 0, 255);
127 gl.clear(gl.COLOR_BUFFER_BIT);
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));
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;
139 if (drawIterations < drawIterationsPerTest) {
140 ++drawIterations;
141 requestAnimationFrame(doDraw);
142 } else {
143 runNextTest();
147 initGL();
148 runNextTest();
150 var successfullyParsed = true;
151 </script>
153 </body>
154 </html>