HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sceneIndexAdapterSceneDelegate.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 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_IMAGING_HD_SCENE_INDEX_ADAPTER_SCENE_DELEGATE_H
8 #define PXR_IMAGING_HD_SCENE_INDEX_ADAPTER_SCENE_DELEGATE_H
9 
12 #include "pxr/usd/sdf/pathTable.h"
13 #include <thread>
14 #include <tbb/concurrent_unordered_map.h>
15 
18 
20 
21 /// Convert an ext computation primvar descriptor from Hydra 2 to Hydra 1
22 /// representation...
23 HD_API
26  TfToken const& name,
28 
29 /// Convert a primvar descriptor from Hydra 2 to Hydra 1 representation...
30 HD_API
33  TfToken const& name,
34  HdPrimvarSchema primvar);
35 
36 /// \class HdSceneIndexAdapterSceneDelegate
37 ///
38 /// Scene delegate which observes notices from an HdSceneIndex and applies them
39 /// to an HdRenderIndex. This serves as "back-end" emulation in order for
40 /// scenes described via the HdSceneIndex/HdDataSource APIs to be accessible
41 /// by legacy render delegates.
42 ///
44  : public HdSceneDelegate
45  , public HdSceneIndexObserver
46 {
47 public:
48 
50  HdSceneIndexBaseRefPtr inputSceneIndex,
51  HdRenderIndex *parentIndex,
52  SdfPath const &delegateID);
53 
55 
56  // satisfying HdSceneIndexObserver ----------------------------------------
57  void PrimsAdded(
58  const HdSceneIndexBase &sender,
59  const AddedPrimEntries &entries) override;
60 
61  void PrimsRemoved(
62  const HdSceneIndexBase &sender,
63  const RemovedPrimEntries &entries) override;
64 
65  void PrimsDirtied(
66  const HdSceneIndexBase &sender,
67  const DirtiedPrimEntries &entries) override;
68 
69  void PrimsRenamed(
70  const HdSceneIndexBase &sender,
71  const RenamedPrimEntries &entries) override;
72 
73  // ------------------------------------------------------------------------
74  // HdSceneIndexDelegate API
75 
76  // ------------------------------------------------------------------------
77  // Rprim API
78 
79  HdMeshTopology GetMeshTopology(SdfPath const &id) override;
81  PxOsdSubdivTags GetSubdivTags(SdfPath const &id) override;
82  GfRange3d GetExtent(SdfPath const &id) override;
83  bool GetVisible(SdfPath const &id) override;
84  bool GetDoubleSided(SdfPath const &id) override;
85  HdCullStyle GetCullStyle(SdfPath const &id) override;
86  VtValue GetShadingStyle(SdfPath const &id) override;
87  HdDisplayStyle GetDisplayStyle(SdfPath const &id) override;
88  HdReprSelector GetReprSelector(SdfPath const &id) override;
89  TfToken GetRenderTag(SdfPath const &id) override;
90  VtArray<TfToken> GetCategories(SdfPath const &id) override;
92  SdfPath const &volumeId) override;
94  TfToken const &paramName) override;
95 
96  // ------------------------------------------------------------------------
97  // Transform API
98 
99  GfMatrix4d GetTransform(SdfPath const &id) override;
100  size_t SampleTransform(SdfPath const &id, size_t maxSampleCount,
101  float *sampleTimes, GfMatrix4d *sampleValues) override;
102  size_t SampleTransform(SdfPath const &id,
103  float startTime, float endTime,
104  size_t maxSampleCount,
105  float *sampleTimes, GfMatrix4d *sampleValues) override;
106 
108  SdfPath const &instancerId) override;
109  size_t SampleInstancerTransform(SdfPath const &instancerId,
110  size_t maxSampleCount, float *sampleTimes,
111  GfMatrix4d *sampleValues) override;
112  size_t SampleInstancerTransform(SdfPath const &instancerId,
113  float startTime, float endTime,
114  size_t maxSampleCount, float *sampleTimes,
115  GfMatrix4d *sampleValues) override;
116 
117  // ------------------------------------------------------------------------
118  // Primvar API
119 
122  SdfPath const &id, HdInterpolation interpolation) override;
123 
124  VtValue Get(SdfPath const &id, TfToken const &key) override;
125  VtValue GetIndexedPrimvar(SdfPath const &id, TfToken const &key,
126  VtIntArray *outIndices) override;
127 
128  size_t SamplePrimvar(SdfPath const &id, TfToken const &key,
129  size_t maxSampleCount, float *sampleTimes,
130  VtValue *sampleValues) override;
131  size_t SamplePrimvar(SdfPath const &id, TfToken const &key,
132  float startTime, float endTime,
133  size_t maxSampleCount, float *sampleTimes,
134  VtValue *sampleValues) override;
135  size_t SampleIndexedPrimvar(SdfPath const &id, TfToken const &key,
136  size_t maxNumSamples, float *times, VtValue *samples,
137  VtIntArray *sampleIndices) override;
138  size_t SampleIndexedPrimvar(SdfPath const &id, TfToken const &key,
139  float startTime, float endTime,
140  size_t maxNumSamples, float *times, VtValue *samples,
141  VtIntArray *sampleIndices) override;
142 
143  // ------------------------------------------------------------------------
144  // Instancer API
145 
146  std::vector<VtArray<TfToken>> GetInstanceCategories(
147  SdfPath const &instancerId) override;
148  VtIntArray GetInstanceIndices(
149  SdfPath const &instancerId, SdfPath const &prototypeId) override;
150  SdfPath GetInstancerId(SdfPath const &primId) override;
151  SdfPathVector GetInstancerPrototypes(SdfPath const &instancerId) override;
152 
153  // ------------------------------------------------------------------------
154  // Path Translation API
155 
156  SdfPath GetDataSharingId(SdfPath const& primId) override;
157 
158  // ------------------------------------------------------------------------
159  // Material API
160 
161  SdfPath GetMaterialId(SdfPath const &id) override;
162  VtValue GetMaterialResource(SdfPath const &id) override;
163  HdIdVectorSharedPtr GetCoordSysBindings(SdfPath const &id) override;
164 
165  // ------------------------------------------------------------------------
166  // Renderbuffer API
167 
169  SdfPath const &id) override;
170 
171  // ------------------------------------------------------------------------
172  // Light API
173 
175  TfToken const &paramName) override;
176 
177  // ------------------------------------------------------------------------
178  // Camera API
179 
180  VtValue GetCameraParamValue(SdfPath const &cameraId,
181  TfToken const &paramName) override;
182 
183  // ------------------------------------------------------------------------
184  // ExtComputation API
185 
186  // ... on the rprim
189  SdfPath const &id, HdInterpolation interpolationMode) override;
190 
191  // ... on the sprim
193  SdfPath const &computationId) override;
195  SdfPath const &computationId, TfToken const &input) override;
197  SdfPath const &computationId,
198  TfToken const &input,
199  size_t maxSampleCount,
200  float *sampleTimes,
201  VtValue *sampleValues) override;
203  SdfPath const &computationId,
204  TfToken const &input,
205  float startTime,
206  float endTime,
207  size_t maxSampleCount,
208  float *sampleTimes,
209  VtValue *sampleValues) override;
210 
212  SdfPath const &computationId) override;
214  SdfPath const &computationId) override;
215 
216  std::string GetExtComputationKernel(SdfPath const &computationId) override;
217  void InvokeExtComputation(SdfPath const &computationId,
218  HdExtComputationContext *context) override;
219 
220  TfTokenVector GetTaskRenderTags(SdfPath const &taskId) override;
221 
222  void Sync(HdSyncRequestVector* request) override;
223  void PostSyncCleanup() override;
224 
225  // NOTE: The remaining scene delegate functions aren't used for emulation:
226  // - GetScenePrimPath
227  // - IsEnabled
228 
229 private:
230  // Compute and return an HdSceneIndexPrim from the input scene index.
231  // Uses a per-thread single-entry cache to re-use this computation
232  // across sequential Get...() calls in the public API. This API returns
233  // the prim by value rather than reference because callers may
234  // indirectly re-invoke _GetInputPrim() on the same thread, but with
235  // a different id path, if they make use of a TBB work queue.
236  HdSceneIndexPrim _GetInputPrim(SdfPath const& id);
237 
238  using _InputPrimCacheEntry = std::pair<SdfPath, HdSceneIndexPrim>;
239 
240  // A cache of the last prim accessed, per thread
241  tbb::concurrent_unordered_map<std::thread::id, _InputPrimCacheEntry,
242  std::hash<std::thread::id> > _inputPrimCache;
243 
244  void _PrimAdded(
245  const SdfPath &primPath,
246  const TfToken &primType);
247 
248  VtValue _GetPrimvar(SdfPath const &id, TfToken const &key,
249  VtIntArray *outIndices);
250 
251  VtValue _GetPrimvar(
252  const HdContainerDataSourceHandle &primvarsDataSource,
253  TfToken const &key,
254  VtIntArray *outIndices);
255 
256  VtValue _GetImageShaderValue(
257  HdSceneIndexPrim prim,
258  const TfToken& key);
259 
260  size_t _SamplePrimvar(SdfPath const &id, TfToken const &key,
261  float startTime, float endTime,
262  size_t maxNumSamples, float *times, VtValue *samples,
263  VtIntArray *sampleIndices);
264 
265  HdSceneIndexBaseRefPtr _inputSceneIndex;
266 
267  struct _PrimCacheEntry
268  {
269  TfToken primType;
270 
271  using PrimvarDescriptorsArray =
272  std::array<HdPrimvarDescriptorVector, HdInterpolationCount>;
273  std::shared_ptr<PrimvarDescriptorsArray> primvarDescriptors;
274  using ExtCmpPrimvarDescriptorsArray =
277  std::shared_ptr<ExtCmpPrimvarDescriptorsArray> extCmpPrimvarDescriptors;
278  };
279 
281  _PrimCacheTable _primCache;
282 
283  std::shared_ptr<_PrimCacheEntry::PrimvarDescriptorsArray>
284  _ComputePrimvarDescriptors(
285  const HdContainerDataSourceHandle &primDataSource);
286  std::shared_ptr<_PrimCacheEntry::ExtCmpPrimvarDescriptorsArray>
287  _ComputeExtCmpPrimvarDescriptors(
288  const HdContainerDataSourceHandle &primDataSource);
289 
290  bool _sceneDelegatesBuilt;
291  std::vector<HdSceneDelegate*> _sceneDelegates;
292 
293  // Hint cache of all prim paths that have been populated with
294  // geomSubset children. This is purely an optimization and
295  // not authoritative -- it may have false positioves, such as
296  // if subsets are removed later. These hints provide a way
297  // to skip the expense of _GatherGeomSubsets() when no subsets
298  // have been populated.
299  std::unordered_set<SdfPath, SdfPath::Hash> _geomSubsetParents;
300 
301  // Cache for rprim locator set -> dirty bits translation.
302  HdDataSourceLocatorSet _cachedLocatorSet;
303  HdDirtyBits _cachedDirtyBits;
304  TfToken _cachedPrimType;
305 };
306 
308 
309 #endif
std::shared_ptr< SdfPathVector > HdIdVectorSharedPtr
A shared pointer to a vector of id's.
Definition: sceneDelegate.h:38
GfMatrix4d GetInstancerTransform(SdfPath const &instancerId) override
Returns the instancer transform.
HdCullStyle
Definition: enums.h:105
VtValue Get(SdfPath const &id, TfToken const &key) override
Returns a named value.
PXR_NAMESPACE_OPEN_SCOPE HD_API HdExtComputationPrimvarDescriptor HdExtComputationPrimvarDescriptorFromSchema(TfToken const &name, HdExtComputationPrimvarSchema primvar)
void PrimsDirtied(const HdSceneIndexBase &sender, const DirtiedPrimEntries &entries) override
HdIdVectorSharedPtr GetCoordSysBindings(SdfPath const &id) override
Returns the coordinate system bindings, or a nullptr if none are bound.
uint32_t HdDirtyBits
Definition: types.h:143
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
VtIntArray GetInstanceIndices(SdfPath const &instancerId, SdfPath const &prototypeId) override
size_t SampleTransform(SdfPath const &id, size_t maxSampleCount, float *sampleTimes, GfMatrix4d *sampleValues) override
HdExtComputationOutputDescriptorVector GetExtComputationOutputDescriptors(SdfPath const &computationId) override
SdfPath GetDataSharingId(SdfPath const &primId) override
VtValue GetLightParamValue(SdfPath const &id, TfToken const &paramName) override
std::vector< HdExtComputationInputDescriptor > HdExtComputationInputDescriptorVector
HdPrimvarDescriptorVector GetPrimvarDescriptors(SdfPath const &id, HdInterpolation interpolation) override
Returns descriptors for all primvars of the given interpolation type.
#define HD_API
Definition: api.h:23
HdBasisCurvesTopology GetBasisCurvesTopology(SdfPath const &id) override
Gets the topological curve data for a given prim.
std::vector< HdExtComputationOutputDescriptor > HdExtComputationOutputDescriptorVector
HdVolumeFieldDescriptorVector GetVolumeFieldDescriptors(SdfPath const &volumeId) override
VtValue GetShadingStyle(SdfPath const &id) override
Returns the shading style for the given prim.
void PrimsRemoved(const HdSceneIndexBase &sender, const RemovedPrimEntries &entries) override
SdfPath GetInstancerId(SdfPath const &primId) override
Returns the parent instancer of the given rprim or instancer.
void Sync(HdSyncRequestVector *request) override
Synchronizes the delegate state for the given request vector.
std::string GetExtComputationKernel(SdfPath const &computationId) override
std::vector< VtArray< TfToken > > GetInstanceCategories(SdfPath const &instancerId) override
Returns the categories for each of the instances in the instancer.
VtValue GetMaterialResource(SdfPath const &id) override
Returns the material ID bound to the rprim rprimId.
VtValue GetVolumeParamValue(SdfPath const &id, TfToken const &paramName) override
GfMatrix4d GetTransform(SdfPath const &id) override
Returns the object space transform, including all parent transforms.
size_t SamplePrimvar(SdfPath const &id, TfToken const &key, size_t maxSampleCount, float *sampleTimes, VtValue *sampleValues) override
Definition: token.h:70
VtValue GetExtComputationInput(SdfPath const &computationId, TfToken const &input) override
size_t SampleInstancerTransform(SdfPath const &instancerId, size_t maxSampleCount, float *sampleTimes, GfMatrix4d *sampleValues) override
std::vector< class SdfPath > SdfPathVector
HdDisplayStyle GetDisplayStyle(SdfPath const &id) override
Returns the display style for the given prim.
void PrimsAdded(const HdSceneIndexBase &sender, const AddedPrimEntries &entries) override
HdCullStyle GetCullStyle(SdfPath const &id) override
Returns the cullstyle for the given prim.
GfRange3d GetExtent(SdfPath const &id) override
VtValue GetIndexedPrimvar(SdfPath const &id, TfToken const &key, VtIntArray *outIndices) override
size_t SampleIndexedPrimvar(SdfPath const &id, TfToken const &key, size_t maxNumSamples, float *times, VtValue *samples, VtIntArray *sampleIndices) override
HdExtComputationInputDescriptorVector GetExtComputationInputDescriptors(SdfPath const &computationId) override
void PrimsRenamed(const HdSceneIndexBase &sender, const RenamedPrimEntries &entries) override
GLuint id
Definition: glcorearb.h:655
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
HdSceneIndexAdapterSceneDelegate(HdSceneIndexBaseRefPtr inputSceneIndex, HdRenderIndex *parentIndex, SdfPath const &delegateID)
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:280
std::vector< HdVolumeFieldDescriptor > HdVolumeFieldDescriptorVector
HdRenderBufferDescriptor GetRenderBufferDescriptor(SdfPath const &id) override
Returns the allocation descriptor for a given render buffer prim.
GLsizei samples
Definition: glcorearb.h:1298
VtArray< TfToken > GetCategories(SdfPath const &id) override
void InvokeExtComputation(SdfPath const &computationId, HdExtComputationContext *context) override
bool GetVisible(SdfPath const &id) override
Returns the authored visible state of the prim.
HdInterpolation
Definition: enums.h:177
PxOsdSubdivTags GetSubdivTags(SdfPath const &id) override
Gets the subdivision surface tags (sharpness, holes, etc).
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TfTokenVector GetTaskRenderTags(SdfPath const &taskId) override
SdfPath GetMaterialId(SdfPath const &id) override
Returns the material ID bound to the rprim rprimId.
TfTokenVector GetExtComputationSceneInputNames(SdfPath const &computationId) override
std::vector< HdPrimvarDescriptor > HdPrimvarDescriptorVector
HD_API HdPrimvarDescriptor HdPrimvarDescriptorFromSchema(TfToken const &name, HdPrimvarSchema primvar)
Convert a primvar descriptor from Hydra 2 to Hydra 1 representation...
TfToken GetRenderTag(SdfPath const &id) override
bool GetDoubleSided(SdfPath const &id) override
Returns the doubleSided state for the given prim.
HdExtComputationPrimvarDescriptorVector GetExtComputationPrimvarDescriptors(SdfPath const &id, HdInterpolation interpolationMode) override
SdfPathVector GetInstancerPrototypes(SdfPath const &instancerId) override
Definition: value.h:89
VtValue GetCameraParamValue(SdfPath const &cameraId, TfToken const &paramName) override
HdMeshTopology GetMeshTopology(SdfPath const &id) override
Gets the topological mesh data for a given prim.
size_t SampleExtComputationInput(SdfPath const &computationId, TfToken const &input, size_t maxSampleCount, float *sampleTimes, VtValue *sampleValues) override
std::vector< HdExtComputationPrimvarDescriptor > HdExtComputationPrimvarDescriptorVector
HdReprSelector GetReprSelector(SdfPath const &id) override
Returns the authored repr (if any) for the given prim.