-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage_stress_test.html
327 lines (269 loc) · 10.3 KB
/
image_stress_test.html
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!-- vim: sw=4 ts=4 expandtab smartindent ft=javascript
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WebGL Demo</title>
<style>
document, body {
margin: 0px; padding: 0px;
overflow: hidden; position: relative;
}
button {
font-size: 2rem;
}
@media (pointer: coarse) {
button {
font-size: 6rem;
margin: 2rem;
}
h1 {
font-size: 3rem;
}
}
</style>
</head>
<body>
<canvas id="glcanvas"></canvas>
<div style="position:absolute;bottom:0px;left:0px;margin:0.2rem;">
<button id="plus_hundred"> +100 </button>
<button id="plus_thousand"> +1000 </button>
</div>
<div style="position:absolute;top:0px;left:0px;margin:0.2rem;">
<h1 id="image_count" style="font-family: monospace;"> 10 images </h1>
<h1 id="fps_count" style="font-family: monospace;"> 60 fps </h1>
</div>
<script>
const canvas = document.getElementById('glcanvas');
const gl = canvas.getContext('webgl2', {antialias: true});
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
if (!gl) { alert('Failed to initialize WebGL'); }
(window.onresize = () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
/* account for e.g. high-retina macbook screens */
if (window.devicePixelRatio > 1) {
canvas.style.width = `${canvas.width}px`;
canvas.style.height = `${canvas.height}px`;
canvas.width *= window.devicePixelRatio;
canvas.height *= window.devicePixelRatio;
}
gl.viewport(
0,
0,
canvas.width,
canvas.height
);
})();
const geo_vbuf_pos = gl.createBuffer();
const geo_vbuf_uv = gl.createBuffer();
const geo_ibuf = gl.createBuffer();
let shaders;
/* compile shaders */
{
const vs_geo = `#version 300 es
in vec3 a_pos;
in vec2 a_uv;
out vec2 v_uv;
void main() {
gl_Position = vec4(a_pos.xyz, 1);
v_uv = a_uv;
}`;
const fs_geo = `#version 300 es
#define PI 3.1415926538
precision mediump float;
uniform sampler2D u_texture;
uniform float u_time;
uniform int u_id;
in vec2 v_uv;
out vec4 a_color;
const uint k = 1103515245U; // GLIB C
vec3 hash33( uvec3 x ) {
x = ((x>>8U)^x.yzx)*k;
x = ((x>>8U)^x.yzx)*k;
x = ((x>>8U)^x.yzx)*k;
return vec3(x)*(1.0/float(0xffffffffU));
}
void main() {
float cycle = 0.0005;
float t = smoothstep(0.0, 1.0, mod(u_time * cycle, 1.0));
int special = int(round(hash33(uvec3(u_id, 100 + int(u_time * cycle), 0)).x - 0.46));
vec2 uv = v_uv;
uv.x -= 0.5;
uv.x *= 2.0;
uv.x /= (special == 1) ? -cos(t*PI*2.0 + PI) : 1.0;
uv.x *= 0.5;
uv.x += 0.5;
a_color = texture(u_texture, uv);
}`;
function createProgram(gl, vertexSource, fragmentSource) {
function createShader(gl, type, source) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw new Error(gl.getShaderInfoLog(shader));
}
return shader;
}
const program = gl.createProgram();
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSource);
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSource);
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw new Error(gl.getProgramInfoLog(program));
}
const wrapper = {program};
const numAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (let i = 0; i < numAttributes; i++) {
const attribute = gl.getActiveAttrib(program, i);
wrapper[attribute.name] = gl.getAttribLocation(program, attribute.name);
}
const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < numUniforms; i++) {
const uniform = gl.getActiveUniform(program, i);
wrapper[uniform.name] = gl.getUniformLocation(program, uniform.name);
}
return wrapper;
}
shaders = {
geo: createProgram(gl, vs_geo, fs_geo)
}
}
const makeTexture = () => {
let atlas = gl.createTexture();
{
const emojis = [
"😀", "😃", "😄", "😁", "😆", "😅", "😂", "🤣", "😜", "😝", "😛", "🤑", "🤗", "🤩", "😍", "🥰", "😘",
"😗", "😙", "😚", "🥺", "😋", "😎", "🥳", "🤔", "🤨", "😐", "😑", "😶", "🙄", "😯", "😦", "😧", "😮",
"😲", "😳", "🥴", "😵", "🤐", "😷", "🤒", "🤕", "🤢", "🤮", "🤧", "😇", "🤠", "🥸", "🧐", "😈", "👿",
"💀", "☠️", "👻", "👽", "🤖", "🎃", "😺", "😸", "😹", "😻", "😼", "😽", "🙀", "😿", "😾", "🐶", "🐱",
"🦁", "🐯", "🐅", "🦊", "🐴", "🦄", "🦓", "🦒", "🐮", "🐷", "🐗", "🦢", "🦩", "🦜", "🐦", "🦋", "🐝",
"🐞", "🐜", "🐌", "🦗", "🕷", "🕸", "🦠", "🐢", "🐍", "🦎", "🐉", "🦖"
];
const canvas = document.createElement("canvas");
const SIZE = canvas.width = canvas.height = 1024;
const ctx = canvas.getContext("2d");
const PAD = SIZE*0.05;
ctx.font = (SIZE - PAD*2) + 'px sans-serif';
ctx.fillText(emojis[Math.floor(emojis.length * Math.random())], PAD, SIZE - PAD*2);
gl.bindTexture(gl.TEXTURE_2D, atlas);
gl.texImage2D(
/* target */ gl.TEXTURE_2D,
/* level */ 0,
/* internalformat */ gl.RGBA,
/* width */ canvas.width,
/* height */ canvas.height,
/* border, */ 0,
/* format, */ gl.RGBA,
/* type, */ gl.UNSIGNED_BYTE,
/* data */ canvas
);
// gl.generateMipmap(gl.TEXTURE_2D);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
}
return atlas;
}
const textures = [];
for (let i = 0; i < 10; i++) add_tex();
function add_tex() {
document.title = (textures.length + 1) + ' 1024p textures!';
image_count.textContent = (textures.length + 1) + ' 1024p textures!';
textures.push(makeTexture());
}
plus_hundred .onclick = () => { for (let i = 0; i < 100; i++) add_tex(); }
plus_thousand.onclick = () => { for (let i = 0; i < 1000; i++) add_tex(); }
let addInterval;
canvas.onpointerdown = e => {
clearInterval(addInterval);
addInterval = setInterval(() => add_tex(), 50);
e.preventDefault();
};
canvas.oncontextmenu = e => {
e.preventDefault();
return false;
}
canvas.onpointerup = e => clearInterval(addInterval);
let last_timestamp;
let dt_avg = 1000 / 60.5;
requestAnimationFrame(function frame(timestamp) {
requestAnimationFrame(frame);
last_timestamp ??= timestamp;
const dt = timestamp - last_timestamp;
last_timestamp = timestamp;
dt_avg = dt_avg * 0.99 + dt * 0.01;
fps_count.textContent = (1000 / dt_avg).toFixed(1) + ' fps';
/* set up premultiplied alpha */
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.BLEND);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
/* clear all */
gl.clearColor(1, 1, 1, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
const tlen = Math.floor(Math.sqrt(textures.length));
let size = 0.5;
if (size * tlen > 1) {
size = 1 / (tlen + 1);
}
for (let i = 0; i < textures.length; i++) {
const geo_idx = [];
const geo_pos = [];
const geo_uv = [];
const atlas = textures[i];
const tile_x = i % tlen ;
const tile_y = Math.floor(i / tlen);
const x = 2 * (tile_x * size) - 1;
const y = 2 * (tile_y * size) - 1;
{
geo_uv.push(
1, 1,
0, 1,
0, 0,
1, 0,
);
const vbuf_i = geo_pos.length / 3;
const ar = window.innerWidth / window.innerHeight;
geo_pos.push((x + size) * 0.8, (y + -size*ar) * 0.8, 0.1);
geo_pos.push((x + -size) * 0.8, (y + -size*ar) * 0.8, 0.1);
geo_pos.push((x + -size) * 0.8, (y + size*ar) * 0.8, 0.1);
geo_pos.push((x + size) * 0.8, (y + size*ar) * 0.8, 0.1);
geo_idx.push(0, 1, 2, 2, 3, 0);
}
/* geo pass */
{
gl.useProgram(shaders.geo.program);
gl.enableVertexAttribArray(shaders.geo.a_pos);
gl.enableVertexAttribArray(shaders.geo.a_uv);
/* upload/bind atlas */
{
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, atlas);
gl.uniform1i(shaders.geo.u_texture, 0);
}
/* upload/bind geometry */
{
gl.bindBuffer(gl.ARRAY_BUFFER, geo_vbuf_pos);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geo_pos), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.geo.a_pos, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, geo_vbuf_uv);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geo_uv), gl.STATIC_DRAW);
gl.vertexAttribPointer(shaders.geo.a_uv, 2, gl.FLOAT, false, 0, 0);
gl.uniform1f(shaders.geo.u_time, timestamp);
gl.uniform1i(shaders.geo.u_id, i);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, geo_ibuf);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(geo_idx), gl.STATIC_DRAW);
}
gl.drawElements(gl.TRIANGLES, geo_idx.length, gl.UNSIGNED_SHORT, 0);
}
}
})
</script>
</body>
</html>