Skip to content

Commit 052bd17

Browse files
authored
Context dynamic dispatch (#3051)
1 parent de070b2 commit 052bd17

File tree

19 files changed

+8685
-3672
lines changed

19 files changed

+8685
-3672
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Additionally `Surface::get_default_config` now returns an Option and returns Non
115115
- Make `Surface::get_default_config` return an Option to prevent panics. By @cwfitzgerald in [#3157](https://github.com/gfx-rs/wgpu/pull/3157)
116116
- Lower the `max_buffer_size` limit value for compatibility with Apple2 and WebGPU compliance. By @jinleili in [#3255](https://github.com/gfx-rs/wgpu/pull/3255)
117117
- Dereferencing a buffer view is now marked inline. By @Wumpf in [#3307](https://github.com/gfx-rs/wgpu/pull/3307)
118+
- The `strict_assert` family of macros was moved to `wgpu-types`. By @i509VCB in [#3051](https://github.com/gfx-rs/wgpu/pull/3051)
118119

119120
#### WebGPU
120121

wgpu-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ emscripten = ["hal/emscripten"]
3434

3535
# Apply run-time checks, even in release builds. These are in addition
3636
# to the validation carried out at public APIs in all builds.
37-
strict_asserts = []
37+
strict_asserts = ["wgt/strict_asserts"]
3838
angle = ["hal/gles"]
3939
# Enable API tracing
4040
trace = ["ron", "serde", "wgt/trace", "arrayvec/serde", "naga/serialize"]

wgpu-core/src/id.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ impl<T> From<SerialId> for Id<T> {
9393
}
9494

9595
impl<T> Id<T> {
96+
/// # Safety
97+
///
98+
/// The raw id must be valid for the type.
99+
pub unsafe fn from_raw(raw: NonZeroId) -> Self {
100+
Self(raw, PhantomData)
101+
}
102+
96103
#[allow(dead_code)]
97104
pub(crate) fn dummy(index: u32) -> Valid<Self> {
98105
Valid(Id::zip(index, 1, Backend::Empty))
@@ -165,13 +172,18 @@ pub(crate) struct Valid<I>(pub I);
165172
/// need to construct `Id` values directly, or access their components, like the
166173
/// WGPU recording player, may use this trait to do so.
167174
pub trait TypedId: Copy {
175+
fn as_raw(&self) -> NonZeroId;
168176
fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self;
169177
fn unzip(self) -> (Index, Epoch, Backend);
170178
fn into_raw(self) -> NonZeroId;
171179
}
172180

173181
#[allow(trivial_numeric_casts)]
174182
impl<T> TypedId for Id<T> {
183+
fn as_raw(&self) -> NonZeroId {
184+
self.0
185+
}
186+
175187
fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self {
176188
assert_eq!(0, epoch >> EPOCH_BITS);
177189
assert_eq!(0, (index as IdType) >> INDEX_BITS);

wgpu-core/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
clippy::pattern_type_mismatch,
3636
)]
3737

38-
#[macro_use]
39-
mod assertions;
40-
4138
pub mod binding_model;
4239
pub mod command;
4340
mod conv;

wgpu-core/src/track/buffer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::{
1919
LifeGuard, RefCount,
2020
};
2121
use hal::BufferUses;
22+
use wgt::{strict_assert, strict_assert_eq};
2223

2324
impl ResourceUses for BufferUses {
2425
const EXCLUSIVE: Self = Self::EXCLUSIVE;

wgpu-core/src/track/metadata.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
};
88
use bit_vec::BitVec;
99
use std::{borrow::Cow, marker::PhantomData, mem};
10+
use wgt::strict_assert;
1011

1112
/// A set of resources, holding a [`RefCount`] and epoch for each member.
1213
///

wgpu-core/src/track/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ pub(crate) use stateless::{StatelessBindGroupSate, StatelessTracker};
114114
pub(crate) use texture::{
115115
TextureBindGroupState, TextureSelector, TextureTracker, TextureUsageScope,
116116
};
117+
use wgt::strict_assert_ne;
117118

118119
/// A structure containing all the information about a particular resource
119120
/// transition. User code should be able to generate a pipeline barrier

wgpu-core/src/track/texture.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use hal::TextureUses;
3434

3535
use arrayvec::ArrayVec;
3636
use naga::FastHashMap;
37+
use wgt::{strict_assert, strict_assert_eq};
3738

3839
use std::{borrow::Cow, iter, marker::PhantomData, ops::Range, vec::Drain};
3940

wgpu-types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
1818
[features]
1919
trace = ["serde"]
2020
replay = ["serde"]
21+
strict_asserts = []
2122

2223
[dependencies]
2324
bitflags = "1"

wgpu-types/src/assertions.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Macros for validation internal to the wgpu.
2+
//!
3+
//! This module defines assertion macros that respect `wgpu-type`'s
4+
//! `"strict_asserts"` feature.
5+
//!
6+
//! Because `wgpu-core`'s public APIs validate their arguments in all
7+
//! types of builds, for performance, the `track` module skips some of
8+
//! Rust's usual run-time checks on its internal operations in release
9+
//! builds. However, some `wgpu-core` applications have a strong
10+
//! preference for robustness over performance. To accommodate them,
11+
//! `wgpu-core`'s `"strict_asserts"` feature enables that validation
12+
//! in both debug and release builds.
13+
14+
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated.
15+
#[cfg(feature = "strict_asserts")]
16+
#[macro_export]
17+
macro_rules! strict_assert {
18+
( $( $arg:tt )* ) => {
19+
assert!( $( $arg )* )
20+
}
21+
}
22+
23+
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated.
24+
#[cfg(feature = "strict_asserts")]
25+
#[macro_export]
26+
macro_rules! strict_assert_eq {
27+
( $( $arg:tt )* ) => {
28+
assert_eq!( $( $arg )* )
29+
}
30+
}
31+
32+
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated.
33+
#[cfg(feature = "strict_asserts")]
34+
#[macro_export]
35+
macro_rules! strict_assert_ne {
36+
( $( $arg:tt )* ) => {
37+
assert_ne!( $( $arg )* )
38+
}
39+
}
40+
41+
/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated.
42+
#[cfg(not(feature = "strict_asserts"))]
43+
#[macro_export]
44+
macro_rules! strict_assert {
45+
( $( $arg:tt )* ) => {
46+
debug_assert!( $( $arg )* )
47+
};
48+
}
49+
50+
/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated.
51+
#[cfg(not(feature = "strict_asserts"))]
52+
#[macro_export]
53+
macro_rules! strict_assert_eq {
54+
( $( $arg:tt )* ) => {
55+
debug_assert_eq!( $( $arg )* )
56+
};
57+
}
58+
59+
/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated.
60+
#[cfg(not(feature = "strict_asserts"))]
61+
#[macro_export]
62+
macro_rules! strict_assert_ne {
63+
( $( $arg:tt )* ) => {
64+
debug_assert_ne!( $( $arg )* )
65+
};
66+
}

0 commit comments

Comments
 (0)