1+ /*// debugger tools
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,26 @@ 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+ if ( attributesSet . index !== index ) {
426+
427+ const buffer = this . get ( index ) . buffer ;
428+ const indexFormat = ( index . array instanceof Uint16Array ) ? GPUIndexFormat . Uint16 : GPUIndexFormat . Uint32 ;
429+
430+ passEncoderGPU . setIndexBuffer ( buffer , indexFormat ) ;
431+
432+ attributesSet . index = index ;
419433
420- passEncoderGPU . setIndexBuffer ( buffer , indexFormat ) ;
434+ }
421435
422436 }
423437
@@ -427,8 +441,16 @@ class WebGPUBackend extends Backend {
427441
428442 for ( let i = 0 , l = attributes . length ; i < l ; i ++ ) {
429443
430- const buffer = this . get ( attributes [ i ] ) . buffer ;
431- passEncoderGPU . setVertexBuffer ( i , buffer ) ;
444+ const attribute = attributes [ i ] ;
445+
446+ if ( attributesSet [ i ] !== attribute ) {
447+
448+ const buffer = this . get ( attribute ) . buffer ;
449+ passEncoderGPU . setVertexBuffer ( i , buffer ) ;
450+
451+ attributesSet [ i ] = attribute ;
452+
453+ }
432454
433455 }
434456
@@ -703,6 +725,7 @@ class WebGPUBackend extends Backend {
703725 if ( renderContext . stencil ) descriptor . depthStencilAttachment . stencilLoadOp = GPULoadOp . Load ;
704726
705727 renderContextData . currentPass = encoder . beginRenderPass ( descriptor ) ;
728+ renderContextData . currentAttributesSet = { } ;
706729
707730 }
708731
0 commit comments