Debian Codegraph @

third_party/rust/metal/src/library.rs

raw
1 // Copyright 2017 GFX developers
2 //
3 // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4 // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5 // http://opensource.org/licenses/MIT>, at your option. This file may not be
6 // copied, modified, or distributed except according to those terms.
7
8 use super::*;
9
10 use objc::runtime::{BOOL, NO, YES};
11
12 use std::ffi::{c_char, CStr};
13 use std::ptr;
14
15 /// Only available on (macos(10.12), ios(10.0)
16 ///
17 /// See <https://developer.apple.com/documentation/metal/mtlpatchtype/>
18 #[repr(u64)]
19 #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
20 pub enum MTLPatchType {
21 None = 0,
22 Triangle = 1,
23 Quad = 2,
24 }
25
26 /// See <https://developer.apple.com/documentation/metal/mtlvertexattribute/>
27 pub enum MTLVertexAttribute {}
28
29 foreign_obj_type! {
30 type CType = MTLVertexAttribute;
31 pub struct VertexAttribute;
32 }
33
34 impl VertexAttributeRef {
35 pub fn name(&self) -> &str {
36 unsafe {
37 let name = msg_send![self, name];
38 crate::nsstring_as_str(name)
39 }
40 }
41
42 pub fn attribute_index(&self) -> u64 {
43 unsafe { msg_send![self, attributeIndex] }
44 }
45
46 pub fn attribute_type(&self) -> MTLDataType {
47 unsafe { msg_send![self, attributeType] }
48 }
49
50 pub fn is_active(&self) -> bool {
51 unsafe { msg_send_bool![self, isActive] }
52 }
53
54 /// Only available on (macos(10.12), ios(10.0)
55 pub fn is_patch_data(&self) -> bool {
56 unsafe { msg_send_bool![self, isPatchData] }
57 }
58
59 /// Only available on (macos(10.12), ios(10.0)
60 pub fn is_patch_control_point_data(&self) -> bool {
61 unsafe { msg_send_bool![self, isPatchControlPointData] }
62 }
63 }
64
65 /// Only available on (macos(10.12), ios(10.0))
66 ///
67 /// See <https://developer.apple.com/documentation/metal/mtlattribute/>
68 pub enum MTLAttribute {}
69
70 foreign_obj_type! {
71 type CType = MTLAttribute;
72 pub struct Attribute;
73 }
74
75 impl AttributeRef {
76 pub fn name(&self) -> &str {
77 unsafe {
78 let name = msg_send![self, name];
79 crate::nsstring_as_str(name)
80 }
81 }
82
83 pub fn attribute_index(&self) -> u64 {
84 unsafe { msg_send![self, attributeIndex] }
85 }
86
87 pub fn attribute_type(&self) -> MTLDataType {
88 unsafe { msg_send![self, attributeType] }
89 }
90
91 pub fn is_active(&self) -> bool {
92 unsafe { msg_send_bool![self, isActive] }
93 }
94
95 /// Only available on (macos(10.12), ios(10.0))
96 pub fn is_patch_data(&self) -> bool {
97 unsafe { msg_send_bool![self, isPatchData] }
98 }
99
100 /// Only available on (macos(10.12), ios(10.0))
101 pub fn is_patch_control_point_data(&self) -> bool {
102 unsafe { msg_send_bool![self, isPatchControlPointData] }
103 }
104 }
105
106 /// See <https://developer.apple.com/documentation/metal/mtlfunctiontype/>
107 #[repr(u64)]
108 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
109 pub enum MTLFunctionType {
110 Vertex = 1,
111 Fragment = 2,
112 Kernel = 3,
113 /// Only available on (macos(11.0), ios(14.0))
114 Visible = 5,
115 /// Only available on (macos(11.0), ios(14.0))
116 Intersection = 6,
117 }
118
119 /// Only available on (macos(10.12), ios(10.0))
120 ///
121 /// See <https://developer.apple.com/documentation/metal/mtlfunctionconstant/>
122 pub enum MTLFunctionConstant {}
123
124 foreign_obj_type! {
125 type CType = MTLFunctionConstant;
126 pub struct FunctionConstant;
127 }
128
129 impl FunctionConstantRef {
130 pub fn name(&self) -> &str {
131 unsafe {
132 let name = msg_send![self, name];
133 crate::nsstring_as_str(name)
134 }
135 }
136
137 pub fn data_type(&self) -> MTLDataType {
138 unsafe { msg_send![self, type] }
139 }
140
141 pub fn index(&self) -> NSUInteger {
142 unsafe { msg_send![self, index] }
143 }
144
145 pub fn required(&self) -> bool {
146 unsafe { msg_send_bool![self, required] }
147 }
148 }
149
150 bitflags::bitflags! {
151 /// Only available on (macos(11.0), ios(14.0))
152 ///
153 /// See <https://developer.apple.com/documentation/metal/mtlfunctionoptions/>
154 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
155 pub struct MTLFunctionOptions: NSUInteger {
156 const None = 0;
157 const CompileToBinary = 1 << 0;
158 }
159 }
160
161 /// Only available on (macos(11.0), ios(14.0))
162 ///
163 /// See <https://developer.apple.com/documentation/metal/mtlfunctiondescriptor/>
164 pub enum MTLFunctionDescriptor {}
165
166 foreign_obj_type! {
167 type CType = MTLFunctionDescriptor;
168 pub struct FunctionDescriptor;
169 }
170
171 impl FunctionDescriptor {
172 pub fn new() -> Self {
173 unsafe {
174 let class = class!(MTLFunctionDescriptor);
175 msg_send![class, new]
176 }
177 }
178 }
179
180 impl FunctionDescriptorRef {
181 pub fn name(&self) -> &str {
182 unsafe {
183 let name = msg_send![self, name];
184 crate::nsstring_as_str(name)
185 }
186 }
187
188 pub fn set_name(&self, name: &str) {
189 unsafe {
190 let ns_name = crate::nsstring_from_str(name);
191 let () = msg_send![self, setName: ns_name];
192 }
193 }
194
195 pub fn specialized_name(&self) -> &str {
196 unsafe {
197 let name = msg_send![self, specializedName];
198 crate::nsstring_as_str(name)
199 }
200 }
201
202 pub fn set_specialized_name(&self, name: &str) {
203 unsafe {
204 let ns_name = crate::nsstring_from_str(name);
205 let () = msg_send![self, setSpecializedName: ns_name];
206 }
207 }
208
209 pub fn constant_values(&self) -> &FunctionConstantValuesRef {
210 unsafe { msg_send![self, constantValues] }
211 }
212
213 pub fn set_constant_values(&self, values: &FunctionConstantValuesRef) {
214 unsafe { msg_send![self, setConstantValues: values] }
215 }
216
217 pub fn options(&self) -> MTLFunctionOptions {
218 unsafe { msg_send![self, options] }
219 }
220
221 pub fn set_options(&self, options: MTLFunctionOptions) {
222 unsafe { msg_send![self, setOptions: options] }
223 }
224 }
225
226 /// Only available on (macos(11.0), ios(14.0))
227 ///
228 /// See <https://developer.apple.com/documentation/metal/mtlintersectionfunctiondescriptor/>
229 pub enum MTLIntersectionFunctionDescriptor {}
230
231 foreign_obj_type! {
232 type CType = MTLIntersectionFunctionDescriptor;
233 pub struct IntersectionFunctionDescriptor;
234 type ParentType = FunctionDescriptor;
235 }
236
237 /// Only available on (macos(11.0), ios(14.0))
238 ///
239 /// See <https://developer.apple.com/documentation/metal/mtlfunctionhandle/>
240 pub enum MTLFunctionHandle {}
241
242 foreign_obj_type! {
243 type CType = MTLFunctionHandle;
244 pub struct FunctionHandle;
245 }
246
247 impl FunctionHandleRef {
248 pub fn device(&self) -> &DeviceRef {
249 unsafe { msg_send![self, device] }
250 }
251
252 pub fn name(&self) -> &str {
253 unsafe {
254 let ns_name = msg_send![self, name];
255 crate::nsstring_as_str(ns_name)
256 }
257 }
258
259 pub fn function_type(&self) -> MTLFunctionType {
260 unsafe { msg_send![self, functionType] }
261 }
262 }
263
264 // TODO:
265 // MTLIntersectionFunctionSignature
266 // MTLIntersectionFunctionTableDescriptor
267 // MTLIntersectionFunctionTable
268
269 /// See <https://developer.apple.com/documentation/metal/mtlfunction/>
270 pub enum MTLFunction {}
271
272 foreign_obj_type! {
273 type CType = MTLFunction;
274 pub struct Function;
275 }
276
277 impl FunctionRef {
278 pub fn device(&self) -> &DeviceRef {
279 unsafe { msg_send![self, device] }
280 }
281
282 /// Only available on (macos(10.12), ios(10.0))
283 pub fn label(&self) -> &str {
284 unsafe {
285 let ns_label = msg_send![self, label];
286 crate::nsstring_as_str(ns_label)
287 }
288 }
289
290 /// Only available on (macos(10.12), ios(10.0))
291 pub fn set_label(&self, label: &str) {
292 unsafe {
293 let ns_label = crate::nsstring_from_str(label);
294 let () = msg_send![self, setLabel: ns_label];
295 }
296 }
297
298 pub fn name(&self) -> &str {
299 unsafe {
300 let name = msg_send![self, name];
301 crate::nsstring_as_str(name)
302 }
303 }
304
305 pub fn function_type(&self) -> MTLFunctionType {
306 unsafe { msg_send![self, functionType] }
307 }
308
309 /// Only available on (macos(10.12), ios(10.0))
310 pub fn patch_type(&self) -> MTLPatchType {
311 unsafe { msg_send![self, patchType] }
312 }
313
314 /// Only available on (macos(10.12), ios(10.0))
315 pub fn patch_control_point_count(&self) -> NSUInteger {
316 unsafe { msg_send![self, patchControlPointCount] }
317 }
318
319 /// Only available on (macos(10.12), ios(10.0))
320 pub fn vertex_attributes(&self) -> &Array<VertexAttribute> {
321 unsafe { msg_send![self, vertexAttributes] }
322 }
323
324 /// Only available on (macos(10.12), ios(10.0))
325 pub fn stage_input_attributes(&self) -> &Array<Attribute> {
326 unsafe { msg_send![self, stageInputAttributes] }
327 }
328
329 pub fn new_argument_encoder(&self, buffer_index: NSUInteger) -> ArgumentEncoder {
330 unsafe {
331 let ptr = msg_send![self, newArgumentEncoderWithBufferIndex: buffer_index];
332 ArgumentEncoder::from_ptr(ptr)
333 }
334 }
335
336 pub fn function_constants_dictionary(&self) -> *mut Object {
337 unsafe { msg_send![self, functionConstantsDictionary] }
338 }
339
340 /// Only available on (macos(11.0), ios(14.0))
341 pub fn options(&self) -> MTLFunctionOptions {
342 unsafe { msg_send![self, options] }
343 }
344 }
345
346 /// See <https://developer.apple.com/documentation/metal/mtllanguageversion/>
347 #[repr(u64)]
348 #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
349 pub enum MTLLanguageVersion {
350 V1_0 = 0x10000,
351 V1_1 = 0x10001,
352 V1_2 = 0x10002,
353 V2_0 = 0x20000,
354 V2_1 = 0x20001,
355 V2_2 = 0x20002,
356 /// available on macOS 11.0+, iOS 14.0+
357 V2_3 = 0x20003,
358 /// available on macOS 12.0+, iOS 15.0+
359 V2_4 = 0x20004,
360 /// available on macOS 13.0+, iOS 16.0+
361 V3_0 = 0x30000,
362 /// available on macOS 14.0+, iOS 17.0+
363 V3_1 = 0x30001,
364 }
365
366 /// See <https://developer.apple.com/documentation/metal/mtlfunctionconstantvalues/>
367 pub enum MTLFunctionConstantValues {}
368
369 foreign_obj_type! {
370 type CType = MTLFunctionConstantValues;
371 pub struct FunctionConstantValues;
372 }
373
374 impl FunctionConstantValues {
375 pub fn new() -> Self {
376 unsafe {
377 let class = class!(MTLFunctionConstantValues);
378 msg_send![class, new]
379 }
380 }
381 }
382
383 impl FunctionConstantValuesRef {
384 pub fn set_constant_value_at_index(
385 &self,
386 value: *const c_void,
387 ty: MTLDataType,
388 index: NSUInteger,
389 ) {
390 unsafe { msg_send![self, setConstantValue:value type:ty atIndex:index] }
391 }
392
393 pub fn set_constant_values_with_range(
394 &self,
395 values: *const c_void,
396 ty: MTLDataType,
397 range: NSRange,
398 ) {
399 unsafe { msg_send![self, setConstantValues:values type:ty withRange:range] }
400 }
401
402 pub fn set_constant_value_with_name(&self, value: *const c_void, ty: MTLDataType, name: &str) {
403 unsafe {
404 let ns_name = crate::nsstring_from_str(name);
405 msg_send![self, setConstantValue:value type:ty withName:ns_name]
406 }
407 }
408 }
409
410 /// Only available on (macos(11.0), ios(14.0))
411 ///
412 /// See <https://developer.apple.com/documentation/metal/mtllibrarytype/>
413 #[repr(u64)]
414 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
415 pub enum MTLLibraryType {
416 Executable = 0,
417 Dynamic = 1,
418 }
419
420 /// See <https://developer.apple.com/documentation/metal/mtlcompileoptions/>
421 pub enum MTLCompileOptions {}
422
423 foreign_obj_type! {
424 type CType = MTLCompileOptions;
425 pub struct CompileOptions;
426 }
427
428 impl CompileOptions {
429 pub fn new() -> Self {
430 unsafe {
431 let class = class!(MTLCompileOptions);
432 msg_send![class, new]
433 }
434 }
435 }
436
437 impl CompileOptionsRef {
438 pub unsafe fn preprocessor_macros(&self) -> *mut Object {
439 msg_send![self, preprocessorMacros]
440 }
441
442 pub unsafe fn set_preprocessor_macros(&self, defines: *mut Object) {
443 msg_send![self, setPreprocessorMacros: defines]
444 }
445
446 pub fn is_fast_math_enabled(&self) -> bool {
447 unsafe { msg_send_bool![self, fastMathEnabled] }
448 }
449
450 pub fn set_fast_math_enabled(&self, enabled: bool) {
451 unsafe { msg_send![self, setFastMathEnabled: enabled] }
452 }
453
454 /// Only available on (macos(10.11), ios(9.0))
455 pub fn language_version(&self) -> MTLLanguageVersion {
456 unsafe { msg_send![self, languageVersion] }
457 }
458
459 /// Only available on (macos(10.11), ios(9.0))
460 pub fn set_language_version(&self, version: MTLLanguageVersion) {
461 unsafe { msg_send![self, setLanguageVersion: version] }
462 }
463
464 /// Only available on (macos(11.0), ios(14.0))
465 pub fn library_type(&self) -> MTLLibraryType {
466 unsafe { msg_send![self, libraryType] }
467 }
468
469 /// Only available on (macos(11.0), ios(14.0))
470 pub fn set_library_type(&self, lib_type: MTLLibraryType) {
471 unsafe { msg_send![self, setLibraryType: lib_type] }
472 }
473
474 /// Only available on (macos(11.0), ios(14.0))
475 pub fn install_name(&self) -> &str {
476 unsafe {
477 let name = msg_send![self, installName];
478 crate::nsstring_as_str(name)
479 }
480 }
481
482 /// Only available on (macos(11.0), ios(14.0))
483 pub fn set_install_name(&self, name: &str) {
484 unsafe {
485 let install_name = crate::nsstring_from_str(name);
486 let () = msg_send![self, setInstallName: install_name];
487 }
488 }
489
490 /// Only available on (macos(11.0), ios(14.0))
491 ///
492 /// Marshal to Rust Vec
493 pub fn libraries(&self) -> Vec<DynamicLibrary> {
494 unsafe {
495 let libraries: *mut Object = msg_send![self, libraries];
496 let count: NSUInteger = msg_send![libraries, count];
497 (0..count)
498 .map(|i| {
499 let lib = msg_send![libraries, objectAtIndex: i];
500 DynamicLibrary::from_ptr(lib)
501 })
502 .collect()
503 }
504 }
505
506 /// Only available on (macos(11.0), ios(14.0))
507 ///
508 /// As raw NSArray
509 pub fn libraries_as_nsarray(&self) -> &ArrayRef<DynamicLibrary> {
510 unsafe { msg_send![self, libraries] }
511 }
512
513 /// Only available on (macos(11.0), ios(14.0))
514 ///
515 /// Marshal from Rust slice
516 pub fn set_libraries(&self, libraries: &[&DynamicLibraryRef]) {
517 let ns_array = Array::<DynamicLibrary>::from_slice(libraries);
518 unsafe { msg_send![self, setLibraries: ns_array] }
519 }
520
521 /// Only available on (macos(11.0), ios(14.0))
522 ///
523 /// From raw NSArray
524 pub fn set_libraries_nsarray(&self, libraries: &ArrayRef<DynamicLibrary>) {
525 unsafe { msg_send![self, setLibraries: libraries] }
526 }
527
528 /// Only available on (macos(11.0), macCatalyst(14.0), ios(13.0))
529 pub fn preserve_invariance(&self) -> bool {
530 unsafe { msg_send_bool![self, preserveInvariance] }
531 }
532
533 /// Only available on (macos(11.0), macCatalyst(14.0), ios(13.0))
534 pub fn set_preserve_invariance(&self, preserve: bool) {
535 unsafe { msg_send![self, setPreserveInvariance: preserve] }
536 }
537 }
538
539 /// See <https://developer.apple.com/documentation/metal/mtllibraryerror/>
540 #[repr(u64)]
541 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
542 pub enum MTLLibraryError {
543 Unsupported = 1,
544 Internal = 2,
545 CompileFailure = 3,
546 CompileWarning = 4,
547 /// Only available on (macos(10.12), ios(10.0))
548 FunctionNotFound = 5,
549 /// Only available on (macos(10.12), ios(10.0))
550 FileNotFound = 6,
551 }
552
553 /// See <https://developer.apple.com/documentation/metal/mtllibrary/>
554 pub enum MTLLibrary {}
555
556 foreign_obj_type! {
557 type CType = MTLLibrary;
558 pub struct Library;
559 }
560
561 impl LibraryRef {
562 pub fn device(&self) -> &DeviceRef {
563 unsafe { msg_send![self, device] }
564 }
565
566 pub fn label(&self) -> &str {
567 unsafe {
568 let label = msg_send![self, label];
569 crate::nsstring_as_str(label)
570 }
571 }
572
573 pub fn set_label(&self, label: &str) {
574 unsafe {
575 let nslabel = crate::nsstring_from_str(label);
576 let () = msg_send![self, setLabel: nslabel];
577 }
578 }
579
580 // FIXME: should rename to new_function
581 pub fn get_function(
582 &self,
583 name: &str,
584 constants: Option<FunctionConstantValues>,
585 ) -> Result<Function, String> {
586 unsafe {
587 let nsname = crate::nsstring_from_str(name);
588
589 let function: *mut MTLFunction = match constants {
590 Some(c) => try_objc! { err => msg_send![self,
591 newFunctionWithName: nsname.as_ref()
592 constantValues: c.as_ref()
593 error: &mut err
594 ]},
595 None => msg_send![self, newFunctionWithName: nsname.as_ref()],
596 };
597
598 if !function.is_null() {
599 Ok(Function::from_ptr(function))
600 } else {
601 Err(format!("Function '{}' does not exist", name))
602 }
603 }
604 }
605
606 // TODO: get_function_async with completion handler
607
608 pub fn function_names(&self) -> Vec<String> {
609 unsafe {
610 let names: *mut Object = msg_send![self, functionNames];
611 let count: NSUInteger = msg_send![names, count];
612 (0..count)
613 .map(|i| {
614 let name = msg_send![names, objectAtIndex: i];
615 nsstring_as_str(name).to_string()
616 })
617 .collect()
618 }
619 }
620
621 /// Only available on (macos(11.0), ios(14.0))
622 pub fn library_type(&self) -> MTLLibraryType {
623 unsafe { msg_send![self, type] }
624 }
625
626 /// Only available on (macos(11.0), ios(14.0))
627 pub fn install_name(&self) -> Option<&str> {
628 unsafe {
629 let maybe_name: *mut Object = msg_send![self, installName];
630 maybe_name.as_ref().map(crate::nsstring_as_str)
631 }
632 }
633
634 /// Only available on (macos(11.0), ios(14.0))
635 pub fn new_function_with_descriptor(
636 &self,
637 descriptor: &FunctionDescriptorRef,
638 ) -> Result<Function, String> {
639 unsafe {
640 let function: *mut MTLFunction = try_objc! {
641 err => msg_send![self,
642 newFunctionWithDescriptor: descriptor
643 error: &mut err
644 ]
645 };
646
647 if !function.is_null() {
648 Ok(Function::from_ptr(function))
649 } else {
650 Err(String::from("new_function_with_descriptor() failed"))
651 }
652 }
653 }
654
655 /// Only available on (macos(11.0), ios(14.0))
656 pub fn new_intersection_function_with_descriptor(
657 &self,
658 descriptor: &IntersectionFunctionDescriptorRef,
659 ) -> Result<Function, String> {
660 unsafe {
661 let function: *mut MTLFunction = try_objc! {
662 err => msg_send![self,
663 newIntersectionFunctionWithDescriptor: descriptor
664 error: &mut err
665 ]
666 };
667
668 if !function.is_null() {
669 Ok(Function::from_ptr(function))
670 } else {
671 Err(String::from(
672 "new_intersection_function_with_descriptor() failed",
673 ))
674 }
675 }
676 }
677 }
678
679 /// Only available on (macos(11.0), ios(14.0))
680 ///
681 /// See <https://developer.apple.com/documentation/metal/mtldynamiclibraryerror/>
682 #[repr(u64)]
683 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
684 pub enum MTLDynamicLibraryError {
685 None = 0,
686 InvalidFile = 1,
687 CompilationFailure = 2,
688 UnresolvedInstallName = 3,
689 DependencyLoadFailure = 4,
690 Unsupported = 5,
691 }
692
693 /// See <https://developer.apple.com/documentation/metal/mtldynamiclibrary/>
694 pub enum MTLDynamicLibrary {}
695
696 foreign_obj_type! {
697 type CType = MTLDynamicLibrary;
698 pub struct DynamicLibrary;
699 }
700
701 impl DynamicLibraryRef {
702 pub fn device(&self) -> &DeviceRef {
703 unsafe { msg_send![self, device] }
704 }
705
706 pub fn label(&self) -> &str {
707 unsafe {
708 let label = msg_send![self, label];
709 crate::nsstring_as_str(label)
710 }
711 }
712
713 pub fn set_label(&self, label: &str) {
714 unsafe {
715 let nslabel = crate::nsstring_from_str(label);
716 let () = msg_send![self, setLabel: nslabel];
717 }
718 }
719
720 pub fn install_name(&self) -> &str {
721 unsafe {
722 let name = msg_send![self, installName];
723 crate::nsstring_as_str(name)
724 }
725 }
726
727 pub fn serialize_to_url(&self, url: &URLRef) -> Result<bool, String> {
728 unsafe { msg_send_bool_error_check![self, serializeToURL: url] }
729 }
730 }
731
732 /// macOS 11.0+ iOS 14.0+
733 ///
734 /// See <https://developer.apple.com/documentation/metal/mtlbinaryarchivedescriptor/>
735 pub enum MTLBinaryArchiveDescriptor {}
736
737 foreign_obj_type! {
738 type CType = MTLBinaryArchiveDescriptor;
739 pub struct BinaryArchiveDescriptor;
740 }
741
742 impl BinaryArchiveDescriptor {
743 pub fn new() -> Self {
744 unsafe {
745 let class = class!(MTLBinaryArchiveDescriptor);
746 msg_send![class, new]
747 }
748 }
749 }
750
751 impl BinaryArchiveDescriptorRef {
752 pub fn url(&self) -> &URLRef {
753 unsafe { msg_send![self, url] }
754 }
755 pub fn set_url(&self, url: &URLRef) {
756 unsafe { msg_send![self, setUrl: url] }
757 }
758 }
759
760 /// macOS 11.0+ iOS 14.0+
761 ///
762 /// See <https://developer.apple.com/documentation/metal/mtlbinaryarchive/>
763 pub enum MTLBinaryArchive {}
764
765 foreign_obj_type! {
766 type CType = MTLBinaryArchive;
767 pub struct BinaryArchive;
768 }
769
770 impl BinaryArchiveRef {
771 pub fn device(&self) -> &DeviceRef {
772 unsafe { msg_send![self, device] }
773 }
774
775 pub fn label(&self) -> &str {
776 unsafe {
777 let label = msg_send![self, label];
778 crate::nsstring_as_str(label)
779 }
780 }
781
782 pub fn set_label(&self, label: &str) {
783 unsafe {
784 let nslabel = crate::nsstring_from_str(label);
785 let () = msg_send![self, setLabel: nslabel];
786 }
787 }
788
789 pub fn add_compute_pipeline_functions_with_descriptor(
790 &self,
791 descriptor: &ComputePipelineDescriptorRef,
792 ) -> Result<bool, String> {
793 unsafe {
794 msg_send_bool_error_check![self, addComputePipelineFunctionsWithDescriptor: descriptor]
795 }
796 }
797
798 pub fn add_render_pipeline_functions_with_descriptor(
799 &self,
800 descriptor: &RenderPipelineDescriptorRef,
801 ) -> Result<bool, String> {
802 unsafe {
803 msg_send_bool_error_check![self, addRenderPipelineFunctionsWithDescriptor: descriptor]
804 }
805 }
806
807 // TODO: addTileRenderPipelineFunctionsWithDescriptor
808 // - (BOOL)addTileRenderPipelineFunctionsWithDescriptor:(MTLTileRenderPipelineDescriptor *)descriptor
809 // error:(NSError * _Nullable *)error;
810
811 pub fn serialize_to_url(&self, url: &URLRef) -> Result<bool, String> {
812 unsafe {
813 let mut err: *mut Object = ptr::null_mut();
814 let result: BOOL = msg_send![self, serializeToURL:url
815 error:&mut err];
816 if !err.is_null() {
817 // FIXME: copy pasta
818 let desc: *mut Object = msg_send![err, localizedDescription];
819 let c_msg: *const c_char = msg_send![desc, UTF8String];
820 let message = CStr::from_ptr(c_msg).to_string_lossy().into_owned();
821 Err(message)
822 } else {
823 match result {
824 YES => Ok(true),
825 NO => Ok(false),
826 #[cfg(not(target_arch = "aarch64"))]
827 _ => unreachable!(),
828 }
829 }
830 }
831 }
832 }
833
834 /// macOS 11.0+ iOS 14.0+
835 ///
836 /// See <https://developer.apple.com/documentation/metal/mtllinkedfunctions/>
837 pub enum MTLLinkedFunctions {}
838
839 foreign_obj_type! {
840 type CType = MTLLinkedFunctions;
841 pub struct LinkedFunctions;
842 }
843
844 impl LinkedFunctions {
845 pub fn new() -> Self {
846 unsafe {
847 let class = class!(MTLLinkedFunctions);
848 msg_send![class, new]
849 }
850 }
851 }
852
853 impl LinkedFunctionsRef {
854 /// Marshal to Rust Vec
855 pub fn functions(&self) -> Vec<Function> {
856 unsafe {
857 let functions: *mut Object = msg_send![self, functions];
858 let count: NSUInteger = msg_send![functions, count];
859 (0..count)
860 .map(|i| {
861 let f = msg_send![functions, objectAtIndex: i];
862 Function::from_ptr(f)
863 })
864 .collect()
865 }
866 }
867
868 /// Marshal from Rust slice
869 pub fn set_functions(&self, functions: &[&FunctionRef]) {
870 let ns_array = Array::<Function>::from_slice(functions);
871 unsafe { msg_send![self, setFunctions: ns_array] }
872 }
873
874 /// Marshal to Rust Vec
875 pub fn binary_functions(&self) -> Vec<Function> {
876 unsafe {
877 let functions: *mut Object = msg_send![self, binaryFunctions];
878 let count: NSUInteger = msg_send![functions, count];
879 (0..count)
880 .map(|i| {
881 let f = msg_send![functions, objectAtIndex: i];
882 Function::from_ptr(f)
883 })
884 .collect()
885 }
886 }
887
888 /// Marshal from Rust slice
889 pub fn set_binary_functions(&self, functions: &[&FunctionRef]) {
890 let ns_array = Array::<Function>::from_slice(functions);
891 unsafe { msg_send![self, setBinaryFunctions: ns_array] }
892 }
893
894 // TODO: figure out NSDictionary wrapper
895 // TODO: groups
896 // @property (readwrite, nonatomic, copy, nullable) NSDictionary<NSString*, NSArray<id<MTLFunction>>*> *groups;
897 }