summaryrefslogtreecommitdiff
path: root/shaders/crt.ci.metal
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-08-16 15:49:12 -0300
committerGabriel Schneider <[email protected]>2026-08-18 23:44:42 -0300
commit1551e409c31992437cb2fa864f576d45c8433801 (patch)
treee2fae8451f87b735a1360c7c2e383fdc40165789 /shaders/crt.ci.metal
parentbe2a9957708cbf0c478ca861c4a1f0f227bbfe10 (diff)
downloadpardes-1551e409c31992437cb2fa864f576d45c8433801.tar.gz
pardes-1551e409c31992437cb2fa864f576d45c8433801.zip
big slow change: prebuilt shaders (SPIR-V/Metal), core gui reflow, docs, web + snapshot refresh
Diffstat (limited to 'shaders/crt.ci.metal')
-rw-r--r--shaders/crt.ci.metal376
1 files changed, 376 insertions, 0 deletions
diff --git a/shaders/crt.ci.metal b/shaders/crt.ci.metal
new file mode 100644
index 00000000..32e26b21
--- /dev/null
+++ b/shaders/crt.ci.metal
@@ -0,0 +1,376 @@
+// Native macOS panel and scene postprocess. Panel kernels consume the core's
+// plain tracks; the final full-window kernel ports crt.frag.glsl and combines
+// CRT, ripple and glitch. Scene constants and sampling geometry stay in step
+// with GLSL, while panel easing/noise stays in step with panel_animation.zig.
+#include <metal_stdlib>
+#include <CoreImage/CoreImage.h>
+using namespace metal;
+
+constant float PARDES_TAU = 6.28318530718;
+
+float pardesNoise(float2 p) {
+ float3 q = fract(float3(p.x, p.y, p.x) * 0.1031);
+ q += dot(q, q.yzx + 33.33);
+ return fract((q.x + q.y) * q.z);
+}
+
+// Exact integer hash from panel_animation.cellNoise. Pane serial and logical
+// cell coordinates, not destination pixels, keep TTY and GPU reveal decisions
+// stable across scale factors and geometry transforms.
+float pardesCellNoise(float2 serialParts, float colValue, float rowValue) {
+ uint serial = uint(max(serialParts.x, 0.0)) |
+ (uint(max(serialParts.y, 0.0)) << 16);
+ uint col = uint(max(colValue, 0.0));
+ uint row = uint(max(rowValue, 0.0));
+ uint x = serial ^ (col * 0x9e3779b9u) ^ (row * 0x85ebca6bu);
+ x ^= x >> 16;
+ x *= 0x7feb352du;
+ x ^= x >> 15;
+ x *= 0x846ca68bu;
+ x ^= x >> 16;
+ return float(x & 0xffffu) / 65535.0;
+}
+
+float2 pardesSceneUV(float2 uv, float timeSeconds, float rippleOn, float glitchOn) {
+ if (rippleOn > 0.5) {
+ float2 centered = uv - 0.5;
+ float radius = length(centered);
+ // Reversed smoothstep edges are undefined in GLSL and Metal alike.
+ float envelope = 1.0 - smoothstep(0.04, 0.72, radius);
+ float wave = sin(radius * 42.0 - timeSeconds * 5.2);
+ uv += centered / max(radius, 0.00001) * wave * envelope * 0.0032;
+ }
+ if (glitchOn > 0.5) {
+ float band = floor(uv.y * 96.0);
+ float seed = pardesNoise(float2(band, floor(timeSeconds * 13.0)));
+ float tear = smoothstep(0.955, 1.0, seed);
+ uv.x += (pardesNoise(float2(seed, band)) - 0.5) * tear * 0.020;
+ }
+ return uv;
+}
+
+float4 pardesSceneSample(
+ coreimage::sampler source,
+ float2 origin,
+ float2 size,
+ float2 uv
+) {
+ return coreimage::unpremultiply(
+ source.sample(source.transform(origin + uv * size)));
+}
+
+// Keep this ordering identical to crt.frag.glsl's sceneSrgb: every caller
+// supplies its own pre-warp coordinate, then ripple/glitch move that sample,
+// then glitch splits its red/blue channels around the moved center.
+float4 pardesSceneSrgb(
+ coreimage::sampler source,
+ float2 origin,
+ float2 size,
+ float2 uv,
+ float timeSeconds,
+ float rippleOn,
+ float glitchOn
+) {
+ float2 warped = pardesSceneUV(uv, timeSeconds, rippleOn, glitchOn);
+ float4 center = pardesSceneSample(source, origin, size, warped);
+ if (glitchOn <= 0.5) return center;
+
+ float band = floor(warped.y * 96.0);
+ float seed = pardesNoise(float2(band, floor(timeSeconds * 13.0)));
+ float tear = smoothstep(0.955, 1.0, seed);
+ float2 chroma = float2((0.35 + 1.25 * tear) / size.x, 0.0);
+ float4 red = pardesSceneSample(source, origin, size, warped + chroma);
+ float4 blue = pardesSceneSample(source, origin, size, warped - chroma);
+ return float4(red.r, center.g, blue.b, center.a);
+}
+
+float3 pardesSceneLinear(
+ coreimage::sampler source,
+ float2 origin,
+ float2 size,
+ float2 uv,
+ float timeSeconds,
+ float rippleOn,
+ float glitchOn
+) {
+ return coreimage::srgb_to_linear(pardesSceneSrgb(
+ source, origin, size, uv, timeSeconds, rippleOn, glitchOn).rgb);
+}
+
+bool pardesInside(float2 p, float4 box) {
+ return p.x >= box.x && p.y >= box.y &&
+ p.x < box.x + box.z && p.y < box.y + box.w;
+}
+
+float pardesPanelProgress(float effect, float frame) {
+ float frames = effect == 1.0 ? 12.0 :
+ effect == 2.0 ? 14.0 :
+ effect == 3.0 ? 10.0 :
+ effect == 4.0 ? 16.0 :
+ effect == 5.0 ? 12.0 : 1.0;
+ if (frames <= 1.0 || frame >= frames - 1.0) return 1.0;
+ float t = clamp(frame / (frames - 1.0), 0.0, 1.0);
+ if (effect == 1.0 || effect == 5.0) { // horizontal/vertical slide
+ float u = 1.0 - t;
+ return 1.0 - u * u * u;
+ }
+ if (effect == 2.0) { // zoom: ease-out back, including its overshoot
+ const float c1 = 1.70158;
+ const float c3 = c1 + 1.0;
+ float u = t - 1.0;
+ return 1.0 + c3 * u * u * u + c1 * u * u;
+ }
+ if (effect == 3.0) return t * t * (3.0 - 2.0 * t); // dissolve
+ return t; // ASCII materialization
+}
+
+float pardesAsciiGlyph(float2 uv, int glyph) {
+ float2 p = uv * 2.0 - 1.0;
+ const float thin = 0.12;
+ float dotShape = 1.0 - smoothstep(0.10, 0.20, length(p - float2(0.0, 0.45)));
+ float barH = 1.0 - smoothstep(thin, thin + 0.08, abs(p.y));
+ float barV = 1.0 - smoothstep(thin, thin + 0.08, abs(p.x));
+ if (glyph == 0) return dotShape;
+ if (glyph == 1) return max(dotShape,
+ 1.0 - smoothstep(0.10, 0.20, length(p + float2(0.0, 0.45))));
+ if (glyph == 2) return max(barH, barV);
+ if (glyph == 3) {
+ float d0 = 1.0 - smoothstep(thin, thin + 0.08, abs(p.x - p.y));
+ float d1 = 1.0 - smoothstep(thin, thin + 0.08, abs(p.x + p.y));
+ return max(max(d0, d1), max(barH, barV));
+ }
+ if (glyph == 4) {
+ float rails = max(
+ 1.0 - smoothstep(thin, thin + 0.08, abs(abs(p.x) - 0.34)),
+ 1.0 - smoothstep(thin, thin + 0.08, abs(abs(p.y) - 0.34)));
+ return rails;
+ }
+ float ring = 1.0 - smoothstep(thin, thin + 0.08,
+ abs(length(p * float2(0.8, 1.0)) - 0.48));
+ return max(ring, glyph == 6 ? dotShape : 0.0);
+}
+
+// Prepare every geometric target before any translated panel is drawn. A
+// vertical opener restores the old frame in its fixed clip; a closing pane is
+// already absent from `canvas` and therefore needs no clear at all.
+extern "C" float4 pardesPanelClear(
+ coreimage::sampler canvas,
+ coreimage::sampler previous,
+ float4 target,
+ float4 ground,
+ float effect,
+ float phase,
+ coreimage::destination destination
+) {
+ float2 coord = destination.coord();
+ if (!pardesInside(coord, target))
+ return canvas.sample(canvas.transform(coord));
+ if (effect == 1.0 || effect == 2.0) return ground;
+ if (effect == 5.0 && phase == 0.0)
+ return previous.sample(previous.transform(coord));
+ return canvas.sample(canvas.transform(coord));
+}
+
+// One panel draw over the accumulated image. Current and previous are complete
+// CoreText+attachment rasters. That makes a closing pane safe after its core
+// allocation is gone and makes ASCII/dissolve true old-to-new data effects.
+extern "C" float4 pardesPanel(
+ coreimage::sampler current,
+ coreimage::sampler previous,
+ coreimage::sampler changedMask,
+ coreimage::sampler canvas,
+ float4 target,
+ float4 from,
+ float4 to,
+ float effect,
+ float phase,
+ float frame,
+ float2 serialParts,
+ float2 cellSize,
+ float2 gridOrigin,
+ coreimage::destination destination
+) {
+ float2 coord = destination.coord();
+ float4 underneath = canvas.sample(canvas.transform(coord));
+ float progress = pardesPanelProgress(effect, frame);
+
+ float2 safeCell = max(cellSize, float2(1.0));
+ // Content transitions stay at canonical geometry. Unchanged cells bypass
+ // the effect completely; changed cells retain the exact old raster until
+ // their deterministic wave reaches them.
+ if (effect == 3.0 || effect == 4.0) {
+ if (!pardesInside(coord, target)) return underneath;
+ // During a live resize the grid is still anchored at the view's top,
+ // leaving a partial strip below its final logical row. Account for
+ // that strip before indexing the compact cell mask.
+ float2 absoluteCell = floor((coord - gridOrigin) / safeCell);
+ float changed = changedMask.sample(changedMask.transform(
+ absoluteCell + float2(0.5))).r;
+ if (changed < 0.5) return underneath;
+
+ float4 oldPixel = previous.sample(previous.transform(coord));
+ float4 newPixel = current.sample(current.transform(coord));
+ // Exact endpoint guards are not redundant: a noise hash can be zero
+ // or one, and frame zero/last must still be wholly old/new.
+ if (progress <= 0.0) return oldPixel;
+ if (progress >= 1.0) return newPixel;
+
+ // Core/TTY coordinates are relative to the target's TOP-left. Core
+ // Image is bottom-up, hence the reversed row term.
+ float2 cell = float2(
+ floor((coord.x - target.x) / safeCell.x),
+ floor((target.y + target.w - coord.y) / safeCell.y));
+ float2 cellCount = max(round(target.zw / safeCell), float2(1.0));
+ cell = clamp(cell, float2(0.0), cellCount - 1.0);
+ float noise = pardesCellNoise(serialParts, cell.x, cell.y);
+ if (effect == 3.0)
+ return noise < progress ? newPixel : oldPixel;
+
+ float diagonal = fmod(cell.x + cell.y * 2.0, 17.0) / 17.0;
+ float threshold = noise * 0.72 + diagonal * 0.28;
+ if (progress >= threshold) return newPixel;
+ if (progress + 0.22 < threshold) return oldPixel;
+
+ float2 cellUV = fract(float2(
+ (coord.x - target.x) / safeCell.x,
+ (target.y + target.w - coord.y) / safeCell.y));
+ int glyph = min(int(floor(noise * 7.0)), 6);
+ float ink = pardesAsciiGlyph(cellUV, glyph);
+ float2 cellOrigin = float2(
+ target.x + cell.x * safeCell.x,
+ target.y + target.w - (cell.y + 1.0) * safeCell.y);
+ float4 bgSample = current.sample(current.transform(
+ cellOrigin + safeCell * float2(0.12, 0.12)));
+ float4 bg = coreimage::unpremultiply(bgSample);
+ float4 fg = coreimage::unpremultiply(newPixel);
+ // A procedural mark usually crosses pixels where the target glyph has
+ // no ink, so sampling `newPixel` alone can make it background-on-
+ // background and erase the ASCII phase. Preserve target ink where it
+ // is present; otherwise derive restrained contrast from the cell's
+ // own background instead of imposing a fixed palette.
+ float3 contrastInk = dot(bg.rgb, float3(0.2126, 0.7152, 0.0722)) > 0.5
+ ? float3(0.0) : float3(1.0);
+ float sourceContrast = smoothstep(0.025, 0.14, length(fg.rgb - bg.rgb));
+ float3 inkColor = mix(contrastInk, fg.rgb, sourceContrast);
+ float3 dimInk = mix(bg.rgb, inkColor, 0.62);
+ return coreimage::premultiply(float4(
+ mix(bg.rgb, dimInk, ink), max(bg.a, newPixel.a)));
+ }
+
+ float4 shown = mix(from, to, progress);
+ shown.zw = max(shown.zw, float2(0.0));
+ if (!pardesInside(coord, shown) || shown.z <= 0.0 || shown.w <= 0.0)
+ return underneath;
+ // Vertical lifecycle motion is deliberately confined to the pane's own
+ // old/new box. It never travels across or clips a surviving neighbour.
+ if (effect == 5.0 && !pardesInside(coord, target)) return underneath;
+
+ float2 within = (coord - shown.xy) / shown.zw;
+ float2 sourceCoord = target.xy + within * target.zw;
+ if (!pardesInside(sourceCoord, target)) return underneath;
+ return phase == 2.0
+ ? previous.sample(previous.transform(sourceCoord))
+ : current.sample(current.transform(sourceCoord));
+}
+
+extern "C" float4 pardesScene(
+ coreimage::sampler source,
+ float timeSeconds,
+ float frame,
+ float crtOn,
+ float rippleOn,
+ float glitchOn,
+ float2 sceneSize,
+ float2 sceneOrigin,
+ coreimage::destination destination
+) {
+ const float barrel = 0.016;
+ const float edgeSoftnessPx = 3.5;
+ const float chromaPx = 0.28;
+ const float bloomRadiusPx = 1.75;
+ const float bloomThreshold = 0.60;
+ const float bloomSoftness = 0.25;
+ const float bloomStrength = 0.045;
+ const float scanPeriodPx = 3.0;
+ const float scanStrength = 0.055;
+ const float maskPeriodPx = 3.0;
+ const float maskStrength = 0.015;
+ const float vignetteStrength = 0.11;
+ const float humStrength = 0.0025;
+ const float noiseStrength = 0.0020;
+ const float exposure = 1.012;
+
+ float2 size = sceneSize;
+ float2 origin = sceneOrigin;
+ float2 pixel = destination.coord() - origin;
+ float2 texel = 1.0 / size;
+ float2 cc = pixel / size * 2.0 - 1.0;
+ float r2 = dot(cc, cc);
+ float2 uv = cc * (1.0 + barrel * r2 * crtOn) * 0.5 + 0.5;
+ float edge = min(min(uv.x, uv.y), min(1.0 - uv.x, 1.0 - uv.y));
+ // Match GLSL's opaque black outside the tube. Transparent black would
+ // reveal AppKit's backdrop and make the same CRT effect have no tube on a
+ // transparent theme.
+ if (crtOn > 0.5 && edge <= 0.0) return float4(0.0, 0.0, 0.0, 1.0);
+
+ float4 center = pardesSceneSrgb(
+ source, origin, size, uv, timeSeconds, rippleOn, glitchOn);
+ float3 scene = center.rgb;
+
+ // Ripple/glitch alone are coordinate effects. Returning the sampled sRGB
+ // values directly keeps exact theme colors exact.
+ if (crtOn <= 0.5) {
+ return coreimage::premultiply(float4(clamp(scene, 0.0, 1.0), center.a));
+ }
+
+ float3 centerLinear = coreimage::srgb_to_linear(scene);
+ float2 radialChroma = cc * (chromaPx * r2) * texel;
+ float3 redSample = pardesSceneLinear(
+ source, origin, size, uv + radialChroma,
+ timeSeconds, rippleOn, glitchOn);
+ float3 blueSample = pardesSceneLinear(
+ source, origin, size, uv - radialChroma,
+ timeSeconds, rippleOn, glitchOn);
+ float3 col = float3(
+ redSample.r,
+ centerLinear.g,
+ blueSample.b
+ );
+
+ float2 bloom = texel * bloomRadiusPx;
+ float3 bloomSum = float3(0.0);
+ float2 bloomOffsets[4] = {
+ float2(bloom.x, 0.0), float2(-bloom.x, 0.0),
+ float2(0.0, bloom.y), float2(0.0, -bloom.y)
+ };
+ for (uint i = 0; i < 4; i++) {
+ // Deliberately raw, like GLSL brightPass: bloom belongs to the source
+ // image and does not recursively warp through the scene effects.
+ float4 sampleValue = pardesSceneSample(
+ source, origin, size, uv + bloomOffsets[i]);
+ float3 linearValue = coreimage::srgb_to_linear(sampleValue.rgb);
+ float peak = max(linearValue.r, max(linearValue.g, linearValue.b));
+ bloomSum += linearValue * smoothstep(bloomThreshold, bloomThreshold + bloomSoftness, peak);
+ }
+ col += bloomSum * (0.25 * bloomStrength);
+
+ float scanWave = 0.5 + 0.5 * cos(pixel.y * PARDES_TAU / scanPeriodPx);
+ col *= 1.0 - scanStrength * scanWave * scanWave;
+ float3 mask = 1.0 + maskStrength * cos(
+ pixel.x * PARDES_TAU / maskPeriodPx +
+ float3(0.0, PARDES_TAU / 3.0, 2.0 * PARDES_TAU / 3.0));
+ col *= mask;
+
+ float2 edgePx = min(uv, 1.0 - uv) * size;
+ float tube = smoothstep(0.0, edgeSoftnessPx, min(edgePx.x, edgePx.y));
+ 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 - timeSeconds * 0.11) * PARDES_TAU);
+ col *= tube * vignette * hum * exposure;
+
+ float luma = dot(col, float3(0.2126, 0.7152, 0.0722));
+ float2 noiseSeed = pixel + float2(frame * 17.0, frame * 59.0);
+ col += (pardesNoise(noiseSeed) - 0.5) * noiseStrength *
+ (0.15 + 0.85 * clamp(luma, 0.0, 1.0));
+ return coreimage::premultiply(float4(
+ coreimage::linear_to_srgb(clamp(col, 0.0, 1.0)), center.a));
+}