A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
// Vertex shader
struct VertexInput {
@location(0) position: vec2<f32>,
@location(1) tex: vec2<f32>,
@location(2) fg_color: vec4<f32>,
@location(3) alt_color: vec4<f32>,
@location(4) hsv: vec3<f32>,
@location(5) has_color: f32,
@location(6) mix_value: f32,
};
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex: vec2<f32>,
@location(1) fg_color: vec4<f32>,
// Flat, not interpolated: `apply_hsv` compares this against identity to
// skip the round trip, and `set_hsv` writes the same value to all four
// corners of a quad. Interpolating a constant is exact in practice, but
// flat makes the comparison correct by construction rather than by
// argument about attribute gradients.
@location(2) @interpolate(flat) hsv: vec3<f32>,
@location(3) has_color: f32,
};
// a regular monochrome text glyph
const IS_GLYPH: f32 = 0.0;
// a color emoji glyph
const IS_COLOR_EMOJI: f32 = 1.0;
// a full color texture attached as the
// background image of the window
const IS_BG_IMAGE: f32 = 2.0;
// like 2.0, except that instead of an
// image, we use the solid bg color
const IS_SOLID_COLOR: f32 = 3.0;
// Grayscale poly quad for non-aa text render layers
const IS_GRAY_SCALE: f32 = 4.0;
struct ShaderUniform {
foreground_text_hsb: vec3<f32>,
milliseconds: u32,
viewport_and_corner: vec4<f32>,
window_border: vec4<f32>,
projection: mat4x4<f32>,
};
@group(0) @binding(0) var<uniform> uniforms: ShaderUniform;
@group(1) @binding(0) var atlas_linear_tex: texture_2d<f32>;
@group(1) @binding(1) var atlas_linear_sampler: sampler;
@group(2) @binding(0) var atlas_nearest_tex: texture_2d<f32>;
@group(2) @binding(1) var atlas_nearest_sampler: sampler;
fn rgb2hsv(c: vec3<f32>) -> vec3<f32>
{
let K = vec4<f32>(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
let p = mix(vec4<f32>(c.bg, K.wz), vec4<f32>(c.gb, K.xy), step(c.b, c.g));
let q = mix(vec4<f32>(p.xyw, c.r), vec4<f32>(c.r, p.yzx), step(p.x, c.r));
let d = q.x - min(q.w, q.y);
let e = 1.0e-10;
return vec3<f32>(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
fn hsv2rgb(c: vec3<f32>) -> vec3<f32>
{
let K = vec4<f32>(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
let p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, vec3(0.0), vec3(1.0)), c.y);
}
fn apply_hsv(c: vec4<f32>, transform: vec3<f32>) -> vec4<f32>
{
// The default transform is exactly (1,1,1) -- `foreground_text_hsb` defaults
// to 1.0 on all three axes and `set_hsv(None)` writes a literal (1,1,1) --
// so without this guard every fragment of a default-configured terminal pays
// for a full rgb2hsv/hsv2rgb round trip that returns (very nearly) its input.
// The GLSL renderer has had the same short-circuit all along; this brings the
// two into agreement. Note the round trip is itself lossy (the epsilons in
// rgb2hsv, the fract/clamp in hsv2rgb), so returning `c` untouched is not
// bit-identical to the old output -- it is about one f32 ULP closer to right.
if all(transform == vec3<f32>(1.0)) {
return c;
}
let hsv = rgb2hsv(c.rgb) * transform;
return vec4<f32>(hsv2rgb(hsv).rgb, c.a);
}
fn rounded_window_coverage(position: vec2<f32>) -> f32 {
let viewport = uniforms.viewport_and_corner.xy;
let radius = uniforms.viewport_and_corner.z;
if radius <= 0.0 {
return 1.0;
}
let in_corner_x = position.x < radius || position.x > viewport.x - radius;
let in_corner_y = position.y < radius || position.y > viewport.y - radius;
if !in_corner_x || !in_corner_y {
return 1.0;
}
let center = vec2<f32>(
select(radius, viewport.x - radius, position.x > viewport.x * 0.5),
select(radius, viewport.y - radius, position.y > viewport.y * 0.5),
);
let feather = 1.0;
return 1.0 - smoothstep(radius - feather, radius, distance(position, center));
}
fn rounded_window_signed_distance(position: vec2<f32>) -> f32 {
let viewport = uniforms.viewport_and_corner.xy;
let half_size = viewport * 0.5;
let radius = min(
max(uniforms.viewport_and_corner.z, 0.0),
min(half_size.x, half_size.y),
);
let q = abs(position - half_size) - (half_size - vec2<f32>(radius));
return length(max(q, vec2<f32>(0.0))) + min(max(q.x, q.y), 0.0) - radius;
}
fn rounded_window_border_coverage(position: vec2<f32>) -> f32 {
let width = uniforms.window_border.w;
if width <= 0.0 {
return 0.0;
}
let signed_distance = rounded_window_signed_distance(position);
return smoothstep(
-width - 0.5,
-width + 0.5,
signed_distance,
);
}
@vertex
fn vs_main(
model: VertexInput,
) -> VertexOutput {
var out: VertexOutput;
out.tex = model.tex;
out.hsv = model.hsv;
out.has_color = model.has_color;
out.fg_color = mix(model.fg_color, model.alt_color, model.mix_value);
out.clip_position = uniforms.projection * vec4<f32>(model.position, 0.0, 1.0);
return out;
}
// Fragment shader
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
var color: vec4<f32>;
var linear_tex: vec4<f32> = textureSample(atlas_linear_tex, atlas_linear_sampler, in.tex);
var nearest_tex: vec4<f32> = textureSample(atlas_nearest_tex, atlas_nearest_sampler, in.tex);
// How far this quad is minified, in atlas texels per screen pixel.
//
// Glyphs are rasterised at the size they are meant to be drawn and sampled
// nearest, which is correct at 1:1 and is what keeps terminal text crisp.
// A quad that is being shrunk -- a terminal travelling into an overview
// card -- breaks that assumption: nearest minification samples one texel in
// every N, so strokes drop out entirely and the text shimmers as the scale
// passes through. Both samples are taken anyway and the two bind groups are
// the same atlas texture, so blending toward the linear one costs a mix and
// leaves everything drawn at its rasterised size untouched.
//
// Derivatives must be taken in uniform control flow, hence up here rather
// than inside the branches below.
let atlas_size = vec2<f32>(textureDimensions(atlas_nearest_tex));
let footprint = max(
length(dpdx(in.tex) * atlas_size),
length(dpdy(in.tex) * atlas_size),
);
// Nearest until the quad is unambiguously being shrunk, then ramp to linear.
//
// The dead zone matters more than the ramp. A glyph drawn at the size it was
// rasterised does not land on exactly one texel per pixel -- cell metrics are
// integer device pixels and the projection carries float error -- so a
// threshold at 1.0 leaks a few percent of linear into every character on
// screen and softens the whole terminal. Nothing below 1.25 is minified
// enough to be worth filtering.
let minified = smoothstep(1.25, 2.0, footprint);
var hsv = in.hsv;
if in.has_color == IS_SOLID_COLOR {
// Solid color block
color = in.fg_color;
} else if in.has_color == IS_BG_IMAGE {
// Window background attachment
// Apply window_background_image_opacity to the background image
color = linear_tex;
color.a *= in.fg_color.a;
} else if in.has_color == IS_COLOR_EMOJI {
// the texture is full color info (eg: color emoji glyph)
color = mix(nearest_tex, linear_tex, minified);
// The vertex alpha still applies, so that a surface being composited at
// partial opacity carries its emoji with it.
color.a *= in.fg_color.a;
} else if in.has_color == IS_GRAY_SCALE {
// Grayscale poly quad for non-aa text render layers
color = in.fg_color;
color.a *= mix(nearest_tex.a, linear_tex.a, minified);
} else if in.has_color == IS_GLYPH {
// the texture is the alpha channel/color mask
// and we need to tint with the fg_color
color = in.fg_color;
// Multiply rather than replace: the glyph mask decides the shape, the
// vertex colour decides how present the whole thing is. Replacing it
// discarded any opacity the caller asked for, which meant terminal text
// could not take part in a surface being composited at partial opacity --
// it stayed solid over everything else as it faded.
color.a = mix(nearest_tex.a, linear_tex.a, minified) * in.fg_color.a;
hsv *= uniforms.foreground_text_hsb;
}
color = apply_hsv(color, hsv);
let border_coverage = rounded_window_border_coverage(in.clip_position.xy);
color = mix(
color,
vec4<f32>(uniforms.window_border.rgb, 1.0),
border_coverage,
);
let coverage = rounded_window_coverage(in.clip_position.xy);
if coverage <= 0.0 {
discard;
}
color.a *= coverage;
return color;
}