Skip to content

Commit 9cc4d2b

Browse files
BoundBoxRenderer: support reverse depth
1 parent c031025 commit 9cc4d2b

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

Components/interface/BoundBoxRenderer.hpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Diligent Graphics LLC
2+
* Copyright 2024-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.
@@ -34,6 +34,7 @@
3434
#include "../../../DiligentCore/Common/interface/RefCntAutoPtr.hpp"
3535
#include "../../../DiligentCore/Common/interface/HashUtils.hpp"
3636
#include "../../../DiligentCore/Common/interface/BasicMath.hpp"
37+
#include "../../../DiligentCore/Primitives/interface/FlagEnum.h"
3738

3839
namespace Diligent
3940
{
@@ -68,6 +69,21 @@ class BoundBoxRenderer
6869
BoundBoxRenderer(const CreateInfo& CI);
6970
~BoundBoxRenderer();
7071

72+
/// Option flags.
73+
enum OPTION_FLAGS : Uint32
74+
{
75+
OPTION_FLAG_NONE = 0u,
76+
77+
/// Manually convert shader output to sRGB color space.
78+
OPTION_FLAG_CONVERT_OUTPUT_TO_SRGB = 1u << 0u,
79+
80+
/// Compute motion vectors.
81+
OPTION_FLAG_COMPUTE_MOTION_VECTORS = 1u << 1u,
82+
83+
/// Use reverse depth (i.e. near plane is at 1.0, far plane is at 0.0).
84+
OPTION_FLAG_USE_REVERSE_DEPTH = 1u << 2u
85+
};
86+
7187
struct RenderAttribs
7288
{
7389
/// Bounding box transformation matrix.
@@ -86,43 +102,36 @@ class BoundBoxRenderer
86102
/// For example, use 0x0000FFFFu to draw a dashed line.
87103
Uint32 PatternMask = 0xFFFFFFFFu;
88104

89-
/// Manually convert shader output to sRGB color space.
90-
bool ConvertOutputToSRGB = false;
91-
92-
bool ComputeMotionVectors = false;
105+
/// Render options.
106+
OPTION_FLAGS Options = OPTION_FLAG_NONE;
93107
};
94108
void Prepare(IDeviceContext* pContext, const RenderAttribs& Attribs);
95109
void Render(IDeviceContext* pContext);
96110

97-
111+
private:
98112
struct PSOKey
99113
{
100-
const bool ConvertOutputToSRGB;
101-
const bool ComputeMotionVectors;
114+
const OPTION_FLAGS Flags;
102115

103-
PSOKey(bool _ConvertOutputToSRGB,
104-
bool _ComputeMotionVectors) :
105-
ConvertOutputToSRGB{_ConvertOutputToSRGB},
106-
ComputeMotionVectors{_ComputeMotionVectors}
116+
explicit PSOKey(OPTION_FLAGS _Flags) :
117+
Flags{_Flags}
107118
{}
108119

109120
constexpr bool operator==(const PSOKey& rhs) const
110121
{
111-
return (ConvertOutputToSRGB == rhs.ConvertOutputToSRGB &&
112-
ComputeMotionVectors == rhs.ComputeMotionVectors);
122+
return Flags == rhs.Flags;
113123
}
114124

115125
struct Hasher
116126
{
117127
size_t operator()(const PSOKey& Key) const
118128
{
119-
return ComputeHash(Key.ConvertOutputToSRGB);
129+
return ComputeHash(Key.Flags);
120130
}
121131
};
122132
};
123133
IPipelineState* GetPSO(const PSOKey& Key);
124134

125-
private:
126135
RefCntAutoPtr<IRenderDevice> m_pDevice;
127136
RefCntAutoPtr<IRenderStateCache> m_pStateCache;
128137
RefCntAutoPtr<IBuffer> m_pCameraAttribsCB;
@@ -143,5 +152,7 @@ class BoundBoxRenderer
143152
struct BoundBoxShaderAttribs;
144153
std::unique_ptr<BoundBoxShaderAttribs> m_ShaderAttribs;
145154
};
155+
DEFINE_FLAG_ENUM_OPERATORS(BoundBoxRenderer::OPTION_FLAGS);
156+
146157

147158
} // namespace Diligent

Components/src/BoundBoxRenderer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Diligent Graphics LLC
2+
* Copyright 2024-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.
@@ -114,8 +114,8 @@ IPipelineState* BoundBoxRenderer::GetPSO(const PSOKey& Key)
114114

115115
ShaderMacroHelper Macros;
116116
Macros
117-
.Add("CONVERT_OUTPUT_TO_SRGB", Key.ConvertOutputToSRGB)
118-
.Add("COMPUTE_MOTION_VECTORS", Key.ComputeMotionVectors);
117+
.Add("CONVERT_OUTPUT_TO_SRGB", (Key.Flags & OPTION_FLAG_CONVERT_OUTPUT_TO_SRGB) != 0)
118+
.Add("COMPUTE_MOTION_VECTORS", (Key.Flags & OPTION_FLAG_COMPUTE_MOTION_VECTORS) != 0);
119119
ShaderCI.Macros = Macros;
120120

121121
RefCntAutoPtr<IShader> pVS;
@@ -157,7 +157,7 @@ IPipelineState* BoundBoxRenderer::GetPSO(const PSOKey& Key)
157157

158158
PsoCI.PSODesc.ResourceLayout.DefaultVariableMergeStages = SHADER_TYPE_VS_PS;
159159
PsoCI.PSODesc.ResourceLayout.DefaultVariableType = SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE;
160-
PsoCI.GraphicsPipeline.DepthStencilDesc.DepthFunc = COMPARISON_FUNC_LESS_EQUAL;
160+
PsoCI.GraphicsPipeline.DepthStencilDesc.DepthFunc = (Key.Flags & OPTION_FLAG_USE_REVERSE_DEPTH) != 0 ? COMPARISON_FUNC_GREATER_EQUAL : COMPARISON_FUNC_LESS_EQUAL;
161161
PsoCI.GraphicsPipeline.DepthStencilDesc.DepthWriteEnable = false;
162162
if (m_AsyncShaders)
163163
PsoCI.Flags |= PSO_CREATE_FLAG_ASYNCHRONOUS;
@@ -175,7 +175,7 @@ IPipelineState* BoundBoxRenderer::GetPSO(const PSOKey& Key)
175175

176176
void BoundBoxRenderer::Prepare(IDeviceContext* pContext, const RenderAttribs& Attribs)
177177
{
178-
m_pCurrentPSO = GetPSO({Attribs.ConvertOutputToSRGB, Attribs.ComputeMotionVectors});
178+
m_pCurrentPSO = GetPSO(PSOKey{Attribs.Options});
179179
if (m_pCurrentPSO == nullptr)
180180
{
181181
UNEXPECTED("Failed to get PSO");

Hydrogent/src/Tasks/HnRenderBoundBoxTask.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 Diligent Graphics LLC
2+
* Copyright 2024-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.
@@ -194,11 +194,14 @@ void HnRenderBoundBoxTask::Prepare(pxr::HdTaskContext* TaskCtx,
194194
}
195195

196196
BoundBoxRenderer::RenderAttribs Attribs;
197-
Attribs.Color = &m_Params.Color;
198-
Attribs.BoundBoxTransform = &BoundBoxTransform;
199-
Attribs.PatternLength = m_Params.PatternLength;
200-
Attribs.PatternMask = m_Params.PatternMask;
201-
Attribs.ComputeMotionVectors = true;
197+
Attribs.Color = &m_Params.Color;
198+
Attribs.BoundBoxTransform = &BoundBoxTransform;
199+
Attribs.PatternLength = m_Params.PatternLength;
200+
Attribs.PatternMask = m_Params.PatternMask;
201+
Attribs.Options = BoundBoxRenderer::OPTION_FLAG_COMPUTE_MOTION_VECTORS;
202+
if (pRenderParam->GetConfig().UseReverseDepth)
203+
Attribs.Options |= BoundBoxRenderer::OPTION_FLAG_USE_REVERSE_DEPTH;
204+
202205
m_BoundBoxRenderer->Prepare(pRenderDelegate->GetDeviceContext(), Attribs);
203206

204207
m_RenderBoundBox = true;

0 commit comments

Comments
 (0)