Skip to content

Commit

Permalink
All Pixel Clipper variants are now baked into the same shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Artoriuz committed Aug 10, 2023
1 parent 6494a13 commit bd25994
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 134 deletions.
63 changes: 61 additions & 2 deletions PixelClipper.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
//!HOOK POSTKERNEL
//!BIND POSTKERNEL
//!BIND PREKERNEL
//!DESC Pixel Clipper (Anti-Ringing)
//!DESC Pixel Clipper (Upscaling AR)
//!WHEN POSTKERNEL.w PREKERNEL.w / 1.000 > POSTKERNEL.h PREKERNEL.h / 1.000 > *

#define TWELVE_TAP_AR 0

const float strength = 1.0;

vec4 hook() {
Expand All @@ -36,23 +38,80 @@ vec4 hook() {
vec4 g = PREKERNEL_tex(vec2((fp + vec2( 1.5, 0.5)) * PREKERNEL_pt));
vec4 j = PREKERNEL_tex(vec2((fp + vec2( 0.5, 1.5)) * PREKERNEL_pt));
vec4 k = PREKERNEL_tex(vec2((fp + vec2( 1.5, 1.5)) * PREKERNEL_pt));
#if (TWELVE_TAP_AR == 1)
vec4 b = PREKERNEL_tex(vec2((fp + vec2(0.5, -0.5)) * PREKERNEL_pt));
vec4 c = PREKERNEL_tex(vec2((fp + vec2(1.5, -0.5)) * PREKERNEL_pt));
vec4 e = PREKERNEL_tex(vec2((fp + vec2(-0.5, 0.5)) * PREKERNEL_pt));
vec4 h = PREKERNEL_tex(vec2((fp + vec2( 2.5, 0.5)) * PREKERNEL_pt));
vec4 i = PREKERNEL_tex(vec2((fp + vec2(-0.5, 1.5)) * PREKERNEL_pt));
vec4 l = PREKERNEL_tex(vec2((fp + vec2( 2.5, 1.5)) * PREKERNEL_pt));
vec4 n = PREKERNEL_tex(vec2((fp + vec2(0.5, 2.5) ) * PREKERNEL_pt));
vec4 o = PREKERNEL_tex(vec2((fp + vec2(1.5, 2.5) ) * PREKERNEL_pt));
#endif

vec4 min_pix = vec4(1e8);
min_pix = min(min_pix, f);
min_pix = min(min_pix, g);
min_pix = min(min_pix, j);
min_pix = min(min_pix, k);
#if (TWELVE_TAP_AR == 1)
min_pix = min(min_pix, b);
min_pix = min(min_pix, c);
min_pix = min(min_pix, e);
min_pix = min(min_pix, h);
min_pix = min(min_pix, i);
min_pix = min(min_pix, l);
min_pix = min(min_pix, n);
min_pix = min(min_pix, o);
#endif

vec4 max_pix = vec4(1e-8);
max_pix = max(max_pix, f);
max_pix = max(max_pix, g);
max_pix = max(max_pix, j);
max_pix = max(max_pix, k);
#if (TWELVE_TAP_AR == 1)
max_pix = max(max_pix, b);
max_pix = max(max_pix, c);
max_pix = max(max_pix, e);
max_pix = max(max_pix, h);
max_pix = max(max_pix, i);
max_pix = max(max_pix, l);
max_pix = max(max_pix, n);
max_pix = max(max_pix, o);
#endif

// Sample current high-res pixel
//Sample current high-res pixel
vec4 hr_pix = POSTKERNEL_texOff(0.0);

// Clamp the intensity so it doesn't ring
vec4 clipped = clamp(hr_pix, min_pix, max_pix);
return mix(hr_pix, clipped, strength);
}

//!HOOK POSTKERNEL
//!BIND POSTKERNEL
//!BIND PREKERNEL
//!DESC Pixel Clipper (Downscaling AR)
//!WHEN POSTKERNEL.w PREKERNEL.w / 1.000 < POSTKERNEL.h PREKERNEL.h / 1.000 < *

const float strength = 1.0;

vec4 hook() {
int radius = int(ceil((PREKERNEL_size.x / POSTKERNEL_size.x) * 0.5));
vec4 pix = vec4(0.0);
vec4 min_pix = vec4(1e8);
vec4 max_pix = vec4(1e-8);

for (int dx = -radius; dx <= radius; dx++) {
for (int dy = -radius; dy <= radius; dy++) {
pix = PREKERNEL_texOff(vec2(dx, dy));
min_pix = min(pix, min_pix);
max_pix = max(pix, max_pix);
}
}

vec4 lr_pix = POSTKERNEL_texOff(0.0);
vec4 clipped = clamp(lr_pix, min_pix, max_pix);
return mix(lr_pix, clipped, strength);
}
82 changes: 0 additions & 82 deletions PixelClipper_12tap.glsl

This file was deleted.

48 changes: 0 additions & 48 deletions PixelClipper_downscaling.glsl

This file was deleted.

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
This is a simple pixel clipper for mpv. You can use it alongside resampling filters to limit the amount of ringing after resampling.

~~The main difference between this and the built-in anti-ringing functionality is that this shader works with polar filters.~~
[Looks like polar AR is now supported in libplacebo](https://code.videolan.org/videolan/libplacebo/-/merge_requests/505), so these shaders shouldn't really be needed anymore unless you don't like the native solution.
[Looks like polar AR is now supported in libplacebo](https://code.videolan.org/videolan/libplacebo/-/merge_requests/505), so this shader shouldn't really be needed anymore unless you don't like the native solution.

Use the 12-tap variant for any filter with radius greater than 2 if you don't want a "cut-off" impulse response. The standard 4-tap shader is marginally better at eliminating ringing though.
Set `TWELVE_TAP_AR 1` if you don't want a "cut-off" impulse response with larger filters. The standard 4-tap shader is marginally better at eliminating ringing though. You can also just remove the downsampling pass if you're
only interested in upsampling AR.

## Instructions
Add something like this to your mpv config:
Expand Down

0 comments on commit bd25994

Please sign in to comment.