HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primGather.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_PRIM_GATHER_H
8 #define PXR_IMAGING_HD_PRIM_GATHER_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/api.h"
12 
13 #include "pxr/usd/sdf/path.h"
14 
15 #include <vector>
16 #include <tbb/enumerable_thread_specific.h>
17 #include <tbb/blocked_range.h>
18 
20 
21 class HdPrimGather final {
22 public:
23  typedef bool (*FilterPredicateFn)(const SdfPath &path, const void *param);
24 
25  HdPrimGather() = default;
26  ~HdPrimGather() = default;
27 
28  ///
29  /// Filter takes a list of paths and returns a list of paths that
30  /// match the following criteria:
31  ///
32  /// - A path is prefixed by at least one include path
33  /// - A path is not prefixed by an exclude path that has more elements
34  /// than the include path with the most element that is a prefix
35  /// of the path.
36  ///
37  /// The list of paths to filter must be pre-sorted with
38  /// ordering defined by std::less<SdfPath &>.
39  ///
40  /// The list of include and exclude paths do not need to be pre-sorted.
41  ///
42  /// If the same path appears in the list of include and exclude paths
43  /// results are undefined.
44  ///
45  /// The resulting set of paths are stored in results arg, the results
46  /// might not be in sorted order.
47  ///
48  HD_API
49  void Filter(const SdfPathVector &paths,
50  const SdfPathVector &includePaths,
51  const SdfPathVector &excludePaths,
52  SdfPathVector *results);
53 
54  ///
55  /// PredicatedFilter takes a list of paths and returns a list of paths that
56  /// match the following criteria:
57  ///
58  /// - A path is prefixed by at least one include path
59  /// - A path is not prefixed by an exclude path that has more elements
60  /// than the include path with the most element that is a prefix
61  /// of the path.
62  /// - The predicate function for the path returns true.
63  ///
64  /// The list of paths to filter must be pre-sorted with
65  /// ordering defined by std::less<SdfPath &>.
66  ///
67  /// The list of include and exclude paths do not need to be pre-sorted.
68  ///
69  /// If the same path appears in the list of include and exclude paths
70  /// results are undefined.
71  ///
72  /// The predicate function has the prototype:
73  /// bool Predicate(const SdfPath &path, void *param);
74  ///
75  /// The function should return true if the path should appear in the results
76  /// and false if not. The Predicate function may be called on worker
77  /// threads and as such must be thread-safe.
78  ///
79  /// The resulting set of paths are stored in results arg, the results
80  /// might not be in sorted order.
81  ///
82  HD_API
83  void PredicatedFilter(const SdfPathVector &paths,
84  const SdfPathVector &includePaths,
85  const SdfPathVector &excludePaths,
86  FilterPredicateFn predicateFn,
87  void *predicateParam,
88  SdfPathVector *results);
89 
90  ///
91  /// Subtree is a simplified form of filter, that gathers
92  /// all prims that meet the single rootPath prefix condition.
93  ///
94  /// The list of paths to filter must be pre-sorted with
95  /// ordering defined by std::less<SdfPath &>.
96  ///
97  /// The returned result maintain the sorted order.
98  ///
99  HD_API
100  void Subtree(const SdfPathVector &paths,
101  const SdfPath &rootPath,
102  SdfPathVector *results);
103 
104  ///
105  /// Subtree is a simplified form of filter, that gathers
106  /// all prims that meet the single rootPath prefix condition.
107  ///
108  /// The list of paths to filter must be pre-sorted with
109  /// ordering defined by std::less<SdfPath &>.
110  ///
111  /// Rather than returning a list the paths, it instead returns
112  /// the start and end (inclusive) indexes into the paths
113  /// vector of that subtree range.
114  ///
115  /// If the rootPath wasn't found or an error occurred, that
116  /// otherwise produces an invalid range. The method returns false.
117  HD_API
118  bool SubtreeAsRange(const SdfPathVector &paths,
119  const SdfPath &rootPath,
120  size_t *start,
121  size_t *end);
122 
123 private:
124  struct _PathFilter {
125  SdfPath _path;
126  bool _includePath; // false = exclude path
127 
128  _PathFilter(const SdfPath &path, bool includePath)
129  : _path(path)
130  , _includePath(includePath)
131  {
132 
133  }
134 
135  bool operator >(const _PathFilter &other) const {
136  return other._path < _path;
137  }
138  };
139  typedef std::vector<_PathFilter> _PathFilterArray;
140 
141  // While processing, the algorithm stores results as a set of ranges
142  // rather than copying all the paths.
143  // This to avoid copying the larger set of paths at intermediate
144  // processing steps.
145  struct _Range {
146  size_t _start;
147  size_t _end; // Inclusive
148 
149  _Range(size_t start, size_t end)
150  : _start(start)
151  , _end(end)
152  {
153 
154  }
155 
156  };
157  typedef std::vector<_Range> _RangeArray;
158  typedef tbb::enumerable_thread_specific<_RangeArray> _ConcurrentRangeArray;
159  typedef tbb::blocked_range<size_t> _ConcurrentRange;
160 
161 
162 
163  _PathFilterArray _filterList;
164  _RangeArray _gatheredRanges;
165  _ConcurrentRangeArray _resultRanges;
166 
167 
168 
169  size_t _FindLowerBound(const SdfPathVector &paths,
170  size_t start,
171  size_t end,
172  const SdfPath &path) const;
173  size_t _FindUpperBound(const SdfPathVector &paths,
174  size_t start,
175  size_t end,
176  const SdfPath &path) const;
177 
178  void _FilterRange(const SdfPathVector &paths,
179  size_t start,
180  size_t end,
181  bool include);
182 
183  void _SetupFilter(const SdfPathVector &includePaths,
184  const SdfPathVector &excludePaths);
185 
186  void _GatherPaths(const SdfPathVector &paths);
187 
188  // Outer Loop called for each range in vector
189  void _DoPredicateTestOnRange(const SdfPathVector &paths,
190  const _Range &range,
191  FilterPredicateFn predicateFn,
192  void *predicateParam);
193 
194  // Inner Loop over each prim in a sub range of _Range.
195  void _DoPredicateTestOnPrims(const SdfPathVector &paths,
196  _ConcurrentRange &range,
197  FilterPredicateFn predicateFn,
198  void *predicateParam);
199 
200  template <class Iterator>
201  static void _WriteResults(const SdfPathVector &paths,
202  const Iterator &rangesBegin,
203  const Iterator &rangesEnd,
204  SdfPathVector *results);
205 
206  void _FilterSubTree(const SdfPathVector &paths,
207  const SdfPath &rootPath);
208 
209  // No default copying or assignment
210  HdPrimGather(const HdPrimGather &) = delete;
211  HdPrimGather &operator =(const HdPrimGather &) = delete;
212 
213 };
214 
215 
217 
218 #endif // PXR_IMAGING_HD_PRIM_GATHER_H
HdPrimGather()=default
GLenum GLint * range
Definition: glcorearb.h:1925
GLuint start
Definition: glcorearb.h:475
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
#define HD_API
Definition: api.h:23
OutGridT const XformOp bool bool
~HdPrimGather()=default
OIIO_FORCEINLINE vbool4 operator>(const vint4 &a, const vint4 &b)
Definition: simd.h:4718
std::vector< class SdfPath > SdfPathVector
GLuint GLuint end
Definition: glcorearb.h:475
bool(* FilterPredicateFn)(const SdfPath &path, const void *param)
Definition: primGather.h:23
Definition: path.h:273
HD_API void Subtree(const SdfPathVector &paths, const SdfPath &rootPath, SdfPathVector *results)
HD_API void Filter(const SdfPathVector &paths, const SdfPathVector &includePaths, const SdfPathVector &excludePaths, SdfPathVector *results)
GLenum GLfloat param
Definition: glcorearb.h:104
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HD_API bool SubtreeAsRange(const SdfPathVector &paths, const SdfPath &rootPath, size_t *start, size_t *end)
HD_API void PredicatedFilter(const SdfPathVector &paths, const SdfPathVector &includePaths, const SdfPathVector &excludePaths, FilterPredicateFn predicateFn, void *predicateParam, SdfPathVector *results)