/*
    CRT Shader by EasyMode
    License: GPL
*/

#pragma parameter SHARPNESS_IMAGE "Sharpness Image" 1.0 1.0 5.0 1.0
#pragma parameter SHARPNESS_EDGES "Sharpness Edges" 3.0 1.0 5.0 1.0
#pragma parameter GLOW_WIDTH "Glow Width" 0.5 0.05 0.65 0.05
#pragma parameter GLOW_HEIGHT "Glow Height" 0.5 0.05 0.65 0.05
#pragma parameter GLOW_HALATION "Glow Halation" 0.1 0.0 1.0 0.01
#pragma parameter GLOW_DIFFUSION "Glow Diffusion" 0.05 0.0 1.0 0.01
#pragma parameter MASK_COLORS "Mask Colors" 2.0 2.0 3.0 1.0
#pragma parameter MASK_STRENGTH "Mask Strength" 0.3 0.0 1.0 0.05
#pragma parameter MASK_SIZE "Mask Size" 1.0 1.0 9.0 1.0
#pragma parameter SCANLINE_SIZE_MIN "Scanline Size Min." 0.5 0.5 1.5 0.05
#pragma parameter SCANLINE_SIZE_MAX "Scanline Size Max." 1.5 0.5 1.5 0.05
#pragma parameter GAMMA_INPUT "Gamma Input" 2.4 1.0 5.0 0.1
#pragma parameter GAMMA_OUTPUT "Gamma Output" 2.4 1.0 5.0 0.1
#pragma parameter BRIGHTNESS "Brightness" 1.5 0.0 2.0 0.05
#ifdef PARAMETER_UNIFORM
uniform float SHARPNESS_IMAGE;
uniform float SHARPNESS_EDGES;
uniform float GLOW_WIDTH;
uniform float GLOW_HEIGHT;
uniform float GLOW_HALATION;
uniform float GLOW_DIFFUSION;
uniform float MASK_COLORS;
uniform float MASK_STRENGTH;
uniform float MASK_SIZE;
uniform float SCANLINE_SIZE_MIN;
uniform float SCANLINE_SIZE_MAX;
uniform float GAMMA_INPUT;
uniform float GAMMA_OUTPUT;
uniform float BRIGHTNESS;
#else
#define SHARPNESS_IMAGE 1.0
#define SHARPNESS_EDGES 3.0
#define GLOW_WIDTH 0.5
#define GLOW_HEIGHT 0.5
#define GLOW_HALATION 0.1
#define GLOW_DIFFUSION 0.05
#define MASK_COLORS 2.0
#define MASK_STRENGTH 0.3
#define MASK_SIZE 1.0
#define SCANLINE_SIZE_MIN 0.5
#define SCANLINE_SIZE_MAX 1.5
#define GAMMA_INPUT 2.4
#define GAMMA_OUTPUT 2.4
#define BRIGHTNESS 1.5
#endif

#include "../../compat_includes.inc"
uniform COMPAT_Texture2D(decal) : TEXUNIT0;
uniform float4x4 modelViewProj;

#define FIX(c) max(abs(c), 1e-5)
#define PI 3.141592653589
#define TEX2D(c) pow(COMPAT_SamplePoint(tex, c).rgb, GAMMA_INPUT)
#define mod(x,y) (x - y * trunc(x/y))

struct out_vertex
{
  float4 position : COMPAT_POS;
  float2 texCoord : TEXCOORD0;
#ifndef HLSL_4
	float4 Color    : COLOR;
#endif
};

out_vertex main_vertex(COMPAT_IN_VERTEX)
{
	out_vertex OUT;
#ifdef HLSL_4
	float4 position = VIN.position;
	float2 texCoord = VIN.texCoord;
#else
	OUT.Color = color;
#endif
	OUT.position = mul(modelViewProj, position);
	OUT.texCoord = texCoord;
	
	return OUT;
}

float3x3 get_color_matrix(COMPAT_Texture2D(tex), float2 co, float2 dx)
{
    return float3x3(TEX2D(co - dx), TEX2D(co), TEX2D(co + dx));
}

float3 blur(float3x3 m, float dist, float rad)
{
    float3 x = float3(dist - 1.0, dist, dist + 1.0) / rad;
    float3 w = exp2(x * x * -1.0);

    return (m[0] * w.x + m[1] * w.y + m[2] * w.z) / (w.x + w.y + w.z);
}

float3 filter_gaussian(COMPAT_Texture2D(tex), float2 co, float2 tex_size)
{
    float2 dx = float2(1.0 / tex_size.x, 0.0);
    float2 dy = float2(0.0, 1.0 / tex_size.y);
    float2 pix_co = co * tex_size;
    float2 tex_co = (floor(pix_co) + 0.5) / tex_size;
    float2 dist = (frac(pix_co) - 0.5) * -1.0;

    float3x3 line0 = get_color_matrix(tex, tex_co - dy, dx);
    float3x3 line1 = get_color_matrix(tex, tex_co, dx);
    float3x3 line2 = get_color_matrix(tex, tex_co + dy, dx);
    float3x3 column = float3x3(blur(line0, dist.x, GLOW_WIDTH),
                               blur(line1, dist.x, GLOW_WIDTH),
                               blur(line2, dist.x, GLOW_WIDTH));

    return blur(column, dist.y, GLOW_HEIGHT);
}

float3 filter_lanczos(COMPAT_Texture2D(tex), float2 co, float2 tex_size, float sharp)
{
    tex_size.x *= sharp;

    float2 dx = float2(1.0 / tex_size.x, 0.0);
    float2 pix_co = co * tex_size - float2(0.5, 0.0);
    float2 tex_co = (floor(pix_co) + float2(0.5, 0.0)) / tex_size;
    float2 dist = frac(pix_co);
    float4 coef = PI * float4(dist.x + 1.0, dist.x, dist.x - 1.0, dist.x - 2.0);

    coef = FIX(coef);
    coef = 2.0 * sin(coef) * sin(coef / 2.0) / (coef * coef);
    coef /= dot(coef, float4(1.0,1.0,1.0,1.0));

    float4 col1 = float4(TEX2D(tex_co), 1.0);
    float4 col2 = float4(TEX2D(tex_co + dx), 1.0);

    return mul(coef, float4x4(col1, col1, col2, col2)).rgb;
}

float3 get_scanline_weight(float x, float3 col)
{
    float3 beam = lerp(float3(SCANLINE_SIZE_MIN,SCANLINE_SIZE_MIN,SCANLINE_SIZE_MIN), float3(SCANLINE_SIZE_MAX,SCANLINE_SIZE_MAX,SCANLINE_SIZE_MAX), col);
    float3 x_mul = 2.0 / beam;
    float3 x_offset = x_mul * 0.5;

    return smoothstep(0.0, 1.0, 1.0 - abs(x * x_mul - x_offset)) * x_offset;
}

float3 get_mask_weight(float x, float2 texture_size, float2 video_size, float2 output_size)
{
    float i = mod(floor(x * output_size.x * texture_size.x / (video_size.x * MASK_SIZE)), MASK_COLORS);

    if (i == 0.0) return lerp(float3(1.0, 0.0, 1.0), float3(1.0, 0.0, 0.0), MASK_COLORS - 2.0);
    else if (i == 1.0) return float3(0.0, 1.0, 0.0);
    else return float3(0.0, 0.0, 1.0);
}

float4 crt_aperture(COMPAT_Texture2D(tex), float2 co, float2 texture_size, float2 video_size, float2 output_size)
{
    float3 col_glow = filter_gaussian(tex, co, texture_size);
    float3 col_soft = filter_lanczos(tex, co, texture_size, SHARPNESS_IMAGE);
    float3 col_sharp = filter_lanczos(tex, co, texture_size, SHARPNESS_EDGES);
    float3 col = sqrt(col_sharp * col_soft);

    col *= get_scanline_weight(frac(co.y * texture_size.y), col_soft);
    col_glow = saturate(col_glow - col);
    col += col_glow * col_glow * GLOW_HALATION;
    col = lerp(col, col * get_mask_weight(co.x, texture_size, video_size, output_size) * MASK_COLORS, MASK_STRENGTH);
    col += col_glow * GLOW_DIFFUSION;
    col = pow(col * BRIGHTNESS, 1.0 / GAMMA_OUTPUT);

    return float4(col, 1.0);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return crt_aperture(decal, VOUT.texCoord, COMPAT_texture_size, COMPAT_video_size, COMPAT_output_size);
}
COMPAT_END
