Loading...
Searching...
No Matches
typeRegistry.h
Go to the documentation of this file.
1//
2// Copyright 2025 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_EXEC_EXEC_TYPE_REGISTRY_H
8#define PXR_EXEC_EXEC_TYPE_REGISTRY_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/exec/api.h"
15#include "pxr/exec/exec/valueExtractorFunction.h"
17#include "pxr/exec/vdf/mask.h"
19#include "pxr/exec/vdf/vector.h"
20
22#include "pxr/base/tf/type.h"
23#include "pxr/base/vt/array.h"
24#include "pxr/base/vt/traits.h"
25#include "pxr/base/vt/types.h"
26#include "pxr/base/vt/value.h"
27
28#include <tbb/concurrent_unordered_map.h>
29
30#include <algorithm>
31
32PXR_NAMESPACE_OPEN_SCOPE
33
34class Exec_ValueExtractor;
35class VdfMask;
36
46{
47public:
48 ExecTypeRegistry(ExecTypeRegistry const&) = delete;
49 ExecTypeRegistry& operator=(ExecTypeRegistry const&) = delete;
50
52
56 EXEC_API
58
100 template <typename ValueType>
101 static void RegisterType(const ValueType &fallback) {
102 static_assert(
104 "VtArray is not a supported execution value type.");
105 static_assert(
106 VdfIsEqualityComparable<ValueType>,
107 "Equality comparison is required for execution value types.");
108 _GetInstanceForRegistration()._RegisterType(fallback);
109 }
110
119 template <typename ValueType>
121 return VdfExecutionTypeRegistry::CheckForRegistration<ValueType>(
122 "Use ExecTypeRegistry::RegisterType<T>() to register execution "
123 "value types.");
124 }
125
127 EXEC_API
128 VdfVector CreateVector(const VtValue &value) const;
129
137 EXEC_API
138 Exec_ValueExtractor GetExtractor(TfType type) const;
139
140private:
141 // Only TfSingleton can create instances.
142 friend class TfSingleton<ExecTypeRegistry>;
143
144 // Provides access for registraion of types only.
145 EXEC_API
146 static ExecTypeRegistry& _GetInstanceForRegistration();
147
149
150 template <typename ValueType>
151 void _RegisterType(ValueType const &fallback);
152
153 template <typename T>
154 struct _CreateVector {
155 // Interface for VdfTypeDispatchTable.
156 static VdfVector Call(const VtValue &value) {
157 return Create(value.UncheckedGet<T>());
158 }
159 // Typed implementation of CreateVector.
160 //
161 // This is separate from Call so that it can be shared with the
162 // Vt known type optimization in CreateVector.
163 static VdfVector Create(const T &value);
164 };
165
166 // Returns the appropriate value extractor for T.
167 //
168 // When T is a VtArray type, the returned extractor expects a VdfVector
169 // holding T::value_type elements as its input.
170 //
171 template <typename T>
172 static auto _MakeExtractorFunction();
173
174 // Specify that values of \p type should be extracted using \p function.
175 EXEC_API
176 void _RegisterExtractor(
177 TfType type,
178 Exec_ValueExtractorFunction &extractor);
179
180private:
181
183
184 // Type-erased conversions from VdfVector to VtValue.
185 //
186 // Inside of execution, there is no distinction between a scalar value and
187 // an array value of length 1. However, systems that interact with
188 // execution may desire single values be returned directly in VtValue or
189 // as a VtValue holding a VtArray depending on the context. The type key
190 // specifies the type held in the resulting VtValue. There are separate
191 // extractors for T and VtArray<T> but they both accepts VdfVectors
192 // holding T.
193 //
194 // Note that this must support the possibility that one thread is querying
195 // extractors at the same time that another thread is registering
196 // additional types.
197 //
198 tbb::concurrent_unordered_map<TfType, Exec_ValueExtractor, TfHash>
199 _extractors;
200};
201
202template <typename ValueType>
203void
204ExecTypeRegistry::_RegisterType(ValueType const &fallback)
205{
206 const TfType type = VdfExecutionTypeRegistry::Define(fallback);
207
208 // CreateVector has internal handling for value types known to Vt so we do
209 // not need to register them here.
210 if constexpr (!VtIsKnownValueType<ValueType>()) {
211 _createVector.RegisterType<ValueType>();
212 }
213
214 _RegisterExtractor(type, *+_MakeExtractorFunction<ValueType>());
215}
216
217template <typename T>
219ExecTypeRegistry::_CreateVector<T>::Create(const T &value)
220{
221 if constexpr (!VtIsArray<T>::value) {
223 v.Set(value);
224 return v;
225 }
226 else {
227 using ElementType = typename T::value_type;
228
229 const size_t size = value.size();
230
231 Vdf_BoxedContainer<ElementType> execValue(size);
232 std::copy_n(value.cdata(), size, execValue.data());
233
235 v.Set(std::move(execValue));
236 return v;
237 }
238}
239
240template <typename T>
241auto
242ExecTypeRegistry::_MakeExtractorFunction()
243{
244 if constexpr (!VtIsArray<T>::value) {
245 return [](const VdfVector &v, const VdfMask::Bits &mask) {
246 const VdfVector::ReadAccessor access =
247 v.GetReadAccessor<T>();
248
249 if (access.IsEmpty()) {
250 TF_VERIFY(mask.GetNumSet() == 0);
251 return VtValue();
252 }
253
254 if (!TF_VERIFY(mask.GetNumSet() == 1)) {
255 return VtValue();
256 }
257
258 const int offset = mask.GetFirstSet();
259 return VtValue(access[offset]);
260 };
261 }
262 else {
263 return [](const VdfVector &v, const VdfMask::Bits &mask) {
264 using ElementType = typename T::value_type;
265
266 if (!TF_VERIFY(mask.AreContiguouslySet())) {
267 return VtValue();
268 }
269
270 const VdfVector::ReadAccessor access =
271 v.GetReadAccessor<ElementType>();
272
273 const int offset = mask.GetFirstSet();
274 const size_t numValues = access.IsBoxed()
275 ? access.GetNumValues()
276 : mask.GetNumSet();
277 return VtValue(v.ExtractAsVtArray<ElementType>(numValues, offset));
278 };
279 }
280}
281
282PXR_NAMESPACE_CLOSE_SCOPE
283
284#endif
Defines all the types "TYPED" for which Vt creates a VtTYPEDArray typedef.
Singleton used to register and access value types used by exec computations.
Definition: typeRegistry.h:46
EXEC_API VdfVector CreateVector(const VtValue &value) const
Construct a VdfVector whose value is copied from value.
TfType CheckForRegistration() const
Confirms that ValueType has been registered.
Definition: typeRegistry.h:120
static EXEC_API const ExecTypeRegistry & GetInstance()
Provides access to the singleton instance, first ensuring it is constructed.
EXEC_API Exec_ValueExtractor GetExtractor(TfType type) const
Returns an extractor that produces a VtValue from values held in execution.
static void RegisterType(const ValueType &fallback)
Registers ValueType as a value type that exec computations can use for input and output values,...
Definition: typeRegistry.h:101
Fast, compressed bit array which is capable of performing logical operations without first decompress...
Manage a single instance of an object (see.
Definition: singleton.h:107
TfType represents a dynamic runtime type.
Definition: type.h:48
This simple container stores multiple values that flow through the network as a single data flow elem...
static TfType Define(const T &fallback)
Registers T with execution's runtime type dispatch system.
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
Dispatches calls to template instantiations based on a TfType that is determined at runtime.
bool RegisterType()
Register an additional type with the type dispatch table.
A VdfTypedVector implements a VdfVector with a specific type.
Definition: typedVector.h:23
A read-only accessor for low-level acces to the contents of the VdfVector.
Definition: vector.h:476
bool IsBoxed() const
Returns true if this accessor is providing element-wise access into a boxed container.
Definition: vector.h:494
bool IsEmpty() const
Returns true if the vector is empty.
Definition: vector.h:485
size_t GetNumValues() const
Returns the size of the vector, i.e.
Definition: vector.h:489
This class is used to abstract away knowledge of the cache data used for each node.
Definition: vector.h:56
void Set(TYPE &&data)
Forwards data into the vector.
Definition: vector.h:162
VtArray< T > ExtractAsVtArray(const size_t size, const int offset) const
Extracts this vector's values into a VtArray<T>.
Definition: vector.h:377
ReadAccessor< TYPE > GetReadAccessor() const
GetReadAccessor() allows low level read-only access to the content of of the VdfVector via the Vdf_Ve...
Definition: vector.h:521
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1046
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266
Manage a single instance of an object.
A trait to detect instantiations of VtArray, specialized in array.h.
Definition: traits.h:22