Loading...
Searching...
No Matches
renderIndex.h
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_RENDER_INDEX_H
8#define PXR_IMAGING_HD_RENDER_INDEX_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hd/api.h"
12#include "pxr/imaging/hd/version.h"
13#include "pxr/imaging/hd/changeTracker.h"
14#include "pxr/imaging/hd/dirtyList.h"
15#include "pxr/imaging/hd/perfLog.h"
16#include "pxr/imaging/hd/primTypeIndex.h"
17#include "pxr/imaging/hd/resourceRegistry.h"
18#include "pxr/imaging/hd/sortedIds.h"
19#include "pxr/imaging/hd/tokens.h"
20
21#include "pxr/imaging/hd/sceneIndex.h"
22#include "pxr/imaging/hd/mergingSceneIndex.h"
23#include "pxr/imaging/hd/legacyPrimSceneIndex.h"
24#include "pxr/imaging/hd/legacyTaskFactory.h"
25#include "pxr/imaging/hd/noticeBatchingSceneIndex.h"
26
27#include "pxr/imaging/hf/perfLog.h"
28
29#include "pxr/usd/sdf/path.h"
30
31#include "pxr/base/gf/vec4i.h"
32#include "pxr/base/tf/hashmap.h"
33
34#include <tbb/enumerable_thread_specific.h>
35
36#include <vector>
37#include <unordered_map>
38#include <memory>
39
40PXR_NAMESPACE_OPEN_SCOPE
41
42class HdRprim;
43class HdSprim;
44class HdBprim;
45class HdDrawItem;
47class HdSceneDelegate;
48class HdRenderDelegate;
50class VtValue;
51class HdInstancer;
52class HdDriver;
53
54using HdDriverVector = std::vector<HdDriver*>;
55using HdRprimCollectionVector = std::vector<HdRprimCollection>;
56using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
57using HdResourceRegistrySharedPtr = std::shared_ptr<class HdResourceRegistry>;
58using HdTaskSharedPtrVector = std::vector<HdTaskSharedPtr>;
59using HdTaskContext = std::unordered_map<TfToken,
60 VtValue,
62
104class HdRenderIndex final
105{
106public:
107 typedef std::vector<HdDrawItem const*> HdDrawItemPtrVector;
108
114 // hgi = Hgi::CreatePlatformDefaultHgi()
115 // hgiDriver = new HdDriver<Hgi*>(HgiTokens→renderDriver, hgi)
116 // HdRenderIndex::New(_renderDelegate, {_hgiDriver})
117 //
126 HD_API
128 HdRenderDelegate *renderDelegate,
129 HdDriverVector const& drivers,
130 const std::string &instanceName=std::string(),
131 const std::string &appName=std::string());
132
144 HdRenderDelegate *renderDelegate,
145 HdDriverVector const& drivers,
146 HdSceneIndexBaseRefPtr const &terminalSceneIndex);
147
148 HD_API
150
152 HD_API
153 void Clear();
154
159 HD_API
160 void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
161
162 // ---------------------------------------------------------------------- //
165 HD_API
167
168 // ---------------------------------------------------------------------- //
170 // ---------------------------------------------------------------------- //
171
175 HD_API
177
183 HD_API
184 void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
185
186 // ---------------------------------------------------------------------- //
188 // ---------------------------------------------------------------------- //
189
191 // by renderTags and collection.
194 HD_API
195 HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
196 TfTokenVector const& renderTags);
197
198 // ---------------------------------------------------------------------- //
200 // ---------------------------------------------------------------------- //
201
202 HdChangeTracker& GetChangeTracker() { return _tracker; }
203 HdChangeTracker const& GetChangeTracker() const { return _tracker; }
204
205 // ---------------------------------------------------------------------- //
207 // ---------------------------------------------------------------------- //
208
210 HD_API
211 bool IsRprimTypeSupported(TfToken const& typeId) const;
212
214 HD_API
215 void InsertRprim(TfToken const& typeId,
216 HdSceneDelegate* sceneDelegate,
217 SdfPath const& rprimId);
218
220 HD_API
221 void RemoveRprim(SdfPath const& id);
222
224 bool HasRprim(SdfPath const& id) {
225 return _rprimMap.find(id) != _rprimMap.end();
226 }
227
229 HD_API
230 HdRprim const *GetRprim(SdfPath const &id) const;
231
233 HD_API
235
238 HD_API
240 SdfPath* sceneDelegateId,
241 SdfPath* instancerId) const;
242
244 HD_API
245 TfToken GetRenderTag(SdfPath const& id) const;
246
249 HdDirtyBits bits);
250
253 HD_API
254 const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
255
256
258 HD_API
259 SdfPathVector GetRprimSubtree(SdfPath const& root);
260
261
262 // ---------------------------------------------------------------------- //
264 // ---------------------------------------------------------------------- //
265
267 HD_API
269 SdfPath const &id);
270
272 HD_API
273 void RemoveInstancer(SdfPath const& id);
274
276 bool HasInstancer(SdfPath const& id) {
277 return _instancerMap.find(id) != _instancerMap.end();
278 }
279
281 HD_API
283
284 // ---------------------------------------------------------------------- //
286 // ---------------------------------------------------------------------- //
287
294 template <typename T>
295 void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
296
303 HD_API
304 void RemoveTask(SdfPath const& id);
305
312 bool HasTask(SdfPath const& id) {
313 return _taskMap.find(id) != _taskMap.end();
314 }
315
317 HD_API
318 HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
319
320 // ---------------------------------------------------------------------- //
322 // ---------------------------------------------------------------------- //
323
325 HD_API
326 bool IsSprimTypeSupported(TfToken const& typeId) const;
327
329 HD_API
330 void InsertSprim(TfToken const& typeId,
331 HdSceneDelegate* delegate,
332 SdfPath const& sprimId);
333
334 HD_API
335 void RemoveSprim(TfToken const& typeId, SdfPath const &id);
336
337 HD_API
338 HdSprim *GetSprim(TfToken const& typeId, SdfPath const &id) const;
339
342 HD_API
343 SdfPathVector GetSprimSubtree(TfToken const& typeId,
344 SdfPath const& root);
345
347 HD_API
348 HdSprim *GetFallbackSprim(TfToken const& typeId) const;
349
350
351 // ---------------------------------------------------------------------- //
353 // ---------------------------------------------------------------------- //
354
356 HD_API
357 bool IsBprimTypeSupported(TfToken const& typeId) const;
358
360 HD_API
361 void InsertBprim(TfToken const& typeId,
362 HdSceneDelegate* delegate,
363 SdfPath const& bprimId);
364
365 HD_API
366 void RemoveBprim(TfToken const& typeId, SdfPath const &id);
367
368 HD_API
369 HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
370
373 HD_API
374 SdfPathVector GetBprimSubtree(TfToken const& typeId,
375 SdfPath const& root);
376
378 HD_API
379 HdBprim *GetFallbackBprim(TfToken const& typeId) const;
380
381 // ---------------------------------------------------------------------- //
383 // ---------------------------------------------------------------------- //
384 HD_API
385 void InsertSceneIndex(
386 const HdSceneIndexBaseRefPtr &inputScene,
387 SdfPath const& scenePathPrefix,
388 bool needsPrefixing = true);
389
390 HD_API
391 void RemoveSceneIndex(
392 const HdSceneIndexBaseRefPtr &inputScene);
393
396 HD_API
397 HdSceneIndexBaseRefPtr GetTerminalSceneIndex() const;
398
399 // ---------------------------------------------------------------------- //
401 // ---------------------------------------------------------------------- //
405 HD_API
406 HdRenderDelegate *GetRenderDelegate() const;
407
408 // The render delegate may require access to a render context / device
409 // that is provided by the application.
410 HD_API
411 HdDriverVector const& GetDrivers() const;
412
415 HD_API
416 HdResourceRegistrySharedPtr GetResourceRegistry() const;
417
421 HD_API
423
431 HD_API
433
439 HD_API
441
447 HD_API
449
455 HD_API
457
458 HD_API
459 std::string GetInstanceName() const;
460
461private:
462 // The render index constructor is private so we can check
463 // renderDelegate before construction: see HdRenderIndex::New(...).
465 HdRenderDelegate *renderDelegate,
466 HdDriverVector const& drivers,
467 const std::string &instanceName,
468 const std::string &appName,
469 HdSceneIndexBaseRefPtr const &terminalSceneIndex);
470
471 // ---------------------------------------------------------------------- //
472 // Private Helper methods
473 // ---------------------------------------------------------------------- //
474
475 // Go through all RPrims and reallocate their instance ids
476 // This is called once we have exhausted all 24bit instance ids.
477 void _CompactPrimIds();
478
479 // Allocate the next available instance id to the prim
480 void _AllocatePrimId(HdRprim* prim);
481
482 using HdTaskCreateFnc =
483 std::function<HdTaskSharedPtr(HdSceneDelegate*, SdfPath const&)>;
484
485 // Implements InsertTask<T>.
486 // Inserts task going through emulation if enabled.
487 HD_API
488 void _InsertSceneDelegateTask(
489 HdSceneDelegate* delegate,
490 SdfPath const& taskId,
491 HdLegacyTaskFactorySharedPtr factory);
492
493 template <typename T>
494 static inline const TfToken & _GetTypeId();
495
496
497 // Private versions of equivalent public methods which insert and remove
498 // from this render index.
499 //
500 // The public versions check to see if scene delegate emulation is active.
501 // If not, they call through to these. Otherwise, they forward to the
502 // the HdLegacyPrimSceneIndex member. If a legacy render delegate is also
503 // in use, the scene index chain will terminate with a
504 // HdSceneIndexAdapterSceneDelegate. That will call the private versions
505 // directly so that the internal render index tables are updated.
506 //
507 // This prevents cyclic insertion/removals while allowing a single
508 // HdRenderIndex to be used for both front and back-end emulation.
509 //
510 // Note: all render index users should call the public APIs; only
511 // sceneIndexAdapterSceneDelegate.cpp should call these versions, to keep
512 // state synchronized. Note, for example, that _RemoveSubtree and _Clear
513 // don't affect the task map, since tasks aren't part of emulation, whereas
514 // RemoveSubtree and Clear do affect the task map...
516 void _InsertRprim(TfToken const& typeId,
517 HdSceneDelegate* sceneDelegate,
518 SdfPath const& rprimId);
519 void _InsertSprim(TfToken const& typeId,
520 HdSceneDelegate* delegate,
521 SdfPath const& sprimId);
522 void _InsertBprim(TfToken const& typeId,
523 HdSceneDelegate* delegate,
524 SdfPath const& bprimId);
525 void _InsertInstancer(HdSceneDelegate* delegate,
526 SdfPath const &id);
527 void _InsertTask(HdSceneDelegate* delegate,
528 SdfPath const &id,
529 HdTaskSharedPtr const &task);
530
531 void _RemoveRprim(SdfPath const& id);
532 void _RemoveSprim(TfToken const& typeId, SdfPath const& id);
533 void _RemoveBprim(TfToken const& typeId, SdfPath const& id);
534 void _RemoveInstancer(SdfPath const& id);
535 void _RemoveSubtree(SdfPath const& id, HdSceneDelegate* sceneDelegate);
536 void _RemoveRprimSubtree(const SdfPath &root,
537 HdSceneDelegate* sceneDelegate);
538 void _RemoveInstancerSubtree(const SdfPath &root,
539 HdSceneDelegate* sceneDelegate);
540 void _RemoveExtComputationSubtree(const SdfPath &root,
541 HdSceneDelegate* sceneDelegate);
542 void _RemoveTaskSubtree(const SdfPath &root,
543 HdSceneDelegate* sceneDelegate);
544 void _RemoveTask(SdfPath const &id);
545 void _Clear();
546
547 // ---------------------------------------------------------------------- //
548 // Index State
549 // ---------------------------------------------------------------------- //
550 struct _RprimInfo {
551 HdSceneDelegate *sceneDelegate;
552 HdRprim *rprim;
553 };
554
555 class _NoticeBatchingContext;
556
557 HdLegacyPrimSceneIndexRefPtr _emulationSceneIndex;
558 std::unique_ptr<_NoticeBatchingContext> _emulationBatchingCtx;
559
560 std::unique_ptr<class HdSceneIndexAdapterSceneDelegate> _siSd;
561
562 HdMergingSceneIndexRefPtr _mergingSceneIndex;
563 std::unique_ptr<_NoticeBatchingContext> _mergingBatchingCtx;
564
565 HdSceneIndexBaseRefPtr _terminalSceneIndex;
566
567 struct _TaskInfo {
568 HdSceneDelegate *sceneDelegate;
569 HdTaskSharedPtr task;
570 };
571
572 typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
573 typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
574 typedef std::vector<SdfPath> _RprimPrimIDVector;
575
576 typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
577 typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
578
579 _RprimMap _rprimMap;
580 Hd_SortedIds _rprimIds;
581
582 _RprimPrimIDVector _rprimPrimIdMap;
583
584 _TaskMap _taskMap;
585
586 _SprimIndex _sprimIndex;
587 _BprimIndex _bprimIndex;
588
589 HdChangeTracker _tracker;
590
591 typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
592 _InstancerMap _instancerMap;
593
594 HdRenderDelegate *_renderDelegate;
595 HdDriverVector _drivers;
596
597
598 std::string _instanceName;
599
600 // ---------------------------------------------------------------------- //
601 // Sync State
602 // ---------------------------------------------------------------------- //
603 HdRprimCollectionVector _collectionsToSync;
604 HdDirtyList _rprimDirtyList;
605
606 // ---------------------------------------------------------------------- //
607
609 void _InitPrimTypes();
610
612 bool _CreateFallbackPrims();
613
615 void _DestroyFallbackPrims();
616
617 typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
618 _ConcurrentDrawItems;
619
620 void _AppendDrawItems(const SdfPathVector &rprimIds,
621 size_t begin,
622 size_t end,
623 HdRprimCollection const& collection,
624 _ConcurrentDrawItems* result);
625
629 static void _ConfigureReprs();
630
631 // Remove default constructor
632 HdRenderIndex() = delete;
633
634 // Don't allow copies
635 HdRenderIndex(const HdRenderIndex &) = delete;
636 HdRenderIndex &operator=(const HdRenderIndex &) = delete;
637
638};
639
640template <typename T>
641void
643{
644 _InsertSceneDelegateTask(
645 delegate, id, HdMakeLegacyTaskFactory<T>());
646}
647
648PXR_NAMESPACE_CLOSE_SCOPE
649
650#endif //PXR_IMAGING_HD_RENDER_INDEX_H
Bprim (buffer prim) is a base class of managing a blob of data that is used to communicate between th...
Definition: bprim.h:40
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine.
Definition: changeTracker.h:35
Used for faster iteration of dirty Rprims by the render index.
Definition: dirtyList.h:86
A draw item is a light-weight representation of an HdRprim's resources and material to be used for re...
Definition: drawItem.h:48
HdDriver represents a device object, commonly a render device, that is owned by the application and p...
Definition: driver.h:23
Hydra Representation of a Client defined computation.
This class exists to facilitate point cloud style instancing.
Definition: instancer.h:108
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:105
HD_API void MergingSceneIndexNoticeBatchEnd()
Flushes any queued scene index observer notices from the merging scene index and disables further que...
static HD_API HdRenderIndex * New(HdRenderDelegate *renderDelegate, HdDriverVector const &drivers, const std::string &instanceName=std::string(), const std::string &appName=std::string())
Create a render index with the given render delegate.
HD_API HdInstancer * GetInstancer(SdfPath const &id) const
Returns the instancer of id.
HD_API void RemoveRprim(SdfPath const &id)
Remove a rprim from index.
HD_API HdTaskSharedPtr const & GetTask(SdfPath const &id) const
Returns the task for the given id.
HD_API void RemoveInstancer(SdfPath const &id)
Remove an instancer from index.
HD_API SdfPathVector GetBprimSubtree(TfToken const &typeId, SdfPath const &root)
Returns the subtree rooted under the given path for the given bprim type.
HD_API void RemoveTask(SdfPath const &id)
void InsertTask(HdSceneDelegate *delegate, SdfPath const &id)
Definition: renderIndex.h:642
TfToken UpdateRenderTag(SdfPath const &id, HdDirtyBits bits)
Like GetRenderTag, but updates the render tag if dirty.
HD_API bool IsBprimTypeSupported(TfToken const &typeId) const
Returns whether the bprim type is supported by this render index.
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...
HD_API void InsertBprim(TfToken const &typeId, HdSceneDelegate *delegate, SdfPath const &bprimId)
Insert a bprim into index.
HD_API HdBprim * GetFallbackBprim(TfToken const &typeId) const
Returns the fallback prim for the Bprim of the given type.
HD_API void Clear()
Clear all r (render), s (state) and b (buffer) prims.
HD_API HdSceneDelegate * GetSceneDelegateForRprim(SdfPath const &id) const
Returns the scene delegate for the given rprim.
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...
HD_API bool IsRprimTypeSupported(TfToken const &typeId) const
Returns whether the rprim type is supported by this render index.
HD_API HdSceneIndexBaseRefPtr GetTerminalSceneIndex() const
The terminal scene index that is driving what is in the render index through emulation.
bool HasRprim(SdfPath const &id)
Returns true if rprim id exists in index.
Definition: renderIndex.h:224
HD_API void SceneIndexEmulationNoticeBatchBegin()
An application or legacy scene delegate may prefer for the scene index observer notices generated fro...
HD_API HdSprim * GetFallbackSprim(TfToken const &typeId) const
Returns the fullback prim for the Sprim of the given type.
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.
HD_API const SdfPathVector & GetRprimIds()
Returns a sorted list of all Rprims in the render index.
Definition: renderIndex.h:254
HD_API void SceneIndexEmulationNoticeBatchEnd()
Flushes any queued scene index observer notices and disables further queueing.
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 HdDrawItemPtrVector GetDrawItems(HdRprimCollection const &collection, TfTokenVector const &renderTags)
Returns a list of relevant draw items that match the criteria specified.
HD_API void MergingSceneIndexNoticeBatchBegin()
Consolidate notices generated by the merging scene index to vectorized batches.
HD_API HdRprim const * GetRprim(SdfPath const &id) const
Returns the rprim of id.
static HdRenderIndex * New(HdRenderDelegate *renderDelegate, HdDriverVector const &drivers, HdSceneIndexBaseRefPtr const &terminalSceneIndex)
Create a render index with the given render delegate that populates itself by observing the given sce...
HD_API TfToken GetRenderTag(SdfPath const &id) const
Returns the render tag for the given rprim.
HD_API void InsertRprim(TfToken const &typeId, HdSceneDelegate *sceneDelegate, SdfPath const &rprimId)
Insert a rprim into 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.
HD_API HdResourceRegistrySharedPtr GetResourceRegistry() const
Returns a shared ptr to the resource registry of the current render delegate.
bool HasTask(SdfPath const &id)
Definition: renderIndex.h:312
bool HasInstancer(SdfPath const &id)
Returns true if instancer id exists in index.
Definition: renderIndex.h:276
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.
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 InsertInstancer(HdSceneDelegate *delegate, SdfPath const &id)
Insert an instancer into index.
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 void InsertSprim(TfToken const &typeId, HdSceneDelegate *delegate, SdfPath const &sprimId)
Insert a sprim into index.
HD_API HdRenderDelegate * GetRenderDelegate() const
Currently, a render index only supports connection to one type of render delegate,...
A named, semantic collection of objects.
The render engine state for a given rprim from the scene graph.
Definition: rprim.h:38
Adapter class providing data exchange with the client scene graph.
Scene delegate which observes notices from an HdSceneIndex and applies them to an HdRenderIndex.
Sprim (state prim) is a base class of managing state for non-drawable scene entity (e....
Definition: sprim.h:35
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:152
Functor to use for hash maps from tokens to other things.
Definition: token.h:149
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440