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

Unify validation error presentation in Naga CLI and WebGPU compilation messages #6436

Conversation

ErichDonGubler
Copy link
Member

@ErichDonGubler ErichDonGubler commented Oct 22, 2024

Connections

Description

This PR changes the format of validation errors emitted by naga CLI and the compilation message API in WebGPU. It changes the presentation of the error in this shader:

fn sdfRect(samplePosition: vec2f, halfSize: vec2f) -> f32 {
    let componentWiseEdgeDistance = abs(samplePosition) - halfSize;
    let outsideDistance = length(max(componentWiseEdgeDistance, vec2f(0)));
    let insideDistance = min(max(componentWiseEdgeDistance.x, componentWiseEdgeDistance.y), 0);
    return outsideDistance + insideDistance;
}

…which is reported like this in Naga CLI:

error:
  ┌─ in.wgsl:1:1
  │
1 │ ╭ fn sdfRect(samplePosition: vec2f, halfSize: vec2f) -> f32 {
2 │ │     let componentWiseEdgeDistance = abs(samplePosition) - halfSize;
3 │ │     let outsideDistance = length(max(componentWiseEdgeDistance, vec2f(0)));
4 │ │     let insideDistance = min(max(componentWiseEdgeDistance.x, componentWiseEdgeDistance.y), 0);
  │ │                          ^^^ naga::Expression [12]
5 │ │     return outsideDistance + insideDistance;
  │ ╰────────────────────────────────────────────^ naga::Function [0]

Function [0] 'sdfRect' is invalid:
        Expression [12] is invalid
        Argument [1] to Min as expression [11] has an invalid type.

…and, particularly badly and motivating this PR, like this in consumers of the compilation message API in WebGPU:

Shader validation error: 
  ┌─ in.wgsl:1:1
  │  
1 │ ╭ fn sdfRect(samplePosition: vec2f, halfSize: vec2f) -> f32 {
2 │ │     let componentWiseEdgeDistance = abs(samplePosition) - halfSize;
3 │ │     let outsideDistance = length(max(componentWiseEdgeDistance, vec2f(0)));
4 │ │     let insideDistance = min(max(componentWiseEdgeDistance.x, componentWiseEdgeDistance.y), 0);
  │ │                          ^^^ naga::Expression [12]
5 │ │     return outsideDistance + insideDistance;
  │ ╰────────────────────────────────────────────^ naga::Function [0]

…to this in both contexts:

error: Function [0] 'sdfRect' is invalid
  ┌─ in.wgsl:1:1
  │
1 │ ╭ fn sdfRect(samplePosition: vec2f, halfSize: vec2f) -> f32 {
2 │ │     let componentWiseEdgeDistance = abs(samplePosition) - halfSize;
3 │ │     let outsideDistance = length(max(componentWiseEdgeDistance, vec2f(0)));
4 │ │     let insideDistance = min(max(componentWiseEdgeDistance.x, componentWiseEdgeDistance.y), 0);
  │ │                          ^^^ naga::Expression [12]
5 │ │     return outsideDistance + insideDistance;
  │ ╰────────────────────────────────────────────^ naga::Function [0]
  │
  = Expression [12] is invalid
  = Argument [1] to Min as expression [11] has an invalid type.

Testing

I have validated the output of this myself, as is hopefully demonstrated. No tests appeared to need to be updated. If we want snapshot coverage for Naga CLI's output, or our WebGPU API's compilation message items, I believe we should file a follow-up issue to address it.

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy. If applicable, add:
    • --target wasm32-unknown-unknown
    • --target wasm32-unknown-emscripten
  • Run cargo xtask test to run tests.
  • Add change to CHANGELOG.md. See simple instructions inside file.

@ErichDonGubler ErichDonGubler added area: ecosystem Help the connected projects grow and prosper api: webgpu Issues with direct interface with WebGPU naga Shader Translator kind: diagnostics Error message should be better labels Oct 22, 2024
@ErichDonGubler ErichDonGubler self-assigned this Oct 22, 2024
@ErichDonGubler ErichDonGubler requested a review from a team as a code owner October 22, 2024 00:07
@ErichDonGubler ErichDonGubler changed the title Unify Naga's validation errors and Unify validation error presentation in Naga CLI and WebGPU compilation messages Oct 22, 2024
Copy link
Member

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

Great seeing code getting removed even when more functionality is added!

For some reason, the implementation of `Display` for
`ShaderError<WithSpan<ValidationError>>>` is building its own
`Diagnostic` with _only_ the labels populated: no messages or notes.
I don't know why this is, but it seems to have a strict subset of the
information already present in the private implementation of
`WithSpan::diagnostic`.

Fix this by exposing `WithSpan::diagnostic` as `pub(crate)`, and
re-using its output in `impl Display for
ShaderError<WithSpan<ValidationError>>>`.
@ErichDonGubler ErichDonGubler force-pushed the erichdongubler/push-yswprqtqzkkv branch from d6a4c22 to 6b283f3 Compare October 22, 2024 18:31
@ErichDonGubler ErichDonGubler enabled auto-merge (rebase) October 22, 2024 18:31
@ErichDonGubler ErichDonGubler merged commit 7aa00a0 into gfx-rs:trunk Oct 22, 2024
27 checks passed
@ErichDonGubler ErichDonGubler deleted the erichdongubler/push-yswprqtqzkkv branch October 22, 2024 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: webgpu Issues with direct interface with WebGPU area: ecosystem Help the connected projects grow and prosper kind: diagnostics Error message should be better naga Shader Translator
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants