Loading...
Searching...
No Matches
primUtils.h
1//
2// Copyright 2019 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_PRIM_UTILS_H
8#define PXR_IMAGING_HD_ST_PRIM_UTILS_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hdSt/api.h"
12#include "pxr/imaging/hdSt/resourceRegistry.h"
13#include "pxr/imaging/hd/sceneDelegate.h"
14#include "pxr/imaging/hd/rprim.h"
15
16#include <memory>
17#include <string>
18#include <vector>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
22class HdChangeTracker;
23class HdDrawItem;
24class HdRenderIndex;
25class HdRenderParam;
26class HdRprim;
27struct HdRprimSharedData;
28class HdStDrawItem;
29class HdStInstancer;
30
31using HdBufferArrayRangeSharedPtr = std::shared_ptr<class HdBufferArrayRange>;
32
33using HdBufferSourceSharedPtrVector = std::vector<HdBufferSourceSharedPtr>;
34using HdBufferSpecVector = std::vector<struct HdBufferSpec>;
35using HdSt_MaterialNetworkShaderSharedPtr =
36 std::shared_ptr<class HdSt_MaterialNetworkShader>;
37
38using HdStComputationSharedPtr = std::shared_ptr<class HdStComputation>;
39
40using HdStResourceRegistrySharedPtr =
41 std::shared_ptr<HdStResourceRegistry>;
42
43// -----------------------------------------------------------------------------
44// Draw invalidation and garbage collection utilities
45// -----------------------------------------------------------------------------
46HDST_API
47void HdStMarkDrawBatchesDirty(HdRenderParam *renderParam);
48
49HDST_API
50void HdStMarkMaterialTagsDirty(HdRenderParam *renderParam);
51
52HDST_API
53void HdStMarkGeomSubsetDrawItemsDirty(HdRenderParam *renderParam);
54
55HDST_API
56void HdStMarkGarbageCollectionNeeded(HdRenderParam *renderParam);
57
58// -----------------------------------------------------------------------------
59// Primvar descriptor filtering utilities
60// -----------------------------------------------------------------------------
61// Get filtered primvar descriptors for drawItem
62HDST_API
63HdPrimvarDescriptorVector
64HdStGetPrimvarDescriptors(
65 HdRprim const * prim,
66 HdStDrawItem const * drawItem,
67 HdSceneDelegate * delegate,
68 HdInterpolation interpolation,
69 const HdReprSharedPtr &repr = nullptr,
70 HdMeshGeomStyle descGeomStyle = HdMeshGeomStyleInvalid,
71 int geomSubsetDescIndex = 0,
72 size_t numGeomSubsets = 0);
73
74// Get filtered instancer primvar descriptors for drawItem
75HDST_API
76HdPrimvarDescriptorVector
77HdStGetInstancerPrimvarDescriptors(
78 HdStInstancer const * instancer,
79 HdSceneDelegate * delegate);
80
81// -----------------------------------------------------------------------------
82// Tracking render tag changes
83// -----------------------------------------------------------------------------
84
85HDST_API
86void HdStUpdateRenderTag(HdSceneDelegate *delegate,
87 HdRenderParam *renderParam,
88 HdRprim *rprim);
89
90// -----------------------------------------------------------------------------
91// Material processing utilities
92// -----------------------------------------------------------------------------
93HDST_API
94void HdStSetMaterialId(HdSceneDelegate *delegate,
95 HdRenderParam *renderParam,
96 HdRprim *rprim);
97
98HDST_API
99void HdStSetMaterialTag(HdRenderParam *renderParam,
100 HdDrawItem *drawItem,
101 const TfToken &materialTag);
102
103HDST_API
104void HdStSetMaterialTag(HdSceneDelegate *delegate,
105 HdRenderParam *renderParam,
106 HdDrawItem *drawItem,
107 SdfPath const & materialId,
108 const bool hasDisplayOpacityPrimvar,
109 const bool displayInOverlay,
110 const bool occludedSelectionShowsThrough);
111// Resolves the material network shader for the given prim (using a fallback
112// material as necessary).
113HDST_API
114HdSt_MaterialNetworkShaderSharedPtr
115HdStGetMaterialNetworkShader(
116 HdRprim const * prim,
117 HdSceneDelegate * delegate);
118
119HDST_API
120HdSt_MaterialNetworkShaderSharedPtr
121HdStGetMaterialNetworkShader(
122 HdRprim const * prim,
123 HdSceneDelegate * delegate,
124 SdfPath const & materialId);
125
126// -----------------------------------------------------------------------------
127// Primvar processing and BAR allocation utilities
128// -----------------------------------------------------------------------------
129// Returns true if range is non-empty and valid.
130HDST_API
131bool HdStIsValidBAR(HdBufferArrayRangeSharedPtr const& range);
132
133// Returns true if curRange can be used as-is (even if it's empty) during
134// primvar processing.
135HDST_API
136bool HdStCanSkipBARAllocationOrUpdate(
137 HdBufferSourceSharedPtrVector const& sources,
138 HdStComputationComputeQueuePairVector const& computations,
139 HdBufferArrayRangeSharedPtr const& curRange,
140 HdDirtyBits dirtyBits);
141
142HDST_API
143bool HdStCanSkipBARAllocationOrUpdate(
144 HdBufferSourceSharedPtrVector const& sources,
145 HdBufferArrayRangeSharedPtr const& curRange,
146 HdDirtyBits dirtyBits);
147
148// Returns the buffer specs that have been removed from curRange based on the
149// new primvar descriptors and internally generated primvar names.
150//
151// Internally generated primvar names will never be among the specs returned,
152HDST_API
153HdBufferSpecVector
154HdStGetRemovedPrimvarBufferSpecs(
155 HdBufferArrayRangeSharedPtr const& curRange,
156 HdPrimvarDescriptorVector const& newPrimvarDescs,
157 HdExtComputationPrimvarDescriptorVector const& newCompPrimvarDescs,
158 TfTokenVector const& internallyGeneratedPrimvarNames,
159 SdfPath const& rprimId);
160
161HDST_API
162HdBufferSpecVector
163HdStGetRemovedPrimvarBufferSpecs(
164 HdBufferArrayRangeSharedPtr const& curRange,
165 HdPrimvarDescriptorVector const& newPrimvarDescs,
166 TfTokenVector const& internallyGeneratedPrimvarNames,
167 SdfPath const& rprimId);
168
169// Returns the buffer specs that have been removed from curRange based on the
170// new primvar descriptors, updated specs and internally generated primvar names. Buffer
171// specs with updated types will be replaced.
172// This overload handles primvar type changes and should be preferred over
173// HdStGetRemovedPrimvarBufferSpecs.
174//
175HDST_API
176HdBufferSpecVector
177HdStGetRemovedOrReplacedPrimvarBufferSpecs(
178 HdBufferArrayRangeSharedPtr const& curRange,
179 HdPrimvarDescriptorVector const& newPrimvarDescs,
180 TfTokenVector const& internallyGeneratedPrimvarNames,
181 HdBufferSpecVector const& updatedSpecs,
182 SdfPath const& rprimId);
183
184// Updates the existing range at drawCoordIndex with newRange and flags garbage
185// collection (for the existing range) and rebuild of all draw batches when
186// necessary.
187HDST_API
188void HdStUpdateDrawItemBAR(
189 HdBufferArrayRangeSharedPtr const& newRange,
190 int drawCoordIndex,
191 HdRprimSharedData *sharedData,
192 HdRenderParam *renderParam,
193 HdChangeTracker *changeTracker);
194
195// Returns true if primvar with primvarName exists within primvar descriptor
196// vector primvars and primvar has a valid value
197HDST_API
198bool HdStIsPrimvarExistentAndValid(
199 HdRprim *prim,
200 HdSceneDelegate *delegate,
201 HdPrimvarDescriptorVector const& primvars,
202 TfToken const& primvarName);
203
204HDST_API
205bool HdStIsPrimvarValidForDrawItem(
206 const HdStDrawItem *drawItem,
207 TfToken const &primvarName,
208 VtValue const &primvarValue);
209
210// -----------------------------------------------------------------------------
211// Constant primvar processing utilities
212// -----------------------------------------------------------------------------
213// Returns whether constant primvars need to be populated/updated based on the
214// dirty bits for a given rprim.
215HDST_API
216bool HdStShouldPopulateConstantPrimvars(
217 HdDirtyBits const *dirtyBits,
218 SdfPath const& id);
219
220// Given prim information it will create sources representing
221// constant primvars and hand it to the resource registry.
222// If transforms are dirty, updates the optional bool.
223// Will include customSpecs in the BAR buffer specs, but is not responsible
224// for their sources.
225HDST_API
226void HdStPopulateConstantPrimvars(
227 HdRprim *prim,
228 HdRprimSharedData *sharedData,
229 HdSceneDelegate *delegate,
230 HdRenderParam *renderParam,
231 HdStDrawItem *drawItem,
232 HdDirtyBits *dirtyBits,
233 HdReprSharedPtr const &repr,
234 HdMeshGeomStyle descGeomStyle,
235 int geomSubsetDescIndex,
236 size_t numGeomSubsets,
237 HdBufferSpecVector const& customSpecs,
238 bool *hasMirroredTransform = nullptr,
239 bool *hasDisplayOpacity = nullptr,
240 bool *hasNormals = nullptr);
241
242HDST_API
243void HdStPopulateConstantPrimvars(
244 HdRprim *prim,
245 HdRprimSharedData *sharedData,
246 HdSceneDelegate *delegate,
247 HdRenderParam *renderParam,
248 HdStDrawItem *drawItem,
249 HdDirtyBits *dirtyBits,
250 HdReprSharedPtr const &repr,
251 HdMeshGeomStyle descGeomStyle,
252 int geomSubsetDescIndex,
253 size_t numGeomSubsets,
254 bool *hasMirroredTransform = nullptr,
255 bool *hasDisplayOpacity = nullptr,
256 bool *hasNormals = nullptr);
257
258// -----------------------------------------------------------------------------
259// Instancer processing utilities
260// -----------------------------------------------------------------------------
261
262// Updates drawItem bindings for changes to instance topology/primvars.
263HDST_API
264void HdStUpdateInstancerData(
265 HdRenderIndex &renderIndex,
266 HdRenderParam *renderParam,
267 HdRprim *prim,
268 HdStDrawItem *drawItem,
269 HdRprimSharedData *sharedData,
270 HdDirtyBits rprimDirtyBits,
271 bool *hasDisplayOpacity = nullptr,
272 bool *hasNormals = nullptr);
273
274// Returns true if primvar with primvarName exists among instance primvar
275// descriptors.
276HDST_API
277bool HdStIsInstancePrimvarExistentAndValid(
278 HdRenderIndex &renderIndex,
279 HdRprim *prim,
280 TfToken const& primvarName);
281
282// -----------------------------------------------------------------------------
283// Topological visibility processing utility
284// -----------------------------------------------------------------------------
285// Creates/Updates/Migrates the topology visiblity BAR with element and point
286// visibility encoded using one bit per element/point of the topology.
287HDST_API
288void HdStProcessTopologyVisibility(
289 VtIntArray invisibleElements,
290 int numTotalElements,
291 VtIntArray invisiblePoints,
292 int numTotalPoints,
293 HdRprimSharedData *sharedData,
294 HdStDrawItem *drawItem,
295 HdRenderParam *renderParam,
296 HdChangeTracker *changeTracker,
297 HdStResourceRegistrySharedPtr const &resourceRegistry,
298 SdfPath const& rprimId);
299
300//
301// De-duplicating and sharing immutable primvar data.
302//
303// Primvar data is identified using a hash computed from the
304// sources of the primvar data, of which there are generally
305// two kinds:
306// - data provided by the scene delegate
307// - data produced by computations
308//
309// Immutable and mutable buffer data is managed using distinct
310// heaps in the resource registry. Aggregation of buffer array
311// ranges within each heap is managed separately.
312//
313// We attempt to balance the benefits of sharing vs efficient
314// varying update using the following simple strategy:
315//
316// - When populating the first repr for an rprim, allocate
317// the primvar range from the immutable heap and attempt
318// to deduplicate the data by looking up the primvarId
319// in the primvar instance registry.
320//
321// - When populating an additional repr for an rprim using
322// an existing immutable primvar range, compute an updated
323// primvarId and allocate from the immutable heap, again
324// attempting to deduplicate.
325//
326// - Otherwise, migrate the primvar data to the mutable heap
327// and abandon further attempts to deduplicate.
328//
329// - The computation of the primvarId for an rprim is cumulative
330// and includes the new sources of data being committed
331// during each successive update.
332//
333// - Once we have migrated a primvar allocation to the mutable
334// heap we will no longer spend time computing a primvarId.
335//
336
337HDST_API
338bool HdStIsEnabledSharedVertexPrimvar();
339
340HDST_API
341uint64_t HdStComputeSharedPrimvarId(
342 uint64_t baseId,
343 HdBufferSourceSharedPtrVector const &sources,
344 HdStComputationComputeQueuePairVector const &computations);
345
346HDST_API
347void HdStGetBufferSpecsFromCompuations(
348 HdStComputationComputeQueuePairVector const& computations,
349 HdBufferSpecVector *bufferSpecs);
350
351PXR_NAMESPACE_CLOSE_SCOPE
352
353#endif // PXR_IMAGING_HD_ST_PRIM_UTILS_H
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine.
A draw item is a light-weight representation of an HdRprim's resources and material to be used for re...
Definition drawItem.h:48
The render index is part of the Hydra 1.0 API and is only used for emulation purposes so that HdScene...
The HdRenderParam is an opaque (to core Hydra) handle, to an object that is obtained from the render ...
The render engine state for a given rprim from the scene graph.
Definition rprim.h:38
Adapter class providing data exchange with the client scene graph.
HdSt implements instancing by drawing each proto multiple times with a single draw call.
Definition instancer.h:51
A path value used to locate objects in layers or scenegraphs.
Definition path.h:281
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:71
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition value.h:90
std::vector< TfToken > TfTokenVector
Convenience types.
Definition token.h:440