Loading...
Searching...
No Matches
materialOverrideResolvingSceneIndex.h
1//
2// Copyright 2024 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_HDSI_MATERIAL_OVERRIDE_RESOLVING_SCENE_INDEX_H
8#define PXR_IMAGING_HDSI_MATERIAL_OVERRIDE_RESOLVING_SCENE_INDEX_H
9
11#include "pxr/imaging/hd/filteringSceneIndex.h"
14#include "pxr/imaging/hdsi/api.h"
15
16#include <optional>
17#include <unordered_map>
18#include <unordered_set>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
22using PathSet = std::unordered_set<SdfPath, SdfPath::Hash>;
23
24TF_DECLARE_WEAK_AND_REF_PTRS(HdsiMaterialOverrideResolvingSceneIndex);
25
26/*
27HdsiMaterialOverrideResolvingSceneIndex applies material overrides in the form
28of edits to a material's interface or directly to parameters of its shader nodes.
29
30Overrides to a material's interface are only applied if their interface mappings
31are found.
32The overrides for a particular 'publicUIName' are specified at path like this:
33
34materialOverride.interfaceValues.<publicUIName>.value
35 -> overrideValueDataSource
36
37The 'overrideValueDataSource' is copied over a network node parameter's original
38'valueDataSource' at paths like this:
39
40material.<renderContext>.nodes.<nodePath>.parameters.<inputName>.value
41 -> valueDataSource
42
43The scene index identifies which network node parameter to override by using
44the material's interface mappings which are defined at paths like this:
45
46material.<renderContext>.interface.parameters.<publicUIName>.mappings
47 -> [(nodePath, inputName), (nodePath, inputName), ...]
48
49Edits of input parameters of shaders within the material are specified at a path
50like this:
51materialOverride.parameterValues.<shaderNodeName>.<parameterName>.value ->
52 paramEditValueDataSource
53
54The 'paramEditValueDataSource' is copied over a network node parameter's original
55'valueDataSource' at paths like this:
56
57material.<renderContext>.nodes.<nodePath>.parameters.<inputName>.value
58 -> valueDataSource
59
60If the same input parameter is overridden both by an edit to the material
61interface and by a direct parameter edit, the interface override will take
62precedence.
63
64If a geometry prim contains materialOverride data sources, a copy of
65the material bound to that location will be generated, and the overrides will
66only be applied to the generated material.
67Any changes to the original material will be reflected in the generated material.
68Geometry prims that contain materialOverride data sources are only
69processed by this Scene Index if they contain a materialBindings data source
70with material binds for one of the following purposes: full, allPurpose.
71A minimum set of materials will be generated to satisfy material overrides: two
72geometry prims bound to the same material and containing the same set of
73material overrides will share the same generated material.
74
75Below is a diagram of the expected attributes needed for material interface
76and parameter edits on a scene index prim of type 'material':
77
78MaterialPrim
79|
80+------materialOverride
81| |
82| +----interfaceValues
83| | |
84| | +-publicUIName
85| | | |
86| | | +---value -> overrideValueDataSource
87| | |
88| | +-publicUIName
89| | | |
90| | | +---value -> overrideValueDataSource
91| | |
92| | +-...
93| |
94| +----parameterValues
95| |
96| +-nodePath
97| | |
98| | +---inputName
99| | | |
100| | | +---value -> paramEditValueDataSource
101| | |
102| | +---inputName
103| | | |
104| | | +---value -> paramEditValueDataSource
105| | |
106| | +---...
107| |
108| +-...
109|
110+------material
111 |
112 +----ri
113 |
114 +--nodes
115 | |
116 | +-nodePath
117 | | |
118 | | +-parameters
119 | | |
120 | | +-inputName
121 | | | |
122 | | | +-value -> valueDataSource
123 | | |
124 | | +-inputName
125 | | |
126 | | +-value -> valueDataSource
127 | |
128 | +-nodePath
129 | |
130 | +-parameters
131 | |
132 | +-...
133 |
134 +--interface
135 |
136 +-parameters
137 | |
138 | +-publicUIName
139 | | |
140 | | +-mappings
141 | | +-i0
142 | | | |
143 | | | +----nodePath
144 | | | |
145 | | | +----inputName
146 | | |
147 | | +-i1
148 | | | |
149 | | | +----nodePath
150 | | | |
151 | | | +----inputName
152 | | |
153 | | +-...
154 | |
155 | +-publicUIName
156 | | |
157 | | +-mappings
158 | | +-i0
159 | | | |
160 | | | +----nodePath
161 | | | |
162 | | | +----inputName
163 | | |
164 | | +-i1
165 | | | |
166 | | | +----nodePath
167 | | | |
168 | | | +----inputName
169 | | |
170 | | +-...
171 | +-...
172 |
173 +-parameterOrder = ...
174*/
175
176class HdsiMaterialOverrideResolvingSceneIndex final :
178{
179 // Forward Declarations
180 struct MaterialData;
181 struct PrimData;
182
183 // Type Definitions
184 using PrimDataMap = std::unordered_map<SdfPath, PrimData, SdfPath::Hash>;
185 using MaterialPathsMap = std::unordered_map<SdfPath, PathSet, SdfPath::Hash>;
186 using MaterialDataMap = std::unordered_map<SdfPath, MaterialData,
187 SdfPath::Hash>;
188 using HashToMaterialPathMap = std::unordered_map<uint64_t, SdfPath>;
189 using MaterialHashMap = std::unordered_map<SdfPath, HashToMaterialPathMap,
190 SdfPath::Hash>;
191
192public:
193 static HdsiMaterialOverrideResolvingSceneIndexRefPtr New(
194 const HdSceneIndexBaseRefPtr &inputScene)
195 {
196 return TfCreateRefPtr(
197 new HdsiMaterialOverrideResolvingSceneIndex(inputScene));
198 }
199
200 // HdSceneIndexBase overrides
201 HDSI_API
202 HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override;
203
204 HDSI_API
205 SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override;
206
207protected:
208
209 HDSI_API
210 HdsiMaterialOverrideResolvingSceneIndex(
211 const HdSceneIndexBaseRefPtr &inputScene);
212
213 // HdSingleInputFilteringSceneIndexBase overrides
214 void _PrimsAdded(
215 const HdSceneIndexBase &sender,
216 const HdSceneIndexObserver::AddedPrimEntries &entries) override;
217
218 void _PrimsRemoved(
219 const HdSceneIndexBase &sender,
220 const HdSceneIndexObserver::RemovedPrimEntries &entries) override;
221
222 void _PrimsDirtied(
223 const HdSceneIndexBase &sender,
224 const HdSceneIndexObserver::DirtiedPrimEntries &entries) override;
225
226private:
229 HdMaterialOverrideSchema _GetMaterialOverrides(
230 const SdfPath& primPath) const;
231
237 SdfPath _GetBoundMaterial(const SdfPath& primPath) const;
238
243 HdSceneIndexObserver::AddedPrimEntries _AddGeneratedMaterials(
245
251 SdfPath _AddGeneratedMaterial(
252 const TfToken& primType,
253 const SdfPath& primPath);
254
259 HdSceneIndexObserver::DirtiedPrimEntries _DirtyGeneratedMaterials(
261
271 void _DirtyBaseMaterial(
272 const SdfPath& primPath,
273 const PathSet& generatedMaterials,
274 PathSet* dirtiedPaths) const;
275
288 void _DirtyGeometry(
290 const HdSceneIndexBaseRefPtr inputScene,
291 const PrimData& primData,
292 PathSet* processedPrimsSet,
293 PathSet* addedPaths,
294 PathSet* dirtiedPaths,
295 PathSet* removedPaths);
296
304 void _DirtyMaterialOverrideLocator(
305 const SdfPath& primPath,
306 const HdSceneIndexBaseRefPtr inputScene,
307 PathSet* addedPaths,
308 PathSet* dirtiedPaths);
309
312 PathSet _GetGeneratedMaterials(const SdfPath& primPath) const;
313
316 bool _IsGeneratedMaterial(const SdfPath& primPath) const;
317
320 void _CreateGeneratedMaterialDataSource(
321 HdSceneIndexPrim& prim,
322 const SdfPath& primPath) const;
323
325 uint64_t _GetHash(const HdMaterialOverrideSchema& materialOverrides) const;
326
329 void _InvalidateMaps(const SdfPath& primPath);
330
333 struct MaterialData
334 {
335 // Path to the material bound to materialOverridePrimPath
336 // before any changes are made by this scene index
337 SdfPath originalMaterialPath;
338
339 // Set of geometry prims using this generated material
340 PathSet boundPrims;
341 };
342
345 struct PrimData
346 {
347 // Path to the generated material bound to this geometry prim
348 SdfPath generatedMaterialPath;
349
350 // Hash of the material overrides found on this prim
351 uint64_t materialOverrideHash;
352 };
353
354 // Map linking a path of a material scope to a set of materials that are
355 // generated by this scene index as a result of resolving material
356 // overrides at non-material locations.
357 MaterialPathsMap _scopeToNewMaterialPaths;
358
359 // Map linking a material to a set of materials generated from it.
360 MaterialPathsMap _oldToNewMaterialPaths;
361
362 // Map linking geometry prims to the path of the generated material that
363 // will be bound to them and to the hash for their material overrides.
364 // The keys in this map represent geometry prims that have
365 // a material binding and a material override data source.
366 PrimDataMap _primData;
367
368 // Map linking materials generated in the process of resolving material
369 // overrides to their data.
370 MaterialDataMap _materialData;
371
372 // Map relating a geometry prim's bound material (before this scene index
373 // executes) and the hash of its material override data source to the path
374 // of a generated material.
375 // Used to allow multiple geometry prims that share the same starting
376 // material and material overrides to use the same generated material.
377 // For example:
378 // PlasticMaterial --> ..0123 --> __MOR_PlasticMaterial_Mesh
379 // --> ..4567 --> __MOR_PlasticMaterial_AnotherMesh
380 // WoodMaterial --> ..0123 --> __MOR_WoodMaterial_Mesh
381 // MetalMaterial --> ..4567 --> __MOR_MetalMaterial_Point
382 MaterialHashMap _materialHashMap;
383};
384
385PXR_NAMESPACE_CLOSE_SCOPE
386
387#endif // PXR_IMAGING_HDSI_MATERIAL_OVERRIDE_RESOLVING_SCENE_INDEX_H
The MaterialOverride schema allows overrides to be made to various parts of materials,...
Abstract interface to scene data.
Definition: sceneIndex.h:54
virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const =0
Returns the paths of all scene index prims located immediately below primPath.
virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const =0
Returns a pair of (prim type, datasource).
An abstract base class for a filtering scene index that observes a single input scene index.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Standard pointer typedefs.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:72
A notice indicating a prim was invalidated.
Small struct representing a 'prim' in the Hydra scene index.
Definition: sceneIndex.h:35