Skip to content

Commit

Permalink
Hydrogent: added option to enable/disable tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 24, 2023
1 parent b0ab97f commit 9988539
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
41 changes: 28 additions & 13 deletions Hydrogent/interface/Tasks/HnTaskManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class HnTaskManager

const pxr::SdfPath& GetId() const { return m_ManagerId; }

/// Returns the task list that can be passed to the Hydra engine for execution.
/// Returns the list of tasks that can be passed to the Hydra engine for execution.
///
/// \param [in] TaskOrder - Optional task order. If not specified, the default order is used:
/// - SetupRendering
Expand All @@ -78,7 +78,9 @@ class HnTaskManager
/// - RenderRprimsTranslucent
/// - ReadRprimId
/// - PostProcess
/// \return The task list that can be passed to pxr::HdEngine::Execute.
/// \return The list of tasks that can be passed to pxr::HdEngine::Execute.
///
/// \remarks Only enabled tasks are returned.
const pxr::HdTaskSharedPtrVector GetTasks(const std::vector<TaskUID>* TaskOrder = nullptr) const;

/// Sets new collection for the render tasks.
Expand All @@ -98,10 +100,12 @@ class HnTaskManager
/// \param [in] TaskId - The task ID that will be used to register the task in the render index.
/// \param [in] UID - The task UID that will be used to identify the task in the task manager.
/// \param [in] Params - The task parameters that will be associated with the task using the task ID.
/// \param [in] Enabled - Whether the task is enabled.
template <typename TaskType, typename TaskParamsType>
void CreateTask(const pxr::SdfPath& TaskId,
TaskUID UID,
TaskParamsType&& Params);
TaskParamsType&& Params,
bool Enabled = true);

/// Creates a new render task.
///
Expand All @@ -110,7 +114,8 @@ class HnTaskManager
template <typename TaskType, typename TaskParamsType>
void CreateTask(const pxr::TfToken& TaskId,
TaskUID UID,
TaskParamsType&& Params);
TaskParamsType&& Params,
bool Enabled = true);

template <typename TaskParamsType>
bool SetTaskParams(TaskUID UID,
Expand All @@ -122,6 +127,9 @@ class HnTaskManager

void SetRenderRprimParams(const HnRenderRprimsTaskParams& Params);

void EnableTask(TaskUID UID, bool Enable);
bool IsTaskEnabled(TaskUID UID) const;

pxr::HdTaskSharedPtr GetTask(TaskUID UID) const;

void RemoveTask(TaskUID UID);
Expand Down Expand Up @@ -214,7 +222,12 @@ class HnTaskManager
};
TaskParamsDelegate m_ParamsDelegate;

std::unordered_map<TaskUID, pxr::SdfPath> m_TaskUIDs;
struct TaskInfo
{
pxr::SdfPath Id;
bool Enabled = false;
};
std::unordered_map<TaskUID, TaskInfo> m_TaskInfo;

std::vector<TaskUID> m_DefaultTaskOrder;
std::vector<pxr::SdfPath> m_RenderTaskIds;
Expand All @@ -229,11 +242,12 @@ void HnTaskManager::SetParameter(const pxr::SdfPath& TaskId, const pxr::TfToken&
template <typename TaskType, typename TaskParamsType>
void HnTaskManager::CreateTask(const pxr::SdfPath& TaskId,
TaskUID UID,
TaskParamsType&& Params)
TaskParamsType&& Params,
bool Enabled)
{
m_RenderIndex.InsertTask<TaskType>(&m_ParamsDelegate, TaskId);
auto it_inserted = m_TaskUIDs.emplace(UID, TaskId);
VERIFY(it_inserted.second, "Task with UID ", UID, " already exists: ", it_inserted.first->second.GetText());
auto it_inserted = m_TaskInfo.emplace(UID, TaskInfo{TaskId, Enabled});
VERIFY(it_inserted.second, "Task with UID ", UID, " already exists: ", it_inserted.first->second.Id.GetText());

m_ParamsDelegate.SetParameter(TaskId, pxr::HdTokens->params, std::forward<TaskParamsType>(Params));
m_DefaultTaskOrder.emplace_back(UID);
Expand All @@ -242,9 +256,10 @@ void HnTaskManager::CreateTask(const pxr::SdfPath& TaskId,
template <typename TaskType, typename TaskParamsType>
void HnTaskManager::CreateTask(const pxr::TfToken& TaskId,
TaskUID UID,
TaskParamsType&& Params)
TaskParamsType&& Params,
bool Enabled)
{
CreateTask<TaskType>(pxr::SdfPath{GetId().AppendChild(TaskId)}, UID, std::forward<TaskParamsType>(Params));
CreateTask<TaskType>(pxr::SdfPath{GetId().AppendChild(TaskId)}, UID, std::forward<TaskParamsType>(Params), Enabled);
}

template <typename TaskParamsType>
Expand All @@ -265,11 +280,11 @@ template <typename TaskParamsType>
bool HnTaskManager::SetTaskParams(TaskUID UID,
TaskParamsType&& Params)
{
const auto it = m_TaskUIDs.find(UID);
if (it == m_TaskUIDs.end())
const auto it = m_TaskInfo.find(UID);
if (it == m_TaskInfo.end())
return false;

return SetTaskParams(it->second, std::forward<TaskParamsType>(Params));
return SetTaskParams(it->second.Id, std::forward<TaskParamsType>(Params));
}

} // namespace USD
Expand Down
45 changes: 32 additions & 13 deletions Hydrogent/src/Tasks/HnTaskManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,29 +146,29 @@ HnTaskManager::HnTaskManager(pxr::HdRenderIndex& RenderIndex,
HnTaskManager::~HnTaskManager()
{
// Remove all tasks from the render index
for (const auto& it : m_TaskUIDs)
for (const auto& it : m_TaskInfo)
{
m_RenderIndex.RemoveTask(it.second);
m_RenderIndex.RemoveTask(it.second.Id);
}
m_TaskUIDs.clear();
m_TaskInfo.clear();
}

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

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

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

void HnTaskManager::SetParameter(const pxr::SdfPath& TaskId, const TfToken& ValueKey, pxr::VtValue Value)
Expand Down Expand Up @@ -247,13 +247,17 @@ const pxr::HdTaskSharedPtrVector HnTaskManager::GetTasks(const std::vector<TaskU
TaskOrder = &m_DefaultTaskOrder;

pxr::HdTaskSharedPtrVector Tasks;
Tasks.reserve(TaskOrder->size());
for (auto UID : *TaskOrder)
{
auto it = m_TaskUIDs.find(UID);
if (it == m_TaskUIDs.end())
auto it = m_TaskInfo.find(UID);
if (it == m_TaskInfo.end())
continue;

Tasks.push_back(m_RenderIndex.GetTask(it->second));
if (!it->second.Enabled)
continue;

Tasks.push_back(m_RenderIndex.GetTask(it->second.Id));
}

return Tasks;
Expand Down Expand Up @@ -309,6 +313,21 @@ const pxr::SdfPath* HnTaskManager::GetSelectedRprimId() const
}
}

void HnTaskManager::EnableTask(TaskUID UID, bool Enable)
{
auto it = m_TaskInfo.find(UID);
if (it == m_TaskInfo.end())
return;

it->second.Enabled = Enable;
}

bool HnTaskManager::IsTaskEnabled(TaskUID UID) const
{
auto it = m_TaskInfo.find(UID);
return it != m_TaskInfo.end() ? it->second.Enabled : false;
}

} // namespace USD

} // namespace Diligent

0 comments on commit 9988539

Please sign in to comment.