HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pointInstancerAdapter.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_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
25 #define PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
26 
27 /// \file usdImaging/pointInstancerAdapter.h
28 
29 #include "pxr/pxr.h"
33 
34 #include <mutex>
35 
37 
38 
39 /// Delegate support for UsdGeomPointInstancer
40 ///
42 public:
44 
46  : BaseAdapter()
47  { }
49 
50  virtual SdfPath Populate(
51  UsdPrim const& prim,
53  UsdImagingInstancerContext const* instancerContext = NULL) override;
54 
55  virtual bool ShouldCullChildren() const override;
56 
57  virtual bool IsInstancerAdapter() const override;
58 
59  // ---------------------------------------------------------------------- //
60  /// \name Parallel Setup and Resolve
61  // ---------------------------------------------------------------------- //
62 
63  virtual void TrackVariability(UsdPrim const& prim,
64  SdfPath const& cachePath,
65  HdDirtyBits* timeVaryingBits,
67  instancerContext = NULL) const override;
68 
69  virtual void UpdateForTime(UsdPrim const& prim,
70  SdfPath const& cachePath,
72  HdDirtyBits requestedBits,
74  instancerContext = NULL) const override;
75 
76  // ---------------------------------------------------------------------- //
77  /// \name Change Processing
78  // ---------------------------------------------------------------------- //
79 
80  virtual HdDirtyBits ProcessPropertyChange(UsdPrim const& prim,
81  SdfPath const& cachePath,
82  TfToken const& propertyName) override;
83 
84  virtual void ProcessPrimResync(SdfPath const& cachePath,
85  UsdImagingIndexProxy* index) override;
86 
87  virtual void ProcessPrimRemoval(SdfPath const& cachePath,
88  UsdImagingIndexProxy* index) override;
89 
90  virtual void MarkDirty(UsdPrim const& prim,
91  SdfPath const& cachePath,
92  HdDirtyBits dirty,
93  UsdImagingIndexProxy* index) override;
94 
95  virtual void MarkRefineLevelDirty(UsdPrim const& prim,
96  SdfPath const& cachePath,
97  UsdImagingIndexProxy* index) override;
98 
99  virtual void MarkReprDirty(UsdPrim const& prim,
100  SdfPath const& cachePath,
101  UsdImagingIndexProxy* index) override;
102 
103  virtual void MarkCullStyleDirty(UsdPrim const& prim,
104  SdfPath const& cachePath,
105  UsdImagingIndexProxy* index) override;
106 
107  virtual void MarkRenderTagDirty(UsdPrim const& prim,
108  SdfPath const& cachePath,
109  UsdImagingIndexProxy* index) override;
110 
111  virtual void MarkTransformDirty(UsdPrim const& prim,
112  SdfPath const& cachePath,
113  UsdImagingIndexProxy* index) override;
114 
115  virtual void MarkVisibilityDirty(UsdPrim const& prim,
116  SdfPath const& cachePath,
117  UsdImagingIndexProxy* index) override;
118 
119 
120 
121  // ---------------------------------------------------------------------- //
122  /// \name Instancing
123  // ---------------------------------------------------------------------- //
124 
125  virtual size_t
126  SampleInstancerTransform(UsdPrim const& instancerPrim,
127  SdfPath const& instancerPath,
129  size_t maxNumSamples,
130  float *sampleTimes,
131  GfMatrix4d *sampleValues) override;
132 
133  virtual size_t
134  SampleTransform(UsdPrim const& prim,
135  SdfPath const& cachePath,
136  UsdTimeCode time,
137  size_t maxNumSamples,
138  float *sampleTimes,
139  GfMatrix4d *sampleValues) override;
140 
141  virtual size_t
142  SamplePrimvar(UsdPrim const& usdPrim,
143  SdfPath const& cachePath,
144  TfToken const& key,
146  size_t maxNumSamples,
147  float *sampleTimes,
148  VtValue *sampleValues) override;
149 
150  virtual PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
151  SdfPath const& cachePath,
152  UsdTimeCode time) const override;
153 
154  // ---------------------------------------------------------------------- //
155  /// \name Nested instancing support
156  // ---------------------------------------------------------------------- //
157 
159  SdfPath const &instancerPath,
160  SdfPath const &protoInstancerPath,
161  UsdTimeCode time) const override;
162 
163  // ---------------------------------------------------------------------- //
164  /// \name Picking & selection
165  // ---------------------------------------------------------------------- //
166 
167  virtual SdfPath GetScenePrimPath(
168  SdfPath const& cachePath,
169  int instanceIndex,
170  HdInstancerContext *instancerContext) const override;
171 
172  virtual bool PopulateSelection(
173  HdSelection::HighlightMode const& highlightMode,
174  SdfPath const &cachePath,
175  UsdPrim const &usdPrim,
176  int const hydraInstanceIndex,
177  VtIntArray const &parentInstanceIndices,
178  HdSelectionSharedPtr const &result) const override;
179 
180  // ---------------------------------------------------------------------- //
181  /// \name Volume field information
182  // ---------------------------------------------------------------------- //
183 
185  GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
186  UsdTimeCode time) const override;
187 
188 protected:
189  virtual void _RemovePrim(SdfPath const& cachePath,
190  UsdImagingIndexProxy* index) override final;
191 
192 private:
193  struct _ProtoPrim;
194  struct _InstancerData;
195 
196  SdfPath _Populate(UsdPrim const& prim,
198  UsdImagingInstancerContext const* instancerContext);
199 
200  void _PopulatePrototype(int protoIndex,
201  _InstancerData& instrData,
202  UsdPrim const& protoRootPrim,
204  UsdImagingInstancerContext const *instancerContext);
205 
206  // Process prim removal and output a set of affected instancer paths is
207  // provided.
208  void _ProcessPrimRemoval(SdfPath const& cachePath,
210  SdfPathVector* instancersToReload);
211 
212  // Removes all instancer data, both locally and from the render index.
213  void _UnloadInstancer(SdfPath const& instancerPath,
215 
216  // Computes per-frame instance indices.
217  typedef std::unordered_map<SdfPath, VtIntArray, SdfPath::Hash> _InstanceMap;
218  _InstanceMap _ComputeInstanceMap(SdfPath const& instancerPath,
219  _InstancerData const& instrData,
220  UsdTimeCode time) const;
221 
222  // Updates per-frame instancer visibility.
223  void _UpdateInstancerVisibility(SdfPath const& instancerPath,
224  _InstancerData const& instrData,
225  UsdTimeCode time) const;
226 
227  // Returns true if the instancer is visible, taking into account all
228  // parent instancers visibilities.
229  bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
230  const;
231 
232  // Gets the associated _ProtoPrim for the given instancer and cache path.
233  _ProtoPrim const& _GetProtoPrim(SdfPath const& instancerPath,
234  SdfPath const& cachePath) const;
235 
236  // Gets the UsdPrim to use from the given _ProtoPrim.
237  const UsdPrim _GetProtoUsdPrim(_ProtoPrim const& proto) const;
238 
239  // Takes the transform in the value cache (this must exist before calling
240  // this method) and applies a corrective transform to 1) remove any
241  // transforms above the model root (root proto path) and 2) apply the
242  // instancer transform.
243  void _CorrectTransform(UsdPrim const& instancer,
244  UsdPrim const& proto,
245  SdfPath const& cachePath,
246  SdfPathVector const& protoPathChain,
247  UsdTimeCode time) const;
248 
249  // Similar to CorrectTransform, requires a visibility value exist in the
250  // ValueCache, removes any visibility opinions above the model root (proto
251  // root path) and applies the instancer visibility.
252  void _ComputeProtoVisibility(UsdPrim const& protoRoot,
253  UsdPrim const& protoGprim,
255  bool* vis) const;
256 
257  /*
258  PointInstancer (InstancerData)
259  |
260  +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
261  | +-- ProtoRprim
262  | +-- ProtoRprim
263  |
264  +-- Prototype[1]------+-- ProtoRprim
265  | +-- ProtoRprim
266  .
267  .
268  */
269 
270  // A proto prim represents a single populated prim under a prototype root
271  // declared on the instancer. For example, a character may be targeted
272  // by the prototypes relationship; it will have many meshes, and each
273  // mesh is represented as a separate proto prim.
274  struct _ProtoPrim {
275  _ProtoPrim() : variabilityBits(0), visible(true) {}
276  // Each prim will become a prototype "child" under the instancer.
277  // paths is a list of paths we had to hop across when resolving native
278  // USD instances.
280  // The prim adapter for the actual prototype prim.
282  // The root prototype path, typically the model root, which is a subtree
283  // and might contain several imageable prims.
284  SdfPath protoRootPath;
285  // Tracks the variability of the underlying adapter to avoid
286  // redundantly reading data. This value is stored as
287  // HdDirtyBits bit flags.
288  // XXX: This is mutable so we can set it in TrackVariability.
289  mutable HdDirtyBits variabilityBits;
290  // When variabilityBits does not include HdChangeTracker::DirtyVisibility
291  // the visible field is the unvarying value for visibility.
292  // XXX: This is mutable so we can set it in TrackVariability.
293  mutable bool visible;
294  };
295 
296  // Indexed by cachePath (each prim has one entry)
297  typedef std::unordered_map<SdfPath, _ProtoPrim, SdfPath::Hash> _ProtoPrimMap;
298 
299  // All data asscoiated with a given Instancer prim. PrimMap could
300  // technically be split out to avoid two lookups, however it seems cleaner
301  // to keep everything bundled up under the instancer path.
302  struct _InstancerData {
303  _InstancerData() {}
304  SdfPath parentInstancerCachePath;
305  _ProtoPrimMap protoPrimMap;
306  SdfPathVector prototypePaths;
307 
308  // XXX: We keep a bunch of state around visibility that's set in
309  // TrackVariability and UpdateForTime. "visible", and "visibleTime"
310  // (the cache key for visible) are set in UpdateForTime and guarded
311  // by "mutex".
312  mutable std::mutex mutex;
313  mutable bool variableVisibility;
314  mutable bool visible;
315  mutable UsdTimeCode visibleTime;
316  };
317 
318  // A map of instancer data, one entry per instancer prim that has been
319  // populated.
320  // Note: this is accessed in multithreaded code paths and must be protected
321  typedef std::unordered_map<SdfPath /*instancerPath*/,
322  _InstancerData,
323  SdfPath::Hash> _InstancerDataMap;
324  _InstancerDataMap _instancerData;
325 };
326 
327 
328 
330 
331 #endif // PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
virtual SdfPath GetScenePrimPath(SdfPath const &cachePath, int instanceIndex, HdInstancerContext *instancerContext) const override
virtual void ProcessPrimResync(SdfPath const &cachePath, UsdImagingIndexProxy *index) override
virtual size_t SampleTransform(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time, size_t maxNumSamples, float *sampleTimes, GfMatrix4d *sampleValues) override
Samples the transform for the given prim.
GT_API const UT_StringHolder time
GLuint index
Definition: glew.h:1814
virtual size_t SampleInstancerTransform(UsdPrim const &instancerPrim, SdfPath const &instancerPath, UsdTimeCode time, size_t maxNumSamples, float *sampleTimes, GfMatrix4d *sampleValues) override
uint32_t HdDirtyBits
Definition: types.h:127
virtual HdDirtyBits ProcessPropertyChange(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &propertyName) override
virtual void MarkRenderTagDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
virtual void MarkDirty(UsdPrim const &prim, SdfPath const &cachePath, HdDirtyBits dirty, UsdImagingIndexProxy *index) override
std::vector< std::pair< SdfPath, int > > HdInstancerContext
Instancer context: a pair of instancer paths and instance indices.
Definition: sceneDelegate.h:61
virtual void MarkReprDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
virtual bool PopulateSelection(HdSelection::HighlightMode const &highlightMode, SdfPath const &cachePath, UsdPrim const &usdPrim, int const hydraInstanceIndex, VtIntArray const &parentInstanceIndices, HdSelectionSharedPtr const &result) const override
virtual void MarkCullStyleDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
Definition: token.h:87
virtual SdfPath Populate(UsdPrim const &prim, UsdImagingIndexProxy *index, UsdImagingInstancerContext const *instancerContext=NULL) override
virtual void MarkTransformDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
Definition: prim.h:132
virtual GfMatrix4d GetRelativeInstancerTransform(SdfPath const &instancerPath, SdfPath const &protoInstancerPath, UsdTimeCode time) const override
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:55
Definition: path.h:288
std::vector< HdVolumeFieldDescriptor > HdVolumeFieldDescriptorVector
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:209
virtual void UpdateForTime(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time, HdDirtyBits requestedBits, UsdImagingInstancerContext const *instancerContext=NULL) const override
virtual bool ShouldCullChildren() const override
virtual size_t SamplePrimvar(UsdPrim const &usdPrim, SdfPath const &cachePath, TfToken const &key, UsdTimeCode time, size_t maxNumSamples, float *sampleTimes, VtValue *sampleValues) override
virtual void ProcessPrimRemoval(SdfPath const &cachePath, UsdImagingIndexProxy *index) override
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
virtual void TrackVariability(UsdPrim const &prim, SdfPath const &cachePath, HdDirtyBits *timeVaryingBits, UsdImagingInstancerContext const *instancerContext=NULL) const override
virtual void MarkVisibilityDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
virtual void _RemovePrim(SdfPath const &cachePath, UsdImagingIndexProxy *index) overridefinal
Returns true if the adapter can be populated into the target index.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< UsdImagingPrimAdapter > UsdImagingPrimAdapterSharedPtr
virtual void MarkRefineLevelDirty(UsdPrim const &prim, SdfPath const &cachePath, UsdImagingIndexProxy *index) override
GLuint64EXT * result
Definition: glew.h:14007
virtual bool IsInstancerAdapter() const override
virtual HdVolumeFieldDescriptorVector GetVolumeFieldDescriptors(UsdPrim const &usdPrim, SdfPath const &id, UsdTimeCode time) const override
std::shared_ptr< class HdSelection > HdSelectionSharedPtr
Definition: selection.h:40
Definition: value.h:174
virtual PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Get the subdiv tags for this prim.
GLenum const void * paths
Definition: glew.h:13589