Skip to content

Commit 04618b3

Browse files
feat(wgsl): add first and either sampling for @interpolate(flat, …) (#6181)
1 parent 34bb9e4 commit 04618b3

29 files changed

+1091
-256
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ By @teoxoy [#6134](https://github.com/gfx-rs/wgpu/pull/6134).
7070
#### Naga
7171

7272
- Support constant evaluation for `firstLeadingBit` and `firstTrailingBit` numeric built-ins in WGSL. Front-ends that translate to these built-ins also benefit from constant evaluation. By @ErichDonGubler in [#5101](https://github.com/gfx-rs/wgpu/pull/5101).
73+
- Add `first` and `either` sampling types for `@interpolate(flat, …)` in WGSL. By @ErichDonGubler in [#6181](https://github.com/gfx-rs/wgpu/pull/6181).
7374

7475
#### Vulkan
7576

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

naga/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ cfg_aliases.workspace = true
8989
[dev-dependencies]
9090
diff = "0.1"
9191
env_logger.workspace = true
92+
itertools.workspace = true
9293
# This _cannot_ have a version specified. If it does, crates.io will look
9394
# for a version of the package on crates when we publish naga. Path dependencies
9495
# are allowed through though.

naga/src/back/glsl/mod.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ pub enum Error {
498498
Custom(String),
499499
#[error("overrides should not be present at this stage")]
500500
Override,
501+
/// [`crate::Sampling::First`] is unsupported.
502+
#[error("`{:?}` sampling is unsupported", crate::Sampling::First)]
503+
FirstSamplingNotSupported,
501504
}
502505

503506
/// Binary operation with a different logic on the GLSL side.
@@ -1534,7 +1537,7 @@ impl<'a, W: Write> Writer<'a, W> {
15341537
// here, regardless of the version.
15351538
if let Some(sampling) = sampling {
15361539
if emit_interpolation_and_auxiliary {
1537-
if let Some(qualifier) = glsl_sampling(sampling) {
1540+
if let Some(qualifier) = glsl_sampling(sampling)? {
15381541
write!(self.out, "{qualifier} ")?;
15391542
}
15401543
}
@@ -4771,14 +4774,15 @@ const fn glsl_interpolation(interpolation: crate::Interpolation) -> &'static str
47714774
}
47724775

47734776
/// Return the GLSL auxiliary qualifier for the given sampling value.
4774-
const fn glsl_sampling(sampling: crate::Sampling) -> Option<&'static str> {
4777+
const fn glsl_sampling(sampling: crate::Sampling) -> BackendResult<Option<&'static str>> {
47754778
use crate::Sampling as S;
47764779

4777-
match sampling {
4778-
S::Center => None,
4780+
Ok(match sampling {
4781+
S::First => return Err(Error::FirstSamplingNotSupported),
4782+
S::Center | S::Either => None,
47794783
S::Centroid => Some("centroid"),
47804784
S::Sample => Some("sample"),
4781-
}
4785+
})
47824786
}
47834787

47844788
/// Helper function that returns the glsl dimension string of [`ImageDimension`](crate::ImageDimension)

naga/src/back/hlsl/conv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl crate::Sampling {
202202
/// Return the HLSL auxiliary qualifier for the given sampling value.
203203
pub(super) const fn to_hlsl_str(self) -> Option<&'static str> {
204204
match self {
205-
Self::Center => None,
205+
Self::Center | Self::First | Self::Either => None,
206206
Self::Centroid => Some("centroid"),
207207
Self::Sample => Some("sample"),
208208
}

naga/src/back/msl/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ impl ResolvedInterpolation {
627627
(I::Linear, S::Centroid) => Self::CentroidNoPerspective,
628628
(I::Linear, S::Sample) => Self::SampleNoPerspective,
629629
(I::Flat, _) => Self::Flat,
630+
_ => unreachable!(),
630631
}
631632
}
632633

naga/src/back/spv/writer.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,12 @@ impl Writer {
15111511
}
15121512
match sampling {
15131513
// Center sampling is the default in SPIR-V.
1514-
None | Some(crate::Sampling::Center) => (),
1514+
None
1515+
| Some(
1516+
crate::Sampling::Center
1517+
| crate::Sampling::First
1518+
| crate::Sampling::Either,
1519+
) => (),
15151520
Some(crate::Sampling::Centroid) => {
15161521
self.decorate(id, Decoration::Centroid, &[]);
15171522
}

naga/src/back/wgsl/writer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,8 @@ const fn sampling_str(sampling: crate::Sampling) -> &'static str {
20612061
S::Center => "",
20622062
S::Centroid => "centroid",
20632063
S::Sample => "sample",
2064+
S::First => "first",
2065+
S::Either => "either",
20642066
}
20652067
}
20662068

naga/src/front/wgsl/parse/conv.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub fn map_sampling(word: &str, span: Span) -> Result<crate::Sampling, Error<'_>
5858
"center" => Ok(crate::Sampling::Center),
5959
"centroid" => Ok(crate::Sampling::Centroid),
6060
"sample" => Ok(crate::Sampling::Sample),
61+
"first" => Ok(crate::Sampling::First),
62+
"either" => Ok(crate::Sampling::Either),
6163
_ => Err(Error::UnknownAttribute(span)),
6264
}
6365
}

naga/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,13 @@ pub enum Sampling {
530530
/// Interpolate the value at each sample location. In multisampling, invoke
531531
/// the fragment shader once per sample.
532532
Sample,
533+
534+
/// Use the value provided by the first vertex of the current primitive.
535+
First,
536+
537+
/// Use the value provided by the first or last vertex of the current primitive. The exact
538+
/// choice is implementation-dependent.
539+
Either,
533540
}
534541

535542
/// Member of a user-defined structure.

0 commit comments

Comments
 (0)