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  const HdSceneIndexBaseRefPtr &inputScene,
362  SdfPath const& scenePathPrefix);
363 
364  HD_API
365  void RemoveSceneIndex(
366  const HdSceneIndexBaseRefPtr &inputScene);
367 
370  HD_API
371  HdSceneIndexBaseRefPtr GetTerminalSceneIndex() const;
372 
373  // ---------------------------------------------------------------------- //
375  // ---------------------------------------------------------------------- //
379  HD_API
380  HdRenderDelegate *GetRenderDelegate() const;
381 
382  // The render delegate may require access to a render context / device
383  // that is provided by the application.
384  HD_API
385  HdDriverVector const& GetDrivers() const;
386 
389  HD_API
390  HdResourceRegistrySharedPtr GetResourceRegistry() const;
391 
395  HD_API
396  static bool IsSceneIndexEmulationEnabled();
397 
405  HD_API
407 
413  HD_API
415 
416 
417 private:
418  // The render index constructor is private so we can check
419  // renderDelegate before construction: see HdRenderIndex::New(...).
421  HdRenderDelegate *renderDelegate,
422  HdDriverVector const& drivers);
423 
424  // ---------------------------------------------------------------------- //
425  // Private Helper methods
426  // ---------------------------------------------------------------------- //
427 
428  // Go through all RPrims and reallocate their instance ids
429  // This is called once we have exhausted all 24bit instance ids.
430  void _CompactPrimIds();
431 
432  // Allocate the next available instance id to the prim
433  void _AllocatePrimId(HdRprim* prim);
434 
435  // Inserts the task into the index and updates tracking state.
436  // _TrackDelegateTask is called by the inlined InsertTask<T>, so it needs
437  // to be marked HD_API.
438  HD_API
439  void _TrackDelegateTask(HdSceneDelegate* delegate,
440  SdfPath const& taskId,
441  HdTaskSharedPtr const& task);
442 
443  template <typename T>
444  static inline const TfToken & _GetTypeId();
445 
446 
447  // Private versions of equivalent public methods which insert and remove
448  // from this render index.
449  //
450  // The public versions check to see if scene delegate emulation is active.
451  // If not, they call through to these. Otherwise, they forward to the
452  // the HdLegacyPrimSceneIndex member. If a legacy render delegate is also
453  // in use, the scene index chain will terminate with a
454  // HdSceneIndexAdapterSceneDelegate. That will call the private versions
455  // directly so that the internal render index tables are updated.
456  //
457  // This prevents cyclic insertion/removals while allowing a single
458  // HdRenderIndex to be used for both front and back-end emulation.
459  //
460  // Note: all render index users should call the public APIs; only
461  // sceneIndexAdapterSceneDelegate.cpp should call these versions, to keep
462  // state synchronized. Note, for example, that _RemoveSubtree and _Clear
463  // don't affect the task map, since tasks aren't part of emulation, whereas
464  // RemoveSubtree and Clear do affect the task map...
466  void _InsertRprim(TfToken const& typeId,
467  HdSceneDelegate* sceneDelegate,
468  SdfPath const& rprimId);
469  void _InsertSprim(TfToken const& typeId,
470  HdSceneDelegate* delegate,
471  SdfPath const& sprimId);
472  void _InsertBprim(TfToken const& typeId,
473  HdSceneDelegate* delegate,
474  SdfPath const& bprimId);
475  void _InsertInstancer(HdSceneDelegate* delegate,
476  SdfPath const &id);
477 
478  void _RemoveRprim(SdfPath const& id);
479  void _RemoveSprim(TfToken const& typeId, SdfPath const& id);
480  void _RemoveBprim(TfToken const& typeId, SdfPath const& id);
481  void _RemoveInstancer(SdfPath const& id);
482  void _RemoveSubtree(SdfPath const& id, HdSceneDelegate* sceneDelegate);
483  void _RemoveRprimSubtree(const SdfPath &root,
484  HdSceneDelegate* sceneDelegate);
485  void _RemoveInstancerSubtree(const SdfPath &root,
486  HdSceneDelegate* sceneDelegate);
487  void _RemoveExtComputationSubtree(const SdfPath &root,
488  HdSceneDelegate* sceneDelegate);
489  void _RemoveTaskSubtree(const SdfPath &root,
490  HdSceneDelegate* sceneDelegate);
491  void _Clear();
492 
493  // ---------------------------------------------------------------------- //
494  // Index State
495  // ---------------------------------------------------------------------- //
496  struct _RprimInfo {
497  HdSceneDelegate *sceneDelegate;
498  HdRprim *rprim;
499  };
500 
501  HdLegacyPrimSceneIndexRefPtr _emulationSceneIndex;
502  HdNoticeBatchingSceneIndexRefPtr _emulationNoticeBatchingSceneIndex;
503  unsigned int _noticeBatchingDepth;
504 
505  std::unique_ptr<class HdSceneIndexAdapterSceneDelegate> _siSd;
506 
507  HdMergingSceneIndexRefPtr _mergingSceneIndex;
508  HdSceneIndexBaseRefPtr _terminalSceneIndex;
509 
510  struct _TaskInfo {
511  HdSceneDelegate *sceneDelegate;
512  HdTaskSharedPtr task;
513  };
514 
515  typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
516  typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
517  typedef std::vector<SdfPath> _RprimPrimIDVector;
518 
519  typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
520  typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
521 
522  _RprimMap _rprimMap;
523  Hd_SortedIds _rprimIds;
524 
525  _RprimPrimIDVector _rprimPrimIdMap;
526 
527  _TaskMap _taskMap;
528 
529  _SprimIndex _sprimIndex;
530  _BprimIndex _bprimIndex;
531 
532  HdChangeTracker _tracker;
533 
534  typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
535  _InstancerMap _instancerMap;
536 
537  HdRenderDelegate *_renderDelegate;
538  HdDriverVector _drivers;
539 
540  // ---------------------------------------------------------------------- //
541  // Sync State
542  // ---------------------------------------------------------------------- //
543  HdRprimCollectionVector _collectionsToSync;
544  HdDirtyList _rprimDirtyList;
545 
546  // ---------------------------------------------------------------------- //
547 
549  void _InitPrimTypes();
550 
552  bool _CreateFallbackPrims();
553 
555  void _DestroyFallbackPrims();
556 
557  typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
558  _ConcurrentDrawItems;
559 
560  void _AppendDrawItems(const SdfPathVector &rprimIds,
561  size_t begin,
562  size_t end,
563  HdRprimCollection const& collection,
564  _ConcurrentDrawItems* result);
565 
569  static void _ConfigureReprs();
570 
571  // Remove default constructor
572  HdRenderIndex() = delete;
573 
574  // Don't allow copies
575  HdRenderIndex(const HdRenderIndex &) = delete;
576  HdRenderIndex &operator=(const HdRenderIndex &) = delete;
577 
578 };
579 
580 template <typename T>
581 void
583 {
584  HD_TRACE_FUNCTION();
585  HF_MALLOC_TAG_FUNCTION();
586 
587  HdTaskSharedPtr task = std::make_shared<T>(delegate, id);
588  _TrackDelegateTask(delegate, id, task);
589 }
590 
591 PXR_NAMESPACE_CLOSE_SCOPE
592 
593 #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 HdSceneIndexBaseRefPtr GetTerminalSceneIndex() const
The terminal scene index that is driving what is in the render index through emulation.
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:582
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.