Loading...
Searching...
No Matches
pointInstancerAdapter.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
8#define PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
9
11
12#include "pxr/usdImaging/usdImaging/api.h"
13#include "pxr/usdImaging/usdImaging/instanceablePrimAdapter.h"
14
15#include "pxr/usd/sdf/path.h"
16
18
19#include "pxr/pxr.h"
20
21#include <atomic>
22#include <mutex>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
29{
30public:
32
33 USDIMAGING_API
35 UsdPrim const& prim,
37 UsdImagingInstancerContext const* instancerContext = nullptr) override;
38
39 USDIMAGING_API
40 bool ShouldCullChildren() const override;
41
42 USDIMAGING_API
43 bool IsInstancerAdapter() const override;
44
45 // ---------------------------------------------------------------------- //
47 // ---------------------------------------------------------------------- //
48
49 USDIMAGING_API
50 TfTokenVector GetImagingSubprims(UsdPrim const& prim) override;
51
52 USDIMAGING_API
53 TfToken GetImagingSubprimType(
54 UsdPrim const& prim,
55 TfToken const& subprim) override;
56
57 USDIMAGING_API
58 HdContainerDataSourceHandle GetImagingSubprimData(
59 UsdPrim const& prim,
60 TfToken const& subprim,
61 const UsdImagingDataSourceStageGlobals &stageGlobals) override;
62
63 USDIMAGING_API
64 HdDataSourceLocatorSet InvalidateImagingSubprim(
65 UsdPrim const& prim,
66 TfToken const& subprim,
67 TfTokenVector const& properties,
68 UsdImagingPropertyInvalidationType invalidationType) override;
69
70 // ---------------------------------------------------------------------- //
72 // ---------------------------------------------------------------------- //
73
74 USDIMAGING_API
75 void TrackVariability(UsdPrim const& prim,
76 SdfPath const& cachePath,
77 HdDirtyBits* timeVaryingBits,
79 instancerContext = NULL) const override;
80
81 USDIMAGING_API
82 void UpdateForTime(UsdPrim const& prim,
83 SdfPath const& cachePath,
84 UsdTimeCode time,
85 HdDirtyBits requestedBits,
87 instancerContext = NULL) const override;
88
89 // ---------------------------------------------------------------------- //
91 // ---------------------------------------------------------------------- //
92
93 USDIMAGING_API
94 HdDirtyBits ProcessPropertyChange(UsdPrim const& prim,
95 SdfPath const& cachePath,
96 TfToken const& propertyName)
97 override;
98
99 USDIMAGING_API
100 void ProcessPrimResync(SdfPath const& cachePath,
101 UsdImagingIndexProxy* index) override;
102
103 USDIMAGING_API
104 void ProcessPrimRemoval(SdfPath const& cachePath,
105 UsdImagingIndexProxy* index) override;
106
107 USDIMAGING_API
108 void MarkDirty(UsdPrim const& prim,
109 SdfPath const& cachePath,
110 HdDirtyBits dirty,
111 UsdImagingIndexProxy* index) override;
112
113 USDIMAGING_API
114 void MarkRefineLevelDirty(UsdPrim const& prim,
115 SdfPath const& cachePath,
116 UsdImagingIndexProxy* index) override;
117
118 USDIMAGING_API
119 void MarkReprDirty(UsdPrim const& prim,
120 SdfPath const& cachePath,
121 UsdImagingIndexProxy* index) override;
122
123 USDIMAGING_API
124 void MarkCullStyleDirty(UsdPrim const& prim,
125 SdfPath const& cachePath,
126 UsdImagingIndexProxy* index) override;
127
128 USDIMAGING_API
129 void MarkRenderTagDirty(UsdPrim const& prim,
130 SdfPath const& cachePath,
131 UsdImagingIndexProxy* index) override;
132
133 USDIMAGING_API
134 void MarkTransformDirty(UsdPrim const& prim,
135 SdfPath const& cachePath,
136 UsdImagingIndexProxy* index) override;
137
138 USDIMAGING_API
139 void MarkVisibilityDirty(UsdPrim const& prim,
140 SdfPath const& cachePath,
141 UsdImagingIndexProxy* index) override;
142
143 USDIMAGING_API
144 void MarkLightParamsDirty(
145 const UsdPrim& prim,
146 const SdfPath& cachePath,
147 UsdImagingIndexProxy* index) override;
148
149 USDIMAGING_API
150 void MarkCollectionsDirty(
151 const UsdPrim& prim,
152 const SdfPath& cachePath,
153 UsdImagingIndexProxy* index) override;
154
155 // ---------------------------------------------------------------------- //
157 // ---------------------------------------------------------------------- //
158
159 USDIMAGING_API
161 SdfPath const& instancerPath,
162 UsdTimeCode time) const override;
163
164 USDIMAGING_API
165 size_t SampleInstancerTransform(UsdPrim const& instancerPrim,
166 SdfPath const& instancerPath,
167 UsdTimeCode time,
168 size_t maxNumSamples,
169 float *sampleTimes,
170 GfMatrix4d *sampleValues) override;
171
172 USDIMAGING_API
174 UsdPrim const& usdPrim,
175 SdfPath const& cachePath) const override;
176
177 USDIMAGING_API
179 UsdPrim const& usdPrim,
180 SdfPath const& cachePath) const override;
181
182 USDIMAGING_API
184 SdfPath const& cachePath,
185 UsdTimeCode time,
186 bool ignoreRootTransform = false) const override;
187
188 USDIMAGING_API
189 size_t SampleTransform(UsdPrim const& prim,
190 SdfPath const& cachePath,
191 UsdTimeCode time,
192 size_t maxNumSamples,
193 float *sampleTimes,
194 GfMatrix4d *sampleValues) override;
195
196 USDIMAGING_API
197 size_t SamplePrimvar(UsdPrim const& usdPrim,
198 SdfPath const& cachePath,
199 TfToken const& key,
200 UsdTimeCode time,
201 size_t maxNumSamples,
202 float *sampleTimes,
203 VtValue *sampleValues,
204 VtIntArray *sampleIndices) override;
205
206 USDIMAGING_API
208 SdfPath const& cachePath,
209 UsdTimeCode time) const override;
210
211 USDIMAGING_API
212 bool IsChildPath(const SdfPath& path) const override;
213
214 bool GetVisible(UsdPrim const& prim,
215 SdfPath const& cachePath,
216 UsdTimeCode time) const override;
217
218 USDIMAGING_API
220 UsdPrim const& usdPrim,
221 SdfPath const& cachePath,
222 TfToken const& instanceInheritablePurpose) const override;
223
224 USDIMAGING_API
226 SdfPath const& cachePath,
227 UsdTimeCode time) const override;
228
229 USDIMAGING_API
230 HdCullStyle GetCullStyle(UsdPrim const& prim,
231 SdfPath const& cachePath,
232 UsdTimeCode time) const override;
233
234 USDIMAGING_API
235 GfRange3d GetExtent(UsdPrim const& usdPrim,
236 SdfPath const& cachePath,
237 UsdTimeCode time) const override;
238
239 USDIMAGING_API
240 bool GetDoubleSided(UsdPrim const& usdPrim,
241 SdfPath const& cachePath,
242 UsdTimeCode time) const override;
243
244
245 USDIMAGING_API
246 SdfPath GetMaterialId(UsdPrim const& prim,
247 SdfPath const& cachePath,
248 UsdTimeCode time) const override;
249
250 USDIMAGING_API
251 VtValue GetLightParamValue(
252 const UsdPrim& prim,
253 const SdfPath& cachePath,
254 const TfToken& paramName,
255 UsdTimeCode time) const override;
256
257 USDIMAGING_API
258 VtValue GetMaterialResource(
259 const UsdPrim& prim,
260 const SdfPath& cachePath,
261 UsdTimeCode time) const override;
262
263 USDIMAGING_API
264 HdExtComputationInputDescriptorVector
265 GetExtComputationInputs(UsdPrim const& prim,
266 SdfPath const& cachePath,
267 const UsdImagingInstancerContext* instancerContext)
268 const override;
269
270 USDIMAGING_API
271 HdExtComputationOutputDescriptorVector
272 GetExtComputationOutputs(UsdPrim const& prim,
273 SdfPath const& cachePath,
274 const UsdImagingInstancerContext* instancerContext)
275 const override;
276
277 USDIMAGING_API
278 HdExtComputationPrimvarDescriptorVector
279 GetExtComputationPrimvars(
280 UsdPrim const& prim,
281 SdfPath const& cachePath,
282 HdInterpolation interpolation,
283 const UsdImagingInstancerContext* instancerContext) const override;
284
285 USDIMAGING_API
286 VtValue
287 GetExtComputationInput(
288 UsdPrim const& prim,
289 SdfPath const& cachePath,
290 TfToken const& name,
291 UsdTimeCode time,
292 const UsdImagingInstancerContext* instancerContext) const override;
293
294 USDIMAGING_API
295 std::string
296 GetExtComputationKernel(
297 UsdPrim const& prim,
298 SdfPath const& cachePath,
299 const UsdImagingInstancerContext* instancerContext) const override;
300
301 USDIMAGING_API
302 VtValue
303 GetInstanceIndices(UsdPrim const& instancerPrim,
304 SdfPath const& instancerCachePath,
305 SdfPath const& prototypeCachePath,
306 UsdTimeCode time) const override;
307
308 USDIMAGING_API
309 VtValue Get(UsdPrim const& prim,
310 SdfPath const& cachePath,
311 TfToken const& key,
312 UsdTimeCode time,
313 VtIntArray *outIndices) const override;
314
315 // ---------------------------------------------------------------------- //
317 // ---------------------------------------------------------------------- //
318
319 USDIMAGING_API
321 SdfPath const &instancerPath,
322 SdfPath const &protoInstancerPath,
323 UsdTimeCode time) const override;
324
325 // ---------------------------------------------------------------------- //
327 // ---------------------------------------------------------------------- //
328
329 USDIMAGING_API
331 SdfPath const& cachePath,
332 int instanceIndex,
333 HdInstancerContext *instancerContext) const override;
334
335 USDIMAGING_API
336 SdfPathVector GetScenePrimPaths(
337 SdfPath const& cachePath,
338 std::vector<int> const& instanceIndices,
339 std::vector<HdInstancerContext> *instancerCtxs) const override;
340
341 USDIMAGING_API
342 bool PopulateSelection(
343 HdSelection::HighlightMode const& highlightMode,
344 SdfPath const &cachePath,
345 UsdPrim const &usdPrim,
346 int const hydraInstanceIndex,
347 VtIntArray const &parentInstanceIndices,
348 HdSelectionSharedPtr const &result) const override;
349
350 // ---------------------------------------------------------------------- //
352 // ---------------------------------------------------------------------- //
353
354 USDIMAGING_API
355 HdVolumeFieldDescriptorVector
356 GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
357 UsdTimeCode time) const override;
358
359protected:
360 friend class UsdImagingInstanceAdapter;
361 // ---------------------------------------------------------------------- //
363 // ---------------------------------------------------------------------- //
364
365 // Given the USD path for a prim of this adapter's type, returns
366 // the prim's Hydra cache path. This version will reserve a path in
367 // the adapter's instancer data map for the given point instancer USD
368 // path, including any necessary variant selection path.
369 //
370 // This method is marked const, but it is not const! If called with the
371 // path of a populated point instancer, it will modify the instancer data
372 // cache and return a new path.
373 USDIMAGING_API
374 SdfPath
375 ResolveCachePath(
376 const SdfPath& usdPath,
377 const UsdImagingInstancerContext* ctx = nullptr) const override;
378
379 USDIMAGING_API
380 void _RemovePrim(SdfPath const& cachePath,
381 UsdImagingIndexProxy* index) override final;
382
383private:
384 struct _ProtoPrim;
385 struct _InstancerData;
386
387 SdfPath _Populate(UsdPrim const& prim,
389 UsdImagingInstancerContext const* instancerContext);
390
391 void _PopulatePrototype(int protoIndex,
392 _InstancerData& instrData,
393 UsdPrim const& protoRootPrim,
395 UsdImagingInstancerContext const *instancerContext);
396
397 // Process prim removal and output a set of affected instancer paths is
398 // provided.
399 void _ProcessPrimRemoval(SdfPath const& cachePath,
401 SdfPathVector* instancersToReload);
402
403 // Removes all instancer data, both locally and from the render index.
404 void _UnloadInstancer(SdfPath const& instancerPath,
405 UsdImagingIndexProxy* index);
406
407 // Updates per-frame instancer visibility.
408 void _UpdateInstancerVisibility(SdfPath const& instancerPath,
409 _InstancerData const& instrData,
410 UsdTimeCode time) const;
411
412 // Returns true if the instancer is visible, taking into account all
413 // parent instancers visibilities.
414 bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
415 const;
416
417 // Gets the associated _ProtoPrim for the given instancer and cache path.
418 _ProtoPrim const& _GetProtoPrim(SdfPath const& instancerPath,
419 SdfPath const& cachePath) const;
420
421 // Gets the associated _ProtoPrim and instancerContext if cachePath is a
422 // child path and returns \c true, otherwise returns \c false.
423 //
424 // Note that the returned instancer context may not be as fully featured as
425 // your needs may be.
426 bool _GetProtoPrimForChild(
427 UsdPrim const& usdPrim,
428 SdfPath const& cachePath,
429 _ProtoPrim const** proto,
430 UsdImagingInstancerContext* ctx) const;
431
432 // Gets the UsdPrim to use from the given _ProtoPrim.
433 const UsdPrim _GetProtoUsdPrim(_ProtoPrim const& proto) const;
434
435 // Takes the transform applies a corrective transform to 1) remove any
436 // transforms above the model root (root proto path) and 2) apply the
437 // instancer transform.
438 GfMatrix4d _CorrectTransform(UsdPrim const& instancer,
439 UsdPrim const& proto,
440 SdfPath const& cachePath,
441 SdfPathVector const& protoPathChain,
442 GfMatrix4d const& inTransform,
443 UsdTimeCode time) const;
444
445 // Similar to CorrectTransform, requires a visibility value exist in the
446 // ValueCache, removes any visibility opinions above the model root (proto
447 // root path) and applies the instancer visibility.
448 void _ComputeProtoVisibility(UsdPrim const& protoRoot,
449 UsdPrim const& protoGprim,
450 UsdTimeCode time,
451 bool* vis) const;
452
453 /*
454 PointInstancer (InstancerData)
455 |
456 +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
457 | +-- ProtoRprim
458 | +-- ProtoRprim
459 |
460 +-- Prototype[1]------+-- ProtoRprim
461 | +-- ProtoRprim
462 .
463 .
464 */
465
466 // A proto prim represents a single populated prim under a prototype root
467 // declared on the instancer. For example, a character may be targeted
468 // by the prototypes relationship; it will have many meshes, and each
469 // mesh is represented as a separate proto prim.
470 struct _ProtoPrim {
471 _ProtoPrim() : variabilityBits(0), visible(true) {}
472 // Each prim will become a prototype "child" under the instancer.
473 // paths is a list of paths we had to hop across when resolving native
474 // USD instances.
475 SdfPathVector paths;
476 // The prim adapter for the actual prototype prim.
477 UsdImagingPrimAdapterSharedPtr adapter;
478 // The root prototype path, typically the model root, which is a subtree
479 // and might contain several imageable prims.
480 SdfPath protoRootPath;
481 // Tracks the variability of the underlying adapter to avoid
482 // redundantly reading data. This value is stored as
483 // HdDirtyBits bit flags.
484 // XXX: This is mutable so we can set it in TrackVariability.
485 mutable HdDirtyBits variabilityBits;
486 // When variabilityBits does not include HdChangeTracker::DirtyVisibility
487 // the visible field is the unvarying value for visibility.
488 // XXX: This is mutable so we can set it in TrackVariability.
489 mutable bool visible;
490 };
491
492 // Indexed by cachePath (each prim has one entry)
493 typedef std::unordered_map<SdfPath, _ProtoPrim, SdfPath::Hash> _ProtoPrimMap;
494
495 // All data associated with a given Instancer prim. PrimMap could
496 // technically be split out to avoid two lookups, however it seems cleaner
497 // to keep everything bundled up under the instancer path.
498 struct _InstancerData {
499 _InstancerData() {}
500 SdfPath parentInstancerCachePath;
501 _ProtoPrimMap protoPrimMap;
502 SdfPathVector prototypePaths;
503
505 PathToIndexMap prototypePathIndices;
506
507
508 // XXX: We keep a bunch of state around visibility that's set in
509 // TrackVariability and UpdateForTime. "visible", and "visibleTime"
510 // (the cache key for visible) are set in UpdateForTime and guarded
511 // by "mutex".
512 mutable std::mutex mutex;
513 mutable bool variableVisibility;
514 mutable bool visible;
515 mutable UsdTimeCode visibleTime;
516 mutable std::atomic_bool initialized = std::atomic_bool(false);
517 };
518
519 // A map of instancer data, one entry per instancer prim that has been
520 // populated. This must be mutable so we can modify it in ResolveCachePath.
521 // Note: this is accessed in multithreaded code paths and must be protected
522 typedef std::unordered_map<SdfPath /*instancerPath*/,
523 _InstancerData,
524 SdfPath::Hash> _InstancerDataMap;
525 mutable _InstancerDataMap _instancerData;
526
527 inline static std::atomic_int _globalVariantCounter = std::atomic_int(0);
528};
529
530PXR_NAMESPACE_CLOSE_SCOPE
531
532#endif // PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:71
Basic type: 3-dimensional floating point range.
Definition: range3d.h:47
Represents a set of data source locators closed under descendancy.
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:39
Tags for non-hierarchial subdiv surfaces.
Definition: subdivTags.h:26
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
This is a space efficient container that mimics the TfHashMap API that uses a vector for storage when...
Definition: denseHashMap.h:41
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
This class is used as a context object with global stage information, that gets passed down to dataso...
This proxy class exposes a subset of the private Delegate API to PrimAdapters.
Definition: indexProxy.h:30
Delegate support for instanced prims.
An abstract adapter class for prims that are instanceable.
Delegate support for UsdGeomPointInstancer.
USDIMAGING_API 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.
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.
USDIMAGING_API SdfPathVector GetInstancerPrototypes(UsdPrim const &usdPrim, SdfPath const &cachePath) const override
Return the list of known prototypes of this prim.
USDIMAGING_API 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.
USDIMAGING_API SdfPath GetScenePrimPath(SdfPath const &cachePath, int instanceIndex, HdInstancerContext *instancerContext) const override
USDIMAGING_API VtValue GetTopology(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
Gets the topology object of a specific Usd prim.
USDIMAGING_API 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...
USDIMAGING_API SdfPath Populate(UsdPrim const &prim, UsdImagingIndexProxy *index, UsdImagingInstancerContext const *instancerContext=nullptr) override
Called to populate the RenderIndex for this UsdPrim.
USDIMAGING_API HdCullStyle GetCullStyle(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
Gets the cullstyle of a specific path in the scene graph.
USDIMAGING_API 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.
USDIMAGING_API bool GetDoubleSided(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Reads double-sided from the given prim. If not authored, returns false.
USDIMAGING_API GfMatrix4d GetRelativeInstancerTransform(SdfPath const &instancerPath, SdfPath const &protoInstancerPath, UsdTimeCode time) const override
Returns the transform of protoInstancerPath relative to instancerPath.
USDIMAGING_API GfRange3d GetExtent(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Reads the extent from the given prim.
USDIMAGING_API PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Get the subdiv tags for this prim.
USDIMAGING_API 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 ...
USDIMAGING_API 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.
USDIMAGING_API GfMatrix4d GetInstancerTransform(UsdPrim const &instancerPrim, SdfPath const &instancerPath, UsdTimeCode time) const override
Get the instancer transform for the given prim.
USDIMAGING_API 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.
USDIMAGING_API 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...
USDIMAGING_API void ProcessPrimRemoval(SdfPath const &cachePath, UsdImagingIndexProxy *index) override
Removes all associated Rprims and dependencies from the render index without scheduling them for repo...
USDIMAGING_API TfToken GetPurpose(UsdPrim const &usdPrim, SdfPath const &cachePath, TfToken const &instanceInheritablePurpose) const override
Returns the purpose token for prim.
USDIMAGING_API SdfPath GetInstancerId(UsdPrim const &usdPrim, SdfPath const &cachePath) const override
Return the instancerId for this prim.
USDIMAGING_API 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...
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:72
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
Object used by instancer prim adapters to pass along context about the instancer and instance prim to...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440