|
| 1 | +//! naga transpiling to wgsl support, hidden behind feature `naga` |
| 2 | +
|
| 3 | +use crate::linkage::spv_entry_point_to_wgsl; |
| 4 | +use anyhow::Context as _; |
| 5 | +use naga::error::ShaderError; |
| 6 | +pub use naga::valid::Capabilities; |
| 7 | +use spirv_builder::{CompileResult, ModuleResult}; |
| 8 | +use std::collections::BTreeMap; |
| 9 | +use std::path::{Path, PathBuf}; |
| 10 | + |
| 11 | +/// convert a single spv file to a wgsl file using naga |
| 12 | +fn spv_to_wgsl(spv_src: &Path, wgsl_dst: &Path, capabilities: Capabilities) -> anyhow::Result<()> { |
| 13 | + let inner = || -> anyhow::Result<()> { |
| 14 | + let spv_bytes = std::fs::read(spv_src).context("could not read spv file")?; |
| 15 | + let opts = naga::front::spv::Options::default(); |
| 16 | + let module = naga::front::spv::parse_u8_slice(&spv_bytes, &opts) |
| 17 | + .map_err(|err| ShaderError { |
| 18 | + source: String::new(), |
| 19 | + label: None, |
| 20 | + inner: Box::new(err), |
| 21 | + }) |
| 22 | + .context("naga could not parse spv")?; |
| 23 | + let mut validator = |
| 24 | + naga::valid::Validator::new(naga::valid::ValidationFlags::default(), capabilities); |
| 25 | + let info = validator |
| 26 | + .validate(&module) |
| 27 | + .map_err(|err| ShaderError { |
| 28 | + source: String::new(), |
| 29 | + label: None, |
| 30 | + inner: Box::new(err), |
| 31 | + }) |
| 32 | + .context("validation of naga module failed")?; |
| 33 | + let wgsl = |
| 34 | + naga::back::wgsl::write_string(&module, &info, naga::back::wgsl::WriterFlags::empty()) |
| 35 | + .context("naga conversion to wgsl failed")?; |
| 36 | + std::fs::write(wgsl_dst, wgsl).context("failed to write wgsl file")?; |
| 37 | + Ok(()) |
| 38 | + }; |
| 39 | + inner().with_context(|| { |
| 40 | + format!( |
| 41 | + "converting spv '{}' to wgsl '{}' failed ", |
| 42 | + spv_src.display(), |
| 43 | + wgsl_dst.display() |
| 44 | + ) |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +/// convert spv file path to a valid unique wgsl file path |
| 49 | +fn wgsl_file_name(path: &Path) -> PathBuf { |
| 50 | + path.with_extension("wgsl") |
| 51 | +} |
| 52 | + |
| 53 | +/// Extension trait for naga transpiling |
| 54 | +pub trait CompileResultNagaExt { |
| 55 | + /// Transpile the spirv binaries to wgsl source code using [`naga`], typically for webgpu compatibility. |
| 56 | + /// |
| 57 | + /// Converts this [`CompileResult`] of spirv binaries and entry points to a [`CompileResult`] pointing to wgsl source code files and their associated wgsl entry |
| 58 | + /// points. |
| 59 | + /// |
| 60 | + /// # Errors |
| 61 | + /// [`naga`] transpile may error in various ways |
| 62 | + fn transpile_to_wgsl(&self, capabilities: Capabilities) -> anyhow::Result<CompileResult>; |
| 63 | +} |
| 64 | + |
| 65 | +impl CompileResultNagaExt for CompileResult { |
| 66 | + #[inline] |
| 67 | + fn transpile_to_wgsl(&self, capabilities: Capabilities) -> anyhow::Result<CompileResult> { |
| 68 | + Ok(match &self.module { |
| 69 | + ModuleResult::SingleModule(spv) => { |
| 70 | + let wgsl = wgsl_file_name(spv); |
| 71 | + spv_to_wgsl(spv, &wgsl, capabilities)?; |
| 72 | + let entry_points = self |
| 73 | + .entry_points |
| 74 | + .iter() |
| 75 | + .map(|entry| spv_entry_point_to_wgsl(entry)) |
| 76 | + .collect(); |
| 77 | + Self { |
| 78 | + entry_points, |
| 79 | + module: ModuleResult::SingleModule(wgsl), |
| 80 | + } |
| 81 | + } |
| 82 | + ModuleResult::MultiModule(map) => { |
| 83 | + let new_map: BTreeMap<String, PathBuf> = map |
| 84 | + .iter() |
| 85 | + .map(|(entry_point, spv)| { |
| 86 | + let wgsl = wgsl_file_name(spv); |
| 87 | + spv_to_wgsl(spv, &wgsl, capabilities)?; |
| 88 | + Ok((spv_entry_point_to_wgsl(entry_point), wgsl)) |
| 89 | + }) |
| 90 | + .collect::<anyhow::Result<_>>()?; |
| 91 | + Self { |
| 92 | + entry_points: new_map.keys().cloned().collect(), |
| 93 | + module: ModuleResult::MultiModule(new_map), |
| 94 | + } |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments