Skip to content

Commit

Permalink
Context dynamic dispatch (#3051)
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB authored Dec 20, 2022
1 parent de070b2 commit 052bd17
Show file tree
Hide file tree
Showing 19 changed files with 8,685 additions and 3,672 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Additionally `Surface::get_default_config` now returns an Option and returns Non
- Make `Surface::get_default_config` return an Option to prevent panics. By @cwfitzgerald in [#3157](https://github.com/gfx-rs/wgpu/pull/3157)
- 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)
- Dereferencing a buffer view is now marked inline. By @Wumpf in [#3307](https://github.com/gfx-rs/wgpu/pull/3307)
- The `strict_assert` family of macros was moved to `wgpu-types`. By @i509VCB in [#3051](https://github.com/gfx-rs/wgpu/pull/3051)

#### WebGPU

Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ emscripten = ["hal/emscripten"]

# Apply run-time checks, even in release builds. These are in addition
# to the validation carried out at public APIs in all builds.
strict_asserts = []
strict_asserts = ["wgt/strict_asserts"]
angle = ["hal/gles"]
# Enable API tracing
trace = ["ron", "serde", "wgt/trace", "arrayvec/serde", "naga/serialize"]
Expand Down
12 changes: 12 additions & 0 deletions wgpu-core/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ impl<T> From<SerialId> for Id<T> {
}

impl<T> Id<T> {
/// # Safety
///
/// The raw id must be valid for the type.
pub unsafe fn from_raw(raw: NonZeroId) -> Self {
Self(raw, PhantomData)
}

#[allow(dead_code)]
pub(crate) fn dummy(index: u32) -> Valid<Self> {
Valid(Id::zip(index, 1, Backend::Empty))
Expand Down Expand Up @@ -165,13 +172,18 @@ pub(crate) struct Valid<I>(pub I);
/// need to construct `Id` values directly, or access their components, like the
/// WGPU recording player, may use this trait to do so.
pub trait TypedId: Copy {
fn as_raw(&self) -> NonZeroId;
fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self;
fn unzip(self) -> (Index, Epoch, Backend);
fn into_raw(self) -> NonZeroId;
}

#[allow(trivial_numeric_casts)]
impl<T> TypedId for Id<T> {
fn as_raw(&self) -> NonZeroId {
self.0
}

fn zip(index: Index, epoch: Epoch, backend: Backend) -> Self {
assert_eq!(0, epoch >> EPOCH_BITS);
assert_eq!(0, (index as IdType) >> INDEX_BITS);
Expand Down
3 changes: 0 additions & 3 deletions wgpu-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
clippy::pattern_type_mismatch,
)]

#[macro_use]
mod assertions;

pub mod binding_model;
pub mod command;
mod conv;
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/track/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
LifeGuard, RefCount,
};
use hal::BufferUses;
use wgt::{strict_assert, strict_assert_eq};

impl ResourceUses for BufferUses {
const EXCLUSIVE: Self = Self::EXCLUSIVE;
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/track/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
};
use bit_vec::BitVec;
use std::{borrow::Cow, marker::PhantomData, mem};
use wgt::strict_assert;

/// A set of resources, holding a [`RefCount`] and epoch for each member.
///
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub(crate) use stateless::{StatelessBindGroupSate, StatelessTracker};
pub(crate) use texture::{
TextureBindGroupState, TextureSelector, TextureTracker, TextureUsageScope,
};
use wgt::strict_assert_ne;

/// A structure containing all the information about a particular resource
/// transition. User code should be able to generate a pipeline barrier
Expand Down
1 change: 1 addition & 0 deletions wgpu-core/src/track/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use hal::TextureUses;

use arrayvec::ArrayVec;
use naga::FastHashMap;
use wgt::{strict_assert, strict_assert_eq};

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

Expand Down
1 change: 1 addition & 0 deletions wgpu-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
trace = ["serde"]
replay = ["serde"]
strict_asserts = []

[dependencies]
bitflags = "1"
Expand Down
66 changes: 66 additions & 0 deletions wgpu-types/src/assertions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Macros for validation internal to the wgpu.
//!
//! This module defines assertion macros that respect `wgpu-type`'s
//! `"strict_asserts"` feature.
//!
//! Because `wgpu-core`'s public APIs validate their arguments in all
//! types of builds, for performance, the `track` module skips some of
//! Rust's usual run-time checks on its internal operations in release
//! builds. However, some `wgpu-core` applications have a strong
//! preference for robustness over performance. To accommodate them,
//! `wgpu-core`'s `"strict_asserts"` feature enables that validation
//! in both debug and release builds.

/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated.
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert {
( $( $arg:tt )* ) => {
assert!( $( $arg )* )
}
}

/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated.
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert_eq {
( $( $arg:tt )* ) => {
assert_eq!( $( $arg )* )
}
}

/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated.
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert_ne {
( $( $arg:tt )* ) => {
assert_ne!( $( $arg )* )
}
}

/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated.
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert {
( $( $arg:tt )* ) => {
debug_assert!( $( $arg )* )
};
}

/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated.
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert_eq {
( $( $arg:tt )* ) => {
debug_assert_eq!( $( $arg )* )
};
}

/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated.
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert_ne {
( $( $arg:tt )* ) => {
debug_assert_ne!( $( $arg )* )
};
}
2 changes: 2 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use std::{num::NonZeroU32, ops::Range};

mod assertions;

// Use this macro instead of the one provided by the bitflags_serde_shim crate
// because the latter produces an error when deserializing bits that are not
// specified in the bitflags, while we want deserialization to succeed and
Expand Down
7 changes: 5 additions & 2 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ name = "water"
test = true

[features]
default = ["wgsl", "expose-ids"]
default = ["wgsl", "expose-ids", "strict_asserts"]
# Apply run-time checks, even in release builds. These are in addition
# to the validation carried out at public APIs in all builds.
strict_asserts = ["wgc/strict_asserts", "wgt/strict_asserts"]
spirv = ["naga/spv-in"]
glsl = ["naga/glsl-in"]
wgsl = ["wgc?/wgsl"]
Expand Down Expand Up @@ -148,10 +151,10 @@ raw-window-handle.workspace = true
serde = { workspace = true, features = ["derive"], optional = true }
smallvec.workspace = true
static_assertions.workspace = true
cfg-if.workspace = true

[dev-dependencies]
bitflags.workspace = true
cfg-if.workspace = true
bytemuck = { workspace = true, features = ["derive"] }
glam.workspace = true
ddsfile.workspace = true
Expand Down
Loading

0 comments on commit 052bd17

Please sign in to comment.