Files
UnrealEngineUWP/Engine/Shaders/Private/ScreenSpaceShadows.usf
tiago costa 7b6c125e5f Implemented cvars to override contact shadow parameters
- also fixing incorrect shadow length in standalone pass when it is specified in world space units

#rb kenzo.terelst

[CL 28317722 by tiago costa in ue5-main branch]
2023-09-28 10:22:32 -04:00

85 lines
2.7 KiB
Plaintext

// Copyright Epic Games, Inc. All Rights Reserved.
/*=============================================================================
ScreenSpaceShadows.usf
=============================================================================*/
#include "ScreenSpaceShadowRayCast.ush"
#include "Common.ush"
#include "SceneTexturesCommon.ush"
#include "ComputeShaderUtils.ush"
#ifdef UPSAMPLE_PASS
# include "ShadowFactorsUpsampleCommon.ush"
#endif
#ifndef THREADGROUP_SIZEX
# define THREADGROUP_SIZEX 1
#endif
#ifndef THREADGROUP_SIZEY
# define THREADGROUP_SIZEY 1
#endif
float3 LightDirection;
float ContactShadowLength;
uint bContactShadowLengthInWS;
float ContactShadowCastingIntensity;
uint DownsampleFactor;
int4 ScissorRectMinAndSize;
RWTexture2D<float2> RWShadowFactors;
[numthreads(THREADGROUP_SIZEX, THREADGROUP_SIZEY, 1)]
void ScreenSpaceShadowsCS(
uint3 GroupId : SV_GroupID,
uint3 DispatchThreadId : SV_DispatchThreadID,
uint3 GroupThreadId : SV_GroupThreadID)
{
uint ThreadIndex = GroupThreadId.y * THREADGROUP_SIZEX + GroupThreadId.x;
float2 ScreenUV = float2((DispatchThreadId.xy * DownsampleFactor + ScissorRectMinAndSize.xy + .5f) * View.BufferSizeAndInvSize.zw);
float2 ScreenPosition = (ScreenUV.xy - View.ScreenPositionScaleBias.wz) / View.ScreenPositionScaleBias.xy;
float SceneDepth = CalcSceneDepth(ScreenUV);
float3 OpaqueTranslatedWorldPosition = mul(float4(GetScreenPositionForProjectionType(ScreenPosition, SceneDepth), SceneDepth, 1), PrimaryView.ScreenToTranslatedWorld).xyz;
const float ContactShadowLengthScreenScale = GetTanHalfFieldOfView().y * SceneDepth;
const float ActualContactShadowLength = ContactShadowLength * (bContactShadowLengthInWS ? 1.0f : ContactShadowLengthScreenScale);
const float Dither = InterleavedGradientNoise(DispatchThreadId.xy + 0.5f, View.StateFrameIndexMod8);
float2 HitUV;
float HitDistance = CastScreenSpaceShadowRay(OpaqueTranslatedWorldPosition, LightDirection, ActualContactShadowLength, 8, Dither, 2.0f, false, HitUV);
float Result = HitDistance > 0.0 ? (1.0f - ContactShadowCastingIntensity) : 1.0f;
#if METAL_PROFILE
// clamp max depth to avoid #inf
SceneDepth = min(SceneDepth, 65500.0f);
#endif
RWShadowFactors[DispatchThreadId.xy] = float2(Result, SceneDepth);
}
#ifdef UPSAMPLE_PASS
Texture2D ShadowFactorsTexture;
SamplerState ShadowFactorsSampler;
float OneOverDownsampleFactor;
void ScreenSpaceShadowsUpsamplePS(
in float4 SVPos : SV_POSITION,
out float4 OutColor : SV_Target0)
{
float Output;
float SceneDepth;
UpsampleShadowFactors(SVPos, ScissorRectMinAndSize, OneOverDownsampleFactor, ShadowFactorsTexture, ShadowFactorsSampler, Output, SceneDepth);
OutColor = EncodeLightAttenuation(half4(Output, Output, Output, Output));
}
#endif // UPSAMPLE_PASS