@@ -611,17 +611,21 @@ function rendererWebGPU(p5, fn) {
611611 const groupEntries = new Map ( ) ; // group index -> array of entries
612612
613613 // Add all uniform group bindings to group 0
614- const group0Entries = [ ] ;
614+ const structEntries = new Map ( ) ;
615615 for ( const bufferGroup of shader . _uniformBufferGroups ) {
616- group0Entries . push ( {
616+ const entries = structEntries . get ( bufferGroup . group ) || [ ] ;
617+ entries . push ( {
617618 bufferGroup,
618619 binding : bufferGroup . binding ,
619620 visibility : GPUShaderStage . VERTEX | GPUShaderStage . FRAGMENT ,
620621 buffer : { type : 'uniform' , hasDynamicOffset : bufferGroup . dynamic } ,
621622 } ) ;
623+ structEntries . set ( bufferGroup . group , entries ) ;
624+ }
625+ for ( const [ group , entries ] of structEntries . entries ( ) ) {
626+ entries . sort ( ( a , b ) => a . binding - b . binding ) ;
627+ groupEntries . set ( group , entries ) ;
622628 }
623- group0Entries . sort ( ( a , b ) => a . binding - b . binding ) ;
624- groupEntries . set ( 0 , group0Entries ) ;
625629
626630 // Add the variable amount of samplers and texture bindings that can come after
627631 for ( const sampler of shader . samplers ) {
@@ -655,6 +659,7 @@ function rendererWebGPU(p5, fn) {
655659
656660 shader . _groupEntries = groupEntries ;
657661 shader . _bindGroupLayouts = [ ...bindGroupLayouts . values ( ) ] ;
662+ shader . _cachedBindGroup = { } ;
658663 shader . _pipelineLayout = this . device . createPipelineLayout ( {
659664 bindGroupLayouts : shader . _bindGroupLayouts ,
660665 } ) ;
@@ -1409,6 +1414,7 @@ function rendererWebGPU(p5, fn) {
14091414
14101415 // Make a shallow copy so that we keep track of the last offset for this uniform
14111416 this . _uniformBuffersForBinding . set ( bufferGroup . binding , { ...uniformBufferInfo } ) ;
1417+ currentShader . _cachedBindGroup [ bufferGroup . group ] = undefined ;
14121418 } else {
14131419 // Bind uniforms to a binding-specific buffer, which may be cached for performance
14141420 let bufferInfo ;
@@ -1432,6 +1438,7 @@ function rendererWebGPU(p5, fn) {
14321438
14331439 currentShader . buffersDirty = currentShader . buffersDirty || { } ;
14341440 currentShader . buffersDirty [ bufferGroup . group + ',' + bufferGroup . binding ] = false ;
1441+ currentShader . _cachedBindGroup [ bufferGroup . group ] = undefined ;
14351442
14361443 // Cache this buffer and data for next frame
14371444 bufferGroup . currentBuffer = bufferInfo ;
@@ -1468,10 +1475,14 @@ function rendererWebGPU(p5, fn) {
14681475 } ) ;
14691476
14701477 const layout = currentShader . _bindGroupLayouts [ group ] ;
1471- const bindGroup = this . device . createBindGroup ( {
1472- layout,
1473- entries : bgEntries ,
1474- } ) ;
1478+ let bindGroup = currentShader . _cachedBindGroup [ group ] ;
1479+ if ( ! bindGroup ) {
1480+ bindGroup = this . device . createBindGroup ( {
1481+ layout,
1482+ entries : bgEntries ,
1483+ } ) ;
1484+ }
1485+ currentShader . _cachedBindGroup [ group ] = bindGroup ;
14751486 passEncoder . setBindGroup (
14761487 group ,
14771488 bindGroup ,
0 commit comments