Skip to content

Commit

Permalink
Address review
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jun 22, 2023
1 parent 97b4662 commit 0488b82
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 116 deletions.
63 changes: 21 additions & 42 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ use std::{

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

pub const MAX_ANISOTROPY: u8 = 16;
pub const MAX_BIND_GROUPS: usize = 8;
Expand Down Expand Up @@ -161,25 +162,25 @@ pub trait Api: Clone + Sized {

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

type Buffer: fmt::Debug + MaybeSend + MaybeSync + 'static;
type Texture: fmt::Debug + MaybeSend + MaybeSync + 'static;
type SurfaceTexture: fmt::Debug + MaybeSend + MaybeSync + Borrow<Self::Texture>;
type TextureView: fmt::Debug + MaybeSend + MaybeSync;
type Sampler: fmt::Debug + MaybeSend + MaybeSync;
type QuerySet: fmt::Debug + MaybeSend + MaybeSync;
type Fence: fmt::Debug + MaybeSend + MaybeSync;
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 BindGroupLayout: MaybeSend + MaybeSync;
type BindGroup: fmt::Debug + MaybeSend + MaybeSync;
type PipelineLayout: MaybeSend + MaybeSync;
type ShaderModule: fmt::Debug + MaybeSend + MaybeSync;
type RenderPipeline: MaybeSend + MaybeSync;
type ComputePipeline: MaybeSend + MaybeSync;
type BindGroupLayout: 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;
}

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

pub trait Surface<A: Api>: MaybeSend + MaybeSync {
pub trait Surface<A: Api>: WasmNotSend + WasmNotSync {
unsafe fn configure(
&mut self,
device: &A::Device,
Expand All @@ -216,7 +217,7 @@ pub trait Surface<A: Api>: MaybeSend + MaybeSync {
unsafe fn discard_texture(&mut self, texture: A::SurfaceTexture);
}

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

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

pub trait Queue<A: Api>: MaybeSend + MaybeSync {
pub trait Queue<A: Api>: WasmNotSend + WasmNotSync {
/// Submits the command buffers for execution on GPU.
///
/// Valid usage:
Expand All @@ -360,7 +361,7 @@ pub trait Queue<A: Api>: MaybeSend + MaybeSync {
/// 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>: MaybeSend + MaybeSync + fmt::Debug {
pub trait CommandEncoder<A: Api>: WasmNotSend + WasmNotSync + 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 Expand Up @@ -1309,28 +1310,6 @@ impl ValidationCanary {
}
}

use send_sync::*;

mod send_sync {
#[cfg(not(target_arch = "wasm32"))]
pub trait MaybeSend: Send {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Send> MaybeSend for T {}
#[cfg(target_arch = "wasm32")]
pub trait MaybeSend {}
#[cfg(target_arch = "wasm32")]
impl<T> MaybeSend for T {}

#[cfg(not(target_arch = "wasm32"))]
pub trait MaybeSync: Sync {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Sync> MaybeSync for T {}
#[cfg(target_arch = "wasm32")]
pub trait MaybeSync {}
#[cfg(target_arch = "wasm32")]
impl<T> MaybeSync for T {}
}

#[test]
fn test_default_limits() {
let limits = wgt::Limits::default();
Expand Down
23 changes: 23 additions & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6312,3 +6312,26 @@ impl Default for InstanceDescriptor {
}
}
}

pub use send_sync::*;

#[doc(hidden)]
mod send_sync {
#[cfg(not(target_arch = "wasm32"))]
pub trait WasmNotSend: Send {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Send> WasmNotSend for T {}
#[cfg(target_arch = "wasm32")]
pub trait WasmNotSend {}
#[cfg(target_arch = "wasm32")]
impl<T> WasmNotSend for T {}

#[cfg(not(target_arch = "wasm32"))]
pub trait WasmNotSync: Sync {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Sync> WasmNotSync for T {}
#[cfg(target_arch = "wasm32")]
pub trait WasmNotSync {}
#[cfg(target_arch = "wasm32")]
impl<T> WasmNotSync for T {}
}
11 changes: 6 additions & 5 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{
context::{ObjectId, Unused},
AdapterInfo, BindGroupDescriptor, BindGroupLayoutDescriptor, BindingResource, BufferBinding,
CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor,
DownlevelCapabilities, Features, Label, Limits, LoadOp, MapMode, MaybeSend, MaybeSync,
Operations, PipelineLayoutDescriptor, RenderBundleEncoderDescriptor, RenderPipelineDescriptor,
DownlevelCapabilities, Features, Label, Limits, LoadOp, MapMode, Operations,
PipelineLayoutDescriptor, RenderBundleEncoderDescriptor, RenderPipelineDescriptor,
SamplerDescriptor, ShaderModuleDescriptor, ShaderModuleDescriptorSpirV, ShaderSource,
SurfaceStatus, TextureDescriptor, TextureViewDescriptor, UncapturedErrorHandler,
};
Expand All @@ -23,6 +23,7 @@ use std::{
};
use wgc::command::{bundle_ffi::*, compute_ffi::*, render_ffi::*};
use wgc::id::TypedId;
use wgt::{WasmNotSend, WasmNotSync};

const LABEL: &str = "label";

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

use crate::{
AnySendSync, BindGroupDescriptor, BindGroupLayoutDescriptor, Buffer, BufferAsyncError,
AnyWasmNotSendSync, BindGroupDescriptor, BindGroupLayoutDescriptor, Buffer, BufferAsyncError,
BufferDescriptor, CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor,
DeviceDescriptor, Error, ErrorFilter, ImageCopyBuffer, ImageCopyTexture, Maintain, MapMode,
MaybeSend, MaybeSync, PipelineLayoutDescriptor, QuerySetDescriptor, RenderBundleDescriptor,
PipelineLayoutDescriptor, QuerySetDescriptor, RenderBundleDescriptor,
RenderBundleEncoderDescriptor, RenderPassDescriptor, RenderPipelineDescriptor,
RequestAdapterOptions, RequestDeviceError, SamplerDescriptor, ShaderModuleDescriptor,
ShaderModuleDescriptorSpirV, Texture, TextureDescriptor, TextureViewDescriptor,
Expand All @@ -27,59 +27,59 @@ 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 + MaybeSend + MaybeSync + 'static {}
impl<T: Debug + MaybeSend + MaybeSync + 'static> ContextData for T {}
pub trait ContextData: Debug + WasmNotSend + WasmNotSync + 'static {}
impl<T: Debug + WasmNotSend + WasmNotSync + 'static> ContextData for T {}

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

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

type RequestAdapterFuture: Future<Output = Option<(Self::AdapterId, Self::AdapterData)>>
+ MaybeSend
+ WasmNotSend
+ 'static;
type RequestDeviceFuture: Future<
Output = Result<
Expand All @@ -91,9 +91,9 @@ pub trait Context: Debug + MaybeSend + MaybeSync + Sized {
),
RequestDeviceError,
>,
> + MaybeSend
> + WasmNotSend
+ 'static;
type PopErrorScopeFuture: Future<Output = Option<Error>> + MaybeSend + 'static;
type PopErrorScopeFuture: Future<Output = Option<Error>> + WasmNotSend + 'static;

fn init(instance_desc: wgt::InstanceDescriptor) -> Self;
fn instance_create_surface(
Expand Down Expand Up @@ -1042,13 +1042,15 @@ impl ObjectId {
#[cfg(not(target_arch = "wasm32"))]
static_assertions::assert_impl_all!(ObjectId: Send, Sync);

pub(crate) fn downcast_ref<T: Debug + MaybeSend + MaybeSync + 'static>(data: &crate::Data) -> &T {
pub(crate) fn downcast_ref<T: Debug + WasmNotSend + WasmNotSync + '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 + MaybeSend + MaybeSync + 'static>(data: &mut crate::Data) -> &mut T {
fn downcast_mut<T: Debug + WasmNotSend + WasmNotSync + '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 @@ -1110,7 +1112,7 @@ pub type SubmittedWorkDoneCallback = Box<dyn FnOnce() + Send + 'static>;
pub type SubmittedWorkDoneCallback = Box<dyn FnOnce() + 'static>;

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

fn instance_create_surface(
Expand Down Expand Up @@ -1182,10 +1184,10 @@ pub(crate) trait DynContext: Debug + MaybeSend + MaybeSync {
Option<ObjectId>,
Option<Box<crate::Data>>,
SurfaceStatus,
Box<dyn AnySendSync>,
Box<dyn AnyWasmNotSendSync>,
);
fn surface_present(&self, texture: &ObjectId, detail: &dyn AnySendSync);
fn surface_texture_discard(&self, texture: &ObjectId, detail: &dyn AnySendSync);
fn surface_present(&self, texture: &ObjectId, detail: &dyn AnyWasmNotSendSync);
fn surface_texture_discard(&self, texture: &ObjectId, detail: &dyn AnyWasmNotSendSync);

fn device_features(&self, device: &ObjectId, device_data: &crate::Data) -> Features;
fn device_limits(&self, device: &ObjectId, device_data: &crate::Data) -> Limits;
Expand Down Expand Up @@ -2094,13 +2096,13 @@ where
Option<ObjectId>,
Option<Box<crate::Data>>,
SurfaceStatus,
Box<dyn AnySendSync>,
Box<dyn AnyWasmNotSendSync>,
) {
let surface = <T::SurfaceId>::from(*surface);
let surface_data = downcast_ref(surface_data);
let (texture, texture_data, status, detail) =
Context::surface_get_current_texture(self, &surface, surface_data);
let detail = Box::new(detail) as Box<dyn AnySendSync>;
let detail = Box::new(detail) as Box<dyn AnyWasmNotSendSync>;
(
texture.map(Into::into),
texture_data.map(|b| Box::new(b) as _),
Expand All @@ -2109,12 +2111,12 @@ where
)
}

fn surface_present(&self, texture: &ObjectId, detail: &dyn AnySendSync) {
fn surface_present(&self, texture: &ObjectId, detail: &dyn AnyWasmNotSendSync) {
let texture = <T::TextureId>::from(*texture);
Context::surface_present(self, &texture, detail.downcast_ref().unwrap())
}

fn surface_texture_discard(&self, texture: &ObjectId, detail: &dyn AnySendSync) {
fn surface_texture_discard(&self, texture: &ObjectId, detail: &dyn AnyWasmNotSendSync) {
let texture = <T::TextureId>::from(*texture);
Context::surface_texture_discard(self, &texture, detail.downcast_ref().unwrap())
}
Expand Down Expand Up @@ -3891,7 +3893,7 @@ where
}
}

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

fn slice_mut(&mut self) -> &mut [u8];
Expand Down
Loading

0 comments on commit 0488b82

Please sign in to comment.