Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / rendering / many-draw-calls.html
1 <!--
2
3 /*
4 ** Copyright (c) 2013 The Khronos Group Inc.
5 **
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:
13 **
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
16 **
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.
24 */
25
26 -->
27
28 <!DOCTYPE html>
29 <html>
30 <head>
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>
35 </head>
36 <body>
37 <script id="vshader" type="x-shader/x-vertex">
38 uniform mat4 transformMatrix;
39 uniform vec3 positionOffset;
40 attribute vec2 aPosition;
41 void main() {
42   gl_Position = transformMatrix * vec4(aPosition, 0.0, 1.0) + vec4(positionOffset, 0.0);
43 }
44 </script>
45 <script id="fshader" type="x-shader/x-fragment">
46 void main() {
47   gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
48 }
49 </script>
50 <div id="description"></div>
51 <canvas id="canvas" width="256" height="256"></canvas>
52 <div id="console"></div>
53 <script>
54 "use strict";
55 enableJSTestPreVerboseLogging();
56 description("Test many draw calls and uniform updates per frame");
57
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>');
59 debug('');
60
61 var contextWasLost = false;
62
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" ]);
68 if (!program) {
69   testFailed("failed to create test program");
70 }
71
72 gl.useProgram(program);
73 var vertexObject = gl.createBuffer();
74 gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
75 gl.enableVertexAttribArray(0);
76
77 // Initialize vertices
78 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
79    -1.0,  1.0,
80     1.0, -1.0,
81     1.0,  1.0,
82    -1.0,  1.0,
83    -1.0, -1.0,
84     1.0, -1.0 ]), gl.STATIC_DRAW);
85 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
86
87 gl.clearColor(0.3, 0.3, 0.3, 1.0);
88
89 // Initialize uniforms
90 var transformLoc = gl.getUniformLocation(program, 'transformMatrix');
91 var offsetLoc = gl.getUniformLocation(program, 'positionOffset');
92
93 // This many draw calls appear to be necessary to trigger the original bug reliably.
94 var tilesPerSide = 100;
95 var numDrawsThisFrame = 0;
96
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");
102     finishTest();
103     return;
104   }
105
106   var totalDraws = tilesPerSide * tilesPerSide;
107   if (numDrawsThisFrame >= totalDraws) {
108     testPassed("All draw calls completed successfully");
109     finishTest();
110     return;
111   }
112
113   numDrawsThisFrame += tilesPerSide;
114
115   gl.clear(gl.COLOR_BUFFER_BIT);
116
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;
123
124   var offset = new Float32Array(3);
125
126   var drawsDoneThisFrame = 0;
127   for (var yy = 0; yy < tilesPerSide; ++yy) {
128     for (var xx = 0; xx < tilesPerSide; ++xx) {
129       if (drawsDoneThisFrame >= numDrawsThisFrame)
130         break;
131
132       gl.uniformMatrix4fv(transformLoc, false, transformMatrix);
133
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]);
137
138       gl.drawArrays(gl.TRIANGLES, 0, 6);
139       ++drawsDoneThisFrame;
140     }
141
142     if (drawsDoneThisFrame >= numDrawsThisFrame)
143       break;
144   }
145
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");
150   }
151
152   wtu.requestAnimFrame(doNextDraw);
153 }
154
155 wtu.requestAnimFrame(doNextDraw);
156 var successfullyParsed = true;
157 </script>
158 </body>
159 </html>