Skip to content

Commit

Permalink
HnRenderParam: improved multithreaded performance of AddDirtyRPrim
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Jan 21, 2025
1 parent 60b4b21 commit f21f0a9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
11 changes: 8 additions & 3 deletions Hydrogent/include/HnRenderParam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@

#include <atomic>
#include <array>
#include <mutex>
#include <vector>
#include <unordered_map>
#include <thread>

#include "HnTypes.hpp"
#include "SpinLock.hpp"

#include "pxr/imaging/hd/renderDelegate.h"
#include "../../PBR/interface/PBR_Renderer.hpp"
Expand Down Expand Up @@ -146,8 +148,11 @@ class HnRenderParam final : public pxr::HdRenderParam
float m_ElapsedTime = 0.0;
uint32_t m_FrameNumber = 0;

std::mutex m_DirtyRPrimsMtx;
std::vector<std::pair<pxr::SdfPath, pxr::HdDirtyBits>> m_DirtyRPrims;
using DirtyRPrimsVector = std::vector<std::pair<pxr::SdfPath, pxr::HdDirtyBits>>;

// Note: spin lock here works much faster than std::mutex
Threading::SpinLock m_DirtyRPrimsLock;
std::unordered_map<std::thread::id, DirtyRPrimsVector> m_DirtyRPrimsPerThread;
};

} // namespace USD
Expand Down
27 changes: 21 additions & 6 deletions Hydrogent/src/HnRenderParam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,33 @@ HnRenderParam::~HnRenderParam()

void HnRenderParam::AddDirtyRPrim(const pxr::SdfPath& RPrimId, pxr::HdDirtyBits DirtyBits)
{
std::lock_guard<std::mutex> Lock{m_DirtyRPrimsMtx};
m_DirtyRPrims.emplace_back(RPrimId, DirtyBits);
DirtyRPrimsVector* pDirtyRPrims = nullptr;
{
Threading::SpinLockGuard Guard{m_DirtyRPrimsLock};

auto it = m_DirtyRPrimsPerThread.find(std::this_thread::get_id());
if (it == m_DirtyRPrimsPerThread.end())
{
it = m_DirtyRPrimsPerThread.emplace(std::this_thread::get_id(), DirtyRPrimsVector{}).first;
it->second.reserve(16);
}
pDirtyRPrims = &it->second;
}
pDirtyRPrims->emplace_back(RPrimId, DirtyBits);
}

void HnRenderParam::CommitDirtyRPrims(pxr::HdChangeTracker& ChangeTracker)
{
std::lock_guard<std::mutex> Lock{m_DirtyRPrimsMtx};
for (const auto& DidrtRPrim : m_DirtyRPrims)
Threading::SpinLockGuard Guard{m_DirtyRPrimsLock};

for (const auto& DirtyRPrims : m_DirtyRPrimsPerThread)
{
ChangeTracker.MarkRprimDirty(DidrtRPrim.first, DidrtRPrim.second);
for (const auto& RPrim : DirtyRPrims.second)
{
ChangeTracker.MarkRprimDirty(RPrim.first, RPrim.second);
}
}
m_DirtyRPrims.clear();
m_DirtyRPrimsPerThread.clear();
}

} // namespace USD
Expand Down

0 comments on commit f21f0a9

Please sign in to comment.