HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mesh.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_ST_MESH_H
8 #define PXR_IMAGING_HD_ST_MESH_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hdSt/api.h"
12 #include "pxr/imaging/hd/version.h"
15 #include "pxr/imaging/hd/mesh.h"
16 #include "pxr/imaging/hd/perfLog.h"
17 
18 #include "pxr/usd/sdf/path.h"
19 #include "pxr/base/vt/array.h"
20 
21 #include <memory>
22 
24 
25 
26 class HdStDrawItem;
27 class HdSceneDelegate;
28 
30  std::shared_ptr<class HdSt_VertexAdjacencyBuilder>;
31 using HdBufferSourceSharedPtr = std::shared_ptr<class HdBufferSource>;
32 using HdSt_MeshTopologySharedPtr = std::shared_ptr<class HdSt_MeshTopology>;
33 
35  std::shared_ptr<class HdStResourceRegistry>;
36 
37 /// A subdivision surface or poly-mesh object.
38 ///
39 class HdStMesh final : public HdMesh
40 {
41 public:
42  HF_MALLOC_TAG_NEW("new HdStMesh");
43 
44  HDST_API
45  HdStMesh(SdfPath const& id);
46 
47  HDST_API
48  ~HdStMesh() override;
49 
50  HDST_API
51  void UpdateRenderTag(HdSceneDelegate *delegate,
52  HdRenderParam *renderParam) override;
53 
54  HDST_API
55  void Sync(HdSceneDelegate *delegate,
56  HdRenderParam *renderParam,
57  HdDirtyBits *dirtyBits,
58  TfToken const &reprToken) override;
59 
60  HDST_API
61  void Finalize(HdRenderParam *renderParam) override;
62 
63  HDST_API
64  HdDirtyBits GetInitialDirtyBitsMask() const override;
65 
66  /// Topology (member) getter
67  HDST_API
68  HdMeshTopologySharedPtr GetTopology() const override;
69 
70  /// Returns whether packed (10_10_10 bits) normals to be used
71  HDST_API
72  static bool IsEnabledPackedNormals();
73 
74 protected:
75  HDST_API
76  void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits) override;
77 
78  HDST_API
79  HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const override;
80 
81  void _UpdateRepr(HdSceneDelegate *sceneDelegate,
82  HdRenderParam *renderParam,
83  TfToken const &reprToken,
84  HdDirtyBits *dirtyBitsState);
85 
87  _GetSharedPrimvarRange(uint64_t primvarId,
88  HdBufferSpecVector const &updatedOrAddedSpecs,
89  HdBufferSpecVector const &removedSpecs,
90  HdBufferArrayRangeSharedPtr const &curRange,
91  bool * isFirstInstance,
92  HdStResourceRegistrySharedPtr const &resourceRegistry) const;
93 
94  bool _MaterialHasPtex(const HdRenderIndex &renderIndex,
95  const SdfPath &materialId) const;
96 
97  bool _UseQuadIndices(const HdRenderIndex &renderIndex,
99 
100  bool _MaterialHasLimitSurface(const HdRenderIndex &renderIndex,
101  const SdfPath &materialId) const;
102 
103  bool _UseLimitRefinement(const HdRenderIndex &renderIndex,
104  const HdMeshTopology &topology) const;
105 
106  bool _UseSmoothNormals(HdSt_MeshTopologySharedPtr const& topology) const;
107 
108  bool _UseFlatNormals(const HdMeshReprDesc &desc) const;
109 
110  void _UpdateDrawItem(HdSceneDelegate *sceneDelegate,
111  HdRenderParam *renderParam,
112  HdStDrawItem *drawItem,
113  HdDirtyBits *dirtyBits,
114  const TfToken &reprToken,
115  const HdReprSharedPtr &repr,
116  const HdMeshReprDesc &desc,
117  bool requireSmoothNormals,
118  bool requireFlatNormals,
119  int geomSubsetDescIndex);
120 
122  HdRenderParam *renderParam,
123  HdStDrawItem *drawItem,
124  const HdMeshReprDesc &desc,
125  const SdfPath &materialId);
126 
127  void _UpdateShadersForAllReprs(HdSceneDelegate *sceneDelegate,
128  HdRenderParam *renderParam,
129  bool updateMaterialNetworkShader,
130  bool updateGeometricShader);
131 
133  HdRenderParam *renderParam);
134 
135  void _PopulateTopology(HdSceneDelegate *sceneDelegate,
136  HdRenderParam *renderParam,
137  HdStDrawItem *drawItem,
138  HdDirtyBits *dirtyBits,
139  const TfToken &reprToken,
140  const HdReprSharedPtr &repr,
141  const HdMeshReprDesc &desc,
142  int geomSubsetDescIndex);
143 
145  HdRenderParam *renderParam,
146  HdStDrawItem *drawItem,
147  const TfToken &reprToken,
148  const HdReprSharedPtr &repr,
149  const HdGeomSubsets &geomSubsets,
150  size_t oldNumGeomSubsets);
151 
153  HdStResourceRegistrySharedPtr resourceRegistry,
154  HdChangeTracker &changeTracker,
155  HdRenderParam *renderParam,
156  HdStDrawItem *drawItem,
157  const TfToken &indexToken,
158  HdBufferSourceSharedPtr indicesSource,
159  HdBufferSourceSharedPtr fvarIndicesSource,
160  HdBufferSourceSharedPtr geomSubsetFaceIndicesHelperSource,
161  const VtIntArray &faceIndices,
162  bool refined);
163 
164  void _GatherFaceVaryingTopologies(HdSceneDelegate *sceneDelegate,
165  const HdReprSharedPtr &repr,
166  const HdMeshReprDesc &desc,
167  HdStDrawItem *drawItem,
168  int geomSubsetDescIndex,
169  HdDirtyBits *dirtyBits,
170  const SdfPath &id,
171  HdSt_MeshTopologySharedPtr topology);
172 
173  void _PopulateAdjacency(
174  HdStResourceRegistrySharedPtr const &resourceRegistry);
175 
176  void _PopulateVertexPrimvars(HdSceneDelegate *sceneDelegate,
177  HdRenderParam *renderParam,
178  const HdReprSharedPtr &repr,
179  const HdMeshReprDesc &desc,
180  HdStDrawItem *drawItem,
181  int geomSubsetDescIndex,
182  HdDirtyBits *dirtyBits,
183  bool requireSmoothNormals);
184 
185  void _PopulateFaceVaryingPrimvars(HdSceneDelegate *sceneDelegate,
186  HdRenderParam *renderParam,
187  const HdReprSharedPtr &repr,
188  const HdMeshReprDesc &desc,
189  HdStDrawItem *drawItem,
190  int geomSubsetDescIndex,
191  HdDirtyBits *dirtyBits);
192 
193  void _PopulateElementPrimvars(HdSceneDelegate *sceneDelegate,
194  HdRenderParam *renderParam,
195  const HdReprSharedPtr &repr,
196  const HdMeshReprDesc &desc,
197  HdStDrawItem *drawItem,
198  int geomSubsetDescIndex,
199  HdDirtyBits *dirtyBits,
200  bool requireFlatNormals);
201 
202  int _GetRefineLevelForDesc(const HdMeshReprDesc &desc) const;
203 
204  // Helper class for meshes to keep track of the topologies of their
205  // face-varying primvars. The face-varying topologies are later passed to
206  // the OSD refiner in an order that will correspond to their face-varying
207  // channel number. We keep a vector of only the topologies in use, paired
208  // with their associated primvar names.
210  {
211  public:
213  return _topologies;
214  }
215 
216  // Add a primvar and its corresponding toplogy to the tracker
217  void AddOrUpdateTopology(const TfToken &primvar,
218  const VtIntArray &topology) {
219  for (size_t i = 0; i < _topologies.size(); ++i) {
220  // Found existing topology
221  if (_topologies[i].first == topology) {
222 
223  if (std::find(_topologies[i].second.begin(),
224  _topologies[i].second.end(),
225  primvar) == _topologies[i].second.end()) {
226  // Topology does not have that primvar assigned
227  RemovePrimvar(primvar);
228  _topologies[i].second.push_back(primvar);
229  }
230  return;
231  }
232  }
233 
234  // Found new topology
235  RemovePrimvar(primvar);
236  _topologies.push_back(
237  std::pair<VtIntArray, std::vector<TfToken>>(
238  topology, {primvar}));
239  }
240 
241  // Remove a primvar from the tracker.
242  void RemovePrimvar(const TfToken &primvar) {
243  for (size_t i = 0; i < _topologies.size(); ++i) {
244  _topologies[i].second.erase(std::find(
245  _topologies[i].second.begin(),
246  _topologies[i].second.end(),
247  primvar), _topologies[i].second.end());
248 
249  }
250  }
251 
252  // Remove unused topologies (topologies with no associated primvars), as
253  // we do not want to build stencil tables for them.
255  _topologies.erase(std::remove_if(
256  _topologies.begin(), _topologies.end(), NoPrimvars),
257  _topologies.end());
258  }
259 
260  // Get the face-varying channel given a primvar name. If the primvar is
261  // not in the tracker, returns -1.
262  int GetChannelFromPrimvar(const TfToken &primvar) const {
263  for (size_t i = 0; i < _topologies.size(); ++i) {
264  if (std::find(_topologies[i].second.begin(),
265  _topologies[i].second.end(),
266  primvar) !=
267  _topologies[i].second.end()) {
268  return i;
269  }
270  }
271  return -1;
272  }
273 
274  // Return a vector of all the face-varying topologies.
275  std::vector<VtIntArray> GetFvarTopologies() const {
276  std::vector<VtIntArray> fvarTopologies;
277  for (const auto& it : _topologies) {
278  fvarTopologies.push_back(it.first);
279  }
280  return fvarTopologies;
281  }
282 
283  size_t GetNumTopologies() const {
284  return _topologies.size();
285  }
286 
287  private:
288  // Helper function that returns true if a <topology, primvar vector> has
289  // no primvars.
290  static bool NoPrimvars(const std::pair<VtIntArray, std::vector<TfToken>>
291  &topology) {
292  return topology.second.empty();
293  }
294 
295  TopologyToPrimvarVector _topologies;
296  };
297 
298 private:
299  enum DrawingCoord {
300  HullTopology = HdDrawingCoord::CustomSlotsBegin,
301  PointsTopology,
302  FreeSlot // If the mesh topology has geom subsets, we might place
303  // them here as geom subsets are processed before instance
304  // primvars. The instance primvars will follow after. If there
305  // are no geom subsets, instance primvars start here.
306  };
307 
308  enum DirtyBits : HdDirtyBits {
309  DirtySmoothNormals = HdChangeTracker::CustomBitsBegin,
310  DirtyFlatNormals = (DirtySmoothNormals << 1),
311  DirtyIndices = (DirtyFlatNormals << 1),
312  DirtyHullIndices = (DirtyIndices << 1),
313  DirtyPointsIndices = (DirtyHullIndices << 1)
314  };
315 
316  HdSt_MeshTopologySharedPtr _topology;
317  HdSt_VertexAdjacencyBuilderSharedPtr _vertexAdjacencyBuilder;
318 
319  HdTopology::ID _topologyId;
320  HdTopology::ID _vertexPrimvarId;
321  HdDirtyBits _customDirtyBitsInUse;
322 
323  HdType _pointsDataType;
324  HdInterpolation _sceneNormalsInterpolation;
325  HdCullStyle _cullStyle;
326  bool _hasMirroredTransform : 1;
327  bool _doubleSided : 1;
328  bool _flatShadingEnabled : 1;
329  bool _displacementEnabled : 1;
330  bool _limitNormals : 1;
331  bool _sceneNormals : 1;
332  bool _hasVaryingTopology : 1; // The prim's topology has changed since
333  // the prim was created
334  bool _displayOpacity : 1;
335  bool _displayInOverlay : 1;
336  bool _occludedSelectionShowsThrough : 1;
337  bool _pointsShadingEnabled : 1;
338 
339  std::unique_ptr<_FvarTopologyTracker> _fvarTopologyTracker;
340 };
341 
342 
344 
345 #endif // PXR_IMAGING_HD_ST_MESH_H
GLint first
Definition: glcorearb.h:405
static const int CustomSlotsBegin
Definition: drawingCoord.h:68
HdCullStyle
Definition: enums.h:105
uint64_t ID
Definition: topology.h:21
HDST_API void Finalize(HdRenderParam *renderParam) override
uint32_t HdDirtyBits
Definition: types.h:143
std::shared_ptr< HdRepr > HdReprSharedPtr
Definition: rprim.h:30
void _PopulateElementPrimvars(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, const HdReprSharedPtr &repr, const HdMeshReprDesc &desc, HdStDrawItem *drawItem, int geomSubsetDescIndex, HdDirtyBits *dirtyBits, bool requireFlatNormals)
std::shared_ptr< class HdMeshTopology > HdMeshTopologySharedPtr
Definition: meshTopology.h:28
bool _UseSmoothNormals(HdSt_MeshTopologySharedPtr const &topology) const
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2138
HDST_API void Sync(HdSceneDelegate *delegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits, TfToken const &reprToken) override
void _PopulateAdjacency(HdStResourceRegistrySharedPtr const &resourceRegistry)
bool _UseFlatNormals(const HdMeshReprDesc &desc) const
HDST_API HdMeshTopologySharedPtr GetTopology() const override
Topology (member) getter.
Definition: token.h:70
std::shared_ptr< class HdSt_MeshTopology > HdSt_MeshTopologySharedPtr
Definition: mesh.h:32
std::vector< VtIntArray > GetFvarTopologies() const
Definition: mesh.h:275
HDST_API void UpdateRenderTag(HdSceneDelegate *delegate, HdRenderParam *renderParam) override
void RemovePrimvar(const TfToken &primvar)
Definition: mesh.h:242
GT_API const UT_StringHolder topology
void _GatherFaceVaryingTopologies(HdSceneDelegate *sceneDelegate, const HdReprSharedPtr &repr, const HdMeshReprDesc &desc, HdStDrawItem *drawItem, int geomSubsetDescIndex, HdDirtyBits *dirtyBits, const SdfPath &id, HdSt_MeshTopologySharedPtr topology)
int GetChannelFromPrimvar(const TfToken &primvar) const
Definition: mesh.h:262
HDST_API ~HdStMesh() override
void AddOrUpdateTopology(const TfToken &primvar, const VtIntArray &topology)
Definition: mesh.h:217
HDST_API HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const override
void _CreateTopologyRangeForGeomSubset(HdStResourceRegistrySharedPtr resourceRegistry, HdChangeTracker &changeTracker, HdRenderParam *renderParam, HdStDrawItem *drawItem, const TfToken &indexToken, HdBufferSourceSharedPtr indicesSource, HdBufferSourceSharedPtr fvarIndicesSource, HdBufferSourceSharedPtr geomSubsetFaceIndicesHelperSource, const VtIntArray &faceIndices, bool refined)
Definition: path.h:273
std::vector< struct HdBufferSpec > HdBufferSpecVector
HdBufferArrayRangeSharedPtr _GetSharedPrimvarRange(uint64_t primvarId, HdBufferSpecVector const &updatedOrAddedSpecs, HdBufferSpecVector const &removedSpecs, HdBufferArrayRangeSharedPtr const &curRange, bool *isFirstInstance, HdStResourceRegistrySharedPtr const &resourceRegistry) const
bool _MaterialHasPtex(const HdRenderIndex &renderIndex, const SdfPath &materialId) const
Definition: mesh.h:39
void _UpdateDrawItem(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, HdDirtyBits *dirtyBits, const TfToken &reprToken, const HdReprSharedPtr &repr, const HdMeshReprDesc &desc, bool requireSmoothNormals, bool requireFlatNormals, int geomSubsetDescIndex)
void _UpdateMaterialTagsForAllReprs(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam)
std::vector< HdGeomSubset > HdGeomSubsets
A group of geometry subsets.
Definition: geomSubset.h:43
std::vector< std::pair< VtIntArray, std::vector< TfToken >>> TopologyToPrimvarVector
size_t GetNumTopologies() const
Definition: mesh.h:283
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static HDST_API bool IsEnabledPackedNormals()
Returns whether packed (10_10_10 bits) normals to be used.
HF_MALLOC_TAG_NEW("new HdStMesh")
HDST_API HdStMesh(SdfPath const &id)
#define HDST_API
Definition: api.h:23
void _PopulateVertexPrimvars(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, const HdReprSharedPtr &repr, const HdMeshReprDesc &desc, HdStDrawItem *drawItem, int geomSubsetDescIndex, HdDirtyBits *dirtyBits, bool requireSmoothNormals)
HdInterpolation
Definition: enums.h:177
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: mesh.h:101
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:27
std::shared_ptr< class HdSt_VertexAdjacencyBuilder > HdSt_VertexAdjacencyBuilderSharedPtr
Definition: mesh.h:30
void _PopulateTopology(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, HdDirtyBits *dirtyBits, const TfToken &reprToken, const HdReprSharedPtr &repr, const HdMeshReprDesc &desc, int geomSubsetDescIndex)
void _UpdateDrawItemGeometricShader(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, const HdMeshReprDesc &desc, const SdfPath &materialId)
void _PopulateFaceVaryingPrimvars(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, const HdReprSharedPtr &repr, const HdMeshReprDesc &desc, HdStDrawItem *drawItem, int geomSubsetDescIndex, HdDirtyBits *dirtyBits)
HDST_API HdDirtyBits GetInitialDirtyBitsMask() const override
bool _UseLimitRefinement(const HdRenderIndex &renderIndex, const HdMeshTopology &topology) const
bool _MaterialHasLimitSurface(const HdRenderIndex &renderIndex, const SdfPath &materialId) const
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
void _UpdateRepr(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, TfToken const &reprToken, HdDirtyBits *dirtyBitsState)
HdType
Definition: types.h:272
std::shared_ptr< class HdStResourceRegistry > HdStResourceRegistrySharedPtr
Definition: commandBuffer.h:30
void _UpdateShadersForAllReprs(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, bool updateMaterialNetworkShader, bool updateGeometricShader)
const TopologyToPrimvarVector & GetTopologyToPrimvarVector() const
Definition: mesh.h:212
bool _UseQuadIndices(const HdRenderIndex &renderIndex, const HdSt_MeshTopologySharedPtr &topology) const
HDST_API void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits) override
int _GetRefineLevelForDesc(const HdMeshReprDesc &desc) const
void _UpdateDrawItemsForGeomSubsets(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, const TfToken &reprToken, const HdReprSharedPtr &repr, const HdGeomSubsets &geomSubsets, size_t oldNumGeomSubsets)