1use crate::import_objc_macros::*;
2use crate::{handle, DeviceCreated, NSUInteger, Object, ObjectPointer};
3
4pub struct MTLLibrary(ObjectPointer);
5handle!(MTLLibrary);
6
7impl MTLLibrary {
8 pub unsafe fn get_function_names(&self) -> Vec<&str> {
9 let names = ObjectPointer(msg_send![self.get_ptr(), functionNames]);
10 let length: NSUInteger = msg_send![names, count];
11 (0..length)
12 .map(|index| {
13 let obj = ObjectPointer(msg_send![names, objectAtIndex: index]);
14 let obj = ObjectPointer(msg_send![obj, retain]);
15 let bytes: *const u8 = msg_send![obj, UTF8String];
16 let len: NSUInteger = msg_send![obj, length];
17 let bytes = std::slice::from_raw_parts(bytes, len as usize);
18 std::str::from_utf8(bytes).unwrap()
19 })
20 .collect()
21 }
22 pub unsafe fn new_function_with_name(&self, name: &str) -> Option<MTLFunction> {
23 let cls = class!(NSString);
24 let bytes = name.as_ptr();
25 let st = ObjectPointer(msg_send![cls, alloc]);
26 let st = ObjectPointer(msg_send![
27 st,
28 initWithBytes:bytes
29 length:name.len()
30 encoding:4 ]);
32 let obj = ObjectPointer(msg_send![self.get_ptr(), newFunctionWithName: st]);
33 if obj.0.is_null() {
34 None
35 } else {
36 Some(MTLFunction::from_ptr(obj))
37 }
38 }
39}
40
41impl Object for MTLLibrary {
42 unsafe fn from_ptr(ptr: ObjectPointer) -> Self
43 where
44 Self: Sized,
45 {
46 MTLLibrary(ptr)
47 }
48
49 fn get_ptr(&self) -> ObjectPointer {
50 self.0
51 }
52}
53
54impl DeviceCreated for MTLLibrary {}
55
56#[repr(u64)]
57pub enum MTLFunctionType {
58 Vertex = 1,
59 Fragment = 2,
60 Kernel = 3,
61}
62
63pub struct MTLFunction(ObjectPointer);
64handle!(MTLFunction);
65
66impl MTLFunction {
67 pub unsafe fn get_function_type(&self) -> MTLFunctionType {
68 msg_send![self.get_ptr(), functionType]
69 }
70 pub unsafe fn get_name(&self) -> &str {
71 let string = ObjectPointer(msg_send![self.get_ptr(), name]);
72 let bytes: *const u8 = msg_send![string, UTF8String];
73 let len: NSUInteger = msg_send![string, length];
74 let bytes = std::slice::from_raw_parts(bytes, len as usize);
75 std::str::from_utf8(bytes).unwrap()
76 }
77}
78
79impl Object for MTLFunction {
80 unsafe fn from_ptr(ptr: ObjectPointer) -> Self
81 where
82 Self: Sized,
83 {
84 MTLFunction(ptr)
85 }
86
87 fn get_ptr(&self) -> ObjectPointer {
88 self.0
89 }
90}
91
92impl DeviceCreated for MTLFunction {}
93
94#[repr(u64)]
95pub enum MTLLanguageVersion {
96 V10 = 1 << 16,
97 V11 = (1 << 16) + 1,
98 V12 = (1 << 16) + 2,
99 V20 = 2 << 16,
100 V21 = (2 << 16) + 1,
101 V22 = (2 << 16) + 2,
102}
103
104pub struct MTLCompileOptions(ObjectPointer);
105handle!(MTLCompileOptions);
106
107impl MTLCompileOptions {
108 pub unsafe fn new() -> MTLCompileOptions {
109 MTLCompileOptions({
110 let c = class!(MTLCompileOptions);
111 msg_send![c, new]
112 })
113 }
114 pub unsafe fn set_fast_math_enabled(&self, enabled: bool) {
115 msg_send![self.get_ptr(), setFastMathEnabled: enabled]
116 }
117 pub unsafe fn set_language_version(&self, version: MTLLanguageVersion) {
118 msg_send![self.get_ptr(), setLanguageVersion: version]
119 }
120}
121
122impl Object for MTLCompileOptions {
123 unsafe fn from_ptr(ptr: ObjectPointer) -> Self
124 where
125 Self: Sized,
126 {
127 MTLCompileOptions(ptr)
128 }
129
130 fn get_ptr(&self) -> ObjectPointer {
131 self.0
132 }
133}