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

#include "Common.usf"

static const half InvDistortionScaleBias = 1 / 4.0f;

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

/**
 * Contains the valid region in the scenecolor texture (in UV space).
 * Trying to sample outside this region will yield garbage colors.
 * X,Y = Upper-left corner
 * Z,W = Lower-left corner
 */
float4		SceneColorRect;

/** distorts screen texture using accumulated distortion offsets */
void Main(
	in float2 TexCoord: TEXCOORD0,
	out float4 OutColor : COLOR0
	)
{
	// sample accumulated distortion and apply inverse scale
	half4 AccumDist = tex2D(AccumulatedDistortionTexture,TexCoord);
	// offset = [R-B,G-A] and flip Y
	half2 DistOffset = (AccumDist.rg - AccumDist.ba) * half2(InvDistortionScaleBias,-InvDistortionScaleBias);
	half2 NewTexCoord = TexCoord + DistOffset;

	// If we're about to sample outside the valid SceneColorRect, set to 0 distortion.
	if ( NewTexCoord.x < SceneColorRect.x || NewTexCoord.x > SceneColorRect.z ||
	     NewTexCoord.y < SceneColorRect.y || NewTexCoord.y > SceneColorRect.w )
	{
		NewTexCoord = TexCoord;
	}
	
#if SUPPORTS_DEPTH_TEXTURES   
	// sample screen using offset coords
	half3 DistColor = tex2D(SceneColorTexture, NewTexCoord).xyz;
	OutColor = half4(DistColor,0);
#else
	// sample screen using offset coords, distorting both scene color and depth
	half4 DistColor = CalcFullSceneColor(NewTexCoord);
	OutColor = RETURN_COLOR( DistColor );
#endif
}