From bec6560e10dc42301ef09fcaf7540ccaf3d2315e Mon Sep 17 00:00:00 2001 From: daxpedda Date: Fri, 17 Nov 2023 01:47:10 +0100 Subject: [PATCH] Add `WasmNotSendSync` (#4702) --- examples/common/src/framework.rs | 4 +-- wgpu-hal/src/lib.rs | 42 +++++++++++------------ wgpu-types/src/lib.rs | 2 ++ wgpu/src/backend/direct.rs | 8 ++--- wgpu/src/context.rs | 58 +++++++++++++++----------------- wgpu/src/lib.rs | 16 ++++----- 6 files changed, 63 insertions(+), 67 deletions(-) diff --git a/examples/common/src/framework.rs b/examples/common/src/framework.rs index a62423f61..d39da341e 100644 --- a/examples/common/src/framework.rs +++ b/examples/common/src/framework.rs @@ -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, @@ -504,7 +504,7 @@ pub struct ExampleTestParams { pub _phantom: std::marker::PhantomData, } -impl From> for GpuTestConfiguration { +impl From> for GpuTestConfiguration { fn from(params: ExampleTestParams) -> Self { GpuTestConfiguration::new() .name(params.name) diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 6c8e36ab7..378d590b3 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -95,7 +95,7 @@ use std::{ use bitflags::bitflags; use thiserror::Error; -use wgt::{WasmNotSend, WasmNotSync}; +use wgt::WasmNotSendSync; // - Vertex + Fragment // - Compute @@ -200,25 +200,25 @@ pub trait Api: Clone + fmt::Debug + Sized { type Queue: Queue; type CommandEncoder: CommandEncoder; - 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; - 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; + 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: Sized + WasmNotSend + WasmNotSync { +pub trait Instance: Sized + WasmNotSendSync { unsafe fn init(desc: &InstanceDescriptor) -> Result; unsafe fn create_surface( &self, @@ -229,7 +229,7 @@ pub trait Instance: Sized + WasmNotSend + WasmNotSync { unsafe fn enumerate_adapters(&self) -> Vec>; } -pub trait Surface: WasmNotSend + WasmNotSync { +pub trait Surface: WasmNotSendSync { /// Configures the surface to use the given device. /// /// # Safety @@ -271,7 +271,7 @@ pub trait Surface: WasmNotSend + WasmNotSync { unsafe fn discard_texture(&mut self, texture: A::SurfaceTexture); } -pub trait Adapter: WasmNotSend + WasmNotSync { +pub trait Adapter: WasmNotSendSync { unsafe fn open( &self, features: wgt::Features, @@ -295,7 +295,7 @@ pub trait Adapter: WasmNotSend + WasmNotSync { unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp; } -pub trait Device: WasmNotSend + WasmNotSync { +pub trait Device: WasmNotSendSync { /// Exit connection to this logical device. unsafe fn exit(self, queue: A::Queue); /// Creates a new buffer. @@ -391,7 +391,7 @@ pub trait Device: WasmNotSend + WasmNotSync { unsafe fn stop_capture(&self); } -pub trait Queue: WasmNotSend + WasmNotSync { +pub trait Queue: WasmNotSendSync { /// Submits the command buffers for execution on GPU. /// /// Valid usage: @@ -415,7 +415,7 @@ pub trait Queue: 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: WasmNotSend + WasmNotSync + fmt::Debug { +pub trait CommandEncoder: 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. diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 81a9c1eca..56a42b4ba 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -6663,6 +6663,8 @@ pub use send_sync::*; #[doc(hidden)] mod send_sync { + pub trait WasmNotSendSync: WasmNotSend + WasmNotSync {} + impl WasmNotSendSync for T {} #[cfg(any( not(target_arch = "wasm32"), all( diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 1a12a5601..c99190baf 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -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"; @@ -306,7 +306,7 @@ impl Context { fn handle_error( &self, sink_mutex: &Mutex, - cause: impl Error + WasmNotSend + WasmNotSync + 'static, + cause: impl Error + WasmNotSendSync + 'static, label_key: &'static str, label: Label, string: &'static str, @@ -340,7 +340,7 @@ impl Context { fn handle_error_nolabel( &self, sink_mutex: &Mutex, - cause: impl Error + WasmNotSend + WasmNotSync + 'static, + cause: impl Error + WasmNotSendSync + 'static, string: &'static str, ) { self.handle_error(sink_mutex, cause, "", None, string) @@ -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)); diff --git a/wgpu/src/context.rs b/wgpu/src/context.rs index 2ada20f4a..ee4b67abe 100644 --- a/wgpu/src/context.rs +++ b/wgpu/src/context.rs @@ -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::{ @@ -27,55 +27,55 @@ impl + From + 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 ContextData for T {} +pub trait ContextData: Debug + WasmNotSendSync + 'static {} +impl 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> @@ -1082,15 +1082,13 @@ impl ObjectId { ))] static_assertions::assert_impl_all!(ObjectId: Send, Sync); -pub(crate) fn downcast_ref( - data: &crate::Data, -) -> &T { +pub(crate) fn downcast_ref(data: &crate::Data) -> &T { strict_assert!(data.is::()); // Copied from std. unsafe { &*(data as *const dyn Any as *const T) } } -fn downcast_mut(data: &mut crate::Data) -> &mut T { +fn downcast_mut(data: &mut crate::Data) -> &mut T { strict_assert!(data.is::()); // Copied from std. unsafe { &mut *(data as *mut dyn Any as *mut T) } @@ -1228,7 +1226,7 @@ pub type DeviceLostCallback = Box; /// 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( @@ -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]; diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index b8e603c60..3e679321e 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -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, }; @@ -380,10 +380,6 @@ impl Drop for Sampler { pub type SurfaceConfiguration = wgt::SurfaceConfiguration>; static_assertions::assert_impl_all!(SurfaceConfiguration: Send, Sync); -trait WgpuSurfaceRequirement: WasmNotSend + WasmNotSync {} - -impl WgpuSurfaceRequirement for T {} - /// Handle to a presentable surface. /// /// A `Surface` represents a platform-specific surface (e.g. a window) onto which rendered images may @@ -394,7 +390,7 @@ impl WgpuSurfaceRequirement for T {} /// serves a similar role. pub struct Surface<'window> { context: Arc, - _surface: Option>, + _surface: Option>, id: ObjectId, data: Box, // Stores the latest `SurfaceConfiguration` that was set using `Surface::configure`. @@ -1966,7 +1962,7 @@ impl Instance { window: W, ) -> Result, 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)); @@ -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 AnyWasmNotSendSync for T { + impl AnyWasmNotSendSync for T { #[inline] fn upcast_any_ref(&self) -> &dyn Any { self