diff --git a/src/commandbuffer.rs b/src/commandbuffer.rs index a59c6995..a0551042 100644 --- a/src/commandbuffer.rs +++ b/src/commandbuffer.rs @@ -141,7 +141,7 @@ impl CommandBufferRef { unsafe { msg_send_id![self, computeCommandEncoderWithDispatchType: ty] } } - pub fn encode_signal_event(&self, event: &EventRef, new_value: u64) { + pub fn encode_signal_event(&self, event: &Event, new_value: u64) { unsafe { msg_send![self, encodeSignalEvent: event @@ -150,7 +150,7 @@ impl CommandBufferRef { } } - pub fn encode_wait_for_event(&self, event: &EventRef, value: u64) { + pub fn encode_wait_for_event(&self, event: &Event, value: u64) { unsafe { msg_send![self, encodeWaitForEvent: event diff --git a/src/device.rs b/src/device.rs index 38e191fd..e8a52965 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1653,8 +1653,8 @@ impl DeviceRef { unsafe { msg_send![self, isDepth24Stencil8PixelFormatSupported] } } - pub fn new_fence(&self) -> Fence { - unsafe { msg_send![self, newFence] } + pub fn new_fence(&self) -> Id { + unsafe { msg_send_id![self, newFence] } } pub fn new_command_queue(&self) -> CommandQueue { @@ -1986,12 +1986,12 @@ impl DeviceRef { unsafe { msg_send![self, newHeapWithDescriptor: descriptor] } } - pub fn new_event(&self) -> Event { - unsafe { msg_send![self, newEvent] } + pub fn new_event(&self) -> Id { + unsafe { msg_send_id![self, newEvent] } } - pub fn new_shared_event(&self) -> SharedEvent { - unsafe { msg_send![self, newSharedEvent] } + pub fn new_shared_event(&self) -> Id { + unsafe { msg_send_id![self, newSharedEvent] } } pub fn heap_buffer_size_and_align( diff --git a/src/encoder.rs b/src/encoder.rs index e2ba6559..f7e3edc9 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -867,7 +867,7 @@ impl RenderCommandEncoder { unsafe { msg_send![self, executeCommandsInBuffer:buffer withRange:with_range] } } - pub fn update_fence(&self, fence: &FenceRef, after_stages: MTLRenderStages) { + pub fn update_fence(&self, fence: &Fence, after_stages: MTLRenderStages) { unsafe { msg_send![self, updateFence: fence @@ -876,7 +876,7 @@ impl RenderCommandEncoder { } } - pub fn wait_for_fence(&self, fence: &FenceRef, before_stages: MTLRenderStages) { + pub fn wait_for_fence(&self, fence: &Fence, before_stages: MTLRenderStages) { unsafe { msg_send![self, waitForFence: fence @@ -1059,11 +1059,11 @@ impl BlitCommandEncoder { } } - pub fn update_fence(&self, fence: &FenceRef) { + pub fn update_fence(&self, fence: &Fence) { unsafe { msg_send![self, updateFence: fence] } } - pub fn wait_for_fence(&self, fence: &FenceRef) { + pub fn wait_for_fence(&self, fence: &Fence) { unsafe { msg_send![self, waitForFence: fence] } } } @@ -1300,11 +1300,11 @@ impl ComputeCommandEncoder { } } - pub fn update_fence(&self, fence: &FenceRef) { + pub fn update_fence(&self, fence: &Fence) { unsafe { msg_send![self, updateFence: fence] } } - pub fn wait_for_fence(&self, fence: &FenceRef) { + pub fn wait_for_fence(&self, fence: &Fence) { unsafe { msg_send![self, waitForFence: fence] } } } diff --git a/src/sync.rs b/src/sync.rs index 7a8dd9b5..3a3c4a39 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -8,39 +8,44 @@ use super::*; use block2::{Block, RcBlock}; use objc2::encode::{Encode, EncodeArguments, Encoding}; -use objc2::foundation::{NSString, NSUInteger}; +use objc2::foundation::{NSObject, NSString, NSUInteger}; use objc2::rc::{Id, Shared}; +use objc2::{extern_protocol, extern_class, ProtocolType, ClassType}; use std::mem; #[cfg(feature = "dispatch_queue")] use dispatch; -type MTLSharedEventNotificationBlock<'a> = RcBlock<(&'a SharedEventRef, u64), ()>; +type MTLSharedEventNotificationBlock<'a> = RcBlock<(&'a SharedEvent, u64), ()>; -pub enum MTLEvent {} - -foreign_obj_type! { - type CType = MTLEvent; +extern_protocol!( + #[derive(Debug, PartialEq, Eq, Hash)] pub struct Event; - pub struct EventRef; -} -impl EventRef { + unsafe impl ProtocolType for Event { + type Super = NSObject; + const NAME: &'static str = "MTLEvent"; + } +); + +impl Event { pub fn device(&self) -> &DeviceRef { unsafe { msg_send![self, device] } } } -pub enum MTLSharedEvent {} - -foreign_obj_type! { - type CType = MTLSharedEvent; +extern_protocol!( + #[derive(Debug, PartialEq, Eq, Hash)] pub struct SharedEvent; - pub struct SharedEventRef; - type ParentType = EventRef; -} -impl SharedEventRef { + unsafe impl ProtocolType for SharedEvent { + #[inherits(NSObject)] + type Super = Event; + const NAME: &'static str = "MTLSharedEvent"; + } +); + +impl SharedEvent { pub fn signaled_value(&self) -> u64 { unsafe { msg_send![self, signaledValue] } } @@ -53,7 +58,7 @@ impl SharedEventRef { /// equals or exceeds a given value. pub fn notify( &self, - listener: &SharedEventListenerRef, + listener: &SharedEventListener, value: u64, block: MTLSharedEventNotificationBlock, ) { @@ -62,7 +67,7 @@ impl SharedEventRef { // Taken from https://github.com/servo/pathfinder/blob/e858c8dc1d8ff02a5b603e21e09a64d6b3e11327/metal/src/lib.rs#L2327 let block = mem::transmute::< MTLSharedEventNotificationBlock, - *mut BlockBase<(&SharedEventRef, u64), ()>, + *mut BlockBase<(&SharedEvent, u64), ()>, >(block); (*block).flags |= BLOCK_HAS_SIGNATURE | BLOCK_HAS_COPY_DISPOSE; (*block).extra = &BLOCK_EXTRA; @@ -70,11 +75,11 @@ impl SharedEventRef { mem::forget(block); } - extern "C" fn dtor(_: *mut BlockBase<(&SharedEventRef, u64), ()>) {} + extern "C" fn dtor(_: *mut BlockBase<(&SharedEvent, u64), ()>) {} const SIGNATURE: &[u8] = b"v16@?0Q8\0"; const SIGNATURE_PTR: *const i8 = &SIGNATURE[0] as *const u8 as *const i8; - static mut BLOCK_EXTRA: BlockExtra<(&SharedEventRef, u64), ()> = BlockExtra { + static mut BLOCK_EXTRA: BlockExtra<(&SharedEvent, u64), ()> = BlockExtra { unknown0: 0 as *mut i32, unknown1: 0 as *mut i32, unknown2: 0 as *mut i32, @@ -84,26 +89,24 @@ impl SharedEventRef { } } -pub enum MTLSharedEventListener {} - -foreign_obj_type! { - type CType = MTLSharedEventListener; +extern_class!( + #[derive(Debug, PartialEq, Eq, Hash)] pub struct SharedEventListener; - pub struct SharedEventListenerRef; -} + + unsafe impl ClassType for SharedEventListener { + type Super = NSObject; + const NAME: &'static str = "MTLSharedEventListener"; + } +); impl SharedEventListener { - pub unsafe fn from_queue_handle(queue: dispatch_queue_t) -> Self { - let listener: SharedEventListener = msg_send![class!(MTLSharedEventListener), alloc]; - let ptr: *mut Object = msg_send![listener.as_ref(), initWithDispatchQueue: queue]; - if ptr.is_null() { - panic!("[MTLSharedEventListener alloc] initWithDispatchQueue failed"); - } - listener + pub unsafe fn from_queue_handle(queue: dispatch_queue_t) -> Id { + let listener = msg_send_id![Self::class(), alloc]; + msg_send_id![listener, initWithDispatchQueue: queue] } #[cfg(feature = "dispatch")] - pub fn from_queue(queue: &dispatch::Queue) -> Self { + pub fn from_queue(queue: &dispatch::Queue) -> Id { unsafe { let raw_queue = std::mem::transmute::<&dispatch::Queue, *const dispatch_queue_t>(queue); Self::from_queue_handle(*raw_queue) @@ -111,15 +114,17 @@ impl SharedEventListener { } } -pub enum MTLFence {} - -foreign_obj_type! { - type CType = MTLFence; +extern_protocol!( + #[derive(Debug, PartialEq, Eq, Hash)] pub struct Fence; - pub struct FenceRef; -} -impl FenceRef { + unsafe impl ProtocolType for Fence { + type Super = NSObject; + const NAME: &'static str = "MTLFence"; + } +); + +impl Fence { pub fn device(&self) -> &DeviceRef { unsafe { msg_send![self, device] } }