GS: Update lottes crt shader.

This commit is contained in:
Stuart Kenny 2024-09-06 16:27:46 +01:00
parent 28e4b10fda
commit 076cc57125
4 changed files with 257 additions and 87 deletions

View File

@ -142,18 +142,19 @@ PS_OUTPUT ps_filter_complex(PS_INPUT input) // triangular
//Lottes CRT //Lottes CRT
#define MaskingType 4 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style. #define MaskingType 4 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style.
#define ScanBrightness -8.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter. #define ScanBrightness -8.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter.
#define FilterCRTAmount -1.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more. #define FilterCRTAmount -3.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more.
#define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments. #define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments.
#define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments. #define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments.
#define MaskAmountDark 0.80 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter. #define MaskAmountDark 0.50 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter.
#define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter. #define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter.
#define ResolutionScale 2.00 //[0.1 to 4.0] The scale of the image resolution. Lowering this can give off a nice retro TV look. Raising it can clear up the image. #define BloomPixel -1.50 //[-2.0 -0.5] Pixel bloom radius. Higher for increased softness of bloom.
#define MaskResolutionScale 0.80 //[0.1 to 2.0] The scale of the CRT mask resolution. Use this for balancing the scanline mask scale for difference resolution scaling. #define BloomScanLine -2.0 //[-4.0 -1.0] Scanline bloom radius. Higher for increased softness of bloom.
#define BloomAmount 0.15 //[0.0 1.0] Bloom intensity. Higher for brighter.
#define Shape 2.0 //[0.0 10.0] Kernal filter shape. Lower values will darken image and introduce moire patterns if used with curvature.
#define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled. #define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled.
float ToLinear1(float c) float ToLinear1(float c)
{ {
c = saturate(c);
return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4); return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
} }
@ -164,7 +165,6 @@ float3 ToLinear(float3 c)
float ToSrgb1(float c) float ToSrgb1(float c)
{ {
c = saturate(c);
return c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) - 0.055; return c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) - 0.055;
} }
@ -175,9 +175,7 @@ float3 ToSrgb(float3 c)
float3 Fetch(float2 pos, float2 off) float3 Fetch(float2 pos, float2 off)
{ {
float2 screenSize = u_source_resolution; pos = (floor(pos * u_target_size + off) + float2(0.5, 0.5)) / u_target_size;
float2 res = (screenSize * ResolutionScale);
pos = round(pos * res + off) / res;
if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5) if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5)
{ {
return float3(0.0, 0.0, 0.0); return float3(0.0, 0.0, 0.0);
@ -190,9 +188,7 @@ float3 Fetch(float2 pos, float2 off)
float2 Dist(float2 pos) float2 Dist(float2 pos)
{ {
float2 crtRes = u_rcp_target_resolution; pos = pos * float2(640, 480);
float2 res = (crtRes * MaskResolutionScale);
pos = (pos * res);
return -((pos - floor(pos)) - float2(0.5, 0.5)); return -((pos - floor(pos)) - float2(0.5, 0.5));
} }
@ -215,7 +211,7 @@ float3 Horz3(float2 pos, float off)
float wc = Gaus(dst + 0.0, scale); float wc = Gaus(dst + 0.0, scale);
float wd = Gaus(dst + 1.0, scale); float wd = Gaus(dst + 1.0, scale);
return (b * wb) + (c * wc) + (d * wd) / (wb + wc + wd); return (b * wb + c * wc + d * wd) / (wb + wc + wd);
} }
float3 Horz5(float2 pos, float off) float3 Horz5(float2 pos, float off)
@ -236,7 +232,32 @@ float3 Horz5(float2 pos, float off)
float wd = Gaus(dst + 1.0, scale); float wd = Gaus(dst + 1.0, scale);
float we = Gaus(dst + 2.0, scale); float we = Gaus(dst + 2.0, scale);
return (a * wa) + (b * wb) + (c * wc) + (d * wd) + (e * we) / (wa + wb + wc + wd + we); return (a * wa + b * wb + c * wc + d * wd + e * we) / (wa + wb + wc + wd + we);
}
float3 Horz7(float2 pos, float off)
{
float3 a = Fetch(pos, float2(-3.0, off));
float3 b = Fetch(pos, float2(-2.0, off));
float3 c = Fetch(pos, float2(-1.0, off));
float3 d = Fetch(pos, float2( 0.0, off));
float3 e = Fetch(pos, float2( 1.0, off));
float3 f = Fetch(pos, float2( 2.0, off));
float3 g = Fetch(pos, float2( 3.0, off));
float dst = Dist(pos).x;
// Convert distance to weight.
float scale = BloomPixel;
float wa = Gaus(dst - 3.0, scale);
float wb = Gaus(dst - 2.0, scale);
float wc = Gaus(dst - 1.0, scale);
float wd = Gaus(dst + 0.0, scale);
float we = Gaus(dst + 1.0, scale);
float wf = Gaus(dst + 2.0, scale);
float wg = Gaus(dst + 3.0, scale);
// Return filtered sample.
return (a * wa + b * wb + c * wc + d * wd + e * we + f * wf + g * wg) / (wa + wb + wc + wd + we + wf + wg);
} }
// Return scanline weight. // Return scanline weight.
@ -246,6 +267,13 @@ float Scan(float2 pos, float off)
return Gaus(dst + off, ScanBrightness); return Gaus(dst + off, ScanBrightness);
} }
float BloomScan(float2 pos, float off)
{
float dst = Dist(pos).y;
return Gaus(dst + off, BloomScanLine);
}
float3 Tri(float2 pos) float3 Tri(float2 pos)
{ {
float3 a = Horz3(pos, -1.0); float3 a = Horz3(pos, -1.0);
@ -259,6 +287,23 @@ float3 Tri(float2 pos)
return (a * wa) + (b * wb) + (c * wc); return (a * wa) + (b * wb) + (c * wc);
} }
float3 Bloom(float2 pos)
{
float3 a = Horz5(pos,-2.0);
float3 b = Horz7(pos,-1.0);
float3 c = Horz7(pos, 0.0);
float3 d = Horz7(pos, 1.0);
float3 e = Horz5(pos, 2.0);
float wa = BloomScan(pos,-2.0);
float wb = BloomScan(pos,-1.0);
float wc = BloomScan(pos, 0.0);
float wd = BloomScan(pos, 1.0);
float we = BloomScan(pos, 2.0);
return a * wa + b * wb + c * wc + d * wd + e * we;
}
float2 Warp(float2 pos) float2 Warp(float2 pos)
{ {
pos = pos * 2.0 - 1.0; pos = pos * 2.0 - 1.0;
@ -368,18 +413,17 @@ float3 Mask(float2 pos)
float4 LottesCRTPass(float4 fragcoord) float4 LottesCRTPass(float4 fragcoord)
{ {
float4 color;
fragcoord -= u_target_rect; fragcoord -= u_target_rect;
float2 inSize = u_target_resolution - (2 * u_target_rect.xy); float2 inSize = u_target_resolution - (2 * u_target_rect.xy);
float4 color;
float2 pos = Warp(fragcoord.xy / inSize);
#if UseShadowMask == 0 float2 pos = Warp(fragcoord.xy / inSize);
color.rgb = Tri(pos); color.rgb = Tri(pos);
#else color.rgb += Bloom(pos) * BloomAmount;
color.rgb = Tri(pos) * Mask(fragcoord.xy); #if UseShadowMask
color.rgb *= Mask(fragcoord.xy);
#endif #endif
color.rgb = ToSrgb(color.rgb); color.rgb = ToSrgb(color.rgb);
color.a = 1.0;
return color; return color;
} }

View File

@ -139,20 +139,19 @@ void ps_filter_complex()
#define MaskingType 4 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style. #define MaskingType 4 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style.
#define ScanBrightness -8.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter. #define ScanBrightness -8.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter.
#define FilterCRTAmount -1.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more. #define FilterCRTAmount -3.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more.
#define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments. #define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments.
#define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments. #define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments.
#define MaskAmountDark 0.80 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter. #define MaskAmountDark 0.50 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter.
#define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter. #define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter.
#define ResolutionScale 2.00 //[0.1 to 4.0] The scale of the image resolution. Lowering this can give off a nice retro TV look. Raising it can clear up the image. #define BloomPixel -1.50 //[-2.0 -0.5] Pixel bloom radius. Higher for increased softness of bloom.
#define MaskResolutionScale 0.80 //[0.1 to 2.0] The scale of the CRT mask resolution. Use this for balancing the scanline mask scale for difference resolution scaling. #define BloomScanLine -2.0 //[-4.0 -1.0] Scanline bloom radius. Higher for increased softness of bloom.
#define BloomAmount 0.15 //[0.0 1.0] Bloom intensity. Higher for brighter.
#define Shape 2.0 //[0.0 10.0] Kernal filter shape. Lower values will darken image and introduce moire patterns if used with curvature.
#define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled. #define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled.
#define saturate(x) clamp(x, 0.0, 1.0)
float ToLinear1(float c) float ToLinear1(float c)
{ {
c = saturate(c);
return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4); return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
} }
@ -163,7 +162,6 @@ vec3 ToLinear(vec3 c)
float ToSrgb1(float c) float ToSrgb1(float c)
{ {
c = saturate(c);
return c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) - 0.055; return c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) - 0.055;
} }
@ -174,9 +172,7 @@ vec3 ToSrgb(vec3 c)
vec3 Fetch(vec2 pos, vec2 off) vec3 Fetch(vec2 pos, vec2 off)
{ {
vec2 screenSize = u_source_resolution; pos = (floor(pos * u_target_size + off) + vec2(0.5, 0.5)) / u_target_size;
vec2 res = (screenSize * ResolutionScale);
pos = round(pos * res + off) / res;
if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5) if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5)
{ {
return vec3(0.0, 0.0, 0.0); return vec3(0.0, 0.0, 0.0);
@ -189,16 +185,14 @@ vec3 Fetch(vec2 pos, vec2 off)
vec2 Dist(vec2 pos) vec2 Dist(vec2 pos)
{ {
vec2 crtRes = u_rcp_target_resolution; pos = pos * vec2(640, 480);
vec2 res = (crtRes * MaskResolutionScale);
pos = (pos * res);
return -((pos - floor(pos)) - vec2(0.5, 0.5)); return -((pos - floor(pos)) - vec2(0.5, 0.5));
} }
float Gaus(float pos, float scale) float Gaus(float pos, float scale)
{ {
return exp2(scale * pos * pos); return exp2(scale * pow(abs(pos), Shape));
} }
vec3 Horz3(vec2 pos, float off) vec3 Horz3(vec2 pos, float off)
@ -214,7 +208,7 @@ vec3 Horz3(vec2 pos, float off)
float wc = Gaus(dst + 0.0, scale); float wc = Gaus(dst + 0.0, scale);
float wd = Gaus(dst + 1.0, scale); float wd = Gaus(dst + 1.0, scale);
return (b * wb) + (c * wc) + (d * wd) / (wb + wc + wd); return (b * wb + c * wc + d * wd) / (wb + wc + wd);
} }
vec3 Horz5(vec2 pos, float off) vec3 Horz5(vec2 pos, float off)
@ -235,7 +229,32 @@ vec3 Horz5(vec2 pos, float off)
float wd = Gaus(dst + 1.0, scale); float wd = Gaus(dst + 1.0, scale);
float we = Gaus(dst + 2.0, scale); float we = Gaus(dst + 2.0, scale);
return (a * wa) + (b * wb) + (c * wc) + (d * wd) + (e * we) / (wa + wb + wc + wd + we); return (a * wa + b * wb + c * wc + d * wd + e * we) / (wa + wb + wc + wd + we);
}
vec3 Horz7(vec2 pos, float off)
{
vec3 a = Fetch(pos, vec2(-3.0, off));
vec3 b = Fetch(pos, vec2(-2.0, off));
vec3 c = Fetch(pos, vec2(-1.0, off));
vec3 d = Fetch(pos, vec2( 0.0, off));
vec3 e = Fetch(pos, vec2( 1.0, off));
vec3 f = Fetch(pos, vec2( 2.0, off));
vec3 g = Fetch(pos, vec2( 3.0, off));
float dst = Dist(pos).x;
// Convert distance to weight.
float scale = BloomPixel;
float wa = Gaus(dst - 3.0, scale);
float wb = Gaus(dst - 2.0, scale);
float wc = Gaus(dst - 1.0, scale);
float wd = Gaus(dst + 0.0, scale);
float we = Gaus(dst + 1.0, scale);
float wf = Gaus(dst + 2.0, scale);
float wg = Gaus(dst + 3.0, scale);
// Return filtered sample.
return (a * wa + b * wb + c * wc + d * wd + e * we + f * wf + g * wg) / (wa + wb + wc + wd + we + wf + wg);
} }
// Return scanline weight. // Return scanline weight.
@ -245,6 +264,13 @@ float Scan(vec2 pos, float off)
return Gaus(dst + off, ScanBrightness); return Gaus(dst + off, ScanBrightness);
} }
float BloomScan(vec2 pos, float off)
{
float dst = Dist(pos).y;
return Gaus(dst + off, BloomScanLine);
}
vec3 Tri(vec2 pos) vec3 Tri(vec2 pos)
{ {
vec3 a = Horz3(pos, -1.0); vec3 a = Horz3(pos, -1.0);
@ -258,6 +284,23 @@ vec3 Tri(vec2 pos)
return (a * wa) + (b * wb) + (c * wc); return (a * wa) + (b * wb) + (c * wc);
} }
vec3 Bloom(vec2 pos)
{
vec3 a = Horz5(pos,-2.0);
vec3 b = Horz7(pos,-1.0);
vec3 c = Horz7(pos, 0.0);
vec3 d = Horz7(pos, 1.0);
vec3 e = Horz5(pos, 2.0);
float wa = BloomScan(pos,-2.0);
float wb = BloomScan(pos,-1.0);
float wc = BloomScan(pos, 0.0);
float wd = BloomScan(pos, 1.0);
float we = BloomScan(pos, 2.0);
return a * wa + b * wb + c * wc + d * wd + e * we;
}
vec2 Warp(vec2 pos) vec2 Warp(vec2 pos)
{ {
pos = pos * 2.0 - 1.0; pos = pos * 2.0 - 1.0;
@ -373,14 +416,12 @@ vec4 LottesCRTPass()
vec2 inSize = u_target_resolution - (2.0 * u_target_rect.xy); vec2 inSize = u_target_resolution - (2.0 * u_target_rect.xy);
vec2 pos = Warp(fragcoord.xy / inSize); vec2 pos = Warp(fragcoord.xy / inSize);
#if UseShadowMask == 0
color.rgb = Tri(pos); color.rgb = Tri(pos);
#else color.rgb += Bloom(pos) * BloomAmount;
color.rgb = Tri(pos) * Mask(fragcoord.xy); #if UseShadowMask
color.rgb *= Mask(fragcoord.xy);
#endif #endif
color.rgb = ToSrgb(color.rgb); color.rgb = ToSrgb(color.rgb);
color.a = 1.0;
return color; return color;
} }

View File

@ -110,20 +110,19 @@ void ps_filter_complex() // triangular
#define MaskingType 4 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style. #define MaskingType 4 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style.
#define ScanBrightness -8.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter. #define ScanBrightness -8.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter.
#define FilterCRTAmount -1.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more. #define FilterCRTAmount -3.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more.
#define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments. #define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments.
#define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments. #define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments.
#define MaskAmountDark 0.80 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter. #define MaskAmountDark 0.50 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter.
#define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter. #define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter.
#define ResolutionScale 2.00 //[0.1 to 4.0] The scale of the image resolution. Lowering this can give off a nice retro TV look. Raising it can clear up the image. #define BloomPixel -1.50 //[-2.0 -0.5] Pixel bloom radius. Higher for increased softness of bloom.
#define MaskResolutionScale 0.80 //[0.1 to 2.0] The scale of the CRT mask resolution. Use this for balancing the scanline mask scale for difference resolution scaling. #define BloomScanLine -2.0 //[-4.0 -1.0] Scanline bloom radius. Higher for increased softness of bloom.
#define BloomAmount 0.15 //[0.0 1.0] Bloom intensity. Higher for brighter.
#define Shape 2.0 //[0.0 10.0] Kernal filter shape. Lower values will darken image and introduce moire patterns if used with curvature.
#define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled. #define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled.
#define saturate(x) clamp(x, 0.0, 1.0)
float ToLinear1(float c) float ToLinear1(float c)
{ {
c = saturate(c);
return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4); return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
} }
@ -134,7 +133,6 @@ vec3 ToLinear(vec3 c)
float ToSrgb1(float c) float ToSrgb1(float c)
{ {
c = saturate(c);
return c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) - 0.055; return c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) - 0.055;
} }
@ -145,9 +143,7 @@ vec3 ToSrgb(vec3 c)
vec3 Fetch(vec2 pos, vec2 off) vec3 Fetch(vec2 pos, vec2 off)
{ {
vec2 screenSize = u_source_resolution; pos = (floor(pos * u_target_size + off) + vec2(0.5, 0.5)) / u_target_size;
vec2 res = (screenSize * ResolutionScale);
pos = round(pos * res + off) / res;
if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5) if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5)
{ {
return vec3(0.0, 0.0, 0.0); return vec3(0.0, 0.0, 0.0);
@ -160,16 +156,14 @@ vec3 Fetch(vec2 pos, vec2 off)
vec2 Dist(vec2 pos) vec2 Dist(vec2 pos)
{ {
vec2 crtRes = u_rcp_target_resolution; pos = pos * vec2(640, 480);
vec2 res = (crtRes * MaskResolutionScale);
pos = (pos * res);
return -((pos - floor(pos)) - vec2(0.5, 0.5)); return -((pos - floor(pos)) - vec2(0.5, 0.5));
} }
float Gaus(float pos, float scale) float Gaus(float pos, float scale)
{ {
return exp2(scale * pos * pos); return exp2(scale * pow(abs(pos), Shape));
} }
vec3 Horz3(vec2 pos, float off) vec3 Horz3(vec2 pos, float off)
@ -185,7 +179,7 @@ vec3 Horz3(vec2 pos, float off)
float wc = Gaus(dst + 0.0, scale); float wc = Gaus(dst + 0.0, scale);
float wd = Gaus(dst + 1.0, scale); float wd = Gaus(dst + 1.0, scale);
return (b * wb) + (c * wc) + (d * wd) / (wb + wc + wd); return (b * wb + c * wc + d * wd) / (wb + wc + wd);
} }
vec3 Horz5(vec2 pos, float off) vec3 Horz5(vec2 pos, float off)
@ -206,7 +200,32 @@ vec3 Horz5(vec2 pos, float off)
float wd = Gaus(dst + 1.0, scale); float wd = Gaus(dst + 1.0, scale);
float we = Gaus(dst + 2.0, scale); float we = Gaus(dst + 2.0, scale);
return (a * wa) + (b * wb) + (c * wc) + (d * wd) + (e * we) / (wa + wb + wc + wd + we); return (a * wa + b * wb + c * wc + d * wd + e * we) / (wa + wb + wc + wd + we);
}
vec3 Horz7(vec2 pos, float off)
{
vec3 a = Fetch(pos, vec2(-3.0, off));
vec3 b = Fetch(pos, vec2(-2.0, off));
vec3 c = Fetch(pos, vec2(-1.0, off));
vec3 d = Fetch(pos, vec2( 0.0, off));
vec3 e = Fetch(pos, vec2( 1.0, off));
vec3 f = Fetch(pos, vec2( 2.0, off));
vec3 g = Fetch(pos, vec2( 3.0, off));
float dst = Dist(pos).x;
// Convert distance to weight.
float scale = BloomPixel;
float wa = Gaus(dst - 3.0, scale);
float wb = Gaus(dst - 2.0, scale);
float wc = Gaus(dst - 1.0, scale);
float wd = Gaus(dst + 0.0, scale);
float we = Gaus(dst + 1.0, scale);
float wf = Gaus(dst + 2.0, scale);
float wg = Gaus(dst + 3.0, scale);
// Return filtered sample.
return (a * wa + b * wb + c * wc + d * wd + e * we + f * wf + g * wg) / (wa + wb + wc + wd + we + wf + wg);
} }
// Return scanline weight. // Return scanline weight.
@ -216,6 +235,13 @@ float Scan(vec2 pos, float off)
return Gaus(dst + off, ScanBrightness); return Gaus(dst + off, ScanBrightness);
} }
float BloomScan(vec2 pos, float off)
{
float dst = Dist(pos).y;
return Gaus(dst + off, BloomScanLine);
}
vec3 Tri(vec2 pos) vec3 Tri(vec2 pos)
{ {
vec3 a = Horz3(pos, -1.0); vec3 a = Horz3(pos, -1.0);
@ -229,6 +255,23 @@ vec3 Tri(vec2 pos)
return (a * wa) + (b * wb) + (c * wc); return (a * wa) + (b * wb) + (c * wc);
} }
vec3 Bloom(vec2 pos)
{
vec3 a = Horz5(pos,-2.0);
vec3 b = Horz7(pos,-1.0);
vec3 c = Horz7(pos, 0.0);
vec3 d = Horz7(pos, 1.0);
vec3 e = Horz5(pos, 2.0);
float wa = BloomScan(pos,-2.0);
float wb = BloomScan(pos,-1.0);
float wc = BloomScan(pos, 0.0);
float wd = BloomScan(pos, 1.0);
float we = BloomScan(pos, 2.0);
return a * wa + b * wb + c * wc + d * wd + e * we;
}
vec2 Warp(vec2 pos) vec2 Warp(vec2 pos)
{ {
pos = pos * 2.0 - 1.0; pos = pos * 2.0 - 1.0;
@ -338,19 +381,17 @@ vec3 Mask(vec2 pos)
vec4 LottesCRTPass() vec4 LottesCRTPass()
{ {
vec4 fragcoord = gl_FragCoord - u_target_rect;
vec4 color; vec4 color;
vec4 fragcoord = gl_FragCoord - u_target_rect;
vec2 inSize = u_target_resolution - (2 * u_target_rect.xy); vec2 inSize = u_target_resolution - (2 * u_target_rect.xy);
vec2 pos = Warp(fragcoord.xy / inSize); vec2 pos = Warp(fragcoord.xy / inSize);
#if UseShadowMask == 0
color.rgb = Tri(pos); color.rgb = Tri(pos);
#else color.rgb += Bloom(pos) * BloomAmount;
color.rgb = Tri(pos) * Mask(fragcoord.xy); #if UseShadowMask
color.rgb *= Mask(fragcoord.xy);
#endif #endif
color.rgb = ToSrgb(color.rgb); color.rgb = ToSrgb(color.rgb);
color.a = 1.0;
return color; return color;
} }

View File

@ -62,13 +62,15 @@ fragment float4 ps_filter_complex(ConvertShaderData data [[stage_in]], ConvertPS
#define MaskingType 4 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style. #define MaskingType 4 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style.
#define ScanBrightness -8.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter. #define ScanBrightness -8.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter.
#define FilterCRTAmount -1.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more. #define FilterCRTAmount -3.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more.
#define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments. #define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments.
#define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments. #define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments.
#define MaskAmountDark 0.80 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter. #define MaskAmountDark 0.50 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter.
#define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter. #define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter.
#define ResolutionScale 2.00 //[0.1 to 4.0] The scale of the image resolution. Lowering this can give off a nice retro TV look. Raising it can clear up the image. #define BloomPixel -1.50 //[-2.0 -0.5] Pixel bloom radius. Higher for increased softness of bloom.
#define MaskResolutionScale 0.80 //[0.1 to 2.0] The scale of the CRT mask resolution. Use this for balancing the scanline mask scale for difference resolution scaling. #define BloomScanLine -2.0 //[-4.0 -1.0] Scanline bloom radius. Higher for increased softness of bloom.
#define BloomAmount 0.15 //[0.0 1.0] Bloom intensity. Higher for brighter.
#define Shape 2.0 //[0.0 10.0] Kernal filter shape. Lower values will darken image and introduce moire patterns if used with curvature.
#define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled. #define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled.
struct LottesCRTPass struct LottesCRTPass
@ -79,7 +81,6 @@ struct LottesCRTPass
float ToLinear1(float c) float ToLinear1(float c)
{ {
c = saturate(c);
return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4); return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
} }
@ -90,7 +91,6 @@ struct LottesCRTPass
float ToSrgb1(float c) float ToSrgb1(float c)
{ {
c = saturate(c);
return c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) - 0.055; return c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) - 0.055;
} }
@ -101,9 +101,7 @@ struct LottesCRTPass
float3 Fetch(float2 pos, float2 off) float3 Fetch(float2 pos, float2 off)
{ {
float2 screenSize = uniform.source_resolution; pos = (floor(pos * uniform.target_size + off) + float2(0.5, 0.5)) / uniform.target_size;
float2 scaledRes = (screenSize * ResolutionScale);
pos = round(pos * scaledRes + off) / scaledRes;
if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5) if (max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5)
{ {
return float3(0.0, 0.0, 0.0); return float3(0.0, 0.0, 0.0);
@ -116,16 +114,14 @@ struct LottesCRTPass
float2 Dist(float2 pos) float2 Dist(float2 pos)
{ {
float2 crtRes = uniform.rcp_target_resolution; pos = pos * float2(640, 480);
float2 res = (crtRes * MaskResolutionScale);
pos = (pos * res);
return -((pos - floor(pos)) - float2(0.5, 0.5)); return -((pos - floor(pos)) - float2(0.5, 0.5));
} }
float Gaus(float pos, float scale) float Gaus(float pos, float scale)
{ {
return exp2(scale * pos * pos); return exp2(scale * pow(abs(pos), Shape));
} }
float3 Horz3(float2 pos, float off) float3 Horz3(float2 pos, float off)
@ -141,7 +137,7 @@ struct LottesCRTPass
float wc = Gaus(dst + 0.0, scale); float wc = Gaus(dst + 0.0, scale);
float wd = Gaus(dst + 1.0, scale); float wd = Gaus(dst + 1.0, scale);
return (b * wb) + (c * wc) + (d * wd) / (wb + wc + wd); return (b * wb + c * wc + d * wd) / (wb + wc + wd);
} }
float3 Horz5(float2 pos, float off) float3 Horz5(float2 pos, float off)
@ -162,7 +158,32 @@ struct LottesCRTPass
float wd = Gaus(dst + 1.0, scale); float wd = Gaus(dst + 1.0, scale);
float we = Gaus(dst + 2.0, scale); float we = Gaus(dst + 2.0, scale);
return (a * wa) + (b * wb) + (c * wc) + (d * wd) + (e * we) / (wa + wb + wc + wd + we); return (a * wa + b * wb + c * wc + d * wd + e * we) / (wa + wb + wc + wd + we);
}
float3 Horz7(float2 pos, float off)
{
float3 a = Fetch(pos, float2(-3.0, off));
float3 b = Fetch(pos, float2(-2.0, off));
float3 c = Fetch(pos, float2(-1.0, off));
float3 d = Fetch(pos, float2( 0.0, off));
float3 e = Fetch(pos, float2( 1.0, off));
float3 f = Fetch(pos, float2( 2.0, off));
float3 g = Fetch(pos, float2( 3.0, off));
float dst = Dist(pos).x;
// Convert distance to weight.
float scale = BloomPixel;
float wa = Gaus(dst - 3.0, scale);
float wb = Gaus(dst - 2.0, scale);
float wc = Gaus(dst - 1.0, scale);
float wd = Gaus(dst + 0.0, scale);
float we = Gaus(dst + 1.0, scale);
float wf = Gaus(dst + 2.0, scale);
float wg = Gaus(dst + 3.0, scale);
// Return filtered sample.
return (a * wa + b * wb + c * wc + d * wd + e * we + f * wf + g * wg) / (wa + wb + wc + wd + we + wf + wg);
} }
// Return scanline weight. // Return scanline weight.
@ -172,6 +193,13 @@ struct LottesCRTPass
return Gaus(dst + off, ScanBrightness); return Gaus(dst + off, ScanBrightness);
} }
float BloomScan(float2 pos, float off)
{
float dst = Dist(pos).y;
return Gaus(dst + off, BloomScanLine);
}
float3 Tri(float2 pos) float3 Tri(float2 pos)
{ {
float3 a = Horz3(pos, -1.0); float3 a = Horz3(pos, -1.0);
@ -185,6 +213,23 @@ struct LottesCRTPass
return (a * wa) + (b * wb) + (c * wc); return (a * wa) + (b * wb) + (c * wc);
} }
float3 Bloom(float2 pos)
{
float3 a = Horz5(pos,-2.0);
float3 b = Horz7(pos,-1.0);
float3 c = Horz7(pos, 0.0);
float3 d = Horz7(pos, 1.0);
float3 e = Horz5(pos, 2.0);
float wa = BloomScan(pos,-2.0);
float wb = BloomScan(pos,-1.0);
float wc = BloomScan(pos, 0.0);
float wd = BloomScan(pos, 1.0);
float we = BloomScan(pos, 2.0);
return a * wa + b * wb + c * wc + d * wd + e * we;
}
float2 Warp(float2 pos) float2 Warp(float2 pos)
{ {
pos = pos * 2.0 - 1.0; pos = pos * 2.0 - 1.0;
@ -294,18 +339,17 @@ struct LottesCRTPass
float4 Run(float4 fragcoord) float4 Run(float4 fragcoord)
{ {
float4 color;
fragcoord -= uniform.target_rect; fragcoord -= uniform.target_rect;
float2 inSize = uniform.target_resolution - (2 * uniform.target_rect.xy); float2 inSize = uniform.target_resolution - (2 * uniform.target_rect.xy);
float4 color;
float2 pos = Warp(fragcoord.xy / inSize);
#if UseShadowMask == 0 float2 pos = Warp(fragcoord.xy / inSize);
color.rgb = Tri(pos); color.rgb = Tri(pos);
#else color.rgb += Bloom(pos) * BloomAmount;
color.rgb = Tri(pos) * Mask(fragcoord.xy); #if UseShadowMask
#endif color.rgb *= Mask(fragcoord.xy);
#endif
color.rgb = ToSrgb(color.rgb); color.rgb = ToSrgb(color.rgb);
color.a = 1.0;
return color; return color;
} }