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