HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mergingSceneIndex.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 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_HD_MERGING_SCENE_H
8 #define PXR_IMAGING_HD_MERGING_SCENE_H
9 
10 #include "pxr/pxr.h"
12 #include "pxr/usd/sdf/pathTable.h"
14 
15 #include <limits>
16 
18 
21 
22 /// \class HdMergingSceneIndex
23 ///
24 /// Merges multiple scenes together. For prims which exist in more than one
25 /// input scene, data sources are overlayed (down to the leaf) with the earlier
26 /// inserted scene having the stronger opinion.
27 /// NOTE: This is currently the only example of a multi-input scene index and
28 /// therefore that pattern hasn't yet been broken into a base class.
30 {
31 public:
32 
33  static HdMergingSceneIndexRefPtr New() {
35  }
36 
37  /// Entry to add a scene to the merging scene index.
38  struct InputScene
39  {
40  /// The scene to add.
41  HdSceneIndexBaseRefPtr scene;
42  /// The shallowest path at which prims in the scene should be
43  /// considered. This is an optional optimization to avoid having to
44  /// query multiple inputs when it's known in advance which might be
45  /// relevant for a given prim.
47  /// The position where to insert the scene.
48  ///
49  /// By default (or when larger when current number of scenes in the
50  /// merging scene index), inserts new scene after the last scene in the
51  /// merging scene index.
53  };
54 
55  /// Adds given scenes.
56  HD_API
57  void InsertInputScenes(
58  const std::vector<InputScene> &inputScenes);
59 
60  /// Removes given scenes.
61  HD_API
62  void RemoveInputScenes(
63  const std::vector<HdSceneIndexBaseRefPtr> &sceneIndices);
64 
65  /// Adds a scene with activeInputSceneRoot specifying the shallowest path
66  /// at which prims should be considered.
67  ///
68  /// Equivalent to `InsertInputScenes({inputScene, activeInputSceneRoot})`.
69  HD_API
70  void AddInputScene(
71  const HdSceneIndexBaseRefPtr &inputScene,
72  const SdfPath &activeInputSceneRoot);
73 
74  HD_API
75  void InsertInputScene(
76  size_t pos,
77  const HdSceneIndexBaseRefPtr &inputScene,
78  const SdfPath &activeInputSceneRoot);
79 
80  HD_API
81  void RemoveInputScene(const HdSceneIndexBaseRefPtr &sceneIndex);
82 
83  /// satisfying HdFilteringSceneIndex
84  HD_API
85  std::vector<HdSceneIndexBaseRefPtr> GetInputScenes() const override;
86 
87  // satisfying HdSceneIndexBase
88  HD_API
89  HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override;
90 
91  HD_API
92  SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override;
93 
94 protected:
95  HD_API
97 
98 private:
99 
100  void _PrimsAdded(
101  const HdSceneIndexBase &sender,
103 
104  void _PrimsRemoved(
105  const HdSceneIndexBase &sender,
107 
108  void _PrimsDirtied(
109  const HdSceneIndexBase &sender,
111 
112  friend class _Observer;
113 
114  // Rebuild _inputsPathTable from the current contents of _inputs.
115  void _RebuildInputsPathTable();
116 
117  class _Observer : public HdSceneIndexObserver
118  {
119  public:
121  : _owner(owner) {}
122 
123  void PrimsAdded(
124  const HdSceneIndexBase &sender,
125  const AddedPrimEntries &entries) override;
126 
127  void PrimsRemoved(
128  const HdSceneIndexBase &sender,
129  const RemovedPrimEntries &entries) override;
130 
131  void PrimsDirtied(
132  const HdSceneIndexBase &sender,
133  const DirtiedPrimEntries &entries) override;
134 
135  void PrimsRenamed(
136  const HdSceneIndexBase &sender,
137  const RenamedPrimEntries &entries) override;
138 
139  private:
140  HdMergingSceneIndex *_owner;
141  };
142 
143  _Observer _observer;
144 
145  struct _InputEntry
146  {
147  HdSceneIndexBaseRefPtr sceneIndex;
148  SdfPath sceneRoot;
149 
150  _InputEntry(const HdSceneIndexBaseRefPtr &sceneIndex,
151  const SdfPath &sceneRoot)
152  : sceneIndex(sceneIndex)
153  , sceneRoot(sceneRoot)
154  {
155  }
156  };
157 
158  // We observe that most merging scene indexes have few inputs, such as 2.
159  // However, in the case of merging USD native instance prototypes in
160  // UsdImaging, we may have hundreds of inputs with non-overlapping
161  // sceneRoots . To avoid an O(N) scan over all inputs when N grows
162  // large, we use an SdfPathTable to store ordered sub-list of inputs
163  // that pertain to an input prim path or input ancestor path.
164  using _InputEntries = TfSmallVector<_InputEntry, 4>;
165  using _InputEntriesByPathTable = SdfPathTable<_InputEntries>;
166 
167  // Look up the input entries potentially relevant to the given path.
168  const _InputEntries &_GetInputEntriesByPath(SdfPath const& path) const;
169 
170  _InputEntries _inputs;
171  _InputEntriesByPathTable _inputsPathTable;
172 
173 };
174 
175 
177 
178 #endif //PXR_IMAGING_HD_MERGING_SCENE_H
HD_API std::vector< HdSceneIndexBaseRefPtr > GetInputScenes() const override
satisfying HdFilteringSceneIndex
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1190
static SDF_API const SdfPath & AbsoluteRootPath()
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
#define HD_API
Definition: api.h:23
HD_API void InsertInputScene(size_t pos, const HdSceneIndexBaseRefPtr &inputScene, const SdfPath &activeInputSceneRoot)
HD_API void InsertInputScenes(const std::vector< InputScene > &inputScenes)
Adds given scenes.
HD_API HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
TF_DECLARE_REF_PTRS(HdMergingSceneIndex)
HD_API void RemoveInputScenes(const std::vector< HdSceneIndexBaseRefPtr > &sceneIndices)
Removes given scenes.
std::vector< class SdfPath > SdfPathVector
HD_API HdMergingSceneIndex()
Entry to add a scene to the merging scene index.
Definition: path.h:273
HdSceneIndexBaseRefPtr scene
The scene to add.
static HdMergingSceneIndexRefPtr New()
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
HD_API void RemoveInputScene(const HdSceneIndexBaseRefPtr &sceneIndex)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HD_API void AddInputScene(const HdSceneIndexBaseRefPtr &inputScene, const SdfPath &activeInputSceneRoot)
HD_API SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override