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.
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>
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;
30 gl_Position = vec4(inPosition,
0.0,
1.0);
33 <script id=
"fshader" type=
"x-shader/x-fragment">
34 precision mediump float;
40 if (color == vec4(
0.0))
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
;
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
);
62 vertices
.push(x
+ w
, y
+ w
);
64 vertices
.push(x
+ w
, y
);
67 var numVertices
= (vertices
.length
/ 2);
73 var drawIterationsPerTest
= 100;
76 gl
= wtu
.create3DContext("canvas");
77 var attribs
= ["inPosition", "inColor"];
78 wtu
.setupProgram(gl
, ["vshader", "fshader"], attribs
);
79 gl
.disable(gl
.DEPTH_TEST
);
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);
95 var drawIterations
= 0;
97 function runNextTest() {
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");
109 if (size
> 64 * 1024 * 1024) {
110 gl
.deleteBuffer(buffer
);
111 testPassed("Tests finished");
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");
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
));
139 if (drawIterations
< drawIterationsPerTest
) {
141 requestAnimationFrame(doDraw
);
150 var successfullyParsed
= true;