Skip to content

Commit c4fe18c

Browse files
committed
make wgsl-out its own mod, reexport naga
1 parent 7fd9a82 commit c4fe18c

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

crates/cargo-gpu/src/naga.rs

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

0 commit comments

Comments
 (0)