Skip to content

Commit

Permalink
HnTaskController: unified task processing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 19, 2023
1 parent 8ef9ef5 commit 71f53dc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
18 changes: 11 additions & 7 deletions Hydrogent/include/Tasks/HnTaskController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ class HnTaskController
/// Sets new render tags for the render tasks.
void SetRenderTags(const pxr::TfTokenVector& RenderTags);

enum TASK_ID
{
TASK_ID_RENDER_DEFAULT,
TASK_ID_RENDER_MASKED,
TASK_ID_RENDER_ADDITIVE,
TASK_ID_RENDER_TRANSLUCENT,
TASK_ID_POST_PROCESS,
TASK_ID_COUNT
};
pxr::HdTaskSharedPtr GetTask(TASK_ID idx) const;

private:
pxr::SdfPath GetRenderTaskId(const pxr::TfToken& MaterialTag) const;
pxr::SdfPath CreateRenderTask(const pxr::TfToken& MaterialTag);
Expand All @@ -81,13 +92,6 @@ class HnTaskController
class TaskParamsDelegate;
std::unique_ptr<TaskParamsDelegate> m_ParamsDelegate;

pxr::SdfPathVector m_RenderTaskIds;

enum TASK_ID
{
TASK_ID_POST_PROCESS,
TASK_ID_COUNT
};
std::array<pxr::SdfPath, TASK_ID_COUNT> m_TaskIds;
};

Expand Down
59 changes: 37 additions & 22 deletions Hydrogent/src/Tasks/HnTaskController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ TF_DEFINE_PRIVATE_TOKENS(
);
// clang-format on

static constexpr std::array<HnTaskController::TASK_ID, 4> RenderTaskIds = {
HnTaskController::TASK_ID_RENDER_DEFAULT,
HnTaskController::TASK_ID_RENDER_MASKED,
HnTaskController::TASK_ID_RENDER_ADDITIVE,
HnTaskController::TASK_ID_RENDER_TRANSLUCENT,
};

} // namespace

class HnTaskController::TaskParamsDelegate final : public pxr::HdSceneDelegate
Expand Down Expand Up @@ -166,25 +173,17 @@ HnTaskController::HnTaskController(pxr::HdRenderIndex& RenderIndex,
m_ControllerId{ControllerId},
m_ParamsDelegate{std::make_unique<TaskParamsDelegate>(RenderIndex, ControllerId)}
{
for (const auto& MaterialTag : {HnMaterialTagTokens->defaultTag,
HnMaterialTagTokens->masked,
HnMaterialTagTokens->additive,
HnMaterialTagTokens->translucent})
{
m_RenderTaskIds.push_back(CreateRenderTask(MaterialTag));
}
m_TaskIds[TASK_ID_RENDER_DEFAULT] = CreateRenderTask(HnMaterialTagTokens->defaultTag);
m_TaskIds[TASK_ID_RENDER_MASKED] = CreateRenderTask(HnMaterialTagTokens->masked);
m_TaskIds[TASK_ID_RENDER_ADDITIVE] = CreateRenderTask(HnMaterialTagTokens->additive);
m_TaskIds[TASK_ID_RENDER_TRANSLUCENT] = CreateRenderTask(HnMaterialTagTokens->translucent);

m_TaskIds[TASK_ID_POST_PROCESS] = CreatePostProcessTask();
}

HnTaskController::~HnTaskController()
{
// Remove all tasks from the render index
for (const auto& Id : m_RenderTaskIds)
{
m_RenderIndex.RemoveTask(Id);
}

for (const auto& Id : m_TaskIds)
{
if (!Id.IsEmpty())
Expand All @@ -194,6 +193,12 @@ HnTaskController::~HnTaskController()
}
}

pxr::HdTaskSharedPtr HnTaskController::GetTask(TASK_ID idx) const
{
const auto& TaskId = m_TaskIds[idx];
return !TaskId.IsEmpty() ? m_RenderIndex.GetTask(TaskId) : nullptr;
}

pxr::SdfPath HnTaskController::GetRenderTaskId(const pxr::TfToken& MaterialTag) const
{
std::string Id = std::string{"renderTask_"} + MaterialTag.GetString();
Expand Down Expand Up @@ -241,13 +246,11 @@ pxr::SdfPath HnTaskController::CreatePostProcessTask()
const pxr::HdTaskSharedPtrVector HnTaskController::GetRenderingTasks() const
{
pxr::HdTaskSharedPtrVector Tasks;

for (const pxr::SdfPath& Id : m_RenderTaskIds)
{
Tasks.push_back(m_RenderIndex.GetTask(Id));
}

for (auto idx : {TASK_ID_POST_PROCESS})
for (auto idx : {TASK_ID_RENDER_DEFAULT,
TASK_ID_RENDER_MASKED,
TASK_ID_RENDER_ADDITIVE,
TASK_ID_RENDER_TRANSLUCENT,
TASK_ID_POST_PROCESS})
{
if (!m_TaskIds[idx].IsEmpty())
{
Expand All @@ -261,8 +264,12 @@ const pxr::HdTaskSharedPtrVector HnTaskController::GetRenderingTasks() const
void HnTaskController::SetCollection(const pxr::HdRprimCollection& Collection)
{
pxr::HdRprimCollection NewCollection = Collection;
for (const pxr::SdfPath& TaskId : m_RenderTaskIds)
for (const auto idx : RenderTaskIds)
{
const auto& TaskId = m_TaskIds[idx];
if (TaskId.IsEmpty())
continue;

pxr::HdRprimCollection OldCollection = m_ParamsDelegate->GetParameter<pxr::HdRprimCollection>(TaskId, pxr::HdTokens->collection);

const pxr::TfToken& OldMaterialTag = OldCollection.GetMaterialTag();
Expand All @@ -278,8 +285,12 @@ void HnTaskController::SetCollection(const pxr::HdRprimCollection& Collection)

void HnTaskController::SetRenderParams(const HnRenderTaskParams& Params)
{
for (const pxr::SdfPath& TaskId : m_RenderTaskIds)
for (const auto idx : RenderTaskIds)
{
const auto& TaskId = m_TaskIds[idx];
if (TaskId.IsEmpty())
continue;

HnRenderTaskParams OldParams = m_ParamsDelegate->GetParameter<HnRenderTaskParams>(TaskId, pxr::HdTokens->params);
if (OldParams == Params)
continue;
Expand All @@ -305,8 +316,12 @@ void HnTaskController::SetPostProcessParams(const HnPostProcessTaskParams& Param

void HnTaskController::SetRenderTags(const pxr::TfTokenVector& RenderTags)
{
for (const pxr::SdfPath& TaskId : m_RenderTaskIds)
for (const auto idx : RenderTaskIds)
{
const auto& TaskId = m_TaskIds[idx];
if (TaskId.IsEmpty())
continue;

pxr::TfTokenVector OldRenderTags = m_ParamsDelegate->GetParameter<pxr::TfTokenVector>(TaskId, pxr::HdTokens->renderTags);
if (OldRenderTags == RenderTags)
continue;
Expand Down

0 comments on commit 71f53dc

Please sign in to comment.