naga/front/
interpolator.rs

1/*!
2Interpolation defaults.
3*/
4
5impl crate::Binding {
6    /// Apply default interpolation (if applicable) for `ty` to the binding.
7    ///
8    /// This function is a utility front ends may use to satisfy the Naga IR's
9    /// requirement, meant to ensure that input languages' policies have been
10    /// applied appropriately, that all I/O `Binding`s from the vertex shader to the
11    /// fragment shader must have non-`None` `interpolation` values.
12    ///
13    /// All the shader languages Naga supports have similar rules:
14    /// perspective-correct, center-sampled interpolation is the default for any
15    /// binding that can vary, and an explicit flat qualifier/attribute/what-have-you is
16    /// required for bindings that cannot.
17    ///
18    /// - If `binding` is not a [`Location`] binding, or if its [`interpolation`] is
19    ///   already set, then this function makes no changes.
20    ///
21    /// - If `ty` is a floating-point scalar, vector, or matrix type, then
22    ///   apply the default [`Perspective`] interpolation and [`Center`] sampling.
23    ///
24    /// - If `ty` is an integral scalar or vector, make no changes; if the interpolation was
25    ///   `None`, it will remain so, and be rejected by the validator.
26    ///
27    /// For struct types, the bindings are defined on the members, so there is nothing to
28    /// adjust on the struct itself.
29    ///
30    /// Other non-struct types are not permitted as user-defined IO values, and will be
31    /// rejected by the validator.
32    ///
33    /// [`Binding`]: crate::Binding
34    /// [`Location`]: crate::Binding::Location
35    /// [`interpolation`]: crate::Binding::Location::interpolation
36    /// [`Perspective`]: crate::Interpolation::Perspective
37    /// [`Flat`]: crate::Interpolation::Flat
38    /// [`Center`]: crate::Sampling::Center
39    pub(crate) fn apply_default_interpolation(&mut self, ty: &crate::TypeInner) {
40        let crate::Binding::Location {
41            interpolation: ref mut interpolation @ None,
42            ref mut sampling,
43            ..
44        } = *self
45        else {
46            return;
47        };
48
49        if let Some(crate::ScalarKind::Float) = ty.scalar_kind() {
50            *interpolation = Some(crate::Interpolation::Perspective);
51            *sampling = Some(crate::Sampling::Center);
52        }
53    }
54}