Skip to content

Commit

Permalink
Make Shader(Module)Source::Wgsl optional
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Oct 6, 2022
1 parent 261069d commit 74bfaf5
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 17 deletions.
13 changes: 8 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
RUST_BACKTRACE: 1
RUST_VERSION: 1.59
RUST_VERSION: '1.60'

# We distinguish the following kinds of builds:
# - local: build for the same target as we compile on, and do local tests
Expand Down Expand Up @@ -178,14 +178,17 @@ jobs:
- name: check web
if: matrix.kind == 'web'
run: |
# build with no features
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu
# build examples
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --examples
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --features wgsl --examples
# build with features
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --features glsl,spirv
# build for WebGL
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --features webgl
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --features webgl,wgsl
# build docs
cargo doc --target ${{ matrix.target }} -p wgpu --no-deps
Expand Down Expand Up @@ -224,9 +227,9 @@ jobs:
# run native tests
for backend in ${{ matrix.backends }}; do
echo "======= NATIVE TESTS $backend ======";
WGPU_BACKEND=$backend cargo nextest run -p wgpu --no-fail-fast
WGPU_BACKEND=$backend cargo nextest run -p wgpu --features wgsl --no-fail-fast
# Test that we catch overflows in `--release` builds too.
WGPU_BACKEND=$backend cargo nextest run --release -p wgpu --no-fail-fast
WGPU_BACKEND=$backend cargo nextest run --release -p wgpu --features wgsl --no-fail-fast
done
fmt:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ For an overview of all the components in the gfx-rs ecosystem, see [the big pict

### MSRV policy

Minimum Supported Rust Version is **1.59**.
Minimum Supported Rust Version is **1.60**.
It is enforced on CI (in "/.github/workflows/ci.yml") with `RUST_VERSION` variable.
This version can only be upgraded in breaking releases.

Expand Down
2 changes: 1 addition & 1 deletion deno_webgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ description = "WebGPU implementation for Deno"
deno_core = "0.151.0"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.19", features = ["full"] }
wgpu-core = { path = "../wgpu-core", features = ["trace", "replay", "serde", "strict_asserts"] }
wgpu-core = { path = "../wgpu-core", features = ["trace", "replay", "serde", "strict_asserts", "wgsl"] }
wgpu-types = { path = "../wgpu-types", features = ["trace", "replay", "serde"] }
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", "strict_asserts"]
features = ["replay", "raw-window-handle", "strict_asserts", "wgsl"]

[dev-dependencies]
serde = "1"
4 changes: 3 additions & 1 deletion wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ replay = ["serde", "wgt/replay", "arrayvec/serde", "naga/deserialize"]
# Enable serializable compute/render passes, and bundle encoders.
serial-pass = ["serde", "wgt/serde", "arrayvec/serde"]
id32 = []
# Enable `ShaderModuleSource::Wgsl`
wgsl = ["naga/wgsl-in"]
vulkan-portability = ["hal/vulkan"]

[dependencies]
Expand All @@ -46,7 +48,7 @@ thiserror = "1"
git = "https://github.com/gfx-rs/naga"
rev = "c52d9102"
version = "0.10"
features = ["clone", "span", "validate", "wgsl-in"]
features = ["clone", "span", "validate"]

[dependencies.wgt]
path = "../wgpu-types"
Expand Down
6 changes: 6 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ impl<A: HalApi> Device<A> {
source: pipeline::ShaderModuleSource<'a>,
) -> Result<pipeline::ShaderModule<A>, pipeline::CreateShaderModuleError> {
let (module, source) = match source {
#[cfg(feature = "wgsl")]
pipeline::ShaderModuleSource::Wgsl(code) => {
profiling::scope!("naga::wgsl::parse_str");
let module = naga::front::wgsl::parse_str(&code).map_err(|inner| {
Expand All @@ -1218,6 +1219,7 @@ impl<A: HalApi> Device<A> {
(Cow::Owned(module), code.into_owned())
}
pipeline::ShaderModuleSource::Naga(module) => (module, String::new()),
pipeline::ShaderModuleSource::Dummy(_) => panic!("found `ShaderModuleSource::Dummy`"),
};
for (_, var) in module.global_variables.iter() {
match var.binding {
Expand Down Expand Up @@ -4400,6 +4402,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
if let Some(ref trace) = device.trace {
let mut trace = trace.lock();
let data = match source {
#[cfg(feature = "wgsl")]
pipeline::ShaderModuleSource::Wgsl(ref code) => {
trace.make_binary("wgsl", code.as_bytes())
}
Expand All @@ -4409,6 +4412,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.unwrap();
trace.make_binary("ron", string.as_bytes())
}
pipeline::ShaderModuleSource::Dummy(_) => {
panic!("found `ShaderModuleSource::Dummy`")
}
};
trace.add(trace::Action::CreateShaderModule {
id: fid.id(),
Expand Down
10 changes: 9 additions & 1 deletion wgpu-core/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
validation, Label, LifeGuard, Stored,
};
use arrayvec::ArrayVec;
use std::{borrow::Cow, error::Error, fmt, num::NonZeroU32};
use std::{borrow::Cow, error::Error, fmt, marker::PhantomData, num::NonZeroU32};
use thiserror::Error;

/// Information about buffer bindings, which
Expand All @@ -20,8 +20,13 @@ pub(crate) struct LateSizedBufferGroup {

#[allow(clippy::large_enum_variant)]
pub enum ShaderModuleSource<'a> {
#[cfg(feature = "wgsl")]
Wgsl(Cow<'a, str>),
Naga(Cow<'static, naga::Module>),
/// Dummy variant because `Naga` doesn't have a lifetime and without enough active features it
/// could be the last one active.
#[doc(hidden)]
Dummy(PhantomData<&'a ()>),
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -63,6 +68,7 @@ pub struct ShaderError<E> {
pub label: Option<String>,
pub inner: E,
}
#[cfg(feature = "wgsl")]
impl fmt::Display for ShaderError<naga::front::wgsl::ParseError> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let label = self.label.as_deref().unwrap_or_default();
Expand Down Expand Up @@ -114,6 +120,7 @@ where
//Note: `Clone` would require `WithSpan: Clone`.
#[derive(Debug, Error)]
pub enum CreateShaderModuleError {
#[cfg(feature = "wgsl")]
#[error(transparent)]
Parsing(#[from] ShaderError<naga::front::wgsl::ParseError>),
#[error("Failed to generate the backend-specific code")]
Expand All @@ -137,6 +144,7 @@ pub enum CreateShaderModuleError {
impl CreateShaderModuleError {
pub fn location(&self, source: &str) -> Option<naga::SourceLocation> {
match *self {
#[cfg(feature = "wgsl")]
CreateShaderModuleError::Parsing(ref err) => err.inner.location(source),
CreateShaderModuleError::Validation(ref err) => err.inner.location(source),
_ => None,
Expand Down
1 change: 1 addition & 0 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ test = true
default = []
spirv = ["naga/spv-in"]
glsl = ["naga/glsl-in"]
wgsl = ["wgc?/wgsl"]
trace = ["serde", "wgc/trace"]
replay = ["serde", "wgc/replay"]
angle = ["wgc/angle"]
Expand Down
25 changes: 19 additions & 6 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor,
DownlevelCapabilities, Features, Label, Limits, LoadOp, MapMode, Operations,
PipelineLayoutDescriptor, RenderBundleEncoderDescriptor, RenderPipelineDescriptor,
SamplerDescriptor, ShaderModuleDescriptor, ShaderModuleDescriptorSpirV, ShaderSource,
SurfaceStatus, TextureDescriptor, TextureFormat, TextureViewDescriptor,
SamplerDescriptor, ShaderModuleDescriptor, ShaderModuleDescriptorSpirV, SurfaceStatus,
TextureDescriptor, TextureFormat, TextureViewDescriptor,
};

use arrayvec::ArrayVec;
Expand Down Expand Up @@ -1094,6 +1094,15 @@ impl crate::Context for Context {
}
}

#[cfg_attr(
not(any(
feature = "spirv",
feature = "glsl",
feature = "wgsl",
feature = "naga"
)),
allow(unreachable_code, unused_variables)
)]
fn device_create_shader_module(
&self,
device: &Self::DeviceId,
Expand All @@ -1107,7 +1116,7 @@ impl crate::Context for Context {
};
let source = match desc.source {
#[cfg(feature = "spirv")]
ShaderSource::SpirV(ref spv) => {
crate::ShaderSource::SpirV(ref spv) => {
// 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
Expand All @@ -1119,7 +1128,7 @@ impl crate::Context for Context {
wgc::pipeline::ShaderModuleSource::Naga(std::borrow::Cow::Owned(module))
}
#[cfg(feature = "glsl")]
ShaderSource::Glsl {
crate::ShaderSource::Glsl {
ref shader,
stage,
ref defines,
Expand All @@ -1134,9 +1143,13 @@ impl crate::Context for Context {

wgc::pipeline::ShaderModuleSource::Naga(std::borrow::Cow::Owned(module))
}
ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)),
#[cfg(feature = "wgsl")]
crate::ShaderSource::Wgsl(ref code) => {
wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code))
}
#[cfg(feature = "naga")]
ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module),
crate::ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module),
crate::ShaderSource::Dummy(_) => panic!("found `ShaderSource::Dummy`"),
};
let (id, error) = wgc::gfx_select!(
device.id => global.device_create_shader_module(device.id, &descriptor, source, ())
Expand Down
15 changes: 14 additions & 1 deletion wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,13 +1334,22 @@ impl crate::Context for Context {
wgt::DownlevelCapabilities::default()
}

#[cfg_attr(
not(any(
feature = "spirv",
feature = "glsl",
feature = "wgsl",
feature = "naga"
)),
allow(unreachable_code, unused_variables)
)]
fn device_create_shader_module(
&self,
device: &Self::DeviceId,
desc: crate::ShaderModuleDescriptor,
_shader_bound_checks: wgt::ShaderBoundChecks,
) -> Self::ShaderModuleId {
let mut descriptor = match desc.source {
let mut descriptor: web_sys::GpuShaderModuleDescriptor = match desc.source {
#[cfg(feature = "spirv")]
crate::ShaderSource::SpirV(ref spv) => {
use naga::{back, front, valid};
Expand Down Expand Up @@ -1392,6 +1401,7 @@ impl crate::Context for Context {
.unwrap();
web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str())
}
#[cfg(feature = "wgsl")]
crate::ShaderSource::Wgsl(ref code) => web_sys::GpuShaderModuleDescriptor::new(code),
#[cfg(feature = "naga")]
crate::ShaderSource::Naga(module) => {
Expand All @@ -1408,6 +1418,9 @@ impl crate::Context for Context {
back::wgsl::write_string(&module, &module_info, writer_flags).unwrap();
web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str())
}
crate::ShaderSource::Dummy(_) => {
panic!("found `ShaderSource::Dummy`")
}
};
if let Some(label) = desc.label {
descriptor.label(label);
Expand Down
6 changes: 6 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,11 +858,17 @@ pub enum ShaderSource<'a> {
defines: naga::FastHashMap<String, String>,
},
/// WGSL module as a string slice.
#[cfg(feature = "wgsl")]
#[cfg_attr(docsrs, doc(cfg(feature = "wgsl")))]
Wgsl(Cow<'a, str>),
/// Naga module.
#[cfg(feature = "naga")]
#[cfg_attr(docsrs, doc(cfg(feature = "naga")))]
Naga(Cow<'static, naga::Module>),
/// Dummy variant because `Naga` doesn't have a lifetime and without enough active features it
/// could be the last one active.
#[doc(hidden)]
Dummy(PhantomData<&'a ()>),
}
static_assertions::assert_impl_all!(ShaderSource: Send, Sync);

Expand Down

0 comments on commit 74bfaf5

Please sign in to comment.