From 362420d8db984cee4da46d3d797268fa172712e8 Mon Sep 17 00:00:00 2001 From: metallic77 <43163462+metallic77@users.noreply.github.com> Date: Tue, 13 Jun 2023 16:25:50 +0300 Subject: [PATCH] add crt-simple (#444) * Add files via upload * Add files via upload * Update crt-simple.slang * Add files via upload * add crt-pocket --- crt/crt-pocket.slangp | 10 + crt/crt-simple.slangp | 5 + crt/shaders/crt-pocket.slang | 389 +++++++++++++++++++++++++++++++++++ crt/shaders/crt-simple.slang | 161 +++++++++++++++ 4 files changed, 565 insertions(+) create mode 100644 crt/crt-pocket.slangp create mode 100644 crt/crt-simple.slangp create mode 100644 crt/shaders/crt-pocket.slang create mode 100644 crt/shaders/crt-simple.slang diff --git a/crt/crt-pocket.slangp b/crt/crt-pocket.slangp new file mode 100644 index 0000000..8d58261 --- /dev/null +++ b/crt/crt-pocket.slangp @@ -0,0 +1,10 @@ +shaders = "2" +feedback_pass = "0" +shader0 = "../misc/shaders/chromaticity.slang" +scale_type0 = source +filter_linear0 = "false" +shader1 = "shaders/crt-pocket.slang" +filter_linear1 = "true" + + + diff --git a/crt/crt-simple.slangp b/crt/crt-simple.slangp new file mode 100644 index 0000000..b19d48d --- /dev/null +++ b/crt/crt-simple.slangp @@ -0,0 +1,5 @@ +shaders = 1 + +shader0 = shaders/crt-simple.slang +filter_linear0 = true +scale_type0 = viewport diff --git a/crt/shaders/crt-pocket.slang b/crt/shaders/crt-pocket.slang new file mode 100644 index 0000000..42e094d --- /dev/null +++ b/crt/shaders/crt-pocket.slang @@ -0,0 +1,389 @@ +#version 450 + + +layout(push_constant) uniform Push +{ + + float SHARPNESSX,SHARPNESSY,WARPX,WARPY,CORNER_ROUND,BORDER,SHADOWMASK,MASKLOW,MASKHIGH,DOTMASK_STRENGTH, +MASKDARK,MASKLIGHT,GAMMAIN,GAMMAOUT,SAT,SCANLOW,SCANHIGH,MINSCAN,VIGNETTE,VPOWER,VSTR,TEMP,SEGA, +BLACK,GLOW,SIZE,quality,nois,BGR,INTERLACE; +} params; + +// Parameter lines go here: +#pragma parameter SHARPNESSX " Horizontal Sharpness" 0.25 0.0 1.0 0.05 +#pragma parameter SHARPNESSY " Vertical Sharpness" 0.7 0.0 1.0 0.05 +#pragma parameter WARPX "Curvature Horizontal" 0.03 0.0 0.2 0.01 +#pragma parameter WARPY "Curvature Vertical" 0.05 0.0 0.2 0.01 +#pragma parameter CORNER_ROUND "Corner Roundness" 0.02 0.005 0.2 0.01 +#pragma parameter BORDER "Border Smoothness" 400.0 100.0 1000.0 25.0 +#pragma parameter INTERLACE "Interlace On/Off" 1.0 0.0 1.0 1.0 +#pragma parameter SCANLOW " Scanline Low Brightness" 0.15 0.01 1.0 0.05 +#pragma parameter SCANHIGH " Scanline High Brightness" 0.20 0.01 1.0 0.05 +#pragma parameter MINSCAN " Scanline Min Brightness" 0.04 0.01 1.0 0.01 +#pragma parameter SHADOWMASK "Mask Type, CGWG, Lottes 1-4" 5.0 -1.0 5.0 1.0 +#pragma parameter BGR "RGB/BGR subpixels" 1.0 0.0 1.0 1.0 +#pragma parameter MASKLOW "Mask Strength Low" 1.0 0.0 1.0 0.05 +#pragma parameter MASKHIGH "Mask Strength High" 0.75 0.0 1.0 0.05 +#pragma parameter DOTMASK_STRENGTH "CGWG Dot Mask Strength" 0.3 0.0 1.0 0.05 +#pragma parameter MASKDARK "Lottes Mask Dark" 0.5 0.0 2.0 0.1 +#pragma parameter MASKLIGHT "Lottes Mask Light" 1.5 0.0 2.0 0.1 +#pragma parameter GAMMAIN " Gamma In" 2.4 1.0 4.0 0.05 +#pragma parameter GAMMAOUT " Gamma Out" 2.25 1.0 4.0 0.05 +#pragma parameter BLACK " Black Level" 0.0 -0.20 0.20 0.01 +#pragma parameter TEMP " Color Temperature in Kelvins" 9311.0 1031.0 12047.0 72.0 +#pragma parameter SAT " Saturation" 1.0 0.0 2.0 0.01 +#pragma parameter SEGA " SEGA Lum Fix" 0.0 0.0 1.0 1.0 +#pragma parameter GLOW " Glow Strength" 0.12 0.0 1.0 0.01 +#pragma parameter quality " Glow Size" 1.0 0.0 1.5 0.05 +#pragma parameter VIGNETTE "Vignette On/Off" 1.0 0.0 1.0 1.0 +#pragma parameter VPOWER "Vignette Power" 0.1 0.0 1.0 0.01 +#pragma parameter VSTR "Vignette Strength" 45.0 0.0 50.0 1.0 +#pragma parameter nois "Noise strength" 0.4 0.0 1.0 0.01 + +#define SHARPNESSX params.SHARPNESSX +#define SHARPNESSY params.SHARPNESSY +#define WARPX params.WARPX +#define WARPY params.WARPY +#define CORNER_ROUND params.CORNER_ROUND +#define BORDER params.BORDER +#define SHADOWMASK params.SHADOWMASK +#define MASKLOW params.MASKLOW +#define MASKHIGH params.MASKHIGH +#define DOTMASK_STRENGTH params.DOTMASK_STRENGTH +#define MASKDARK params.MASKDARK +#define MASKLIGHT params.MASKLIGHT +#define GAMMAIN params.GAMMAIN +#define GAMMAOUT params.GAMMAOUT +#define SAT params.SAT +#define SCANLOW params.SCANLOW +#define SCANHIGH params.SCANHIGH +#define MINSCAN params.MINSCAN +#define VIGNETTE params.VIGNETTE +#define VPOWER params.VPOWER +#define VSTR params.VSTR +#define TEMP params.TEMP +#define SEGA params.SEGA +#define BLACK params.BLACK +#define GLOW params.GLOW +#define SIZE params.SIZE +#define quality params.quality +#define nois params.nois +#define BGR params.BGR +#define INTERLACE params.INTERLACE + + +#define CURVATURE +#define PI 3.141592654 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + mat4 MVP; +} global; + +#define SourceSize global.SourceSize +#define OriginalSize global.OriginalSize +#define OutputSize global.OutputSize +#define mod_factor vTexCoord.x * OutputSize.x/OriginalSize.x*SourceSize.x +#define scale vec4(SourceSize.xy/OriginalSize.xy,OriginalSize.xy/SourceSize.xy) +#define filterWidth (OriginalSize.y / OutputSize.y) / 3.0 + +#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 * 1.0001; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; + +layout(set = 0, binding = 1) uniform sampler2D Source; + + +#define iTimer (float(global.FrameCount) / 60.0) + + +vec2 curve(vec2 pos) +{ + pos = pos*2.0-1.0; + pos *= vec2(1.0 + (pos.y*pos.y)*WARPX, + 1.0 + (pos.x*pos.x)*WARPY); + + return pos*0.5 + 0.5; +} + +float corner(vec2 coord) +{ + coord *= scale.xy; + coord = (coord - vec2(0.5)) * 1.0 + vec2(0.5); + coord = min(coord, vec2(1.0)-coord) * vec2(1.0, OriginalSize.y/OriginalSize.x); + vec2 cdist = vec2(CORNER_ROUND); + coord = (cdist - min(coord,cdist)); + float dist = sqrt(dot(coord,coord)); + return clamp((cdist.x-dist)*BORDER,0.0, 1.0); +} + + + +float CalcScanLineWeight(float dist, float scan) +{ + return max(1.0-dist*dist/scan, MINSCAN); +} + +float ScanLine(float dy, float l) +{ + float scan = mix(SCANLOW, SCANHIGH, l); + float scanLineWeight = CalcScanLineWeight(dy, scan); + scanLineWeight += CalcScanLineWeight(dy-filterWidth, scan); + scanLineWeight += CalcScanLineWeight(dy+filterWidth, scan); + scanLineWeight *= 0.3333333; + return scanLineWeight; +} + + +// Shadow mask. +vec3 Mask(vec2 pos) +{ + vec3 mask = vec3(MASKDARK, MASKDARK, MASKDARK); + + // Very compressed TV style shadow mask. + if (SHADOWMASK == 1.0) + { + float line = MASKLIGHT; + float odd = 0.0; + + if (fract(pos.x/6.0) < 0.5) + odd = 1.0; + if (fract((pos.y + odd)/2.0) < 0.5) + line = MASKDARK; + + pos.x = fract(pos.x/3.0); + + if (pos.x < 0.333) (BGR == 1.0) ? mask.b = MASKLIGHT : mask.r = MASKLIGHT; + else if (pos.x < 0.666) mask.g = MASKLIGHT; + else (BGR == 1.0) ? mask.r = MASKLIGHT : mask.b = MASKLIGHT; + mask*=line; + } + + // Aperture-grille. + else if (SHADOWMASK == 2.0) + { + pos.x = fract(pos.x/3.0); + + if (pos.x < 0.333) (BGR == 1.0) ? mask.b = MASKLIGHT : mask.r = MASKLIGHT; + else if (pos.x < 0.666) mask.g = MASKLIGHT; + else (BGR == 1.0) ? mask.r = MASKLIGHT : mask.b = MASKLIGHT; + } + + // Stretched VGA style shadow mask (same as prior shaders). + else if (SHADOWMASK == 3.0) + { + pos.x += pos.y*3.0; + pos.x = fract(pos.x/6.0); + + if (pos.x < 0.333) (BGR == 1.0) ? mask.b = MASKLIGHT : mask.r = MASKLIGHT; + else if (pos.x < 0.666) mask.g = MASKLIGHT; + else (BGR == 1.0) ? mask.r = MASKLIGHT : mask.b = MASKLIGHT; + } + + // VGA style shadow mask. + else if (SHADOWMASK == 4.0) + { + pos.xy = floor(pos.xy*vec2(1.0, 0.5)); + pos.x += pos.y*3.0; + pos.x = fract(pos.x/6.0); + + if (pos.x < 0.333) (BGR == 1.0) ? mask.b = MASKLIGHT : mask.r = MASKLIGHT; + else if (pos.x < 0.666) mask.g = MASKLIGHT; + else (BGR == 1.0) ? mask.r = MASKLIGHT : mask.b = MASKLIGHT; + } + else if (SHADOWMASK == 5.0) + { + float line = MASKLIGHT; + float odd = 0.0; + + if (fract(pos.x/4.0) < 0.5) + odd = 1.0; + if (fract((pos.y + odd)/2.0) < 0.5) + line = MASKDARK; + + pos.x = fract(pos.x/2.0); + + if (pos.x < 0.5) {mask.r = MASKLIGHT; mask.b = MASKLIGHT;} + else mask.g = MASKLIGHT; + mask*=line; + } + + + + else if (SHADOWMASK == -1.0) mask = vec3(1.0); + return mask; +} + +mat3 vign() +{ + vec2 vpos = vTexCoord * SourceSize.xy/OriginalSize.xy; + vpos *= 1.0 - vpos; + + float vig = vpos.x * vpos.y * VSTR; + + vig = min(pow(vig, VPOWER), 1.0); + + if (VIGNETTE == 0.0) vig=1.0; + + return mat3(vig, 0, 0, + 0, vig, 0, + 0, 0, vig); +} + +float saturate(float v) + { + return clamp(v, 0.0, 1.0); + } + +// https://www.shadertoy.com/view/lsSXW1 +vec3 ColorTemperatureToRGB(float temperatureInKelvins) +{ + vec3 retColor; + temperatureInKelvins = clamp(temperatureInKelvins, 1000.0, 40000.0) / 100.0; + + if (temperatureInKelvins <= 66.0) + { + retColor.r = 1.0; + retColor.g = saturate(0.39008157876901960784 * log(temperatureInKelvins) - 0.63184144378862745098); + } + else + { + float t = temperatureInKelvins - 60.0; + retColor.r = saturate(1.29293618606274509804 * pow(t, -0.1332047592)); + retColor.g = saturate(1.12989086089529411765 * pow(t, -0.0755148492)); + } + + if (temperatureInKelvins >= 66.0) + retColor.b = 1.0; + else if(temperatureInKelvins <= 19.0) + retColor.b = 0.0; + else + retColor.b = saturate(0.54320678911019607843 * log(temperatureInKelvins - 10.0) - 1.19625408914); + + return retColor; +} + + +vec3 glow0 (vec2 uv) +{ + vec3 sum = vec3(0.0); + float x = uv.x; + float y = uv.y; + float xx = SourceSize.z*quality; + float yy = SourceSize.w*quality; + + sum += texture(Source, vec2(x-xx,y)).rgb*0.1; + sum += texture(Source,vec2(x-xx,y-yy)).rgb*0.1; + sum += texture(Source,vec2(x,y-yy)).rgb*0.1; + sum += texture(Source,vec2(x+xx,y-yy)).rgb*0.1; + sum += texture(Source,vec2(x+xx,y)).rgb*0.1; + sum += texture(Source,vec2(x+xx,y+yy)).rgb*0.1; + sum += texture(Source,vec2(x, y-yy)).rgb*0.1; + sum += texture(Source,vec2(x+xx,y+yy)).rgb*0.1; + + sum += texture(Source,vec2(x-2.0*xx,y)).rgb*0.05; + sum += texture(Source,vec2(x-2.0*xx,y-yy)).rgb*0.05; + sum += texture(Source,vec2(x-xx,y-2.0*yy)).rgb*0.05; + sum += texture(Source,vec2(x,y-2.0*yy)).rgb*0.05; + sum += texture(Source,vec2(x+xx,y-2.0*yy)).rgb*0.05; + sum += texture(Source,vec2(x+2.0*xx,y-yy)).rgb*0.05; + sum += texture(Source,vec2(x+2.0*xx,y)).rgb*0.05; + sum += texture(Source,vec2(x+2.0*xx,y+yy)).rgb*0.05; + sum += texture(Source,vec2(x+xx,y+2.0*yy)).rgb*0.05; + sum += texture(Source,vec2(x,y+2.0*yy)).rgb*0.05; + sum += texture(Source,vec2(x-xx,y+2.0*yy)).rgb*0.05; + sum += texture(Source,vec2(x-2.0*xx,y-yy)).rgb*0.05; + + return sum * GLOW; +} + +float noise(vec2 co) +{ +return fract(sin(iTimer * dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); +} + +void main() +{ + +// FILTER + vec2 stepxy = SourceSize.zw; + if (OriginalSize.y > 400.0 && INTERLACE == 0.0) stepxy.y *=2.0; + + vec2 texCoord = curve(vTexCoord.xy*(SourceSize.xy/OriginalSize.xy))*(OriginalSize.xy/SourceSize.xy.xy); + + vec2 pos = texCoord.xy + stepxy * 0.5; + vec2 OGL2Pos = texCoord * SourceSize.xy; + vec2 pC4 = floor(OGL2Pos) + 0.5; + vec2 coord = pC4 / SourceSize.xy; + vec2 f = fract(pos / stepxy); + if (OriginalSize.y > 400.0 && INTERLACE == 1.0) f = vec2(1.0); + else if (OriginalSize.y > 400.0 && INTERLACE == 0.0) f; + + float x = mix(texCoord.x,coord.x,SHARPNESSX); + float y = mix(texCoord.y,coord.y,SHARPNESSY); + vec4 color = texture(Source,vec2(x,y)); + + float lum2 = dot(color.rgb,vec3(0.29,0.60,0.11)); + +//gamma in + color = pow(color, vec4(GAMMAIN)); + +//Color temp + color.rgb *= ColorTemperatureToRGB(TEMP); + + float lum = max(max(color.r,color.g),color.b); +// noise + color.rgb *= mix(1.0 + noise(texCoord)*nois,1.0,lum); +//scanlines +float ss = ScanLine(f.y, lum)+ScanLine(1.0-f.y, lum); + color *= ss; +//mask + float mask = 1.0 - DOTMASK_STRENGTH; + vec3 dotMaskWeights = mix(vec3(1.0, mask, 1.0), + vec3(mask, 1.0, mask), + floor(mod(mod_factor, 2.0))); + float msk = mix(MASKLOW,MASKHIGH,lum2); + + if (SHADOWMASK == 0.0) + { + color.rgb *= mix(vec3(1.0),dotMaskWeights, msk); + } + else + { + + color.rgb *= mix(vec3(1.0),Mask(floor(1.000001 * vTexCoord.xy*OutputSize.xy + vec2(0.5,0.5))), msk); + } + +//gamma out + color = pow(color, vec4(1.0/GAMMAOUT)); +//glow + color.rgb += glow0(pos); +//saturation + color.rgb = mix(vec3(lum2)*ss,color.rgb, SAT); +//black level + color.rgb -= vec3(BLACK); + color.rgb *= vec3(1.0)/vec3(1.0-BLACK); +//corner + color *= corner(texCoord); + + if (SEGA == 1.0) color.rgb *= 1.0625; + +// vignette + color.rgb *= vign(); + FragColor = color; +} \ No newline at end of file diff --git a/crt/shaders/crt-simple.slang b/crt/shaders/crt-simple.slang new file mode 100644 index 0000000..8f8fe02 --- /dev/null +++ b/crt/shaders/crt-simple.slang @@ -0,0 +1,161 @@ +#version 450 + +/* + * CRT-simple shader + * + * Copyright (C) 2011 DOLLS. Based on cgwg's CRT shader. + * ported and improved by DariusG @2023 + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * + */ +layout(push_constant) uniform Push +{ + vec4 SourceSize; + vec4 OriginalSize; + vec4 OutputSize; + uint FrameCount; + float DISTORTION,SCANLINE,INPUTGAMMA,OUTPUTGAMMA,MASK,SIZE; +} params; + +// Parameter lines go here: +#pragma parameter DISTORTION "Distortion" 0.12 0.0 0.30 0.01 +#pragma parameter SCANLINE "Scanline Weight" 0.3 0.2 0.6 0.05 +#pragma parameter INPUTGAMMA "Input Gamma" 2.4 0.0 4.0 0.05 +#pragma parameter OUTPUTGAMMA "Output Gamma" 2.2 0.0 4.0 0.05 +#pragma parameter MASK "Mask Brightness" 0.7 0.0 1.0 0.05 +#pragma parameter SIZE "Mask Size" 1.0 1.0 2.0 1.0 + +#define DISTORTION params.DISTORTION +#define SCANLINE params.SCANLINE +#define INPUTGAMMA params.INPUTGAMMA +#define OUTPUTGAMMA params.OUTPUTGAMMA +#define MASK params.MASK +#define SIZE params.SIZE + +#define SourceSize params.SourceSize +#define OriginalSize params.OriginalSize +#define OutputSize params.OutputSize +#define CURVATURE +#define PI 3.141592653589 +#define outgamma 1.0 / OUTPUTGAMMA +#define scale SourceSize.xy/OriginalSize.xy + +#define pi 3.141592654 + +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 * 1.0001; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; + +layout(set = 0, binding = 1) uniform sampler2D Source; + + +/////////////////////////////////////////////////////////////////////////////////////////////// + + + + // Calculate the influence of a scanline on the current pixel. + // + // 'distance' is the distance in texture coordinates from the current + // pixel to the scanline in question. + // 'color' is the colour of the scanline at the horizontal location of + // the current pixel. + vec4 scanlineWeights(float distance, vec4 color) + { + // The "width" of the scanline beam is set as 2*(1 + x^4) for + // each RGB channel. + vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0)); + + // The "weights" lines basically specify the formula that gives + // you the profile of the beam, i.e. the intensity as + // a function of distance from the vertical center of the + // scanline. In this case, it is gaussian if width=2, and + // becomes nongaussian for larger widths. Ideally this should + // be normalized so that the integral across the beam is + // independent of its width. That is, for a narrower beam + // "weights" should have a higher peak at the center of the + // scanline than for a wider beam. + vec4 weights = vec4(distance / SCANLINE); + return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid); + } + + +vec2 Distort(vec2 coord) +{ + vec2 CURVATURE_DISTORTION = vec2(DISTORTION, DISTORTION*1.5); + // Barrel distortion shrinks the display area a bit, this will allow us to counteract that. + vec2 barrelScale = 1.0 - (0.23 * CURVATURE_DISTORTION); + coord *= SourceSize.xy/OriginalSize.xy; + coord -= vec2(0.5); + float rsq = coord.x * coord.x + coord.y * coord.y; + coord += coord * (CURVATURE_DISTORTION * rsq); + coord *= barrelScale; + if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5) + coord = vec2(-1.0); // If out of bounds, return an invalid value. + else + { + coord += vec2(0.5); + coord /= SourceSize.xy/OriginalSize.xy; + } + + return coord; +} +void main() +{ + // Texture coordinates of the texel containing the active pixel. + vec2 abspos = vTexCoord.xy*SourceSize.xy*scale; + vec2 xy; + + #ifdef CURVATURE + xy = Distort(vTexCoord.xy); float xblur = xy.x; + #else + xy = vTexCoord.xy; + #endif + // Of all the pixels that are mapped onto the texel we are + // currently rendering, which pixel are we currently rendering? + vec2 ratio_scale = xy * SourceSize.xy - 0.5; + vec2 uv_ratio = fract(ratio_scale); + + // Snap to the center of the underlying texel. + xy = (floor(ratio_scale) + 0.5) / SourceSize.xy; + xy.x = xblur; + // Calculate the effective colour of the current and next + // scanlines at the horizontal location of the current pixel. + vec4 col = texture(Source,xy); + col=pow(col,vec4(INPUTGAMMA)); + vec4 col2 = texture(Source,xy + vec2(0.0, SourceSize.w)); + col2=pow(col2,vec4(INPUTGAMMA)); + + // Calculate the influence of the current and next scanlines on + // the current pixel. + vec4 weights = scanlineWeights(uv_ratio.y, col); + vec4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2); + + vec3 mul_res = (col * weights + col2 * weights2).rgb; + vec2 fragCoord = vTexCoord*OutputSize.xy; + // dot-mask emulation: + vec3 dotMaskWeights = mix(vec3(MASK), vec3(1.0),fract(fragCoord.x*0.5/SIZE)); + + mul_res *= dotMaskWeights; + + FragColor = vec4(vec3(pow(mul_res, vec3(outgamma))), 1.0); + }