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"
31 #include "pxr/imaging/hd/drawingCoord.h"
32 #include "pxr/imaging/hd/rprimSharedData.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 
43 PXR_NAMESPACE_OPEN_SCOPE
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
GetRprimID()76     SdfPath const &GetRprimID() const { return _sharedData->rprimID; }
77 
78     HD_API
GetBounds()79     GfBBox3d const & GetBounds() const { return _sharedData->bounds; }
80 
81     HD_API
GetExtent()82     GfRange3d const& GetExtent() const {
83         return _sharedData->bounds.GetRange();
84     }
85 
86     HD_API
GetMatrix()87     GfMatrix4d const& GetMatrix() const {
88         return _sharedData->bounds.GetMatrix();
89     }
90 
91     /// Returns a BufferRange of constant-Primvar.
92     HD_API
GetConstantPrimvarRange()93     HdBufferArrayRangeSharedPtr const &GetConstantPrimvarRange() const {
94         return _sharedData->barContainer.Get(
95             _drawingCoord.GetConstantPrimvarIndex());
96     }
97 
98     /// Returns the number of nested levels of instance primvars.
99     HD_API
GetInstancePrimvarNumLevels()100     int GetInstancePrimvarNumLevels() const {
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
GetInstancePrimvarRange(int level)117     HdBufferArrayRangeSharedPtr const &GetInstancePrimvarRange(int level) const {
118         return _sharedData->barContainer.Get(
119             _drawingCoord.GetInstancePrimvarIndex(level));
120     }
121 
122     /// Returns a BufferRange of instance-index indirection.
123     HD_API
GetInstanceIndexRange()124     HdBufferArrayRangeSharedPtr const &GetInstanceIndexRange() const {
125         return _sharedData->barContainer.Get(
126             _drawingCoord.GetInstanceIndexIndex());
127     }
128 
129     /// Returns a BufferRange of element-Primvars.
130     HD_API
GetElementPrimvarRange()131     HdBufferArrayRangeSharedPtr const &GetElementPrimvarRange() const {
132         return _sharedData->barContainer.Get(
133             _drawingCoord.GetElementPrimvarIndex());
134     }
135 
136     /// Returns a BufferArrayRange of topology.
137     HD_API
GetTopologyRange()138     HdBufferArrayRangeSharedPtr const &GetTopologyRange() const {
139         return _sharedData->barContainer.Get(
140             _drawingCoord.GetTopologyIndex());
141     }
142 
143     HD_API
GetTopologyVisibilityRange()144     HdBufferArrayRangeSharedPtr const &GetTopologyVisibilityRange() const {
145         return _sharedData->barContainer.Get(
146             _drawingCoord.GetTopologyVisibilityIndex());
147     }
148 
149     /// Returns a BufferArrayRange of vertex-primvars.
150     HD_API
GetVertexPrimvarRange()151     HdBufferArrayRangeSharedPtr const &GetVertexPrimvarRange() const {
152         return _sharedData->barContainer.Get(
153             _drawingCoord.GetVertexPrimvarIndex());
154     }
155 
156     /// Returns a BufferArrayRange of varying primvars.
157     HD_API
GetVaryingPrimvarRange()158     HdBufferArrayRangeSharedPtr const &GetVaryingPrimvarRange() const {
159         return _sharedData->barContainer.Get(
160             _drawingCoord.GetVaryingPrimvarIndex());
161     }
162 
163 
164     /// Returns a BufferArrayRange of face-varying primvars.
165     HD_API
GetFaceVaryingPrimvarRange()166     HdBufferArrayRangeSharedPtr const &GetFaceVaryingPrimvarRange() const {
167         return _sharedData->barContainer.Get(
168             _drawingCoord.GetFaceVaryingPrimvarIndex());
169     }
170 
171     HD_API
GetDrawingCoord()172     HdDrawingCoord *GetDrawingCoord() {
173         return &_drawingCoord;
174     }
175 
176     /// Returns the authored visibility, expressed by the delegate.
177     HD_API
GetVisible()178     bool GetVisible() const { return _sharedData->visible; }
179 
180     HD_API
GetMaterialTag()181     TfToken const& GetMaterialTag() const {
182         return _materialTag;
183     }
184 
185     HD_API
SetMaterialTag(TfToken const & materialTag)186     void SetMaterialTag(TfToken const &materialTag) {
187         _materialTag = materialTag;
188     }
189 
GetFvarTopologyToPrimvarVector()190     TopologyToPrimvarVector const &GetFvarTopologyToPrimvarVector() const {
191         return _sharedData->fvarTopologyToPrimvarVector;
192     }
193 
194     /// Returns true if the drawItem has instancer.
195     HD_API
HasInstancer()196     bool HasInstancer() const {
197         TF_VERIFY(_sharedData->instancerLevels != -1);
198         return (_sharedData->instancerLevels > 0);
199     }
200 
201     /// Returns the hash of the versions of underlying buffers. When the
202     /// hash changes, it means the drawing coord might have been reassigned,
203     /// so any drawing coord caching buffer (e.g. indirect dispatch buffer)
204     /// has to be rebuilt at the moment.
205     /// Note that this value is a hash, not sequential.
206     HD_API
207     size_t GetBufferArraysHash() const;
208 
209     /// Returns the hash of the element offsets of the underlying BARs.
210     /// When the hash changes, it means that any drawing coord caching
211     /// buffer (e.g. the indirect dispatch buffer) has to be rebuilt.
212     /// Note that this value is a hash, not sequential.
213     HD_API
214     size_t GetElementOffsetsHash() const;
215 
216     /// Tests the intersection with the view projection matrix.
217     /// Returns true if this drawItem is in the frustum.
218     ///
219     /// XXX: Currently if this drawitem uses HW instancing, always returns true.
220     HD_API
221     bool IntersectsViewVolume(GfMatrix4d const &viewProjMatrix) const;
222 
223     HD_API
224     friend std::ostream &operator <<(std::ostream &out,
225                                      const HdDrawItem& self);
226 
227 protected:
228     // TfHash support.
229     template <class HashState>
230     friend void TfHashAppend(HashState &h, HdDrawItem const &di);
231 
232     /// Returns the shared data
233     HD_API
_GetSharedData()234     HdRprimSharedData const *_GetSharedData() const {
235         return _sharedData;
236     }
237 
238     /// Allows derived classes to return a hash of the versions of buffers
239     /// they manage.
240     /// Called by GetBufferArraysHash.
241     HD_API
242     virtual size_t _GetBufferArraysHash() const;
243 
244     /// Allows derived classes to return a hash of the element offsets of
245     /// the underlying BARs they manage.
246     /// Called by GetBufferArraysHash.
247     HD_API
248     virtual size_t _GetElementOffsetsHash() const;
249 
250 private:
251     // configuration of how to bundle the drawing coordinate for this draw item
252     // out of BARs in sharedData
253     HdDrawingCoord _drawingCoord;
254 
255     // pointer to shared data across reprs, owned by rprim:
256     //    bufferArrayRanges, bounds, visibility
257     HdRprimSharedData const *_sharedData;
258 
259     /// The materialTag allows the draw items of rprims to be organized into
260     /// different collections based on properties of the prim's material.
261     /// E.g. A renderer may wish to organize opaque and translucent prims
262     /// into different collections so they can be rendered seperately.
263     TfToken _materialTag;
264 };
265 
266 
267 PXR_NAMESPACE_CLOSE_SCOPE
268 
269 #endif //PXR_IMAGING_HD_DRAW_ITEM_H
270