Skip to content

Commit

Permalink
Hydrogent: moved run-time parameters from setup rendering task to ren…
Browse files Browse the repository at this point in the history
…der Rprims task
  • Loading branch information
TheMostDiligent committed Oct 23, 2023
1 parent 0137d25 commit fe3bd10
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 91 deletions.
23 changes: 23 additions & 0 deletions Hydrogent/interface/HnRenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "pxr/imaging/hd/types.h"
#include "pxr/imaging/hd/renderPass.h"

#include "HnTypes.hpp"

namespace Diligent
{

Expand All @@ -38,6 +40,20 @@ namespace USD
class HnMesh;
class HnMaterial;

struct HnMeshRenderParams
{
HN_RENDER_MODE RenderMode = HN_RENDER_MODE_SOLID;

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

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

float4x4 Transform = float4x4::Identity();
};

/// Hydra render pass implementation in Hydrogent.
class HnRenderPass final : public pxr::HdRenderPass
{
Expand All @@ -48,6 +64,11 @@ class HnRenderPass final : public pxr::HdRenderPass
HnRenderPass(pxr::HdRenderIndex* pIndex,
const pxr::HdRprimCollection& Collection);

void SetMeshRenderParams(const HnMeshRenderParams& Params)
{
m_Params = Params;
}

protected:
// Virtual API: Execute the buckets corresponding to renderTags;
// renderTags.empty() implies execute everything.
Expand All @@ -65,6 +86,8 @@ class HnRenderPass final : public pxr::HdRenderPass
const HnMaterial& Material);

private:
HnMeshRenderParams m_Params;

pxr::HdRenderIndex::HdDrawItemPtrVector m_DrawItems;

unsigned int m_CollectionVersion = ~0u;
Expand Down
63 changes: 0 additions & 63 deletions Hydrogent/interface/HnRenderPassState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,6 @@ class HnRenderPassState final : public pxr::HdRenderPassState
m_NumRenderTargets = NumRTs;
}

void SetRenderMode(HN_RENDER_MODE RenderMode)
{
m_RenderMode = RenderMode;
}
HN_RENDER_MODE GetRenderMode() const
{
return m_RenderMode;
}

void SetFrontFaceCCW(bool FrontFaceCCW)
{
m_FrontFaceCCW = FrontFaceCCW;
Expand All @@ -86,51 +77,6 @@ class HnRenderPassState final : public pxr::HdRenderPassState
return m_FrontFaceCCW;
}

void SetDebugView(int DebugView)
{
m_DebugView = DebugView;
}
int GetDebugView() const
{
return m_DebugView;
}

void SetOcclusionStrength(float OcclusionStrength)
{
m_OcclusionStrength = OcclusionStrength;
}
float GetOcclusionStrength() const
{
return m_OcclusionStrength;
}

void SetEmissionScale(float EmissionScale)
{
m_EmissionScale = EmissionScale;
}
float GetEmissionScale() const
{
return m_EmissionScale;
}

void SetIBLScale(float IBLScale)
{
m_IBLScale = IBLScale;
}
float GetIBLScale() const
{
return m_IBLScale;
}

void SetTransform(const float4x4& Transform)
{
m_Transform = Transform;
}
const float4x4& GetTransform() const
{
return m_Transform;
}

Uint32 GetNumRenderTargets() const
{
return m_NumRenderTargets;
Expand All @@ -154,16 +100,7 @@ class HnRenderPassState final : public pxr::HdRenderPassState
std::array<TEXTURE_FORMAT, MAX_RENDER_TARGETS> m_RTVFormats = {};
TEXTURE_FORMAT m_DepthFormat = TEX_FORMAT_UNKNOWN;

HN_RENDER_MODE m_RenderMode = HN_RENDER_MODE_SOLID;

bool m_FrontFaceCCW = false;

int m_DebugView = 0;
float m_OcclusionStrength = 1;
float m_EmissionScale = 1;
float m_IBLScale = 1;

float4x4 m_Transform = float4x4::Identity();
};

} // namespace USD
Expand Down
28 changes: 27 additions & 1 deletion Hydrogent/interface/Tasks/HnRenderRprimsTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#pragma once

#include "HnTask.hpp"
#include "HnTypes.hpp"

#include "../../../Common/interface/BasicMath.hpp"

#include "pxr/imaging/hd/renderPass.h"

Expand All @@ -38,10 +41,30 @@ namespace USD

struct HnRenderRprimsTaskParams
{
HN_RENDER_MODE RenderMode = HN_RENDER_MODE_SOLID;

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

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

float4x4 Transform = float4x4::Identity();

constexpr bool operator==(const HnRenderRprimsTaskParams& rhs) const
{
return true;
// clang-format off
return RenderMode == rhs.RenderMode &&
DebugView == rhs.DebugView &&
OcclusionStrength == rhs.OcclusionStrength &&
EmissionScale == rhs.EmissionScale &&
IBLScale == rhs.IBLScale &&
WireframeColor == rhs.WireframeColor &&
Transform == rhs.Transform;
// clang-format on
}

constexpr bool operator!=(const HnRenderRprimsTaskParams& rhs) const
{
return !(*this == rhs);
Expand Down Expand Up @@ -70,6 +93,9 @@ class HnRenderRprimsTask final : public HnTask
return m_RenderTags;
}

private:
void UpdateRenderPassParams(const HnRenderRprimsTaskParams& Params);

private:
pxr::TfTokenVector m_RenderTags;
pxr::HdRenderPassSharedPtr m_RenderPass;
Expand Down
33 changes: 14 additions & 19 deletions Hydrogent/src/HnRenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ struct HnRenderPass::RenderState
IDeviceContext* const pCtx;
IBuffer* const pPBRAttribsCB;

const HnRenderPassState& RPState;
const PBR_Renderer::ALPHA_MODE AlphaMode;

USD_Renderer::PbrPsoCacheAccessor PbrPSOCache;
Expand Down Expand Up @@ -96,19 +95,17 @@ void HnRenderPass::_Execute(const pxr::HdRenderPassStateSharedPtr& RPState,
RenderState State{
pRenderDelegate->GetDeviceContext(),
USDRenderer->GetPBRAttribsCB(),
static_cast<const HnRenderPassState&>(*RPState),
MaterialTagToPbrAlphaMode(m_MaterialTag),
};

GraphicsPipelineDesc GraphicsDesc = State.RPState.GetGraphicsPipelineDesc();
const HN_RENDER_MODE RenderMode = State.RPState.GetRenderMode();
if (RenderMode == HN_RENDER_MODE_SOLID)
GraphicsPipelineDesc GraphicsDesc = static_cast<const HnRenderPassState*>(RPState.get())->GetGraphicsPipelineDesc();
if (m_Params.RenderMode == HN_RENDER_MODE_SOLID)
{
GraphicsDesc.PrimitiveTopology = PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
State.PbrPSOCache = USDRenderer->GetPbrPsoCacheAccessor(GraphicsDesc);
VERIFY_EXPR(State.PbrPSOCache);
}
else if (RenderMode == HN_RENDER_MODE_MESH_EDGES)
else if (m_Params.RenderMode == HN_RENDER_MODE_MESH_EDGES)
{
GraphicsDesc.PrimitiveTopology = PRIMITIVE_TOPOLOGY_LINE_LIST;
State.WireframePSOCache = USDRenderer->GetWireframePsoCacheAccessor(GraphicsDesc);
Expand Down Expand Up @@ -224,9 +221,7 @@ void HnRenderPass::RenderMesh(RenderState& State,
}
}

const auto RenderMode = State.RPState.GetRenderMode();

auto* pIB = (RenderMode == HN_RENDER_MODE_MESH_EDGES) ?
auto* pIB = (m_Params.RenderMode == HN_RENDER_MODE_MESH_EDGES) ?
Mesh.GetEdgeIndexBuffer() :
Mesh.GetTriangleIndexBuffer();

Expand All @@ -236,7 +231,7 @@ void HnRenderPass::RenderMesh(RenderState& State,
auto PSOFlags = PBR_Renderer::PSO_FLAG_ENABLE_CUSTOM_DATA_OUTPUT;

IPipelineState* pPSO = nullptr;
if (RenderMode == HN_RENDER_MODE_SOLID)
if (m_Params.RenderMode == HN_RENDER_MODE_SOLID)
{
if (pNormalsVB != nullptr)
PSOFlags |= PBR_Renderer::PSO_FLAG_USE_VERTEX_NORMALS;
Expand All @@ -260,7 +255,7 @@ void HnRenderPass::RenderMesh(RenderState& State,
"This may indicate an issue in how alpha mode is determined in the material, or (less likely) an issue in Rprim sorting by Hydra.");
pPSO = State.PbrPSOCache.Get({PSOFlags, static_cast<PBR_Renderer::ALPHA_MODE>(State.AlphaMode), /*DoubleSided = */ false}, true);
}
else if (RenderMode == HN_RENDER_MODE_MESH_EDGES)
else if (m_Params.RenderMode == HN_RENDER_MODE_MESH_EDGES)
{
pPSO = State.WireframePSOCache.Get({PSOFlags, /*DoubleSided = */ false}, true);
}
Expand All @@ -284,21 +279,21 @@ void HnRenderPass::RenderMesh(RenderState& State,
{
MapHelper<HLSL::PBRShaderAttribs> pDstShaderAttribs{State.pCtx, State.pPBRAttribsCB, MAP_WRITE, MAP_FLAG_DISCARD};

pDstShaderAttribs->Transforms.NodeMatrix = Mesh.GetTransform() * State.RPState.GetTransform();
pDstShaderAttribs->Transforms.NodeMatrix = Mesh.GetTransform() * m_Params.Transform;
pDstShaderAttribs->Transforms.JointCount = 0;

static_assert(sizeof(pDstShaderAttribs->Material) == sizeof(ShaderAttribs), "The sizeof(PBRMaterialShaderInfo) is inconsistent with sizeof(ShaderAttribs)");
memcpy(&pDstShaderAttribs->Material, &ShaderAttribs, sizeof(ShaderAttribs));

auto& RendererParams = pDstShaderAttribs->Renderer;

RendererParams.DebugViewType = State.RPState.GetDebugView();
RendererParams.OcclusionStrength = State.RPState.GetOcclusionStrength();
RendererParams.EmissionScale = State.RPState.GetEmissionScale();
RendererParams.IBLScale = State.RPState.GetIBLScale();
RendererParams.DebugViewType = m_Params.DebugView;
RendererParams.OcclusionStrength = m_Params.OcclusionStrength;
RendererParams.EmissionScale = m_Params.EmissionScale;
RendererParams.IBLScale = m_Params.IBLScale;

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

// Tone mapping is performed in the post-processing pass
Expand All @@ -313,7 +308,7 @@ void HnRenderPass::RenderMesh(RenderState& State,
}

State.pCtx->CommitShaderResources(pSRB, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
DrawIndexedAttribs DrawAttrs = (RenderMode == HN_RENDER_MODE_MESH_EDGES) ?
DrawIndexedAttribs DrawAttrs = (m_Params.RenderMode == HN_RENDER_MODE_MESH_EDGES) ?
DrawIndexedAttribs{Mesh.GetNumEdges() * 2, VT_UINT32, DRAW_FLAG_VERIFY_ALL} :
DrawIndexedAttribs{Mesh.GetNumTriangles() * 3, VT_UINT32, DRAW_FLAG_VERIFY_ALL};

Expand Down
21 changes: 20 additions & 1 deletion Hydrogent/src/Tasks/HnRenderRprimsTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Tasks/HnRenderRprimsTask.hpp"

#include "HnRenderPassState.hpp"
#include "HnRenderPass.hpp"
#include "HnTokens.hpp"

#include "pxr/imaging/hd/renderDelegate.h"
Expand All @@ -48,6 +49,22 @@ HnRenderRprimsTask::~HnRenderRprimsTask()
{
}

void HnRenderRprimsTask::UpdateRenderPassParams(const HnRenderRprimsTaskParams& Params)
{
if (!m_RenderPass)
return;

HnMeshRenderParams RenderPassParams;
RenderPassParams.RenderMode = Params.RenderMode;
RenderPassParams.DebugView = Params.DebugView;
RenderPassParams.OcclusionStrength = Params.OcclusionStrength;
RenderPassParams.EmissionScale = Params.EmissionScale;
RenderPassParams.IBLScale = Params.IBLScale;
RenderPassParams.WireframeColor = Params.WireframeColor;
RenderPassParams.Transform = Params.Transform;
static_cast<HnRenderPass*>(m_RenderPass.get())->SetMeshRenderParams(RenderPassParams);
}

void HnRenderRprimsTask::Sync(pxr::HdSceneDelegate* Delegate,
pxr::HdTaskContext* TaskCtx,
pxr::HdDirtyBits* DirtyBits)
Expand All @@ -71,6 +88,8 @@ void HnRenderRprimsTask::Sync(pxr::HdSceneDelegate* Delegate,
pxr::HdRenderIndex& Index = Delegate->GetRenderIndex();
pxr::HdRenderDelegate* RenderDelegate = Index.GetRenderDelegate();
m_RenderPass = RenderDelegate->CreateRenderPass(&Index, Collection);
// Need to set params for the new render pass.
*DirtyBits |= pxr::HdChangeTracker::DirtyParams;
}
else
{
Expand All @@ -85,7 +104,7 @@ void HnRenderRprimsTask::Sync(pxr::HdSceneDelegate* Delegate,
if (ParamsValue.IsHolding<HnRenderRprimsTaskParams>())
{
HnRenderRprimsTaskParams Params = ParamsValue.UncheckedGet<HnRenderRprimsTaskParams>();
(void)Params;
UpdateRenderPassParams(Params);
}
else
{
Expand Down
7 changes: 0 additions & 7 deletions Hydrogent/src/Tasks/HnSetupRenderingTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ HnSetupRenderingTask::~HnSetupRenderingTask()

void HnSetupRenderingTask::UpdateRenderPassState(const HnSetupRenderingTaskParams& Params)
{
m_RenderPassState->SetRenderMode(Params.RenderMode);
m_RenderPassState->SetDebugView(Params.DebugView);
m_RenderPassState->SetOcclusionStrength(Params.OcclusionStrength);
m_RenderPassState->SetEmissionScale(Params.EmissionScale);
m_RenderPassState->SetIBLScale(Params.IBLScale);
m_RenderPassState->SetTransform(Params.Transform);

VERIFY_EXPR(Params.ColorFormat != TEX_FORMAT_UNKNOWN);
m_RenderPassState->SetNumRenderTargets(Params.MeshIdFormat != TEX_FORMAT_UNKNOWN ? 2 : 1);
m_RenderPassState->SetRenderTargetFormat(0, Params.ColorFormat);
Expand Down
2 changes: 2 additions & 0 deletions Hydrogent/src/Tasks/HnTaskManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ void HnTaskManager::CreateRenderRprimsTask(const pxr::TfToken& MaterialTag, Task
m_ParamsDelegate.SetParameter(RenderRprimsTaskId, pxr::HdTokens->params, TaskParams);
m_ParamsDelegate.SetParameter(RenderRprimsTaskId, pxr::HdTokens->collection, Collection);
m_ParamsDelegate.SetParameter(RenderRprimsTaskId, pxr::HdTokens->renderTags, RenderTags);

m_RenderTaskIds.emplace_back(RenderRprimsTaskId);
}

void HnTaskManager::SetRenderRprimParams(const HnRenderRprimsTaskParams& Params)
Expand Down

0 comments on commit fe3bd10

Please sign in to comment.