Skip to main content

mtl_gpu/indirect/
mod.rs

1//! Metal indirect command buffer and command types.
2//!
3//! Corresponds to `Metal/MTLIndirectCommandBuffer.hpp` and `Metal/MTLIndirectCommandEncoder.hpp`.
4//!
5//! Indirect command buffers allow you to encode GPU commands from the GPU itself,
6//! enabling GPU-driven rendering and compute workflows.
7//!
8//! # Example
9//!
10//! ```ignore
11//! use mtl_gpu::{device, IndirectCommandBufferDescriptor, IndirectCommandType};
12//!
13//! let device = device::system_default().expect("no Metal device");
14//!
15//! let mut desc = IndirectCommandBufferDescriptor::new().unwrap();
16//! desc.set_command_types(IndirectCommandType::DRAW | IndirectCommandType::DRAW_INDEXED);
17//! desc.set_max_vertex_buffer_bind_count(8);
18//!
19//! let icb = device.new_indirect_command_buffer(&desc, 100, ResourceOptions::default());
20//! ```
21
22mod buffer;
23mod buffer_descriptor;
24mod compute_command;
25mod render_command;
26
27pub use buffer::IndirectCommandBuffer;
28pub use buffer_descriptor::IndirectCommandBufferDescriptor;
29pub use compute_command::IndirectComputeCommand;
30pub use render_command::IndirectRenderCommand;
31
32// ============================================================================
33// IndirectCommandBufferExecutionRange
34// ============================================================================
35
36/// Execution range for indirect command buffers.
37///
38/// C++ equivalent: `MTL::IndirectCommandBufferExecutionRange`
39#[repr(C, packed)]
40#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
41pub struct IndirectCommandBufferExecutionRange {
42    /// The starting index.
43    pub location: u32,
44    /// The number of commands.
45    pub length: u32,
46}
47
48impl IndirectCommandBufferExecutionRange {
49    /// Create a new execution range.
50    #[inline]
51    pub const fn new(location: u32, length: u32) -> Self {
52        Self { location, length }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use std::ffi::c_void;
60
61    #[test]
62    fn test_indirect_command_buffer_execution_range_size() {
63        assert_eq!(
64            std::mem::size_of::<IndirectCommandBufferExecutionRange>(),
65            8
66        );
67    }
68
69    #[test]
70    fn test_indirect_command_buffer_descriptor_size() {
71        assert_eq!(
72            std::mem::size_of::<IndirectCommandBufferDescriptor>(),
73            std::mem::size_of::<*mut c_void>()
74        );
75    }
76
77    #[test]
78    fn test_indirect_command_buffer_size() {
79        assert_eq!(
80            std::mem::size_of::<IndirectCommandBuffer>(),
81            std::mem::size_of::<*mut c_void>()
82        );
83    }
84
85    #[test]
86    fn test_indirect_render_command_size() {
87        assert_eq!(
88            std::mem::size_of::<IndirectRenderCommand>(),
89            std::mem::size_of::<*mut c_void>()
90        );
91    }
92
93    #[test]
94    fn test_indirect_compute_command_size() {
95        assert_eq!(
96            std::mem::size_of::<IndirectComputeCommand>(),
97            std::mem::size_of::<*mut c_void>()
98        );
99    }
100}