2016-07-13 12:55:48 +10:00
|
|
|
#version 450
|
|
|
|
|
2016-08-03 03:13:40 +10:00
|
|
|
layout(push_constant) uniform Push
|
|
|
|
{
|
|
|
|
vec4 OutputSize;
|
|
|
|
vec4 OriginalSize;
|
|
|
|
vec4 SourceSize;
|
|
|
|
uint FrameCount;
|
|
|
|
float CRTgamma;
|
|
|
|
float monitorgamma;
|
|
|
|
float d;
|
|
|
|
float R;
|
|
|
|
float cornersize;
|
|
|
|
float cornersmooth;
|
|
|
|
float x_tilt;
|
|
|
|
float y_tilt;
|
|
|
|
float overscan_x;
|
|
|
|
float overscan_y;
|
|
|
|
float DOTMASK;
|
|
|
|
float SHARPER;
|
|
|
|
float scanline_weight;
|
|
|
|
float CURVATURE;
|
2019-06-18 01:21:17 +10:00
|
|
|
float interlace_detect;
|
2020-01-31 09:02:47 +11:00
|
|
|
float lum;
|
2016-08-03 03:13:40 +10:00
|
|
|
} registers;
|
|
|
|
|
2016-07-13 12:55:48 +10:00
|
|
|
layout(std140, set = 0, binding = 0) uniform UBO
|
|
|
|
{
|
2016-07-16 09:01:38 +10:00
|
|
|
mat4 MVP;
|
2016-08-03 03:13:40 +10:00
|
|
|
vec4 OutputSize;
|
2016-07-13 12:55:48 +10:00
|
|
|
} global;
|
|
|
|
|
2016-08-03 03:13:40 +10:00
|
|
|
#pragma parameter CRTgamma "CRTGeom Target Gamma" 2.4 0.1 5.0 0.1
|
|
|
|
#pragma parameter monitorgamma "CRTGeom Monitor Gamma" 2.2 0.1 5.0 0.1
|
|
|
|
#pragma parameter d "CRTGeom Distance" 1.5 0.1 3.0 0.1
|
|
|
|
#pragma parameter CURVATURE "CRTGeom Curvature Toggle" 1.0 0.0 1.0 1.0
|
|
|
|
#pragma parameter R "CRTGeom Curvature Radius" 2.0 0.1 10.0 0.1
|
|
|
|
#pragma parameter cornersize "CRTGeom Corner Size" 0.03 0.001 1.0 0.005
|
|
|
|
#pragma parameter cornersmooth "CRTGeom Corner Smoothness" 1000.0 80.0 2000.0 100.0
|
|
|
|
#pragma parameter x_tilt "CRTGeom Horizontal Tilt" 0.0 -0.5 0.5 0.05
|
|
|
|
#pragma parameter y_tilt "CRTGeom Vertical Tilt" 0.0 -0.5 0.5 0.05
|
|
|
|
#pragma parameter overscan_x "CRTGeom Horiz. Overscan %" 100.0 -125.0 125.0 1.0
|
|
|
|
#pragma parameter overscan_y "CRTGeom Vert. Overscan %" 100.0 -125.0 125.0 1.0
|
2020-01-31 09:02:47 +11:00
|
|
|
#pragma parameter DOTMASK "CRTGeom Dot Mask Strength" 0.3 0.0 1.0 0.05
|
2016-08-03 03:13:40 +10:00
|
|
|
#pragma parameter SHARPER "CRTGeom Sharpness" 1.0 1.0 3.0 1.0
|
|
|
|
#pragma parameter scanline_weight "CRTGeom Scanline Weight" 0.3 0.1 0.5 0.05
|
2020-01-31 09:02:47 +11:00
|
|
|
#pragma parameter lum "CRTGeom Luminance" 0.0 0.0 1.0 0.01
|
2019-06-18 01:21:17 +10:00
|
|
|
#pragma parameter interlace_detect "CRTGeom Interlacing Simulation" 1.0 0.0 1.0 1.0
|
2016-07-13 12:55:48 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
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:
|
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
http://board.byuu.org/viewtopic.php?p=26075#p26075
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
"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."
|
2016-07-13 12:55:48 +10:00
|
|
|
)
|
2016-07-16 09:01:38 +10:00
|
|
|
This shader variant is pre-configured with screen curvature
|
2016-07-13 12:55:48 +10:00
|
|
|
*/
|
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
// Comment the next line to disable interpolation in linear gamma (and
|
|
|
|
// gain speed).
|
|
|
|
#define LINEAR_PROCESSING
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
// Enable 3x oversampling of the beam profile; improves moire effect caused by scanlines+curvature
|
|
|
|
#define OVERSAMPLE
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
// Use the older, purely gaussian beam profile; uncomment for speed
|
2020-01-31 09:02:47 +11:00
|
|
|
//#define USEGAUSSIAN
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
// Macros.
|
|
|
|
#define FIX(c) max(abs(c), 1e-5);
|
|
|
|
#define PI 3.141592653589
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
#ifdef LINEAR_PROCESSING
|
2016-08-03 03:13:40 +10:00
|
|
|
# define TEX2D(c) pow(texture(Source, (c)), vec4(registers.CRTgamma))
|
2016-07-16 09:01:38 +10:00
|
|
|
#else
|
|
|
|
# define TEX2D(c) texture(Source, (c))
|
|
|
|
#endif
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
// aspect ratio
|
|
|
|
vec2 aspect = vec2(1.0, 0.75);
|
|
|
|
vec2 overscan = vec2(1.01, 1.01);
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 05:24:05 +10:00
|
|
|
#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 vec2 sinangle;
|
|
|
|
layout(location = 2) out vec2 cosangle;
|
|
|
|
layout(location = 3) out vec3 stretch;
|
|
|
|
layout(location = 4) out vec2 ilfac;
|
|
|
|
layout(location = 5) out vec2 one;
|
|
|
|
layout(location = 6) out float mod_factor;
|
2016-07-19 04:16:56 +10:00
|
|
|
layout(location = 7) out vec2 TextureSize;
|
2016-07-16 05:24:05 +10:00
|
|
|
|
|
|
|
float intersect(vec2 xy)
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-08-03 03:13:40 +10:00
|
|
|
float A = dot(xy,xy) + registers.d*registers.d;
|
|
|
|
float B = 2.0*(registers.R*(dot(xy,sinangle)-registers.d*cosangle.x*cosangle.y)-registers.d*registers.d);
|
|
|
|
float C = registers.d*registers.d + 2.0*registers.R*registers.d*cosangle.x*cosangle.y;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
return (-B-sqrt(B*B-4.0*A*C))/(2.0*A);
|
|
|
|
}
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 05:24:05 +10:00
|
|
|
vec2 bkwtrans(vec2 xy)
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-07-19 04:16:56 +10:00
|
|
|
float c = intersect(xy);
|
2016-08-03 03:13:40 +10:00
|
|
|
vec2 point = (vec2(c, c)*xy - vec2(-registers.R, -registers.R)*sinangle) / vec2(registers.R, registers.R);
|
2016-07-19 04:16:56 +10:00
|
|
|
vec2 poc = point/cosangle;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
2016-07-19 04:16:56 +10:00
|
|
|
vec2 tang = sinangle/cosangle;
|
|
|
|
float A = dot(tang, tang) + 1.0;
|
|
|
|
float B = -2.0*dot(poc, tang);
|
|
|
|
float C = dot(poc, poc) - 1.0;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
2016-07-19 04:16:56 +10:00
|
|
|
float a = (-B + sqrt(B*B - 4.0*A*C))/(2.0*A);
|
|
|
|
vec2 uv = (point - a*sinangle)/cosangle;
|
2016-08-03 03:13:40 +10:00
|
|
|
float r = FIX(registers.R*acos(a));
|
2016-07-16 09:01:38 +10:00
|
|
|
|
2016-08-03 03:13:40 +10:00
|
|
|
return uv*r/sin(r/registers.R);
|
2016-07-16 09:01:38 +10:00
|
|
|
}
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 05:24:05 +10:00
|
|
|
vec2 fwtrans(vec2 uv)
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
|
|
|
float r = FIX(sqrt(dot(uv,uv)));
|
2016-08-03 03:13:40 +10:00
|
|
|
uv *= sin(r/registers.R)/r;
|
|
|
|
float x = 1.0-cos(r/registers.R);
|
|
|
|
float D = registers.d/registers.R + x*cosangle.x*cosangle.y+dot(uv,sinangle);
|
2016-07-16 09:01:38 +10:00
|
|
|
|
2016-08-03 03:13:40 +10:00
|
|
|
return registers.d*(uv*cosangle-x*sinangle)/D;
|
2016-07-16 09:01:38 +10:00
|
|
|
}
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 05:24:05 +10:00
|
|
|
vec3 maxscale()
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-08-03 03:13:40 +10:00
|
|
|
vec2 c = bkwtrans(-registers.R * sinangle / (1.0 + registers.R/registers.d*cosangle.x*cosangle.y));
|
2016-07-16 09:01:38 +10:00
|
|
|
vec2 a = vec2(0.5,0.5)*aspect;
|
2016-07-19 04:16:56 +10:00
|
|
|
|
|
|
|
vec2 lo = vec2(fwtrans(vec2(-a.x, c.y)).x,
|
|
|
|
fwtrans(vec2( c.x, -a.y)).y)/aspect;
|
|
|
|
|
|
|
|
vec2 hi = vec2(fwtrans(vec2(+a.x, c.y)).x,
|
|
|
|
fwtrans(vec2( c.x, +a.y)).y)/aspect;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
return vec3((hi+lo)*aspect*0.5,max(hi.x-lo.x,hi.y-lo.y));
|
|
|
|
}
|
|
|
|
|
2016-07-13 12:55:48 +10:00
|
|
|
void main()
|
|
|
|
{
|
2016-07-16 09:01:38 +10:00
|
|
|
gl_Position = global.MVP * Position;
|
2016-07-19 02:09:14 +10:00
|
|
|
vTexCoord = TexCoord * vec2(1.00001);
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
// Precalculate a bunch of useful values we'll need in the fragment
|
|
|
|
// shader.
|
2016-08-03 03:13:40 +10:00
|
|
|
sinangle = sin(vec2(registers.x_tilt, registers.y_tilt));
|
|
|
|
cosangle = cos(vec2(registers.x_tilt, registers.y_tilt));
|
2016-07-19 04:16:56 +10:00
|
|
|
stretch = maxscale();
|
2016-08-03 03:13:40 +10:00
|
|
|
TextureSize = vec2(registers.SHARPER * registers.SourceSize.x, registers.SourceSize.y);
|
2016-07-19 04:16:56 +10:00
|
|
|
|
2016-08-03 03:13:40 +10:00
|
|
|
ilfac = vec2(1.0, clamp(floor(registers.SourceSize.y/200.0), 1.0, 2.0));
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
// The size of one texel, in texture-coordinates.
|
2016-07-19 04:16:56 +10:00
|
|
|
one = ilfac / TextureSize;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
// Resulting X pixel-coordinate of the pixel we're drawing.
|
2016-08-03 03:13:40 +10:00
|
|
|
mod_factor = vTexCoord.x * registers.SourceSize.x * registers.OutputSize.x / registers.SourceSize.x;
|
2016-07-13 12:55:48 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma stage fragment
|
|
|
|
layout(location = 0) in vec2 vTexCoord;
|
2016-07-16 05:24:05 +10:00
|
|
|
layout(location = 1) in vec2 sinangle;
|
|
|
|
layout(location = 2) in vec2 cosangle;
|
|
|
|
layout(location = 3) in vec3 stretch;
|
|
|
|
layout(location = 4) in vec2 ilfac;
|
|
|
|
layout(location = 5) in vec2 one;
|
|
|
|
layout(location = 6) in float mod_factor;
|
2016-07-19 04:16:56 +10:00
|
|
|
layout(location = 7) in vec2 TextureSize;
|
2016-07-13 12:55:48 +10:00
|
|
|
layout(location = 0) out vec4 FragColor;
|
|
|
|
layout(set = 0, binding = 2) uniform sampler2D Source;
|
|
|
|
|
2016-07-16 05:24:05 +10:00
|
|
|
float intersect(vec2 xy)
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-08-03 03:13:40 +10:00
|
|
|
float A = dot(xy,xy) + registers.d*registers.d;
|
|
|
|
float B = 2.0*(registers.R*(dot(xy,sinangle) - registers.d*cosangle.x*cosangle.y) - registers.d*registers.d);
|
|
|
|
float C = registers.d*registers.d + 2.0*registers.R*registers.d*cosangle.x*cosangle.y;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
return (-B-sqrt(B*B - 4.0*A*C))/(2.0*A);
|
|
|
|
}
|
2016-07-16 05:24:05 +10:00
|
|
|
|
|
|
|
vec2 bkwtrans(vec2 xy)
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-07-19 04:16:56 +10:00
|
|
|
float c = intersect(xy);
|
2016-08-03 03:13:40 +10:00
|
|
|
vec2 point = (vec2(c, c)*xy - vec2(-registers.R, -registers.R)*sinangle) / vec2(registers.R, registers.R);
|
2016-07-19 04:16:56 +10:00
|
|
|
vec2 poc = point/cosangle;
|
|
|
|
vec2 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);
|
|
|
|
vec2 uv = (point - a*sinangle) / cosangle;
|
2016-08-03 03:13:40 +10:00
|
|
|
float r = FIX(registers.R*acos(a));
|
2016-07-16 09:01:38 +10:00
|
|
|
|
2016-08-03 03:13:40 +10:00
|
|
|
return uv*r/sin(r/registers.R);
|
2016-07-16 09:01:38 +10:00
|
|
|
}
|
2016-07-16 05:24:05 +10:00
|
|
|
|
|
|
|
vec2 fwtrans(vec2 uv)
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-07-19 04:16:56 +10:00
|
|
|
float r = FIX(sqrt(dot(uv, uv)));
|
2016-08-03 03:13:40 +10:00
|
|
|
uv *= sin(r/registers.R)/r;
|
|
|
|
float x = 1.0 - cos(r/registers.R);
|
|
|
|
float D = registers.d/registers.R + x*cosangle.x*cosangle.y + dot(uv,sinangle);
|
2016-07-16 09:01:38 +10:00
|
|
|
|
2016-08-03 03:13:40 +10:00
|
|
|
return registers.d*(uv*cosangle - x*sinangle)/D;
|
2016-07-16 09:01:38 +10:00
|
|
|
}
|
2016-07-16 05:24:05 +10:00
|
|
|
|
|
|
|
vec3 maxscale()
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-08-03 03:13:40 +10:00
|
|
|
vec2 c = bkwtrans(-registers.R * sinangle / (1.0 + registers.R/registers.d*cosangle.x*cosangle.y));
|
2016-07-19 04:16:56 +10:00
|
|
|
vec2 a = vec2(0.5, 0.5)*aspect;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
2016-07-19 04:16:56 +10:00
|
|
|
vec2 lo = vec2(fwtrans(vec2(-a.x, c.y)).x,
|
|
|
|
fwtrans(vec2( c.x, -a.y)).y)/aspect;
|
|
|
|
vec2 hi = vec2(fwtrans(vec2(+a.x, c.y)).x,
|
|
|
|
fwtrans(vec2( c.x, +a.y)).y)/aspect;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
return vec3((hi+lo)*aspect*0.5,max(hi.x-lo.x, hi.y-lo.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
// "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
|
|
|
|
vec4 wid = 0.3 + 0.1 * pow(color, vec4(3.0));
|
|
|
|
vec4 weights = vec4(distance / wid);
|
2020-01-31 09:02:47 +11:00
|
|
|
return (registers.lum + 0.4) * exp(-weights * weights) / wid;
|
2016-07-16 09:01:38 +10:00
|
|
|
#else
|
|
|
|
vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0));
|
2016-08-03 03:13:40 +10:00
|
|
|
vec4 weights = vec4(distance / registers.scanline_weight);
|
2020-01-31 09:02:47 +11:00
|
|
|
return (registers.lum + 1.4) * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
|
2016-07-16 09:01:38 +10:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-16 05:24:05 +10:00
|
|
|
vec2 transform(vec2 coord)
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-07-19 04:16:56 +10:00
|
|
|
coord = (coord - vec2(0.5, 0.5))*aspect*stretch.z + stretch.xy;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
2016-07-19 04:16:56 +10:00
|
|
|
return (bkwtrans(coord) /
|
2016-08-03 03:13:40 +10:00
|
|
|
vec2(registers.overscan_x / 100.0, registers.overscan_y / 100.0)/aspect + vec2(0.5, 0.5));
|
2016-07-16 09:01:38 +10:00
|
|
|
}
|
|
|
|
|
2016-07-16 05:24:05 +10:00
|
|
|
float corner(vec2 coord)
|
2016-07-16 09:01:38 +10:00
|
|
|
{
|
2016-08-03 03:13:40 +10:00
|
|
|
coord = (coord - vec2(0.5)) * vec2(registers.overscan_x / 100.0, registers.overscan_y / 100.0) + vec2(0.5, 0.5);
|
2016-07-16 09:01:38 +10:00
|
|
|
coord = min(coord, vec2(1.0) - coord) * aspect;
|
2016-08-03 03:13:40 +10:00
|
|
|
vec2 cdist = vec2(registers.cornersize);
|
2016-07-16 09:01:38 +10:00
|
|
|
coord = (cdist - min(coord, cdist));
|
|
|
|
float dist = sqrt(dot(coord, coord));
|
|
|
|
|
2016-08-03 03:13:40 +10:00
|
|
|
return clamp((cdist.x - dist)*registers.cornersmooth, 0.0, 1.0);
|
2016-07-16 09:01:38 +10:00
|
|
|
}
|
|
|
|
|
2016-07-13 12:55:48 +10:00
|
|
|
void main()
|
|
|
|
{
|
2016-07-16 09:01:38 +10:00
|
|
|
// 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.
|
2016-08-03 03:13:40 +10:00
|
|
|
vec2 xy;
|
|
|
|
if (registers.CURVATURE > 0.5)
|
|
|
|
xy = transform(vTexCoord);
|
|
|
|
else
|
|
|
|
xy = vTexCoord;
|
2016-07-19 04:16:56 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
float cval = corner(xy);
|
2016-07-13 12:55:48 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
// Of all the pixels that are mapped onto the texel we are
|
|
|
|
// currently rendering, which pixel are we currently rendering?
|
2019-06-18 01:21:17 +10:00
|
|
|
|
|
|
|
vec2 ilvec = vec2(0.0, ilfac.y * registers.interlace_detect > 1.5 ? mod(float(registers.FrameCount), 2.0) : 0.0);
|
2016-07-19 04:16:56 +10:00
|
|
|
|
|
|
|
vec2 ratio_scale = (xy * TextureSize - vec2(0.5, 0.5) + ilvec) / ilfac;
|
2016-07-16 09:01:38 +10:00
|
|
|
vec2 uv_ratio = fract(ratio_scale);
|
|
|
|
|
|
|
|
// Snap to the center of the underlying texel.
|
2016-07-19 04:16:56 +10:00
|
|
|
xy = (floor(ratio_scale)*ilfac + vec2(0.5, 0.5) - ilvec) / TextureSize;
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
// Calculate Lanczos scaling coefficients describing the effect
|
|
|
|
// of various neighbour texels in a scanline on the current
|
|
|
|
// pixel.
|
|
|
|
vec4 coeffs = PI * vec4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x);
|
|
|
|
|
|
|
|
// Prevent division by zero.
|
|
|
|
coeffs = FIX(coeffs);
|
|
|
|
|
|
|
|
// Lanczos2 kernel.
|
|
|
|
coeffs = 2.0 * sin(coeffs) * sin(coeffs / 2.0) / (coeffs * coeffs);
|
|
|
|
|
|
|
|
// Normalize.
|
|
|
|
coeffs /= dot(coeffs, vec4(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.
|
|
|
|
vec4 col = clamp(
|
|
|
|
mat4(
|
|
|
|
TEX2D(xy + vec2(-one.x, 0.0)),
|
|
|
|
TEX2D(xy),
|
|
|
|
TEX2D(xy + vec2(one.x, 0.0)),
|
|
|
|
TEX2D(xy + vec2(2.0 * one.x, 0.0))
|
|
|
|
) * coeffs,
|
|
|
|
0.0, 1.0
|
|
|
|
);
|
|
|
|
vec4 col2 = clamp(
|
|
|
|
mat4(
|
|
|
|
TEX2D(xy + vec2(-one.x, one.y)),
|
|
|
|
TEX2D(xy + vec2(0.0, one.y)),
|
|
|
|
TEX2D(xy + one),
|
|
|
|
TEX2D(xy + vec2(2.0 * one.x, one.y))
|
|
|
|
) * coeffs,
|
|
|
|
0.0, 1.0
|
|
|
|
);
|
2016-07-13 12:55:48 +10:00
|
|
|
|
|
|
|
#ifndef LINEAR_PROCESSING
|
2016-08-03 03:13:40 +10:00
|
|
|
col = pow(col , vec4(registers.CRTgamma));
|
|
|
|
col2 = pow(col2, vec4(registers.CRTgamma));
|
2016-07-13 12:55:48 +10:00
|
|
|
#endif
|
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
// 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);
|
2016-07-19 04:16:56 +10:00
|
|
|
|
2016-07-13 12:55:48 +10:00
|
|
|
#ifdef OVERSAMPLE
|
2016-07-19 04:16:56 +10:00
|
|
|
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;
|
2016-07-13 12:55:48 +10:00
|
|
|
#endif
|
2016-07-19 04:16:56 +10:00
|
|
|
|
2016-07-16 09:01:38 +10:00
|
|
|
vec3 mul_res = (col * weights + col2 * weights2).rgb * vec3(cval);
|
|
|
|
|
|
|
|
// dot-mask emulation:
|
|
|
|
// Output pixels are alternately tinted green and magenta.
|
|
|
|
vec3 dotMaskWeights = mix(
|
2016-08-03 03:13:40 +10:00
|
|
|
vec3(1.0, 1.0 - registers.DOTMASK, 1.0),
|
|
|
|
vec3(1.0 - registers.DOTMASK, 1.0, 1.0 - registers.DOTMASK),
|
2016-07-19 02:09:14 +10:00
|
|
|
floor(mod(mod_factor, 2.0))
|
2016-07-16 09:01:38 +10:00
|
|
|
);
|
|
|
|
|
|
|
|
mul_res *= dotMaskWeights;
|
|
|
|
|
|
|
|
// Convert the image gamma for display on our output device.
|
2016-08-03 03:13:40 +10:00
|
|
|
mul_res = pow(mul_res, vec3(1.0 / registers.monitorgamma));
|
2016-07-16 09:01:38 +10:00
|
|
|
|
|
|
|
FragColor = vec4(mul_res, 1.0);
|
2016-07-13 12:55:48 +10:00
|
|
|
}
|