Skip to content
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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

victorvde
Copy link

Closes #7

Notes:

  1. Uses String everywhere. It might be possible to use const_str to optimize it to const &str but I don't think that's really necessary.
  2. Rust Atomic<u32> is translated to WGSL atomic<u32>, not sure if that really makes sense but it's intuitive at least.
  3. This doesn't work great with the include_wgsl! macro.

Copy link
Owner

@teoxoy teoxoy left a 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.


  1. Uses String everywhere. It might be possible to use const_str to optimize it to const &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.


  1. Rust Atomic<u32> is translated to WGSL atomic<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!

src/types/array.rs Outdated Show resolved Hide resolved
derive/impl/src/lib.rs Outdated Show resolved Hide resolved
derive/impl/src/lib.rs Outdated Show resolved Hide resolved
derive/impl/src/lib.rs Outdated Show resolved Hide resolved
@victorvde
Copy link
Author

Thanks for the review, I addressed most of the comments.

--

I think it would be ideal if the methods would return &'static str since we have all the required information at compile time.

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).

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.

Could you explain the rationale?
The first option duplicates code and makes the feature harder to use. The derive already implements multiple traits anyway.
The second option makes a compile-time error into a run-time error which I really don't like.
One thing I considered was putting everything behind a Cargo feature flag, but I'm not sure if that would address any concerns.

@victorvde victorvde changed the title Add function to generate WGSL struct definition Add associated constant for WGSL struct definition Jul 29, 2023
@teoxoy
Copy link
Owner

teoxoy commented Aug 19, 2023

I think it would be ideal if the methods would return &'static str since we have all the required information at compile time.

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).

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.

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.

Could you explain the rationale? The first option duplicates code and makes the feature harder to use. The derive already implements multiple traits anyway. The second option makes a compile-time error into a run-time error which I really don't like. One thing I considered was putting everything behind a Cargo feature flag, but I'm not sure if that would address any concerns.

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?
If it's not that bad, we shouldn't need a feature flag for it.

Copy link
Owner

@teoxoy teoxoy left a 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"
Copy link
Owner

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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))]
Copy link
Owner

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!

Copy link
Owner

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?

/// 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;
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate WGSL to reduce duplication of definitions
2 participants