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_INSTANCER_H
25 #define PXR_IMAGING_HD_INSTANCER_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/types.h"
31 
32 #include "pxr/usd/sdf/path.h"
33 
34 #include <mutex>
35 
37 
38 class HdSceneDelegate;
39 class HdRenderIndex;
40 class HdRprim;
41 class HdRenderParam;
42 
43 /// \class HdInstancer
44 ///
45 /// This class exists to facilitate point cloud style instancing. Instancers,
46 /// conceptually, are instructions to draw N objects; for each object, store
47 /// which Rprim you're drawing and what instance-specific primvars you're
48 /// binding.
49 ///
50 /// "/InstancerA": prototypes = ["/sphere", "/cube", "/sphere"];
51 /// hydra:instanceTranslations = [<0,0,0>, <1,0,0>, <0,1,0>]
52 ///
53 /// Hydra stores this in reverse: Rprims store which instancer is drawing them,
54 /// and the instancer stores which indices in that array of N objects are the
55 /// given Rprim.
56 ///
57 /// "/sphere": instancerId = "/InstancerA"
58 /// "/cube": instancerId = "/InstancerA"
59 /// Instancer A: indices("/sphere") = [0, 2]
60 /// indices("/cube") = [1]
61 /// hydra:instanceTranslations = [<0,0,0>, <1,0,0>, <0,1,0>]
62 ///
63 /// Instancing is implemented by the prototype drawing itself multiple times,
64 /// and looking up per-instance data each time based on "indices": so
65 /// "/sphere" would draw itself once with translate=<0,0,0> and once with
66 /// translate=<0,1,0>.
67 ///
68 /// To make things more exciting, instancers can be nested.
69 ///
70 /// "/cube": instancerId = "/InstancerA"
71 /// "/InstancerA": instancerId = "/InstancerB"
72 /// indices("/cube") = [0, 1]
73 /// hydra:instanceTranslations = [<0,0,0>, <1,0,0>]
74 /// "/InstancerB": indices("/InstancerA") = [0, 1]
75 /// hydra:instanceTranslations = [<0,0,0>, <0,1,0>]
76 ///
77 /// In this case, "/cube" draws itself four times, for each of the
78 /// index tuples <0,0>, <0,1>, <1,0>, <1,1> where the first index is
79 /// the index in instancerA, and the second index is in instancerB.
80 ///
81 /// If the same primvar (e.g. "hydra:instanceTranslations") shows up at multiple
82 /// levels of nesting, it's resolved as follows:
83 ///
84 /// Transforms
85 /// ----------
86 ///
87 /// Instance primvars "hydra:instanceTranslations", "hydra:instanceRotations",
88 /// "hydra:instanceScales", and "hydra:instanceTransforms" are used to compute
89 /// the final transform of an instance. "hydra:instanceTranslations" and
90 /// "hydra:instanceScales" are interpreted as vec3: position, and axis-aligned
91 /// scale respectively. "hydra:instanceRotations" is interpreted as a vec4
92 /// quaternion (<real, i, j k>), and "hydra:instanceTransforms" is a 4x4 matrix.
93 /// In the transform computation, everything is converted to a 4x4 matrix.
94 ///
95 /// There are additional transforms: "instancerTransform" comes from
96 /// HdSceneDelegate::GetInstancerTransform(instancer, proto), and represents
97 /// the constant transform between the instancer and the prototype. It
98 /// varies with each level of nesting, but not across instances.
99 ///
100 /// "transform" is the proto Rprim's local transform.
101 ///
102 /// The final instance transform for instance "index" is computed as:
103 ///
104 /// nested_transform(level) = instancerTransform(level) *
105 /// hydra:instanceTranslations(level, index) *
106 /// hydra:instanceRotations(level, index) *
107 /// hydra:instanceScales(level, index) *
108 /// hydra:instanceTransforms(level, index);
109 /// output_transform = product(i : nested-levels - 1 -> 0) {
110 /// nested_transform(i)
111 /// } * transform;
112 ///
113 /// Any transforms not provided by the scene delegate are set to identity.
114 ///
115 /// Class responsibilities
116 /// ======================
117 ///
118 /// HdInstancer's primary role is to track the "indices" arrays for each
119 /// proto used by an instancer, and any provided primvar arrays. The
120 /// implementation is in the renderer-specific instancers, like HdStInstancer.
121 ///
122 /// All data access (aside from local caches) is routed to the HdSceneDelegate.
123 ///
124 
125 class HdInstancer {
126 public:
127  /// Constructor.
128  HD_API
129  HdInstancer(HdSceneDelegate* delegate, SdfPath const& id);
130 
131  HD_API
132  virtual ~HdInstancer();
133 
134  /// Returns the identifier.
135  SdfPath const& GetId() const { return _id; }
136 
137  /// Returns the parent instancer identifier.
138  SdfPath const& GetParentId() const { return _parentId; }
139 
140  HdSceneDelegate* GetDelegate() const { return _delegate; }
141 
142  HD_API
144  HdRprim const& rprim);
145 
146  HD_API
147  static TfTokenVector const & GetBuiltinPrimvarNames();
148 
149  HD_API
150  virtual void Sync(HdSceneDelegate *sceneDelegate,
151  HdRenderParam *renderParam,
152  HdDirtyBits *dirtyBits);
153 
154  HD_API
155  virtual void Finalize(HdRenderParam *renderParam);
156 
157  HD_API
158  virtual HdDirtyBits GetInitialDirtyBitsMask() const;
159 
160  HD_API
161  static void _SyncInstancerAndParents(
162  HdRenderIndex &renderIndex,
163  SdfPath const& instancerId);
164 
165 protected:
166  HD_API
167  void _UpdateInstancer(HdSceneDelegate *delegate,
168  HdDirtyBits *dirtyBits);
169 
170 private:
171  HdSceneDelegate* _delegate;
172  SdfPath _id;
173  SdfPath _parentId;
174 
175  // XXX: This mutex exists for _SyncInstancerAndParents, which will go
176  // away when the render index calls sync on instancers.
177  std::mutex _instanceLock;
178 };
179 
180 
182 
183 #endif // PXR_IMAGING_HD_INSTANCER_H
virtual HD_API void Sync(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits)
uint32_t HdDirtyBits
Definition: types.h:158
virtual HD_API ~HdInstancer()
#define HD_API
Definition: api.h:40
HdSceneDelegate * GetDelegate() const
Definition: instancer.h:140
virtual HD_API void Finalize(HdRenderParam *renderParam)
Definition: rprim.h:54
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
Definition: path.h:290
SdfPath const & GetId() const
Returns the identifier.
Definition: instancer.h:135
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
GLuint index
Definition: glcorearb.h:786
virtual HD_API HdDirtyBits GetInitialDirtyBitsMask() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HD_API void _UpdateInstancer(HdSceneDelegate *delegate, HdDirtyBits *dirtyBits)
SdfPath const & GetParentId() const
Returns the parent instancer identifier.
Definition: instancer.h:138
HD_API HdInstancer(HdSceneDelegate *delegate, SdfPath const &id)
Constructor.
static HD_API TfTokenVector const & GetBuiltinPrimvarNames()
static HD_API void _SyncInstancerAndParents(HdRenderIndex &renderIndex, SdfPath const &instancerId)
static HD_API int GetInstancerNumLevels(HdRenderIndex &index, HdRprim const &rprim)