1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use core::{ffi::c_void, ptr::NonNull};
use objc2::{Message, extern_protocol, msg_send, rc::Retained, runtime::ProtocolObject};
use objc2_foundation::{NSError, NSObjectProtocol, NSString};
use crate::{
MTLBuffer, MTLIOCommandBufferHandler, MTLIOFileHandle, MTLIOStatus, MTLOrigin, MTLSharedEvent, MTLSize, MTLTexture,
MetalError,
};
extern_protocol!(
/// Represents a list of IO commands for a queue to execute.
///
/// Availability: macOS 13.0+, iOS 16.0+
pub unsafe trait MTLIOCommandBuffer: NSObjectProtocol {
/// Encodes a command that loads from a handle and offset into a memory location.
///
/// The source range must lie within `source_handle`.
///
/// `pointer` must remain valid for writes of `size` bytes until this
/// command buffer completes. The pointed-to memory must not be accessed
/// in a way that conflicts with Metal's asynchronous write during that
/// interval.
#[unsafe(method(loadBytes:size:sourceHandle:sourceHandleOffset:))]
#[unsafe(method_family = none)]
fn load_bytes_size_source_handle_source_handle_offset(
&self,
pointer: NonNull<c_void>,
size: usize,
source_handle: &ProtocolObject<dyn MTLIOFileHandle>,
source_handle_offset: usize,
);
/// Encodes a command that loads from a handle and offset into a buffer and an offset.
///
/// The source and destination byte ranges must satisfy Metal's
/// validation requirements.
#[unsafe(method(loadBuffer:offset:size:sourceHandle:sourceHandleOffset:))]
#[unsafe(method_family = none)]
fn load_buffer_offset_size_source_handle_source_handle_offset(
&self,
buffer: &ProtocolObject<dyn MTLBuffer>,
offset: usize,
size: usize,
source_handle: &ProtocolObject<dyn MTLIOFileHandle>,
source_handle_offset: usize,
);
/// Encodes a command that loads a region from a handle into a texture.
///
/// The source byte range and destination texture region, slice, and
/// mip level must satisfy Metal's validation requirements.
#[unsafe(method(loadTexture:slice:level:size:sourceBytesPerRow:sourceBytesPerImage:destinationOrigin:sourceHandle:sourceHandleOffset:))]
#[unsafe(method_family = none)]
fn load_texture_slice_level_size_source_bytes_per_row_source_bytes_per_image_destination_origin_source_handle_source_handle_offset(
&self,
texture: &ProtocolObject<dyn MTLTexture>,
slice: usize,
level: usize,
size: MTLSize,
source_bytes_per_row: usize,
source_bytes_per_image: usize,
destination_origin: MTLOrigin,
source_handle: &ProtocolObject<dyn MTLIOFileHandle>,
source_handle_offset: usize,
);
/// Encodes a command that writes the status of this command buffer upon completion to a buffer at a given offset.
///
/// `offset` must identify sufficient writable storage in `buffer` for
/// the status value.
#[unsafe(method(copyStatusToBuffer:offset:))]
#[unsafe(method_family = none)]
fn copy_status_to_buffer_offset(
&self,
buffer: &ProtocolObject<dyn MTLBuffer>,
offset: usize,
);
/// Commit so it can be executed as soon as possible.
#[unsafe(method(commit))]
#[unsafe(method_family = none)]
fn commit(&self);
/// Synchronously wait for completion.
#[unsafe(method(waitUntilCompleted))]
#[unsafe(method_family = none)]
fn wait_until_completed(&self);
/// Request cancellation of an in-flight command buffer.
#[unsafe(method(tryCancel))]
#[unsafe(method_family = none)]
fn try_cancel(&self);
/// Add a barrier to order previously encoded commands before subsequent ones start.
#[unsafe(method(addBarrier))]
#[unsafe(method_family = none)]
fn add_barrier(&self);
/// Pop the latest named string off of the stack.
#[unsafe(method(popDebugGroup))]
#[unsafe(method_family = none)]
fn pop_debug_group(&self);
/// Completion status of the command buffer.
#[unsafe(method(status))]
#[unsafe(method_family = none)]
fn status(&self) -> MTLIOStatus;
/// Append this command buffer to the end of its command queue.
#[unsafe(method(enqueue))]
#[unsafe(method_family = none)]
fn enqueue(&self);
/// Pauses execution until the specified shared event reaches a given value.
#[unsafe(method(waitForEvent:value:))]
#[unsafe(method_family = none)]
fn wait_for_event_value(
&self,
event: &ProtocolObject<dyn MTLSharedEvent>,
value: u64,
);
/// Signals a shared event with a given value.
#[unsafe(method(signalEvent:value:))]
#[unsafe(method_family = none)]
fn signal_event_value(
&self,
event: &ProtocolObject<dyn MTLSharedEvent>,
value: u64,
);
}
);
#[allow(unused)]
pub trait MTLIOCommandBufferExt: MTLIOCommandBuffer + Message {
/// The execution error, if the command buffer failed.
fn error(&self) -> Option<MetalError>;
/// Push a new named string onto a stack of string labels.
fn push_debug_group(
&self,
name: &str,
);
/// Optional label.
fn label(&self) -> Option<String>;
/// Setter for [`label`][Self::label].
fn set_label(
&self,
label: Option<&str>,
);
/// Add a block to be called when this command buffer has completed execution.
///
/// Availability: macOS 13.0+, iOS 16.0+
fn add_completed_handler(
&self,
handler: &MTLIOCommandBufferHandler,
);
}
impl MTLIOCommandBufferExt for ProtocolObject<dyn MTLIOCommandBuffer> {
fn error(&self) -> Option<MetalError> {
let error: Option<Retained<NSError>> = unsafe { msg_send![self, error] };
error.map(MetalError::from_nserror)
}
fn push_debug_group(
&self,
name: &str,
) {
unsafe {
let _: () = msg_send![self, pushDebugGroup: &*NSString::from_str(name)];
}
}
fn label(&self) -> Option<String> {
let s: Option<Retained<NSString>> = unsafe { msg_send![self, label] };
s.map(|v| v.to_string())
}
fn set_label(
&self,
label: Option<&str>,
) {
unsafe {
let _: () = msg_send![self, setLabel: label.map(NSString::from_str).as_deref()];
}
}
fn add_completed_handler(
&self,
handler: &MTLIOCommandBufferHandler,
) {
unsafe {
let _: () = msg_send![self, addCompletedHandler: handler.as_block()];
}
}
}