naga/back/wgsl/
mod.rs

1/*!
2Backend for [WGSL][wgsl] (WebGPU Shading Language).
3
4[wgsl]: https://gpuweb.github.io/gpuweb/wgsl.html
5*/
6
7mod polyfill;
8mod writer;
9
10use thiserror::Error;
11
12pub use writer::{Writer, WriterFlags};
13
14#[derive(Error, Debug)]
15pub enum Error {
16    #[error(transparent)]
17    FmtError(#[from] std::fmt::Error),
18    #[error("{0}")]
19    Custom(String),
20    #[error("{0}")]
21    Unimplemented(String), // TODO: Error used only during development
22    #[error("Unsupported math function: {0:?}")]
23    UnsupportedMathFunction(crate::MathFunction),
24    #[error("Unsupported relational function: {0:?}")]
25    UnsupportedRelationalFunction(crate::RelationalFunction),
26}
27
28pub fn write_string(
29    module: &crate::Module,
30    info: &crate::valid::ModuleInfo,
31    flags: WriterFlags,
32) -> Result<String, Error> {
33    let mut w = Writer::new(String::new(), flags);
34    w.write(module, info)?;
35    let output = w.finish();
36    Ok(output)
37}
38
39impl crate::AtomicFunction {
40    const fn to_wgsl(self) -> &'static str {
41        match self {
42            Self::Add => "Add",
43            Self::Subtract => "Sub",
44            Self::And => "And",
45            Self::InclusiveOr => "Or",
46            Self::ExclusiveOr => "Xor",
47            Self::Min => "Min",
48            Self::Max => "Max",
49            Self::Exchange { compare: None } => "Exchange",
50            Self::Exchange { .. } => "CompareExchangeWeak",
51        }
52    }
53}