Files
ghostty-rs/crates/ghostty-renderer/shaders/glsl/cell_text.f.glsl
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

101 lines
3.7 KiB
GLSL

#include "common.glsl"
layout(binding = 0) uniform sampler2DRect atlas_grayscale;
layout(binding = 1) uniform sampler2DRect atlas_color;
in CellTextVertexOut {
flat uint atlas;
flat vec4 color;
flat vec4 bg_color;
vec2 tex_coord;
} in_data;
// Values `atlas` can take.
const uint ATLAS_GRAYSCALE = 0u;
const uint ATLAS_COLOR = 1u;
// Must declare this output for some versions of OpenGL.
layout(location = 0) out vec4 out_FragColor;
void main() {
bool use_linear_blending = (bools & USE_LINEAR_BLENDING) != 0;
bool use_linear_correction = (bools & USE_LINEAR_CORRECTION) != 0;
switch (in_data.atlas) {
default:
case ATLAS_GRAYSCALE:
{
// Our input color is always linear.
vec4 color = in_data.color;
// If we're not doing linear blending, then we need to
// re-apply the gamma encoding to our color manually.
//
// Since the alpha is premultiplied, we need to divide
// it out before unlinearizing and re-multiply it after.
if (!use_linear_blending) {
color.rgb /= vec3(color.a);
color = unlinearize(color);
color.rgb *= vec3(color.a);
}
// Fetch our alpha mask for this pixel.
float a = texture(atlas_grayscale, in_data.tex_coord).r;
// Linear blending weight correction corrects the alpha value to
// produce blending results which match gamma-incorrect blending.
if (use_linear_correction) {
// Short explanation of how this works:
//
// We get the luminances of the foreground and background colors,
// and then unlinearize them and perform blending on them. This
// gives us our desired luminance, which we derive our new alpha
// value from by mapping the range [bg_l, fg_l] to [0, 1], since
// our final blend will be a linear interpolation from bg to fg.
//
// This yields virtually identical results for grayscale blending,
// and very similar but non-identical results for color blending.
vec4 bg = in_data.bg_color;
float fg_l = luminance(color.rgb);
float bg_l = luminance(bg.rgb);
// To avoid numbers going haywire, we don't apply correction
// when the bg and fg luminances are within 0.001 of each other.
if (abs(fg_l - bg_l) > 0.001) {
float blend_l = linearize(unlinearize(fg_l) * a + unlinearize(bg_l) * (1.0 - a));
a = clamp((blend_l - bg_l) / (fg_l - bg_l), 0.0, 1.0);
}
}
// Multiply our whole color by the alpha mask.
// Since we use premultiplied alpha, this is
// the correct way to apply the mask.
color *= a;
out_FragColor = color;
return;
}
case ATLAS_COLOR:
{
// For now, we assume that color glyphs
// are already premultiplied linear colors.
vec4 color = texture(atlas_color, in_data.tex_coord);
// If we are doing linear blending, we can return this right away.
if (use_linear_blending) {
out_FragColor = color;
return;
}
// Otherwise we need to unlinearize the color. Since the alpha is
// premultiplied, we need to divide it out before unlinearizing.
color.rgb /= vec3(color.a);
color = unlinearize(color);
color.rgb *= vec3(color.a);
out_FragColor = color;
return;
}
}
}