Skip to content

Commit 02cc2ae

Browse files
authored
[BREAKING] Make Shader(Module)Source::Wgsl optional (#2890)
1 parent 6451d31 commit 02cc2ae

File tree

12 files changed

+63
-9
lines changed

12 files changed

+63
-9
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ jobs:
178178
- name: check web
179179
if: matrix.kind == 'web'
180180
run: |
181+
# build with no features
182+
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --no-default-features
183+
181184
# build examples
182185
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu --examples
183186
@@ -194,7 +197,7 @@ jobs:
194197
if: matrix.kind == 'em'
195198
run: |
196199
# build for Emscripten/WebGL
197-
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu -p wgpu-hal --features webgl,emscripten
200+
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu -p wgpu-hal --no-default-features --features webgl,emscripten
198201
199202
# build raw-gles example
200203
cargo ${{matrix.tool}} --target ${{ matrix.target }} --example raw-gles --features webgl,emscripten
@@ -203,7 +206,7 @@ jobs:
203206
if: matrix.kind == 'local' || matrix.kind == 'other'
204207
run: |
205208
# check with no features
206-
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu -p wgpu-core -p wgpu-info -p player
209+
cargo ${{matrix.tool}} --target ${{ matrix.target }} -p wgpu -p wgpu-core -p wgpu-info -p player --no-default-features
207210
208211
# check with all features
209212
# explicitly don't mention wgpu-hal so that --all-features don't apply to it

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Bottom level categories:
4747
- Convert all `Default` Implementations on Enums to `derive(Default)`
4848
- Implement `Default` for `CompositeAlphaMode`
4949

50+
### Added/New Features
51+
52+
Add the `"wgsl"` feature, to enable WGSL shaders in `wgpu-core` and `wgpu`. Enabled by default in `wgpu`. By @daxpedda in [#2890](https://github.com/gfx-rs/wgpu/pull/2890).
53+
5054
### Bug Fixes
5155

5256
#### General

deno_webgpu/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ description = "WebGPU implementation for Deno"
1414
deno_core = "0.151.0"
1515
serde = { version = "1.0", features = ["derive"] }
1616
tokio = { version = "1.19", features = ["full"] }
17-
wgpu-core = { path = "../wgpu-core", features = ["trace", "replay", "serde", "strict_asserts"] }
17+
wgpu-core = { path = "../wgpu-core", features = ["trace", "replay", "serde", "strict_asserts", "wgsl"] }
1818
wgpu-types = { path = "../wgpu-types", features = ["trace", "replay", "serde"] }

player/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ features = ["replay"]
2929
[dependencies.wgc]
3030
path = "../wgpu-core"
3131
package = "wgpu-core"
32-
features = ["replay", "raw-window-handle", "strict_asserts"]
32+
features = ["replay", "raw-window-handle", "strict_asserts", "wgsl"]
3333

3434
[dev-dependencies]
3535
serde = "1"

wgpu-core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ replay = ["serde", "wgt/replay", "arrayvec/serde", "naga/deserialize"]
2424
# Enable serializable compute/render passes, and bundle encoders.
2525
serial-pass = ["serde", "wgt/serde", "arrayvec/serde"]
2626
id32 = []
27+
# Enable `ShaderModuleSource::Wgsl`
28+
wgsl = ["naga/wgsl-in"]
2729
vulkan-portability = ["hal/vulkan"]
2830

2931
[dependencies]
@@ -46,7 +48,7 @@ thiserror = "1"
4648
git = "https://github.com/gfx-rs/naga"
4749
rev = "c52d9102"
4850
version = "0.10"
49-
features = ["clone", "span", "validate", "wgsl-in"]
51+
features = ["clone", "span", "validate"]
5052

5153
[dependencies.wgt]
5254
path = "../wgpu-types"

wgpu-core/src/device/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,7 @@ impl<A: HalApi> Device<A> {
12031203
source: pipeline::ShaderModuleSource<'a>,
12041204
) -> Result<pipeline::ShaderModule<A>, pipeline::CreateShaderModuleError> {
12051205
let (module, source) = match source {
1206+
#[cfg(feature = "wgsl")]
12061207
pipeline::ShaderModuleSource::Wgsl(code) => {
12071208
profiling::scope!("naga::wgsl::parse_str");
12081209
let module = naga::front::wgsl::parse_str(&code).map_err(|inner| {
@@ -1215,6 +1216,7 @@ impl<A: HalApi> Device<A> {
12151216
(Cow::Owned(module), code.into_owned())
12161217
}
12171218
pipeline::ShaderModuleSource::Naga(module) => (module, String::new()),
1219+
pipeline::ShaderModuleSource::Dummy(_) => panic!("found `ShaderModuleSource::Dummy`"),
12181220
};
12191221
for (_, var) in module.global_variables.iter() {
12201222
match var.binding {
@@ -4397,6 +4399,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
43974399
if let Some(ref trace) = device.trace {
43984400
let mut trace = trace.lock();
43994401
let data = match source {
4402+
#[cfg(feature = "wgsl")]
44004403
pipeline::ShaderModuleSource::Wgsl(ref code) => {
44014404
trace.make_binary("wgsl", code.as_bytes())
44024405
}
@@ -4406,6 +4409,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
44064409
.unwrap();
44074410
trace.make_binary("ron", string.as_bytes())
44084411
}
4412+
pipeline::ShaderModuleSource::Dummy(_) => {
4413+
panic!("found `ShaderModuleSource::Dummy`")
4414+
}
44094415
};
44104416
trace.add(trace::Action::CreateShaderModule {
44114417
id: fid.id(),

wgpu-core/src/pipeline.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
validation, Label, LifeGuard, Stored,
77
};
88
use arrayvec::ArrayVec;
9-
use std::{borrow::Cow, error::Error, fmt, num::NonZeroU32};
9+
use std::{borrow::Cow, error::Error, fmt, marker::PhantomData, num::NonZeroU32};
1010
use thiserror::Error;
1111

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

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

2732
#[derive(Clone, Debug)]
@@ -63,6 +68,7 @@ pub struct ShaderError<E> {
6368
pub label: Option<String>,
6469
pub inner: E,
6570
}
71+
#[cfg(feature = "wgsl")]
6672
impl fmt::Display for ShaderError<naga::front::wgsl::ParseError> {
6773
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6874
let label = self.label.as_deref().unwrap_or_default();
@@ -114,6 +120,7 @@ where
114120
//Note: `Clone` would require `WithSpan: Clone`.
115121
#[derive(Debug, Error)]
116122
pub enum CreateShaderModuleError {
123+
#[cfg(feature = "wgsl")]
117124
#[error(transparent)]
118125
Parsing(#[from] ShaderError<naga::front::wgsl::ParseError>),
119126
#[error("Failed to generate the backend-specific code")]
@@ -137,6 +144,7 @@ pub enum CreateShaderModuleError {
137144
impl CreateShaderModuleError {
138145
pub fn location(&self, source: &str) -> Option<naga::SourceLocation> {
139146
match *self {
147+
#[cfg(feature = "wgsl")]
140148
CreateShaderModuleError::Parsing(ref err) => err.inner.location(source),
141149
CreateShaderModuleError::Validation(ref err) => err.inner.location(source),
142150
_ => None,

wgpu-hal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ homepage = "https://github.com/gfx-rs/wgpu"
88
repository = "https://github.com/gfx-rs/wgpu"
99
keywords = ["graphics"]
1010
license = "MIT OR Apache-2.0"
11-
rust-version = "1.59"
11+
rust-version = "1.60"
1212

1313
[lib]
1414

wgpu/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ name = "water"
7676
test = true
7777

7878
[features]
79-
default = []
79+
default = ["wgsl"]
8080
spirv = ["naga/spv-in"]
8181
glsl = ["naga/glsl-in"]
82+
wgsl = ["wgc?/wgsl"]
8283
trace = ["serde", "wgc/trace"]
8384
replay = ["serde", "wgc/replay"]
8485
angle = ["wgc/angle"]

wgpu/src/backend/direct.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,15 @@ impl crate::Context for Context {
10941094
}
10951095
}
10961096

1097+
#[cfg_attr(
1098+
not(any(
1099+
feature = "spirv",
1100+
feature = "glsl",
1101+
feature = "wgsl",
1102+
feature = "naga"
1103+
)),
1104+
allow(unreachable_code, unused_variables)
1105+
)]
10971106
fn device_create_shader_module(
10981107
&self,
10991108
device: &Self::DeviceId,
@@ -1134,9 +1143,11 @@ impl crate::Context for Context {
11341143

11351144
wgc::pipeline::ShaderModuleSource::Naga(std::borrow::Cow::Owned(module))
11361145
}
1146+
#[cfg(feature = "wgsl")]
11371147
ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)),
11381148
#[cfg(feature = "naga")]
11391149
ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module),
1150+
ShaderSource::Dummy(_) => panic!("found `ShaderSource::Dummy`"),
11401151
};
11411152
let (id, error) = wgc::gfx_select!(
11421153
device.id => global.device_create_shader_module(device.id, &descriptor, source, ())

0 commit comments

Comments
 (0)