7#ifndef EXT_RMANPKG_PLUGIN_RENDERMAN_PLUGIN_HD_PRMAN_INSTANCER_H
8#define EXT_RMANPKG_PLUGIN_RENDERMAN_PLUGIN_HD_PRMAN_INSTANCER_H
10#include "hdPrman/concurrentMap.h"
11#include "hdPrman/renderParam.h"
12#include "hdPrman/rixStrings.h"
14#include "pxr/imaging/hd/instancer.h"
15#include "pxr/imaging/hd/sceneDelegate.h"
16#include "pxr/imaging/hd/timeSampleArray.h"
17#include "pxr/imaging/hd/types.h"
19#include "pxr/usd/sdf/path.h"
23#include "pxr/base/tf/hashmap.h"
26#include "pxr/base/vt/value.h"
32#include <RiTypesHelper.h>
34#include <tbb/queuing_mutex.h>
35#include <tbb/spin_mutex.h>
41#include <initializer_list>
43#include <unordered_map>
44#include <unordered_set>
47#define HDPRMAN_INSTANCER_MAX_TIME_SAMPLES 1
49PXR_NAMESPACE_OPEN_SCOPE
63 HdDirtyBits *dirtyBits)
override;
67 HdDirtyBits GetInitialDirtyBitsMask()
const override;
118 const HdDirtyBits& dirtyBits,
119 const SdfPath& hydraPrototypeId,
120 const std::vector<riley::GeometryPrototypeId>& rileyPrototypeIds,
121 const riley::CoordinateSystemList& coordSysList,
122 const RtParamList& prototypeParams,
125 const std::vector<riley::MaterialId>& rileyMaterialIds,
126 const SdfPathVector& prototypePaths,
127 const riley::LightShaderId& lightShaderId = riley::LightShaderId::InvalidId());
142 const SdfPath& prototypePrimPath,
143 const std::vector<riley::GeometryPrototypeId>& protoIdsToKeep = {});
156 struct _RtParamListHashFunctor
158 size_t operator()(
const RtParamList& params)
const noexcept
161 RtParamList copy = params;
162 return TfHash()(copy.Hash());
166 struct _RtParamListEqualToFunctor
168 bool operator()(
const RtParamList& lhs,
const RtParamList& rhs)
const noexcept
170 return _RtParamListHashFunctor()(lhs) == _RtParamListHashFunctor()(rhs);
184 std::unordered_set<TfToken, TfToken::HashFunctor> categories;
196 _FlattenData() =
default;
197 _FlattenData(
const VtTokenArray& cats)
198 : categories(cats.begin(), cats.end()) { }
199 _FlattenData(
const VtTokenArray& cats,
bool vis)
200 : categories(cats.begin(), cats.end())
205 _FlattenData(
const _FlattenData& other)
206 : categories(other.categories.cbegin(), other.categories.cend())
207 , renderTag(other.renderTag)
209 params.Update(other.params);
215 void Inherit(
const _FlattenData& rhs)
217 categories.insert(rhs.categories.cbegin(), rhs.categories.cend());
218 params.Inherit(rhs.params);
220 renderTag = rhs.renderTag;
227 void Update(
const _FlattenData& rhs)
229 categories.insert(rhs.categories.cbegin(), rhs.categories.cend());
230 params.Update(rhs.params);
231 if (!rhs.renderTag.IsEmpty()) {
232 renderTag = rhs.renderTag;
239 void UpdateVisAndFilterParamList(RtParamList& other) {
241 for (
const RtUString& param : _GetVisibilityParams()) {
243 if (other.GetInteger(param, val)) {
245 params.Remove(param);
247 params.SetInteger(param, val);
260 RtUString groupingMembership;
261 if (other.GetString(RixStr.k_grouping_membership, groupingMembership)) {
262 params.SetString(RixStr.k_grouping_membership, groupingMembership);
268 for (
const RtUString& param : _GetLightLinkParams()) {
274 void SetVisibility(
bool visible) {
276 for (
const RtUString& param : _GetVisibilityParams()) {
277 params.Remove(param);
280 for (
const RtUString& param : _GetVisibilityParams()) {
281 params.SetInteger(param, 0);
287 bool operator==(
const _FlattenData& rhs)
const noexcept
289 return categories == rhs.categories &&
290 _RtParamListEqualToFunctor()(params, rhs.params) &&
291 renderTag == rhs.renderTag;
295 size_t operator()(
const _FlattenData& fd)
const noexcept
300 for (
const TfToken& tok : fd.categories) {
303 hash ^= _RtParamListHashFunctor()(fd.params);
304 hash ^=
TfHash()(fd.renderTag.GetString());
309 std::string ToString()
const;
312 static std::vector<RtUString> _GetLightLinkParams()
316 static const std::vector<RtUString> LightLinkParams = {
317 RixStr.k_lightfilter_subset,
318 RixStr.k_lighting_subset,
319 RixStr.k_grouping_membership,
320 RixStr.k_lighting_excludesubset
322 return LightLinkParams;
325 static std::vector<RtUString> _GetVisibilityParams()
329 static const std::vector<RtUString> VisParams = {
330 RixStr.k_visibility_camera,
331 RixStr.k_visibility_indirect,
332 RixStr.k_visibility_transmission
341 _FlattenData flattenData;
343 _GfMatrixSA transform;
345 _InstanceData() =
default;
346 _InstanceData(
const RtParamList& p,
const _GfMatrixSA& xform)
353 using _FlattenGroupMap = HdPrmanConcurrentMap<
355 riley::GeometryPrototypeId,
356 _FlattenData::HashFunctor>;
358 struct _RileyInstanceId
360 riley::GeometryPrototypeId groupId;
361 riley::GeometryInstanceId geoInstanceId;
362 riley::LightInstanceId lightInstanceId;
365 using _InstanceIdVec = std::vector<_RileyInstanceId>;
369 size_t operator()(
const riley::GeometryPrototypeId&
id)
const noexcept
371 return std::hash<uint32_t>()(
id.AsUInt32());
375 using _ProtoInstMap = std::unordered_map<
376 riley::GeometryPrototypeId,
380 using _ProtoGroupCounterMap = HdPrmanConcurrentMap<
381 riley::GeometryPrototypeId,
385 struct _ProtoMapEntry
391 struct _ChildPopulateLockMapKey
397 size_t operator()(
const _ChildPopulateLockMapKey& key)
const
400 key.childInstancerId, key.prototypePrimPath);
404 bool operator==(
const _ChildPopulateLockMapKey& other)
const
406 return childInstancerId == other.childInstancerId &&
407 prototypePrimPath == other.prototypePrimPath;
411 using _ProtoMap = HdPrmanConcurrentMap<
412 SdfPath, _ProtoMapEntry, SdfPath::Hash>;
414 using _ChildPopulateLockMap = HdPrmanConcurrentMap<
415 _ChildPopulateLockMapKey,
417 _ChildPopulateLockMapKey::Hash>;
424 void _SyncPrimvars(
const HdDirtyBits* dirtyBits);
427 void _SyncTransforms(
const HdDirtyBits* dirtyBits, HdPrman_RenderParam *);
430 void _SyncCategories(
const HdDirtyBits* dirtyBits);
433 void _SyncVisibility(
const HdDirtyBits* dirtyBits);
436 void _SyncRenderTag(
const HdDirtyBits* dirtyBits);
440 void _ComposeInstances(
442 const std::vector<_InstanceData>& subInstances,
443 std::vector<_InstanceData>& instances);
448 void _ComposeInstanceFlattenData(
450 RtParamList& instanceParams,
452 const _FlattenData& fromBelow = _FlattenData());
459 void _ComposePrototypeData(
461 const RtParamList& globalProtoParams,
463 const std::vector<riley::GeometryPrototypeId>& protoIds,
464 const SdfPathVector& subProtoPaths,
465 const std::vector<_FlattenData>& subProtoFlats,
466 std::vector<RtParamList>& protoParams,
467 std::vector<_FlattenData>& protoFlats);
474 bool _GatherChangedPrototypeIds(
475 const _ProtoMapEntry& protoMapEntry,
476 const std::vector<riley::GeometryPrototypeId>& inputProtoIds,
477 std::vector<riley::GeometryPrototypeId>& newProtoIds,
478 std::vector<riley::GeometryPrototypeId>& deadProtoIds);
482 void _SetPrototypesDirty();
493 void _PopulateInstances(
495 const HdDirtyBits& dirtyBits,
496 const SdfPath& hydraPrototypeId,
497 const SdfPath& prototypePrimPath,
498 const std::vector<riley::GeometryPrototypeId>& rileyPrototypeIds,
499 const riley::CoordinateSystemList& coordSysList,
500 const RtParamList& prototypeParams,
503 const std::vector<riley::MaterialId>& rileyMaterialIds,
504 const SdfPathVector& prototypePaths,
505 const riley::LightShaderId& lightShaderId,
506 const std::vector<_InstanceData>& subInstances,
507 const std::vector<_FlattenData>& prototypeFlats);
513 void _PopulateInstancesForChild(
515 const HdDirtyBits& dirtyBits,
516 const SdfPath& childInstancerId,
517 const SdfPath& prototypePrimPath,
518 const std::vector<riley::GeometryPrototypeId>& rileyPrototypeIds,
519 const riley::CoordinateSystemList& coordSysList,
520 const RtParamList& prototypeParams,
523 const std::vector<riley::MaterialId>& rileyMaterialIds,
524 const SdfPathVector& prototypePaths,
525 const riley::LightShaderId& lightShaderId,
526 const std::vector<_InstanceData>& subInstances,
527 const std::vector<_FlattenData>& prototypeFlats);
529 void _DepopulateInstances(
531 const SdfPath& prototypePrimPath,
532 const std::vector<riley::GeometryPrototypeId>& protoIdsToKeep = {});
536 void _DepopulateInstancesForChild(
538 const SdfPath& childInstancerId,
539 const SdfPath& prototypePrimPath,
540 const std::vector<riley::GeometryPrototypeId>& protoIdsToKeep = {});
543 HdPrmanInstancer* _GetParentInstancer();
548 void _ResizeProtoMap(
550 const SdfPath& prototypePrimPath,
551 const std::vector<riley::GeometryPrototypeId>& rileyPrototypeIds,
556 bool _CleanDisusedGroupIds(HdPrman_RenderParam* param);
565 bool _AcquireGroupIdAndIncrementCounter(
566 HdPrman_RenderParam* param,
567 const _FlattenData& flattenGroup,
568 riley::GeometryPrototypeId& groupId);
572 void _GetInstanceParams(
573 size_t instanceIndex,
574 RtParamList& params);
577 void _GetPrototypeParams(
584 void _GetInstanceTransform(
585 size_t instanceIndex,
587 const _GfMatrixSA& left = _GfMatrixSA());
598 void _IncrementGroupCounter(
const riley::GeometryPrototypeId&
id);
602 void _DecrementGroupCounter(
const riley::GeometryPrototypeId&
id);
612 riley::CoordinateSystemList _coordSysList = { 0,
nullptr };
616 std::vector<VtTokenArray> _instanceCategories;
619 _FlattenData _instancerFlat;
622 TfHashMap<TfToken, _PrimvarValue, TfToken::HashFunctor> _primvarMap;
631 _FlattenGroupMap _groupMap;
635 _ProtoGroupCounterMap _groupCounters;
640 tbb::spin_mutex _groupIdAcquisitionLock;
657 _ChildPopulateLockMap _childPopulateLocks;
660PXR_NAMESPACE_CLOSE_SCOPE
Defines all the types "TYPED" for which Vt creates a VtTYPEDArray typedef.
This class exists to facilitate point cloud style instancing.
The HdRenderParam is an opaque (to core Hydra) handle, to an object that is obtained from the render ...
Adapter class providing data exchange with the client scene graph.
A path value used to locate objects in layers or scenegraphs.
A user-extensible hashing mechanism for use with runtime hash tables.
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Token for efficient comparison, assignment, and hashing of known strings.
bool IsEmpty() const
Returns true iff this token contains the empty string "".
std::string const & GetString() const
Return the string that this token represents.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
An array of a value sampled over time, in struct-of-arrays layout.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...