All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/perfLog.h"
32 #include "pxr/imaging/hd/primTypeIndex.h"
33 #include "pxr/imaging/hd/resourceRegistry.h"
34 #include "pxr/imaging/hd/sortedIds.h"
35 #include "pxr/imaging/hd/textureResource.h"
36 #include "pxr/imaging/hd/tokens.h"
37 
38 #include "pxr/imaging/hf/perfLog.h"
39 
40 #include "pxr/usd/sdf/path.h"
41 
42 #include "pxr/base/gf/vec4i.h"
43 #include "pxr/base/tf/hashmap.h"
44 
45 #include <boost/noncopyable.hpp>
46 #include <boost/shared_ptr.hpp>
47 #include <boost/smart_ptr.hpp>
48 
49 #include <tbb/enumerable_thread_specific.h>
50 
51 #include <vector>
52 #include <unordered_map>
53 #include <memory>
54 
55 PXR_NAMESPACE_OPEN_SCOPE
56 
57 class HdRprim;
58 class HdSprim;
59 class HdBprim;
60 class HdDrawItem;
61 class HdRprimCollection;
62 class HdSceneDelegate;
63 class HdRenderDelegate;
64 class HdExtComputation;
65 class VtValue;
66 class HdInstancer;
67 
68 typedef boost::shared_ptr<class HdDirtyList> HdDirtyListSharedPtr;
69 typedef boost::shared_ptr<class HdTask> HdTaskSharedPtr;
70 typedef boost::shared_ptr<class HdResourceRegistry> HdResourceRegistrySharedPtr;
71 typedef std::vector<HdTaskSharedPtr> HdTaskSharedPtrVector;
72 typedef std::unordered_map<TfToken,
73  VtValue,
74  TfToken::HashFunctor> HdTaskContext;
75 
117 class HdRenderIndex final : public boost::noncopyable {
118 public:
119  typedef std::vector<HdDrawItem const*> HdDrawItemPtrVector;
120 
123  HD_API
124  static HdRenderIndex* New(HdRenderDelegate *renderDelegate);
125 
126  HD_API
127  ~HdRenderIndex();
128 
130  HD_API
131  void Clear();
132 
137  HD_API
138  void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
139 
140  // ---------------------------------------------------------------------- //
143  HD_API
144  SdfPath GetRprimPathFromPrimId(int primId) const;
145 
146  // ---------------------------------------------------------------------- //
148  // ---------------------------------------------------------------------- //
149 
156  HD_API
157  void EnqueuePrimsToSync(
158  HdDirtyListSharedPtr const &dirtyList,
159  HdRprimCollection const &collection);
160 
166  HD_API
167  void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
168 
169  // ---------------------------------------------------------------------- //
171  // ---------------------------------------------------------------------- //
172 
174  // by renderTags and collection.
177  HD_API
178  HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
179  TfTokenVector const& renderTags);
180 
181  // ---------------------------------------------------------------------- //
183  // ---------------------------------------------------------------------- //
184 
185  HdChangeTracker& GetChangeTracker() { return _tracker; }
186  HdChangeTracker const& GetChangeTracker() const { return _tracker; }
187 
188  // ---------------------------------------------------------------------- //
190  // ---------------------------------------------------------------------- //
191 
193  HD_API
194  bool IsRprimTypeSupported(TfToken const& typeId) const;
195 
197  HD_API
198  void InsertRprim(TfToken const& typeId,
199  HdSceneDelegate* sceneDelegate,
200  SdfPath const& rprimId,
201  SdfPath const& instancerId = SdfPath());
202 
204  HD_API
205  void RemoveRprim(SdfPath const& id);
206 
208  bool HasRprim(SdfPath const& id) {
209  return _rprimMap.find(id) != _rprimMap.end();
210  }
211 
213  HD_API
214  HdRprim const *GetRprim(SdfPath const &id) const;
215 
217  HD_API
219 
222  HD_API
224  SdfPath* sceneDelegateId,
225  SdfPath* instancerId) const;
226 
228  HD_API
229  TfToken GetRenderTag(SdfPath const& id) const;
230 
232  HD_API
233  TfToken GetMaterialTag(SdfPath const& id) const;
234 
237  HD_API
238  const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
239 
240 
242  HD_API
243  SdfPathVector GetRprimSubtree(SdfPath const& root);
244 
245 
246  // ---------------------------------------------------------------------- //
248  // ---------------------------------------------------------------------- //
249 
251  HD_API
252  void InsertInstancer(HdSceneDelegate* delegate,
253  SdfPath const &id,
254  SdfPath const &parentId = SdfPath());
255 
257  HD_API
258  void RemoveInstancer(SdfPath const& id);
259 
261  bool HasInstancer(SdfPath const& id) {
262  return _instancerMap.find(id) != _instancerMap.end();
263  }
264 
266  HD_API
267  HdInstancer *GetInstancer(SdfPath const &id) const;
268 
269  // ---------------------------------------------------------------------- //
271  // ---------------------------------------------------------------------- //
272 
274  template <typename T>
275  void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
276 
278  HD_API
279  void RemoveTask(SdfPath const& id);
280 
282  bool HasTask(SdfPath const& id) {
283  return _taskMap.find(id) != _taskMap.end();
284  }
285 
287  HD_API
288  HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
289 
290  // ---------------------------------------------------------------------- //
292  // ---------------------------------------------------------------------- //
293 
295  HD_API
296  bool IsSprimTypeSupported(TfToken const& typeId) const;
297 
299  HD_API
300  void InsertSprim(TfToken const& typeId,
301  HdSceneDelegate* delegate,
302  SdfPath const& sprimId);
303 
304  HD_API
305  void RemoveSprim(TfToken const& typeId, SdfPath const &id);
306 
307  HD_API
308  HdSprim *GetSprim(TfToken const& typeId, SdfPath const &id) const;
309 
312  HD_API
313  SdfPathVector GetSprimSubtree(TfToken const& typeId,
314  SdfPath const& root);
315 
317  HD_API
318  HdSprim *GetFallbackSprim(TfToken const& typeId) const;
319 
320 
321  // ---------------------------------------------------------------------- //
323  // ---------------------------------------------------------------------- //
324 
326  HD_API
327  bool IsBprimTypeSupported(TfToken const& typeId) const;
328 
330  HD_API
331  void InsertBprim(TfToken const& typeId,
332  HdSceneDelegate* delegate,
333  SdfPath const& bprimId);
334 
335  HD_API
336  void RemoveBprim(TfToken const& typeId, SdfPath const &id);
337 
338  HD_API
339  HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
340 
343  HD_API
344  SdfPathVector GetBprimSubtree(TfToken const& typeId,
345  SdfPath const& root);
346 
348  HD_API
349  HdBprim *GetFallbackBprim(TfToken const& typeId) const;
350 
353  HD_API
355  GetTextureKey(HdTextureResource::ID id) const;
356 
357 
358  // ---------------------------------------------------------------------- //
360  // ---------------------------------------------------------------------- //
364  HD_API
365  HdRenderDelegate *GetRenderDelegate() const;
366 
369  HD_API
370  HdResourceRegistrySharedPtr GetResourceRegistry() const;
371 
372 private:
373  // The render index constructor is private so we can check
374  // renderDelegate before construction: see HdRenderIndex::New(...).
375  HdRenderIndex(HdRenderDelegate *renderDelegate);
376 
377  // ---------------------------------------------------------------------- //
378  // Private Helper methods
379  // ---------------------------------------------------------------------- //
380 
381  // Go through all RPrims and reallocate their instance ids
382  // This is called once we have exhausted all all 24bit instance ids.
383  void _CompactPrimIds();
384 
385  // Allocate the next available instance id to the prim
386  void _AllocatePrimId(HdRprim* prim);
387 
388  // Inserts the task into the index and updates tracking state.
389  // _TrackDelegateTask is called by the inlined InsertTask<T>, so it needs
390  // to be marked HD_API.
391  HD_API
392  void _TrackDelegateTask(HdSceneDelegate* delegate,
393  SdfPath const& taskId,
394  HdTaskSharedPtr const& task);
395 
396  template <typename T>
397  static inline const TfToken & _GetTypeId();
398 
399  void _RemoveRprimSubtree(const SdfPath &root,
400  HdSceneDelegate* sceneDelegate);
401  void _RemoveInstancerSubtree(const SdfPath &root,
402  HdSceneDelegate* sceneDelegate);
403  void _RemoveExtComputationSubtree(const SdfPath &root,
404  HdSceneDelegate* sceneDelegate);
405  void _RemoveTaskSubtree(const SdfPath &root,
406  HdSceneDelegate* sceneDelegate);
407 
408  // ---------------------------------------------------------------------- //
409  // Index State
410  // ---------------------------------------------------------------------- //
411  struct _RprimInfo {
412  HdSceneDelegate *sceneDelegate;
413  HdRprim *rprim;
414  };
415 
416  struct _TaskInfo {
417  HdSceneDelegate *sceneDelegate;
418  HdTaskSharedPtr task;
419  };
420 
421  typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
422  typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
423  typedef std::vector<SdfPath> _RprimPrimIDVector;
424 
425  typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
426  typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
427 
428  _RprimMap _rprimMap;
429  Hd_SortedIds _rprimIds;
430 
431  _RprimPrimIDVector _rprimPrimIdMap;
432 
433  _TaskMap _taskMap;
434 
435  _SprimIndex _sprimIndex;
436  _BprimIndex _bprimIndex;
437 
438  HdChangeTracker _tracker;
439 
440  typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
441  _InstancerMap _instancerMap;
442 
443  struct _SyncQueueEntry {
444  HdDirtyListSharedPtr dirtyList;
445  HdRprimCollection collection;
446 
447  };
448  typedef std::vector<_SyncQueueEntry> _SyncQueue;
449  _SyncQueue _syncQueue;
450 
451  HdRenderDelegate *_renderDelegate;
452 
453  // ---------------------------------------------------------------------- //
454  // Sync State
455  // ---------------------------------------------------------------------- //
456  TfTokenVector _activeRenderTags;
457  unsigned int _renderTagVersion;
458 
460  void _InitPrimTypes();
461 
463  bool _CreateFallbackPrims();
464 
466  void _DestroyFallbackPrims();
467 
468  void _GatherRenderTags(const HdTaskSharedPtrVector *tasks);
469 
470  typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
471  _ConcurrentDrawItems;
472 
473  void _AppendDrawItems(const SdfPathVector &rprimIds,
474  size_t begin,
475  size_t end,
476  HdRprimCollection const& collection,
477  _ConcurrentDrawItems* result);
478 
482  static void _ConfigureReprs();
483 
484  // Remove default constructor
485  HdRenderIndex() = delete;
486 };
487 
488 template <typename T>
489 void
491 {
492  HD_TRACE_FUNCTION();
493  HF_MALLOC_TAG_FUNCTION();
494 
495  HdTaskSharedPtr task = boost::make_shared<T>(delegate, id);
496  _TrackDelegateTask(delegate, id, task);
497 }
498 
499 PXR_NAMESPACE_CLOSE_SCOPE
500 
501 #endif //PXR_IMAGING_HD_RENDER_INDEX_H
Hydra Representation of a Client defined computation.
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:117
HD_API bool IsSprimTypeSupported(TfToken const &typeId) const
Returns whether the sprim type is supported by this render index.
HD_API bool IsRprimTypeSupported(TfToken const &typeId) const
Returns whether the rprim type is supported by this render index.
HD_API void InsertBprim(TfToken const &typeId, HdSceneDelegate *delegate, SdfPath const &bprimId)
Insert a bprim into index.
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine...
Definition: changeTracker.h:53
HD_API HdSceneDelegate * GetSceneDelegateForRprim(SdfPath const &id) const
Returns the scene delegate for the given rprim.
HD_API HdInstancer * GetInstancer(SdfPath const &id) const
Returns the instancer of id.
This class exists to facilitate point cloud style instancing.
Definition: instancer.h:119
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...
Functor to use for hash maps from tokens to other things.
Definition: token.h:168
HD_API HdResourceRegistrySharedPtr GetResourceRegistry() const
Returns a shared ptr to the resource registry of the current render delegate.
HD_API TfToken GetRenderTag(SdfPath const &id) const
Returns the render tag for the given rprim.
HD_API void InsertSprim(TfToken const &typeId, HdSceneDelegate *delegate, SdfPath const &sprimId)
Insert a sprim into index.
HD_API const SdfPathVector & GetRprimIds()
Returns a sorted list of all Rprims in the render index.
Definition: renderIndex.h:238
HD_API void RemoveRprim(SdfPath const &id)
Remove a rprim from 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 void InsertRprim(TfToken const &typeId, HdSceneDelegate *sceneDelegate, SdfPath const &rprimId, SdfPath const &instancerId=SdfPath())
Insert a rprim into index.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
The render engine state for a given rprim from the scene graph.
Definition: rprim.h:67
HD_API void Clear()
Clear all r (render), s (state) and b (buffer) prims.
HD_API void RemoveTask(SdfPath const &id)
Removes the given task from the RenderIndex.
HD_API void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext)
Syncs input tasks, B &amp; S prims, (external) computations and processes all pending dirty lists (which ...
Adapter class providing data exchange with the client scene graph.
bool HasInstancer(SdfPath const &id)
Returns true if instancer id exists in index.
Definition: renderIndex.h:261
static HD_API HdRenderIndex * New(HdRenderDelegate *renderDelegate)
Create a render index with the given render delegate.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:433
bool HasTask(SdfPath const &id)
Returns true if a task exists in the index with the given id.
Definition: renderIndex.h:282
A named, semantic collection of objects.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
HD_API SdfPathVector GetRprimSubtree(SdfPath const &root)
Returns the subtree rooted under the given path.
bool HasRprim(SdfPath const &id)
Returns true if rprim id exists in index.
Definition: renderIndex.h:208
HD_API SdfPathVector GetBprimSubtree(TfToken const &typeId, SdfPath const &root)
Returns the subtree rooted under the given path for the given bprim type.
Sprim (state prim) is a base class of managing state for non-drawable scene entity (e...
Definition: sprim.h:54
HD_API HdTaskSharedPtr const & GetTask(SdfPath const &id) const
Returns the task for the given id.
HD_API void EnqueuePrimsToSync(HdDirtyListSharedPtr const &dirtyList, HdRprimCollection const &collection)
Adds the dirty list to the sync queue.
HD_API HdResourceRegistry::TextureKey GetTextureKey(HdTextureResource::ID id) const
Helper utility to convert texture resource id&#39;s which are unique to this render index, into a globally unique texture key.
A draw item is a light-weight representation of an HdRprim&#39;s resources and material to be used for re...
Definition: drawItem.h:66
HD_API HdBprim * GetFallbackBprim(TfToken const &typeId) const
Returns the fallback prim for the Bprim of the given type.
HD_API bool GetSceneDelegateAndInstancerIds(SdfPath const &id, SdfPath *sceneDelegateId, SdfPath *instancerId) const
Query function to return the id&#39;s of the scene delegate and instancer associated with the Rprim at th...
HD_API HdDrawItemPtrVector GetDrawItems(HdRprimCollection const &collection, TfTokenVector const &renderTags)
Returns a list of relevant draw items that match the criteria specified.
HD_API HdSprim * GetFallbackSprim(TfToken const &typeId) const
Returns the fullback prim for the Sprim of the given type.
HD_API bool IsBprimTypeSupported(TfToken const &typeId) const
Returns whether the bprim type is supported by this render index.
Bprim (buffer prim) is a base class of managing a blob of data that is used to communicate between th...
Definition: bprim.h:56
size_t TextureKey
Globally unique id for texture, see HdRenderIndex::GetTextureKey() for details.
void InsertTask(HdSceneDelegate *delegate, SdfPath const &id)
Inserts a new task into the render index with an identifier of id.
Definition: renderIndex.h:490
HD_API void InsertInstancer(HdSceneDelegate *delegate, SdfPath const &id, SdfPath const &parentId=SdfPath())
Insert an instancer into index.
HD_API void RemoveInstancer(SdfPath const &id)
Remove an instancer from index.
HD_API TfToken GetMaterialTag(SdfPath const &id) const
Return the material tag for the given rprim.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
HD_API HdRprim const * GetRprim(SdfPath const &id) const
Returns the rprim of id.
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 HdRenderDelegate * GetRenderDelegate() const
Currently, a render index only supports connection to one type of render delegate, due to the inserted information and change tracking being specific to that delegate type.