Skip to content

Commit

Permalink
Use extern_protocol! in sync types
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 2, 2022
1 parent 0771ad9 commit 46f2ffa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/commandbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Fence, Shared> {
unsafe { msg_send_id![self, newFence] }
}

pub fn new_command_queue(&self) -> CommandQueue {
Expand Down Expand Up @@ -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<Event, Shared> {
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<SharedEvent, Shared> {
unsafe { msg_send_id![self, newSharedEvent] }
}

pub fn heap_buffer_size_and_align(
Expand Down
12 changes: 6 additions & 6 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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] }
}
}
Expand Down Expand Up @@ -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] }
}
}
Expand Down
89 changes: 47 additions & 42 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
}
Expand All @@ -53,7 +58,7 @@ impl SharedEventRef {
/// equals or exceeds a given value.
pub fn notify(
&self,
listener: &SharedEventListenerRef,
listener: &SharedEventListener,
value: u64,
block: MTLSharedEventNotificationBlock,
) {
Expand All @@ -62,19 +67,19 @@ 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;
let () = msg_send![self, notifyListener:listener atValue:value block:block];
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,
Expand All @@ -84,42 +89,42 @@ 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<Self, Shared> {
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<Self, Shared> {
unsafe {
let raw_queue = std::mem::transmute::<&dispatch::Queue, *const dispatch_queue_t>(queue);
Self::from_queue_handle(*raw_queue)
}
}
}

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] }
}
Expand Down

0 comments on commit 46f2ffa

Please sign in to comment.