Skip to content

Commit

Permalink
HnTaskController: improved render task processing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 19, 2023
1 parent 6b6f45b commit dfee169
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
17 changes: 14 additions & 3 deletions Hydrogent/include/Tasks/HnTaskController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <memory>
#include <unordered_map>
#include <vector>

#include "Tasks/HnTask.hpp"

Expand Down Expand Up @@ -63,6 +64,15 @@ class HnTaskController

const pxr::SdfPath& GetControllerId() const { return m_ControllerId; }

/// Returns the task list that can be passed to the Hydra engine for execution.
///
/// \param [in] TaskOrder - Optional task order. If not specified, the default order is used:
/// - RenderDefault
/// - RenderMasked
/// - RenderAdditive
/// - RenderTranslucent
/// - PostProcess
/// \return The task list that can be passed to pxr::HdEngine::Execute.
const pxr::HdTaskSharedPtrVector GetTasks(const std::vector<TaskUID>* TaskOrder = nullptr) const;

/// Sets new collection for the render tasks.
Expand Down Expand Up @@ -102,17 +112,18 @@ class HnTaskController
class TaskParamsDelegate;
std::unique_ptr<TaskParamsDelegate> m_ParamsDelegate;

std::unordered_map<TaskUID, pxr::SdfPath> m_TaskIds;
std::unordered_map<TaskUID, pxr::SdfPath> m_TaskUIDs;

std::vector<TaskUID> m_DefaultTaskOrder;
std::vector<TaskUID> m_DefaultTaskOrder;
std::vector<pxr::SdfPath> m_RenderTaskIds;
};

template <typename TaskType>
void HnTaskController::CreateTask(const pxr::SdfPath& TaskId,
TaskUID UID)
{
m_RenderIndex.InsertTask<HnRenderTask>(m_ParamsDelegate.get(), TaskId);
auto it_inserted = m_TaskIds.emplace(UID, TaskId);
auto it_inserted = m_TaskUIDs.emplace(UID, TaskId);
VERIFY(!it_inserted.second, "Task with UID ", UID, " already exists: ", it_inserted.first->second.GetText());
}

Expand Down
2 changes: 1 addition & 1 deletion Hydrogent/src/HnRendererImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void HnRendererImpl::Update()
if (m_ImagingDelegate)
{
m_ImagingDelegate->ApplyPendingUpdates();
pxr::HdTaskSharedPtrVector tasks = m_TaskController->GetRenderingTasks();
pxr::HdTaskSharedPtrVector tasks = m_TaskController->GetTasks();
tasks.push_back(std::make_shared<SyncTask>(m_GeometryPass, m_RenderTags));
m_Engine.Execute(&m_ImagingDelegate->GetRenderIndex(), &tasks);
}
Expand Down
57 changes: 17 additions & 40 deletions Hydrogent/src/Tasks/HnTaskController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ TF_DEFINE_PRIVATE_TOKENS(
);
// clang-format on

static constexpr std::array<HnTaskController::TaskUID, 4> RenderTaskUIDs = {
HnTaskController::TaskUID_RenderDefault,
HnTaskController::TaskUID_RenderMasked,
HnTaskController::TaskUID_RenderAdditive,
HnTaskController::TaskUID_RenderTranslucent,
};

} // namespace

class HnTaskController::TaskParamsDelegate final : public pxr::HdSceneDelegate
Expand Down Expand Up @@ -194,29 +187,29 @@ HnTaskController::HnTaskController(pxr::HdRenderIndex& RenderIndex,
HnTaskController::~HnTaskController()
{
// Remove all tasks from the render index
for (const auto& it : m_TaskIds)
for (const auto& it : m_TaskUIDs)
{
m_RenderIndex.RemoveTask(it.second);
}
m_TaskIds.clear();
m_TaskUIDs.clear();
}

pxr::HdTaskSharedPtr HnTaskController::GetTask(TaskUID UID) const
{
auto it = m_TaskIds.find(UID);
return it == m_TaskIds.end() ?
auto it = m_TaskUIDs.find(UID);
return it == m_TaskUIDs.end() ?
m_RenderIndex.GetTask(it->second) :
nullptr;
}

void HnTaskController::RemoveTask(TaskUID UID)
{
auto it = m_TaskIds.find(UID);
if (it == m_TaskIds.end())
auto it = m_TaskUIDs.find(UID);
if (it == m_TaskUIDs.end())
return;

m_RenderIndex.RemoveTask(it->second);
m_TaskIds.erase(it);
m_TaskUIDs.erase(it);
}

pxr::SdfPath HnTaskController::GetRenderTaskId(const pxr::TfToken& MaterialTag) const
Expand All @@ -233,7 +226,7 @@ void HnTaskController::SetParameter(const pxr::SdfPath& Id, const TfToken& Value

void HnTaskController::CreateRenderTask(const pxr::TfToken& MaterialTag, TaskUID UID)
{
const pxr::SdfPath RenderTaskId = GetRenderTaskId(MaterialTag);
pxr::SdfPath RenderTaskId = GetRenderTaskId(MaterialTag);
// Note that we pass the delegate to the scene index. This delegate will be passed
// to the task's Sync() method.
CreateTask<HnRenderTask>(RenderTaskId, UID);
Expand All @@ -253,6 +246,8 @@ void HnTaskController::CreateRenderTask(const pxr::TfToken& MaterialTag, TaskUID
m_ParamsDelegate->SetParameter(RenderTaskId, pxr::HdTokens->params, TaskParams);
m_ParamsDelegate->SetParameter(RenderTaskId, pxr::HdTokens->collection, Collection);
m_ParamsDelegate->SetParameter(RenderTaskId, pxr::HdTokens->renderTags, RenderTags);

m_RenderTaskIds.emplace_back(std::move(RenderTaskId));
}

void HnTaskController::CreatePostProcessTask()
Expand All @@ -272,8 +267,8 @@ const pxr::HdTaskSharedPtrVector HnTaskController::GetTasks(const std::vector<Ta
pxr::HdTaskSharedPtrVector Tasks;
for (auto UID : *TaskOrder)
{
auto it = m_TaskIds.find(UID);
if (it == m_TaskIds.end())
auto it = m_TaskUIDs.find(UID);
if (it == m_TaskUIDs.end())
continue;

Tasks.push_back(m_RenderIndex.GetTask(it->second));
Expand All @@ -285,14 +280,8 @@ const pxr::HdTaskSharedPtrVector HnTaskController::GetTasks(const std::vector<Ta
void HnTaskController::SetCollection(const pxr::HdRprimCollection& Collection)
{
pxr::HdRprimCollection NewCollection = Collection;
for (const auto UID : RenderTaskUIDs)
for (const auto& TaskId : m_RenderTaskIds)
{
auto it = m_TaskIds.find(UID);
if (it == m_TaskIds.end())
continue;

const auto& TaskId = it->second;

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

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

void HnTaskController::SetRenderParams(const HnRenderTaskParams& Params)
{
for (const auto UID : RenderTaskUIDs)
for (const auto& TaskId : m_RenderTaskIds)
{
auto it = m_TaskIds.find(UID);
if (it == m_TaskIds.end())
continue;

const auto& TaskId = it->second;

HnRenderTaskParams OldParams = m_ParamsDelegate->GetParameter<HnRenderTaskParams>(TaskId, pxr::HdTokens->params);
if (OldParams == Params)
continue;
Expand All @@ -327,8 +310,8 @@ void HnTaskController::SetRenderParams(const HnRenderTaskParams& Params)

void HnTaskController::SetPostProcessParams(const HnPostProcessTaskParams& Params)
{
const auto it = m_TaskIds.find(TaskUID_PostProcess);
if (it == m_TaskIds.end())
const auto it = m_TaskUIDs.find(TaskUID_PostProcess);
if (it == m_TaskUIDs.end())
return;

const auto& TaskId = it->second;
Expand All @@ -343,14 +326,8 @@ void HnTaskController::SetPostProcessParams(const HnPostProcessTaskParams& Param

void HnTaskController::SetRenderTags(const pxr::TfTokenVector& RenderTags)
{
for (const auto UID : RenderTaskUIDs)
for (const auto& TaskId : m_RenderTaskIds)
{
auto it = m_TaskIds.find(UID);
if (it == m_TaskIds.end())
continue;

const auto& TaskId = it->second;

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

0 comments on commit dfee169

Please sign in to comment.