Files
ghostty-rs/crates/ghostty-renderer/src/math.rs
T
ddidderr 568ce784f9 feat(renderer): port the OpenGL backend, shaders and size math
Ports the terminal-independent half of ZIG/src/renderer (OpenGL only):
- opengl/*: buffers, frames, pipelines, render passes, samplers,
  targets, textures and the OpenGL GraphicsAPI backend on top of glow;
  the GTK GLArea glue is replaced by a get_proc_address loader closure
- the GLSL shaders, copied verbatim and embedded; uniform/vertex structs
  as bytemuck Pod types with std140 offsets asserted in tests
- shadertoy custom shaders converted with naga (GLSL in -> validate ->
  GLSL 430 out) instead of glslang + spirv-cross
- size (screen/grid/cell/padding math), cursor style logic, backend
  health, options and message types

The crate opts into unsafe_code = "deny"; unsafe glow calls live only in
opengl/gl.rs. A headless EGL integration test builds every pipeline and
the converted shadertoy shaders on Mesa and reads back rendered pixels.

Note: Zig's shadertoy Uniforms order the selection background/foreground
colors opposite to the GLSL block (likely an upstream bug); kept as in
Zig for parity and recorded in the notes.

Deferred to the next renderer pass: generic, cell, image, link, row,
State, Thread, Overlay (need terminal render state).

Test Plan:
- cargo test -p ghostty-renderer (33 tests incl. tests/egl_headless.rs)
2026-10-11 22:26:09 +02:00

35 lines
951 B
Rust

//! Math helpers (`src/math.zig`), only what the renderer needs.
/// A 4x4 matrix as four column vectors (same layout as Zig's `[4]@Vector(4, f32)`).
pub type Mat = [[f32; 4]; 4];
/// 2D orthographic projection matrix
pub fn ortho2d(left: f32, right: f32, bottom: f32, top: f32) -> Mat {
let w = right - left;
let h = top - bottom;
[
[2.0 / w, 0.0, 0.0, 0.0],
[0.0, 2.0 / h, 0.0, 0.0],
[0.0, 0.0, -1.0, 0.0],
[-(right + left) / w, -(top + bottom) / h, 0.0, 1.0],
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ortho2d_maps_corners() {
let m = ortho2d(0.0, 800.0, 600.0, 0.0);
let apply = |x: f32, y: f32| {
(
m[0][0] * x + m[1][0] * y + m[3][0],
m[0][1] * x + m[1][1] * y + m[3][1],
)
};
assert_eq!(apply(0.0, 0.0), (-1.0, 1.0));
assert_eq!(apply(800.0, 600.0), (1.0, -1.0));
}
}