Shaders: Add new pack of shaders (reshade) (#3232)

- Add crt-geom, super-xbr, geom, multi-LUT, deblur-luma, bicubic and lanczos3. All .fx shaders;
- Added some LUTs.
This commit is contained in:
Hyllian
2024-06-23 21:16:51 -03:00
committed by GitHub
parent 6021e435ba
commit 9189588554
10 changed files with 1931 additions and 0 deletions

View File

@ -0,0 +1,555 @@
#include "ReShade.fxh"
// DariusG presents
// 'crt-Cyclon'
// Why? Because it's speedy!
// A super-fast shader based on the magnificent crt-Geom, optimized for full speed
// on a Xiaomi Note 3 Pro cellphone (around 170(?) gflops gpu or so)
// This shader uses parts from:
// crt-Geom (scanlines)
// Quillez (main filter)
// Grade (some primaries)
// Dogway's inverse Gamma
// Masks-slot-color handling, tricks etc are mine.
// 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.
uniform float SCANLINE <
ui_type = "drag";
ui_min = 0.2;
ui_max = 0.6;
ui_step = 0.05;
ui_label = "Scanline Weight";
> = 0.3;
uniform float INTERLACE <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "Interlacing On/Off";
> = 1.0;
uniform float bogus_msk <
ui_type = "drag";
ui_min = 0.0;
ui_max = 0.0;
ui_step = 0.0;
ui_label = " [ MASK SETTINGS ] ";
> = 0.0;
uniform float M_TYPE <
ui_type = "drag";
ui_min = -1.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "Mask Type: -1:None, 0:CGWG, 1:RGB";
> = 1.0;
uniform float MSIZE <
ui_type = "drag";
ui_min = 1.0;
ui_max = 2.0;
ui_step = 1.0;
ui_label = "Mask Size";
> = 1.0;
uniform float SLOT <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "Slot Mask On/Off";
> = 1.0;
uniform float SLOTW <
ui_type = "drag";
ui_min = 2.0;
ui_max = 3.0;
ui_step = 1.0;
ui_label = "Slot Mask Width";
> = 3.0;
uniform float BGR <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "Subpixels BGR/RGB";
> = 0.0;
uniform float Maskl <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 0.05;
ui_label = "Mask Brightness Dark";
> = 0.3;
uniform float Maskh <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 0.05;
ui_label = "Mask Brightness Bright";
> = 0.75;
uniform float bogus_geom <
ui_type = "drag";
ui_min = 0.0;
ui_max = 0.0;
ui_step = 0.0;
ui_label = " [ GEOMETRY SETTINGS ] ";
> = 0.0;
uniform float bzl <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "Bezel On/Off";
> = 1.0;
uniform float ambient <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 0.05;
ui_label = "Ambient Light";
> = 0.40;
uniform float zoomx <
ui_type = "drag";
ui_min = -1.0;
ui_max = 1.0;
ui_step = 0.005;
ui_label = "Zoom Image X";
> = 0.0;
uniform float zoomy <
ui_type = "drag";
ui_min = -1.0;
ui_max = 1.0;
ui_step = 0.005;
ui_label = "Zoom Image Y";
> = 0.0;
uniform float centerx <
ui_type = "drag";
ui_min = -5.0;
ui_max = 5.0;
ui_step = 0.0;
ui_label = "Image Center X";
> = 0.0;
uniform float centery <
ui_type = "drag";
ui_min = -5.0;
ui_max = 5.0;
ui_step = 0.05;
ui_label = "Image Center Y";
> = 0.0;
uniform float WARPX <
ui_type = "drag";
ui_min = 0.00;
ui_max = 0.25;
ui_step = 0.01;
ui_label = "Curvature Horizontal";
> = 0.02;
uniform float WARPY <
ui_type = "drag";
ui_min = 0.00;
ui_max = 0.25;
ui_step = 0.01;
ui_label = "Curvature Vertical";
> = 0.01;
uniform float vig <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "Vignette On/Off";
> = 1.0;
uniform float bogus_col <
ui_type = "drag";
ui_min = 0.0;
ui_max = 0.0;
ui_step = 0.0;
ui_label = " [ COLOR SETTINGS ] ";
> = 0.0;
uniform float BR_DEP <
ui_type = "drag";
ui_min = 0.0;
ui_max = 0.333;
ui_step = 0.01;
ui_label = "Scan/Mask Brightness Dependence";
> = 0.2;
uniform float c_space <
ui_type = "drag";
ui_min = 0.0;
ui_max = 3.0;
ui_step = 1.0;
ui_label = "Color Space: sRGB,PAL,NTSC-U,NTSC-J";
> = 0.0;
uniform float EXT_GAMMA <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "External Gamma In (Glow etc)";
> = 0.0;
uniform float SATURATION <
ui_type = "drag";
ui_min = 0.0;
ui_max = 2.0;
ui_step = 0.05;
ui_label = "Saturation";
> = 1.0;
uniform float BRIGHTNESS_ <
ui_type = "drag";
ui_min = 0.0;
ui_max = 2.0;
ui_step = 0.01;
ui_label = "Brightness, Sega fix:1.06";
> = 1.0;
uniform float BLACK <
ui_type = "drag";
ui_min = -0.20;
ui_max = 0.20;
ui_step = 0.0;
ui_label = "Black Level";
> = 0.0;
uniform float RG <
ui_type = "drag";
ui_min = -0.25;
ui_max = 0.25;
ui_step = 0.01;
ui_label = "Green <-to-> Red Hue";
> = 0.0;
uniform float RB <
ui_type = "drag";
ui_min = 0.0;
ui_max = -0.25;
ui_step = 0.2;
ui_label = "Blue <-to-> Red Hue";
> = 0.0;
uniform float GB <
ui_type = "drag";
ui_min = -0.25;
ui_max = 0.25;
ui_step = 0.01;
ui_label = "Blue <-to-> Green Hue";
> = 0.0;
uniform float bogus_con <
ui_type = "drag";
ui_min = 0.0;
ui_max = 0.0;
ui_step = 0.0;
ui_label = " [ CONVERGENCE SETTINGS ] ";
> = 0.0;
uniform float C_STR <
ui_type = "drag";
ui_min = 0.0;
ui_max = 0.5;
ui_step = 0.05;
ui_label = "Convergence Overall Strength";
> = 0.0;
uniform float CONV_R <
ui_type = "drag";
ui_min = -1.0;
ui_max = 1.0;
ui_step = 0.05;
ui_label = "Convergence Red X-Axis";
> = 0.0;
uniform float CONV_G <
ui_type = "drag";
ui_min = -1.0;
ui_max = 1.0;
ui_step = 0.05;
ui_label = "Convergence Green X-axis";
> = 0.0;
uniform float CONV_B <
ui_type = "drag";
ui_min = -1.0;
ui_max = 1.0;
ui_step = 0.05;
ui_label = "Convergence Blue X-Axis";
> = 0.0;
uniform float POTATO <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "Potato Boost(Simple Gamma, adjust Mask)";
> = 0.0;
#define blck ((1.0)/(1.0-BLACK))
#define pi 3.1415926535897932384626433
uniform float2 BufferViewportRatio < source = "buffer_to_viewport_ratio"; >;
uniform float2 InternalPixelSize < source = "internal_pixel_size"; >;
uniform float2 NativePixelSize < source = "native_pixel_size"; >;
uniform float2 NormalizedInternalPixelSize < source = "normalized_internal_pixel_size"; >;
uniform float2 NormalizedNativePixelSize < source = "normalized_native_pixel_size"; >;
uniform float UpscaleMultiplier < source = "upscale_multiplier"; >;
uniform float2 ViewportSize < source = "viewportsize"; >;
uniform int FrameCount < source = "framecount"; >;
texture tBezel < source = "crt-cyclon/bezel.png"; >
{
Width = BUFFER_WIDTH;
Height = BUFFER_HEIGHT;
MipLevels = 1;
};
sampler sBezel { Texture = tBezel; AddressU = BORDER; AddressV = BORDER; MinFilter = LINEAR; MagFilter = LINEAR;};
float3 Mask(float2 pos, float CGWG)
{
float3 mask = float3(CGWG,CGWG,CGWG);
if (M_TYPE == 0.0){
if (POTATO == 1.0) { float pot = (1.0-CGWG)*sin(pos.x*pi)+CGWG; return float3(pot,pot,pot); }
else{
float m = frac(pos.x*0.5);
if (m<0.5) mask.rb = float2(1.0,1.0);
else mask.g = 1.0;
return mask;
}
}
if (M_TYPE == 1.0){
if (POTATO == 1.0) { float pot = (1.0-CGWG)*sin(pos.x*pi*0.6667)+CGWG; return float3(pot,pot,pot );}
else{
float m = frac(pos.x*0.3333);
if (m<0.3333) mask.rgb = (BGR == 0.0) ? float3(mask.r, mask.g, 1.0) : float3(1.0, mask.g, mask.b);
else if (m<0.6666) mask.g = 1.0;
else mask.rgb = (BGR == 0.0) ? float3(1.0, mask.g, mask.b) : float3(mask.r, mask.g, 1.0);
return mask;
}
}
else return float3(1.0,1.0,1.0);
}
float scanlineWeights(float distance, float3 color, float x)
{
// "wid" controls the width of the scanline beam, for each RGB
// channel 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.
float wid = SCANLINE + 0.15 * dot(color, float3(0.25-0.8*x, 0.25-0.8*x, 0.25-0.8*x)); //0.8 vignette strength
float weights = distance / wid;
return 0.4 * exp(-weights * weights ) / wid;
}
#define pwr float3(1.0/((-1.0*SCANLINE+1.0)*(-0.8*CGWG+1.0))-1.2,1.0/((-1.0*SCANLINE+1.0)*(-0.8*CGWG+1.0))-1.2,1.0/((-1.0*SCANLINE+1.0)*(-0.8*CGWG+1.0))-1.2)
// Returns gamma corrected output, compensated for scanline+mask embedded gamma
float3 inv_gamma(float3 col, float3 power)
{
float3 cir = col-1.0;
cir *= cir;
col = lerp(sqrt(col),sqrt(1.0-cir),power);
return col;
}
// standard 6500k
static const float3x3 PAL = float3x3(
1.0740 , -0.0574 , -0.0119 ,
0.0384 , 0.9699 , -0.0059 ,
-0.0079 , 0.0204 , 0.9884 );
// standard 6500k
static const float3x3 NTSC = float3x3(
0.9318 , 0.0412 , 0.0217 ,
0.0135 , 0.9711 , 0.0148 ,
0.0055 , -0.0143 , 1.0085 );
// standard 8500k
static const float3x3 NTSC_J = float3x3(
0.9501 , -0.0431 , 0.0857 ,
0.0265 , 0.9278 , 0.0432 ,
0.0011 , -0.0206 , 1.3153 );
float3 slot(float2 pos)
{
float h = frac(pos.x/SLOTW);
float v = frac(pos.y);
float odd;
if (v<0.5) odd = 0.0; else odd = 1.0;
if (odd == 0.0)
{if (h<0.5) return float3(0.5,0.5,0.5); else return float3(1.5,1.5,1.5);}
else if (odd == 1.0)
{if (h<0.5) return float3(1.5,1.5,1.5); else return float3(0.5,0.5,0.5);}
}
float2 Warp(float2 pos)
{
pos = pos*2.0-1.0;
pos *= float2(1.0+pos.y*pos.y*WARPX, 1.0+pos.x*pos.x*WARPY);
pos = pos*0.5+0.5;
return pos;
}
uniform float2 BufferHeight < source = "bufferheight"; >;
float4 CRT_CYCLON_PS(float4 vpos: SV_Position, float2 vTexCoord : TEXCOORD0) : SV_Target
{
float4 SourceSize = float4(1.0 / (NormalizedInternalPixelSize * UpscaleMultiplier), NormalizedInternalPixelSize * UpscaleMultiplier);
float2 OutputSize = ViewportSize;
float2 scale = BufferViewportRatio.xy;
float2 warpcoords = (vTexCoord-float2(0.5,0.5)) * BufferViewportRatio + float2(0.5,0.5);
// Hue matrix inside main() to avoid GLES error
float3x3 hue = float3x3(
1.0, -RG, -RB,
RG, 1.0, -GB,
RB, GB, 1.0
);
// zoom in and center screen for bezel
float2 pos = Warp((vTexCoord*float2(1.0-zoomx,1.0-zoomy)-float2(centerx,centery)/100.0));
float4 bez = float4(0.0,0.0,0.0,0.0);
// if (bzl == 1.0) bez = tex2D(sBezel,vTexCoord*SourceSize.xy/OriginalSize.xy*0.97+float2(0.015,0.015));
// if (bzl == 1.0) bez = tex2D(sBezel,vTexCoord*scale*0.97+float2(0.015,0.015));
if (bzl == 1.0) bez = tex2D(sBezel,warpcoords*0.97+float2(0.015,0.015)); // This fix Bezel to adjust to Game's aspect ratio.
bez.rgb = lerp(bez.rgb, float3(ambient,ambient,ambient),0.5);
float2 bpos = pos;
float2 ps = SourceSize.zw;
float2 dx = float2(ps.x,0.0);
// Quilez
float2 ogl2 = pos*SourceSize.xy;
float2 i = floor(pos*SourceSize.xy) + 0.5;
float f = ogl2.y - i.y;
pos.y = (i.y + 4.0*f*f*f)*ps.y; // smooth
pos.x = lerp(pos.x, i.x*ps.x, 0.2);
// Convergence
float3 res0 = tex2D(ReShade::BackBuffer,pos).rgb;
float resr = tex2D(ReShade::BackBuffer,pos + dx*CONV_R).r;
float resb = tex2D(ReShade::BackBuffer,pos + dx*CONV_B).b;
float resg = tex2D(ReShade::BackBuffer,pos + dx*CONV_G).g;
float3 res = float3( res0.r*(1.0-C_STR) + resr*C_STR,
res0.g*(1.0-C_STR) + resg*C_STR,
res0.b*(1.0-C_STR) + resb*C_STR
);
// Vignette
float x = 0.0;
if (vig == 1.0){
x = vTexCoord.x*scale.x-0.5;
// x = vTexCoord.x-0.5;
x = x*x;}
float l = dot(float3(BR_DEP,BR_DEP,BR_DEP),res);
// Color Spaces
if(EXT_GAMMA != 1.0) res *= res;
if (c_space != 0.0) {
if (c_space == 1.0) res = mul(PAL,res);
if (c_space == 2.0) res = mul(NTSC,res);
if (c_space == 3.0) res = mul(NTSC_J,res);
// Apply CRT-like luminances
res /= float3(0.24,0.69,0.07);
res *= float3(0.29,0.6,0.11);
res = clamp(res,0.0,1.0);
}
float s = frac(bpos.y*SourceSize.y-0.5);
// handle interlacing
if (SourceSize.y > 400.0)
{
s = frac(bpos.y*SourceSize.y/2.0-0.5);
// if (INTERLACE == 1.0) s = mod(float(FrameCount),2.0) < 1.0 ? s: s+0.5;
if (INTERLACE == 1.0) s = (float(FrameCount) % 2.0) < 1.0 ? s: s+0.5;
}
// Calculate CRT-Geom scanlines weight and apply
float weight = scanlineWeights(s, res, x);
float weight2 = scanlineWeights(1.0-s, res, x);
res *= weight + weight2;
// Masks
float2 xy = vTexCoord*OutputSize.xy*scale/MSIZE;
// float2 xy = vTexCoord*OutputSize.xy/MSIZE;
float CGWG = lerp(Maskl, Maskh, l);
res *= Mask(xy, CGWG);
// Apply slot mask on top of Trinitron-like mask
if (SLOT == 1.0) res *= lerp(slot(xy/2.0),float3(1.0,1.0,1.0),CGWG);
if (POTATO == 0.0) res = inv_gamma(res,pwr);
else {res = sqrt(res); res *= lerp(1.3,1.1,l);}
// Saturation
float lum = dot(float3(0.29,0.60,0.11),res);
res = lerp(float3(lum,lum,lum),res,SATURATION);
// Brightness, Hue and Black Level
res *= BRIGHTNESS_;
res = mul(hue,res);
res -= float3(BLACK,BLACK,BLACK);
res *= blck;
// Apply bezel code, adapted from New-Pixie
if (bzl >0.0)
res.rgb = lerp(res.rgb, lerp(max(res.rgb, 0.0), pow( abs(bez.rgb), float3( 1.4,1.4,1.4 ) ), bez.w * bez.w), float3( 1.0,1.0,1.0 ) );
return float4(res, 1.0);
}
technique CRT_CYCLON
{
pass PS_CRT_CYCLON
{
VertexShader = PostProcessVS;
PixelShader = CRT_CYCLON_PS;
}
}

View File

@ -0,0 +1,628 @@
#include "ReShade.fxh"
/*
CRT-interlaced
Copyright (C) 2010-2012 cgwg, Themaister and DOLLS
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.
(cgwg gave their consent to have the original version of this shader
distributed under the GPL in this message:
http://board.byuu.org/viewtopic.php?p=26075#p26075
"Feel free to distribute my shaders under the GPL. After all, the
barrel distortion code was taken from the Curvature shader, which is
under the GPL."
)
This shader variant is pre-configured with screen curvature
*/
uniform float CRTgamma <
ui_type = "drag";
ui_min = 0.1;
ui_max = 5.0;
ui_step = 0.1;
ui_label = "CRTGeom Target Gamma";
> = 2.4;
uniform float monitorgamma <
ui_type = "drag";
ui_min = 0.1;
ui_max = 5.0;
ui_step = 0.1;
ui_label = "CRTGeom Monitor Gamma";
> = 2.2;
uniform float d <
ui_type = "drag";
ui_category = "Curvature";
ui_min = 0.1;
ui_max = 3.0;
ui_step = 0.1;
ui_label = "CRTGeom Distance";
> = 1.5;
uniform bool CURVATURE <
ui_category = "Curvature";
ui_type = "radio";
ui_label = "CRTGeom Curvature Toggle";
> = 1.0;
uniform float invert_aspect <
ui_type = "drag";
ui_category = "Curvature";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "CRTGeom Curvature Aspect Inversion";
> = 0.0;
uniform float R <
ui_type = "drag";
ui_category = "Curvature";
ui_min = 0.1;
ui_max = 10.0;
ui_step = 0.1;
ui_label = "CRTGeom Curvature Radius";
> = 2.0;
uniform float cornersize <
ui_type = "drag";
ui_category = "Curvature";
ui_min = 0.001;
ui_max = 1.0;
ui_step = 0.005;
ui_label = "CRTGeom Corner Size";
> = 0.03;
uniform float cornersmooth <
ui_type = "drag";
ui_category = "Curvature";
ui_min = 80.0;
ui_max = 2000.0;
ui_step = 100.0;
ui_label = "CRTGeom Corner Smoothness";
> = 1000.0;
uniform float x_tilt <
ui_type = "drag";
ui_category = "Curvature";
ui_min = -0.5;
ui_max = 0.5;
ui_step = 0.05;
ui_label = "CRTGeom Horizontal Tilt";
> = 0.0;
uniform float y_tilt <
ui_type = "drag";
ui_category = "Curvature";
ui_min = -0.5;
ui_max = 0.5;
ui_step = 0.05;
ui_label = "CRTGeom Vertical Tilt";
> = 0.0;
uniform float overscan_x <
ui_type = "drag";
ui_min = -125.0;
ui_max = 125.0;
ui_step = 0.5;
ui_label = "CRTGeom Horiz. Overscan %";
> = 100.0;
uniform float overscan_y <
ui_type = "drag";
ui_min = -125.0;
ui_max = 125.0;
ui_step = 0.5;
ui_label = "CRTGeom Vert. Overscan %";
> = 100.0;
uniform float DOTMASK <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 0.05;
ui_label = "CRTGeom Dot Mask Strength";
> = 0.3;
uniform float SHARPER <
ui_type = "drag";
ui_min = 1.0;
ui_max = 3.0;
ui_step = 1.0;
ui_label = "CRTGeom Sharpness";
> = 1.0;
uniform float scanline_weight <
ui_type = "drag";
ui_min = 0.1;
ui_max = 0.5;
ui_step = 0.05;
ui_label = "CRTGeom Scanline Weight";
> = 0.3;
uniform float vertical_scanlines <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "CRTGeom Vertical Scanlines";
> = 0.0;
uniform float lum <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 0.01;
ui_label = "CRTGeom Luminance";
> = 0.0;
uniform float interlace_detect <
ui_type = "drag";
ui_min = 0.0;
ui_max = 1.0;
ui_step = 1.0;
ui_label = "CRTGeom Interlacing Simulation";
> = 1.0;
uniform float FrameCount < source = "framecount"; >;
uniform float2 BufferViewportRatio < source = "buffer_to_viewport_ratio"; >;
uniform float2 InternalPixelSize < source = "internal_pixel_size"; >;
uniform float2 NativePixelSize < source = "native_pixel_size"; >;
uniform float2 NormalizedInternalPixelSize < source = "normalized_internal_pixel_size"; >;
uniform float2 NormalizedNativePixelSize < source = "normalized_native_pixel_size"; >;
uniform float UpscaleMultiplier < source = "upscale_multiplier"; >;
uniform float2 ViewportSize < source = "viewportsize"; >;
// Comment the next line to disable interpolation in linear gamma (and
// gain speed).
#define LINEAR_PROCESSING
// Enable 3x oversampling of the beam profile; improves moire effect caused by scanlines+curvature
#define OVERSAMPLE
// Use the older, purely gaussian beam profile; uncomment for speed
//#define USEGAUSSIAN
// Macros.
#define FIX(c) max(abs(c), 1e-5);
#define PI 3.141592653589
#ifdef LINEAR_PROCESSING
# define TEX2D(c) pow(tex2D(ReShade::BackBuffer, (c)), float4(CRTgamma,CRTgamma,CRTgamma,CRTgamma))
#else
# define TEX2D(c) tex2D(ReShade::BackBuffer, (c))
#endif
// aspect ratio
#define aspect (invert_aspect>0.5?float2(0.75,1.0):float2(1.0,0.75))
#define overscan (float2(1.01,1.01));
struct ST_VertexOut
{
float2 sinangle : TEXCOORD1;
float2 cosangle : TEXCOORD2;
float3 stretch : TEXCOORD3;
float2 ilfac : TEXCOORD4;
float2 one : TEXCOORD5;
float mod_factor : TEXCOORD6;
float2 TextureSize : TEXCOORD7;
};
float vs_intersect(float2 xy, float2 sinangle, float2 cosangle)
{
float A = dot(xy,xy) + d*d;
float B = 2.0*(R*(dot(xy,sinangle)-d*cosangle.x*cosangle.y)-d*d);
float C = d*d + 2.0*R*d*cosangle.x*cosangle.y;
return (-B-sqrt(B*B-4.0*A*C))/(2.0*A);
}
float2 vs_bkwtrans(float2 xy, float2 sinangle, float2 cosangle)
{
float c = vs_intersect(xy, sinangle, cosangle);
float2 point = (float2(c, c)*xy - float2(-R, -R)*sinangle) / float2(R, R);
float2 poc = point/cosangle;
float2 tang = sinangle/cosangle;
float A = dot(tang, tang) + 1.0;
float B = -2.0*dot(poc, tang);
float C = dot(poc, poc) - 1.0;
float a = (-B + sqrt(B*B - 4.0*A*C))/(2.0*A);
float2 uv = (point - a*sinangle)/cosangle;
float r = FIX(R*acos(a));
return uv*r/sin(r/R);
}
float2 vs_fwtrans(float2 uv, float2 sinangle, float2 cosangle)
{
float r = FIX(sqrt(dot(uv,uv)));
uv *= sin(r/R)/r;
float x = 1.0-cos(r/R);
float D = d/R + x*cosangle.x*cosangle.y+dot(uv,sinangle);
return d*(uv*cosangle-x*sinangle)/D;
}
float3 vs_maxscale(float2 sinangle, float2 cosangle)
{
float2 c = vs_bkwtrans(-R * sinangle / (1.0 + R/d*cosangle.x*cosangle.y), sinangle, cosangle);
float2 a = float2(0.5,0.5)*aspect;
float2 lo = float2(vs_fwtrans(float2(-a.x, c.y), sinangle, cosangle).x,
vs_fwtrans(float2( c.x, -a.y), sinangle, cosangle).y)/aspect;
float2 hi = float2(vs_fwtrans(float2(+a.x, c.y), sinangle, cosangle).x,
vs_fwtrans(float2( c.x, +a.y), sinangle, cosangle).y)/aspect;
return float3((hi+lo)*aspect*0.5,max(hi.x-lo.x,hi.y-lo.y));
}
// Vertex shader generating a triangle covering the entire screen
void VS_CRT_Geom(in uint id : SV_VertexID, out float4 position : SV_Position, out float2 texcoord : TEXCOORD, out ST_VertexOut vVARS)
{
texcoord.x = (id == 2) ? 2.0 : 0.0;
texcoord.y = (id == 1) ? 2.0 : 0.0;
position = float4(texcoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
float2 SourceSize = 1.0/NormalizedNativePixelSize;
float2 OutputSize = ViewportSize*BufferViewportRatio;
// Precalculate a bunch of useful values we'll need in the fragment
// shader.
vVARS.sinangle = sin(float2(x_tilt, y_tilt));
vVARS.cosangle = cos(float2(x_tilt, y_tilt));
vVARS.stretch = vs_maxscale(vVARS.sinangle, vVARS.cosangle);
if(vertical_scanlines < 0.5)
{
vVARS.TextureSize = float2(SHARPER * SourceSize.x, SourceSize.y);
vVARS.ilfac = float2(1.0, clamp(floor(SourceSize.y/(interlace_detect > 0.5 ? 200.0 : 1000)), 1.0, 2.0));
// The size of one texel, in texture-coordinates.
vVARS.one = vVARS.ilfac / vVARS.TextureSize;
// Resulting X pixel-coordinate of the pixel we're drawing.
vVARS.mod_factor = texcoord.x * SourceSize.x * OutputSize.x / SourceSize.x;
}else{
vVARS.TextureSize = float2(SourceSize.x, SHARPER * SourceSize.y);
vVARS.ilfac = float2(clamp(floor(SourceSize.x/(interlace_detect > 0.5 ? 200.0 : 1000)), 1.0, 2.0), 1.0);
// The size of one texel, in texture-coordinates.
vVARS.one = vVARS.ilfac / vVARS.TextureSize;
// Resulting X pixel-coordinate of the pixel we're drawing.
vVARS.mod_factor = texcoord.y * SourceSize.y * OutputSize.y / SourceSize.y;
}
}
float intersect(float2 xy, float2 sinangle, float2 cosangle)
{
float A = dot(xy,xy) + d*d;
float B, C;
if(vertical_scanlines < 0.5)
{
B = 2.0*(R*(dot(xy,sinangle) - d*cosangle.x*cosangle.y) - d*d);
C = d*d + 2.0*R*d*cosangle.x*cosangle.y;
}else{
B = 2.0*(R*(dot(xy,sinangle) - d*cosangle.y*cosangle.x) - d*d);
C = d*d + 2.0*R*d*cosangle.y*cosangle.x;
}
return (-B-sqrt(B*B - 4.0*A*C))/(2.0*A);
}
float2 bkwtrans(float2 xy, float2 sinangle, float2 cosangle)
{
float c = intersect(xy, sinangle, cosangle);
float2 point = (float2(c, c)*xy - float2(-R, -R)*sinangle) / float2(R, R);
float2 poc = point/cosangle;
float2 tang = sinangle/cosangle;
float A = dot(tang, tang) + 1.0;
float B = -2.0*dot(poc, tang);
float C = dot(poc, poc) - 1.0;
float a = (-B + sqrt(B*B - 4.0*A*C)) / (2.0*A);
float2 uv = (point - a*sinangle) / cosangle;
float r = FIX(R*acos(a));
return uv*r/sin(r/R);
}
float2 fwtrans(float2 uv, float2 sinangle, float2 cosangle)
{
float r = FIX(sqrt(dot(uv, uv)));
uv *= sin(r/R)/r;
float x = 1.0 - cos(r/R);
float D;
if(vertical_scanlines < 0.5)
D = d/R + x*cosangle.x*cosangle.y + dot(uv,sinangle);
else
D = d/R + x*cosangle.y*cosangle.x + dot(uv,sinangle);
return d*(uv*cosangle - x*sinangle)/D;
}
float3 maxscale(float2 sinangle, float2 cosangle)
{
if(vertical_scanlines < 0.5)
{
float2 c = bkwtrans(-R * sinangle / (1.0 + R/d*cosangle.x*cosangle.y), sinangle, cosangle);
float2 a = float2(0.5, 0.5)*aspect;
float2 lo = float2(fwtrans(float2(-a.x, c.y), sinangle, cosangle).x,
fwtrans(float2( c.x, -a.y), sinangle, cosangle).y)/aspect;
float2 hi = float2(fwtrans(float2(+a.x, c.y), sinangle, cosangle).x,
fwtrans(float2( c.x, +a.y), sinangle, cosangle).y)/aspect;
return float3((hi+lo)*aspect*0.5,max(hi.x-lo.x, hi.y-lo.y));
}else{
float2 c = bkwtrans(-R * sinangle / (1.0 + R/d*cosangle.y*cosangle.x), sinangle, cosangle);
float2 a = float2(0.5, 0.5)*aspect;
float2 lo = float2(fwtrans(float2(-a.y, c.x), sinangle, cosangle).y,
fwtrans(float2( c.y, -a.x), sinangle, cosangle).x)/aspect;
float2 hi = float2(fwtrans(float2(+a.y, c.x), sinangle, cosangle).y,
fwtrans(float2( c.y, +a.x), sinangle, cosangle).x)/aspect;
return float3((hi+lo)*aspect*0.5,max(hi.y-lo.y, hi.x-lo.x));
}
}
// 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.
float4 scanlineWeights(float distance, float4 color)
{
// "wid" controls the width of the scanline beam, for each RGB
// channel 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.
#ifdef USEGAUSSIAN
float4 wid = 0.3 + 0.1 * pow(color, float4(3.0, 3.0, 3.0, 3.0));
float dsw = distance / scanline_weight;
float4 weights = float4(dsw, dsw, dsw, dsw);
return (lum + 0.4) * exp(-weights * weights) / wid;
#else
float4 wid = 2.0 + 2.0 * pow(color, float4(4.0, 4.0, 4.0, 4.0));
float dsw = distance / scanline_weight;
float4 weights = float4(dsw, dsw, dsw, dsw);
return (lum + 1.4) * exp(-pow(weights * rsqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
#endif
}
float2 transform(float2 coord, float2 sinangle, float2 cosangle, float3 stretch)
{
coord = (coord - float2(0.5, 0.5))*aspect*stretch.z + stretch.xy;
return (bkwtrans(coord, sinangle, cosangle) /
float2(overscan_x / 100.0, overscan_y / 100.0)/aspect + float2(0.5, 0.5));
}
float corner(float2 coord)
{
coord = (coord - float2(0.5, 0.5)) * float2(overscan_x / 100.0, overscan_y / 100.0) + float2(0.5, 0.5);
coord = min(coord, float2(1.0, 1.0) - coord) * aspect;
float2 cdist = float2(cornersize, cornersize);
coord = (cdist - min(coord, cdist));
float dist = sqrt(dot(coord, coord));
if(vertical_scanlines < 0.5)
return clamp((cdist.x - dist)*cornersmooth, 0.0, 1.0);
else
return clamp((cdist.y - dist)*cornersmooth, 0.0, 1.0);
}
float fwidth(float value){
return abs(ddx(value)) + abs(ddy(value));
}
float4 PS_CRT_Geom(float4 vpos: SV_Position, float2 vTexCoord : TEXCOORD, in ST_VertexOut vVARS) : SV_Target
{
// Here's a helpful diagram to keep in mind while trying to
// understand the code:
//
// | | | | |
// -------------------------------
// | | | | |
// | 01 | 11 | 21 | 31 | <-- current scanline
// | | @ | | |
// -------------------------------
// | | | | |
// | 02 | 12 | 22 | 32 | <-- next scanline
// | | | | |
// -------------------------------
// | | | | |
//
// Each character-cell represents a pixel on the output
// surface, "@" represents the current pixel (always somewhere
// in the bottom half of the current scan-line, or the top-half
// of the next scanline). The grid of lines represents the
// edges of the texels of the underlying texture.
// Texture coordinates of the texel containing the active pixel.
float2 xy;
if (CURVATURE > 0.5)
xy = transform(vTexCoord, vVARS.sinangle, vVARS.cosangle, vVARS.stretch);
else
xy = vTexCoord;
float cval = corner(xy);
// Of all the pixels that are mapped onto the texel we are
// currently rendering, which pixel are we currently rendering?
float2 ilvec;
if(vertical_scanlines < 0.5)
ilvec = float2(0.0, vVARS.ilfac.y * interlace_detect > 1.5 ? (float(FrameCount) % 2.0) : 0.0);
else
ilvec = float2(vVARS.ilfac.x * interlace_detect > 1.5 ? (float(FrameCount) % 2.0) : 0.0, 0.0);
float2 ratio_scale = (xy * vVARS.TextureSize - float2(0.5, 0.5) + ilvec) / vVARS.ilfac;
float2 uv_ratio = frac(ratio_scale);
// Snap to the center of the underlying texel.
xy = (floor(ratio_scale)*vVARS.ilfac + float2(0.5, 0.5) - ilvec) / vVARS.TextureSize;
// Calculate Lanczos scaling coefficients describing the effect
// of various neighbour texels in a scanline on the current
// pixel.
float4 coeffs;
if(vertical_scanlines < 0.5)
coeffs = PI * float4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x);
else
coeffs = PI * float4(1.0 + uv_ratio.y, uv_ratio.y, 1.0 - uv_ratio.y, 2.0 - uv_ratio.y);
// Prevent division by zero.
coeffs = FIX(coeffs);
// Lanczos2 kernel.
coeffs = 2.0 * sin(coeffs) * sin(coeffs / 2.0) / (coeffs * coeffs);
// Normalize.
coeffs /= dot(coeffs, float4(1.0, 1.0, 1.0, 1.0));
// Calculate the effective colour of the current and next
// scanlines at the horizontal location of the current pixel,
// using the Lanczos coefficients above.
float4 col, col2;
if(vertical_scanlines < 0.5)
{
col = clamp(
mul(coeffs, float4x4(
TEX2D(xy + float2(-vVARS.one.x, 0.0)),
TEX2D(xy),
TEX2D(xy + float2(vVARS.one.x, 0.0)),
TEX2D(xy + float2(2.0 * vVARS.one.x, 0.0))
)),
0.0, 1.0
);
col2 = clamp(
mul(coeffs, float4x4(
TEX2D(xy + float2(-vVARS.one.x, vVARS.one.y)),
TEX2D(xy + float2(0.0, vVARS.one.y)),
TEX2D(xy + vVARS.one),
TEX2D(xy + float2(2.0 * vVARS.one.x, vVARS.one.y))
)),
0.0, 1.0
);
}else{
col = clamp(
mul(coeffs, float4x4(
TEX2D(xy + float2(0.0, -vVARS.one.y)),
TEX2D(xy),
TEX2D(xy + float2(0.0, vVARS.one.y)),
TEX2D(xy + float2(0.0, 2.0 * vVARS.one.y))
)),
0.0, 1.0
);
col2 = clamp(
mul(coeffs, float4x4(
TEX2D(xy + float2(vVARS.one.x, -vVARS.one.y)),
TEX2D(xy + float2(vVARS.one.x, 0.0)),
TEX2D(xy + vVARS.one),
TEX2D(xy + float2(vVARS.one.x, 2.0 * vVARS.one.y))
)),
0.0, 1.0
);
}
#ifndef LINEAR_PROCESSING
col = pow(col , float4(CRTgamma, CRTgamma, CRTgamma, CRTgamma));
col2 = pow(col2, float4(CRTgamma, CRTgamma, CRTgamma, CRTgamma));
#endif
// Calculate the influence of the current and next scanlines on
// the current pixel.
float4 weights, weights2;
if(vertical_scanlines < 0.5)
{
weights = scanlineWeights(uv_ratio.y, col);
weights2 = scanlineWeights(1.0 - uv_ratio.y, col2);
#ifdef OVERSAMPLE
float filter = fwidth(ratio_scale.y);
uv_ratio.y = uv_ratio.y + 1.0/3.0*filter;
weights = (weights + scanlineWeights(uv_ratio.y, col))/3.0;
weights2 = (weights2 + scanlineWeights(abs(1.0 - uv_ratio.y), col2))/3.0;
uv_ratio.y = uv_ratio.y - 2.0/3.0*filter;
weights = weights + scanlineWeights(abs(uv_ratio.y), col)/3.0;
weights2 = weights2 + scanlineWeights(abs(1.0 - uv_ratio.y), col2)/3.0;
#endif
}else{
weights = scanlineWeights(uv_ratio.x, col);
weights2 = scanlineWeights(1.0 - uv_ratio.x, col2);
#ifdef OVERSAMPLE
float filter = fwidth(ratio_scale.x);
uv_ratio.x = uv_ratio.x + 1.0/3.0*filter;
weights = (weights + scanlineWeights(uv_ratio.x, col))/3.0;
weights2 = (weights2 + scanlineWeights(abs(1.0 - uv_ratio.x), col2))/3.0;
uv_ratio.x = uv_ratio.x - 2.0/3.0*filter;
weights = weights + scanlineWeights(abs(uv_ratio.x), col)/3.0;
weights2 = weights2 + scanlineWeights(abs(1.0 - uv_ratio.x), col2)/3.0;
#endif
}
float3 mul_res = (col * weights + col2 * weights2).rgb;
mul_res *= float3(cval, cval, cval);
// dot-mask emulation:
// Output pixels are alternately tinted green and magenta.
float3 dotMaskWeights = lerp(
float3(1.0, 1.0 - DOTMASK, 1.0),
float3(1.0 - DOTMASK, 1.0, 1.0 - DOTMASK),
floor((vVARS.mod_factor % 2.0))
);
mul_res *= dotMaskWeights;
// Convert the image gamma for display on our output device.
mul_res = pow(mul_res, float3(1.0 / monitorgamma, 1.0 / monitorgamma, 1.0 / monitorgamma));
return float4(mul_res, 1.0);
}
technique CRT_Geom
{
pass
{
VertexShader = VS_CRT_Geom;
PixelShader = PS_CRT_Geom;
}
}