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  USDIMAGING_API
66  TfTokenVector GetImagingSubprims() override;
67 
68  USDIMAGING_API
69  TfToken GetImagingSubprimType(TfToken const& subprim) override;
70 
71  USDIMAGING_API
72  HdContainerDataSourceHandle GetImagingSubprimData(
73  TfToken const& subprim,
74  UsdPrim const& prim,
75  const UsdImagingDataSourceStageGlobals &stageGlobals) override;
76 
77  USDIMAGING_API
78  HdDataSourceLocatorSet InvalidateImagingSubprim(
79  TfToken const& subprim,
80  TfTokenVector const& properties) override;
81 
82  // ---------------------------------------------------------------------- //
84  // ---------------------------------------------------------------------- //
85 
86  virtual void TrackVariability(UsdPrim const& prim,
87  SdfPath const& cachePath,
88  HdDirtyBits* timeVaryingBits,
90  instancerContext = NULL) const override;
91 
92  virtual void UpdateForTime(UsdPrim const& prim,
93  SdfPath const& cachePath,
94  UsdTimeCode time,
95  HdDirtyBits requestedBits,
97  instancerContext = NULL) const override;
98 
99  // ---------------------------------------------------------------------- //
101  // ---------------------------------------------------------------------- //
102 
103  virtual HdDirtyBits ProcessPropertyChange(UsdPrim const& prim,
104  SdfPath const& cachePath,
105  TfToken const& propertyName)
106  override;
107 
108  virtual void ProcessPrimResync(SdfPath const& cachePath,
109  UsdImagingIndexProxy* index) override;
110 
111  virtual void ProcessPrimRemoval(SdfPath const& cachePath,
112  UsdImagingIndexProxy* index) override;
113 
114  virtual void MarkDirty(UsdPrim const& prim,
115  SdfPath const& cachePath,
116  HdDirtyBits dirty,
117  UsdImagingIndexProxy* index) override;
118 
119  virtual void MarkRefineLevelDirty(UsdPrim const& prim,
120  SdfPath const& cachePath,
121  UsdImagingIndexProxy* index) override;
122 
123  virtual void MarkReprDirty(UsdPrim const& prim,
124  SdfPath const& cachePath,
125  UsdImagingIndexProxy* index) override;
126 
127  virtual void MarkCullStyleDirty(UsdPrim const& prim,
128  SdfPath const& cachePath,
129  UsdImagingIndexProxy* index) override;
130 
131  virtual void MarkRenderTagDirty(UsdPrim const& prim,
132  SdfPath const& cachePath,
133  UsdImagingIndexProxy* index) override;
134 
135  virtual void MarkTransformDirty(UsdPrim const& prim,
136  SdfPath const& cachePath,
137  UsdImagingIndexProxy* index) override;
138 
139  virtual void MarkVisibilityDirty(UsdPrim const& prim,
140  SdfPath const& cachePath,
141  UsdImagingIndexProxy* index) override;
142 
143  // ---------------------------------------------------------------------- //
145  // ---------------------------------------------------------------------- //
146 
147  GfMatrix4d GetInstancerTransform(UsdPrim const& instancerPrim,
148  SdfPath const& instancerPath,
149  UsdTimeCode time) const override;
150 
151  size_t SampleInstancerTransform(UsdPrim const& instancerPrim,
152  SdfPath const& instancerPath,
153  UsdTimeCode time,
154  size_t maxNumSamples,
155  float *sampleTimes,
156  GfMatrix4d *sampleValues) override;
157 
159  UsdPrim const& usdPrim,
160  SdfPath const& cachePath) const override;
161 
162  SdfPathVector GetInstancerPrototypes(
163  UsdPrim const& usdPrim,
164  SdfPath const& cachePath) const override;
165 
166  GfMatrix4d GetTransform(UsdPrim const& prim,
167  SdfPath const& cachePath,
168  UsdTimeCode time,
169  bool ignoreRootTransform = false) const override;
170 
171  size_t SampleTransform(UsdPrim const& prim,
172  SdfPath const& cachePath,
173  UsdTimeCode time,
174  size_t maxNumSamples,
175  float *sampleTimes,
176  GfMatrix4d *sampleValues) override;
177 
178  size_t SamplePrimvar(UsdPrim const& usdPrim,
179  SdfPath const& cachePath,
180  TfToken const& key,
181  UsdTimeCode time,
182  size_t maxNumSamples,
183  float *sampleTimes,
184  VtValue *sampleValues,
185  VtIntArray *sampleIndices) override;
186 
187  PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
188  SdfPath const& cachePath,
189  UsdTimeCode time) const override;
190 
191  bool GetVisible(UsdPrim const& prim,
192  SdfPath const& cachePath,
193  UsdTimeCode time) const override;
194 
196  UsdPrim const& usdPrim,
197  SdfPath const& cachePath,
198  TfToken const& instanceInheritablePurpose) const override;
199 
200  VtValue GetTopology(UsdPrim const& prim,
201  SdfPath const& cachePath,
202  UsdTimeCode time) const override;
203 
204  HdCullStyle GetCullStyle(UsdPrim const& prim,
205  SdfPath const& cachePath,
206  UsdTimeCode time) const override;
207 
208  GfRange3d GetExtent(UsdPrim const& usdPrim,
209  SdfPath const& cachePath,
210  UsdTimeCode time) const override;
211 
212  bool GetDoubleSided(UsdPrim const& usdPrim,
213  SdfPath const& cachePath,
214  UsdTimeCode time) const override;
215 
216 
217  SdfPath GetMaterialId(UsdPrim const& prim,
218  SdfPath const& cachePath,
219  UsdTimeCode time) const override;
220 
221  HdExtComputationInputDescriptorVector
222  GetExtComputationInputs(UsdPrim const& prim,
223  SdfPath const& cachePath,
224  const UsdImagingInstancerContext* instancerContext)
225  const override;
226 
227  HdExtComputationOutputDescriptorVector
228  GetExtComputationOutputs(UsdPrim const& prim,
229  SdfPath const& cachePath,
230  const UsdImagingInstancerContext* instancerContext)
231  const override;
232 
233  HdExtComputationPrimvarDescriptorVector
234  GetExtComputationPrimvars(
235  UsdPrim const& prim,
236  SdfPath const& cachePath,
237  HdInterpolation interpolation,
238  const UsdImagingInstancerContext* instancerContext) const override;
239 
240  VtValue
241  GetExtComputationInput(
242  UsdPrim const& prim,
243  SdfPath const& cachePath,
244  TfToken const& name,
245  UsdTimeCode time,
246  const UsdImagingInstancerContext* instancerContext) const override;
247 
248  std::string
249  GetExtComputationKernel(
250  UsdPrim const& prim,
251  SdfPath const& cachePath,
252  const UsdImagingInstancerContext* instancerContext) const override;
253 
254  VtValue
255  GetInstanceIndices(UsdPrim const& instancerPrim,
256  SdfPath const& instancerCachePath,
257  SdfPath const& prototypeCachePath,
258  UsdTimeCode time) const override;
259 
260  VtValue Get(UsdPrim const& prim,
261  SdfPath const& cachePath,
262  TfToken const& key,
263  UsdTimeCode time,
264  VtIntArray *outIndices) const override;
265 
266  // ---------------------------------------------------------------------- //
268  // ---------------------------------------------------------------------- //
269 
271  SdfPath const &instancerPath,
272  SdfPath const &protoInstancerPath,
273  UsdTimeCode time) const override;
274 
275  // ---------------------------------------------------------------------- //
277  // ---------------------------------------------------------------------- //
278 
279  virtual SdfPath GetScenePrimPath(
280  SdfPath const& cachePath,
281  int instanceIndex,
282  HdInstancerContext *instancerContext) const override;
283 
284  virtual SdfPathVector GetScenePrimPaths(
285  SdfPath const& cachePath,
286  std::vector<int> const& instanceIndices,
287  std::vector<HdInstancerContext> *instancerCtxs) const override;
288 
289  virtual bool PopulateSelection(
290  HdSelection::HighlightMode const& highlightMode,
291  SdfPath const &cachePath,
292  UsdPrim const &usdPrim,
293  int const hydraInstanceIndex,
294  VtIntArray const &parentInstanceIndices,
295  HdSelectionSharedPtr const &result) const override;
296 
297  // ---------------------------------------------------------------------- //
299  // ---------------------------------------------------------------------- //
300 
301  virtual HdVolumeFieldDescriptorVector
302  GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
303  UsdTimeCode time) const override;
304 
305 protected:
306  virtual void _RemovePrim(SdfPath const& cachePath,
307  UsdImagingIndexProxy* index) override final;
308 
309 private:
310  struct _ProtoPrim;
311  struct _InstancerData;
312 
313  SdfPath _Populate(UsdPrim const& prim,
314  UsdImagingIndexProxy* index,
315  UsdImagingInstancerContext const* instancerContext);
316 
317  void _PopulatePrototype(int protoIndex,
318  _InstancerData& instrData,
319  UsdPrim const& protoRootPrim,
320  UsdImagingIndexProxy* index,
321  UsdImagingInstancerContext const *instancerContext);
322 
323  // Process prim removal and output a set of affected instancer paths is
324  // provided.
325  void _ProcessPrimRemoval(SdfPath const& cachePath,
326  UsdImagingIndexProxy* index,
327  SdfPathVector* instancersToReload);
328 
329  // Removes all instancer data, both locally and from the render index.
330  void _UnloadInstancer(SdfPath const& instancerPath,
331  UsdImagingIndexProxy* index);
332 
333  // Updates per-frame instancer visibility.
334  void _UpdateInstancerVisibility(SdfPath const& instancerPath,
335  _InstancerData const& instrData,
336  UsdTimeCode time) const;
337 
338  // Returns true if the instancer is visible, taking into account all
339  // parent instancers visibilities.
340  bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
341  const;
342 
343  // Gets the associated _ProtoPrim for the given instancer and cache path.
344  _ProtoPrim const& _GetProtoPrim(SdfPath const& instancerPath,
345  SdfPath const& cachePath) const;
346 
347  // Gets the associated _ProtoPrim and instancerContext if cachePath is a
348  // child path and returns \c true, otherwise returns \c false.
349  //
350  // Note that the returned instancer context may not be as fully featured as
351  // your needs may be.
352  bool _GetProtoPrimForChild(
353  UsdPrim const& usdPrim,
354  SdfPath const& cachePath,
355  _ProtoPrim const** proto,
356  UsdImagingInstancerContext* ctx) const;
357 
358  // Gets the UsdPrim to use from the given _ProtoPrim.
359  const UsdPrim _GetProtoUsdPrim(_ProtoPrim const& proto) const;
360 
361  // Takes the transform applies a corrective transform to 1) remove any
362  // transforms above the model root (root proto path) and 2) apply the
363  // instancer transform.
364  GfMatrix4d _CorrectTransform(UsdPrim const& instancer,
365  UsdPrim const& proto,
366  SdfPath const& cachePath,
367  SdfPathVector const& protoPathChain,
368  GfMatrix4d const& inTransform,
369  UsdTimeCode time) const;
370 
371  // Similar to CorrectTransform, requires a visibility value exist in the
372  // ValueCache, removes any visibility opinions above the model root (proto
373  // root path) and applies the instancer visibility.
374  void _ComputeProtoVisibility(UsdPrim const& protoRoot,
375  UsdPrim const& protoGprim,
376  UsdTimeCode time,
377  bool* vis) const;
378 
379  /*
380  PointInstancer (InstancerData)
381  |
382  +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
383  | +-- ProtoRprim
384  | +-- ProtoRprim
385  |
386  +-- Prototype[1]------+-- ProtoRprim
387  | +-- ProtoRprim
388  .
389  .
390  */
391 
392  // A proto prim represents a single populated prim under a prototype root
393  // declared on the instancer. For example, a character may be targeted
394  // by the prototypes relationship; it will have many meshes, and each
395  // mesh is represented as a separate proto prim.
396  struct _ProtoPrim {
397  _ProtoPrim() : variabilityBits(0), visible(true) {}
398  // Each prim will become a prototype "child" under the instancer.
399  // paths is a list of paths we had to hop across when resolving native
400  // USD instances.
401  SdfPathVector paths;
402  // The prim adapter for the actual prototype prim.
403  UsdImagingPrimAdapterSharedPtr adapter;
404  // The root prototype path, typically the model root, which is a subtree
405  // and might contain several imageable prims.
406  SdfPath protoRootPath;
407  // Tracks the variability of the underlying adapter to avoid
408  // redundantly reading data. This value is stored as
409  // HdDirtyBits bit flags.
410  // XXX: This is mutable so we can set it in TrackVariability.
411  mutable HdDirtyBits variabilityBits;
412  // When variabilityBits does not include HdChangeTracker::DirtyVisibility
413  // the visible field is the unvarying value for visibility.
414  // XXX: This is mutable so we can set it in TrackVariability.
415  mutable bool visible;
416  };
417 
418  // Indexed by cachePath (each prim has one entry)
419  typedef std::unordered_map<SdfPath, _ProtoPrim, SdfPath::Hash> _ProtoPrimMap;
420 
421  // All data associated with a given Instancer prim. PrimMap could
422  // technically be split out to avoid two lookups, however it seems cleaner
423  // to keep everything bundled up under the instancer path.
424  struct _InstancerData {
425  _InstancerData() {}
426  SdfPath parentInstancerCachePath;
427  _ProtoPrimMap protoPrimMap;
428  SdfPathVector prototypePaths;
429 
430  using PathToIndexMap = TfDenseHashMap<SdfPath, size_t, SdfPath::Hash>;
431  PathToIndexMap prototypePathIndices;
432 
433 
434  // XXX: We keep a bunch of state around visibility that's set in
435  // TrackVariability and UpdateForTime. "visible", and "visibleTime"
436  // (the cache key for visible) are set in UpdateForTime and guarded
437  // by "mutex".
438  mutable std::mutex mutex;
439  mutable bool variableVisibility;
440  mutable bool visible;
441  mutable UsdTimeCode visibleTime;
442  };
443 
444  // A map of instancer data, one entry per instancer prim that has been
445  // populated.
446  // Note: this is accessed in multithreaded code paths and must be protected
447  typedef std::unordered_map<SdfPath /*instancerPath*/,
448  _InstancerData,
449  SdfPath::Hash> _InstancerDataMap;
450  _InstancerDataMap _instancerData;
451 };
452 
453 
454 
455 PXR_NAMESPACE_CLOSE_SCOPE
456 
457 #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
This class is used as a context object with global stage information, that gets passed down to dataso...
Base class for all PrimAdapters.
Definition: primAdapter.h:67
Represents a set of data source locators closed under descendancy.
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:134
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
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.