diff --git a/Cargo.lock b/Cargo.lock index 01b0e3a88..90f4bd7f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1053,6 +1053,7 @@ dependencies = [ "num-traits 0.2.14", "petgraph", "rose_tree", + "serde", "spirv_headers", "thiserror", ] diff --git a/player/src/lib.rs b/player/src/lib.rs index 48fd018a3..c84393ea7 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -239,17 +239,15 @@ impl GlobalPlay for wgc::hub::Global { self.bind_group_drop::(id); } Action::CreateShaderModule { id, desc, data } => { + log::info!("Creating shader from {}", data); + let code = fs::read_to_string(dir.join(&data)).unwrap(); let source = if data.ends_with(".wgsl") { - let code = fs::read_to_string(dir.join(data)).unwrap(); wgc::pipeline::ShaderModuleSource::Wgsl(Cow::Owned(code)) + } else if data.ends_with(".ron") { + let module = ron::de::from_str(&code).unwrap(); + wgc::pipeline::ShaderModuleSource::Naga(module) } else { - let byte_vec = fs::read(dir.join(&data)) - .unwrap_or_else(|e| panic!("Unable to open '{}': {:?}", data, e)); - let spv = byte_vec - .chunks(4) - .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]])) - .collect::>(); - wgc::pipeline::ShaderModuleSource::SpirV(Cow::Owned(spv)) + panic!("Unknown shader {}", data); }; let (_, error) = self.device_create_shader_module::(device, &desc, source, id); if let Some(e) = error { diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index fee13faff..b04af3d26 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -14,9 +14,9 @@ license = "MIT OR Apache-2.0" [features] default = [] # Enable API tracing -trace = ["ron", "serde", "wgt/trace", "arrayvec/serde"] +trace = ["ron", "serde", "wgt/trace", "arrayvec/serde", "naga/serialize"] # Enable API replaying -replay = ["serde", "wgt/replay", "arrayvec/serde"] +replay = ["serde", "wgt/replay", "arrayvec/serde", "naga/deserialize"] # Enable serializable compute/render passes, and bundle encoders. serial-pass = ["serde", "wgt/serde", "arrayvec/serde"] @@ -37,7 +37,7 @@ thiserror = "1" [dependencies.naga] git = "https://github.com/gfx-rs/naga" tag = "gfx-26" -features = ["spv-in", "wgsl-in"] +features = ["wgsl-in"] [dependencies.wgt] path = "../wgpu-types" diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 323adbe79..0216cd8ee 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -838,27 +838,6 @@ impl Device { source: pipeline::ShaderModuleSource<'a>, ) -> Result, pipeline::CreateShaderModuleError> { let module = match source { - pipeline::ShaderModuleSource::SpirV(spv) => { - profiling::scope!("naga::spv::parse"); - // Parse the given shader code and store its representation. - let options = naga::front::spv::Options { - adjust_coordinate_space: false, // we require NDC_Y_UP feature - strict_capabilities: true, - flow_graph_dump_prefix: None, - }; - let parser = naga::front::spv::Parser::new(spv.iter().cloned(), &options); - match parser.parse() { - Ok(module) => module, - Err(err) => { - log::warn!( - "Failed to parse shader SPIR-V code for {:?}: {:?}", - desc.label, - err - ); - return Err(pipeline::CreateShaderModuleError::Parsing); - } - } - } pipeline::ShaderModuleSource::Wgsl(code) => { profiling::scope!("naga::wgsl::parse_str"); // TODO: refactor the corresponding Naga error to be owned, and then @@ -3476,17 +3455,14 @@ impl Global { if let Some(ref trace) = device.trace { let mut trace = trace.lock(); let data = match source { - pipeline::ShaderModuleSource::SpirV(ref spv) => { - trace.make_binary("spv", unsafe { - std::slice::from_raw_parts(spv.as_ptr() as *const u8, spv.len() * 4) - }) - } pipeline::ShaderModuleSource::Wgsl(ref code) => { trace.make_binary("wgsl", code.as_bytes()) } - pipeline::ShaderModuleSource::Naga(_) => { - // we don't want to enable Naga serialization just for this alone - trace.make_binary("ron", &[]) + pipeline::ShaderModuleSource::Naga(ref module) => { + let string = + ron::ser::to_string_pretty(module, ron::ser::PrettyConfig::default()) + .unwrap(); + trace.make_binary("ron", string.as_bytes()) } }; trace.add(trace::Action::CreateShaderModule { diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index c7ccb9598..6d9510a5f 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -9,7 +9,6 @@ use std::borrow::Cow; use thiserror::Error; pub enum ShaderModuleSource<'a> { - SpirV(Cow<'a, [u32]>), Wgsl(Cow<'a, str>), Naga(naga::Module), } diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 4b2ede3b9..45a5bd6b3 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -17,6 +17,7 @@ all-features = true [features] default = [] +spirv = ["naga/spv-in"] trace = ["serde", "wgc/trace"] replay = ["serde", "wgc/replay"] webgl = ["wgc"] @@ -64,6 +65,11 @@ async-executor = "1.0" pollster = "0.2" env_logger = "0.8" +[dependencies.naga] +git = "https://github.com/gfx-rs/naga" +tag = "gfx-26" +optional = true + # used to test all the example shaders [dev-dependencies.naga] git = "https://github.com/gfx-rs/naga" @@ -78,9 +84,12 @@ features = ["wgsl-in", "spv-out"] [[example]] name="hello-compute" -path="examples/hello-compute/main.rs" test = true +[[example]] +name="texture-arrays" +required-features = ["spirv"] + [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = "0.2.73" # remember to change version in wiki as well web-sys = { version = "=0.3.50", features = [ diff --git a/wgpu/README.md b/wgpu/README.md index 69af9056f..7d974f5b4 100644 --- a/wgpu/README.md +++ b/wgpu/README.md @@ -43,6 +43,17 @@ The following environment variables can be used to configure how the framework e See [wiki article](https://github.com/gfx-rs/wgpu-rs/wiki/Running-on-the-Web-with-WebGPU-and-WebGL). +## Shaders + +[WGSL](https://gpuweb.github.io/gpuweb/wgsl/) is the main shading language of WebGPU. + +Users can run the [naga](https://github.com/gfx-rs/naga) binary in the following way to convert their SPIR-V shaders to WGSL: +```bash +cargo run -- +``` + +In addition, SPIR-V can be used by enabling the `spirv` feature, and the cost of slightly increased build times. + ## Development If you need to test local fixes to gfx or other dependencies, the simplest way is to add a Cargo patch. For example, when working on DX12 backend on Windows, you can check out the latest release branch in the [gfx-hal repository](https://github.com/gfx-rs/gfx) (e.g. currently `hal-0.8`) and add this patch to the end of `Cargo.toml`: diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index feec3f9a1..333290411 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -810,7 +810,19 @@ impl crate::Context for Context { label: desc.label.map(Borrowed), }; let source = match desc.source { - ShaderSource::SpirV(ref spv) => wgc::pipeline::ShaderModuleSource::SpirV(Borrowed(spv)), + #[cfg(feature = "spirv")] + ShaderSource::SpirV(ref spv) => { + profiling::scope!("naga::spv::parse"); + // Parse the given shader code and store its representation. + let options = naga::front::spv::Options { + adjust_coordinate_space: false, // we require NDC_Y_UP feature + strict_capabilities: true, + flow_graph_dump_prefix: None, + }; + let parser = naga::front::spv::Parser::new(spv.iter().cloned(), &options); + let module = parser.parse().unwrap(); + wgc::pipeline::ShaderModuleSource::Naga(module) + } ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)), }; let (id, error) = wgc::gfx_select!( diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index cfa102328..2dbce8e86 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1140,6 +1140,7 @@ impl crate::Context for Context { desc: &crate::ShaderModuleDescriptor, ) -> Self::ShaderModuleId { let mut descriptor = match desc.source { + #[cfg(feature = "spirv")] crate::ShaderSource::SpirV(ref spv) => { web_sys::GpuShaderModuleDescriptor::new(&js_sys::Uint32Array::from(&**spv)) } diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 8dd0caf7f..d38c28a80 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -739,6 +739,7 @@ pub enum ShaderSource<'a> { /// /// wgpu will attempt to parse and validate it, but the original binary /// is passed to `gfx-rs` and `spirv_cross` for translation. + #[cfg(feature = "spirv")] SpirV(Cow<'a, [u32]>), /// WGSL module as a string slice. /// diff --git a/wgpu/src/util/mod.rs b/wgpu/src/util/mod.rs index d932cf90b..b18548eb0 100644 --- a/wgpu/src/util/mod.rs +++ b/wgpu/src/util/mod.rs @@ -4,9 +4,10 @@ mod belt; mod device; mod encoder; +use std::future::Future; +#[cfg(feature = "spirv")] use std::{ borrow::Cow, - future::Future, mem::{align_of, size_of}, ptr::copy_nonoverlapping, }; @@ -24,15 +25,16 @@ pub use encoder::RenderEncoder; /// - Input length isn't multiple of 4 /// - Input is longer than [`usize::max_value`] /// - SPIR-V magic number is missing from beginning of stream +#[cfg(feature = "spirv")] pub fn make_spirv(data: &[u8]) -> super::ShaderSource { super::ShaderSource::SpirV(make_spirv_raw(data)) } /// Version of [`make_spirv`] intended for use with [`Device::create_shader_module_spirv`]. /// Returns raw slice instead of ShaderSource. +#[cfg(feature = "spirv")] pub fn make_spirv_raw(data: &[u8]) -> Cow<[u32]> { const MAGIC_NUMBER: u32 = 0x0723_0203; - assert_eq!( data.len() % size_of::(), 0,