Skip to content

Commit 828b851

Browse files
committed
make wgsl-out its own mod, reexport naga
1 parent 6be1240 commit 828b851

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

crates/cargo-gpu/src/naga.rs

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
33
use anyhow::Context as _;
44
use naga::error::ShaderError;
5-
pub use naga::valid::Capabilities;
5+
use naga::valid::Capabilities;
66
use naga::valid::ModuleInfo;
77
use naga::Module;
88
use spirv_builder::{CompileResult, GenericCompileResult};
99
use std::path::{Path, PathBuf};
1010

11+
pub use naga;
12+
1113
/// Naga [`Module`] with [`ModuleInfo`]
1214
#[derive(Clone, Debug)]
1315
#[expect(
@@ -80,33 +82,42 @@ impl CompileResultNagaExt for CompileResult {
8082
)]
8183
pub struct NagaTranspile(pub GenericCompileResult<NagaModule>);
8284

83-
impl NagaTranspile {
84-
/// Transpile to wgsl source code, typically for webgpu compatibility.
85-
///
86-
/// Returns a [`CompileResult`] of wgsl source code files and their associated wgsl entry points.
87-
///
88-
/// # Errors
89-
/// converting naga module to wgsl may fail
90-
#[inline]
91-
#[cfg(feature = "wgsl-out")]
92-
pub fn to_wgsl(
93-
&self,
94-
writer_flags: naga::back::wgsl::WriterFlags,
95-
) -> anyhow::Result<CompileResult> {
96-
self.0.try_map(
97-
|entry| Ok(crate::linkage::spv_entry_point_to_wgsl(entry)),
98-
|module| {
99-
let inner = || -> anyhow::Result<_> {
100-
let wgsl_dst = module.spv_path.with_extension("wgsl");
101-
let wgsl =
102-
naga::back::wgsl::write_string(&module.module, &module.info, writer_flags)
103-
.context("naga conversion to wgsl failed")?;
104-
std::fs::write(&wgsl_dst, wgsl).context("failed to write wgsl file")?;
105-
Ok(wgsl_dst)
106-
};
107-
inner()
108-
.with_context(|| format!("transpiling to wgsl '{}'", module.spv_path.display()))
109-
},
110-
)
85+
/// feature gate `wgsl-out`
86+
#[cfg(feature = "wgsl-out")]
87+
mod wgsl_out {
88+
use crate::NagaTranspile;
89+
use anyhow::Context as _;
90+
use naga::back::wgsl::WriterFlags;
91+
use spirv_builder::CompileResult;
92+
93+
impl NagaTranspile {
94+
/// Transpile to wgsl source code, typically for webgpu compatibility.
95+
///
96+
/// Returns a [`CompileResult`] of wgsl source code files and their associated wgsl entry points.
97+
///
98+
/// # Errors
99+
/// converting naga module to wgsl may fail
100+
#[inline]
101+
pub fn to_wgsl(&self, writer_flags: WriterFlags) -> anyhow::Result<CompileResult> {
102+
self.0.try_map(
103+
|entry| Ok(crate::linkage::spv_entry_point_to_wgsl(entry)),
104+
|module| {
105+
let inner = || -> anyhow::Result<_> {
106+
let wgsl_dst = module.spv_path.with_extension("wgsl");
107+
let wgsl = naga::back::wgsl::write_string(
108+
&module.module,
109+
&module.info,
110+
writer_flags,
111+
)
112+
.context("naga conversion to wgsl failed")?;
113+
std::fs::write(&wgsl_dst, wgsl).context("failed to write wgsl file")?;
114+
Ok(wgsl_dst)
115+
};
116+
inner().with_context(|| {
117+
format!("transpiling to wgsl '{}'", module.spv_path.display())
118+
})
119+
},
120+
)
121+
}
111122
}
112123
}

0 commit comments

Comments
 (0)