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"
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 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 
42 {
43 public:
45 
47  : BaseAdapter()
48  { }
50 
51  virtual SdfPath Populate(
52  UsdPrim const& prim,
53  UsdImagingIndexProxy* index,
54  UsdImagingInstancerContext const* instancerContext = nullptr) override;
55 
56  virtual bool ShouldCullChildren() const override;
57 
58  virtual bool IsInstancerAdapter() const override;
59 
60  // ---------------------------------------------------------------------- //
62  // ---------------------------------------------------------------------- //
63 
64  virtual void TrackVariability(UsdPrim const& prim,
65  SdfPath const& cachePath,
66  HdDirtyBits* timeVaryingBits,
68  instancerContext = NULL) const override;
69 
70  virtual void UpdateForTime(UsdPrim const& prim,
71  SdfPath const& cachePath,
72  UsdTimeCode time,
73  HdDirtyBits requestedBits,
75  instancerContext = NULL) const override;
76 
77  // ---------------------------------------------------------------------- //
79  // ---------------------------------------------------------------------- //
80 
81  virtual HdDirtyBits ProcessPropertyChange(UsdPrim const& prim,
82  SdfPath const& cachePath,
83  TfToken const& propertyName)
84  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  // ---------------------------------------------------------------------- //
123  // ---------------------------------------------------------------------- //
124 
125  GfMatrix4d GetInstancerTransform(UsdPrim const& instancerPrim,
126  SdfPath const& instancerPath,
127  UsdTimeCode time) const override;
128 
129  size_t SampleInstancerTransform(UsdPrim const& instancerPrim,
130  SdfPath const& instancerPath,
131  UsdTimeCode time,
132  size_t maxNumSamples,
133  float *sampleTimes,
134  GfMatrix4d *sampleValues) override;
135 
136  GfMatrix4d GetTransform(UsdPrim const& prim,
137  SdfPath const& cachePath,
138  UsdTimeCode time,
139  bool ignoreRootTransform = false) const override;
140 
141  size_t SampleTransform(UsdPrim const& prim,
142  SdfPath const& cachePath,
143  UsdTimeCode time,
144  size_t maxNumSamples,
145  float *sampleTimes,
146  GfMatrix4d *sampleValues) override;
147 
148  size_t SamplePrimvar(UsdPrim const& usdPrim,
149  SdfPath const& cachePath,
150  TfToken const& key,
151  UsdTimeCode time,
152  size_t maxNumSamples,
153  float *sampleTimes,
154  VtValue *sampleValues) override;
155 
156  PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
157  SdfPath const& cachePath,
158  UsdTimeCode time) const override;
159 
160  bool GetVisible(UsdPrim const& prim,
161  SdfPath const& cachePath,
162  UsdTimeCode time) const override;
163 
165  UsdPrim const& usdPrim,
166  SdfPath const& cachePath,
167  TfToken const& instanceInheritablePurpose) const override;
168 
169  VtValue GetTopology(UsdPrim const& prim,
170  SdfPath const& cachePath,
171  UsdTimeCode time) const override;
172 
173  HdCullStyle GetCullStyle(UsdPrim const& prim,
174  SdfPath const& cachePath,
175  UsdTimeCode time) const override;
176 
177  GfRange3d GetExtent(UsdPrim const& usdPrim,
178  SdfPath const& cachePath,
179  UsdTimeCode time) const override;
180 
181  bool GetDoubleSided(UsdPrim const& usdPrim,
182  SdfPath const& cachePath,
183  UsdTimeCode time) const override;
184 
185 
186  SdfPath GetMaterialId(UsdPrim const& prim,
187  SdfPath const& cachePath,
188  UsdTimeCode time) const override;
189 
190  HdExtComputationInputDescriptorVector
191  GetExtComputationInputs(UsdPrim const& prim,
192  SdfPath const& cachePath,
193  const UsdImagingInstancerContext* instancerContext)
194  const override;
195 
196  HdExtComputationOutputDescriptorVector
197  GetExtComputationOutputs(UsdPrim const& prim,
198  SdfPath const& cachePath,
199  const UsdImagingInstancerContext* instancerContext)
200  const override;
201 
202  HdExtComputationPrimvarDescriptorVector
203  GetExtComputationPrimvars(
204  UsdPrim const& prim,
205  SdfPath const& cachePath,
206  HdInterpolation interpolation,
207  const UsdImagingInstancerContext* instancerContext) const override;
208 
209  VtValue
210  GetExtComputationInput(
211  UsdPrim const& prim,
212  SdfPath const& cachePath,
213  TfToken const& name,
214  UsdTimeCode time,
215  const UsdImagingInstancerContext* instancerContext) const override;
216 
217  std::string
218  GetExtComputationKernel(
219  UsdPrim const& prim,
220  SdfPath const& cachePath,
221  const UsdImagingInstancerContext* instancerContext) const override;
222 
223 
224  VtValue Get(UsdPrim const& prim,
225  SdfPath const& cachePath,
226  TfToken const& key,
227  UsdTimeCode time) const override;
228 
229  // ---------------------------------------------------------------------- //
231  // ---------------------------------------------------------------------- //
232 
234  SdfPath const &instancerPath,
235  SdfPath const &protoInstancerPath,
236  UsdTimeCode time) const override;
237 
238  // ---------------------------------------------------------------------- //
240  // ---------------------------------------------------------------------- //
241 
242  virtual SdfPath GetScenePrimPath(
243  SdfPath const& cachePath,
244  int instanceIndex,
245  HdInstancerContext *instancerContext) const override;
246 
247  virtual bool PopulateSelection(
248  HdSelection::HighlightMode const& highlightMode,
249  SdfPath const &cachePath,
250  UsdPrim const &usdPrim,
251  int const hydraInstanceIndex,
252  VtIntArray const &parentInstanceIndices,
253  HdSelectionSharedPtr const &result) const override;
254 
255  // ---------------------------------------------------------------------- //
257  // ---------------------------------------------------------------------- //
258 
259  virtual HdVolumeFieldDescriptorVector
260  GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
261  UsdTimeCode time) const override;
262 
263 protected:
264  virtual void _RemovePrim(SdfPath const& cachePath,
265  UsdImagingIndexProxy* index) override final;
266 
267 private:
268  struct _ProtoPrim;
269  struct _InstancerData;
270 
271  SdfPath _Populate(UsdPrim const& prim,
272  UsdImagingIndexProxy* index,
273  UsdImagingInstancerContext const* instancerContext);
274 
275  void _PopulatePrototype(int protoIndex,
276  _InstancerData& instrData,
277  UsdPrim const& protoRootPrim,
278  UsdImagingIndexProxy* index,
279  UsdImagingInstancerContext const *instancerContext);
280 
281  // Process prim removal and output a set of affected instancer paths is
282  // provided.
283  void _ProcessPrimRemoval(SdfPath const& cachePath,
284  UsdImagingIndexProxy* index,
285  SdfPathVector* instancersToReload);
286 
287  // Removes all instancer data, both locally and from the render index.
288  void _UnloadInstancer(SdfPath const& instancerPath,
289  UsdImagingIndexProxy* index);
290 
291  // Computes per-frame instance indices.
292  typedef std::unordered_map<SdfPath, VtIntArray, SdfPath::Hash> _InstanceMap;
293  _InstanceMap _ComputeInstanceMap(SdfPath const& instancerPath,
294  _InstancerData const& instrData,
295  UsdTimeCode time) const;
296 
297  // Updates per-frame instancer visibility.
298  void _UpdateInstancerVisibility(SdfPath const& instancerPath,
299  _InstancerData const& instrData,
300  UsdTimeCode time) const;
301 
302  // Returns true if the instancer is visible, taking into account all
303  // parent instancers visibilities.
304  bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
305  const;
306 
307  // Gets the associated _ProtoPrim for the given instancer and cache path.
308  _ProtoPrim const& _GetProtoPrim(SdfPath const& instancerPath,
309  SdfPath const& cachePath) const;
310 
311  // Gets the associated _ProtoPrim and instancerContext if cachePath is a
312  // child path and returns \c true, otherwise returns \c false.
313  //
314  // Note that the returned instancer context may not be as fully featured as
315  // your needs may be.
316  bool _GetProtoPrimForChild(
317  UsdPrim const& usdPrim,
318  SdfPath const& cachePath,
319  _ProtoPrim const** proto,
320  UsdImagingInstancerContext* ctx) const;
321 
322  // Gets the UsdPrim to use from the given _ProtoPrim.
323  const UsdPrim _GetProtoUsdPrim(_ProtoPrim const& proto) const;
324 
325  // Takes the transform applies a corrective transform to 1) remove any
326  // transforms above the model root (root proto path) and 2) apply the
327  // instancer transform.
328  GfMatrix4d _CorrectTransform(UsdPrim const& instancer,
329  UsdPrim const& proto,
330  SdfPath const& cachePath,
331  SdfPathVector const& protoPathChain,
332  GfMatrix4d const& inTransform,
333  UsdTimeCode time) const;
334 
335  // Similar to CorrectTransform, requires a visibility value exist in the
336  // ValueCache, removes any visibility opinions above the model root (proto
337  // root path) and applies the instancer visibility.
338  void _ComputeProtoVisibility(UsdPrim const& protoRoot,
339  UsdPrim const& protoGprim,
340  UsdTimeCode time,
341  bool* vis) const;
342 
343  /*
344  PointInstancer (InstancerData)
345  |
346  +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
347  | +-- ProtoRprim
348  | +-- ProtoRprim
349  |
350  +-- Prototype[1]------+-- ProtoRprim
351  | +-- ProtoRprim
352  .
353  .
354  */
355 
356  // A proto prim represents a single populated prim under a prototype root
357  // declared on the instancer. For example, a character may be targeted
358  // by the prototypes relationship; it will have many meshes, and each
359  // mesh is represented as a separate proto prim.
360  struct _ProtoPrim {
361  _ProtoPrim() : variabilityBits(0), visible(true) {}
362  // Each prim will become a prototype "child" under the instancer.
363  // paths is a list of paths we had to hop across when resolving native
364  // USD instances.
365  SdfPathVector paths;
366  // The prim adapter for the actual prototype prim.
367  UsdImagingPrimAdapterSharedPtr adapter;
368  // The root prototype path, typically the model root, which is a subtree
369  // and might contain several imageable prims.
370  SdfPath protoRootPath;
371  // Tracks the variability of the underlying adapter to avoid
372  // redundantly reading data. This value is stored as
373  // HdDirtyBits bit flags.
374  // XXX: This is mutable so we can set it in TrackVariability.
375  mutable HdDirtyBits variabilityBits;
376  // When variabilityBits does not include HdChangeTracker::DirtyVisibility
377  // the visible field is the unvarying value for visibility.
378  // XXX: This is mutable so we can set it in TrackVariability.
379  mutable bool visible;
380  };
381 
382  // Indexed by cachePath (each prim has one entry)
383  typedef std::unordered_map<SdfPath, _ProtoPrim, SdfPath::Hash> _ProtoPrimMap;
384 
385  // All data asscoiated with a given Instancer prim. PrimMap could
386  // technically be split out to avoid two lookups, however it seems cleaner
387  // to keep everything bundled up under the instancer path.
388  struct _InstancerData {
389  _InstancerData() {}
390  SdfPath parentInstancerCachePath;
391  _ProtoPrimMap protoPrimMap;
392  SdfPathVector prototypePaths;
393 
394  // XXX: We keep a bunch of state around visibility that's set in
395  // TrackVariability and UpdateForTime. "visible", and "visibleTime"
396  // (the cache key for visible) are set in UpdateForTime and guarded
397  // by "mutex".
398  mutable std::mutex mutex;
399  mutable bool variableVisibility;
400  mutable bool visible;
401  mutable UsdTimeCode visibleTime;
402  };
403 
404  // A map of instancer data, one entry per instancer prim that has been
405  // populated.
406  // Note: this is accessed in multithreaded code paths and must be protected
407  typedef std::unordered_map<SdfPath /*instancerPath*/,
408  _InstancerData,
409  SdfPath::Hash> _InstancerDataMap;
410  _InstancerDataMap _instancerData;
411 };
412 
413 
414 
415 PXR_NAMESPACE_CLOSE_SCOPE
416 
417 #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.
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:66
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.
PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Get the subdiv tags for this prim.
VtValue Get(UsdPrim const &prim, SdfPath const &cachePath, TfToken const &key, UsdTimeCode time) const override
Gets the value of the parameter named key for the given prim (which has the given cache path) and giv...
GfMatrix4d GetInstancerTransform(UsdPrim const &instancerPrim, SdfPath const &instancerPath, UsdTimeCode time) const override
Get the instancer transform for the given prim.
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.
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...
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.