From dfee169423260ed79f087bb5872052d11612d952 Mon Sep 17 00:00:00 2001 From: assiduous Date: Thu, 19 Oct 2023 09:30:45 -0700 Subject: [PATCH] HnTaskController: improved render task processing --- Hydrogent/include/Tasks/HnTaskController.hpp | 17 ++++-- Hydrogent/src/HnRendererImpl.cpp | 2 +- Hydrogent/src/Tasks/HnTaskController.cpp | 57 ++++++-------------- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/Hydrogent/include/Tasks/HnTaskController.hpp b/Hydrogent/include/Tasks/HnTaskController.hpp index 9d9f0300..ef3c0880 100644 --- a/Hydrogent/include/Tasks/HnTaskController.hpp +++ b/Hydrogent/include/Tasks/HnTaskController.hpp @@ -28,6 +28,7 @@ #include #include +#include #include "Tasks/HnTask.hpp" @@ -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* TaskOrder = nullptr) const; /// Sets new collection for the render tasks. @@ -102,9 +112,10 @@ class HnTaskController class TaskParamsDelegate; std::unique_ptr m_ParamsDelegate; - std::unordered_map m_TaskIds; + std::unordered_map m_TaskUIDs; - std::vector m_DefaultTaskOrder; + std::vector m_DefaultTaskOrder; + std::vector m_RenderTaskIds; }; template @@ -112,7 +123,7 @@ void HnTaskController::CreateTask(const pxr::SdfPath& TaskId, TaskUID UID) { m_RenderIndex.InsertTask(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()); } diff --git a/Hydrogent/src/HnRendererImpl.cpp b/Hydrogent/src/HnRendererImpl.cpp index 269e85df..a5ce8b59 100644 --- a/Hydrogent/src/HnRendererImpl.cpp +++ b/Hydrogent/src/HnRendererImpl.cpp @@ -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(m_GeometryPass, m_RenderTags)); m_Engine.Execute(&m_ImagingDelegate->GetRenderIndex(), &tasks); } diff --git a/Hydrogent/src/Tasks/HnTaskController.cpp b/Hydrogent/src/Tasks/HnTaskController.cpp index 27577a17..e55a3a06 100644 --- a/Hydrogent/src/Tasks/HnTaskController.cpp +++ b/Hydrogent/src/Tasks/HnTaskController.cpp @@ -54,13 +54,6 @@ TF_DEFINE_PRIVATE_TOKENS( ); // clang-format on -static constexpr std::array RenderTaskUIDs = { - HnTaskController::TaskUID_RenderDefault, - HnTaskController::TaskUID_RenderMasked, - HnTaskController::TaskUID_RenderAdditive, - HnTaskController::TaskUID_RenderTranslucent, -}; - } // namespace class HnTaskController::TaskParamsDelegate final : public pxr::HdSceneDelegate @@ -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 @@ -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(RenderTaskId, UID); @@ -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() @@ -272,8 +267,8 @@ const pxr::HdTaskSharedPtrVector HnTaskController::GetTasks(const std::vectorsecond)); @@ -285,14 +280,8 @@ const pxr::HdTaskSharedPtrVector HnTaskController::GetTasks(const std::vectorsecond; - pxr::HdRprimCollection OldCollection = m_ParamsDelegate->GetParameter(TaskId, pxr::HdTokens->collection); const pxr::TfToken& OldMaterialTag = OldCollection.GetMaterialTag(); @@ -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(TaskId, pxr::HdTokens->params); if (OldParams == Params) continue; @@ -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; @@ -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(TaskId, pxr::HdTokens->renderTags); if (OldRenderTags == RenderTags) continue;