Skip to content

Commit

Permalink
SYNC: all the WGSL directive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed Jan 22, 2025
4 parents a9edb6f + cd062f1 + 871659f + e47a46f commit 4b16f14
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 9 deletions.
48 changes: 41 additions & 7 deletions naga/src/front/wgsl/parse/directive/enable_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,36 @@ use crate::{front::wgsl::error::Error, Span};

/// Tracks the status of every enable-extension known to Naga.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EnableExtensions {}
pub struct EnableExtensions {
#[cfg(test)]
definitely_not_standard: bool,
}

impl EnableExtensions {
pub(crate) const fn empty() -> Self {
Self {}
Self {
#[cfg(test)]
definitely_not_standard: false,
}
}

/// Add an enable-extension to the set requested by a module.
#[allow(unreachable_code)]
pub(crate) fn add(&mut self, ext: ImplementedEnableExtension) {
let _field: &mut bool = match ext {};
let _field: &mut bool = match ext {
#[cfg(test)]
ImplementedEnableExtension::DefinitelyNotStandard => &mut self.definitely_not_standard,
};
*_field = true;
}

/// Query whether an enable-extension tracked here has been requested.
#[allow(unused)]
pub(crate) const fn contains(&self, ext: ImplementedEnableExtension) -> bool {
match ext {}
match ext {
#[cfg(test)]
ImplementedEnableExtension::DefinitelyNotStandard => self.definitely_not_standard,
}
}
}

Expand All @@ -37,12 +49,14 @@ impl Default for EnableExtensions {
/// WGSL spec.: <https://www.w3.org/TR/WGSL/#enable-extensions-sec>
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum EnableExtension {
#[allow(unused)]
#[cfg_attr(not(test), allow(unused))]
Implemented(ImplementedEnableExtension),
Unimplemented(UnimplementedEnableExtension),
}

impl EnableExtension {
#[cfg(test)]
const DEFINITELY_NOT_STANDARD: &'static str = "definitely_not_standard";
const F16: &'static str = "f16";
const CLIP_DISTANCES: &'static str = "clip_distances";
const DUAL_SOURCE_BLENDING: &'static str = "dual_source_blending";
Expand All @@ -64,7 +78,10 @@ impl EnableExtension {
/// Maps this [`EnableExtension`] into the sentinel word associated with it in WGSL.
pub const fn to_ident(self) -> &'static str {
match self {
Self::Implemented(kind) => match kind {},
Self::Implemented(kind) => match kind {
#[cfg(test)]
ImplementedEnableExtension::DefinitelyNotStandard => Self::DEFINITELY_NOT_STANDARD,
},
Self::Unimplemented(kind) => match kind {
UnimplementedEnableExtension::F16 => Self::F16,
UnimplementedEnableExtension::ClipDistances => Self::CLIP_DISTANCES,
Expand All @@ -76,10 +93,15 @@ impl EnableExtension {

/// A variant of [`EnableExtension::Implemented`].
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum ImplementedEnableExtension {}
#[cfg_attr(test, derive(strum::EnumIter))]
pub enum ImplementedEnableExtension {
#[cfg(test)]
DefinitelyNotStandard,
}

/// A variant of [`EnableExtension::Unimplemented`].
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(test, derive(strum::EnumIter))]
pub enum UnimplementedEnableExtension {
/// Enables `f16`/`half` primitive support in all shader languages.
///
Expand Down Expand Up @@ -110,3 +132,15 @@ impl UnimplementedEnableExtension {
}
}
}
#[cfg(test)]
mod test {
use strum::IntoEnumIterator as _;

use super::ImplementedEnableExtension;

fn valid() {
for extension in ImplementedEnableExtension::iter() {}
}

fn unimplemented() {}
}
121 changes: 119 additions & 2 deletions naga/src/front/wgsl/parse/directive/language_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! The focal point of this module is the [`LanguageExtension`] API.
use strum::VariantArray;
#[cfg(test)]
use strum::IntoEnumIterator;

/// A language extension recognized by Naga, but not guaranteed to be present in all environments.
///
Expand All @@ -15,6 +17,9 @@ pub enum LanguageExtension {
}

impl LanguageExtension {
#[cfg(test)]
const NAGA_REPLACE_ME_WITH_A_REAL_EXTENSION_PLZ: &'static str =
"naga_replace_me_with_a_real_extension_plz";
const READONLY_AND_READWRITE_STORAGE_TEXTURES: &'static str =
"readonly_and_readwrite_storage_textures";
const PACKED4X8_INTEGER_DOT_PRODUCT: &'static str = "packed_4x8_integer_dot_product";
Expand All @@ -24,6 +29,10 @@ impl LanguageExtension {
/// Convert from a sentinel word in WGSL into its associated [`LanguageExtension`], if possible.
pub fn from_ident(s: &str) -> Option<Self> {
Some(match s {
#[cfg(test)]
Self::NAGA_REPLACE_ME_WITH_A_REAL_EXTENSION_PLZ => {
Self::Implemented(ImplementedLanguageExtension::NagaReplaceMeWithARealExtensionPlz)
}
Self::READONLY_AND_READWRITE_STORAGE_TEXTURES => Self::Unimplemented(
UnimplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures,
),
Expand All @@ -43,7 +52,12 @@ impl LanguageExtension {
/// Maps this [`LanguageExtension`] into the sentinel word associated with it in WGSL.
pub const fn to_ident(self) -> &'static str {
match self {
Self::Implemented(kind) => kind.to_ident(),
Self::Implemented(kind) => match kind {
#[cfg(test)]
ImplementedLanguageExtension::NagaReplaceMeWithARealExtensionPlz => {
Self::NAGA_REPLACE_ME_WITH_A_REAL_EXTENSION_PLZ
}
},
Self::Unimplemented(kind) => match kind {
UnimplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures => {
Self::READONLY_AND_READWRITE_STORAGE_TEXTURES
Expand All @@ -60,11 +74,22 @@ impl LanguageExtension {
},
}
}

#[cfg(test)]
fn iter() -> impl Iterator<Item = Self> {
let implemented = ImplementedLanguageExtension::iter().map(Self::Implemented);
let unimplemented = UnimplementedLanguageExtension::iter().map(Self::Unimplemented);
implemented.chain(unimplemented)
}
}

/// A variant of [`LanguageExtension::Implemented`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, VariantArray)]
pub enum ImplementedLanguageExtension {}
#[cfg_attr(test, derive(strum::EnumIter))]
pub enum ImplementedLanguageExtension {
#[cfg(test)]
NagaReplaceMeWithARealExtensionPlz,
}

impl ImplementedLanguageExtension {
/// Returns slice of all variants of [`ImplementedLanguageExtension`].
Expand All @@ -80,6 +105,7 @@ impl ImplementedLanguageExtension {

/// A variant of [`LanguageExtension::Unimplemented`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(test, derive(strum::EnumIter))]
pub enum UnimplementedLanguageExtension {
ReadOnlyAndReadWriteStorageTextures,
Packed4x8IntegerDotProduct,
Expand All @@ -97,3 +123,94 @@ impl UnimplementedLanguageExtension {
}
}
}

#[cfg(test)]
mod test {
use itertools::Itertools;
use strum::IntoEnumIterator;

use crate::front::wgsl::assert_parse_err;

use super::{ImplementedLanguageExtension, LanguageExtension};

#[test]
fn implemented() {
#[derive(Clone, Debug, strum::EnumIter)]
enum Count {
ByItself,
WithOther,
}

#[derive(Clone, Debug, strum::EnumIter)]
enum Separation {
SameLineNoSpace,
SameLine,
MultiLine,
}

#[derive(Clone, Debug, strum::EnumIter)]
enum TrailingComma {
Yes,
No,
}

#[track_caller]
fn test_requires(before: &str, idents: &[&str], ident_sep: &str, after: &str) {
let ident_list = idents.join(ident_sep);
let shader = format!("requires{before}{ident_list}{after};");
let expected_msg = "".to_string();
assert_parse_err(&shader, &expected_msg);
}

let implemented_extensions =
ImplementedLanguageExtension::iter().map(LanguageExtension::Implemented);

let iter = implemented_extensions
.clone()
.cartesian_product(Count::iter())
.cartesian_product(Separation::iter())
.cartesian_product(TrailingComma::iter());
for (((extension, count), separation), trailing_comma) in iter {
let before;
let ident_sep;
match separation {
Separation::SameLine => {
before = " ";
ident_sep = ", ";
}
Separation::SameLineNoSpace => {
before = " ";
ident_sep = ",";
}
Separation::MultiLine => {
before = "\n ";
ident_sep = ",\n ";
}
}
let after = match trailing_comma {
TrailingComma::Yes => ident_sep,
TrailingComma::No => before,
};
match count {
Count::ByItself => test_requires(before, &[extension.to_ident()], ident_sep, after),
Count::WithOther => {
for other_extension in implemented_extensions.clone() {
for list in [[extension, other_extension], [other_extension, extension]] {
let list = list.map(|e| e.to_ident());
test_requires(before, &list, ident_sep, after);
}
}
}
}
}
}

#[test]
fn unimplemented() {}

#[test]
fn unknown() {}

#[test]
fn malformed() {}
}

0 comments on commit 4b16f14

Please sign in to comment.