/* COMPATIBILITY 
   - HLSL compilers
   - Cg   compilers
*/

/*
   Dot Mask
   Authors: cgwg, Timothy Lottes
   License: GPL
   
   Note: This shader is just the dotmask functions from cgwg's CRT shader and crt-lottes.
*/

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

#define mod_factor texCoord.x * texture_size.x * output_size.x / video_size.x

#pragma parameter DOTMASK_STRENGTH "CGWG Dot Mask Strength" 0.3 0.0 1.0 0.01
#pragma parameter maskDark "Lottes maskDark" 0.5 0.0 2.0 0.1
#pragma parameter maskLight "Lottes maskLight" 1.5 0.0 2.0 0.1
#pragma parameter shadowMask "Mask Style" 3.0 0.0 4.0 1.0
#ifdef PARAMETER_UNIFORM
uniform float DOTMASK_STRENGTH;
uniform float maskDark;
uniform float maskLight;
uniform float shadowMask;
#else
#define DOTMASK_STRENGTH 0.3
#define maskDark 0.5
#define maskLight 1.5
#define shadowMask 3.0
#endif

float3 Mask(float2 pos){
  float3 mask=float3(maskDark,maskDark,maskDark);

  // Very compressed TV style shadow mask.
  if (shadowMask == 1) {
    float mask_line = maskLight;
    float odd = 0.0;
    if(frac(pos.x/6.0)<0.5)odd = 1.0;
    if(frac((pos.y+odd)/2.0)<0.5) mask_line = maskDark;  
    pos.x = frac(pos.x/3.0);
   
    if(pos.x<0.333)mask.r = maskLight;
    else if(pos.x<0.666)mask.g = maskLight;
    else mask.b = maskLight;
    mask *= mask_line;  
  } 

  // Aperture-grille.
  else if (shadowMask == 2) {
    pos.x=frac(pos.x/3.0);

    if(pos.x<0.333)mask.r=maskLight;
    else if(pos.x<0.666)mask.g=maskLight;
    else mask.b=maskLight;
  } 

  // Stretched VGA style shadow mask (same as prior shaders).
  else if (shadowMask == 3) {
    pos.x+=pos.y*3.0;
    pos.x=frac(pos.x/6.0);

    if(pos.x<0.333)mask.r=maskLight;
    else if(pos.x<0.666)mask.g=maskLight;
    else mask.b=maskLight;
  }

  // VGA style shadow mask.
  else if (shadowMask == 4) {
    pos.xy=floor(pos.xy*float2(1.0,0.5));
    pos.x+=pos.y*3.0;
    pos.x=frac(pos.x/6.0);

    if(pos.x<0.333)mask.r=maskLight;
    else if(pos.x<0.666)mask.g=maskLight;
    else mask.b=maskLight;
  }

  return mask;}  

float4 dotmask(float2 texture_size, float2 video_size, float2 output_size, float2 texCoord, COMPAT_Texture2D(decal))
{
   float3 res = COMPAT_Sample(decal, texCoord).rgb;
   
   float mask = 1.0 - DOTMASK_STRENGTH;
   
   // dot-mask emulation:
   // Output pixels are alternately tinted green and magenta.
   float3 dotMaskWeights = lerp(
                           float3(1.0, mask, 1.0),
                           float3(mask, 1.0, mask),
                           floor(fmod(mod_factor, 2.0)));
if (shadowMask == 0) {
   res *= dotMaskWeights;
}
else {
   res *= Mask(floor(texCoord.xy*(texture_size.xy/video_size.xy)*output_size.xy)+float2(0.5,0.5));
}

      return float4(res, 1.0);
}

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