From 7fa668fc3541f8a047378314bbead022ab15dc88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 15 Oct 2024 15:58:45 +0200 Subject: [PATCH] Add Sharp Bilinear Upscale post shader, thanks Silent --- assets/shaders/defaultshaders.ini | 5 +++++ assets/shaders/upscale_sharp_bilinear.fsh | 25 +++++++++++++++++++++++ assets/shaders/upscale_sharp_bilinear.vsh | 9 ++++++++ 3 files changed, 39 insertions(+) create mode 100644 assets/shaders/upscale_sharp_bilinear.fsh create mode 100644 assets/shaders/upscale_sharp_bilinear.vsh diff --git a/assets/shaders/defaultshaders.ini b/assets/shaders/defaultshaders.ini index 8b80d1dc03..22d2f006de 100644 --- a/assets/shaders/defaultshaders.ini +++ b/assets/shaders/defaultshaders.ini @@ -243,3 +243,8 @@ SettingDefaultValue1=0.4 SettingMaxValue1=0.8 SettingMinValue1=0.3 SettingStep1=0.05 +[UpscaleSharpBilinear] +Name=Sharp Bilinear Upscaler +Fragment=upscale_sharp_bilinear.fsh +Vertex=upscale_sharp_bilinear.vsh +OutputResolution=True \ No newline at end of file diff --git a/assets/shaders/upscale_sharp_bilinear.fsh b/assets/shaders/upscale_sharp_bilinear.fsh new file mode 100644 index 0000000000..23e340c856 --- /dev/null +++ b/assets/shaders/upscale_sharp_bilinear.fsh @@ -0,0 +1,25 @@ +#ifdef GL_ES +#extension GL_OES_standard_derivatives : enable +precision mediump float; +precision mediump int; +#endif + +uniform sampler2D sampler0; +varying vec2 v_position; + +uniform vec2 u_texelDelta; // (1.0 / bufferWidth, 1.0 / bufferHeight) +uniform vec2 u_pixelDelta; // (1.0 / targetWidth, 1.0 / targetHeight) + +// Returns pixel sharpened to nearest pixel boundary. +vec2 sharpSample(vec4 texSize, vec2 coord) { + vec2 boxSize = clamp(fwidth(coord) * texSize.zw, 1e-5, 1.0); + coord = coord * texSize.zw - 0.5 * boxSize; + vec2 txOffset = smoothstep(vec2(1.0) - boxSize, vec2(1.0), fract(coord)); + return (floor(coord) + 0.5 + txOffset) * texSize.xy; +} + +void main() { + vec2 sharpPos = sharpSample(vec4(u_texelDelta, 1.0 / u_texelDelta), v_position); + vec4 color = texture2D(sampler0, sharpPos); + gl_FragColor = color; +} diff --git a/assets/shaders/upscale_sharp_bilinear.vsh b/assets/shaders/upscale_sharp_bilinear.vsh new file mode 100644 index 0000000000..dee13a0a53 --- /dev/null +++ b/assets/shaders/upscale_sharp_bilinear.vsh @@ -0,0 +1,9 @@ +attribute vec4 a_position; +attribute vec2 a_texcoord0; + +varying vec2 v_position; + +void main() { + gl_Position = a_position; + v_position = a_texcoord0; +}