Skip to content

Commit 1cac31f

Browse files
USDScene : Unique hashes for each USDScene even if they opened the same file
1 parent 94bda88 commit 1cac31f

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

contrib/IECoreUSD/src/IECoreUSD/USDScene.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,14 @@ Imath::M44d localTransform( const pxr::UsdPrim &prim, pxr::UsdTimeCode time )
516516
return result;
517517
}
518518

519+
// Used to assign a unique hash to each USD file. Using a global counter rather than the file name
520+
// means that we treat the same file as separate if it is closed and reopened. This means it's not
521+
// a problem if USD changes things when a file is reopened. USD appears to not in general guarantee
522+
// that anything is the same when reopening an unchanged file - things we're aware of that could
523+
// cause problems without this conservative uniquifying are: how instance prototype names are
524+
// assigned, and hashes ( indices ) of SdfPaths
525+
std::atomic< int > g_usdFileCounter = 0;
526+
519527
} // namespace
520528

521529
class USDScene::Location : public RefCounted
@@ -539,7 +547,8 @@ class USDScene::IO : public RefCounted
539547
: m_fileName( fileName ), m_openMode( openMode ), m_stage( stage ),
540548
m_rootPrim( m_stage->GetPseudoRoot() ),
541549
m_timeCodesPerSecond( m_stage->GetTimeCodesPerSecond() ),
542-
m_shaderNetworkCache( 10 * 1024 * 1024 ) // 10Mb
550+
m_shaderNetworkCache( 10 * 1024 * 1024 ), // 10Mb
551+
m_uniqueId( g_usdFileCounter.fetch_add( 1, std::memory_order_relaxed ) )
543552
{
544553
// Although the USD API implies otherwise, we need a different
545554
// cache per-purpose because `UsdShadeMaterialBindingAPI::ComputeBoundMaterial()`
@@ -656,6 +665,11 @@ class USDScene::IO : public RefCounted
656665
return m_shaderNetworkCache.get( output );
657666
}
658667

668+
inline int uniqueId()
669+
{
670+
return m_uniqueId;
671+
}
672+
659673
private :
660674

661675
static pxr::UsdStageRefPtr makeStage( const std::string &fileName, IndexedIO::OpenMode openMode )
@@ -691,6 +705,10 @@ class USDScene::IO : public RefCounted
691705

692706
ShaderNetworkCache m_shaderNetworkCache;
693707

708+
// Used to identify a file uniquely ( including between different openings of the same filename,
709+
// since closing and reopening a file may cause USD to shuffle the contents ).
710+
const int m_uniqueId;
711+
694712
};
695713

696714
USDScene::USDScene( const std::string &fileName, IndexedIO::OpenMode openMode )
@@ -1307,7 +1325,7 @@ void USDScene::hashSet( const Name &name, IECore::MurmurHash &h ) const
13071325
{
13081326
SceneInterface::hashSet( name, h );
13091327

1310-
h.append( m_root->fileName() );
1328+
h.append( m_root->uniqueId() );
13111329
append( m_location->prim.GetPath(), h );
13121330
h.append( name );
13131331
}
@@ -1463,7 +1481,7 @@ void USDScene::boundHash( double time, IECore::MurmurHash &h ) const
14631481
{
14641482
if( pxr::UsdGeomBoundable boundable = pxr::UsdGeomBoundable( m_location->prim ) )
14651483
{
1466-
h.append( m_root->fileName() );
1484+
h.append( m_root->uniqueId() );
14671485
appendPrimOrMasterPath( m_location->prim, h );
14681486
if( boundable.GetExtentAttr().ValueMightBeTimeVarying() )
14691487
{
@@ -1476,7 +1494,7 @@ void USDScene::transformHash( double time, IECore::MurmurHash &h ) const
14761494
{
14771495
if( pxr::UsdGeomXformable xformable = pxr::UsdGeomXformable( m_location->prim ) )
14781496
{
1479-
h.append( m_root->fileName() );
1497+
h.append( m_root->uniqueId() );
14801498
appendPrimOrMasterPath( m_location->prim, h );
14811499

14821500
bool mightBeTimeVarying = xformable.TransformMightBeTimeVarying();
@@ -1573,7 +1591,7 @@ void USDScene::attributesHash( double time, IECore::MurmurHash &h ) const
15731591

15741592
if( haveAttributes || haveMaterials )
15751593
{
1576-
h.append( m_root->fileName() );
1594+
h.append( m_root->uniqueId() );
15771595

15781596
if( haveAttributes )
15791597
{
@@ -1594,7 +1612,7 @@ void USDScene::objectHash( double time, IECore::MurmurHash &h ) const
15941612
{
15951613
if( ObjectAlgo::canReadObject( m_location->prim ) )
15961614
{
1597-
h.append( m_root->fileName() );
1615+
h.append( m_root->uniqueId() );
15981616
appendPrimOrMasterPath( m_location->prim, h );
15991617
if( ObjectAlgo::objectMightBeTimeVarying( m_location->prim ) )
16001618
{
@@ -1604,13 +1622,13 @@ void USDScene::objectHash( double time, IECore::MurmurHash &h ) const
16041622
}
16051623
void USDScene::childNamesHash( double time, IECore::MurmurHash &h ) const
16061624
{
1607-
h.append( m_root->fileName() );
1625+
h.append( m_root->uniqueId() );
16081626
appendPrimOrMasterPath( m_location->prim, h );
16091627
}
16101628

16111629
void USDScene::hierarchyHash( double time, IECore::MurmurHash &h ) const
16121630
{
1613-
h.append( m_root->fileName() );
1631+
h.append( m_root->uniqueId() );
16141632
appendPrimOrMasterPath( m_location->prim, h );
16151633
h.append( time );
16161634
}

0 commit comments

Comments
 (0)