From a6caee7084fbb5907e58a25ba09336edab76868c Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 16 Aug 2026 05:11:15 -0700 Subject: [PATCH] Gate `@interpolate(linear)` behind a downlevel flag (#9972) --- CHANGELOG.md | 5 +++ naga/src/back/glsl/features.rs | 9 ++++- naga/src/back/glsl/mod.rs | 7 ++-- naga/src/back/hlsl/mod.rs | 1 + naga/src/back/msl/mod.rs | 1 + naga/src/back/spv/mod.rs | 1 + naga/src/valid/interface.rs | 9 ++++- naga/src/valid/mod.rs | 6 +++ naga/tests/in/wgsl/interpolate.toml | 1 + naga/tests/in/wgsl/interpolate_compat.toml | 2 + naga/tests/naga/validation.rs | 46 ++++++++++++++++++++++ naga/tests/naga/wgsl_errors.rs | 30 ++++++++++++++ wgpu-hal/src/gles/adapter.rs | 7 ++++ wgpu-hal/src/vulkan/adapter.rs | 3 +- wgpu-naga-bridge/src/lib.rs | 4 ++ wgpu-types/src/limits.rs | 7 ++++ 16 files changed, 132 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10f456006..c901074fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,9 +75,13 @@ Bottom level categories: - `TextureFormat::is_srgb()` has been renamed to `TextureFormat::has_srgb_suffix()` to clarify its function. By @kpreid in [#9758](https://github.com/gfx-rs/wgpu/pull/9758). - Remove the never-constructed `CreateBlasError::InvalidAabbStride` variant. `create_blas` takes no stride, so it could never be produced; AABB stride is validated at build time as `BuildAccelerationStructureError::InvalidAabbStride`. By @mstampfli in [#9935](https://github.com/gfx-rs/wgpu/pull/9935). +- Added `DownlevelFlags::LINEAR_INTERPOLATION`, indicating that the adapter supports `@interpolate(linear)`. It is absent on GLES/WebGL2, since GLSL ES has no `noperspective` qualifier. By @emilk in [#9972](https://github.com/gfx-rs/wgpu/pull/9972). + #### naga - `naga::valid::ValidationError` is now always returned boxed, to avoid `clippy::large_result_err` warning. By @beicause in [#9612](https://github.com/gfx-rs/wgpu/pull/9612) +- Added `naga::valid::Capabilities::LINEAR_INTERPOLATION`, which is now required in order to use `@interpolate(linear)`. By @emilk in [#9972](https://github.com/gfx-rs/wgpu/pull/9972). +- The GLSL backend's `MissingFeatures` error now names the GLSL version that lacks the features, e.g. `GLSL 300 es doesn't support the required feature(s): NOPERSPECTIVE_QUALIFIER`. By @emilk in [#9972](https://github.com/gfx-rs/wgpu/pull/9972). ### Bug Fixes @@ -124,6 +128,7 @@ Bottom level categories: #### GLES - Avoid duplicate `EGL_SURFACE_TYPE` attributes when selecting an EGL framebuffer configuration, which caused Mesa to return no matching configurations. By @BlueJayLouche and @scroix in [#10048](https://github.com/gfx-rs/wgpu/pull/10048). +- `@interpolate(linear)` is now rejected by `Device::create_shader_module` on adapters that lack `DownlevelFlags::LINEAR_INTERPOLATION` (GLES/WebGL2), with a shader label and a source span. Previously such a shader validated fine and then failed at pipeline creation with `The selected version doesn't support Features(NOPERSPECTIVE_QUALIFIER)`. By @emilk in [#9972](https://github.com/gfx-rs/wgpu/pull/9972). - Fixed signed integer `%` (and `%=`) returning the wrong result for negative operands in the GLSL (OpenGL/GLES) backend, e.g. `-1 % 768` yielding `255` instead of `-1`. GLSL's `%` is undefined when either operand is negative, so signed remainder is now lowered as `a - b * (a / b)`, matching the SPIR-V, HLSL, and Metal backends. By @mstampfli in [#9687](https://github.com/gfx-rs/wgpu/pull/9687). - Fix negative argument for `atomicSub` yielding incorrect GLSL. By @ErichDonGubler in [#9924](https://github.com/gfx-rs/wgpu/pull/9924). diff --git a/naga/src/back/glsl/features.rs b/naga/src/back/glsl/features.rs index 366405c57..68a864a1e 100644 --- a/naga/src/back/glsl/features.rs +++ b/naga/src/back/glsl/features.rs @@ -62,6 +62,13 @@ bitflags::bitflags! { } } +impl core::fmt::Display for Features { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + // `Debug` would print this as `Features(A | B)`; we only want `A | B`. + bitflags::parser::to_writer(self, f) + } +} + /// Helper structure used to store the required [`Features`] needed to output a /// [`Module`](crate::Module) /// @@ -144,7 +151,7 @@ impl FeaturesManager { if missing.is_empty() { Ok(()) } else { - Err(Error::MissingFeatures(missing)) + Err(Error::MissingFeatures(missing, version)) } } diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index 7ef406cce..41f301fa5 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -416,9 +416,9 @@ pub enum Error { FmtError(#[from] FmtError), /// The specified [`Version`] doesn't have all required [`Features`]. /// - /// Contains the missing [`Features`]. - #[error("The selected version doesn't support {0:?}")] - MissingFeatures(Features), + /// Contains the missing [`Features`], and the version that lacks them. + #[error("GLSL {1} doesn't support the required feature(s): {0}")] + MissingFeatures(Features, Version), /// [`AddressSpace::Immediate`](crate::AddressSpace::Immediate) was used more than /// once in the entry point, which isn't supported. #[error("Multiple immediates aren't supported")] @@ -506,4 +506,5 @@ pub fn supported_capabilities() -> valid::Capabilities { | Caps::MEMORY_DECORATION_COHERENT | Caps::MEMORY_DECORATION_VOLATILE | Caps::STORAGE_TEXTURE_16BIT_NORM_FORMATS + | Caps::LINEAR_INTERPOLATION } diff --git a/naga/src/back/hlsl/mod.rs b/naga/src/back/hlsl/mod.rs index 3222c24a1..ad8a5611e 100644 --- a/naga/src/back/hlsl/mod.rs +++ b/naga/src/back/hlsl/mod.rs @@ -830,4 +830,5 @@ pub fn supported_capabilities() -> crate::valid::Capabilities { // No DRAW_INDEX // No MEMORY_DECORATION_VOLATILE | Caps::MEMORY_DECORATION_COHERENT + | Caps::LINEAR_INTERPOLATION } diff --git a/naga/src/back/msl/mod.rs b/naga/src/back/msl/mod.rs index 45a3a8ac4..c16bc637d 100644 --- a/naga/src/back/msl/mod.rs +++ b/naga/src/back/msl/mod.rs @@ -875,6 +875,7 @@ pub fn supported_capabilities() -> crate::valid::Capabilities { // No DRAW_INDEX // No MEMORY_DECORATION_VOLATILE | Caps::MEMORY_DECORATION_COHERENT + | Caps::LINEAR_INTERPOLATION } #[test] diff --git a/naga/src/back/spv/mod.rs b/naga/src/back/spv/mod.rs index c5c817fcd..64e1bb1d5 100644 --- a/naga/src/back/spv/mod.rs +++ b/naga/src/back/spv/mod.rs @@ -1226,4 +1226,5 @@ pub fn supported_capabilities() -> crate::valid::Capabilities { | Caps::DRAW_INDEX | Caps::MEMORY_DECORATION_COHERENT | Caps::MEMORY_DECORATION_VOLATILE + | Caps::LINEAR_INTERPOLATION } diff --git a/naga/src/valid/interface.rs b/naga/src/valid/interface.rs index 22011750e..307c43c33 100644 --- a/naga/src/valid/interface.rs +++ b/naga/src/valid/interface.rs @@ -785,12 +785,17 @@ impl VaryingContext<'_> { // qualifiers, so we won't complain about that here. let _ = sampling; - let required = match sampling { + let mut required = match sampling { Some(crate::Sampling::Sample) => Capabilities::MULTISAMPLED_SHADING, _ => Capabilities::empty(), }; + if interpolation == Some(crate::Interpolation::Linear) { + required |= Capabilities::LINEAR_INTERPOLATION; + } if !self.capabilities.contains(required) { - return Err(VaryingError::UnsupportedCapability(required)); + return Err(VaryingError::UnsupportedCapability( + required - self.capabilities, + )); } if interpolation != Some(crate::Interpolation::PerVertex) { diff --git a/naga/src/valid/mod.rs b/naga/src/valid/mod.rs index 0d7863e53..05303c152 100644 --- a/naga/src/valid/mod.rs +++ b/naga/src/valid/mod.rs @@ -218,6 +218,12 @@ bitflags::bitflags! { const MEMORY_DECORATION_VOLATILE = 1 << 42; /// Support for 16-bit integer types. const SHADER_INT16 = 1 << 43; + /// Support for [`Interpolation::Linear`] (`@interpolate(linear)` in WGSL). + /// + /// This is core WebGPU, but GLSL ES (and thus WebGL) has no `noperspective` qualifier (unless enabled by extensions). + /// + /// [`Interpolation::Linear`]: crate::Interpolation::Linear + const LINEAR_INTERPOLATION = 1 << 44; } } diff --git a/naga/tests/in/wgsl/interpolate.toml b/naga/tests/in/wgsl/interpolate.toml index bb9f69738..1f4b8b6df 100644 --- a/naga/tests/in/wgsl/interpolate.toml +++ b/naga/tests/in/wgsl/interpolate.toml @@ -1,4 +1,5 @@ targets = "SPIRV | METAL | HLSL | WGSL" +capabilities = "MULTISAMPLED_SHADING | LINEAR_INTERPOLATION" [glsl] version.Desktop = 400 diff --git a/naga/tests/in/wgsl/interpolate_compat.toml b/naga/tests/in/wgsl/interpolate_compat.toml index 57f942c84..e0e255b83 100644 --- a/naga/tests/in/wgsl/interpolate_compat.toml +++ b/naga/tests/in/wgsl/interpolate_compat.toml @@ -1,3 +1,5 @@ +capabilities = "MULTISAMPLED_SHADING | LINEAR_INTERPOLATION" + [glsl] version.Desktop = 400 writer_flags = "" diff --git a/naga/tests/naga/validation.rs b/naga/tests/naga/validation.rs index b6e32fc40..66e048d60 100644 --- a/naga/tests/naga/validation.rs +++ b/naga/tests/naga/validation.rs @@ -538,6 +538,52 @@ fn no_flat_first_in_glsl() { )); } +#[test] +fn no_linear_interpolation_in_glsl_es() { + use dummy_interpolation_shader::DummyInterpolationShader; + + let DummyInterpolationShader { + source: _, + module, + interpolate_attr, + entry_point, + } = DummyInterpolationShader::new(naga::Interpolation::Linear, None); + + let mut validator = naga::valid::Validator::new(Default::default(), valid::Capabilities::all()); + let module_info = validator.validate(&module).unwrap(); + + let options = naga::back::glsl::Options { + version: naga::back::glsl::Version::Embedded { + version: 300, + is_webgl: true, + }, + ..Default::default() + }; + let pipeline_options = naga::back::glsl::PipelineOptions { + shader_stage: naga::ShaderStage::Fragment, + entry_point: entry_point.to_owned(), + multiview: None, + }; + let err = naga::back::glsl::Writer::new( + String::new(), + &module, + &module_info, + &options, + &pipeline_options, + Default::default(), + ) + .err() + .unwrap_or_else(|| { + panic!("`{interpolate_attr}` should fail backend validation on GLSL ES"); + }); + + // The message must name the version and the feature, so that it is actionable. + assert_eq!( + err.to_string(), + "GLSL 300 es doesn't support the required feature(s): NOPERSPECTIVE_QUALIFIER" + ); +} + mod dummy_interpolation_shader { pub struct DummyInterpolationShader { pub source: String, diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index 0e75454a1..60f20c62c 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -1288,6 +1288,36 @@ fn per_vertex_capability() { } } +#[test] +fn linear_interpolation_capability() { + // Regression test for https://github.com/gfx-rs/wgpu/issues/9971: `@interpolate(linear)` + // has no GLSL ES equivalent, so it must be rejected during validation rather than + // silently passing and then failing in the GLSL backend at pipeline creation. + let source = r#" + @fragment + fn fs_main(@location(0) @interpolate(linear) v: f32) -> @location(0) vec4 { + return vec4(v, 0.0, 0.0, 1.0); + } + "#; + + check_one_validation! { + source, + Err(naga::valid::ValidationError::EntryPoint { + stage: naga::ShaderStage::Fragment, + source: valid::EntryPointError::Argument( + 0, + valid::VaryingError::UnsupportedCapability(Capabilities::LINEAR_INTERPOLATION), + ), + .. + }) + } + + no_validation_error( + source, + Capabilities::default() | Capabilities::LINEAR_INTERPOLATION, + ); +} + #[test] fn multiple_enables_valid() { check_success( diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index a07a6647b..39b027db4 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -439,6 +439,13 @@ impl super::Adapter { wgt::DownlevelFlags::MULTISAMPLED_SHADING, supported((3, 2), (4, 0)) || extensions.contains("OES_sample_variables"), ); + // GLSL ES has no `noperspective` qualifier, so `@interpolate(linear)` is only + // expressible on desktop GLSL (where we require at least 330, well past the 130 + // that introduced `noperspective`). + downlevel_flags.set( + wgt::DownlevelFlags::LINEAR_INTERPOLATION, + !shading_language_version.is_es(), + ); let query_buffers = extensions.contains("GL_ARB_query_buffer_object") || extensions.contains("GL_AMD_query_buffer_object"); if query_buffers { diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index 46750496f..42de411a2 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -696,7 +696,8 @@ impl PhysicalDeviceFeatures { | Df::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES | Df::NONBLOCKING_QUERY_RESOLVE | Df::SHADER_F16_IN_F32 - | Df::MSL2_1; + | Df::MSL2_1 + | Df::LINEAR_INTERPOLATION; dl_flags.set( Df::SURFACE_VIEW_FORMATS, diff --git a/wgpu-naga-bridge/src/lib.rs b/wgpu-naga-bridge/src/lib.rs index d2e265bca..1791f152f 100644 --- a/wgpu-naga-bridge/src/lib.rs +++ b/wgpu-naga-bridge/src/lib.rs @@ -183,6 +183,10 @@ pub fn features_to_naga_capabilities( Caps::RAY_TRACING_PIPELINE, features.intersects(wgt::Features::EXPERIMENTAL_RAY_TRACING_PIPELINES), ); + caps.set( + Caps::LINEAR_INTERPOLATION, + downlevel.contains(wgt::DownlevelFlags::LINEAR_INTERPOLATION), + ); caps } diff --git a/wgpu-types/src/limits.rs b/wgpu-types/src/limits.rs index f911f2c5d..fbcf767a9 100644 --- a/wgpu-types/src/limits.rs +++ b/wgpu-types/src/limits.rs @@ -1236,6 +1236,13 @@ bitflags::bitflags! { /// /// See . const TEXTURE_COMPRESSION = 1 << 25; + + /// Supports `@interpolate(linear)` (a.k.a. `noperspective`) on shader inter-stage + /// variables. + /// + /// GLSL ES has no `noperspective` qualifier, so the GLES backend only supports this + /// on desktop OpenGL, not on GLES/WebGL2. + const LINEAR_INTERPOLATION = 1 << 26; } }