Skip to content

Commit

Permalink
Update to USD 24.05
Browse files Browse the repository at this point in the history
  • Loading branch information
ppt-adsk committed May 8, 2024
1 parent 942adf4 commit 888bbd7
Showing 1 changed file with 172 additions and 16 deletions.
188 changes: 172 additions & 16 deletions lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndexUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,182 @@
#include "mayaHydraDataSource.h"

#include <pxr/imaging/hd/retainedDataSource.h>
#include <pxr/imaging/hd/basisCurvesSchema.h>
#include <pxr/imaging/hd/basisCurvesTopologySchema.h>
#include <pxr/imaging/hd/cameraSchema.h>
#include <pxr/imaging/hd/legacyDisplayStyleSchema.h>
#include <pxr/imaging/hd/lightSchema.h>
#include <pxr/imaging/hd/materialBindingSchema.h>
#include <pxr/imaging/hd/materialBindingsSchema.h>
#include <pxr/imaging/hd/materialConnectionSchema.h>
#include <pxr/imaging/hd/materialNetworkSchema.h>
#include <pxr/imaging/hd/materialNodeSchema.h>
#include <pxr/imaging/hd/materialSchema.h>
#include <pxr/imaging/hd/meshSchema.h>
#include <pxr/imaging/hd/meshTopologySchema.h>
#include <pxr/imaging/hd/primvarSchema.h>
#include <pxr/imaging/hd/primvarsSchema.h>
#include <pxr/imaging/hd/visibilitySchema.h>
#include <pxr/imaging/hd/volumeFieldSchema.h>
#include <pxr/imaging/hd/xformSchema.h>
#include <pxr/imaging/hd/extentSchema.h>
#include "pxr/imaging/hd/materialNodeParameterSchema.h"

PXR_NAMESPACE_OPEN_SCOPE

#if PXR_VERSION > 2311
static bool
_ConvertHdMaterialNetworkToHdDataSources(
const HdMaterialNetworkMap &hdNetworkMap,
HdContainerDataSourceHandle *result)
{
HD_TRACE_FUNCTION();

TfTokenVector terminalsNames;
std::vector<HdDataSourceBaseHandle> terminalsValues;
std::vector<TfToken> nodeNames;
std::vector<HdDataSourceBaseHandle> nodeValues;

struct ParamData {
VtValue value;
TfToken colorSpace;
};

for (auto const &iter: hdNetworkMap.map) {
const TfToken &terminalName = iter.first;
const HdMaterialNetwork &hdNetwork = iter.second;

if (hdNetwork.nodes.empty()) {
continue;
}

terminalsNames.push_back(terminalName);

// Transfer over individual nodes.
// Note that the same nodes may be shared by multiple terminals.
// We simply overwrite them here.
for (const HdMaterialNode &node : hdNetwork.nodes) {
std::vector<TfToken> paramsNames;
std::vector<HdDataSourceBaseHandle> paramsValues;

// Gather parameter value and colorspace metadata in paramsInfo, a
// mapping of the parameter name to its value and colorspace data.
std::map<std::string, ParamData> paramsInfo;
for (const auto &p : node.parameters) {

// Strip "colorSpace" prefix
const std::pair<std::string, bool> res =
SdfPath::StripPrefixNamespace(p.first,
HdMaterialNodeParameterSchemaTokens->colorSpace);

// Colorspace metadata
if (res.second) {
paramsInfo[res.first].colorSpace = p.second.Get<TfToken>();
}
// Value
else {
paramsInfo[p.first].value = p.second.Get<VtValue>();
}
}

// Create and store the HdMaterialNodeParameter DataSource
for (const auto &item : paramsInfo) {
paramsNames.push_back(TfToken(item.first));
paramsValues.push_back(
HdMaterialNodeParameterSchema::Builder()
.SetValue(
HdRetainedTypedSampledDataSource<VtValue>::New(
item.second.value))
.SetColorSpace(item.second.colorSpace.IsEmpty()
? nullptr
: HdRetainedTypedSampledDataSource<TfToken>::New(
item.second.colorSpace))
.Build()
);
}

// Accumulate array connections to the same input
TfDenseHashMap<TfToken,
TfSmallVector<HdDataSourceBaseHandle, 8>, TfToken::HashFunctor>
connectionsMap;

TfSmallVector<TfToken, 8> cNames;
TfSmallVector<HdDataSourceBaseHandle, 8> cValues;

for (const HdMaterialRelationship &rel : hdNetwork.relationships) {
if (rel.outputId == node.path) {
TfToken outputPath = rel.inputId.GetToken();
TfToken outputName = TfToken(rel.inputName.GetString());

HdDataSourceBaseHandle c =
HdMaterialConnectionSchema::Builder()
.SetUpstreamNodePath(
HdRetainedTypedSampledDataSource<TfToken>::New(
outputPath))
.SetUpstreamNodeOutputName(
HdRetainedTypedSampledDataSource<TfToken>::New(
outputName))
.Build();

connectionsMap[
TfToken(rel.outputName.GetString())].push_back(c);
}
}

cNames.reserve(connectionsMap.size());
cValues.reserve(connectionsMap.size());

// NOTE: not const because HdRetainedSmallVectorDataSource needs
// a non-const HdDataSourceBaseHandle*
for (auto &entryPair : connectionsMap) {
cNames.push_back(entryPair.first);
cValues.push_back(
HdRetainedSmallVectorDataSource::New(
entryPair.second.size(), entryPair.second.data()));
}

nodeNames.push_back(node.path.GetToken());
nodeValues.push_back(
HdMaterialNodeSchema::Builder()
.SetParameters(
HdRetainedContainerDataSource::New(
paramsNames.size(),
paramsNames.data(),
paramsValues.data()))
.SetInputConnections(
HdRetainedContainerDataSource::New(
cNames.size(),
cNames.data(),
cValues.data()))
.SetNodeIdentifier(
HdRetainedTypedSampledDataSource<TfToken>::New(
node.identifier))
.Build());
}

terminalsValues.push_back(
HdMaterialConnectionSchema::Builder()
.SetUpstreamNodePath(
HdRetainedTypedSampledDataSource<TfToken>::New(
hdNetwork.nodes.back().path.GetToken()))
.SetUpstreamNodeOutputName(
HdRetainedTypedSampledDataSource<TfToken>::New(
terminalsNames.back()))
.Build());
}

HdContainerDataSourceHandle nodesDefaultContext =
HdRetainedContainerDataSource::New(
nodeNames.size(),
nodeNames.data(),
nodeValues.data());

HdContainerDataSourceHandle terminalsDefaultContext =
HdRetainedContainerDataSource::New(
terminalsNames.size(),
terminalsNames.data(),
terminalsValues.data());

// Create the material network, potentially one per network selector
HdDataSourceBaseHandle network = HdMaterialNetworkSchema::Builder()
.SetNodes(nodesDefaultContext)
.SetTerminals(terminalsDefaultContext)
.Build();

TfToken defaultContext = HdMaterialSchemaTokens->universalRenderContext;
*result = HdMaterialSchema::BuildRetained(
1,
&defaultContext,
&network);

return true;
}
#else
static bool
_ConvertHdMaterialNetworkToHdDataSources(
const HdMaterialNetworkMap &hdNetworkMap,
Expand Down Expand Up @@ -168,6 +322,8 @@ _ConvertHdMaterialNetworkToHdDataSources(
return true;
}

#endif

PXR_NAMESPACE_CLOSE_SCOPE

#endif // MAYAHYDRASCENEINDEXUTILS_H
#endif // MAYAHYDRASCENEINDEXUTILS_H

0 comments on commit 888bbd7

Please sign in to comment.