diff --git a/player/Cargo.toml b/player/Cargo.toml index 4ff38897eb3..dcb6ecb849a 100644 --- a/player/Cargo.toml +++ b/player/Cargo.toml @@ -29,7 +29,7 @@ features = ["replay"] [dependencies.wgc] path = "../wgpu-core" package = "wgpu-core" -features = ["replay", "raw-window-handle"] +features = ["spirv", "replay", "raw-window-handle"] [dev-dependencies] serde = "1" diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index fee13faff06..2cc5c49eb30 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -13,6 +13,7 @@ license = "MIT OR Apache-2.0" [features] default = [] +spirv = ["naga/spv-in"] # Enable API tracing trace = ["ron", "serde", "wgt/trace", "arrayvec/serde"] # Enable API replaying @@ -37,7 +38,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 39a9ea0ba9d..2c38fcd9485 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -851,6 +851,7 @@ impl Device { source: pipeline::ShaderModuleSource<'a>, ) -> Result, pipeline::CreateShaderModuleError> { let module = match source { + #[cfg(feature = "spirv")] pipeline::ShaderModuleSource::SpirV(spv) => { profiling::scope!("naga::spv::parse"); // Parse the given shader code and store its representation. @@ -3431,6 +3432,7 @@ impl Global { if let Some(ref trace) = device.trace { let mut trace = trace.lock(); let data = match source { + #[cfg(feature = "spirv")] pipeline::ShaderModuleSource::SpirV(ref spv) => { trace.make_binary("spv", unsafe { std::slice::from_raw_parts(spv.as_ptr() as *const u8, spv.len() * 4) diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index f853ad0c143..ee66cb988df 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -13,6 +13,7 @@ use std::borrow::Cow; use thiserror::Error; pub enum ShaderModuleSource<'a> { + #[cfg(feature = "spirv")] SpirV(Cow<'a, [u32]>), Wgsl(Cow<'a, str>), Naga(naga::Module), diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 4b2ede3b980..6d1cfdc0d2b 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -17,6 +17,7 @@ all-features = true [features] default = [] +spirv = ["wgc/spirv"] trace = ["serde", "wgc/trace"] replay = ["serde", "wgc/replay"] webgl = ["wgc"] @@ -78,9 +79,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 69af9056f81..595f134299c 100644 --- a/wgpu/README.md +++ b/wgpu/README.md @@ -43,6 +43,12 @@ 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. + +In addition, SPIR-V can be used by enabling the `spirv` feature. + ## 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 8805aca6051..5ec357f14ff 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -810,6 +810,7 @@ impl crate::Context for Context { label: desc.label.map(Borrowed), }; let source = match desc.source { + #[cfg(feature = "spirv")] ShaderSource::SpirV(ref spv) => wgc::pipeline::ShaderModuleSource::SpirV(Borrowed(spv)), ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)), }; diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index 20a3f94fa33..d8c92499840 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 6cce1dbe1be..b60919608ec 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -734,6 +734,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 374a7efe339..e6812caa7d1 100644 --- a/wgpu/src/util/mod.rs +++ b/wgpu/src/util/mod.rs @@ -4,12 +4,7 @@ mod belt; mod device; mod encoder; -use std::{ - borrow::Cow, - future::Future, - mem::{align_of, size_of}, - ptr::copy_nonoverlapping, -}; +use std::future::Future; pub use belt::StagingBelt; pub use device::{BufferInitDescriptor, DeviceExt}; @@ -24,9 +19,15 @@ 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 { - const MAGIC_NUMBER: u32 = 0x0723_0203; + use std::{ + borrow::Cow, + mem::{align_of, size_of}, + ptr::copy_nonoverlapping, + }; + const MAGIC_NUMBER: u32 = 0x0723_0203; assert_eq!( data.len() % size_of::(), 0,