Moved some decal rendering logic to CalculateDecalFadeAlpha.

- will be used to support decals in raytracing techniques.

#rb jeremy.moore
#preflight 628ccfbe8e7f9e3cc5d73d84

[CL 20347248 by tiago costa in ue5-main branch]
This commit is contained in:
tiago costa
2022-05-24 09:18:38 -04:00
parent 7a21ea463f
commit 8f4c0a9a62
5 changed files with 49 additions and 35 deletions

View File

@@ -206,7 +206,7 @@ bool FMeshDecalMeshProcessor::TryAddMeshBatch(
if (Material.GetRenderingThreadShaderMap())
{
const EShaderPlatform ShaderPlatform = ViewIfDynamicMeshCommand->GetShaderPlatform();
const FDecalBlendDesc DecalBlendDesc = DecalRendering::ComputeDecalBlendDesc(ShaderPlatform, &Material);
const FDecalBlendDesc DecalBlendDesc = DecalRendering::ComputeDecalBlendDesc(ShaderPlatform, Material);
const bool bShouldRender =
DecalRendering::IsCompatibleWithRenderStage(DecalBlendDesc, PassDecalStage) &&

View File

@@ -72,38 +72,38 @@ namespace DecalRendering
}
}
FDecalBlendDesc ComputeDecalBlendDesc(EShaderPlatform Platform, FMaterial const* Material)
FDecalBlendDesc ComputeDecalBlendDesc(EShaderPlatform Platform, const FMaterial& Material)
{
FDecalBlendDesc Desc;
if (Strata::IsStrataEnabled())
{
check(Material->IsStrataMaterial());
check(Material.IsStrataMaterial());
const bool bUseDiffuseAlbedoAndF0 =
Material->HasMaterialPropertyConnected(EMaterialProperty::MP_DiffuseColor) || // This is used for Strata Slab using (DiffuseAlbedo | F0) parameterization
Material->HasMaterialPropertyConnected(EMaterialProperty::MP_SpecularColor); // This is used for Strata Slab using (DiffuseAlbedo | F0) parameterization
Material.HasMaterialPropertyConnected(EMaterialProperty::MP_DiffuseColor) || // This is used for Strata Slab using (DiffuseAlbedo | F0) parameterization
Material.HasMaterialPropertyConnected(EMaterialProperty::MP_SpecularColor); // This is used for Strata Slab using (DiffuseAlbedo | F0) parameterization
Desc.BlendMode = Material->GetBlendMode();
Desc.bWriteBaseColor = Material->HasMaterialPropertyConnected(EMaterialProperty::MP_BaseColor) || bUseDiffuseAlbedoAndF0;
Desc.bWriteNormal = Material->HasMaterialPropertyConnected(EMaterialProperty::MP_Normal);
Desc.BlendMode = Material.GetBlendMode();
Desc.bWriteBaseColor = Material.HasMaterialPropertyConnected(EMaterialProperty::MP_BaseColor) || bUseDiffuseAlbedoAndF0;
Desc.bWriteNormal = Material.HasMaterialPropertyConnected(EMaterialProperty::MP_Normal);
Desc.bWriteRoughnessSpecularMetallic =
bUseDiffuseAlbedoAndF0 ||
Material->HasMaterialPropertyConnected(EMaterialProperty::MP_Metallic) ||
Material->HasMaterialPropertyConnected(EMaterialProperty::MP_Specular) ||
Material->HasMaterialPropertyConnected(EMaterialProperty::MP_Roughness);
Material.HasMaterialPropertyConnected(EMaterialProperty::MP_Metallic) ||
Material.HasMaterialPropertyConnected(EMaterialProperty::MP_Specular) ||
Material.HasMaterialPropertyConnected(EMaterialProperty::MP_Roughness);
Desc.bWriteEmissive=
Material->HasMaterialPropertyConnected(EMaterialProperty::MP_EmissiveColor);
Material.HasMaterialPropertyConnected(EMaterialProperty::MP_EmissiveColor);
Desc.bWriteAmbientOcclusion =
Material->HasMaterialPropertyConnected(EMaterialProperty::MP_AmbientOcclusion);
Material.HasMaterialPropertyConnected(EMaterialProperty::MP_AmbientOcclusion);
}
else
{
Desc.BlendMode = Material->GetBlendMode();
Desc.bWriteBaseColor = Material->HasBaseColorConnected();
Desc.bWriteNormal = Material->HasNormalConnected();
Desc.bWriteRoughnessSpecularMetallic = Material->HasRoughnessConnected() || Material->HasSpecularConnected() || Material->HasMetallicConnected();
Desc.bWriteEmissive = Material->HasEmissiveColorConnected();
Desc.bWriteAmbientOcclusion = Material->HasAmbientOcclusionConnected();
Desc.BlendMode = Material.GetBlendMode();
Desc.bWriteBaseColor = Material.HasBaseColorConnected();
Desc.bWriteNormal = Material.HasNormalConnected();
Desc.bWriteRoughnessSpecularMetallic = Material.HasRoughnessConnected() || Material.HasSpecularConnected() || Material.HasMetallicConnected();
Desc.bWriteEmissive = Material.HasEmissiveColorConnected();
Desc.bWriteAmbientOcclusion = Material.HasAmbientOcclusionConnected();
}
FinalizeBlendDesc(Platform, Desc);
return Desc;

View File

@@ -78,7 +78,7 @@ enum class EDecalRasterizerState : uint8
namespace DecalRendering
{
/** Build the packed decal description from a decal material. */
FDecalBlendDesc ComputeDecalBlendDesc(EShaderPlatform Platform, FMaterial const* Material);
FDecalBlendDesc ComputeDecalBlendDesc(EShaderPlatform Platform, const FMaterial& Material);
/** Build the packed decal description from a decal material. */
FDecalBlendDesc ComputeDecalBlendDesc(EShaderPlatform Platform, FMaterialShaderParameters const& MaterialShaderParameters);

View File

@@ -29,7 +29,7 @@ FTransientDecalRenderData::FTransientDecalRenderData(const FScene& InScene, cons
MaterialProxy = InDecalProxy->DecalMaterial->GetRenderProxy();
MaterialResource = &MaterialProxy->GetMaterialWithFallback(InScene.GetFeatureLevel(), MaterialProxy);
check(MaterialProxy && MaterialResource);
DecalBlendDesc = DecalRendering::ComputeDecalBlendDesc(InScene.GetShaderPlatform(), MaterialResource);
DecalBlendDesc = DecalRendering::ComputeDecalBlendDesc(InScene.GetShaderPlatform(), *MaterialResource);
}
/**
@@ -209,6 +209,30 @@ IMPLEMENT_MATERIAL_SHADER_TYPE(, FDeferredDecalAmbientOcclusionPS, TEXT("/Engine
namespace DecalRendering
{
float GetDecalFadeScreenSizeMultiplier()
{
return CVarDecalFadeScreenSizeMultiplier.GetValueOnRenderThread();
}
float CalculateDecalFadeAlpha(float DecalFadeScreenSize, const FMatrix& ComponentToWorldMatrix, const FViewInfo& View, float FadeMultiplier)
{
check(View.IsPerspectiveProjection());
float Distance = (View.ViewMatrices.GetViewOrigin() - ComponentToWorldMatrix.GetOrigin()).Size();
float Radius = ComponentToWorldMatrix.GetMaximumAxisScale();
float CurrentScreenSize = ((Radius / Distance) * FadeMultiplier);
// fading coefficient needs to increase with increasing field of view and decrease with increasing resolution
// FadeCoeffScale is an empirically determined constant to bring us back roughly to fraction of screen size for FadeScreenSize
const float FadeCoeffScale = 600.0f;
float FOVFactor = ((2.0f / View.ViewMatrices.GetProjectionMatrix().M[0][0]) / View.ViewRect.Width()) * FadeCoeffScale;
float FadeCoeff = DecalFadeScreenSize * FOVFactor;
float FadeRange = FadeCoeff * 0.5f;
float Alpha = (CurrentScreenSize - FadeCoeff) / FadeRange;
return FMath::Clamp(Alpha, 0.0f, 1.0f);
}
bool BuildVisibleDecalList(const FScene& Scene, const FViewInfo& View, EDecalRenderStage DecalRenderStage, FTransientDecalRenderDataList* OutVisibleDecals)
{
QUICK_SCOPE_CYCLE_COUNTER(BuildVisibleDecalList);
@@ -225,7 +249,7 @@ namespace DecalRendering
return false;
}
const float FadeMultiplier = CVarDecalFadeScreenSizeMultiplier.GetValueOnRenderThread();
const float FadeMultiplier = GetDecalFadeScreenSizeMultiplier();
const EShaderPlatform ShaderPlatform = View.GetShaderPlatform();
const bool bIsPerspectiveProjection = View.IsPerspectiveProjection();
@@ -267,19 +291,7 @@ namespace DecalRendering
{
if (bIsPerspectiveProjection && Data.DecalProxy->FadeScreenSize != 0.0f)
{
float Distance = (View.ViewMatrices.GetViewOrigin() - ComponentToWorldMatrix.GetOrigin()).Size();
float Radius = ComponentToWorldMatrix.GetMaximumAxisScale();
float CurrentScreenSize = ((Radius / Distance) * FadeMultiplier);
// fading coefficient needs to increase with increasing field of view and decrease with increasing resolution
// FadeCoeffScale is an empirically determined constant to bring us back roughly to fraction of screen size for FadeScreenSize
const float FadeCoeffScale = 600.0f;
float FOVFactor = ((2.0f/View.ViewMatrices.GetProjectionMatrix().M[0][0]) / View.ViewRect.Width()) * FadeCoeffScale;
float FadeCoeff = Data.DecalProxy->FadeScreenSize * FOVFactor;
float FadeRange = FadeCoeff * 0.5f;
float Alpha = (CurrentScreenSize - FadeCoeff) / FadeRange;
Data.FadeAlpha = FMath::Min(Alpha, 1.0f);
Data.FadeAlpha = CalculateDecalFadeAlpha(Data.DecalProxy->FadeScreenSize, ComponentToWorldMatrix, View, FadeMultiplier);
}
const bool bShouldRender = Data.FadeAlpha > 0.0f;

View File

@@ -34,6 +34,8 @@ typedef TArray<FTransientDecalRenderData, SceneRenderingAllocator> FTransientDec
*/
namespace DecalRendering
{
float GetDecalFadeScreenSizeMultiplier();
float CalculateDecalFadeAlpha(float DecalFadeScreenSize, const FMatrix& ComponentToWorldMatrix, const FViewInfo& View, float FadeMultiplier);
FMatrix ComputeComponentToClipMatrix(const FViewInfo& View, const FMatrix& DecalComponentToWorld);
void SetVertexShaderOnly(FRHICommandList& RHICmdList, FGraphicsPipelineStateInitializer& GraphicsPSOInit, const FViewInfo& View, const FMatrix& FrustumComponentToClip);
bool BuildVisibleDecalList(const FScene& Scene, const FViewInfo& View, EDecalRenderStage DecalRenderStage, FTransientDecalRenderDataList* OutVisibleDecals);