All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
renderIndex.h
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_RENDER_INDEX_H
25 #define PXR_IMAGING_HD_RENDER_INDEX_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
30 #include "pxr/imaging/hd/changeTracker.h"
31 #include "pxr/imaging/hd/perfLog.h"
32 #include "pxr/imaging/hd/primTypeIndex.h"
33 #include "pxr/imaging/hd/resourceRegistry.h"
34 #include "pxr/imaging/hd/sortedIds.h"
35 #include "pxr/imaging/hd/tokens.h"
36 
37 #include "pxr/imaging/hf/perfLog.h"
38 
39 #include "pxr/usd/sdf/path.h"
40 
41 #include "pxr/base/gf/vec4i.h"
42 #include "pxr/base/tf/hashmap.h"
43 
44 #include <tbb/enumerable_thread_specific.h>
45 
46 #include <vector>
47 #include <unordered_map>
48 #include <memory>
49 
50 PXR_NAMESPACE_OPEN_SCOPE
51 
52 class HdRprim;
53 class HdSprim;
54 class HdBprim;
55 class HdDrawItem;
56 class HdRprimCollection;
57 class HdSceneDelegate;
58 class HdRenderDelegate;
59 class HdExtComputation;
60 class VtValue;
61 class HdInstancer;
62 class HdDriver;
63 
64 using HdDirtyListSharedPtr = std::shared_ptr<class HdDirtyList>;
65 
66 using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
67 
68 using HdResourceRegistrySharedPtr = std::shared_ptr<class HdResourceRegistry>;
69 using HdTaskSharedPtrVector = std::vector<HdTaskSharedPtr>;
70 using HdTaskContext = std::unordered_map<TfToken,
71  VtValue,
73 using HdDriverVector = std::vector<HdDriver*>;
74 
116 class HdRenderIndex final
117 {
118 public:
119  typedef std::vector<HdDrawItem const*> HdDrawItemPtrVector;
120 
126  // hgi = Hgi::CreatePlatformDefaultHgi()
127  // hgiDriver = new HdDriver<Hgi*>(HgiTokens→renderDriver, hgi)
128  // HdRenderIndex::New(_renderDelegate, {_hgiDriver})
129  HD_API
130  static HdRenderIndex* New(
131  HdRenderDelegate *renderDelegate,
132  HdDriverVector const& drivers);
133 
134  HD_API
135  ~HdRenderIndex();
136 
138  HD_API
139  void Clear();
140 
145  HD_API
146  void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
147 
148  // ---------------------------------------------------------------------- //
151  HD_API
152  SdfPath GetRprimPathFromPrimId(int primId) const;
153 
154  // ---------------------------------------------------------------------- //
156  // ---------------------------------------------------------------------- //
157 
164  HD_API
165  void EnqueuePrimsToSync(
166  HdDirtyListSharedPtr const &dirtyList,
167  HdRprimCollection const &collection);
168 
174  HD_API
175  void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
176 
177  // ---------------------------------------------------------------------- //
179  // ---------------------------------------------------------------------- //
180 
182  // by renderTags and collection.
185  HD_API
186  HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
187  TfTokenVector const& renderTags);
188 
189  // ---------------------------------------------------------------------- //
191  // ---------------------------------------------------------------------- //
192 
193  HdChangeTracker& GetChangeTracker() { return _tracker; }
194  HdChangeTracker const& GetChangeTracker() const { return _tracker; }
195 
196  // ---------------------------------------------------------------------- //
198  // ---------------------------------------------------------------------- //
199 
201  HD_API
202  bool IsRprimTypeSupported(TfToken const& typeId) const;
203 
205  HD_API
206  void InsertRprim(TfToken const& typeId,
207  HdSceneDelegate* sceneDelegate,
208  SdfPath const& rprimId);
209 
211  HD_API
212  void RemoveRprim(SdfPath const& id);
213 
215  bool HasRprim(SdfPath const& id) {
216  return _rprimMap.find(id) != _rprimMap.end();
217  }
218 
220  HD_API
221  HdRprim const *GetRprim(SdfPath const &id) const;
222 
224  HD_API
226 
229  HD_API
231  SdfPath* sceneDelegateId,
232  SdfPath* instancerId) const;
233 
235  HD_API
236  TfToken GetRenderTag(SdfPath const& id) const;
237 
239  HD_API
240  TfToken GetMaterialTag(SdfPath const& id) const;
241 
244  HD_API
245  const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
246 
247 
249  HD_API
250  SdfPathVector GetRprimSubtree(SdfPath const& root);
251 
252 
253  // ---------------------------------------------------------------------- //
255  // ---------------------------------------------------------------------- //
256 
258  HD_API
259  void InsertInstancer(HdSceneDelegate* delegate,
260  SdfPath const &id);
261 
263  HD_API
264  void RemoveInstancer(SdfPath const& id);
265 
267  bool HasInstancer(SdfPath const& id) {
268  return _instancerMap.find(id) != _instancerMap.end();
269  }
270 
272  HD_API
273  HdInstancer *GetInstancer(SdfPath const &id) const;
274 
275  // ---------------------------------------------------------------------- //
277  // ---------------------------------------------------------------------- //
278 
280  template <typename T>
281  void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
282 
284  HD_API
285  void RemoveTask(SdfPath const& id);
286 
288  bool HasTask(SdfPath const& id) {
289  return _taskMap.find(id) != _taskMap.end();
290  }
291 
293  HD_API
294  HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
295 
296  // ---------------------------------------------------------------------- //
298  // ---------------------------------------------------------------------- //
299 
301  HD_API
302  bool IsSprimTypeSupported(TfToken const& typeId) const;
303 
305  HD_API
306  void InsertSprim(TfToken const& typeId,
307  HdSceneDelegate* delegate,
308  SdfPath const& sprimId);
309 
310  HD_API
311  void RemoveSprim(TfToken const& typeId, SdfPath const &id);
312 
313  HD_API
314  HdSprim *GetSprim(TfToken const& typeId, SdfPath const &id) const;
315 
318  HD_API
319  SdfPathVector GetSprimSubtree(TfToken const& typeId,
320  SdfPath const& root);
321 
323  HD_API
324  HdSprim *GetFallbackSprim(TfToken const& typeId) const;
325 
326 
327  // ---------------------------------------------------------------------- //
329  // ---------------------------------------------------------------------- //
330 
332  HD_API
333  bool IsBprimTypeSupported(TfToken const& typeId) const;
334 
336  HD_API
337  void InsertBprim(TfToken const& typeId,
338  HdSceneDelegate* delegate,
339  SdfPath const& bprimId);
340 
341  HD_API
342  void RemoveBprim(TfToken const& typeId, SdfPath const &id);
343 
344  HD_API
345  HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
346 
349  HD_API
350  SdfPathVector GetBprimSubtree(TfToken const& typeId,
351  SdfPath const& root);
352 
354  HD_API
355  HdBprim *GetFallbackBprim(TfToken const& typeId) const;
356 
357  // ---------------------------------------------------------------------- //
359  // ---------------------------------------------------------------------- //
363  HD_API
364  HdRenderDelegate *GetRenderDelegate() const;
365 
366  // The render delegate may require access to a render context / device
367  // that is provided by the application.
368  HD_API
369  HdDriverVector const& GetDrivers() const;
370 
373  HD_API
374  HdResourceRegistrySharedPtr GetResourceRegistry() const;
375 
376 private:
377  // The render index constructor is private so we can check
378  // renderDelegate before construction: see HdRenderIndex::New(...).
380  HdRenderDelegate *renderDelegate,
381  HdDriverVector const& drivers);
382 
383  // ---------------------------------------------------------------------- //
384  // Private Helper methods
385  // ---------------------------------------------------------------------- //
386 
387  // Go through all RPrims and reallocate their instance ids
388  // This is called once we have exhausted all all 24bit instance ids.
389  void _CompactPrimIds();
390 
391  // Allocate the next available instance id to the prim
392  void _AllocatePrimId(HdRprim* prim);
393 
394  // Inserts the task into the index and updates tracking state.
395  // _TrackDelegateTask is called by the inlined InsertTask<T>, so it needs
396  // to be marked HD_API.
397  HD_API
398  void _TrackDelegateTask(HdSceneDelegate* delegate,
399  SdfPath const& taskId,
400  HdTaskSharedPtr const& task);
401 
402  template <typename T>
403  static inline const TfToken & _GetTypeId();
404 
405  void _RemoveRprimSubtree(const SdfPath &root,
406  HdSceneDelegate* sceneDelegate);
407  void _RemoveInstancerSubtree(const SdfPath &root,
408  HdSceneDelegate* sceneDelegate);
409  void _RemoveExtComputationSubtree(const SdfPath &root,
410  HdSceneDelegate* sceneDelegate);
411  void _RemoveTaskSubtree(const SdfPath &root,
412  HdSceneDelegate* sceneDelegate);
413 
414  // ---------------------------------------------------------------------- //
415  // Index State
416  // ---------------------------------------------------------------------- //
417  struct _RprimInfo {
418  HdSceneDelegate *sceneDelegate;
419  HdRprim *rprim;
420  };
421 
422  struct _TaskInfo {
423  HdSceneDelegate *sceneDelegate;
424  HdTaskSharedPtr task;
425  };
426 
427  typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
428  typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
429  typedef std::vector<SdfPath> _RprimPrimIDVector;
430 
431  typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
432  typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
433 
434  _RprimMap _rprimMap;
435  Hd_SortedIds _rprimIds;
436 
437  _RprimPrimIDVector _rprimPrimIdMap;
438 
439  _TaskMap _taskMap;
440 
441  _SprimIndex _sprimIndex;
442  _BprimIndex _bprimIndex;
443 
444  HdChangeTracker _tracker;
445 
446  typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
447  _InstancerMap _instancerMap;
448 
449  struct _SyncQueueEntry {
450  HdDirtyListSharedPtr dirtyList;
451  HdRprimCollection collection;
452 
453  };
454  typedef std::vector<_SyncQueueEntry> _SyncQueue;
455  _SyncQueue _syncQueue;
456 
469  std::unordered_map<HdDirtyBits, const SdfPathVector> _dirtyRprimIdsMap;
470 
471  friend class HdDirtyList;
472  const SdfPathVector& _GetDirtyRprimIds(HdDirtyBits mask);
473 
474  HdRenderDelegate *_renderDelegate;
475  HdDriverVector _drivers;
476 
477  // ---------------------------------------------------------------------- //
478  // Sync State
479  // ---------------------------------------------------------------------- //
480  TfTokenVector _activeRenderTags;
481  unsigned int _renderTagVersion;
482 
484  void _InitPrimTypes();
485 
487  bool _CreateFallbackPrims();
488 
490  void _DestroyFallbackPrims();
491 
492  void _GatherRenderTags(const HdTaskSharedPtrVector *tasks);
493 
494  typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
495  _ConcurrentDrawItems;
496 
497  void _AppendDrawItems(const SdfPathVector &rprimIds,
498  size_t begin,
499  size_t end,
500  HdRprimCollection const& collection,
501  _ConcurrentDrawItems* result);
502 
506  static void _ConfigureReprs();
507 
508  // Remove default constructor
509  HdRenderIndex() = delete;
510 
511  // Don't allow copies
512  HdRenderIndex(const HdRenderIndex &) = delete;
513  HdRenderIndex &operator=(const HdRenderIndex &) = delete;
514 
515 };
516 
517 template <typename T>
518 void
520 {
521  HD_TRACE_FUNCTION();
522  HF_MALLOC_TAG_FUNCTION();
523 
524  HdTaskSharedPtr task = std::make_shared<T>(delegate, id);
525  _TrackDelegateTask(delegate, id, task);
526 }
527 
528 PXR_NAMESPACE_CLOSE_SCOPE
529 
530 #endif //PXR_IMAGING_HD_RENDER_INDEX_H
Hydra Representation of a Client defined computation.
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:116
HD_API bool IsSprimTypeSupported(TfToken const &typeId) const
Returns whether the sprim type is supported by this render index.
HD_API bool IsRprimTypeSupported(TfToken const &typeId) const
Returns whether the rprim type is supported by this render index.
HD_API void InsertBprim(TfToken const &typeId, HdSceneDelegate *delegate, SdfPath const &bprimId)
Insert a bprim into index.
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine...
Definition: changeTracker.h:50
HD_API HdSceneDelegate * GetSceneDelegateForRprim(SdfPath const &id) const
Returns the scene delegate for the given rprim.
HD_API HdInstancer * GetInstancer(SdfPath const &id) const
Returns the instancer of id.
This class exists to facilitate point cloud style instancing.
Definition: instancer.h:124
HD_API void RemoveSubtree(const SdfPath &root, HdSceneDelegate *sceneDelegate)
Clear all entries in the render index under the given root and belong to a specified delegate...
Functor to use for hash maps from tokens to other things.
Definition: token.h:166
HD_API void InsertInstancer(HdSceneDelegate *delegate, SdfPath const &id)
Insert an instancer into index.
HD_API HdResourceRegistrySharedPtr GetResourceRegistry() const
Returns a shared ptr to the resource registry of the current render delegate.
HD_API TfToken GetRenderTag(SdfPath const &id) const
Returns the render tag for the given rprim.
HD_API void InsertSprim(TfToken const &typeId, HdSceneDelegate *delegate, SdfPath const &sprimId)
Insert a sprim into index.
HD_API void InsertRprim(TfToken const &typeId, HdSceneDelegate *sceneDelegate, SdfPath const &rprimId)
Insert a rprim into index.
HD_API const SdfPathVector & GetRprimIds()
Returns a sorted list of all Rprims in the render index.
Definition: renderIndex.h:245
HD_API void RemoveRprim(SdfPath const &id)
Remove a rprim from index.
HD_API SdfPath GetRprimPathFromPrimId(int primId) const
Given a prim id, returns the path of the corresponding rprim or an empty path if none is found...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HdDriver represents a device object, commonly a render device, that is owned by the application and p...
Definition: driver.h:40
The render engine state for a given rprim from the scene graph.
Definition: rprim.h:68
HD_API void Clear()
Clear all r (render), s (state) and b (buffer) prims.
HD_API void RemoveTask(SdfPath const &id)
Removes the given task from the RenderIndex.
HD_API void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext)
Syncs input tasks, B &amp; S prims, (external) computations and processes all pending dirty lists (which ...
Adapter class providing data exchange with the client scene graph.
bool HasInstancer(SdfPath const &id)
Returns true if instancer id exists in index.
Definition: renderIndex.h:267
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
bool HasTask(SdfPath const &id)
Returns true if a task exists in the index with the given id.
Definition: renderIndex.h:288
A named, semantic collection of objects.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
HD_API SdfPathVector GetRprimSubtree(SdfPath const &root)
Returns the subtree rooted under the given path.
Used for faster iteration of dirty rprims, filtered by mask.
Definition: dirtyList.h:101
bool HasRprim(SdfPath const &id)
Returns true if rprim id exists in index.
Definition: renderIndex.h:215
HD_API SdfPathVector GetBprimSubtree(TfToken const &typeId, SdfPath const &root)
Returns the subtree rooted under the given path for the given bprim type.
Sprim (state prim) is a base class of managing state for non-drawable scene entity (e...
Definition: sprim.h:52
HD_API HdTaskSharedPtr const & GetTask(SdfPath const &id) const
Returns the task for the given id.
HD_API void EnqueuePrimsToSync(HdDirtyListSharedPtr const &dirtyList, HdRprimCollection const &collection)
Adds the dirty list to the sync queue.
A draw item is a light-weight representation of an HdRprim&#39;s resources and material to be used for re...
Definition: drawItem.h:66
HD_API HdBprim * GetFallbackBprim(TfToken const &typeId) const
Returns the fallback prim for the Bprim of the given type.
HD_API bool GetSceneDelegateAndInstancerIds(SdfPath const &id, SdfPath *sceneDelegateId, SdfPath *instancerId) const
Query function to return the id&#39;s of the scene delegate and instancer associated with the Rprim at th...
static HD_API HdRenderIndex * New(HdRenderDelegate *renderDelegate, HdDriverVector const &drivers)
Create a render index with the given render delegate.
HD_API HdDrawItemPtrVector GetDrawItems(HdRprimCollection const &collection, TfTokenVector const &renderTags)
Returns a list of relevant draw items that match the criteria specified.
HD_API HdSprim * GetFallbackSprim(TfToken const &typeId) const
Returns the fullback prim for the Sprim of the given type.
HD_API bool IsBprimTypeSupported(TfToken const &typeId) const
Returns whether the bprim type is supported by this render index.
Bprim (buffer prim) is a base class of managing a blob of data that is used to communicate between th...
Definition: bprim.h:56
void InsertTask(HdSceneDelegate *delegate, SdfPath const &id)
Inserts a new task into the render index with an identifier of id.
Definition: renderIndex.h:519
HD_API void RemoveInstancer(SdfPath const &id)
Remove an instancer from index.
HD_API TfToken GetMaterialTag(SdfPath const &id) const
Return the material tag for the given rprim.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:168
HD_API HdRprim const * GetRprim(SdfPath const &id) const
Returns the rprim of id.
HD_API SdfPathVector GetSprimSubtree(TfToken const &typeId, SdfPath const &root)
Returns the subtree rooted under the given path for the given sprim type.
HD_API HdRenderDelegate * GetRenderDelegate() const
Currently, a render index only supports connection to one type of render delegate, due to the inserted information and change tracking being specific to that delegate type.