gfx-rs / wgpu-rs

Rust bindings to wgpu native library
https://wgpu.rs
Mozilla Public License 2.0
1.69k stars 186 forks source link

Incorrect shader arg interpolation lint in v0.8? #883

Closed dhardy closed 3 years ago

dhardy commented 3 years ago

I'm updating KAS to v0.8, and get this error:

wgpu error: Validation Error

Caused by:
    In Device::create_render_pipeline
      note: label = `SR render_pipeline`
    error matching FRAGMENT shader requirements against the pipeline
    location[2] Float32x2 interpolated as Some(Flat) with sampling None is not provided by the previous stage outputs
    input interpolation doesn't match provided Some(Perspective)

This appears to be caused by use of flat interpolation in my fragment shader:

layout(location = 0) flat in vec4 fragColor;

This has nothing to do with Perspective projections; interpolating or not interpolating shader inputs is valid either way. Or maybe Perspective refers to something else than the view projection? Anyway, I don't "need" flat here but also see no reason not to use it (since I don't need interpolation of the colour input), so I think this validation error is simply wrong?

kvark commented 3 years ago

What it says is that your output color from VS must be interpolated in the same way. I.e. it has to be declared as layout(location = 0) flat out vec4 fragColor in VS.

kvark commented 3 years ago

cc @jimblandy just in case :)

dhardy commented 3 years ago

That solution works. Curious, because I thought I had already tried that only to hit shader compiler error.

Anyway, is this actually an error? On this page I find this quote:

While interpolation qualifiers can be put on any of these variables, the only ones that actually have an effect are those on the fragment shader. The pre-GL 4.3 rules on matching interfaces mean you have to provide them on other stages, but interpolation is controlled entirely by the qualifiers on the fragment shader input variables.

The specification (section 4.5) covers the interpolation modes but doesn't appear clear on where they may (or should) be used. Maybe that's elsewhere; I haven't read the whole thing.

kvark commented 3 years ago

The upstream investigation in https://github.com/gpuweb/gpuweb/issues/802 revealed that D3D requires the interpolation to match between stages. This is more strict than in Vulkan or Metal, and it became the choice for WGSL. Now, accepting SPIR-V in addition to WGSL puts us into a weird position: at the shader module level, we don't know which entry points are used with which. So we can't magically patch the vertex output interpolation...

The only reasonable choice for now that I see is that wgpu-rs expects SPIR-V to be sort of adjusted for WebGPU already. Interpolation is one of the aspects. There are also additional things, like gl_ClipDistance and gl_PointSize being unsupported.

dhardy commented 3 years ago

I guess we can just close this then.