Skip to content

Commit

Permalink
WebGPURenderer: Update attribute only when needed (#28701)
Browse files Browse the repository at this point in the history
* update attribute only when necessary

* fix interleavedbuffers

* fixing attribute updates might have changed puppeteer?

* support instanceMatrix.needsUpdate

* cleanup

* instanceColor can be undefined
  • Loading branch information
RenaudRohlinger authored Jun 19, 2024
1 parent 6ea0fb9 commit a83652d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
24 changes: 24 additions & 0 deletions examples/jsm/nodes/accessors/InstanceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { normalLocal } from './NormalNode.js';
import { positionLocal } from './PositionNode.js';
import { nodeProxy, vec3, mat3, mat4 } from '../shadernode/ShaderNode.js';
import { DynamicDrawUsage, InstancedInterleavedBuffer, InstancedBufferAttribute } from 'three';
import { NodeUpdateType } from '../core/constants.js';

class InstanceNode extends Node {

Expand All @@ -18,6 +19,11 @@ class InstanceNode extends Node {

this.instanceColorNode = null;

this.updateType = NodeUpdateType.FRAME;

this.buffer = null;
this.bufferColor = null;

}

setup( /*builder*/ ) {
Expand All @@ -31,6 +37,7 @@ class InstanceNode extends Node {
const instanceAttribute = instanceMesh.instanceMatrix;
const buffer = new InstancedInterleavedBuffer( instanceAttribute.array, 16, 1 );

this.buffer = buffer;
const bufferFn = instanceAttribute.usage === DynamicDrawUsage ? instancedDynamicBufferAttribute : instancedBufferAttribute;

const instanceBuffers = [
Expand All @@ -54,6 +61,7 @@ class InstanceNode extends Node {
const buffer = new InstancedBufferAttribute( instanceColorAttribute.array, 3 );
const bufferFn = instanceColorAttribute.usage === DynamicDrawUsage ? instancedDynamicBufferAttribute : instancedBufferAttribute;

this.bufferColor = buffer;
this.instanceColorNode = vec3( bufferFn( buffer, 'vec3', 3, 0 ) );

}
Expand Down Expand Up @@ -85,6 +93,22 @@ class InstanceNode extends Node {

}

update( /*frame*/ ) {

if ( this.instanceMesh.instanceMatrix.version !== this.buffer.version ) {

this.buffer.version = this.instanceMesh.instanceMatrix.version;

}

if ( this.instanceMesh.instanceColor && this.instanceMesh.instanceColor.version !== this.bufferColor.version ) {

this.bufferColor.version = this.instanceMesh.instanceColor.version;

}

}

}

export default InstanceNode;
Expand Down
8 changes: 4 additions & 4 deletions examples/jsm/renderers/common/Geometries.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Geometries extends DataMap {
this.info = info;

this.wireframes = new WeakMap();
this.attributeCall = new WeakMap();
this.attributeVersion = new WeakMap();

}

Expand Down Expand Up @@ -170,13 +170,13 @@ class Geometries extends DataMap {

updateAttribute( attribute, type ) {

const callId = this.info.render.calls;
const attributeData = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;

if ( this.attributeCall.get( attribute ) !== callId ) {
if ( this.attributeVersion.get( attribute ) !== attributeData.version ) {

this.attributes.update( attribute, type );

this.attributeCall.set( attribute, callId );
this.attributeVersion.set( attribute, attributeData.version );

}

Expand Down
Binary file modified examples/screenshots/webgpu_instance_mesh.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions examples/webgpu_instance_mesh.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@

}

mesh.instanceMatrix.needsUpdate = true;

}

await renderer.render( scene, camera );
Expand Down

0 comments on commit a83652d

Please sign in to comment.