Files
UnrealEngineUWP/Engine/Shaders/Private/Strata/StrataLightingCommon.ush
eric mcdaniel 21d52344ab Remove unused rectangular light texture reference
- issue
  - IntegrateLight() in RectLightIntegrate.ush takes a rect texture but the result from SampleSourceTexture() is never consumed
    - this is causing issues with non-optimized shaders on a certain platform as the compiler fails to eliminate the texture reference
    - this was introduced way back in 2018 via an integration with CL 4358751

- fix
  - remove the unused SampleSourceTexture() call from IntegrateLight()
    - also remove rect texture parameter IntegrateLight() as its now unreferenced and update the callsites
  - eliminate rect texture declarations that are no longer referenced and move the ones that are still used into the referencing preprocess branches that consumes them to give the compiler a little help eliminated them when not used

- testing
  - EngineTest cook on Win64 and console platforms split between local and preflight

#rb Benjamin.Rouveyrol
#jira none, fallout from UDN investigation
#preflight 628807ec7e018d20dc17727f

[CL 20371469 by eric mcdaniel in ue5-main branch]
2022-05-25 17:07:18 -04:00

255 lines
11 KiB
Plaintext

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#ifdef USE_STRATA_FORWARD_LIGHTING_COMMON
// Common function for forward lighting per pixel using light data
float3 StrataForwardLightingCommon(
float Dither,
FStrataIntegrationSettings Settings,
FDeferredLightData LightData,
float3 ToLight,
float LightMask,
float4 LightAttenuation,
FRectTexture RectTexture,
uint LightChannelMask,
uint PrimitiveLightingChannelMask,
float3 TranslatedWorldPosition,
float SceneDepth,
float3 BSDFColoredVisibility,
FStrataPixelHeader StrataPixelHeader,
FStrataBSDFContext StrataBSDFContext,
inout bool bEvaluateHasBeenExecuted,
inout FStrataEvaluateResult BSDFEvaluate)
{
bEvaluateHasBeenExecuted = false;
float3 Color = 0.0;
if (LightMask > 0.0)
{
// Evaluate the ShadowTerm that can then be used when integrating the lighting
FShadowTerms ShadowTerms = { StrataGetAO(StrataPixelHeader), 1.0, 1.0, InitHairTransmittanceData() };
float4 PrecomputedShadowFactors = HasZeroPrecShadowMask(StrataPixelHeader) ? 0.0f : 1.0f;
#if TRANSLUCENCY_LIGHTING_SURFACE_FORWARDSHADING
PrecomputedShadowFactors.x = ComputeDirectionalLightStaticShadowing(TranslatedWorldPosition).x;
#else
PrecomputedShadowFactors.x = 1;
#endif
const uint FakeShadingModelID = 0;
const float FakeContactShadowOpacity = 1.0f;
GetShadowTerms(SceneDepth, PrecomputedShadowFactors, FakeShadingModelID, FakeContactShadowOpacity,
LightData, TranslatedWorldPosition, ToLight, LightAttenuation, Dither, ShadowTerms);
float Roughness = StrataGetBSDFRoughness(StrataBSDFContext.BSDF);
FAreaLightIntegrateContext AreaLightContext = InitAreaLightIntegrateContext();
BRANCH
if (ShadowTerms.SurfaceShadow + ShadowTerms.TransmissionShadow > 0)
{
BSDFEvaluate = (FStrataEvaluateResult)0;
#if NON_DIRECTIONAL_DIRECT_LIGHTING
float Lighting;
if (LightData.bRectLight)
{
FRect Rect = GetRect(ToLight, LightData);
if (!IsRectVisible(Rect))
{
LightMask = 0.0f; // Rect light can be non visible due to barn door occlusion
}
AreaLightContext = CreateRectIntegrateContext(Roughness, StrataBSDFContext.N, StrataBSDFContext.V, Rect, RectTexture);
Lighting = IntegrateLight(Rect);
// We must have the evaluate inside the if due to the rectlight texture: it must be now be ambiguous which texture is going ot be used.
// After te compilation, a local resource must map to a unique global resource (the default or the actual rect light texture).
BSDFEvaluate = StrataIntegrateBSDF(StrataBSDFContext, ShadowTerms, AreaLightContext, Settings);
bEvaluateHasBeenExecuted = true;
}
else
{
FCapsuleLight Capsule = GetCapsule(ToLight, LightData);
AreaLightContext = CreateCapsuleIntegrateContext(Roughness, StrataBSDFContext.N, StrataBSDFContext.V, Capsule, LightData.bInverseSquared);
Lighting = IntegrateLight(Capsule, LightData.bInverseSquared);
BSDFEvaluate = StrataIntegrateBSDF(StrataBSDFContext, ShadowTerms, AreaLightContext, Settings);
bEvaluateHasBeenExecuted = true;
}
FLATTEN
if (LightChannelMask & PrimitiveLightingChannelMask)
{
float3 DiffuseLuminance = Diffuse_Lambert(BSDFEvaluate.DiffuseColor) * Lighting;
const float3 LightCommonMultiplier = LightData.Color * LightMask;
Color += DiffuseLuminance * LightCommonMultiplier * BSDFColoredVisibility;
}
#else
if (LightData.bRectLight)
{
FRect Rect = GetRect(ToLight, LightData);
if (!IsRectVisible(Rect))
{
LightMask = 0.0f; // Rect light can be non visible due to barn door occlusion
}
AreaLightContext = CreateRectIntegrateContext(Roughness, StrataBSDFContext.N, StrataBSDFContext.V, Rect, RectTexture);
// We must have the evaluate inside the if due to the rectlight texture: it must be now be ambiguous which texture is going ot be used.
// After te compilation, a local resource must map to a unique global resource (the default or the actual rect light texture).
BSDFEvaluate = StrataIntegrateBSDF(StrataBSDFContext, ShadowTerms, AreaLightContext, Settings);
bEvaluateHasBeenExecuted = true;
}
else
{
FCapsuleLight Capsule = GetCapsule(ToLight, LightData);
AreaLightContext = CreateCapsuleIntegrateContext(Roughness, StrataBSDFContext.N, StrataBSDFContext.V, Capsule, LightData.bInverseSquared);
BSDFEvaluate = StrataIntegrateBSDF(StrataBSDFContext, ShadowTerms, AreaLightContext, Settings);
bEvaluateHasBeenExecuted = true;
}
FLATTEN
if (LightChannelMask & PrimitiveLightingChannelMask)
{
float3 DiffuseLuminance = BSDFEvaluate.IntegratedDiffuseValue;
float3 SpecularLuminance = BSDFEvaluate.IntegratedSpecularValue * LightData.SpecularScale;
const float3 LightCommonMultiplier = LightData.Color * LightMask;
Color += (DiffuseLuminance + SpecularLuminance) * LightCommonMultiplier * BSDFColoredVisibility;
}
#endif
}
}
return Color;
}
#endif // USE_STRATA_FORWARD_LIGHTING_COMMON
#ifdef USE_STRATA_ENV_LIGHTING_COMMON
void StrataEnvLightingCommon(
in FStrataEnvLightResult StrataEnvLight,
in FStrataPixelHeader StrataPixelHeader,
in FStrataBSDFContext StrataBSDFContext,
in FStrataBSDF BSDF,
in float3 BentNormal,
in float3 BSDFThroughput,
in uint CaptureDataStartIndex,
in uint NumCulledReflectionCaptures,
in float ScreenAmbientOcclusion,
in float CloudVolumetricAOShadow,
in float TopLayerSpecularContributionFactor,
in float3 TranslatedWorldPosition,
in float CombinedScreenAndMaterialAO,
inout float SSRReductionFactor,
inout float3 DiffuseLighting,
inout float3 SpecularLighting)
{
// Diffuse component
DiffuseLighting = 0;
#if ENABLE_DYNAMIC_SKY_LIGHT
const bool bProcessFrontFaceDiffuse = any(StrataEnvLight.DiffuseWeight > 0.0f);
const bool bProcessBackFaceDiffuse = any(StrataEnvLight.DiffuseBackFaceWeight > 0.0f);
if (bProcessFrontFaceDiffuse || bProcessBackFaceDiffuse)
{
// Compute the common sky visibility factors
FSkyLightVisibilityData SkyVisData = GetSkyLightVisibilityData(StrataBSDFContext.N, StrataBSDFContext.N, StrataGetAO(StrataPixelHeader), ScreenAmbientOcclusion, BentNormal);
if (bProcessFrontFaceDiffuse)
{
// Finally sample the sky diffuse contribution (spherical harmonic, Lambert BRDF)
float3 DiffuseLookup = GetSkySHDiffuse(BentNormal) * View.SkyLightColor.rgb;
// And accumulate
// Note: Use diffuse directional albedo (i.e., DiffuseWeight) as first order approximation for env. integration (STRATA_TODO instead compute SH coefficients for Chan)
DiffuseLighting = CloudVolumetricAOShadow * BSDFThroughput * (SkyVisData.SkyDiffuseLookUpMul * DiffuseLookup + SkyVisData.SkyDiffuseLookUpAdd) * StrataEnvLight.DiffuseWeight;
}
if (bProcessBackFaceDiffuse)
{
// We do not evaluate back face sky light visibility data because all the data we have is for the front face only. This could be evaluated at some cost.
// However, we do apply SkyVisData.SkyDiffuseLookUpMul for scaling consistency.
// Finally sample the sky diffuse contribution (spherical harmonic, Lambert BRDF) along the opposite normal direction
float3 DiffuseLookup = GetSkySHDiffuse(-BentNormal) * View.SkyLightColor.rgb;
// And accumulate
// Note: Use diffuse directional albedo (i.e., DiffuseWeight) as first order approximation for env. integration (STRATA_TODO instead compute SH coefficients for Chan)
DiffuseLighting += CloudVolumetricAOShadow * BSDFThroughput * (SkyVisData.SkyDiffuseLookUpMul * DiffuseLookup) * StrataEnvLight.DiffuseBackFaceWeight;
}
}
#endif // ENABLE_DYNAMIC_SKY_LIGHT
// Specular component
const bool bIsTopLayer = BSDF_GETISTOPLAYER(BSDF);
SpecularLighting = 0;
if (any(StrataEnvLight.SpecularWeight > 0.0f))
{
float IndirectIrradiance = 0.0; // STRATA_TODO GBuffer.IndirectIrradiance, that will likely have to take 8 mnore bits on the header
#if ENABLE_SKY_LIGHT && ALLOW_STATIC_LIGHTING
BRANCH
// Add in diffuse contribution from dynamic skylights so reflection captures will have something to mix with
if (ReflectionStruct.SkyLightParameters.y > 0 && ReflectionStruct.SkyLightParameters.z > 0)
{
IndirectIrradiance += GetDynamicSkyIndirectIrradiance(BentNormal, StrataBSDFContext.N);
}
#endif
// Compute some extra occlusion information from DFAO and sky light data
float IndirectSpecularOcclusion = 1.0f;
float3 ExtraIndirectSpecular = 0.0f;
#if SUPPORT_DFAO_INDIRECT_OCCLUSION
float IndirectDiffuseOcclusion;
const bool bTwoSideFoliage = false;
GetDistanceFieldAOSpecularOcclusion(BentNormal, StrataEnvLight.SpecularDirection, StrataEnvLight.SpecularSafeRoughness, bTwoSideFoliage, IndirectSpecularOcclusion, IndirectDiffuseOcclusion, ExtraIndirectSpecular);
// Apply DFAO to IndirectIrradiance before mixing with indirect specular
IndirectIrradiance *= IndirectDiffuseOcclusion;
#endif
float RoughnessSquared = StrataEnvLight.SpecularSafeRoughness * StrataEnvLight.SpecularSafeRoughness;
float SpecularOcclusion = IndirectSpecularOcclusion * GetSpecularOcclusion(StrataBSDFContext.SatNoV, RoughnessSquared, CombinedScreenAndMaterialAO);
const bool bCompositeSkylight = true;
SpecularLighting += BSDFThroughput * StrataEnvLight.SpecularWeight *
CompositeReflectionCapturesAndSkylightTWS(
(bIsTopLayer ? TopLayerSpecularContributionFactor : 1.0f) * SpecularOcclusion,
TranslatedWorldPosition,
StrataEnvLight.SpecularDirection,
StrataEnvLight.SpecularSafeRoughness,
IndirectIrradiance,
IndirectSpecularOcclusion,
ExtraIndirectSpecular,
NumCulledReflectionCaptures,
CaptureDataStartIndex,
0,
bCompositeSkylight);
#if STRATA_FASTPATH==0
if (BSDF_GETHASHAZINESS(BSDF))
{
SpecularLighting += BSDFThroughput * StrataEnvLight.SpecularHazeWeight *
CompositeReflectionCapturesAndSkylightTWS(
(bIsTopLayer ? TopLayerSpecularContributionFactor : 1.0f) * SpecularOcclusion,
TranslatedWorldPosition,
StrataEnvLight.SpecularDirection,
StrataEnvLight.SpecularHazeSafeRoughness,
IndirectIrradiance,
IndirectSpecularOcclusion,
ExtraIndirectSpecular,
NumCulledReflectionCaptures,
CaptureDataStartIndex,
0,
bCompositeSkylight);
// SSR is traced for the sharpest lob. The smoothest one does not rely on SSR so we need to lower energy coming from SSR according to the lobe blend weight.
// And we also try to make the transition smooth using Haziness
SSRReductionFactor -= bIsTopLayer ? dot(BSDFThroughput, float3(1.0f / 3.0f, 1.0f / 3.0f, 1.0f / 3.0f)) * StrataEnvLight.SSRReduction : 0.0f;
}
#endif
}
}
#endif // STRATA_ENV_LIGHTING_COMMON