Skip to content

Commit 6717a48

Browse files
PBR Renderer: implemented layerd OIT blend
1 parent 296edc3 commit 6717a48

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

PBR/src/PBR_Renderer.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,8 +1954,8 @@ void PBR_Renderer::CreatePSO(PsoHashMapType& PsoHashMap,
19541954

19551955
RefCntAutoPtr<IShader>& pPS = m_PixelShaders[{
19561956
PSOFlags,
1957-
// Opaque and Blend modes use the same shader
1958-
Key.GetAlphaMode() == ALPHA_MODE_MASK ? ALPHA_MODE_MASK : ALPHA_MODE_OPAQUE,
1957+
// Non-OIT blend uses the same shader as opaque
1958+
(Key.GetAlphaMode() == ALPHA_MODE_BLEND && OITLayerCount == 0) ? ALPHA_MODE_OPAQUE : Key.GetAlphaMode(),
19591959
CULL_MODE_BACK,
19601960
Key,
19611961
}];
@@ -1987,12 +1987,7 @@ void PBR_Renderer::CreatePSO(PsoHashMapType& PsoHashMap,
19871987
pPS = m_Device.CreateShader(ShaderCI);
19881988
}
19891989

1990-
GraphicsPipeline = GraphicsDesc;
1991-
if (Key.GetType() == RenderPassType::OITLayers)
1992-
{
1993-
GraphicsPipeline.DepthStencilDesc.DepthWriteEnable = False;
1994-
}
1995-
1990+
GraphicsPipeline = GraphicsDesc;
19961991
GraphicsPipeline.InputLayout = InputLayout;
19971992

19981993
IPipelineResourceSignature* ppSignatures[MAX_RESOURCE_SIGNATURES];
@@ -2018,7 +2013,8 @@ void PBR_Renderer::CreatePSO(PsoHashMapType& PsoHashMap,
20182013
VERIFY(Key.GetLoadingAnimation() == LoadingAnimationMode::None, "Loading animation must be disabled for OIT Layers render pass");
20192014
VERIFY(Key.GetDebugView() == DebugViewType::None, "Debug view must be disabled for OIT Layers render pass");
20202015

2021-
PSOCreateInfo.GraphicsPipeline.BlendDesc = BS_UpdateOITTail;
2016+
PSOCreateInfo.GraphicsPipeline.BlendDesc = BS_UpdateOITTail;
2017+
GraphicsPipeline.DepthStencilDesc.DepthWriteEnable = False;
20222018
}
20232019
else
20242020
{
@@ -2030,7 +2026,15 @@ void PBR_Renderer::CreatePSO(PsoHashMapType& PsoHashMap,
20302026
else if (AlphaMode == ALPHA_MODE_BLEND)
20312027
{
20322028
VERIFY(!IsUnshaded, "Unshaded mode should use OpaquePSO. The PSOKey's ctor sets the alpha mode to opaque.");
2033-
GraphicsPipeline.BlendDesc = BS_PremultipliedAlphaBlend;
2029+
if (OITLayerCount > 0)
2030+
{
2031+
PSOCreateInfo.GraphicsPipeline.BlendDesc = BS_AdditiveBlend;
2032+
GraphicsPipeline.DepthStencilDesc.DepthWriteEnable = False;
2033+
}
2034+
else
2035+
{
2036+
GraphicsPipeline.BlendDesc = BS_PremultipliedAlphaBlend;
2037+
}
20342038
}
20352039
else
20362040
{

Shaders/PBR/private/RenderPBR.psh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
# include "Iridescence.fxh"
1111
#endif
1212

13+
#if NUM_OIT_LAYERS > 0
14+
# include "OIT.fxh"
15+
#endif
16+
1317
#include "VSOutputStruct.generated"
1418
// struct VSOutput
1519
// {
@@ -68,6 +72,11 @@ Texture2DArray<float> g_ShadowMap;
6872
SamplerComparisonState g_ShadowMap_sampler;
6973
#endif
7074

75+
#if NUM_OIT_LAYERS > 0
76+
StructuredBuffer<uint> g_OITLayers;
77+
Texture2D<float4> g_OITTail;
78+
#endif
79+
7180
PBRMaterialTextureAttribs GetDefaultTextureAttribs()
7281
{
7382
PBRMaterialTextureAttribs Attribs;
@@ -371,6 +380,39 @@ float4 GetLoadingAnimationColor(float3 WorldPos, float Factor)
371380
}
372381
#endif
373382

383+
#if NUM_OIT_LAYERS > 0
384+
float GetOITTransmittance(float Depth, uint2 Pos)
385+
{
386+
uint D = uint(Depth * 16777215.0);
387+
float T = 1.0;
388+
389+
uint Offset = GetOITLayerDataOffset(uint2(Pos.xy), uint2(g_Frame.Camera.f4ViewportSize.xy), uint(NUM_OIT_LAYERS));
390+
391+
uint layer = 0u;
392+
while (layer < uint(NUM_OIT_LAYERS))
393+
{
394+
uint LayerDT = g_OITLayers[Offset + layer];
395+
uint LayerD = GetOITLayerDepth(LayerDT);
396+
// +1u helps to avoid precision issues.
397+
if (D <= LayerD + 1u)
398+
{
399+
break;
400+
}
401+
float LayerT = GetOITLayerTransmittance(LayerDT);
402+
T *= LayerT;
403+
++layer;
404+
}
405+
if (layer == uint(NUM_OIT_LAYERS))
406+
{
407+
float4 Tail = g_OITTail.Load(int3(Pos.xy, 0));
408+
// Average contribution of all tail layers.
409+
T /= max(255.0 * Tail.x, 1.0);
410+
}
411+
412+
return T;
413+
}
414+
#endif
415+
374416
PSOutput main(in VSOutput VSOut,
375417
in bool IsFrontFace : SV_IsFrontFace)
376418
{
@@ -497,6 +539,13 @@ PSOutput main(in VSOutput VSOut,
497539
if (BasicAttribs.AlphaMode == PBR_ALPHA_MODE_BLEND)
498540
{
499541
OutColor.rgb *= BaseColor.a;
542+
#if NUM_OIT_LAYERS > 0
543+
if (BaseColor.a > OIT_OPACITY_THRESHOLD)
544+
{
545+
float T = GetOITTransmittance(VSOut.ClipPos.z, uint2(VSOut.ClipPos.xy));
546+
OutColor.rgb *= T;
547+
}
548+
#endif
500549
}
501550

502551
// Add highlight color

0 commit comments

Comments
 (0)