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 crate::{Array, MTLTextureType};
9 
10 use cocoa_foundation::foundation::NSUInteger;
11 use objc::runtime::{NO, YES};
12 
13 #[repr(u64)]
14 #[allow(non_camel_case_types)]
15 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
16 pub enum MTLDataType {
17     None = 0,
18 
19     Struct = 1,
20     Array = 2,
21 
22     Float = 3,
23     Float2 = 4,
24     Float3 = 5,
25     Float4 = 6,
26 
27     Float2x2 = 7,
28     Float2x3 = 8,
29     Float2x4 = 9,
30 
31     Float3x2 = 10,
32     Float3x3 = 11,
33     Float3x4 = 12,
34 
35     Float4x2 = 13,
36     Float4x3 = 14,
37     Float4x4 = 15,
38 
39     Half = 16,
40     Half2 = 17,
41     Half3 = 18,
42     Half4 = 19,
43 
44     Half2x2 = 20,
45     Half2x3 = 21,
46     Half2x4 = 22,
47 
48     Half3x2 = 23,
49     Half3x3 = 24,
50     Half3x4 = 25,
51 
52     Half4x2 = 26,
53     Half4x3 = 27,
54     Half4x4 = 28,
55 
56     Int = 29,
57     Int2 = 30,
58     Int3 = 31,
59     Int4 = 32,
60 
61     UInt = 33,
62     UInt2 = 34,
63     UInt3 = 35,
64     UInt4 = 36,
65 
66     Short = 37,
67     Short2 = 38,
68     Short3 = 39,
69     Short4 = 40,
70 
71     UShort = 41,
72     UShort2 = 42,
73     UShort3 = 43,
74     UShort4 = 44,
75 
76     Char = 45,
77     Char2 = 46,
78     Char3 = 47,
79     Char4 = 48,
80 
81     UChar = 49,
82     UChar2 = 50,
83     UChar3 = 51,
84     UChar4 = 52,
85 
86     Bool = 53,
87     Bool2 = 54,
88     Bool3 = 55,
89     Bool4 = 56,
90 
91     Texture = 58,
92     Sampler = 59,
93     Pointer = 60,
94     R8Unorm = 62,
95     R8Snorm = 63,
96     R16Unorm = 64,
97     R16Snorm = 65,
98     RG8Unorm = 66,
99     RG8Snorm = 67,
100     RG16Unorm = 68,
101     RG16Snorm = 69,
102     RGBA8Unorm = 70,
103     RGBA8Unorm_sRGB = 71,
104     RGBA8Snorm = 72,
105     RGBA16Unorm = 73,
106     RGBA16Snorm = 74,
107     RGB10A2Unorm = 75,
108     RG11B10Float = 76,
109     RGB9E5Float = 77,
110 }
111 
112 #[repr(u64)]
113 #[allow(non_camel_case_types)]
114 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
115 pub enum MTLArgumentType {
116     Buffer = 0,
117     ThreadgroupMemory = 1,
118     Texture = 2,
119     Sampler = 3,
120     ImageblockData = 16,
121     Imageblock = 17,
122 }
123 
124 #[repr(u64)]
125 #[allow(non_camel_case_types)]
126 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
127 pub enum MTLArgumentAccess {
128     ReadOnly = 0,
129     ReadWrite = 1,
130     WriteOnly = 2,
131 }
132 
133 pub enum MTLStructMember {}
134 
135 foreign_obj_type! {
136     type CType = MTLStructMember;
137     pub struct StructMember;
138     pub struct StructMemberRef;
139 }
140 
141 impl StructMemberRef {
name(&self) -> &str142     pub fn name(&self) -> &str {
143         unsafe {
144             let name = msg_send![self, name];
145             crate::nsstring_as_str(name)
146         }
147     }
148 
offset(&self) -> NSUInteger149     pub fn offset(&self) -> NSUInteger {
150         unsafe { msg_send![self, offset] }
151     }
152 
data_type(&self) -> MTLDataType153     pub fn data_type(&self) -> MTLDataType {
154         unsafe { msg_send![self, dataType] }
155     }
156 
struct_type(&self) -> MTLStructType157     pub fn struct_type(&self) -> MTLStructType {
158         unsafe { msg_send![self, structType] }
159     }
160 
array_type(&self) -> MTLArrayType161     pub fn array_type(&self) -> MTLArrayType {
162         unsafe { msg_send![self, arrayType] }
163     }
164 }
165 
166 pub enum MTLStructType {}
167 
168 foreign_obj_type! {
169     type CType = MTLStructType;
170     pub struct StructType;
171     pub struct StructTypeRef;
172 }
173 
174 impl StructTypeRef {
members(&self) -> &Array<StructMember>175     pub fn members(&self) -> &Array<StructMember> {
176         unsafe { msg_send![self, members] }
177     }
178 
member_from_name(&self, name: &str) -> Option<&StructMemberRef>179     pub fn member_from_name(&self, name: &str) -> Option<&StructMemberRef> {
180         let nsname = crate::nsstring_from_str(name);
181 
182         unsafe { msg_send![self, memberByName: nsname] }
183     }
184 }
185 
186 pub enum MTLArrayType {}
187 
188 foreign_obj_type! {
189     type CType = MTLArrayType;
190     pub struct ArrayType;
191     pub struct ArrayTypeRef;
192 }
193 
194 impl ArrayTypeRef {
array_length(&self) -> NSUInteger195     pub fn array_length(&self) -> NSUInteger {
196         unsafe { msg_send![self, arrayLength] }
197     }
198 
stride(&self) -> NSUInteger199     pub fn stride(&self) -> NSUInteger {
200         unsafe { msg_send![self, stride] }
201     }
202 
element_type(&self) -> MTLDataType203     pub fn element_type(&self) -> MTLDataType {
204         unsafe { msg_send![self, elementType] }
205     }
206 
element_struct_type(&self) -> MTLStructType207     pub fn element_struct_type(&self) -> MTLStructType {
208         unsafe { msg_send![self, elementStructType] }
209     }
210 
element_array_type(&self) -> MTLArrayType211     pub fn element_array_type(&self) -> MTLArrayType {
212         unsafe { msg_send![self, elementArrayType] }
213     }
214 }
215 
216 pub enum MTLArgument {}
217 
218 foreign_obj_type! {
219     type CType = MTLArgument;
220     pub struct Argument;
221     pub struct ArgumentRef;
222 }
223 
224 impl ArgumentRef {
name(&self) -> &str225     pub fn name(&self) -> &str {
226         unsafe {
227             let name = msg_send![self, name];
228             crate::nsstring_as_str(name)
229         }
230     }
231 
type_(&self) -> MTLArgumentType232     pub fn type_(&self) -> MTLArgumentType {
233         unsafe { msg_send![self, type] }
234     }
235 
access(&self) -> MTLArgumentAccess236     pub fn access(&self) -> MTLArgumentAccess {
237         unsafe { msg_send![self, access] }
238     }
239 
index(&self) -> NSUInteger240     pub fn index(&self) -> NSUInteger {
241         unsafe { msg_send![self, index] }
242     }
243 
is_active(&self) -> bool244     pub fn is_active(&self) -> bool {
245         unsafe {
246             match msg_send![self, isActive] {
247                 YES => true,
248                 NO => false,
249                 _ => unreachable!(),
250             }
251         }
252     }
253 
buffer_alignment(&self) -> NSUInteger254     pub fn buffer_alignment(&self) -> NSUInteger {
255         unsafe { msg_send![self, bufferAlignment] }
256     }
257 
buffer_data_size(&self) -> NSUInteger258     pub fn buffer_data_size(&self) -> NSUInteger {
259         unsafe { msg_send![self, bufferDataSize] }
260     }
261 
buffer_data_type(&self) -> MTLDataType262     pub fn buffer_data_type(&self) -> MTLDataType {
263         unsafe { msg_send![self, bufferDataType] }
264     }
265 
buffer_struct_type(&self) -> &StructTypeRef266     pub fn buffer_struct_type(&self) -> &StructTypeRef {
267         unsafe { msg_send![self, bufferStructType] }
268     }
269 
threadgroup_memory_alignment(&self) -> NSUInteger270     pub fn threadgroup_memory_alignment(&self) -> NSUInteger {
271         unsafe { msg_send![self, threadgroupMemoryAlignment] }
272     }
273 
threadgroup_memory_data_size(&self) -> NSUInteger274     pub fn threadgroup_memory_data_size(&self) -> NSUInteger {
275         unsafe { msg_send![self, threadgroupMemoryDataSize] }
276     }
277 
texture_type(&self) -> MTLTextureType278     pub fn texture_type(&self) -> MTLTextureType {
279         unsafe { msg_send![self, textureType] }
280     }
281 
texture_data_type(&self) -> MTLDataType282     pub fn texture_data_type(&self) -> MTLDataType {
283         unsafe { msg_send![self, textureDataType] }
284     }
285 }
286 
287 pub enum MTLArgumentDescriptor {}
288 
289 foreign_obj_type! {
290     type CType = MTLArgumentDescriptor;
291     pub struct ArgumentDescriptor;
292     pub struct ArgumentDescriptorRef;
293 }
294 
295 impl ArgumentDescriptor {
new<'a>() -> &'a ArgumentDescriptorRef296     pub fn new<'a>() -> &'a ArgumentDescriptorRef {
297         unsafe {
298             let class = class!(MTLArgumentDescriptor);
299             msg_send![class, argumentDescriptor]
300         }
301     }
302 }
303 
304 impl ArgumentDescriptorRef {
set_data_type(&self, ty: MTLDataType)305     pub fn set_data_type(&self, ty: MTLDataType) {
306         unsafe { msg_send![self, setDataType: ty] }
307     }
308 
set_index(&self, index: NSUInteger)309     pub fn set_index(&self, index: NSUInteger) {
310         unsafe { msg_send![self, setIndex: index] }
311     }
312 
set_access(&self, access: MTLArgumentAccess)313     pub fn set_access(&self, access: MTLArgumentAccess) {
314         unsafe { msg_send![self, setAccess: access] }
315     }
316 
set_array_length(&self, length: NSUInteger)317     pub fn set_array_length(&self, length: NSUInteger) {
318         unsafe { msg_send![self, setArrayLength: length] }
319     }
320 
set_texture_type(&self, ty: MTLTextureType)321     pub fn set_texture_type(&self, ty: MTLTextureType) {
322         unsafe { msg_send![self, setTextureType: ty] }
323     }
324 }
325