#pragma parameter INTERLACING_TOGGLE "Interlacing Detection Toggle" 1.0 0.0 1.0 1.0
#pragma parameter PERCENT "Scanline Brightness %" 0.9 0.0 1.0 0.05
#pragma parameter DOTMASK "cgwg Dot Mask Toggle" 0.3 0.0 0.3 0.3
#pragma parameter LOTTE_TOGGLE "Lotte Dotmask Toggle" 0.0 0.0 0.3 0.3
#pragma parameter BRIGHTBOOST "Interlaced BrightBoost" 1.1 0.0 2.0 0.05

#ifdef PARAMETER_UNIFORM
uniform float PERCENT;
uniform float DOTMASK;
uniform float INTERLACING_TOGGLE;
uniform float LOTTE_TOGGLE;
uniform float BRIGHTBOOST;
#else
#define PERCENT 0.7
#define DOTMASK 0.3
#define INTERLACING_TOGGLE 1.0
#define LOTTE_TOGGLE 0.0
#define BRIGHTBOOST 1.1
#endif

/*
   Interlacing
   Author: hunterk
   License: GPL (due to use of cgwg's GPLed dotmask code)
   
   Note: This shader is designed to work with the typical interlaced output from an emulator, which displays both even and odd fields twice.
   This shader will un-weave the image, resulting in a standard, alternating-field interlacing.
*/

/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
   - FX11 compilers
*/

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

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;
}

const float3x3 yiq_mat = float3x3(
	0.2989, 0.5870, 0.1140,
	0.5959, -0.2744, -0.3216,
	0.2115, -0.5229, 0.3114
);

float3 rgb2yiq(float3 col)
{
	return mul(yiq_mat, col);
}

// lotte's Shadow mask.
float3 Mask(float2 pos)
{
	pos.x += pos.y * 3.0;
	float3 mask = float3(1.0 - LOTTE_TOGGLE,1.0 - LOTTE_TOGGLE,1.0 - LOTTE_TOGGLE);
	pos.x = fract(pos.x/6.0);
	if(pos.x<0.333) mask.r = (1.0 + LOTTE_TOGGLE);
	else if(pos.x<0.666) mask.g = (1.0 + LOTTE_TOGGLE);
	else mask.b = (1.0 + LOTTE_TOGGLE);
	return mask;
}

#define one_pixel float2(1.0 / texture_size)

float4 interlacing_phosphor(float2 texture_size, float2 video_size, float2 output_size, float frame_count, float2 texCoord, COMPAT_Texture2D(decal))
{
	float4 res = COMPAT_Sample(decal, texCoord);
	float one_right = rgb2yiq(COMPAT_Sample(decal, texCoord + float2(0.5 * one_pixel.x, 0.0)).rgb).r;
	float one_left = rgb2yiq(COMPAT_Sample(decal, texCoord - float2(0.5 * one_pixel.x, 0.0)).rgb).r;
	float y = 0.0;
	float4 lineblank = float4(1,1,1,1);
	float4 lotte_mask = float4(Mask(floor(texCoord.xy*(texture_size.xy/video_size.xy)*output_size.xy)+float2(0.5,0.5)), 1.0);
   
	// cgwg's aperture grille emulation:
	// Output pixels are alternately tinted green and magenta.
	float4 dotMaskWeights = float4(lerp(
		float3(1.0, 1.0 - DOTMASK, 1.0),
		float3(1.0 - DOTMASK, 1.0, 1.0 - DOTMASK),
		floor(fmod((texCoord.x * texture_size.x * output_size.x / video_size.x), 2.0))
		), 1.0);

   // assume anything with a vertical resolution greater than 400 lines is interlaced
		if (video_size.y > 400.0)
	{
		y = texture_size.y * texCoord.y + (frame_count * INTERLACING_TOGGLE);
		lineblank = float4(0,0,0,0);
		res *= BRIGHTBOOST;
	}
	else
		y = 2.000001 * texture_size.y * texCoord.y;

	if (fmod(y, 2.0) > 0.99999) return res * dotMaskWeights * lotte_mask * 1.15;
	else
	{
		float intermed = clamp((one_right + one_left) / 2.0, 0.0, PERCENT);
		return (float4(intermed,intermed,intermed,intermed) * lineblank * res * dotMaskWeights * lotte_mask * 1.15);
	}
}

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