Skip to content

Commit

Permalink
WebGPURenderer: Reduce memory churn when using chain maps. (mrdoob#30249
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Mugen87 authored Jan 2, 2025
1 parent 12095d2 commit 00fa06f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
12 changes: 7 additions & 5 deletions src/renderers/common/Lighting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LightsNode } from '../../nodes/Nodes.js';
import ChainMap from './ChainMap.js';

const _defaultLights = /*@__PURE__*/ new LightsNode();
const _chainKeys = [];

/**
* This renderer module manages the lights nodes which are unique
Expand Down Expand Up @@ -49,19 +50,20 @@ class Lighting extends ChainMap {

if ( scene.isQuadMesh ) return _defaultLights;

// tiled lighting
_chainKeys[ 0 ] = scene;
_chainKeys[ 1 ] = camera;

const keys = [ scene, camera ];

let node = this.get( keys );
let node = this.get( _chainKeys );

if ( node === undefined ) {

node = this.createNode();
this.set( keys, node );
this.set( _chainKeys, node );

}

_chainKeys.length = 0;

return node;

}
Expand Down
12 changes: 9 additions & 3 deletions src/renderers/common/RenderBundles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ChainMap from './ChainMap.js';
import RenderBundle from './RenderBundle.js';

const _chainKeys = [];

/**
* This renderer module manages render bundles.
*
Expand Down Expand Up @@ -32,17 +34,21 @@ class RenderBundles {
get( bundleGroup, camera ) {

const bundles = this.bundles;
const keys = [ bundleGroup, camera ];

let bundle = bundles.get( keys );
_chainKeys[ 0 ] = bundleGroup;
_chainKeys[ 1 ] = camera;

let bundle = bundles.get( _chainKeys );

if ( bundle === undefined ) {

bundle = new RenderBundle( bundleGroup, camera );
bundles.set( keys, bundle );
bundles.set( _chainKeys, bundle );

}

_chainKeys.length = 0;

return bundle;

}
Expand Down
17 changes: 10 additions & 7 deletions src/renderers/common/RenderContexts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ChainMap from './ChainMap.js';
import RenderContext from './RenderContext.js';

const _chainKeys = [];

/**
* This module manages the render contexts of the renderer.
*
Expand Down Expand Up @@ -33,13 +35,12 @@ class RenderContexts {
*/
get( scene = null, camera = null, renderTarget = null ) {

const chainKey = [];
if ( scene !== null ) chainKey.push( scene );
if ( camera !== null ) chainKey.push( camera );
if ( scene !== null ) _chainKeys.push( scene );
if ( camera !== null ) _chainKeys.push( camera );

if ( chainKey.length === 0 ) {
if ( _chainKeys.length === 0 ) {

chainKey.push( { id: 'default' } );
_chainKeys.push( { id: 'default' } );

}

Expand All @@ -61,16 +62,18 @@ class RenderContexts {

const chainMap = this.getChainMap( attachmentState );

let renderState = chainMap.get( chainKey );
let renderState = chainMap.get( _chainKeys );

if ( renderState === undefined ) {

renderState = new RenderContext();

chainMap.set( chainKey, renderState );
chainMap.set( _chainKeys, renderState );

}

_chainKeys.length = 0;

if ( renderTarget !== null ) renderState.sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;

return renderState;
Expand Down
12 changes: 9 additions & 3 deletions src/renderers/common/RenderLists.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ChainMap from './ChainMap.js';
import RenderList from './RenderList.js';

const _chainKeys = [];

/**
* This renderer module manages the render lists which are unique
* per scene and camera combination.
Expand Down Expand Up @@ -42,17 +44,21 @@ class RenderLists {
get( scene, camera ) {

const lists = this.lists;
const keys = [ scene, camera ];

let list = lists.get( keys );
_chainKeys[ 0 ] = scene;
_chainKeys[ 1 ] = camera;

let list = lists.get( _chainKeys );

if ( list === undefined ) {

list = new RenderList( this.lighting, scene, camera );
lists.set( keys, list );
lists.set( _chainKeys, list );

}

_chainKeys.length = 0;

return list;

}
Expand Down
16 changes: 9 additions & 7 deletions src/renderers/common/RenderObjects.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ChainMap from './ChainMap.js';
import RenderObject from './RenderObject.js';

const _chainArray = [];
const _chainKeys = [];

/**
* This module manages the render objects of the renderer.
Expand Down Expand Up @@ -92,18 +92,18 @@ class RenderObjects {
const chainMap = this.getChainMap( passId );

// reuse chainArray
_chainArray[ 0 ] = object;
_chainArray[ 1 ] = material;
_chainArray[ 2 ] = renderContext;
_chainArray[ 3 ] = lightsNode;
_chainKeys[ 0 ] = object;
_chainKeys[ 1 ] = material;
_chainKeys[ 2 ] = renderContext;
_chainKeys[ 3 ] = lightsNode;

let renderObject = chainMap.get( _chainArray );
let renderObject = chainMap.get( _chainKeys );

if ( renderObject === undefined ) {

renderObject = this.createRenderObject( this.nodes, this.geometries, this.renderer, object, material, scene, camera, lightsNode, renderContext, clippingContext, passId );

chainMap.set( _chainArray, renderObject );
chainMap.set( _chainKeys, renderObject );

} else {

Expand Down Expand Up @@ -133,6 +133,8 @@ class RenderObjects {

}

_chainKeys.length = 0;

return renderObject;

}
Expand Down
20 changes: 14 additions & 6 deletions src/renderers/common/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CubeUVReflectionMapping, EquirectangularReflectionMapping, Equirectangu
import { hashArray } from '../../../nodes/core/NodeUtils.js';

const _outputNodeMap = new WeakMap();
const _chainKeys = [];

/**
* This renderer module manages node-related objects and is the
Expand Down Expand Up @@ -136,10 +137,13 @@ class Nodes extends DataMap {

// other groups are updated just when groupNode.needsUpdate is true

const groupChain = [ groupNode, nodeUniformsGroup ];
_chainKeys[ 0 ] = groupNode;
_chainKeys[ 1 ] = nodeUniformsGroup;

let groupData = this.groupsData.get( groupChain );
if ( groupData === undefined ) this.groupsData.set( groupChain, groupData = {} );
let groupData = this.groupsData.get( _chainKeys );
if ( groupData === undefined ) this.groupsData.set( _chainKeys, groupData = {} );

_chainKeys.length = 0;

if ( groupData.version !== groupNode.version ) {

Expand Down Expand Up @@ -382,10 +386,12 @@ class Nodes extends DataMap {
*/
getCacheKey( scene, lightsNode ) {

const chain = [ scene, lightsNode ];
_chainKeys[ 0 ] = scene;
_chainKeys[ 1 ] = lightsNode;

const callId = this.renderer.info.calls;

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

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

Expand All @@ -405,10 +411,12 @@ class Nodes extends DataMap {
cacheKey: hashArray( values )
};

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

}

_chainKeys.length = 0;

return cacheKeyData.cacheKey;

}
Expand Down

0 comments on commit 00fa06f

Please sign in to comment.