1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
//----------------------------------------------------------
// helper Globals
//----------------------------------------------------------
var shaderProgram;
var gl;
var fps=0;
var fpsmax=0;
var timecount=0;
var fpssum=0;
var fpscount=1;
var benchtime=0;
var username="";
//----------------------------------------------------------
// getShader collects shader and compiles it. Errors are printed in alert
//----------------------------------------------------------
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
//----------------------------------------------------------
// Matrix stack variables
//----------------------------------------------------------
var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();
//----------------------------------------------------------
// Push Matrix
//----------------------------------------------------------
function mvPushMatrix() {
var copy = mat4.create();
mat4.set(mvMatrix, copy);
mvMatrixStack.push(copy);
}
//----------------------------------------------------------
// Pop Matrix
//----------------------------------------------------------
function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}
//----------------------------------------------------------
// Startup webGL and alert if not available
//----------------------------------------------------------
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}if (!gl){
alert("Could not initialise WebGL, sorry :-(");
}
}
//----------------------------------------------------------
// Measure fps and update div if available
//----------------------------------------------------------
function updatefps(elapsed,fpsdiv)
{
if(!isNaN(elapsed)){
timecount+=(elapsed/1000.0);
fpssum+=(elapsed/10.0);
}
if(elapsed<1) elapsed=1;
fpscount++;
fps=Math.round((100.0/(fpssum/fpscount))*10.0)/10.0;
if(fps>fpsmax&&timecount>5.0) fpsmax=fps;
fpsd=document.getElementById(fpsdiv);
if(fpsd!=null) fpsd.innerHTML=fps;
}
//----------------------------------------------------------
// Sends benchmark every X seconds
//----------------------------------------------------------
function sendbenchmark(delay,app)
{
// Every 8 seconds send benchmark data to server!
if(Math.abs(timecount-benchtime)>delay){
benchtime=timecount;
Benchmark(username,app,fps,fpsmax,Math.round(timecount*10.0)/10.0);
}
}
//----------------------------------------------------------
// Generate benchmark userID
//----------------------------------------------------------
function genID(app)
{
username = localStorage.getItem("Benchuser"); //Try to fetch username data
if (username==null||username==""){
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
text="";
for( var i=0; i < 16; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length));
username=text;
localStorage.setItem("Benchuser",username);
}else{
}
Benchmark(username,app,fps,fpsmax,Math.round(timecount*10.0)/10.0);
}
//----------------------------------------------------------
// Send Benchmark using httpajax.js
//----------------------------------------------------------
function Benchmark(username,app,fps,maxfps,runtime)
{
var paramstr='User='+escape(username);
paramstr+="&App="+escape(app);
paramstr+="&Fps="+escape(fps);
paramstr+="&MaxFps="+escape(maxfps);
paramstr+="&RunTime="+escape(runtime);
AjaxService("Benchy.php",paramstr);
}
|