Skip to content

Commit

Permalink
Make spirv an optional feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jun 21, 2021
1 parent 56d584a commit a04151a
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion player/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ impl<A: HalApi> Device<A> {
source: pipeline::ShaderModuleSource<'a>,
) -> Result<pipeline::ShaderModule<A>, 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.
Expand Down Expand Up @@ -3476,6 +3477,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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)
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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),
Expand Down
6 changes: 5 additions & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ all-features = true

[features]
default = []
spirv = ["wgc/spirv"]
trace = ["serde", "wgc/trace"]
replay = ["serde", "wgc/replay"]
webgl = ["wgc"]
Expand Down Expand Up @@ -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 = [
Expand Down
12 changes: 12 additions & 0 deletions wgpu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ 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 -- <input.spv> <output.wgsl>

```

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`:
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
};
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
15 changes: 8 additions & 7 deletions wgpu/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -24,15 +19,21 @@ 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.
pub fn make_spirv_raw(data: &[u8]) -> Cow<[u32]> {
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::<u32>(),
0,
Expand Down

0 comments on commit a04151a

Please sign in to comment.