From dd2c03e97f6a61610c0df189fd0471ef84b3ae1a Mon Sep 17 00:00:00 2001 From: aardgoose Date: Sat, 17 Aug 2024 22:18:26 +0100 Subject: [PATCH 1/2] sort and minimixe setbindgroup() --- src/nodes/core/NodeBuilder.js | 17 +++++++++++++++++ src/nodes/core/UniformGroupNode.js | 6 +++--- src/renderers/webgpu/WebGPUBackend.js | 15 +++++++++++---- src/renderers/webgpu/nodes/WGSLNodeBuilder.js | 2 ++ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/nodes/core/NodeBuilder.js b/src/nodes/core/NodeBuilder.js index 955cd081e38226..70b7c9890439b5 100644 --- a/src/nodes/core/NodeBuilder.js +++ b/src/nodes/core/NodeBuilder.js @@ -277,6 +277,23 @@ class NodeBuilder { } + sortBindingGroups() { + + const bindingsGroups = this.getBindings(); + + bindingsGroups.sort( ( a, b ) => ( a.bindings[ 0 ].groupNode.order - b.bindings[ 0 ].groupNode.order ) ); + + for ( let i = 0; i < bindingsGroups.length; i ++ ) { + + const bindingGroup = bindingsGroups[ i ]; + this.bindingsIndexes[ bindingGroup.name ].group = i; + + bindingGroup.index = i; + + } + + } + setHashNode( node, hash ) { this.hashNodes[ hash ] = node; diff --git a/src/nodes/core/UniformGroupNode.js b/src/nodes/core/UniformGroupNode.js index 95575246f4de58..19d0ed6a10640c 100644 --- a/src/nodes/core/UniformGroupNode.js +++ b/src/nodes/core/UniformGroupNode.js @@ -2,7 +2,7 @@ import Node, { registerNodeClass } from './Node.js'; class UniformGroupNode extends Node { - constructor( name, shared = false ) { + constructor( name, shared = false, order = 1 ) { super( 'string' ); @@ -10,7 +10,7 @@ class UniformGroupNode extends Node { this.version = 0; this.shared = shared; - + this.order = order; this.isUniformGroup = true; } @@ -48,7 +48,7 @@ export default UniformGroupNode; registerNodeClass( 'UniformGroup', UniformGroupNode ); export const uniformGroup = ( name ) => new UniformGroupNode( name ); -export const sharedUniformGroup = ( name ) => new UniformGroupNode( name, true ); +export const sharedUniformGroup = ( name, order = 0 ) => new UniformGroupNode( name, true, order ); export const frameGroup = sharedUniformGroup( 'frame' ); export const renderGroup = sharedUniformGroup( 'render' ); diff --git a/src/renderers/webgpu/WebGPUBackend.js b/src/renderers/webgpu/WebGPUBackend.js index f59f5d3968b1ed..a24fa625570289 100644 --- a/src/renderers/webgpu/WebGPUBackend.js +++ b/src/renderers/webgpu/WebGPUBackend.js @@ -425,7 +425,7 @@ class WebGPUBackend extends Backend { renderContextData.descriptor = descriptor; renderContextData.encoder = encoder; renderContextData.currentPass = currentPass; - renderContextData.currentSets = { attributes: {}, pipeline: null, index: null }; + renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null }; renderContextData.renderBundles = []; // @@ -848,12 +848,19 @@ class WebGPUBackend extends Backend { // bind groups + const currentBindingGroups = currentSets.bindingGroups; + for ( let i = 0, l = bindings.length; i < l; i ++ ) { const bindGroup = bindings[ i ]; const bindingsData = this.get( bindGroup ); - passEncoderGPU.setBindGroup( bindGroup.index, bindingsData.group ); + if ( currentBindingGroups[ bindGroup.index ] !== bindGroup.id ) { + + passEncoderGPU.setBindGroup( bindGroup.index, bindingsData.group ); + currentBindingGroups[ bindGroup.index ] = bindGroup.id; + + } } @@ -1253,7 +1260,7 @@ class WebGPUBackend extends Backend { renderContextData._currentPass = renderContextData.currentPass; renderContextData._currentSets = renderContextData.currentSets; - renderContextData.currentSets = { attributes: {}, pipeline: null, index: null }; + renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null }; renderContextData.currentPass = this.pipelineUtils.createBundleEncoder( renderContext ); } @@ -1482,7 +1489,7 @@ class WebGPUBackend extends Backend { if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load; renderContextData.currentPass = encoder.beginRenderPass( descriptor ); - renderContextData.currentSets = { attributes: {}, pipeline: null, index: null }; + renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null }; } diff --git a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js index fd532039ee6c95..fd11cc15673e45 100644 --- a/src/renderers/webgpu/nodes/WGSLNodeBuilder.js +++ b/src/renderers/webgpu/nodes/WGSLNodeBuilder.js @@ -1043,6 +1043,8 @@ ${ flowData.code } const shadersData = this.material !== null ? { fragment: {}, vertex: {} } : { compute: {} }; + this.sortBindingGroups(); + for ( const shaderStage in shadersData ) { const stageData = shadersData[ shaderStage ]; From 106a0616e10ef2f0a5b0fe442daa216bda85f83e Mon Sep 17 00:00:00 2001 From: aardgoose Date: Thu, 29 Aug 2024 09:55:00 +0100 Subject: [PATCH 2/2] lint --- src/renderers/webgpu/WebGPUBackend.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderers/webgpu/WebGPUBackend.js b/src/renderers/webgpu/WebGPUBackend.js index a24fa625570289..4f4366489c1c0b 100644 --- a/src/renderers/webgpu/WebGPUBackend.js +++ b/src/renderers/webgpu/WebGPUBackend.js @@ -1260,7 +1260,7 @@ class WebGPUBackend extends Backend { renderContextData._currentPass = renderContextData.currentPass; renderContextData._currentSets = renderContextData.currentSets; - renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null }; + renderContextData.currentSets = { attributes: {}, bindingGroups: [], pipeline: null, index: null }; renderContextData.currentPass = this.pipelineUtils.createBundleEncoder( renderContext ); }