Skip to content

Commit dbf7b60

Browse files
PBR Renderer: render opacity instead of transparency to alpha channel
1 parent 73d9455 commit dbf7b60

File tree

7 files changed

+24
-21
lines changed

7 files changed

+24
-21
lines changed

Hydrogent/interface/Tasks/HnBeginFrameTask.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct HnBeginFrameTaskParams
7373
};
7474
RenderTargetFormats Formats;
7575

76-
float4 ClearColor = {0, 0, 0, 0};
76+
float3 ClearColor = {0, 0, 0};
7777

7878
bool UseReverseDepth = false;
7979

@@ -89,8 +89,8 @@ struct HnBeginFrameTaskParams
8989
float4 UnshadedColor = {1, 1, 1, 1};
9090
float PointSize = 1;
9191

92-
float4 LoadingAnimationColor0 = {0.1f, 0.100f, 0.10f, 1.0f};
93-
float4 LoadingAnimationColor1 = {1.0f, 0.675f, 0.25f, 1.0f};
92+
float3 LoadingAnimationColor0 = {0.1f, 0.100f, 0.10f};
93+
float3 LoadingAnimationColor1 = {1.0f, 0.675f, 0.25f};
9494
float LoadingAnimationWorldScale = 1.0f;
9595
float LoadingAnimationSpeed = 0.25f;
9696
float LoadingAnimationTransitionDuration = 0.5f;

Hydrogent/shaders/HnPostProcess.psh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void main(in FullScreenTriangleVSOutput VSOut,
142142
}
143143
#endif
144144

145-
float Opacity = 1.0 - Color.a;
145+
float Opacity = Color.a;
146146

147147
float SSRScale = g_Attribs.SSRScale * Opacity;
148148
if (SSRScale > 0.0)

Hydrogent/src/Tasks/HnBeginFrameTask.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,14 @@ void HnBeginFrameTask::PrepareRenderTargets(pxr::HdRenderIndex* RenderIndex,
258258
{
259259
if (i == HnFrameRenderTargets::GBUFFER_TARGET_SCENE_COLOR)
260260
{
261-
if (RenderParam->GetViewMode() != HN_VIEW_MODE_SCENE_DEPTH)
262-
{
263-
// NB: we should clear alpha to one as it accumulates the total transmittance
264-
ClearValues[i] = float4{m_Params.ClearColor.r, m_Params.ClearColor.g, m_Params.ClearColor.b, 1.0};
265-
}
266-
else
261+
float3 ClearColor = m_Params.ClearColor;
262+
if (RenderParam->GetViewMode() == HN_VIEW_MODE_SCENE_DEPTH)
267263
{
268264
// Clear background to white in scene depth debug view mode
269-
ClearValues[i] = float4{1};
265+
ClearColor = float3{1.0};
270266
}
267+
// NB: we should clear alpha to zero as it accumulates the total opacity
268+
ClearValues[i] = float4{ClearColor, 0.0};
271269
}
272270
else
273271
{
@@ -672,8 +670,8 @@ void HnBeginFrameTask::UpdateFrameConstants(IDeviceContext* pCtx,
672670
}
673671

674672
RendererParams.LoadingAnimation.Factor = LoadingAnimationFactor;
675-
RendererParams.LoadingAnimation.Color0 = m_Params.Renderer.LoadingAnimationColor0;
676-
RendererParams.LoadingAnimation.Color1 = m_Params.Renderer.LoadingAnimationColor1;
673+
RendererParams.LoadingAnimation.Color0 = float4{m_Params.Renderer.LoadingAnimationColor0, 0.0};
674+
RendererParams.LoadingAnimation.Color1 = float4{m_Params.Renderer.LoadingAnimationColor1, 0.0};
677675
RendererParams.LoadingAnimation.WorldScale = m_Params.Renderer.LoadingAnimationWorldScale;
678676
RendererParams.LoadingAnimation.Speed = m_Params.Renderer.LoadingAnimationSpeed;
679677

Hydrogent/src/Tasks/HnRenderBoundBoxTask.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ void main(in BoundBoxVSOutput VSOut,
8787
{
8888
BoundBoxOutput BoundBox = GetBoundBoxOutput(VSOut);
8989
90-
// Write 1.0 alpha as if bound box was fully transparent.
91-
Color = float4(BoundBox.Color.rgb, 1.0);
90+
// Write 0.0 alpha as if bound box was fully transparent.
91+
Color = float4(BoundBox.Color.rgb, 0.0);
9292
9393
MotionVec = float4(BoundBox.MotionVector, 0.0, 1.0);
9494
}

Hydrogent/src/Tasks/HnRenderEnvMapTask.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void HnRenderEnvMapTask::Prepare(pxr::HdTaskContext* TaskCtx,
176176
EnvMapAttribs.pEnvMap = pEnvMapSRV;
177177
EnvMapAttribs.AverageLogLum = 0.3f;
178178
EnvMapAttribs.MipLevel = 1;
179-
EnvMapAttribs.Alpha = 1;
179+
EnvMapAttribs.Alpha = 0; // Fully transparent
180180
EnvMapAttribs.Options = EnvMapRenderer::OPTION_FLAG_COMPUTE_MOTION_VECTORS;
181181

182182
{

PBR/src/PBR_Renderer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,9 +2057,14 @@ void PBR_Renderer::CreatePSO(PsoHashMapType& PsoHashMap,
20572057
RT0.BlendOp = BLEND_OPERATION_ADD;
20582058
}
20592059

2060-
// Compute total transmittance, e.g. (1.0 - A0) * (1.0 - A1) ... * (1.0 - An),
2060+
// Compute total opacity, e.g. 1.0 - (1.0 - A0) * (1.0 - A1) ... * (1.0 - An),
20612061
// in alpha channel.
2062-
RT0.SrcBlendAlpha = BLEND_FACTOR_ZERO; // SrcA * 0
2062+
// Dst[n] = 1.0 - (1.0 - A[0]) * ... * (1.0 - A[n])
2063+
// Dst[n+1] = 1.0 - (1.0 - A[0]) * ... * (1.0 - A[n]) * (1.0 - A[n+1])
2064+
// Dst[n+1] = 1.0 - (1.0 - Dst[n]) * (1.0 - A[n+1]) = Dst[n] + A[n+1] - Dst[n] * A[n+1]
2065+
// Dst[n+1] = A[n+1] * 1.0 + Dst[n] * (1.0 - A[n+1])
2066+
// SrcA DstA SrcA
2067+
RT0.SrcBlendAlpha = BLEND_FACTOR_ONE; // SrcA * 1.0
20632068
RT0.DestBlendAlpha = BLEND_FACTOR_INV_SRC_ALPHA; // DstA * (1.0 - SrcA)
20642069
RT0.BlendOpAlpha = BLEND_OPERATION_ADD;
20652070

Shaders/PBR/private/RenderPBR.psh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,16 +624,16 @@ PSOutput main(in VSOutput VSOut,
624624
if (Factor > 0.0)
625625
{
626626
float4 LoadingAnimationColor = GetLoadingAnimationColor(Shading.Pos, Factor);
627-
OutColor = lerp(OutColor, float4(LoadingAnimationColor.rgb, 1.0), LoadingAnimationColor.a);
627+
OutColor = lerp(OutColor, float4(LoadingAnimationColor.rgb, 0.0), LoadingAnimationColor.a);
628628
}
629629
}
630630
#endif
631631

632632
OutColor.rgb *= Transmittance;
633633
if (BasicAttribs.AlphaMode != PBR_ALPHA_MODE_BLEND)
634634
{
635-
// Write 0 alpha to indicate full opacity
636-
OutColor.a = 0.0;
635+
// Write 1.0 alpha to indicate full opacity
636+
OutColor.a = 1.0;
637637
}
638638

639639
#if CONVERT_OUTPUT_TO_SRGB

0 commit comments

Comments
 (0)