Browse Source

WebGPURenderer: Don't set redundant state (#26186)

* BufferAttribute: Added .uuid

* WebGPURenderer: Don't set redundant state

* cleanup

* BufferAttribute: Fix serialization and tests

* Attributes: Rename .getUUID() to .getHash()

* Attributes: Rename .getHash() to .getUUID()

* BufferGeometryLoader: Fix undefined uuid

* cleanup

* Reverting
sunag 2 years ago
parent
commit
52bc19c541
1 changed files with 29 additions and 6 deletions
  1. 29 6
      examples/jsm/renderers/webgpu/WebGPUBackend.js

+ 29 - 6
examples/jsm/renderers/webgpu/WebGPUBackend.js

@@ -1,3 +1,7 @@
+/*// debugger tools
+import 'https://greggman.github.io/webgpu-avoid-redundant-state-setting/webgpu-check-redundant-state-setting.js';
+//*/
+
 import { GPUFeatureName, GPUTextureFormat, GPULoadOp, GPUStoreOp, GPUIndexFormat, GPUTextureViewDimension } from './utils/WebGPUConstants.js';
 
 import WebGPUNodeBuilder from './nodes/WGSLNodeBuilder.js';
@@ -243,6 +247,7 @@ class WebGPUBackend extends Backend {
 		renderContextData.descriptor = descriptor;
 		renderContextData.encoder = encoder;
 		renderContextData.currentPass = currentPass;
+		renderContextData.currentAttributesSet = {};
 
 		//
 
@@ -395,6 +400,7 @@ class WebGPUBackend extends Backend {
 		const bindingsData = this.get( renderObject.getBindings() );
 		const contextData = this.get( context );
 		const pipelineGPU = this.get( pipeline ).pipeline;
+		const attributesSet = contextData.currentAttributesSet;
 
 		// pipeline
 
@@ -406,18 +412,26 @@ class WebGPUBackend extends Backend {
 		const bindGroupGPU = bindingsData.group;
 		passEncoderGPU.setBindGroup( 0, bindGroupGPU );
 
-		// index
+		// attributes
 
 		const index = renderObject.getIndex();
 
 		const hasIndex = ( index !== null );
 
+		// index
+
 		if ( hasIndex === true ) {
 
-			const buffer = this.get( index ).buffer;
-			const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
+			if ( attributesSet.index !== index ) {
+			
+				const buffer = this.get( index ).buffer;
+				const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
+
+				passEncoderGPU.setIndexBuffer( buffer, indexFormat );
+
+				attributesSet.index = index;
 
-			passEncoderGPU.setIndexBuffer( buffer, indexFormat );
+			}
 
 		}
 
@@ -427,8 +441,16 @@ class WebGPUBackend extends Backend {
 
 		for ( let i = 0, l = attributes.length; i < l; i ++ ) {
 
-			const buffer = this.get( attributes[ i ] ).buffer;
-			passEncoderGPU.setVertexBuffer( i, buffer );
+			const attribute = attributes[ i ];
+
+			if ( attributesSet[ i ] !== attribute ) {
+
+				const buffer = this.get( attribute ).buffer;
+				passEncoderGPU.setVertexBuffer( i, buffer );
+
+				attributesSet[ i ] = attribute;
+
+			}
 
 		}
 
@@ -703,6 +725,7 @@ class WebGPUBackend extends Backend {
 		if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
 
 		renderContextData.currentPass = encoder.beginRenderPass( descriptor );
+		renderContextData.currentAttributesSet = {};
 
 	}