@@ -454,9 +454,21 @@ function rendererWebGPU(p5, fn) {
454454 const _b = args [ 2 ] || 0 ;
455455 const _a = args [ 3 ] || 0 ;
456456
457- // If PENDING and no custom framebuffer, clear means stay UNPROMOTED
458- if ( this . _frameState === FRAME_STATE . PENDING && ! this . activeFramebuffer ( ) ) {
459- this . _frameState = FRAME_STATE . UNPROMOTED ;
457+ // If PENDING and no custom framebuffer, clear means stay UNPROMOTED.
458+ // However, if we are still in setup (frameCount == 0), we must promote
459+ // so that mainFramebuffer gets the cleared content. This ensures that if
460+ // draw() later promotes without a copy, it starts from the correct state
461+ // rather than a stale mainFramebuffer.
462+ // Note: a mid-draw-loop transition from UNPROMOTED back to PROMOTED
463+ // (i.e. calling background() some frames but not others) will still
464+ // lose intermediate UNPROMOTED frame content.
465+ if ( this . _frameState !== FRAME_STATE . PROMOTED && ! this . activeFramebuffer ( ) ) {
466+ if ( this . _pInst . frameCount > 0 ) {
467+ this . _frameState = FRAME_STATE . UNPROMOTED ;
468+ } else {
469+ this . _promoteToFramebufferWithoutCopy ( ) ;
470+ // clear() then targets mainFramebuffer via activeFramebuffer()
471+ }
460472 }
461473
462474 this . _finishActiveRenderPass ( ) ;
@@ -1143,8 +1155,11 @@ function rendererWebGPU(p5, fn) {
11431155
11441156 _resetBuffersBeforeDraw ( ) {
11451157 this . _finishActiveRenderPass ( ) ;
1158+
11461159 // Set state to PENDING - we'll decide on first draw
1147- this . _frameState = FRAME_STATE . PENDING ;
1160+ if ( this . _pInst . frameCount > 0 ) {
1161+ this . _frameState = FRAME_STATE . PENDING ;
1162+ }
11481163
11491164 // Clear depth buffer but DON'T start any render pass yet
11501165 const activeFramebuffer = this . activeFramebuffer ( ) ;
@@ -1255,6 +1270,8 @@ function rendererWebGPU(p5, fn) {
12551270 // once we're drawing to the framebuffer, because normally
12561271 // those are reset.
12571272 const savedModelMatrix = this . states . uModelMatrix . copy ( ) ;
1273+ this . states . uModelMatrix . set ( this . states . uModelMatrix . copy ( ) ) ;
1274+ this . states . uModelMatrix . reset ( ) ;
12581275 this . mainFramebuffer . defaultCamera . set ( this . states . curCamera ) ;
12591276
12601277 this . mainFramebuffer . begin ( ) ;
@@ -1263,6 +1280,11 @@ function rendererWebGPU(p5, fn) {
12631280 }
12641281
12651282 _promoteToFramebufferWithoutCopy ( ) {
1283+ // Already promoted this frame
1284+ if ( this . _frameState === FRAME_STATE . PROMOTED ) {
1285+ return ;
1286+ }
1287+
12661288 // Ensure mainFramebuffer matches canvas size
12671289 if ( this . mainFramebuffer . width !== this . width ||
12681290 this . mainFramebuffer . height !== this . height ) {
@@ -1277,6 +1299,8 @@ function rendererWebGPU(p5, fn) {
12771299
12781300 // Preserve transformation state
12791301 const savedModelMatrix = this . states . uModelMatrix . copy ( ) ;
1302+ this . states . uModelMatrix . set ( this . states . uModelMatrix . copy ( ) ) ;
1303+ this . states . uModelMatrix . reset ( ) ;
12801304 this . mainFramebuffer . defaultCamera . set ( this . states . curCamera ) ;
12811305
12821306 // Begin rendering to mainFramebuffer
@@ -1590,7 +1614,6 @@ function rendererWebGPU(p5, fn) {
15901614 }
15911615 this . flushDraw ( ) ;
15921616
1593- // this._pInst.background('red');
15941617 this . _pInst . push ( ) ;
15951618 this . states . setValue ( 'enableLighting' , false ) ;
15961619 this . states . setValue ( 'activeImageLight' , null ) ;
0 commit comments