Skip to main content

mtl_gpu/
lib.rs

1// Clippy allows for FFI binding patterns
2#![allow(clippy::not_unsafe_ptr_arg_deref)] // Raw pointer args are passed to Obj-C, not dereferenced in Rust
3#![allow(clippy::missing_safety_doc)] // from_raw patterns are consistent across the crate
4#![allow(clippy::module_inception)] // module::module pattern mirrors Metal API structure
5#![allow(clippy::self_named_constructors)] // Matches Objective-C factory method naming
6#![allow(clippy::forget_non_drop)] // Intentional for preventing block cleanup in async callbacks
7#![allow(clippy::too_many_arguments)] // Matches Metal API signatures
8
9//! Metal framework bindings for Rust.
10//!
11//! This crate provides safe, idiomatic Rust bindings to Apple's Metal graphics API.
12//! It is a 1:1 translation of the official metal-cpp library with zero external dependencies.
13//!
14//! # Module Organization
15//!
16//! The crate is organized to mirror the Metal framework structure:
17//!
18//! - [`types`] - Core value types (Origin, Size, Region, Viewport, etc.)
19//! - [`enums`] - All Metal enumerations and option flags
20//! - [`device`] - GPU device interface and creation functions
21//! - [`buffer`] - Buffer resources for data storage
22//! - [`texture`] - Texture resources for image data
23//! - [`command_queue`] - Command queue for command buffer scheduling
24//! - [`command_buffer`] - Command buffer for GPU command encoding
25//! - [`sampler`] - Sampler state for texture sampling
26//! - [`depth_stencil`] - Depth and stencil testing state
27//!
28//! # Example
29//!
30//! ```ignore
31//! use mtl_gpu::device;
32//!
33//! // Get the default GPU
34//! let device = device::system_default().expect("no Metal device");
35//! println!("Using GPU: {}", device.name());
36//!
37//! // Create a command queue
38//! let queue = device.new_command_queue().expect("failed to create queue");
39//!
40//! // Create a buffer
41//! let buffer = device.new_buffer(1024, ResourceOptions::STORAGE_MODE_SHARED)
42//!     .expect("failed to create buffer");
43//! ```
44
45// Core modules
46pub mod device;
47pub mod enums;
48pub mod error;
49pub mod types;
50
51// Allocation and Resource protocols
52pub mod allocation;
53pub mod resource;
54
55// Resource modules
56pub mod buffer;
57pub mod heap;
58pub mod texture;
59
60// Command modules
61pub mod command_buffer;
62pub mod command_queue;
63
64// Encoder modules
65pub mod encoder;
66
67// State modules
68pub mod depth_stencil;
69pub mod sampler;
70
71// Library and pipeline modules
72pub mod library;
73pub mod pipeline;
74
75// Pass descriptor modules
76pub mod pass;
77
78// Synchronization modules
79pub mod sync;
80
81// Acceleration structure modules
82pub mod acceleration;
83
84// IO modules
85pub mod io;
86
87// Argument reflection modules
88pub mod argument;
89pub mod argument_descriptor;
90
91// Command buffer encoder info
92pub mod command_buffer_encoder_info;
93
94// Binary archive modules
95pub mod binary_archive;
96
97// Capture/debugging modules
98pub mod capture;
99
100// Counter/profiling modules
101pub mod counter;
102
103// Indirect command modules
104pub mod indirect;
105
106// Function stitching modules
107pub mod function_stitching;
108
109// Function table modules
110pub mod function_table;
111
112// Residency set modules
113pub mod residency_set;
114
115// Tensor modules
116pub mod tensor;
117
118// Drawable modules
119pub mod drawable;
120
121// Vertex descriptor modules
122pub mod vertex;
123
124// Stage input/output descriptor modules
125pub mod stage_input_output;
126
127// Rasterization rate modules
128pub mod rasterization_rate;
129
130// Log state modules
131pub mod log_state;
132
133// Function log modules
134pub mod function_log;
135
136// Texture view pool modules
137pub mod texture_view_pool;
138
139// Metal 4 modules
140pub mod mtl4;
141
142// Re-export commonly used types at crate root
143pub use enums::*;
144pub use error::ValidationError;
145pub use types::*;
146
147// Re-export Device and creation functions for convenience
148pub use device::{Architecture, Device};
149
150// Re-export resource types
151pub use buffer::Buffer;
152pub use heap::{Heap, HeapDescriptor};
153pub use texture::{SharedTextureHandle, Texture, TextureDescriptor, TextureViewDescriptor};
154
155// Re-export command types
156pub use command_buffer::{CommandBuffer, CommandBufferDescriptor};
157pub use command_queue::{CommandQueue, CommandQueueDescriptor};
158
159// Re-export state types
160pub use depth_stencil::{DepthStencilDescriptor, DepthStencilState, StencilDescriptor};
161pub use sampler::{SamplerDescriptor, SamplerState};
162
163// Re-export library types
164pub use library::{
165    Attribute, CompileOptions, DynamicLibrary, Function, FunctionConstant, FunctionConstantValues,
166    FunctionDescriptor, FunctionReflection, IntersectionFunctionDescriptor, Library,
167    LinkedFunctions, VertexAttribute,
168};
169
170// Re-export pipeline types
171pub use pipeline::{
172    ComputePipelineDescriptor, ComputePipelineState, LogicalToPhysicalColorAttachmentMap,
173    MeshRenderPipelineDescriptor, PipelineBufferDescriptor, PipelineBufferDescriptorArray,
174    RenderPipelineColorAttachmentDescriptor, RenderPipelineColorAttachmentDescriptorArray,
175    RenderPipelineDescriptor, RenderPipelineFunctionsDescriptor, RenderPipelineReflection,
176    RenderPipelineState, TileRenderPipelineColorAttachmentDescriptor,
177    TileRenderPipelineColorAttachmentDescriptorArray, TileRenderPipelineDescriptor,
178};
179
180// Re-export sync types
181pub use sync::{Event, Fence, SharedEvent, SharedEventHandle, SharedEventListener};
182
183// Re-export pass descriptor types
184pub use pass::{
185    BlitPassDescriptor, BlitPassSampleBufferAttachmentDescriptor,
186    BlitPassSampleBufferAttachmentDescriptorArray, ComputePassDescriptor,
187    ComputePassSampleBufferAttachmentDescriptor, ComputePassSampleBufferAttachmentDescriptorArray,
188    RenderPassColorAttachmentDescriptor, RenderPassColorAttachmentDescriptorArray,
189    RenderPassDepthAttachmentDescriptor, RenderPassDescriptor,
190    RenderPassSampleBufferAttachmentDescriptor, RenderPassSampleBufferAttachmentDescriptorArray,
191    RenderPassStencilAttachmentDescriptor, ResourceStatePassDescriptor,
192    ResourceStatePassSampleBufferAttachmentDescriptor,
193    ResourceStatePassSampleBufferAttachmentDescriptorArray,
194};
195
196// Re-export acceleration structure types
197pub use acceleration::{
198    AccelerationStructure, AccelerationStructureBoundingBoxGeometryDescriptor,
199    AccelerationStructureCommandEncoder, AccelerationStructureCurveGeometryDescriptor,
200    AccelerationStructureDescriptor, AccelerationStructureGeometryDescriptor,
201    AccelerationStructureMotionBoundingBoxGeometryDescriptor,
202    AccelerationStructureMotionCurveGeometryDescriptor,
203    AccelerationStructureMotionTriangleGeometryDescriptor, AccelerationStructurePassDescriptor,
204    AccelerationStructurePassSampleBufferAttachmentDescriptor,
205    AccelerationStructurePassSampleBufferAttachmentDescriptorArray, AccelerationStructureSizes,
206    AccelerationStructureTriangleGeometryDescriptor,
207    IndirectInstanceAccelerationStructureDescriptor, InstanceAccelerationStructureDescriptor,
208    MotionKeyframeData, PrimitiveAccelerationStructureDescriptor,
209};
210
211// Re-export encoder types
212pub use encoder::{
213    BlitCommandEncoder, ComputeCommandEncoder, DispatchThreadgroupsIndirectArguments,
214    DispatchThreadsIndirectArguments, MapIndirectArguments, ParallelRenderCommandEncoder,
215    RenderCommandEncoder, ResourceStateCommandEncoder, StageInRegionIndirectArguments,
216};
217
218// Re-export IO types
219pub use io::{
220    IOCommandBuffer, IOCommandQueue, IOCommandQueueDescriptor, IOCompressionContext, IOFileHandle,
221    IOScratchBuffer, IOScratchBufferAllocator, io_compression_context_append_data,
222    io_compression_context_default_chunk_size, io_create_compression_context,
223    io_flush_and_destroy_compression_context,
224};
225
226// Re-export argument types
227pub use argument::{
228    ATTRIBUTE_STRIDE_STATIC, Argument, ArgumentEncoder, ArrayType, Binding, BufferBinding,
229    ObjectPayloadBinding, PointerType, StructMember, StructType, TensorBinding,
230    TensorReferenceType, TextureBinding, TextureReferenceType, ThreadgroupBinding, Type,
231};
232pub use argument_descriptor::ArgumentDescriptor;
233
234// Re-export command buffer encoder info
235pub use command_buffer_encoder_info::CommandBufferEncoderInfo;
236
237// Re-export binary archive types
238pub use binary_archive::{BinaryArchive, BinaryArchiveDescriptor, BinaryArchiveError};
239
240// Re-export function stitching types
241pub use function_stitching::{
242    FunctionStitchingAttribute, FunctionStitchingAttributeAlwaysInline,
243    FunctionStitchingFunctionNode, FunctionStitchingGraph, FunctionStitchingInputNode,
244    FunctionStitchingNode, StitchedLibraryDescriptor, StitchedLibraryOptions,
245};
246
247// Re-export residency set types
248pub use residency_set::{ResidencySet, ResidencySetDescriptor};
249
250// Re-export tensor types
251pub use tensor::{Tensor, TensorDescriptor, TensorExtents};
252
253// Re-export drawable types
254pub use drawable::{Drawable, TimeInterval};
255
256// Re-export capture types
257pub use capture::{CaptureDescriptor, CaptureManager, CaptureScope};
258
259// Re-export counter types
260pub use counter::{
261    COUNTER_DONT_SAMPLE, COUNTER_ERROR_VALUE, Counter, CounterResultStageUtilization,
262    CounterResultStatistic, CounterResultTimestamp, CounterSampleBuffer,
263    CounterSampleBufferDescriptor, CounterSet,
264};
265
266// Re-export indirect command types
267pub use indirect::{
268    IndirectCommandBuffer, IndirectCommandBufferDescriptor, IndirectCommandBufferExecutionRange,
269    IndirectComputeCommand, IndirectRenderCommand,
270};
271
272// Re-export vertex descriptor types
273pub use vertex::{
274    BUFFER_LAYOUT_STRIDE_DYNAMIC, VertexAttributeDescriptor, VertexAttributeDescriptorArray,
275    VertexBufferLayoutDescriptor, VertexBufferLayoutDescriptorArray, VertexDescriptor,
276};
277
278// Re-export function table types
279pub use function_table::{
280    FunctionHandle, IntersectionFunctionBufferArguments, IntersectionFunctionTable,
281    IntersectionFunctionTableDescriptor, VisibleFunctionTable, VisibleFunctionTableDescriptor,
282};
283
284// Re-export rasterization rate types
285pub use rasterization_rate::{
286    RasterizationRateLayerArray, RasterizationRateLayerDescriptor, RasterizationRateMap,
287    RasterizationRateMapDescriptor, RasterizationRateSampleArray,
288};
289
290// Re-export log state types
291pub use log_state::{LogState, LogStateDescriptor};
292
293// Re-export function log types
294pub use function_log::{FunctionLog, FunctionLogDebugLocation, LogContainer};
295
296// Re-export texture view pool types
297pub use texture_view_pool::{ResourceViewPoolDescriptor, TextureViewPool};
298
299// Re-export foundation types for convenience
300pub use mtl_foundation::{Integer, UInteger};
301
302// Re-export protocol traits
303pub use allocation::Allocation;
304pub use resource::Resource;
305
306// Re-export stage input/output types
307pub use stage_input_output::{
308    AttributeDescriptor, AttributeDescriptorArray, BufferLayoutDescriptor,
309    BufferLayoutDescriptorArray, StageInputOutputDescriptor,
310};