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 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"
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 
215  inline VtValue
216  GetPrimvar(HdSceneDelegate* delegate, const TfToken &name) const;
217 
218  HD_API
219  VtMatrix4dArray GetInstancerTransforms(HdSceneDelegate* delegate);
220 
222  HD_API
223  bool IsDirty(HdChangeTracker &changeTracker) const;
224 
226  bool IsVisible() const { return _sharedData.visible; }
227 
228  HD_API
229  void UpdateReprSelector(HdSceneDelegate* delegate,
230  HdDirtyBits *dirtyBits);
231 
232 protected:
233  // ---------------------------------------------------------------------- //
235  // ---------------------------------------------------------------------- //
236 
247  virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const = 0;
248 
261  virtual void _InitRepr(TfToken const &reprToken,
262  HdDirtyBits *dirtyBits) = 0;
263 
264  // ---------------------------------------------------------------------- //
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 
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,
286  HdBufferSourceVector const &sources,
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
298  HdRprimSharedData _sharedData;
299 
300  // authored repr selector
301  HdReprSelector _authoredReprSelector;
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> >
306  _ReprVector;
307  _ReprVector _reprs;
308 
309  struct _ReprComparator {
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>
325  struct _ReprDescConfigs {
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 
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 
368 inline HdPrimvarDescriptorVector
370  HdInterpolation interpolation) const
371 {
372  return delegate->GetPrimvarDescriptors(GetId(), interpolation);
373 }
374 
375 inline VtValue
376 HdRprim::GetPrimvar(HdSceneDelegate* delegate, const TfToken &name) const
377 {
378  return delegate->Get(GetId(), name);
379 }
380 
381 PXR_NAMESPACE_CLOSE_SCOPE
382 
383 #endif //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:119
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:55
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:369
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:226
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:363
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.
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:357
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