From 1e7efb23545c63796cc0237937c263b7bfb39880 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Sun, 4 Jul 2021 04:19:57 +0200 Subject: [PATCH] Add sharp bilinear postprocessing shader. --- Data/Sys/Shaders/sharp_bilinear.glsl | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Data/Sys/Shaders/sharp_bilinear.glsl diff --git a/Data/Sys/Shaders/sharp_bilinear.glsl b/Data/Sys/Shaders/sharp_bilinear.glsl new file mode 100644 index 0000000000..e94748b959 --- /dev/null +++ b/Data/Sys/Shaders/sharp_bilinear.glsl @@ -0,0 +1,47 @@ +// Based on https://github.com/libretro/slang-shaders/blob/master/interpolation/shaders/sharp-bilinear.slang +// by Themaister, Public Domain license +// Does a bilinear stretch, with a preapplied Nx nearest-neighbor scale, +// giving a sharper image than plain bilinear. + +/* +[configuration] +[OptionRangeFloat] +GUIName = Prescale Factor (set to 0 for automatic) +OptionName = PRESCALE_FACTOR +MinValue = 0.0 +MaxValue = 16.0 +StepAmount = 1.0 +DefaultValue = 0.0 +[/configuration] +*/ + +float CalculatePrescale(float config_scale) { + if (config_scale == 0.0) { + float2 source_size = GetResolution(); + float2 window_size = GetWindowResolution(); + return ceil(max(window_size.x / source_size.x, window_size.y / source_size.y)); + } else { + return config_scale; + } +} + +void main() +{ + float2 source_size = GetResolution(); + float2 texel = GetCoordinates() * source_size; + float2 texel_floored = floor(texel); + float2 s = fract(texel); + float config_scale = GetOption(PRESCALE_FACTOR); + float scale = CalculatePrescale(config_scale); + float region_range = 0.5 - 0.5 / scale; + + // Figure out where in the texel to sample to get correct pre-scaled bilinear. + // Uses the hardware bilinear interpolator to avoid having to sample 4 times manually. + + float2 center_dist = s - 0.5; + float2 f = (center_dist - clamp(center_dist, -region_range, region_range)) * scale + 0.5; + + float2 mod_texel = texel_floored + f; + + SetOutput(SampleLocation(mod_texel / source_size)); +}