All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
changeTracker.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_CHANGE_TRACKER_H
25 #define HD_CHANGE_TRACKER_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/rprimCollection.h"
31 #include "pxr/imaging/hd/types.h"
32 #include "pxr/usd/sdf/path.h"
33 #include "pxr/base/tf/hashmap.h"
34 
35 #include <atomic>
36 #include <boost/noncopyable.hpp>
37 #include <boost/shared_ptr.hpp>
38 #include <boost/unordered_map.hpp>
39 #include <boost/weak_ptr.hpp>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
43 
44 class TfToken;
45 
55 class HdChangeTracker : public boost::noncopyable {
56 public:
57 
58  enum RprimDirtyBits : HdDirtyBits {
59  Clean = 0,
60  InitRepr = 1 << 0,
61  Varying = 1 << 1,
62  AllDirty = ~Varying,
63  DirtyPrimID = 1 << 2,
64  DirtyExtent = 1 << 3,
65  DirtyDisplayStyle = 1 << 4,
66  DirtyPoints = 1 << 5,
67  DirtyPrimvar = 1 << 6,
68  DirtyMaterialId = 1 << 7,
69  DirtyTopology = 1 << 8,
70  DirtyTransform = 1 << 9,
71  DirtyVisibility = 1 << 10,
72  DirtyNormals = 1 << 11,
73  DirtyDoubleSided = 1 << 12,
74  DirtyCullStyle = 1 << 13,
75  DirtySubdivTags = 1 << 14,
76  DirtyWidths = 1 << 15,
77  DirtyInstancer = 1 << 16,
78  DirtyInstanceIndex = 1 << 17,
79  DirtyRepr = 1 << 18,
80  DirtyRenderTag = 1 << 19,
81  DirtyComputationPrimvarDesc = 1 << 20,
82  DirtyCategories = 1 << 21,
83  AllSceneDirtyBits = ((1<<22) - 1),
84 
85  NewRepr = 1 << 22,
86 
87  CustomBitsBegin = 1 << 23,
88  CustomBitsEnd = 1 << 30,
89  };
90 
91  // Dirty bits for Tasks
92  enum NonRprimDirtyBits : HdDirtyBits {
93  //Varying = 1 << 0,
94  DirtyType = 1 << 1,
95  DirtyParams = 1 << 2,
96  DirtyCollection = 1 << 3,
97  DirtyRenderTags = 1 << 4,
98  };
99 
100  HD_API
101  HdChangeTracker();
102  HD_API
103  virtual ~HdChangeTracker();
104 
105  // ---------------------------------------------------------------------- //
108  // ---------------------------------------------------------------------- //
109 
111  HD_API
112  void RprimInserted(SdfPath const& id, HdDirtyBits initialDirtyState);
113 
115  HD_API
116  void RprimRemoved(SdfPath const& id);
117 
118  // ---------------------------------------------------------------------- //
122  // ---------------------------------------------------------------------- //
123 
125  HD_API
126  HdDirtyBits GetRprimDirtyBits(SdfPath const& id) const;
127 
130  HD_API
131  void MarkRprimDirty(SdfPath const& id, HdDirtyBits bits=AllDirty);
132 
136  HD_API
137  void MarkRprimClean(SdfPath const& id, HdDirtyBits newBits=Clean);
138 
140  HD_API
141  void MarkPrimvarDirty(SdfPath const& id, TfToken const& name);
142 
146  HD_API
147  void MarkAllRprimsDirty(HdDirtyBits bits);
148 
156  HD_API
157  void ResetVaryingState();
158 
162  HD_API
163  void ResetRprimVaryingState(SdfPath const& id);
164 
165 
166  // ---------------------------------------------------------------------- //
167 
169  HD_API
170  bool IsRprimDirty(SdfPath const& id);
171 
173  HD_API
174  bool IsExtentDirty(SdfPath const& id);
175 
177  HD_API
178  bool IsDisplayStyleDirty(SdfPath const& id);
179 
182  HD_API
183  bool IsPrimvarDirty(SdfPath const& id, TfToken const& name);
184 
186  HD_API
187  bool IsAnyPrimvarDirty(SdfPath const& id);
188 
190  HD_API
191  bool IsTopologyDirty(SdfPath const& id);
192 
194  HD_API
195  bool IsDoubleSidedDirty(SdfPath const& id);
196 
198  HD_API
199  bool IsCullStyleDirty(SdfPath const& id);
200 
202  HD_API
203  bool IsSubdivTagsDirty(SdfPath const& id);
204 
206  HD_API
207  bool IsTransformDirty(SdfPath const& id);
208 
210  HD_API
211  bool IsVisibilityDirty(SdfPath const& id);
212 
214  HD_API
215  bool IsPrimIdDirty(SdfPath const& id);
216 
218  static bool IsDirty(HdDirtyBits dirtyBits) {
219  return (dirtyBits & AllDirty) != 0;
220  }
221 
223  static bool IsClean(HdDirtyBits dirtyBits) {
224  return (dirtyBits & AllDirty) == 0;
225  }
226 
228  static bool IsVarying(HdDirtyBits dirtyBits) {
229  return (dirtyBits & Varying) != 0;
230  }
231 
233  HD_API
234  static bool IsExtentDirty(HdDirtyBits dirtyBits, SdfPath const& id);
235 
237  HD_API
238  static bool IsDisplayStyleDirty(HdDirtyBits dirtyBits, SdfPath const& id);
239 
241  HD_API
242  static bool IsSubdivTagsDirty(HdDirtyBits dirtyBits, SdfPath const& id);
243 
246  HD_API
247  static bool IsPrimvarDirty(HdDirtyBits dirtyBits, SdfPath const& id,
248  TfToken const& name);
249 
252  HD_API
253  static bool IsAnyPrimvarDirty(HdDirtyBits dirtyBits, SdfPath const& id);
254 
256  HD_API
257  static bool IsTopologyDirty(HdDirtyBits dirtyBits, SdfPath const& id);
258 
260  HD_API
261  static bool IsDoubleSidedDirty(HdDirtyBits dirtyBits, SdfPath const& id);
262 
264  HD_API
265  static bool IsCullStyleDirty(HdDirtyBits dirtyBits, SdfPath const& id);
266 
268  HD_API
269  static bool IsTransformDirty(HdDirtyBits dirtyBits, SdfPath const& id);
270 
272  HD_API
273  static bool IsVisibilityDirty(HdDirtyBits dirtyBits, SdfPath const& id);
274 
276  HD_API
277  static bool IsPrimIdDirty(HdDirtyBits dirtyBits, SdfPath const& id);
278 
280  HD_API
281  static bool IsInstancerDirty(HdDirtyBits dirtyBits, SdfPath const& id);
282 
284  HD_API
285  static bool IsInstanceIndexDirty(HdDirtyBits dirtyBits, SdfPath const& id);
286 
287  HD_API
288  static bool IsReprDirty(HdDirtyBits dirtyBits, SdfPath const &id);
289 
290  // ---------------------------------------------------------------------- //
291 
293  HD_API
294  static void MarkPrimvarDirty(HdDirtyBits *dirtyBits, TfToken const &name);
295 
296  // ---------------------------------------------------------------------- //
300  // ---------------------------------------------------------------------- //
301 
303  HD_API
304  void InstancerInserted(SdfPath const& id);
305 
307  HD_API
308  void InstancerRemoved(SdfPath const& id);
309 
312  HD_API
313  void InstancerRPrimInserted(SdfPath const& instancerId, SdfPath const& rprimId);
314 
317  HD_API
318  void InstancerRPrimRemoved(SdfPath const& instancerId, SdfPath const& rprimId);
319 
320  // ---------------------------------------------------------------------- //
324  // ---------------------------------------------------------------------- //
325 
327  HD_API
328  void TaskInserted(SdfPath const& id, HdDirtyBits initialDirtyState);
329 
331  HD_API
332  void TaskRemoved(SdfPath const& id);
333 
335  HD_API
336  void MarkTaskDirty(SdfPath const& id, HdDirtyBits bits=AllDirty);
337 
339  HD_API
340  HdDirtyBits GetTaskDirtyBits(SdfPath const& id);
341 
343  HD_API
344  void MarkTaskClean(SdfPath const& id, HdDirtyBits newBits=Clean);
345 
352  HD_API
353  void MarkRenderTagsDirty();
354 
356  HD_API
357  unsigned GetRenderTagVersion() const;
358 
359  // ---------------------------------------------------------------------- //
363  // ---------------------------------------------------------------------- //
364 
366  HD_API
367  HdDirtyBits GetInstancerDirtyBits(SdfPath const& id);
368 
371  HD_API
372  void MarkInstancerDirty(SdfPath const& id, HdDirtyBits bits=AllDirty);
373 
375  HD_API
376  void MarkInstancerClean(SdfPath const& id, HdDirtyBits newBits=Clean);
377 
378  // ---------------------------------------------------------------------- //
382  // ---------------------------------------------------------------------- //
383 
385  HD_API
386  void SprimInserted(SdfPath const& id, HdDirtyBits initialDirtyState);
387 
389  HD_API
390  void SprimRemoved(SdfPath const& id);
391 
393  HD_API
394  HdDirtyBits GetSprimDirtyBits(SdfPath const& id);
395 
397  HD_API
398  void MarkSprimDirty(SdfPath const& id, HdDirtyBits bits);
399 
401  HD_API
402  void MarkSprimClean(SdfPath const& id, HdDirtyBits newBits=Clean);
403 
404  // ---------------------------------------------------------------------- //
408  // ---------------------------------------------------------------------- //
409 
411  HD_API
412  void BprimInserted(SdfPath const& id, HdDirtyBits initialDirtyState);
413 
415  HD_API
416  void BprimRemoved(SdfPath const& id);
417 
419  HD_API
420  HdDirtyBits GetBprimDirtyBits(SdfPath const& id);
421 
423  HD_API
424  void MarkBprimDirty(SdfPath const& id, HdDirtyBits bits);
425 
427  HD_API
428  void MarkBprimClean(SdfPath const& id, HdDirtyBits newBits=Clean);
429 
430  // ---------------------------------------------------------------------- //
434  // ---------------------------------------------------------------------- //
435 
438  _needsGarbageCollection = false;
439  }
440 
443  _needsGarbageCollection = true;
444  }
445 
449  return _needsGarbageCollection;
450  }
451 
452  void ClearBprimGarbageCollectionNeeded() {
453  _needsBprimGarbageCollection = false;
454  }
455 
458  _needsBprimGarbageCollection = true;
459  }
460 
464  return _needsBprimGarbageCollection;
465  }
466 
467 
468  // ---------------------------------------------------------------------- //
472  // ---------------------------------------------------------------------- //
473 
475  HD_API
476  void AddCollection(TfToken const& collectionName);
477 
480  HD_API
481  void MarkCollectionDirty(TfToken const& collectionName);
482 
484  HD_API
485  unsigned GetCollectionVersion(TfToken const& collectionName) const;
486 
489  HD_API
490  unsigned GetVisibilityChangeCount() const;
491 
494  unsigned GetVaryingStateVersion() const {
495  return _varyingStateVersion;
496  }
497 
500  HD_API
501  void MarkBatchesDirty();
502 
504  HD_API
505  unsigned GetBatchVersion() const;
506 
507  // ---------------------------------------------------------------------- //
511  // ---------------------------------------------------------------------- //
512 
516  unsigned GetRprimIndexVersion() const {
517  return _rprimIndexVersion;
518  }
519 
523  unsigned GetSprimIndexVersion() const {
524  return _sprimIndexVersion;
525  }
526 
530  unsigned GetBprimIndexVersion() const {
531  return _bprimIndexVersion;
532  }
533 
537  unsigned GetInstancerIndexVersion() const {
538  return _instancerIndexVersion;
539  }
540 
541 
547  unsigned GetSceneStateVersion() const {
548  return _sceneStateVersion;
549  }
550 
551  // ---------------------------------------------------------------------- //
555  // ---------------------------------------------------------------------- //
556 
558  HD_API
559  void AddState(TfToken const& name);
560 
563  HD_API
564  void MarkStateDirty(TfToken const& name);
565 
567  HD_API
568  unsigned GetStateVersion(TfToken const &name) const;
569 
570  // ---------------------------------------------------------------------- //
574  // ---------------------------------------------------------------------- //
575  HD_API
576  static std::string StringifyDirtyBits(HdDirtyBits dirtyBits);
577 
578  HD_API
579  static void DumpDirtyBits(HdDirtyBits dirtyBits);
580 
582 
583 private:
584 
585  static void _LogCacheAccess(TfToken const& cacheName,
586  SdfPath const& id, bool hit);
587 
588  typedef TfHashMap<SdfPath, HdDirtyBits, SdfPath::Hash> _IDStateMap;
589  typedef TfHashMap<TfToken, int, TfToken::HashFunctor> _CollectionStateMap;
590  typedef TfHashMap<SdfPath, SdfPathSet, SdfPath::Hash> _InstancerRprimMap;
591  typedef TfHashMap<TfToken, unsigned, TfToken::HashFunctor> _GeneralStateMap;
592 
593  // Core dirty state.
594  _IDStateMap _rprimState;
595  _IDStateMap _instancerState;
596  _IDStateMap _taskState;
597  _IDStateMap _sprimState;
598  _IDStateMap _bprimState;
599  _GeneralStateMap _generalState;
600 
601  // Collection versions / state.
602  _CollectionStateMap _collectionState;
603  bool _needsGarbageCollection;
604  bool _needsBprimGarbageCollection;
605 
606  // Provides reverse-association between instancers and the rprims that use
607  // them.
608  _InstancerRprimMap _instancerRprimMap;
609 
610  // Typically the Rprims that get marked dirty per update iteration end up
611  // being a stable set of objects; to leverage this fact, we require the
612  // delegate notify the change tracker when that state changes, which bumps
613  // the varyingStateVersion, which triggers downstream invalidation.
614  unsigned _varyingStateVersion;
615 
616  // Tracks changes (insertions/removals) of prims in the render index.
617  // This is used to indicating that cached gather operations need to be
618  // re-evaluated, such as dirty lists or batch building.
619  unsigned _rprimIndexVersion;
620  unsigned _sprimIndexVersion;
621  unsigned _bprimIndexVersion;
622  unsigned _instancerIndexVersion;
623 
624  // The following tracks any changes of state. As a result it is very broad.
625  // The use case to detect, when no changes have been made, as to
626  // avoid the need to sync or reset progressive renderers.
627  unsigned _sceneStateVersion;
628 
629  // Used to detect that visibility changed somewhere in the render index.
630  unsigned _visChangeCount;
631 
632  // Used to detect changes to the set of active render tags
633  unsigned _renderTagVersion;
634 
635  // Used to validate draw batches.
636  std::atomic_uint _batchVersion;
637 };
638 
639 
640 PXR_NAMESPACE_CLOSE_SCOPE
641 
642 #endif //HD_CHANGE_TRACKER_H
HD_API void ResetRprimVaryingState(SdfPath const &id)
Reset the varying state on one Rprim This is done for Rprims, where we choose not to clean them (due ...
void SetBprimGarbageCollectionNeeded()
Sets the garbageCollectionNeeded flag.
bool IsBprimGarbageCollectionNeeded() const
Returns true if garbage collection was flagged to be run.
HD_API void MarkBatchesDirty()
Marks all batches dirty, meaning they need to be validated and potentially rebuilt.
HD_API unsigned GetVisibilityChangeCount() const
Returns the number of changes to visibility.
HD_API void MarkInstancerClean(SdfPath const &id, HdDirtyBits newBits=Clean)
Clean the specified dirty bits for the instancer with id.
HD_API void MarkBprimDirty(SdfPath const &id, HdDirtyBits bits)
Set the dirty flags to bits.
HD_API void MarkRenderTagsDirty()
Called to flag when the set of active render tags have changed.
HD_API HdDirtyBits GetTaskDirtyBits(SdfPath const &id)
Get the dirty bits for Task with the given id.
HD_API void MarkPrimvarDirty(SdfPath const &id, TfToken const &name)
Mark the primvar for the rprim with id as being dirty.
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine...
Definition: changeTracker.h:55
HD_API void RprimInserted(SdfPath const &id, HdDirtyBits initialDirtyState)
Start tracking Rprim with the given id.
HD_API void SprimRemoved(SdfPath const &id)
Stop tracking sprim with the given id.
HD_API void MarkTaskClean(SdfPath const &id, HdDirtyBits newBits=Clean)
Set the dirty flags to newBits.
HD_API bool IsCullStyleDirty(SdfPath const &id)
Returns true if the rprim identified by id has dirty cullstyle.
unsigned GetSprimIndexVersion() const
Returns the current version of the Render Index&#39;s SPrim set.
static HD_API bool IsInstanceIndexDirty(HdDirtyBits dirtyBits, SdfPath const &id)
Returns true if the dirtyBits has a dirty instance index. id is for perflog.
static bool IsVarying(HdDirtyBits dirtyBits)
Returns true if the dirtyBits has no flags set except the varying flag.
HD_API void MarkRprimClean(SdfPath const &id, HdDirtyBits newBits=Clean)
Clear the dirty flags for an HdRprim.
HD_API HdDirtyBits GetInstancerDirtyBits(SdfPath const &id)
Returns the dirty bits for the instancer with id.
HD_API unsigned GetBatchVersion() const
Returns the current batch version.
unsigned GetInstancerIndexVersion() const
Returns the current version of the Render Index&#39;s Instancer set.
HD_API void BprimInserted(SdfPath const &id, HdDirtyBits initialDirtyState)
Start tracking bprim with the given id.
HD_API bool IsAnyPrimvarDirty(SdfPath const &id)
Returns true if the rprim identified by id has any dirty primvars.
HD_API void MarkInstancerDirty(SdfPath const &id, HdDirtyBits bits=AllDirty)
Flag the Instancer with the given id as being dirty.
HD_API HdDirtyBits GetSprimDirtyBits(SdfPath const &id)
Get the dirty bits for sprim with the given id.
HD_API void InstancerRemoved(SdfPath const &id)
Stop tracking Instancer with the given id.
unsigned GetVaryingStateVersion() const
Returns the current version of varying state.
bool IsGarbageCollectionNeeded() const
Returns true if garbage collection was flagged to be run.
HD_API void TaskInserted(SdfPath const &id, HdDirtyBits initialDirtyState)
Start tracking Task with the given id.
HD_API void MarkStateDirty(TfToken const &name)
Marks a named state as being dirty., this bumps the version of the state.
HD_API bool IsDoubleSidedDirty(SdfPath const &id)
Returns true if the rprim identified by id has dirty doubleSided state.
HD_API HdDirtyBits GetBprimDirtyBits(SdfPath const &id)
Get the dirty bits for bprim with the given id.
unsigned GetRprimIndexVersion() const
Returns the current version of the Render Index&#39;s RPrim set.
HD_API bool IsTransformDirty(SdfPath const &id)
Returns true if the rprim identified by id has a dirty transform.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
HD_API void InstancerRPrimInserted(SdfPath const &instancerId, SdfPath const &rprimId)
Add the gived rprimId to the list of rprims associated with the instancer instancerId.
HD_API bool IsRprimDirty(SdfPath const &id)
Returns true if the rprim identified by id has any dirty flags set.
HD_API unsigned GetStateVersion(TfToken const &name) const
Returns the current version of the named state.
HD_API HdDirtyBits GetRprimDirtyBits(SdfPath const &id) const
Returns the dirty bits for the rprim with id.
HD_API bool IsPrimIdDirty(SdfPath const &id)
Returns true if the rprim identified by id has a dirty primID.
HD_API void MarkSprimDirty(SdfPath const &id, HdDirtyBits bits)
Set the dirty flags to bits.
HD_API void ResetVaryingState()
Clear Varying bit of all prims.
void ClearGarbageCollectionNeeded()
Clears the garbageCollectionNeeded flag.
HD_API void MarkTaskDirty(SdfPath const &id, HdDirtyBits bits=AllDirty)
Set the dirty flags to bits.
HD_API void MarkCollectionDirty(TfToken const &collectionName)
Marks a named collection as being dirty, this bumps the version of the collection.
static bool IsDirty(HdDirtyBits dirtyBits)
Returns true if the dirtyBits has any flags set other than the varying flag.
unsigned GetSceneStateVersion() const
Returns the current version of the scene state.
unsigned GetBprimIndexVersion() const
Returns the current version of the Render Index&#39;s BPrim set.
HD_API void MarkBprimClean(SdfPath const &id, HdDirtyBits newBits=Clean)
Set the dirty flags to newBits.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
HD_API void InstancerRPrimRemoved(SdfPath const &instancerId, SdfPath const &rprimId)
Remove the gived rprimId to the list of rprims associated with the instancer instancerId.
HD_API bool IsTopologyDirty(SdfPath const &id)
Returns true if the rprim identified by id has a dirty topology.
static bool IsClean(HdDirtyBits dirtyBits)
Returns true if the dirtyBits has no flags set except the varying flag.
HD_API void AddCollection(TfToken const &collectionName)
Adds a named collection for tracking.
HD_API void BprimRemoved(SdfPath const &id)
Stop tracking bprim with the given id.
HD_API void MarkRprimDirty(SdfPath const &id, HdDirtyBits bits=AllDirty)
Flag the Rprim with the given id as being dirty.
HD_API void MarkAllRprimsDirty(HdDirtyBits bits)
Flag all the Rprim with the given id as being dirty.
void SetGarbageCollectionNeeded()
Sets the garbageCollectionNeeded flag.
HD_API bool IsVisibilityDirty(SdfPath const &id)
Returns true if the rprim identified by id has dirty visibility.
HD_API bool IsSubdivTagsDirty(SdfPath const &id)
Returns true if the rprim identified by id has a dirty subdiv tags.
HD_API void SprimInserted(SdfPath const &id, HdDirtyBits initialDirtyState)
Start tracking sprim with the given id.
HD_API bool IsExtentDirty(SdfPath const &id)
Returns true if the rprim identified by id has a dirty extent.
HD_API bool IsDisplayStyleDirty(SdfPath const &id)
Returns true if the rprim identified by id has a dirty display style.
HD_API void MarkSprimClean(SdfPath const &id, HdDirtyBits newBits=Clean)
Set the dirty flags to newBits.
HD_API void AddState(TfToken const &name)
Adds a named state for tracking.
HD_API void RprimRemoved(SdfPath const &id)
Stop tracking Rprim with the given id.
HD_API unsigned GetCollectionVersion(TfToken const &collectionName) const
Returns the current version of the named collection.
HD_API void InstancerInserted(SdfPath const &id)
Start tracking Instancer with the given id.
HD_API bool IsPrimvarDirty(SdfPath const &id, TfToken const &name)
Returns true if the rprim identified by id with primvar name is dirty.
HD_API unsigned GetRenderTagVersion() const
Retrieve the current version number of the render tag set.
static HD_API bool IsInstancerDirty(HdDirtyBits dirtyBits, SdfPath const &id)
Returns true if the dirtyBits has a dirty instancer. id is for perflog.
HD_API void TaskRemoved(SdfPath const &id)
Stop tracking Task with the given id.