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
156 static HdRenderIndex *
157 New(HdRenderDelegate *renderDelegate);
158
159 HD_API
161
163 HD_API
164 void Clear();
165
170 HD_API
171 void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
172
173 // ---------------------------------------------------------------------- //
176 HD_API
178
179 // ---------------------------------------------------------------------- //
181 // ---------------------------------------------------------------------- //
182
186 HD_API
188
194 HD_API
195 void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
196
197 // ---------------------------------------------------------------------- //
199 // ---------------------------------------------------------------------- //
200
202 // by renderTags and collection.
205 HD_API
206 HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
207 TfTokenVector const& renderTags);
208
209 // ---------------------------------------------------------------------- //
211 // ---------------------------------------------------------------------- //
212
213 HdChangeTracker& GetChangeTracker() { return _tracker; }
214 HdChangeTracker const& GetChangeTracker() const { return _tracker; }
215
216 // ---------------------------------------------------------------------- //
218 // ---------------------------------------------------------------------- //
219
221 HD_API
222 bool IsRprimTypeSupported(TfToken const& typeId) const;
223
225 HD_API
226 void InsertRprim(TfToken const& typeId,
227 HdSceneDelegate* sceneDelegate,
228 SdfPath const& rprimId);
229
231 HD_API
232 void RemoveRprim(SdfPath const& id);
233
235 bool HasRprim(SdfPath const& id) {
236 return _rprimMap.find(id) != _rprimMap.end();
237 }
238
240 HD_API
241 HdRprim const *GetRprim(SdfPath const &id) const;
242
244 HD_API
246
253 HD_API
255 SdfPath* sceneDelegateId,
256 SdfPath* instancerId) const;
257
259 HD_API
260 TfToken GetRenderTag(SdfPath const& id) const;
261
264 HdDirtyBits bits);
265
268 HD_API
269 const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
270
271
273 HD_API
274 SdfPathVector GetRprimSubtree(SdfPath const& root);
275
276
277 // ---------------------------------------------------------------------- //
279 // ---------------------------------------------------------------------- //
280
282 HD_API
284 SdfPath const &id);
285
287 HD_API
288 void RemoveInstancer(SdfPath const& id);
289
291 bool HasInstancer(SdfPath const& id) {
292 return _instancerMap.find(id) != _instancerMap.end();
293 }
294
296 HD_API
298
299 // ---------------------------------------------------------------------- //
301 // ---------------------------------------------------------------------- //
302
309 template <typename T>
310 void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
311
318 HD_API
319 void RemoveTask(SdfPath const& id);
320
327 bool HasTask(SdfPath const& id) {
328 return _taskMap.find(id) != _taskMap.end();
329 }
330
332 HD_API
333 HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
334
335 // ---------------------------------------------------------------------- //
337 // ---------------------------------------------------------------------- //
338
340 HD_API
341 bool IsSprimTypeSupported(TfToken const& typeId) const;
342
344 HD_API
345 void InsertSprim(TfToken const& typeId,
346 HdSceneDelegate* delegate,
347 SdfPath const& sprimId);
348
349 HD_API
350 void RemoveSprim(TfToken const& typeId, SdfPath const &id);
351
352 HD_API
353 HdSprim *GetSprim(TfToken const& typeId, SdfPath const &id) const;
354
357 HD_API
358 SdfPathVector GetSprimSubtree(TfToken const& typeId,
359 SdfPath const& root);
360
362 HD_API
363 HdSprim *GetFallbackSprim(TfToken const& typeId) const;
364
365
366 // ---------------------------------------------------------------------- //
368 // ---------------------------------------------------------------------- //
369
371 HD_API
372 bool IsBprimTypeSupported(TfToken const& typeId) const;
373
375 HD_API
376 void InsertBprim(TfToken const& typeId,
377 HdSceneDelegate* delegate,
378 SdfPath const& bprimId);
379
380 HD_API
381 void RemoveBprim(TfToken const& typeId, SdfPath const &id);
382
383 HD_API
384 HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
385
388 HD_API
389 SdfPathVector GetBprimSubtree(TfToken const& typeId,
390 SdfPath const& root);
391
393 HD_API
394 HdBprim *GetFallbackBprim(TfToken const& typeId) const;
395
396 // ---------------------------------------------------------------------- //
398 // ---------------------------------------------------------------------- //
399 HD_API
400 void InsertSceneIndex(
401 const HdSceneIndexBaseRefPtr &inputScene,
402 SdfPath const& scenePathPrefix,
403 bool needsPrefixing = true);
404
405 HD_API
406 void RemoveSceneIndex(
407 const HdSceneIndexBaseRefPtr &inputScene);
408
411 HD_API
412 HdSceneIndexBaseRefPtr GetTerminalSceneIndex() const;
413
414 HD_API
415 HdSceneIndexBaseRefPtr GetEmulationSceneIndex() const;
416
417 // ---------------------------------------------------------------------- //
419 // ---------------------------------------------------------------------- //
423 HD_API
424 HdRenderDelegate *GetRenderDelegate() const;
425
426 // The render delegate may require access to a render context / device
427 // that is provided by the application.
428 HD_API
429 HdDriverVector const& GetDrivers() const;
430
433 HD_API
434 HdResourceRegistrySharedPtr GetResourceRegistry() const;
435
439 HD_API
441
449 HD_API
451
457 HD_API
459
465 HD_API
467
473 HD_API
475
476 HD_API
477 std::string GetInstanceName() const;
478
479private:
480 // The render index constructor is private so we can check
481 // renderDelegate before construction: see HdRenderIndex::New(...).
483 HdRenderDelegate *renderDelegate,
484 HdDriverVector const& drivers,
485 const std::string &instanceName,
486 const std::string &appName,
487 HdSceneIndexBaseRefPtr const &terminalSceneIndex = nullptr,
488 bool createFrontEndEmulationOnly = false);
489
490 // ---------------------------------------------------------------------- //
491 // Private Helper methods
492 // ---------------------------------------------------------------------- //
493
494 // Go through all RPrims and reallocate their instance ids
495 // This is called once we have exhausted all 24bit instance ids.
496 void _CompactPrimIds();
497
498 // Allocate the next available instance id to the prim
499 void _AllocatePrimId(HdRprim* prim);
500
501 using HdTaskCreateFnc =
502 std::function<HdTaskSharedPtr(HdSceneDelegate*, SdfPath const&)>;
503
504 // Implements InsertTask<T>.
505 // Inserts task going through emulation if enabled.
506 HD_API
507 void _InsertSceneDelegateTask(
508 HdSceneDelegate* delegate,
509 SdfPath const& taskId,
510 HdLegacyTaskFactorySharedPtr factory);
511
512 template <typename T>
513 static inline const TfToken & _GetTypeId();
514
515
516 // Private versions of equivalent public methods which insert and remove
517 // from this render index.
518 //
519 // The public versions check to see if scene delegate emulation is active.
520 // If not, they call through to these. Otherwise, they forward to the
521 // the HdLegacyPrimSceneIndex member. If a legacy render delegate is also
522 // in use, the scene index chain will terminate with a
523 // HdSceneIndexAdapterSceneDelegate. That will call the private versions
524 // directly so that the internal render index tables are updated.
525 //
526 // This prevents cyclic insertion/removals while allowing a single
527 // HdRenderIndex to be used for both front and back-end emulation.
528 //
529 // Note: all render index users should call the public APIs; only
530 // sceneIndexAdapterSceneDelegate.cpp should call these versions, to keep
531 // state synchronized. Note, for example, that _RemoveSubtree and _Clear
532 // don't affect the task map, since tasks aren't part of emulation, whereas
533 // RemoveSubtree and Clear do affect the task map...
535 void _InsertRprim(TfToken const& typeId,
536 HdSceneDelegate* sceneDelegate,
537 SdfPath const& rprimId);
538 void _InsertSprim(TfToken const& typeId,
539 HdSceneDelegate* delegate,
540 SdfPath const& sprimId);
541 void _InsertBprim(TfToken const& typeId,
542 HdSceneDelegate* delegate,
543 SdfPath const& bprimId);
544 void _InsertInstancer(HdSceneDelegate* delegate,
545 SdfPath const &id);
546 void _InsertTask(HdSceneDelegate* delegate,
547 SdfPath const &id,
548 HdTaskSharedPtr const &task);
549
550 void _RemoveRprim(SdfPath const& id);
551 void _RemoveSprim(TfToken const& typeId, SdfPath const& id);
552 void _RemoveBprim(TfToken const& typeId, SdfPath const& id);
553 void _RemoveInstancer(SdfPath const& id);
554 void _RemoveSubtree(SdfPath const& id, HdSceneDelegate* sceneDelegate);
555 void _RemoveRprimSubtree(const SdfPath &root,
556 HdSceneDelegate* sceneDelegate);
557 void _RemoveInstancerSubtree(const SdfPath &root,
558 HdSceneDelegate* sceneDelegate);
559 void _RemoveExtComputationSubtree(const SdfPath &root,
560 HdSceneDelegate* sceneDelegate);
561 void _RemoveTaskSubtree(const SdfPath &root,
562 HdSceneDelegate* sceneDelegate);
563 void _RemoveTask(SdfPath const &id);
564 void _Clear();
565
566 // ---------------------------------------------------------------------- //
567 // Index State
568 // ---------------------------------------------------------------------- //
569 struct _RprimInfo {
570 HdSceneDelegate *sceneDelegate;
571 HdRprim *rprim;
572 };
573
574 class _NoticeBatchingContext;
575
576 HdLegacyPrimSceneIndexRefPtr _emulationSceneIndex;
577 std::unique_ptr<_NoticeBatchingContext> _emulationBatchingCtx;
578
579 std::unique_ptr<class HdSceneIndexAdapterSceneDelegate> _siSd;
580
581 HdMergingSceneIndexRefPtr _mergingSceneIndex;
582 std::unique_ptr<_NoticeBatchingContext> _mergingBatchingCtx;
583
584 HdSceneIndexBaseRefPtr _terminalSceneIndex;
585
586 struct _TaskInfo {
587 HdSceneDelegate *sceneDelegate;
588 HdTaskSharedPtr task;
589 };
590
591 typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
592 typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
593 typedef std::vector<SdfPath> _RprimPrimIDVector;
594
595 typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
596 typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
597
598 _RprimMap _rprimMap;
599 Hd_SortedIds _rprimIds;
600
601 _RprimPrimIDVector _rprimPrimIdMap;
602
603 _TaskMap _taskMap;
604
605 _SprimIndex _sprimIndex;
606 _BprimIndex _bprimIndex;
607
608 HdChangeTracker _tracker;
609
610 typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
611 _InstancerMap _instancerMap;
612
613 HdRenderDelegate *_renderDelegate;
614 HdDriverVector _drivers;
615
616 HdSceneIndexBaseRefPtr _finalEmulationSceneIndex;
617
618 std::string _instanceName;
619
620 // ---------------------------------------------------------------------- //
621 // Sync State
622 // ---------------------------------------------------------------------- //
623 HdRprimCollectionVector _collectionsToSync;
624 HdDirtyList _rprimDirtyList;
625
626 // ---------------------------------------------------------------------- //
627
629 void _InitPrimTypes();
630
632 bool _CreateFallbackPrims();
633
635 void _DestroyFallbackPrims();
636
637 typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
638 _ConcurrentDrawItems;
639
640 void _AppendDrawItems(const SdfPathVector &rprimIds,
641 size_t begin,
642 size_t end,
643 HdRprimCollection const& collection,
644 _ConcurrentDrawItems* result);
645
649 static void _ConfigureReprs();
650
651 // Remove default constructor
652 HdRenderIndex() = delete;
653
654 // Don't allow copies
655 HdRenderIndex(const HdRenderIndex &) = delete;
656 HdRenderIndex &operator=(const HdRenderIndex &) = delete;
657
658};
659
660template <typename T>
661void
663{
664 _InsertSceneDelegateTask(
665 delegate, id, HdMakeLegacyTaskFactory<T>());
666}
667
668PXR_NAMESPACE_CLOSE_SCOPE
669
670#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:662
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:235
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:269
static HdRenderIndex * New(HdRenderDelegate *renderDelegate)
Create a render index for "front-end" emulation.
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:327
bool HasInstancer(SdfPath const &id)
Returns true if instancer id exists in index.
Definition: renderIndex.h:291
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: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