4 ** Copyright (c) 2013 The Khronos Group Inc.
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:
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
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.
31 <meta charset="utf-8">
32 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
33 <script src="../../resources/js-test-pre.js"></script>
34 <script src="../resources/webgl-test-utils.js"></script>
37 <script id="vshader" type="x-shader/x-vertex">
38 uniform mat4 transformMatrix;
39 uniform vec3 positionOffset;
40 attribute vec2 aPosition;
42 gl_Position = transformMatrix * vec4(aPosition, 0.0, 1.0) + vec4(positionOffset, 0.0);
45 <script id="fshader" type="x-shader/x-fragment">
47 gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
50 <div id="description"></div>
51 <canvas id="canvas" width="256" height="256"></canvas>
52 <div id="console"></div>
55 enableJSTestPreVerboseLogging();
56 description("Test many draw calls and uniform updates per frame");
58 debug('Regression test for Chromium <a href="http://crbug.com/320724">Issue 320724</a> and <a href="http://crbug.com/322726">Issue 322726</a>');
61 var contextWasLost = false;
63 var wtu = WebGLTestUtils;
64 var canvas = document.getElementById('canvas');
65 var gl = wtu.create3DContext(canvas);
66 canvas.addEventListener('webglcontextlost', function(event) { contextWasLost = true; }, false);
67 var program = wtu.setupProgram(gl, ["vshader", "fshader"], [ "aPosition" ]);
69 testFailed("failed to create test program");
72 gl.useProgram(program);
73 var vertexObject = gl.createBuffer();
74 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
75 gl.enableVertexAttribArray(0);
77 // Initialize vertices
78 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
84 1.0, -1.0 ]), gl.STATIC_DRAW);
85 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
87 gl.clearColor(0.3, 0.3, 0.3, 1.0);
89 // Initialize uniforms
90 var transformLoc = gl.getUniformLocation(program, 'transformMatrix');
91 var offsetLoc = gl.getUniformLocation(program, 'positionOffset');
93 // This many draw calls appear to be necessary to trigger the original bug reliably.
94 var tilesPerSide = 100;
95 var numDrawsThisFrame = 0;
97 var doNextDraw = function() {
98 // Sometimes, the original bug can't be caught cooperatively, and it
99 // causes the entire tab to hang irrevocably.
100 if (contextWasLost) {
101 testFailed("WebGL context was lost while running the test");
106 var totalDraws = tilesPerSide * tilesPerSide;
107 if (numDrawsThisFrame >= totalDraws) {
108 testPassed("All draw calls completed successfully");
113 numDrawsThisFrame += tilesPerSide;
115 gl.clear(gl.COLOR_BUFFER_BIT);
117 var transformMatrix = new Float32Array(16);
118 transformMatrix[15] = 1.0;
119 var scaleFactor = 1.0 / tilesPerSide;
120 transformMatrix[0] = scaleFactor;
121 transformMatrix[5] = scaleFactor;
122 transformMatrix[10] = scaleFactor;
124 var offset = new Float32Array(3);
126 var drawsDoneThisFrame = 0;
127 for (var yy = 0; yy < tilesPerSide; ++yy) {
128 for (var xx = 0; xx < tilesPerSide; ++xx) {
129 if (drawsDoneThisFrame >= numDrawsThisFrame)
132 gl.uniformMatrix4fv(transformLoc, false, transformMatrix);
134 offset[0] = 2.0 * ((0.5 + xx) / tilesPerSide) - 1.0;
135 offset[1] = 2.0 * ((0.5 + yy) / tilesPerSide) - 1.0;
136 gl.uniform3f(offsetLoc, offset[0], offset[1], offset[2]);
138 gl.drawArrays(gl.TRIANGLES, 0, 6);
139 ++drawsDoneThisFrame;
142 if (drawsDoneThisFrame >= numDrawsThisFrame)
146 var iterations = numDrawsThisFrame / tilesPerSide;
147 if (iterations % 10 === 0) {
148 // Needed to avoid test timeout within the harness on some slower platforms
149 testPassed("Completed " + iterations + " iterations");
152 wtu.requestAnimFrame(doNextDraw);
155 wtu.requestAnimFrame(doNextDraw);
156 var successfullyParsed = true;