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/rprimSharedData.h"
31 #include "pxr/imaging/hd/repr.h"
32 #include "pxr/imaging/hd/sceneDelegate.h"
33 #include "pxr/imaging/hd/types.h"
34 #include "pxr/usd/sdf/path.h"
35 #include "pxr/base/gf/range3d.h"
36 #include "pxr/base/arch/inttypes.h"
37 
38 #include <memory>
39 #include <vector>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 class HdChangeTracker;
44 class HdRenderIndex;
45 class HdRenderParam;
46 
47 using HdReprSharedPtr = std::shared_ptr<HdRepr>;
48 
54 class HdRprim
55 {
56 public:
57  HD_API
58  HdRprim(SdfPath const& id);
59 
60  HD_API
61  virtual ~HdRprim();
62 
63  // ---------------------------------------------------------------------- //
65  // ---------------------------------------------------------------------- //
66 
69  virtual HdDirtyBits GetInitialDirtyBitsMask() const = 0;
70 
76  HD_API
77  bool CanSkipDirtyBitPropagationAndSync(HdDirtyBits bits) const;
78 
86  HD_API
87  HdDirtyBits PropagateRprimDirtyBits(HdDirtyBits bits);
88 
97  HD_API
98  void InitRepr(HdSceneDelegate* delegate,
99  TfToken const &reprToken,
100  HdDirtyBits *dirtyBits);
101 
131  virtual void Sync(HdSceneDelegate *delegate,
132  HdRenderParam *renderParam,
133  HdDirtyBits *dirtyBits,
134  TfToken const &reprToken) = 0;
135  // ---------------------------------------------------------------------- //
137  // ---------------------------------------------------------------------- //
138 
143  HD_API
144  const HdRepr::DrawItemUniquePtrVector &
145  GetDrawItems(TfToken const& reprToken) const;
146 
147  // ---------------------------------------------------------------------- //
149  // ---------------------------------------------------------------------- //
153  HD_API
154  virtual void Finalize(HdRenderParam *renderParam);
155 
156  // ---------------------------------------------------------------------- //
158  // ---------------------------------------------------------------------- //
159 
163  SdfPath const& GetId() const { return _sharedData.rprimID; }
164 
166  int32_t GetPrimId() const { return _primId; };
167 
169  HD_API
170  void SetPrimId(int32_t primId);
171 
174  SdfPath const& GetInstancerId() const { return _instancerId; }
175 
179  SdfPath const& GetMaterialId() const { return _materialId; }
180 
182  HD_API
183  void SetMaterialId(SdfPath const& materialId);
184 
185  HdReprSelector const& GetReprSelector() const {
186  return _authoredReprSelector;
187  }
188 
189  TfToken const& GetRenderTag() const {
190  return _renderTag;
191  }
192 
194  inline TfToken GetRenderTag(HdSceneDelegate* delegate) const;
195 
197  inline GfRange3d GetExtent(HdSceneDelegate* delegate) const;
198 
200  inline HdPrimvarDescriptorVector
202  HdInterpolation interpolation) const;
203 
206  HD_API
207  virtual TfTokenVector const & GetBuiltinPrimvarNames() const = 0;
208 
209  inline VtValue
210  GetPrimvar(HdSceneDelegate* delegate, const TfToken &name) const;
211 
212  inline VtValue
213  GetIndexedPrimvar(HdSceneDelegate* delegate, const TfToken &name,
214  VtIntArray *indices) const;
215 
216  HD_API
217  VtMatrix4dArray GetInstancerTransforms(HdSceneDelegate* delegate);
218 
220  HD_API
221  bool IsDirty(HdChangeTracker &changeTracker) const;
222 
224  bool IsVisible() const { return _sharedData.visible; }
225 
226  HD_API
227  void UpdateReprSelector(HdSceneDelegate* delegate,
228  HdDirtyBits *dirtyBits);
229 
230  HD_API
231  virtual void UpdateRenderTag(HdSceneDelegate *delegate,
232  HdRenderParam *renderParam);
233 
234 protected:
235  // ---------------------------------------------------------------------- //
237  // ---------------------------------------------------------------------- //
238 
249  virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const = 0;
250 
263  virtual void _InitRepr(TfToken const &reprToken,
264  HdDirtyBits *dirtyBits) = 0;
265 
266  // ---------------------------------------------------------------------- //
268  // ---------------------------------------------------------------------- //
269  HD_API
270  HdReprSharedPtr const & _GetRepr(TfToken const &reprToken) const;
271 
272  HD_API
273  void _UpdateVisibility(HdSceneDelegate *sceneDelegate,
274  HdDirtyBits *dirtyBits);
275 
276  HD_API
277  void _UpdateInstancer(HdSceneDelegate *sceneDelegate,
278  HdDirtyBits *dirtyBits);
279 
280 private:
281  SdfPath _instancerId;
282  SdfPath _materialId;
283 
284  // Used for id renders.
285  int32_t _primId;
286 
287 protected:
288  // shared data across reprs: bufferArrayRanges, bounds, visibility
289  HdRprimSharedData _sharedData;
290 
291  // authored repr selector
292  HdReprSelector _authoredReprSelector;
293 
294  // authored render tag
295  TfToken _renderTag;
296 
297  // total number of reprs is relatively small (less than 5 or so
298  // in most case), we use linear container for efficiency.
299  using _ReprVector = std::vector<std::pair<TfToken, HdReprSharedPtr>>;
300  _ReprVector _reprs;
301 
302  struct _ReprComparator
303  {
304  _ReprComparator(TfToken const &name) : _name(name) {}
305  bool operator() (const std::pair<TfToken, HdReprSharedPtr> &e) const {
306  return _name == e.first;
307  }
308  private:
309  TfToken _name;
310  };
311 
312 
313  // Repr configuration descriptors. All concrete types (HdMesh, HdPoints ..)
314  // have this static map to lookup descriptors for the given reprToken.
315  //
316  // N : # of descriptors for the repr.
317  //
318  template<typename DESC_TYPE, int N=1>
319  struct _ReprDescConfigs
320  {
321  using DescArray = std::array<DESC_TYPE, N>;
322  static const int MAX_DESCS = N;
323 
324  DescArray Find(TfToken const &reprToken) const {
325  // linear search, we expect only a handful reprs configured.
326  TF_FOR_ALL (it, _configs) {
327  if (it->first == reprToken) return it->second;
328  }
329  TF_CODING_ERROR("Repr %s not found", reprToken.GetText());
330  return DescArray();
331  }
332  void AddOrUpdate(TfToken const &reprToken, DescArray descs) {
333  for (auto& config : _configs) {
334  if (config.first == reprToken) {
335  // Overrwrite the existing entry.
336  config.second = descs;
337  return;
338  }
339  }
340  _configs.push_back(std::make_pair(reprToken, descs));
341  }
342  std::vector<std::pair<TfToken, DescArray> > _configs;
343  };
344 
345 };
346 
348 //
349 // Delegate API Wrappers
350 //
351 TfToken
352 HdRprim::GetRenderTag(HdSceneDelegate* delegate) const
353 {
354  return delegate->GetRenderTag(GetId());
355 }
356 
357 GfRange3d
359 {
360  return delegate->GetExtent(GetId());
361 }
362 
363 inline HdPrimvarDescriptorVector
365  HdInterpolation interpolation) const
366 {
367  return delegate->GetPrimvarDescriptors(GetId(), interpolation);
368 }
369 
370 inline VtValue
371 HdRprim::GetPrimvar(HdSceneDelegate* delegate, const TfToken &name) const
372 {
373  return delegate->Get(GetId(), name);
374 }
375 
376 inline VtValue
377 HdRprim::GetIndexedPrimvar(HdSceneDelegate* delegate, const TfToken &name,
378  VtIntArray *indices) const
379 {
380  return delegate->GetIndexedPrimvar(GetId(), name, indices);
381 }
382 
383 PXR_NAMESPACE_CLOSE_SCOPE
384 
385 #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,...
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:120
Basic type: 3-dimensional floating point range.
Definition: range3d.h:63
HD_API bool IsDirty(HdChangeTracker &changeTracker) const
Returns true if any dirty flags are set for this rprim.
int32_t GetPrimId() const
Return the unique instance id.
Definition: rprim.h:166
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine.
Definition: changeTracker.h:51
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
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.
SdfPath const & GetMaterialId() const
Returns the path of the material to which this Rprim is bound.
Definition: rprim.h:179
SdfPath const & GetInstancerId() const
Returns the identifier of the instancer (if any) for this Rprim.
Definition: rprim.h:174
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:224
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
HD_API void SetPrimId(int32_t primId)
Set the unique instance id.
The render engine state for a given rprim from the scene graph.
Definition: rprim.h:54
#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.
HD_API bool CanSkipDirtyBitPropagationAndSync(HdDirtyBits bits) const
This function gives an Rprim the chance to "early exit" from dirty bit propagation,...
GfRange3d GetExtent(HdSceneDelegate *delegate) const
Returns the bounds of the rprim in local, untransformed space.
Definition: rprim.h:358
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
virtual HD_API VtValue GetIndexedPrimvar(SdfPath const &id, TfToken const &key, VtIntArray *outIndices)
Returns a named primvar value.
Define integral types.
SdfPath const & GetId() const
Returns the identifier of this Rprim.
Definition: rprim.h:163
virtual HD_API HdPrimvarDescriptorVector GetPrimvarDescriptors(SdfPath const &id, HdInterpolation interpolation)
Returns descriptors for all primvars of the given interpolation type.
HdPrimvarDescriptorVector GetPrimvarDescriptors(HdSceneDelegate *delegate, HdInterpolation interpolation) const
Primvar Query.
Definition: rprim.h:364
virtual HD_API GfRange3d GetExtent(SdfPath const &id)
Gets the axis aligned bounds of a prim.
virtual void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits)=0
Initialize the given representation of this Rprim.
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 HdDirtyBits PropagateRprimDirtyBits(HdDirtyBits bits)
This function gives an Rprim the chance to set additional dirty bits based on those set in the change...
HD_API const HdRepr::DrawItemUniquePtrVector & GetDrawItems(TfToken const &reprToken) const
Returns the draw items for the requested repr token, if any.
char const * GetText() const
Return the text that this token represents.
Definition: token.h:196
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166