Skip to content

[naga wgsl] Add to_wgsl functions for some Naga IR types. #7180

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

Merged
merged 6 commits into from
Mar 5, 2025

Conversation

jimblandy
Copy link
Member

Add to_wgsl functions that return the WGSL representations of the following types as &'static str values:

  • MathFunction
  • BuiltIn
  • Interpolation
  • Sampling

Use these functions in the WGSL backend.

Also add to_wgsl_debug functions that return some legible string for all possible values, not just those that are defined by the WGSL standard.

There are other types that it would make sense to do this for, like ImageDimension, but these are some of the types I ended up needing for diagnostics and logging in #6833, from which I'm trying to spin them out.

@jimblandy jimblandy force-pushed the naga-common-to-wgsl branch 2 times, most recently from 3e875d7 to 27d8268 Compare March 3, 2025 20:27
@jimblandy
Copy link
Member Author

I've tried to tidy this up a bit by introducing a trait, and trying to get some mileage out of that in error handling.

@jimblandy jimblandy force-pushed the naga-common-to-wgsl branch 3 times, most recently from d479b03 to 5498b85 Compare March 5, 2025 02:14
@jimblandy
Copy link
Member Author

Sorry for the churn in this non-draft PR. I found a few more things I needed.

@jimblandy jimblandy force-pushed the naga-common-to-wgsl branch 2 times, most recently from f306a9d to 5e26ca1 Compare March 5, 2025 02:19
Copy link
Member

@ErichDonGubler ErichDonGubler left a comment

Choose a reason for hiding this comment

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

I've taken too long to feel comfortable blocking this review. I think the new trait is handy, but have some (non-blocking) concerns with its durability over time.

@jimblandy jimblandy force-pushed the naga-common-to-wgsl branch 2 times, most recently from 4c17a35 to 05c8ad7 Compare March 5, 2025 18:41
@jimblandy jimblandy requested a review from ErichDonGubler March 5, 2025 18:42
Copy link
Member

@ErichDonGubler ErichDonGubler left a comment

Choose a reason for hiding this comment

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

Still LGTM! Added a couple of nits again. 😈

@jimblandy jimblandy force-pushed the naga-common-to-wgsl branch from 05c8ad7 to 76043e8 Compare March 5, 2025 19:09
Introduce a new variant of `naga::back::wgsl::Error`, `Unsupported`,
and let it subsume `UnsupportedMathFunction` and various uses of
`Custom`.

Introduce a helper function, `Error::unsupported`, for building these
errors.
Define new traits in `common::wgsl`, `ToWgsl` and `TryToWgsl`, for
getting the WGSL representation of some Naga IR types as `&'static
str` values:

- `MathFunction`
- `BuiltIn`
- `Interpolation`
- `Sampling`
- `StorageFormat`

Use these functions in the WGSL backend, taking advantage of
`TryToWgsl` to consolidate error reporting.
Move `back::vector_size_str` to `common`, so that front ends can use
it too. This commit is code motion and `use` adjustment only, there
should be no change in behavior.
Replace `naga::back::wgsl::writer::scalar_kind_str` with a `TryToWgsl`
implementation for `Scalar` in `common::wgsl`. Use this where needed
in the WGSL backend.
Replace `naga::back::wgsl::writer::image_dimension_str` with a
`ToWgsl` implementation for `ImageDimension` in `common::wgsl`. Use
this where needed in the WGSL backend.
Move `back::wgsl::address_space_str` to `common::wgsl`, so that the
front end can use it too. This commit is code motion and `use`
adjustment only; there should be no change in behavior.
@jimblandy jimblandy force-pushed the naga-common-to-wgsl branch from 76043e8 to 4ecd313 Compare March 5, 2025 19:11
@jimblandy jimblandy enabled auto-merge (rebase) March 5, 2025 19:11
@jimblandy jimblandy merged commit fc83073 into gfx-rs:trunk Mar 5, 2025
34 checks passed
@jimblandy jimblandy deleted the naga-common-to-wgsl branch March 6, 2025 01:11
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.

2 participants