HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rprim.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_IMAGING_HD_RPRIM_H
25 #define PXR_IMAGING_HD_RPRIM_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hd/api.h"
29 #include "pxr/imaging/hd/version.h"
33 #include "pxr/imaging/hd/repr.h"
35 #include "pxr/imaging/hd/types.h"
36 #include "pxr/usd/sdf/path.h"
37 #include "pxr/base/gf/range3d.h"
38 #include "pxr/base/arch/inttypes.h"
39 
40 #include <memory>
41 #include <vector>
42 
44 
45 
46 class HdBufferSource;
47 class HdDrawItem;
48 class HdMaterial;
49 class HdRenderIndex;
50 class HdRenderParam;
51 
52 using HdReprSharedPtr = std::shared_ptr<HdRepr>;
53 
54 using HdBufferSourceSharedPtr = std::shared_ptr<HdBufferSource>;
55 using HdBufferSourceSharedPtrVector = std::vector<HdBufferSourceSharedPtr>;
56 
57 using HdBufferSpecVector = std::vector<struct HdBufferSpec>;
58 using HdBufferArrayRangeSharedPtr = std::shared_ptr<HdBufferArrayRange>;
59 
60 using HdComputationSharedPtr = std::shared_ptr<class HdComputation>;
61 using HdComputationSharedPtrVector = std::vector<HdComputationSharedPtr>;
62 
63 /// \class HdRprim
64 ///
65 /// The render engine state for a given rprim from the scene graph. All data
66 /// access (aside from local caches) is delegated to the HdSceneDelegate.
67 ///
68 class HdRprim {
69 public:
70  HD_API
71  HdRprim(SdfPath const& id);
72 
73  HD_API
74  virtual ~HdRprim();
75 
76  // ---------------------------------------------------------------------- //
77  /// \name Rprim Hydra Engine API : Pre-Sync & Sync-Phase
78  // ---------------------------------------------------------------------- //
79 
80  /// Returns the set of dirty bits that should be
81  /// added to the change tracker for this prim, when this prim is inserted.
82  virtual HdDirtyBits GetInitialDirtyBitsMask() const = 0;
83 
84  /// This function gives an Rprim the chance to "early exit" from dirty
85  /// bit propagation, delegate sync and rprim sync altogether. It is
86  /// a temporary measure to prevent unnecessary work, like in the case of
87  /// invisible prims. The dirty bits in the change tracker remain the same.
88  /// See the implementation for the finer details.
89  HD_API
91 
92  /// This function gives an Rprim the chance to set additional dirty bits
93  /// based on those set in the change tracker, before passing the dirty bits
94  /// to the scene delegate.
95  /// It calls into _PropagateDirtyBits, which gives the Rprim an opportunity
96  /// to specify the additional data needed to process the requested changes.
97  ///
98  /// The return value is the new set of dirty bits.
99  HD_API
101 
102  /// Initialize the representation of this Rprim by calling _InitRepr.
103  /// This is called prior to dirty bit propagation & sync, the first time the
104  /// repr is used, or when the authored representation is dirty.
105  ///
106  /// dirtyBits is an in/out value. It is initialized to the dirty bits
107  /// from the change tracker. InitRepr can then set additional dirty bits
108  /// if additional data is required from the scene delegate when this
109  /// repr is synced.
110  HD_API
111  void InitRepr(HdSceneDelegate* delegate,
112  TfToken const &reprToken,
113  HdDirtyBits *dirtyBits);
114 
115  /// Pull invalidated scene data and prepare/update the renderable
116  /// representation.
117  ///
118  /// This function is told which scene data to pull through the
119  /// dirtyBits parameter. The first time it's called, dirtyBits comes
120  /// from _GetInitialDirtyBits(), which provides initial dirty state,
121  /// but after that it's driven by invalidation tracking in the scene
122  /// delegate.
123  ///
124  /// The contract for this function is that the prim can only pull on scene
125  /// delegate buffers that are marked dirty. Scene delegates can and do
126  /// implement just-in-time data schemes that mean that pulling on clean
127  /// data will be at best incorrect, and at worst a crash.
128  ///
129  /// This function is called in parallel from worker threads, so it needs
130  /// to be threadsafe; calls into HdSceneDelegate are ok.
131  ///
132  /// \param sceneDelegate The data source for this geometry item.
133  /// \param renderParam A render delegate object that holds rendering
134  /// parameters that scene geometry may use.
135  /// \param dirtyBits A specifier for which scene data has changed.
136  /// \param reprToken The representation that needs to be updated. This is
137  /// useful for backends that support multiple display
138  /// representations for an rprim. A given representation
139  /// may choose to pull on a subset of the dirty state.
140  /// \param dirtyBits On input specifies which state is dirty and can be
141  /// pulled from the scene delegate.
142  /// On output specifies which bits are still dirty and
143  /// were not cleaned by the sync.
144  virtual void Sync(HdSceneDelegate *delegate,
145  HdRenderParam *renderParam,
146  HdDirtyBits *dirtyBits,
147  TfToken const &reprToken) = 0;
148  // ---------------------------------------------------------------------- //
149  /// \name Rprim Hydra Engine API : Execute Phase
150  // ---------------------------------------------------------------------- //
151 
152  /// Returns the draw items for the requested repr token, if any.
153  /// These draw items should be constructed and cached beforehand by Sync().
154  /// If no draw items exist, or reprToken cannot be found, nullptr will be
155  /// returned.
156  HD_API
158  GetDrawItems(TfToken const& reprToken) const;
159 
160  // ---------------------------------------------------------------------- //
161  /// \name Rprim Hydra Engine API : Cleanup
162  // ---------------------------------------------------------------------- //
163  /// Finalizes object resources. This function might not delete resources,
164  /// but it should deal with resource ownership so that the rprim is
165  /// deletable.
166  HD_API
167  virtual void Finalize(HdRenderParam *renderParam);
168 
169  // ---------------------------------------------------------------------- //
170  /// \name Rprim Data API
171  // ---------------------------------------------------------------------- //
172 
173  /// Returns the identifier of this Rprim. This is both used in the
174  /// RenderIndex and the SceneDelegate and acts as the associative key for
175  /// the Rprim in both contexts.
176  SdfPath const& GetId() const { return _sharedData.rprimID; }
177 
178  /// Return the unique instance id
179  int32_t GetPrimId() const { return _primId; };
180 
181  /// Set the unique instance id
182  HD_API
183  void SetPrimId(int32_t primId);
184 
185  /// Returns the identifier of the instancer (if any) for this Rprim. If this
186  /// Rprim is not instanced, an empty SdfPath will be returned.
187  SdfPath const& GetInstancerId() const { return _instancerId; }
188 
189  /// Returns the path of the material to which this Rprim is bound. The
190  /// material object itself can be fetched from the RenderIndex using
191  /// this identifier.
192  SdfPath const& GetMaterialId() const { return _materialId; }
193 
194  /// Sets a new material binding to be used by this rprim
195  HD_API
196  void SetMaterialId(SdfPath const& materialId);
197 
198  /// The MaterialTag allows rprims to be organized into different
199  /// collections based on properties of the prim's material.
200  /// E.g. A renderer may wish to organize opaque and translucent prims
201  /// into different collections so they can be rendered seperately.
202  TfToken const& GetMaterialTag() const { return _sharedData.materialTag; }
203 
204  /// Sets the material tag used by the rprim.
205  HD_API
206  void SetMaterialTag(TfToken const& materialTag);
207 
209  return _authoredReprSelector;
210  }
211 
212  /// Returns the render tag associated to this rprim
213  inline TfToken GetRenderTag(HdSceneDelegate* delegate) const;
214 
215  /// Returns the bounds of the rprim in local, untransformed space.
216  inline GfRange3d GetExtent(HdSceneDelegate* delegate) const;
217 
218  /// Primvar Query
221  HdInterpolation interpolation) const;
222 
223  /// Returns the names of built-in primvars, i.e. primvars that
224  /// are part of the core geometric schema for this prim.
225  HD_API
226  virtual TfTokenVector const & GetBuiltinPrimvarNames() const = 0;
227 
228  inline VtValue
229  GetPrimvar(HdSceneDelegate* delegate, const TfToken &name) const;
230 
231  inline VtValue
232  GetIndexedPrimvar(HdSceneDelegate* delegate, const TfToken &name,
233  VtIntArray *indices) const;
234 
235  HD_API
236  VtMatrix4dArray GetInstancerTransforms(HdSceneDelegate* delegate);
237 
238  /// Returns true if any dirty flags are set for this rprim.
239  HD_API
240  bool IsDirty(HdChangeTracker &changeTracker) const;
241 
242  /// Is the prim itself visible
243  bool IsVisible() const { return _sharedData.visible; }
244 
245  HD_API
246  void UpdateReprSelector(HdSceneDelegate* delegate,
247  HdDirtyBits *dirtyBits);
248 
249 protected:
250  // ---------------------------------------------------------------------- //
251  /// \name Rprim Hydra Engine API : Pre-Sync & Sync-Phase
252  // ---------------------------------------------------------------------- //
253 
254  /// This callback from Rprim gives the prim an opportunity to set
255  /// additional dirty bits based on those already set. This is done
256  /// before the dirty bits are passed to the scene delegate, so can be
257  /// used to communicate that extra information is needed by the prim to
258  /// process the changes.
259  ///
260  /// The return value is the new set of dirty bits, which replaces the bits
261  /// passed in.
262  ///
263  /// See HdRprim::PropagateRprimDirtyBits()
264  virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const = 0;
265 
266  /// Initialize the given representation of this Rprim.
267  /// This is called prior to syncing the prim, the first time the repr
268  /// is used.
269  ///
270  /// reprToken is the name of the representation to initalize.
271  ///
272  /// dirtyBits is an in/out value. It is initialized to the dirty bits
273  /// from the change tracker. InitRepr can then set additional dirty bits
274  /// if additional data is required from the scene delegate when this
275  /// repr is synced. InitRepr occurs before dirty bit propagation.
276  ///
277  /// See HdRprim::InitRepr()
278  virtual void _InitRepr(TfToken const &reprToken,
279  HdDirtyBits *dirtyBits) = 0;
280 
281  // ---------------------------------------------------------------------- //
282  /// \name Rprim Shared API
283  // ---------------------------------------------------------------------- //
284  HD_API
285  HdReprSharedPtr const & _GetRepr(TfToken const &reprToken) const;
286 
287  HD_API
288  void _UpdateVisibility(HdSceneDelegate *sceneDelegate,
289  HdDirtyBits *dirtyBits);
290 
291  HD_API
292  void _UpdateInstancer(HdSceneDelegate *sceneDelegate,
293  HdDirtyBits *dirtyBits);
294 
295 private:
296  SdfPath _instancerId;
297  SdfPath _materialId;
298 
299  // Used for id renders.
300  int32_t _primId;
301 
302 protected:
303  // shared data across reprs: bufferArrayRanges, bounds, visibility
305 
306  // authored repr selector
308 
309  // total number of reprs is relatively small (less than 5 or so
310  // in most case), we use linear container for efficiency.
311  typedef std::vector<std::pair<TfToken, HdReprSharedPtr> >
314 
316  _ReprComparator(TfToken const &name) : _name(name) {}
317  bool operator() (const std::pair<TfToken, HdReprSharedPtr> &e) const {
318  return _name == e.first;
319  }
320  private:
321  TfToken _name;
322  };
323 
324 
325  // Repr configuration descriptors. All concrete types (HdMesh, HdPoints ..)
326  // have this static map to lookup descriptors for the given reprToken.
327  //
328  // N : # of descriptors for the repr.
329  //
330  template<typename DESC_TYPE, int N=1>
332  typedef std::array<DESC_TYPE, N> DescArray;
333  static const int MAX_DESCS = N;
334 
335  DescArray Find(TfToken const &reprToken) const {
336  // linear search, we expect only a handful reprs configured.
337  TF_FOR_ALL (it, _configs) {
338  if (it->first == reprToken) return it->second;
339  }
340  TF_CODING_ERROR("Repr %s not found", reprToken.GetText());
341  return DescArray();
342  }
343  void AddOrUpdate(TfToken const &reprToken, DescArray descs) {
344  for (auto& config : _configs) {
345  if (config.first == reprToken) {
346  // Overrwrite the existing entry.
347  config.second = descs;
348  return;
349  }
350  }
351  _configs.push_back(std::make_pair(reprToken, descs));
352  }
353  std::vector<std::pair<TfToken, DescArray> > _configs;
354  };
355 
356 };
357 
358 ////////////////////////////////////////////////////////////////////////////////
359 //
360 // Delegate API Wrappers
361 //
362 TfToken
364 {
365  return delegate->GetRenderTag(GetId());
366 }
367 
368 GfRange3d
370 {
371  return delegate->GetExtent(GetId());
372 }
373 
376  HdInterpolation interpolation) const
377 {
378  return delegate->GetPrimvarDescriptors(GetId(), interpolation);
379 }
380 
381 inline VtValue
383 {
384  return delegate->Get(GetId(), name);
385 }
386 
387 inline VtValue
389  VtIntArray *indices) const
390 {
391  return delegate->GetIndexedPrimvar(GetId(), name, indices);
392 }
393 
395 
396 #endif //PXR_IMAGING_HD_RPRIM_H
bool IsVisible() const
Is the prim itself visible.
Definition: rprim.h:243
HD_API void SetMaterialTag(TfToken const &materialTag)
Sets the material tag used by the rprim.
SdfPath const & GetInstancerId() const
Definition: rprim.h:187
virtual HD_API VtValue GetIndexedPrimvar(SdfPath const &id, TfToken const &key, VtIntArray *outIndices)
HD_API const HdRepr::DrawItemUniquePtrVector & GetDrawItems(TfToken const &reprToken) const
virtual void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits)=0
uint32_t HdDirtyBits
Definition: types.h:127
#define TF_CODING_ERROR
DescArray Find(TfToken const &reprToken) const
Definition: rprim.h:335
virtual HD_API TfTokenVector const & GetBuiltinPrimvarNames() const =0
#define HD_API
Definition: api.h:40
std::shared_ptr< HdRepr > HdReprSharedPtr
Definition: rprim.h:52
std::vector< HdBufferSourceSharedPtr > HdBufferSourceSharedPtrVector
Definition: bufferSource.h:44
GLuint const GLchar * name
Definition: glcorearb.h:785
std::vector< std::pair< TfToken, DescArray > > _configs
Definition: rprim.h:353
SdfPath const & GetMaterialId() const
Definition: rprim.h:192
HD_API void _UpdateInstancer(HdSceneDelegate *sceneDelegate, HdDirtyBits *dirtyBits)
HD_API HdReprSharedPtr const & _GetRepr(TfToken const &reprToken) const
int32_t GetPrimId() const
Return the unique instance id.
Definition: rprim.h:179
HD_API void SetPrimId(int32_t primId)
Set the unique instance id.
Definition: token.h:87
static const int MAX_DESCS
Definition: rprim.h:333
virtual HdDirtyBits GetInitialDirtyBitsMask() const =0
TfToken GetRenderTag(HdSceneDelegate *delegate) const
Returns the render tag associated to this rprim.
Definition: rprim.h:363
Definition: rprim.h:68
HdRprimSharedData _sharedData
Definition: rprim.h:304
bool operator()(const std::pair< TfToken, HdReprSharedPtr > &e) const
Definition: rprim.h:317
HdPrimvarDescriptorVector GetPrimvarDescriptors(HdSceneDelegate *delegate, HdInterpolation interpolation) const
Primvar Query.
Definition: rprim.h:375
VtValue GetPrimvar(HdSceneDelegate *delegate, const TfToken &name) const
Definition: rprim.h:382
HD_API HdRprim(SdfPath const &id)
GLsizei GLenum const void * indices
Definition: glcorearb.h:405
virtual void Sync(HdSceneDelegate *delegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits, TfToken const &reprToken)=0
virtual HD_API HdPrimvarDescriptorVector GetPrimvarDescriptors(SdfPath const &id, HdInterpolation interpolation)
Returns descriptors for all primvars of the given interpolation type.
std::shared_ptr< class HdComputation > HdComputationSharedPtr
Definition: computation.h:41
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
virtual HD_API GfRange3d GetExtent(SdfPath const &id)
virtual HD_API TfToken GetRenderTag(SdfPath const &id)
Definition: path.h:288
std::vector< struct HdBufferSpec > HdBufferSpecVector
char const * GetText() const
Definition: token.h:196
HdReprSelector _authoredReprSelector
Definition: rprim.h:307
HD_API bool IsDirty(HdChangeTracker &changeTracker) const
Returns true if any dirty flags are set for this rprim.
virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const =0
virtual HD_API void Finalize(HdRenderParam *renderParam)
virtual HD_API VtValue Get(SdfPath const &id, TfToken const &key)
Returns a named value.
std::vector< DrawItemUniquePtr > DrawItemUniquePtrVector
Definition: repr.h:160
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
VtValue GetIndexedPrimvar(HdSceneDelegate *delegate, const TfToken &name, VtIntArray *indices) const
Definition: rprim.h:388
SdfPath const & GetId() const
Definition: rprim.h:176
_ReprComparator(TfToken const &name)
Definition: rprim.h:316
HdReprSelector const & GetReprSelector() const
Definition: rprim.h:208
_ReprVector _reprs
Definition: rprim.h:313
HD_API void SetMaterialId(SdfPath const &materialId)
Sets a new material binding to be used by this rprim.
HD_API void _UpdateVisibility(HdSceneDelegate *sceneDelegate, HdDirtyBits *dirtyBits)
TfToken const & GetMaterialTag() const
Definition: rprim.h:202
HdInterpolation
Definition: enums.h:194
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
GA_API const UT_StringHolder N
void AddOrUpdate(TfToken const &reprToken, DescArray descs)
Definition: rprim.h:343
#define TF_FOR_ALL(iter, c)
Definition: iterator.h:390
std::array< DESC_TYPE, N > DescArray
Definition: rprim.h:332
std::vector< HdComputationSharedPtr > HdComputationSharedPtrVector
Definition: computation.h:42
HD_API void UpdateReprSelector(HdSceneDelegate *delegate, HdDirtyBits *dirtyBits)
std::vector< HdPrimvarDescriptor > HdPrimvarDescriptorVector
HD_API void InitRepr(HdSceneDelegate *delegate, TfToken const &reprToken, HdDirtyBits *dirtyBits)
std::vector< std::pair< TfToken, HdReprSharedPtr > > _ReprVector
Definition: rprim.h:312
virtual HD_API ~HdRprim()
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
HD_API VtMatrix4dArray GetInstancerTransforms(HdSceneDelegate *delegate)
HD_API bool CanSkipDirtyBitPropagationAndSync(HdDirtyBits bits) const
Definition: value.h:168
GfRange3d GetExtent(HdSceneDelegate *delegate) const
Returns the bounds of the rprim in local, untransformed space.
Definition: rprim.h:369
HD_API HdDirtyBits PropagateRprimDirtyBits(HdDirtyBits bits)