-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add associated constant for WGSL struct definition #44
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! And sorry for the delayed review.
- Uses
String
everywhere. It might be possible to use const_str to optimize it toconst &str
but I don't think that's really necessary.
I think it would be ideal if the methods would return &'static str
since we have all the required information at compile time.
- Rust
Atomic<u32>
is translated to WGSLatomic<u32>
, not sure if that really makes sense but it's intuitive at least.
What I had in mind for atomics is a new #[shader_atomic]
attribute that can be used on any u32
/i32
since I don't think you'd want cpu atomics to get translated to gpu atomics.
Regarding the new trait, I think we should either add a separate derive for it or have a shader_declaration
method on ShaderType
where it would return an Option
.
Let me know what you think!
Thanks for the review, I addressed most of the comments. --
This was trickier than expected as traits and const don't work too well together. As such there is now an arbitrary limit of 8912 characters (~300+ fields).
Could you explain the rationale? |
Oh wow, I also ran into quite a few rough edges with rust's const stuff, especially when throwing traits in the mix and this seems to be another. I like the solution you came up with! I don't think anyone will use structs that big and an easy solution would be to break them down into multiple smaller structs; at least until rust supports more const features.
My main concern was compile time and binary size but I guess if you don't use the declaration it won't be part of the binary. Would you be able to test the compile time impact of the new functionality? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The syn version bump and clippy lint are fixed on main, could you rebase?
@@ -2,7 +2,7 @@ | |||
name = "encase" | |||
version = "0.6.1" | |||
edition = "2021" | |||
rust-version = "1.63" | |||
rust-version = "1.64" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to bump the MSRV?
@@ -18,7 +18,7 @@ Having to manually lay out data into GPU buffers can become very tedious and err | |||
|
|||
The core trait is [`ShaderType`] which mainly contains metadata about the given type. | |||
|
|||
The [`WriteInto`], [`ReadFrom`] and [`CreateFrom`] traits represent the ability of a type to be written into the buffer, read from the buffer and created from the buffer respectively. | |||
The [`WriteInto`], [`ReadFrom`] and [`CreateFrom`] traits represent the ability of a type to be written into the buffer, read from the buffer and created from the buffer respectively. The [`ShaderStructDeclaration`] trait allows to generate the WGSL struct definition. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The [`WriteInto`], [`ReadFrom`] and [`CreateFrom`] traits represent the ability of a type to be written into the buffer, read from the buffer and created from the buffer respectively. The [`ShaderStructDeclaration`] trait allows to generate the WGSL struct definition. | |
The [`WriteInto`], [`ReadFrom`] and [`CreateFrom`] traits represent the ability of a type to be written into the buffer, read from the buffer and created from the buffer respectively. The [`ShaderStructDeclaration`] trait contains the WGSL struct definition. |
@@ -14,7 +14,7 @@ pub use syn; | |||
#[macro_export] | |||
macro_rules! implement { | |||
($path:expr) => { | |||
#[proc_macro_derive(ShaderType, attributes(align, size))] | |||
#[proc_macro_derive(ShaderType, attributes(align, size, shader_atomic))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for implementing the new attribute!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you mention it in the docs here?
Line 22 in 308bb72
/// Used to implement `ShaderType` for structs |
} else if attr.meta.path().is_ident("shader_atomic") { | ||
match attr.meta.require_path_only() { | ||
Ok(_) => { | ||
data.shader_atomic = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to make sure that this is only set for integer scalars. The validation could go in generate_field_trait_constraints
but I think we need some new metadata for integer scalars to distinguish them from other types.
Closes #7
Notes:
String
everywhere. It might be possible to use const_str to optimize it toconst &str
but I don't think that's really necessary.Atomic<u32>
is translated to WGSLatomic<u32>
, not sure if that really makes sense but it's intuitive at least.include_wgsl!
macro.