1 // Copyright 2016 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 block::Block;
11 
12 #[repr(u32)]
13 #[allow(non_camel_case_types)]
14 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
15 pub enum MTLCommandBufferStatus {
16     NotEnqueued = 0,
17     Enqueued = 1,
18     Committed = 2,
19     Scheduled = 3,
20     Completed = 4,
21     Error = 5,
22 }
23 
24 #[repr(u32)]
25 #[allow(non_camel_case_types)]
26 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
27 pub enum MTLCommandBufferError {
28     None = 0,
29     Internal = 1,
30     Timeout = 2,
31     PageFault = 3,
32     Blacklisted = 4,
33     NotPermitted = 7,
34     OutOfMemory = 8,
35     InvalidResource = 9,
36     Memoryless = 10,
37     DeviceRemoved = 11,
38 }
39 
40 #[repr(u32)]
41 #[allow(non_camel_case_types)]
42 #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
43 pub enum MTLDispatchType {
44     Serial = 0,
45     Concurrent = 1,
46 }
47 
48 type CommandBufferHandler<'a> = Block<(&'a CommandBufferRef,), ()>;
49 
50 pub enum MTLCommandBuffer {}
51 
52 foreign_obj_type! {
53     type CType = MTLCommandBuffer;
54     pub struct CommandBuffer;
55     pub struct CommandBufferRef;
56 }
57 
58 impl CommandBufferRef {
label(&self) -> &str59     pub fn label(&self) -> &str {
60         unsafe {
61             let label = msg_send![self, label];
62             crate::nsstring_as_str(label)
63         }
64     }
65 
set_label(&self, label: &str)66     pub fn set_label(&self, label: &str) {
67         unsafe {
68             let nslabel = crate::nsstring_from_str(label);
69             let () = msg_send![self, setLabel: nslabel];
70         }
71     }
72 
enqueue(&self)73     pub fn enqueue(&self) {
74         unsafe { msg_send![self, enqueue] }
75     }
76 
commit(&self)77     pub fn commit(&self) {
78         unsafe { msg_send![self, commit] }
79     }
80 
status(&self) -> MTLCommandBufferStatus81     pub fn status(&self) -> MTLCommandBufferStatus {
82         unsafe { msg_send![self, status] }
83     }
84 
present_drawable(&self, drawable: &DrawableRef)85     pub fn present_drawable(&self, drawable: &DrawableRef) {
86         unsafe { msg_send![self, presentDrawable: drawable] }
87     }
88 
wait_until_completed(&self)89     pub fn wait_until_completed(&self) {
90         unsafe { msg_send![self, waitUntilCompleted] }
91     }
92 
wait_until_scheduled(&self)93     pub fn wait_until_scheduled(&self) {
94         unsafe { msg_send![self, waitUntilScheduled] }
95     }
96 
add_completed_handler(&self, block: &CommandBufferHandler)97     pub fn add_completed_handler(&self, block: &CommandBufferHandler) {
98         unsafe { msg_send![self, addCompletedHandler: block] }
99     }
100 
new_blit_command_encoder(&self) -> &BlitCommandEncoderRef101     pub fn new_blit_command_encoder(&self) -> &BlitCommandEncoderRef {
102         unsafe { msg_send![self, blitCommandEncoder] }
103     }
104 
new_compute_command_encoder(&self) -> &ComputeCommandEncoderRef105     pub fn new_compute_command_encoder(&self) -> &ComputeCommandEncoderRef {
106         unsafe { msg_send![self, computeCommandEncoder] }
107     }
108 
new_render_command_encoder( &self, descriptor: &RenderPassDescriptorRef, ) -> &RenderCommandEncoderRef109     pub fn new_render_command_encoder(
110         &self,
111         descriptor: &RenderPassDescriptorRef,
112     ) -> &RenderCommandEncoderRef {
113         unsafe { msg_send![self, renderCommandEncoderWithDescriptor: descriptor] }
114     }
115 
new_parallel_render_command_encoder( &self, descriptor: &RenderPassDescriptorRef, ) -> &ParallelRenderCommandEncoderRef116     pub fn new_parallel_render_command_encoder(
117         &self,
118         descriptor: &RenderPassDescriptorRef,
119     ) -> &ParallelRenderCommandEncoderRef {
120         unsafe { msg_send![self, parallelRenderCommandEncoderWithDescriptor: descriptor] }
121     }
122 
compute_command_encoder_with_dispatch_type( &self, ty: MTLDispatchType, ) -> &ComputeCommandEncoderRef123     pub fn compute_command_encoder_with_dispatch_type(
124         &self,
125         ty: MTLDispatchType,
126     ) -> &ComputeCommandEncoderRef {
127         unsafe { msg_send![self, computeCommandEncoderWithDispatchType: ty] }
128     }
129 }
130