|
| 1 | +//! WGSL directives. The focal point of this API is [`DirectiveKind`]. |
| 2 | +//! |
| 3 | +//! See also <https://www.w3.org/TR/WGSL/#directives>. |
| 4 | +
|
| 5 | +/// A parsed sentinel word indicating the type of directive to be parsed next. |
| 6 | +#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] |
| 7 | +pub enum DirectiveKind { |
| 8 | + Unimplemented(UnimplementedDirectiveKind), |
| 9 | +} |
| 10 | + |
| 11 | +impl DirectiveKind { |
| 12 | + const DIAGNOSTIC: &'static str = "diagnostic"; |
| 13 | + const ENABLE: &'static str = "enable"; |
| 14 | + const REQUIRES: &'static str = "requires"; |
| 15 | + |
| 16 | + /// Convert from a sentinel word in WGSL into its associated [`DirectiveKind`], if possible. |
| 17 | + pub fn from_ident(s: &str) -> Option<Self> { |
| 18 | + Some(match s { |
| 19 | + Self::DIAGNOSTIC => Self::Unimplemented(UnimplementedDirectiveKind::Diagnostic), |
| 20 | + Self::ENABLE => Self::Unimplemented(UnimplementedDirectiveKind::Enable), |
| 21 | + Self::REQUIRES => Self::Unimplemented(UnimplementedDirectiveKind::Requires), |
| 22 | + _ => return None, |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + /// Maps this [`DirectiveKind`] into the sentinel word associated with it in WGSL. |
| 27 | + pub const fn to_ident(self) -> &'static str { |
| 28 | + match self { |
| 29 | + Self::Unimplemented(kind) => match kind { |
| 30 | + UnimplementedDirectiveKind::Diagnostic => Self::DIAGNOSTIC, |
| 31 | + UnimplementedDirectiveKind::Enable => Self::ENABLE, |
| 32 | + UnimplementedDirectiveKind::Requires => Self::REQUIRES, |
| 33 | + }, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + #[cfg(test)] |
| 38 | + fn iter() -> impl Iterator<Item = Self> { |
| 39 | + use strum::IntoEnumIterator; |
| 40 | + |
| 41 | + UnimplementedDirectiveKind::iter().map(Self::Unimplemented) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// A [`DirectiveKind`] that is not yet implemented. See [`DirectiveKind::Unimplemented`]. |
| 46 | +#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] |
| 47 | +#[cfg_attr(test, derive(strum::EnumIter))] |
| 48 | +pub enum UnimplementedDirectiveKind { |
| 49 | + Diagnostic, |
| 50 | + Enable, |
| 51 | + Requires, |
| 52 | +} |
| 53 | + |
| 54 | +impl UnimplementedDirectiveKind { |
| 55 | + pub const fn tracking_issue_num(self) -> u16 { |
| 56 | + match self { |
| 57 | + Self::Diagnostic => 5320, |
| 58 | + Self::Requires => 6350, |
| 59 | + Self::Enable => 5476, |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod test { |
| 66 | + use strum::IntoEnumIterator; |
| 67 | + |
| 68 | + use crate::front::wgsl::assert_parse_err; |
| 69 | + |
| 70 | + use super::{DirectiveKind, UnimplementedDirectiveKind}; |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn unimplemented_directives() { |
| 74 | + for unsupported_shader in UnimplementedDirectiveKind::iter() { |
| 75 | + let shader; |
| 76 | + let expected_msg; |
| 77 | + match unsupported_shader { |
| 78 | + UnimplementedDirectiveKind::Diagnostic => { |
| 79 | + shader = "diagnostic(off,derivative_uniformity);"; |
| 80 | + expected_msg = "\ |
| 81 | +error: `diagnostic` is not yet implemented |
| 82 | + ┌─ wgsl:1:1 |
| 83 | + │ |
| 84 | +1 │ diagnostic(off,derivative_uniformity); |
| 85 | + │ ^^^^^^^^^^ this global directive is standard, but not yet implemented |
| 86 | + │ |
| 87 | + = note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/5320>, so they can prioritize it! |
| 88 | +
|
| 89 | +"; |
| 90 | + } |
| 91 | + UnimplementedDirectiveKind::Enable => { |
| 92 | + shader = "enable f16;"; |
| 93 | + expected_msg = "\ |
| 94 | +error: `enable` is not yet implemented |
| 95 | + ┌─ wgsl:1:1 |
| 96 | + │ |
| 97 | +1 │ enable f16; |
| 98 | + │ ^^^^^^ this global directive is standard, but not yet implemented |
| 99 | + │ |
| 100 | + = note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/5476>, so they can prioritize it! |
| 101 | +
|
| 102 | +"; |
| 103 | + } |
| 104 | + UnimplementedDirectiveKind::Requires => { |
| 105 | + shader = "requires readonly_and_readwrite_storage_textures"; |
| 106 | + expected_msg = "\ |
| 107 | +error: `requires` is not yet implemented |
| 108 | + ┌─ wgsl:1:1 |
| 109 | + │ |
| 110 | +1 │ requires readonly_and_readwrite_storage_textures |
| 111 | + │ ^^^^^^^^ this global directive is standard, but not yet implemented |
| 112 | + │ |
| 113 | + = note: Let Naga maintainers know that you ran into this at <https://github.com/gfx-rs/wgpu/issues/6350>, so they can prioritize it! |
| 114 | +
|
| 115 | +"; |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + assert_parse_err(shader, expected_msg); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn directive_after_global_decl() { |
| 125 | + for unsupported_shader in DirectiveKind::iter() { |
| 126 | + let directive; |
| 127 | + let expected_msg; |
| 128 | + match unsupported_shader { |
| 129 | + DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Diagnostic) => { |
| 130 | + directive = "diagnostic(off,derivative_uniformity)"; |
| 131 | + expected_msg = "\ |
| 132 | +error: expected global declaration, but found a global directive |
| 133 | + ┌─ wgsl:2:1 |
| 134 | + │ |
| 135 | +2 │ diagnostic(off,derivative_uniformity); |
| 136 | + │ ^^^^^^^^^^ written after first global declaration |
| 137 | + │ |
| 138 | + = note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module? |
| 139 | +
|
| 140 | +"; |
| 141 | + } |
| 142 | + DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Enable) => { |
| 143 | + directive = "enable f16"; |
| 144 | + expected_msg = "\ |
| 145 | +error: expected global declaration, but found a global directive |
| 146 | + ┌─ wgsl:2:1 |
| 147 | + │ |
| 148 | +2 │ enable f16; |
| 149 | + │ ^^^^^^ written after first global declaration |
| 150 | + │ |
| 151 | + = note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module? |
| 152 | +
|
| 153 | +"; |
| 154 | + } |
| 155 | + DirectiveKind::Unimplemented(UnimplementedDirectiveKind::Requires) => { |
| 156 | + directive = "requires readonly_and_readwrite_storage_textures"; |
| 157 | + expected_msg = "\ |
| 158 | +error: expected global declaration, but found a global directive |
| 159 | + ┌─ wgsl:2:1 |
| 160 | + │ |
| 161 | +2 │ requires readonly_and_readwrite_storage_textures; |
| 162 | + │ ^^^^^^^^ written after first global declaration |
| 163 | + │ |
| 164 | + = note: global directives are only allowed before global declarations; maybe hoist this closer to the top of the shader module? |
| 165 | +
|
| 166 | +"; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + let shader = format!( |
| 171 | + "\ |
| 172 | +@group(0) @binding(0) var<storage> thing: i32; |
| 173 | +{directive}; |
| 174 | +" |
| 175 | + ); |
| 176 | + assert_parse_err(&shader, expected_msg); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments