ScanMountGoat / wgsl_to_wgpu

Generate typesafe Rust bindings from WGSL shaders to wgpu
MIT License
42 stars 12 forks source link

Make this a procedural macro. #47

Closed andriyDev closed 11 months ago

andriyDev commented 1 year ago

I believe this could just be a procedural macro. Something like:

include_shader!("src/shader.wgsl");

This could write the wgsl to the OUT_DIR so the generated rs is only in the output directory (not committed to git for example). And now all users would have to do is call that macro for every shader they want to use.

ScanMountGoat commented 1 year ago

A procedural macro would output a token stream wherever the macro is called. There's no need to specify an output directory. In your example, the shader.wgsl file is already part of the project, so it could just be included in the generated code using include_str!. There are potential path issues with writing files from proc macros.

This crate used to be a procedural macro, but I eventually switched to making it a build script. The issue is that some code wants to pass in the shader source itself instead of a hardcoded file path. This is very easy to do with a build script. There's probably some way to make it work with a proc macro, but I feel a build script makes the intent clearer. You can always not commit the generated files to git and allow them to be regenerated if not present. I'm open to any ideas you may have to make the code generation process better.

ScanMountGoat commented 11 months ago

I'm closing this as I feel a build script is a better fit for the project. I've updated the readme to include information on how to properly write to OUT_DIR to avoid any issues with build.rs modifying source files. This also avoids needing to deal with git on those files. If you prefer the syntax of a proc macro, you can always make your own proc macro that parses the output of wgsl_to_wgpu to a TokenStream and outputs that.