This document is for a version of USD that is under development. See this page for the current release.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mesh.h
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"
13#include "pxr/imaging/hd/changeTracker.h"
14#include "pxr/imaging/hd/drawingCoord.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
23PXR_NAMESPACE_OPEN_SCOPE
24
25
26class HdStDrawItem;
27class HdSceneDelegate;
28
29using HdSt_VertexAdjacencyBuilderSharedPtr =
30 std::shared_ptr<class HdSt_VertexAdjacencyBuilder>;
31using HdBufferSourceSharedPtr = std::shared_ptr<class HdBufferSource>;
32using HdSt_MeshTopologySharedPtr = std::shared_ptr<class HdSt_MeshTopology>;
33
34using HdStResourceRegistrySharedPtr =
35 std::shared_ptr<class HdStResourceRegistry>;
36
39class HdStMesh final : public HdMesh
40{
41public:
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
67 HDST_API
68 HdMeshTopologySharedPtr GetTopology() const override;
69
71 HDST_API
73
74protected:
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
86 HdBufferArrayRangeSharedPtr
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,
98 const HdSt_MeshTopologySharedPtr &topology) const;
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
121 void _UpdateDrawItemGeometricShader(HdSceneDelegate *sceneDelegate,
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
132 void _UpdateMaterialTagsForAllReprs(HdSceneDelegate *sceneDelegate,
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
144 void _UpdateDrawItemsForGeomSubsets(HdSceneDelegate *sceneDelegate,
145 HdRenderParam *renderParam,
146 HdStDrawItem *drawItem,
147 const TfToken &reprToken,
148 const HdReprSharedPtr &repr,
149 const HdGeomSubsets &geomSubsets,
150 size_t oldNumGeomSubsets);
151
152 void _CreateTopologyRangeForGeomSubset(
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.
209 class _FvarTopologyTracker
210 {
211 public:
212 const TopologyToPrimvarVector & GetTopologyToPrimvarVector() const {
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.
254 void RemoveUnusedTopologies() {
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
298private:
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 _occludedSelectionShowsThrough : 1;
336 bool _pointsShadingEnabled : 1;
337
338 std::unique_ptr<_FvarTopologyTracker> _fvarTopologyTracker;
339};
340
341
342PXR_NAMESPACE_CLOSE_SCOPE
343
344#endif // PXR_IMAGING_HD_ST_MESH_H
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine.
Definition: changeTracker.h:35
Hydra Schema for a subdivision surface or poly-mesh object.
Definition: mesh.h:102
Topology data for meshes.
Definition: meshTopology.h:38
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:104
The HdRenderParam is an opaque (to core Hydra) handle, to an object that is obtained from the render ...
Adapter class providing data exchange with the client scene graph.
A subdivision surface or poly-mesh object.
Definition: mesh.h:40
HDST_API void Sync(HdSceneDelegate *delegate, HdRenderParam *renderParam, HdDirtyBits *dirtyBits, TfToken const &reprToken) override
Pull invalidated scene data and prepare/update the renderable representation.
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...
HDST_API void _InitRepr(TfToken const &reprToken, HdDirtyBits *dirtyBits) override
Initialize the given representation of this Rprim.
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 HdMeshTopologySharedPtr GetTopology() const override
Topology (member) getter.
static HDST_API bool IsEnabledPackedNormals()
Returns whether packed (10_10_10 bits) normals to be used.
HDST_API void Finalize(HdRenderParam *renderParam) override
Finalizes object resources.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Descriptor to configure the drawItem(s) for a repr.
Definition: mesh.h:38