Skip to content

Commit ca125d8

Browse files
TheBlekKuklin Georgiy
authored andcommitted
Modify webgpu_compute_particles example to utilize indirect compute dispatch
1 parent e1aaac6 commit ca125d8

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

examples/webgpu_compute_particles.html

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@
3131
<script type="module">
3232

3333
import * as THREE from 'three/webgpu';
34-
import { Fn, If, uniform, float, uv, vec3, hash, shapeCircle,
34+
import { Fn, If, Switch, uniform, float, uv, vec2, vec3, hash, shapeCircle, storage,
3535
instancedArray, instanceIndex } from 'three/tsl';
3636

3737
import { Inspector } from 'three/addons/inspector/Inspector.js';
3838

3939
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
4040

41-
const particleCount = 200000;
41+
const particleCount = 200_000;
42+
const movableParticleCount = particleCount / 2;
4243

4344
const gravity = uniform( - .00098 );
4445
const bounce = uniform( .8 );
@@ -186,6 +187,24 @@
186187

187188
//
188189

190+
const movableParticleIndirectBuffer = new THREE.IndirectStorageBufferAttribute(new Uint32Array(3));
191+
const movableParticleBufferNode = storage( movableParticleIndirectBuffer, 'uint' );
192+
193+
const computeMovableCount = Fn( () => {
194+
195+
If( instanceIndex.lessThan( 3 ), () => {
196+
const count = movableParticleBufferNode.element(instanceIndex);
197+
198+
Switch(instanceIndex)
199+
.Case(0, () => count.assign(Math.ceil(movableParticleCount / 64)))
200+
.Case(1, () => count.assign(1))
201+
.Case(2, () => count.assign(1));
202+
})
203+
204+
} )().compute( 3 );
205+
206+
renderer.computeAsync( computeMovableCount );
207+
189208
function onMove( event ) {
190209

191210
if ( isOrbitControlsActive ) return;
@@ -207,7 +226,7 @@
207226

208227
// compute
209228

210-
renderer.compute( computeHit );
229+
renderer.compute( computeHit, movableParticleIndirectBuffer );
211230

212231
}
213232

0 commit comments

Comments
 (0)