@@ -64,6 +64,11 @@ export interface GpuDataManager {
64
64
*/
65
65
dispose ( ) : void ;
66
66
67
+ /**
68
+ * create session related data.
69
+ */
70
+ onCreateSession ( ) : void ;
71
+
67
72
/**
68
73
* release session related data.
69
74
* @param sessionId - specify the session ID.
@@ -200,6 +205,9 @@ class GpuDataManagerImpl implements GpuDataManager {
200
205
// a SessionID -> GPUBuffer[] mapping.
201
206
private capturedPendingBuffers : Map < number , GPUBuffer [ ] > ;
202
207
208
+ // The session count.
209
+ private sessionCount : number ;
210
+
203
211
constructor ( private backend : WebGpuBackend ) {
204
212
this . storageCache = new Map ( ) ;
205
213
this . freeBuffers = new Map ( ) ;
@@ -213,6 +221,8 @@ class GpuDataManagerImpl implements GpuDataManager {
213
221
this . freeBuffers . set ( key , [ ] ) ;
214
222
this . freeUniformBuffers . set ( key , [ ] ) ;
215
223
}
224
+
225
+ this . sessionCount = 0 ;
216
226
}
217
227
218
228
upload ( id : GpuDataId , data : Uint8Array ) : void {
@@ -360,7 +370,12 @@ class GpuDataManagerImpl implements GpuDataManager {
360
370
release ( id : GpuDataId ) : number {
361
371
const cachedData = this . storageCache . get ( id ) ;
362
372
if ( ! cachedData ) {
363
- throw new Error ( 'releasing data does not exist' ) ;
373
+ if ( this . storageCache . size === 0 ) {
374
+ // cache was previously cleared, no need to release anything.
375
+ return 0 ;
376
+ } else {
377
+ throw new Error ( 'releasing data does not exist' ) ;
378
+ }
364
379
}
365
380
366
381
LOG_DEBUG ( 'verbose' , ( ) => `[WebGPU] GpuDataManager.release(id=${ id } ), gpuDataId=${ cachedData . gpuData . id } ` ) ;
@@ -460,6 +475,10 @@ class GpuDataManagerImpl implements GpuDataManager {
460
475
this . capturedPendingBuffers = new Map ( ) ;
461
476
}
462
477
478
+ onCreateSession ( ) {
479
+ this . sessionCount += 1 ;
480
+ }
481
+
463
482
onReleaseSession ( sessionId : number ) {
464
483
// release the captured pending buffers.
465
484
const pendingBuffers = this . capturedPendingBuffers . get ( sessionId ) ;
@@ -469,6 +488,16 @@ class GpuDataManagerImpl implements GpuDataManager {
469
488
} ) ;
470
489
this . capturedPendingBuffers . delete ( sessionId ) ;
471
490
}
491
+
492
+ // release the storage cache if no active sessions.
493
+ this . sessionCount -= 1 ;
494
+ if ( this . sessionCount === 0 ) {
495
+ LOG_DEBUG ( 'warning' , ( ) => '[WebGPU] Clearing webgpu buffer cache' ) ;
496
+ this . storageCache . forEach ( ( storage ) => {
497
+ storage . gpuData . buffer . destroy ( ) ;
498
+ } ) ;
499
+ this . storageCache = new Map ( ) ;
500
+ }
472
501
}
473
502
}
474
503
0 commit comments