Skip to content

Commit

Permalink
Add sharp bilinear postprocessing shader.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdmiralCurtiss committed Feb 4, 2023
1 parent aece99f commit 796009d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Data/Sys/Shaders/sharp_bilinear.glsl
Original file line number Diff line number Diff line change
@@ -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 = 0.0; // 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));
}

0 comments on commit 796009d

Please sign in to comment.