Loading...
Searching...
No Matches
renderIndex.h
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10// names, trademarks, service marks, or product names of the Licensor
11// and its affiliates, except as required to comply with Section 4(c) of
12// the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16// http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24#ifndef PXR_IMAGING_HD_RENDER_INDEX_H
25#define PXR_IMAGING_HD_RENDER_INDEX_H
26
27#include "pxr/pxr.h"
28#include "pxr/imaging/hd/api.h"
29#include "pxr/imaging/hd/version.h"
30#include "pxr/imaging/hd/changeTracker.h"
31#include "pxr/imaging/hd/dirtyList.h"
32#include "pxr/imaging/hd/perfLog.h"
33#include "pxr/imaging/hd/primTypeIndex.h"
34#include "pxr/imaging/hd/resourceRegistry.h"
35#include "pxr/imaging/hd/sortedIds.h"
36#include "pxr/imaging/hd/tokens.h"
37
38#include "pxr/imaging/hd/sceneIndex.h"
39#include "pxr/imaging/hd/mergingSceneIndex.h"
40#include "pxr/imaging/hd/legacyPrimSceneIndex.h"
41#include "pxr/imaging/hd/noticeBatchingSceneIndex.h"
42
43#include "pxr/imaging/hf/perfLog.h"
44
45#include "pxr/usd/sdf/path.h"
46
47#include "pxr/base/gf/vec4i.h"
48#include "pxr/base/tf/hashmap.h"
49
50#include <tbb/enumerable_thread_specific.h>
51
52#include <vector>
53#include <unordered_map>
54#include <memory>
55
56PXR_NAMESPACE_OPEN_SCOPE
57
58class HdRprim;
59class HdSprim;
60class HdBprim;
61class HdDrawItem;
63class HdSceneDelegate;
64class HdRenderDelegate;
66class VtValue;
67class HdInstancer;
68class HdDriver;
69
70using HdDriverVector = std::vector<HdDriver*>;
71using HdRprimCollectionVector = std::vector<HdRprimCollection>;
72using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
73using HdResourceRegistrySharedPtr = std::shared_ptr<class HdResourceRegistry>;
74using HdTaskSharedPtrVector = std::vector<HdTaskSharedPtr>;
75using HdTaskContext = std::unordered_map<TfToken,
76 VtValue,
78
120class HdRenderIndex final
121{
122public:
123 typedef std::vector<HdDrawItem const*> HdDrawItemPtrVector;
124
130 // hgi = Hgi::CreatePlatformDefaultHgi()
131 // hgiDriver = new HdDriver<Hgi*>(HgiTokens→renderDriver, hgi)
132 // HdRenderIndex::New(_renderDelegate, {_hgiDriver})
133 //
137 HD_API
139 HdRenderDelegate *renderDelegate,
140 HdDriverVector const& drivers,
141 const std::string &instanceName=std::string());
142
143 HD_API
145
147 HD_API
148 void Clear();
149
154 HD_API
155 void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
156
157 // ---------------------------------------------------------------------- //
160 HD_API
162
163 // ---------------------------------------------------------------------- //
165 // ---------------------------------------------------------------------- //
166
170 HD_API
172
178 HD_API
179 void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
180
181 // ---------------------------------------------------------------------- //
183 // ---------------------------------------------------------------------- //
184
186 // by renderTags and collection.
189 HD_API
190 HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
191 TfTokenVector const& renderTags);
192
193 // ---------------------------------------------------------------------- //
195 // ---------------------------------------------------------------------- //
196
197 HdChangeTracker& GetChangeTracker() { return _tracker; }
198 HdChangeTracker const& GetChangeTracker() const { return _tracker; }
199
200 // ---------------------------------------------------------------------- //
202 // ---------------------------------------------------------------------- //
203
205 HD_API
206 bool IsRprimTypeSupported(TfToken const& typeId) const;
207
209 HD_API
210 void InsertRprim(TfToken const& typeId,
211 HdSceneDelegate* sceneDelegate,
212 SdfPath const& rprimId);
213
215 HD_API
216 void RemoveRprim(SdfPath const& id);
217
219 bool HasRprim(SdfPath const& id) {
220 return _rprimMap.find(id) != _rprimMap.end();
221 }
222
224 HD_API
225 HdRprim const *GetRprim(SdfPath const &id) const;
226
228 HD_API
230
233 HD_API
235 SdfPath* sceneDelegateId,
236 SdfPath* instancerId) const;
237
239 HD_API
240 TfToken GetRenderTag(SdfPath const& id) const;
241
244 HdDirtyBits bits);
245
248 HD_API
249 const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
250
251
253 HD_API
254 SdfPathVector GetRprimSubtree(SdfPath const& root);
255
256
257 // ---------------------------------------------------------------------- //
259 // ---------------------------------------------------------------------- //
260
262 HD_API
264 SdfPath const &id);
265
267 HD_API
268 void RemoveInstancer(SdfPath const& id);
269
271 bool HasInstancer(SdfPath const& id) {
272 return _instancerMap.find(id) != _instancerMap.end();
273 }
274
276 HD_API
278
279 // ---------------------------------------------------------------------- //
281 // ---------------------------------------------------------------------- //
282
284 template <typename T>
285 void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
286
288 HD_API
289 void RemoveTask(SdfPath const& id);
290
292 bool HasTask(SdfPath const& id) {
293 return _taskMap.find(id) != _taskMap.end();
294 }
295
297 HD_API
298 HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
299
300 // ---------------------------------------------------------------------- //
302 // ---------------------------------------------------------------------- //
303
305 HD_API
306 bool IsSprimTypeSupported(TfToken const& typeId) const;
307
309 HD_API
310 void InsertSprim(TfToken const& typeId,
311 HdSceneDelegate* delegate,
312 SdfPath const& sprimId);
313
314 HD_API
315 void RemoveSprim(TfToken const& typeId, SdfPath const &id);
316
317 HD_API
318 HdSprim *GetSprim(TfToken const& typeId, SdfPath const &id) const;
319
322 HD_API
323 SdfPathVector GetSprimSubtree(TfToken const& typeId,
324 SdfPath const& root);
325
327 HD_API
328 HdSprim *GetFallbackSprim(TfToken const& typeId) const;
329
330
331 // ---------------------------------------------------------------------- //
333 // ---------------------------------------------------------------------- //
334
336 HD_API
337 bool IsBprimTypeSupported(TfToken const& typeId) const;
338
340 HD_API
341 void InsertBprim(TfToken const& typeId,
342 HdSceneDelegate* delegate,
343 SdfPath const& bprimId);
344
345 HD_API
346 void RemoveBprim(TfToken const& typeId, SdfPath const &id);
347
348 HD_API
349 HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
350
353 HD_API
354 SdfPathVector GetBprimSubtree(TfToken const& typeId,
355 SdfPath const& root);
356
358 HD_API
359 HdBprim *GetFallbackBprim(TfToken const& typeId) const;
360
361 // ---------------------------------------------------------------------- //
363 // ---------------------------------------------------------------------- //
364 HD_API
365 void InsertSceneIndex(
366 const HdSceneIndexBaseRefPtr &inputScene,
367 SdfPath const& scenePathPrefix,
368 bool needsPrefixing = true);
369
370 HD_API
371 void RemoveSceneIndex(
372 const HdSceneIndexBaseRefPtr &inputScene);
373
376 HD_API
377 HdSceneIndexBaseRefPtr GetTerminalSceneIndex() const;
378
379 // ---------------------------------------------------------------------- //
381 // ---------------------------------------------------------------------- //
385 HD_API
386 HdRenderDelegate *GetRenderDelegate() const;
387
388 // The render delegate may require access to a render context / device
389 // that is provided by the application.
390 HD_API
391 HdDriverVector const& GetDrivers() const;
392
395 HD_API
396 HdResourceRegistrySharedPtr GetResourceRegistry() const;
397
401 HD_API
403
411 HD_API
413
419 HD_API
421
422 HD_API
423 std::string GetInstanceName() const;
424
425private:
426 // The render index constructor is private so we can check
427 // renderDelegate before construction: see HdRenderIndex::New(...).
429 HdRenderDelegate *renderDelegate,
430 HdDriverVector const& drivers,
431 const std::string &instanceName=std::string());
432
433 // ---------------------------------------------------------------------- //
434 // Private Helper methods
435 // ---------------------------------------------------------------------- //
436
437 // Go through all RPrims and reallocate their instance ids
438 // This is called once we have exhausted all 24bit instance ids.
439 void _CompactPrimIds();
440
441 // Allocate the next available instance id to the prim
442 void _AllocatePrimId(HdRprim* prim);
443
444 // Inserts the task into the index and updates tracking state.
445 // _TrackDelegateTask is called by the inlined InsertTask<T>, so it needs
446 // to be marked HD_API.
447 HD_API
448 void _TrackDelegateTask(HdSceneDelegate* delegate,
449 SdfPath const& taskId,
450 HdTaskSharedPtr const& task);
451
452 template <typename T>
453 static inline const TfToken & _GetTypeId();
454
455
456 // Private versions of equivalent public methods which insert and remove
457 // from this render index.
458 //
459 // The public versions check to see if scene delegate emulation is active.
460 // If not, they call through to these. Otherwise, they forward to the
461 // the HdLegacyPrimSceneIndex member. If a legacy render delegate is also
462 // in use, the scene index chain will terminate with a
463 // HdSceneIndexAdapterSceneDelegate. That will call the private versions
464 // directly so that the internal render index tables are updated.
465 //
466 // This prevents cyclic insertion/removals while allowing a single
467 // HdRenderIndex to be used for both front and back-end emulation.
468 //
469 // Note: all render index users should call the public APIs; only
470 // sceneIndexAdapterSceneDelegate.cpp should call these versions, to keep
471 // state synchronized. Note, for example, that _RemoveSubtree and _Clear
472 // don't affect the task map, since tasks aren't part of emulation, whereas
473 // RemoveSubtree and Clear do affect the task map...
475 void _InsertRprim(TfToken const& typeId,
476 HdSceneDelegate* sceneDelegate,
477 SdfPath const& rprimId);
478 void _InsertSprim(TfToken const& typeId,
479 HdSceneDelegate* delegate,
480 SdfPath const& sprimId);
481 void _InsertBprim(TfToken const& typeId,
482 HdSceneDelegate* delegate,
483 SdfPath const& bprimId);
484 void _InsertInstancer(HdSceneDelegate* delegate,
485 SdfPath const &id);
486
487 void _RemoveRprim(SdfPath const& id);
488 void _RemoveSprim(TfToken const& typeId, SdfPath const& id);
489 void _RemoveBprim(TfToken const& typeId, SdfPath const& id);
490 void _RemoveInstancer(SdfPath const& id);
491 void _RemoveSubtree(SdfPath const& id, HdSceneDelegate* sceneDelegate);
492 void _RemoveRprimSubtree(const SdfPath &root,
493 HdSceneDelegate* sceneDelegate);
494 void _RemoveInstancerSubtree(const SdfPath &root,
495 HdSceneDelegate* sceneDelegate);
496 void _RemoveExtComputationSubtree(const SdfPath &root,
497 HdSceneDelegate* sceneDelegate);
498 void _RemoveTaskSubtree(const SdfPath &root,
499 HdSceneDelegate* sceneDelegate);
500 void _Clear();
501
502 // ---------------------------------------------------------------------- //
503 // Index State
504 // ---------------------------------------------------------------------- //
505 struct _RprimInfo {
506 HdSceneDelegate *sceneDelegate;
507 HdRprim *rprim;
508 };
509
510 HdLegacyPrimSceneIndexRefPtr _emulationSceneIndex;
511 HdNoticeBatchingSceneIndexRefPtr _emulationNoticeBatchingSceneIndex;
512 unsigned int _noticeBatchingDepth;
513
514 std::unique_ptr<class HdSceneIndexAdapterSceneDelegate> _siSd;
515
516 HdMergingSceneIndexRefPtr _mergingSceneIndex;
517 HdSceneIndexBaseRefPtr _terminalSceneIndex;
518
519 struct _TaskInfo {
520 HdSceneDelegate *sceneDelegate;
521 HdTaskSharedPtr task;
522 };
523
524 typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
525 typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
526 typedef std::vector<SdfPath> _RprimPrimIDVector;
527
528 typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
529 typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
530
531 _RprimMap _rprimMap;
532 Hd_SortedIds _rprimIds;
533
534 _RprimPrimIDVector _rprimPrimIdMap;
535
536 _TaskMap _taskMap;
537
538 _SprimIndex _sprimIndex;
539 _BprimIndex _bprimIndex;
540
541 HdChangeTracker _tracker;
542
543 typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
544 _InstancerMap _instancerMap;
545
546 HdRenderDelegate *_renderDelegate;
547 HdDriverVector _drivers;
548
549
550 std::string _instanceName;
551
552 // ---------------------------------------------------------------------- //
553 // Sync State
554 // ---------------------------------------------------------------------- //
555 HdRprimCollectionVector _collectionsToSync;
556 HdDirtyList _rprimDirtyList;
557
558 // ---------------------------------------------------------------------- //
559
561 void _InitPrimTypes();
562
564 bool _CreateFallbackPrims();
565
567 void _DestroyFallbackPrims();
568
569 typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
570 _ConcurrentDrawItems;
571
572 void _AppendDrawItems(const SdfPathVector &rprimIds,
573 size_t begin,
574 size_t end,
575 HdRprimCollection const& collection,
576 _ConcurrentDrawItems* result);
577
581 static void _ConfigureReprs();
582
583 // Remove default constructor
584 HdRenderIndex() = delete;
585
586 // Don't allow copies
587 HdRenderIndex(const HdRenderIndex &) = delete;
588 HdRenderIndex &operator=(const HdRenderIndex &) = delete;
589
590};
591
592template <typename T>
593void
595{
596 HD_TRACE_FUNCTION();
597 HF_MALLOC_TAG_FUNCTION();
598
599 HdTaskSharedPtr task = std::make_shared<T>(delegate, id);
600 _TrackDelegateTask(delegate, id, task);
601}
602
603PXR_NAMESPACE_CLOSE_SCOPE
604
605#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:57
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine.
Definition: changeTracker.h:52
Used for faster iteration of dirty Rprims by the render index.
Definition: dirtyList.h:103
A draw item is a light-weight representation of an HdRprim's resources and material to be used for re...
Definition: drawItem.h:65
HdDriver represents a device object, commonly a render device, that is owned by the application and p...
Definition: driver.h:40
Hydra Representation of a Client defined computation.
This class exists to facilitate point cloud style instancing.
Definition: instancer.h:125
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:121
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:594
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:219
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:249
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:292
bool HasInstancer(SdfPath const &id)
Returns true if instancer id exists in index.
Definition: renderIndex.h:271
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:55
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:52
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:164
Functor to use for hash maps from tokens to other things.
Definition: token.h:166
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457