-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sharp bilinear postprocessing shader.
- Loading branch information
1 parent
aece99f
commit 796009d
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |