//--------------------------------------------------------------------------------------
// File: Pick.fx
//
// The effect file for the Pick sample.  
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4 g_MaterialAmbientColor:register(c10);      // Material's ambient color
float4 g_MaterialDiffuseColor:register(c11);      // Material's diffuse color
float3 g_LightDir;                  // Light's direction in world space
float4 g_LightDiffuse;              // Light's diffuse color

float    g_fTime;                   // App's time in seconds
float4x4 g_mWorld;                  // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix

texture g_MeshTexture;              // Color texture for mesh

float4 vGravity = float4(0,-2.0,0,0);
float Layer     = 0; // 0 to 1 for the level


//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler:register(s10) = 
sampler_state
{
    Texture = <g_MeshTexture>;
}
;


//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;   // vertex position 
    float4 Diffuse    : COLOR0;     // vertex diffuse color (note that COLOR0 is clamped from 0..1)
    float2 TextureUV  : TEXCOORD0;  // vertex texture coords 
};


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;
	float4 Pos = vPos;
	
	Pos.z -= cos( Pos.x/2.5);
	Pos.z -= cos( Pos.y/2.5);
	
	Output.Diffuse    = 0;
    Output.Position =vPos+ mul(Pos,g_mWorldViewProjection);
    Output.TextureUV = vTexCoord0; 
    
    return Output;    
}


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT FullScreenVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;

	Output.Diffuse    = 0;
    Output.Position = vPos;
	//Output.Position.z-=0.1f;
    Output.TextureUV = vTexCoord0; 
    
    return Output;    
}


//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};


//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In ) 
{ 
    PS_OUTPUT Output;

  
	Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV);

    return Output;
}

float2 g_TexelSize : TEXELSIZE;
float3  dt = half3(1,1,1);

PS_OUTPUT Scale4x( VS_OUTPUT In)
{
	PS_OUTPUT Output;

	// calculating offsets, coordinates

	float2 dx = float2(2*g_TexelSize.x, 0.0); 
	float2 dy = float2( 0.0,2*g_TexelSize.y);
	
	float2 pixcoord  = 0.5*In.TextureUV/g_TexelSize;
	float2 fp        = frac(pixcoord);
	float2 d11       = In.TextureUV-fp*g_TexelSize;


	// Reading the texels

	float3 B = tex2D(MeshTextureSampler,d11-dy).xyz;
	float3 D = tex2D(MeshTextureSampler,d11-dx).xyz;
	float3 E = tex2D(MeshTextureSampler,d11   ).xyz;
	float3 F = tex2D(MeshTextureSampler,d11+dx).xyz;
	float3 H = tex2D(MeshTextureSampler,d11+dy).xyz;
	
	// The Scale2x algorithm

	float3 E0 = D == B && B != H && D != F ? D : E;
	float3 E1 = B == F && B != H && D != F ? F : E;
	float3 E2 = D == H && B != H && D != F ? D : E;
	float3 E3 = H == F && B != H && D != F ? F : E;

	// Product distribution

	fp = 1-step(fp,0.5);

	Output.RGBColor = float4((E3*fp.x+E2*(1-fp.x))*fp.y+(E1*fp.x+E0*(1-fp.x))*(1-fp.y),1); 
	
	return Output;
}

const float3 dtt = float3(65536,255,1);

float reduce(float3 color)
{ 
	return dot(color,dtt);
}

int GET_RESULT(float A, float B, float C, float D)
{
	int x = 0; int y = 0; int r = 0;
	if (A == C) x+=1; else if (B == C) y+=1;
	if (A == D) x+=1; else if (B == D) y+=1;
	if (x <= 1) r+=1; 
	if (y <= 1) r-=1;
	return r;
} 

const float colors = 256.0; // number of colors the display can show

PS_OUTPUT SuperEagle( VS_OUTPUT In)
{
	PS_OUTPUT Output;
	
	float2 dx = float2(g_TexelSize.x, 0.0); 
	float2 dy = float2( 0.0,g_TexelSize.y);
	
	float2 pixcoord  = In.TextureUV/g_TexelSize;
	float2 fp        = frac(pixcoord);
	float2 d11       = In.TextureUV-fp*g_TexelSize;


	// Reading the texels

	float3 B = tex2D(MeshTextureSampler,d11-dy).xyz;
	float3 D = tex2D(MeshTextureSampler,d11-dx).xyz;
	float3 E = tex2D(MeshTextureSampler,d11   ).xyz;
	float3 F = tex2D(MeshTextureSampler,d11+dx).xyz;
	float3 H = tex2D(MeshTextureSampler,d11+dy).xyz;
	
	
	// The Scale2x algorithm

	float3 E0 = D == B && B != H && D != F ? D : E;
	float3 E1 = B == F && B != H && D != F ? F : E;
	float3 E2 = D == H && B != H && D != F ? D : E;
	float3 E3 = H == F && B != H && D != F ? F : E;


	// Product interpolation

	Output.RGBColor = float4((E3*fp.x+E2*(1-fp.x))*fp.y+(E1*fp.x+E0*(1-fp.x))*(1-fp.y),1); 
	return Output;
}


PS_OUTPUT AAFilter( VS_OUTPUT In)
{
	PS_OUTPUT Output;
		
	float dx = g_TexelSize.x*0.5;
	float dy = g_TexelSize.y*0.5;
	
	float2 C = In.TextureUV;
	float2 L = In.TextureUV + float2(-dx, 0);
	float2 R = In.TextureUV + float2( dx, 0);
	float2 U = In.TextureUV + float2( 0,-dy);
	float2 D = In.TextureUV + float2( 0, dy);
	
	float3 c11 = tex2D(MeshTextureSampler, C ).xyz;
	float3 c01 = tex2D(MeshTextureSampler, L ).xyz;
	float3 c21 = tex2D(MeshTextureSampler, R ).xyz;
	float3 c10 = tex2D(MeshTextureSampler, U ).xyz;
	float3 c12 = tex2D(MeshTextureSampler, D ).xyz;

	float k1 = dot(abs(c01-c21),dt)+0.0001;
	float k2 = dot(abs(c10-c12),dt)+0.0001;

	// Product interpolation

	Output.RGBColor = float4((k1*(c10+c12)+k2*(c01+c21))/(2.0*(k1+k2)),0);
	return Output;
}





float blurThreshold = 1.9f;  
float AntiAliasingAlpha(float alpha)  
{  
    float rho       = alpha;  
      
    float sat       = 0.8; // saturation  
    float invsat    = 1 - sat;  
          
    float a         = invsat * rho * sat;  
 
    // Uncomment the following line to remove edge blurring  
    // blurThreshold = 1;  
      
    if( rho < blurThreshold )  
    {  
        a = 1.0f;  
    }  
    else 
    {  
        float normrho   = (rho      - blurThreshold) * 1 / (1 - blurThreshold);  
        a               = cos(normrho  * 3.14159/2);  
    }  
      
    return a;  
};

PS_OUTPUT AAFilter2( VS_OUTPUT In)
{
	//In.TextureUV.y-=0.1f;

	PS_OUTPUT Output;
		
	float2 C = In.TextureUV;
	float2 oldPixel = { In.TextureUV.x * 1280.0F, In.TextureUV.y * 720.0F }; 
    float2 pixelCo = ceil(oldPixel); 
     
    float2 pos = {pixelCo.x % 2,pixelCo.y % 2}; 
             
    float2 offset_x = { g_TexelSize.x, 0 };    
    float2 offset_y = { 0, g_TexelSize.y };    
     // insted sample the arverige pixel corner into a float value
    float E = tex2D(MeshTextureSampler,  In.TextureUV); 
    float D = tex2D(MeshTextureSampler,  In.TextureUV-offset_x); 
    float F = tex2D(MeshTextureSampler,  In.TextureUV+offset_x); 
    float B = tex2D(MeshTextureSampler,  In.TextureUV-offset_y); 
    float H = tex2D(MeshTextureSampler,  In.TextureUV+offset_y); 
     
    float E0 = E;  
    float E1 = E;  
    float E2 = E;  
    float E3 = E;  
     
    if (B != H && D != F)  
    { 
        E0 = D == B ? D : E; 
        E1 = B == F ? F : E; 
        E2 = D == H ? D : E; 
        E3 = H == F ? F : E; 
    }  
     
    float4 ret; 
     
    if(pos.x == 1) 
    { 
        if(pos.y == 1) 
        { 
            ret = E0;    
        } else { 
            ret = E1; 
        } 
    } else { 
        if(pos.y == 1) 
        { 
            ret = E2; 
        } else { 
            ret = E3; 
        }        
    } 
    // out put gray antialias     this will allso enhance the scene detail so your output will be more detail like normal mapping with out using normals
    // you total eleminate normal mapping and only use deffuse mapping , course this is a mix of black and withe * monocrome texture output with antialias x2
    // i does simliar thing in my own render pipeline in order to save space on the hardisk and you doint have to create all does texture maps,, speed up things
    //return ret; 

    float4 finalColor = tex2D(MeshTextureSampler,In.TextureUV).rgba;
        
            // ajust the value to get the right color blance
            //finalColor        = finalColor + ret * (ret / 1.3);
            finalColor.a     = AntiAliasingAlpha(finalColor.a);

	// Product interpolation

	Output.RGBColor = finalColor;
	return Output;
}

//--------------------------------------------------------------------------------------
// Renders Background 
//--------------------------------------------------------------------------------------
technique RenderFullScreen
{
    pass P0
    {          
		VertexShader = compile vs_2_0 FullScreenVS();
        PixelShader  = compile ps_2_0 RenderScenePS(); 
    }
}

//--------------------------------------------------------------------------------------
// Renders scene 
//--------------------------------------------------------------------------------------
technique RenderModel
{
    pass P0
    {          
		VertexShader = compile vs_2_0 RenderSceneVS();
        PixelShader  = compile ps_2_0 AAFilter2(); 
    }
}
