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 
28 
29 #include "pxr/pxr.h"
31 #include "pxr/usdImaging/usdImaging/version.h"
34 
35 #include <mutex>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 
43 {
44 public:
46 
48  : BaseAdapter()
49  { }
51 
52  virtual SdfPath Populate(
53  UsdPrim const& prim,
54  UsdImagingIndexProxy* index,
55  UsdImagingInstancerContext const* instancerContext = nullptr) 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)
85  override;
86 
87  virtual void ProcessPrimResync(SdfPath const& cachePath,
88  UsdImagingIndexProxy* index) override;
89 
90  virtual void ProcessPrimRemoval(SdfPath const& cachePath,
91  UsdImagingIndexProxy* index) override;
92 
93  virtual void MarkDirty(UsdPrim const& prim,
94  SdfPath const& cachePath,
95  HdDirtyBits dirty,
96  UsdImagingIndexProxy* index) override;
97 
98  virtual void MarkRefineLevelDirty(UsdPrim const& prim,
99  SdfPath const& cachePath,
100  UsdImagingIndexProxy* index) override;
101 
102  virtual void MarkReprDirty(UsdPrim const& prim,
103  SdfPath const& cachePath,
104  UsdImagingIndexProxy* index) override;
105 
106  virtual void MarkCullStyleDirty(UsdPrim const& prim,
107  SdfPath const& cachePath,
108  UsdImagingIndexProxy* index) override;
109 
110  virtual void MarkRenderTagDirty(UsdPrim const& prim,
111  SdfPath const& cachePath,
112  UsdImagingIndexProxy* index) override;
113 
114  virtual void MarkTransformDirty(UsdPrim const& prim,
115  SdfPath const& cachePath,
116  UsdImagingIndexProxy* index) override;
117 
118  virtual void MarkVisibilityDirty(UsdPrim const& prim,
119  SdfPath const& cachePath,
120  UsdImagingIndexProxy* index) override;
121 
122  // ---------------------------------------------------------------------- //
124  // ---------------------------------------------------------------------- //
125 
126  GfMatrix4d GetInstancerTransform(UsdPrim const& instancerPrim,
127  SdfPath const& instancerPath,
128  UsdTimeCode time) const override;
129 
130  size_t SampleInstancerTransform(UsdPrim const& instancerPrim,
131  SdfPath const& instancerPath,
132  UsdTimeCode time,
133  size_t maxNumSamples,
134  float *sampleTimes,
135  GfMatrix4d *sampleValues) override;
136 
138  UsdPrim const& usdPrim,
139  SdfPath const& cachePath) const override;
140 
141  SdfPathVector GetInstancerPrototypes(
142  UsdPrim const& usdPrim,
143  SdfPath const& cachePath) const override;
144 
145  GfMatrix4d GetTransform(UsdPrim const& prim,
146  SdfPath const& cachePath,
147  UsdTimeCode time,
148  bool ignoreRootTransform = false) const override;
149 
150  size_t SampleTransform(UsdPrim const& prim,
151  SdfPath const& cachePath,
152  UsdTimeCode time,
153  size_t maxNumSamples,
154  float *sampleTimes,
155  GfMatrix4d *sampleValues) override;
156 
157  size_t SamplePrimvar(UsdPrim const& usdPrim,
158  SdfPath const& cachePath,
159  TfToken const& key,
160  UsdTimeCode time,
161  size_t maxNumSamples,
162  float *sampleTimes,
163  VtValue *sampleValues,
164  VtIntArray *sampleIndices) override;
165 
166  PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
167  SdfPath const& cachePath,
168  UsdTimeCode time) const override;
169 
170  bool GetVisible(UsdPrim const& prim,
171  SdfPath const& cachePath,
172  UsdTimeCode time) const override;
173 
175  UsdPrim const& usdPrim,
176  SdfPath const& cachePath,
177  TfToken const& instanceInheritablePurpose) const override;
178 
179  VtValue GetTopology(UsdPrim const& prim,
180  SdfPath const& cachePath,
181  UsdTimeCode time) const override;
182 
183  HdCullStyle GetCullStyle(UsdPrim const& prim,
184  SdfPath const& cachePath,
185  UsdTimeCode time) const override;
186 
187  GfRange3d GetExtent(UsdPrim const& usdPrim,
188  SdfPath const& cachePath,
189  UsdTimeCode time) const override;
190 
191  bool GetDoubleSided(UsdPrim const& usdPrim,
192  SdfPath const& cachePath,
193  UsdTimeCode time) const override;
194 
195 
196  SdfPath GetMaterialId(UsdPrim const& prim,
197  SdfPath const& cachePath,
198  UsdTimeCode time) const override;
199 
200  HdExtComputationInputDescriptorVector
201  GetExtComputationInputs(UsdPrim const& prim,
202  SdfPath const& cachePath,
203  const UsdImagingInstancerContext* instancerContext)
204  const override;
205 
206  HdExtComputationOutputDescriptorVector
207  GetExtComputationOutputs(UsdPrim const& prim,
208  SdfPath const& cachePath,
209  const UsdImagingInstancerContext* instancerContext)
210  const override;
211 
212  HdExtComputationPrimvarDescriptorVector
213  GetExtComputationPrimvars(
214  UsdPrim const& prim,
215  SdfPath const& cachePath,
216  HdInterpolation interpolation,
217  const UsdImagingInstancerContext* instancerContext) const override;
218 
219  VtValue
220  GetExtComputationInput(
221  UsdPrim const& prim,
222  SdfPath const& cachePath,
223  TfToken const& name,
224  UsdTimeCode time,
225  const UsdImagingInstancerContext* instancerContext) const override;
226 
227  std::string
228  GetExtComputationKernel(
229  UsdPrim const& prim,
230  SdfPath const& cachePath,
231  const UsdImagingInstancerContext* instancerContext) const override;
232 
233  VtValue
234  GetInstanceIndices(UsdPrim const& instancerPrim,
235  SdfPath const& instancerCachePath,
236  SdfPath const& prototypeCachePath,
237  UsdTimeCode time) const override;
238 
239  VtValue Get(UsdPrim const& prim,
240  SdfPath const& cachePath,
241  TfToken const& key,
242  UsdTimeCode time,
243  VtIntArray *outIndices) const override;
244 
245  // ---------------------------------------------------------------------- //
247  // ---------------------------------------------------------------------- //
248 
250  SdfPath const &instancerPath,
251  SdfPath const &protoInstancerPath,
252  UsdTimeCode time) const override;
253 
254  // ---------------------------------------------------------------------- //
256  // ---------------------------------------------------------------------- //
257 
258  virtual SdfPath GetScenePrimPath(
259  SdfPath const& cachePath,
260  int instanceIndex,
261  HdInstancerContext *instancerContext) const override;
262 
263  virtual SdfPathVector GetScenePrimPaths(
264  SdfPath const& cachePath,
265  std::vector<int> const& instanceIndices,
266  std::vector<HdInstancerContext> *instancerCtxs) const override;
267 
268  virtual bool PopulateSelection(
269  HdSelection::HighlightMode const& highlightMode,
270  SdfPath const &cachePath,
271  UsdPrim const &usdPrim,
272  int const hydraInstanceIndex,
273  VtIntArray const &parentInstanceIndices,
274  HdSelectionSharedPtr const &result) const override;
275 
276  // ---------------------------------------------------------------------- //
278  // ---------------------------------------------------------------------- //
279 
280  virtual HdVolumeFieldDescriptorVector
281  GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
282  UsdTimeCode time) const override;
283 
284 protected:
285  virtual void _RemovePrim(SdfPath const& cachePath,
286  UsdImagingIndexProxy* index) override final;
287 
288 private:
289  struct _ProtoPrim;
290  struct _InstancerData;
291 
292  SdfPath _Populate(UsdPrim const& prim,
293  UsdImagingIndexProxy* index,
294  UsdImagingInstancerContext const* instancerContext);
295 
296  void _PopulatePrototype(int protoIndex,
297  _InstancerData& instrData,
298  UsdPrim const& protoRootPrim,
299  UsdImagingIndexProxy* index,
300  UsdImagingInstancerContext const *instancerContext);
301 
302  // Process prim removal and output a set of affected instancer paths is
303  // provided.
304  void _ProcessPrimRemoval(SdfPath const& cachePath,
305  UsdImagingIndexProxy* index,
306  SdfPathVector* instancersToReload);
307 
308  // Removes all instancer data, both locally and from the render index.
309  void _UnloadInstancer(SdfPath const& instancerPath,
310  UsdImagingIndexProxy* index);
311 
312  // Updates per-frame instancer visibility.
313  void _UpdateInstancerVisibility(SdfPath const& instancerPath,
314  _InstancerData const& instrData,
315  UsdTimeCode time) const;
316 
317  // Returns true if the instancer is visible, taking into account all
318  // parent instancers visibilities.
319  bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
320  const;
321 
322  // Gets the associated _ProtoPrim for the given instancer and cache path.
323  _ProtoPrim const& _GetProtoPrim(SdfPath const& instancerPath,
324  SdfPath const& cachePath) const;
325 
326  // Gets the associated _ProtoPrim and instancerContext if cachePath is a
327  // child path and returns \c true, otherwise returns \c false.
328  //
329  // Note that the returned instancer context may not be as fully featured as
330  // your needs may be.
331  bool _GetProtoPrimForChild(
332  UsdPrim const& usdPrim,
333  SdfPath const& cachePath,
334  _ProtoPrim const** proto,
335  UsdImagingInstancerContext* ctx) const;
336 
337  // Gets the UsdPrim to use from the given _ProtoPrim.
338  const UsdPrim _GetProtoUsdPrim(_ProtoPrim const& proto) const;
339 
340  // Takes the transform applies a corrective transform to 1) remove any
341  // transforms above the model root (root proto path) and 2) apply the
342  // instancer transform.
343  GfMatrix4d _CorrectTransform(UsdPrim const& instancer,
344  UsdPrim const& proto,
345  SdfPath const& cachePath,
346  SdfPathVector const& protoPathChain,
347  GfMatrix4d const& inTransform,
348  UsdTimeCode time) const;
349 
350  // Similar to CorrectTransform, requires a visibility value exist in the
351  // ValueCache, removes any visibility opinions above the model root (proto
352  // root path) and applies the instancer visibility.
353  void _ComputeProtoVisibility(UsdPrim const& protoRoot,
354  UsdPrim const& protoGprim,
355  UsdTimeCode time,
356  bool* vis) const;
357 
358  /*
359  PointInstancer (InstancerData)
360  |
361  +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
362  | +-- ProtoRprim
363  | +-- ProtoRprim
364  |
365  +-- Prototype[1]------+-- ProtoRprim
366  | +-- ProtoRprim
367  .
368  .
369  */
370 
371  // A proto prim represents a single populated prim under a prototype root
372  // declared on the instancer. For example, a character may be targeted
373  // by the prototypes relationship; it will have many meshes, and each
374  // mesh is represented as a separate proto prim.
375  struct _ProtoPrim {
376  _ProtoPrim() : variabilityBits(0), visible(true) {}
377  // Each prim will become a prototype "child" under the instancer.
378  // paths is a list of paths we had to hop across when resolving native
379  // USD instances.
380  SdfPathVector paths;
381  // The prim adapter for the actual prototype prim.
382  UsdImagingPrimAdapterSharedPtr adapter;
383  // The root prototype path, typically the model root, which is a subtree
384  // and might contain several imageable prims.
385  SdfPath protoRootPath;
386  // Tracks the variability of the underlying adapter to avoid
387  // redundantly reading data. This value is stored as
388  // HdDirtyBits bit flags.
389  // XXX: This is mutable so we can set it in TrackVariability.
390  mutable HdDirtyBits variabilityBits;
391  // When variabilityBits does not include HdChangeTracker::DirtyVisibility
392  // the visible field is the unvarying value for visibility.
393  // XXX: This is mutable so we can set it in TrackVariability.
394  mutable bool visible;
395  };
396 
397  // Indexed by cachePath (each prim has one entry)
398  typedef std::unordered_map<SdfPath, _ProtoPrim, SdfPath::Hash> _ProtoPrimMap;
399 
400  // All data associated with a given Instancer prim. PrimMap could
401  // technically be split out to avoid two lookups, however it seems cleaner
402  // to keep everything bundled up under the instancer path.
403  struct _InstancerData {
404  _InstancerData() {}
405  SdfPath parentInstancerCachePath;
406  _ProtoPrimMap protoPrimMap;
407  SdfPathVector prototypePaths;
408 
409  using PathToIndexMap = TfDenseHashMap<SdfPath, size_t, SdfPath::Hash>;
410  PathToIndexMap prototypePathIndices;
411 
412 
413  // XXX: We keep a bunch of state around visibility that's set in
414  // TrackVariability and UpdateForTime. "visible", and "visibleTime"
415  // (the cache key for visible) are set in UpdateForTime and guarded
416  // by "mutex".
417  mutable std::mutex mutex;
418  mutable bool variableVisibility;
419  mutable bool visible;
420  mutable UsdTimeCode visibleTime;
421  };
422 
423  // A map of instancer data, one entry per instancer prim that has been
424  // populated.
425  // Note: this is accessed in multithreaded code paths and must be protected
426  typedef std::unordered_map<SdfPath /*instancerPath*/,
427  _InstancerData,
428  SdfPath::Hash> _InstancerDataMap;
429  _InstancerDataMap _instancerData;
430 };
431 
432 
433 
434 PXR_NAMESPACE_CLOSE_SCOPE
435 
436 #endif // PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
Tags for non-hierarchial subdiv surfaces.
Definition: subdivTags.h:43
Basic type: 3-dimensional floating point range.
Definition: range3d.h:64
GfMatrix4d GetTransform(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time, bool ignoreRootTransform=false) const override
Fetches the transform for the given prim at the given time from a pre-computed cache of prim transfor...
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.
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.
Delegate support for UsdGeomPointInstancer.
VtValue Get(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &key, UsdTimeCode time, VtIntArray *outIndices) const override
Gets the value of the parameter named key for the given prim (which has the given cache path) and giv...
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:55
This proxy class exposes a subset of the private Delegate API to PrimAdapters.
Definition: indexProxy.h:47
virtual void ProcessPrimRemoval(SdfPath const &cachePath, UsdImagingIndexProxy *index) override
Removes all associated Rprims and dependencies from the render index without scheduling them for repo...
bool GetDoubleSided(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Reads double-sided from the given prim. If not authored, returns false.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
virtual GfMatrix4d GetRelativeInstancerTransform(SdfPath const &instancerPath, SdfPath const &protoInstancerPath, UsdTimeCode time) const override
Returns the transform of protoInstancerPath relative to instancerPath.
virtual SdfPath GetScenePrimPath(SdfPath const &cachePath, int instanceIndex, HdInstancerContext *instancerContext) const override
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.
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
virtual SdfPath Populate(UsdPrim const &prim, UsdImagingIndexProxy *index, UsdImagingInstancerContext const *instancerContext=nullptr) override
Called to populate the RenderIndex for this UsdPrim.
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
bool GetVisible(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
Returns true if the given prim is visible, taking into account inherited visibility values.
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:132
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
GfRange3d GetExtent(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Reads the extent from the given prim.
size_t SamplePrimvar(UsdPrim const &usdPrim, SdfPath const &cachePath, TfToken const &key, UsdTimeCode time, size_t maxNumSamples, float *sampleTimes, VtValue *sampleValues, VtIntArray *sampleIndices) override
Sample the primvar for the given prim.
PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Get the subdiv tags for this prim.
GfMatrix4d GetInstancerTransform(UsdPrim const &instancerPrim, SdfPath const &instancerPath, UsdTimeCode time) const override
Get the instancer transform for the given prim.
SdfPath GetInstancerId(UsdPrim const &usdPrim, SdfPath const &cachePath) const override
Return the instancerId for this prim.
Object used by instancer prim adapters to pass along context about the instancer and instance prim to...
VtValue GetTopology(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
Gets the topology object of a specific Usd prim.
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...
SdfPathVector GetInstancerPrototypes(UsdPrim const &usdPrim, SdfPath const &cachePath) const override
Return the list of known prototypes of this prim.
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:166
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.
HdCullStyle GetCullStyle(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
Gets the cullstyle of a specific path in the scene graph.
TfToken GetPurpose(UsdPrim const &usdPrim, SdfPath const &cachePath, TfToken const &instanceInheritablePurpose) const override
Returns the purpose token for prim.