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 USDIMAGING_POINT_INSTANCER_ADAPTER_H
25 #define USDIMAGING_POINT_INSTANCER_ADAPTER_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usdImaging/usdImaging/version.h"
31 #include "pxr/usdImaging/usdImaging/primAdapter.h"
32 #include "pxr/usdImaging/usdImaging/gprimAdapter.h"
33 
34 #include <mutex>
35 #include <boost/unordered_map.hpp>
36 #include <boost/shared_ptr.hpp>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
40 
44 public:
46 
48  : BaseAdapter()
49  { }
51 
52  virtual SdfPath Populate(
53  UsdPrim const& prim,
54  UsdImagingIndexProxy* index,
55  UsdImagingInstancerContext const* instancerContext = NULL) override;
56 
57  virtual bool ShouldCullChildren() const override;
58 
59  virtual bool IsInstancerAdapter() const override;
60 
61  // ---------------------------------------------------------------------- //
63  // ---------------------------------------------------------------------- //
64 
65  virtual void TrackVariability(UsdPrim const& prim,
66  SdfPath const& cachePath,
67  HdDirtyBits* timeVaryingBits,
69  instancerContext = NULL) const override;
70 
71  virtual void UpdateForTime(UsdPrim const& prim,
72  SdfPath const& cachePath,
73  UsdTimeCode time,
74  HdDirtyBits requestedBits,
76  instancerContext = NULL) const override;
77 
78  // ---------------------------------------------------------------------- //
80  // ---------------------------------------------------------------------- //
81 
82  virtual HdDirtyBits ProcessPropertyChange(UsdPrim const& prim,
83  SdfPath const& cachePath,
84  TfToken const& propertyName) override;
85 
86  virtual void ProcessPrimResync(SdfPath const& cachePath,
87  UsdImagingIndexProxy* index) override;
88 
89  virtual void ProcessPrimRemoval(SdfPath const& cachePath,
90  UsdImagingIndexProxy* index) override;
91 
92  virtual void MarkDirty(UsdPrim const& prim,
93  SdfPath const& cachePath,
94  HdDirtyBits dirty,
95  UsdImagingIndexProxy* index) override;
96 
97  virtual void MarkRefineLevelDirty(UsdPrim const& prim,
98  SdfPath const& cachePath,
99  UsdImagingIndexProxy* index) override;
100 
101  virtual void MarkReprDirty(UsdPrim const& prim,
102  SdfPath const& cachePath,
103  UsdImagingIndexProxy* index) override;
104 
105  virtual void MarkCullStyleDirty(UsdPrim const& prim,
106  SdfPath const& cachePath,
107  UsdImagingIndexProxy* index) override;
108 
109  virtual void MarkRenderTagDirty(UsdPrim const& prim,
110  SdfPath const& cachePath,
111  UsdImagingIndexProxy* index) override;
112 
113  virtual void MarkTransformDirty(UsdPrim const& prim,
114  SdfPath const& cachePath,
115  UsdImagingIndexProxy* index) override;
116 
117  virtual void MarkVisibilityDirty(UsdPrim const& prim,
118  SdfPath const& cachePath,
119  UsdImagingIndexProxy* index) override;
120 
121 
122 
123  // ---------------------------------------------------------------------- //
125  // ---------------------------------------------------------------------- //
126 
127  virtual SdfPath GetPathForInstanceIndex(SdfPath const &protoCachePath,
128  int protoIndex,
129  int *instanceCountForThisLevel,
130  int *instancerIndex,
131  SdfPath *masterCachePath = NULL,
132  SdfPathVector *
133  instanceContext = NULL) override;
134 
135  virtual size_t
136  SampleInstancerTransform(UsdPrim const& instancerPrim,
137  SdfPath const& instancerPath,
138  UsdTimeCode time,
139  size_t maxNumSamples,
140  float *sampleTimes,
141  GfMatrix4d *sampleValues) override;
142 
143  virtual size_t
144  SampleTransform(UsdPrim const& prim,
145  SdfPath const& cachePath,
146  UsdTimeCode time,
147  size_t maxNumSamples,
148  float *sampleTimes,
149  GfMatrix4d *sampleValues) override;
150 
151  virtual size_t
152  SamplePrimvar(UsdPrim const& usdPrim,
153  SdfPath const& cachePath,
154  TfToken const& key,
155  UsdTimeCode time,
156  size_t maxNumSamples,
157  float *sampleTimes,
158  VtValue *sampleValues) override;
159 
160  virtual PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
161  SdfPath const& cachePath,
162  UsdTimeCode time) const override;
163 
164  // ---------------------------------------------------------------------- //
166  // ---------------------------------------------------------------------- //
167  virtual SdfPath GetPathForInstanceIndex(SdfPath const &instancerCachePath,
168  SdfPath const &protoCachePath,
169  int protoIndex,
170  int *instanceCountForThisLevel,
171  int *instancerIndex,
172  SdfPath *masterCachePath,
173  SdfPathVector *instanceContext) override;
174 
175  virtual VtIntArray GetInstanceIndices(SdfPath const &instancerPath,
176  SdfPath const &protoRprimPath,
177  UsdTimeCode time) override;
178 
180  SdfPath const &instancerPath,
181  SdfPath const &protoInstancerPath,
182  UsdTimeCode time) const override;
183 
184  // ---------------------------------------------------------------------- //
186  // ---------------------------------------------------------------------- //
187  virtual bool PopulateSelection(
188  HdSelection::HighlightMode const& highlightMode,
189  SdfPath const &path,
190  VtIntArray const &instanceIndices,
191  HdSelectionSharedPtr const &result) override;
192 
193  // ---------------------------------------------------------------------- //
195  // ---------------------------------------------------------------------- //
196 
197  virtual HdVolumeFieldDescriptorVector
198  GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
199  UsdTimeCode time) const override;
200 
201  // ---------------------------------------------------------------------- //
203  // ---------------------------------------------------------------------- //
204 
205  virtual SdfPathVector GetDependPaths(SdfPath const &path) const override;
206 
207 protected:
208  virtual void _RemovePrim(SdfPath const& cachePath,
209  UsdImagingIndexProxy* index) override final;
210 
211 private:
212  struct _ProtoRprim;
213  struct _InstancerData;
214 
215  SdfPath _Populate(UsdPrim const& prim,
216  UsdImagingIndexProxy* index,
217  UsdImagingInstancerContext const* instancerContext);
218 
219  void _PopulatePrototype(int protoIndex,
220  _InstancerData& instrData,
221  UsdPrim const& protoRootPrim,
222  UsdImagingIndexProxy* index,
223  UsdImagingInstancerContext const *instancerContext);
224 
225  // Process prim removal and output a set of affected instancer paths is
226  // provided.
227  void _ProcessPrimRemoval(SdfPath const& cachePath,
228  UsdImagingIndexProxy* index,
229  SdfPathVector* instancersToReload);
230 
231  // Removes all instancer data, both locally and from the render index.
232  void _UnloadInstancer(SdfPath const& instancerPath,
233  UsdImagingIndexProxy* index);
234 
235  // Updates per-frame instance indices.
236  void _UpdateInstanceMap(SdfPath const &instancerPath,
237  UsdTimeCode time) const;
238 
239  // Updates per-frame instancer visibility.
240  void _UpdateInstancerVisibility(SdfPath const& instancerPath,
241  UsdTimeCode time) const;
242 
243  // Returns true if the instancer is visible, taking into account all
244  // parent instancers visibilities.
245  bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
246  const;
247 
248  // Update the dirty bits per-instancer. This is only executed once per
249  // instancer, this method uses the instancer mutex to avoid redundant work.
250  //
251  // Returns the instancer's dirty bits.
252  int _UpdateDirtyBits(UsdPrim const& instancerPrim) const;
253 
254  // Gets the associated _ProtoRprim for the given instancer and cache path.
255  _ProtoRprim const& _GetProtoRprim(SdfPath const& instancerPath,
256  SdfPath const& cachePath) const;
257 
258  // Gets the UsdPrim to use from the given _ProtoRprim.
259  const UsdPrim _GetProtoUsdPrim(_ProtoRprim const& proto) const;
260 
261  // Takes the transform in the value cache (this must exist before calling
262  // this method) and applies a corrective transform to 1) remove any
263  // transforms above the model root (root proto path) and 2) apply the
264  // instancer transform.
265  void _CorrectTransform(UsdPrim const& instancer,
266  UsdPrim const& proto,
267  SdfPath const& cachePath,
268  SdfPathVector const& protoPathChain,
269  UsdTimeCode time) const;
270 
271  // Similar to CorrectTransform, requires a visibility value exist in the
272  // ValueCache, removes any visibility opinions above the model root (proto
273  // root path) and applies the instancer visibility.
274  void _ComputeProtoVisibility(UsdPrim const& protoRoot,
275  UsdPrim const& protoGprim,
276  UsdTimeCode time,
277  bool* vis) const;
278 
279  // Computes the Purpose for the prototype, stopping at the proto root.
280  void _ComputeProtoPurpose(UsdPrim const& protoRoot,
281  UsdPrim const& protoGprim,
282  TfToken* purpose) const;
283 
284  /*
285  PointInstancer (InstancerData)
286  |
287  +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
288  | +-- ProtoRprim
289  | +-- ProtoRprim
290  |
291  +-- Prototype[1]------+-- ProtoRprim
292  | +-- ProtoRprim
293  .
294  .
295  */
296 
297  // A _Prototype represents a complete set of rprims for a given prototype
298  // path declared on the instancer;
299  struct _Prototype {
300  // The enabled flag is used to disable all rprims associated with a
301  // prototype; it marks them as invisible and disables data updates.
302  bool enabled;
303  // When requiresUpdate is false and enabled is true, it indicates that
304  // the rprim was drawn for a previous frame with the newly desired time;
305  // this is a cache hit and Usd-data fetch is skipped.
306  bool requiresUpdate;
307  // A vector of prototype indices that also index into the primvar data.
308  // All elements in this array can be dispatched as a single hardware
309  // draw call (though this is a detail of the renderer implementation).
310  VtIntArray indices;
311  // The root prototype path, typically the model root, which is not a
312  // gprim and not actually a prototype from Hydra's perspective.
313  SdfPath protoRootPath;
314  };
315  typedef boost::shared_ptr<_Prototype> _PrototypeSharedPtr;
316 
317  // A proto rprim represents a single rprim under a prototype root declared
318  // on the instancer. For example, a character may be targeted by the
319  // prototypes relationship, which will have many meshes, each mesh is
320  // represented as a proto rprim.
321  struct _ProtoRprim {
322  _ProtoRprim() : variabilityBits(0), visible(true) {}
323  // Each rprim will become a prototype "child" under the instancer.
324  // paths is a list of paths we had to hop across when resolving native
325  // USD instances.
326  SdfPathVector paths;
327  // The prim adapter for the actual prototype gprim.
328  UsdImagingPrimAdapterSharedPtr adapter;
329  // The prototype group that this rprim belongs to.
330  // Over time, as instances are animated, multiple copies of the
331  // prototype may be required to, for example, draw two different frames
332  // of animation. This ID maps the rprim its associated instance data
333  // over time.
334  _PrototypeSharedPtr prototype;
335  // Tracks the variability of the underlying adapter to avoid
336  // redundantly reading data. This value is stored as
337  // HdDirtyBits bit flags.
338  HdDirtyBits variabilityBits;
339  // When variabilityBits does not include HdChangeTracker::DirtyVisibility
340  // the visible field is the unvarying value for visibility.
341  bool visible;
342  };
343 
344  // Indexed by cachePath (each rprim has one entry)
345  typedef boost::unordered_map<SdfPath,
346  _ProtoRprim, SdfPath::Hash> _ProtoRPrimMap;
347 
348  // Map from usdPath -> cachePath(s), useful for change processing, when all
349  // we get is a usdPath.
350  typedef boost::unordered_map<SdfPath,
351  SdfPathVector, SdfPath::Hash> _UsdToCacheMap;
352 
353  // All data asscoiated with a given Instancer prim. PrimMap could
354  // technically be split out to avoid two lookups, however it seems cleaner
355  // to keep everything bundled up under the instancer path.
356  struct _InstancerData {
357  _InstancerData() {}
358  SdfPath parentInstancerCachePath;
359  _ProtoRPrimMap protoRprimMap;
360  _UsdToCacheMap usdToCacheMap;
361  std::vector<_PrototypeSharedPtr> prototypes;
362  std::mutex mutex;
363  HdDirtyBits dirtyBits;
364  bool visible;
365 
366  // _InstancerData::visible and _Prototype::indices are both caches,
367  // so we want to record what usd timecode they correspond to.
368  UsdTimeCode visibleTime;
369  UsdTimeCode indicesTime;
370  };
371 
372  // A map of instancer data, one entry per instancer prim that has been
373  // populated.
374  // Note: this is accessed in multithreaded code paths and must be protected
375  typedef boost::unordered_map<SdfPath /*instancerPath*/,
376  _InstancerData,
377  SdfPath::Hash> _InstancerDataMap;
378  mutable _InstancerDataMap _instancerData;
379 };
380 
381 
382 
383 PXR_NAMESPACE_CLOSE_SCOPE
384 
385 #endif // USDIMAGING_POINT_INSTANCER_ADAPTER_H
Tags for non-hierarchial subdiv surfaces.
Definition: subdivTags.h:41
virtual size_t SamplePrimvar(UsdPrim const &usdPrim, SdfPath const &cachePath, TfToken const &key, UsdTimeCode time, size_t maxNumSamples, float *sampleTimes, VtValue *sampleValues) override
Sample the primvar for the given prim.
Delegate support for UsdGeomPointInstancer.
virtual SdfPath Populate(UsdPrim const &prim, UsdImagingIndexProxy *index, UsdImagingInstancerContext const *instancerContext=NULL) override
Called to populate the RenderIndex for this UsdPrim.
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:53
This proxy class exposes a subset of the private Delegate API to PrimAdapters.
Definition: indexProxy.h:47
virtual VtIntArray GetInstanceIndices(SdfPath const &instancerPath, SdfPath const &protoRprimPath, UsdTimeCode time) override
Returns the instance index array for protoRprimPath, instanced by instancerPath.
virtual void ProcessPrimRemoval(SdfPath const &cachePath, UsdImagingIndexProxy *index) override
Removes all associated Rprims and dependencies from the render index without scheduling them for repo...
virtual PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Get the subdiv tags for this prim.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
virtual GfMatrix4d GetRelativeInstancerTransform(SdfPath const &instancerPath, SdfPath const &protoInstancerPath, UsdTimeCode time) const override
Returns the transform of protoInstancerPath relative to instancerPath.
virtual void TrackVariability(UsdPrim const &prim, SdfPath const &cachePath, HdDirtyBits *timeVaryingBits, UsdImagingInstancerContext const *instancerContext=NULL) const override
For the given prim, variability is detected and stored in timeVaryingBits.
virtual SdfPath GetPathForInstanceIndex(SdfPath const &protoCachePath, int protoIndex, int *instanceCountForThisLevel, int *instancerIndex, SdfPath *masterCachePath=NULL, SdfPathVector *instanceContext=NULL) override
Returns the usd path of the original instancer prim corresponding to the given instanced protoCachePa...
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:85
Base class for all PrimAdapters.
Definition: primAdapter.h:67
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
virtual size_t SampleInstancerTransform(UsdPrim const &instancerPrim, SdfPath const &instancerPath, UsdTimeCode time, size_t maxNumSamples, float *sampleTimes, GfMatrix4d *sampleValues) override
Sample the instancer transform for the given prim.
Object used by instancer prim adapters to pass along context about the instancer and instance prim to...
virtual SdfPathVector GetDependPaths(SdfPath const &path) const override
Returns the depending rprim paths which don&#39;t exist in descendants.
virtual void ProcessPrimResync(SdfPath const &cachePath, UsdImagingIndexProxy *index) override
When a PrimResync event occurs, the prim may have been deleted entirely, adapter plug-ins should over...
virtual HdDirtyBits ProcessPropertyChange(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &propertyName) override
Returns a bit mask of attributes to be updated, or HdChangeTracker::AllDirty if the entire prim must ...
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
virtual void UpdateForTime(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time, HdDirtyBits requestedBits, UsdImagingInstancerContext const *instancerContext=NULL) const override
Populates the cache for the given prim, time and requestedBits.
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.