diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index eb0d755e1a..c9f3cea1b3 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -1108,6 +1108,8 @@ impl crate::Context for Context { wgc::pipeline::ShaderModuleSource::Naga(module) } ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)), + #[cfg(feature = "naga")] + ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module), }; let (id, error) = wgc::gfx_select!( device.id => global.device_create_shader_module(device.id, &descriptor, source, PhantomData) diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index e65ee9b4a6..f4f40ee412 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1307,6 +1307,21 @@ impl crate::Context for Context { web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str()) } crate::ShaderSource::Wgsl(ref code) => web_sys::GpuShaderModuleDescriptor::new(code), + #[cfg(feature = "naga")] + crate::ShaderSource::Naga(module) => { + use naga::{back, valid}; + + let mut validator = valid::Validator::new( + valid::ValidationFlags::all(), + valid::Capabilities::all(), + ); + let module_info = validator.validate(&module).unwrap(); + + let writer_flags = naga::back::wgsl::WriterFlags::empty(); + let wgsl_text = + back::wgsl::write_string(&module, &module_info, writer_flags).unwrap(); + web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str()) + } }; if let Some(label) = desc.label { descriptor.label(label); diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index f14298c422..dfa052c231 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -813,6 +813,10 @@ pub enum ShaderSource<'a> { }, /// WGSL module as a string slice. Wgsl(Cow<'a, str>), + /// Naga module. + #[cfg(feature = "naga")] + #[cfg_attr(docsrs, doc(cfg(feature = "naga")))] + Naga(naga::Module), } /// Descriptor for use with [`Device::create_shader_module`].