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 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"
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 <memory>
41 #include <vector>
42 
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 
63 /// \class HdRprim
64 ///
65 /// The render engine state for a given rprim from the scene graph. All data
66 /// access (aside from local caches) is delegated to the HdSceneDelegate.
67 ///
68 class HdRprim {
69 public:
70  HD_API
71  HdRprim(SdfPath const& id,
72  SdfPath const& instancerId);
73  HD_API
74  virtual ~HdRprim();
75 
76  // ---------------------------------------------------------------------- //
77  /// \name Rprim Hydra Engine API : Pre-Sync & Sync-Phase
78  // ---------------------------------------------------------------------- //
79 
80  /// Returns the set of dirty bits that should be
81  /// added to the change tracker for this prim, when this prim is inserted.
82  virtual HdDirtyBits GetInitialDirtyBitsMask() const = 0;
83 
84  /// This function gives an Rprim the chance to "early exit" from dirty
85  /// bit propagation, delegate sync and rprim sync altogether. It is
86  /// a temporary measure to prevent unnecessary work, like in the case of
87  /// invisible prims. The dirty bits in the change tracker remain the same.
88  /// See the implementation for the finer details.
89  HD_API
91 
92  /// This function gives an Rprim the chance to set additional dirty bits
93  /// based on those set in the change tracker, before passing the dirty bits
94  /// to the scene delegate.
95  /// It calls into _PropagateDirtyBits, which gives the Rprim an opportunity
96  /// to specify the additional data needed to process the requested changes.
97  ///
98  /// The return value is the new set of dirty bits.
99  HD_API
101 
102  /// Initialize the representation of this Rprim by calling _InitRepr.
103  /// This is called prior to dirty bit propagation & sync, the first time the
104  /// repr is used, or when the authored representation is dirty.
105  ///
106  /// dirtyBits is an in/out value. It is initialized to the dirty bits
107  /// from the change tracker. InitRepr can then set additional dirty bits
108  /// if additional data is required from the scene delegate when this
109  /// repr is synced.
110  HD_API
111  void InitRepr(HdSceneDelegate* delegate,
112  TfToken const &reprToken,
113  HdDirtyBits *dirtyBits);
114 
115  /// Pull invalidated scene data and prepare/update the renderable
116  /// representation.
117  ///
118  /// This function is told which scene data to pull through the
119  /// dirtyBits parameter. The first time it's called, dirtyBits comes
120  /// from _GetInitialDirtyBits(), which provides initial dirty state,
121  /// but after that it's driven by invalidation tracking in the scene
122  /// delegate.
123  ///
124  /// The contract for this function is that the prim can only pull on scene
125  /// delegate buffers that are marked dirty. Scene delegates can and do
126  /// implement just-in-time data schemes that mean that pulling on clean
127  /// data will be at best incorrect, and at worst a crash.
128  ///
129  /// This function is called in parallel from worker threads, so it needs
130  /// to be threadsafe; calls into HdSceneDelegate are ok.
131  ///
132  /// \param sceneDelegate The data source for this geometry item.
133  /// \param renderParam A render delegate object that holds rendering
134  /// parameters that scene geometry may use.
135  /// \param dirtyBits A specifier for which scene data has changed.
136  /// \param reprToken The representation that needs to be updated. This is
137  /// useful for backends that support multiple display
138  /// representations for an rprim. A given representation
139  /// may choose to pull on a subset of the dirty state.
140  /// \param dirtyBits On input specifies which state is dirty and can be
141  /// pulled from the scene delegate.
142  /// On output specifies which bits are still dirty and
143  /// were not cleaned by the sync.
144  virtual void Sync(HdSceneDelegate *delegate,
145  HdRenderParam *renderParam,
146  HdDirtyBits *dirtyBits,
147  TfToken const &reprToken) = 0;
148  // ---------------------------------------------------------------------- //
149  /// \name Rprim Hydra Engine API : Execute Phase
150  // ---------------------------------------------------------------------- //
151 
152  /// Returns the draw items for the requested repr token, if any.
153  /// These draw items should be constructed and cached beforehand by Sync().
154  /// If no draw items exist, or reprToken cannot be found, nullptr will be
155  /// returned.
156  HD_API
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  /// Returns the names of built-in primvars, i.e. primvars that
216  /// are part of the core geometric schema for this prim.
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 
226  /// Returns true if any dirty flags are set for this rprim.
227  HD_API
228  bool IsDirty(HdChangeTracker &changeTracker) const;
229 
230  /// Is the prim itself visible
231  bool IsVisible() const { return _sharedData.visible; }
232 
233  HD_API
234  void UpdateReprSelector(HdSceneDelegate* delegate,
235  HdDirtyBits *dirtyBits);
236 
237 protected:
238  // ---------------------------------------------------------------------- //
239  /// \name Rprim Hydra Engine API : Pre-Sync & Sync-Phase
240  // ---------------------------------------------------------------------- //
241 
242  /// This callback from Rprim gives the prim an opportunity to set
243  /// additional dirty bits based on those already set. This is done
244  /// before the dirty bits are passed to the scene delegate, so can be
245  /// used to communicate that extra information is needed by the prim to
246  /// process the changes.
247  ///
248  /// The return value is the new set of dirty bits, which replaces the bits
249  /// passed in.
250  ///
251  /// See HdRprim::PropagateRprimDirtyBits()
252  virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const = 0;
253 
254  /// Initialize the given representation of this Rprim.
255  /// This is called prior to syncing the prim, the first time the repr
256  /// is used.
257  ///
258  /// reprToken is the name of the representation to initalize.
259  ///
260  /// dirtyBits is an in/out value. It is initialized to the dirty bits
261  /// from the change tracker. InitRepr can then set additional dirty bits
262  /// if additional data is required from the scene delegate when this
263  /// repr is synced. InitRepr occurs before dirty bit propagation.
264  ///
265  /// See HdRprim::InitRepr()
266  virtual void _InitRepr(TfToken const &reprToken,
267  HdDirtyBits *dirtyBits) = 0;
268 
269  // ---------------------------------------------------------------------- //
270  /// \name Rprim Shared API
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 
279  /// Sets a new material binding to be used by this rprim
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,
292  HdComputationSharedPtrVector 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
304 
305  // authored repr selector
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> >
313 
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>
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 
357 ////////////////////////////////////////////////////////////////////////////////
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 
375  HdInterpolation interpolation) const
376 {
377  return delegate->GetPrimvarDescriptors(GetId(), interpolation);
378 }
379 
380 inline VtValue
382 {
383  return delegate->Get(GetId(), name);
384 }
385 
387 
388 #endif //PXR_IMAGING_HD_RPRIM_H
bool IsVisible() const
Is the prim itself visible.
Definition: rprim.h:231
SdfPath const & GetInstancerId() const
Definition: rprim.h:187
GLsizei GLenum * sources
Definition: glew.h:3954
GLuint const GLchar * name
Definition: glew.h:1814
HD_API const HdRepr::DrawItemUniquePtrVector & GetDrawItems(TfToken const &reprToken) const
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.
uint32_t HdDirtyBits
Definition: types.h:127
#define TF_CODING_ERROR
DescArray Find(TfToken const &reprToken) const
Definition: rprim.h:334
virtual HD_API TfTokenVector const & GetBuiltinPrimvarNames() const =0
#define HD_API
Definition: api.h:40
std::shared_ptr< HdRepr > HdReprSharedPtr
Definition: rprim.h:52
HD_API HdRprim(SdfPath const &id, SdfPath const &instancerId)
std::vector< HdBufferSourceSharedPtr > HdBufferSourceSharedPtrVector
Definition: bufferSource.h:44
std::vector< std::pair< TfToken, DescArray > > _configs
Definition: rprim.h:352
SdfPath const & GetMaterialId() const
Definition: rprim.h:192
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:87
static const int MAX_DESCS
Definition: rprim.h:332
virtual HdDirtyBits GetInitialDirtyBitsMask() const =0
TfToken GetRenderTag(HdSceneDelegate *delegate) const
Returns the render tag associated to this rprim.
Definition: rprim.h:362
Definition: rprim.h:68
HdRprimSharedData _sharedData
Sets a new material binding to be used by this rprim.
Definition: rprim.h:303
bool operator()(const std::pair< TfToken, HdReprSharedPtr > &e) const
Definition: rprim.h:316
HdPrimvarDescriptorVector GetPrimvarDescriptors(HdSceneDelegate *delegate, HdInterpolation interpolation) const
Primvar Query.
Definition: rprim.h:374
VtValue GetPrimvar(HdSceneDelegate *delegate, const TfToken &name) const
Definition: rprim.h:381
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.
std::shared_ptr< class HdComputation > HdComputationSharedPtr
Definition: computation.h:41
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:446
virtual HD_API GfRange3d GetExtent(SdfPath const &id)
virtual HD_API TfToken GetRenderTag(SdfPath const &id)
Definition: path.h:288
std::vector< struct HdBufferSpec > HdBufferSpecVector
char const * GetText() const
Definition: token.h:196
HdReprSelector _authoredReprSelector
Sets a new material binding to be used by this rprim.
Definition: rprim.h:306
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
HD_API uint64_t _ComputeSharedPrimvarId(uint64_t baseId, HdBufferSourceSharedPtrVector const &sources, HdComputationSharedPtrVector const &computations) const
Sets a new material binding to be used by this rprim.
virtual HD_API void Finalize(HdRenderParam *renderParam)
virtual HD_API VtValue Get(SdfPath const &id, TfToken const &key)
Returns a named value.
std::vector< DrawItemUniquePtr > DrawItemUniquePtrVector
Definition: repr.h:152
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1346
SdfPath const & GetId() const
Definition: rprim.h:176
_ReprComparator(TfToken const &name)
Definition: rprim.h:315
HdReprSelector const & GetReprSelector() const
Definition: rprim.h:200
_ReprVector _reprs
Sets a new material binding to be used by this rprim.
Definition: rprim.h:312
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:194
static HD_API bool _IsEnabledSharedVertexPrimvar()
Sets a new material binding to be used by this rprim.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
GA_API const UT_StringHolder N
void AddOrUpdate(TfToken const &reprToken, DescArray descs)
Definition: rprim.h:342
#define TF_FOR_ALL(iter, c)
Definition: iterator.h:390
std::array< DESC_TYPE, N > DescArray
Definition: rprim.h:331
std::vector< HdComputationSharedPtr > HdComputationSharedPtrVector
Definition: computation.h:42
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:311
virtual HD_API ~HdRprim()
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
HD_API VtMatrix4dArray GetInstancerTransforms(HdSceneDelegate *delegate)
HD_API bool CanSkipDirtyBitPropagationAndSync(HdDirtyBits bits) const
Definition: value.h:174
GfRange3d GetExtent(HdSceneDelegate *delegate) const
Returns the bounds of the rprim in local, untransformed space.
Definition: rprim.h:368
HD_API HdDirtyBits PropagateRprimDirtyBits(HdDirtyBits bits)