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 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_ST_INSTANCER_H
25 #define PXR_IMAGING_HD_ST_INSTANCER_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
31 #include "pxr/usd/sdf/path.h"
32 #include "pxr/base/vt/array.h"
33 #include "pxr/base/tf/hashmap.h"
34 
35 #include <mutex>
36 
38 
39 class HdRprim;
40 class HdStDrawItem;
41 struct HdRprimSharedData;
42 
43 using HdBufferArrayRangeSharedPtr = std::shared_ptr<class HdBufferArrayRange>;
44 
45 /// \class HdStInstancer
46 ///
47 /// HdSt implements instancing by drawing each proto multiple times with
48 /// a single draw call. Application of instance primvars (like transforms)
49 /// is done in shaders. Instance transforms in particular are computed in
50 /// ApplyInstanceTransform in instancing.glslfx.
51 ///
52 /// If this instancer is nested, instance indices will be computed
53 /// recursively by ascending the hierarchy. HdStInstancer computes a flattened
54 /// index structure for each prototype by taking the cartesian product of the
55 /// instance indices at each level.
56 ///
57 /// For example:
58 /// - InstancerA draws instances [ProtoX, InstancerB, ProtoX, InstancerB]
59 /// - InstancerB draws instances [ProtoY, ProtoZ, ProtoY]
60 /// The flattened index for Proto Y is:
61 /// [0, 0, 1]; [1, 0, 3]; [2, 2, 1]; [3, 2, 3];
62 /// where the first tuple element is the position in the flattened index;
63 /// the second tuple element is the position in Instancer B;
64 /// and the last tuple element is the position in Instancer A.
65 ///
66 /// The flattened index gives the number of times the proto is drawn, and the
67 /// index tuple can be passed to the shader so that each instance can look up
68 /// its instance primvars in the bound primvar arrays.
69 
70 class HdStInstancer : public HdInstancer {
71 public:
72  /// Constructor.
73  HDST_API
74  HdStInstancer(HdSceneDelegate* delegate, SdfPath const &id,
75  SdfPath const &parentInstancerId);
76 
77  /// Updates _instancePrimvarRange with the instance rate primvars.
78  HDST_API
80  HdStDrawItem *drawItem,
81  int drawCoordIndex);
82 
84  return _instancePrimvarRange;
85  }
86 
87  /// Populates the instance index indirection buffer for \p prototypeId and
88  /// returns the buffer range.
89  HDST_API
91 
92  /// Populates the rprim's draw item with the appropriate instancer
93  /// buffer range data.
94  HDST_API
95  void PopulateDrawItem(HdRprim *prim,
96  HdStDrawItem *drawItem,
97  HdRprimSharedData *sharedData,
98  HdDirtyBits dirtyBits);
99 
100 protected:
101  HDST_API
102  void _GetInstanceIndices(SdfPath const &prototypeId,
103  std::vector<VtIntArray> *instanceIndicesArray);
104 
105 private:
106  std::mutex _instanceLock;
107  size_t _numInstancePrimvars;
108 
109  // Cache the BARs since multiple prototype Rprims will generally refer to
110  // the same instancer.
111  HdBufferArrayRangeSharedPtr _instancePrimvarRange;
114  SdfPath::Hash> _instanceIndexRangeMap;
115 };
116 
117 
119 
120 #endif // PXR_IMAGING_HD_ST_INSTANCER_H
HDST_API HdStInstancer(HdSceneDelegate *delegate, SdfPath const &id, SdfPath const &parentInstancerId)
Constructor.
HDST_API void _GetInstanceIndices(SdfPath const &prototypeId, std::vector< VtIntArray > *instanceIndicesArray)
uint32_t HdDirtyBits
Definition: types.h:127
std::shared_ptr< class HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: instancer.h:43
HDST_API HdBufferArrayRangeSharedPtr GetInstanceIndices(SdfPath const &prototypeId)
Definition: rprim.h:68
HDST_API void UpdateInstancePrimvarRange(HdRprim *prim, HdStDrawItem *drawItem, int drawCoordIndex)
Updates _instancePrimvarRange with the instance rate primvars.
HDST_API void PopulateDrawItem(HdRprim *prim, HdStDrawItem *drawItem, HdRprimSharedData *sharedData, HdDirtyBits dirtyBits)
Definition: path.h:288
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
#define HDST_API
Definition: api.h:40
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
HdBufferArrayRangeSharedPtr GetInstancePrimvarRange() const
Definition: instancer.h:83