All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primUtils.h
1 //
2 // Copyright 2019 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_PRIM_UTILS_H
25 #define PXR_IMAGING_HD_ST_PRIM_UTILS_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdSt/api.h"
29 #include "pxr/imaging/hdSt/resourceRegistry.h"
30 #include "pxr/imaging/hd/sceneDelegate.h"
31 
32 #include <memory>
33 #include <string>
34 #include <vector>
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 class HdChangeTracker;
39 class HdDrawItem;
40 class HdRenderIndex;
41 class HdRenderParam;
42 class HdRprim;
43 struct HdRprimSharedData;
44 class HdStDrawItem;
45 class HdStInstancer;
46 
47 using HdBufferArrayRangeSharedPtr = std::shared_ptr<class HdBufferArrayRange>;
48 
49 using HdBufferSourceSharedPtrVector = std::vector<HdBufferSourceSharedPtr>;
50 using HdBufferSpecVector = std::vector<struct HdBufferSpec>;
51 using HdStShaderCodeSharedPtr = std::shared_ptr<class HdStShaderCode>;
52 
53 using HdComputationSharedPtr = std::shared_ptr<class HdComputation>;
54 
55 using HdStResourceRegistrySharedPtr =
56  std::shared_ptr<HdStResourceRegistry>;
57 
58 // -----------------------------------------------------------------------------
59 // Draw invalidation and garbage collection utilities
60 // -----------------------------------------------------------------------------
61 HDST_API
62 void HdStMarkDrawBatchesDirty(HdRenderParam *renderParam);
63 
64 HDST_API
65 void HdStMarkMaterialTagsDirty(HdRenderParam *renderParam);
66 
67 HDST_API
68 void HdStMarkGarbageCollectionNeeded(HdRenderParam *renderParam);
69 
70 // -----------------------------------------------------------------------------
71 // Primvar descriptor filtering utilities
72 // -----------------------------------------------------------------------------
73 // Get filtered primvar descriptors for drawItem
74 HDST_API
75 HdPrimvarDescriptorVector
76 HdStGetPrimvarDescriptors(
77  HdRprim const * prim,
78  HdStDrawItem const * drawItem,
79  HdSceneDelegate * delegate,
80  HdInterpolation interpolation);
81 
82 // Get filtered instancer primvar descriptors for drawItem
83 HDST_API
84 HdPrimvarDescriptorVector
85 HdStGetInstancerPrimvarDescriptors(
86  HdStInstancer const * instancer,
87  HdSceneDelegate * delegate);
88 
89 // -----------------------------------------------------------------------------
90 // Material processing utilities
91 // -----------------------------------------------------------------------------
92 HDST_API
93 void HdStSetMaterialId(HdSceneDelegate *delegate,
94  HdRenderParam *renderParam,
95  HdRprim *rprim);
96 
97 HDST_API
98 void HdStSetMaterialTag(HdSceneDelegate *delegate,
99  HdRenderParam *renderParam,
100  HdRprim *rprim,
101  bool hasDisplayOpacityPrimvar,
102  bool occludedSelectionShowsThrough);
103 // Resolves the material shader for the given prim (using a fallback
104 // material as necessary).
105 HDST_API
106 HdStShaderCodeSharedPtr
107 HdStGetMaterialShader(
108  HdRprim const * prim,
109  HdSceneDelegate * delegate);
110 
111 // -----------------------------------------------------------------------------
112 // Primvar processing and BAR allocation utilities
113 // -----------------------------------------------------------------------------
114 // Returns true if range is non-empty and valid.
115 HDST_API
116 bool HdStIsValidBAR(HdBufferArrayRangeSharedPtr const& range);
117 
118 // Returns true if curRange can be used as-is (even if it's empty) during
119 // primvar processing.
120 HDST_API
121 bool HdStCanSkipBARAllocationOrUpdate(
122  HdBufferSourceSharedPtrVector const& sources,
123  HdStComputationSharedPtrVector const& computations,
124  HdBufferArrayRangeSharedPtr const& curRange,
125  HdDirtyBits dirtyBits);
126 
127 HDST_API
128 bool HdStCanSkipBARAllocationOrUpdate(
129  HdBufferSourceSharedPtrVector const& sources,
130  HdBufferArrayRangeSharedPtr const& curRange,
131  HdDirtyBits dirtyBits);
132 
133 // Returns the buffer specs that have been removed from curRange based on the
134 // new primvar descriptors and internally generated primvar names.
135 //
136 // Internally generated primvar names will never be among the specs returned,
137 HDST_API
138 HdBufferSpecVector
139 HdStGetRemovedPrimvarBufferSpecs(
140  HdBufferArrayRangeSharedPtr const& curRange,
141  HdPrimvarDescriptorVector const& newPrimvarDescs,
142  HdExtComputationPrimvarDescriptorVector const& newCompPrimvarDescs,
143  TfTokenVector const& internallyGeneratedPrimvarNames,
144  SdfPath const& rprimId);
145 
146 HDST_API
147 HdBufferSpecVector
148 HdStGetRemovedPrimvarBufferSpecs(
149  HdBufferArrayRangeSharedPtr const& curRange,
150  HdPrimvarDescriptorVector const& newPrimvarDescs,
151  TfTokenVector const& internallyGeneratedPrimvarNames,
152  SdfPath const& rprimId);
153 
154 // Updates the existing range at drawCoordIndex with newRange and flags garbage
155 // collection (for the existing range) and rebuild of all draw batches when
156 // necessary.
157 HDST_API
158 void HdStUpdateDrawItemBAR(
159  HdBufferArrayRangeSharedPtr const& newRange,
160  int drawCoordIndex,
161  HdRprimSharedData *sharedData,
162  HdRenderParam *renderParam,
163  HdChangeTracker *changeTracker);
164 
165 // Returns true if primvar with primvarName exists within primvar descriptor
166 // vector primvars and primvar has a valid value
167 HDST_API
168 bool HdStIsPrimvarExistentAndValid(
169  HdRprim *prim,
170  HdSceneDelegate *delegate,
171  HdPrimvarDescriptorVector const& primvars,
172  TfToken const& primvarName);
173 
174 // -----------------------------------------------------------------------------
175 // Constant primvar processing utilities
176 // -----------------------------------------------------------------------------
177 // Returns whether constant primvars need to be populated/updated based on the
178 // dirty bits for a given rprim.
179 HDST_API
180 bool HdStShouldPopulateConstantPrimvars(
181  HdDirtyBits const *dirtyBits,
182  SdfPath const& id);
183 
184 // Given prim information it will create sources representing
185 // constant primvars and hand it to the resource registry.
186 // If transforms are dirty, updates the optional bool.
187 HDST_API
188 void HdStPopulateConstantPrimvars(
189  HdRprim *prim,
190  HdRprimSharedData *sharedData,
191  HdSceneDelegate *delegate,
192  HdRenderParam *renderParam,
193  HdDrawItem *drawItem,
194  HdDirtyBits *dirtyBits,
195  HdPrimvarDescriptorVector const& constantPrimvars,
196  bool *hasMirroredTransform = nullptr);
197 
198 // -----------------------------------------------------------------------------
199 // Instancer processing utilities
200 // -----------------------------------------------------------------------------
201 
202 // Updates drawItem bindings for changes to instance topology/primvars.
203 HDST_API
204 void HdStUpdateInstancerData(
205  HdRenderIndex &renderIndex,
206  HdRenderParam *renderParam,
207  HdRprim *prim,
208  HdStDrawItem *drawItem,
209  HdRprimSharedData *sharedData,
210  HdDirtyBits rprimDirtyBits);
211 
212 // Returns true if primvar with primvarName exists among instance primvar
213 // descriptors.
214 HDST_API
215 bool HdStIsInstancePrimvarExistentAndValid(
216  HdRenderIndex &renderIndex,
217  HdRprim *prim,
218  TfToken const& primvarName);
219 
220 // -----------------------------------------------------------------------------
221 // Topological visibility processing utility
222 // -----------------------------------------------------------------------------
223 // Creates/Updates/Migrates the topology visiblity BAR with element and point
224 // visibility encoded using one bit per element/point of the topology.
225 HDST_API
226 void HdStProcessTopologyVisibility(
227  VtIntArray invisibleElements,
228  int numTotalElements,
229  VtIntArray invisiblePoints,
230  int numTotalPoints,
231  HdRprimSharedData *sharedData,
232  HdStDrawItem *drawItem,
233  HdRenderParam *renderParam,
234  HdChangeTracker *changeTracker,
235  HdStResourceRegistrySharedPtr const &resourceRegistry,
236  SdfPath const& rprimId);
237 
238 //
239 // De-duplicating and sharing immutable primvar data.
240 //
241 // Primvar data is identified using a hash computed from the
242 // sources of the primvar data, of which there are generally
243 // two kinds:
244 // - data provided by the scene delegate
245 // - data produced by computations
246 //
247 // Immutable and mutable buffer data is managed using distinct
248 // heaps in the resource registry. Aggregation of buffer array
249 // ranges within each heap is managed separately.
250 //
251 // We attempt to balance the benefits of sharing vs efficient
252 // varying update using the following simple strategy:
253 //
254 // - When populating the first repr for an rprim, allocate
255 // the primvar range from the immutable heap and attempt
256 // to deduplicate the data by looking up the primvarId
257 // in the primvar instance registry.
258 //
259 // - When populating an additional repr for an rprim using
260 // an existing immutable primvar range, compute an updated
261 // primvarId and allocate from the immutable heap, again
262 // attempting to deduplicate.
263 //
264 // - Otherwise, migrate the primvar data to the mutable heap
265 // and abandon further attempts to deduplicate.
266 //
267 // - The computation of the primvarId for an rprim is cumulative
268 // and includes the new sources of data being committed
269 // during each successive update.
270 //
271 // - Once we have migrated a primvar allocation to the mutable
272 // heap we will no longer spend time computing a primvarId.
273 //
274 
275 HDST_API
276 bool HdStIsEnabledSharedVertexPrimvar();
277 
278 HDST_API
279 uint64_t HdStComputeSharedPrimvarId(
280  uint64_t baseId,
281  HdBufferSourceSharedPtrVector const &sources,
282  HdStComputationSharedPtrVector const &computations);
283 
284 HDST_API
285 void HdStGetBufferSpecsFromCompuations(
286  HdStComputationSharedPtrVector const& computations,
287  HdBufferSpecVector *bufferSpecs);
288 
289 PXR_NAMESPACE_CLOSE_SCOPE
290 
291 #endif // PXR_IMAGING_HD_ST_PRIM_UTILS_H
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:116
Tracks changes from the HdSceneDelegate, providing invalidation cues to the render engine...
Definition: changeTracker.h:50
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
The render engine state for a given rprim from the scene graph.
Definition: rprim.h:68
Adapter class providing data exchange with the client scene graph.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:442
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
HdSt implements instancing by drawing each proto multiple times with a single draw call...
Definition: instancer.h:68
A draw item is a light-weight representation of an HdRprim&#39;s resources and material to be used for re...
Definition: drawItem.h:66