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

//    Pixellate Shader
//    Copyright (c) 2011, 2012 Fes
//    Permission to use, copy, modify, and/or distribute this software for any
//    purpose with or without fee is hereby granted, provided that the above
//    copyright notice and this permission notice appear in all copies.
//    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
//    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
//    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
//    SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
//   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
//    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
//    IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//    (Fes gave their permission to have this shader distributed under this
//    licence in this forum post:
//        http://board.byuu.org/viewtopic.php?p=57295#p57295

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

float4 pixellate(float2 texture_size, float2 video_size, float2 output_size, float2 texCoord, COMPAT_Texture2D(decal))
{
   float2 texelSize = 1.0 / texture_size;

   float2 range = float2(abs(video_size.x / (output_size.x * texture_size.x)), abs(video_size.y / (output_size.y * texture_size.y)));
   range = range / 2.0 * 0.999;

   float left   = texCoord.x - range.x;
   float top    = texCoord.y + range.y;
   float right  = texCoord.x + range.x;
   float bottom = texCoord.y - range.y;

   float3 topLeftColor     = COMPAT_SamplePoint(decal, (floor(float2(left, top)     / texelSize) + 0.5) * texelSize).rgb;
   float3 bottomRightColor = COMPAT_SamplePoint(decal, (floor(float2(right, bottom) / texelSize) + 0.5) * texelSize).rgb;
   float3 bottomLeftColor  = COMPAT_SamplePoint(decal, (floor(float2(left, bottom)  / texelSize) + 0.5) * texelSize).rgb;
   float3 topRightColor    = COMPAT_SamplePoint(decal, (floor(float2(right, top)    / texelSize) + 0.5) * texelSize).rgb;

   float2 border = clamp(round(texCoord / texelSize) * texelSize, float2(left, bottom), float2(right, top));

   float totalArea = 4.0 * range.x * range.y;

   float3 averageColor;
   averageColor  = ((border.x - left)  * (top - border.y)    / totalArea) * topLeftColor;
   averageColor += ((right - border.x) * (border.y - bottom) / totalArea) * bottomRightColor;
   averageColor += ((border.x - left)  * (border.y - bottom) / totalArea) * bottomLeftColor;
   averageColor += ((right - border.x) * (top - border.y)    / totalArea) * topRightColor;

   return float4(averageColor, 1.0);
}

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