mtl_gpu/pass/
compute_pass.rs1use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::Referencing;
7use mtl_sys::{msg_send_0, msg_send_1, sel};
8
9use crate::enums::DispatchType;
10
11use super::ComputePassSampleBufferAttachmentDescriptorArray;
12
13#[repr(transparent)]
17pub struct ComputePassDescriptor(NonNull<c_void>);
18
19impl ComputePassDescriptor {
20 #[inline]
26 pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
27 NonNull::new(ptr).map(Self)
28 }
29
30 #[inline]
32 pub fn as_raw(&self) -> *mut c_void {
33 self.0.as_ptr()
34 }
35
36 pub fn new() -> Option<Self> {
40 unsafe {
41 let class = mtl_sys::class!(MTLComputePassDescriptor);
42 let ptr: *mut c_void = msg_send_0(class.as_ptr(), sel!(computePassDescriptor));
43 if ptr.is_null() {
44 return None;
45 }
46 let _: *mut c_void = msg_send_0(ptr, sel!(retain));
47 Self::from_raw(ptr)
48 }
49 }
50
51 #[inline]
59 pub fn dispatch_type(&self) -> DispatchType {
60 unsafe { msg_send_0(self.as_ptr(), sel!(dispatchType)) }
61 }
62
63 #[inline]
67 pub fn set_dispatch_type(&self, dispatch_type: DispatchType) {
68 unsafe {
69 msg_send_1::<(), DispatchType>(self.as_ptr(), sel!(setDispatchType:), dispatch_type);
70 }
71 }
72
73 pub fn sample_buffer_attachments(
77 &self,
78 ) -> Option<ComputePassSampleBufferAttachmentDescriptorArray> {
79 unsafe {
80 let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(sampleBufferAttachments));
81 ComputePassSampleBufferAttachmentDescriptorArray::from_raw(ptr)
82 }
83 }
84}
85
86impl Default for ComputePassDescriptor {
87 fn default() -> Self {
88 Self::new().expect("failed to create ComputePassDescriptor")
89 }
90}
91
92impl Clone for ComputePassDescriptor {
93 fn clone(&self) -> Self {
94 unsafe {
95 msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
96 }
97 Self(self.0)
98 }
99}
100
101impl Drop for ComputePassDescriptor {
102 fn drop(&mut self) {
103 unsafe {
104 msg_send_0::<()>(self.as_ptr(), sel!(release));
105 }
106 }
107}
108
109impl Referencing for ComputePassDescriptor {
110 #[inline]
111 fn as_ptr(&self) -> *const c_void {
112 self.0.as_ptr()
113 }
114}
115
116unsafe impl Send for ComputePassDescriptor {}
117unsafe impl Sync for ComputePassDescriptor {}
118
119impl std::fmt::Debug for ComputePassDescriptor {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121 f.debug_struct("ComputePassDescriptor")
122 .field("dispatch_type", &self.dispatch_type())
123 .finish()
124 }
125}