mtl_gpu/indirect/
buffer.rs1use std::ffi::c_void;
4use std::ptr::NonNull;
5
6use mtl_foundation::{Referencing, UInteger};
7use mtl_sys::{msg_send_0, msg_send_1, sel};
8
9use crate::types::ResourceID;
10
11use super::{IndirectComputeCommand, IndirectRenderCommand};
12
13#[repr(transparent)]
20pub struct IndirectCommandBuffer(NonNull<c_void>);
21
22impl IndirectCommandBuffer {
23 #[inline]
29 pub unsafe fn from_raw(ptr: *mut c_void) -> Option<Self> {
30 NonNull::new(ptr).map(Self)
31 }
32
33 #[inline]
35 pub fn as_raw(&self) -> *mut c_void {
36 self.0.as_ptr()
37 }
38
39 #[inline]
43 pub fn size(&self) -> UInteger {
44 unsafe { msg_send_0(self.as_ptr(), sel!(size)) }
45 }
46
47 #[inline]
51 pub fn gpu_resource_id(&self) -> ResourceID {
52 unsafe { msg_send_0(self.as_ptr(), sel!(gpuResourceID)) }
53 }
54
55 pub fn reset(&self, location: UInteger, length: UInteger) {
59 unsafe {
60 let range = mtl_foundation::Range::new(location, length);
61 msg_send_1::<(), mtl_foundation::Range>(self.as_ptr(), sel!(resetWithRange:), range);
62 }
63 }
64
65 pub fn indirect_render_command(&self, index: UInteger) -> Option<IndirectRenderCommand> {
69 unsafe {
70 let ptr: *mut c_void =
71 msg_send_1(self.as_ptr(), sel!(indirectRenderCommandAtIndex:), index);
72 IndirectRenderCommand::from_raw(ptr)
73 }
74 }
75
76 pub fn indirect_compute_command(&self, index: UInteger) -> Option<IndirectComputeCommand> {
80 unsafe {
81 let ptr: *mut c_void =
82 msg_send_1(self.as_ptr(), sel!(indirectComputeCommandAtIndex:), index);
83 IndirectComputeCommand::from_raw(ptr)
84 }
85 }
86}
87
88impl Clone for IndirectCommandBuffer {
89 fn clone(&self) -> Self {
90 unsafe {
91 msg_send_0::<*mut c_void>(self.as_ptr(), sel!(retain));
92 }
93 Self(self.0)
94 }
95}
96
97impl Drop for IndirectCommandBuffer {
98 fn drop(&mut self) {
99 unsafe {
100 msg_send_0::<()>(self.as_ptr(), sel!(release));
101 }
102 }
103}
104
105impl Referencing for IndirectCommandBuffer {
106 #[inline]
107 fn as_ptr(&self) -> *const c_void {
108 self.0.as_ptr()
109 }
110}
111
112unsafe impl Send for IndirectCommandBuffer {}
113unsafe impl Sync for IndirectCommandBuffer {}
114
115impl std::fmt::Debug for IndirectCommandBuffer {
116 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117 f.debug_struct("IndirectCommandBuffer")
118 .field("size", &self.size())
119 .finish()
120 }
121}