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