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 HD_RENDER_INDEX_H
25 #define 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 
58 class HdRprim;
59 class HdSprim;
60 class HdBprim;
61 class HdDrawItem;
62 class HdRprimCollection;
63 class HdSceneDelegate;
64 class HdRenderDelegate;
65 class HdExtComputation;
66 class VtValue;
67 class HdInstancer;
68 
69 
70 typedef boost::shared_ptr<class HdDirtyList> HdDirtyListSharedPtr;
71 typedef boost::shared_ptr<class HdTask> HdTaskSharedPtr;
72 typedef boost::shared_ptr<class HdResourceRegistry> HdResourceRegistrySharedPtr;
73 typedef std::vector<HdTaskSharedPtr> HdTaskSharedPtrVector;
74 typedef std::unordered_map<TfToken,
75  VtValue,
76  TfToken::HashFunctor> HdTaskContext;
77 
119 class HdRenderIndex final : public boost::noncopyable {
120 public:
121  typedef std::vector<HdDrawItem const*> HdDrawItemPtrVector;
122 
125  HD_API
126  static HdRenderIndex* New(HdRenderDelegate *renderDelegate);
127 
128  HD_API
129  ~HdRenderIndex();
130 
132  HD_API
133  void Clear();
134 
139  HD_API
140  void RemoveSubtree(const SdfPath &root, HdSceneDelegate* sceneDelegate);
141 
142  // ---------------------------------------------------------------------- //
145  HD_API
146  SdfPath GetRprimPathFromPrimId(int primId) const;
147 
148  // ---------------------------------------------------------------------- //
150  // ---------------------------------------------------------------------- //
151 
158  HD_API
159  void Sync(HdDirtyListSharedPtr const &dirtyList,
160  HdRprimCollection const &collection);
161 
167  HD_API
168  void SyncAll(HdTaskSharedPtrVector *tasks, HdTaskContext *taskContext);
169 
170  // ---------------------------------------------------------------------- //
172  // ---------------------------------------------------------------------- //
173 
175  // by renderTags and collection.
178  HD_API
179  HdDrawItemPtrVector GetDrawItems(HdRprimCollection const& collection,
180  TfTokenVector const& renderTags);
181 
182  // ---------------------------------------------------------------------- //
184  // ---------------------------------------------------------------------- //
185 
186  HdChangeTracker& GetChangeTracker() { return _tracker; }
187  HdChangeTracker const& GetChangeTracker() const { return _tracker; }
188 
189  // ---------------------------------------------------------------------- //
191  // ---------------------------------------------------------------------- //
192 
194  HD_API
195  bool IsRprimTypeSupported(TfToken const& typeId) const;
196 
198  HD_API
199  void InsertRprim(TfToken const& typeId,
200  HdSceneDelegate* sceneDelegate,
201  SdfPath const& rprimId,
202  SdfPath const& instancerId = SdfPath());
203 
205  HD_API
206  void RemoveRprim(SdfPath const& id);
207 
209  bool HasRprim(SdfPath const& id) {
210  return _rprimMap.find(id) != _rprimMap.end();
211  }
212 
214  HD_API
215  HdRprim const *GetRprim(SdfPath const &id) const;
216 
218  HD_API
220 
223  HD_API
225  SdfPath* sceneDelegateId,
226  SdfPath* instancerId) const;
227 
229  HD_API
230  TfToken GetRenderTag(SdfPath const& id) const;
231 
233  HD_API
234  TfToken GetMaterialTag(SdfPath const& id) const;
235 
238  HD_API
239  const SdfPathVector &GetRprimIds() { return _rprimIds.GetIds(); }
240 
241 
243  HD_API
244  SdfPathVector GetRprimSubtree(SdfPath const& root);
245 
246 
247  // ---------------------------------------------------------------------- //
249  // ---------------------------------------------------------------------- //
250 
252  HD_API
253  void InsertInstancer(HdSceneDelegate* delegate,
254  SdfPath const &id,
255  SdfPath const &parentId = SdfPath());
256 
258  HD_API
259  void RemoveInstancer(SdfPath const& id);
260 
262  bool HasInstancer(SdfPath const& id) {
263  return _instancerMap.find(id) != _instancerMap.end();
264  }
265 
267  HD_API
268  HdInstancer *GetInstancer(SdfPath const &id) const;
269 
270  // ---------------------------------------------------------------------- //
272  // ---------------------------------------------------------------------- //
273 
275  template <typename T>
276  void InsertTask(HdSceneDelegate* delegate, SdfPath const& id);
277 
279  HD_API
280  void RemoveTask(SdfPath const& id);
281 
283  bool HasTask(SdfPath const& id) {
284  return _taskMap.find(id) != _taskMap.end();
285  }
286 
288  HD_API
289  HdTaskSharedPtr const& GetTask(SdfPath const& id) const;
290 
291  // ---------------------------------------------------------------------- //
293  // ---------------------------------------------------------------------- //
294 
296  HD_API
297  bool IsSprimTypeSupported(TfToken const& typeId) const;
298 
300  HD_API
301  void InsertSprim(TfToken const& typeId,
302  HdSceneDelegate* delegate,
303  SdfPath const& sprimId);
304 
305  HD_API
306  void RemoveSprim(TfToken const& typeId, SdfPath const &id);
307 
308  HD_API
309  HdSprim const *GetSprim(TfToken const& typeId, SdfPath const &id) const;
310 
313  HD_API
314  SdfPathVector GetSprimSubtree(TfToken const& typeId,
315  SdfPath const& root);
316 
318  HD_API
319  HdSprim *GetFallbackSprim(TfToken const& typeId) const;
320 
321 
322  // ---------------------------------------------------------------------- //
324  // ---------------------------------------------------------------------- //
325 
327  HD_API
328  bool IsBprimTypeSupported(TfToken const& typeId) const;
329 
331  HD_API
332  void InsertBprim(TfToken const& typeId,
333  HdSceneDelegate* delegate,
334  SdfPath const& bprimId);
335 
336  HD_API
337  void RemoveBprim(TfToken const& typeId, SdfPath const &id);
338 
339  HD_API
340  HdBprim *GetBprim(TfToken const& typeId, SdfPath const &id) const;
341 
344  HD_API
345  SdfPathVector GetBprimSubtree(TfToken const& typeId,
346  SdfPath const& root);
347 
349  HD_API
350  HdBprim *GetFallbackBprim(TfToken const& typeId) const;
351 
354  HD_API
356  GetTextureKey(HdTextureResource::ID id) const;
357 
358 
359  // ---------------------------------------------------------------------- //
361  // ---------------------------------------------------------------------- //
365  HD_API
366  HdRenderDelegate *GetRenderDelegate() const;
367 
370  HD_API
371  HdResourceRegistrySharedPtr GetResourceRegistry() const;
372 
373 private:
374  // The render index constructor is private so we can check
375  // renderDelegate before construction: see HdRenderIndex::New(...).
376  HdRenderIndex(HdRenderDelegate *renderDelegate);
377 
378  // ---------------------------------------------------------------------- //
379  // Private Helper methods
380  // ---------------------------------------------------------------------- //
381 
382  // Go through all RPrims and reallocate their instance ids
383  // This is called once we have exhausted all all 24bit instance ids.
384  void _CompactPrimIds();
385 
386  // Allocate the next available instance id to the prim
387  void _AllocatePrimId(HdRprim* prim);
388 
389  // Inserts the task into the index and updates tracking state.
390  // _TrackDelegateTask is called by the inlined InsertTask<T>, so it needs
391  // to be marked HD_API.
392  HD_API
393  void _TrackDelegateTask(HdSceneDelegate* delegate,
394  SdfPath const& taskId,
395  HdTaskSharedPtr const& task);
396 
397  template <typename T>
398  static inline const TfToken & _GetTypeId();
399 
400 
401  void _RemoveRprimSubtree(const SdfPath &root,
402  HdSceneDelegate* sceneDelegate);
403  void _RemoveInstancerSubtree(const SdfPath &root,
404  HdSceneDelegate* sceneDelegate);
405  void _RemoveExtComputationSubtree(const SdfPath &root,
406  HdSceneDelegate* sceneDelegate);
407  void _RemoveTaskSubtree(const SdfPath &root,
408  HdSceneDelegate* sceneDelegate);
409 
410 
411  // ---------------------------------------------------------------------- //
412  // Index State
413  // ---------------------------------------------------------------------- //
414  struct _RprimInfo {
415  HdSceneDelegate *sceneDelegate;
416  HdRprim *rprim;
417  };
418 
419  struct _TaskInfo {
420  HdSceneDelegate *sceneDelegate;
421  HdTaskSharedPtr task;
422  };
423 
424  typedef std::unordered_map<SdfPath, _TaskInfo, SdfPath::Hash> _TaskMap;
425  typedef TfHashMap<SdfPath, _RprimInfo, SdfPath::Hash> _RprimMap;
426  typedef std::vector<SdfPath> _RprimPrimIDVector;
427 
428  typedef Hd_PrimTypeIndex<HdSprim> _SprimIndex;
429  typedef Hd_PrimTypeIndex<HdBprim> _BprimIndex;
430 
431  _RprimMap _rprimMap;
432  Hd_SortedIds _rprimIds;
433 
434  _RprimPrimIDVector _rprimPrimIdMap;
435 
436  _TaskMap _taskMap;
437 
438  _SprimIndex _sprimIndex;
439  _BprimIndex _bprimIndex;
440 
441  HdChangeTracker _tracker;
442 
443  typedef TfHashMap<SdfPath, HdInstancer*, SdfPath::Hash> _InstancerMap;
444  _InstancerMap _instancerMap;
445 
446  struct _SyncQueueEntry {
447  HdDirtyListSharedPtr dirtyList;
448  HdRprimCollection collection;
449 
450  };
451  typedef std::vector<_SyncQueueEntry> _SyncQueue;
452  _SyncQueue _syncQueue;
453 
454  HdRenderDelegate *_renderDelegate;
455 
456  // ---------------------------------------------------------------------- //
457  // Sync State
458  // ---------------------------------------------------------------------- //
459  TfTokenVector _activeRenderTags;
460  unsigned int _renderTagVersion;
461 
463  void _InitPrimTypes();
464 
466  bool _CreateFallbackPrims();
467 
469  void _DestroyFallbackPrims();
470 
471  void _GatherRenderTags(const HdTaskSharedPtrVector *tasks);
472 
473  typedef tbb::enumerable_thread_specific<HdDrawItemPtrVector>
474  _ConcurrentDrawItems;
475 
476  void _AppendDrawItems(const SdfPathVector &rprimIds,
477  size_t begin,
478  size_t end,
479  HdRprimCollection const& collection,
480  _ConcurrentDrawItems* result);
481 
485  static void _ConfigureReprs();
486 
487  // Remove default constructor
488  HdRenderIndex() = delete;
489 };
490 
491 template <typename T>
492 void
494 {
495  HD_TRACE_FUNCTION();
496  HF_MALLOC_TAG_FUNCTION();
497 
498  HdTaskSharedPtr task = boost::make_shared<T>(delegate, id);
499  _TrackDelegateTask(delegate, id, task);
500 }
501 
502 PXR_NAMESPACE_CLOSE_SCOPE
503 
504 #endif //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:119
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:55
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:239
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:262
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:438
bool HasTask(SdfPath const &id)
Returns true if a task exists in the index with the given id.
Definition: renderIndex.h:283
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:209
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 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 RegisterTextureResource() 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:493
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 void Sync(HdDirtyListSharedPtr const &dirtyList, HdRprimCollection const &collection)
Adds the dirty list to the sync queue.
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.