feat: add renderer method .copyBufferToBuffer()#33044
Open
thelazylamaGit wants to merge 2 commits intomrdoob:devfrom
Open
feat: add renderer method .copyBufferToBuffer()#33044thelazylamaGit wants to merge 2 commits intomrdoob:devfrom
thelazylamaGit wants to merge 2 commits intomrdoob:devfrom
Conversation
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Exposes webGPU api's
copyBufferToBuffer()method, much like howcopyTextureToTexture()and others are currently exposed.For the webGL fallback it uses
gl.copyBufferSubDataas the equivalent, however, I haven't properly tested this yet as im not too familiar with webGL and my main use case for this feature is when using compute shaders with webGPU.The backend functions are pretty light wrappers over the native api and accept bytes whilst the exposed method on the renderer takes in items and automatically converts to the correct amount of bytes based on the itemSize so users don't have to deal with byte conversions.
I've begun working on a physarum slime mold simulation and added this feature as I wanted a way to dynamically increase my particle buffer size without just setting a large maximum buffer size and early returning. I currently use a similar method for resizing storageTextures using
renderer.copyTextureToTexture()wherein I resize the texture and copy the old contents over.The idea was to do the same but for my particle buffer using the added
renderer.copyBufferToBuffer()method.Now this setup does work as expected, however, is quite inefficient because it constantly rebuilds the compute pipeline when ideally it should only rebuild the buffer's bind group. Also note the garbage collector does end up removing the old compute pipelines here but only because .dispose() is called before reassigning them, otherwise it would stack infinitely.
shapeMatch_hex-2-35-params_22026-02-22.22-34-13_AV1.webm
Now although this setup is quite inefficient, it behaves exactly as I want it to with old particles being copied over and new particles being added seamlessly. However, when attempting to rework the code so that it doesn't rebuild compute pipelines and instead uses a stable
computeKernelnode with a dyanmic dispatch size, it stops behaving as i'd like.computeKernel2026-02-22.23-09-16_AV1.webm
In this case the computePipelines stay stable but it seems like the buffer reference isn't correctly updated or gets duplicated somehow? when setting particle count below the inital count of 10, it doubles it so that there are 4 working moving particles but then 4 more static particles. Even stranger yet is that when then running my renderer/texture resize logic the compute shaders do update to use the new buffer but the old buffer doesn't get copied over so the old particles just fade out.
I've tried all sorts of things in an attempt to get this to work such as
but to no avail
There was a pr that aimed at allowing storageBuffers to be reassigned dynamically #32847 but either it doesn't fully work here or im missing something in my implementation.
Overall, I think exposing a
copyBufferToBuffermethod would be useful and fit in nicely with the other exposed methods such ascopyFramebufferToTextureandcopyTextureToTexture, it just seems there needs to be some kinks worked out with how StorageBufferAttributes are cached and or referenced in compute functions. Im not very familiar with the intricacies of TSL's binding & cache system so any help here would be much appreciated, thanks.