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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HDSI_COORD_SYS_PRIM_SCENE_INDEX_H
25 #define PXR_IMAGING_HDSI_COORD_SYS_PRIM_SCENE_INDEX_H
26 
27 #include "pxr/imaging/hdsi/api.h"
28 
30 
32 
34 
35 ///
36 /// \class HdsiCoordSysPrimSceneIndex
37 ///
38 /// If prim P has a coord sys binding FOO to another prim Q, the scene
39 /// index will add a coord sys prim Q.__coordSys:FOO under Q.
40 /// It will rewrite the coord sys binding on Q to point to
41 /// Q.__coordSys:FOO and use Q's xform data source for Q.__coordSys:FOO.
42 ///
43 /// Motivation: USD allows for a coord sys binding to target any xformable
44 /// prim. Some render delegates, however, only allow a coord sys binding
45 /// to point to a prim of type coord sys. This scene index is creating prim's
46 /// of that type for such render delegates.
47 ///
48 /// For compatibility with the UsdImagingDelegate which is already adding a
49 /// coord sys prim under Q itself using a property path, we ignore coord sys
50 /// bindings to paths which are not prim paths.
51 ///
52 /// An example:
53 ///
54 /// Input to scene index:
55 ///
56 /// /MyXform
57 /// dataSource:
58 /// xform: [ some xform ]
59 /// /MyPrim
60 /// dataSource:
61 /// coordSysBinding:
62 /// FOO: /MyXform
63 ///
64 /// Will be transformed to:
65 ///
66 /// /MyXform
67 /// dataSource:
68 /// xform: [ some xform ]
69 /// /MyXform.__coordSys:FOO
70 /// dataSource:
71 /// coordSys:
72 /// name: FOO
73 /// xform: [ as above ]
74 /// /MyPrim
75 /// dataSource:
76 /// coordSysBinding:
77 /// FOO: /MyXform.__coordSys:FOO
78 ///
80 {
81 public:
82 
83  /// Creates a new coord sys prim scene index.
84  ///
85  static HdsiCoordSysPrimSceneIndexRefPtr New(
86  HdSceneIndexBaseRefPtr const &inputScene)
87  {
88  return TfCreateRefPtr(
89  new HdsiCoordSysPrimSceneIndex(inputScene));
90  }
91 
92  HDSI_API
93  HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override;
94 
95  HDSI_API
96  SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override;
97 
98 protected:
99 
100  HDSI_API
102  HdSceneIndexBaseRefPtr const &inputScene);
103 
104  // satisfying HdSingleInputFilteringSceneIndexBase
105  void _PrimsAdded(
106  const HdSceneIndexBase &sender,
107  const HdSceneIndexObserver::AddedPrimEntries &entries) override;
108 
109  void _PrimsRemoved(
110  const HdSceneIndexBase &sender,
111  const HdSceneIndexObserver::RemovedPrimEntries &entries) override;
112 
113  void _PrimsDirtied(
114  const HdSceneIndexBase &sender,
115  const HdSceneIndexObserver::DirtiedPrimEntries &entries) override;
116 
117  struct _Binding
118  {
121  };
122  using _Bindings = std::vector<_Binding>;
123  using _PrimToBindings = std::map<SdfPath, _Bindings>;
124 
125  // Record coordSys bindings of prim at primPath. That is, add entries to the
126  // below data structures if needed and increase ref-counts.
127  // Optionally, return prims of type coord system that this scene index needs
128  // to add.
129  void _AddBindingsForPrim(const SdfPath &primPath,
130  SdfPathSet * addedCoordSysPrims = nullptr);
131  // Remove coordSys bindings. That is, decrease ref-counts and remove entries
132  // from below data structures if needed.
133  // Optionally, return prims of type coord system that this scene index needs
134  // to remove.
135  void _RemoveBindings(const _Bindings &bindings,
136  SdfPathSet * removedCoordSysPrims);
137  // Similar to above, but give the prim path explicitly to look-up bindings
138  // in map.
139  void _RemoveBindingsForPrim(const SdfPath &primPath,
140  SdfPathSet * removedCoordSysPrims);
141  // Removes bindings for given prim and all its descendants stored in below
142  // data structures.
143  void _RemoveBindingsForSubtree(const SdfPath &primPath,
144  SdfPathSet * removedCoordSysPrims);
145 
146  // If path is for coord sys prim added by this scene index, give the
147  // prim source for it.
148  HdContainerDataSourceHandle _GetCoordSysPrimSource(
149  const SdfPath &primPath) const;
150 
151 private:
152  using _NameToRefCount =
153  std::unordered_map<TfToken, size_t, TfToken::HashFunctor>;
154  using _PrimToNameToRefCount =
155  std::unordered_map<SdfPath, _NameToRefCount, SdfPath::Hash>;
156  // Maps prim which is targeted by coord sys binding to name of binding to
157  // count how many bindings are referencing that prim using that name.
158  //
159  // We delete an inner entry when there is no longer any coord sys binding
160  // with that name targeting the prim.
161  // We delete a prim when it is no longer targeted by any binding.
162  //
163  // This map is used to determine which coord sys prims we need to create
164  // under the targeted prim.
165  //
166  //
167  // In the above example, the content of the map will be:
168  //
169  // {
170  // /MyXform: {
171  // FOO: 1
172  // }
173  // }
174  //
175  _PrimToNameToRefCount _targetedPrimToNameToRefCount;
176 
177  // Maps prim to the coord sys bindings of that prim.
178  //
179  // Used to decrease ref counts when a prim gets deleted or modified.
180  //
181  // In the above example, the content of the map will be:
182  //
183  // { /MyPrim: [(FOO, /MyXform)] }
184  //
185  _PrimToBindings _primToBindings;
186 };
187 
189 
190 #endif
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1198
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:87
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:291
std::vector< _Binding > _Bindings
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:212
#define HDSI_API
Definition: api.h:40
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:210
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
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:91
void _PrimsDirtied(const HdSceneIndexBase &sender, const HdSceneIndexObserver::DirtiedPrimEntries &entries) override