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::NSUInteger;
11 use objc::runtime::{NO, YES};
12 
13 #[repr(u64)]
14 #[allow(non_camel_case_types)]
15 #[derive(Copy, Clone, Debug)]
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(u32)]
113 #[allow(non_camel_case_types)]
114 pub enum MTLArgumentType {
115     Buffer = 0,
116     ThreadgroupMemory = 1,
117     Texture = 2,
118     Sampler = 3,
119     ImageblockData = 16,
120     Imageblock = 17,
121 }
122 
123 #[repr(u32)]
124 #[allow(non_camel_case_types)]
125 pub enum MTLArgumentAccess {
126     ReadOnly = 0,
127     ReadWrite = 1,
128     WriteOnly = 2,
129 }
130 
131 pub enum MTLStructMember {}
132 
133 foreign_obj_type! {
134     type CType = MTLStructMember;
135     pub struct StructMember;
136     pub struct StructMemberRef;
137 }
138 
139 impl StructMemberRef {
name(&self) -> &str140     pub fn name(&self) -> &str {
141         unsafe {
142             let name = msg_send![self, name];
143             crate::nsstring_as_str(name)
144         }
145     }
146 
offset(&self) -> NSUInteger147     pub fn offset(&self) -> NSUInteger {
148         unsafe { msg_send![self, offset] }
149     }
150 
data_type(&self) -> MTLDataType151     pub fn data_type(&self) -> MTLDataType {
152         unsafe { msg_send![self, dataType] }
153     }
154 
struct_type(&self) -> MTLStructType155     pub fn struct_type(&self) -> MTLStructType {
156         unsafe { msg_send![self, structType] }
157     }
158 
array_type(&self) -> MTLArrayType159     pub fn array_type(&self) -> MTLArrayType {
160         unsafe { msg_send![self, arrayType] }
161     }
162 }
163 
164 pub enum MTLStructType {}
165 
166 foreign_obj_type! {
167     type CType = MTLStructType;
168     pub struct StructType;
169     pub struct StructTypeRef;
170 }
171 
172 impl StructTypeRef {
members(&self) -> &Array<StructMember>173     pub fn members(&self) -> &Array<StructMember> {
174         unsafe { msg_send![self, members] }
175     }
176 
member_from_name(&self, name: &str) -> Option<&StructMemberRef>177     pub fn member_from_name(&self, name: &str) -> Option<&StructMemberRef> {
178         let nsname = crate::nsstring_from_str(name);
179 
180         unsafe { msg_send![self, memberByName: nsname] }
181     }
182 }
183 
184 pub enum MTLArrayType {}
185 
186 foreign_obj_type! {
187     type CType = MTLArrayType;
188     pub struct ArrayType;
189     pub struct ArrayTypeRef;
190 }
191 
192 impl ArrayTypeRef {
array_length(&self) -> NSUInteger193     pub fn array_length(&self) -> NSUInteger {
194         unsafe { msg_send![self, arrayLength] }
195     }
196 
stride(&self) -> NSUInteger197     pub fn stride(&self) -> NSUInteger {
198         unsafe { msg_send![self, stride] }
199     }
200 
element_type(&self) -> MTLDataType201     pub fn element_type(&self) -> MTLDataType {
202         unsafe { msg_send![self, elementType] }
203     }
204 
element_struct_type(&self) -> MTLStructType205     pub fn element_struct_type(&self) -> MTLStructType {
206         unsafe { msg_send![self, elementStructType] }
207     }
208 
element_array_type(&self) -> MTLArrayType209     pub fn element_array_type(&self) -> MTLArrayType {
210         unsafe { msg_send![self, elementArrayType] }
211     }
212 }
213 
214 pub enum MTLArgument {}
215 
216 foreign_obj_type! {
217     type CType = MTLArgument;
218     pub struct Argument;
219     pub struct ArgumentRef;
220 }
221 
222 impl ArgumentRef {
name(&self) -> &str223     pub fn name(&self) -> &str {
224         unsafe {
225             let name = msg_send![self, name];
226             crate::nsstring_as_str(name)
227         }
228     }
229 
type_(&self) -> MTLArgumentType230     pub fn type_(&self) -> MTLArgumentType {
231         unsafe { msg_send![self, type] }
232     }
233 
access(&self) -> MTLArgumentAccess234     pub fn access(&self) -> MTLArgumentAccess {
235         unsafe { msg_send![self, access] }
236     }
237 
index(&self) -> NSUInteger238     pub fn index(&self) -> NSUInteger {
239         unsafe { msg_send![self, index] }
240     }
241 
is_active(&self) -> bool242     pub fn is_active(&self) -> bool {
243         unsafe {
244             match msg_send![self, isActive] {
245                 YES => true,
246                 NO => false,
247                 _ => unreachable!(),
248             }
249         }
250     }
251 
buffer_alignment(&self) -> NSUInteger252     pub fn buffer_alignment(&self) -> NSUInteger {
253         unsafe { msg_send![self, bufferAlignment] }
254     }
255 
buffer_data_size(&self) -> NSUInteger256     pub fn buffer_data_size(&self) -> NSUInteger {
257         unsafe { msg_send![self, bufferDataSize] }
258     }
259 
buffer_data_type(&self) -> MTLDataType260     pub fn buffer_data_type(&self) -> MTLDataType {
261         unsafe { msg_send![self, bufferDataType] }
262     }
263 
buffer_struct_type(&self) -> &StructTypeRef264     pub fn buffer_struct_type(&self) -> &StructTypeRef {
265         unsafe { msg_send![self, bufferStructType] }
266     }
267 
threadgroup_memory_alignment(&self) -> NSUInteger268     pub fn threadgroup_memory_alignment(&self) -> NSUInteger {
269         unsafe { msg_send![self, threadgroupMemoryAlignment] }
270     }
271 
threadgroup_memory_data_size(&self) -> NSUInteger272     pub fn threadgroup_memory_data_size(&self) -> NSUInteger {
273         unsafe { msg_send![self, threadgroupMemoryDataSize] }
274     }
275 
texture_type(&self) -> MTLTextureType276     pub fn texture_type(&self) -> MTLTextureType {
277         unsafe { msg_send![self, textureType] }
278     }
279 
texture_data_type(&self) -> MTLDataType280     pub fn texture_data_type(&self) -> MTLDataType {
281         unsafe { msg_send![self, textureDataType] }
282     }
283 }
284 
285 pub enum MTLArgumentDescriptor {}
286 
287 foreign_obj_type! {
288     type CType = MTLArgumentDescriptor;
289     pub struct ArgumentDescriptor;
290     pub struct ArgumentDescriptorRef;
291 }
292 
293 impl ArgumentDescriptor {
new<'a>() -> &'a ArgumentDescriptorRef294     pub fn new<'a>() -> &'a ArgumentDescriptorRef {
295         unsafe {
296             let class = class!(MTLArgumentDescriptor);
297             msg_send![class, argumentDescriptor]
298         }
299     }
300 }
301 
302 impl ArgumentDescriptorRef {
set_data_type(&self, ty: MTLDataType)303     pub fn set_data_type(&self, ty: MTLDataType) {
304         unsafe { msg_send![self, setDataType: ty] }
305     }
306 
set_index(&self, index: NSUInteger)307     pub fn set_index(&self, index: NSUInteger) {
308         unsafe { msg_send![self, setIndex: index] }
309     }
310 
set_access(&self, access: MTLArgumentAccess)311     pub fn set_access(&self, access: MTLArgumentAccess) {
312         unsafe { msg_send![self, setAccess: access] }
313     }
314 
set_array_length(&self, length: NSUInteger)315     pub fn set_array_length(&self, length: NSUInteger) {
316         unsafe { msg_send![self, setArrayLength: length] }
317     }
318 
set_texture_type(&self, ty: MTLTextureType)319     pub fn set_texture_type(&self, ty: MTLTextureType) {
320         unsafe { msg_send![self, setTextureType: ty] }
321     }
322 }
323