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
109class HdRenderIndex final
110{
111public:
112 typedef std::vector<HdDrawItem const*> HdDrawItemPtrVector;
113
123 // hgi = Hgi::CreatePlatformDefaultHgi()
124 // hgiDriver = new HdDriver<Hgi*>(HgiTokens→renderDriver, hgi)
125 // HdRenderIndex::New(_renderDelegate, {_hgiDriver})
126 //
135 HD_API
137 HdRenderDelegate *renderDelegate,
138 HdDriverVector const& drivers,
139 const std::string &instanceName=std::string(),
140 const std::string &appName=std::string());
141
153 HdRenderDelegate *renderDelegate,
154 HdDriverVector const& drivers,
155 HdSceneIndexBaseRefPtr const &terminalSceneIndex);
156
165 static HdRenderIndex *
166 NewForFrontendEmulation(HdRenderDelegate *nullRenderDelegate);
167
168 HD_API
170
172 HD_API
173 void Clear();
174
179 HD_API
180 void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
181
182 // ---------------------------------------------------------------------- //
185 HD_API
187
188 // ---------------------------------------------------------------------- //
190 // ---------------------------------------------------------------------- //
191
195 HD_API
197
203 HD_API
204 void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
205
207 HD_API bool IsSyncAllInProgress() const;
208
209 // ---------------------------------------------------------------------- //
211 // ---------------------------------------------------------------------- //
212
214 // by renderTags and collection.
217 HD_API
218 HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
219 TfTokenVector const& renderTags);
220
221 // ---------------------------------------------------------------------- //
223 // ---------------------------------------------------------------------- //
224
225 HdChangeTracker& GetChangeTracker() { return _tracker; }
226 HdChangeTracker const& GetChangeTracker() const { return _tracker; }
227
228 // ---------------------------------------------------------------------- //
230 // ---------------------------------------------------------------------- //
231
233 HD_API
234 bool IsRprimTypeSupported(TfToken const& typeId) const;
235
237 HD_API
238 void InsertRprim(TfToken const& typeId,
239 HdSceneDelegate* sceneDelegate,
240 SdfPath const& rprimId);
241
243 HD_API
244 void RemoveRprim(SdfPath const& id);
245
247 bool HasRprim(SdfPath const& id) {
248 return _rprimMap.find(id) != _rprimMap.end();
249 }
250
252 HD_API
253 HdRprim const *GetRprim(SdfPath const &id) const;
254
256 HD_API
258
265 HD_API
267 SdfPath* sceneDelegateId,
268 SdfPath* instancerId) const;
269
271 HD_API
272 TfToken GetRenderTag(SdfPath const& id) const;
273
276 HdDirtyBits bits);
277
280 HD_API
281 const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
282
283
285 HD_API
286 SdfPathVector GetRprimSubtree(SdfPath const& root);
287
288
289 // ---------------------------------------------------------------------- //
291 // ---------------------------------------------------------------------- //
292
294 HD_API
296 SdfPath const &id);
297
299 HD_API
300 void RemoveInstancer(SdfPath const& id);
301
303 bool HasInstancer(SdfPath const& id) {
304 return _instancerMap.find(id) != _instancerMap.end();
305 }
306
308 HD_API
310
311 // ---------------------------------------------------------------------- //
313 // ---------------------------------------------------------------------- //
314
321 template <typename T>
322 void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
323
330 HD_API
331 void RemoveTask(SdfPath const& id);
332
339 bool HasTask(SdfPath const& id) {
340 return _taskMap.find(id) != _taskMap.end();
341 }
342
344 HD_API
345 HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
346
347 // ---------------------------------------------------------------------- //
349 // ---------------------------------------------------------------------- //
350
352 HD_API
353 bool IsSprimTypeSupported(TfToken const& typeId) const;
354
356 HD_API
357 void InsertSprim(TfToken const& typeId,
358 HdSceneDelegate* delegate,
359 SdfPath const& sprimId);
360
361 HD_API
362 void RemoveSprim(TfToken const& typeId, SdfPath const &id);
363
364 HD_API
365 HdSprim *GetSprim(TfToken const& typeId, SdfPath const &id) const;
366
369 HD_API
370 SdfPathVector GetSprimSubtree(TfToken const& typeId,
371 SdfPath const& root);
372
374 HD_API
375 HdSprim *GetFallbackSprim(TfToken const& typeId) const;
376
377
378 // ---------------------------------------------------------------------- //
380 // ---------------------------------------------------------------------- //
381
383 HD_API
384 bool IsBprimTypeSupported(TfToken const& typeId) const;
385
387 HD_API
388 void InsertBprim(TfToken const& typeId,
389 HdSceneDelegate* delegate,
390 SdfPath const& bprimId);
391
392 HD_API
393 void RemoveBprim(TfToken const& typeId, SdfPath const &id);
394
395 HD_API
396 HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
397
400 HD_API
401 SdfPathVector GetBprimSubtree(TfToken const& typeId,
402 SdfPath const& root);
403
405 HD_API
406 HdBprim *GetFallbackBprim(TfToken const& typeId) const;
407
408 // ---------------------------------------------------------------------- //
410 // ---------------------------------------------------------------------- //
411 HD_API
412 void InsertSceneIndex(
413 const HdSceneIndexBaseRefPtr &inputScene,
414 SdfPath const& scenePathPrefix,
415 bool needsPrefixing = true);
416
417 HD_API
418 void RemoveSceneIndex(
419 const HdSceneIndexBaseRefPtr &inputScene);
420
423 HD_API
424 HdSceneIndexBaseRefPtr GetTerminalSceneIndex() const;
425
426 HD_API
427 HdSceneIndexBaseRefPtr GetEmulationSceneIndex() const;
428
429 // ---------------------------------------------------------------------- //
431 // ---------------------------------------------------------------------- //
435 HD_API
436 HdRenderDelegate *GetRenderDelegate() const;
437
438 // The render delegate may require access to a render context / device
439 // that is provided by the application.
440 HD_API
441 HdDriverVector const& GetDrivers() const;
442
445 HD_API
446 HdResourceRegistrySharedPtr GetResourceRegistry() const;
447
455 HD_API
457
463 HD_API
465
471 HD_API
473
479 HD_API
481
482 HD_API
483 std::string GetInstanceName() const;
484
485private:
486 // The render index constructor is private so we can check
487 // renderDelegate before construction: see HdRenderIndex::New(...).
489 HdRenderDelegate *renderDelegate,
490 HdDriverVector const& drivers,
491 const std::string &instanceName,
492 const std::string &appName,
493 HdSceneIndexBaseRefPtr const &terminalSceneIndex = nullptr,
494 bool createFrontendEmulationOnly = false);
495
496 // ---------------------------------------------------------------------- //
497 // Private Helper methods
498 // ---------------------------------------------------------------------- //
499
500 // Go through all RPrims and reallocate their instance ids
501 // This is called once we have exhausted all 24bit instance ids.
502 void _CompactPrimIds();
503
504 // Allocate the next available instance id to the prim
505 void _AllocatePrimId(HdRprim* prim);
506
507 using HdTaskCreateFnc =
508 std::function<HdTaskSharedPtr(HdSceneDelegate*, SdfPath const&)>;
509
510 // Implements InsertTask<T>.
511 // Inserts task going through emulation if enabled.
512 HD_API
513 void _InsertSceneDelegateTask(
514 HdSceneDelegate* delegate,
515 SdfPath const& taskId,
516 HdLegacyTaskFactorySharedPtr factory);
517
518 template <typename T>
519 static inline const TfToken & _GetTypeId();
520
521
522 // Private versions of equivalent public methods which insert and remove
523 // from this render index.
524 //
525 // The public versions check to see if scene delegate emulation is active.
526 // If not, they call through to these. Otherwise, they forward to the
527 // the HdLegacyPrimSceneIndex member. If a legacy render delegate is also
528 // in use, the scene index chain will terminate with a
529 // HdSceneIndexAdapterSceneDelegate. That will call the private versions
530 // directly so that the internal render index tables are updated.
531 //
532 // This prevents cyclic insertion/removals while allowing a single
533 // HdRenderIndex to be used for both front and back-end emulation.
534 //
535 // Note: all render index users should call the public APIs; only
536 // sceneIndexAdapterSceneDelegate.cpp should call these versions, to keep
537 // state synchronized. Note, for example, that _RemoveSubtree and _Clear
538 // don't affect the task map, since tasks aren't part of emulation, whereas
539 // RemoveSubtree and Clear do affect the task map...
541 void _InsertRprim(TfToken const& typeId,
542 HdSceneDelegate* sceneDelegate,
543 SdfPath const& rprimId);
544 void _InsertSprim(TfToken const& typeId,
545 HdSceneDelegate* delegate,
546 SdfPath const& sprimId);
547 void _InsertBprim(TfToken const& typeId,
548 HdSceneDelegate* delegate,
549 SdfPath const& bprimId);
550 void _InsertInstancer(HdSceneDelegate* delegate,
551 SdfPath const &id);
552 void _InsertTask(HdSceneDelegate* delegate,
553 SdfPath const &id,
554 HdTaskSharedPtr const &task);
555
556 void _RemoveRprim(SdfPath const& id);
557 void _RemoveSprim(TfToken const& typeId, SdfPath const& id);
558 void _RemoveBprim(TfToken const& typeId, SdfPath const& id);
559 void _RemoveInstancer(SdfPath const& id);
560 void _RemoveSubtree(SdfPath const& id, HdSceneDelegate* sceneDelegate);
561 void _RemoveRprimSubtree(const SdfPath &root,
562 HdSceneDelegate* sceneDelegate);
563 void _RemoveInstancerSubtree(const SdfPath &root,
564 HdSceneDelegate* sceneDelegate);
565 void _RemoveExtComputationSubtree(const SdfPath &root,
566 HdSceneDelegate* sceneDelegate);
567 void _RemoveTaskSubtree(const SdfPath &root,
568 HdSceneDelegate* sceneDelegate);
569 void _RemoveTask(SdfPath const &id);
570 void _Clear();
571
572 // ---------------------------------------------------------------------- //
573 // Index State
574 // ---------------------------------------------------------------------- //
575 struct _RprimInfo {
576 HdSceneDelegate *sceneDelegate;
577 HdRprim *rprim;
578 };
579
580 class _NoticeBatchingContext;
581
582 HdLegacyPrimSceneIndexRefPtr _emulationSceneIndex;
583 std::unique_ptr<_NoticeBatchingContext> _emulationBatchingCtx;
584
585 std::unique_ptr<class HdSceneIndexAdapterSceneDelegate> _siSd;
586
587 HdMergingSceneIndexRefPtr _mergingSceneIndex;
588 std::unique_ptr<_NoticeBatchingContext> _mergingBatchingCtx;
589
590 HdSceneIndexBaseRefPtr _terminalSceneIndex;
591
592 struct _TaskInfo {
593 HdSceneDelegate *sceneDelegate;
594 HdTaskSharedPtr task;
595 };
596
597 typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
598 typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
599 typedef std::vector<SdfPath> _RprimPrimIDVector;
600
601 typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
602 typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
603
604 _RprimMap _rprimMap;
605 Hd_SortedIds _rprimIds;
606
607 _RprimPrimIDVector _rprimPrimIdMap;
608
609 _TaskMap _taskMap;
610
611 _SprimIndex _sprimIndex;
612 _BprimIndex _bprimIndex;
613
614 HdChangeTracker _tracker;
615
616 typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
617 _InstancerMap _instancerMap;
618
619 HdRenderDelegate *_renderDelegate;
620 HdDriverVector _drivers;
621
622 HdSceneIndexBaseRefPtr _finalEmulationSceneIndex;
623
624 std::string _instanceName;
625
626 // ---------------------------------------------------------------------- //
627 // Sync State
628 // ---------------------------------------------------------------------- //
629 HdRprimCollectionVector _collectionsToSync;
630 HdDirtyList _rprimDirtyList;
631
632 // Call depth to SyncAll(). Should only ever be 0 or 1.
633 std::atomic<int> _syncAllDepth;
634
635 // ---------------------------------------------------------------------- //
636
638 void _InitPrimTypes();
639
641 bool _CreateFallbackPrims();
642
644 void _DestroyFallbackPrims();
645
646 typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
647 _ConcurrentDrawItems;
648
649 void _AppendDrawItems(const SdfPathVector &rprimIds,
650 size_t begin,
651 size_t end,
652 HdRprimCollection const& collection,
653 _ConcurrentDrawItems* result);
654
658 static void _ConfigureReprs();
659
660 // Remove default constructor
661 HdRenderIndex() = delete;
662
663 // Don't allow copies
664 HdRenderIndex(const HdRenderIndex &) = delete;
665 HdRenderIndex &operator=(const HdRenderIndex &) = delete;
666
667};
668
669template <typename T>
670void
672{
673 _InsertSceneDelegateTask(
674 delegate, id, HdMakeLegacyTaskFactory<T>());
675}
676
677PXR_NAMESPACE_CLOSE_SCOPE
678
679#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.
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 render index is part of the Hydra 1.0 API and is only used for emulation purposes so that HdScene...
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())
"instanceName" is an optional identifier useful for applications to associate this render index with ...
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)
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.
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.
HD_API void SceneIndexEmulationNoticeBatchEnd()
Flushes any queued scene index observer notices and disables further queueing.
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.
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.
static HdRenderIndex * NewForBackendEmulation(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...
static HdRenderIndex * NewForFrontendEmulation(HdRenderDelegate *nullRenderDelegate)
Create a render index for "front-end" emulation.
bool HasTask(SdfPath const &id)
bool HasInstancer(SdfPath const &id)
Returns true if instancer id exists in index.
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 IsSyncAllInProgress() const
Returns true while a thread is invoking SyncAll().
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:281
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:90
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