string Description = "This shader pixilates the scene by the specified amount";
string Thumbnail = "Pixilate.png";

float2 ViewSize : ViewSize;

//box filter, declare in pixel offsets convert to texel offsets in PS
float2 PixelOffsets[9] =
{
    { -1,  -1 },
    { -1,  0  },
    { -1,  1  },
    { 0,   1  },
    { 1,   1  },
    { 1,   0  },
    { 1,   -1 },
    { 0,   -1 },
    { 0,   0  },
};

float AmountOfPixels
<
	string SasUIControl = "slider";
	float SasUIMax = 1.0;
	float SasUIMin = 0.05;
	float SasUIStep = 0.01;
> = 0.100000;

//scene image
texture frame : RENDERCOLORTARGET
< 
	string ResourceName = "";
	float2 ViewportRatio = { 1.0, 1.0 };
>;

sampler2D frameSamp = sampler_state {
    Texture = < frame >;
    MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
    AddressU = Clamp; AddressV = Clamp;
};

struct input 
{
	float4 pos : POSITION;
	float2 uv : TEXCOORD0;
};
 
struct output {

	float4 pos: POSITION;
	float2 uv: TEXCOORD0;

};

output VS( input IN ) 
{
	output OUT;

	//needs to be shifted by half a pixel.
    //Go here for more info: http://www.sjbrown.co.uk/?article=directx_texels
    
	float4 oPos = float4( IN.pos.xy + float2( -1.0f/ViewSize.x, 1.0f/ViewSize.y ),0.0,1.0 );
	OUT.pos = oPos;

	float2 uv = (IN.pos.xy + 1.0) / 2.0;
	uv.y = 1 - uv.y; 
	OUT.uv = uv;
	
	return OUT;	
}

//copy the low res downsample into a high res screen quad using bilinear filtering
float4 PSPresent( output IN, uniform sampler2D srcTex ) : COLOR
{
	//scale up the uv co-ordinate, round it to an integer value then divide it back to the 0-1 range
	float2 scale = ViewSize*AmountOfPixels;
	float2 newUV = round(IN.uv*scale)/scale;
    return tex2D(srcTex,newUV);
}
  
technique Pixilate
<
	//specify where the original scene should be drawn
	string RenderColorTarget = "frame";
>
{
	//single pass to modify the image and output it to the screen
	pass Present
	<
		string RenderColorTarget = "";
	>
	{
		VertexShader = compile vs_1_1 VS();
		PixelShader = compile ps_2_0 PSPresent( frameSamp );
	}
}
