HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
coordSysPrimSceneIndex.h
Go to the documentation of this file.
1 //
2 // Copyright 2023 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HDSI_COORD_SYS_PRIM_SCENE_INDEX_H
8 #define PXR_IMAGING_HDSI_COORD_SYS_PRIM_SCENE_INDEX_H
9 
10 #include "pxr/imaging/hdsi/api.h"
11 
13 
15 
17 
18 ///
19 /// \class HdsiCoordSysPrimSceneIndex
20 ///
21 /// If prim P has a coord sys binding FOO to another prim Q, the scene
22 /// index will add a coord sys prim Q.__coordSys:FOO under Q.
23 /// It will rewrite the coord sys binding on P to point to
24 /// Q.__coordSys:FOO and use Q's xform data source for Q.__coordSys:FOO.
25 ///
26 /// Motivation: USD allows for a coord sys binding to target any xformable
27 /// prim. Some render delegates, however, only allow a coord sys binding
28 /// to point to a prim of type coord sys. This scene index is creating prim's
29 /// of that type for such render delegates.
30 ///
31 /// For compatibility with the UsdImagingDelegate which is already adding a
32 /// coord sys prim under Q itself using a property path, we ignore coord sys
33 /// bindings to paths which are not prim paths.
34 ///
35 /// An example:
36 ///
37 /// Input to scene index:
38 ///
39 /// /MyXform
40 /// dataSource:
41 /// xform: [ some xform ]
42 /// /MyPrim
43 /// dataSource:
44 /// coordSysBinding:
45 /// FOO: /MyXform
46 ///
47 /// Will be transformed to:
48 ///
49 /// /MyXform
50 /// dataSource:
51 /// xform: [ some xform ]
52 /// /MyXform.__coordSys:FOO
53 /// dataSource:
54 /// coordSys:
55 /// name: FOO
56 /// xform: [ as above ]
57 /// /MyPrim
58 /// dataSource:
59 /// coordSysBinding:
60 /// FOO: /MyXform.__coordSys:FOO
61 ///
63 {
64 public:
65 
66  /// Creates a new coord sys prim scene index.
67  ///
68  static HdsiCoordSysPrimSceneIndexRefPtr New(
69  HdSceneIndexBaseRefPtr const &inputScene)
70  {
71  return TfCreateRefPtr(
72  new HdsiCoordSysPrimSceneIndex(inputScene));
73  }
74 
75  HDSI_API
76  HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override;
77 
78  HDSI_API
79  SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override;
80 
81 protected:
82 
83  HDSI_API
85  HdSceneIndexBaseRefPtr const &inputScene);
86 
87  // satisfying HdSingleInputFilteringSceneIndexBase
88  void _PrimsAdded(
89  const HdSceneIndexBase &sender,
90  const HdSceneIndexObserver::AddedPrimEntries &entries) override;
91 
92  void _PrimsRemoved(
93  const HdSceneIndexBase &sender,
94  const HdSceneIndexObserver::RemovedPrimEntries &entries) override;
95 
96  void _PrimsDirtied(
97  const HdSceneIndexBase &sender,
98  const HdSceneIndexObserver::DirtiedPrimEntries &entries) override;
99 
100  struct _Binding
101  {
104  };
105  using _Bindings = std::vector<_Binding>;
106  using _PrimToBindings = std::map<SdfPath, _Bindings>;
107 
108  // Record coordSys bindings of prim at primPath. That is, add entries to the
109  // below data structures if needed and increase ref-counts.
110  // Optionally, return prims of type coord system that this scene index needs
111  // to add.
112  void _AddBindingsForPrim(const SdfPath &primPath,
113  SdfPathSet * addedCoordSysPrims = nullptr);
114  // Remove coordSys bindings. That is, decrease ref-counts and remove entries
115  // from below data structures if needed.
116  // Optionally, return prims of type coord system that this scene index needs
117  // to remove.
118  void _RemoveBindings(const _Bindings &bindings,
119  SdfPathSet * removedCoordSysPrims);
120  // Similar to above, but give the prim path explicitly to look-up bindings
121  // in map.
122  void _RemoveBindingsForPrim(const SdfPath &primPath,
123  SdfPathSet * removedCoordSysPrims);
124  // Removes bindings for given prim and all its descendants stored in below
125  // data structures.
126  void _RemoveBindingsForSubtree(const SdfPath &primPath,
127  SdfPathSet * removedCoordSysPrims);
128 
129  // If path is for coord sys prim added by this scene index, give the
130  // prim source for it.
131  HdContainerDataSourceHandle _GetCoordSysPrimSource(
132  const SdfPath &primPath) const;
133 
134 private:
135  using _NameToRefCount =
136  std::unordered_map<TfToken, size_t, TfToken::HashFunctor>;
137  using _PrimToNameToRefCount =
138  std::unordered_map<SdfPath, _NameToRefCount, SdfPath::Hash>;
139  // Maps prim which is targeted by coord sys binding to name of binding to
140  // count how many bindings are referencing that prim using that name.
141  //
142  // We delete an inner entry when there is no longer any coord sys binding
143  // with that name targeting the prim.
144  // We delete a prim when it is no longer targeted by any binding.
145  //
146  // This map is used to determine which coord sys prims we need to create
147  // under the targeted prim.
148  //
149  //
150  // In the above example, the content of the map will be:
151  //
152  // {
153  // /MyXform: {
154  // FOO: 1
155  // }
156  // }
157  //
158  _PrimToNameToRefCount _targetedPrimToNameToRefCount;
159 
160  // Maps prim to the coord sys bindings of that prim.
161  //
162  // Used to decrease ref counts when a prim gets deleted or modified.
163  //
164  // In the above example, the content of the map will be:
165  //
166  // { /MyPrim: [(FOO, /MyXform)] }
167  //
168  _PrimToBindings _primToBindings;
169 };
170 
172 
173 #endif
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1190
HdContainerDataSourceHandle _GetCoordSysPrimSource(const SdfPath &primPath) const
void _RemoveBindingsForSubtree(const SdfPath &primPath, SdfPathSet *removedCoordSysPrims)
HDSI_API SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override
void _PrimsAdded(const HdSceneIndexBase &sender, const HdSceneIndexObserver::AddedPrimEntries &entries) override
void _RemoveBindingsForPrim(const SdfPath &primPath, SdfPathSet *removedCoordSysPrims)
void _RemoveBindings(const _Bindings &bindings, SdfPathSet *removedCoordSysPrims)
void _AddBindingsForPrim(const SdfPath &primPath, SdfPathSet *addedCoordSysPrims=nullptr)
Definition: token.h:70
std::vector< class SdfPath > SdfPathVector
HDSI_API HdsiCoordSysPrimSceneIndex(HdSceneIndexBaseRefPtr const &inputScene)
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_REF_PTRS(HdsiCoordSysPrimSceneIndex)
void _PrimsRemoved(const HdSceneIndexBase &sender, const HdSceneIndexObserver::RemovedPrimEntries &entries) override
Definition: path.h:273
std::vector< _Binding > _Bindings
#define HDSI_API
Definition: api.h:23
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:192
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HDSI_API HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
static HdsiCoordSysPrimSceneIndexRefPtr New(HdSceneIndexBaseRefPtr const &inputScene)
std::map< SdfPath, _Bindings > _PrimToBindings
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void _PrimsDirtied(const HdSceneIndexBase &sender, const HdSceneIndexObserver::DirtiedPrimEntries &entries) override