<?phpnamespace App\Hd;use JumPHP\Gl\WebGl;/** * The "CRT 1995" option: the picture as a 14" VGA colour monitor of the time showed it, computed by the GPU at each * frame (one post shader pass: 8 reads of the frame per pixel of the window, nothing for the PHP). * * Not the scanlines of an emulator: tools/crt/crt.py is the slow, complete model this shader follows — * - the video amplifier's bandwidth smooths each line (in volts, before the gamma): the dithering melts; * - 31 kHz monitor: each of the 200 lines drawn twice (400 scanlines), so thin gaps between lines; * - the beam is a gaussian spot, wider when brighter (bright lines bloom over the gaps); * - light = voltage ^ 2.5; a fine slot mask (only when the window has the pixels for it); a nearly flat face. */final class CrtRenderer{ public const SHADER = <<<'GLSL' uniform vec2 u_src; vec3 px(vec2 cell) { return texture2D(u_frame, (cell + 0.5) / u_src).rgb; } // one scanline of the game's row: the signal along the line through the amplifier (4 taps, gaussian, volts) vec3 signal(float row, float x) { float c = floor(x - 0.5); float f = x - 0.5 - c; vec3 sum = vec3(0.0); float ws = 0.0; for (int i = -1; i <= 2; i++) { float d = float(i) - f; float w = exp(-2.9 * d * d); // sigma ~0.42 game pixel sum += px(vec2(c + float(i), row)) * w; ws += w; } return sum / ws; } vec3 beam(vec3 light, float d) { vec3 s = 0.30 + 0.22 * sqrt(light); // spot width (scanline pitches), wider when brighter return light * exp(-0.5 * d * d / (s * s)) / (s * 2.5066); } void main() { // a nearly flat face (the tubes of 1995 were only slightly curved) vec2 cc = v_uv - 0.5; float r2 = dot(cc, cc); vec2 uv = 0.5 + cc * (1.0 + 0.012 * r2); if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) { gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); return; } vec2 p = uv * u_src; float sl = p.y * 2.0; // the 400 scanlines (double scan) float li = floor(sl); float d1 = sl - li - 0.5; float l2 = li + (d1 < 0.0 ? -1.0 : 1.0); float d2 = sl - l2 - 0.5; vec3 a = pow(signal(floor(li * 0.5), p.x), vec3(2.5)); vec3 b = pow(signal(clamp(floor(l2 * 0.5), 0.0, u_src.y - 1.0), p.x), vec3(2.5)); vec3 col = beam(a, d1) + beam(b, d2); // the slot mask: R, G, B columns of window pixels, only when a game pixel is 3+ window pixels wide float k = clamp((u_resolution.x / u_src.x - 2.5) / 3.0, 0.0, 1.0) * 0.28; float m = mod(floor(gl_FragCoord.x), 3.0); vec3 mask = vec3(m < 0.5 ? 1.0 : 0.6, (m > 0.5 && m < 1.5) ? 1.0 : 0.6, m > 1.5 ? 1.0 : 0.6); col *= mix(vec3(1.0), mask, k) * (1.0 + k * 0.6); col = col / (1.0 + 0.12 * col) * 1.12; // phosphor saturation col *= 1.0 - 0.35 * r2; // corners a little darker gl_FragColor = vec4(pow(col, vec3(1.0 / 2.2)) + 0.008, 1.0); } GLSL; private bool $on = false; /** @var array{0: int, 1: int} */ private array $size = [320, 200]; public function __construct(private WebGl $gl) { } /** Over whatever the 2D shows (any edition): false without OpenGL / WebGL. */ public function enable(): bool { if (!$this->gl->isAvailable()) { return false; } $this->gl->setLayer('above', 'native', [255, 0, 255]); // the post shader then runs on the window's pixels $this->gl->setPostShader(self::SHADER, ['u_src' => [320.0, 200.0]]); $this->size = [320, 200]; $this->on = true; return true; } /** The size of the VM's frame (320 x 200, films 320 x 240...). */ public function source(int $w, int $h): void { if ($this->on && $w > 0 && $h > 0 && [$w, $h] !== $this->size) { $this->size = [$w, $h]; $this->gl->setUniform('u_src', [(float) $w, (float) $h]); } } public function disable(): void { if ($this->on) { $this->gl->disable(); $this->on = false; } }}