From 0d2ac12e55d4041e74d4aa0d04c7d58068622da6 Mon Sep 17 00:00:00 2001 From: rsn8887 Date: Mon, 15 Jul 2019 16:56:00 -0500 Subject: [PATCH] add sharp-bilinear-simple, -scanlines and -2x-prescale shaders For more information, see https://github.com/rsn8887/Sharp-Bilinear-Shaders --- retro/shaders/sharp-bilinear-scanlines.slang | 87 ++++++++++++++++++++ retro/shaders/sharp-bilinear-simple.slang | 67 +++++++++++++++ retro/sharp-bilinear-2x-prescale.slangp | 7 ++ retro/sharp-bilinear-scanlines.slangp | 4 + retro/sharp-bilinear-simple.slangp | 4 + 5 files changed, 169 insertions(+) create mode 100644 retro/shaders/sharp-bilinear-scanlines.slang create mode 100644 retro/shaders/sharp-bilinear-simple.slang create mode 100644 retro/sharp-bilinear-2x-prescale.slangp create mode 100644 retro/sharp-bilinear-scanlines.slangp create mode 100644 retro/sharp-bilinear-simple.slangp diff --git a/retro/shaders/sharp-bilinear-scanlines.slang b/retro/shaders/sharp-bilinear-scanlines.slang new file mode 100644 index 0000000..085f211 --- /dev/null +++ b/retro/shaders/sharp-bilinear-scanlines.slang @@ -0,0 +1,87 @@ +#version 450 + +layout(push_constant) uniform Push +{ + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; + float SCANLINE_BASE_BRIGHTNESS; + float SCANLINE_HORIZONTAL_MODULATION; + float SCANLINE_VERTICAL_MODULATION; +} params; + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; +} global; + +/* + Author: rsn8887 (based on TheMaister) + License: Public domain + + This is an integer prescale filter that should be combined + with a bilinear hardware filtering (GL_BILINEAR filter or some such) to achieve + a smooth scaling result with minimum blur. This is good for pixelgraphics + that are scaled by non-integer factors. + + The prescale factor and texel coordinates are precalculated + in the vertex shader for speed. +*/ + +// Parameter lines go here: +#pragma parameter SCANLINE_BASE_BRIGHTNESS "Scanline Base Brightness" 0.60 0.0 1.0 0.01 +#pragma parameter SCANLINE_HORIZONTAL_MODULATION "Scanline Horizontal Modulation" 0.0 0.0 2.00 0.01 +#pragma parameter SCANLINE_VERTICAL_MODULATION "Scanline Vertical Modulation" 0.75 0.0 2.0 0.01 + +#define pi 3.141592654 + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; +layout(location = 1) out vec2 precalc_texel; +layout(location = 2) out vec2 precalc_scale; +layout(location = 3) out vec2 omega; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; + omega = vec2(2.0 * pi * params.SourceSize.x, 2.0 * pi * params.SourceSize.y); + precalc_scale = max(floor(params.OutputSize.xy / params.SourceSize.xy), vec2(1.0, 1.0)); + precalc_texel = vTexCoord * params.SourceSize.xy; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 1) in vec2 precalc_texel; +layout(location = 2) in vec2 precalc_scale; +layout(location = 3) in vec2 omega; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +void main() +{ + vec2 texel = precalc_texel; + vec2 scale = precalc_scale; + vec2 texel_floored = floor(texel); + vec2 s = fract(texel); + vec2 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. + + vec2 center_dist = s - 0.5; + vec2 f = (center_dist - clamp(center_dist, -region_range, region_range)) * scale + 0.5; + + vec2 mod_texel = texel_floored + f; + + vec3 res = texture(Source, mod_texel / params.SourceSize.xy).xyz; + + // thick scanlines (thickness pre-calculated in vertex shader based on source resolution) + vec2 sine_comp = vec2(params.SCANLINE_HORIZONTAL_MODULATION, params.SCANLINE_VERTICAL_MODULATION); + + vec3 scanline = res * (params.SCANLINE_BASE_BRIGHTNESS + dot(sine_comp * sin(vTexCoord * omega), vec2(1.0, 1.0))); + + FragColor = vec4(scanline.rgb, 1.0); +} diff --git a/retro/shaders/sharp-bilinear-simple.slang b/retro/shaders/sharp-bilinear-simple.slang new file mode 100644 index 0000000..eddaa9d --- /dev/null +++ b/retro/shaders/sharp-bilinear-simple.slang @@ -0,0 +1,67 @@ +#version 450 + +layout(push_constant) uniform Push +{ + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} params; + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; +} global; + +/* + Author: rsn8887 (based on TheMaister) + License: Public domain + + This is an integer prescale filter that should be combined + with a bilinear hardware filtering (GL_BILINEAR filter or some such) to achieve + a smooth scaling result with minimum blur. This is good for pixelgraphics + that are scaled by non-integer factors. + + The prescale factor and texel coordinates are precalculated + in the vertex shader for speed. +*/ + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; +layout(location = 1) out vec2 precalc_texel; +layout(location = 2) out vec2 precalc_scale; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; + precalc_scale = max(floor(params.OutputSize.xy / params.SourceSize.xy), vec2(1.0, 1.0)); + precalc_texel = vTexCoord * params.SourceSize.xy; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 1) in vec2 precalc_texel; +layout(location = 2) in vec2 precalc_scale; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +void main() +{ + vec2 texel = precalc_texel; + vec2 scale = precalc_scale; + vec2 texel_floored = floor(texel); + vec2 s = fract(texel); + vec2 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. + + vec2 center_dist = s - 0.5; + vec2 f = (center_dist - clamp(center_dist, -region_range, region_range)) * scale + 0.5; + + vec2 mod_texel = texel_floored + f; + + FragColor = vec4(texture(Source, mod_texel / params.SourceSize.xy).rgb, 1.0); +} diff --git a/retro/sharp-bilinear-2x-prescale.slangp b/retro/sharp-bilinear-2x-prescale.slangp new file mode 100644 index 0000000..54cf3a8 --- /dev/null +++ b/retro/sharp-bilinear-2x-prescale.slangp @@ -0,0 +1,7 @@ +shaders = 2 +shader0 = "../stock.slang" +filter_linear0 = false +scale_type0 = source +scale0 = 2.0 +shader1 = "../stock.slang" +filter_linear1 = true \ No newline at end of file diff --git a/retro/sharp-bilinear-scanlines.slangp b/retro/sharp-bilinear-scanlines.slangp new file mode 100644 index 0000000..dc8bc5f --- /dev/null +++ b/retro/sharp-bilinear-scanlines.slangp @@ -0,0 +1,4 @@ +shaders = 1 + +shader0 = "shaders/sharp-bilinear-scanlines.slang" +filter_linear0 = true diff --git a/retro/sharp-bilinear-simple.slangp b/retro/sharp-bilinear-simple.slangp new file mode 100644 index 0000000..c78dfdb --- /dev/null +++ b/retro/sharp-bilinear-simple.slangp @@ -0,0 +1,4 @@ +shaders = 1 + +shader0 = "shaders/sharp-bilinear-simple.slang" +filter_linear0 = true