All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rprim.h
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"
30 #include "pxr/imaging/hd/changeTracker.h"
31 #include "pxr/imaging/hd/drawItem.h"
32 #include "pxr/imaging/hd/rprimSharedData.h"
33 #include "pxr/imaging/hd/repr.h"
34 #include "pxr/imaging/hd/sceneDelegate.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 <boost/shared_ptr.hpp>
41 #include <vector>
42 
43 PXR_NAMESPACE_OPEN_SCOPE
44 
45 
46 class HdBufferSource;
47 class HdDrawItem;
48 class HdMaterial;
49 class HdRenderIndex;
50 class HdRenderParam;
51 
52 typedef boost::shared_ptr<HdRepr> HdReprSharedPtr;
53 typedef boost::shared_ptr<HdBufferSource> HdBufferSourceSharedPtr;
54 
55 typedef std::vector<struct HdBufferSpec> HdBufferSpecVector;
56 typedef boost::shared_ptr<class HdBufferSource> HdBufferSourceSharedPtr;
57 typedef std::vector<HdBufferSourceSharedPtr> HdBufferSourceVector;
58 typedef boost::shared_ptr<HdBufferArrayRange> HdBufferArrayRangeSharedPtr;
59 typedef boost::shared_ptr<class HdComputation> HdComputationSharedPtr;
60 typedef std::vector<HdComputationSharedPtr> HdComputationVector;
61 
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  // ---------------------------------------------------------------------- //
77  // ---------------------------------------------------------------------- //
78 
81  virtual HdDirtyBits GetInitialDirtyBitsMask() const = 0;
82 
88  HD_API
89  bool CanSkipDirtyBitPropagationAndSync(HdDirtyBits bits) const;
90 
98  HD_API
99  HdDirtyBits PropagateRprimDirtyBits(HdDirtyBits bits);
100 
109  HD_API
110  void InitRepr(HdSceneDelegate* delegate,
111  TfToken const &reprToken,
112  HdDirtyBits *dirtyBits);
113 
143  virtual void Sync(HdSceneDelegate *delegate,
144  HdRenderParam *renderParam,
145  HdDirtyBits *dirtyBits,
146  TfToken const &reprToken) = 0;
147  // ---------------------------------------------------------------------- //
149  // ---------------------------------------------------------------------- //
150 
155  using HdDrawItemPtrVector = std::vector<HdDrawItem*>;
156  HD_API
157  const HdDrawItemPtrVector*
158  GetDrawItems(TfToken const& reprToken) const;
159 
160  // ---------------------------------------------------------------------- //
162  // ---------------------------------------------------------------------- //
166  HD_API
167  virtual void Finalize(HdRenderParam *renderParam);
168 
169  // ---------------------------------------------------------------------- //
171  // ---------------------------------------------------------------------- //
172 
176  SdfPath const& GetId() const { return _sharedData.rprimID; }
177 
179  int32_t GetPrimId() const { return _primId; };
180 
182  HD_API
183  void SetPrimId(int32_t primId);
184 
187  SdfPath const& GetInstancerId() const { return _instancerId; }
188 
192  SdfPath const& GetMaterialId() const { return _materialId; }
193 
198  TfToken const& GetMaterialTag() const { return _sharedData.materialTag; }
199 
200  HdReprSelector const& GetReprSelector() const {
201  return _authoredReprSelector;
202  }
203 
205  inline TfToken GetRenderTag(HdSceneDelegate* delegate) const;
206 
208  inline GfRange3d GetExtent(HdSceneDelegate* delegate) const;
209 
211  inline HdPrimvarDescriptorVector
213  HdInterpolation interpolation) const;
214 
217  HD_API
218  virtual TfTokenVector const & GetBuiltinPrimvarNames() const = 0;
219 
220  inline VtValue
221  GetPrimvar(HdSceneDelegate* delegate, const TfToken &name) const;
222 
223  HD_API
224  VtMatrix4dArray GetInstancerTransforms(HdSceneDelegate* delegate);
225 
227  HD_API
228  bool IsDirty(HdChangeTracker &changeTracker) const;
229 
231  bool IsVisible() const { return _sharedData.visible; }
232 
233  HD_API
234  void UpdateReprSelector(HdSceneDelegate* delegate,
235  HdDirtyBits *dirtyBits);
236 
237 protected:
238  // ---------------------------------------------------------------------- //
240  // ---------------------------------------------------------------------- //
241 
252  virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const = 0;
253 
266  virtual void _InitRepr(TfToken const &reprToken,
267  HdDirtyBits *dirtyBits) = 0;
268 
269  // ---------------------------------------------------------------------- //
271  // ---------------------------------------------------------------------- //
272  HD_API
273  HdReprSharedPtr const & _GetRepr(TfToken const &reprToken) const;
274 
275  HD_API
276  void _UpdateVisibility(HdSceneDelegate *sceneDelegate,
277  HdDirtyBits *dirtyBits);
278 
280  HD_API
281  void _SetMaterialId(HdChangeTracker &changeTracker,
282  SdfPath const& materialId);
283 
284  // methods to assist allocating and migrating shared primvar ranges
285  HD_API
286  static bool _IsEnabledSharedVertexPrimvar();
287 
288  HD_API
289  uint64_t
290  _ComputeSharedPrimvarId(uint64_t baseId,
291  HdBufferSourceVector const &sources,
292  HdComputationVector const &computations) const;
293 
294 private:
295  SdfPath _instancerId;
296  SdfPath _materialId;
297 
298  // Used for id renders.
299  int32_t _primId;
300 
301 protected:
302  // shared data across reprs: bufferArrayRanges, bounds, visibility
303  HdRprimSharedData _sharedData;
304 
305  // authored repr selector
306  HdReprSelector _authoredReprSelector;
307 
308  // total number of reprs is relatively small (less than 5 or so
309  // in most case), we use linear container for efficiency.
310  typedef std::vector<std::pair<TfToken, HdReprSharedPtr> >
311  _ReprVector;
312  _ReprVector _reprs;
313 
314  struct _ReprComparator {
315  _ReprComparator(TfToken const &name) : _name(name) {}
316  bool operator() (const std::pair<TfToken, HdReprSharedPtr> &e) const {
317  return _name == e.first;
318  }
319  private:
320  TfToken _name;
321  };
322 
323 
324  // Repr configuration descriptors. All concrete types (HdMesh, HdPoints ..)
325  // have this static map to lookup descriptors for the given reprToken.
326  //
327  // N : # of descriptors for the repr.
328  //
329  template<typename DESC_TYPE, int N=1>
330  struct _ReprDescConfigs {
331  typedef std::array<DESC_TYPE, N> DescArray;
332  static const int MAX_DESCS = N;
333 
334  DescArray Find(TfToken const &reprToken) const {
335  // linear search, we expect only a handful reprs configured.
336  TF_FOR_ALL (it, _configs) {
337  if (it->first == reprToken) return it->second;
338  }
339  TF_CODING_ERROR("Repr %s not found", reprToken.GetText());
340  return DescArray();
341  }
342  void AddOrUpdate(TfToken const &reprToken, DescArray descs) {
343  for (auto& config : _configs) {
344  if (config.first == reprToken) {
345  // Overrwrite the existing entry.
346  config.second = descs;
347  return;
348  }
349  }
350  _configs.push_back(std::make_pair(reprToken, descs));
351  }
352  std::vector<std::pair<TfToken, DescArray> > _configs;
353  };
354 
355 };
356 
358 //
359 // Delegate API Wrappers
360 //
361 TfToken
363 {
364  return delegate->GetRenderTag(GetId());
365 }
366 
367 GfRange3d
369 {
370  return delegate->GetExtent(GetId());
371 }
372 
373 inline HdPrimvarDescriptorVector
375  HdInterpolation interpolation) const
376 {
377  return delegate->GetPrimvarDescriptors(GetId(), interpolation);
378 }
379 
380 inline VtValue
381 HdRprim::GetPrimvar(HdSceneDelegate* delegate, const TfToken &name) const
382 {
383  return delegate->Get(GetId(), name);
384 }
385 
386 PXR_NAMESPACE_CLOSE_SCOPE
387 
388 #endif //PXR_IMAGING_HD_RPRIM_H
virtual HdDirtyBits GetInitialDirtyBitsMask() const =0
Returns the set of dirty bits that should be added to the change tracker for this prim...
int32_t GetPrimId() const
Return the unique instance id.
Definition: rprim.h:179
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:117
Basic type: 3-dimensional floating point range.
Definition: range3d.h:64
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine...
Definition: changeTracker.h:53
virtual HD_API VtValue Get(SdfPath const &id, TfToken const &key)
Returns a named value.
virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const =0
This callback from Rprim gives the prim an opportunity to set additional dirty bits based on those al...
virtual void Sync(HdSceneDelegate *delegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits, TfToken const &reprToken)=0
Pull invalidated scene data and prepare/update the renderable representation.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:87
HdPrimvarDescriptorVector GetPrimvarDescriptors(HdSceneDelegate *delegate, HdInterpolation interpolation) const
Primvar Query.
Definition: rprim.h:374
virtual HD_API TfTokenVector const & GetBuiltinPrimvarNames() const =0
Returns the names of built-in primvars, i.e.
The HdRenderParam is an opaque (to core Hydra) handle, to an object that is obtained from the render ...
std::vector< HdDrawItem * > HdDrawItemPtrVector
Returns the draw items for the requested repr token, if any.
Definition: rprim.h:155
HD_API void InitRepr(HdSceneDelegate *delegate, TfToken const &reprToken, HdDirtyBits *dirtyBits)
Initialize the representation of this Rprim by calling _InitRepr.
bool IsVisible() const
Is the prim itself visible.
Definition: rprim.h:231
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
HD_API void SetPrimId(int32_t primId)
Set the unique instance id.
GfRange3d GetExtent(HdSceneDelegate *delegate) const
Returns the bounds of the rprim in local, untransformed space.
Definition: rprim.h:368
The render engine state for a given rprim from the scene graph.
Definition: rprim.h:67
A transient buffer of data that has not yet been committed.
Definition: bufferSource.h:57
#define TF_FOR_ALL(iter, c)
Macro for iterating over a container.
Definition: iterator.h:390
Adapter class providing data exchange with the client scene graph.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:433
SdfPath const & GetId() const
Returns the identifier of this Rprim.
Definition: rprim.h:176
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
TfToken GetRenderTag(HdSceneDelegate *delegate) const
Returns the render tag associated to this rprim.
Definition: rprim.h:362
HD_API bool CanSkipDirtyBitPropagationAndSync(HdDirtyBits bits) const
This function gives an Rprim the chance to &quot;early exit&quot; from dirty bit propagation, delegate sync and rprim sync altogether.
HD_API bool IsDirty(HdChangeTracker &changeTracker) const
Returns true if any dirty flags are set for this rprim.
virtual HD_API HdPrimvarDescriptorVector GetPrimvarDescriptors(SdfPath const &id, HdInterpolation interpolation)
Returns descriptors for all primvars of the given interpolation type.
virtual HD_API GfRange3d GetExtent(SdfPath const &id)
Gets the axis aligned bounds of a prim.
SdfPath const & GetMaterialId() const
Returns the path of the material to which this Rprim is bound.
Definition: rprim.h:192
virtual void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits)=0
Initialize the given representation of this Rprim.
TfToken const & GetMaterialTag() const
The MaterialTag allows rprims to be organized into different collections based on properties of the p...
Definition: rprim.h:198
A draw item is a light-weight representation of an HdRprim&#39;s resources and material to be used for re...
Definition: drawItem.h:66
SdfPath const & GetInstancerId() const
Returns the identifier of the instancer (if any) for this Rprim.
Definition: rprim.h:187
virtual HD_API TfToken GetRenderTag(SdfPath const &id)
Returns the render tag that will be used to bucket prims during render pass bucketing.
Describes one or more authored display representations for an rprim.
Definition: repr.h:48
HD_API void _SetMaterialId(HdChangeTracker &changeTracker, SdfPath const &materialId)
Sets a new material binding to be used by this rprim.
virtual HD_API void Finalize(HdRenderParam *renderParam)
Finalizes object resources.
HD_API HdDirtyBits PropagateRprimDirtyBits(HdDirtyBits bits)
This function gives an Rprim the chance to set additional dirty bits based on those set in the change...
Hydra Schema for a material object.
Definition: material.h:37
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
char const * GetText() const
Return the text that this token represents.
Definition: token.h:198