/*============================================================================= MotionBlurPixelShader.usf: Blurs based on velocity vector. Copyright 1998-2007 Epic Games, Inc. All Rights Reserved. =============================================================================*/ #include "Common.usf" #ifndef NUM_SAMPLES #define NUM_SAMPLES 5 #endif /*============================================================================= * Vertex Shader *============================================================================*/ void MainVertexShader( float4 InPosition : POSITION, float2 InScreenUV : TEXCOORD0, out float4 OutScreenPosition : TEXCOORD0, out float4 OutPosition : POSITION ) { OutPosition = InPosition; // Target position OutScreenPosition = float4(InPosition.xy,InScreenUV); // Background UV and position } /*============================================================================= * Pixel Shader *============================================================================*/ float4 RenderTargetClampParameter; float2 ClampUV(float2 UV) { // SM2 currently doesn't support correctly clamped motion blur (not enough instructions) #if !SM2_PROFILE // Clamp the sampling UVs to avoid sampling from outside viewport region // (hence render target size can be bigger than viewport region) UV.x = clamp(UV.x, RenderTargetClampParameter.x, RenderTargetClampParameter.z); UV.y = clamp(UV.y, RenderTargetClampParameter.y, RenderTargetClampParameter.w); #endif return UV; } sampler2D VelocityBuffer; float4x4 ScreenToWorld; float4x4 PrevViewProjMatrix; float4 StaticVelocityParameters = { 0.5f, -0.5f, 16.0f/1280.0f, 16.0f/720.0f }; float4 DynamicVelocityParameters = { 2.0f*16.0f/1280.0f, -2.0f*16.0f/720.0f, -16.0f/1280.0f, 16.0f/720.0f }; void MainPixelShader( float4 ScreenPosition : TEXCOORD0, out float4 OutColor : COLOR0 ) { half2 PixelVelocity; half2 NominalVelocity; // NOTE: Maximum value for an fp16 depth-value is 65504. float2 ScreenUV = ScreenPosition.zw; half2 DynamicVelocity2D = tex2D( VelocityBuffer, ScreenUV ).xy; // 0.0f (-1.0f after scale-and-bias) in the velocity buffer has special meaning. It means "use static background velocity". // Velocity rendering clamps to 1/255, so every pixel it touches will fail this check. half Selector = DynamicVelocity2D.r; if ( Selector > 0.0f ) { // Scale dynamic velocity from [-1,+1] to [-MAX_VELOCITY,+MAX_VELOCITY] pixels NominalVelocity = DynamicVelocity2D * 2.0f - 1.0f; PixelVelocity = NominalVelocity*DynamicVelocityParameters.xy; } else { half CurrDepth = min( CalcSceneDepth( ScreenUV ), 65504); if ( CurrDepth < 14.0f ) CurrDepth = 65504.0f; float4 ProjectedPosition = float4( ScreenPosition.xy * CurrDepth, CurrDepth, 1 ); float4 PrevScreenPosition = MulMatrix( PrevViewProjMatrix, ProjectedPosition ); PrevScreenPosition.xy /= PrevScreenPosition.w; // Scale static velocity from projection space to texel space and clamp to +/- MAX_VELOCITY pixels. half2 StaticVelocity = ScreenPosition.xy - PrevScreenPosition.xy; PixelVelocity = clamp( StaticVelocity*StaticVelocityParameters.xy, -StaticVelocityParameters.zw, StaticVelocityParameters.zw ); NominalVelocity = PixelVelocity/StaticVelocityParameters.zw; } // Make it nicely curved. half Scale = dot(NominalVelocity, NominalVelocity); PixelVelocity *= Scale; // Average NUM_SAMPLES screen colors along the velocity vector. half2 Step = PixelVelocity / NUM_SAMPLES; ScreenUV -= 2*Step; half3 Color = tex2D( SceneColorTexture, ClampUV(ScreenUV) ).rgb; UNROLL for ( int i=1; i < NUM_SAMPLES; ++i ) { ScreenUV += Step; Color += tex2D( SceneColorTexture, ClampUV(ScreenUV) ).rgb; } OutColor = RETURN_COLOR( float4(Color/NUM_SAMPLES, 1) );// + float4(PrevColor,0) } void MainPixelShaderDynamicVelocitiesOnly( float4 ScreenPosition : TEXCOORD0, out float4 OutColor : COLOR0 ) { half2 ScreenUV = ScreenPosition.zw; half2 DynamicVelocity = tex2D( VelocityBuffer, ScreenUV ).xy; // 0.0f (-1.0f after scale-and-bias) in the velocity buffer has special meaning. It means "use static background velocity". // Velocity rendering clamps to 1/255, so every pixel it touches will fail this check. half Selector = DynamicVelocity.r; if ( Selector == 0.0f ) { // Scale velocity from [-1,+1] to [-MAX_VELOCITY,+MAX_VELOCITY] pixels half2 Velocity = DynamicVelocity * 2.0f - 1.0f; half Scale = dot(Velocity, Velocity); half2 PixelVelocity = Velocity*DynamicVelocityParameters.xy; PixelVelocity *= Scale; half2 Step = PixelVelocity / NUM_SAMPLES; ScreenUV -= 2*Step; half3 Color = tex2D( SceneColorTexture, ClampUV(ScreenUV) ).rgb; UNROLL for ( int i=1; i < NUM_SAMPLES; ++i ) { ScreenUV += Step; Color += tex2D( SceneColorTexture, ClampUV(ScreenUV) ).rgb; } OutColor = RETURN_COLOR( float4(Color/NUM_SAMPLES, 1) ); } else { float3 Color = tex2D( SceneColorTexture, ScreenUV ).rgb; OutColor = RETURN_COLOR( float4(Color, 1) ); } }