#![doc = include_str!("../README.md")]
#![allow(
non_camel_case_types,
non_upper_case_globals,
non_snake_case,
clippy::missing_transmute_annotations
)]
#[cfg(test)]
mod tests;
pub mod buffer;
pub mod command;
pub mod device;
pub mod dispatch;
pub mod encoder;
pub mod ffi;
pub mod pipeline;
pub mod shader;
pub mod sync;
pub mod texture;
pub use buffer::Buffer;
pub use command::{Commands, Queue};
pub use device::Gpu;
pub use dispatch::{Batch, Dispatch, GpuFuture};
pub use encoder::{Copier, Encoder};
pub use pipeline::Pipeline;
pub use shader::{Shader, ShaderLib};
pub use sync::{Event, Fence, SharedEvent};
pub use texture::Texture;
pub use unimem::{Block, Layout, MemError, Tape};
pub use acpu::numeric::fp16::{f32_to_fp16, fp16_to_f32};
pub use acpu::{cast_f16_f32, cast_f32_f16};
#[inline]
pub fn autorelease_pool<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
struct PoolGuard(*mut std::ffi::c_void);
impl Drop for PoolGuard {
#[mutants::skip] fn drop(&mut self) {
unsafe { ffi::objc_autoreleasePoolPop(self.0) }
}
}
let pool = unsafe { ffi::objc_autoreleasePoolPush() };
let _guard = PoolGuard(pool);
f()
}
#[derive(Debug)]
#[non_exhaustive]
pub enum GpuError {
DeviceNotFound,
BufferCreationFailed(String),
LibraryCompilationFailed(String),
FunctionNotFound(String),
PipelineCreationFailed(String),
CommandBufferError(String),
EncoderCreationFailed,
QueueCreationFailed,
TextureCreationFailed(String),
Io(std::io::Error),
}
impl std::fmt::Display for GpuError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GpuError::DeviceNotFound => write!(f, "No Metal device found"),
GpuError::BufferCreationFailed(msg) => write!(f, "Buffer creation failed: {}", msg),
GpuError::LibraryCompilationFailed(msg) => {
write!(f, "Shader compilation failed: {}", msg)
}
GpuError::FunctionNotFound(name) => write!(f, "Function not found: {}", name),
GpuError::PipelineCreationFailed(msg) => {
write!(f, "Pipeline creation failed: {}", msg)
}
GpuError::CommandBufferError(msg) => write!(f, "Command buffer error: {}", msg),
GpuError::EncoderCreationFailed => write!(f, "Encoder creation failed"),
GpuError::QueueCreationFailed => write!(f, "Command queue creation failed"),
GpuError::TextureCreationFailed(msg) => {
write!(f, "Texture creation failed: {}", msg)
}
GpuError::Io(e) => write!(f, "IO error: {}", e),
}
}
}
impl std::error::Error for GpuError {}
impl From<std::io::Error> for GpuError {
fn from(e: std::io::Error) -> Self {
GpuError::Io(e)
}
}