All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mesh.h
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"
30 #include "pxr/imaging/hd/changeTracker.h"
31 #include "pxr/imaging/hd/drawingCoord.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 
40 PXR_NAMESPACE_OPEN_SCOPE
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 
50 using HdStResourceRegistrySharedPtr =
51  std::shared_ptr<class HdStResourceRegistry>;
52 
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 
79  HDST_API
80  HdMeshTopologySharedPtr GetTopology() const override;
81 
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 
98  HdBufferArrayRangeSharedPtr
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 
123  void _UpdateDrawItemGeometricShader(HdSceneDelegate *sceneDelegate,
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.
173  class _FvarTopologyTracker
174  {
175  public:
176  const TopologyToPrimvarVector & GetTopologyToPrimvarVector() const {
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.
218  void RemoveUnusedTopologies() {
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 
302 PXR_NAMESPACE_CLOSE_SCOPE
303 
304 #endif // PXR_IMAGING_HD_ST_MESH_H
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:116
HDST_API HdDirtyBits _PropagateDirtyBits(HdDirtyBits bits) const override
This callback from Rprim gives the prim an opportunity to set additional dirty bits based on those al...
Descriptor to configure the drawItem(s) for a repr.
Definition: mesh.h:54
HDST_API void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits) override
Initialize the given representation of this Rprim.
The HdRenderParam is an opaque (to core Hydra) handle, to an object that is obtained from the render ...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
Adapter class providing data exchange with the client scene graph.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
A subdivision surface or poly-mesh object.
Definition: mesh.h:55
HDST_API HdMeshTopologySharedPtr GetTopology() const override
Topology (member) getter.
Hydra Schema for a subdivision surface or poly-mesh object.
Definition: mesh.h:105
HDST_API HdDirtyBits GetInitialDirtyBitsMask() const override
Returns the set of dirty bits that should be added to the change tracker for this prim...
HDST_API void Finalize(HdRenderParam *renderParam) override
Finalizes object resources.
HDST_API void Sync(HdSceneDelegate *delegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits, TfToken const &reprToken) override
Pull invalidated scene data and prepare/update the renderable representation.
static HDST_API bool IsEnabledPackedNormals()
Returns whether packed (10_10_10 bits) normals to be used.