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 
46 using Hd_VertexAdjacencySharedPtr = std::shared_ptr<class Hd_VertexAdjacency>;
47 using HdBufferSourceSharedPtr = std::shared_ptr<class HdBufferSource>;
48 using HdSt_MeshTopologySharedPtr = std::shared_ptr<class HdSt_MeshTopology>;
49 
51  std::shared_ptr<class HdStResourceRegistry>;
52 
53 /// A subdivision surface or poly-mesh object.
54 ///
55 class HdStMesh final : public HdMesh
56 {
57 public:
58  HF_MALLOC_TAG_NEW("new HdStMesh");
59 
60  HDST_API
61  HdStMesh(SdfPath const& id);
62 
63  HDST_API
64  ~HdStMesh() override;
65 
66  HDST_API
67  void Sync(HdSceneDelegate *delegate,
68  HdRenderParam *renderParam,
69  HdDirtyBits *dirtyBits,
70  TfToken const &reprToken) override;
71 
72  HDST_API
73  void Finalize(HdRenderParam *renderParam) override;
74 
75  HDST_API
76  HdDirtyBits GetInitialDirtyBitsMask() const override;
77 
78  /// Topology (member) getter
79  HDST_API
80  HdMeshTopologySharedPtr GetTopology() const override;
81 
82  /// Returns whether packed (10_10_10 bits) normals to be used
83  HDST_API
84  static bool IsEnabledPackedNormals();
85 
86 protected:
87  HDST_API
88  void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits) override;
89 
90  HDST_API
91  HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const override;
92 
93  void _UpdateRepr(HdSceneDelegate *sceneDelegate,
94  HdRenderParam *renderParam,
95  TfToken const &reprToken,
96  HdDirtyBits *dirtyBitsState);
97 
99  _GetSharedPrimvarRange(uint64_t primvarId,
100  HdBufferSpecVector const &updatedOrAddedSpecs,
101  HdBufferSpecVector const &removedSpecs,
102  HdBufferArrayRangeSharedPtr const &curRange,
103  bool * isFirstInstance,
104  HdStResourceRegistrySharedPtr const &resourceRegistry) const;
105 
106  bool _UseQuadIndices(const HdRenderIndex &renderIndex,
107  HdSt_MeshTopologySharedPtr const & topology) const;
108 
109  bool _UseLimitRefinement(const HdRenderIndex &renderIndex) const;
110 
111  bool _UseSmoothNormals(HdSt_MeshTopologySharedPtr const& topology) const;
112 
113  bool _UseFlatNormals(const HdMeshReprDesc &desc) const;
114 
115  void _UpdateDrawItem(HdSceneDelegate *sceneDelegate,
116  HdRenderParam *renderParam,
117  HdStDrawItem *drawItem,
118  HdDirtyBits *dirtyBits,
119  const HdMeshReprDesc &desc,
120  bool requireSmoothNormals,
121  bool requireFlatNormals);
122 
124  HdRenderParam *renderParam,
125  HdStDrawItem *drawItem,
126  const HdMeshReprDesc &desc);
127 
128  void _UpdateShadersForAllReprs(HdSceneDelegate *sceneDelegate,
129  HdRenderParam *renderParam,
130  bool updateMaterialShader,
131  bool updateGeometricShader);
132 
133  void _PopulateTopology(HdSceneDelegate *sceneDelegate,
134  HdRenderParam *renderParam,
135  HdStDrawItem *drawItem,
136  HdDirtyBits *dirtyBits,
137  const HdMeshReprDesc &desc);
138 
139  void _PopulateAdjacency(
140  HdStResourceRegistrySharedPtr const &resourceRegistry);
141 
142  void _GatherFaceVaryingTopologies(HdSceneDelegate *sceneDelegate,
143  HdStDrawItem *drawItem,
144  HdDirtyBits *dirtyBits,
145  const SdfPath &id,
146  HdSt_MeshTopologySharedPtr topology);
147 
148  void _PopulateVertexPrimvars(HdSceneDelegate *sceneDelegate,
149  HdRenderParam *renderParam,
150  HdStDrawItem *drawItem,
151  HdDirtyBits *dirtyBits,
152  bool requireSmoothNormals);
153 
154  void _PopulateFaceVaryingPrimvars(HdSceneDelegate *sceneDelegate,
155  HdRenderParam *renderParam,
156  HdStDrawItem *drawItem,
157  HdDirtyBits *dirtyBits,
158  const HdMeshReprDesc &desc);
159 
160  void _PopulateElementPrimvars(HdSceneDelegate *sceneDelegate,
161  HdRenderParam *renderParam,
162  HdStDrawItem *drawItem,
163  HdDirtyBits *dirtyBits,
164  bool requireFlatNormals);
165 
166  int _GetRefineLevelForDesc(const HdMeshReprDesc &desc) const;
167 
168  // Helper class for meshes to keep track of the topologies of their
169  // face-varying primvars. The face-varying topologies are later passed to
170  // the OSD refiner in an order that will correspond to their face-varying
171  // channel number. We keep a vector of only the topologies in use, paired
172  // with their associated primvar names.
174  {
175  public:
177  return _topologies;
178  }
179 
180  // Add a primvar and its corresponding toplogy to the tracker
181  void AddOrUpdateTopology(const TfToken &primvar,
182  const VtIntArray &topology) {
183  for (size_t i = 0; i < _topologies.size(); ++i) {
184  // Found existing topology
185  if (_topologies[i].first == topology) {
186 
187  if (std::find(_topologies[i].second.begin(),
188  _topologies[i].second.end(),
189  primvar) == _topologies[i].second.end()) {
190  // Topology does not have that primvar assigned
191  RemovePrimvar(primvar);
192  _topologies[i].second.push_back(primvar);
193  }
194  return;
195  }
196  }
197 
198  // Found new topology
199  RemovePrimvar(primvar);
200  _topologies.push_back(
201  std::pair<VtIntArray, std::vector<TfToken>>(
202  topology, {primvar}));
203  }
204 
205  // Remove a primvar from the tracker.
206  void RemovePrimvar(const TfToken &primvar) {
207  for (size_t i = 0; i < _topologies.size(); ++i) {
208  _topologies[i].second.erase(std::find(
209  _topologies[i].second.begin(),
210  _topologies[i].second.end(),
211  primvar), _topologies[i].second.end());
212 
213  }
214  }
215 
216  // Remove unused topologies (topologies with no associated primvars), as
217  // we do not want to build stencil tables for them.
219  _topologies.erase(std::remove_if(
220  _topologies.begin(), _topologies.end(), NoPrimvars),
221  _topologies.end());
222  }
223 
224  // Get the face-varying channel given a primvar name. If the primvar is
225  // not in the tracker, returns -1.
226  int GetChannelFromPrimvar(const TfToken &primvar) const {
227  for (size_t i = 0; i < _topologies.size(); ++i) {
228  if (std::find(_topologies[i].second.begin(),
229  _topologies[i].second.end(),
230  primvar) !=
231  _topologies[i].second.end()) {
232  return i;
233  }
234  }
235  return -1;
236  }
237 
238  // Return a vector of all the face-varying topologies.
239  std::vector<VtIntArray> GetFvarTopologies() const {
240  std::vector<VtIntArray> fvarTopologies;
241  for (const auto& it : _topologies) {
242  fvarTopologies.push_back(it.first);
243  }
244  return fvarTopologies;
245  }
246 
247  size_t GetNumTopologies() const {
248  return _topologies.size();
249  }
250 
251  private:
252  // Helper function that returns true if a <topology, primvar vector> has
253  // no primvars.
254  static bool NoPrimvars(const std::pair<VtIntArray, std::vector<TfToken>>
255  &topology) {
256  return topology.second.empty();
257  }
258 
259  TopologyToPrimvarVector _topologies;
260  };
261 
262 private:
263  enum DrawingCoord {
264  HullTopology = HdDrawingCoord::CustomSlotsBegin,
265  PointsTopology,
266  InstancePrimvar // has to be at the very end
267  };
268 
269  enum DirtyBits : HdDirtyBits {
270  DirtySmoothNormals = HdChangeTracker::CustomBitsBegin,
271  DirtyFlatNormals = (DirtySmoothNormals << 1),
272  DirtyIndices = (DirtyFlatNormals << 1),
273  DirtyHullIndices = (DirtyIndices << 1),
274  DirtyPointsIndices = (DirtyHullIndices << 1)
275  };
276 
277  HdSt_MeshTopologySharedPtr _topology;
278  Hd_VertexAdjacencySharedPtr _vertexAdjacency;
279 
280  HdTopology::ID _topologyId;
281  HdTopology::ID _vertexPrimvarId;
282  HdDirtyBits _customDirtyBitsInUse;
283 
284  HdType _pointsDataType;
285  HdInterpolation _sceneNormalsInterpolation;
286  HdCullStyle _cullStyle;
287  bool _hasMirroredTransform : 1;
288  bool _doubleSided : 1;
289  bool _flatShadingEnabled : 1;
290  bool _displacementEnabled : 1;
291  bool _limitNormals : 1;
292  bool _sceneNormals : 1;
293  bool _hasVaryingTopology : 1; // The prim's topology has changed since
294  // the prim was created
295  bool _displayOpacity : 1;
296  bool _occludedSelectionShowsThrough : 1;
297 
298  std::unique_ptr<_FvarTopologyTracker> _fvarTopologyTracker;
299 };
300 
301 
303 
304 #endif // PXR_IMAGING_HD_ST_MESH_H
static const int CustomSlotsBegin
Definition: drawingCoord.h:85
HdCullStyle
Definition: enums.h:122
GLint first
Definition: glcorearb.h:404
uint64_t ID
Definition: topology.h:38
HDST_API void Finalize(HdRenderParam *renderParam) override
uint32_t HdDirtyBits
Definition: types.h:127
void _UpdateDrawItemGeometricShader(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, const HdMeshReprDesc &desc)
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 _UseQuadIndices(const HdRenderIndex &renderIndex, HdSt_MeshTopologySharedPtr const &topology) const
void _PopulateElementPrimvars(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, HdDirtyBits *dirtyBits, bool requireFlatNormals)
void _UpdateDrawItem(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, HdDirtyBits *dirtyBits, const HdMeshReprDesc &desc, bool requireSmoothNormals, bool requireFlatNormals)
bool _UseFlatNormals(const HdMeshReprDesc &desc) const
HDST_API HdMeshTopologySharedPtr GetTopology() const override
Topology (member) getter.
void _PopulateVertexPrimvars(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, HdDirtyBits *dirtyBits, bool requireSmoothNormals)
Definition: token.h:87
std::shared_ptr< class HdSt_MeshTopology > HdSt_MeshTopologySharedPtr
Definition: mesh.h:48
std::vector< VtIntArray > GetFvarTopologies() const
Definition: mesh.h:239
void _UpdateShadersForAllReprs(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, bool updateMaterialShader, bool updateGeometricShader)
void RemovePrimvar(const TfToken &primvar)
Definition: mesh.h:206
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2929
GT_API const UT_StringHolder topology
int GetChannelFromPrimvar(const TfToken &primvar) const
Definition: mesh.h:226
HDST_API ~HdStMesh() override
void AddOrUpdateTopology(const TfToken &primvar, const VtIntArray &topology)
Definition: mesh.h:181
HDST_API HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const override
Definition: path.h:288
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
Definition: mesh.h:55
std::vector< std::pair< VtIntArray, std::vector< TfToken >>> TopologyToPrimvarVector
size_t GetNumTopologies() const
Definition: mesh.h:247
void _PopulateFaceVaryingPrimvars(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, HdDirtyBits *dirtyBits, const HdMeshReprDesc &desc)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
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
HdInterpolation
Definition: enums.h:194
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: mesh.h:105
std::shared_ptr< HdBufferArrayRange > HdBufferArrayRangeSharedPtr
Definition: bufferArray.h:45
void _GatherFaceVaryingTopologies(HdSceneDelegate *sceneDelegate, HdStDrawItem *drawItem, HdDirtyBits *dirtyBits, const SdfPath &id, HdSt_MeshTopologySharedPtr topology)
HDST_API HdDirtyBits GetInitialDirtyBitsMask() const override
bool _UseLimitRefinement(const HdRenderIndex &renderIndex) const
std::shared_ptr< class Hd_VertexAdjacency > Hd_VertexAdjacencySharedPtr
void _PopulateTopology(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, HdStDrawItem *drawItem, HdDirtyBits *dirtyBits, const HdMeshReprDesc &desc)
std::shared_ptr< class HdBufferSource > HdBufferSourceSharedPtr
void _UpdateRepr(HdSceneDelegate *sceneDelegate, HdRenderParam *renderParam, TfToken const &reprToken, HdDirtyBits *dirtyBitsState)
HdType
Definition: types.h:256
std::shared_ptr< class HdStResourceRegistry > HdStResourceRegistrySharedPtr
Definition: commandBuffer.h:47
const TopologyToPrimvarVector & GetTopologyToPrimvarVector() const
Definition: mesh.h:176
HDST_API void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits) override
int _GetRefineLevelForDesc(const HdMeshReprDesc &desc) const