Skip to main content

ironaccelerator_opengl/
lib.rs

1//! # `ironaccelerator-opengl`
2//!
3//! OpenGL 4.3+ compute shader backend for IronAccelerator. Used as a
4//! legacy / embedded-GPU fallback when Vulkan, Metal, or WebGPU aren't
5//! available (e.g. older Linux Mesa stacks, integrated Intel GPUs on
6//! locked-down kernels).
7//!
8//! OpenGL has no standalone physical-device enumeration — a GL context must
9//! already exist on the calling thread. The host supplies a loader closure
10//! via [`bind_current_context`]; after that, `OpenGlBackend::enumerate`
11//! reports the single device bound to the current context.
12//!
13//! Once bound, the compute path is buffer → program → dispatch → readback:
14//!
15//! ```no_run
16//! use ironaccelerator_opengl::{bind_current_context, dispatch, gl, Program, Ssbo};
17//!
18//! // `loader` resolves GL symbols — e.g. `glfw`/`winit`'s `get_proc_address`.
19//! # fn loader(_: &str) -> *const core::ffi::c_void { core::ptr::null() }
20//! unsafe { bind_current_context(loader) };
21//! let gl = gl().expect("a GL 4.3+ context is current");
22//!
23//! let src: Vec<u8> = (0..256u32).flat_map(|i| (i as f32).to_le_bytes()).collect();
24//! let ssbo = Ssbo::with_data(gl, &src).unwrap();
25//! ssbo.bind(gl, 0);
26//!
27//! let program = Program::from_glsl(gl, r"#version 430
28//!     layout(local_size_x = 64) in;
29//!     layout(std430, binding = 0) buffer Data { float v[]; };
30//!     void main() { v[gl_GlobalInvocationID.x] *= 2.0; }").unwrap();
31//! dispatch(gl, &program, [256 / 64, 1, 1]);
32//!
33//! let mut out = vec![0u8; src.len()];
34//! ssbo.read_bytes(gl, &mut out);
35//! ```
36
37#![warn(missing_docs)]
38#![allow(clippy::missing_safety_doc)]
39
40pub mod backend;
41pub mod compute;
42pub mod drv;
43
44pub use backend::{OpenGlBackend, OPENGL_BACKEND};
45pub use compute::{dispatch, gl, GlDevice, Program, Ssbo};
46pub use drv::{bind_current_context, info, GlInfo};
47
48/// Register the OpenGL backend. Call [`bind_current_context`] first if you
49/// want the backend to report a live device.
50pub fn register(reg: &mut ironaccelerator_core::BackendRegistry) {
51    reg.register(&OPENGL_BACKEND);
52}