1+ /*// debugger utils
2+ import 'https://greggman.github.io/webgpu-avoid-redundant-state-setting/webgpu-check-redundant-state-setting.js';
3+ //*/
4+
15import { GPUFeatureName , GPUTextureFormat , GPULoadOp , GPUStoreOp , GPUIndexFormat , GPUTextureViewDimension } from './utils/WebGPUConstants.js' ;
26
37import WebGPUNodeBuilder from './nodes/WGSLNodeBuilder.js' ;
@@ -243,6 +247,7 @@ class WebGPUBackend extends Backend {
243247 renderContextData . descriptor = descriptor ;
244248 renderContextData . encoder = encoder ;
245249 renderContextData . currentPass = currentPass ;
250+ renderContextData . currentAttributesSet = { } ;
246251
247252 //
248253
@@ -395,6 +400,7 @@ class WebGPUBackend extends Backend {
395400 const bindingsData = this . get ( renderObject . getBindings ( ) ) ;
396401 const contextData = this . get ( context ) ;
397402 const pipelineGPU = this . get ( pipeline ) . pipeline ;
403+ const attributesSet = contextData . currentAttributesSet ;
398404
399405 // pipeline
400406
@@ -406,18 +412,28 @@ class WebGPUBackend extends Backend {
406412 const bindGroupGPU = bindingsData . group ;
407413 passEncoderGPU . setBindGroup ( 0 , bindGroupGPU ) ;
408414
409- // index
415+ // attributes
410416
411417 const index = renderObject . getIndex ( ) ;
412418
413419 const hasIndex = ( index !== null ) ;
414420
421+ // index
422+
415423 if ( hasIndex === true ) {
416424
417- const buffer = this . get ( index ) . buffer ;
418- const indexFormat = ( index . array instanceof Uint16Array ) ? GPUIndexFormat . Uint16 : GPUIndexFormat . Uint32 ;
425+ const indexHash = renderObject . getIndexHash ( ) ;
419426
420- passEncoderGPU . setIndexBuffer ( buffer , indexFormat ) ;
427+ if ( attributesSet . index !== indexHash ) {
428+
429+ const buffer = this . get ( index ) . buffer ;
430+ const indexFormat = ( index . array instanceof Uint16Array ) ? GPUIndexFormat . Uint16 : GPUIndexFormat . Uint32 ;
431+
432+ passEncoderGPU . setIndexBuffer ( buffer , indexFormat ) ;
433+
434+ attributesSet . index = indexHash ;
435+
436+ }
421437
422438 }
423439
@@ -427,8 +443,17 @@ class WebGPUBackend extends Backend {
427443
428444 for ( let i = 0 , l = attributes . length ; i < l ; i ++ ) {
429445
430- const buffer = this . get ( attributes [ i ] ) . buffer ;
431- passEncoderGPU . setVertexBuffer ( i , buffer ) ;
446+ const attribute = attributes [ i ] ;
447+ const attributeHash = renderObject . getAttributeHash ( i ) ;
448+
449+ if ( attributesSet [ i ] !== attributeHash ) {
450+
451+ const buffer = this . get ( attribute ) . buffer ;
452+ passEncoderGPU . setVertexBuffer ( i , buffer ) ;
453+
454+ attributesSet [ i ] = attributeHash ;
455+
456+ }
432457
433458 }
434459
@@ -703,6 +728,7 @@ class WebGPUBackend extends Backend {
703728 if ( renderContext . stencil ) descriptor . depthStencilAttachment . stencilLoadOp = GPULoadOp . Load ;
704729
705730 renderContextData . currentPass = encoder . beginRenderPass ( descriptor ) ;
731+ renderContextData . currentAttributesSet = { } ;
706732
707733 }
708734
0 commit comments