HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
repr.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 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_REPR_H
8 #define PXR_IMAGING_HD_REPR_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/api.h"
13 #include "pxr/imaging/hd/tokens.h"
14 #include <vector>
15 
17 
18 
19 /// \class HdReprSelector
20 ///
21 /// Describes one or more authored display representations for an rprim.
22 /// Display opinions are separated by the topology index they represent.
23 /// This allows the application to specify one or more topological
24 /// representations for a given HdRprim.
25 /// For some visualizations, an application may choose to provide an opinion for
26 /// the display of the refined surface, the unrefined hull and the points
27 /// separately from the rprim's authored opinions.
28 /// HdReprSelector allows these opinions to compose/merge into a final composite
29 /// representation to be used for rendering.
30 ///
32 {
33 public:
34  explicit HdReprSelector()
35  : refinedToken()
36  , unrefinedToken()
37  , pointsToken() { }
38 
39  explicit HdReprSelector(TfToken const &token)
40  : refinedToken(token)
41  , unrefinedToken()
42  , pointsToken() { }
43 
44  explicit HdReprSelector(
45  TfToken const &refined,
46  TfToken const &unrefined)
47  : refinedToken(refined)
48  , unrefinedToken(unrefined)
49  , pointsToken() { }
50 
51  explicit HdReprSelector(
52  TfToken const &refined,
53  TfToken const &unrefined,
54  TfToken const &points)
55  : refinedToken(refined)
56  , unrefinedToken(unrefined)
57  , pointsToken(points) { }
58 
59  /// Currenly support upto 3 topology tokens.
60  static const size_t MAX_TOPOLOGY_REPRS = 3;
61 
62  /// Returns true if the passed in reprToken is in the set of tokens
63  /// for any topology index.
64  HD_API
65  bool Contains(const TfToken &reprToken) const;
66 
67  /// Returns true if the topology token at an index is active, i.e., neither
68  /// empty nor disabled.
69  HD_API
70  bool IsActiveRepr(size_t topologyIndex) const;
71 
72  /// Returns true if any of the topology tokens is valid, i.e., neither
73  /// empty nor disabled.
74  HD_API
75  bool AnyActiveRepr() const;
76 
77  /// Returns a selector that is the composite of this selector 'over'
78  /// the passed in selector.
79  /// For each token that IsEmpty in this selector return the corresponding
80  /// token in the passed in selector.
81  /// Effectively this performs a merge operation where this selector wins
82  /// for each topological index it has an opinion on.
83  HD_API
84  HdReprSelector CompositeOver(const HdReprSelector &under) const;
85 
86  HD_API
87  bool operator==(const HdReprSelector &rhs) const;
88 
89  HD_API
90  bool operator!=(const HdReprSelector &rhs) const;
91 
92  HD_API
93  bool operator<(const HdReprSelector &rhs) const;
94 
95  HD_API
96  size_t Hash() const;
97 
98  HD_API
99  char const* GetText() const;
100 
101  HD_API
102  friend std::ostream &operator <<(std::ostream &stream,
103  HdReprSelector const& t);
104 
105 
106  HD_API
107  TfToken const &operator[](size_t topologyIndex) const;
108 
109 private:
110  // TfHash support.
111  template <class HashState>
112  friend void
113  TfHashAppend(HashState &h, HdReprSelector const &rs) {
114  h.Append(rs.refinedToken, rs.unrefinedToken, rs.pointsToken);
115  }
116 
117  TfToken refinedToken;
118  TfToken unrefinedToken;
119  TfToken pointsToken;
120 };
121 
122 
123 /// \class HdRepr
124 ///
125 /// An HdRepr refers to a (single) topological representation of an rprim, and
126 /// owns the draw item(s) that visually represent it. The draw items are
127 /// populated by the rprim.
128 /// The relevant compositional hierarchy is:
129 ///
130 /// HdRprim
131 /// |
132 /// +--HdRepr(s)
133 /// |
134 /// +--HdDrawItem(s)
135 ///
136 /// When multiple topological representations are required for an rprim, we use
137 /// HdReprSelector to compose the individual representations.
138 ///
139 class HdRepr final
140 {
141 public:
142  using DrawItemUniquePtr = std::unique_ptr<HdDrawItem>;
143  using DrawItemUniquePtrVector = std::vector<DrawItemUniquePtr>;
144 
145  HD_API
146  HdRepr();
147  HD_API
148  ~HdRepr();
149 
150  /// Returns the draw items for this representation.
152  return _drawItems;
153  }
154 
155  /// Transfers ownership of a draw item to this repr.
156  /// Do not use for adding geom subset draw items.
157  void AddDrawItem(std::unique_ptr<HdDrawItem> &&item) {
158  _drawItems.insert(_drawItems.begin() + _geomSubsetsStart,
159  std::move(item));
160  _geomSubsetsStart++;
161  }
162 
163  /// Returns the draw item at the requested index.
164  ///
165  /// Note that the pointer returned is owned by this object and must not be
166  /// deleted.
167  HdDrawItem* GetDrawItem(size_t index) const {
168  return _drawItems[index].get();
169  }
170 
171  /// HdRepr can hold geom subset draw items, which are unique in that they
172  /// not created at the time the repr is created, but instead when populating
173  /// the mesh topology of a mesh rprim. The number of geom subset draw items
174  /// in a repr can change over time.
175  /// We make some assumptions when using these geom subset related functions.
176  /// We assume the geom subset draw items will only be added (or cleared)
177  /// after all of the main draw items for a repr have been added.
178  /// We also assume that the geom subset draw items for a repr desc are all
179  /// added one after the other before moving onto the next repr desc.
180  /// Thus the order of draw items in the _drawItems member might go something
181  /// like (assuming two repr descs and three geom subsets in this example):
182  /// [ main DI for desc 1, main DI for desc 2, GS1 DI for desc 1,
183  /// GS2 DI for desc 1, GS3 DI for desc 1, GS1 DI for desc 2,
184  /// GS2 DI for desc 2, GS3 DI for desc 2 ]
185  /// It is also possible for there to exist a main draw item for a particular
186  /// repr desc but no geom subsets for that repr desc, while having geom
187  /// subsets exist for a different repr desc.
188 
189  /// Transfers ownership of a draw item to this repr.
190  /// To be used only for geom subset draw items.
191  void AddGeomSubsetDrawItem(std::unique_ptr<HdDrawItem> &&item) {
192  _drawItems.push_back(std::move(item));
193  }
194 
195  /// Utility similar to GetDrawItem for getting geom subset draw items.
196  HdDrawItem* GetDrawItemForGeomSubset(size_t reprDescIndex,
197  size_t numGeomSubsets, size_t geomSubsetIndex) const {
198  return _drawItems[_geomSubsetsStart + reprDescIndex * numGeomSubsets +
199  geomSubsetIndex].get();
200  }
201 
202  /// Removes all of the geom subset draw items from the repr.
204  _drawItems.erase(
205  _drawItems.begin() + _geomSubsetsStart, _drawItems.end());
206  }
207 
208 private:
209  // Noncopyable
210  HdRepr(const HdRepr&) = delete;
211  HdRepr& operator=(const HdRepr&) = delete;
212 
213 private:
214  // Contains normal draw items first, potentially followed by geom subset
215  // draw items
216  DrawItemUniquePtrVector _drawItems;
217 
218  // Index into _drawItems indicating where the geom subset draw items begin.
219  size_t _geomSubsetsStart;
220 };
221 
222 
224 
225 #endif //PXR_IMAGING_HD_REPR_H
GLuint GLuint stream
Definition: glcorearb.h:1832
void ClearGeomSubsetDrawItems()
Removes all of the geom subset draw items from the repr.
Definition: repr.h:203
GLdouble GLdouble GLint GLint const GLdouble * points
Definition: glad.h:2676
std::unique_ptr< HdDrawItem > DrawItemUniquePtr
Definition: repr.h:142
HD_API bool operator<(const HdReprSelector &rhs) const
HD_API friend std::ostream & operator<<(std::ostream &stream, HdReprSelector const &t)
HD_API bool operator==(const HdReprSelector &rhs) const
#define HD_API
Definition: api.h:23
friend void TfHashAppend(HashState &h, HdReprSelector const &rs)
Definition: repr.h:113
HD_API TfToken const & operator[](size_t topologyIndex) const
HdDrawItem * GetDrawItem(size_t index) const
Definition: repr.h:167
Definition: token.h:70
HdReprSelector(TfToken const &token)
Definition: repr.h:39
HD_API size_t Hash() const
HdReprSelector()
Definition: repr.h:34
HD_API bool operator!=(const HdReprSelector &rhs) const
static const size_t MAX_TOPOLOGY_REPRS
Currenly support upto 3 topology tokens.
Definition: repr.h:60
HD_API bool Contains(const TfToken &reprToken) const
HdReprSelector(TfToken const &refined, TfToken const &unrefined, TfToken const &points)
Definition: repr.h:51
GLdouble t
Definition: glad.h:2397
HdDrawItem * GetDrawItemForGeomSubset(size_t reprDescIndex, size_t numGeomSubsets, size_t geomSubsetIndex) const
Utility similar to GetDrawItem for getting geom subset draw items.
Definition: repr.h:196
HD_API char const * GetText() const
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
HD_API bool AnyActiveRepr() const
HD_API bool IsActiveRepr(size_t topologyIndex) const
std::vector< DrawItemUniquePtr > DrawItemUniquePtrVector
Definition: repr.h:143
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HD_API HdReprSelector CompositeOver(const HdReprSelector &under) const
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HdReprSelector(TfToken const &refined, TfToken const &unrefined)
Definition: repr.h:44
HD_API HdRepr()
const DrawItemUniquePtrVector & GetDrawItems() const
Returns the draw items for this representation.
Definition: repr.h:151
void AddDrawItem(std::unique_ptr< HdDrawItem > &&item)
Definition: repr.h:157
Definition: repr.h:139
HD_API ~HdRepr()
void AddGeomSubsetDrawItem(std::unique_ptr< HdDrawItem > &&item)
Definition: repr.h:191