All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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/noticeBatchingSceneIndex.h"
25
26#include "pxr/imaging/hf/perfLog.h"
27
28#include "pxr/usd/sdf/path.h"
29
30#include "pxr/base/gf/vec4i.h"
31#include "pxr/base/tf/hashmap.h"
32
33#include <tbb/enumerable_thread_specific.h>
34
35#include <vector>
36#include <unordered_map>
37#include <memory>
38
39PXR_NAMESPACE_OPEN_SCOPE
40
41class HdRprim;
42class HdSprim;
43class HdBprim;
44class HdDrawItem;
46class HdSceneDelegate;
47class HdRenderDelegate;
49class VtValue;
50class HdInstancer;
51class HdDriver;
52
53using HdDriverVector = std::vector<HdDriver*>;
54using HdRprimCollectionVector = std::vector<HdRprimCollection>;
55using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
56using HdResourceRegistrySharedPtr = std::shared_ptr<class HdResourceRegistry>;
57using HdTaskSharedPtrVector = std::vector<HdTaskSharedPtr>;
58using HdTaskContext = std::unordered_map<TfToken,
59 VtValue,
61
103class HdRenderIndex final
104{
105public:
106 typedef std::vector<HdDrawItem const*> HdDrawItemPtrVector;
107
113 // hgi = Hgi::CreatePlatformDefaultHgi()
114 // hgiDriver = new HdDriver<Hgi*>(HgiTokens→renderDriver, hgi)
115 // HdRenderIndex::New(_renderDelegate, {_hgiDriver})
116 //
120 HD_API
122 HdRenderDelegate *renderDelegate,
123 HdDriverVector const& drivers,
124 const std::string &instanceName=std::string());
125
126 HD_API
128
130 HD_API
131 void Clear();
132
137 HD_API
138 void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
139
140 // ---------------------------------------------------------------------- //
143 HD_API
145
146 // ---------------------------------------------------------------------- //
148 // ---------------------------------------------------------------------- //
149
153 HD_API
155
161 HD_API
162 void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
163
164 // ---------------------------------------------------------------------- //
166 // ---------------------------------------------------------------------- //
167
169 // by renderTags and collection.
172 HD_API
173 HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
174 TfTokenVector const& renderTags);
175
176 // ---------------------------------------------------------------------- //
178 // ---------------------------------------------------------------------- //
179
180 HdChangeTracker& GetChangeTracker() { return _tracker; }
181 HdChangeTracker const& GetChangeTracker() const { return _tracker; }
182
183 // ---------------------------------------------------------------------- //
185 // ---------------------------------------------------------------------- //
186
188 HD_API
189 bool IsRprimTypeSupported(TfToken const& typeId) const;
190
192 HD_API
193 void InsertRprim(TfToken const& typeId,
194 HdSceneDelegate* sceneDelegate,
195 SdfPath const& rprimId);
196
198 HD_API
199 void RemoveRprim(SdfPath const& id);
200
202 bool HasRprim(SdfPath const& id) {
203 return _rprimMap.find(id) != _rprimMap.end();
204 }
205
207 HD_API
208 HdRprim const *GetRprim(SdfPath const &id) const;
209
211 HD_API
213
216 HD_API
218 SdfPath* sceneDelegateId,
219 SdfPath* instancerId) const;
220
222 HD_API
223 TfToken GetRenderTag(SdfPath const& id) const;
224
227 HdDirtyBits bits);
228
231 HD_API
232 const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
233
234
236 HD_API
237 SdfPathVector GetRprimSubtree(SdfPath const& root);
238
239
240 // ---------------------------------------------------------------------- //
242 // ---------------------------------------------------------------------- //
243
245 HD_API
247 SdfPath const &id);
248
250 HD_API
251 void RemoveInstancer(SdfPath const& id);
252
254 bool HasInstancer(SdfPath const& id) {
255 return _instancerMap.find(id) != _instancerMap.end();
256 }
257
259 HD_API
261
262 // ---------------------------------------------------------------------- //
264 // ---------------------------------------------------------------------- //
265
267 template <typename T>
268 void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
269
271 HD_API
272 void RemoveTask(SdfPath const& id);
273
275 bool HasTask(SdfPath const& id) {
276 return _taskMap.find(id) != _taskMap.end();
277 }
278
280 HD_API
281 HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
282
283 // ---------------------------------------------------------------------- //
285 // ---------------------------------------------------------------------- //
286
288 HD_API
289 bool IsSprimTypeSupported(TfToken const& typeId) const;
290
292 HD_API
293 void InsertSprim(TfToken const& typeId,
294 HdSceneDelegate* delegate,
295 SdfPath const& sprimId);
296
297 HD_API
298 void RemoveSprim(TfToken const& typeId, SdfPath const &id);
299
300 HD_API
301 HdSprim *GetSprim(TfToken const& typeId, SdfPath const &id) const;
302
305 HD_API
306 SdfPathVector GetSprimSubtree(TfToken const& typeId,
307 SdfPath const& root);
308
310 HD_API
311 HdSprim *GetFallbackSprim(TfToken const& typeId) const;
312
313
314 // ---------------------------------------------------------------------- //
316 // ---------------------------------------------------------------------- //
317
319 HD_API
320 bool IsBprimTypeSupported(TfToken const& typeId) const;
321
323 HD_API
324 void InsertBprim(TfToken const& typeId,
325 HdSceneDelegate* delegate,
326 SdfPath const& bprimId);
327
328 HD_API
329 void RemoveBprim(TfToken const& typeId, SdfPath const &id);
330
331 HD_API
332 HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
333
336 HD_API
337 SdfPathVector GetBprimSubtree(TfToken const& typeId,
338 SdfPath const& root);
339
341 HD_API
342 HdBprim *GetFallbackBprim(TfToken const& typeId) const;
343
344 // ---------------------------------------------------------------------- //
346 // ---------------------------------------------------------------------- //
347 HD_API
348 void InsertSceneIndex(
349 const HdSceneIndexBaseRefPtr &inputScene,
350 SdfPath const& scenePathPrefix,
351 bool needsPrefixing = true);
352
353 HD_API
354 void RemoveSceneIndex(
355 const HdSceneIndexBaseRefPtr &inputScene);
356
359 HD_API
360 HdSceneIndexBaseRefPtr GetTerminalSceneIndex() const;
361
362 // ---------------------------------------------------------------------- //
364 // ---------------------------------------------------------------------- //
368 HD_API
369 HdRenderDelegate *GetRenderDelegate() const;
370
371 // The render delegate may require access to a render context / device
372 // that is provided by the application.
373 HD_API
374 HdDriverVector const& GetDrivers() const;
375
378 HD_API
379 HdResourceRegistrySharedPtr GetResourceRegistry() const;
380
384 HD_API
386
394 HD_API
396
402 HD_API
404
410 HD_API
412
418 HD_API
420
421 HD_API
422 std::string GetInstanceName() const;
423
424private:
425 // The render index constructor is private so we can check
426 // renderDelegate before construction: see HdRenderIndex::New(...).
428 HdRenderDelegate *renderDelegate,
429 HdDriverVector const& drivers,
430 const std::string &instanceName=std::string());
431
432 // ---------------------------------------------------------------------- //
433 // Private Helper methods
434 // ---------------------------------------------------------------------- //
435
436 // Go through all RPrims and reallocate their instance ids
437 // This is called once we have exhausted all 24bit instance ids.
438 void _CompactPrimIds();
439
440 // Allocate the next available instance id to the prim
441 void _AllocatePrimId(HdRprim* prim);
442
443 using HdTaskCreateFnc =
444 std::function<HdTaskSharedPtr(HdSceneDelegate*, SdfPath const&)>;
445
446 // Inserts the task into the index and updates tracking state.
447 // _TrackDelegateTask is called by the inlined InsertTask<T>, so it needs
448 // to be marked HD_API.
449 HD_API
450 void _TrackDelegateTask(HdSceneDelegate* delegate,
451 SdfPath const& taskId,
452 HdTaskCreateFnc taskCreateFnc);
453
454 template <typename T>
455 static inline const TfToken & _GetTypeId();
456
457
458 // Private versions of equivalent public methods which insert and remove
459 // from this render index.
460 //
461 // The public versions check to see if scene delegate emulation is active.
462 // If not, they call through to these. Otherwise, they forward to the
463 // the HdLegacyPrimSceneIndex member. If a legacy render delegate is also
464 // in use, the scene index chain will terminate with a
465 // HdSceneIndexAdapterSceneDelegate. That will call the private versions
466 // directly so that the internal render index tables are updated.
467 //
468 // This prevents cyclic insertion/removals while allowing a single
469 // HdRenderIndex to be used for both front and back-end emulation.
470 //
471 // Note: all render index users should call the public APIs; only
472 // sceneIndexAdapterSceneDelegate.cpp should call these versions, to keep
473 // state synchronized. Note, for example, that _RemoveSubtree and _Clear
474 // don't affect the task map, since tasks aren't part of emulation, whereas
475 // RemoveSubtree and Clear do affect the task map...
477 void _InsertRprim(TfToken const& typeId,
478 HdSceneDelegate* sceneDelegate,
479 SdfPath const& rprimId);
480 void _InsertSprim(TfToken const& typeId,
481 HdSceneDelegate* delegate,
482 SdfPath const& sprimId);
483 void _InsertBprim(TfToken const& typeId,
484 HdSceneDelegate* delegate,
485 SdfPath const& bprimId);
486 void _InsertInstancer(HdSceneDelegate* delegate,
487 SdfPath const &id);
488
489 void _RemoveRprim(SdfPath const& id);
490 void _RemoveSprim(TfToken const& typeId, SdfPath const& id);
491 void _RemoveBprim(TfToken const& typeId, SdfPath const& id);
492 void _RemoveInstancer(SdfPath const& id);
493 void _RemoveSubtree(SdfPath const& id, HdSceneDelegate* sceneDelegate);
494 void _RemoveRprimSubtree(const SdfPath &root,
495 HdSceneDelegate* sceneDelegate);
496 void _RemoveInstancerSubtree(const SdfPath &root,
497 HdSceneDelegate* sceneDelegate);
498 void _RemoveExtComputationSubtree(const SdfPath &root,
499 HdSceneDelegate* sceneDelegate);
500 void _RemoveTaskSubtree(const SdfPath &root,
501 HdSceneDelegate* sceneDelegate);
502 void _Clear();
503
504 // ---------------------------------------------------------------------- //
505 // Index State
506 // ---------------------------------------------------------------------- //
507 struct _RprimInfo {
508 HdSceneDelegate *sceneDelegate;
509 HdRprim *rprim;
510 };
511
512 class _NoticeBatchingContext;
513
514 HdLegacyPrimSceneIndexRefPtr _emulationSceneIndex;
515 std::unique_ptr<_NoticeBatchingContext> _emulationBatchingCtx;
516
517 std::unique_ptr<class HdSceneIndexAdapterSceneDelegate> _siSd;
518
519 HdMergingSceneIndexRefPtr _mergingSceneIndex;
520 std::unique_ptr<_NoticeBatchingContext> _mergingBatchingCtx;
521
522 HdSceneIndexBaseRefPtr _terminalSceneIndex;
523
524 struct _TaskInfo {
525 HdSceneDelegate *sceneDelegate;
526 HdTaskSharedPtr task;
527 };
528
529 typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
530 typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
531 typedef std::vector<SdfPath> _RprimPrimIDVector;
532
533 typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
534 typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
535
536 _RprimMap _rprimMap;
537 Hd_SortedIds _rprimIds;
538
539 _RprimPrimIDVector _rprimPrimIdMap;
540
541 _TaskMap _taskMap;
542
543 _SprimIndex _sprimIndex;
544 _BprimIndex _bprimIndex;
545
546 HdChangeTracker _tracker;
547
548 typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
549 _InstancerMap _instancerMap;
550
551 HdRenderDelegate *_renderDelegate;
552 HdDriverVector _drivers;
553
554
555 std::string _instanceName;
556
557 // ---------------------------------------------------------------------- //
558 // Sync State
559 // ---------------------------------------------------------------------- //
560 HdRprimCollectionVector _collectionsToSync;
561 HdDirtyList _rprimDirtyList;
562
563 // ---------------------------------------------------------------------- //
564
566 void _InitPrimTypes();
567
569 bool _CreateFallbackPrims();
570
572 void _DestroyFallbackPrims();
573
574 typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
575 _ConcurrentDrawItems;
576
577 void _AppendDrawItems(const SdfPathVector &rprimIds,
578 size_t begin,
579 size_t end,
580 HdRprimCollection const& collection,
581 _ConcurrentDrawItems* result);
582
586 static void _ConfigureReprs();
587
588 // Remove default constructor
589 HdRenderIndex() = delete;
590
591 // Don't allow copies
592 HdRenderIndex(const HdRenderIndex &) = delete;
593 HdRenderIndex &operator=(const HdRenderIndex &) = delete;
594
595};
596
597template <typename T>
598void
600{
601 auto createTask = [](HdSceneDelegate* _delegate, SdfPath const& _id) -> HdTaskSharedPtr
602 {
603 return std::make_shared<T>(_delegate, _id);
604 };
605
606 _TrackDelegateTask(delegate, id, createTask);
607}
608
609PXR_NAMESPACE_CLOSE_SCOPE
610
611#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:104
HD_API void MergingSceneIndexNoticeBatchEnd()
Flushes any queued scene index observer notices from the merging scene index and disables further que...
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)
Removes the given task from the RenderIndex.
void InsertTask(HdSceneDelegate *delegate, SdfPath const &id)
Inserts a new task into the render index with an identifier of id.
Definition: renderIndex.h:599
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:202
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:232
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.
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)
Returns true if a task exists in the index with the given id.
Definition: renderIndex.h:275
bool HasInstancer(SdfPath const &id)
Returns true if instancer id exists in index.
Definition: renderIndex.h:254
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.
static HD_API HdRenderIndex * New(HdRenderDelegate *renderDelegate, HdDriverVector const &drivers, const std::string &instanceName=std::string())
Create a render index with the given render delegate.
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:147
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