// SPDX-FileCopyrightText: Copyright 2026 Mireille Meyer, Gloria Goertz // SPDX-License-Identifier: BSD-2-Clause #include #include using namespace metal; // x, y u, v constant float4 VERTICES[4] = { float4(-1.0, -1.0, 0.0, 1.0), float4( 1.0, -1.0, 1.0, 1.0), float4(-1.0, 1.0, 0.0, 0.0), float4( 1.0, 1.0, 1.0, 0.0) }; struct FragIn { float4 position [[position]]; float2 coords; }; struct FragOut { float4 color [[color(0)]]; }; constexpr sampler SAMPLER(mag_filter::nearest, min_filter::nearest, address::clamp_to_edge); vertex FragIn VCommonFilter(uint id [[vertex_id]]) { return { float4(VERTICES[id].xy, 0.0, 1.0), VERTICES[id].zw }; } // Color Correction // TODO(Gloria): this shader is not very accurate at the moment fragment FragOut FColorCorrectAGB(FragIn in [[stage_in]], texture2d tex [[texture(0)]]) { const float darkenScreen = 0.29; const float targetGamma = 1.0; // 2.2; const float displayGamma = 1.0; // 2.2; const float sat = 1.0; const float lum = 0.94; const float contrast = 1.0; const float blr = 0.0; const float blg = 0.0; const float blb = 0.0; const float r = 0.82; const float g = 0.665; const float b = 0.73; const float rg = 0.125; const float rb = 0.195; const float gr = 0.24; const float gb = 0.075; const float br = -0.06; const float bg = 0.21; float4 screen = pow(tex.sample(SAMPLER, in.coords), float4(float3(targetGamma + darkenScreen), 1.0)).rgba; float4 avgLum = float4(0.5); screen = mix(screen, avgLum, (1.0 - contrast)); float4x4 color = float4x4( r, rg, rb, 0.0, // red gr, g, gb, 0.0, // green br, bg, b, 0.0, // blue blr, blg, blb, 0.0 // alpha (unused) ); float4x4 adjust = float4x4( (1.0 - sat) * 0.3086 + sat, (1.0 - sat) * 0.3086, (1.0 - sat) * 0.3086, 1.0, (1.0 - sat) * 0.6094, (1.0 - sat) * 0.6094 + sat, (1.0 - sat) * 0.6094, 1.0, (1.0 - sat) * 0.0820, (1.0 - sat) * 0.0820, (1.0 - sat) * 0.0820 + sat, 1.0, 0.0, 0.0, 0.0, 1.0 ); color = color * adjust; screen = clamp(screen * lum, 0.0, 1.0); screen = color * screen; return FragOut { float4(pow(screen, float4(1.0 / displayGamma)).rgb, 1.0) }; } fragment FragOut FColorCorrectHigan(FragIn in [[stage_in]], texture2d tex [[texture(0)]]) { float4 color = tex.sample(SAMPLER, in.coords); color.rgb = pow(color.rgb, float3(4.0)); return { float4(pow(float3( 1.000 * color.r + 0.196 * color.g, 0.039 * color.r + 0.901 * color.g + 0.117 * color.b, 0.196 * color.r + 0.039 * color.g + 0.862 * color.b ), float3(1.0 / 2.2)), 1.0) }; } fragment FragOut FLCDGhost(FragIn in [[stage_in]], texture2d tex [[texture(0)]], texture2d texPrev [[texture(1)]]) { float4 color = tex.sample(SAMPLER, in.coords); float4 colorPrev = texPrev.sample(SAMPLER, in.coords); return { mix(color, colorPrev, 0.5)}; } // LCD1x vertex FragIn VLCD1x(uint id [[vertex_id]]) { return { float4(VERTICES[id].xy, 0.0, 1.0), float2(VERTICES[id].z, 1.0 - VERTICES[id].w) * 1.0001 }; } fragment FragOut FLCD1x(FragIn in [[stage_in]], texture2d tex [[texture(0)]]) { const float BRIGHTEN_SCANLINES = 16.0; const float BRIGHTEN_LCD = 24.0; const float PI = M_PI_F; // 3.141592653589793 const float2 INPUT_SIZE = float2(240,160); float2 angle = 2.0 * PI * ((in.coords.xy * INPUT_SIZE.xy) - 0.25); float factorY = (BRIGHTEN_SCANLINES + sin(angle.y)) / (BRIGHTEN_SCANLINES + 1.0); float factorX = (BRIGHTEN_LCD + sin(angle.x)) / (BRIGHTEN_LCD + 1.0); float3 color = factorY * factorX * (tex.sample(SAMPLER, in.coords).rgb); return { float4(color.rgb, 1.0) }; } // Sharp Bilinear constant float2 SHARP_BI_INPUT_SIZE = float2(240, 160); struct FragInSharpBi { float4 position [[position]]; float2 texel; float2 scale; }; vertex FragInSharpBi VSharpBi(uint id [[vertex_id]]) { // TODO(Gloria): this should be passed through a UBO or some shitinstead const float2 outputSize = float2(240, 160); float2 uv = float2(VERTICES[id].z, 1.0 - VERTICES[id].w); float2 texel = uv * SHARP_BI_INPUT_SIZE; float2 scale = max(floor(outputSize / SHARP_BI_INPUT_SIZE), float2(1.0, 1.0)); return { float4(VERTICES[id].xy, 0.0, 1.0), texel, scale }; } fragment FragOut FSharpBi(FragInSharpBi in [[stage_in]], texture2d tex [[texture(0)]]) { float2 texelFloor = floor(in.texel); float2 s = fract(in.texel); float2 regionRange = 0.5 - (0.5 / in.scale); float2 centerDist = s - 0.5; float2 f = (centerDist - clamp(centerDist, -regionRange, regionRange)) * in.scale + 0.5; float2 modTexel = texelFloor + f; return { float4(tex.sample(SAMPLER, modTexel / SHARP_BI_INPUT_SIZE).rgb, 1.0) }; } // Direct vertex FragIn VDirect(uint id [[vertex_id]]) { return { float4(VERTICES[id].x, 1.0 - VERTICES[id].y, 0.0, 1.0), float2(VERTICES[id].z, 1.0 - VERTICES[id].w) }; } fragment FragOut FDirect(FragIn in [[stage_in]], texture2d tex [[texture(0)]]) { return { tex.sample(SAMPLER, in.coords) }; }