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
use std::path::Path;
use objc2::{Message, extern_protocol, msg_send, rc::Retained, runtime::ProtocolObject};
use objc2_foundation::{NSError, NSObjectProtocol, NSString, NSURL};
use crate::{
MTL4MeshRenderPipelineDescriptor, MTLComputePipelineDescriptor, MTLDevice, MTLFunctionDescriptor, MTLLibrary,
MTLRenderPipelineDescriptor, function_stitching::MTLStitchedLibraryDescriptor,
render_pipeline::MTLTileRenderPipelineDescriptor,
};
// Error domain symbol is declared in `binary_archive::types`.
extern_protocol!(
/// A container of pipeline state descriptors and their associated compiled code.
///
/// Availability: macOS 11.0+, iOS 14.0+
pub unsafe trait MTLBinaryArchive: NSObjectProtocol {
/// The device this resource was created against. This resource can only be used with this device.
#[unsafe(method(device))]
#[unsafe(method_family = none)]
fn device(&self) -> Retained<ProtocolObject<dyn MTLDevice>>;
/// Add the function(s) from a compute pipeline state to the archive.
///
/// If the function fails, `error` will be set to describe the failure. This can be (but is not required to be) an error from the `MTLBinaryArchiveDomain` domain.
///
/// Functions referenced multiple times are silently accepted.
#[unsafe(method(addComputePipelineFunctionsWithDescriptor:error:_))]
#[unsafe(method_family = none)]
fn add_compute_pipeline_functions(
&self,
descriptor: &MTLComputePipelineDescriptor,
) -> Result<(), Retained<NSError>>;
/// Add the function(s) from a render pipeline state to the archive.
///
/// If the function fails, `error` will be set to describe the failure. This can be (but is not required to be) an error from the `MTLBinaryArchiveDomain` domain.
///
/// Functions referenced multiple times are silently accepted.
#[unsafe(method(addRenderPipelineFunctionsWithDescriptor:error:_))]
#[unsafe(method_family = none)]
fn add_render_pipeline_functions(
&self,
descriptor: &MTLRenderPipelineDescriptor,
) -> Result<(), Retained<NSError>>;
/// Add the function(s) from a tile render pipeline state to the archive.
///
/// If the function fails, `error` will be set to describe the failure. This can be (but is not required to be) an error from the `MTLBinaryArchiveDomain` domain.
///
/// Functions referenced multiple times are silently accepted.
///
/// Availability: tvOS 14.5+
#[unsafe(method(addTileRenderPipelineFunctionsWithDescriptor:error:_))]
#[unsafe(method_family = none)]
fn add_tile_render_pipeline_functions(
&self,
descriptor: &MTLTileRenderPipelineDescriptor,
) -> Result<(), Retained<NSError>>;
/// Add the function(s) from a mesh render pipeline state to the archive.
///
/// If the function fails, `error` will be set to describe the failure. This can be (but is not required to be) an error from the `MTLBinaryArchiveDomain` domain.
///
/// Functions referenced multiple times are silently accepted.
///
/// Availability: macOS 15.0+, iOS 18.0+
#[unsafe(method(addMeshRenderPipelineFunctionsWithDescriptor:error:_))]
#[unsafe(method_family = none)]
fn add_mesh_render_pipeline_functions(
&self,
descriptor: &MTL4MeshRenderPipelineDescriptor,
) -> Result<(), Retained<NSError>>;
/// Add the function(s) from a stitched library to the archive.
///
/// If the function fails, `error` will be set to describe the failure. This can be (but is not required to be) an error from the `MTLBinaryArchiveDomain` domain.
///
/// Functions referenced multiple times are silently accepted.
///
/// Availability: macOS 15.0+, iOS 18.0+
#[unsafe(method(addLibraryWithDescriptor:error:_))]
#[unsafe(method_family = none)]
fn add_library_with_descriptor(
&self,
descriptor: &MTLStitchedLibraryDescriptor,
) -> Result<(), Retained<NSError>>;
/// Add a `visible` or `intersection` function to the archive.
///
/// If the function fails, `error` will be set to describe the failure. This can be (but is not required to be) an error from the `MTLBinaryArchiveDomain` domain. Other possible errors can be file access or I/O related.
///
/// Functions referenced multiple times are silently accepted.
///
/// Availability: macOS 12.0+, iOS 15.0+
#[unsafe(method(addFunctionWithDescriptor:library:error:_))]
#[unsafe(method_family = none)]
fn add_function_with_descriptor_library(
&self,
descriptor: &MTLFunctionDescriptor,
library: &ProtocolObject<dyn MTLLibrary>,
) -> Result<(), Retained<NSError>>;
}
);
#[allow(unused)]
pub trait MTLBinaryArchiveExt: MTLBinaryArchive + Message {
/// A string to help identify this object.
fn label(&self) -> Option<String>;
/// Setter for `label`.
fn set_label(
&self,
label: Option<&str>,
);
/// Write the contents of a `MTLBinaryArchive` to a file path.
fn serialize_to_path(
&self,
path: &Path,
) -> Result<(), Retained<NSError>>;
}
impl MTLBinaryArchiveExt for ProtocolObject<dyn MTLBinaryArchive> {
fn label(&self) -> Option<String> {
let label: Option<Retained<NSString>> = unsafe { msg_send![self, label] };
label.map(|s| s.to_string())
}
fn set_label(
&self,
label: Option<&str>,
) {
unsafe {
let _: () = msg_send![self, setLabel: label.map(NSString::from_str).as_deref()];
}
}
fn serialize_to_path(
&self,
path: &Path,
) -> Result<(), Retained<NSError>> {
let url = NSURL::from_file_path(path).expect("path must be a valid file URL path");
unsafe { msg_send![self, serializeToURL: &*url, error: _] }
}
}