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 
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 bool PopulateSelection(
264  HdSelection::HighlightMode const& highlightMode,
265  SdfPath const &cachePath,
266  UsdPrim const &usdPrim,
267  int const hydraInstanceIndex,
268  VtIntArray const &parentInstanceIndices,
269  HdSelectionSharedPtr const &result) const override;
270 
271  // ---------------------------------------------------------------------- //
273  // ---------------------------------------------------------------------- //
274 
275  virtual HdVolumeFieldDescriptorVector
276  GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
277  UsdTimeCode time) const override;
278 
279 protected:
280  virtual void _RemovePrim(SdfPath const& cachePath,
281  UsdImagingIndexProxy* index) override final;
282 
283 private:
284  struct _ProtoPrim;
285  struct _InstancerData;
286 
287  SdfPath _Populate(UsdPrim const& prim,
288  UsdImagingIndexProxy* index,
289  UsdImagingInstancerContext const* instancerContext);
290 
291  void _PopulatePrototype(int protoIndex,
292  _InstancerData& instrData,
293  UsdPrim const& protoRootPrim,
294  UsdImagingIndexProxy* index,
295  UsdImagingInstancerContext const *instancerContext);
296 
297  // Process prim removal and output a set of affected instancer paths is
298  // provided.
299  void _ProcessPrimRemoval(SdfPath const& cachePath,
300  UsdImagingIndexProxy* index,
301  SdfPathVector* instancersToReload);
302 
303  // Removes all instancer data, both locally and from the render index.
304  void _UnloadInstancer(SdfPath const& instancerPath,
305  UsdImagingIndexProxy* index);
306 
307  // Updates per-frame instancer visibility.
308  void _UpdateInstancerVisibility(SdfPath const& instancerPath,
309  _InstancerData const& instrData,
310  UsdTimeCode time) const;
311 
312  // Returns true if the instancer is visible, taking into account all
313  // parent instancers visibilities.
314  bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
315  const;
316 
317  // Gets the associated _ProtoPrim for the given instancer and cache path.
318  _ProtoPrim const& _GetProtoPrim(SdfPath const& instancerPath,
319  SdfPath const& cachePath) const;
320 
321  // Gets the associated _ProtoPrim and instancerContext if cachePath is a
322  // child path and returns \c true, otherwise returns \c false.
323  //
324  // Note that the returned instancer context may not be as fully featured as
325  // your needs may be.
326  bool _GetProtoPrimForChild(
327  UsdPrim const& usdPrim,
328  SdfPath const& cachePath,
329  _ProtoPrim const** proto,
330  UsdImagingInstancerContext* ctx) const;
331 
332  // Gets the UsdPrim to use from the given _ProtoPrim.
333  const UsdPrim _GetProtoUsdPrim(_ProtoPrim const& proto) const;
334 
335  // Takes the transform applies a corrective transform to 1) remove any
336  // transforms above the model root (root proto path) and 2) apply the
337  // instancer transform.
338  GfMatrix4d _CorrectTransform(UsdPrim const& instancer,
339  UsdPrim const& proto,
340  SdfPath const& cachePath,
341  SdfPathVector const& protoPathChain,
342  GfMatrix4d const& inTransform,
343  UsdTimeCode time) const;
344 
345  // Similar to CorrectTransform, requires a visibility value exist in the
346  // ValueCache, removes any visibility opinions above the model root (proto
347  // root path) and applies the instancer visibility.
348  void _ComputeProtoVisibility(UsdPrim const& protoRoot,
349  UsdPrim const& protoGprim,
350  UsdTimeCode time,
351  bool* vis) const;
352 
353  /*
354  PointInstancer (InstancerData)
355  |
356  +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
357  | +-- ProtoRprim
358  | +-- ProtoRprim
359  |
360  +-- Prototype[1]------+-- ProtoRprim
361  | +-- ProtoRprim
362  .
363  .
364  */
365 
366  // A proto prim represents a single populated prim under a prototype root
367  // declared on the instancer. For example, a character may be targeted
368  // by the prototypes relationship; it will have many meshes, and each
369  // mesh is represented as a separate proto prim.
370  struct _ProtoPrim {
371  _ProtoPrim() : variabilityBits(0), visible(true) {}
372  // Each prim will become a prototype "child" under the instancer.
373  // paths is a list of paths we had to hop across when resolving native
374  // USD instances.
375  SdfPathVector paths;
376  // The prim adapter for the actual prototype prim.
377  UsdImagingPrimAdapterSharedPtr adapter;
378  // The root prototype path, typically the model root, which is a subtree
379  // and might contain several imageable prims.
380  SdfPath protoRootPath;
381  // Tracks the variability of the underlying adapter to avoid
382  // redundantly reading data. This value is stored as
383  // HdDirtyBits bit flags.
384  // XXX: This is mutable so we can set it in TrackVariability.
385  mutable HdDirtyBits variabilityBits;
386  // When variabilityBits does not include HdChangeTracker::DirtyVisibility
387  // the visible field is the unvarying value for visibility.
388  // XXX: This is mutable so we can set it in TrackVariability.
389  mutable bool visible;
390  };
391 
392  // Indexed by cachePath (each prim has one entry)
393  typedef std::unordered_map<SdfPath, _ProtoPrim, SdfPath::Hash> _ProtoPrimMap;
394 
395  // All data associated with a given Instancer prim. PrimMap could
396  // technically be split out to avoid two lookups, however it seems cleaner
397  // to keep everything bundled up under the instancer path.
398  struct _InstancerData {
399  _InstancerData() {}
400  SdfPath parentInstancerCachePath;
401  _ProtoPrimMap protoPrimMap;
402  SdfPathVector prototypePaths;
403 
404  using PathToIndexMap = TfDenseHashMap<SdfPath, size_t, SdfPath::Hash>;
405  PathToIndexMap prototypePathIndices;
406 
407 
408  // XXX: We keep a bunch of state around visibility that's set in
409  // TrackVariability and UpdateForTime. "visible", and "visibleTime"
410  // (the cache key for visible) are set in UpdateForTime and guarded
411  // by "mutex".
412  mutable std::mutex mutex;
413  mutable bool variableVisibility;
414  mutable bool visible;
415  mutable UsdTimeCode visibleTime;
416  };
417 
418  // A map of instancer data, one entry per instancer prim that has been
419  // populated.
420  // Note: this is accessed in multithreaded code paths and must be protected
421  typedef std::unordered_map<SdfPath /*instancerPath*/,
422  _InstancerData,
423  SdfPath::Hash> _InstancerDataMap;
424  _InstancerDataMap _instancerData;
425 };
426 
427 
428 
429 PXR_NAMESPACE_CLOSE_SCOPE
430 
431 #endif // PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
Tags for non-hierarchial subdiv surfaces.
Definition: subdivTags.h:41
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 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:65
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 &quot;Prim&quot; as ...
Definition: prim.h:132
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
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:168
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.