-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
User provided mesh binding / per-instance mesh data #13373
Comments
@pcwalton Tagging you because you suggested this as a solution, and it's a great idea that would really help advance using bevy for art :). Also, in reviewing the feasibility of implementing this, the most gnarly part passes through the preprocessing compute shader bits which you wrote. My approach is basically going to be this:
Does this general approach make sense? Do you have any other more general advice for how to approach this? |
One of the problems here is that we have a lot of nice wrappers around things like gpu buffers, creating an "erased" variant for each one is a bit unfortunate. If we permitted the pollution of lots of the rendering code with a user supplied generic type, it would significantly help the implementation, but I'm not sure how to implement the default case where there is no user supplied mesh instance data without specialization (which would still make the code much uglier). |
Because of mesh preprocessing, users cannot rely on `@builtin(instance_index)` in order to reference external data, as the instance index is not stable, either from frame to frame or relative to the total spawn order of mesh instances. Add a user supplied mesh index that can be used for referencing external data when drawing instanced meshes. Fixes bevyengine#13373
# Objective Because of mesh preprocessing, users cannot rely on `@builtin(instance_index)` in order to reference external data, as the instance index is not stable, either from frame to frame or relative to the total spawn order of mesh instances. ## Solution Add a user supplied mesh index that can be used for referencing external data when drawing instanced meshes. Closes #13373 ## Testing Benchmarked `many_cubes` showing no difference in total frame time. ## Showcase https://github.com/user-attachments/assets/80620147-aafc-4d9d-a8ee-e2149f7c8f3b --------- Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
What problem does this solve or what need does it fill?
A common pattern in creative coding is to use the instance index in a shader in order to sample from something like a texture or read from a SSBO. For example, the shader might use one texture in order to offset the position of the mesh and a second texture in order to determine the vertex color.
Currently, batch rendered entities (i.e. entities that share the same mesh and material) are instanced in a random order relative to how they were spawned. This is due a variety of causes:
Consequently, because the instance index is "random", it's not possible in a custom shader to know where to sample from an input texture.
See the following pseudo UV maps to see what this looks like with different rendering features enabled:
![](/https://private-user-images.githubusercontent.com/10366310/330599072-01e09e9c-3d85-4ba8-8603-f779a46c14d9.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzMDQzMzIsIm5iZiI6MTczOTMwNDAzMiwicGF0aCI6Ii8xMDM2NjMxMC8zMzA1OTkwNzItMDFlMDllOWMtM2Q4NS00YmE4LTg2MDMtZjc3OWE0NmMxNGQ5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjExVDIwMDAzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTI0OGMxNGEzYzcxNDQwNTNhNWJlYTc0ZDk3MDVjYzBmNjc5MGZhZWY1ZDEyNjc2MjMyMWI2ZDZlNDFjMGJmY2EmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.-51atLCgKiwtA3ImXQE-CwsuCPf3GFZmWNxneP2bVcE)
![](/https://private-user-images.githubusercontent.com/10366310/330599078-d679f4af-f151-4c9c-ad6d-aee7f959828d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzMDQzMzIsIm5iZiI6MTczOTMwNDAzMiwicGF0aCI6Ii8xMDM2NjMxMC8zMzA1OTkwNzgtZDY3OWY0YWYtZjE1MS00YzljLWFkNmQtYWVlN2Y5NTk4MjhkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjExVDIwMDAzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTRiMWNkZGY3ZWM2MzA5NGM2MjUwMjMyYTU1Mzg3MDc0YjA3Y2E2MWNmMmFkNmYxMjVjMGE0NDRkNzAxM2RjMTYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.2KjTp8ZbWGPUQx-vU4I54zrYyL_ttbhfEA6xkOwVubY)
What solution would you like?
The user should be able to provide a custom binding representing per-instance mesh data, i.e. a SSBO that they can index into with the instance index to retrieve additional data required for their vertex shader.
The difficult part here is that in order to write into the user data buffer, it needs to be plumbed everywhere the
MeshUniform
is created, including our new preprocessing compute shader. In order not to pollute all our rendering code, this means we need some kind of generic plugin for instance data that gets type erased in our rendering code so that it can optionally injected everywhere it needs to go.What alternative(s) have you considered?
We could, alternatively, track some kind of total "spawn order" and add this as a new index to
MeshUniform
. The user could then bind their buffer to their material and index into that instead. This has a few disadvantages:u32
to every mesh uniform that is likely unnecessary for most users.Additional context
There's prior art here I'm happy to reference in terms of how this typically works in creative coding applications.
The text was updated successfully, but these errors were encountered: