Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renderer: Added sync .compute() support #29547

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/webgpu_compute_geometry.html
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@

async function animate() {

if ( computeUpdate ) await renderer.computeAsync( computeUpdate );
if ( computeUpdate ) renderer.compute( computeUpdate );

renderer.render( scene, camera );

Expand Down
4 changes: 2 additions & 2 deletions examples/webgpu_compute_particles.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@

//

renderer.compute( computeInit );
renderer.computeAsync( computeInit );

// click event

Expand Down Expand Up @@ -219,7 +219,7 @@

// compute

renderer.compute( computeHit );
renderer.computeAsync( computeHit );

}

Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_compute_particles_rain.html
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@

//

renderer.compute( computeInit );
renderer.computeAsync( computeInit );

//

Expand Down
6 changes: 3 additions & 3 deletions examples/webgpu_compute_points.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
// compute

computeNode = computeShaderFn().compute( particleNum );
computeNode.onInit = ( { renderer } ) => {
computeNode.onInit( ( { renderer } ) => {

const precomputeShaderNode = Fn( () => {

Expand All @@ -101,9 +101,9 @@

} );

renderer.compute( precomputeShaderNode().compute( particleNum ) );
renderer.computeAsync( precomputeShaderNode().compute( particleNum ) );

};
} );

// use a compute shader to animate the point cloud's vertex data.

Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_compute_texture.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
document.body.appendChild( renderer.domElement );

// compute texture
renderer.compute( computeNode );
renderer.computeAsync( computeNode );

window.addEventListener( 'resize', onWindowResize );

Expand Down
6 changes: 3 additions & 3 deletions examples/webgpu_compute_water.html
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@
const buttonCompute = {
smoothWater: function () {

renderer.compute( computeSmooth );
renderer.computeAsync( computeSmooth );

}
};
Expand Down Expand Up @@ -488,11 +488,11 @@

}

renderer.compute( computeHeight );
renderer.computeAsync( computeHeight );

if ( effectController.spheresEnabled ) {

renderer.compute( computeSphere );
renderer.computeAsync( computeSphere );

}

Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_tsl_compute_attractors_particles.html
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@

const reset = () => {

renderer.compute( initCompute );
renderer.computeAsync( initCompute );

};

Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_tsl_vfx_linkedparticles.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
const particleVelocities = storage( new THREE.StorageInstancedBufferAttribute( nbParticles, 4 ), 'vec4', nbParticles );

// init particles buffers
renderer.compute( /*#__PURE__*/ Fn( () => {
renderer.computeAsync( /*#__PURE__*/ Fn( () => {

particlePositions.element( instanceIndex ).xyz.assign( vec3( 10000.0 ) );
particlePositions.element( instanceIndex ).w.assign( vec3( - 1.0 ) ); // life is stored in w component; x<0 means dead
Expand Down
10 changes: 9 additions & 1 deletion src/nodes/gpgpu/ComputeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ComputeNode extends Node {
this.version = 1;
this.updateBeforeType = NodeUpdateType.OBJECT;

this.onInitFunction = null;

this.updateDispatchCount();

}
Expand Down Expand Up @@ -54,7 +56,13 @@ class ComputeNode extends Node {

}

onInit() { }
onInit( callback ) {

this.onInitFunction = callback;

return this;

}

updateBefore( { renderer } ) {

Expand Down
38 changes: 27 additions & 11 deletions src/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1149,9 +1149,17 @@ class Renderer {

}

async computeAsync( computeNodes ) {
compute( computeNodes ) {

if ( this._initialized === false ) await this.init();
if ( this._initialized === false ) {

console.warn( 'THREE.Renderer: .compute() called before the backend is initialized. Try using .computeAsync() instead.' );

return this.computeAsync( computeNodes );

}

//

const nodeFrame = this._nodes.nodeFrame;

Expand Down Expand Up @@ -1202,7 +1210,13 @@ class Renderer {

//

computeNode.onInit( { renderer: this } );
const onInitFn = computeNode.onInitFunction;

if ( onInitFn !== null ) {

onInitFn.call( computeNode, { renderer: this } );

}

}

Expand All @@ -1218,14 +1232,22 @@ class Renderer {

backend.finishCompute( computeNodes );

await this.backend.resolveTimestampAsync( computeNodes, 'compute' );

//

nodeFrame.renderId = previousRenderId;

}

async computeAsync( computeNodes ) {

if ( this._initialized === false ) await this.init();

this.compute( computeNodes );

await this.backend.resolveTimestampAsync( computeNodes, 'compute' );

}

async hasFeatureAsync( name ) {

if ( this._initialized === false ) await this.init();
Expand Down Expand Up @@ -1633,12 +1655,6 @@ class Renderer {

}

get compute() {

return this.computeAsync;

}

get compile() {

return this.compileAsync;
Expand Down