Loading...
Searching...
No Matches
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
37PXR_NAMESPACE_OPEN_SCOPE
38
39
43{
44public:
46
48 : BaseAdapter()
49 { }
51
53 UsdPrim const& prim,
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,
84 UsdImagingPropertyInvalidationType invalidationType) override;
85
86 // ---------------------------------------------------------------------- //
88 // ---------------------------------------------------------------------- //
89
90 virtual void TrackVariability(UsdPrim const& prim,
91 SdfPath const& cachePath,
92 HdDirtyBits* timeVaryingBits,
94 instancerContext = NULL) const override;
95
96 virtual void UpdateForTime(UsdPrim const& prim,
97 SdfPath const& cachePath,
98 UsdTimeCode time,
99 HdDirtyBits requestedBits,
101 instancerContext = NULL) const override;
102
103 // ---------------------------------------------------------------------- //
105 // ---------------------------------------------------------------------- //
106
107 virtual HdDirtyBits ProcessPropertyChange(UsdPrim const& prim,
108 SdfPath const& cachePath,
109 TfToken const& propertyName)
110 override;
111
112 virtual void ProcessPrimResync(SdfPath const& cachePath,
113 UsdImagingIndexProxy* index) override;
114
115 virtual void ProcessPrimRemoval(SdfPath const& cachePath,
116 UsdImagingIndexProxy* index) override;
117
118 virtual void MarkDirty(UsdPrim const& prim,
119 SdfPath const& cachePath,
120 HdDirtyBits dirty,
121 UsdImagingIndexProxy* index) override;
122
123 virtual void MarkRefineLevelDirty(UsdPrim const& prim,
124 SdfPath const& cachePath,
125 UsdImagingIndexProxy* index) override;
126
127 virtual void MarkReprDirty(UsdPrim const& prim,
128 SdfPath const& cachePath,
129 UsdImagingIndexProxy* index) override;
130
131 virtual void MarkCullStyleDirty(UsdPrim const& prim,
132 SdfPath const& cachePath,
133 UsdImagingIndexProxy* index) override;
134
135 virtual void MarkRenderTagDirty(UsdPrim const& prim,
136 SdfPath const& cachePath,
137 UsdImagingIndexProxy* index) override;
138
139 virtual void MarkTransformDirty(UsdPrim const& prim,
140 SdfPath const& cachePath,
141 UsdImagingIndexProxy* index) override;
142
143 virtual void MarkVisibilityDirty(UsdPrim const& prim,
144 SdfPath const& cachePath,
145 UsdImagingIndexProxy* index) override;
146
147 virtual void MarkLightParamsDirty(
148 const UsdPrim& prim,
149 const SdfPath& cachePath,
150 UsdImagingIndexProxy* index) override;
151
152 virtual void MarkCollectionsDirty(
153 const UsdPrim& prim,
154 const SdfPath& cachePath,
155 UsdImagingIndexProxy* index) override;
156
157 // ---------------------------------------------------------------------- //
159 // ---------------------------------------------------------------------- //
160
162 SdfPath const& instancerPath,
163 UsdTimeCode time) const override;
164
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
173 UsdPrim const& usdPrim,
174 SdfPath const& cachePath) const override;
175
177 UsdPrim const& usdPrim,
178 SdfPath const& cachePath) const override;
179
181 SdfPath const& cachePath,
182 UsdTimeCode time,
183 bool ignoreRootTransform = false) const override;
184
185 size_t SampleTransform(UsdPrim const& prim,
186 SdfPath const& cachePath,
187 UsdTimeCode time,
188 size_t maxNumSamples,
189 float *sampleTimes,
190 GfMatrix4d *sampleValues) override;
191
192 size_t SamplePrimvar(UsdPrim const& usdPrim,
193 SdfPath const& cachePath,
194 TfToken const& key,
195 UsdTimeCode time,
196 size_t maxNumSamples,
197 float *sampleTimes,
198 VtValue *sampleValues,
199 VtIntArray *sampleIndices) override;
200
202 SdfPath const& cachePath,
203 UsdTimeCode time) const override;
204
205 bool GetVisible(UsdPrim const& prim,
206 SdfPath const& cachePath,
207 UsdTimeCode time) const override;
208
210 UsdPrim const& usdPrim,
211 SdfPath const& cachePath,
212 TfToken const& instanceInheritablePurpose) const override;
213
215 SdfPath const& cachePath,
216 UsdTimeCode time) const override;
217
218 HdCullStyle GetCullStyle(UsdPrim const& prim,
219 SdfPath const& cachePath,
220 UsdTimeCode time) const override;
221
222 GfRange3d GetExtent(UsdPrim const& usdPrim,
223 SdfPath const& cachePath,
224 UsdTimeCode time) const override;
225
226 bool GetDoubleSided(UsdPrim const& usdPrim,
227 SdfPath const& cachePath,
228 UsdTimeCode time) const override;
229
230
231 SdfPath GetMaterialId(UsdPrim const& prim,
232 SdfPath const& cachePath,
233 UsdTimeCode time) const override;
234
235 VtValue GetLightParamValue(
236 const UsdPrim& prim,
237 const SdfPath& cachePath,
238 const TfToken& paramName,
239 UsdTimeCode time) const override;
240
241 VtValue GetMaterialResource(
242 const UsdPrim& prim,
243 const SdfPath& cachePath,
244 UsdTimeCode time) const override;
245
246 HdExtComputationInputDescriptorVector
247 GetExtComputationInputs(UsdPrim const& prim,
248 SdfPath const& cachePath,
249 const UsdImagingInstancerContext* instancerContext)
250 const override;
251
252 HdExtComputationOutputDescriptorVector
253 GetExtComputationOutputs(UsdPrim const& prim,
254 SdfPath const& cachePath,
255 const UsdImagingInstancerContext* instancerContext)
256 const override;
257
258 HdExtComputationPrimvarDescriptorVector
259 GetExtComputationPrimvars(
260 UsdPrim const& prim,
261 SdfPath const& cachePath,
262 HdInterpolation interpolation,
263 const UsdImagingInstancerContext* instancerContext) const override;
264
265 VtValue
266 GetExtComputationInput(
267 UsdPrim const& prim,
268 SdfPath const& cachePath,
269 TfToken const& name,
270 UsdTimeCode time,
271 const UsdImagingInstancerContext* instancerContext) const override;
272
273 std::string
274 GetExtComputationKernel(
275 UsdPrim const& prim,
276 SdfPath const& cachePath,
277 const UsdImagingInstancerContext* instancerContext) const override;
278
279 VtValue
280 GetInstanceIndices(UsdPrim const& instancerPrim,
281 SdfPath const& instancerCachePath,
282 SdfPath const& prototypeCachePath,
283 UsdTimeCode time) const override;
284
285 VtValue Get(UsdPrim const& prim,
286 SdfPath const& cachePath,
287 TfToken const& key,
288 UsdTimeCode time,
289 VtIntArray *outIndices) const override;
290
291 // ---------------------------------------------------------------------- //
293 // ---------------------------------------------------------------------- //
294
296 SdfPath const &instancerPath,
297 SdfPath const &protoInstancerPath,
298 UsdTimeCode time) const override;
299
300 // ---------------------------------------------------------------------- //
302 // ---------------------------------------------------------------------- //
303
305 SdfPath const& cachePath,
306 int instanceIndex,
307 HdInstancerContext *instancerContext) const override;
308
309 virtual SdfPathVector GetScenePrimPaths(
310 SdfPath const& cachePath,
311 std::vector<int> const& instanceIndices,
312 std::vector<HdInstancerContext> *instancerCtxs) const override;
313
314 virtual bool PopulateSelection(
315 HdSelection::HighlightMode const& highlightMode,
316 SdfPath const &cachePath,
317 UsdPrim const &usdPrim,
318 int const hydraInstanceIndex,
319 VtIntArray const &parentInstanceIndices,
320 HdSelectionSharedPtr const &result) const override;
321
322 // ---------------------------------------------------------------------- //
324 // ---------------------------------------------------------------------- //
325
326 virtual HdVolumeFieldDescriptorVector
327 GetVolumeFieldDescriptors(UsdPrim const& usdPrim, SdfPath const &id,
328 UsdTimeCode time) const override;
329
330protected:
331 virtual void _RemovePrim(SdfPath const& cachePath,
332 UsdImagingIndexProxy* index) override final;
333
334private:
335 struct _ProtoPrim;
336 struct _InstancerData;
337
338 SdfPath _Populate(UsdPrim const& prim,
340 UsdImagingInstancerContext const* instancerContext);
341
342 void _PopulatePrototype(int protoIndex,
343 _InstancerData& instrData,
344 UsdPrim const& protoRootPrim,
346 UsdImagingInstancerContext const *instancerContext);
347
348 // Process prim removal and output a set of affected instancer paths is
349 // provided.
350 void _ProcessPrimRemoval(SdfPath const& cachePath,
352 SdfPathVector* instancersToReload);
353
354 // Removes all instancer data, both locally and from the render index.
355 void _UnloadInstancer(SdfPath const& instancerPath,
356 UsdImagingIndexProxy* index);
357
358 // Updates per-frame instancer visibility.
359 void _UpdateInstancerVisibility(SdfPath const& instancerPath,
360 _InstancerData const& instrData,
361 UsdTimeCode time) const;
362
363 // Returns true if the instancer is visible, taking into account all
364 // parent instancers visibilities.
365 bool _GetInstancerVisible(SdfPath const &instancerPath, UsdTimeCode time)
366 const;
367
368 // Gets the associated _ProtoPrim for the given instancer and cache path.
369 _ProtoPrim const& _GetProtoPrim(SdfPath const& instancerPath,
370 SdfPath const& cachePath) const;
371
372 // Gets the associated _ProtoPrim and instancerContext if cachePath is a
373 // child path and returns \c true, otherwise returns \c false.
374 //
375 // Note that the returned instancer context may not be as fully featured as
376 // your needs may be.
377 bool _GetProtoPrimForChild(
378 UsdPrim const& usdPrim,
379 SdfPath const& cachePath,
380 _ProtoPrim const** proto,
381 UsdImagingInstancerContext* ctx) const;
382
383 // Gets the UsdPrim to use from the given _ProtoPrim.
384 const UsdPrim _GetProtoUsdPrim(_ProtoPrim const& proto) const;
385
386 // Takes the transform applies a corrective transform to 1) remove any
387 // transforms above the model root (root proto path) and 2) apply the
388 // instancer transform.
389 GfMatrix4d _CorrectTransform(UsdPrim const& instancer,
390 UsdPrim const& proto,
391 SdfPath const& cachePath,
392 SdfPathVector const& protoPathChain,
393 GfMatrix4d const& inTransform,
394 UsdTimeCode time) const;
395
396 // Similar to CorrectTransform, requires a visibility value exist in the
397 // ValueCache, removes any visibility opinions above the model root (proto
398 // root path) and applies the instancer visibility.
399 void _ComputeProtoVisibility(UsdPrim const& protoRoot,
400 UsdPrim const& protoGprim,
401 UsdTimeCode time,
402 bool* vis) const;
403
404 /*
405 PointInstancer (InstancerData)
406 |
407 +-- Prototype[0]------+-- ProtoRprim (mesh, curve, ...)
408 | +-- ProtoRprim
409 | +-- ProtoRprim
410 |
411 +-- Prototype[1]------+-- ProtoRprim
412 | +-- ProtoRprim
413 .
414 .
415 */
416
417 // A proto prim represents a single populated prim under a prototype root
418 // declared on the instancer. For example, a character may be targeted
419 // by the prototypes relationship; it will have many meshes, and each
420 // mesh is represented as a separate proto prim.
421 struct _ProtoPrim {
422 _ProtoPrim() : variabilityBits(0), visible(true) {}
423 // Each prim will become a prototype "child" under the instancer.
424 // paths is a list of paths we had to hop across when resolving native
425 // USD instances.
426 SdfPathVector paths;
427 // The prim adapter for the actual prototype prim.
428 UsdImagingPrimAdapterSharedPtr adapter;
429 // The root prototype path, typically the model root, which is a subtree
430 // and might contain several imageable prims.
431 SdfPath protoRootPath;
432 // Tracks the variability of the underlying adapter to avoid
433 // redundantly reading data. This value is stored as
434 // HdDirtyBits bit flags.
435 // XXX: This is mutable so we can set it in TrackVariability.
436 mutable HdDirtyBits variabilityBits;
437 // When variabilityBits does not include HdChangeTracker::DirtyVisibility
438 // the visible field is the unvarying value for visibility.
439 // XXX: This is mutable so we can set it in TrackVariability.
440 mutable bool visible;
441 };
442
443 // Indexed by cachePath (each prim has one entry)
444 typedef std::unordered_map<SdfPath, _ProtoPrim, SdfPath::Hash> _ProtoPrimMap;
445
446 // All data associated with a given Instancer prim. PrimMap could
447 // technically be split out to avoid two lookups, however it seems cleaner
448 // to keep everything bundled up under the instancer path.
449 struct _InstancerData {
450 _InstancerData() {}
451 SdfPath parentInstancerCachePath;
452 _ProtoPrimMap protoPrimMap;
453 SdfPathVector prototypePaths;
454
456 PathToIndexMap prototypePathIndices;
457
458
459 // XXX: We keep a bunch of state around visibility that's set in
460 // TrackVariability and UpdateForTime. "visible", and "visibleTime"
461 // (the cache key for visible) are set in UpdateForTime and guarded
462 // by "mutex".
463 mutable std::mutex mutex;
464 mutable bool variableVisibility;
465 mutable bool visible;
466 mutable UsdTimeCode visibleTime;
467 };
468
469 // A map of instancer data, one entry per instancer prim that has been
470 // populated.
471 // Note: this is accessed in multithreaded code paths and must be protected
472 typedef std::unordered_map<SdfPath /*instancerPath*/,
473 _InstancerData,
474 SdfPath::Hash> _InstancerDataMap;
475 _InstancerDataMap _instancerData;
476};
477
478
479
480PXR_NAMESPACE_CLOSE_SCOPE
481
482#endif // PXR_USD_IMAGING_USD_IMAGING_POINT_INSTANCER_ADAPTER_H
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Basic type: 3-dimensional floating point range.
Definition: range3d.h:64
Represents a set of data source locators closed under descendancy.
HighlightMode
Selection modes allow differentiation in selection highlight behavior.
Definition: selection.h:56
Tags for non-hierarchial subdiv surfaces.
Definition: subdivTags.h:43
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:291
This is a space efficient container that mimics the TfHashMap API that uses a vector for storage when...
Definition: denseHashMap.h:58
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
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:47
Delegate support for UsdGeomPointInstancer.
bool GetDoubleSided(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Reads double-sided from the given prim. If not authored, returns false.
PxOsdSubdivTags GetSubdivTags(UsdPrim const &usdPrim, SdfPath const &cachePath, UsdTimeCode time) const override
Get the subdiv tags for this prim.
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.
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.
HdCullStyle GetCullStyle(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
Gets the cullstyle of a specific path in the scene graph.
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.
SdfPath GetInstancerId(UsdPrim const &usdPrim, SdfPath const &cachePath) const override
Return the instancerId for 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 ...
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 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.
TfToken GetPurpose(UsdPrim const &usdPrim, SdfPath const &cachePath, TfToken const &instanceInheritablePurpose) const override
Returns the purpose token for prim.
virtual GfMatrix4d GetRelativeInstancerTransform(SdfPath const &instancerPath, SdfPath const &protoInstancerPath, UsdTimeCode time) const override
Returns the transform of protoInstancerPath relative to instancerPath.
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.
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...
VtValue GetTopology(UsdPrim const &prim, SdfPath const &cachePath, UsdTimeCode time) const override
Gets the topology object of a specific Usd prim.
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 SdfPath Populate(UsdPrim const &prim, UsdImagingIndexProxy *index, UsdImagingInstancerContext const *instancerContext=nullptr) override
Called to populate the RenderIndex for this UsdPrim.
GfMatrix4d GetInstancerTransform(UsdPrim const &instancerPrim, SdfPath const &instancerPath, UsdTimeCode time) const override
Get the instancer transform for the given prim.
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 GetScenePrimPath(SdfPath const &cachePath, int instanceIndex, HdInstancerContext *instancerContext) const override
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...
SdfPathVector GetInstancerPrototypes(UsdPrim const &usdPrim, SdfPath const &cachePath) const override
Return the list of known prototypes of this prim.
Base class for all PrimAdapters.
Definition: primAdapter.h:70
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:134
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:84
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:165
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:457