HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XUSD_HydraInstancer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Side Effects Software Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * NAME: XUSD_HydraInstancer.h (HUSD Library, C++)
17  *
18  * COMMENTS: Basic instancer for creating instance transforms.
19  *
20  */
21 
22 #ifndef XUSD_HydraInstancer_h
23 #define XUSD_HydraInstancer_h
24 
25 #include "HUSD_API.h"
26 #include <UT/UT_Lock.h>
27 #include <UT/UT_Map.h>
28 #include <UT/UT_UniquePtr.h>
29 #include <GT/GT_Transform.h>
30 #include <GT/GT_TransformArray.h>
31 
32 #include <pxr/pxr.h>
35 #include <pxr/base/tf/hashmap.h>
36 #include <pxr/base/tf/token.h>
37 
38 class HUSD_Scene;
39 
41 
43 {
44 public:
46  SdfPath const& id,
47  SdfPath const &parentInstancerId);
48  virtual ~XUSD_HydraInstancer();
49 
50  // Checks the change tracker to determine whether instance primvars are
51  // dirty, and if so pulls them. Since primvars can only be pulled once,
52  // and are cached, this function is not re-entrant. However, this function
53  // is called by ComputeInstanceTransforms, which is called (potentially)
54  // by HdMantraMesh::Sync(), which is dispatched in parallel, so it needs
55  // to be guarded by _instanceLock.
56  //
57  // The @c nsegs variable indicates the number of segments/samples required
58  // for motion blur. The function returns the actual number of segments on
59  // the instancer.
60  //
61  // Pulled primvars are cached in _primvarMap.
62  int syncPrimvars(bool recurse, int nsegs=1);
63 
64  // Return the number of evaluated motion segments
65  int motionSegments() const
66  {
67  return SYSmax(myXSegments, myPSegments);
68  }
69 
70  // Grab the transforms for this instancer, and flatten it with any parent
71  // instancers if 'recurse' is true. syncPrimvars() must be called first.
72  VtMatrix4dArray computeTransforms(const SdfPath &protoId,
73  bool recurse,
74  const GfMatrix4d *protoXform,
75  float shutter_time=0);
76 
77  // Grab the transforms and scene ids for each instance. If 'recurse' is
78  // true, flatten both the transforms and ids for nested instancers.
79  // syncPrimvars() must be called first.
80  VtMatrix4dArray computeTransformsAndIDs(const SdfPath &protoId,
81  bool recurse,
82  const GfMatrix4d *protoXform,
83  int level,
85  HUSD_Scene *scene,
86  float shutter=0);
87 
88  bool isPointInstancer() const { return myIsPointInstancer; }
89 
90 protected:
92  : public UT_NonCopyable
93  {
95  public:
97  : myBuffers()
98  , mySize(0)
99  {
100  }
102  : myBuffers(UTmakeUnique<BufferPtr[]>(size))
103  , mySize(size)
104  {
105  }
107  : myBuffers(std::move(src.myBuffers))
108  , mySize(src.mySize)
109  {
110  src.mySize = 0;
111  }
113  {
114  myBuffers = std::move(src.myBuffers);
115  mySize = src.mySize;
116  src.mySize = 0;
117  return *this;
118  }
120  {
121  }
122  exint size() const { return mySize; }
123  const HdVtBufferSource *buffer(exint i) const
124  {
125  UT_ASSERT_P(i >= 0 && i < mySize);
126  return myBuffers[i].get();
127  }
128  const HdVtBufferSource *operator[](exint i) const { return buffer(i); }
130  {
131  myBuffers[idx] = std::move(b);
132  }
133  private:
134  UT_UniquePtr<BufferPtr[]> myBuffers;
135  exint mySize;
136  };
137 
138  /// Given a shutter time and a number of motion segments, return the motion
139  /// segment and interpolant. If seg0 != seg1, then values should be
140  /// interpolated using: @code
141  /// val = SYSlerp(primvar[seg0], primvar[seg1], lerp);
142  /// @endcode
143  void getSegment(float time, int &seg0, int &seg1, float &lerp,
144  bool for_transform) const;
145 
146  const float *xtimes() const { return myXTimes.get(); }
147  int xsegments() const { return myXSegments; }
148  const float *ptimes() const { return myPTimes.get(); }
149  int psegments() const { return myPSegments; }
150 
151  // Map of the latest primvar data for this instancer, keyed by
152  // primvar name. Primvar values are VtValue, an any-type; they are
153  // interpreted at consumption time (here, in ComputeInstanceTransforms).
155  UT_UniquePtr<float[]> myXTimes; // USD time samples for xforms
156  UT_UniquePtr<float[]> myPTimes; // USD time samples for primvars
157  UT_UniquePtr<GfMatrix4d[]> myXforms; // Transform matrices
158  int myXSegments; // Number of xform segments
159  int myPSegments; // Number of primvar segments
160  int myNSegments; // Requested segments
161 
162  mutable UT_Lock myLock;
163 
164 private:
165  VtMatrix4dArray privComputeTransforms(const SdfPath &prototypeId,
166  bool recurse,
167  const GfMatrix4d *protoXform,
168  int level,
169  UT_StringArray *instances,
170  UT_IntArray *ids,
171  HUSD_Scene *scene,
172  float shutter_time);
173  bool myIsPointInstancer;
174 };
175 
177 {
178 public:
179  XUSD_HydraTransforms() : myDataId(-1) {}
181 
182  void setDataId(int64 id) { myDataId = id; }
183  virtual int64 getDataId() const { return myDataId; }
184 private:
185  int64 myDataId;
186 };
187 
189 
190 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1447
GLuint id
Definition: glew.h:1679
GLsizeiptr size
Definition: glew.h:1681
GLenum src
Definition: glew.h:2410
Unsorted map container.
Definition: UT_Map.h:83
const float * xtimes() const
const float * ptimes() const
GT_API const UT_StringHolder time
Scene information for the native viewport renderer.
Definition: HUSD_Scene.h:59
virtual int64 getDataId() const
Data id support for derived classes (-1 means always re-update).
virtual bool lerp(GA_AttributeOperand &d, GA_AttributeOperand &a, GA_AttributeOperand &b, GA_AttributeOperand &t) const
d = SYSlerp(a, b, t);
#define HUSD_API
Definition: HUSD_API.h:32
long long int64
Definition: SYS_Types.h:111
void setBuffer(exint idx, BufferPtr b)
UT_UniquePtr< float[]> myPTimes
UT_Map< TfToken, PrimvarMapItem, TfToken::HashFunctor > myPrimvarMap
int64 exint
Definition: SYS_Types.h:120
GLuint buffer
Definition: glew.h:1680
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:134
std::enable_if< !std::is_array< T >::value, UT_UniquePtr< T >>::type UTmakeUnique(REST &&...args)
Definition: UT_UniquePtr.h:83
bool isPointInstancer() const
Definition: path.h:287
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:47
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
PrimvarMapItem & operator=(PrimvarMapItem &&src)
const HdVtBufferSource * buffer(exint i) const
const GLuint * ids
Definition: glew.h:1684
UT_UniquePtr< GfMatrix4d[]> myXforms
UT_UniquePtr< float[]> myXTimes
const HdVtBufferSource * operator[](exint i) const
GLint level
Definition: glew.h:1252