Skip to content

Commit

Permalink
Hydrogent: code cleanup + comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 25, 2023
1 parent cdc3430 commit 93b3dd2
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 37 deletions.
3 changes: 2 additions & 1 deletion Hydrogent/interface/HnRenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ struct HnMeshRenderParams
float EmissionScale = 1;
float IBLScale = 1;

float4 WireframeColor = float4(1, 1, 1, 1);
float4 WireframeColor = float4{1, 1, 1, 1};
float4 PointColor = float4{1, 1, 1, 1};

float4x4 Transform = float4x4::Identity();

Expand Down
14 changes: 13 additions & 1 deletion Hydrogent/interface/Tasks/HnPostProcessTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct HnPostProcessTaskParams

float4 SelectionColor = float4{0.75f, 0.75f, 0.25f, 0.5f};

/// Desaturation factor for non-selected objects
float NonselectionDesaturationFactor = 0.0f;

// Tone mappig attribs
Expand Down Expand Up @@ -75,7 +76,18 @@ struct HnPostProcessTaskParams
}
};

/// Post processing task implementation in Hydrogent.
/// Performs post processing:
/// - Tone mapping
/// - Selection outline
/// - Converts output to sRGB, if needed
///
/// \remarks The task uses MeshId buffer to determine which pixels belong to selected objects.
/// The index of the selected objects is negative.
///
/// The task gets offscreen color and mesh id Bprim buffer ids from the task context.
/// It then retrieves the buffers from the render index.
/// The final color buffer is also retrieved from the render index using the FinalColorTargetId
/// extracted from the context.
class HnPostProcessTask final : public HnTask
{
public:
Expand Down
2 changes: 1 addition & 1 deletion Hydrogent/interface/Tasks/HnReadRprimIdTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct HnReadRprimIdTaskParams
}
};

/// Render task implementation in Hydrogent.
/// Reads RPrim index from the mesh id target.
class HnReadRprimIdTask final : public HnTask
{
public:
Expand Down
2 changes: 1 addition & 1 deletion Hydrogent/interface/Tasks/HnRenderEnvMapTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct HnRenderEnvMapTaskParams
}
};

/// Post processing task implementation in Hydrogent.
/// Renders the environment map.
class HnRenderEnvMapTask final : public HnTask
{
public:
Expand Down
7 changes: 6 additions & 1 deletion Hydrogent/interface/Tasks/HnRenderRprimsTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ struct HnRenderRprimsTaskParams
float IBLScale = 1;

float4 WireframeColor = float4(1, 1, 1, 1);
float4 PointColor = float4(1, 1, 1, 1);

float4x4 Transform = float4x4::Identity();

/// Selected prim id.
/// Selected rprim is rendered with negative index.
pxr::SdfPath SelectedPrimId;

constexpr bool operator==(const HnRenderRprimsTaskParams& rhs) const
Expand All @@ -74,7 +77,9 @@ struct HnRenderRprimsTaskParams
}
};

/// Render task implementation in Hydrogent.
/// Renders the Rprims by executing the render pass.
/// The task should be executed after the HnSetupRenderingTask that prepares
/// render targets and sets the required task context parameters.
class HnRenderRprimsTask final : public HnTask
{
public:
Expand Down
27 changes: 10 additions & 17 deletions Hydrogent/interface/Tasks/HnSetupRenderingTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,9 @@ struct HnSetupRenderingTaskParams
return ColorFormat == rhs.ColorFormat &&
MeshIdFormat == rhs.MeshIdFormat &&
DepthFormat == rhs.DepthFormat &&
RenderMode == rhs.RenderMode &&
FrontFaceCCW == rhs.FrontFaceCCW &&
DebugView == rhs.DebugView &&
OcclusionStrength == rhs.OcclusionStrength &&
EmissionScale == rhs.EmissionScale &&
IBLScale == rhs.IBLScale &&
ClearColor == rhs.ClearColor &&
ClearDepth == rhs.ClearDepth &&
Transform == rhs.Transform &&
DepthBias == rhs.DepthBias &&
SlopeScaledDepthBias == rhs.SlopeScaledDepthBias &&
DepthFunc == rhs.DepthFunc &&
Expand All @@ -86,20 +80,11 @@ struct HnSetupRenderingTaskParams
TEXTURE_FORMAT MeshIdFormat = TEX_FORMAT_UNKNOWN;
TEXTURE_FORMAT DepthFormat = TEX_FORMAT_UNKNOWN;

HN_RENDER_MODE RenderMode = HN_RENDER_MODE_SOLID;

bool FrontFaceCCW = false;

int DebugView = 0;
float OcclusionStrength = 1;
float EmissionScale = 1;
float IBLScale = 1;

float4 ClearColor;
float4 ClearColor = {0, 0, 0, 0};
float ClearDepth = 1.f;

float4x4 Transform = float4x4::Identity();

float DepthBias = 0;
float SlopeScaledDepthBias = 0;
pxr::HdCompareFunction DepthFunc = pxr::HdCmpFuncLess;
Expand All @@ -120,7 +105,15 @@ struct HnSetupRenderingTaskParams
pxr::SdfPath FinalColorTargetId;
};

/// Post processing task implementation in Hydrogent.
/// Sets up rendering state for subsequent tasks:
/// - Prepares color and mesh id render targets and depth buffer
/// - Retrieves final color Bprim from the render index using the FinalColorTargetId
/// - (Re)creates the render targets if necessary
/// - Inserts them into the render index as Bprims
/// - Passes Bprim Id to subsequent tasks via the task context
/// - Updates render pass state
/// - Updates task context with the render pass state so that subsequent tasks can use it
/// - Binds and clears render targets and depth buffer
class HnSetupRenderingTask final : public HnTask
{
public:
Expand Down
1 change: 1 addition & 0 deletions Hydrogent/shaders/HnPostProcess.psh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void main(in PSInput PSIn,
Color.rgb = ToneMap(Color.rgb, g_Attribs.ToneMapping, g_Attribs.AverageLogLum);
#endif

// Selected rprim is rendered with negative index
float IsSelected0 = g_MeshId.Load(int3(PSIn.Pos.xy + float2(-1.0, -1.0), 0)).r < 0.0 ? -1.0 : +1.0;
float IsSelected1 = g_MeshId.Load(int3(PSIn.Pos.xy + float2( 0.0, -1.0), 0)).r < 0.0 ? -1.0 : +1.0;
float IsSelected2 = g_MeshId.Load(int3(PSIn.Pos.xy + float2(+1.0, -1.0), 0)).r < 0.0 ? -1.0 : +1.0;
Expand Down
1 change: 1 addition & 0 deletions Hydrogent/shaders_inc/HnPostProcess.psh.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
" Color.rgb = ToneMap(Color.rgb, g_Attribs.ToneMapping, g_Attribs.AverageLogLum);\n"
"#endif\n"
"\n"
" // Selected rprim is rendered with negative index\n"
" float IsSelected0 = g_MeshId.Load(int3(PSIn.Pos.xy + float2(-1.0, -1.0), 0)).r < 0.0 ? -1.0 : +1.0;\n"
" float IsSelected1 = g_MeshId.Load(int3(PSIn.Pos.xy + float2( 0.0, -1.0), 0)).r < 0.0 ? -1.0 : +1.0;\n"
" float IsSelected2 = g_MeshId.Load(int3(PSIn.Pos.xy + float2(+1.0, -1.0), 0)).r < 0.0 ? -1.0 : +1.0;\n"
Expand Down
2 changes: 1 addition & 1 deletion Hydrogent/src/HnRenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void HnRenderPass::RenderMesh(RenderState& State,
RendererParams.IBLScale = m_Params.IBLScale;

RendererParams.PrefilteredCubeMipLevels = 5; //m_Settings.UseIBL ? static_cast<float>(m_pPrefilteredEnvMapSRV->GetTexture()->GetDesc().MipLevels) : 0.f;
RendererParams.WireframeColor = m_Params.WireframeColor;
RendererParams.WireframeColor = m_Params.RenderMode == HN_RENDER_MODE_POINTS ? m_Params.PointColor : m_Params.WireframeColor;
RendererParams.HighlightColor = float4{0, 0, 0, 0};

// Tone mapping is performed in the post-processing pass
Expand Down
3 changes: 0 additions & 3 deletions Hydrogent/src/HnRenderPassState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ void HnRenderPassState::Begin(IDeviceContext* pContext)
VERIFY(_stepSizeLighting == 0, "Step size lighting is not supported");

//GfVec4f _overrideColor;
//GfVec4f _wireframeColor;
//GfVec4f _pointColor;
//GfVec4f _maskColor;
//GfVec4f _indicatorColor;
//float _pointSelectedSize;
//GfVec2f _drawRange;

pContext->SetStencilRef(_stencilRef);
pContext->SetBlendFactors(_blendConstantColor.data());
Viewport VP{_viewport[0], _viewport[1], _viewport[2], _viewport[3]};
pContext->SetViewports(1, &VP, 0, 0);
Expand Down
4 changes: 4 additions & 0 deletions Hydrogent/src/Tasks/HnPostProcessTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ void HnPostProcessTask::Prepare(pxr::HdTaskContext* TaskCtx,
{
m_RenderIndex = RenderIndex;

// Final color Bprim is initialized by the HnSetupRenderingTask.
ITextureView* pFinalColorRTV = GetRenderBufferTarget(*RenderIndex, TaskCtx, HnRenderResourceTokens->finalColorTarget);
if (pFinalColorRTV == nullptr)
{
Expand All @@ -213,6 +214,9 @@ void HnPostProcessTask::Execute(pxr::HdTaskContext* TaskCtx)
UNEXPECTED("Render index is null. This likely indicates that Prepare() has not been called.");
return;
}

// Render target Bprims are initialized by the HnSetupRenderingTask.

ITextureView* pFinalColorRTV = GetRenderBufferTarget(*m_RenderIndex, TaskCtx, HnRenderResourceTokens->finalColorTarget);
if (pFinalColorRTV == nullptr)
{
Expand Down
3 changes: 3 additions & 0 deletions Hydrogent/src/Tasks/HnReadRprimIdTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ void HnReadRprimIdTask::Execute(pxr::HdTaskContext* TaskCtx)
UNEXPECTED("Mesh ID readback queue is null.");
return;
}

// Render target Bprims are initialized by the HnSetupRenderingTask.

ITextureView* pMeshIdRTV = GetRenderBufferTarget(*m_RenderIndex, TaskCtx, HnRenderResourceTokens->meshIdTarget);
if (pMeshIdRTV == nullptr)
{
Expand Down
15 changes: 5 additions & 10 deletions Hydrogent/src/Tasks/HnRenderRprimsTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void HnRenderRprimsTask::UpdateRenderPassParams(const HnRenderRprimsTaskParams&
RenderPassParams.EmissionScale = Params.EmissionScale;
RenderPassParams.IBLScale = Params.IBLScale;
RenderPassParams.WireframeColor = Params.WireframeColor;
RenderPassParams.PointColor = Params.PointColor;
RenderPassParams.Transform = Params.Transform;
RenderPassParams.SelectedPrimId = Params.SelectedPrimId;
static_cast<HnRenderPass*>(m_RenderPass.get())->SetMeshRenderParams(RenderPassParams);
Expand All @@ -74,9 +75,8 @@ void HnRenderRprimsTask::Sync(pxr::HdSceneDelegate* Delegate,

if (*DirtyBits & pxr::HdChangeTracker::DirtyCollection)
{
pxr::VtValue CollectionVal = Delegate->Get(GetId(), pxr::HdTokens->collection);

pxr::HdRprimCollection Collection = CollectionVal.Get<pxr::HdRprimCollection>();
pxr::VtValue CollectionVal = Delegate->Get(GetId(), pxr::HdTokens->collection);
pxr::HdRprimCollection Collection = CollectionVal.Get<pxr::HdRprimCollection>();

if (Collection.GetName().IsEmpty())
{
Expand All @@ -101,16 +101,11 @@ void HnRenderRprimsTask::Sync(pxr::HdSceneDelegate* Delegate,

if (*DirtyBits & pxr::HdChangeTracker::DirtyParams)
{
pxr::VtValue ParamsValue = Delegate->Get(GetId(), pxr::HdTokens->params);
if (ParamsValue.IsHolding<HnRenderRprimsTaskParams>())
HnRenderRprimsTaskParams Params;
if (GetTaskParams(Delegate, Params))
{
HnRenderRprimsTaskParams Params = ParamsValue.UncheckedGet<HnRenderRprimsTaskParams>();
UpdateRenderPassParams(Params);
}
else
{
UNEXPECTED("Unknown task parameters type: ", ParamsValue.GetTypeName());
}
}

if (*DirtyBits & pxr::HdChangeTracker::DirtyRenderTags)
Expand Down
3 changes: 2 additions & 1 deletion Hydrogent/src/Tasks/HnSetupRenderingTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void HnSetupRenderingTask::PrepareRenderTargets(pxr::HdRenderIndex* RenderIndex,
auto TargetDesc = FinalTargetDesc;
TargetDesc.Name = Name;
TargetDesc.Format = Format;
TargetDesc.BindFlags = (IsDepth ? BIND_DEPTH_STENCIL : BIND_RENDER_TARGET) | BIND_SHADER_RESOURCE;
TargetDesc.BindFlags = IsDepth ? BIND_DEPTH_STENCIL : (BIND_RENDER_TARGET | BIND_SHADER_RESOURCE);

RefCntAutoPtr<ITexture> pTarget;
pDevice->CreateTexture(TargetDesc, nullptr, &pTarget);
Expand Down Expand Up @@ -203,6 +203,7 @@ void HnSetupRenderingTask::Execute(pxr::HdTaskContext* TaskCtx)
constexpr float Zero[] = {0, 0, 0, 0};
pCtx->ClearRenderTarget(pRTVs[1], Zero, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
pCtx->ClearDepthStencil(m_pDepthDSV, CLEAR_DEPTH_FLAG, m_ClearDepth, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
pCtx->SetStencilRef(m_RenderPassState->GetStencilRef());
}

} // namespace USD
Expand Down

0 comments on commit 93b3dd2

Please sign in to comment.