@@ -9,6 +9,32 @@ class WebGPUBindingUtils {
99
1010 this . backend = backend ;
1111
12+ this . lowwaterMark = Infinity ;
13+ this . highwaterMark = 0 ;
14+
15+ this . commonBufferGPU = null ;
16+
17+ }
18+
19+ getCommonBuffer ( commonUniformBuffer ) {
20+
21+ let bufferGPU = this . commonBufferGPU ;
22+
23+ if ( bufferGPU === null ) {
24+
25+ bufferGPU = this . backend . device . createBuffer ( {
26+ label : 'bindingBuffer_common' ,
27+ size : commonUniformBuffer . byteLength ,
28+ usage : GPUBufferUsage . UNIFORM | GPUBufferUsage . COPY_DST
29+ } ) ;
30+
31+ this . commonBufferGPU = bufferGPU ;
32+ this . commonUniformBuffer = commonUniformBuffer ;
33+
34+ }
35+
36+ return bufferGPU
37+
1238 }
1339
1440 createBindingsLayout ( bindings ) {
@@ -128,10 +154,18 @@ class WebGPUBindingUtils {
128154 const backend = this . backend ;
129155 const device = backend . device ;
130156
131- const buffer = binding . buffer ;
132- const bufferGPU = backend . get ( binding ) . buffer ;
157+ if ( binding . isNodeUniformsGroup && binding . allocateCommon ( ) ) {
158+
159+ const buffer = binding . buffer ;
133160
134- device . queue . writeBuffer ( bufferGPU , 0 , buffer , 0 ) ;
161+ this . lowwaterMark = Math . min ( this . lowwaterMark , buffer . byteOffset ) ;
162+ this . highwaterMark = Math . max ( this . highwaterMark , buffer . byteOffset + buffer . byteLength ) ;
163+
164+ } else {
165+
166+ const bufferGPU = backend . get ( binding ) . buffer ;
167+ device . queue . writeBuffer ( bufferGPU , 0 , binding . buffer , 0 ) ;
168+ }
135169
136170 }
137171
@@ -149,23 +183,42 @@ class WebGPUBindingUtils {
149183
150184 const bindingData = backend . get ( binding ) ;
151185
152- if ( bindingData . buffer === undefined ) {
186+ let resource ;
153187
154- const byteLength = binding . byteLength ;
188+ if ( binding . isNodeUniformsGroup && binding . allocateCommon ( ) ) {
155189
156- const usage = GPUBufferUsage . UNIFORM | GPUBufferUsage . COPY_DST ;
190+ const buffer = binding . buffer ;
157191
158- const bufferGPU = device . createBuffer ( {
159- label : 'bindingBuffer_' + binding . name ,
160- size : byteLength ,
161- usage : usage
162- } ) ;
192+ resource = {
193+ label : 'bindingBufferCommon_' + binding . name ,
194+ buffer : this . getCommonBuffer ( binding . commonUniformBuffer ) ,
195+ offset : buffer . byteOffset ,
196+ size : buffer . byteLength
197+ } ;
163198
164- bindingData . buffer = bufferGPU ;
199+ } else {
200+
201+ if ( bindingData . buffer === undefined ) {
202+
203+ const byteLength = binding . byteLength ;
204+
205+ const usage = GPUBufferUsage . UNIFORM | GPUBufferUsage . COPY_DST ;
206+
207+ const bufferGPU = device . createBuffer ( {
208+ label : 'bindingBuffer_' + binding . name ,
209+ size : byteLength ,
210+ usage : usage
211+ } ) ;
212+
213+ bindingData . buffer = bufferGPU ;
214+
215+ }
216+
217+ resource = { buffer : bindingData . buffer } ;
165218
166219 }
167220
168- entriesGPU . push ( { binding : bindingPoint , resource : { buffer : bindingData . buffer } } ) ;
221+ entriesGPU . push ( { binding : bindingPoint , resource } ) ;
169222
170223 } else if ( binding . isStorageBuffer ) {
171224
@@ -239,6 +292,19 @@ class WebGPUBindingUtils {
239292
240293 }
241294
295+ endPass ( ) {
296+
297+ const device = this . backend . device ;
298+
299+ if ( this . commonBufferGPU === null || this . lowwaterMark === Infinity ) return ;
300+
301+ device . queue . writeBuffer ( this . commonBufferGPU , this . lowwaterMark , this . commonUniformBuffer . arrayBuffer , this . lowwaterMark ) ;
302+
303+ this . lowwaterMark = Infinity ;
304+ this . highwaterMark = 0 ;
305+
306+ }
307+
242308}
243309
244310export default WebGPUBindingUtils ;
0 commit comments