From 65cb75e53c57bb492f9af1fa573e876e4dd1fab9 Mon Sep 17 00:00:00 2001 From: MajorPainTheCactus Date: Sat, 4 Sep 2021 11:17:02 +0100 Subject: [PATCH] Added integer scaling mode pixel perfect scanline shader for use with the new HDR mode in RetroArch --- .../shaders/integer-scaling-scanlines.slang | 89 +++++++++++++++++++ .../shaders/integer-scaling-scanlines.slangp | 4 + 2 files changed, 93 insertions(+) create mode 100644 scanlines/shaders/integer-scaling-scanlines.slang create mode 100644 scanlines/shaders/integer-scaling-scanlines.slangp diff --git a/scanlines/shaders/integer-scaling-scanlines.slang b/scanlines/shaders/integer-scaling-scanlines.slang new file mode 100644 index 0000000..7fc50f4 --- /dev/null +++ b/scanlines/shaders/integer-scaling-scanlines.slang @@ -0,0 +1,89 @@ +/* +Simple pixel perfect scanlines for use *only* with interger scaling mode. +Perfect for low res output and low performance machines. +Also great if you dont want any moire patterns pin sharp scanlines +Use in combination with HDR mode in latest RetroArch and very bright +HDR monitors to get 'glowing' scanlines much like the original CRT's but sharper + +Recommended setting for 720p 1 pixel width scalines at 2x or 3x scalings +Recommended setting for 1080p 2 or 3 pixel width scalines at 4x or 5x scalings +Recommended setting for 4K 4 or 5 pixel width scalines at 8x or 9x scalings +*/ + +#version 450 + +layout(push_constant) uniform Push +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + float ScanlineWidth; + float ScreenWidth; + float ScreenHeight; +} params; + +#pragma parameter ScanlineWidth "Scanline Width" 4.0 0.0 20.0 1.0 +#pragma parameter ScreenWidth "Screen Width" 3840.0 0.0 7680.0 1.0 +#pragma parameter ScreenHeight "Screen Height" 2160.0 0.0 4320.0 1.0 + +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; +layout(location = 1) out float Scale; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; + + vec2 ScreenSize = max(params.OutputSize.xy, vec2(params.ScreenWidth, params.ScreenHeight)); + + if((params.SourceSize.x > ScreenSize.x) || (params.SourceSize.y > ScreenSize.y)) + { + Scale = 1.0; + } + else + { + float ScaleFactor = 2.0; + + while(((params.SourceSize.x * ScaleFactor) <= ScreenSize.x) && ((params.SourceSize.y * ScaleFactor) <= ScreenSize.y)) + { + ScaleFactor += 1.0; + } + + Scale = ScaleFactor - 1.0; + } +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 1) in float Scale; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +float ModInteger(float a, float b) +{ + float m = a - floor((a + 0.5) / b) * b; + return floor(m + 0.5); +} + +void main() +{ + vec2 InPixels = (vTexCoord * params.SourceSize.xy) * vec2(Scale); + + if(ModInteger(floor(InPixels.y), Scale) < params.ScanlineWidth) + { + FragColor = vec4(texture(Source, vTexCoord).xyz, 1.0); + } + else + { + FragColor = vec4(0.0,0.0,0.0,1.0); + } +} diff --git a/scanlines/shaders/integer-scaling-scanlines.slangp b/scanlines/shaders/integer-scaling-scanlines.slangp new file mode 100644 index 0000000..b817f7d --- /dev/null +++ b/scanlines/shaders/integer-scaling-scanlines.slangp @@ -0,0 +1,4 @@ +shaders = 1 + +shader0 = integer-scaling-scanlines.slang +filter_linear0 = false