Thanks to visit codestin.com
Credit goes to docs.rs

metal/
commandbuffer.rs

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
8use super::*;
9
10use block::Block;
11
12/// See <https://developer.apple.com/documentation/metal/mtlcommandbufferstatus>
13#[repr(u32)]
14#[allow(non_camel_case_types)]
15#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
16pub enum MTLCommandBufferStatus {
17    NotEnqueued = 0,
18    Enqueued = 1,
19    Committed = 2,
20    Scheduled = 3,
21    Completed = 4,
22    Error = 5,
23}
24
25/// See <https://developer.apple.com/documentation/metal/mtlcommandbuffererror>
26#[repr(u32)]
27#[allow(non_camel_case_types)]
28#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
29pub enum MTLCommandBufferError {
30    None = 0,
31    Internal = 1,
32    Timeout = 2,
33    PageFault = 3,
34    Blacklisted = 4,
35    NotPermitted = 7,
36    OutOfMemory = 8,
37    InvalidResource = 9,
38    Memoryless = 10,
39    DeviceRemoved = 11,
40}
41
42/// See <https://developer.apple.com/documentation/metal/mtldispatchtype>
43#[repr(u32)]
44#[allow(non_camel_case_types)]
45#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
46pub enum MTLDispatchType {
47    Serial = 0,
48    Concurrent = 1,
49}
50
51type CommandBufferHandler<'a> = Block<(&'a CommandBufferRef,), ()>;
52
53/// See <https://developer.apple.com/documentation/metal/mtlcommandbuffer>.
54pub enum MTLCommandBuffer {}
55
56foreign_obj_type! {
57    type CType = MTLCommandBuffer;
58    pub struct CommandBuffer;
59}
60
61impl CommandBufferRef {
62    pub fn label(&self) -> &str {
63        unsafe {
64            let label = msg_send![self, label];
65            crate::nsstring_as_str(label)
66        }
67    }
68
69    pub fn set_label(&self, label: &str) {
70        unsafe {
71            let nslabel = crate::nsstring_from_str(label);
72            let () = msg_send![self, setLabel: nslabel];
73        }
74    }
75
76    pub fn enqueue(&self) {
77        unsafe { msg_send![self, enqueue] }
78    }
79
80    pub fn commit(&self) {
81        unsafe { msg_send![self, commit] }
82    }
83
84    pub fn status(&self) -> MTLCommandBufferStatus {
85        unsafe { msg_send![self, status] }
86    }
87
88    pub fn present_drawable(&self, drawable: &DrawableRef) {
89        unsafe { msg_send![self, presentDrawable: drawable] }
90    }
91
92    pub fn wait_until_completed(&self) {
93        unsafe { msg_send![self, waitUntilCompleted] }
94    }
95
96    pub fn wait_until_scheduled(&self) {
97        unsafe { msg_send![self, waitUntilScheduled] }
98    }
99
100    pub fn add_completed_handler(&self, block: &CommandBufferHandler) {
101        unsafe { msg_send![self, addCompletedHandler: block] }
102    }
103
104    pub fn add_scheduled_handler(&self, block: &CommandBufferHandler) {
105        unsafe { msg_send![self, addScheduledHandler: block] }
106    }
107
108    /// Create a blit command encoder.
109    ///
110    /// Although this method is named with a `new_` prefix, the actual Metal
111    /// method is not, and it returns an object that has been added to the
112    /// autorelease pool.  See <https://github.com/gfx-rs/metal-rs/issues/128>.
113    pub fn new_blit_command_encoder(&self) -> &BlitCommandEncoderRef {
114        unsafe { msg_send![self, blitCommandEncoder] }
115    }
116
117    pub fn blit_command_encoder_with_descriptor(
118        &self,
119        descriptor: &BlitPassDescriptorRef,
120    ) -> &BlitCommandEncoderRef {
121        unsafe { msg_send![self, blitCommandEncoderWithDescriptor: descriptor] }
122    }
123
124    /// Create a compute command encoder.
125    ///
126    /// Although this method is named with a `new_` prefix, the actual Metal
127    /// method is not, and it returns an object that has been added to the
128    /// autorelease pool.  See <https://github.com/gfx-rs/metal-rs/issues/128>.
129    pub fn new_compute_command_encoder(&self) -> &ComputeCommandEncoderRef {
130        unsafe { msg_send![self, computeCommandEncoder] }
131    }
132
133    pub fn compute_command_encoder_with_dispatch_type(
134        &self,
135        ty: MTLDispatchType,
136    ) -> &ComputeCommandEncoderRef {
137        unsafe { msg_send![self, computeCommandEncoderWithDispatchType: ty] }
138    }
139
140    pub fn compute_command_encoder_with_descriptor(
141        &self,
142        descriptor: &ComputePassDescriptorRef,
143    ) -> &ComputeCommandEncoderRef {
144        unsafe { msg_send![self, computeCommandEncoderWithDescriptor: descriptor] }
145    }
146
147    /// Create a render command encoder.
148    ///
149    /// Although this method is named with a `new_` prefix, the actual Metal
150    /// method is not, and it returns an object that has been added to the
151    /// autorelease pool.  See <https://github.com/gfx-rs/metal-rs/issues/128>.
152    pub fn new_render_command_encoder(
153        &self,
154        descriptor: &RenderPassDescriptorRef,
155    ) -> &RenderCommandEncoderRef {
156        unsafe { msg_send![self, renderCommandEncoderWithDescriptor: descriptor] }
157    }
158
159    /// Create a parallel render command encoder.
160    ///
161    /// Although this method is named with a `new_` prefix, the actual Metal
162    /// method is not, and it returns an object that has been added to the
163    /// autorelease pool.  See <https://github.com/gfx-rs/metal-rs/issues/128>.
164    pub fn new_parallel_render_command_encoder(
165        &self,
166        descriptor: &RenderPassDescriptorRef,
167    ) -> &ParallelRenderCommandEncoderRef {
168        unsafe { msg_send![self, parallelRenderCommandEncoderWithDescriptor: descriptor] }
169    }
170
171    /// Create an acceleration structure command encoder.
172    ///
173    /// Although this method is named with a `new_` prefix, the actual Metal
174    /// method is not, and it returns an object that has been added to the
175    /// autorelease pool.  See <https://github.com/gfx-rs/metal-rs/issues/128>.
176    pub fn new_acceleration_structure_command_encoder(
177        &self,
178    ) -> &AccelerationStructureCommandEncoderRef {
179        unsafe { msg_send![self, accelerationStructureCommandEncoder] }
180    }
181
182    /// Create an acceleration structure command encoder.
183    ///
184    /// Although this method is named with a `new_` prefix, the actual Metal
185    /// method is not, and it returns an object that has been added to the
186    /// autorelease pool.  See <https://github.com/gfx-rs/metal-rs/issues/128>.
187    pub fn acceleration_structure_command_encoder_with_descriptor(
188        &self,
189        descriptor: &AccelerationStructurePassDescriptorRef,
190    ) -> &AccelerationStructureCommandEncoderRef {
191        unsafe { msg_send![self, accelerationStructureCommandEncoderWithDescriptor: descriptor] }
192    }
193
194    pub fn encode_signal_event(&self, event: &EventRef, new_value: u64) {
195        unsafe {
196            msg_send![self,
197                encodeSignalEvent: event
198                value: new_value
199            ]
200        }
201    }
202
203    pub fn encode_wait_for_event(&self, event: &EventRef, value: u64) {
204        unsafe {
205            msg_send![self,
206                encodeWaitForEvent: event
207                value: value
208            ]
209        }
210    }
211
212    pub fn push_debug_group(&self, name: &str) {
213        unsafe {
214            let nslabel = crate::nsstring_from_str(name);
215            msg_send![self, pushDebugGroup: nslabel]
216        }
217    }
218
219    pub fn pop_debug_group(&self) {
220        unsafe { msg_send![self, popDebugGroup] }
221    }
222}