From 93b3dd27eae26bbe88392d66b98b9a0b00506989 Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 24 Oct 2023 19:15:38 -0700 Subject: [PATCH] Hydrogent: code cleanup + comments --- Hydrogent/interface/HnRenderPass.hpp | 3 ++- .../interface/Tasks/HnPostProcessTask.hpp | 14 +++++++++- .../interface/Tasks/HnReadRprimIdTask.hpp | 2 +- .../interface/Tasks/HnRenderEnvMapTask.hpp | 2 +- .../interface/Tasks/HnRenderRprimsTask.hpp | 7 ++++- .../interface/Tasks/HnSetupRenderingTask.hpp | 27 +++++++------------ Hydrogent/shaders/HnPostProcess.psh | 1 + Hydrogent/shaders_inc/HnPostProcess.psh.h | 1 + Hydrogent/src/HnRenderPass.cpp | 2 +- Hydrogent/src/HnRenderPassState.cpp | 3 --- Hydrogent/src/Tasks/HnPostProcessTask.cpp | 4 +++ Hydrogent/src/Tasks/HnReadRprimIdTask.cpp | 3 +++ Hydrogent/src/Tasks/HnRenderRprimsTask.cpp | 15 ++++------- Hydrogent/src/Tasks/HnSetupRenderingTask.cpp | 3 ++- 14 files changed, 50 insertions(+), 37 deletions(-) diff --git a/Hydrogent/interface/HnRenderPass.hpp b/Hydrogent/interface/HnRenderPass.hpp index d1c65911..dc2d7610 100644 --- a/Hydrogent/interface/HnRenderPass.hpp +++ b/Hydrogent/interface/HnRenderPass.hpp @@ -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(); diff --git a/Hydrogent/interface/Tasks/HnPostProcessTask.hpp b/Hydrogent/interface/Tasks/HnPostProcessTask.hpp index 8b0e371d..f9100bb6 100644 --- a/Hydrogent/interface/Tasks/HnPostProcessTask.hpp +++ b/Hydrogent/interface/Tasks/HnPostProcessTask.hpp @@ -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 @@ -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: diff --git a/Hydrogent/interface/Tasks/HnReadRprimIdTask.hpp b/Hydrogent/interface/Tasks/HnReadRprimIdTask.hpp index 26f06b8f..644bebf7 100644 --- a/Hydrogent/interface/Tasks/HnReadRprimIdTask.hpp +++ b/Hydrogent/interface/Tasks/HnReadRprimIdTask.hpp @@ -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: diff --git a/Hydrogent/interface/Tasks/HnRenderEnvMapTask.hpp b/Hydrogent/interface/Tasks/HnRenderEnvMapTask.hpp index 6cbea680..a207441c 100644 --- a/Hydrogent/interface/Tasks/HnRenderEnvMapTask.hpp +++ b/Hydrogent/interface/Tasks/HnRenderEnvMapTask.hpp @@ -50,7 +50,7 @@ struct HnRenderEnvMapTaskParams } }; -/// Post processing task implementation in Hydrogent. +/// Renders the environment map. class HnRenderEnvMapTask final : public HnTask { public: diff --git a/Hydrogent/interface/Tasks/HnRenderRprimsTask.hpp b/Hydrogent/interface/Tasks/HnRenderRprimsTask.hpp index bcd32a86..d6392c32 100644 --- a/Hydrogent/interface/Tasks/HnRenderRprimsTask.hpp +++ b/Hydrogent/interface/Tasks/HnRenderRprimsTask.hpp @@ -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 @@ -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: diff --git a/Hydrogent/interface/Tasks/HnSetupRenderingTask.hpp b/Hydrogent/interface/Tasks/HnSetupRenderingTask.hpp index 12d8f262..aa52df89 100644 --- a/Hydrogent/interface/Tasks/HnSetupRenderingTask.hpp +++ b/Hydrogent/interface/Tasks/HnSetupRenderingTask.hpp @@ -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 && @@ -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; @@ -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: diff --git a/Hydrogent/shaders/HnPostProcess.psh b/Hydrogent/shaders/HnPostProcess.psh index 1b6fbc6a..ffe28b56 100644 --- a/Hydrogent/shaders/HnPostProcess.psh +++ b/Hydrogent/shaders/HnPostProcess.psh @@ -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; diff --git a/Hydrogent/shaders_inc/HnPostProcess.psh.h b/Hydrogent/shaders_inc/HnPostProcess.psh.h index 8a099c49..9cc43369 100644 --- a/Hydrogent/shaders_inc/HnPostProcess.psh.h +++ b/Hydrogent/shaders_inc/HnPostProcess.psh.h @@ -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" diff --git a/Hydrogent/src/HnRenderPass.cpp b/Hydrogent/src/HnRenderPass.cpp index 28694c93..cfd1fa8f 100644 --- a/Hydrogent/src/HnRenderPass.cpp +++ b/Hydrogent/src/HnRenderPass.cpp @@ -320,7 +320,7 @@ void HnRenderPass::RenderMesh(RenderState& State, RendererParams.IBLScale = m_Params.IBLScale; RendererParams.PrefilteredCubeMipLevels = 5; //m_Settings.UseIBL ? static_cast(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 diff --git a/Hydrogent/src/HnRenderPassState.cpp b/Hydrogent/src/HnRenderPassState.cpp index d349f47d..d7c3b19e 100644 --- a/Hydrogent/src/HnRenderPassState.cpp +++ b/Hydrogent/src/HnRenderPassState.cpp @@ -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); diff --git a/Hydrogent/src/Tasks/HnPostProcessTask.cpp b/Hydrogent/src/Tasks/HnPostProcessTask.cpp index 77a54d62..caa8a910 100644 --- a/Hydrogent/src/Tasks/HnPostProcessTask.cpp +++ b/Hydrogent/src/Tasks/HnPostProcessTask.cpp @@ -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) { @@ -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) { diff --git a/Hydrogent/src/Tasks/HnReadRprimIdTask.cpp b/Hydrogent/src/Tasks/HnReadRprimIdTask.cpp index 4fe2b8e8..08cc34e9 100644 --- a/Hydrogent/src/Tasks/HnReadRprimIdTask.cpp +++ b/Hydrogent/src/Tasks/HnReadRprimIdTask.cpp @@ -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) { diff --git a/Hydrogent/src/Tasks/HnRenderRprimsTask.cpp b/Hydrogent/src/Tasks/HnRenderRprimsTask.cpp index 2750fa38..bb5acf16 100644 --- a/Hydrogent/src/Tasks/HnRenderRprimsTask.cpp +++ b/Hydrogent/src/Tasks/HnRenderRprimsTask.cpp @@ -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(m_RenderPass.get())->SetMeshRenderParams(RenderPassParams); @@ -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::VtValue CollectionVal = Delegate->Get(GetId(), pxr::HdTokens->collection); + pxr::HdRprimCollection Collection = CollectionVal.Get(); if (Collection.GetName().IsEmpty()) { @@ -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 Params; + if (GetTaskParams(Delegate, Params)) { - HnRenderRprimsTaskParams Params = ParamsValue.UncheckedGet(); UpdateRenderPassParams(Params); } - else - { - UNEXPECTED("Unknown task parameters type: ", ParamsValue.GetTypeName()); - } } if (*DirtyBits & pxr::HdChangeTracker::DirtyRenderTags) diff --git a/Hydrogent/src/Tasks/HnSetupRenderingTask.cpp b/Hydrogent/src/Tasks/HnSetupRenderingTask.cpp index 189faebe..681dd50c 100644 --- a/Hydrogent/src/Tasks/HnSetupRenderingTask.cpp +++ b/Hydrogent/src/Tasks/HnSetupRenderingTask.cpp @@ -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 pTarget; pDevice->CreateTexture(TargetDesc, nullptr, &pTarget); @@ -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