HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
drawItem.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_DRAW_ITEM_H
25 #define PXR_IMAGING_HD_DRAW_ITEM_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/perfLog.h"
33 
34 #include "pxr/imaging/hf/perfLog.h"
35 
36 #include "pxr/base/gf/matrix4d.h"
37 #include "pxr/base/gf/bbox3d.h"
38 #include "pxr/base/gf/vec2i.h"
39 #include "pxr/base/gf/vec4f.h"
40 
41 #include <iosfwd>
42 
44 
45 /// \class HdDrawItem
46 ///
47 /// A draw item is a light-weight representation of an HdRprim's resources and
48 /// material to be used for rendering. The visual representation (HdRepr) of an
49 /// HdRprim might require multiple draw items.
50 ///
51 /// HdDrawItem(s) are created by the HdRprim (HdMesh, HdBasisCurve, ..) for each
52 /// HdRepr. The relevant compositional hierarchy is:
53 ///
54 /// HdRprim
55 /// |
56 /// +--HdRepr(s)
57 /// |
58 /// +--HdDrawItem(s)
59 ///
60 /// HdDrawItem(s) are consumed by HdRenderPass for its HdRprimCollection via
61 /// HdRenderIndex::GetDrawItems.
62 ///
63 /// \note
64 /// Rendering backends may choose to specialize this class.
65 ///
66 class HdDrawItem {
67 public:
68  HF_MALLOC_TAG_NEW("new HdDrawItem");
69 
70  HD_API
71  HdDrawItem(HdRprimSharedData const *sharedData);
72  HD_API
73  virtual ~HdDrawItem();
74 
75  HD_API
76  SdfPath const &GetRprimID() const { return _sharedData->rprimID; }
77 
78  HD_API
79  GfBBox3d const & GetBounds() const { return _sharedData->bounds; }
80 
81  HD_API
82  GfRange3d const& GetExtent() const {
83  return _sharedData->bounds.GetRange();
84  }
85 
86  HD_API
87  GfMatrix4d const& GetMatrix() const {
88  return _sharedData->bounds.GetMatrix();
89  }
90 
91  /// Returns a BufferRange of constant-Primvar.
92  HD_API
94  return _sharedData->barContainer.Get(
95  _drawingCoord.GetConstantPrimvarIndex());
96  }
97 
98  /// Returns the number of nested levels of instance primvars.
99  HD_API
101  TF_VERIFY(_sharedData->instancerLevels != -1);
102  return _sharedData->instancerLevels;
103  }
104 
105  /// Returns a BufferRange of instance-Primvars at \p level
106  /// the level is assigned to nested instancers in a bottom-up manner.
107  ///
108  /// example: (numLevels = 2)
109  ///
110  /// instancerA (level = 1)
111  /// |
112  /// +-- instancerB (level = 0)
113  /// |
114  /// +-- mesh_prototype
115  ///
116  HD_API
118  return _sharedData->barContainer.Get(
119  _drawingCoord.GetInstancePrimvarIndex(level));
120  }
121 
122  /// Returns a BufferRange of instance-index indirection.
123  HD_API
125  return _sharedData->barContainer.Get(
126  _drawingCoord.GetInstanceIndexIndex());
127  }
128 
129  /// Returns a BufferRange of element-Primvars.
130  HD_API
132  return _sharedData->barContainer.Get(
133  _drawingCoord.GetElementPrimvarIndex());
134  }
135 
136  /// Returns a BufferArrayRange of topology.
137  HD_API
139  return _sharedData->barContainer.Get(
140  _drawingCoord.GetTopologyIndex());
141  }
142 
143  HD_API
145  return _sharedData->barContainer.Get(
146  _drawingCoord.GetTopologyVisibilityIndex());
147  }
148 
149  /// Returns a BufferArrayRange of vertex-primvars.
150  HD_API
152  return _sharedData->barContainer.Get(
153  _drawingCoord.GetVertexPrimvarIndex());
154  }
155 
156  /// Returns a BufferArrayRange of varying primvars.
157  HD_API
159  return _sharedData->barContainer.Get(
160  _drawingCoord.GetVaryingPrimvarIndex());
161  }
162 
163 
164  /// Returns a BufferArrayRange of face-varying primvars.
165  HD_API
167  return _sharedData->barContainer.Get(
168  _drawingCoord.GetFaceVaryingPrimvarIndex());
169  }
170 
171  HD_API
173  return &_drawingCoord;
174  }
175 
176  /// Returns the authored visibility, expressed by the delegate.
177  HD_API
178  bool GetVisible() const { return _sharedData->visible; }
179 
180  HD_API
181  TfToken const& GetMaterialTag() const {
182  return _sharedData->materialTag;
183  }
184 
186  return _sharedData->fvarTopologyToPrimvarVector;
187  }
188 
189  /// Returns true if the drawItem has instancer.
190  HD_API
191  bool HasInstancer() const {
192  TF_VERIFY(_sharedData->instancerLevels != -1);
193  return (_sharedData->instancerLevels > 0);
194  }
195 
196  /// Returns the hash of the versions of underlying buffers. When the
197  /// hash changes, it means the drawing coord might have been reassigned,
198  /// so any drawing coord caching buffer (e.g. indirect dispatch buffer)
199  /// has to be rebuilt at the moment.
200  /// Note that this value is a hash, not sequential.
201  HD_API
202  size_t GetBufferArraysHash() const;
203 
204  /// Returns the hash of the element offsets of the underlying BARs.
205  /// When the hash changes, it means that any drawing coord caching
206  /// buffer (e.g. the indirect dispatch buffer) has to be rebuilt.
207  /// Note that this value is a hash, not sequential.
208  HD_API
209  size_t GetElementOffsetsHash() const;
210 
211  /// Tests the intersection with the view projection matrix.
212  /// Returns true if this drawItem is in the frustum.
213  ///
214  /// XXX: Currently if this drawitem uses HW instancing, always returns true.
215  HD_API
216  bool IntersectsViewVolume(GfMatrix4d const &viewProjMatrix) const;
217 
218  HD_API
219  friend std::ostream &operator <<(std::ostream &out,
220  const HdDrawItem& self);
221 
222 protected:
223  // TfHash support.
224  template <class HashState>
225  friend void TfHashAppend(HashState &h, HdDrawItem const &di);
226 
227  /// Returns the shared data
228  HD_API
230  return _sharedData;
231  }
232 
233  /// Allows derived classes to return a hash of the versions of buffers
234  /// they manage.
235  /// Called by GetBufferArraysHash.
236  HD_API
237  virtual size_t _GetBufferArraysHash() const;
238 
239  /// Allows derived classes to return a hash of the element offsets of
240  /// the underlying BARs they manage.
241  /// Called by GetBufferArraysHash.
242  HD_API
243  virtual size_t _GetElementOffsetsHash() const;
244 
245 private:
246  // configuration of how to bundle the drawing coordinate for this draw item
247  // out of BARs in sharedData
248  HdDrawingCoord _drawingCoord;
249 
250  // pointer to shared data across reprs, owned by rprim:
251  // bufferArrayRanges, bounds, visibility
252  HdRprimSharedData const *_sharedData;
253 };
254 
255 
257 
258 #endif //PXR_IMAGING_HD_DRAW_ITEM_H
friend void TfHashAppend(HashState &h, HdDrawItem const &di)
HD_API GfBBox3d const & GetBounds() const
Definition: drawItem.h:79
TopologyToPrimvarVector const & GetFvarTopologyToPrimvarVector() const
Definition: drawItem.h:185
HD_API HdBufferArrayRangeSharedPtr const & Get(int index) const
virtual HD_API size_t _GetElementOffsetsHash() const
GLint level
Definition: glcorearb.h:107
#define HD_API
Definition: api.h:40
int GetConstantPrimvarIndex() const
Definition: drawingCoord.h:102
HD_API int GetInstancePrimvarNumLevels() const
Returns the number of nested levels of instance primvars.
Definition: drawItem.h:100
HD_API bool IntersectsViewVolume(GfMatrix4d const &viewProjMatrix) const
HD_API friend std::ostream & operator<<(std::ostream &out, const HdDrawItem &self)
HD_API HdBufferArrayRangeSharedPtr const & GetInstanceIndexRange() const
Returns a BufferRange of instance-index indirection.
Definition: drawItem.h:124
HD_API HdRprimSharedData const * _GetSharedData() const
Returns the shared data.
Definition: drawItem.h:229
HdBufferArrayRangeContainer barContainer
virtual HD_API ~HdDrawItem()
HD_API size_t GetBufferArraysHash() const
Definition: token.h:87
int GetTopologyVisibilityIndex() const
Definition: drawingCoord.h:114
HF_MALLOC_TAG_NEW("new HdDrawItem")
HD_API HdBufferArrayRangeSharedPtr const & GetFaceVaryingPrimvarRange() const
Returns a BufferArrayRange of face-varying primvars.
Definition: drawItem.h:166
HD_API HdDrawingCoord * GetDrawingCoord()
Definition: drawItem.h:172
HD_API size_t GetElementOffsetsHash() const
HD_API GfMatrix4d const & GetMatrix() const
Definition: drawItem.h:87
int GetFaceVaryingPrimvarIndex() const
Definition: drawingCoord.h:112
TopologyToPrimvarVector fvarTopologyToPrimvarVector
const GfRange3d & GetRange() const
Returns the range of the axis-aligned untransformed box.
Definition: bbox3d.h:141
HD_API HdBufferArrayRangeSharedPtr const & GetTopologyVisibilityRange() const
Definition: drawItem.h:144
int GetTopologyIndex() const
Definition: drawingCoord.h:106
int GetInstancePrimvarIndex(int level) const
Definition: drawingCoord.h:121
HD_API HdDrawItem(HdRprimSharedData const *sharedData)
Definition: path.h:288
int GetVaryingPrimvarIndex() const
Definition: drawingCoord.h:116
HD_API HdBufferArrayRangeSharedPtr const & GetInstancePrimvarRange(int level) const
Definition: drawItem.h:117
virtual HD_API size_t _GetBufferArraysHash() const
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2001
std::vector< std::pair< VtIntArray, std::vector< TfToken >>> TopologyToPrimvarVector
HD_API HdBufferArrayRangeSharedPtr const & GetVertexPrimvarRange() const
Returns a BufferArrayRange of vertex-primvars.
Definition: drawItem.h:151
int GetVertexPrimvarIndex() const
Definition: drawingCoord.h:104
HD_API TfToken const & GetMaterialTag() const
Definition: drawItem.h:181
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
HD_API bool GetVisible() const
Returns the authored visibility, expressed by the delegate.
Definition: drawItem.h:178
HD_API HdBufferArrayRangeSharedPtr const & GetConstantPrimvarRange() const
Returns a BufferRange of constant-Primvar.
Definition: drawItem.h:93
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HD_API SdfPath const & GetRprimID() const
Definition: drawItem.h:76
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
HD_API HdBufferArrayRangeSharedPtr const & GetVaryingPrimvarRange() const
Returns a BufferArrayRange of varying primvars.
Definition: drawItem.h:158
HD_API GfRange3d const & GetExtent() const
Definition: drawItem.h:82
int GetElementPrimvarIndex() const
Definition: drawingCoord.h:108
HD_API HdBufferArrayRangeSharedPtr const & GetElementPrimvarRange() const
Returns a BufferRange of element-Primvars.
Definition: drawItem.h:131
HD_API bool HasInstancer() const
Returns true if the drawItem has instancer.
Definition: drawItem.h:191
int GetInstanceIndexIndex() const
Definition: drawingCoord.h:110
const GfMatrix4d & GetMatrix() const
Returns the transformation matrix.
Definition: bbox3d.h:152
HD_API HdBufferArrayRangeSharedPtr const & GetTopologyRange() const
Returns a BufferArrayRange of topology.
Definition: drawItem.h:138