summaryrefslogtreecommitdiff
path: root/shaders/post
diff options
context:
space:
mode:
Diffstat (limited to 'shaders/post')
-rw-r--r--shaders/post/crt.glsl83
-rw-r--r--shaders/post/prefix.glsl74
2 files changed, 157 insertions, 0 deletions
diff --git a/shaders/post/crt.glsl b/shaders/post/crt.glsl
new file mode 100644
index 00000000..768157ef
--- /dev/null
+++ b/shaders/post/crt.glsl
@@ -0,0 +1,83 @@
+// Crt: a slot-mask tube's light, with nothing that moves a pixel. There is
+// no barrel: every click lands where it is drawn, rails and grips at the
+// window's edges included. What is left is light: a beam profile for the
+// scanlines, an aperture mask, a soft bloom of the brightest ink, a faint
+// vignette and the hum of a live tube. Everything is worked out in linear
+// light and dithered back into 8 bits, so the vignette never bands. It is a
+// Shadertoy file: pardes puts its prefix before it (shaders/post/prefix.glsl).
+
+const float TAU = 6.28318530718;
+// Level 3 glows wider and from dimmer ink.
+float bloomRadiusPx() { return pardesByLevel(1.75, 1.75, 3.0); }
+float bloomThreshold() { return pardesByLevel(0.6, 0.6, 0.3); } // linear-light channel peak
+const float BLOOM_SOFTNESS = 0.25;
+const float SCAN_PERIOD_PX = 3.0;
+const float MASK_PERIOD_PX = 3.0;
+const float HUM_HZ = 0.11;
+
+// Each strength at levels 1, 2 and 3 (`Crt 1`..`Crt 3`): 2 is the default,
+// 3 as strong as the old barrel CRT looked.
+float chromaPx() { return pardesByLevel(0.2, 0.35, 2.2); } // red and blue apart, at the corners
+float bloomStrength() { return pardesByLevel(0.03, 0.05, 0.3); }
+float scanStrength() { return pardesByLevel(0.035, 0.07, 0.42); }
+float maskStrength() { return pardesByLevel(0.01, 0.018, 0.12); }
+float vignetteStrength() { return pardesByLevel(0.06, 0.12, 0.42); } // at the far corners only
+float humStrength() { return pardesByLevel(0.002, 0.004, 0.012); }
+float exposure() { return pardesByLevel(1.008, 1.015, 1.1); }
+
+vec3 toLinear(vec3 c) {
+ return mix(pow((c + 0.055) / 1.055, vec3(2.4)), c / 12.92, lessThanEqual(c, vec3(0.04045)));
+}
+
+vec3 toSrgb(vec3 c) {
+ c = max(c, vec3(0.0));
+ return mix(1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, c * 12.92, lessThanEqual(c, vec3(0.0031308)));
+}
+
+vec3 scene(vec2 uv) {
+ return toLinear(texture(iChannel0, uv).rgb);
+}
+
+vec3 brightPass(vec2 uv) {
+ vec3 c = scene(uv);
+ float peak = max(c.r, max(c.g, c.b));
+ return c * smoothstep(bloomThreshold(), bloomThreshold() + BLOOM_SOFTNESS, peak);
+}
+
+// Interleaved gradient noise: a dither with no visible pattern.
+float ign(vec2 p) {
+ return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715))));
+}
+
+void mainImage(out vec4 fragColor, in vec2 fragCoord) {
+ vec2 size = iResolution.xy;
+ vec2 texel = 1.0 / size;
+ vec2 uv = fragCoord / size;
+ vec2 cc = uv * 2.0 - 1.0;
+ float r2 = dot(cc, cc);
+
+ // The beam converges least at the corners: red and blue drift apart
+ // there, by a third of a pixel.
+ vec2 chroma = cc * (chromaPx() * r2 * 0.5) * texel;
+ vec3 col = vec3(scene(uv + chroma).r, scene(uv).g, scene(uv - chroma).b);
+
+ vec2 b = texel * bloomRadiusPx();
+ vec3 bloom = brightPass(uv + vec2(b.x, 0.0)) + brightPass(uv - vec2(b.x, 0.0)) +
+ brightPass(uv + vec2(0.0, b.y)) + brightPass(uv - vec2(0.0, b.y));
+ col += bloom * (0.25 * bloomStrength());
+
+ // A beam is brightest on its line and falls off between lines; the
+ // mask is three phosphor stripes a triad. Both average to one, so the
+ // theme keeps its brightness.
+ float line = 0.5 + 0.5 * cos(fragCoord.y * TAU / SCAN_PERIOD_PX);
+ col *= 1.0 - scanStrength() * (line * line - 0.375) / 0.625;
+ col *= 1.0 + maskStrength() * cos(fragCoord.x * TAU / MASK_PERIOD_PX + vec3(0.0, TAU / 3.0, 2.0 * TAU / 3.0));
+
+ float vignette = 1.0 - vignetteStrength() * pow(clamp(r2 * 0.5, 0.0, 1.0), 1.6);
+ float hum = 1.0 + humStrength() * sin((uv.y * 1.35 - iTime * HUM_HZ) * TAU);
+ col *= vignette * hum * exposure();
+
+ vec3 srgb = toSrgb(col);
+ srgb += (ign(fragCoord + float(iFrame % 64) * vec2(5.588238, 5.588238)) - 0.5) / 255.0;
+ fragColor = vec4(clamp(srgb, 0.0, 1.0), texture(iChannel0, uv).a);
+}
diff --git a/shaders/post/prefix.glsl b/shaders/post/prefix.glsl
new file mode 100644
index 00000000..e718a98f
--- /dev/null
+++ b/shaders/post/prefix.glsl
@@ -0,0 +1,74 @@
+#version 450
+// pardes's Shadertoy prefix (docs/render-pipeline.md ยง6), put before every
+// post pass: a bundled one in shaders/post/ at build time, a user's file at
+// run time. The uniform names, types and std140 order are ghostty 1.3.2's
+// (src/renderer/shaders/shadertoy_prefix.glsl), so a shader written for
+// ghostty runs here; only the bindings are SDL GPU's (a fragment shader's
+// textures in set 2, its uniform blocks in set 3). Y is down everywhere, as
+// on ghostty's Metal renderer: fragCoord, iChannel0's uv and the cursor
+// rectangles share one top-left space, and iCurrentCursor is (left x,
+// BOTTOM-edge y, width, height) in framebuffer pixels.
+
+layout(set = 3, binding = 0, std140) uniform Globals {
+ uniform vec3 iResolution;
+ uniform float iTime;
+ uniform float iTimeDelta;
+ uniform float iFrameRate;
+ uniform int iFrame;
+ uniform float iChannelTime[4];
+ uniform vec3 iChannelResolution[4];
+ uniform vec4 iMouse;
+ uniform vec4 iDate;
+ uniform float iSampleRate;
+ uniform vec4 iCurrentCursor;
+ uniform vec4 iPreviousCursor;
+ uniform vec4 iCurrentCursorColor;
+ uniform vec4 iPreviousCursorColor;
+ uniform int iCurrentCursorStyle;
+ uniform int iPreviousCursorStyle;
+ uniform int iCursorVisible;
+ uniform float iTimeCursorChange;
+ uniform float iTimeFocus;
+ uniform int iFocus;
+ uniform vec3 iPalette[256];
+ uniform vec3 iBackgroundColor;
+ uniform vec3 iForegroundColor;
+ uniform vec3 iCursorColor;
+ uniform vec3 iCursorText;
+ uniform vec3 iSelectionForegroundColor;
+ uniform vec3 iSelectionBackgroundColor;
+};
+
+// pardes's own: pardesOpaque is 1 when the window is opaque, so a pass
+// cannot punch holes in it through its alpha; pardesLevel is a bundled
+// pass's strength, 1 to 3 (`Crt 3`), and 0 for a user's file.
+layout(set = 3, binding = 1, std140) uniform PardesPost {
+ float pardesOpaque;
+ float pardesLevel;
+};
+
+// A bundled pass's constant at levels 1, 2 and 3.
+float pardesByLevel(float one, float two, float three) {
+ return pardesLevel < 1.5 ? one : (pardesLevel < 2.5 ? two : three);
+}
+
+#define CURSORSTYLE_BLOCK 0
+#define CURSORSTYLE_BLOCK_HOLLOW 1
+#define CURSORSTYLE_BAR 2
+#define CURSORSTYLE_UNDERLINE 3
+#define CURSORSTYLE_LOCK 4
+
+layout(set = 2, binding = 0) uniform sampler2D iChannel0;
+
+layout(location = 0) out vec4 _fragColor;
+
+#define texture2D texture
+
+void mainImage(out vec4 fragColor, in vec2 fragCoord);
+void main() {
+ mainImage(_fragColor, gl_FragCoord.xy);
+ if (pardesOpaque > 0.5) _fragColor.a = 1.0;
+}
+
+// The shader's own lines count from 1, so glslc's errors name them.
+#line 1