/*============================================================================= 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; // width, height, 1/width, 1/height float4 InputTextureSize; /** 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 f stop. */ static half FStop = PackedParameters.b; /** The falloff exponent. */ static half FalloffExponent = PackedParameters.a; /** xy: clamp for min/max blur amount values - default would be [1,1], zw: 1/SourceImageExtent.xy */ float4 MinMaxBlurClamp; // .x : size // .y : rotation // .z : edge count float4 DOFKernelParams; // make these adjustable #define DepthOfFieldNearTransitionRegion (300.0f) #define DepthOfFieldFarTransitionRegion (500.0f) /** * 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) * 1/InverseFocusRadius), FStop ) ); } #define DepthOfFieldFocalLength (50.0f) float ComputeNearCoC(float Depth) { // Old dof formula (adapted to new ranges) if (FalloffExponent > 0.f) { return pow( saturate((Depth - FocusDistance) * -InverseFocusRadius), FalloffExponent ); } // New dof formula else { return saturate((FocusDistance - Depth) / max(DepthOfFieldNearTransitionRegion*FStop, 0.0001f)); } } float ComputeFarCoC(float Depth) { // Old dof formula (adapted to new ranges) if (FalloffExponent > 0.f) { return pow( saturate((Depth - FocusDistance) * InverseFocusRadius), FalloffExponent ); } // New dof formula else { return saturate((Depth - FocusDistance - InverseFocusRadius) / max(DepthOfFieldFarTransitionRegion*FStop, 0.0001f)); } } // ME3START - Rob Krajcarski - October 2, 2011 - Preport of MotionBlur improvements (see UberPostProcessEffect.cpp for more details) 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 } // ME3END