Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding vectorized version of GetScenePrimPath to HdSceneDelegate #1744

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pxr/imaging/hd/sceneDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ HdSceneDelegate::GetScenePrimPath(SdfPath const& rprimId,
return rprimId.ReplacePrefix(_delegateID, SdfPath::AbsoluteRootPath());
}

/*virtual*/
SdfPathVector
HdSceneDelegate::GetScenePrimPaths(SdfPath const& rprimId,
std::vector<int> instanceIndices,
std::vector<HdInstancerContext> *instancerContexts)
{
return SdfPathVector(instanceIndices.size());
}


// -----------------------------------------------------------------------//
Expand Down
10 changes: 10 additions & 0 deletions pxr/imaging/hd/sceneDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,16 @@ class HdSceneDelegate {
int instanceIndex,
HdInstancerContext *instancerContext = nullptr);

/// A vectorized version of GetScenePrimPath that allows the prim adapter
/// to amortize expensive calculations across a number of path evaluations
/// in a single call. Note that only a single rprimId is supported. This
/// allows this call to be forwarded directly to a single prim adapter
/// rather than requiring a lot of data shuffling.
HD_API
virtual SdfPathVector GetScenePrimPaths(SdfPath const& rprimId,
std::vector<int> instanceIndices,
std::vector<HdInstancerContext> *instancerContexts = nullptr);

// -----------------------------------------------------------------------//
/// \name Material Aspects
// -----------------------------------------------------------------------//
Expand Down
17 changes: 17 additions & 0 deletions pxr/usdImaging/usdImaging/delegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,23 @@ UsdImagingDelegate::GetScenePrimPath(SdfPath const& rprimId,
return protoPath;
}

SdfPathVector
UsdImagingDelegate::GetScenePrimPaths(SdfPath const& rprimId,
std::vector<int> instanceIndices,
std::vector<HdInstancerContext> *instancerContexts)
{
SdfPath cachePath = ConvertIndexPathToCachePath(rprimId);
_HdPrimInfo *primInfo = _GetHdPrimInfo(cachePath);
if (!primInfo || !primInfo->adapter) {
TF_WARN("GetScenePrimPaths: Couldn't find rprim <%s>",
rprimId.GetText());
return SdfPathVector(instanceIndices.size(), cachePath);
}

return primInfo->adapter->GetScenePrimPaths(
cachePath, instanceIndices, instancerContexts);
}

bool
UsdImagingDelegate::PopulateSelection(
HdSelection::HighlightMode const& highlightMode,
Expand Down
6 changes: 6 additions & 0 deletions pxr/usdImaging/usdImaging/delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ class UsdImagingDelegate : public HdSceneDelegate, public TfWeakBase {
int instanceIndex,
HdInstancerContext *instancerContext = nullptr) override;

USDIMAGING_API
virtual SdfPathVector
GetScenePrimPaths(SdfPath const& rprimId,
std::vector<int> instanceIndices,
std::vector<HdInstancerContext> *instancerContexts = nullptr) override;

// ExtComputation support
USDIMAGING_API
TfTokenVector
Expand Down
93 changes: 63 additions & 30 deletions pxr/usdImaging/usdImaging/instanceAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2384,16 +2384,34 @@ UsdImagingInstanceAdapter::_ComputeInstanceMap(
return indices;
}

struct UsdImagingInstanceAdapter::_GetScenePrimPathFn
/* virtual */
SdfPath
UsdImagingInstanceAdapter::GetScenePrimPath(
SdfPath const& cachePath,
int instanceIndex,
HdInstancerContext *instancerContext) const
{
_GetScenePrimPathFn(
HD_TRACE_FUNCTION();

// Pass nullptr to instancerCtxs because this value is never used by
// our implementation of this method.
SdfPathVector paths = GetScenePrimPaths(
cachePath, { instanceIndex }, nullptr);
return paths.size() > 0 ? paths[0] : SdfPath();
}

struct UsdImagingInstanceAdapter::_GetScenePrimPathsFn
{
_GetScenePrimPathsFn(
const UsdImagingInstanceAdapter* adapter_,
int instanceIndex_,
const std::vector<int> &instanceIndices_,
const SdfPath &protoPath_)
: adapter(adapter_)
, instanceIndex(instanceIndex_)
, protoPath(protoPath_)
{ }
{
instanceIndices.insert(
instanceIndices_.begin(), instanceIndices_.end());
}

void Initialize(size_t numInstances)
{
Expand All @@ -2405,7 +2423,7 @@ struct UsdImagingInstanceAdapter::_GetScenePrimPathFn
// If this iteration is the right instance index, compose all the USD
// prototype paths together to get the instance proxy path. Include the
// proto path (of the child prim), if one was provided.
if (instanceIdx == instanceIndex) {
if (instanceIndices.find(instanceIdx) != instanceIndices.end()) {
SdfPathVector instanceChain;
// To get the correct prim-in-prototype, we need to add the
// prototype path to the instance chain. However, there's a case in
Expand All @@ -2421,24 +2439,27 @@ struct UsdImagingInstanceAdapter::_GetScenePrimPathFn
for (UsdPrim const& prim : instanceContext) {
instanceChain.push_back(prim.GetPath());
}
primPath = adapter->_GetPrimPathFromInstancerChain(instanceChain);
return false;
primPaths.emplace(instanceIdx,
adapter->_GetPrimPathFromInstancerChain(instanceChain));
// We can stop iterating when we've found a prim path for each
// instance index.
return primPaths.size() != instanceIndices.size();
}
return true;
}

const UsdImagingInstanceAdapter* adapter;
const size_t instanceIndex;
const SdfPath& protoPath;
SdfPath primPath;
std::set<int> instanceIndices;
std::map<int, SdfPath> primPaths;
};

/* virtual */
SdfPath
UsdImagingInstanceAdapter::GetScenePrimPath(
SdfPathVector
UsdImagingInstanceAdapter::GetScenePrimPaths(
SdfPath const& cachePath,
int instanceIndex,
HdInstancerContext *instancerContext) const
std::vector<int> const& instanceIndices,
std::vector<HdInstancerContext> *instancerCtxs) const
{
HD_TRACE_FUNCTION();

Expand All @@ -2451,21 +2472,21 @@ UsdImagingInstanceAdapter::GetScenePrimPath(
if (_IsChildPrim(usdPrim, cachePath)) {

TF_DEBUG(USDIMAGING_SELECTION).Msg(
"GetScenePrimPath: instance proto = %s\n", cachePath.GetText());
"GetScenePrimPaths: instance proto = %s\n", cachePath.GetText());

UsdImagingInstancerContext instancerContext;
_ProtoPrim const& proto = _GetProtoPrim(
cachePath.GetAbsoluteRootOrPrimPath(),
cachePath, &instancerContext);

if (!proto.adapter) {
return SdfPath();
return SdfPathVector(instanceIndices.size(), cachePath);
}

_InstancerData const* instrData =
TfMapLookupPtr(_instancerData, instancerContext.instancerCachePath);
if (!instrData) {
return SdfPath();
return SdfPathVector(instanceIndices.size(), cachePath);
}

UsdPrim instancerPrim = _GetPrim(instancerContext.instancerCachePath);
Expand All @@ -2474,34 +2495,46 @@ UsdImagingInstanceAdapter::GetScenePrimPath(
// invisible instances).
VtIntArray indices = _ComputeInstanceMap(instancerPrim, *instrData,
_GetTimeWithOffset(0.0));

instanceIndex = indices[instanceIndex];

_GetScenePrimPathFn primPathFn(this, instanceIndex, proto.path);
_RunForAllInstancesToDraw(instancerPrim, &primPathFn);
return primPathFn.primPath;
std::vector<int> remappedIndices;

remappedIndices.reserve(instanceIndices.size());
for (size_t i = 0; i < instanceIndices.size(); i++)
remappedIndices.push_back(indices[instanceIndices[i]]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating a list remapped a list of remapped indices it could be beneficial to have a bitset (std::vector) where each bit set marks an index to fetch. This would change the O(N) operation of std::vector<int>::find(instanceIndex) to an O(1) operation. There are worst case scenarios where the path for each instance is being queries which would run in O(N^2) runtime with this implementation.

To avoid costly memory reallocations and copies it'd be be good to run over the list on instanceIndices twice, one time to check for the max index to determine required size of the vector and once to set all the bits for indices[instanceIndices[i]].

This solution has the downside of allocating too much memory for a single large index. To counter this in addition to the maximum remapped index one could determine the minimum remapped index as well to offset instanceIndex in the operator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, what std::vector<int>::find(instanceIndex) operation are you trying to avoid? I really don't understand what you're suggesting here... Could you provide some code that shows exactly what you want to change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll prepare a patch with the suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marktucker I've pushed a change with my ideas to my usd fork based on your branch here: https://github.com/mtavenrath/USD_sideeffects/commit/48b77edc80d90b0d0e014bb20e475632a913a344. There's no std::set/std::map anymore and everythings runs in linear time. I've tested the change with a scene with 1M instances querying all of them with a result of a few seconds only for all paths.

The idea is that requestedIndicesMap specifies if a index is requested at all (!= INT_MAX) and the value of requestedIndicesMap specifies the location for the index in the resulting vector. Thus there's no need to construct & iterate the result map anymore.

There is one 'worst case' scenario if someone queries the first and last instance. For this case requestedIndicesMap will be as large as the number of instances. Given it's single allocation only with sizeof(int) == 4 the cost of the allocation is neglectable when looking at memory consumed by the whole scene.

std::map/std::set quickly consume way more memory for way less elements given that each entry in those data structures has a size of at least 16 bytes and each entry is a single allocation and thus has allocation memory overhead as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tcauchois , I'll add @mtavenrath to NVIDIA's CLA. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mtavenrath! Just to defend my implementation a little bit, it was in fact only ever doing a std::set<int>::find, not std::vector<int>::find as you claimed. So things were never as bad as you feared. That said, your implementation is definitely going to be faster, and I suspect also smaller in memory footprint in the large-number cases that we're concerned about here. I haven't had a chance to test it out, but I definitely like the concept.

For purely organization and attribution purposes, I think @tcauchois 's plan of accepting my PR, then your PR on top makes the most sense (assuming these PRs are both acceptable).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marktucker I realised that std::set::find instead of std::vetor is being used while implementing my idea and have to excuse myself for making that rash comment. Your attempt to do the picking is actually better than my first attempt which provided a function which returned a std::vectorwith all instance paths. Indexing had to be done on the applications side.

I agree that it makes sense to have two separate PRs for the two stages of optimization.


SdfPathVector result;
result.reserve(instanceIndices.size());
_GetScenePrimPathsFn primPathsFn(this, remappedIndices, proto.path);
_RunForAllInstancesToDraw(instancerPrim, &primPathsFn);
for (size_t i = 0; i < remappedIndices.size(); i++)
result.push_back(primPathsFn.primPaths[remappedIndices[i]]);
return result;
} else {

TF_DEBUG(USDIMAGING_SELECTION).Msg(
"GetScenePrimPath: instance = %s\n", cachePath.GetText());
"GetScenePrimPaths: instance = %s\n", cachePath.GetText());

SdfPath const* instancerPath =
TfMapLookupPtr(_instanceToInstancerMap, cachePath);
if (instancerPath == nullptr) {
return SdfPath();
return SdfPathVector(instanceIndices.size(), cachePath);
}
_InstancerData const* instrData =
TfMapLookupPtr(_instancerData, *instancerPath);
if (instrData == nullptr) {
return SdfPath();
return SdfPathVector(instanceIndices.size(), cachePath);
}
_GetScenePrimPathFn primPathFn(this, instanceIndex,

SdfPathVector result;
result.reserve(instanceIndices.size());
_GetScenePrimPathsFn primPathsFn(this, instanceIndices,
SdfPath::EmptyPath());
_RunForAllInstancesToDraw(_GetPrim(*instancerPath), &primPathFn);
return primPathFn.primPath;
_RunForAllInstancesToDraw(_GetPrim(*instancerPath), &primPathsFn);
for (size_t i = 0; i < instanceIndices.size(); i++)
result.push_back(primPathsFn.primPaths[instanceIndices[i]]);
return result;
}

return SdfPath();
return SdfPathVector(instanceIndices.size(), cachePath);
}

struct UsdImagingInstanceAdapter::_PopulateInstanceSelectionFn
Expand Down
6 changes: 6 additions & 0 deletions pxr/usdImaging/usdImaging/instanceAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ class UsdImagingInstanceAdapter : public UsdImagingPrimAdapter
int instanceIndex,
HdInstancerContext *instancerContext) const override;

virtual SdfPathVector GetScenePrimPaths(
SdfPath const& cachePath,
std::vector<int> const& instanceIndices,
std::vector<HdInstancerContext> *instancerCtxs) const override;

virtual bool PopulateSelection(
HdSelection::HighlightMode const& highlightMode,
SdfPath const &cachePath,
Expand Down Expand Up @@ -455,6 +460,7 @@ class UsdImagingInstanceAdapter : public UsdImagingPrimAdapter

struct _PopulateInstanceSelectionFn;
struct _GetScenePrimPathFn;
struct _GetScenePrimPathsFn;

// Helper functions for dealing with "actual" instances to be drawn.
//
Expand Down
23 changes: 23 additions & 0 deletions pxr/usdImaging/usdImaging/pointInstancerAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,29 @@ UsdImagingPointInstancerAdapter::GetScenePrimPath(
return _GetPrimPathFromInstancerChain(paths);
}

/* virtual */
SdfPathVector
UsdImagingPointInstancerAdapter::GetScenePrimPaths(
SdfPath const& cachePath,
std::vector<int> const& instanceIndices,
std::vector<HdInstancerContext> *instancerCtxs) const
{
SdfPathVector result;
HdInstancerContext instanceCtx;

result.reserve(instanceIndices.size());
if (instancerCtxs)
instancerCtxs->reserve(instanceIndices.size());
for (size_t i = 0; i < instanceIndices.size(); i++) {
result.push_back(
GetScenePrimPath(cachePath, instanceIndices[i], &instanceCtx));
if (instancerCtxs)
instancerCtxs->push_back(std::move(instanceCtx));
}

return result;
}

static
size_t
_GatherAuthoredTransformTimeSamples(
Expand Down
5 changes: 5 additions & 0 deletions pxr/usdImaging/usdImaging/pointInstancerAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ class UsdImagingPointInstancerAdapter : public UsdImagingPrimAdapter
int instanceIndex,
HdInstancerContext *instancerContext) const override;

virtual SdfPathVector GetScenePrimPaths(
SdfPath const& cachePath,
std::vector<int> const& instanceIndices,
std::vector<HdInstancerContext> *instancerCtxs) const override;

virtual bool PopulateSelection(
HdSelection::HighlightMode const& highlightMode,
SdfPath const &cachePath,
Expand Down
11 changes: 11 additions & 0 deletions pxr/usdImaging/usdImaging/primAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,17 @@ UsdImagingPrimAdapter::GetScenePrimPath(
return cachePath;
}

/*virtual*/
SdfPathVector
UsdImagingPrimAdapter::GetScenePrimPaths(SdfPath const& cachePath,
std::vector<int> const& instanceIndices,
std::vector<HdInstancerContext> *instancerCtxs) const
{
// Note: if we end up here, we're not instanced, since primInfo
// holds the instance adapter for instanced gprims.
return SdfPathVector(instanceIndices.size(), cachePath);
}

/*virtual*/
bool
UsdImagingPrimAdapter::PopulateSelection(
Expand Down
9 changes: 7 additions & 2 deletions pxr/usdImaging/usdImaging/primAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,13 @@ class UsdImagingPrimAdapter

USDIMAGING_API
virtual SdfPath GetScenePrimPath(SdfPath const& cachePath,
int instanceIndex,
HdInstancerContext *instancerCtx) const;
int instanceIndex,
HdInstancerContext *instancerCtx) const;

USDIMAGING_API
virtual SdfPathVector GetScenePrimPaths(SdfPath const& cachePath,
std::vector<int> const& instanceIndices,
std::vector<HdInstancerContext> *instancerCtxs) const;

// Add the given usdPrim to the HdSelection object, to mark it for
// selection highlighting. cachePath is the path of the object referencing
Expand Down