Skip to main content

edgefirst_image/gl/
mod.rs

1// SPDX-FileCopyrightText: Copyright 2025 Au-Zone Technologies
2// SPDX-License-Identifier: Apache-2.0
3
4#![cfg(any(target_os = "linux", target_os = "macos"))]
5#![cfg(feature = "opengl")]
6// Several types defined at the `gl` module root (EglDisplayKind,
7// TransferBackend, RegionOfInterest, etc.) are consumed only by the
8// Linux-only inner modules (`context`, `processor`, ...). The macOS
9// path uses its own `MacosGlProcessor` + `iosurface_import` modules and
10// does not touch every shared type, so some appear unused on macOS.
11// Rather than fragmenting the type definitions per platform, suppress
12// the dead-code lint on non-Linux targets.
13#![cfg_attr(not(target_os = "linux"), allow(dead_code))]
14
15// Module layout:
16//   - `platform/` — cross-platform display/EGL-loader seam (both OSes)
17//   - Linux-only:  `context`, `processor`, `threaded`, `dma_import`,
18//                  `cache`, `resources`, `shaders`, `tests`
19//   - macOS-only:  `iosurface_import`, `macos_processor`
20// The macOS processor is parallel to (not a refactor of) the Linux
21// threaded processor — see `crates/image/ARCHITECTURE.md` for the
22// rationale and the planned convergence story.
23
24macro_rules! function {
25    () => {{
26        fn f() {}
27        fn type_name_of<T>(_: T) -> &'static str {
28            std::any::type_name::<T>()
29        }
30        let name = type_name_of(f);
31
32        // Find and cut the rest of the path
33        match &name[..name.len() - 3].rfind(':') {
34            Some(pos) => &name[pos + 1..name.len() - 3],
35            None => &name[..name.len() - 3],
36        }
37    }};
38}
39
40mod cache;
41#[cfg(target_os = "linux")]
42mod context;
43#[cfg(target_os = "linux")]
44mod dma_import;
45// Cfg-agnostic: the float render-path classifier is the single source of
46// truth for the "(PixelFormat, DType, TensorMemory) → float path" decision and
47// is compiled on every platform so both the Linux and macOS backends share one
48// definition (see `crates/image/ARCHITECTURE.md`, review item #4).
49// Portable renderer helpers (crop uniforms, …) shared by both platform
50// backends. No gbm/IOSurface types; compiled on both.
51mod core;
52mod float_dispatch;
53// Pure decision table for the proto-segmentation render path (upload
54// strategy × program × count uniform). No GL types; host-tested
55// exhaustively. See `proto_dispatch.rs`.
56mod proto_dispatch;
57// Portable GL render lowering (y-flip viewport, source UV, batch chunk planner).
58// No platform types; compiled on both platforms, consumed by the converged
59// tile/batch renderer. See `render.rs`.
60mod render;
61// PixelFormat -> DRM FourCC mapping via the portable `drm_fourcc` crate (NOT
62// `gbm`). DRM FourCC is a Linux/DMA-BUF concept, so this lives with the other
63// Linux graphics modules; the point is that it carries no `gbm` coupling, so
64// `shaders.rs` and the format code no longer pull in `gbm`.
65#[cfg(target_os = "linux")]
66mod fourcc;
67#[cfg(target_os = "macos")]
68mod iosurface_import;
69mod platform;
70mod processor;
71mod resources;
72mod shaders;
73mod shaders_common;
74// Engine GL tests, in three tiers (see crates/image/TESTING.md):
75//   portable                       — run on Linux AND macOS/ANGLE
76//   cfg(target_os = "linux")       — display probing, PBO/CUDA paths
77//   cfg(all(linux, dma_test_formats)) — DMA-BUF import/pool specifics
78// Per-item cfg gates inside the module select the tier; the mount
79// itself is unconditional so the macOS lane runs the portable tier.
80mod tests;
81mod threaded;
82
83#[cfg(target_os = "linux")]
84pub use context::probe_egl_displays;
85// These are accessed by sibling sub-modules via `super::context::` directly.
86// No re-export needed at the mod.rs level.
87pub use cache::{CacheStats, GlCacheStats};
88pub use threaded::GLProcessorThreaded;
89
90/// Dynamically-loaded EGL 1.4 instance. The lifetime parameter is
91/// `'static` because the underlying `libloading::Library` is intentionally
92/// leaked at first load (see `EGL_LIB` in `context.rs` and the equivalent
93/// on macOS — drivers may retain internal state past explicit cleanup, so
94/// dlclose can SIGBUS on process exit).
95///
96/// Defined here at the `gl` module root so the `platform/` trait and both
97/// platform implementations can name it without dragging in a cross-cfg
98/// re-export. The Linux `context.rs` and the macOS `platform/macos.rs`
99/// both use this same alias.
100pub(super) type Egl = edgefirst_egl::Instance<
101    edgefirst_egl::Dynamic<&'static libloading::Library, edgefirst_egl::EGL1_4>,
102>;
103
104/// Identifies the type of EGL display used for headless OpenGL ES rendering.
105///
106/// The HAL creates a surfaceless GLES 3.0 context
107/// (`EGL_KHR_surfaceless_context` + `EGL_KHR_no_config_context`) and
108/// renders exclusively through FBOs backed by EGLImages imported from
109/// DMA-buf file descriptors. No window or PBuffer surface is created.
110///
111/// Displays are probed in priority order: PlatformDevice first (zero
112/// external dependencies), then GBM, then Default. Use
113/// [`probe_egl_displays`] to discover which are available and
114/// [`ImageProcessorConfig::egl_display`](crate::ImageProcessorConfig::egl_display)
115/// to override the auto-detection.
116///
117/// # Display Types
118///
119/// - **`PlatformDevice`** — Uses `EGL_EXT_device_enumeration` to query
120///   available EGL devices via `eglQueryDevicesEXT`, then selects the first
121///   device with `eglGetPlatformDisplay(EGL_EXT_platform_device, ...)`.
122///   Headless and compositor-free with zero external library dependencies.
123///   Works on NVIDIA GPUs and newer Vivante drivers.
124///
125/// - **`Gbm`** — Opens a DRM render node (e.g. `/dev/dri/renderD128`) and
126///   creates a GBM (Generic Buffer Manager) device, then calls
127///   `eglGetPlatformDisplay(EGL_PLATFORM_GBM_KHR, gbm_device)`. Requires
128///   `libgbm` and a DRM render node. Needed on ARM Mali (i.MX95) and older
129///   Vivante drivers that do not expose `EGL_EXT_platform_device`.
130///
131/// - **`Default`** — Calls `eglGetDisplay(EGL_DEFAULT_DISPLAY)`, letting the
132///   EGL implementation choose the display. On Wayland systems this connects
133///   to the compositor; on X11 it connects to the X server. May block on
134///   headless systems where a compositor is expected but not running.
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
136pub enum EglDisplayKind {
137    Gbm,
138    PlatformDevice,
139    Default,
140}
141
142impl std::fmt::Display for EglDisplayKind {
143    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144        match self {
145            EglDisplayKind::Gbm => write!(f, "GBM"),
146            EglDisplayKind::PlatformDevice => write!(f, "PlatformDevice"),
147            EglDisplayKind::Default => write!(f, "Default"),
148        }
149    }
150}
151
152/// A validated, available EGL display discovered by [`probe_egl_displays`].
153#[derive(Debug, Clone)]
154pub struct EglDisplayInfo {
155    /// The type of EGL display.
156    pub kind: EglDisplayKind,
157    /// Human-readable description for logging/diagnostics
158    /// (e.g. "GBM via /dev/dri/renderD128").
159    pub description: String,
160}
161
162/// Tracks which data-transfer method is active for moving pixels
163/// between CPU memory and GPU textures/framebuffers.
164#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165pub(crate) enum TransferBackend {
166    /// Zero-copy via EGLImage imported from DMA-buf file descriptors.
167    /// Available on i.MX8 (Vivante), i.MX95 (Mali), Jetson, and any
168    /// platform where `EGL_EXT_image_dma_buf_import` is present AND
169    /// the GPU can actually render through DMA-buf-backed textures.
170    DmaBuf,
171
172    /// Zero-copy via `EGL_ANGLE_iosurface_client_buffer` (macOS).
173    /// Available when ANGLE's Metal backend is loaded and the EGL
174    /// extension is advertised. The IOSurface is wrapped as an EGL
175    /// pbuffer and bound to a 2D texture via `eglBindTexImage`.
176    #[cfg(target_os = "macos")]
177    IOSurface,
178
179    /// GPU buffer via Pixel Buffer Object. Used when DMA-buf is unavailable
180    /// but OpenGL is present. Data stays in GPU-accessible memory.
181    Pbo,
182
183    /// Synchronous `glTexSubImage2D` upload + `glReadPixels` readback.
184    /// Used when DMA-buf is unavailable or when the DMA-buf verification
185    /// probe fails (e.g. NVIDIA discrete GPUs where EGLImage creation
186    /// succeeds but rendered data is all zeros).
187    Sync,
188}
189
190impl TransferBackend {
191    /// Returns `true` if DMA-buf zero-copy is available.
192    pub(crate) fn is_dma(self) -> bool {
193        self == TransferBackend::DmaBuf
194    }
195
196    /// Returns `true` if the platform can import `TensorMemory::Dma`
197    /// tensors zero-copy: DMA-BUF EGLImages on Linux, IOSurface pbuffers
198    /// on macOS. Path-selection sites use this; probes that are
199    /// specifically about DMA-BUF semantics (e.g. the render-roundtrip
200    /// verification) keep `is_dma`.
201    pub(crate) fn is_zero_copy(self) -> bool {
202        #[cfg(target_os = "macos")]
203        if self == TransferBackend::IOSurface {
204            return true;
205        }
206        self == TransferBackend::DmaBuf
207    }
208}
209
210/// Interpolation mode for int8 proto textures (GL_R8I cannot use GL_LINEAR).
211#[derive(Debug, Clone, Copy, PartialEq, Eq)]
212pub enum Int8InterpolationMode {
213    /// texelFetch at nearest texel — simplest, fastest GPU execution.
214    Nearest,
215    /// texelFetch × 4 neighbors with shader-computed bilinear weights (default).
216    Bilinear,
217    /// Two-pass: dequant int8→f16 FBO, then existing f16 shader with GL_LINEAR.
218    TwoPass,
219}
220
221/// A rectangular region of interest expressed as normalised [0, 1] coordinates.
222#[derive(Debug, Clone, Copy)]
223pub(super) struct RegionOfInterest {
224    pub(super) left: f32,
225    pub(super) top: f32,
226    pub(super) right: f32,
227    pub(super) bottom: f32,
228}
229
230impl RegionOfInterest {
231    /// Build a source ROI from a pixel-space crop rectangle with a half-texel
232    /// inset. The inset ensures that `GL_LINEAR` filtering never samples
233    /// outside the crop boundary — at the extreme texture coordinates the
234    /// bilinear kernel is centred on the boundary texel and cannot reach
235    /// adjacent padding pixels.
236    ///
237    /// The result is clamped to [0, 1] so an out-of-bounds crop rectangle
238    /// cannot produce invalid texture coordinates.
239    ///
240    /// `crop`: pixel-space rectangle (left, top, width, height).
241    /// `tex_w`, `tex_h`: full texture dimensions in pixels.
242    pub(super) fn from_crop_clamped(crop: &crate::Rect, tex_w: usize, tex_h: usize) -> Self {
243        let half_x = 0.5 / tex_w as f32;
244        let half_y = 0.5 / tex_h as f32;
245        RegionOfInterest {
246            left: (crop.left as f32 / tex_w as f32 + half_x).clamp(0.0, 1.0),
247            top: ((crop.top + crop.height) as f32 / tex_h as f32 - half_y).clamp(0.0, 1.0),
248            right: ((crop.left + crop.width) as f32 / tex_w as f32 - half_x).clamp(0.0, 1.0),
249            bottom: (crop.top as f32 / tex_h as f32 + half_y).clamp(0.0, 1.0),
250        }
251    }
252}