Skip to content

Commit

Permalink
Renderer Call ID
Browse files Browse the repository at this point in the history
  • Loading branch information
sunag committed Nov 2, 2023
1 parent 79cc152 commit 5e9b96c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 51 deletions.
26 changes: 6 additions & 20 deletions examples/jsm/renderers/common/Animation.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
class Animation {

constructor() {
constructor( nodes ) {

this.nodes = null;
this.nodes = nodes;

this.animationLoop = null;
this.requestId = null;

this.isAnimating = false;

this.context = self;
this._init();

}

start() {

if ( this.isAnimating === true || this.animationLoop === null || this.nodes === null ) return;

this.isAnimating = true;
_init() {

const update = ( time, frame ) => {

this.requestId = self.requestAnimationFrame( update );

this.nodes.nodeFrame.update();

this.animationLoop( time, frame );
if ( this.animationLoop !== null ) this.animationLoop( time, frame );

};

this.requestId = self.requestAnimationFrame( update );

}

stop() {
dispose() {

self.cancelAnimationFrame( this.requestId );

this.isAnimating = false;

}

setAnimationLoop( callback ) {
Expand All @@ -47,12 +39,6 @@ class Animation {

}

setNodes( nodes ) {

this.nodes = nodes;

}

}

export default Animation;
6 changes: 3 additions & 3 deletions examples/jsm/renderers/common/Bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ class Bindings extends DataMap {
const { backend } = this;

const updateMap = this.updateMap;
const frame = this.info.render.frame;
const callId = this.info.call;

let needsBindingsUpdate = false;

// iterate over all bindings and check if buffer updates or a new binding group is required

for ( const binding of bindings ) {

const isUpdated = updateMap.get( binding ) === frame;
const isUpdated = updateMap.get( binding ) === callId;

if ( isUpdated ) continue;

Expand Down Expand Up @@ -137,7 +137,7 @@ class Bindings extends DataMap {

}

updateMap.set( binding, frame );
updateMap.set( binding, callId );

}

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.attributeFrame = new WeakMap();
this.attributeCall = new WeakMap();

}

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

updateAttribute( attribute, type ) {

const frame = this.info.render.frame;
const callId = this.info.render.call;

if ( this.attributeFrame.get( attribute ) !== frame ) {
if ( this.attributeCall.get( attribute ) !== callId ) {

this.attributes.update( attribute, type );

this.attributeFrame.set( attribute, frame );
this.attributeCall.set( attribute, callId );

}

Expand Down
13 changes: 11 additions & 2 deletions examples/jsm/renderers/common/Info.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ class Info {

this.autoReset = true;

this.call = 0;

this.render = {
frame: 0,
call: 0,
drawCalls: 0,
triangles: 0,
points: 0,
lines: 0
};

this.compute = {
call: 0
};

this.memory = {
geometries: 0,
textures: 0
Expand Down Expand Up @@ -60,7 +66,10 @@ class Info {

this.reset();

this.render.frame = 0;
this.call = 0;

this.render.call = 0;
this.compute.call = 0;

this.memory.geometries = 0;
this.memory.textures = 0;
Expand Down
42 changes: 26 additions & 16 deletions examples/jsm/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Renderer {
this._attributes = null;
this._geometries = null;
this._nodes = null;
this._animation = null;
this._bindings = null;
this._objects = null;
this._pipelines = null;
Expand All @@ -70,8 +71,6 @@ class Renderer {
this._textures = null;
this._background = null;

this._animation = new Animation();

this._currentRenderContext = null;
this._lastRenderContext = null;

Expand Down Expand Up @@ -136,6 +135,7 @@ class Renderer {
}

this._nodes = new Nodes( this, backend );
this._animation = new Animation( this._nodes );
this._attributes = new Attributes( backend );
this._background = new Background( this, this._nodes );
this._geometries = new Geometries( this._attributes, this.info );
Expand All @@ -148,9 +148,6 @@ class Renderer {

//

this._animation.setNodes( this._nodes );
this._animation.start();

this._initialized = true;

resolve();
Expand Down Expand Up @@ -197,8 +194,14 @@ class Renderer {
this._currentRenderContext = renderContext;
this._currentRenderObjectFunction = this._renderObjectFunction || this.renderObject;

//

nodeFrame.renderId ++;

this.info.call ++;

this.info.render.call ++;

//

const coordinateSystem = this.coordinateSystem;
Expand All @@ -213,16 +216,12 @@ class Renderer {

//

if ( this._animation.isAnimating === false ) nodeFrame.update();

if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();

if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();

if ( this.info.autoReset === true ) this.info.reset();

this.info.render.frame ++;

//

let viewport = this._viewport;
Expand Down Expand Up @@ -361,15 +360,11 @@ class Renderer {

}

setAnimationLoop( callback ) {

if ( this._initialized === false ) this.init();
async setAnimationLoop( callback ) {

const animation = this._animation;

animation.setAnimationLoop( callback );
if ( this._initialized === false ) await this.init();

( callback === null ) ? animation.stop() : animation.start();
this._animation.setAnimationLoop( callback );

}

Expand Down Expand Up @@ -621,6 +616,7 @@ class Renderer {

this.info.dispose();

this._animation.dispose();
this._objects.dispose();
this._properties.dispose();
this._pipelines.dispose();
Expand Down Expand Up @@ -665,12 +661,26 @@ class Renderer {

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

//

this.info.call ++;

this.info.compute.call ++;

//

const backend = this.backend;
const pipelines = this._pipelines;
const bindings = this._bindings;
const nodes = this._nodes;
const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];

if ( computeList[ 0 ] === undefined || computeList[ 0 ].isComputeNode !== true ) {

throw new Error( 'THREE.Renderer: .compute() expects a ComputeNode.' );

}

backend.beginCompute( computeNodes );

for ( const computeNode of computeList ) {
Expand Down
12 changes: 6 additions & 6 deletions examples/jsm/renderers/common/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Nodes extends DataMap {
this.backend = backend;
this.nodeFrame = new NodeFrame();
this.nodeBuilderCache = new Map();
this.frameHashCache = new ChainMap();
this.callHashCache = new ChainMap();

}

Expand Down Expand Up @@ -148,11 +148,11 @@ class Nodes extends DataMap {
getCacheKey( scene, lightsNode ) {

const chain = [ scene, lightsNode ];
const frameId = this.nodeFrame.frameId;
const callId = this.renderer.info.call;

let cacheKeyData = this.frameHashCache.get( chain );
let cacheKeyData = this.callHashCache.get( chain );

if ( cacheKeyData === undefined || cacheKeyData.frameId !== frameId ) {
if ( cacheKeyData === undefined || cacheKeyData.callId !== callId ) {

const environmentNode = this.getEnvironmentNode( scene );
const fogNode = this.getFogNode( scene );
Expand All @@ -166,11 +166,11 @@ class Nodes extends DataMap {
if ( toneMappingNode ) cacheKey.push( toneMappingNode.getCacheKey() );

cacheKeyData = {
frameId,
callId,
cacheKey: cacheKey.join( ',' )
};

this.frameHashCache.set( chain, cacheKeyData );
this.callHashCache.set( chain, cacheKeyData );

}

Expand Down

0 comments on commit 5e9b96c

Please sign in to comment.