/*=============================================================================
	DistortApplyScreenPixelShader.usf: Pixel shader for rendering screen distortion pass
	Copyright © 2004-2006 Epic Games, Inc. All Rights Reserved.
=============================================================================*/

#include "Common.usf"

static const half DistortionScaleBias = 4.0f;

/** 
* contains accumulated distortion values as 
* R=positive horizontal offset
* G=positive vertical offset
* B=negative horizontal offset
* A=negative vertical offset 
*/
sampler	AccumulatedDistortionTexture;

/** distorts screen texture using accumulated distortion offsets */
void Main(
	in float2 TexCoord: TEXCOORD0,
	out float4 OutColor : COLOR0
	)
{
	half SceneDepth = CalcSceneDepth(TexCoord);
	// sample accumulated distortion and apply inverse scale
	half4 AccumDist = tex2D(AccumulatedDistortionTexture,TexCoord)/DistortionScaleBias;
	// offset = [R-B,G-A] and flip Y
	float2 DistOffset = float2(AccumDist.r - AccumDist.b, AccumDist.g - AccumDist.a) * float2(1,-1);	
	// sample screen using offset coords
	half3 DistColor = tex2D(SceneColorTexture, TexCoord + DistOffset).xyz;
	
	OutColor = RETURN_COLOR( half4(DistColor,SceneDepth) );
}


