Loading...
Searching...
No Matches
generativeProceduralResolvingSceneIndex.h
1//
2// Copyright 2022 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_GP_GENERATIVE_PROCEDURAL_RESOLVING_SCENE_INDEX_H
8#define PXR_IMAGING_HD_GP_GENERATIVE_PROCEDURAL_RESOLVING_SCENE_INDEX_H
9
10#include "pxr/imaging/hdGp/generativeProcedural.h"
11#include "pxr/imaging/hd/filteringSceneIndex.h"
13
14#include <tbb/concurrent_unordered_map.h>
15#include <mutex>
16#include <unordered_map>
17#include <unordered_set>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
44
47{
48public:
49
50 static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(
51 const HdSceneIndexBaseRefPtr &inputScene) {
52 return TfCreateRefPtr(
54 }
55
56 static HdGpGenerativeProceduralResolvingSceneIndexRefPtr New(
57 const HdSceneIndexBaseRefPtr &inputScene,
58 const TfToken &targetPrimTypeName) {
59 return TfCreateRefPtr(
61 inputScene, targetPrimTypeName));
62 }
63
65
66 HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override;
67 SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override;
68
69protected:
70
72 const HdSceneIndexBaseRefPtr &inputScene);
73
75 const HdSceneIndexBaseRefPtr &inputScene,
76 const TfToken &targetPrimTypeName);
77
79
81 const HdSceneIndexBase &sender,
82 const HdSceneIndexObserver::AddedPrimEntries &entries) override;
83
84 void _PrimsRemoved(
85 const HdSceneIndexBase &sender,
86 const HdSceneIndexObserver::RemovedPrimEntries &entries) override;
87
88 void _PrimsDirtied(
89 const HdSceneIndexBase &sender,
90 const HdSceneIndexObserver::DirtiedPrimEntries &entries) override;
91
93
95 const TfToken &messageType,
96 const HdDataSourceBaseHandle &args) override;
97
98private:
99
100 static HdGpGenerativeProcedural *_ConstructProcedural(
101 const TfToken &typeName, const SdfPath &proceduralPrimPath);
102
103
104 // MEMBER TYPES ///////////////////////////////////////////////////////////
105
107
108 static void _CombinePathArrays(const _DensePathSet &s, SdfPathVector *v);
109
110 class _ProcEntry : public TfWeakBase
111 {
112 public:
113 enum State : unsigned char {
114 StateUncooked = 0,
115 StateDependenciesCooking,
116 StateDependenciesCooked,
117 StateCooking,
118 StateCooked,
119 };
120
121 using _PathSetMap =
123
124 std::atomic<State> state;
125 TfToken typeName;
126 std::shared_ptr<HdGpGenerativeProcedural> proc;
129 _PathSetMap childHierarchy;
130 std::mutex cookMutex;
131
132
133 _ProcEntry()
134 : state(StateUncooked)
135 {}
136
137 _ProcEntry(const _ProcEntry &rhs)
138 {
139 state.store(rhs.state.load());
140 proc = rhs.proc;
141 typeName = rhs.typeName;
142 childTypes = rhs.childTypes;
143 dependencies = rhs.dependencies;
144 childHierarchy = rhs.childHierarchy;
145 }
146 };
147
148 TF_DECLARE_WEAK_PTRS(_ProcEntry);
149
150 struct _GeneratedPrimEntry
151 {
152 _GeneratedPrimEntry()
153 : responsibleProc(nullptr)
154 {}
155
156 _GeneratedPrimEntry(_ProcEntry * p)
157 : responsibleProc(p)
158 {}
159
160 _GeneratedPrimEntry(const _GeneratedPrimEntry &rhs)
161 {
162 responsibleProc.store(rhs.responsibleProc.load());
163 }
164 std::atomic<_ProcEntry *> responsibleProc;
165 };
166
167 using _GeneratedPrimsMap = tbb::concurrent_unordered_map<
168 SdfPath, _GeneratedPrimEntry, SdfPath::Hash>;
169
170 using _ProcEntryMap =
171 std::unordered_map<SdfPath, _ProcEntry, TfHash>;
172
173 using _WeakProcEntryMap =
174 tbb::concurrent_unordered_map<SdfPath, _ProcEntryPtr, TfHash>;
175
176 using _PathSet = std::unordered_set<SdfPath, TfHash>;
177
178 using _DependencyMap =
179 std::unordered_map<SdfPath, _PathSet, SdfPath::Hash>;
180
181 struct _Notices
182 {
186 };
187
188 // MEMBER FUNCTIONS ///////////////////////////////////////////////////////
189
190 _ProcEntry * _UpdateProceduralDependencies(
191 const SdfPath &proceduralPrimPath) const;
192
193 _ProcEntry * _UpdateProcedural(
194 const SdfPath &proceduralPrimPath,
195 bool forceUpdate,
196 _Notices *outputNotices,
198 *dirtiedDependencies = nullptr
199 ) const;
200
201
202 void _UpdateProceduralResult(
203 _ProcEntry *procEntry,
204 const SdfPath &proceduralPrimPath,
206 _Notices *outputNotices) const;
207
208
209 void _RemoveProcedural(
210 const SdfPath &proceduralPrimPath,
211 _Notices *outputNotices=nullptr) const;
212
213 // XXX Does thread-unsafe deletion.
214 // Removes deleted entries from _generatedPrims.
215 // This is private for now but intended for future use by a discussed formal
216 // method on HdSceneIndexBase itself.
217 void _GarbageCollect();
218
219 // MEMBER VARIABLES ///////////////////////////////////////////////////////
220 // procedural prim path -> entry
221 mutable _ProcEntryMap _procedurals;
222
223 mutable _WeakProcEntryMap _activeSyncProcedurals;
224
225 // reverse mapping of dependency -> dependent roots
226 mutable _DependencyMap _dependencies;
227
228 mutable _GeneratedPrimsMap _generatedPrims;
229
230 // no shared mutex, shared/unique lock is the same
231 using _MapMutex = std::mutex;
232 using _MapLock = std::lock_guard<_MapMutex>;
233 mutable _MapMutex _dependenciesMutex;
234 mutable _MapMutex _proceduralsMutex;
235
236 TfToken _targetPrimTypeName;
237
238 bool _attemptAsync;
239};
240
241PXR_NAMESPACE_CLOSE_SCOPE
242
243#endif
HdGpGenerativeProcedural is the base class for procedurals which have full access to an input scene i...
HdGpGenerativeProceduralResolvingSceneIndex is a scene index which evaluates prims representing gener...
void _PrimsAdded(const HdSceneIndexBase &sender, const HdSceneIndexObserver::AddedPrimEntries &entries) override
SATISFYING HdSingleInputFilteringSceneIndexBase ///////////////////////.
HdSceneIndexPrim GetPrim(const SdfPath &primPath) const override
SATISFYING HdSceneIndexBase ///////////////////////////////////////////.
SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const override
Returns the paths of all scene index prims located immediately below primPath.
void _SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args) override
Implement in order to react directly to system messages sent from downstream.
Abstract interface to scene data.
Definition: sceneIndex.h:48
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:274
This is a space efficient container that mimics the TfHashMap API that uses a vector for storage when...
Definition: denseHashMap.h:41
This is a space efficient container that mimics the TfHashSet API that uses a vector for storage when...
Definition: denseHashSet.h:39
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:45
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:58
Small struct representing a 'prim' in the Hydra scene index.
Definition: sceneIndex.h:35