Skip to content

Commit 00fa06f

Browse files
authored
WebGPURenderer: Reduce memory churn when using chain maps. (#30249)
1 parent 12095d2 commit 00fa06f

File tree

6 files changed

+58
-31
lines changed

6 files changed

+58
-31
lines changed

src/renderers/common/Lighting.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { LightsNode } from '../../nodes/Nodes.js';
22
import ChainMap from './ChainMap.js';
33

44
const _defaultLights = /*@__PURE__*/ new LightsNode();
5+
const _chainKeys = [];
56

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

5051
if ( scene.isQuadMesh ) return _defaultLights;
5152

52-
// tiled lighting
53+
_chainKeys[ 0 ] = scene;
54+
_chainKeys[ 1 ] = camera;
5355

54-
const keys = [ scene, camera ];
55-
56-
let node = this.get( keys );
56+
let node = this.get( _chainKeys );
5757

5858
if ( node === undefined ) {
5959

6060
node = this.createNode();
61-
this.set( keys, node );
61+
this.set( _chainKeys, node );
6262

6363
}
6464

65+
_chainKeys.length = 0;
66+
6567
return node;
6668

6769
}

src/renderers/common/RenderBundles.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import ChainMap from './ChainMap.js';
22
import RenderBundle from './RenderBundle.js';
33

4+
const _chainKeys = [];
5+
46
/**
57
* This renderer module manages render bundles.
68
*
@@ -32,17 +34,21 @@ class RenderBundles {
3234
get( bundleGroup, camera ) {
3335

3436
const bundles = this.bundles;
35-
const keys = [ bundleGroup, camera ];
3637

37-
let bundle = bundles.get( keys );
38+
_chainKeys[ 0 ] = bundleGroup;
39+
_chainKeys[ 1 ] = camera;
40+
41+
let bundle = bundles.get( _chainKeys );
3842

3943
if ( bundle === undefined ) {
4044

4145
bundle = new RenderBundle( bundleGroup, camera );
42-
bundles.set( keys, bundle );
46+
bundles.set( _chainKeys, bundle );
4347

4448
}
4549

50+
_chainKeys.length = 0;
51+
4652
return bundle;
4753

4854
}

src/renderers/common/RenderContexts.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import ChainMap from './ChainMap.js';
22
import RenderContext from './RenderContext.js';
33

4+
const _chainKeys = [];
5+
46
/**
57
* This module manages the render contexts of the renderer.
68
*
@@ -33,13 +35,12 @@ class RenderContexts {
3335
*/
3436
get( scene = null, camera = null, renderTarget = null ) {
3537

36-
const chainKey = [];
37-
if ( scene !== null ) chainKey.push( scene );
38-
if ( camera !== null ) chainKey.push( camera );
38+
if ( scene !== null ) _chainKeys.push( scene );
39+
if ( camera !== null ) _chainKeys.push( camera );
3940

40-
if ( chainKey.length === 0 ) {
41+
if ( _chainKeys.length === 0 ) {
4142

42-
chainKey.push( { id: 'default' } );
43+
_chainKeys.push( { id: 'default' } );
4344

4445
}
4546

@@ -61,16 +62,18 @@ class RenderContexts {
6162

6263
const chainMap = this.getChainMap( attachmentState );
6364

64-
let renderState = chainMap.get( chainKey );
65+
let renderState = chainMap.get( _chainKeys );
6566

6667
if ( renderState === undefined ) {
6768

6869
renderState = new RenderContext();
6970

70-
chainMap.set( chainKey, renderState );
71+
chainMap.set( _chainKeys, renderState );
7172

7273
}
7374

75+
_chainKeys.length = 0;
76+
7477
if ( renderTarget !== null ) renderState.sampleCount = renderTarget.samples === 0 ? 1 : renderTarget.samples;
7578

7679
return renderState;

src/renderers/common/RenderLists.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import ChainMap from './ChainMap.js';
22
import RenderList from './RenderList.js';
33

4+
const _chainKeys = [];
5+
46
/**
57
* This renderer module manages the render lists which are unique
68
* per scene and camera combination.
@@ -42,17 +44,21 @@ class RenderLists {
4244
get( scene, camera ) {
4345

4446
const lists = this.lists;
45-
const keys = [ scene, camera ];
4647

47-
let list = lists.get( keys );
48+
_chainKeys[ 0 ] = scene;
49+
_chainKeys[ 1 ] = camera;
50+
51+
let list = lists.get( _chainKeys );
4852

4953
if ( list === undefined ) {
5054

5155
list = new RenderList( this.lighting, scene, camera );
52-
lists.set( keys, list );
56+
lists.set( _chainKeys, list );
5357

5458
}
5559

60+
_chainKeys.length = 0;
61+
5662
return list;
5763

5864
}

src/renderers/common/RenderObjects.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ChainMap from './ChainMap.js';
22
import RenderObject from './RenderObject.js';
33

4-
const _chainArray = [];
4+
const _chainKeys = [];
55

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

9494
// reuse chainArray
95-
_chainArray[ 0 ] = object;
96-
_chainArray[ 1 ] = material;
97-
_chainArray[ 2 ] = renderContext;
98-
_chainArray[ 3 ] = lightsNode;
95+
_chainKeys[ 0 ] = object;
96+
_chainKeys[ 1 ] = material;
97+
_chainKeys[ 2 ] = renderContext;
98+
_chainKeys[ 3 ] = lightsNode;
9999

100-
let renderObject = chainMap.get( _chainArray );
100+
let renderObject = chainMap.get( _chainKeys );
101101

102102
if ( renderObject === undefined ) {
103103

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

106-
chainMap.set( _chainArray, renderObject );
106+
chainMap.set( _chainKeys, renderObject );
107107

108108
} else {
109109

@@ -133,6 +133,8 @@ class RenderObjects {
133133

134134
}
135135

136+
_chainKeys.length = 0;
137+
136138
return renderObject;
137139

138140
}

src/renderers/common/nodes/Nodes.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { CubeUVReflectionMapping, EquirectangularReflectionMapping, Equirectangu
99
import { hashArray } from '../../../nodes/core/NodeUtils.js';
1010

1111
const _outputNodeMap = new WeakMap();
12+
const _chainKeys = [];
1213

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

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

139-
const groupChain = [ groupNode, nodeUniformsGroup ];
140+
_chainKeys[ 0 ] = groupNode;
141+
_chainKeys[ 1 ] = nodeUniformsGroup;
140142

141-
let groupData = this.groupsData.get( groupChain );
142-
if ( groupData === undefined ) this.groupsData.set( groupChain, groupData = {} );
143+
let groupData = this.groupsData.get( _chainKeys );
144+
if ( groupData === undefined ) this.groupsData.set( _chainKeys, groupData = {} );
145+
146+
_chainKeys.length = 0;
143147

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

@@ -382,10 +386,12 @@ class Nodes extends DataMap {
382386
*/
383387
getCacheKey( scene, lightsNode ) {
384388

385-
const chain = [ scene, lightsNode ];
389+
_chainKeys[ 0 ] = scene;
390+
_chainKeys[ 1 ] = lightsNode;
391+
386392
const callId = this.renderer.info.calls;
387393

388-
let cacheKeyData = this.callHashCache.get( chain );
394+
let cacheKeyData = this.callHashCache.get( _chainKeys );
389395

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

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

408-
this.callHashCache.set( chain, cacheKeyData );
414+
this.callHashCache.set( _chainKeys, cacheKeyData );
409415

410416
}
411417

418+
_chainKeys.length = 0;
419+
412420
return cacheKeyData.cacheKey;
413421

414422
}

0 commit comments

Comments
 (0)