Skip to content

Commit

Permalink
Use extern_protocol! in encoder types
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 2, 2022
1 parent 9e35be1 commit 0771ad9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 69 deletions.
20 changes: 10 additions & 10 deletions src/commandbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,33 @@ impl CommandBufferRef {
unsafe { msg_send![self, addCompletedHandler: block] }
}

pub fn new_blit_command_encoder(&self) -> &BlitCommandEncoderRef {
unsafe { msg_send![self, blitCommandEncoder] }
pub fn new_blit_command_encoder(&self) -> Id<BlitCommandEncoder, Shared> {
unsafe { msg_send_id![self, blitCommandEncoder] }
}

pub fn new_compute_command_encoder(&self) -> &ComputeCommandEncoderRef {
unsafe { msg_send![self, computeCommandEncoder] }
pub fn new_compute_command_encoder(&self) -> Id<ComputeCommandEncoder, Shared> {
unsafe { msg_send_id![self, computeCommandEncoder] }
}

pub fn new_render_command_encoder(
&self,
descriptor: &RenderPassDescriptorRef,
) -> &RenderCommandEncoderRef {
unsafe { msg_send![self, renderCommandEncoderWithDescriptor: descriptor] }
) -> Id<RenderCommandEncoder, Shared> {
unsafe { msg_send_id![self, renderCommandEncoderWithDescriptor: descriptor] }
}

pub fn new_parallel_render_command_encoder(
&self,
descriptor: &RenderPassDescriptorRef,
) -> &ParallelRenderCommandEncoderRef {
unsafe { msg_send![self, parallelRenderCommandEncoderWithDescriptor: descriptor] }
) -> Id<ParallelRenderCommandEncoder, Shared> {
unsafe { msg_send_id![self, parallelRenderCommandEncoderWithDescriptor: descriptor] }
}

pub fn compute_command_encoder_with_dispatch_type(
&self,
ty: MTLDispatchType,
) -> &ComputeCommandEncoderRef {
unsafe { msg_send![self, computeCommandEncoderWithDispatchType: ty] }
) -> Id<ComputeCommandEncoder, Shared> {
unsafe { msg_send_id![self, computeCommandEncoderWithDispatchType: ty] }
}

pub fn encode_signal_event(&self, event: &EventRef, new_value: u64) {
Expand Down
4 changes: 2 additions & 2 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1977,9 +1977,9 @@ impl DeviceRef {
unsafe { msg_send![self, supportsPullModelInterpolation] }
}

pub fn new_argument_encoder(&self, arguments: &[&ArgumentDescriptorRef]) -> ArgumentEncoder {
pub fn new_argument_encoder(&self, arguments: &[&ArgumentDescriptorRef]) -> Id<ArgumentEncoder, Shared> {
let arguments: &ArrayRef<ArgumentDescriptor> = Array::from_slice(arguments);
unsafe { msg_send![self, newArgumentEncoderWithArguments: arguments] }
unsafe { msg_send_id![self, newArgumentEncoderWithArguments: arguments] }
}

pub fn new_heap(&self, descriptor: &HeapDescriptorRef) -> Heap {
Expand Down
118 changes: 64 additions & 54 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use super::*;
use std::ops::Range;

use objc2::encode::{Encode, Encoding};
use objc2::foundation::{NSInteger, NSRange, NSString, NSUInteger};
use objc2::foundation::{NSObject, NSInteger, NSRange, NSString, NSUInteger};
use objc2::rc::{Id, Shared};
use objc2::{extern_protocol, ProtocolType};

#[repr(u64)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -198,15 +199,17 @@ unsafe impl RefEncode for VertexAmplificationViewMapping {
const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

pub enum MTLCommandEncoder {}

foreign_obj_type! {
type CType = MTLCommandEncoder;
extern_protocol!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct CommandEncoder;
pub struct CommandEncoderRef;
}

impl CommandEncoderRef {
unsafe impl ProtocolType for CommandEncoder {
type Super = NSObject;
const NAME: &'static str = "MTLCommandEncoder";
}
);

impl CommandEncoder {
pub fn label(&self) -> Id<NSString, Shared> {
unsafe { msg_send_id![self, label] }
}
Expand Down Expand Up @@ -235,31 +238,35 @@ impl CommandEncoderRef {
}
}

pub enum MTLParallelRenderCommandEncoder {}

foreign_obj_type! {
type CType = MTLParallelRenderCommandEncoder;
extern_protocol!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ParallelRenderCommandEncoder;
pub struct ParallelRenderCommandEncoderRef;
type ParentType = CommandEncoderRef;
}

impl ParallelRenderCommandEncoderRef {
pub fn render_command_encoder(&self) -> &RenderCommandEncoderRef {
unsafe { msg_send![self, renderCommandEncoder] }
unsafe impl ProtocolType for ParallelRenderCommandEncoder {
#[inherits(NSObject)]
type Super = CommandEncoder;
const NAME: &'static str = "MTLParallelRenderCommandEncoder";
}
}
);

pub enum MTLRenderCommandEncoder {}
impl ParallelRenderCommandEncoder {
pub fn render_command_encoder(&self) -> Id<RenderCommandEncoder, Shared> {
unsafe { msg_send_id![self, renderCommandEncoder] }
}
}

foreign_obj_type! {
type CType = MTLRenderCommandEncoder;
extern_protocol!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct RenderCommandEncoder;
pub struct RenderCommandEncoderRef;
type ParentType = CommandEncoderRef;
}

impl RenderCommandEncoderRef {
unsafe impl ProtocolType for RenderCommandEncoder {
#[inherits(NSObject)]
type Super = CommandEncoder;
const NAME: &'static str = "MTLRenderCommandEncoder";
}
);

impl RenderCommandEncoder {
pub fn set_render_pipeline_state(&self, pipeline_state: &RenderPipelineStateRef) {
unsafe { msg_send![self, setRenderPipelineState: pipeline_state] }
}
Expand Down Expand Up @@ -879,16 +886,18 @@ impl RenderCommandEncoderRef {
}
}

pub enum MTLBlitCommandEncoder {}

foreign_obj_type! {
type CType = MTLBlitCommandEncoder;
extern_protocol!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct BlitCommandEncoder;
pub struct BlitCommandEncoderRef;
type ParentType = CommandEncoderRef;
}

impl BlitCommandEncoderRef {
unsafe impl ProtocolType for BlitCommandEncoder {
#[inherits(NSObject)]
type Super = CommandEncoder;
const NAME: &'static str = "MTLBlitCommandEncoder";
}
);

impl BlitCommandEncoder {
pub fn synchronize_resource(&self, resource: &Resource) {
unsafe { msg_send![self, synchronizeResource: resource] }
}
Expand Down Expand Up @@ -1059,16 +1068,18 @@ impl BlitCommandEncoderRef {
}
}

pub enum MTLComputeCommandEncoder {}

foreign_obj_type! {
type CType = MTLComputeCommandEncoder;
extern_protocol!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ComputeCommandEncoder;
pub struct ComputeCommandEncoderRef;
type ParentType = CommandEncoderRef;
}

impl ComputeCommandEncoderRef {
unsafe impl ProtocolType for ComputeCommandEncoder {
#[inherits(NSObject)]
type Super = CommandEncoder;
const NAME: &'static str = "MTLComputeCommandEncoder";
}
);

impl ComputeCommandEncoder {
pub fn set_compute_pipeline_state(&self, state: &ComputePipelineStateRef) {
unsafe { msg_send![self, setComputePipelineState: state] }
}
Expand Down Expand Up @@ -1298,15 +1309,17 @@ impl ComputeCommandEncoderRef {
}
}

pub enum MTLArgumentEncoder {}

foreign_obj_type! {
type CType = MTLArgumentEncoder;
extern_protocol!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ArgumentEncoder;
pub struct ArgumentEncoderRef;
}

impl ArgumentEncoderRef {
unsafe impl ProtocolType for ArgumentEncoder {
type Super = NSObject;
const NAME: &'static str = "MTLArgumentEncoder";
}
);

impl ArgumentEncoder {
pub fn encoded_length(&self) -> NSUInteger {
unsafe { msg_send![self, encodedLength] }
}
Expand Down Expand Up @@ -1438,10 +1451,7 @@ impl ArgumentEncoderRef {
unsafe { msg_send![self, constantDataAtIndex: at_index] }
}

pub fn new_argument_encoder_for_buffer(&self, index: NSUInteger) -> ArgumentEncoder {
unsafe {
let ptr = msg_send![self, newArgumentEncoderForBufferAtIndex: index];
ArgumentEncoder::from_ptr(ptr)
}
pub fn new_argument_encoder_for_buffer(&self, index: NSUInteger) -> Id<ArgumentEncoder, Shared> {
unsafe { msg_send_id![self, newArgumentEncoderForBufferAtIndex: index] }
}
}
5 changes: 2 additions & 3 deletions src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,9 @@ impl FunctionRef {
unsafe { msg_send![self, stageInputAttributes] }
}

pub fn new_argument_encoder(&self, buffer_index: NSUInteger) -> ArgumentEncoder {
pub fn new_argument_encoder(&self, buffer_index: NSUInteger) -> Id<ArgumentEncoder, Shared> {
unsafe {
let ptr = msg_send![self, newArgumentEncoderWithBufferIndex: buffer_index];
ArgumentEncoder::from_ptr(ptr)
msg_send_id![self, newArgumentEncoderWithBufferIndex: buffer_index]
}
}

Expand Down

0 comments on commit 0771ad9

Please sign in to comment.