Repository navigation
Expand file tree
/
Copy pathgpu-test.js
More file actions
464 lines (379 loc) · 18.1 KB
/
Copy pathgpu-test.js
File metadata and controls
464 lines (379 loc) · 18.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// GPU Benchmark with Particle System and Shader Effects
class ParticleRenderer {
constructor(canvas) {
this.canvas = canvas;
this.gl = canvas.getContext('webgl', {
antialias: false,
powerPreference: "high-performance"
}) || canvas.getContext('experimental-webgl', {
antialias: false,
powerPreference: "high-performance"
});
if (!this.gl) {
alert('WebGL not supported in your browser');
return;
}
this.time = 0;
this.particleCount = 1000000; // Maximum particle count for extreme GPU stress
this.mode = 0; // Different visual modes
this.initWebGL();
this.initPostProcessing();
this.resize();
window.addEventListener('resize', () => this.resize());
}
initWebGL() {
const gl = this.gl;
// Vertex shader for particles
const vsSource = `
attribute vec2 aPosition;
attribute float aSpeed;
attribute float aPhase;
uniform float uTime;
uniform vec2 uResolution;
uniform float uMode;
varying vec4 vColor;
void main() {
vec2 pos = aPosition;
// Multiple complex motion calculations for extreme GPU stress
float angle = aPhase + uTime * aSpeed;
float radius = 0.3 + sin(uTime * 0.5 + aPhase) * 0.2;
// Add extra motion layers
float wave1 = sin(uTime * 1.5 + aPhase * 3.0) * 0.1;
float wave2 = cos(uTime * 2.3 + aPhase * 5.0) * 0.08;
float wave3 = sin(uTime * 0.7 + aPhase * 7.0) * 0.06;
if(uMode < 1.0) {
// Complex spiral pattern with multiple layers
float spiral1 = cos(angle) * radius * (1.0 + sin(uTime * 0.3 + aPhase) * 0.5);
float spiral2 = sin(angle) * radius * (1.0 + cos(uTime * 0.3 + aPhase) * 0.5);
pos.x = spiral1 + wave1 * cos(uTime + aPhase);
pos.y = spiral2 + wave2 * sin(uTime + aPhase);
pos += vec2(wave3 * sin(uTime * 3.0), wave3 * cos(uTime * 3.0));
} else if(uMode < 2.0) {
// Complex wave pattern with multiple frequencies
pos.x = aPosition.x + wave1;
pos.y = aPosition.y + sin(aPosition.x * 10.0 + uTime * 2.0) * 0.3;
pos.y += cos(aPosition.x * 15.0 - uTime * 3.0) * 0.2;
pos.x += sin(aPosition.y * 8.0 + uTime * 1.5) * 0.15;
pos += vec2(wave2, wave3);
} else {
// Complex vortex pattern with multiple rotations
float dist = length(aPosition);
pos = aPosition * (1.0 + sin(uTime + dist * 5.0) * 0.3);
// Multiple rotation layers
float rotAngle1 = uTime * aSpeed + dist * 3.0;
float rotAngle2 = uTime * aSpeed * 0.5 - dist * 2.0;
float c1 = cos(rotAngle1);
float s1 = sin(rotAngle1);
pos = vec2(pos.x * c1 - pos.y * s1, pos.x * s1 + pos.y * c1);
float c2 = cos(rotAngle2);
float s2 = sin(rotAngle2);
pos = vec2(pos.x * c2 - pos.y * s2, pos.x * s2 + pos.y * c2) * 0.8;
pos += vec2(wave1 * cos(dist * 5.0), wave2 * sin(dist * 5.0));
}
gl_Position = vec4(pos, 0.0, 1.0);
gl_PointSize = 2.0 + sin(uTime * 2.0 + aPhase) * 1.0 + cos(uTime * 3.0 + aPhase * 2.0) * 0.5;
// Color based on position and time
float hue = fract(aPhase + uTime * 0.1);
vColor = vec4(
0.5 + 0.5 * sin(hue * 6.28),
0.5 + 0.5 * sin(hue * 6.28 + 2.09),
0.5 + 0.5 * sin(hue * 6.28 + 4.18),
0.8
);
}
`;
// Fragment shader with complex calculations
const fsSource = `
precision highp float;
uniform float uTime;
uniform vec2 uResolution;
varying vec4 vColor;
// Complex shader calculations to stress GPU
float noise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
// More complex noise for extreme GPU work
float fbm(vec2 p) {
float value = 0.0;
float amplitude = 0.5;
for(int i = 0; i < 8; i++) { // Doubled iterations for more complexity
value += amplitude * noise(p);
p *= 2.0;
amplitude *= 0.5;
}
return value;
}
// Additional complex noise layer
float turbulence(vec2 p) {
float t = 0.0;
float f = 1.0;
for(int i = 0; i < 6; i++) {
t += abs(noise(p * f)) / f;
f *= 2.0;
}
return t;
}
void main() {
vec2 center = gl_PointCoord - 0.5;
float dist = length(center);
// Multiple layers of effects with extra calculations
float pulse = sin(uTime * 5.0 + dist * 20.0) * 0.5 + 0.5;
float glow = 1.0 - smoothstep(0.0, 0.5, dist);
glow = pow(glow, 2.0);
// Add multiple layers of fractal noise for extreme complexity
float n = fbm(gl_FragCoord.xy * 0.005 + uTime * 0.1);
float t = turbulence(gl_FragCoord.xy * 0.003 - uTime * 0.05);
float n2 = fbm(gl_FragCoord.xy * 0.01 + vec2(cos(uTime * 0.2), sin(uTime * 0.2)));
// Very complex color mixing with multiple layers
vec3 color = vColor.rgb * glow * (0.7 + pulse * 0.3);
color = mix(color, vec3(n), 0.2);
color = mix(color, vec3(t * 0.5 + 0.5), 0.15);
color += vec3(sin(uTime + gl_FragCoord.x * 0.01)) * 0.1;
color += vec3(cos(uTime * 0.5 + gl_FragCoord.y * 0.01)) * 0.1;
color *= 1.0 + n2 * 0.3;
// Multiple edge calculations for more GPU work
float edge = smoothstep(0.3, 0.5, dist);
float edge2 = smoothstep(0.1, 0.3, dist);
color *= (1.0 - edge * 0.5);
color += vec3(edge2 * pulse * 0.2);
gl_FragColor = vec4(color, vColor.a * glow);
}
`;
// Compile shaders
const vertexShader = this.compileShader(vsSource, gl.VERTEX_SHADER);
const fragmentShader = this.compileShader(fsSource, gl.FRAGMENT_SHADER);
// Create program
this.program = gl.createProgram();
gl.attachShader(this.program, vertexShader);
gl.attachShader(this.program, fragmentShader);
gl.linkProgram(this.program);
if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) {
console.error('Unable to initialize shader program');
return;
}
// Generate particle data
const positions = [];
const speeds = [];
const phases = [];
for (let i = 0; i < this.particleCount; i++) {
// Random positions in circle
const angle = Math.random() * Math.PI * 2;
const radius = Math.random();
positions.push(Math.cos(angle) * radius, Math.sin(angle) * radius);
speeds.push(0.5 + Math.random() * 1.5);
phases.push(Math.random() * Math.PI * 2);
}
// Create buffers
this.positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
this.speedBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.speedBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(speeds), gl.STATIC_DRAW);
this.phaseBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.phaseBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(phases), gl.STATIC_DRAW);
// Get locations
this.attribLocations = {
position: gl.getAttribLocation(this.program, 'aPosition'),
speed: gl.getAttribLocation(this.program, 'aSpeed'),
phase: gl.getAttribLocation(this.program, 'aPhase'),
};
this.uniformLocations = {
time: gl.getUniformLocation(this.program, 'uTime'),
resolution: gl.getUniformLocation(this.program, 'uResolution'),
mode: gl.getUniformLocation(this.program, 'uMode'),
};
// Enable blending for nice particle effects
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
}
compileShader(source, type) {
const gl = this.gl;
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error('Shader compilation error:', gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
initPostProcessing() {
const gl = this.gl;
// Create framebuffer for render-to-texture
this.framebuffer = gl.createFramebuffer();
this.renderTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this.renderTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.canvas.width, this.canvas.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_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);
// Second texture for ping-pong rendering
this.renderTexture2 = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this.renderTexture2);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.canvas.width, this.canvas.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_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);
// Framebuffer 1
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.renderTexture, 0);
// Framebuffer 2 for ping-pong
this.framebuffer2 = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer2);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.renderTexture2, 0);
// Post-processing shader with heavy effects
const postVS = `
attribute vec2 aPosition;
varying vec2 vTexCoord;
void main() {
vTexCoord = aPosition * 0.5 + 0.5;
gl_Position = vec4(aPosition, 0.0, 1.0);
}
`;
const postFS = `
precision highp float;
uniform sampler2D uTexture;
uniform float uTime;
uniform vec2 uResolution;
varying vec2 vTexCoord;
// Heavy blur with multiple samples
vec4 blur(sampler2D tex, vec2 uv, float intensity) {
vec4 color = vec4(0.0);
float total = 0.0;
for(float x = -4.0; x <= 4.0; x += 1.0) {
for(float y = -4.0; y <= 4.0; y += 1.0) {
vec2 offset = vec2(x, y) * intensity / uResolution;
float weight = 1.0 / (1.0 + length(vec2(x, y)));
color += texture2D(tex, uv + offset) * weight;
total += weight;
}
}
return color / total;
}
// Chromatic aberration
vec4 chromaticAberration(sampler2D tex, vec2 uv, float amount) {
vec2 direction = uv - 0.5;
float r = texture2D(tex, uv + direction * amount * 0.01).r;
float g = texture2D(tex, uv).g;
float b = texture2D(tex, uv - direction * amount * 0.01).b;
return vec4(r, g, b, 1.0);
}
// Bloom effect
vec4 bloom(sampler2D tex, vec2 uv) {
vec4 color = texture2D(tex, uv);
vec4 blurred = blur(tex, uv, 3.0);
return color + blurred * 0.5;
}
void main() {
vec2 uv = vTexCoord;
// Apply chromatic aberration
vec4 color = chromaticAberration(uTexture, uv, sin(uTime * 0.5) * 2.0 + 2.0);
// Heavy bloom with multiple passes
vec4 bloom1 = blur(uTexture, uv, 2.0);
vec4 bloom2 = blur(uTexture, uv, 4.0);
vec4 bloom3 = blur(uTexture, uv, 8.0);
color += bloom1 * 0.4;
color += bloom2 * 0.3;
color += bloom3 * 0.2;
// Vignette
float dist = length(uv - 0.5);
color *= 1.0 - dist * 0.5;
// Color grading
color.rgb = pow(color.rgb, vec3(0.9));
color.rgb *= 1.2;
gl_FragColor = color;
}
`;
const postVertexShader = this.compileShader(postVS, gl.VERTEX_SHADER);
const postFragmentShader = this.compileShader(postFS, gl.FRAGMENT_SHADER);
this.postProgram = gl.createProgram();
gl.attachShader(this.postProgram, postVertexShader);
gl.attachShader(this.postProgram, postFragmentShader);
gl.linkProgram(this.postProgram);
// Full screen quad
const quadVertices = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]);
this.quadBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.quadBuffer);
gl.bufferData(gl.ARRAY_BUFFER, quadVertices, gl.STATIC_DRAW);
this.postAttribLocation = gl.getAttribLocation(this.postProgram, 'aPosition');
this.postUniformLocations = {
texture: gl.getUniformLocation(this.postProgram, 'uTexture'),
time: gl.getUniformLocation(this.postProgram, 'uTime'),
resolution: gl.getUniformLocation(this.postProgram, 'uResolution')
};
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
resize() {
const displayWidth = this.canvas.clientWidth;
const displayHeight = this.canvas.clientHeight;
if (this.canvas.width !== displayWidth || this.canvas.height !== displayHeight) {
this.canvas.width = displayWidth;
this.canvas.height = displayHeight;
this.gl.viewport(0, 0, displayWidth, displayHeight);
// Resize framebuffer textures
const gl = this.gl;
gl.bindTexture(gl.TEXTURE_2D, this.renderTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, displayWidth, displayHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.bindTexture(gl.TEXTURE_2D, this.renderTexture2);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, displayWidth, displayHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
}
}
render() {
const gl = this.gl;
// PASS 1: Render particles to framebuffer
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
gl.viewport(0, 0, this.canvas.width, this.canvas.height);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Re-enable blending for particles
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
gl.useProgram(this.program);
// Bind position buffer
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
gl.vertexAttribPointer(this.attribLocations.position, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(this.attribLocations.position);
// Bind speed buffer
gl.bindBuffer(gl.ARRAY_BUFFER, this.speedBuffer);
gl.vertexAttribPointer(this.attribLocations.speed, 1, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(this.attribLocations.speed);
// Bind phase buffer
gl.bindBuffer(gl.ARRAY_BUFFER, this.phaseBuffer);
gl.vertexAttribPointer(this.attribLocations.phase, 1, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(this.attribLocations.phase);
// Set uniforms
gl.uniform1f(this.uniformLocations.time, this.time);
gl.uniform2f(this.uniformLocations.resolution, this.canvas.width, this.canvas.height);
gl.uniform1f(this.uniformLocations.mode, this.mode);
// Draw particles
gl.drawArrays(gl.POINTS, 0, this.particleCount);
// PASS 2: Apply post-processing effects (render to screen)
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.viewport(0, 0, this.canvas.width, this.canvas.height);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(this.postProgram);
// Bind the rendered texture
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, this.renderTexture);
gl.uniform1i(this.postUniformLocations.texture, 0);
gl.uniform1f(this.postUniformLocations.time, this.time);
gl.uniform2f(this.postUniformLocations.resolution, this.canvas.width, this.canvas.height);
// Draw full screen quad
gl.bindBuffer(gl.ARRAY_BUFFER, this.quadBuffer);
gl.vertexAttribPointer(this.postAttribLocation, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(this.postAttribLocation);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
// Disable blending for post-processing
gl.disable(gl.BLEND);
}
reset() {
this.time = 0;
this.mode = 0;
}
}