@@ -55,6 +55,9 @@ function rendererWebGPU(p5, fn) {
5555 this . _hasPendingDraws = false ;
5656 this . _pendingCommandEncoders = [ ] ;
5757
58+ // Queue of callbacks to run after next submit (mainly for safe texture deletion)
59+ this . _postSubmitCallbacks = [ ] ;
60+
5861 // Retired buffers to destroy at end of frame
5962 this . _retiredBuffers = [ ] ;
6063
@@ -111,6 +114,7 @@ function rendererWebGPU(p5, fn) {
111114 this . mainFramebuffer = this . createFramebuffer ( { _useCanvasFormat : true } ) ;
112115 this . _updateSize ( ) ;
113116 this . _update ( ) ;
117+ this . flushDraw ( ) ;
114118 }
115119
116120 async _setAttributes ( key , value ) {
@@ -165,7 +169,8 @@ function rendererWebGPU(p5, fn) {
165169 _updateSize ( ) {
166170 if ( this . depthTexture && this . depthTexture . destroy ) {
167171 this . flushDraw ( ) ;
168- this . depthTexture . destroy ( ) ;
172+ const textureToDestroy = this . depthTexture ;
173+ this . _postSubmitCallbacks . push ( ( ) => textureToDestroy . destroy ( ) ) ;
169174 this . depthTextureView = null ;
170175 }
171176 this . depthTexture = this . device . createTexture ( {
@@ -1084,6 +1089,17 @@ function rendererWebGPU(p5, fn) {
10841089 // Submit the commands
10851090 this . queue . submit ( commandsToSubmit ) ;
10861091
1092+ // Execute post-submit callbacks after GPU work completes
1093+ if ( this . _postSubmitCallbacks . length > 0 ) {
1094+ const callbacks = this . _postSubmitCallbacks ;
1095+ this . _postSubmitCallbacks = [ ] ;
1096+ this . device . queue . onSubmittedWorkDone ( ) . then ( ( ) => {
1097+ for ( const callback of callbacks ) {
1098+ callback ( ) ;
1099+ }
1100+ } ) ;
1101+ }
1102+
10871103 // Reset canvas texture cache for next frame
10881104 this . currentCanvasColorTexture = null ;
10891105 this . currentCanvasColorTextureView = null ;
@@ -1112,10 +1128,6 @@ function rendererWebGPU(p5, fn) {
11121128 this . flushDraw ( ) ;
11131129 }
11141130
1115- finishSetup ( ) {
1116- this . flushDraw ( ) ;
1117- }
1118-
11191131 async finishDraw ( ) {
11201132 this . flushDraw ( ) ;
11211133
@@ -1652,7 +1664,7 @@ function rendererWebGPU(p5, fn) {
16521664 bindTextureToShader ( _texture , _sampler , _uniformName , _unit ) { }
16531665
16541666 deleteTexture ( { gpuTexture } ) {
1655- gpuTexture . destroy ( ) ;
1667+ this . _postSubmitCallbacks . push ( ( ) => gpuTexture . destroy ( ) ) ;
16561668 }
16571669
16581670 _getLightShader ( ) {
@@ -2212,6 +2224,7 @@ function rendererWebGPU(p5, fn) {
22122224 if ( ! this . pixelReadBuffer || this . pixelReadBufferSize < requiredSize ) {
22132225 // Clean up old buffer
22142226 if ( this . pixelReadBuffer ) {
2227+ this . flushDraw ( ) ;
22152228 this . pixelReadBuffer . destroy ( ) ;
22162229 }
22172230
@@ -2278,21 +2291,26 @@ function rendererWebGPU(p5, fn) {
22782291 }
22792292
22802293 recreateFramebufferTextures ( framebuffer ) {
2294+ this . flushDraw ( ) ;
22812295 // Clean up existing textures
22822296 if ( framebuffer . colorTexture && framebuffer . colorTexture . destroy ) {
2283- framebuffer . colorTexture . destroy ( ) ;
2297+ const tex = framebuffer . colorTexture ;
2298+ this . _postSubmitCallbacks . push ( ( ) => tex . destroy ( ) ) ;
22842299 framebuffer . colorTextureView = null ;
22852300 }
22862301 if ( framebuffer . aaColorTexture && framebuffer . aaColorTexture . destroy ) {
2287- framebuffer . aaColorTexture . destroy ( ) ;
2302+ const tex = framebuffer . aaColorTexture ;
2303+ this . _postSubmitCallbacks . push ( ( ) => tex . destroy ( ) ) ;
22882304 framebuffer . aaColorTextureView = null ;
22892305 }
22902306 if ( framebuffer . depthTexture && framebuffer . depthTexture . destroy ) {
2291- framebuffer . depthTexture . destroy ( ) ;
2307+ const tex = framebuffer . depthTexture ;
2308+ this . _postSubmitCallbacks . push ( ( ) => tex . destroy ( ) ) ;
22922309 framebuffer . depthTextureView = null ;
22932310 }
22942311 if ( framebuffer . aaDepthTexture && framebuffer . aaDepthTexture . destroy ) {
2295- framebuffer . aaDepthTexture . destroy ( ) ;
2312+ const tex = framebuffer . aaDepthTexture ;
2313+ this . _postSubmitCallbacks . push ( ( ) => tex . destroy ( ) ) ;
22962314 framebuffer . aaDepthTextureView = null ;
22972315 }
22982316
@@ -2455,9 +2473,11 @@ function rendererWebGPU(p5, fn) {
24552473 }
24562474
24572475 _deleteFramebufferTexture ( texture ) {
2476+ this . flushDraw ( ) ;
24582477 const handle = texture . rawTexture ( ) ;
24592478 if ( handle . texture && handle . texture . destroy ) {
2460- handle . texture . destroy ( ) ;
2479+ const tex = handle . texture ;
2480+ this . _postSubmitCallbacks . push ( ( ) => tex . destroy ( ) ) ;
24612481 }
24622482 this . textures . delete ( texture ) ;
24632483 }
@@ -2468,14 +2488,18 @@ function rendererWebGPU(p5, fn) {
24682488 }
24692489
24702490 deleteFramebufferResources ( framebuffer ) {
2491+ this . flushDraw ( ) ;
24712492 if ( framebuffer . colorTexture && framebuffer . colorTexture . destroy ) {
2472- framebuffer . colorTexture . destroy ( ) ;
2493+ const tex = framebuffer . colorTexture ;
2494+ this . _postSubmitCallbacks . push ( ( ) => tex . destroy ( ) ) ;
24732495 }
24742496 if ( framebuffer . depthTexture && framebuffer . depthTexture . destroy ) {
2475- framebuffer . depthTexture . destroy ( ) ;
2497+ const tex = framebuffer . depthTexture ;
2498+ this . _postSubmitCallbacks . push ( ( ) => tex . destroy ( ) ) ;
24762499 }
24772500 if ( framebuffer . aaDepthTexture && framebuffer . aaDepthTexture . destroy ) {
2478- framebuffer . aaDepthTexture . destroy ( ) ;
2501+ const tex = framebuffer . aaDepthTexture ;
2502+ this . _postSubmitCallbacks . push ( ( ) => tex . destroy ( ) ) ;
24792503 }
24802504 }
24812505
0 commit comments