2020-06-23 18:40:00 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
/*=============================================================================
LightShadowShaderParameters . h
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
# include "VolumeLighting.h"
# include "SceneView.h"
# include "SceneRendering.h"
# include "LightSceneInfo.h"
# include "ShadowRendering.h"
# include "Components/LightComponent.h"
# include "Engine/MapBuildDataRegistry.h"
IMPLEMENT_GLOBAL_SHADER_PARAMETER_STRUCT ( FVolumeShadowingShaderParametersGlobal0 , " Light0Shadow " ) ;
IMPLEMENT_GLOBAL_SHADER_PARAMETER_STRUCT ( FVolumeShadowingShaderParametersGlobal1 , " Light1Shadow " ) ;
2021-03-08 23:14:54 -04:00
const FProjectedShadowInfo * GetFirstWholeSceneShadowMap ( const FVisibleLightInfo & VisibleLightInfo )
2020-06-23 18:40:00 -04:00
{
2021-03-08 23:14:54 -04:00
for ( int32 ShadowIndex = 0 ; ShadowIndex < VisibleLightInfo . ShadowsToProject . Num ( ) ; ShadowIndex + + )
2020-06-23 18:40:00 -04:00
{
2021-03-08 23:14:54 -04:00
const FProjectedShadowInfo * ProjectedShadowInfo = VisibleLightInfo . ShadowsToProject [ ShadowIndex ] ;
2020-06-23 18:40:00 -04:00
2021-03-08 23:14:54 -04:00
if ( ProjectedShadowInfo - > bAllocated
& & ProjectedShadowInfo - > bWholeSceneShadow
& & ! ProjectedShadowInfo - > bRayTracedDistanceField )
2020-06-23 18:40:00 -04:00
{
return ProjectedShadowInfo ;
}
}
2020-08-31 19:38:04 -04:00
return nullptr ;
2020-06-23 18:40:00 -04:00
}
2021-04-02 20:11:25 -04:00
static auto SetVolumeShadowingDefaultShaderParametersGlobal = [ ] ( FRDGBuilder & GraphBuilder , auto & ShaderParams )
2020-06-23 18:40:00 -04:00
{
2021-04-02 20:11:25 -04:00
const FRDGSystemTextures & SystemTextures = FRDGSystemTextures : : Get ( GraphBuilder ) ;
2021-05-25 17:12:22 -04:00
FRDGTextureRef BlackDepthCubeTexture = SystemTextures . BlackDepthCube ;
2021-04-02 20:11:25 -04:00
2020-06-23 18:40:00 -04:00
ShaderParams . WorldToShadowMatrix = FMatrix : : Identity ;
2021-09-22 10:01:48 -04:00
ShaderParams . ShadowmapMinMax = FVector4f ( 1.0f ) ;
ShaderParams . DepthBiasParameters = FVector4f ( 1.0f ) ;
ShaderParams . ShadowInjectParams = FVector4f ( 1.0f ) ;
2020-06-23 18:40:00 -04:00
memset ( ShaderParams . ClippingPlanes . GetData ( ) , 0 , sizeof ( ShaderParams . ClippingPlanes ) ) ;
ShaderParams . bStaticallyShadowed = 0 ;
ShaderParams . WorldToStaticShadowMatrix = FMatrix : : Identity ;
2021-09-22 10:01:48 -04:00
ShaderParams . StaticShadowBufferSize = FVector4f ( 1.0f ) ;
2021-04-02 20:11:25 -04:00
ShaderParams . ShadowDepthTexture = SystemTextures . White ;
2020-06-23 18:40:00 -04:00
ShaderParams . StaticShadowDepthTexture = GWhiteTexture - > TextureRHI ;
ShaderParams . ShadowDepthTextureSampler = TStaticSamplerState < SF_Point , AM_Clamp , AM_Clamp , AM_Clamp > : : GetRHI ( ) ;
ShaderParams . StaticShadowDepthTextureSampler = TStaticSamplerState < SF_Bilinear , AM_Clamp , AM_Clamp , AM_Clamp > : : GetRHI ( ) ;
memset ( ShaderParams . OnePassPointShadowProjection . ShadowViewProjectionMatrices . GetData ( ) , 0 , sizeof ( ShaderParams . OnePassPointShadowProjection . ShadowViewProjectionMatrices ) ) ;
ShaderParams . OnePassPointShadowProjection . InvShadowmapResolution = 1.0f ;
2021-04-02 20:11:25 -04:00
ShaderParams . OnePassPointShadowProjection . ShadowDepthCubeTexture = BlackDepthCubeTexture ;
ShaderParams . OnePassPointShadowProjection . ShadowDepthCubeTexture2 = BlackDepthCubeTexture ;
2020-06-23 18:40:00 -04:00
ShaderParams . OnePassPointShadowProjection . ShadowDepthCubeTextureSampler = TStaticSamplerState < SF_Bilinear , AM_Clamp , AM_Clamp , AM_Clamp , 0 , 0 , 0 , SCF_Less > : : GetRHI ( ) ;
} ;
static auto GetVolumeShadowingShaderParametersGlobal = [ ] (
2021-04-02 20:11:25 -04:00
FRDGBuilder & GraphBuilder ,
2020-06-23 18:40:00 -04:00
auto & ShaderParams ,
const FViewInfo & View ,
const FLightSceneInfo * LightSceneInfo ,
const FProjectedShadowInfo * ShadowInfo ,
int32 InnerSplitIndex )
{
const bool bDynamicallyShadowed = ShadowInfo ! = NULL ;
if ( bDynamicallyShadowed )
{
2021-09-22 10:01:48 -04:00
FVector4f ShadowmapMinMaxValue ;
2020-06-23 18:40:00 -04:00
ShaderParams . WorldToShadowMatrix = ShadowInfo - > GetWorldToShadowMatrix ( ShaderParams . ShadowmapMinMax ) ;
}
2021-08-18 16:28:23 -04:00
else
{
ShaderParams . WorldToShadowMatrix = FMatrix : : Identity ;
2021-09-22 10:01:48 -04:00
ShaderParams . ShadowmapMinMax = FVector4f ( 1.0f ) ;
2021-08-18 16:28:23 -04:00
}
2020-06-23 18:40:00 -04:00
// default to ignore the plane
2021-09-22 10:01:48 -04:00
FVector4f Planes [ 2 ] = { FVector4f ( 0 , 0 , 0 , - 1 ) , FVector4f ( 0 , 0 , 0 , - 1 ) } ;
2020-06-23 18:40:00 -04:00
// .zw:DistanceFadeMAD to use MAD for efficiency in the shader, default to ignore the plane
2021-09-22 10:01:48 -04:00
FVector4f ShadowInjectParamValue ( 1 , 1 , 0 , 0 ) ;
2020-06-23 18:40:00 -04:00
if ( InnerSplitIndex ! = INDEX_NONE )
{
FShadowCascadeSettings ShadowCascadeSettings ;
LightSceneInfo - > Proxy - > GetShadowSplitBounds ( View , InnerSplitIndex , LightSceneInfo - > IsPrecomputedLightingValid ( ) , & ShadowCascadeSettings ) ;
ensureMsgf ( ShadowCascadeSettings . ShadowSplitIndex ! = INDEX_NONE , TEXT ( " FLightSceneProxy::GetShadowSplitBounds did not return an initialized ShadowCascadeSettings " ) ) ;
// near cascade plane
{
ShadowInjectParamValue . X = ShadowCascadeSettings . SplitNearFadeRegion = = 0 ? 1.0f : 1.0f / ShadowCascadeSettings . SplitNearFadeRegion ;
2021-09-22 10:01:48 -04:00
Planes [ 0 ] = FVector4f ( ( FVector ) ( ShadowCascadeSettings . NearFrustumPlane ) , - ShadowCascadeSettings . NearFrustumPlane . W ) ;
2020-06-23 18:40:00 -04:00
}
uint32 CascadeCount = LightSceneInfo - > Proxy - > GetNumViewDependentWholeSceneShadows ( View , LightSceneInfo - > IsPrecomputedLightingValid ( ) ) ;
// far cascade plane
if ( InnerSplitIndex ! = CascadeCount - 1 )
{
ShadowInjectParamValue . Y = 1.0f / ( ShadowCascadeSettings . SplitFarFadeRegion = = 0.0f ? 0.0001f : ShadowCascadeSettings . SplitFarFadeRegion ) ;
2021-09-22 10:01:48 -04:00
Planes [ 1 ] = FVector4f ( ( FVector ) ( ShadowCascadeSettings . FarFrustumPlane ) , - ShadowCascadeSettings . FarFrustumPlane . W ) ;
2020-06-23 18:40:00 -04:00
}
const FVector2D FadeParams = LightSceneInfo - > Proxy - > GetDirectionalLightDistanceFadeParameters ( View . GetFeatureLevel ( ) , LightSceneInfo - > IsPrecomputedLightingValid ( ) , View . MaxShadowCascades ) ;
// setup constants for the MAD in shader
ShadowInjectParamValue . Z = FadeParams . Y ;
ShadowInjectParamValue . W = - FadeParams . X * FadeParams . Y ;
}
ShaderParams . ShadowInjectParams = ShadowInjectParamValue ;
ShaderParams . ClippingPlanes [ 0 ] = Planes [ 0 ] ;
ShaderParams . ClippingPlanes [ 1 ] = Planes [ 1 ] ;
2021-04-02 20:11:25 -04:00
const FRDGSystemTextures & SystemTextures = FRDGSystemTextures : : Get ( GraphBuilder ) ;
2020-06-23 18:40:00 -04:00
ELightComponentType LightType = ( ELightComponentType ) LightSceneInfo - > Proxy - > GetLightType ( ) ;
2021-04-02 20:11:25 -04:00
FRDGTexture * ShadowDepthTextureResource = nullptr ;
2020-06-23 18:40:00 -04:00
if ( bDynamicallyShadowed )
{
2021-09-22 10:01:48 -04:00
ShaderParams . DepthBiasParameters = FVector4f ( ShadowInfo - > GetShaderDepthBias ( ) , ShadowInfo - > GetShaderSlopeDepthBias ( ) , ShadowInfo - > GetShaderMaxSlopeDepthBias ( ) , 1.0f / ( ShadowInfo - > MaxSubjectZ - ShadowInfo - > MinSubjectZ ) ) ;
2020-06-23 18:40:00 -04:00
if ( LightType = = LightType_Point | | LightType = = LightType_Rect )
{
2021-04-02 20:11:25 -04:00
ShadowDepthTextureResource = SystemTextures . Black ;
2020-06-23 18:40:00 -04:00
}
else
{
2021-04-02 20:11:25 -04:00
ShadowDepthTextureResource = GraphBuilder . RegisterExternalTexture ( ShadowInfo - > RenderTargets . DepthTarget ) ;
2020-06-23 18:40:00 -04:00
}
}
else
{
2021-04-02 20:11:25 -04:00
ShadowDepthTextureResource = SystemTextures . Black ;
2021-08-18 16:28:23 -04:00
ShaderParams . DepthBiasParameters = FVector ( 1.0f ) ;
2020-06-23 18:40:00 -04:00
}
check ( ShadowDepthTextureResource )
ShaderParams . ShadowDepthTexture = ShadowDepthTextureResource ;
ShaderParams . ShadowDepthTextureSampler = TStaticSamplerState < SF_Point , AM_Clamp , AM_Clamp , AM_Clamp > : : GetRHI ( ) ;
const FStaticShadowDepthMap * StaticShadowDepthMap = LightSceneInfo - > Proxy - > GetStaticShadowDepthMap ( ) ;
const uint32 bStaticallyShadowedValue = LightSceneInfo - > IsPrecomputedLightingValid ( ) & & StaticShadowDepthMap & & StaticShadowDepthMap - > Data & & StaticShadowDepthMap - > TextureRHI ? 1 : 0 ;
FRHITexture * StaticShadowDepthMapTexture = bStaticallyShadowedValue ? StaticShadowDepthMap - > TextureRHI : GWhiteTexture - > TextureRHI ;
const FMatrix WorldToStaticShadow = bStaticallyShadowedValue ? StaticShadowDepthMap - > Data - > WorldToLight : FMatrix : : Identity ;
2021-09-22 10:01:48 -04:00
const FVector4f StaticShadowBufferSizeValue = bStaticallyShadowedValue ? FVector4f ( StaticShadowDepthMap - > Data - > ShadowMapSizeX , StaticShadowDepthMap - > Data - > ShadowMapSizeY , 1.0f / StaticShadowDepthMap - > Data - > ShadowMapSizeX , 1.0f / StaticShadowDepthMap - > Data - > ShadowMapSizeY ) : FVector4f ( 0 , 0 , 0 , 0 ) ;
2020-06-23 18:40:00 -04:00
ShaderParams . bStaticallyShadowed = bStaticallyShadowedValue ;
ShaderParams . StaticShadowDepthTexture = StaticShadowDepthMapTexture ;
ShaderParams . StaticShadowDepthTextureSampler = TStaticSamplerState < SF_Bilinear , AM_Clamp , AM_Clamp , AM_Clamp > : : GetRHI ( ) ;
ShaderParams . WorldToStaticShadowMatrix = WorldToStaticShadow ;
ShaderParams . StaticShadowBufferSize = StaticShadowBufferSizeValue ;
//
// See FOnePassPointShadowProjectionShaderParameters from ShadowRendering.h
//
2021-04-02 20:11:25 -04:00
FRDGTexture * ShadowDepthTextureValue = ShadowInfo
2021-05-25 17:12:22 -04:00
? GraphBuilder . RegisterExternalTexture ( ShadowInfo - > RenderTargets . DepthTarget )
: SystemTextures . BlackDepthCube ;
2021-04-02 20:11:25 -04:00
2020-08-11 01:36:57 -04:00
ShaderParams . OnePassPointShadowProjection . ShadowDepthCubeTexture = ShadowDepthTextureValue ;
ShaderParams . OnePassPointShadowProjection . ShadowDepthCubeTexture2 = ShadowDepthTextureValue ;
ShaderParams . OnePassPointShadowProjection . ShadowDepthCubeTextureSampler = TStaticSamplerState < SF_Bilinear , AM_Clamp , AM_Clamp , AM_Clamp , 0 , 0 , 0 , SCF_Less > : : GetRHI ( ) ;
2020-06-23 18:40:00 -04:00
if ( bDynamicallyShadowed )
{
if ( ShadowInfo )
{
2021-09-22 10:01:48 -04:00
TArray < FMatrix44f > SIOnePassShadowViewProjectionMatrices = LWC : : ConvertArrayType < FMatrix44f > ( ShadowInfo - > OnePassShadowViewProjectionMatrices ) ; // LWC_TODO: Precision loss. Perf pessimization
memcpy ( ShaderParams . OnePassPointShadowProjection . ShadowViewProjectionMatrices . GetData ( ) , SIOnePassShadowViewProjectionMatrices . GetData ( ) , SIOnePassShadowViewProjectionMatrices . Num ( ) * sizeof ( FMatrix44f ) ) ;
2020-06-23 18:40:00 -04:00
ShaderParams . OnePassPointShadowProjection . InvShadowmapResolution = 1.0f / float ( ShadowInfo - > ResolutionX ) ;
}
else
{
memset ( ShaderParams . OnePassPointShadowProjection . ShadowViewProjectionMatrices . GetData ( ) , 0 , sizeof ( ShaderParams . OnePassPointShadowProjection . ShadowViewProjectionMatrices ) ) ;
ShaderParams . OnePassPointShadowProjection . InvShadowmapResolution = 0.0f ;
}
}
} ;
void SetVolumeShadowingShaderParameters (
2021-04-02 20:11:25 -04:00
FRDGBuilder & GraphBuilder ,
2020-06-23 18:40:00 -04:00
FVolumeShadowingShaderParametersGlobal0 & ShaderParams ,
const FViewInfo & View ,
const FLightSceneInfo * LightSceneInfo ,
const FProjectedShadowInfo * ShadowInfo ,
int32 InnerSplitIndex )
{
FLightShaderParameters LightParameters ;
LightSceneInfo - > Proxy - > GetLightShaderParameters ( LightParameters ) ;
ShaderParams . Position = LightParameters . Position ;
ShaderParams . InvRadius = LightParameters . InvRadius ;
GetVolumeShadowingShaderParametersGlobal (
2021-04-02 20:11:25 -04:00
GraphBuilder ,
2020-06-23 18:40:00 -04:00
ShaderParams . VolumeShadowingShaderParameters ,
View ,
LightSceneInfo ,
ShadowInfo ,
InnerSplitIndex ) ;
}
void SetVolumeShadowingShaderParameters (
2021-04-02 20:11:25 -04:00
FRDGBuilder & GraphBuilder ,
2020-06-23 18:40:00 -04:00
FVolumeShadowingShaderParametersGlobal1 & ShaderParams ,
const FViewInfo & View ,
const FLightSceneInfo * LightSceneInfo ,
const FProjectedShadowInfo * ShadowInfo ,
int32 InnerSplitIndex )
{
FLightShaderParameters LightParameters ;
LightSceneInfo - > Proxy - > GetLightShaderParameters ( LightParameters ) ;
ShaderParams . Position = LightParameters . Position ;
ShaderParams . InvRadius = LightParameters . InvRadius ;
GetVolumeShadowingShaderParametersGlobal (
2021-04-02 20:11:25 -04:00
GraphBuilder ,
2020-06-23 18:40:00 -04:00
ShaderParams . VolumeShadowingShaderParameters ,
View ,
LightSceneInfo ,
ShadowInfo ,
InnerSplitIndex ) ;
}
2021-04-02 20:11:25 -04:00
void SetVolumeShadowingDefaultShaderParameters ( FRDGBuilder & GraphBuilder , FVolumeShadowingShaderParametersGlobal0 & ShaderParams )
2020-06-23 18:40:00 -04:00
{
ShaderParams . Position = FVector ( 1.0f ) ;
ShaderParams . InvRadius = 1.0f ;
2021-04-02 20:11:25 -04:00
SetVolumeShadowingDefaultShaderParametersGlobal ( GraphBuilder , ShaderParams . VolumeShadowingShaderParameters ) ;
2020-06-23 18:40:00 -04:00
}
2021-04-02 20:11:25 -04:00
void SetVolumeShadowingDefaultShaderParameters ( FRDGBuilder & GraphBuilder , FVolumeShadowingShaderParametersGlobal1 & ShaderParams )
2020-06-23 18:40:00 -04:00
{
ShaderParams . Position = FVector ( 1.0f ) ;
ShaderParams . InvRadius = 1.0f ;
2021-04-02 20:11:25 -04:00
SetVolumeShadowingDefaultShaderParametersGlobal ( GraphBuilder , ShaderParams . VolumeShadowingShaderParameters ) ;
2020-06-23 18:40:00 -04:00
}