Skip to content

Commit 90f7174

Browse files
Hydrogent: use reverse depth buffer (#250)
1 parent 7adae45 commit 90f7174

File tree

5 files changed

+63
-70
lines changed

5 files changed

+63
-70
lines changed

Hydrogent/include/HnRenderParam.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 Diligent Graphics LLC
2+
* Copyright 2023-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,7 @@ class HnRenderParam final : public pxr::HdRenderParam
5252
bool UseIndexPool = false;
5353
bool AsyncShaderCompilation = false;
5454
bool UseNativeStartVertex = false;
55+
bool UseReverseDepth = false;
5556
HN_MATERIAL_TEXTURES_BINDING_MODE TextureBindingMode = {};
5657
float MetersPerUnit = 1.0f;
5758
Uint64 GeometryLoadBudget = 0;

Hydrogent/interface/Tasks/HnBeginFrameTask.hpp

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 Diligent Graphics LLC
2+
* Copyright 2023-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -73,52 +73,7 @@ struct HnBeginFrameTaskParams
7373
};
7474
RenderTargetFormats Formats;
7575

76-
struct RenderState
77-
{
78-
bool FrontFaceCCW = false;
79-
80-
float DepthBias = 0;
81-
float SlopeScaledDepthBias = 0;
82-
pxr::HdCompareFunction DepthFunc = pxr::HdCmpFuncLess;
83-
bool DepthBiasEnabled = false;
84-
bool DepthTestEnabled = true;
85-
bool DepthClampEnabled = false;
86-
87-
pxr::HdCullStyle CullStyle = pxr::HdCullStyleBack;
88-
89-
pxr::HdCompareFunction StencilFunc = pxr::HdCmpFuncAlways;
90-
int StencilRef = 0;
91-
int StencilMask = 0xFF;
92-
pxr::HdStencilOp StencilFailOp = pxr::HdStencilOpKeep;
93-
pxr::HdStencilOp StencilZFailOp = pxr::HdStencilOpKeep;
94-
pxr::HdStencilOp StencilZPassOp = pxr::HdStencilOpKeep;
95-
bool StencilEnabled = false;
96-
97-
constexpr bool operator==(const RenderState& rhs) const
98-
{
99-
// clang-format off
100-
return FrontFaceCCW == rhs.FrontFaceCCW &&
101-
DepthBias == rhs.DepthBias &&
102-
SlopeScaledDepthBias == rhs.SlopeScaledDepthBias &&
103-
DepthFunc == rhs.DepthFunc &&
104-
DepthBiasEnabled == rhs.DepthBiasEnabled &&
105-
DepthTestEnabled == rhs.DepthTestEnabled &&
106-
DepthClampEnabled == rhs.DepthClampEnabled &&
107-
CullStyle == rhs.CullStyle &&
108-
StencilFunc == rhs.StencilFunc &&
109-
StencilRef == rhs.StencilRef &&
110-
StencilMask == rhs.StencilMask &&
111-
StencilFailOp == rhs.StencilFailOp &&
112-
StencilZFailOp == rhs.StencilZFailOp &&
113-
StencilZPassOp == rhs.StencilZPassOp &&
114-
StencilEnabled == rhs.StencilEnabled;
115-
// clang-format on
116-
}
117-
};
118-
RenderState State;
119-
12076
float4 ClearColor = {0, 0, 0, 0};
121-
float ClearDepth = 1.f;
12277

12378
pxr::SdfPath FinalColorTargetId;
12479
pxr::SdfPath CameraId;
@@ -157,13 +112,11 @@ struct HnBeginFrameTaskParams
157112
bool operator==(const HnBeginFrameTaskParams& rhs) const
158113
{
159114
// clang-format off
160-
return Formats == rhs.Formats &&
161-
ClearColor == rhs.ClearColor &&
162-
ClearDepth == rhs.ClearDepth &&
163-
State == rhs.State &&
164-
FinalColorTargetId == rhs.FinalColorTargetId &&
165-
CameraId == rhs.CameraId &&
166-
Renderer == rhs.Renderer;
115+
return Formats == rhs.Formats &&
116+
ClearColor == rhs.ClearColor &&
117+
FinalColorTargetId == rhs.FinalColorTargetId &&
118+
CameraId == rhs.CameraId &&
119+
Renderer == rhs.Renderer;
167120
// clang-format on
168121
}
169122
bool operator!=(const HnBeginFrameTaskParams& rhs) const
@@ -231,6 +184,7 @@ class HnBeginFrameTask final : public HnTask
231184

232185
Uint32 m_FrameBufferWidth = 0;
233186
Uint32 m_FrameBufferHeight = 0;
187+
float m_DepthClearValue = 1.f;
234188

235189
Timer m_FrameTimer;
236190

Hydrogent/src/HnCamera.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ void HnCamera::Sync(pxr::HdSceneDelegate* SceneDelegate,
6060
pxr::HdDirtyBits OrigDirtyBits = *DirtyBits;
6161
pxr::HdCamera::Sync(SceneDelegate, RenderParam, DirtyBits);
6262

63-
const float MetersPerUnit = RenderParam ? static_cast<const HnRenderParam*>(RenderParam)->GetConfig().MetersPerUnit : 0.01f;
64-
const float UnitsPerMeter = 1.f / MetersPerUnit;
63+
const HnRenderParam::Configuration& RenderConfig = static_cast<const HnRenderParam*>(RenderParam)->GetConfig();
64+
const float MetersPerUnit = RenderConfig.MetersPerUnit;
65+
const float UnitsPerMeter = 1.f / MetersPerUnit;
6566
if (OrigDirtyBits & pxr::HdCamera::DirtyTransform)
6667
{
6768
// USD camera transform is defined in scene units, with camera looking along -Z axis.
@@ -97,19 +98,25 @@ void HnCamera::Sync(pxr::HdSceneDelegate* SceneDelegate,
9798
const RenderDeviceInfo& DeviceInfo = pDevice->GetDeviceInfo();
9899
const bool NegativeOneToOneNDCZ = DeviceInfo.GetNDCAttribs().MinZ == -1;
99100

101+
float NearPlane = ClippingRangeMeters.GetMin();
102+
float FarPlane = ClippingRangeMeters.GetMax();
103+
if (RenderConfig.UseReverseDepth)
104+
{
105+
std::swap(NearPlane, FarPlane);
106+
}
100107
if (GetProjection() == pxr::HdCamera::Projection::Perspective)
101108
{
102109
m_ProjectionMatrix = {};
103110

104111
m_ProjectionMatrix._11 = FocalLengthUnits / (0.5f * HorzApertureUnits);
105112
m_ProjectionMatrix._22 = FocalLengthUnits / (0.5f * VertApertureUnits);
106113

107-
m_ProjectionMatrix.SetNearFarClipPlanes(ClippingRangeMeters.GetMin(), ClippingRangeMeters.GetMax(), NegativeOneToOneNDCZ);
114+
m_ProjectionMatrix.SetNearFarClipPlanes(NearPlane, FarPlane, NegativeOneToOneNDCZ);
108115
}
109116
else if (GetProjection() == pxr::HdCamera::Projection::Orthographic)
110117
{
111118

112-
m_ProjectionMatrix = float4x4::Ortho(HorzApertureMeters, VertApertureMeters, ClippingRangeMeters.GetMin(), ClippingRangeMeters.GetMax(), NegativeOneToOneNDCZ);
119+
m_ProjectionMatrix = float4x4::Ortho(HorzApertureMeters, VertApertureMeters, NearPlane, FarPlane, NegativeOneToOneNDCZ);
113120
}
114121
else
115122
{

Hydrogent/src/HnRenderDelegate.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 Diligent Graphics LLC
2+
* Copyright 2023-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -467,6 +467,7 @@ HnRenderDelegate::HnRenderDelegate(const CreateInfo& CI) :
467467
CI.UseIndexPool,
468468
CI.AsyncShaderCompilation,
469469
!CI.pDevice->GetDeviceInfo().IsGLDevice(), // UseNativeStartVertex
470+
CI.pDevice->GetDeviceInfo().NDC.MinZ == 0, // UseReverseDepth
470471
CI.TextureBindingMode,
471472
CI.MetersPerUnit,
472473
CI.GeometryLoadBudget,

Hydrogent/src/Tasks/HnBeginFrameTask.cpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,46 @@ static void UpdateRenderPassState(const HnBeginFrameTaskParams& Params,
114114
const TEXTURE_FORMAT* RTVFormats,
115115
Uint32 NumRTVs,
116116
TEXTURE_FORMAT DSVFormat,
117+
const HnRenderParam* RenderParam,
117118
HnRenderPassState& RPState)
118119
{
119120
RPState.SetNumRenderTargets(NumRTVs);
120121
for (Uint32 i = 0; i < NumRTVs; ++i)
121122
RPState.SetRenderTargetFormat(i, RTVFormats[i]);
123+
122124
RPState.SetDepthStencilFormat(DSVFormat);
123125

124-
RPState.SetDepthBias(Params.State.DepthBias, Params.State.SlopeScaledDepthBias);
125-
RPState.SetDepthFunc(Params.State.DepthFunc);
126-
RPState.SetDepthBiasEnabled(Params.State.DepthBiasEnabled);
127-
RPState.SetEnableDepthTest(Params.State.DepthTestEnabled);
128-
RPState.SetEnableDepthClamp(Params.State.DepthClampEnabled);
126+
bool FrontFaceCCW = true;
127+
128+
float DepthBias = 0;
129+
float SlopeScaledDepthBias = 0;
130+
pxr::HdCompareFunction DepthFunc = RenderParam->GetConfig().UseReverseDepth ? pxr::HdCmpFuncGreater : pxr::HdCmpFuncLess;
131+
bool DepthBiasEnabled = false;
132+
bool DepthTestEnabled = true;
133+
bool DepthClampEnabled = false;
134+
135+
pxr::HdCullStyle CullStyle = pxr::HdCullStyleBack;
136+
137+
pxr::HdCompareFunction StencilFunc = pxr::HdCmpFuncAlways;
138+
int StencilRef = 0;
139+
int StencilMask = 0xFF;
140+
pxr::HdStencilOp StencilFailOp = pxr::HdStencilOpKeep;
141+
pxr::HdStencilOp StencilZFailOp = pxr::HdStencilOpKeep;
142+
pxr::HdStencilOp StencilZPassOp = pxr::HdStencilOpKeep;
143+
bool StencilEnabled = false;
129144

130-
RPState.SetCullStyle(Params.State.CullStyle);
145+
RPState.SetDepthFunc(DepthFunc);
146+
RPState.SetDepthBias(DepthBias, SlopeScaledDepthBias);
147+
RPState.SetDepthBiasEnabled(DepthBiasEnabled);
148+
RPState.SetEnableDepthTest(DepthTestEnabled);
149+
RPState.SetEnableDepthClamp(DepthClampEnabled);
131150

132-
RPState.SetStencil(Params.State.StencilFunc, Params.State.StencilRef, Params.State.StencilMask,
133-
Params.State.StencilFailOp, Params.State.StencilZFailOp, Params.State.StencilZPassOp);
151+
RPState.SetCullStyle(CullStyle);
134152

135-
RPState.SetFrontFaceCCW(Params.State.FrontFaceCCW);
153+
RPState.SetStencilEnabled(StencilEnabled);
154+
RPState.SetStencil(StencilFunc, StencilRef, StencilMask, StencilFailOp, StencilZFailOp, StencilZPassOp);
155+
156+
RPState.SetFrontFaceCCW(FrontFaceCCW);
136157
}
137158

138159
static TEXTURE_FORMAT GetFallbackTextureFormat(TEXTURE_FORMAT Format)
@@ -154,25 +175,35 @@ void HnBeginFrameTask::Sync(pxr::HdSceneDelegate* Delegate,
154175
{
155176
if (GetTaskParams(Delegate, m_Params))
156177
{
178+
pxr::HdRenderIndex& RenderIndex = Delegate->GetRenderIndex();
179+
const HnRenderDelegate* RenderDelegate = static_cast<const HnRenderDelegate*>(RenderIndex.GetRenderDelegate());
180+
const HnRenderParam* RenderParam = static_cast<const HnRenderParam*>(RenderDelegate->GetRenderParam());
181+
157182
UpdateRenderPassState(m_Params,
158183
m_Params.Formats.GBuffer.data(),
159184
m_Params.Formats.GBuffer.size(),
160185
m_Params.Formats.Depth,
186+
RenderParam,
161187
m_RenderPassStates[HnRenderResourceTokens->renderPass_OpaqueSelected]);
162188

163189
UpdateRenderPassState(m_Params,
164190
m_Params.Formats.GBuffer.data(),
165191
m_Params.Formats.GBuffer.size(),
166192
m_Params.Formats.Depth,
193+
RenderParam,
167194
m_RenderPassStates[HnRenderResourceTokens->renderPass_OpaqueUnselected_TransparentAll]);
168195

169196
UpdateRenderPassState(m_Params,
170197
nullptr,
171198
0,
172199
m_Params.Formats.Depth,
200+
RenderParam,
173201
m_RenderPassStates[HnRenderResourceTokens->renderPass_TransparentSelected]);
174202

203+
m_DepthClearValue = RenderParam->GetConfig().UseReverseDepth ? 0.f : 1.f;
204+
175205
(*TaskCtx)[HnRenderResourceTokens->suspendSuperSampling] = pxr::VtValue{true};
206+
(*TaskCtx)[HnRenderResourceTokens->backgroundDepth] = pxr::VtValue{m_DepthClearValue};
176207
}
177208
}
178209

@@ -289,7 +320,7 @@ void HnBeginFrameTask::PrepareRenderTargets(pxr::HdRenderIndex* RenderIndex,
289320

290321
// We first render selected objects using the selection depth buffer.
291322
// Selection depth buffer is copied to the main depth buffer by the HnCopySelectionDepthTask.
292-
RP_OpaqueSelected.Begin(HnFrameRenderTargets::GBUFFER_TARGET_COUNT, m_FrameRenderTargets.GBufferRTVs.data(), m_FrameRenderTargets.SelectionDepthDSV, ClearValues.data(), m_Params.ClearDepth, ~0u);
323+
RP_OpaqueSelected.Begin(HnFrameRenderTargets::GBUFFER_TARGET_COUNT, m_FrameRenderTargets.GBufferRTVs.data(), m_FrameRenderTargets.SelectionDepthDSV, ClearValues.data(), m_DepthClearValue, ~0u);
293324
RP_OpaqueUnselected_TransparentAll.Begin(HnFrameRenderTargets::GBUFFER_TARGET_COUNT, m_FrameRenderTargets.GBufferRTVs.data(), m_FrameRenderTargets.DepthDSV);
294325
RP_TransparentSelected.Begin(0, nullptr, m_FrameRenderTargets.SelectionDepthDSV);
295326

@@ -365,7 +396,6 @@ void HnBeginFrameTask::Prepare(pxr::HdTaskContext* TaskCtx,
365396
(*TaskCtx)[HnRenderResourceTokens->closestSelectedLocation0Target] = pxr::VtValue{m_ClosestSelLocnTargetId[0]};
366397
(*TaskCtx)[HnRenderResourceTokens->closestSelectedLocation1Target] = pxr::VtValue{m_ClosestSelLocnTargetId[1]};
367398
(*TaskCtx)[HnRenderResourceTokens->jitteredFinalColorTarget] = pxr::VtValue{m_JitteredFinalColorTargetId};
368-
(*TaskCtx)[HnRenderResourceTokens->backgroundDepth] = pxr::VtValue{m_Params.ClearDepth};
369399

370400
bool ResetTAA = false;
371401
if (!m_Params.CameraId.IsEmpty())

0 commit comments

Comments
 (0)