Skip to content

Commit c8d45ef

Browse files
committed
workgroup size 64
1 parent bacae73 commit c8d45ef

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

src/pages/particle-life-gpu/bin-fill.wgsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Config {
1313
@group(1) @binding(3) var<storage, read_write> binContents: array<u32>;
1414
@group(2) @binding(0) var<storage, read> particlePositions: array<vec2f>;
1515

16-
@compute @workgroup_size(256)
16+
@compute @workgroup_size(64)
1717
fn clear(
1818
@builtin(global_invocation_id) id: vec3u,
1919
) {
@@ -26,7 +26,7 @@ fn clear(
2626
atomicStore(&binCursor[i], 0u);
2727
}
2828

29-
@compute @workgroup_size(256)
29+
@compute @workgroup_size(64)
3030
fn size(
3131
@builtin(global_invocation_id) id: vec3u,
3232
) {
@@ -43,7 +43,7 @@ fn size(
4343
atomicAdd(&binSize[index], 1u);
4444
}
4545

46-
@compute @workgroup_size(256)
46+
@compute @workgroup_size(64)
4747
fn prepare(
4848
@builtin(global_invocation_id) id: vec3u,
4949
) {
@@ -58,7 +58,7 @@ fn prepare(
5858
binOffset[i] = offset;
5959
}
6060

61-
@compute @workgroup_size(256)
61+
@compute @workgroup_size(64)
6262
fn fill(
6363
@builtin(global_invocation_id) id: vec3u,
6464
) {

src/pages/particle-life-gpu/index.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ async function start(
7979
const width = ctx.canvas.width
8080
const height = ctx.canvas.height
8181

82-
const repulsionRange = 25
82+
const repulsionRange = 35
8383
const attractionRange = 26
8484
const repulsionStrength = 30
8585
const attractionStrength = 30
@@ -90,7 +90,7 @@ async function start(
9090
const toBinX = widthDivisions / width
9191
const toBinY = heightDivisions / height
9292
const binCount = widthDivisions * heightDivisions
93-
// const particleCount = 130_000
93+
// const particleCount = 200_000
9494
const particleCount = 100_000
9595
// const particleCount = 20_000
9696

@@ -331,16 +331,16 @@ async function start(
331331
pass.setBindGroup(0, binConfigBindGroup)
332332
pass.setBindGroup(1, binBindGroup)
333333
pass.setBindGroup(2, particlePositionBindGroup)
334-
pass.dispatchWorkgroups(Math.ceil(binCount / 256))
334+
pass.dispatchWorkgroups(Math.ceil(binCount / 64))
335335

336336
pass.setPipeline(binSizePipeline)
337-
pass.dispatchWorkgroups(Math.ceil(particleCount / 256))
337+
pass.dispatchWorkgroups(Math.ceil(particleCount / 64))
338338

339339
pass.setPipeline(binPreparePipeline)
340-
pass.dispatchWorkgroups(Math.ceil(binCount / 256))
340+
pass.dispatchWorkgroups(Math.ceil(binCount / 64))
341341

342342
pass.setPipeline(binFillPipeline)
343-
pass.dispatchWorkgroups(Math.ceil(particleCount / 256))
343+
pass.dispatchWorkgroups(Math.ceil(particleCount / 64))
344344

345345
pass.end()
346346

@@ -519,7 +519,7 @@ async function start(
519519
pass.setBindGroup(2, updateConfigBindGroup)
520520
pass.setBindGroup(3, updateBinsBindGroup)
521521

522-
pass.dispatchWorkgroups(Math.ceil(particleCount / 256))
522+
pass.dispatchWorkgroups(Math.ceil(particleCount / 64))
523523

524524
pass.end()
525525
const commandBuffer = encoder.finish()
@@ -633,6 +633,7 @@ async function start(
633633
device.queue.submit([commandBuffer])
634634
}
635635

636+
let frameCount = 0
636637
let playing = document.visibilityState === 'visible'
637638
let lastTime = performance.now()
638639
let rafId = requestAnimationFrame(function frame(time) {
@@ -641,8 +642,12 @@ async function start(
641642
if (!playing) return
642643
const dt = time - lastTime
643644
lastTime = time
645+
frameCount++
644646
onFrame(dt)
645-
computeBins()
647+
if (frameCount > 10) {
648+
computeBins()
649+
frameCount = 0
650+
}
646651
updateParticles(dt / 1000)
647652
drawParticles()
648653
})

src/pages/particle-life-gpu/update.wgsl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct Config {
3131

3232
const COLOR_COUNT = 6u;
3333

34-
@compute @workgroup_size(256)
34+
@compute @workgroup_size(64)
3535
fn update(
3636
@builtin(global_invocation_id) id: vec3u,
3737
) {
@@ -57,6 +57,8 @@ fn update(
5757
let half_width = config.width / 2.0;
5858
let half_height = config.height / 2.0;
5959

60+
// var total_count = 0u;
61+
6062
for (var bin_x = min_bin_x; bin_x <= max_bin_x; bin_x++) {
6163
let wrapped_bin_x = (bin_x + wd) % wd;
6264
for (var bin_y = min_bin_y; bin_y <= max_bin_y; bin_y++) {
@@ -99,17 +101,23 @@ fn update(
99101
let other_color = particleColors[other_index];
100102
let interaction_index = color * COLOR_COUNT + other_color;
101103
let interaction_strength = interactions[interaction_index];
104+
// if (interaction_strength > 0.0) {
105+
// total_count += 1u;
106+
// }
102107
let normalized_distance = (config.attractionRange - (dist - config.repulsionRange)) / config.attractionRange;
103108
let symmetric_distance = 1.0 - abs(normalized_distance * 2.0 - 1.0);
104109
let strength = symmetric_distance * interaction_strength * config.attractionStrength * dt;
110+
// if (total_count > 200u) {
111+
// strength = strength * (200.0 / f32(total_count));
112+
// }
105113
velocity += normalize(offset) * strength;
106114
}
107115
}
108116
}
109117
}
110118

111119
// dampen velocity
112-
velocity -= velocity * 8 * dt;
120+
velocity -= vec2f(velocity.x * velocity.x * sign(velocity.x), velocity.y * velocity.y * sign(velocity.y)) * 0.2 * dt;
113121

114122
// update position
115123
position += velocity * dt;

src/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export const ROUTES = {
236236
image: particle_life_gpu_image
237237
},
238238
git: {
239-
lastModified: 1762613219000,
239+
lastModified: 1762619297000,
240240
firstAdded: 1762551884000
241241
},
242242
},

0 commit comments

Comments
 (0)