From 7b1827a2134cb1b3687c56051944496a89a46ff6 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Tue, 16 Feb 2016 22:56:22 +0100 Subject: [PATCH] Add initial CRT-cubic and CRT-lanczos. --- crt/crt-cubic.slangp | 18 +++++++++++++ crt/crt-lanczos.slangp | 18 +++++++++++++ crt/cubic.slang | 51 ++++++++++++++++++++++++++++++++++++ crt/linearize.slang | 32 +++++++++++++++++++++++ crt/retroarch.slangp | 12 +++++++++ crt/scanline.slang | 59 ++++++++++++++++++++++++++++++++++++++++++ crt/sinc.slang | 55 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 245 insertions(+) create mode 100644 crt/crt-cubic.slangp create mode 100644 crt/crt-lanczos.slangp create mode 100644 crt/cubic.slang create mode 100644 crt/linearize.slang create mode 100644 crt/retroarch.slangp create mode 100644 crt/scanline.slang create mode 100644 crt/sinc.slang diff --git a/crt/crt-cubic.slangp b/crt/crt-cubic.slangp new file mode 100644 index 0000000..8a822d3 --- /dev/null +++ b/crt/crt-cubic.slangp @@ -0,0 +1,18 @@ +shaders = 3 +shader0 = linearize.slang +shader1 = cubic.slang +shader2 = scanline.slang + +filter_linear0 = false +scale_type0 = source +scale0 = 1.0 +srgb_framebuffer0 = true + +filter_linear1 = false +scale_type_x1 = viewport +scale_x1 = 1.0 +scale_type_y1 = source +scale_y1 = 1.0 +srgb_framebuffer1 = true + +filter_linear2 = false diff --git a/crt/crt-lanczos.slangp b/crt/crt-lanczos.slangp new file mode 100644 index 0000000..b53084a --- /dev/null +++ b/crt/crt-lanczos.slangp @@ -0,0 +1,18 @@ +shaders = 3 +shader0 = linearize.slang +shader1 = sinc.slang +shader2 = scanline.slang + +filter_linear0 = false +scale_type0 = source +scale0 = 1.0 +srgb_framebuffer0 = true + +filter_linear1 = false +scale_type_x1 = viewport +scale_x1 = 1.0 +scale_type_y1 = source +scale_y1 = 1.0 +srgb_framebuffer1 = true + +filter_linear2 = false diff --git a/crt/cubic.slang b/crt/cubic.slang new file mode 100644 index 0000000..888ddc1 --- /dev/null +++ b/crt/cubic.slang @@ -0,0 +1,51 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +#define PI 3.1415926535 + +void main() +{ + float u = vTexCoord.x * global.SourceSize.x - 0.5; + float a = fract(u); + vec2 tex = vec2((floor(u) + 0.5) * global.SourceSize.z, vTexCoord.y); + +#define TEX(x, y) textureLod(Source, tex + vec2(x * global.SourceSize.z, 0.0), 0.0).rgb + + vec3 i0 = TEX(-1, 0); + vec3 i1 = TEX( 0, 0); + vec3 i2 = TEX(+1, 0); + vec3 i3 = TEX(+2, 0); + + float a2 = a * a; + float a3 = a2 * a; + + vec3 color = i1 + + (i2 - i0) * 0.5 * a + + (i0 - (2.5 * i1) + (2.0 * i2) - (0.5 * i3)) * a2 + + ((i3 - i0) + 3.0 * (i1 - i2)) * 0.5 * a3; + + FragColor = vec4(color, 1.0); +} diff --git a/crt/linearize.slang b/crt/linearize.slang new file mode 100644 index 0000000..763412c --- /dev/null +++ b/crt/linearize.slang @@ -0,0 +1,32 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +vec3 gamma(vec3 v) +{ + return pow(v, vec3(2.2)); +} + +void main() +{ + FragColor = vec4(gamma(texture(Source, vTexCoord).rgb), 1.0); +} diff --git a/crt/retroarch.slangp b/crt/retroarch.slangp new file mode 100644 index 0000000..21da28b --- /dev/null +++ b/crt/retroarch.slangp @@ -0,0 +1,12 @@ +shaders = "1" +shader0 = "/home/maister/.config/retroarch/shaders/sinc.slang" +filter_linear0 = "false" +wrap_mode0 = "clamp_to_border" +mipmap_input0 = "false" +alias0 = "" +float_framebuffer0 = "false" +srgb_framebuffer0 = "true" +scale_type_x0 = "viewport" +scale_x0 = "1.000000" +scale_type_y0 = "source" +scale_y0 = "1.000000" diff --git a/crt/scanline.slang b/crt/scanline.slang new file mode 100644 index 0000000..c70031d --- /dev/null +++ b/crt/scanline.slang @@ -0,0 +1,59 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +vec3 invgamma(vec3 v) +{ + return pow(v, vec3(1.0 / 2.2)); +} + +float luma(vec3 col) +{ + return dot(col, vec3(0.29, 0.60, 0.11)); +} + +void main() +{ + vec2 tex = vTexCoord * global.SourceSize.xy; + + float frac = fract(tex.y) - 0.5; + tex.y = floor(tex.y) + 0.5; + tex = tex * global.SourceSize.zw; + +#define TEX(x, y) textureLod(Source, tex + vec2(0, y * global.SourceSize.w), 0.0).rgb + vec3 l0 = TEX(0, -1); + vec3 l1 = TEX(0, 0); + vec3 l2 = TEX(0, 1); + + vec3 dist = (3.5 - 1.0 * vec3(luma(l0), luma(l1), luma(l2))) * (frac + vec3(+1.0, 0.0, -1.0)); + dist = exp2(-dist * dist); + + vec3 color = + dist.x * l0 + + dist.y * l1 + + dist.z * l2; + + FragColor = vec4(invgamma(color), 1.0); +} diff --git a/crt/sinc.slang b/crt/sinc.slang new file mode 100644 index 0000000..01bd5ee --- /dev/null +++ b/crt/sinc.slang @@ -0,0 +1,55 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +#define PI 3.1415926535 + +float sinc(float x) +{ + return sin(x) / x; +} + +float kernel(float x) +{ + x = max(abs(x) * PI, 0.0001); + return sinc(x) * sinc(0.5 * x); +} + +void main() +{ + float u = vTexCoord.x * global.SourceSize.x - 0.5; + float a = fract(u); + vec2 tex = vec2((floor(u) + 0.5) * global.SourceSize.z, vTexCoord.y); + +#define TEX(x, y) textureLod(Source, tex + vec2(x * global.SourceSize.z, 0.0), 0.0).rgb + + vec3 color = + kernel(a + 1.0) * TEX(-1, 0) + + kernel(a ) * TEX( 0, 0) + + kernel(a - 1.0) * TEX( 1, 0) + + kernel(a - 2.0) * TEX( 2, 0); + + FragColor = vec4(color, 1.0); +}