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/dirtyList.h"
32 #include "pxr/imaging/hd/perfLog.h"
33 #include "pxr/imaging/hd/primTypeIndex.h"
34 #include "pxr/imaging/hd/resourceRegistry.h"
35 #include "pxr/imaging/hd/sortedIds.h"
36 #include "pxr/imaging/hd/tokens.h"
37 
38 #include "pxr/imaging/hd/sceneIndex.h"
39 #include "pxr/imaging/hd/mergingSceneIndex.h"
40 #include "pxr/imaging/hd/legacyPrimSceneIndex.h"
41 #include "pxr/imaging/hd/noticeBatchingSceneIndex.h"
42 
43 #include "pxr/imaging/hf/perfLog.h"
44 
45 #include "pxr/usd/sdf/path.h"
46 
47 #include "pxr/base/gf/vec4i.h"
48 #include "pxr/base/tf/hashmap.h"
49 
50 #include <tbb/enumerable_thread_specific.h>
51 
52 #include <vector>
53 #include <unordered_map>
54 #include <memory>
55 
56 PXR_NAMESPACE_OPEN_SCOPE
57 
58 class HdRprim;
59 class HdSprim;
60 class HdBprim;
61 class HdDrawItem;
62 class HdRprimCollection;
63 class HdSceneDelegate;
64 class HdRenderDelegate;
65 class HdExtComputation;
66 class VtValue;
67 class HdInstancer;
68 class HdDriver;
69 
70 using HdDriverVector = std::vector<HdDriver*>;
71 using HdRprimCollectionVector = std::vector<HdRprimCollection>;
72 using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
73 using HdResourceRegistrySharedPtr = std::shared_ptr<class HdResourceRegistry>;
74 using HdTaskSharedPtrVector = std::vector<HdTaskSharedPtr>;
75 using HdTaskContext = std::unordered_map<TfToken,
76  VtValue,
78 
120 class HdRenderIndex final
121 {
122 public:
123  typedef std::vector<HdDrawItem const*> HdDrawItemPtrVector;
124 
130  // hgi = Hgi::CreatePlatformDefaultHgi()
131  // hgiDriver = new HdDriver<Hgi*>(HgiTokens→renderDriver, hgi)
132  // HdRenderIndex::New(_renderDelegate, {_hgiDriver})
133  HD_API
134  static HdRenderIndex* New(
135  HdRenderDelegate *renderDelegate,
136  HdDriverVector const& drivers);
137 
138  HD_API
139  ~HdRenderIndex();
140 
142  HD_API
143  void Clear();
144 
149  HD_API
150  void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
151 
152  // ---------------------------------------------------------------------- //
155  HD_API
156  SdfPath GetRprimPathFromPrimId(int primId) const;
157 
158  // ---------------------------------------------------------------------- //
160  // ---------------------------------------------------------------------- //
161 
165  HD_API
166  void EnqueueCollectionToSync(HdRprimCollection const &collection);
167 
173  HD_API
174  void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
175 
176  // ---------------------------------------------------------------------- //
178  // ---------------------------------------------------------------------- //
179 
181  // by renderTags and collection.
184  HD_API
185  HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
186  TfTokenVector const& renderTags);
187 
188  // ---------------------------------------------------------------------- //
190  // ---------------------------------------------------------------------- //
191 
192  HdChangeTracker& GetChangeTracker() { return _tracker; }
193  HdChangeTracker const& GetChangeTracker() const { return _tracker; }
194 
195  // ---------------------------------------------------------------------- //
197  // ---------------------------------------------------------------------- //
198 
200  HD_API
201  bool IsRprimTypeSupported(TfToken const& typeId) const;
202 
204  HD_API
205  void InsertRprim(TfToken const& typeId,
206  HdSceneDelegate* sceneDelegate,
207  SdfPath const& rprimId);
208 
210  HD_API
211  void RemoveRprim(SdfPath const& id);
212 
214  bool HasRprim(SdfPath const& id) {
215  return _rprimMap.find(id) != _rprimMap.end();
216  }
217 
219  HD_API
220  HdRprim const *GetRprim(SdfPath const &id) const;
221 
223  HD_API
225 
228  HD_API
230  SdfPath* sceneDelegateId,
231  SdfPath* instancerId) const;
232 
234  HD_API
235  TfToken GetRenderTag(SdfPath const& id) const;
236 
238  TfToken UpdateRenderTag(SdfPath const& id,
239  HdDirtyBits bits);
240 
243  HD_API
244  const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
245 
246 
248  HD_API
249  SdfPathVector GetRprimSubtree(SdfPath const& root);
250 
251 
252  // ---------------------------------------------------------------------- //
254  // ---------------------------------------------------------------------- //
255 
257  HD_API
258  void InsertInstancer(HdSceneDelegate* delegate,
259  SdfPath const &id);
260 
262  HD_API
263  void RemoveInstancer(SdfPath const& id);
264 
266  bool HasInstancer(SdfPath const& id) {
267  return _instancerMap.find(id) != _instancerMap.end();
268  }
269 
271  HD_API
272  HdInstancer *GetInstancer(SdfPath const &id) const;
273 
274  // ---------------------------------------------------------------------- //
276  // ---------------------------------------------------------------------- //
277 
279  template <typename T>
280  void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
281 
283  HD_API
284  void RemoveTask(SdfPath const& id);
285 
287  bool HasTask(SdfPath const& id) {
288  return _taskMap.find(id) != _taskMap.end();
289  }
290 
292  HD_API
293  HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
294 
295  // ---------------------------------------------------------------------- //
297  // ---------------------------------------------------------------------- //
298 
300  HD_API
301  bool IsSprimTypeSupported(TfToken const& typeId) const;
302 
304  HD_API
305  void InsertSprim(TfToken const& typeId,
306  HdSceneDelegate* delegate,
307  SdfPath const& sprimId);
308 
309  HD_API
310  void RemoveSprim(TfToken const& typeId, SdfPath const &id);
311 
312  HD_API
313  HdSprim *GetSprim(TfToken const& typeId, SdfPath const &id) const;
314 
317  HD_API
318  SdfPathVector GetSprimSubtree(TfToken const& typeId,
319  SdfPath const& root);
320 
322  HD_API
323  HdSprim *GetFallbackSprim(TfToken const& typeId) const;
324 
325 
326  // ---------------------------------------------------------------------- //
328  // ---------------------------------------------------------------------- //
329 
331  HD_API
332  bool IsBprimTypeSupported(TfToken const& typeId) const;
333 
335  HD_API
336  void InsertBprim(TfToken const& typeId,
337  HdSceneDelegate* delegate,
338  SdfPath const& bprimId);
339 
340  HD_API
341  void RemoveBprim(TfToken const& typeId, SdfPath const &id);
342 
343  HD_API
344  HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
345 
348  HD_API
349  SdfPathVector GetBprimSubtree(TfToken const& typeId,
350  SdfPath const& root);
351 
353  HD_API
354  HdBprim *GetFallbackBprim(TfToken const& typeId) const;
355 
356  // ---------------------------------------------------------------------- //
358  // ---------------------------------------------------------------------- //
359  HD_API
360  void InsertSceneIndex(
361  HdSceneIndexBaseRefPtr inputSceneIndex,
362  SdfPath const& scenePathPrefix);
363 
364  HD_API
365  void RemoveSceneIndex(
366  HdSceneIndexBaseRefPtr inputSceneIndex);
367 
368  // ---------------------------------------------------------------------- //
370  // ---------------------------------------------------------------------- //
374  HD_API
375  HdRenderDelegate *GetRenderDelegate() const;
376 
377  // The render delegate may require access to a render context / device
378  // that is provided by the application.
379  HD_API
380  HdDriverVector const& GetDrivers() const;
381 
384  HD_API
385  HdResourceRegistrySharedPtr GetResourceRegistry() const;
386 
390  HD_API
391  static bool IsSceneIndexEmulationEnabled();
392 
401  HD_API
403 
411  HD_API
413 
414 
415 private:
416  // The render index constructor is private so we can check
417  // renderDelegate before construction: see HdRenderIndex::New(...).
419  HdRenderDelegate *renderDelegate,
420  HdDriverVector const& drivers);
421 
422  // ---------------------------------------------------------------------- //
423  // Private Helper methods
424  // ---------------------------------------------------------------------- //
425 
426  // Go through all RPrims and reallocate their instance ids
427  // This is called once we have exhausted all all 24bit instance ids.
428  void _CompactPrimIds();
429 
430  // Allocate the next available instance id to the prim
431  void _AllocatePrimId(HdRprim* prim);
432 
433  // Inserts the task into the index and updates tracking state.
434  // _TrackDelegateTask is called by the inlined InsertTask<T>, so it needs
435  // to be marked HD_API.
436  HD_API
437  void _TrackDelegateTask(HdSceneDelegate* delegate,
438  SdfPath const& taskId,
439  HdTaskSharedPtr const& task);
440 
441  template <typename T>
442  static inline const TfToken & _GetTypeId();
443 
444 
445  // Private versions of equivalent public methods which insert and remove
446  // from this render index.
447  //
448  // The public versions check to see if scene delegate emulation is active.
449  // If not, they call through to these. Otherwise, they forward to the
450  // the HdLegacyPrimSceneIndex member. If a legacy render delegate is also
451  // in use, the scene index chain will terminate with a
452  // HdSceneIndexAdapterSceneDelegate. That will call the private versions
453  // directly so that the internal render index tables are updated.
454  //
455  // This prevents cyclic insertion/removals while allowing a single
456  // HdRenderIndex to be used for both front and back-end emulation.
457  //
458  // Note: all render index users should call the public APIs; only
459  // sceneIndexAdapterSceneDelegate.cpp should call these versions, to keep
460  // state synchronized. Note, for example, that _RemoveSubtree and _Clear
461  // don't affect the task map, since tasks aren't part of emulation, whereas
462  // RemoveSubtree and Clear do affect the task map...
464  void _InsertRprim(TfToken const& typeId,
465  HdSceneDelegate* sceneDelegate,
466  SdfPath const& rprimId);
467  void _InsertSprim(TfToken const& typeId,
468  HdSceneDelegate* delegate,
469  SdfPath const& sprimId);
470  void _InsertBprim(TfToken const& typeId,
471  HdSceneDelegate* delegate,
472  SdfPath const& bprimId);
473  void _InsertInstancer(HdSceneDelegate* delegate,
474  SdfPath const &id);
475 
476  void _RemoveRprim(SdfPath const& id);
477  void _RemoveSprim(TfToken const& typeId, SdfPath const& id);
478  void _RemoveBprim(TfToken const& typeId, SdfPath const& id);
479  void _RemoveInstancer(SdfPath const& id);
480  void _RemoveSubtree(SdfPath const& id, HdSceneDelegate* sceneDelegate);
481  void _RemoveRprimSubtree(const SdfPath &root,
482  HdSceneDelegate* sceneDelegate);
483  void _RemoveInstancerSubtree(const SdfPath &root,
484  HdSceneDelegate* sceneDelegate);
485  void _RemoveExtComputationSubtree(const SdfPath &root,
486  HdSceneDelegate* sceneDelegate);
487  void _RemoveTaskSubtree(const SdfPath &root,
488  HdSceneDelegate* sceneDelegate);
489  void _Clear();
490 
491  // ---------------------------------------------------------------------- //
492  // Index State
493  // ---------------------------------------------------------------------- //
494  struct _RprimInfo {
495  HdSceneDelegate *sceneDelegate;
496  HdRprim *rprim;
497  };
498 
499  HdLegacyPrimSceneIndexRefPtr _emulationSceneIndex;
500  HdNoticeBatchingSceneIndexRefPtr _emulationNoticeBatchingSceneIndex;
501  std::unique_ptr<class HdSceneIndexAdapterSceneDelegate> _siSd;
502 
503  HdMergingSceneIndexRefPtr _mergingSceneIndex;
504 
505  struct _TaskInfo {
506  HdSceneDelegate *sceneDelegate;
507  HdTaskSharedPtr task;
508  };
509 
510  typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
511  typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
512  typedef std::vector<SdfPath> _RprimPrimIDVector;
513 
514  typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
515  typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
516 
517  _RprimMap _rprimMap;
518  Hd_SortedIds _rprimIds;
519 
520  _RprimPrimIDVector _rprimPrimIdMap;
521 
522  _TaskMap _taskMap;
523 
524  _SprimIndex _sprimIndex;
525  _BprimIndex _bprimIndex;
526 
527  HdChangeTracker _tracker;
528 
529  typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
530  _InstancerMap _instancerMap;
531 
532  HdRenderDelegate *_renderDelegate;
533  HdDriverVector _drivers;
534 
535  // ---------------------------------------------------------------------- //
536  // Sync State
537  // ---------------------------------------------------------------------- //
538  HdRprimCollectionVector _collectionsToSync;
539  HdDirtyList _rprimDirtyList;
540 
541  // ---------------------------------------------------------------------- //
542 
544  void _InitPrimTypes();
545 
547  bool _CreateFallbackPrims();
548 
550  void _DestroyFallbackPrims();
551 
552  typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
553  _ConcurrentDrawItems;
554 
555  void _AppendDrawItems(const SdfPathVector &rprimIds,
556  size_t begin,
557  size_t end,
558  HdRprimCollection const& collection,
559  _ConcurrentDrawItems* result);
560 
564  static void _ConfigureReprs();
565 
566  // Remove default constructor
567  HdRenderIndex() = delete;
568 
569  // Don't allow copies
570  HdRenderIndex(const HdRenderIndex &) = delete;
571  HdRenderIndex &operator=(const HdRenderIndex &) = delete;
572 
573 };
574 
575 template <typename T>
576 void
578 {
579  HD_TRACE_FUNCTION();
580  HF_MALLOC_TAG_FUNCTION();
581 
582  HdTaskSharedPtr task = std::make_shared<T>(delegate, id);
583  _TrackDelegateTask(delegate, id, task);
584 }
585 
586 PXR_NAMESPACE_CLOSE_SCOPE
587 
588 #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:120
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.
HD_API HdRenderDelegate * GetRenderDelegate() const
Currently, a render index only supports connection to one type of render delegate,...
HD_API HdRprim const * GetRprim(SdfPath const &id) const
Returns the rprim of id.
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine.
Definition: changeTracker.h:51
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 void SceneIndexEmulationNoticeBatchBegin()
An application or legacy scene delegate may prefer for the scene index observer notices generated fro...
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:244
HD_API void RemoveRprim(SdfPath const &id)
Remove a rprim from index.
Scene delegate which observes notices from an HdSceneIndex and applies them to an HdRenderIndex.
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:54
HD_API void Clear()
Clear all r (render), s (state) and b (buffer) prims.
HD_API HdInstancer * GetInstancer(SdfPath const &id) const
Returns the instancer of id.
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 & S prims, (external) computations and updates the Rprim dirty list to then sync...
Adapter class providing data exchange with the client scene graph.
HD_API HdSprim * GetFallbackSprim(TfToken const &typeId) const
Returns the fullback prim for the Sprim of the given type.
bool HasInstancer(SdfPath const &id)
Returns true if instancer id exists in index.
Definition: renderIndex.h:266
HD_API bool IsBprimTypeSupported(TfToken const &typeId) const
Returns whether the bprim type is supported by this render index.
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:287
A named, semantic collection of objects.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
HD_API bool IsSprimTypeSupported(TfToken const &typeId) const
Returns whether the sprim type is supported by this render index.
HD_API SdfPathVector GetRprimSubtree(SdfPath const &root)
Returns the subtree rooted under the given path.
Used for faster iteration of dirty Rprims by the render index.
Definition: dirtyList.h:103
bool HasRprim(SdfPath const &id)
Returns true if rprim id exists in index.
Definition: renderIndex.h:214
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:51
HD_API HdSceneDelegate * GetSceneDelegateForRprim(SdfPath const &id) const
Returns the scene delegate for the given rprim.
A draw item is a light-weight representation of an HdRprim's resources and material to be used for re...
Definition: drawItem.h:66
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.
Bprim (buffer prim) is a base class of managing a blob of data that is used to communicate between th...
Definition: bprim.h:56
HD_API void SceneIndexEmulationNoticeBatchEnd()
Flushes any queued scene index observer notices and disables further queueing.
HD_API void EnqueueCollectionToSync(HdRprimCollection const &collection)
Hydra's core currently needs to know the collections used by tasks to aggregate the reprs that need t...
void InsertTask(HdSceneDelegate *delegate, SdfPath const &id)
Inserts a new task into the render index with an identifier of id.
Definition: renderIndex.h:577
HD_API HdBprim * GetFallbackBprim(TfToken const &typeId) const
Returns the fallback prim for the Bprim of the given type.
TfToken UpdateRenderTag(SdfPath const &id, HdDirtyBits bits)
Like GetRenderTag, but updates the render tag if dirty.
static HD_API bool IsSceneIndexEmulationEnabled()
Returns true if scene index features are available This is true by default but can be controlled via ...
HD_API bool GetSceneDelegateAndInstancerIds(SdfPath const &id, SdfPath *sceneDelegateId, SdfPath *instancerId) const
Query function to return the id's of the scene delegate and instancer associated with the Rprim at th...
HD_API void RemoveInstancer(SdfPath const &id)
Remove an instancer from index.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166
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.
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 TfToken GetRenderTag(SdfPath const &id) const
Returns the render tag for the given rprim.
HD_API HdTaskSharedPtr const & GetTask(SdfPath const &id) const
Returns the task for the given id.
HD_API HdResourceRegistrySharedPtr GetResourceRegistry() const
Returns a shared ptr to the resource registry of the current render delegate.