Skip to main content

ultrafast_gpu/
lib.rs

1//! Thin native GPU layer for ultrafast.
2//!
3//! This is deliberately not a general graphics abstraction. It exposes the
4//! handful of concepts the renderer needs (textures, compute kernels, command
5//! lists) with static dispatch to one native backend per platform, and it
6//! counts every CPU<->GPU transfer so copies are always visible.
7//!
8//! Backends: Metal (macOS). Vulkan + CUDA interop (Linux, NVIDIA L4) is next.
9
10// Engine code: wide kernel and pass signatures and indexed loops over
11// parallel arrays are the clearest form here.
12#![allow(clippy::too_many_arguments, clippy::type_complexity, clippy::needless_range_loop)]
13
14mod stats;
15pub use stats::TransferStats;
16
17#[cfg(backend_metal)]
18pub mod metal;
19#[cfg(backend_metal)]
20mod surface;
21pub mod vulkan;
22
23#[cfg(backend_metal)]
24pub use metal::{
25    Bindings, Buffer, BufferBinding, CommandList, Device, Draw, DrawPipeline, DrawTarget, Kernel, Library, Submitted,
26    Texture,
27};
28#[cfg(backend_metal)]
29pub use surface::{Drawable, Surface};
30#[cfg(backend_vulkan)]
31pub use vulkan::ExportedMemory;
32#[cfg(backend_vulkan)]
33pub use vulkan::{
34    Bindings, Buffer, BufferBinding, CommandList, Device, Draw, DrawPipeline, DrawTarget, Kernel, Library, Submitted,
35    Texture,
36};
37#[cfg(all(backend_vulkan, windows))]
38pub use vulkan::{Drawable, Surface};
39
40/// How a draw pipeline combines fragments with the target.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum Blend {
43    /// Replace (3D meshes with depth).
44    Opaque,
45    /// Premultiplied source-over.
46    Over,
47    /// Additive (sparks, light).
48    Add,
49}
50
51/// The backend this build renders with: "metal" or "vulkan".
52#[cfg(backend_metal)]
53pub const BACKEND: &str = "metal";
54#[cfg(backend_vulkan)]
55pub const BACKEND: &str = "vulkan";
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
58pub enum PixelFormat {
59    /// Linear premultiplied working format.
60    Rgba16Float,
61    /// 8-bit export / image format.
62    Rgba8Unorm,
63    /// 8-bit BGRA, the native layout of video frames and display surfaces.
64    Bgra8Unorm,
65    /// Single channel float (coverage accumulators, depth-like data).
66    R32Float,
67    /// Single channel half (masks, coverage).
68    R16Float,
69    /// Full-precision RGBA (simulation state).
70    Rgba32Float,
71    /// Depth attachment for 3D passes.
72    Depth32Float,
73}
74
75impl PixelFormat {
76    pub fn bytes_per_pixel(self) -> usize {
77        match self {
78            PixelFormat::Rgba16Float => 8,
79            PixelFormat::Rgba8Unorm | PixelFormat::Bgra8Unorm | PixelFormat::R32Float | PixelFormat::Depth32Float => 4,
80            PixelFormat::R16Float => 2,
81            PixelFormat::Rgba32Float => 16,
82        }
83    }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
87pub struct TextureDesc {
88    pub width: u32,
89    pub height: u32,
90    pub format: PixelFormat,
91    /// Whether the CPU will read this texture back. Readable textures live in
92    /// shared memory; everything else is GPU-private.
93    pub cpu_readable: bool,
94}
95
96/// GPU execution time of one submitted command list, in milliseconds.
97#[derive(Debug, Clone, Copy, Default)]
98pub struct GpuTiming {
99    pub gpu_ms: f64,
100}
101
102#[derive(Debug, thiserror::Error)]
103pub enum GpuError {
104    #[error("no GPU device available")]
105    NoDevice,
106    #[error("shader compile failed: {0}")]
107    ShaderCompile(String),
108    #[error("kernel `{0}` not found")]
109    NoSuchKernel(String),
110    #[error("pipeline creation failed for `{0}`: {1}")]
111    Pipeline(String, String),
112    #[error("command buffer failed: {0}")]
113    Execution(String),
114}