MCPcopy
hub / github.com/processing/p5.js / readFramebufferPixel

Method readFramebufferPixel

src/webgpu/p5.RendererWebGPU.js:3835–3869  ·  view source on GitHub ↗
(framebuffer, x, y)

Source from the content-addressed store, hash-verified

3833 }
3834
3835 async readFramebufferPixel(framebuffer, x, y) {
3836 this.flushDraw();
3837 // await this.finishDraw();
3838 // Ensure all pending GPU work is complete before reading pixels
3839 // await this.queue.onSubmittedWorkDone();
3840
3841 const bytesPerPixel = 4;
3842 const alignedBytesPerRow = this._alignBytesPerRow(bytesPerPixel);
3843 const bufferSize = alignedBytesPerRow;
3844
3845 const stagingBuffer = this._ensurePixelReadBuffer(bufferSize);
3846
3847 const commandEncoder = this.device.createCommandEncoder();
3848 commandEncoder.copyTextureToBuffer(
3849 {
3850 texture: framebuffer.colorTexture,
3851 origin: { x, y, z: 0 }
3852 },
3853 { buffer: stagingBuffer, bytesPerRow: alignedBytesPerRow },
3854 { width: 1, height: 1, depthOrArrayLayers: 1 }
3855 );
3856
3857 this.device.queue.submit([commandEncoder.finish()]);
3858
3859 await stagingBuffer.mapAsync(GPUMapMode.READ, 0, bufferSize);
3860 const mappedRange = stagingBuffer.getMappedRange(0, bufferSize);
3861 const pixelData = new Uint8Array(mappedRange);
3862 const result = [pixelData[0], pixelData[1], pixelData[2], pixelData[3]];
3863
3864
3865 this._ensurePixelsAreRGBA(framebuffer, result);
3866
3867 stagingBuffer.unmap();
3868 return result;
3869 }
3870
3871 async readFramebufferRegion(framebuffer, x, y, w, h) {
3872 this.flushDraw();

Callers

nothing calls this directly

Calls 5

flushDrawMethod · 0.95
_alignBytesPerRowMethod · 0.95
_ensurePixelsAreRGBAMethod · 0.95
finishMethod · 0.80

Tested by

no test coverage detected