@@ -175,7 +175,7 @@ function rendererWebGPU(p5, fn) {
175175 depthOrArrayLayers : 1 ,
176176 } ,
177177 format : this . depthFormat ,
178- usage : GPUTextureUsage . RENDER_ATTACHMENT ,
178+ usage : GPUTextureUsage . RENDER_ATTACHMENT | GPUTextureUsage . COPY_SRC ,
179179 } ) ;
180180 this . depthTextureView = this . depthTexture . createView ( ) ;
181181
@@ -864,8 +864,10 @@ function rendererWebGPU(p5, fn) {
864864 this . mainFramebuffer . resize ( this . width , this . height ) ;
865865 }
866866
867- // Copy canvas texture to mainFramebuffer
867+ // Copy canvas textures to mainFramebuffer
868868 const commandEncoder = this . device . createCommandEncoder ( ) ;
869+
870+ // Copy color texture
869871 commandEncoder . copyTextureToTexture (
870872 {
871873 texture : canvasTexture ,
@@ -884,19 +886,36 @@ function rendererWebGPU(p5, fn) {
884886 }
885887 ) ;
886888
889+ // Copy depth texture
890+ commandEncoder . copyTextureToTexture (
891+ {
892+ texture : this . depthTexture ,
893+ origin : { x : 0 , y : 0 , z : 0 } ,
894+ mipLevel : 0 ,
895+ } ,
896+ {
897+ texture : this . mainFramebuffer . depthTexture ,
898+ origin : { x : 0 , y : 0 , z : 0 } ,
899+ mipLevel : 0 ,
900+ } ,
901+ {
902+ width : Math . ceil ( this . width * this . _pixelDensity ) ,
903+ height : Math . ceil ( this . height * this . _pixelDensity ) ,
904+ depthOrArrayLayers : 1 ,
905+ }
906+ ) ;
907+
887908 this . _pendingCommandEncoders . push ( commandEncoder . finish ( ) ) ;
888909 this . _hasPendingDraws = true ;
889910
890- // Save current transformation state
911+ // We want to make sure the transformation state is the same
912+ // once we're drawing to the framebuffer, because normally
913+ // those are reset.
891914 const savedModelMatrix = this . states . uModelMatrix . copy ( ) ;
892-
893- // Copy current camera state to framebuffer's camera
894915 this . mainFramebuffer . defaultCamera . set ( this . states . curCamera ) ;
895916
896- // Activate mainFramebuffer for subsequent draws
897917 this . mainFramebuffer . begin ( ) ;
898918
899- // Restore transformation state
900919 this . states . uModelMatrix . set ( savedModelMatrix ) ;
901920 }
902921
@@ -2322,7 +2341,9 @@ function rendererWebGPU(p5, fn) {
23222341 // Create non-multisampled depth texture for texture binding (always needed)
23232342 const depthTextureDescriptor = {
23242343 ...depthBaseDescriptor ,
2325- usage : GPUTextureUsage . RENDER_ATTACHMENT | GPUTextureUsage . TEXTURE_BINDING ,
2344+ usage : GPUTextureUsage . RENDER_ATTACHMENT |
2345+ GPUTextureUsage . TEXTURE_BINDING |
2346+ ( framebuffer . _useCanvasFormat ? GPUTextureUsage . COPY_DST : 0 ) ,
23262347 sampleCount : 1 ,
23272348 } ;
23282349 framebuffer . depthTexture = this . device . createTexture ( depthTextureDescriptor ) ;
@@ -2423,6 +2444,9 @@ function rendererWebGPU(p5, fn) {
24232444 }
24242445
24252446 _getWebGPUDepthFormat ( framebuffer ) {
2447+ if ( framebuffer . _useCanvasFormat ) {
2448+ return this . depthFormat ;
2449+ }
24262450 if ( framebuffer . useStencil ) {
24272451 return framebuffer . depthFormat === constants . FLOAT ? 'depth32float-stencil8' : 'depth24plus-stencil8' ;
24282452 } else {
@@ -2523,10 +2547,7 @@ function rendererWebGPU(p5, fn) {
25232547 stagingBuffer . unmap ( ) ;
25242548 }
25252549
2526- // Convert BGRA to RGBA if reading from canvas-format framebuffer on BGRA systems
2527- if ( framebuffer . _useCanvasFormat && this . presentationFormat === 'bgra8unorm' ) {
2528- this . _convertBGRtoRGB ( result ) ;
2529- }
2550+ this . _ensurePixelsAreRGBA ( framebuffer , result ) ;
25302551
25312552 return result ;
25322553 }
@@ -2559,6 +2580,9 @@ function rendererWebGPU(p5, fn) {
25592580 const pixelData = new Uint8Array ( mappedRange ) ;
25602581 const result = [ pixelData [ 0 ] , pixelData [ 1 ] , pixelData [ 2 ] , pixelData [ 3 ] ] ;
25612582
2583+
2584+ this . _ensurePixelsAreRGBA ( framebuffer , result ) ;
2585+
25622586 stagingBuffer . unmap ( ) ;
25632587 return result ;
25642588 }
@@ -2611,6 +2635,7 @@ function rendererWebGPU(p5, fn) {
26112635 pixelData . set ( mappedData . subarray ( srcOffset , srcOffset + unalignedBytesPerRow ) , dstOffset ) ;
26122636 }
26132637 }
2638+ this . _ensurePixelsAreRGBA ( framebuffer , pixelData ) ;
26142639
26152640 // WebGPU doesn't need vertical flipping unlike WebGL
26162641 const region = new Image ( width , height ) ;
@@ -2655,13 +2680,19 @@ function rendererWebGPU(p5, fn) {
26552680 // Main canvas pixel methods
26562681 //////////////////////////////////////////////
26572682
2683+ _ensurePixelsAreRGBA ( framebuffer , result ) {
2684+ // Convert BGRA to RGBA if reading from canvas-format framebuffer on BGRA systems
2685+ if ( framebuffer . _useCanvasFormat && this . presentationFormat === 'bgra8unorm' ) {
2686+ this . _convertBGRtoRGB ( result ) ;
2687+ }
2688+ }
2689+
26582690 _convertBGRtoRGB ( pixelData ) {
26592691 for ( let i = 0 ; i < pixelData . length ; i += 4 ) {
26602692 const temp = pixelData [ i ] ;
26612693 pixelData [ i ] = pixelData [ i + 2 ] ;
26622694 pixelData [ i + 2 ] = temp ;
26632695 }
2664- return pixelData ;
26652696 }
26662697
26672698 async loadPixels ( ) {
0 commit comments