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 
36 
37 class HdRprim;
38 class HdStDrawItem;
39 struct HdRprimSharedData;
40 
41 using HdBufferArrayRangeSharedPtr = std::shared_ptr<class HdBufferArrayRange>;
42 
43 /// \class HdStInstancer
44 ///
45 /// HdSt implements instancing by drawing each proto multiple times with
46 /// a single draw call. Application of instance primvars (like transforms)
47 /// is done in shaders. Instance transforms in particular are computed in
48 /// ApplyInstanceTransform in instancing.glslfx.
49 ///
50 /// If this instancer is nested, instance indices will be computed
51 /// recursively by ascending the hierarchy. HdStInstancer computes a flattened
52 /// index structure for each prototype by taking the cartesian product of the
53 /// instance indices at each level.
54 ///
55 /// For example:
56 /// - InstancerA draws instances [ProtoX, InstancerB, ProtoX, InstancerB]
57 /// - InstancerB draws instances [ProtoY, ProtoZ, ProtoY]
58 /// The flattened index for Proto Y is:
59 /// [0, 0, 1]; [1, 0, 3]; [2, 2, 1]; [3, 2, 3];
60 /// where the first tuple element is the position in the flattened index;
61 /// the second tuple element is the position in Instancer B;
62 /// and the last tuple element is the position in Instancer A.
63 ///
64 /// The flattened index gives the number of times the proto is drawn, and the
65 /// index tuple can be passed to the shader so that each instance can look up
66 /// its instance primvars in the bound primvar arrays.
67 
68 class HdStInstancer : public HdInstancer {
69 public:
70  /// Constructor.
71  HDST_API
72  HdStInstancer(HdSceneDelegate* delegate, SdfPath const &id);
73 
74  // Updates the instance primvar buffers.
75  // XXX: Note, this is currently called from rprimUtils instead of the
76  // render index sync phase, so it needs to take a mutex.
77  HDST_API
78  void Sync(HdSceneDelegate *sceneDelegate,
79  HdRenderParam *renderParam,
80  HdDirtyBits *dirtyBits) override;
81 
83  return _instancePrimvarRange;
84  }
85 
86  /// Populates the instance index indirection buffer for \p prototypeId and
87  /// returns a flat array of instance index tuples.
88  HDST_API
89  VtIntArray GetInstanceIndices(SdfPath const &prototypeId);
90 
91 protected:
92  HDST_API
93  void _GetInstanceIndices(SdfPath const &prototypeId,
94  std::vector<VtIntArray> *instanceIndicesArray);
95 
96  HDST_API
97  void _SyncPrimvars(HdSceneDelegate *sceneDelegate,
98  HdDirtyBits *dirtyBits);
99 
100 private:
101  // # of entries in an instance primvar. This should be consistent between
102  // all primvars, and also consistent with the instance indices (meaning
103  // no instance index is out-of-range).
104  size_t _instancePrimvarNumElements;
105 
106  // The BAR of the instance primvars for this instancer.
107  // (Note: instance indices are computed per prototype and the rprim owns
108  // the bar).
109  HdBufferArrayRangeSharedPtr _instancePrimvarRange;
110 };
111 
112 
114 
115 #endif // PXR_IMAGING_HD_ST_INSTANCER_H
HDST_API void _GetInstanceIndices(SdfPath const &prototypeId, std::vector< VtIntArray > *instanceIndicesArray)
uint32_t HdDirtyBits
Definition: types.h:127
HDST_API void _SyncPrimvars(HdSceneDelegate *sceneDelegate, HdDirtyBits *dirtyBits)
HDST_API HdStInstancer(HdSceneDelegate *delegate, SdfPath const &id)
Constructor.
Definition: rprim.h:68
HDST_API VtIntArray GetInstanceIndices(SdfPath const &prototypeId)
HDST_API void Sync(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits) override
Definition: path.h:288
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
#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:82