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 <memory>
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 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 
68 class HdRprim {
69 public:
70  HD_API
71  HdRprim(SdfPath const& id);
72 
73  HD_API
74  virtual ~HdRprim();
75 
76  // ---------------------------------------------------------------------- //
78  // ---------------------------------------------------------------------- //
79 
82  virtual HdDirtyBits GetInitialDirtyBitsMask() const = 0;
83 
89  HD_API
90  bool CanSkipDirtyBitPropagationAndSync(HdDirtyBits bits) const;
91 
99  HD_API
100  HdDirtyBits PropagateRprimDirtyBits(HdDirtyBits bits);
101 
110  HD_API
111  void InitRepr(HdSceneDelegate* delegate,
112  TfToken const &reprToken,
113  HdDirtyBits *dirtyBits);
114 
144  virtual void Sync(HdSceneDelegate *delegate,
145  HdRenderParam *renderParam,
146  HdDirtyBits *dirtyBits,
147  TfToken const &reprToken) = 0;
148  // ---------------------------------------------------------------------- //
150  // ---------------------------------------------------------------------- //
151 
156  HD_API
157  const HdRepr::DrawItemUniquePtrVector &
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 
195  HD_API
196  void SetMaterialId(SdfPath const& materialId);
197 
202  TfToken const& GetMaterialTag() const { return _sharedData.materialTag; }
203 
205  HD_API
206  void SetMaterialTag(TfToken const& materialTag);
207 
208  HdReprSelector const& GetReprSelector() const {
209  return _authoredReprSelector;
210  }
211 
213  inline TfToken GetRenderTag(HdSceneDelegate* delegate) const;
214 
216  inline GfRange3d GetExtent(HdSceneDelegate* delegate) const;
217 
219  inline HdPrimvarDescriptorVector
221  HdInterpolation interpolation) const;
222 
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 
239  HD_API
240  bool IsDirty(HdChangeTracker &changeTracker) const;
241 
243  bool IsVisible() const { return _sharedData.visible; }
244 
245  HD_API
246  void UpdateReprSelector(HdSceneDelegate* delegate,
247  HdDirtyBits *dirtyBits);
248 
249 protected:
250  // ---------------------------------------------------------------------- //
252  // ---------------------------------------------------------------------- //
253 
264  virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const = 0;
265 
278  virtual void _InitRepr(TfToken const &reprToken,
279  HdDirtyBits *dirtyBits) = 0;
280 
281  // ---------------------------------------------------------------------- //
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
304  HdRprimSharedData _sharedData;
305 
306  // authored repr selector
307  HdReprSelector _authoredReprSelector;
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> >
312  _ReprVector;
313  _ReprVector _reprs;
314 
315  struct _ReprComparator {
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>
331  struct _ReprDescConfigs {
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 
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 
374 inline HdPrimvarDescriptorVector
376  HdInterpolation interpolation) const
377 {
378  return delegate->GetPrimvarDescriptors(GetId(), interpolation);
379 }
380 
381 inline VtValue
382 HdRprim::GetPrimvar(HdSceneDelegate* delegate, const TfToken &name) const
383 {
384  return delegate->Get(GetId(), name);
385 }
386 
387 inline VtValue
388 HdRprim::GetIndexedPrimvar(HdSceneDelegate* delegate, const TfToken &name,
389  VtIntArray *indices) const
390 {
391  return delegate->GetIndexedPrimvar(GetId(), name, indices);
392 }
393 
394 PXR_NAMESPACE_CLOSE_SCOPE
395 
396 #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:116
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:50
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:85
HdPrimvarDescriptorVector GetPrimvarDescriptors(HdSceneDelegate *delegate, HdInterpolation interpolation) const
Primvar Query.
Definition: rprim.h:375
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 ...
HD_API void InitRepr(HdSceneDelegate *delegate, TfToken const &reprToken, HdDirtyBits *dirtyBits)
Initialize the representation of this Rprim by calling _InitRepr.
HD_API void SetMaterialId(SdfPath const &materialId)
Sets a new material binding to be used by this rprim.
bool IsVisible() const
Is the prim itself visible.
Definition: rprim.h:243
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HD_API void SetMaterialTag(TfToken const &materialTag)
Sets the material tag used by the rprim.
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:369
The render engine state for a given rprim from the scene graph.
Definition: rprim.h:68
A transient buffer of data that has not yet been committed.
Definition: bufferSource.h:55
#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:442
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:288
TfToken GetRenderTag(HdSceneDelegate *delegate) const
Returns the render tag associated to this rprim.
Definition: rprim.h:363
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 VtValue GetIndexedPrimvar(SdfPath const &id, TfToken const &key, VtIntArray *outIndices)
Returns a named primvar value.
Define integral types.
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:202
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
virtual HD_API void Finalize(HdRenderParam *renderParam)
Finalizes object resources.
HD_API const HdRepr::DrawItemUniquePtrVector & GetDrawItems(TfToken const &reprToken) const
Returns the draw items for the requested repr token, if any.
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:168
char const * GetText() const
Return the text that this token represents.
Definition: token.h:196