@@ -79,21 +79,15 @@ class Bindings extends DataMap {
7979
8080 const bindings = renderObject . getBindings ( ) ;
8181
82- for ( const bindGroup of bindings ) {
83-
84- const groupData = this . get ( bindGroup ) ;
82+ const renderObjectData = this . get ( renderObject ) ;
8583
86- if ( groupData . bindGroup === undefined ) {
84+ if ( renderObjectData . initialized !== true ) {
8785
88- // each object defines an array of bindings (ubos, textures, samplers etc.)
86+ // bind groups are created once per object
8987
90- this . _init ( bindGroup ) ;
88+ this . _createBindings ( bindings ) ;
9189
92- this . backend . createBindings ( bindGroup , bindings , 0 ) ;
93-
94- groupData . bindGroup = bindGroup ;
95-
96- }
90+ renderObjectData . initialized = true ;
9791
9892 }
9993
@@ -110,20 +104,15 @@ class Bindings extends DataMap {
110104 getForCompute ( computeNode ) {
111105
112106 const bindings = this . nodes . getForCompute ( computeNode ) . bindings ;
107+ const computeNodeData = this . get ( computeNode ) ;
113108
114- for ( const bindGroup of bindings ) {
115-
116- const groupData = this . get ( bindGroup ) ;
117-
118- if ( groupData . bindGroup === undefined ) {
109+ if ( computeNodeData . initialized !== true ) {
119110
120- this . _init ( bindGroup ) ;
111+ // bind groups are created once per object
121112
122- this . backend . createBindings ( bindGroup , bindings , 0 ) ;
113+ this . _createBindings ( bindings ) ;
123114
124- groupData . bindGroup = bindGroup ;
125-
126- }
115+ computeNodeData . initialized = true ;
127116
128117 }
129118
@@ -162,12 +151,9 @@ class Bindings extends DataMap {
162151
163152 const bindings = this . nodes . getForCompute ( computeNode ) . bindings ;
164153
165- for ( const bindGroup of bindings ) {
154+ this . _destroyBindings ( bindings ) ;
166155
167- this . backend . deleteBindGroupData ( bindGroup ) ;
168- this . delete ( bindGroup ) ;
169-
170- }
156+ this . delete ( computeNode ) ;
171157
172158 }
173159
@@ -180,60 +166,125 @@ class Bindings extends DataMap {
180166
181167 const bindings = renderObject . getBindings ( ) ;
182168
183- for ( const bindGroup of bindings ) {
184-
185- this . backend . deleteBindGroupData ( bindGroup ) ;
186- this . delete ( bindGroup ) ;
169+ this . _destroyBindings ( bindings ) ;
187170
188- }
171+ this . delete ( renderObject ) ;
189172
190173 }
191174
192175 /**
193- * Updates the given array of bindings.
176+ * Creates the bindings for the given array of bindings.
194177 *
195178 * @param {Array<BindGroup> } bindings - The bind groups.
196179 */
197- _updateBindings ( bindings ) {
180+ _createBindings ( bindings ) {
198181
199182 for ( const bindGroup of bindings ) {
200183
201- this . _update ( bindGroup , bindings ) ;
184+ // binding group
185+
186+ const groupData = this . get ( bindGroup ) ;
187+
188+ if ( groupData . bindGroup === undefined ) {
189+
190+ // initialize
191+
192+ for ( const binding of bindGroup . bindings ) {
193+
194+ if ( binding . isUniformBuffer ) {
195+
196+ this . backend . createUniformBuffer ( binding ) ;
197+ this . info . createUniformBuffer ( binding ) ;
198+
199+ } else if ( binding . isSampledTexture ) {
200+
201+ this . textures . updateTexture ( binding . texture ) ;
202+
203+ } else if ( binding . isSampler ) {
204+
205+ this . textures . updateSampler ( binding . texture ) ;
206+
207+ } else if ( binding . isStorageBuffer ) {
208+
209+ const attribute = binding . attribute ;
210+ const attributeType = attribute . isIndirectStorageBufferAttribute ? AttributeType . INDIRECT : AttributeType . STORAGE ;
211+
212+ this . attributes . update ( attribute , attributeType ) ;
213+
214+ }
215+
216+ }
217+
218+ // each object defines an array of bindings (ubos, textures, samplers etc.)
219+
220+ this . backend . createBindings ( bindGroup , bindings , 0 ) ;
221+
222+ groupData . bindGroup = bindGroup ;
223+ groupData . usedTimes = 1 ;
224+
225+ } else {
226+
227+ groupData . usedTimes ++ ;
228+
229+ }
202230
203231 }
204232
205233 }
206234
207235 /**
208- * Initializes the given bind group .
236+ * Deletes the given array of bindings .
209237 *
210- * @param {BindGroup } bindGroup - The bind group to initialize .
238+ * @param {Array< BindGroup> } bindings - The bind groups .
211239 */
212- _init ( bindGroup ) {
240+ _destroyBindings ( bindings ) {
213241
214- for ( const binding of bindGroup . bindings ) {
242+ for ( const bindGroup of bindings ) {
215243
216- if ( binding . isSampledTexture ) {
244+ const groupData = this . get ( bindGroup ) ;
245+ groupData . usedTimes -- ;
217246
218- this . textures . updateTexture ( binding . texture ) ;
247+ if ( groupData . usedTimes === 0 ) {
219248
220- } else if ( binding . isSampler ) {
249+ for ( const binding of bindGroup . bindings ) {
221250
222- this . textures . updateSampler ( binding . texture ) ;
251+ if ( binding . isUniformBuffer ) {
223252
224- } else if ( binding . isStorageBuffer ) {
253+ this . backend . destroyUniformBuffer ( binding ) ;
254+ this . info . destroyUniformBuffer ( binding ) ;
225255
226- const attribute = binding . attribute ;
227- const attributeType = attribute . isIndirectStorageBufferAttribute ? AttributeType . INDIRECT : AttributeType . STORAGE ;
256+ // release arrays
228257
229- this . attributes . update ( attribute , attributeType ) ;
258+ binding . release ( ) ;
259+
260+ }
261+
262+ }
263+
264+ this . backend . deleteBindGroupData ( bindGroup ) ;
265+ this . delete ( bindGroup ) ;
230266
231267 }
232268
233269 }
234270
235271 }
236272
273+ /**
274+ * Updates the given array of bindings.
275+ *
276+ * @param {Array<BindGroup> } bindings - The bind groups.
277+ */
278+ _updateBindings ( bindings ) {
279+
280+ for ( const bindGroup of bindings ) {
281+
282+ this . _update ( bindGroup , bindings ) ;
283+
284+ }
285+
286+ }
287+
237288 /**
238289 * Updates the given bind group.
239290 *
0 commit comments