/*
   Author: Gigaherz
   License: Public domain
*/

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

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

/* configuration (higher values mean brighter image but reduced effect depth) */
const int brighten_scanlines = 16;
const int brighten_lcd = 4;

struct out_vertex
{
	float4 position : COMPAT_POS;
	float2 texCoord : TEXCOORD0;
	float2 omega    : TEXCOORD1;
#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;

    OUT.omega = 3.141592654 * 2 * COMPAT_texture_size;
	return OUT;
}

const float3 offsets = 3.141592654 * float3(1.0/2,1.0/2 - 2.0/3,1.0/2-4.0/3);

float4 lcd3x(COMPAT_Texture2D(decal), float2 omega, float2 texCoord)
{
    float3 res = COMPAT_SamplePoint(decal, texCoord).xyz;

    float2 angle = texCoord * omega;
	
    float yfactor = (brighten_scanlines + sin(angle.y)) / (brighten_scanlines + 1);
    float3 xfactors = (brighten_lcd + sin(angle.x + offsets)) / (brighten_lcd + 1);
   
    float3 color = yfactor * xfactors * res;
   
    return float4(color.x, color.y, color.z, 1.0);
}

float4 main_fragment(COMPAT_IN_FRAGMENT) : COMPAT_Output
{
	return lcd3x(decal, VOUT.omega, VOUT.texCoord);
}
COMPAT_END
