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 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_IMAGING_HD_SCENE_INDEX_ADAPTER_SCENE_DELEGATE_H
25 #define PXR_IMAGING_HD_SCENE_INDEX_ADAPTER_SCENE_DELEGATE_H
26 
29 #include "pxr/usd/sdf/pathTable.h"
30 #include <thread>
31 #include <tbb/concurrent_unordered_map.h>
32 
34 
35 /// \class HdSceneIndexAdapterSceneDelegate
36 ///
37 /// Scene delegate which observes notices from an HdSceneIndex and applies them
38 /// to an HdRenderIndex. This serves as "back-end" emulation in order for
39 /// scenes described via the HdSceneIndex/HdDataSource APIs to be accessible
40 /// by legacy render delegates.
41 ///
43  : public HdSceneDelegate
44  , public HdSceneIndexObserver
45 {
46 public:
47 
49  HdSceneIndexBaseRefPtr inputSceneIndex,
50  HdRenderIndex *parentIndex,
51  SdfPath const &delegateID);
52 
54 
55  // ------------------------------------------------------------------------
56 
57  /// Returns the end of a scene index chain containing the filters
58  /// necessary for input to an instance of this scene delegate.
59  static HdSceneIndexBaseRefPtr AppendDefaultSceneFilters(
60  HdSceneIndexBaseRefPtr inputSceneIndex, SdfPath const &delegateID);
61 
62  // satisfying HdSceneIndexObserver ----------------------------------------
63  void PrimsAdded(
64  const HdSceneIndexBase &sender,
65  const AddedPrimEntries &entries) override;
66 
67  void PrimsRemoved(
68  const HdSceneIndexBase &sender,
69  const RemovedPrimEntries &entries) override;
70 
71  void PrimsDirtied(
72  const HdSceneIndexBase &sender,
73  const DirtiedPrimEntries &entries) override;
74 
75  void PrimsRenamed(
76  const HdSceneIndexBase &sender,
77  const RenamedPrimEntries &entries) override;
78 
79  // ------------------------------------------------------------------------
80  // HdSceneIndexDelegate API
81 
82  // ------------------------------------------------------------------------
83  // Rprim API
84 
85  HdMeshTopology GetMeshTopology(SdfPath const &id) override;
87  PxOsdSubdivTags GetSubdivTags(SdfPath const &id) override;
88  GfRange3d GetExtent(SdfPath const &id) override;
89  bool GetVisible(SdfPath const &id) override;
90  bool GetDoubleSided(SdfPath const &id) override;
91  HdCullStyle GetCullStyle(SdfPath const &id) override;
92  VtValue GetShadingStyle(SdfPath const &id) override;
93  HdDisplayStyle GetDisplayStyle(SdfPath const &id) override;
94  HdReprSelector GetReprSelector(SdfPath const &id) override;
95  TfToken GetRenderTag(SdfPath const &id) override;
96  VtArray<TfToken> GetCategories(SdfPath const &id) override;
98  SdfPath const &volumeId) override;
99 
100  // ------------------------------------------------------------------------
101  // Transform API
102 
103  GfMatrix4d GetTransform(SdfPath const &id) override;
104  size_t SampleTransform(SdfPath const &id, 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 
113  // ------------------------------------------------------------------------
114  // Primvar API
115 
118  SdfPath const &id, HdInterpolation interpolation) override;
119 
120  VtValue Get(SdfPath const &id, TfToken const &key) override;
121  VtValue GetIndexedPrimvar(SdfPath const &id, TfToken const &key,
122  VtIntArray *outIndices) override;
123 
124  size_t SamplePrimvar(SdfPath const &id, TfToken const &key,
125  size_t maxSampleCount, float *sampleTimes,
126  VtValue *sampleValues) override;
127  size_t SampleIndexedPrimvar(SdfPath const &id, TfToken const &key,
128  size_t maxNumSamples, float *times, VtValue *samples,
129  VtIntArray *sampleIndices) override;
130 
131 
132  // ------------------------------------------------------------------------
133  // Instancer API
134 
135  std::vector<VtArray<TfToken>> GetInstanceCategories(
136  SdfPath const &instancerId) override;
137  VtIntArray GetInstanceIndices(
138  SdfPath const &instancerId, SdfPath const &prototypeId) override;
139  SdfPath GetInstancerId(SdfPath const &primId) override;
140  SdfPathVector GetInstancerPrototypes(SdfPath const &instancerId) override;
141 
142  // ------------------------------------------------------------------------
143  // Path Translation API
144 
145  SdfPath GetDataSharingId(SdfPath const& primId) override;
146 
147  // ------------------------------------------------------------------------
148  // Material API
149 
150  SdfPath GetMaterialId(SdfPath const &id) override;
151  VtValue GetMaterialResource(SdfPath const &id) override;
152  HdIdVectorSharedPtr GetCoordSysBindings(SdfPath const &id) override;
153 
154  // ------------------------------------------------------------------------
155  // Renderbuffer API
156 
158  SdfPath const &id) override;
159 
160  // ------------------------------------------------------------------------
161  // Light API
162 
164  TfToken const &paramName) override;
165 
166  // ------------------------------------------------------------------------
167  // Camera API
168 
169  VtValue GetCameraParamValue(SdfPath const &cameraId,
170  TfToken const &paramName) override;
171 
172  // ------------------------------------------------------------------------
173  // ExtComputation API
174 
175  // ... on the rprim
178  SdfPath const &id, HdInterpolation interpolationMode) override;
179 
180  // ... on the sprim
182  SdfPath const &computationId) override;
184  SdfPath const &computationId, TfToken const &input) override;
186  SdfPath const &computationId,
187  TfToken const &input,
188  size_t maxSampleCount,
189  float *sampleTimes,
190  VtValue *sampleValues) override;
191 
193  SdfPath const &computationId) override;
195  SdfPath const &computationId) override;
196 
197  std::string GetExtComputationKernel(SdfPath const &computationId) override;
198  void InvokeExtComputation(SdfPath const &computationId,
199  HdExtComputationContext *context) override;
200 
201  void Sync(HdSyncRequestVector* request) override;
202  void PostSyncCleanup() override;
203 
204  // NOTE: The remaining scene delegate functions aren't used for emulation:
205  // - GetTaskRenderTags
206  // - GetScenePrimPath
207  // - IsEnabled
208 
209 private:
210  // Compute and return an HdSceneIndexPrim from the input scene index.
211  // Uses a per-thread single-entry cache to re-use this computation
212  // across sequential Get...() calls in the public API. This API returns
213  // the prim by value rather than reference because callers may
214  // indirectly re-invoke _GetInputPrim() on the same thread, but with
215  // a different id path, if they make use of a TBB work queue.
216  HdSceneIndexPrim _GetInputPrim(SdfPath const& id);
217 
218  using _InputPrimCacheEntry = std::pair<SdfPath, HdSceneIndexPrim>;
219 
220  // A cache of the last prim accessed, per thread
221  tbb::concurrent_unordered_map<std::thread::id, _InputPrimCacheEntry,
222  std::hash<std::thread::id> > _inputPrimCache;
223 
224  void _PrimAdded(
225  const SdfPath &primPath,
226  const TfToken &primType);
227 
228  VtValue _GetPrimvar(SdfPath const &id, TfToken const &key,
229  VtIntArray *outIndices);
230 
231  VtValue _GetPrimvar(
232  const HdContainerDataSourceHandle &primvarsDataSource,
233  TfToken const &key,
234  VtIntArray *outIndices);
235 
236 
237  size_t _SamplePrimvar(SdfPath const &id, TfToken const &key,
238  size_t maxNumSamples, float *times, VtValue *samples,
239  VtIntArray *sampleIndices);
240 
241  HdSceneIndexBaseRefPtr _inputSceneIndex;
242 
243  struct _PrimCacheEntry
244  {
245  _PrimCacheEntry()
246  : primvarDescriptorsState(ReadStateUnread)
247  , extCmpPrimvarDescriptorsState(ReadStateUnread)
248  {}
249 
250  _PrimCacheEntry(const _PrimCacheEntry &rhs)
251  {
252  primType = rhs.primType;
253  primvarDescriptorsState.store(rhs.primvarDescriptorsState.load());
254  extCmpPrimvarDescriptorsState.store(
255  rhs.extCmpPrimvarDescriptorsState.load());
256  }
257 
258  TfToken primType;
259 
260  enum ReadState : unsigned char {
261  ReadStateUnread = 0,
262  ReadStateReading,
263  ReadStateRead,
264  };
265 
266  std::atomic<ReadState> primvarDescriptorsState;
267  std::atomic<ReadState> extCmpPrimvarDescriptorsState;
268  std::map<HdInterpolation, HdPrimvarDescriptorVector>
269  primvarDescriptors;
270  std::map<HdInterpolation, HdExtComputationPrimvarDescriptorVector>
271  extCmpPrimvarDescriptors;
272  };
273 
275  _PrimCacheTable _primCache;
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:55
GfMatrix4d GetInstancerTransform(SdfPath const &instancerId) override
Returns the instancer transform.
HdCullStyle
Definition: enums.h:122
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:158
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
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 all 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:87
VtValue GetExtComputationInput(SdfPath const &computationId, TfToken const &input) override
size_t SampleInstancerTransform(SdfPath const &instancerId, size_t maxSampleCount, float *sampleTimes, GfMatrix4d *sampleValues) override
HdDisplayStyle GetDisplayStyle(SdfPath const &id) override
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:457
HdSceneIndexAdapterSceneDelegate(HdSceneIndexBaseRefPtr inputSceneIndex, HdRenderIndex *parentIndex, SdfPath const &delegateID)
Definition: path.h:290
std::vector< HdVolumeFieldDescriptor > HdVolumeFieldDescriptorVector
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:211
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
Returns the prim categories.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
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:194
PxOsdSubdivTags GetSubdivTags(SdfPath const &id) override
Gets the subdivision surface tags (sharpness, holes, etc).
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
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:164
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.