Update crt-geom.slang

Changed the formatting, tab size is 4 spaces.
Based on the discussion in IRC, I carefully deleted every instance of global.SourceSize.zw, because z = 1/x and w = 1/y and zw is NOT the same as IN.video_size.
This commit is contained in:
rz5 2016-07-16 00:01:38 +01:00 committed by GitHub
parent f14ba96715
commit c864dbb2d4

View file

@ -46,33 +46,33 @@ layout(std140, set = 0, binding = 0) uniform UBO
This shader variant is pre-configured with screen curvature This shader variant is pre-configured with screen curvature
*/ */
// Comment the next line to disable interpolation in linear gamma (and // Comment the next line to disable interpolation in linear gamma (and
// gain speed). // gain speed).
#define LINEAR_PROCESSING #define LINEAR_PROCESSING
// Enable 3x oversampling of the beam profile; improves moire effect caused by scanlines+curvature // Enable 3x oversampling of the beam profile; improves moire effect caused by scanlines+curvature
#define OVERSAMPLE #define OVERSAMPLE
// Use the older, purely gaussian beam profile; uncomment for speed // Use the older, purely gaussian beam profile; uncomment for speed
#define USEGAUSSIAN #define USEGAUSSIAN
// Use interlacing detection; may interfere with other shaders if combined // Use interlacing detection; may interfere with other shaders if combined
#define INTERLACED #define INTERLACED
// Macros. // Macros.
#define FIX(c) max(abs(c), 1e-5); #define FIX(c) max(abs(c), 1e-5);
#define PI 3.141592653589 #define PI 3.141592653589
#ifdef LINEAR_PROCESSING #ifdef LINEAR_PROCESSING
# define TEX2D(c) pow(texture(Source, (c)), vec4(CRTgamma)) # define TEX2D(c) pow(texture(Source, (c)), vec4(CRTgamma))
#else #else
# define TEX2D(c) texture(Source, (c)) # define TEX2D(c) texture(Source, (c))
#endif #endif
// aspect ratio // aspect ratio
vec2 aspect = vec2(1.0, 0.75); vec2 aspect = vec2(1.0, 0.75);
vec2 angle = vec2(0.0, 0.0); vec2 angle = vec2(0.0, 0.0);
vec2 overscan = vec2(1.01, 1.01); vec2 overscan = vec2(1.01, 1.01);
#pragma stage vertex #pragma stage vertex
layout(location = 0) in vec4 Position; layout(location = 0) in vec4 Position;
@ -86,58 +86,64 @@ layout(location = 5) out vec2 one;
layout(location = 6) out float mod_factor; layout(location = 6) out float mod_factor;
float intersect(vec2 xy) float intersect(vec2 xy)
{ {
float A = dot(xy,xy)+d*d; float A = dot(xy,xy) + d*d;
float B = 2.0*(R*(dot(xy,sinangle)-d*cosangle.x*cosangle.y)-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; float C = d*d + 2.0*R*d*cosangle.x*cosangle.y;
return (-B-sqrt(B*B-4.0*A*C))/(2.0*A); return (-B-sqrt(B*B-4.0*A*C))/(2.0*A);
} }
vec2 bkwtrans(vec2 xy) vec2 bkwtrans(vec2 xy)
{ {
float c = intersect(xy); float c = intersect(xy);
vec2 point = vec2(c)*xy; vec2 point = vec2(c)*xy;
point -= vec2(-R)*sinangle; point -= vec2(-R)*sinangle;
point /= vec2(R); point /= vec2(R);
vec2 tang = sinangle/cosangle; vec2 tang = sinangle/cosangle;
vec2 poc = point/cosangle; vec2 poc = point/cosangle;
float A = dot(tang,tang)+1.0; float A = dot(tang,tang)+1.0;
float B = -2.0*dot(poc,tang); float B = -2.0*dot(poc,tang);
float C = dot(poc,poc)-1.0; float C = dot(poc,poc)-1.0;
float a = (-B+sqrt(B*B-4.0*A*C))/(2.0*A); float a = (-B+sqrt(B*B-4.0*A*C))/(2.0*A);
vec2 uv = (point-a*sinangle)/cosangle; vec2 uv = (point-a*sinangle)/cosangle;
float r = R*acos(a); float r = R*acos(a);
return uv*r/sin(r/R); return uv*r/sin(r/R);
} }
vec2 fwtrans(vec2 uv) vec2 fwtrans(vec2 uv)
{ {
float r = FIX(sqrt(dot(uv,uv))); float r = FIX(sqrt(dot(uv,uv)));
uv *= sin(r/R)/r; uv *= sin(r/R)/r;
float x = 1.0-cos(r/R); float x = 1.0-cos(r/R);
float D = d/R + x*cosangle.x*cosangle.y+dot(uv,sinangle); float D = d/R + x*cosangle.x*cosangle.y+dot(uv,sinangle);
return d*(uv*cosangle-x*sinangle)/D; return d*(uv*cosangle-x*sinangle)/D;
} }
vec3 maxscale() vec3 maxscale()
{ {
vec2 c = bkwtrans(-R * sinangle / (1.0 + R/d*cosangle.x*cosangle.y)); vec2 c = bkwtrans(-R * sinangle / (1.0 + R/d*cosangle.x*cosangle.y));
vec2 a = vec2(0.5,0.5)*aspect; vec2 a = vec2(0.5,0.5)*aspect;
vec2 lo = vec2(fwtrans(vec2(-a.x,c.y)).x, vec2 lo = vec2(fwtrans(vec2(-a.x,c.y)).x,
fwtrans(vec2(c.x,-a.y)).y)/aspect; fwtrans(vec2(c.x,-a.y)).y)/aspect;
vec2 hi = vec2(fwtrans(vec2(+a.x,c.y)).x, vec2 hi = vec2(fwtrans(vec2(+a.x,c.y)).x,
fwtrans(vec2(c.x,+a.y)).y)/aspect; fwtrans(vec2(c.x,+a.y)).y)/aspect;
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. return vec3((hi+lo)*aspect*0.5,max(hi.x-lo.x,hi.y-lo.y));
// }
// 'distance' is the distance in texture coordinates from the current
// pixel to the scanline in question. // Calculate the influence of a scanline on the current pixel.
// 'color' is the colour of the scanline at the horizontal location of //
// the current pixel. // 'distance' is the distance in texture coordinates from the current
vec4 scanlineWeights(float distance, vec4 color) // 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 // "wid" controls the width of the scanline beam, for each RGB
// channel The "weights" lines basically specify the formula // channel The "weights" lines basically specify the formula
// that gives you the profile of the beam, i.e. the intensity as // that gives you the profile of the beam, i.e. the intensity as
@ -151,13 +157,15 @@ vec3 maxscale()
#ifdef USEGAUSSIAN #ifdef USEGAUSSIAN
vec4 wid = 0.3 + 0.1 * pow(color, vec4(3.0)); vec4 wid = 0.3 + 0.1 * pow(color, vec4(3.0));
vec4 weights = vec4(distance / wid); vec4 weights = vec4(distance / wid);
return 0.4 * exp(-weights * weights) / wid; return 0.4 * exp(-weights * weights) / wid;
#else #else
vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0)); vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0));
vec4 weights = vec4(distance / scanline_weight); vec4 weights = vec4(distance / scanline_weight);
return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid); return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
#endif #endif
} }
void main() void main()
{ {
@ -176,7 +184,7 @@ void main()
one = ilfac / global.SourceSize.xy; one = ilfac / global.SourceSize.xy;
// Resulting X pixel-coordinate of the pixel we're drawing. // Resulting X pixel-coordinate of the pixel we're drawing.
mod_factor = TexCoord.x * (global.SourceSize.x / global.SourceSize.z) * (global.SourceSize.z / global.SourceSize.x); mod_factor = TexCoord.x * global.SourceSize.x * global.OutputSize.x / global.SourceSize.x;
} }
#pragma stage fragment #pragma stage fragment
@ -191,58 +199,67 @@ layout(location = 0) out vec4 FragColor;
layout(set = 0, binding = 2) uniform sampler2D Source; layout(set = 0, binding = 2) uniform sampler2D Source;
float intersect(vec2 xy) float intersect(vec2 xy)
{ {
float A = dot(xy,xy)+d*d; float A = dot(xy,xy) + d*d;
float B = 2.0*(R*(dot(xy,sinangle)-d*cosangle.x*cosangle.y)-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; float C = d*d + 2.0*R*d*cosangle.x*cosangle.y;
return (-B-sqrt(B*B-4.0*A*C))/(2.0*A);
} return (-B-sqrt(B*B - 4.0*A*C))/(2.0*A);
}
vec2 bkwtrans(vec2 xy) vec2 bkwtrans(vec2 xy)
{ {
float c = intersect(xy); float c = intersect(xy);
vec2 point = vec2(c)*xy; vec2 point = vec2(c)*xy;
point -= vec2(-R)*sinangle; point -= vec2(-R)*sinangle;
point /= vec2(R); point /= vec2(R);
vec2 tang = sinangle/cosangle; vec2 tang = sinangle/cosangle;
vec2 poc = point/cosangle; vec2 poc = point/cosangle;
float A = dot(tang,tang)+1.0; float A = dot(tang,tang)+1.0;
float B = -2.0*dot(poc,tang); float B = -2.0*dot(poc,tang);
float C = dot(poc,poc)-1.0; float C = dot(poc,poc)-1.0;
float a = (-B+sqrt(B*B-4.0*A*C))/(2.0*A); float a = (-B+sqrt(B*B-4.0*A*C))/(2.0*A);
vec2 uv = (point-a*sinangle)/cosangle; vec2 uv = (point-a*sinangle)/cosangle;
float r = R*acos(a); float r = R*acos(a);
return uv*r/sin(r/R); return uv*r/sin(r/R);
} }
vec2 fwtrans(vec2 uv) vec2 fwtrans(vec2 uv)
{ {
float r = FIX(sqrt(dot(uv,uv))); float r = FIX(sqrt(dot(uv,uv)));
uv *= sin(r/R)/r; uv *= sin(r/R)/r;
float x = 1.0-cos(r/R); float x = 1.0-cos(r/R);
float D = d/R + x*cosangle.x*cosangle.y+dot(uv,sinangle); float D = d/R + x*cosangle.x*cosangle.y + dot(uv,sinangle);
return d*(uv*cosangle-x*sinangle)/D; return d*(uv*cosangle-x*sinangle)/D;
} }
vec3 maxscale() vec3 maxscale()
{ {
vec2 c = bkwtrans(-R * sinangle / (1.0 + R/d*cosangle.x*cosangle.y)); vec2 c = bkwtrans(-R * sinangle / (1.0 + R/d*cosangle.x*cosangle.y));
vec2 a = vec2(0.5,0.5)*aspect; vec2 a = vec2(0.5,0.5)*aspect;
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;
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. vec2 lo = vec2(fwtrans(vec2(-a.x,c.y)).x,
// fwtrans(vec2(c.x, -a.y)).y)/aspect;
// 'distance' is the distance in texture coordinates from the current vec2 hi = vec2(fwtrans(vec2(+a.x,c.y)).x,
// pixel to the scanline in question. fwtrans(vec2(c.x, +a.y)).y)/aspect;
// 'color' is the colour of the scanline at the horizontal location of
// the current pixel. return vec3((hi+lo)*aspect*0.5,max(hi.x-lo.x, hi.y-lo.y));
vec4 scanlineWeights(float distance, vec4 color) }
{
// 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 // "wid" controls the width of the scanline beam, for each RGB
// channel The "weights" lines basically specify the formula // channel The "weights" lines basically specify the formula
// that gives you the profile of the beam, i.e. the intensity as // that gives you the profile of the beam, i.e. the intensity as
@ -253,34 +270,36 @@ vec3 maxscale()
// independent of its width. That is, for a narrower beam // independent of its width. That is, for a narrower beam
// "weights" should have a higher peak at the center of the // "weights" should have a higher peak at the center of the
// scanline than for a wider beam. // scanline than for a wider beam.
#ifdef USEGAUSSIAN #ifdef USEGAUSSIAN
vec4 wid = 0.3 + 0.1 * pow(color, vec4(3.0)); vec4 wid = 0.3 + 0.1 * pow(color, vec4(3.0));
vec4 weights = vec4(distance / wid); vec4 weights = vec4(distance / wid);
return 0.4 * exp(-weights * weights) / wid; return 0.4 * exp(-weights * weights) / wid;
#else #else
vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0)); vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0));
vec4 weights = vec4(distance / scanline_weight); vec4 weights = vec4(distance / scanline_weight);
return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid); return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
#endif #endif
} }
vec2 transform(vec2 coord) vec2 transform(vec2 coord)
{ {
coord *= global.SourceSize.xy / global.SourceSize.zw; coord *= global.SourceSize.xy;
coord = (coord-vec2(0.5))*aspect*stretch.z+stretch.xy; coord = (coord-vec2(0.5))*aspect*stretch.z+stretch.xy;
return (bkwtrans(coord)/vec2(overscan_x / 100.0, overscan_y / 100.0)/aspect+vec2(0.5)) * global.SourceSize.zw / global.SourceSize.xy;
} return (bkwtrans(coord)/vec2(overscan_x / 100.0, overscan_y / 100.0)/aspect+vec2(0.5)) * global.SourceSize.xy;
}
float corner(vec2 coord) float corner(vec2 coord)
{ {
// coord *= global.SourceSize.xy / global.SourceSize.zw; // coord *= global.SourceSize.xy / global.SourceSize.zw;
coord = (coord - vec2(0.5)) * vec2(overscan_x / 100.0, overscan_y / 100.0) + vec2(0.5); coord = (coord - vec2(0.5)) * vec2(overscan_x / 100.0, overscan_y / 100.0) + vec2(0.5);
coord = min(coord, vec2(1.0)-coord) * aspect; coord = min(coord, vec2(1.0) - coord) * aspect;
vec2 cdist = vec2(cornersize); vec2 cdist = vec2(cornersize);
coord = (cdist - min(coord,cdist)); coord = (cdist - min(coord, cdist));
float dist = sqrt(dot(coord,coord)); float dist = sqrt(dot(coord, coord));
return clamp((cdist.x-dist)*cornersmooth,0.0, 1.0);
} return clamp((cdist.x-dist)*cornersmooth, 0.0, 1.0);
}
void main() void main()
{ {
@ -316,9 +335,9 @@ void main()
// Of all the pixels that are mapped onto the texel we are // Of all the pixels that are mapped onto the texel we are
// currently rendering, which pixel are we currently rendering? // currently rendering, which pixel are we currently rendering?
#ifdef INTERLACED #ifdef INTERLACED
vec2 ilvec = vec2(0.0,ilfac.y > 1.5 ? mod(float(global.FrameCount),2.0) : 0.0); vec2 ilvec = vec2(0.0, ilfac.y > 1.5 ? mod(float(global.FrameCount), 2.0) : 0.0);
#else #else
vec2 ilvec = vec2(0.0,ilfac.y); vec2 ilvec = vec2(0.0, ilfac.y);
#endif #endif
vec2 ratio_scale = (xy * global.SourceSize.xy - vec2(0.5, 0.5) + ilvec)/ilfac; vec2 ratio_scale = (xy * global.SourceSize.xy - vec2(0.5, 0.5) + ilvec)/ilfac;
#ifdef OVERSAMPLE #ifdef OVERSAMPLE
@ -346,18 +365,24 @@ void main()
// Calculate the effective colour of the current and next // Calculate the effective colour of the current and next
// scanlines at the horizontal location of the current pixel, // scanlines at the horizontal location of the current pixel,
// using the Lanczos coefficients above. // using the Lanczos coefficients above.
vec4 col = clamp(mat4( vec4 col = clamp(
mat4(
TEX2D(xy + vec2(-one.x, 0.0)), TEX2D(xy + vec2(-one.x, 0.0)),
TEX2D(xy), TEX2D(xy),
TEX2D(xy + vec2(one.x, 0.0)), TEX2D(xy + vec2(one.x, 0.0)),
TEX2D(xy + vec2(2.0 * one.x, 0.0))) * coeffs, TEX2D(xy + vec2(2.0 * one.x, 0.0))
0.0, 1.0); ) * coeffs,
vec4 col2 = clamp(mat4( 0.0, 1.0
);
vec4 col2 = clamp(
mat4(
TEX2D(xy + vec2(-one.x, one.y)), TEX2D(xy + vec2(-one.x, one.y)),
TEX2D(xy + vec2(0.0, one.y)), TEX2D(xy + vec2(0.0, one.y)),
TEX2D(xy + one), TEX2D(xy + one),
TEX2D(xy + vec2(2.0 * one.x, one.y))) * coeffs, TEX2D(xy + vec2(2.0 * one.x, one.y))
0.0, 1.0); ) * coeffs,
0.0, 1.0
);
#ifndef LINEAR_PROCESSING #ifndef LINEAR_PROCESSING
col = pow(col , vec4(CRTgamma)); col = pow(col , vec4(CRTgamma));
@ -369,12 +394,12 @@ void main()
vec4 weights = scanlineWeights(uv_ratio.y, col); vec4 weights = scanlineWeights(uv_ratio.y, col);
vec4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2); vec4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2);
#ifdef OVERSAMPLE #ifdef OVERSAMPLE
uv_ratio.y =uv_ratio.y+1.0/3.0*filter_; uv_ratio.y = uv_ratio.y + 1.0/3.0*filter_;
weights = (weights+scanlineWeights(uv_ratio.y, col))/3.0; weights = (weights + scanlineWeights(uv_ratio.y, col))/3.0;
weights2=(weights2+scanlineWeights(abs(1.0-uv_ratio.y), col2))/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_; uv_ratio.y = uv_ratio.y - 2.0/3.0*filter_;
weights=weights+scanlineWeights(abs(uv_ratio.y), col)/3.0; weights = weights + scanlineWeights(abs(uv_ratio.y), col)/3.0;
weights2=weights2+scanlineWeights(abs(1.0-uv_ratio.y), col2)/3.0; weights2 = weights2 + scanlineWeights(abs(1.0-uv_ratio.y), col2)/3.0;
#endif #endif
vec3 mul_res = (col * weights + col2 * weights2).rgb * vec3(cval); vec3 mul_res = (col * weights + col2 * weights2).rgb * vec3(cval);
@ -391,5 +416,5 @@ void main()
// Convert the image gamma for display on our output device. // Convert the image gamma for display on our output device.
mul_res = pow(mul_res, vec3(1.0 / monitorgamma)); mul_res = pow(mul_res, vec3(1.0 / monitorgamma));
FragColor = vec4(mul_res, 1.0); FragColor = vec4(mul_res, 1.0);
} }