Skip to content

Commit

Permalink
Add Naga variant to ShaderSource
Browse files Browse the repository at this point in the history
  • Loading branch information
rttad committed Jun 22, 2022
1 parent f27a978 commit fa6acdc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,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(ref module) => wgc::pipeline::ShaderModuleSource::Naga(module.take()),
};
let (id, error) = wgc::gfx_select!(
device.id => global.device_create_shader_module(device.id, &descriptor, source, PhantomData)
Expand Down
16 changes: 16 additions & 0 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,22 @@ 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(ref cell) => {
use naga::{back, valid};

let module = cell.take();
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);
Expand Down
4 changes: 4 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,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(std::cell::Cell<naga::Module>),
}

/// Descriptor for use with [`Device::create_shader_module`].
Expand Down

0 comments on commit fa6acdc

Please sign in to comment.