HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primTypeIndex.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_PRIM_TYPE_INDEX_H
8 #define PXR_IMAGING_HD_PRIM_TYPE_INDEX_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/types.h"
13 #include "pxr/base/tf/token.h"
14 #include "pxr/usd/sdf/path.h"
15 
16 #include <set>
17 #include <vector>
18 #include <unordered_map>
19 
21 
22 class HdChangeTracker;
23 class HdRenderDelegate;
24 class HdRenderParam;
25 class HdSceneDelegate;
26 class SdfPath;
27 using HdSceneDelegatePtrVector = std::vector<HdSceneDelegate*>;
28 
29 /// This class is only used by the render index.
30 /// It provides functionality to manage and store one class of prim
31 /// such as a Sprim or Bprim.
32 template <class PrimType>
34 public:
37 
38  ///
39  /// Initialize this prim index, specifying the typeId tokens
40  /// that should be supported by this index.
41  ///
42  void InitPrimTypes(const TfTokenVector &primTypes);
43 
44  ///
45  /// Removes and frees all prims in this index.
46  /// The render delegate is responsible for freeing the actual memory
47  /// allocated to the prim.
48  /// The prim is also removed from the change tracker.
49  ///
50  void Clear(HdChangeTracker &tracker, HdRenderDelegate *renderDelegate);
51 
52  ///
53  /// Add a new a prim to the render index identified by the globally unique
54  /// identifier, primId.
55  /// typeId is the type of the prim to create, which is allocated using
56  /// the provided render delegate. The Scene delegate provided is
57  /// associated with the prim and is the one used to pull the data for the
58  /// prim during sync processing.
59  /// As well as being inserted into this index, the prim is added to the
60  /// change tracker, with the initial dirty state provided by the prim itself.
61  ///
62  void InsertPrim(const TfToken &typeId,
63  HdSceneDelegate *sceneDelegate,
64  const SdfPath &primId,
65  HdChangeTracker &tracker,
66  HdRenderDelegate *renderDelegate);
67 
68  ///
69  /// Removes the prim identifier by primId. TypeId is the type of that
70  /// prim. Memory for the prim is deallocated using the render delegate.
71  /// The prim is also removed from the change tracker.
72  ///
73  void RemovePrim(const TfToken &typeId,
74  const SdfPath &primId,
75  HdChangeTracker &tracker,
76  HdRenderDelegate *renderDelegate);
77 
78  ///
79  /// Removes the subtree of prims identifier by root that are owned
80  /// by the given scene delegate.
81  /// This function affects all prim types.
82  /// Memory for the prim is deallocated using the render delegate.
83  /// The prim is also removed from the change tracker.
84  ///
85  void RemoveSubtree(const SdfPath &root,
86  HdSceneDelegate* sceneDelegate,
87  HdChangeTracker &tracker,
88  HdRenderDelegate *renderDelegate);
89 
90  /// Obtains a modifiable pointer the prim with the given type and id.
91  /// If no prim with the given id is in the index or the type id is
92  /// incorrect, then nullptr is returned.
93  PrimType *GetPrim(const TfToken &typeId,
94  const SdfPath &primId) const;
95 
96  ///
97  /// Obtain a prim, that implements the schema given by type id, that
98  /// can be used as a substitute for any prim of that type in the event of
99  /// an error.
100  ///
101  /// Hydra guarantees that the prim is not null for any type that
102  /// is supported by the back-end.
103  ///
104  PrimType *GetFallbackPrim(TfToken const &typeId) const;
105 
106  ///
107  /// Returns a list of Prim Ids in outPaths of prims that type match
108  /// typeId who are namespace children of rootPath.
109  /// rootPath does not need to match any prim in the index or
110  /// it may point to a prim of a different type.
111  ///
112  void GetPrimSubtree(const TfToken &typeId,
113  const SdfPath &rootPath,
114  SdfPathVector *outPaths);
115 
116  ///
117  /// Uses the provided render delegate to create the fallback prims
118  /// for use by the index. The prim types created are based on those
119  /// specified by InitPrimTypes.
120  ///
121  /// If the render delegate fails to create a prim, this function returns
122  /// false and the index is remain uninitialized and shouldn't be used.
123  ///
124  bool CreateFallbackPrims(HdRenderDelegate *renderDelegate);
125 
126  ///
127  /// Clean-up function for the index. Uses the delegate to deallocate
128  /// the memory used by the fallback prims. The index is returned to
129  /// an uninitialized state and shouldn't be used, unless reinitialized.
130  ///
131  void DestroyFallbackPrims(HdRenderDelegate *renderDelegate);
132 
133  ///
134  /// Main Sync Processing function.
135  ///
136  /// Will call the Sync function on all prims in the index that
137  /// are marked dirty in the specified change tracker.
138  /// Also updates an internal list of scene delegates for the dirty prims.
139  ///
140  void SyncPrims(HdChangeTracker &tracker,
141  HdRenderParam *renderParam,
142  HdRenderDelegate *renderDelegate);
143 
144  /// Returns a vector of unique scene delegates corresponding to the dirty
145  /// prims that were sync'd in SyncPrims.
147 
148 private:
149  struct _PrimInfo {
150  HdSceneDelegate *sceneDelegate;
151  PrimType *prim;
152  };
153 
154  typedef std::unordered_map<SdfPath, _PrimInfo, SdfPath::Hash> _PrimMap;
155 
156  struct _PrimTypeEntry
157  {
158  _PrimMap primMap;
159  Hd_SortedIds primIds; // Primarily for sub-tree searching
160  PrimType *fallbackPrim;
161 
162  _PrimTypeEntry()
163  : primMap()
164  , primIds()
165  , fallbackPrim(nullptr)
166  {
167  }
168  };
169 
170  typedef std::unordered_map<TfToken, size_t, TfToken::HashFunctor> _TypeIndex;
171 
172  typedef std::vector<_PrimTypeEntry> _PrimTypeList;
173 
174  _PrimTypeList _entries;
175  _TypeIndex _index;
176  HdSceneDelegatePtrVector _dirtyPrimDelegates;
177  TfTokenVector _primTypeNames;
178 
179 
180  // Template methods that are expected to be specialized on PrimType.
181  // These are to handle prim type specific function names on called objects.
182  static void _TrackerInsertPrim(HdChangeTracker &tracker,
183  const SdfPath &path,
184  HdDirtyBits initialDirtyState);
185 
186  static void _TrackerRemovePrim(HdChangeTracker &tracker,
187  const SdfPath &path);
188 
189  static HdDirtyBits _TrackerGetPrimDirtyBits(HdChangeTracker &tracker,
190  const SdfPath &path);
191 
192  static void _TrackerMarkPrimClean(HdChangeTracker &tracker,
193  const SdfPath &path,
194  HdDirtyBits dirtyBits);
195 
196  static PrimType *_RenderDelegateCreatePrim(HdRenderDelegate *renderDelegate,
197  const TfToken &typeId,
198  const SdfPath &primId);
199  static PrimType *_RenderDelegateCreateFallbackPrim(
200  HdRenderDelegate *renderDelegate,
201  const TfToken &typeId);
202 
203  static void _RenderDelegateDestroyPrim(HdRenderDelegate *renderDelegate,
204  PrimType *prim);
205 
206  // No copying
207  Hd_PrimTypeIndex(const Hd_PrimTypeIndex &) = delete;
208  Hd_PrimTypeIndex &operator =(const Hd_PrimTypeIndex &) = delete;
209 };
210 
212 
213 #endif // PXR_IMAGING_HD_PRIM_TYPE_INDEX_H
const HdSceneDelegatePtrVector & GetSceneDelegatesForDirtyPrims()
PrimType * GetPrim(const TfToken &typeId, const SdfPath &primId) const
void InitPrimTypes(const TfTokenVector &primTypes)
uint32_t HdDirtyBits
Definition: types.h:143
bool CreateFallbackPrims(HdRenderDelegate *renderDelegate)
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
void RemovePrim(const TfToken &typeId, const SdfPath &primId, HdChangeTracker &tracker, HdRenderDelegate *renderDelegate)
PrimType * GetFallbackPrim(TfToken const &typeId) const
Definition: token.h:70
std::vector< class SdfPath > SdfPathVector
void InsertPrim(const TfToken &typeId, HdSceneDelegate *sceneDelegate, const SdfPath &primId, HdChangeTracker &tracker, HdRenderDelegate *renderDelegate)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
void SyncPrims(HdChangeTracker &tracker, HdRenderParam *renderParam, HdRenderDelegate *renderDelegate)
Definition: path.h:273
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
void Clear(HdChangeTracker &tracker, HdRenderDelegate *renderDelegate)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void RemoveSubtree(const SdfPath &root, HdSceneDelegate *sceneDelegate, HdChangeTracker &tracker, HdRenderDelegate *renderDelegate)
std::vector< HdSceneDelegate * > HdSceneDelegatePtrVector
Definition: primTypeIndex.h:27
void GetPrimSubtree(const TfToken &typeId, const SdfPath &rootPath, SdfPathVector *outPaths)
void DestroyFallbackPrims(HdRenderDelegate *renderDelegate)