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"
31 #include "pxr/imaging/hd/repr.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 
42 
43 class HdChangeTracker;
44 class HdRenderIndex;
45 class HdRenderParam;
46 
47 using HdReprSharedPtr = std::shared_ptr<HdRepr>;
48 
49 /// \class HdRprim
50 ///
51 /// The render engine state for a given rprim from the scene graph. All data
52 /// access (aside from local caches) is delegated to the HdSceneDelegate.
53 ///
54 class HdRprim
55 {
56 public:
57  HD_API
58  HdRprim(SdfPath const& id);
59 
60  HD_API
61  virtual ~HdRprim();
62 
63  // ---------------------------------------------------------------------- //
64  /// \name Rprim Hydra Engine API : Pre-Sync & Sync-Phase
65  // ---------------------------------------------------------------------- //
66 
67  /// Returns the set of dirty bits that should be
68  /// added to the change tracker for this prim, when this prim is inserted.
69  virtual HdDirtyBits GetInitialDirtyBitsMask() const = 0;
70 
71  /// This function gives an Rprim the chance to "early exit" from dirty
72  /// bit propagation, delegate sync and rprim sync altogether. It is
73  /// a temporary measure to prevent unnecessary work, like in the case of
74  /// invisible prims. The dirty bits in the change tracker remain the same.
75  /// See the implementation for the finer details.
76  HD_API
78 
79  /// This function gives an Rprim the chance to set additional dirty bits
80  /// based on those set in the change tracker, before passing the dirty bits
81  /// to the scene delegate.
82  /// It calls into _PropagateDirtyBits, which gives the Rprim an opportunity
83  /// to specify the additional data needed to process the requested changes.
84  ///
85  /// The return value is the new set of dirty bits.
86  HD_API
88 
89  /// Initialize the representation of this Rprim by calling _InitRepr.
90  /// This is called prior to dirty bit propagation & sync, the first time the
91  /// repr is used, or when the authored representation is dirty.
92  ///
93  /// dirtyBits is an in/out value. It is initialized to the dirty bits
94  /// from the change tracker. InitRepr can then set additional dirty bits
95  /// if additional data is required from the scene delegate when this
96  /// repr is synced.
97  HD_API
98  void InitRepr(HdSceneDelegate* delegate,
99  TfToken const &reprToken,
100  HdDirtyBits *dirtyBits);
101 
102  /// Pull invalidated scene data and prepare/update the renderable
103  /// representation.
104  ///
105  /// This function is told which scene data to pull through the
106  /// dirtyBits parameter. The first time it's called, dirtyBits comes
107  /// from _GetInitialDirtyBits(), which provides initial dirty state,
108  /// but after that it's driven by invalidation tracking in the scene
109  /// delegate.
110  ///
111  /// The contract for this function is that the prim can only pull on scene
112  /// delegate buffers that are marked dirty. Scene delegates can and do
113  /// implement just-in-time data schemes that mean that pulling on clean
114  /// data will be at best incorrect, and at worst a crash.
115  ///
116  /// This function is called in parallel from worker threads, so it needs
117  /// to be threadsafe; calls into HdSceneDelegate are ok.
118  ///
119  /// \param sceneDelegate The data source for this geometry item.
120  /// \param renderParam A render delegate object that holds rendering
121  /// parameters that scene geometry may use.
122  /// \param dirtyBits A specifier for which scene data has changed.
123  /// \param reprToken The representation that needs to be updated. This is
124  /// useful for backends that support multiple display
125  /// representations for an rprim. A given representation
126  /// may choose to pull on a subset of the dirty state.
127  /// \param dirtyBits On input specifies which state is dirty and can be
128  /// pulled from the scene delegate.
129  /// On output specifies which bits are still dirty and
130  /// were not cleaned by the sync.
131  virtual void Sync(HdSceneDelegate *delegate,
132  HdRenderParam *renderParam,
133  HdDirtyBits *dirtyBits,
134  TfToken const &reprToken) = 0;
135  // ---------------------------------------------------------------------- //
136  /// \name Rprim Hydra Engine API : Execute Phase
137  // ---------------------------------------------------------------------- //
138 
139  /// Returns the draw items for the requested repr token, if any.
140  /// These draw items should be constructed and cached beforehand by Sync().
141  /// If no draw items exist, or reprToken cannot be found, nullptr will be
142  /// returned.
143  HD_API
145  GetDrawItems(TfToken const& reprToken) const;
146 
147  // ---------------------------------------------------------------------- //
148  /// \name Rprim Hydra Engine API : Cleanup
149  // ---------------------------------------------------------------------- //
150  /// Finalizes object resources. This function might not delete resources,
151  /// but it should deal with resource ownership so that the rprim is
152  /// deletable.
153  HD_API
154  virtual void Finalize(HdRenderParam *renderParam);
155 
156  // ---------------------------------------------------------------------- //
157  /// \name Rprim Data API
158  // ---------------------------------------------------------------------- //
159 
160  /// Returns the identifier of this Rprim. This is both used in the
161  /// RenderIndex and the SceneDelegate and acts as the associative key for
162  /// the Rprim in both contexts.
163  SdfPath const& GetId() const { return _sharedData.rprimID; }
164 
165  /// Return the unique instance id
166  int32_t GetPrimId() const { return _primId; };
167 
168  /// Set the unique instance id
169  HD_API
170  void SetPrimId(int32_t primId);
171 
172  /// Returns the identifier of the instancer (if any) for this Rprim. If this
173  /// Rprim is not instanced, an empty SdfPath will be returned.
174  SdfPath const& GetInstancerId() const { return _instancerId; }
175 
176  /// Returns the path of the material to which this Rprim is bound. The
177  /// material object itself can be fetched from the RenderIndex using
178  /// this identifier.
179  SdfPath const& GetMaterialId() const { return _materialId; }
180 
181  /// Sets a new material binding to be used by this rprim
182  HD_API
183  void SetMaterialId(SdfPath const& materialId);
184 
186  return _authoredReprSelector;
187  }
188 
189  TfToken const& GetRenderTag() const {
190  return _renderTag;
191  }
192 
193  /// Returns the render tag associated to this rprim
194  inline TfToken GetRenderTag(HdSceneDelegate* delegate) const;
195 
196  /// Returns the bounds of the rprim in local, untransformed space.
197  inline GfRange3d GetExtent(HdSceneDelegate* delegate) const;
198 
199  /// Primvar Query
202  HdInterpolation interpolation) const;
203 
204  /// Returns the names of built-in primvars, i.e. primvars that
205  /// are part of the core geometric schema for this prim.
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 
219  /// Returns true if any dirty flags are set for this rprim.
220  HD_API
221  bool IsDirty(HdChangeTracker &changeTracker) const;
222 
223  /// Is the prim itself visible
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  // ---------------------------------------------------------------------- //
236  /// \name Rprim Hydra Engine API : Pre-Sync & Sync-Phase
237  // ---------------------------------------------------------------------- //
238 
239  /// This callback from Rprim gives the prim an opportunity to set
240  /// additional dirty bits based on those already set. This is done
241  /// before the dirty bits are passed to the scene delegate, so can be
242  /// used to communicate that extra information is needed by the prim to
243  /// process the changes.
244  ///
245  /// The return value is the new set of dirty bits, which replaces the bits
246  /// passed in.
247  ///
248  /// See HdRprim::PropagateRprimDirtyBits()
249  virtual HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const = 0;
250 
251  /// Initialize the given representation of this Rprim.
252  /// This is called prior to syncing the prim, the first time the repr
253  /// is used.
254  ///
255  /// reprToken is the name of the representation to initalize.
256  ///
257  /// dirtyBits is an in/out value. It is initialized to the dirty bits
258  /// from the change tracker. InitRepr can then set additional dirty bits
259  /// if additional data is required from the scene delegate when this
260  /// repr is synced. InitRepr occurs before dirty bit propagation.
261  ///
262  /// See HdRprim::InitRepr()
263  virtual void _InitRepr(TfToken const &reprToken,
264  HdDirtyBits *dirtyBits) = 0;
265 
266  // ---------------------------------------------------------------------- //
267  /// \name Rprim Shared API
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
290 
291  // authored repr selector
293 
294  // authored render tag
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>>;
301 
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>
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 
347 ////////////////////////////////////////////////////////////////////////////////
348 //
349 // Delegate API Wrappers
350 //
351 TfToken
353 {
354  return delegate->GetRenderTag(GetId());
355 }
356 
357 GfRange3d
359 {
360  return delegate->GetExtent(GetId());
361 }
362 
365  HdInterpolation interpolation) const
366 {
367  return delegate->GetPrimvarDescriptors(GetId(), interpolation);
368 }
369 
370 inline VtValue
372 {
373  return delegate->Get(GetId(), name);
374 }
375 
376 inline VtValue
378  VtIntArray *indices) const
379 {
380  return delegate->GetIndexedPrimvar(GetId(), name, indices);
381 }
382 
384 
385 #endif //PXR_IMAGING_HD_RPRIM_H
bool IsVisible() const
Is the prim itself visible.
Definition: rprim.h:224
TfToken _renderTag
Definition: rprim.h:295
SdfPath const & GetInstancerId() const
Definition: rprim.h:174
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
virtual HD_API VtValue GetIndexedPrimvar(SdfPath const &id, TfToken const &key, VtIntArray *outIndices)
HD_API const HdRepr::DrawItemUniquePtrVector & GetDrawItems(TfToken const &reprToken) const
virtual void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits)=0
uint32_t HdDirtyBits
Definition: types.h:158
#define TF_CODING_ERROR
DescArray Find(TfToken const &reprToken) const
Definition: rprim.h:324
virtual HD_API TfTokenVector const & GetBuiltinPrimvarNames() const =0
#define HD_API
Definition: api.h:40
std::shared_ptr< HdRepr > HdReprSharedPtr
Definition: rprim.h:47
std::vector< std::pair< TfToken, DescArray > > _configs
Definition: rprim.h:342
SdfPath const & GetMaterialId() const
Definition: rprim.h:179
TfToken const & GetRenderTag() const
Definition: rprim.h:189
HD_API void _UpdateInstancer(HdSceneDelegate *sceneDelegate, HdDirtyBits *dirtyBits)
HD_API HdReprSharedPtr const & _GetRepr(TfToken const &reprToken) const
int32_t GetPrimId() const
Return the unique instance id.
Definition: rprim.h:166
HD_API void SetPrimId(int32_t primId)
Set the unique instance id.
Definition: token.h:87
static const int MAX_DESCS
Definition: rprim.h:322
virtual HdDirtyBits GetInitialDirtyBitsMask() const =0
Definition: rprim.h:54
HdRprimSharedData _sharedData
Definition: rprim.h:289
bool operator()(const std::pair< TfToken, HdReprSharedPtr > &e) const
Definition: rprim.h:305
HdPrimvarDescriptorVector GetPrimvarDescriptors(HdSceneDelegate *delegate, HdInterpolation interpolation) const
Primvar Query.
Definition: rprim.h:364
VtValue GetPrimvar(HdSceneDelegate *delegate, const TfToken &name) const
Definition: rprim.h:371
HD_API HdRprim(SdfPath const &id)
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::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
virtual HD_API GfRange3d GetExtent(SdfPath const &id)
virtual HD_API TfToken GetRenderTag(SdfPath const &id)
GLuint const GLchar * name
Definition: glcorearb.h:786
std::vector< std::pair< TfToken, HdReprSharedPtr >> _ReprVector
Definition: rprim.h:299
Definition: path.h:290
char const * GetText() const
Definition: token.h:196
HdReprSelector _authoredReprSelector
Definition: rprim.h:292
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
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:160
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
VtValue GetIndexedPrimvar(HdSceneDelegate *delegate, const TfToken &name, VtIntArray *indices) const
Definition: rprim.h:377
SdfPath const & GetId() const
Definition: rprim.h:163
_ReprComparator(TfToken const &name)
Definition: rprim.h:304
HdReprSelector const & GetReprSelector() const
Definition: rprim.h:185
_ReprVector _reprs
Definition: rprim.h:300
std::array< DESC_TYPE, N > DescArray
Definition: rprim.h:321
HD_API void SetMaterialId(SdfPath const &materialId)
Sets a new material binding to be used by this rprim.
HD_API void _UpdateVisibility(HdSceneDelegate *sceneDelegate, HdDirtyBits *dirtyBits)
virtual HD_API void UpdateRenderTag(HdSceneDelegate *delegate, HdRenderParam *renderParam)
HdInterpolation
Definition: enums.h:194
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GA_API const UT_StringHolder N
void AddOrUpdate(TfToken const &reprToken, DescArray descs)
Definition: rprim.h:332
#define TF_FOR_ALL(iter, c)
Definition: iterator.h:390
HD_API void UpdateReprSelector(HdSceneDelegate *delegate, HdDirtyBits *dirtyBits)
std::vector< HdPrimvarDescriptor > HdPrimvarDescriptorVector
HD_API void InitRepr(HdSceneDelegate *delegate, TfToken const &reprToken, HdDirtyBits *dirtyBits)
virtual HD_API ~HdRprim()
HD_API VtMatrix4dArray GetInstancerTransforms(HdSceneDelegate *delegate)
HD_API bool CanSkipDirtyBitPropagationAndSync(HdDirtyBits bits) const
Definition: value.h:164
GfRange3d GetExtent(HdSceneDelegate *delegate) const
Returns the bounds of the rprim in local, untransformed space.
Definition: rprim.h:358
HD_API HdDirtyBits PropagateRprimDirtyBits(HdDirtyBits bits)