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