/*=============================================================================
	DOFAndBloomCommon.usf: Code which is common to depth of field shaders.
	Copyright 1998-2010 Epic Games, Inc. All Rights Reserved.
=============================================================================*/

/** The packed parameters. */
half4 PackedParameters;

/** The distance which is 100% focused. */
static half FocusDistance = PackedParameters.r;

/** One over the minimum distance from the focal which is 0% focused. */
static half InverseFocusRadius = PackedParameters.g;

/** The focus falloff exponent. */
static half FocusExponent = PackedParameters.b;

/** xy: clamp for min/max blur amount values - default would be [1,1], zw: 1/SourceImageExtent.xy */
float4 MinMaxBlurClamp;

/**
 * Computes the unfocused percent for a scene depth.
 * @param SceneDepth - The scene depth.
 * @return A unfocused percent for the depth.
 */
half CalcUnfocusedPercent(float SceneDepth)
{
	// Blur amount based on ratio of distance to focus plane to the focus radius
	// it is clamped to [-1,1]. Note that FocusDistance can be negative if it 
	// falls behind the near plane.
	half RelativeDistance = SceneDepth - FocusDistance;
	half MaxUnfocusedPercent = RelativeDistance < 0 ? MinMaxBlurClamp.x : MinMaxBlurClamp.y;
	return min( MaxUnfocusedPercent, pow( saturate(abs(RelativeDistance) * InverseFocusRadius), FocusExponent ) );
}


const static float X360DepthRange = 15000.0f;

/** bring from DeviceZ to from texture usable range */
float CompressDeviceZIntoHalfResTextureChannel(float In)
{
#if XBOX
	// stored in 16bit int range -32..32, divided by the max expected depth (might not be enough)
	return ConvertFromDeviceZ(In) * 32.0f / X360DepthRange;
#else
	// stored in half
	return ConvertFromDeviceZ(In);
#endif
}

/** bring from texture usable range to worldspace range */
float DecompressDeviceZFromHalfResTextureChannel(float In)
{
#if XBOX
	return In * X360DepthRange / 32.0f;
#else
	return In;
#endif
}
