HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
instancer.h
Go to the documentation of this file.
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_ST_INSTANCER_H
8 #define PXR_IMAGING_HD_ST_INSTANCER_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
14 #include "pxr/usd/sdf/path.h"
15 #include "pxr/base/vt/array.h"
16 #include "pxr/base/tf/hashmap.h"
17 
19 
20 class HdRprim;
21 class HdStDrawItem;
22 struct HdRprimSharedData;
23 
24 using HdBufferArrayRangeSharedPtr = std::shared_ptr<class HdBufferArrayRange>;
25 
26 /// \class HdStInstancer
27 ///
28 /// HdSt implements instancing by drawing each proto multiple times with
29 /// a single draw call. Application of instance primvars (like transforms)
30 /// is done in shaders. Instance transforms in particular are computed in
31 /// ApplyInstanceTransform in instancing.glslfx.
32 ///
33 /// If this instancer is nested, instance indices will be computed
34 /// recursively by ascending the hierarchy. HdStInstancer computes a flattened
35 /// index structure for each prototype by taking the cartesian product of the
36 /// instance indices at each level.
37 ///
38 /// For example:
39 /// - InstancerA draws instances [ProtoX, InstancerB, ProtoX, InstancerB]
40 /// - InstancerB draws instances [ProtoY, ProtoZ, ProtoY]
41 /// The flattened index for Proto Y is:
42 /// [0, 0, 1]; [1, 0, 3]; [2, 2, 1]; [3, 2, 3];
43 /// where the first tuple element is the position in the flattened index;
44 /// the second tuple element is the position in Instancer B;
45 /// and the last tuple element is the position in Instancer A.
46 ///
47 /// The flattened index gives the number of times the proto is drawn, and the
48 /// index tuple can be passed to the shader so that each instance can look up
49 /// its instance primvars in the bound primvar arrays.
50 
51 class HdStInstancer : public HdInstancer {
52 public:
53  /// Constructor.
54  HDST_API
55  HdStInstancer(HdSceneDelegate* delegate, SdfPath const &id);
56 
57  // Updates the instance primvar buffers.
58  // XXX: Note, this is currently called from rprimUtils instead of the
59  // render index sync phase, so it needs to take a mutex.
60  HDST_API
61  void Sync(HdSceneDelegate *sceneDelegate,
62  HdRenderParam *renderParam,
63  HdDirtyBits *dirtyBits) override;
64 
66  return _instancePrimvarRange;
67  }
68 
69  /// Populates the instance index indirection buffer for \p prototypeId and
70  /// returns a flat array of instance index tuples.
71  HDST_API
72  VtIntArray GetInstanceIndices(SdfPath const &prototypeId);
73 
74 protected:
75  HDST_API
76  void _GetInstanceIndices(SdfPath const &prototypeId,
77  std::vector<VtIntArray> *instanceIndicesArray);
78 
79  HDST_API
80  void _SyncPrimvars(HdSceneDelegate *sceneDelegate,
81  HdDirtyBits *dirtyBits);
82 
83 private:
84  // # of entries in an instance primvar. This should be consistent between
85  // all primvars, and also consistent with the instance indices (meaning
86  // no instance index is out-of-range).
87  size_t _instancePrimvarNumElements;
88 
89  // The BAR of the instance primvars for this instancer.
90  // (Note: instance indices are computed per prototype and the rprim owns
91  // the bar).
92  HdBufferArrayRangeSharedPtr _instancePrimvarRange;
93 
94  // Visibility
95  bool _visible;
96 };
97 
98 
100 
101 #endif // PXR_IMAGING_HD_ST_INSTANCER_H
HDST_API void _GetInstanceIndices(SdfPath const &prototypeId, std::vector< VtIntArray > *instanceIndicesArray)
uint32_t HdDirtyBits
Definition: types.h:143
HDST_API void _SyncPrimvars(HdSceneDelegate *sceneDelegate, HdDirtyBits *dirtyBits)
HDST_API HdStInstancer(HdSceneDelegate *delegate, SdfPath const &id)
Constructor.
Definition: rprim.h:37
HDST_API VtIntArray GetInstanceIndices(SdfPath const &prototypeId)
HDST_API void Sync(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits) override
Definition: path.h:273
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define HDST_API
Definition: api.h:23
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
HdBufferArrayRangeSharedPtr GetInstancePrimvarRange() const
Definition: instancer.h:65