-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[naga] Move WGSL type formatting code to common::wgsl
#7280
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffda18a
to
99f0658
Compare
Converted to draft because I realized there's a piece I forgot to hook up. |
99f0658
to
28ed012
Compare
Okay, all fixed |
1b523ed
to
d1ca26f
Compare
Document various functions in the WGSL backend. Clean up some `write!` and `writeln!` macro usage. No change in behavior.
Rename `naga::back::wgsl::Writer::write_value_type` to `write_type_inner`, since its job is generating WGSL code for a `TypeInner`. Add documentation.
This change is purely syntactic, and should have no effect on the meaning of the program.
In `naga::back::wgsl`, introduce a new trait, `TypeContext`, which provides all the context necessary to generate WGSL code for a type. `Writer::write_type` and `write_type_inner` become default methods on `TypeContext`. `Writer` replaces them with functions that simply create a `TypeContext` from the `Writer` and forward the call. For simplicity's sake, rather than trying to return errors for Naga IR that we can't generate WGSL for, just panic. Validation should have rejected any such values, and it is not practical to expect Naga backends when given invalid modules: backends need to be free to assume that handles are valid, that arenas are free of cycles, and so on.
Promote `src/common/wgsl.rs` to `src/common/wgsl/mod.rs`. No functional changes, just code motion.
Move the `TypeContext` trait from `naga::back::wgsl` into `naga::common::wgsl`. Adjust imports and publicity markers as needed. There should be no changes to behavior in this commit, only code motion.
In `naga::back::wgsl`, move `WriterTypeContext` out of the way, and coalesce `Writer`'s methods back into a single `impl` block. This is just code motion; there should be no change in behavior.
d1ca26f
to
e18779b
Compare
ErichDonGubler
approved these changes
Mar 12, 2025
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.
Commits made this 👨🏻🍳 💋 easy to review. LGTM!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
kind: refactor
Making existing function faster or nicer
lang: WGSL
WebGPU Shading Language
naga
Shader Translator
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Move the WGSL backend's code for printing types into
common::wgsl
, so that it can be used by the front end for printing error messages as well.This is preparation for #6833, which uses the builtin function database to produce very nice (well, for Naga, at least) diagnostics for ill-formed calls to builtin functions. However, those diagnostics depend on the front end having a way to print types at all.
This doesn't have any effect on behavior; it's just some code motion (and the refactoring needed to make that possible).
To make reviewing easier, each commit is independent. I felt it would be helpful to avoid mixing code motion with semantically interesting changes, so I went to some trouble for that. But as a result, some intermediate commits look a little odd: the backend's type writing code sort of gets excised out from
Writer
, and then adjusted to be more and more independent, up to the point that moving it out of the backend entirely requires no interesting code changes, justpub
anduse
adjustments.