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 
17 
18 /// \class HdSceneIndexAdapterSceneDelegate
19 ///
20 /// Scene delegate which observes notices from an HdSceneIndex and applies them
21 /// to an HdRenderIndex. This serves as "back-end" emulation in order for
22 /// scenes described via the HdSceneIndex/HdDataSource APIs to be accessible
23 /// by legacy render delegates.
24 ///
26  : public HdSceneDelegate
27  , public HdSceneIndexObserver
28 {
29 public:
30 
32  HdSceneIndexBaseRefPtr inputSceneIndex,
33  HdRenderIndex *parentIndex,
34  SdfPath const &delegateID);
35 
37 
38  // ------------------------------------------------------------------------
39 
40  /// Returns the end of a scene index chain containing the filters
41  /// necessary for input to an instance of this scene delegate.
42  static HdSceneIndexBaseRefPtr AppendDefaultSceneFilters(
43  HdSceneIndexBaseRefPtr inputSceneIndex, SdfPath const &delegateID);
44 
45  // satisfying HdSceneIndexObserver ----------------------------------------
46  void PrimsAdded(
47  const HdSceneIndexBase &sender,
48  const AddedPrimEntries &entries) override;
49 
50  void PrimsRemoved(
51  const HdSceneIndexBase &sender,
52  const RemovedPrimEntries &entries) override;
53 
54  void PrimsDirtied(
55  const HdSceneIndexBase &sender,
56  const DirtiedPrimEntries &entries) override;
57 
58  void PrimsRenamed(
59  const HdSceneIndexBase &sender,
60  const RenamedPrimEntries &entries) override;
61 
62  // ------------------------------------------------------------------------
63  // HdSceneIndexDelegate API
64 
65  // ------------------------------------------------------------------------
66  // Rprim API
67 
68  HdMeshTopology GetMeshTopology(SdfPath const &id) override;
70  PxOsdSubdivTags GetSubdivTags(SdfPath const &id) override;
71  GfRange3d GetExtent(SdfPath const &id) override;
72  bool GetVisible(SdfPath const &id) override;
73  bool GetDoubleSided(SdfPath const &id) override;
74  HdCullStyle GetCullStyle(SdfPath const &id) override;
75  VtValue GetShadingStyle(SdfPath const &id) override;
76  HdDisplayStyle GetDisplayStyle(SdfPath const &id) override;
77  HdReprSelector GetReprSelector(SdfPath const &id) override;
78  TfToken GetRenderTag(SdfPath const &id) override;
79  VtArray<TfToken> GetCategories(SdfPath const &id) override;
81  SdfPath const &volumeId) override;
82 
83  // ------------------------------------------------------------------------
84  // Transform API
85 
86  GfMatrix4d GetTransform(SdfPath const &id) override;
87  size_t SampleTransform(SdfPath const &id, size_t maxSampleCount,
88  float *sampleTimes, GfMatrix4d *sampleValues) override;
89  size_t SampleTransform(SdfPath const &id,
90  float startTime, float endTime,
91  size_t maxSampleCount,
92  float *sampleTimes, GfMatrix4d *sampleValues) override;
93 
95  SdfPath const &instancerId) override;
96  size_t SampleInstancerTransform(SdfPath const &instancerId,
97  size_t maxSampleCount, float *sampleTimes,
98  GfMatrix4d *sampleValues) override;
99  size_t SampleInstancerTransform(SdfPath const &instancerId,
100  float startTime, float endTime,
101  size_t maxSampleCount, float *sampleTimes,
102  GfMatrix4d *sampleValues) override;
103 
104  // ------------------------------------------------------------------------
105  // Primvar API
106 
109  SdfPath const &id, HdInterpolation interpolation) override;
110 
111  VtValue Get(SdfPath const &id, TfToken const &key) override;
112  VtValue GetIndexedPrimvar(SdfPath const &id, TfToken const &key,
113  VtIntArray *outIndices) override;
114 
115  size_t SamplePrimvar(SdfPath const &id, TfToken const &key,
116  size_t maxSampleCount, float *sampleTimes,
117  VtValue *sampleValues) override;
118  size_t SamplePrimvar(SdfPath const &id, TfToken const &key,
119  float startTime, float endTime,
120  size_t maxSampleCount, float *sampleTimes,
121  VtValue *sampleValues) override;
122  size_t SampleIndexedPrimvar(SdfPath const &id, TfToken const &key,
123  size_t maxNumSamples, float *times, VtValue *samples,
124  VtIntArray *sampleIndices) override;
125  size_t SampleIndexedPrimvar(SdfPath const &id, TfToken const &key,
126  float startTime, float endTime,
127  size_t maxNumSamples, float *times, VtValue *samples,
128  VtIntArray *sampleIndices) override;
129 
130  // ------------------------------------------------------------------------
131  // Instancer API
132 
133  std::vector<VtArray<TfToken>> GetInstanceCategories(
134  SdfPath const &instancerId) override;
135  VtIntArray GetInstanceIndices(
136  SdfPath const &instancerId, SdfPath const &prototypeId) override;
137  SdfPath GetInstancerId(SdfPath const &primId) override;
138  SdfPathVector GetInstancerPrototypes(SdfPath const &instancerId) override;
139 
140  // ------------------------------------------------------------------------
141  // Path Translation API
142 
143  SdfPath GetDataSharingId(SdfPath const& primId) override;
144 
145  // ------------------------------------------------------------------------
146  // Material API
147 
148  SdfPath GetMaterialId(SdfPath const &id) override;
149  VtValue GetMaterialResource(SdfPath const &id) override;
150  HdIdVectorSharedPtr GetCoordSysBindings(SdfPath const &id) override;
151 
152  // ------------------------------------------------------------------------
153  // Renderbuffer API
154 
156  SdfPath const &id) override;
157 
158  // ------------------------------------------------------------------------
159  // Light API
160 
162  TfToken const &paramName) override;
163 
164  // ------------------------------------------------------------------------
165  // Camera API
166 
167  VtValue GetCameraParamValue(SdfPath const &cameraId,
168  TfToken const &paramName) override;
169 
170  // ------------------------------------------------------------------------
171  // ExtComputation API
172 
173  // ... on the rprim
176  SdfPath const &id, HdInterpolation interpolationMode) override;
177 
178  // ... on the sprim
180  SdfPath const &computationId) override;
182  SdfPath const &computationId, TfToken const &input) override;
184  SdfPath const &computationId,
185  TfToken const &input,
186  size_t maxSampleCount,
187  float *sampleTimes,
188  VtValue *sampleValues) override;
190  SdfPath const &computationId,
191  TfToken const &input,
192  float startTime,
193  float endTime,
194  size_t maxSampleCount,
195  float *sampleTimes,
196  VtValue *sampleValues) override;
197 
199  SdfPath const &computationId) override;
201  SdfPath const &computationId) override;
202 
203  std::string GetExtComputationKernel(SdfPath const &computationId) override;
204  void InvokeExtComputation(SdfPath const &computationId,
205  HdExtComputationContext *context) override;
206 
207  TfTokenVector GetTaskRenderTags(SdfPath const &taskId) override;
208 
209  void Sync(HdSyncRequestVector* request) override;
210  void PostSyncCleanup() override;
211 
212  // NOTE: The remaining scene delegate functions aren't used for emulation:
213  // - GetScenePrimPath
214  // - IsEnabled
215 
216 private:
217  // Compute and return an HdSceneIndexPrim from the input scene index.
218  // Uses a per-thread single-entry cache to re-use this computation
219  // across sequential Get...() calls in the public API. This API returns
220  // the prim by value rather than reference because callers may
221  // indirectly re-invoke _GetInputPrim() on the same thread, but with
222  // a different id path, if they make use of a TBB work queue.
223  HdSceneIndexPrim _GetInputPrim(SdfPath const& id);
224 
225  using _InputPrimCacheEntry = std::pair<SdfPath, HdSceneIndexPrim>;
226 
227  // A cache of the last prim accessed, per thread
228  tbb::concurrent_unordered_map<std::thread::id, _InputPrimCacheEntry,
229  std::hash<std::thread::id> > _inputPrimCache;
230 
231  void _PrimAdded(
232  const SdfPath &primPath,
233  const TfToken &primType);
234 
235  VtValue _GetPrimvar(SdfPath const &id, TfToken const &key,
236  VtIntArray *outIndices);
237 
238  VtValue _GetPrimvar(
239  const HdContainerDataSourceHandle &primvarsDataSource,
240  TfToken const &key,
241  VtIntArray *outIndices);
242 
243  VtValue _GetImageShaderValue(
244  HdSceneIndexPrim prim,
245  const TfToken& key);
246 
247  size_t _SamplePrimvar(SdfPath const &id, TfToken const &key,
248  float startTime, float endTime,
249  size_t maxNumSamples, float *times, VtValue *samples,
250  VtIntArray *sampleIndices);
251 
252  HdSceneIndexBaseRefPtr _inputSceneIndex;
253 
254  struct _PrimCacheEntry
255  {
256  TfToken primType;
257 
258  using PrimvarDescriptorsArray =
259  std::array<HdPrimvarDescriptorVector, HdInterpolationCount>;
260  std::shared_ptr<PrimvarDescriptorsArray> primvarDescriptors;
261  using ExtCmpPrimvarDescriptorsArray =
264  std::shared_ptr<ExtCmpPrimvarDescriptorsArray> extCmpPrimvarDescriptors;
265  };
266 
268  _PrimCacheTable _primCache;
269 
270  std::shared_ptr<_PrimCacheEntry::PrimvarDescriptorsArray>
271  _ComputePrimvarDescriptors(
272  const HdContainerDataSourceHandle &primDataSource);
273  std::shared_ptr<_PrimCacheEntry::ExtCmpPrimvarDescriptorsArray>
274  _ComputeExtCmpPrimvarDescriptors(
275  const HdContainerDataSourceHandle &primDataSource);
276 
277  bool _sceneDelegatesBuilt;
278  std::vector<HdSceneDelegate*> _sceneDelegates;
279 
280  // Cache for rprim locator set -> dirty bits translation.
281  HdDataSourceLocatorSet _cachedLocatorSet;
282  HdDirtyBits _cachedDirtyBits;
283  TfToken _cachedPrimType;
284 };
285 
287 
288 #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.
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
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.
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.
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)
Definition: path.h:273
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
static HdSceneIndexBaseRefPtr AppendDefaultSceneFilters(HdSceneIndexBaseRefPtr inputSceneIndex, SdfPath const &delegateID)
VtArray< TfToken > GetCategories(SdfPath const &id) override
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
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
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:146
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.