Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPURenderer: recycleBuffer() - reduce buffer creation overhead. #29341

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/webgpu_multiple_rendertargets_readback.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@

readbackTarget = new THREE.RenderTarget( size, size, { count: 2 } );

pixelBuffer = new Uint8Array( size ** 2 * 4 ).fill( 0 );
pixelBufferTexture = new THREE.DataTexture( pixelBuffer, size, size );
pixelBufferTexture.type = THREE.UnsignedByteType;
pixelBufferTexture.format = THREE.RGBAFormat;
Expand Down Expand Up @@ -175,6 +174,8 @@

const selection = options.selection;

if ( pixelBuffer !== undefined ) renderer.recycleBuffer( pixelBuffer );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be executed automatically in readRenderTargetPixelsAsync? It would be nice if we could hide renderer.recycleBuffer() from users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know what the lifetime of the returned buffer is after it is in the hands of the developer, unless you mean they could pass it in as a parameter to readRenderTargetPixelAsync( ..., bufferFromLastCall )?

Copy link
Collaborator

@sunag sunag Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if the user use RenderTarget.dispose() or reuse readRenderTargetPixelAsync()? Maybe we could link the buffer with the RenderTarget?


if ( selection === 'diffuse' ) {

pixelBuffer = await renderer.readRenderTargetPixelsAsync( readbackTarget, 0, 0, width, height, 0 ); // zero is optional
Expand Down
6 changes: 6 additions & 0 deletions src/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,12 @@ class Renderer {

}

recycleBuffer( buffer ) {

this.backend.recycleBuffer( buffer );

}

_projectObject( object, camera, groupOrder, renderList ) {

if ( object.visible === false ) return;
Expand Down
6 changes: 6 additions & 0 deletions src/renderers/webgl-fallback/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,12 @@ class WebGLBackend extends Backend {

}

recycleBuffer( buffer ) {

this.textureUtils.recycleBuffer( buffer );

}

_setFramebuffer( descriptor ) {

const { gl, state } = this;
Expand Down
43 changes: 42 additions & 1 deletion src/renderers/webgl-fallback/utils/WebGLTextureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class WebGLTextureUtils {

}

this.bufferCache = new WeakSet();
this.readBufferCache = {};

}

_init( gl ) {
Expand Down Expand Up @@ -835,7 +838,24 @@ class WebGLTextureUtils {

await backend.utils._clientWaitAsync();

const dstBuffer = new typedArrayType( byteLength / typedArrayType.BYTES_PER_ELEMENT );
const cacheList = this.readBufferCache[ byteLength ];

let dstBuffer;

if ( cacheList !== undefined ) {

const arrayBuffer = cacheList.pop();
if ( arrayBuffer !== undefined ) dstBuffer = new typedArrayType( arrayBuffer );

}

if ( dstBuffer === undefined ) {

dstBuffer = new typedArrayType( byteLength / typedArrayType.BYTES_PER_ELEMENT );

}

this.bufferCache.add( dstBuffer );

gl.bindBuffer( gl.PIXEL_PACK_BUFFER, buffer );
gl.getBufferSubData( gl.PIXEL_PACK_BUFFER, 0, dstBuffer );
Expand All @@ -847,6 +867,27 @@ class WebGLTextureUtils {

}

recycleBuffer( buffer ) {

if ( this.bufferCache.has( buffer ) ) {

const arrayBuffer = buffer.buffer;

const cache = this.readBufferCache;
const cacheKey = arrayBuffer.byteLength;

let cacheList = cache[ cacheKey ];

if ( cacheList === undefined ) cacheList = cache[ cacheKey ] = [];

cacheList.push( arrayBuffer );

this.bufferCache.delete( buffer );

}

}

_getTypedArrayType( glType ) {

const { gl } = this;
Expand Down
5 changes: 5 additions & 0 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,11 @@ class WebGPUBackend extends Backend {

}

recycleBuffer( buffer ) {

return this.textureUtils.recycleBuffer( buffer );

}

initTimestampQuery( renderContext, descriptor ) {

Expand Down
60 changes: 52 additions & 8 deletions src/renderers/webgpu/utils/WebGPUTextureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class WebGPUTextureUtils {
this.depthTexture = new DepthTexture();
this.depthTexture.name = 'depthBuffer';

this.bufferCache = new WeakMap();
this.readBufferCache = {};

}

createSampler( texture ) {
Expand Down Expand Up @@ -381,12 +384,28 @@ class WebGPUTextureUtils {
let bytesPerRow = width * bytesPerTexel;
bytesPerRow = Math.ceil( bytesPerRow / 256 ) * 256; // Align to 256 bytes

const readBuffer = device.createBuffer(
{
size: width * height * bytesPerTexel,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
}
);
const size = width * height * bytesPerTexel;

const cacheList = this.readBufferCache[ size ];

let readBuffer;

if ( cacheList !== undefined ) {

readBuffer = cacheList.pop();

}

if ( readBuffer === undefined ) {

readBuffer = device.createBuffer(
{
size,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
}
);

}

const encoder = device.createCommandEncoder();

Expand All @@ -412,9 +431,34 @@ class WebGPUTextureUtils {

await readBuffer.mapAsync( GPUMapMode.READ );

const buffer = readBuffer.getMappedRange();
const buffer = new typedArrayType( readBuffer.getMappedRange() );

return new typedArrayType( buffer );
this.bufferCache.set( buffer, readBuffer );

return buffer;

}

recycleBuffer( buffer ) {

const bufferGPU = this.bufferCache.get( buffer );

if ( bufferGPU !== undefined ) {

bufferGPU.unmap();

const cache = this.readBufferCache;
const cacheKey = bufferGPU.size;

let cacheList = cache[ cacheKey ];

if ( cacheList === undefined ) cacheList = cache[ cacheKey ] = [];

cacheList.push( bufferGPU );

this.bufferCache.delete( buffer );

}

}

Expand Down