HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
request.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 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_EXEC_VDF_REQUEST_H
8 #define PXR_EXEC_VDF_REQUEST_H
9 
10 /// \file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
17 
18 #include "pxr/base/tf/bits.h"
19 #include "pxr/base/tf/hash.h"
20 
21 #include <cstddef>
22 #include <iterator>
23 #include <memory>
24 #include <vector>
25 
27 
28 class VdfNetwork;
29 
30 class VdfRequest
31 {
32 public:
33  /// Constructor for empty VdfRequest.
34  ///
35  VDF_API
36  VdfRequest();
37 
38  /// Convenience constructor for a VdfRequest with a singular output.
39  ///
40  VDF_API
41  explicit VdfRequest(const VdfMaskedOutput& output);
42 
43  /// Constructor from a vector. Automatically sorts and uniques the vector.
44  /// NOTE : The sort does not distinguish between masked outputs that
45  /// contain the same VdfOutput pointer.
46  ///
47  VDF_API
48  explicit VdfRequest(const VdfMaskedOutputVector& vector);
49 
50  /// Move constructor from a vector. Automatically sorts and uniques the
51  /// vector.
52  /// NOTE : The sort does not distinguish between masked outputs that
53  /// contain the same VdfOutput pointer.
54  ///
55  VDF_API
56  explicit VdfRequest(VdfMaskedOutputVector&& vector);
57 
58  /// Destructor.
59  ///
60  VDF_API
61  ~VdfRequest();
62 
63  ////////////////////////////////////////////////////////////////////////////
64  // Queries
65  ////////////////////////////////////////////////////////////////////////////
66 
67  /// Returns true if the size of the vector is 0.
68  ///
69  VDF_API
70  bool IsEmpty() const;
71 
72  /// Returns the size of the internally held vector.
73  ///
74  VDF_API
75  size_t GetSize() const;
76 
77  /// Returns the network pointer of the first masked output. It assumes
78  /// that all the masked outputs are from the same network. If the request
79  /// is empty, returns NULL.
80  ///
81  VDF_API
82  const VdfNetwork* GetNetwork() const;
83 
84  ////////////////////////////////////////////////////////////////////////////
85  // Iterators
86  ////////////////////////////////////////////////////////////////////////////
87 
89  {
90  public:
91  using iterator_category = std::forward_iterator_tag;
92  using value_type = const VdfMaskedOutput;
93  using reference = const VdfMaskedOutput &;
94  using pointer = const VdfMaskedOutput *;
95  using difference_type = std::ptrdiff_t;
96 
97  VDF_API
99 
100  reference operator*() const { return _Dereference(); }
101  pointer operator->() const { return &(_Dereference()); }
102 
104  _Increment();
105  return *this;
106  }
107 
109  const_iterator r(*this);
110  _Increment();
111  return r;
112  }
113 
114  bool operator==(const const_iterator& rhs) const {
115  return _Equal(rhs);
116  }
117 
118  bool operator!=(const const_iterator& rhs) const {
119  return !_Equal(rhs);
120  }
121 
122  private:
123  friend class VdfRequest;
124 
125  VDF_API
127  const VdfMaskedOutput* mo,
128  const VdfMaskedOutput* first,
129  const TfBits* bits);
130 
131  VDF_API
132  void _Increment();
133 
134  bool _Equal(const const_iterator& rhs) const {
135  return _mo == rhs._mo;
136  }
137 
138  const VdfMaskedOutput& _Dereference() const {
139  return *_mo;
140  }
141 
142  VDF_API
143  size_t _ComputeIndex() const;
144 
145  // Data members
146  const VdfMaskedOutput *_mo;
147  const VdfMaskedOutput *_firstMo;
148  const TfBits *_bits;
149  };
150 
151  /// Returns a vector iterator to the beginning of the internally held vector.
152  ///
153  VDF_API
154  const_iterator begin() const;
155 
156  /// Returns a vector iterator to the end of the internally held vector.
157  ///
158  VDF_API
159  const_iterator end() const;
160 
161  ////////////////////////////////////////////////////////////////////////////
162  // Random access using integer indices
163  ////////////////////////////////////////////////////////////////////////////
164 
166  {
167  public:
168  /// Construct an indexed view ontop of the \p request.
169  ///
170  IndexedView(const VdfRequest &request) : _r(&request) {}
171 
172  /// Returns the size of the indexed view.
173  ///
174  size_t GetSize() const {
175  return _r->_request->size();
176  }
177 
178  /// Returns a pointer to the element stored at index \p i or
179  /// nullptr if the element stored at index \p i is removed from the
180  /// request.
181  ///
182  const VdfMaskedOutput *Get(const size_t i) const {
183  return !_r->_bits.GetSize() || _r->_bits.IsSet(i)
184  ? &(*_r->_request)[i]
185  : nullptr;
186  }
187 
188  private:
189  const VdfRequest *_r;
190  };
191 
192  ////////////////////////////////////////////////////////////////////////////
193  // Request subset operators
194  ////////////////////////////////////////////////////////////////////////////
195 
196  /// Marks the element at the index of the VdfMaskedOutput that
197  /// \p iterator points to as added.
198  ///
199  VDF_API
200  void Add(const const_iterator& iterator);
201 
202  /// Marks the element at the index of the VdfMaskedOutput that \p iterator
203  /// points to as removed.
204  ///
205  VDF_API
206  void Remove(const const_iterator& iterator);
207 
208  /// Marks all the elements in the request as being "added". The bit set
209  /// should be empty.
210  ///
211  VDF_API
212  void AddAll();
213 
214  /// Marks all the elements in the request as being "removed". The bit set
215  /// should be the same size as the length of the vector, but all the bits
216  /// should be unset.
217  ///
218  VDF_API
219  void RemoveAll();
220 
221  /// Returns true if the internally held vector is the same, either by
222  /// pointing to the same vector or by containing the same contents.
223  ///
224  friend bool operator==(const VdfRequest& lhs, const VdfRequest& rhs)
225  {
226  return (lhs._request == rhs._request ||
227  *lhs._request == *rhs._request) &&
228  lhs._bits == rhs._bits;
229  }
230 
231  /// Returns false if the internally held vector is the same, either by
232  /// pointing to the same vector or by containing the same contents.
233  ///
234  friend bool operator!=(const VdfRequest& lhs, const VdfRequest& rhs)
235  {
236  return !(lhs == rhs);
237  }
238 
239  struct Hash {
240  size_t operator()(const VdfRequest &request) const
241  {
242  size_t hash = TfBits::Hash()(request._bits);
243 
244  // Instead of hashing on the complete request we just do it on the
245  // first three outputs (if any).
246  const size_t num = std::min<size_t>(request.GetSize(), 3);
247 
248  const VdfMaskedOutputVector &outputs = *request._request;
249  for(size_t i = 0; i < num; i++) {
250  hash = TfHash::Combine(
251  hash, VdfMaskedOutput::Hash()(outputs[i]));
252  }
253 
254  // Also add the last entry.
255  if (request.GetSize() > 3) {
256  hash = TfHash::Combine(
257  hash, VdfMaskedOutput::Hash()(outputs.back()));
258  }
259 
260  return hash;
261  }
262  };
263 
264 private:
265  /// Marks the element at \p index in the request as being "added". The bit
266  /// set should be the same size as the length of the vector and the bit
267  /// at \p index should be set.
268  ///
269  void _Add(size_t index);
270 
271  /// Marks the element at \p index in the request as being "removed". The bit
272  /// set should be the same size as the length of the vector and the bit at
273  /// \p index should be unset.
274  ///
275  void _Remove(size_t index);
276 
277 private:
278  // Internally held vector that is guaranteed to be sorted and uniqued.
279  std::shared_ptr<const VdfMaskedOutputVector> _request;
280 
281  // Used for holding "subsets" without changing the vector.
282  // An empty bit set is a sentinel for a full vector (i.e. all the elements
283  // in the internally held vector are part of the request). This bit set
284  // should only ever be of size 0 or the size of the vector.
285  //
286  TfBits _bits;
287 };
288 
290 
291 #endif
reference operator*() const
Definition: request.h:100
friend bool operator==(const VdfRequest &lhs, const VdfRequest &rhs)
Definition: request.h:224
GLint first
Definition: glcorearb.h:405
size_t GetSize() const
Definition: request.h:174
bool operator!=(const const_iterator &rhs) const
Definition: request.h:118
VDF_API const_iterator begin() const
IndexedView(const VdfRequest &request)
Definition: request.h:170
VDF_API const VdfNetwork * GetNetwork() const
VDF_API void Add(const const_iterator &iterator)
STATIC_INLINE size_t Hash(const char *s, size_t len)
Definition: farmhash.h:2099
VDF_API void RemoveAll()
const_iterator & operator++()
Definition: request.h:103
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
VDF_API bool IsEmpty() const
#define VDF_API
Definition: api.h:25
std::forward_iterator_tag iterator_category
Definition: request.h:91
std::ptrdiff_t difference_type
Definition: request.h:95
VDF_API VdfRequest()
Fast bit array that keeps track of the number of bits set and can find the next set in a timely manne...
Definition: bits.h:48
const VdfMaskedOutput * Get(const size_t i) const
Definition: request.h:182
const_iterator operator++(int)
Definition: request.h:108
pointer operator->() const
Definition: request.h:101
VDF_API void Remove(const const_iterator &iterator)
size_t GetSize() const
Definition: bits.h:475
VDF_API size_t GetSize() const
Class to hold on to an externally owned output and a mask.
Definition: maskedOutput.h:31
bool operator==(const const_iterator &rhs) const
Definition: request.h:114
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
VDF_API const_iterator end() const
friend bool operator!=(const VdfRequest &lhs, const VdfRequest &rhs)
Definition: request.h:234
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GLboolean r
Definition: glcorearb.h:1222
size_t operator()(const VdfRequest &request) const
Definition: request.h:240
VDF_API ~VdfRequest()
bool IsSet(size_t index) const
Definition: bits.h:412
std::vector< VdfMaskedOutput > VdfMaskedOutputVector
VDF_API void AddAll()