Swoorup / wgsl-bindgen

Generate typesafe Rust bindings for wgsl shaders in wgpu
https://crates.io/crates/wgsl_bindgen
MIT License
34 stars 6 forks source link

Allow re-use of bind groups #33

Open DCNick3 opened 3 months ago

DCNick3 commented 3 months ago

Thank you very much for this project, I can see it improving QoL when using wgsl very much!

I have a question/feature request: can you re-use bind groups with same layouts between different shader entries?

For example, I want to have a bind group for a texture consisting of a texture binding and a sampler that I use in multiple shader entries. As I understand it, with current codegen, I will have to create a separate bind group object for each entry, as the newtype wrappers would be incompatible.

Ideally, I would have a common newtype TextureBindGroup (either defined in my or in the generated code) which I would just store in the same struct as my texture and re-use it whenever I need to pass it to a shader.

Swoorup commented 3 months ago

Not quite sure since we use the naga oil syntax.

It seems you can choose only certain bind group entries in the current module. And also possibly override certain entries for the same group by using an import from another file?

If that weren't the case otherwise, it would have been a simple change.

Swoorup commented 2 months ago

Was thinking this, One way is to make all entries optional for the bond group that is common across modules. To support a mix match, could use enum. But it would make the code complex.

Swoorup commented 2 months ago

I kinda need this ATM myself. It would be great if you could provide an example files for your use case.

bconnorwhite commented 2 months ago

You might need to handle wgsl files individually, rather than using entry points.

My solution to this was to build a .rs file for each .wgsl file, and mirror the import path in Rust so that each entry point for example has access to the same shared bind group: https://github.com/bconnorwhite/wgsl_to_wgpu/tree/import-modules

Swoorup commented 2 months ago

You might need to handle wgsl files individually, rather than using entry points.

My solution to this was to build a .rs file for each .wgsl file, and mirror the import path in Rust so that each entry point for example has access to the same shared bind group: https://github.com/bconnorwhite/wgsl_to_wgpu/tree/import-modules

Think that should be fairly simple, if we just look up the mangled symbols. Just worried about the common bind group entry themselves being overridden, as that is allowed by naga_oil. That seems like a rare thing to do. I'll try to write up a prototype sometime.