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