Loading...
Searching...
No Matches
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
40PXR_NAMESPACE_OPEN_SCOPE
41
42
43class HdStDrawItem;
44class HdSceneDelegate;
45
46using HdSt_VertexAdjacencyBuilderSharedPtr =
47 std::shared_ptr<class HdSt_VertexAdjacencyBuilder>;
48using HdBufferSourceSharedPtr = std::shared_ptr<class HdBufferSource>;
49using HdSt_MeshTopologySharedPtr = std::shared_ptr<class HdSt_MeshTopology>;
50
51using HdStResourceRegistrySharedPtr =
52 std::shared_ptr<class HdStResourceRegistry>;
53
56class HdStMesh final : public HdMesh
57{
58public:
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
84 HDST_API
85 HdMeshTopologySharedPtr GetTopology() const override;
86
88 HDST_API
90
91protected:
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
103 HdBufferArrayRangeSharedPtr
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
138 void _UpdateDrawItemGeometricShader(HdSceneDelegate *sceneDelegate,
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
149 void _UpdateMaterialTagsForAllReprs(HdSceneDelegate *sceneDelegate,
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
161 void _UpdateDrawItemsForGeomSubsets(HdSceneDelegate *sceneDelegate,
162 HdRenderParam *renderParam,
163 HdStDrawItem *drawItem,
164 const TfToken &reprToken,
165 const HdReprSharedPtr &repr,
166 const HdGeomSubsets &geomSubsets,
167 size_t oldNumGeomSubsets);
168
169 void _CreateTopologyRangeForGeomSubset(
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.
226 class _FvarTopologyTracker
227 {
228 public:
229 const TopologyToPrimvarVector & GetTopologyToPrimvarVector() const {
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.
271 void RemoveUnusedTopologies() {
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
315private:
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
359PXR_NAMESPACE_CLOSE_SCOPE
360
361#endif // PXR_IMAGING_HD_ST_MESH_H
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine.
Definition: changeTracker.h:52
Hydra Schema for a subdivision surface or poly-mesh object.
Definition: mesh.h:113
Topology data for meshes.
Definition: meshTopology.h:55
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:121
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:57
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:290
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
Descriptor to configure the drawItem(s) for a repr.
Definition: mesh.h:55