Skip to content

Commit

Permalink
HYDRA-814 : Use RootOverridesSceneIndex to replace a scene index chai…
Browse files Browse the repository at this point in the history
…n for data producer scene indices (including Usd stages)
  • Loading branch information
lanierd-adsk committed Feb 9, 2024
1 parent c02cd46 commit a4fc977
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 316 deletions.
6 changes: 3 additions & 3 deletions lib/flowViewport/API/fvpDataProducerSceneIndexInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ namespace FVP_NS_DEF
*
* @param[in] customDataProducerSceneIndex is the custom scene index to add.
*
* @param[in, out] inoutPreFix is the prefix you want to add to your data producer scene index primitives, it may be modified by this function if you provide a dccnode.
* If you don't want any prefix, pass SdfPath::AbsoluteRootPath() to this parameter.
* @param[in] preFix is the prefix you want to add to your data producer scene index primitives.
* If you don't want any prefix, pass SdfPath::AbsoluteRootPath() to this parameter.
*
* @param[in] dccNode is a MObject* from a DAG node for Maya, if you provide the pointer value, then we automatically track some events such as transform
* or visibility updated and we hide automatically the primitives from the data producer scene index.
Expand All @@ -73,7 +73,7 @@ namespace FVP_NS_DEF
* @return true if the operation succeeded, false otherwise.
*/
virtual bool addDataProducerSceneIndex(const PXR_NS::HdSceneIndexBaseRefPtr& customDataProducerSceneIndex,
PXR_NS::SdfPath& inoutPreFix,
const PXR_NS::SdfPath& preFix,
void* dccNode = nullptr,
const std::string& hydraViewportId = PXR_NS::FvpViewportAPITokens->allViewports,
const std::string& rendererNames = PXR_NS::FvpViewportAPITokens->allRenderers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,15 @@ DataProducerSceneIndexInterfaceImp& DataProducerSceneIndexInterfaceImp::get()
PXR_NS::FVP_NS_DEF::DataProducerSceneIndexDataBaseRefPtr DataProducerSceneIndexInterfaceImp::addUsdStageSceneIndex(UsdImagingCreateSceneIndicesInfo& createInfo,
HdSceneIndexBaseRefPtr& finalSceneIndex,
UsdImagingStageSceneIndexRefPtr& stageSceneIndex,
SdfPath& inoutPrefix,
const SdfPath& preFix,
void* dccNode)
{
PXR_NS::FVP_NS_DEF::DataProducerSceneIndexDataBaseRefPtr dataProducerSceneIndexData =
_CreateDataProducerSceneIndexDataForUsdStage(createInfo, finalSceneIndex, stageSceneIndex, inoutPrefix, dccNode);
_CreateDataProducerSceneIndexDataForUsdStage(createInfo, finalSceneIndex, stageSceneIndex, preFix, dccNode);
if (nullptr == dataProducerSceneIndexData){
return nullptr;
}

inoutPrefix = dataProducerSceneIndexData->GetPrefix();

return dataProducerSceneIndexData;
}

Expand All @@ -76,20 +74,18 @@ bool DataProducerSceneIndexInterfaceImp::addUsdStageDataProducerSceneIndexDataBa
}

bool DataProducerSceneIndexInterfaceImp::addDataProducerSceneIndex(const PXR_NS::HdSceneIndexBaseRefPtr& customDataProducerSceneIndex,
PXR_NS::SdfPath& inoutPrefix,
const PXR_NS::SdfPath& preFix,
void* dccNode /*= nullptr*/,
const std::string& hydraViewportId /*= allViewports*/,
const std::string& rendererNames /*= allRenderers*/
)
{
PXR_NS::FVP_NS_DEF::DataProducerSceneIndexDataBaseRefPtr dataProducerSceneIndexData =
_CreateDataProducerSceneIndexData(customDataProducerSceneIndex, rendererNames, inoutPrefix, dccNode);
_CreateDataProducerSceneIndexData(customDataProducerSceneIndex, rendererNames, preFix, dccNode);
if (nullptr == dataProducerSceneIndexData){
return false;
}

inoutPrefix = dataProducerSceneIndexData->GetPrefix();

//PXR_NS::FvpViewportAPITokens->allViewports == hydraViewportId means the user wants customDataProducerSceneIndex to be applied in all viewports.
if (PXR_NS::FvpViewportAPITokens->allViewports == hydraViewportId){
//Apply this data producer scene index to all viewports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ class DataProducerSceneIndexInterfaceImp : public DataProducerSceneIndexInterfac
FVP_API PXR_NS::FVP_NS_DEF::DataProducerSceneIndexDataBaseRefPtr addUsdStageSceneIndex( PXR_NS::UsdImagingCreateSceneIndicesInfo& createInfo,
PXR_NS::HdSceneIndexBaseRefPtr& finalSceneIndex,
PXR_NS::UsdImagingStageSceneIndexRefPtr& stageSceneIndex,
PXR_NS::SdfPath& inoutPreFix,
const PXR_NS::SdfPath& preFix,
void* dccNode);

///Specific internal function for Usd Stages
FVP_API bool addUsdStageDataProducerSceneIndexDataBaseToAllViewports(PXR_NS::FVP_NS_DEF::DataProducerSceneIndexDataBaseRefPtr& dataProducerSceneIndexData);

///From FVP_NS_DEF::DataProducerSceneIndexInterface
bool addDataProducerSceneIndex(const PXR_NS::HdSceneIndexBaseRefPtr& customDataProducerSceneIndex,
PXR_NS::SdfPath& inoutPreFix,
const PXR_NS::SdfPath& preFix,
void* dccNode = nullptr,
const std::string& hydraViewportId = PXR_NS::FvpViewportAPITokens->allViewports,
const std::string& rendererNames = PXR_NS::FvpViewportAPITokens->allRenderers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,16 @@

//Local headers
#include "fvpDataProducerSceneIndexDataBase.h"
#include "flowViewport/sceneIndex/fvpParentDataModifierSceneIndex.h"
#include "flowViewport/sceneIndex/fvpPathInterfaceSceneIndex.h"
#include "flowViewport/API/fvpViewportAPITokens.h"

//Hydra headers
#include <pxr/imaging/hd/dirtyBitsTranslator.h>
#include <pxr/imaging/hd/tokens.h>
#include <pxr/imaging/hd/changeTracker.h>
#include <pxr/imaging/hd/retainedDataSource.h>
#include <pxr/imaging/hd/retainedSceneIndex.h>
#include <pxr/imaging/hd/flatteningSceneIndex.h>
#include <pxr/imaging/hd/prefixingSceneIndex.h>
#include <pxr/imaging/hd/mergingSceneIndex.h>
#include <pxr/imaging/hd/flattenedVisibilityDataSourceProvider.h>
#include <pxr/imaging/hd/flattenedXformDataSourceProvider.h>
#include <pxr/imaging/hd/flatteningSceneIndex.h>
#include <pxr/imaging/hd/flattenedDataSourceProviders.h>
#include <pxr/imaging/hd/visibilitySchema.h>
#include <pxr/imaging/hd/xformSchema.h>
#include <pxr/imaging/hd/primvarsSchema.h>
#include <pxr/imaging/hd/flattenedDataSourceProviders.h>
#include <pxr/imaging/hd/flattenedVisibilityDataSourceProvider.h>
#include <pxr/imaging/hd/flattenedXformDataSourceProvider.h>

PXR_NAMESPACE_OPEN_SCOPE

Expand All @@ -49,8 +40,6 @@ namespace FVP_NS_DEF {
*/
DataProducerSceneIndexDataBase::DataProducerSceneIndexDataBase(const CreationParameters& params)
{
_parentMatrix.SetIdentity();

_dataProducerSceneIndex = params._customDataProducerSceneIndex;
_prefix = params._prefix;
_lastSceneIndexChain = params._customDataProducerSceneIndex;
Expand All @@ -61,8 +50,6 @@ DataProducerSceneIndexDataBase::DataProducerSceneIndexDataBase(const CreationPar
//For Usd stages
DataProducerSceneIndexDataBase::DataProducerSceneIndexDataBase(const CreationParametersForUsdStage& params)
{
_parentMatrix.SetIdentity();

_dataProducerSceneIndex = nullptr;//Will be set later
_prefix = params._prefix;
_lastSceneIndexChain = nullptr;//Will be set later
Expand All @@ -72,100 +59,35 @@ DataProducerSceneIndexDataBase::DataProducerSceneIndexDataBase(const CreationPar

void DataProducerSceneIndexDataBase::UpdateHydraTransformFromParentPath()
{
if (! _parentDataModifierSceneIndex){
if (! _rootOverridesSceneIndex){
return;
}

//Update the matrix in the filtering scene index
_parentDataModifierSceneIndex->SetParentTransformMatrix(_parentMatrix);

//This is still a WIP trying to dirty the prims correctly...
/*HdDataSourceLocatorSet locators;
locators.append(HdXformSchema::GetDefaultLocator());
locators.append(HdVisibilitySchema::GetDefaultLocator());
locators.append(HdPrimvarsSchema::GetDefaultLocator());
_retainedSceneIndex->DirtyPrims({{ _parentPath, locators}});
*/

//Set the transform prim as dirty so it gets recomputed
/*HdDataSourceLocatorSet locators;
HdDirtyBitsTranslator::RprimDirtyBitsToLocatorSet(HdTokens->transform, HdChangeTracker::AllSceneDirtyBits, &locators);
//Set dirty this field in the retained scene index for the parent prim
//_retainedSceneIndex->DirtyPrims( { {_parentPath, locators} } );
*/

//Update by removing the prim and adding it again
RemoveParentPrimFromSceneIndex();
AddParentPrimToSceneIndex();
_rootOverridesSceneIndex->SetRootTransform(_parentMatrix);
}

void DataProducerSceneIndexDataBase::UpdateVisibilityFromDCCNode(bool isVisible)
{
if (! _parentDataModifierSceneIndex){
return;
}
//Update the visibility in the filtering scene index
_parentDataModifierSceneIndex->SetParentVisibility(isVisible);

//This is still a WIP trying to dirty the prims correctly...
/* //Set the transform prim as dirty so it gets recomputed
HdDataSourceLocatorSet locators;
HdDirtyBitsTranslator::RprimDirtyBitsToLocatorSet(HdTokens->transform, HdChangeTracker::DirtyVisibility, &locators);
//Set dirty this field in the retained scene index for the parent prim
//_retainedSceneIndex->DirtyPrims({{_parentPath, locators}});
*/

//Update by removing the prim and adding it again
RemoveParentPrimFromSceneIndex();
AddParentPrimToSceneIndex();
}

void DataProducerSceneIndexDataBase::AddParentPrimToSceneIndex()
{
if(_visible){
if (! _rootOverridesSceneIndex){
return;
}

//Arrays of added prims
HdRetainedSceneIndex::AddedPrimEntries addedPrims;

//We are creating a XForm prim which has only 2 attributes a matrix and a visibility.
//This prim will be the parent of all data producer scene index primitives so we can change their transform or visibility from the parent
HdRetainedSceneIndex::AddedPrimEntry parentPrimEntry;
parentPrimEntry.primPath = _prefix;
parentPrimEntry.primType = HdTokens->transform;
parentPrimEntry.dataSource = HdRetainedContainerDataSource::New(
HdXformSchemaTokens->xform,
HdXformSchema::Builder().SetMatrix(HdRetainedTypedSampledDataSource<GfMatrix4d>::New(_parentMatrix)).Build(),

HdVisibilitySchemaTokens->visibility,
HdVisibilitySchema::BuildRetained(HdRetainedTypedSampledDataSource<bool>::New(true))
);

addedPrims.emplace_back(parentPrimEntry);


//Add new prims to the scene index
_retainedSceneIndex->AddPrims(addedPrims);
_visible = true;
}

void DataProducerSceneIndexDataBase::RemoveParentPrimFromSceneIndex()
{
if(!_visible){
return;
}

_retainedSceneIndex->RemovePrims({ _prefix});
_visible = false;
_rootOverridesSceneIndex->SetRootVisibility(isVisible);
}

void DataProducerSceneIndexDataBase::_CreateSceneIndexChainForDataProducerSceneIndex()
{
if (_dccNode){
_CreateSceneIndexChainForDataProducerSceneIndexWithDCCNode(_dataProducerSceneIndex);
//Add a flattening scene index so that the root override scene index is applied (flatten only visibility and xform)
using namespace HdMakeDataSourceContainingFlattenedDataSourceProvider;
static HdContainerDataSourceHandle const flattenDataSource =
HdRetainedContainerDataSource::New(
HdVisibilitySchema::GetSchemaToken(),
Make<HdFlattenedVisibilityDataSourceProvider>(),
HdXformSchema::GetSchemaToken(),
Make<HdFlattenedXformDataSourceProvider>());
_lastSceneIndexChain = HdFlatteningSceneIndex::New(_lastSceneIndexChain, flattenDataSource);
}
else{
_CreateSceneIndexChainForDataProducerSceneIndexWithoutDCCNode(_dataProducerSceneIndex);
Expand All @@ -177,14 +99,17 @@ void DataProducerSceneIndexDataBase::_CreateSceneIndexChainForDataProducerSceneI
HdSceneIndexBaseRefPtr DataProducerSceneIndexDataBase::_CreateUsdStageSceneIndexChain(HdSceneIndexBaseRefPtr const & inputStageSceneIndex)
{
_CreateSceneIndexChainForDataProducerSceneIndexWithDCCNode(inputStageSceneIndex);
UpdateHydraTransformFromParentPath();//Update the transform, this is useful when deleting the node and undoing it
return _lastSceneIndexChain;
}

void DataProducerSceneIndexDataBase::_CreateSceneIndexChainForUsdStageSceneIndex(CreationParametersForUsdStage& params)
{
//Set the overridesSceneIndexCallback to insert our scene indices chain after the stage scene index and before the flatten scene index
//If we don't do so, we cannot add a parent which will apply its matrix to the children because of the flatten scene index in the usd stage chain.
params._createInfo.overridesSceneIndexCallback = std::bind(&DataProducerSceneIndexDataBase::_CreateUsdStageSceneIndexChain, this, std::placeholders::_1);
if (params._dccNode){
//Set the overridesSceneIndexCallback to insert our scene indices chain after the stage scene index and before the flatten scene index
//If we don't do so, we cannot modify the transform or visibility to the children because of the flatten scene index in the usd stage chain.
params._createInfo.overridesSceneIndexCallback = std::bind(&DataProducerSceneIndexDataBase::_CreateUsdStageSceneIndexChain, this, std::placeholders::_1);
}

//Create the scene indices chain
UsdImagingSceneIndices sceneIndices = UsdImagingCreateSceneIndices(params._createInfo);
Expand All @@ -196,42 +121,8 @@ void DataProducerSceneIndexDataBase::_CreateSceneIndexChainForUsdStageSceneIndex

void DataProducerSceneIndexDataBase::_CreateSceneIndexChainForDataProducerSceneIndexWithDCCNode(HdSceneIndexBaseRefPtr const & inputSceneIndex)
{
//Create a parent path to parent the whole inputSceneIndex prims, try to use the DCC node name
std::string nodeName = GetDCCNodeName();

if (nodeName.empty()){
_prefix = _prefix.AppendPath(SdfPath(TfStringPrintf("DataProducerSI_%p", &(*inputSceneIndex))));
}else{
//A nodeName was provided by the DCC implementation, and it was sanitized for Hydra
_prefix = _prefix.AppendPath(SdfPath(nodeName));
}

//Create a retainedsceneindex to inject a parent path to the be the parent of inputSceneIndex
_retainedSceneIndex = HdRetainedSceneIndex::New();
// Add a prim inside which will be the parent of inputSceneIndex, its SdfPath will updated in _inOutData._parentPath
AddParentPrimToSceneIndex();

//Create a filtering scene index to update the information (transform, visibility,...) from the parent prim.
_parentDataModifierSceneIndex = ParentDataModifierSceneIndex::New(_retainedSceneIndex, _prefix, _parentMatrix, true);

//Add a prefixing scene index to inputSceneIndex to set the parent which we added to the retainedsceneindex
HdPrefixingSceneIndexRefPtr prefixingSceneIndex = HdPrefixingSceneIndex::New(inputSceneIndex, _prefix);

//Use a merging scene index to merge the prefixing and the retainedsceneindex
HdMergingSceneIndexRefPtr mergingSceneIndex = HdMergingSceneIndex::New();
mergingSceneIndex->AddInputScene(_parentDataModifierSceneIndex, SdfPath::AbsoluteRootPath());
mergingSceneIndex->AddInputScene(prefixingSceneIndex, SdfPath::AbsoluteRootPath());

//Add a flattening scene index to the merging scene index, flatten only transform and visibility
static HdContainerDataSourceHandle const flattenedTransformAndVisibilityDataSourceHandle =
HdRetainedContainerDataSource::New(
HdVisibilitySchema::GetSchemaToken(),
HdMakeDataSourceContainingFlattenedDataSourceProvider::Make<HdFlattenedVisibilityDataSourceProvider>(),
HdXformSchema::GetSchemaToken(),
HdMakeDataSourceContainingFlattenedDataSourceProvider::Make<HdFlattenedXformDataSourceProvider>()
);

_lastSceneIndexChain = HdFlatteningSceneIndex::New(mergingSceneIndex, flattenedTransformAndVisibilityDataSourceHandle);//Flattening scene index to apply transform/visibility on children
_rootOverridesSceneIndex = UsdImagingRootOverridesSceneIndex::New(inputSceneIndex);
_lastSceneIndexChain = _rootOverridesSceneIndex;
}

void DataProducerSceneIndexDataBase::_CreateSceneIndexChainForDataProducerSceneIndexWithoutDCCNode(HdSceneIndexBaseRefPtr const & inputSceneIndex)
Expand All @@ -242,7 +133,6 @@ void DataProducerSceneIndexDataBase::_CreateSceneIndexChainForDataProducerSceneI
sceneIndex = HdPrefixingSceneIndex::New(sceneIndex, _prefix);
}

//Add a PathInterfaceSceneIndex for selection
_lastSceneIndexChain = sceneIndex;
}

Expand Down
Loading

0 comments on commit a4fc977

Please sign in to comment.