Skip to content

Commit

Permalink
Merge pull request #2190 from nvmkuruc/usdgeomhash
Browse files Browse the repository at this point in the history
Replace boost hash usage with TfHash in pxr/usd/usdGeom

(Internal change: 2269112)
  • Loading branch information
pixar-oss committed Mar 31, 2023
2 parents 363c02d + c6901f6 commit 8a43c77
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
9 changes: 1 addition & 8 deletions pxr/usd/usdGeom/bboxCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include "pxr/base/work/withScopedParallelism.h"

#include "pxr/base/tf/hash.h"
#include "pxr/base/tf/pyLock.h"
#include "pxr/base/tf/stringUtils.h"
#include "pxr/base/tf/token.h"
Expand Down Expand Up @@ -1520,13 +1521,5 @@ UsdGeomBBoxCache::_PrimContext::ToString() const {
}
}

size_t hash_value(const UsdGeomBBoxCache::_PrimContext &key)
{
size_t hash = hash_value(key.prim);
boost::hash_combine(hash, key.instanceInheritablePurpose.Hash());
return hash;
}


PXR_NAMESPACE_CLOSE_SCOPE

13 changes: 11 additions & 2 deletions pxr/usd/usdGeom/bboxCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "pxr/usd/usdGeom/pointInstancer.h"
#include "pxr/usd/usd/attributeQuery.h"
#include "pxr/base/gf/bbox3d.h"
#include "pxr/base/tf/hash.h"
#include "pxr/base/tf/hashmap.h"
#include "pxr/base/work/dispatcher.h"

Expand Down Expand Up @@ -541,10 +542,18 @@ class UsdGeomBBoxCache
// Helper to determine if we should use extents hints for \p prim.
inline bool _UseExtentsHintForPrim(UsdPrim const &prim) const;

// Specialize TfHashAppend for TfHash
template <typename HashState>
friend void TfHashAppend(HashState& h, const _PrimContext &key)
{
h.Append(key.prim);
h.Append(key.instanceInheritablePurpose);
}

// Need hash_value for boost to key cache entries by prim context.
friend size_t hash_value(const _PrimContext &key);
friend size_t hash_value(const _PrimContext &key) { return TfHash{}(key); }

typedef boost::hash<_PrimContext> _PrimContextHash;
typedef TfHash _PrimContextHash;
typedef TfHashMap<_PrimContext, _Entry, _PrimContextHash> _PrimBBoxHashMap;

// Finds the cache entry for the prim context if it exists.
Expand Down
9 changes: 7 additions & 2 deletions pxr/usd/usdGeom/primvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -694,10 +694,15 @@ class UsdGeomPrimvar
return lhs.GetAttr().GetPath() < rhs.GetAttr().GetPath();
}

// Specialize TfHashAppend for TfHash
template <typename HashState>
friend void TfHashAppend(HashState& h, const UsdGeomPrimvar& obj) {
h.Append(obj.GetAttr());
}

// hash_value overload for std/boost hash.
USDGEOM_API
friend size_t hash_value(const UsdGeomPrimvar &obj) {
return hash_value(obj.GetAttr());
return TfHash{}(obj);
}


Expand Down
12 changes: 12 additions & 0 deletions pxr/usd/usdGeom/testenv/testUsdGeomPrimvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,5 +692,17 @@ def test_InvalidPrimvar(self):
with self.assertRaises(RuntimeError):
u1.GetElementSize()

def test_Hash(self):
"""Validate different primvar objects referring to the same underlying
property hash to the same value."""
stage = Usd.Stage.CreateInMemory()
mesh = UsdGeom.Mesh.Define(stage, '/mesh')
meshPrimvarsAPI = UsdGeom.PrimvarsAPI(mesh)
primvar = meshPrimvarsAPI.CreatePrimvar("pv", Sdf.ValueTypeNames.Int)
self.assertTrue(primvar)
self.assertIsNot(primvar, meshPrimvarsAPI.GetPrimvar("pv"))
self.assertEqual(primvar, meshPrimvarsAPI.GetPrimvar("pv"))
self.assertEqual(hash(primvar), hash(meshPrimvarsAPI.GetPrimvar("pv")))

if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion pxr/usd/usdGeom/wrapPrimvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ _GetTimeSamplesInInterval(const UsdGeomPrimvar &self,
return result;
}

static size_t __hash__(const UsdGeomPrimvar &self) { return hash_value(self); }
static size_t __hash__(const UsdGeomPrimvar &self) { return TfHash{}(self); }

// We override __getattribute__ for UsdGeomPrimvar to check object validity
// and raise an exception instead of crashing from Python.
Expand Down
5 changes: 2 additions & 3 deletions pxr/usd/usdGeom/xformCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
#include "pxr/usd/usdGeom/xformable.h"

#include "pxr/base/gf/matrix4d.h"
#include "pxr/base/tf/hash.h"
#include "pxr/base/tf/hashmap.h"
#include "pxr/base/tf/token.h"

#include <boost/functional/hash.hpp>

PXR_NAMESPACE_OPEN_SCOPE


Expand Down Expand Up @@ -170,7 +169,7 @@ class UsdGeomXformCache
// Helper function to get or create a new entry for a prim in the ctm cache.
_Entry * _GetCacheEntryForPrim(const UsdPrim &prim);

typedef TfHashMap<UsdPrim, _Entry, boost::hash<UsdPrim> > _PrimHashMap;
typedef TfHashMap<UsdPrim, _Entry, TfHash> _PrimHashMap;
_PrimHashMap _ctmCache;

// The time at which this stack is querying and caching attribute values.
Expand Down

0 comments on commit 8a43c77

Please sign in to comment.