gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
12.41k stars 909 forks source link

Improve error message for invalid @location types. #5184

Open ted-dokos opened 8 months ago

ted-dokos commented 8 months ago

Motivation I'm a newbie to writing shaders. I recently tried to use @location incorrectly:

struct Rotor {
    s: f32,
    xy: f32,
    xz: f32,
    yz: f32
}

struct InstanceInput {
    @location(5) position: vec3<f32>,
    @location(6) scale: f32,
    @location(7) rotation: Rotor, // <-- This is the problem
};

@vertex fn main(in : InstanceInput) -> @builtin(position) vec4f { return vec4f(); }

The error message I got confused me:

> naga .\x.wgsl
error: 
   ┌─ x.wgsl:8:1
   │
 8 │ ╭ struct InstanceInput {
 9 │ │     @location(5) position: vec3<f32>,
10 │ │     @location(6) scale: f32,
11 │ │     @location(7) rotation: Rotor, // <-- This is the problem
   │ ╰────────────────────────────────^ naga::Type [4]

Entry point main at Vertex is invalid:
        Argument 0 varying error
        The type [2] does not match the varying

I had trouble understanding what I was doing wrong. I later learned from the WGSL Spec that @location can only be used with numeric scalars or vectors.

By contrast, the error from tint was a lot easier to understand:

cannot apply @location to declaration of type 'Rotor'
@location must only be applied to declarations of numeric scalar or numeric vector type

Proposed solution There is already some code to test against IO_SHAREABLE. We would just need to change the message.

I'd happily volunteer to make this change. Just let me know if this is an acceptable proposal.

teoxoy commented 8 months ago

I'd happily volunteer to make this change. Just let me know if this is an acceptable proposal.

Improving the error message would be great!