Skip to content

Commit

Permalink
Merge pull request #2751 from Autodesk/vlasovi/MAYA-126504
Browse files Browse the repository at this point in the history
MAYA-126504 - Performance Issue: AntiquesMall Scene now takes 20+ minutes to load in Maya
  • Loading branch information
neilh-adsk authored Nov 29, 2022
2 parents a0c52c7 + f2a446a commit 8b7b598
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 61 deletions.
71 changes: 17 additions & 54 deletions lib/mayaUsd/render/vp2RenderDelegate/draw_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,8 @@

#include <pxr/imaging/hd/mesh.h>

#include <maya/MUserData.h>

PXR_NAMESPACE_OPEN_SCOPE

namespace {
class HdVP2DrawItemUserData : public MUserData
{
public:
#if MAYA_API_VERSION >= 20220000
HdVP2DrawItemUserData() { }
#else
HdVP2DrawItemUserData()
: MUserData(true)
{
}
#endif
~HdVP2DrawItemUserData() override { }

int increaseUseCount() { return ++_useCount; }
int decreaseUseCount() { return --_useCount; }

private:
// A render item can be shared between Reprs. We need to track usage so it only gets removed
// from the subscene once all references are gone.
int _useCount = 0;
};

#if MAYA_API_VERSION >= 20220000
using HdVP2DrawItemUserDataPtr = MSharedPtr<HdVP2DrawItemUserData>;
#endif
} // namespace

/*! \brief Constructor.
Data holder for its corresponding render item to facilitate parallelized evaluation.
Expand Down Expand Up @@ -78,14 +48,8 @@ HdVP2DrawItem::~HdVP2DrawItem()
MSubSceneContainer* subSceneContainer = param ? param->GetContainer() : nullptr;
if (subSceneContainer) {
for (const auto& renderItemData : _renderItems) {
#if MAYA_API_VERSION >= 20220000
auto sharingData = HdVP2DrawItemUserDataPtr::dynamic_pointer_cast<>(
renderItemData._renderItem->getCustomData());
#else
HdVP2DrawItemUserData* sharingData = dynamic_cast<HdVP2DrawItemUserData*>(
renderItemData._renderItem->customData());
#endif
if (!sharingData || sharingData->decreaseUseCount() == 0) {
const auto& sharedRenderItemCounter = renderItemData._sharedRenderItemCounter;
if (!sharedRenderItemCounter || (--(*sharedRenderItemCounter)) == 0) {
TF_VERIFY(renderItemData._renderItemName == renderItemData._renderItem->name());
subSceneContainer->remove(renderItemData._renderItem->name());
}
Expand All @@ -105,22 +69,6 @@ HdVP2DrawItem::AddRenderItem(MHWRender::MRenderItem* item, const HdGeomSubset* g
renderItemData._renderItem = item;
renderItemData._renderItemName = item->name();
renderItemData._enabled = item->isEnabled();
#if MAYA_API_VERSION >= 20220000
auto sharingData = HdVP2DrawItemUserDataPtr::dynamic_pointer_cast<>(item->getCustomData());
#else
HdVP2DrawItemUserData* sharingData
= dynamic_cast<HdVP2DrawItemUserData*>(renderItemData._renderItem->customData());
#endif
if (!sharingData) {
// create the custom data
#if MAYA_API_VERSION >= 20220000
sharingData = HdVP2DrawItemUserDataPtr(new HdVP2DrawItemUserData());
#else
sharingData = new HdVP2DrawItemUserData();
#endif
item->setCustomData(sharingData);
}
sharingData->increaseUseCount();
if (geomSubset) {
renderItemData._geomSubset = *geomSubset;
}
Expand All @@ -131,6 +79,21 @@ HdVP2DrawItem::AddRenderItem(MHWRender::MRenderItem* item, const HdGeomSubset* g
return renderItemData;
}

void HdVP2DrawItem::ShareRenderItem(HdVP2DrawItem& sourceDrawItem)
{
TF_VERIFY(_renderItems.size() == 0);
RenderItemData& srcData = sourceDrawItem.GetRenderItemData();
AddRenderItem(srcData._renderItem);

if (!srcData._sharedRenderItemCounter) {
srcData._sharedRenderItemCounter = std::make_shared<size_t>(1);
}

RenderItemData& dstData = GetRenderItemData();
dstData._sharedRenderItemCounter = srcData._sharedRenderItemCounter;
++(*dstData._sharedRenderItemCounter);
}

/* static */
SdfPath HdVP2DrawItem::RenderItemToPrimPath(const MHWRender::MRenderItem& item)
{
Expand Down
10 changes: 4 additions & 6 deletions lib/mayaUsd/render/vp2RenderDelegate/draw_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class HdVP2DrawItem final : public HdDrawItem
MString _renderItemName;
//! Pointer of the render item for fast access. No ownership is held.
MHWRender::MRenderItem* _renderItem { nullptr };
//! If the render item is shared, this will store the shared counter
std::shared_ptr<size_t> _sharedRenderItemCounter;

//! The geom subset this render item represents. _geomSubset.id is StdPath::EmptyPath() if
//! there is no geom subset.
Expand Down Expand Up @@ -163,13 +165,9 @@ class HdVP2DrawItem final : public HdDrawItem
*/
MHWRender::MRenderItem* GetRenderItem() const { return GetRenderItemData()._renderItem; }

/*! \brief Set pointer of the associated render item
/*! \brief Shares a single render item between the source and the destination draw items
*/
void SetRenderItem(MHWRender::MRenderItem* item)
{
TF_VERIFY(_renderItems.size() == 0);
AddRenderItem(item);
}
void ShareRenderItem(HdVP2DrawItem& sourceDrawItem);

/*! \brief Set a usage to the render item
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/mayaUsd/render/vp2RenderDelegate/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ void HdVP2Mesh::_InitRepr(const TfToken& reprToken, HdDirtyBits* dirtyBits)
#endif
if (shDrawItem
&& shDrawItem->MatchesUsage(HdVP2DrawItem::kSelectionHighlight)) {
drawItem->SetRenderItem(shDrawItem->GetRenderItem());
drawItem->ShareRenderItem(*shDrawItem);
foundShared = true;
break;
}
Expand Down

0 comments on commit 8b7b598

Please sign in to comment.