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