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 public:
44 
46  : BaseAdapter()
47  { }
49 
50  virtual SdfPath Populate(
51  UsdPrim const& prim,
52  UsdImagingIndexProxy* index,
53  UsdImagingInstancerContext const* instancerContext = NULL) override;
54 
55  virtual bool ShouldCullChildren() const override;
56 
57  virtual bool IsInstancerAdapter() const override;
58 
59  // ---------------------------------------------------------------------- //
61  // ---------------------------------------------------------------------- //
62 
63  virtual void TrackVariability(UsdPrim const& prim,
64  SdfPath const& cachePath,
65  HdDirtyBits* timeVaryingBits,
67  instancerContext = NULL) const override;
68 
69  virtual void UpdateForTime(UsdPrim const& prim,
70  SdfPath const& cachePath,
71  UsdTimeCode time,
72  HdDirtyBits requestedBits,
74  instancerContext = NULL) const override;
75 
76  // ---------------------------------------------------------------------- //
78  // ---------------------------------------------------------------------- //
79 
80  virtual HdDirtyBits ProcessPropertyChange(UsdPrim const& prim,
81  SdfPath const& cachePath,
82  TfToken const& propertyName) override;
83 
84  virtual void ProcessPrimResync(SdfPath const& cachePath,
85  UsdImagingIndexProxy* index) override;
86 
87  virtual void ProcessPrimRemoval(SdfPath const& cachePath,
88  UsdImagingIndexProxy* index) override;
89 
90  virtual void MarkDirty(UsdPrim const& prim,
91  SdfPath const& cachePath,
92  HdDirtyBits dirty,
93  UsdImagingIndexProxy* index) override;
94 
95  virtual void MarkRefineLevelDirty(UsdPrim const& prim,
96  SdfPath const& cachePath,
97  UsdImagingIndexProxy* index) override;
98 
99  virtual void MarkReprDirty(UsdPrim const& prim,
100  SdfPath const& cachePath,
101  UsdImagingIndexProxy* index) override;
102 
103  virtual void MarkCullStyleDirty(UsdPrim const& prim,
104  SdfPath const& cachePath,
105  UsdImagingIndexProxy* index) override;
106 
107  virtual void MarkRenderTagDirty(UsdPrim const& prim,
108  SdfPath const& cachePath,
109  UsdImagingIndexProxy* index) override;
110 
111  virtual void MarkTransformDirty(UsdPrim const& prim,
112  SdfPath const& cachePath,
113  UsdImagingIndexProxy* index) override;
114 
115  virtual void MarkVisibilityDirty(UsdPrim const& prim,
116  SdfPath const& cachePath,
117  UsdImagingIndexProxy* index) override;
118 
119 
120 
121  // ---------------------------------------------------------------------- //
123  // ---------------------------------------------------------------------- //
124 
125  virtual SdfPath GetPathForInstanceIndex(SdfPath const &protoCachePath,
126  int protoIndex,
127  int *instanceCountForThisLevel,
128  int *instancerIndex,
129  SdfPath *masterCachePath = NULL,
130  SdfPathVector *
131  instanceContext = NULL) override;
132 
133  virtual size_t
134  SampleInstancerTransform(UsdPrim const& instancerPrim,
135  SdfPath const& instancerPath,
136  UsdTimeCode time,
137  size_t maxNumSamples,
138  float *sampleTimes,
139  GfMatrix4d *sampleValues) override;
140 
141  virtual size_t
142  SampleTransform(UsdPrim const& prim,
143  SdfPath const& cachePath,
144  UsdTimeCode time,
145  size_t maxNumSamples,
146  float *sampleTimes,
147  GfMatrix4d *sampleValues) override;
148 
149  virtual size_t
150  SamplePrimvar(UsdPrim const& usdPrim,
151  SdfPath const& cachePath,
152  TfToken const& key,
153  UsdTimeCode time,
154  size_t maxNumSamples,
155  float *sampleTimes,
156  VtValue *sampleValues) override;
157 
158  virtual PxOsdSubdivTags GetSubdivTags(UsdPrim const& usdPrim,
159  SdfPath const& cachePath,
160  UsdTimeCode time) const override;
161 
162  // ---------------------------------------------------------------------- //
164  // ---------------------------------------------------------------------- //
165 
167  SdfPath const &instancerPath,
168  SdfPath const &protoInstancerPath,
169  UsdTimeCode time) const override;
170 
171  // ---------------------------------------------------------------------- //
173  // ---------------------------------------------------------------------- //
174 
175  virtual bool PopulateSelection(
176  HdSelection::HighlightMode const& highlightMode,
177  SdfPath const &cachePath,
178  UsdPrim const &usdPrim,
179  VtIntArray const &instanceIndices,
180  HdSelectionSharedPtr const &result) override;
181 
182  virtual SdfPath GetPathForInstanceIndex(SdfPath const &instancerCachePath,
183  SdfPath const &protoCachePath,
184  int protoIndex,
185  int *instanceCountForThisLevel,
186  int *instancerIndex,
187  SdfPath *masterCachePath,
188  SdfPathVector *instanceContext) override;
189 
190  // ---------------------------------------------------------------------- //
192  // ---------------------------------------------------------------------- //
193 
194  virtual HdVolumeFieldDescriptorVector
195  GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
196  UsdTimeCode time) const override;
197 
198 protected:
199  virtual void _RemovePrim(SdfPath const& cachePath,
200  UsdImagingIndexProxy* index) override final;
201 
202 private:
203  struct _ProtoPrim;
204  struct _InstancerData;
205 
206  SdfPath _Populate(UsdPrim const& prim,
207  UsdImagingIndexProxy* index,
208  UsdImagingInstancerContext const* instancerContext);
209 
210  void _PopulatePrototype(int protoIndex,
211  _InstancerData& instrData,
212  UsdPrim const& protoRootPrim,
213  UsdImagingIndexProxy* index,
214  UsdImagingInstancerContext const *instancerContext);
215 
216  // Process prim removal and output a set of affected instancer paths is
217  // provided.
218  void _ProcessPrimRemoval(SdfPath const& cachePath,
219  UsdImagingIndexProxy* index,
220  SdfPathVector* instancersToReload);
221 
222  // Removes all instancer data, both locally and from the render index.
223  void _UnloadInstancer(SdfPath const& instancerPath,
224  UsdImagingIndexProxy* index);
225 
226  // Computes per-frame instance indices.
227  typedef std::unordered_map<SdfPath, VtIntArray, SdfPath::Hash> _InstanceMap;
228  _InstanceMap _ComputeInstanceMap(SdfPath const& instancerPath,
229  _InstancerData const& instrData,
230  UsdTimeCode time) const;
231 
232  // Updates per-frame instancer visibility.
233  void _UpdateInstancerVisibility(SdfPath const& instancerPath,
234  _InstancerData const& instrData,
235  UsdTimeCode time) const;
236 
237  // Returns true if the instancer is visible, taking into account all
238  // parent instancers visibilities.
239  bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
240  const;
241 
242  // Gets the associated _ProtoPrim for the given instancer and cache path.
243  _ProtoPrim const& _GetProtoPrim(SdfPath const& instancerPath,
244  SdfPath const& cachePath) const;
245 
246  // Gets the UsdPrim to use from the given _ProtoPrim.
247  const UsdPrim _GetProtoUsdPrim(_ProtoPrim const& proto) const;
248 
249  // Takes the transform in the value cache (this must exist before calling
250  // this method) and applies a corrective transform to 1) remove any
251  // transforms above the model root (root proto path) and 2) apply the
252  // instancer transform.
253  void _CorrectTransform(UsdPrim const& instancer,
254  UsdPrim const& proto,
255  SdfPath const& cachePath,
256  SdfPathVector const& protoPathChain,
257  UsdTimeCode time) const;
258 
259  // Similar to CorrectTransform, requires a visibility value exist in the
260  // ValueCache, removes any visibility opinions above the model root (proto
261  // root path) and applies the instancer visibility.
262  void _ComputeProtoVisibility(UsdPrim const& protoRoot,
263  UsdPrim const& protoGprim,
264  UsdTimeCode time,
265  bool* vis) const;
266 
267  // Computes the Purpose for the prototype, stopping at the proto root.
268  void _ComputeProtoPurpose(UsdPrim const& protoRoot,
269  UsdPrim const& protoGprim,
270  TfToken* purpose) const;
271 
272  /*
273  PointInstancer (InstancerData)
274  |
275  +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
276  | +-- ProtoRprim
277  | +-- ProtoRprim
278  |
279  +-- Prototype[1]------+-- ProtoRprim
280  | +-- ProtoRprim
281  .
282  .
283  */
284 
285  // A proto prim represents a single populated prim under a prototype root
286  // declared on the instancer. For example, a character may be targeted
287  // by the prototypes relationship; it will have many meshes, and each
288  // mesh is represented as a separate proto prim.
289  struct _ProtoPrim {
290  _ProtoPrim() : variabilityBits(0), visible(true) {}
291  // Each prim will become a prototype "child" under the instancer.
292  // paths is a list of paths we had to hop across when resolving native
293  // USD instances.
294  SdfPathVector paths;
295  // The prim adapter for the actual prototype prim.
296  UsdImagingPrimAdapterSharedPtr adapter;
297  // The root prototype path, typically the model root, which is a subtree
298  // and might contain several imageable prims.
299  SdfPath protoRootPath;
300  // Tracks the variability of the underlying adapter to avoid
301  // redundantly reading data. This value is stored as
302  // HdDirtyBits bit flags.
303  // XXX: This is mutable so we can set it in TrackVariability.
304  mutable HdDirtyBits variabilityBits;
305  // When variabilityBits does not include HdChangeTracker::DirtyVisibility
306  // the visible field is the unvarying value for visibility.
307  // XXX: This is mutable so we can set it in TrackVariability.
308  mutable bool visible;
309  };
310 
311  // Indexed by cachePath (each prim has one entry)
312  typedef std::unordered_map<SdfPath, _ProtoPrim, SdfPath::Hash> _ProtoPrimMap;
313 
314  // All data asscoiated with a given Instancer prim. PrimMap could
315  // technically be split out to avoid two lookups, however it seems cleaner
316  // to keep everything bundled up under the instancer path.
317  struct _InstancerData {
318  _InstancerData() {}
319  SdfPath parentInstancerCachePath;
320  _ProtoPrimMap protoPrimMap;
321  SdfPathVector prototypePaths;
322 
323  // XXX: We keep a bunch of state around visibility that's set in
324  // TrackVariability and UpdateForTime. "visible", and "visibleTime"
325  // (the cache key for visible) are set in UpdateForTime and guarded
326  // by "mutex".
327  mutable std::mutex mutex;
328  mutable bool variableVisibility;
329  mutable bool visible;
330  mutable UsdTimeCode visibleTime;
331  };
332 
333  // A map of instancer data, one entry per instancer prim that has been
334  // populated.
335  // Note: this is accessed in multithreaded code paths and must be protected
336  typedef std::unordered_map<SdfPath /*instancerPath*/,
337  _InstancerData,
338  SdfPath::Hash> _InstancerDataMap;
339  _InstancerDataMap _instancerData;
340 };
341 
342 
343 
344 PXR_NAMESPACE_CLOSE_SCOPE
345 
346 #endif // PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
Tags for non-hierarchial subdiv surfaces.
Definition: subdivTags.h:41
virtual 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.
Delegate support for UsdGeomPointInstancer.
virtual SdfPath Populate(UsdPrim const &prim, UsdImagingIndexProxy *index, UsdImagingInstancerContext const *instancerContext=NULL) override
Called to populate the RenderIndex for this UsdPrim.
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:53
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...
virtual PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Get the subdiv tags for this prim.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
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.
virtual SdfPath GetPathForInstanceIndex(SdfPath const &protoCachePath, int protoIndex, int *instanceCountForThisLevel, int *instancerIndex, SdfPath *masterCachePath=NULL, SdfPathVector *instanceContext=NULL) override
Returns the usd path of the original instancer prim corresponding to the given instanced protoCachePa...
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
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
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a &quot;Prim&quot; as ...
Definition: prim.h:131
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
virtual 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.
Object used by instancer prim adapters to pass along context about the instancer and instance prim to...
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:182
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.
virtual 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.