From 196f54b49119fbfb892b5f59619d08052f39b21b Mon Sep 17 00:00:00 2001 From: aardgoose Date: Sat, 17 Aug 2024 14:02:22 +0100 Subject: [PATCH 1/2] cache BindGroupLayouts --- src/nodes/core/NodeBuilder.js | 4 ++-- src/renderers/common/BindGroup.js | 3 ++- src/renderers/common/nodes/NodeBuilderState.js | 2 +- src/renderers/webgpu/utils/WebGPUBindingUtils.js | 13 +++++++++++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/nodes/core/NodeBuilder.js b/src/nodes/core/NodeBuilder.js index 697bd8158e8ef2..e233085a9d6ea8 100644 --- a/src/nodes/core/NodeBuilder.js +++ b/src/nodes/core/NodeBuilder.js @@ -197,7 +197,7 @@ class NodeBuilder { if ( bindGroup === undefined ) { - bindGroup = new BindGroup( groupName, bindingsArray, this.bindingsIndexes[ groupName ].group ); + bindGroup = new BindGroup( groupName, bindingsArray, this.bindingsIndexes[ groupName ].group, bindingsArray ); bindGroupsCache.set( bindingsArray, bindGroup ); @@ -205,7 +205,7 @@ class NodeBuilder { } else { - bindGroup = new BindGroup( groupName, bindingsArray, this.bindingsIndexes[ groupName ].group ); + bindGroup = new BindGroup( groupName, bindingsArray, this.bindingsIndexes[ groupName ].group, bindingsArray ); } diff --git a/src/renderers/common/BindGroup.js b/src/renderers/common/BindGroup.js index 8a48a1bae22b23..d9f6247d5e40a7 100644 --- a/src/renderers/common/BindGroup.js +++ b/src/renderers/common/BindGroup.js @@ -2,11 +2,12 @@ let _id = 0; class BindGroup { - constructor( name = '', bindings = [], index = 0 ) { + constructor( name = '', bindings = [], index = 0, cacheKey ) { this.name = name; this.bindings = bindings; this.index = index; + this.cacheKey = cacheKey; this.id = _id ++; diff --git a/src/renderers/common/nodes/NodeBuilderState.js b/src/renderers/common/nodes/NodeBuilderState.js index 8e0a40c5069df2..3911a2432ea42a 100644 --- a/src/renderers/common/nodes/NodeBuilderState.js +++ b/src/renderers/common/nodes/NodeBuilderState.js @@ -32,7 +32,7 @@ class NodeBuilderState { if ( shared !== true ) { - const bindingsGroup = new BindGroup( instanceGroup.name, [], instanceGroup.index ); + const bindingsGroup = new BindGroup( instanceGroup.name, [], instanceGroup.index, instanceGroup ); bindings.push( bindingsGroup ); for ( const instanceBinding of instanceGroup.bindings ) { diff --git a/src/renderers/webgpu/utils/WebGPUBindingUtils.js b/src/renderers/webgpu/utils/WebGPUBindingUtils.js index 9900b58ede46e0..78db63351f664d 100644 --- a/src/renderers/webgpu/utils/WebGPUBindingUtils.js +++ b/src/renderers/webgpu/utils/WebGPUBindingUtils.js @@ -9,6 +9,7 @@ class WebGPUBindingUtils { constructor( backend ) { this.backend = backend; + this.bindGroupLayoutCache = new WeakMap(); } @@ -135,12 +136,20 @@ class WebGPUBindingUtils { createBindings( bindGroup ) { - const backend = this.backend; + const { backend, bindGroupLayoutCache } = this; const bindingsData = backend.get( bindGroup ); // setup (static) binding layout and (dynamic) binding group - const bindLayoutGPU = this.createBindingsLayout( bindGroup ); + let bindLayoutGPU = bindGroupLayoutCache.get( bindGroup.cacheKey ); + + if ( bindLayoutGPU === undefined ) { + + bindLayoutGPU = this.createBindingsLayout( bindGroup ); + bindGroupLayoutCache.set( bindGroup.cacheKey, bindLayoutGPU ); + + } + const bindGroupGPU = this.createBindGroup( bindGroup, bindLayoutGPU ); bindingsData.layout = bindLayoutGPU; From 95498304e90bed999acfe7d59b58c0f76a2e9c31 Mon Sep 17 00:00:00 2001 From: aardgoose Date: Mon, 19 Aug 2024 09:51:52 +0100 Subject: [PATCH 2/2] rename parameter and add default --- src/renderers/common/BindGroup.js | 4 ++-- src/renderers/webgpu/utils/WebGPUBindingUtils.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/renderers/common/BindGroup.js b/src/renderers/common/BindGroup.js index d9f6247d5e40a7..fd350b2328dc92 100644 --- a/src/renderers/common/BindGroup.js +++ b/src/renderers/common/BindGroup.js @@ -2,12 +2,12 @@ let _id = 0; class BindGroup { - constructor( name = '', bindings = [], index = 0, cacheKey ) { + constructor( name = '', bindings = [], index = 0, bindingsReference = [] ) { this.name = name; this.bindings = bindings; this.index = index; - this.cacheKey = cacheKey; + this.bindingsReference = bindingsReference; this.id = _id ++; diff --git a/src/renderers/webgpu/utils/WebGPUBindingUtils.js b/src/renderers/webgpu/utils/WebGPUBindingUtils.js index 78db63351f664d..8c681ea67ba7c4 100644 --- a/src/renderers/webgpu/utils/WebGPUBindingUtils.js +++ b/src/renderers/webgpu/utils/WebGPUBindingUtils.js @@ -141,12 +141,12 @@ class WebGPUBindingUtils { // setup (static) binding layout and (dynamic) binding group - let bindLayoutGPU = bindGroupLayoutCache.get( bindGroup.cacheKey ); + let bindLayoutGPU = bindGroupLayoutCache.get( bindGroup.bindingsReference ); if ( bindLayoutGPU === undefined ) { bindLayoutGPU = this.createBindingsLayout( bindGroup ); - bindGroupLayoutCache.set( bindGroup.cacheKey, bindLayoutGPU ); + bindGroupLayoutCache.set( bindGroup.bindingsReference, bindLayoutGPU ); }