Skip to content

Commit

Permalink
Add WasmNotSendSync (#4702)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Nov 17, 2023
1 parent 3ec547c commit bec6560
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 67 deletions.
4 changes: 2 additions & 2 deletions examples/common/src/framework.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use wgpu::{Instance, Surface, WasmNotSend, WasmNotSync};
use wgpu::{Instance, Surface, WasmNotSendSync};
use wgpu_test::GpuTestConfiguration;
use winit::{
dpi::PhysicalSize,
Expand Down Expand Up @@ -504,7 +504,7 @@ pub struct ExampleTestParams<E> {
pub _phantom: std::marker::PhantomData<E>,
}

impl<E: Example + WasmNotSend + WasmNotSync> From<ExampleTestParams<E>> for GpuTestConfiguration {
impl<E: Example + WasmNotSendSync> From<ExampleTestParams<E>> for GpuTestConfiguration {
fn from(params: ExampleTestParams<E>) -> Self {
GpuTestConfiguration::new()
.name(params.name)
Expand Down
42 changes: 21 additions & 21 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ use std::{

use bitflags::bitflags;
use thiserror::Error;
use wgt::{WasmNotSend, WasmNotSync};
use wgt::WasmNotSendSync;

// - Vertex + Fragment
// - Compute
Expand Down Expand Up @@ -200,25 +200,25 @@ pub trait Api: Clone + fmt::Debug + Sized {

type Queue: Queue<Self>;
type CommandEncoder: CommandEncoder<Self>;
type CommandBuffer: WasmNotSend + WasmNotSync + fmt::Debug;
type CommandBuffer: WasmNotSendSync + fmt::Debug;

type Buffer: fmt::Debug + WasmNotSend + WasmNotSync + 'static;
type Texture: fmt::Debug + WasmNotSend + WasmNotSync + 'static;
type SurfaceTexture: fmt::Debug + WasmNotSend + WasmNotSync + Borrow<Self::Texture>;
type TextureView: fmt::Debug + WasmNotSend + WasmNotSync;
type Sampler: fmt::Debug + WasmNotSend + WasmNotSync;
type QuerySet: fmt::Debug + WasmNotSend + WasmNotSync;
type Fence: fmt::Debug + WasmNotSend + WasmNotSync;
type Buffer: fmt::Debug + WasmNotSendSync + 'static;
type Texture: fmt::Debug + WasmNotSendSync + 'static;
type SurfaceTexture: fmt::Debug + WasmNotSendSync + Borrow<Self::Texture>;
type TextureView: fmt::Debug + WasmNotSendSync;
type Sampler: fmt::Debug + WasmNotSendSync;
type QuerySet: fmt::Debug + WasmNotSendSync;
type Fence: fmt::Debug + WasmNotSendSync;

type BindGroupLayout: fmt::Debug + WasmNotSend + WasmNotSync;
type BindGroup: fmt::Debug + WasmNotSend + WasmNotSync;
type PipelineLayout: WasmNotSend + WasmNotSync;
type ShaderModule: fmt::Debug + WasmNotSend + WasmNotSync;
type RenderPipeline: WasmNotSend + WasmNotSync;
type ComputePipeline: WasmNotSend + WasmNotSync;
type BindGroupLayout: fmt::Debug + WasmNotSendSync;
type BindGroup: fmt::Debug + WasmNotSendSync;
type PipelineLayout: WasmNotSendSync;
type ShaderModule: fmt::Debug + WasmNotSendSync;
type RenderPipeline: WasmNotSendSync;
type ComputePipeline: WasmNotSendSync;
}

pub trait Instance<A: Api>: Sized + WasmNotSend + WasmNotSync {
pub trait Instance<A: Api>: Sized + WasmNotSendSync {
unsafe fn init(desc: &InstanceDescriptor) -> Result<Self, InstanceError>;
unsafe fn create_surface(
&self,
Expand All @@ -229,7 +229,7 @@ pub trait Instance<A: Api>: Sized + WasmNotSend + WasmNotSync {
unsafe fn enumerate_adapters(&self) -> Vec<ExposedAdapter<A>>;
}

pub trait Surface<A: Api>: WasmNotSend + WasmNotSync {
pub trait Surface<A: Api>: WasmNotSendSync {
/// Configures the surface to use the given device.
///
/// # Safety
Expand Down Expand Up @@ -271,7 +271,7 @@ pub trait Surface<A: Api>: WasmNotSend + WasmNotSync {
unsafe fn discard_texture(&mut self, texture: A::SurfaceTexture);
}

pub trait Adapter<A: Api>: WasmNotSend + WasmNotSync {
pub trait Adapter<A: Api>: WasmNotSendSync {
unsafe fn open(
&self,
features: wgt::Features,
Expand All @@ -295,7 +295,7 @@ pub trait Adapter<A: Api>: WasmNotSend + WasmNotSync {
unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;
}

pub trait Device<A: Api>: WasmNotSend + WasmNotSync {
pub trait Device<A: Api>: WasmNotSendSync {
/// Exit connection to this logical device.
unsafe fn exit(self, queue: A::Queue);
/// Creates a new buffer.
Expand Down Expand Up @@ -391,7 +391,7 @@ pub trait Device<A: Api>: WasmNotSend + WasmNotSync {
unsafe fn stop_capture(&self);
}

pub trait Queue<A: Api>: WasmNotSend + WasmNotSync {
pub trait Queue<A: Api>: WasmNotSendSync {
/// Submits the command buffers for execution on GPU.
///
/// Valid usage:
Expand All @@ -415,7 +415,7 @@ pub trait Queue<A: Api>: WasmNotSend + WasmNotSync {
/// Serves as a parent for all the encoded command buffers.
/// Works in bursts of action: one or more command buffers are recorded,
/// then submitted to a queue, and then it needs to be `reset_all()`.
pub trait CommandEncoder<A: Api>: WasmNotSend + WasmNotSync + fmt::Debug {
pub trait CommandEncoder<A: Api>: WasmNotSendSync + fmt::Debug {
/// Begin encoding a new command buffer.
unsafe fn begin_encoding(&mut self, label: Label) -> Result<(), DeviceError>;
/// Discard currently recorded list, if any.
Expand Down
2 changes: 2 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6663,6 +6663,8 @@ pub use send_sync::*;

#[doc(hidden)]
mod send_sync {
pub trait WasmNotSendSync: WasmNotSend + WasmNotSync {}
impl<T: WasmNotSend + WasmNotSync> WasmNotSendSync for T {}
#[cfg(any(
not(target_arch = "wasm32"),
all(
Expand Down
8 changes: 4 additions & 4 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{
use wgc::command::{bundle_ffi::*, compute_ffi::*, render_ffi::*};
use wgc::device::DeviceLostClosure;
use wgc::id::TypedId;
use wgt::{WasmNotSend, WasmNotSync};
use wgt::WasmNotSendSync;

const LABEL: &str = "label";

Expand Down Expand Up @@ -306,7 +306,7 @@ impl Context {
fn handle_error(
&self,
sink_mutex: &Mutex<ErrorSinkRaw>,
cause: impl Error + WasmNotSend + WasmNotSync + 'static,
cause: impl Error + WasmNotSendSync + 'static,
label_key: &'static str,
label: Label,
string: &'static str,
Expand Down Expand Up @@ -340,7 +340,7 @@ impl Context {
fn handle_error_nolabel(
&self,
sink_mutex: &Mutex<ErrorSinkRaw>,
cause: impl Error + WasmNotSend + WasmNotSync + 'static,
cause: impl Error + WasmNotSendSync + 'static,
string: &'static str,
) {
self.handle_error(sink_mutex, cause, "", None, string)
Expand All @@ -349,7 +349,7 @@ impl Context {
#[track_caller]
fn handle_error_fatal(
&self,
cause: impl Error + WasmNotSend + WasmNotSync + 'static,
cause: impl Error + WasmNotSendSync + 'static,
operation: &'static str,
) -> ! {
panic!("Error in {operation}: {f}", f = self.format_error(&cause));
Expand Down
58 changes: 28 additions & 30 deletions wgpu/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use wgt::{
strict_assert, strict_assert_eq, AdapterInfo, BufferAddress, BufferSize, Color,
DeviceLostReason, DownlevelCapabilities, DynamicOffset, Extent3d, Features, ImageDataLayout,
ImageSubresourceRange, IndexFormat, Limits, ShaderStages, SurfaceStatus, TextureFormat,
TextureFormatFeatures, WasmNotSend, WasmNotSync,
TextureFormatFeatures, WasmNotSend, WasmNotSendSync,
};

use crate::{
Expand All @@ -27,55 +27,55 @@ impl<T: Into<ObjectId> + From<ObjectId> + Debug + 'static> ContextId for T {}
/// Meta trait for an data associated with an id tracked by a context.
///
/// There is no need to manually implement this trait since there is a blanket implementation for this trait.
pub trait ContextData: Debug + WasmNotSend + WasmNotSync + 'static {}
impl<T: Debug + WasmNotSend + WasmNotSync + 'static> ContextData for T {}
pub trait ContextData: Debug + WasmNotSendSync + 'static {}
impl<T: Debug + WasmNotSendSync + 'static> ContextData for T {}

pub trait Context: Debug + WasmNotSend + WasmNotSync + Sized {
type AdapterId: ContextId + WasmNotSend + WasmNotSync;
pub trait Context: Debug + WasmNotSendSync + Sized {
type AdapterId: ContextId + WasmNotSendSync;
type AdapterData: ContextData;
type DeviceId: ContextId + WasmNotSend + WasmNotSync;
type DeviceId: ContextId + WasmNotSendSync;
type DeviceData: ContextData;
type QueueId: ContextId + WasmNotSend + WasmNotSync;
type QueueId: ContextId + WasmNotSendSync;
type QueueData: ContextData;
type ShaderModuleId: ContextId + WasmNotSend + WasmNotSync;
type ShaderModuleId: ContextId + WasmNotSendSync;
type ShaderModuleData: ContextData;
type BindGroupLayoutId: ContextId + WasmNotSend + WasmNotSync;
type BindGroupLayoutId: ContextId + WasmNotSendSync;
type BindGroupLayoutData: ContextData;
type BindGroupId: ContextId + WasmNotSend + WasmNotSync;
type BindGroupId: ContextId + WasmNotSendSync;
type BindGroupData: ContextData;
type TextureViewId: ContextId + WasmNotSend + WasmNotSync;
type TextureViewId: ContextId + WasmNotSendSync;
type TextureViewData: ContextData;
type SamplerId: ContextId + WasmNotSend + WasmNotSync;
type SamplerId: ContextId + WasmNotSendSync;
type SamplerData: ContextData;
type BufferId: ContextId + WasmNotSend + WasmNotSync;
type BufferId: ContextId + WasmNotSendSync;
type BufferData: ContextData;
type TextureId: ContextId + WasmNotSend + WasmNotSync;
type TextureId: ContextId + WasmNotSendSync;
type TextureData: ContextData;
type QuerySetId: ContextId + WasmNotSend + WasmNotSync;
type QuerySetId: ContextId + WasmNotSendSync;
type QuerySetData: ContextData;
type PipelineLayoutId: ContextId + WasmNotSend + WasmNotSync;
type PipelineLayoutId: ContextId + WasmNotSendSync;
type PipelineLayoutData: ContextData;
type RenderPipelineId: ContextId + WasmNotSend + WasmNotSync;
type RenderPipelineId: ContextId + WasmNotSendSync;
type RenderPipelineData: ContextData;
type ComputePipelineId: ContextId + WasmNotSend + WasmNotSync;
type ComputePipelineId: ContextId + WasmNotSendSync;
type ComputePipelineData: ContextData;
type CommandEncoderId: ContextId + WasmNotSend + WasmNotSync;
type CommandEncoderId: ContextId + WasmNotSendSync;
type CommandEncoderData: ContextData;
type ComputePassId: ContextId;
type ComputePassData: ContextData;
type RenderPassId: ContextId;
type RenderPassData: ContextData;
type CommandBufferId: ContextId + WasmNotSend + WasmNotSync;
type CommandBufferId: ContextId + WasmNotSendSync;
type CommandBufferData: ContextData;
type RenderBundleEncoderId: ContextId;
type RenderBundleEncoderData: ContextData;
type RenderBundleId: ContextId + WasmNotSend + WasmNotSync;
type RenderBundleId: ContextId + WasmNotSendSync;
type RenderBundleData: ContextData;
type SurfaceId: ContextId + WasmNotSend + WasmNotSync;
type SurfaceId: ContextId + WasmNotSendSync;
type SurfaceData: ContextData;

type SurfaceOutputDetail: WasmNotSend + WasmNotSync + 'static;
type SubmissionIndex: ContextId + Clone + Copy + WasmNotSend + WasmNotSync;
type SurfaceOutputDetail: WasmNotSendSync + 'static;
type SubmissionIndex: ContextId + Clone + Copy + WasmNotSendSync;
type SubmissionIndexData: ContextData + Copy;

type RequestAdapterFuture: Future<Output = Option<(Self::AdapterId, Self::AdapterData)>>
Expand Down Expand Up @@ -1082,15 +1082,13 @@ impl ObjectId {
))]
static_assertions::assert_impl_all!(ObjectId: Send, Sync);

pub(crate) fn downcast_ref<T: Debug + WasmNotSend + WasmNotSync + 'static>(
data: &crate::Data,
) -> &T {
pub(crate) fn downcast_ref<T: Debug + WasmNotSendSync + 'static>(data: &crate::Data) -> &T {
strict_assert!(data.is::<T>());
// Copied from std.
unsafe { &*(data as *const dyn Any as *const T) }
}

fn downcast_mut<T: Debug + WasmNotSend + WasmNotSync + 'static>(data: &mut crate::Data) -> &mut T {
fn downcast_mut<T: Debug + WasmNotSendSync + 'static>(data: &mut crate::Data) -> &mut T {
strict_assert!(data.is::<T>());
// Copied from std.
unsafe { &mut *(data as *mut dyn Any as *mut T) }
Expand Down Expand Up @@ -1228,7 +1226,7 @@ pub type DeviceLostCallback = Box<dyn FnOnce(DeviceLostReason, String) + Send +
pub type DeviceLostCallback = Box<dyn FnOnce(DeviceLostReason, String) + 'static>;

/// An object safe variant of [`Context`] implemented by all types that implement [`Context`].
pub(crate) trait DynContext: Debug + WasmNotSend + WasmNotSync {
pub(crate) trait DynContext: Debug + WasmNotSendSync {
fn as_any(&self) -> &dyn Any;

unsafe fn instance_create_surface(
Expand Down Expand Up @@ -4089,7 +4087,7 @@ where
}
}

pub trait QueueWriteBuffer: WasmNotSend + WasmNotSync {
pub trait QueueWriteBuffer: WasmNotSendSync {
fn slice(&self) -> &[u8];

fn slice_mut(&mut self) -> &mut [u8];
Expand Down
16 changes: 6 additions & 10 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use wgt::{
StencilFaceState, StencilOperation, StencilState, StorageTextureAccess, SurfaceCapabilities,
SurfaceStatus, TextureAspect, TextureDimension, TextureFormat, TextureFormatFeatureFlags,
TextureFormatFeatures, TextureSampleType, TextureUsages, TextureViewDimension, VertexAttribute,
VertexFormat, VertexStepMode, WasmNotSend, WasmNotSync, COPY_BUFFER_ALIGNMENT,
VertexFormat, VertexStepMode, WasmNotSend, WasmNotSendSync, WasmNotSync, COPY_BUFFER_ALIGNMENT,
COPY_BYTES_PER_ROW_ALIGNMENT, MAP_ALIGNMENT, PUSH_CONSTANT_ALIGNMENT,
QUERY_RESOLVE_BUFFER_ALIGNMENT, QUERY_SET_MAX_QUERIES, QUERY_SIZE, VERTEX_STRIDE_ALIGNMENT,
};
Expand Down Expand Up @@ -380,10 +380,6 @@ impl Drop for Sampler {
pub type SurfaceConfiguration = wgt::SurfaceConfiguration<Vec<TextureFormat>>;
static_assertions::assert_impl_all!(SurfaceConfiguration: Send, Sync);

trait WgpuSurfaceRequirement: WasmNotSend + WasmNotSync {}

impl<T: WasmNotSend + WasmNotSync> WgpuSurfaceRequirement for T {}

/// Handle to a presentable surface.
///
/// A `Surface` represents a platform-specific surface (e.g. a window) onto which rendered images may
Expand All @@ -394,7 +390,7 @@ impl<T: WasmNotSend + WasmNotSync> WgpuSurfaceRequirement for T {}
/// serves a similar role.
pub struct Surface<'window> {
context: Arc<C>,
_surface: Option<Box<dyn WgpuSurfaceRequirement + 'window>>,
_surface: Option<Box<dyn WasmNotSendSync + 'window>>,
id: ObjectId,
data: Box<Data>,
// Stores the latest `SurfaceConfiguration` that was set using `Surface::configure`.
Expand Down Expand Up @@ -1966,7 +1962,7 @@ impl Instance {
window: W,
) -> Result<Surface<'window>, CreateSurfaceError>
where
W: HasWindowHandle + HasDisplayHandle + WasmNotSend + WasmNotSync + 'window,
W: HasWindowHandle + HasDisplayHandle + WasmNotSendSync + 'window,
{
let mut surface = unsafe { self.create_surface_from_raw(&window) }?;
surface._surface = Some(Box::new(window));
Expand Down Expand Up @@ -5460,12 +5456,12 @@ mod send_sync {
use std::any::Any;
use std::fmt;

use wgt::{WasmNotSend, WasmNotSync};
use wgt::WasmNotSendSync;

pub trait AnyWasmNotSendSync: Any + WasmNotSend + WasmNotSync {
pub trait AnyWasmNotSendSync: Any + WasmNotSendSync {
fn upcast_any_ref(&self) -> &dyn Any;
}
impl<T: Any + WasmNotSend + WasmNotSync> AnyWasmNotSendSync for T {
impl<T: Any + WasmNotSendSync> AnyWasmNotSendSync for T {
#[inline]
fn upcast_any_ref(&self) -> &dyn Any {
self
Expand Down

0 comments on commit bec6560

Please sign in to comment.