|
2 | 2 |
|
3 | 3 | use anyhow::Context as _;
|
4 | 4 | use naga::error::ShaderError;
|
5 |
| -pub use naga::valid::Capabilities; |
| 5 | +use naga::valid::Capabilities; |
6 | 6 | use naga::valid::ModuleInfo;
|
7 | 7 | use naga::Module;
|
8 | 8 | use spirv_builder::{CompileResult, GenericCompileResult};
|
9 | 9 | use std::path::{Path, PathBuf};
|
10 | 10 |
|
| 11 | +pub use naga; |
| 12 | + |
11 | 13 | /// Naga [`Module`] with [`ModuleInfo`]
|
12 | 14 | #[derive(Clone, Debug)]
|
13 | 15 | #[expect(
|
@@ -80,33 +82,42 @@ impl CompileResultNagaExt for CompileResult {
|
80 | 82 | )]
|
81 | 83 | pub struct NagaTranspile(pub GenericCompileResult<NagaModule>);
|
82 | 84 |
|
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 | + } |
111 | 122 | }
|
112 | 123 | }
|
0 commit comments