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
74 template <typename ValueType>
75 static void RegisterType(const ValueType &fallback) {
76 static_assert(
78 "VtArray is not a supported execution value type.");
79 static_assert(
80 VdfIsEqualityComparable<ValueType>,
81 "Equality comparison is required for execution value types.");
82 _GetInstanceForRegistration()._RegisterType(fallback);
83 }
84
93 template <typename ValueType>
95 return VdfExecutionTypeRegistry::CheckForRegistration<ValueType>();
96 }
97
99 EXEC_API
100 VdfVector CreateVector(const VtValue &value) const;
101
109 EXEC_API
110 Exec_ValueExtractor GetExtractor(TfType type) const;
111
112private:
113 // Only TfSingleton can create instances.
114 friend class TfSingleton<ExecTypeRegistry>;
115
116 // Provides access for registraion of types only.
117 EXEC_API
118 static ExecTypeRegistry& _GetInstanceForRegistration();
119
121
122 template <typename ValueType>
123 void _RegisterType(ValueType const &fallback);
124
125 template <typename T>
126 struct _CreateVector {
127 // Interface for VdfTypeDispatchTable.
128 static VdfVector Call(const VtValue &value) {
129 return Create(value.UncheckedGet<T>());
130 }
131 // Typed implementation of CreateVector.
132 //
133 // This is separate from Call so that it can be shared with the
134 // Vt known type optimization in CreateVector.
135 static VdfVector Create(const T &value);
136 };
137
138 // Returns the appropriate value extractor for T.
139 //
140 // When T is a VtArray type, the returned extractor expects a VdfVector
141 // holding T::value_type elements as its input.
142 //
143 template <typename T>
144 static auto _MakeExtractorFunction();
145
146 // Specify that values of \p type should be extracted using \p function.
147 EXEC_API
148 void _RegisterExtractor(
149 TfType type,
150 Exec_ValueExtractorFunction &extractor);
151
152private:
153
155
156 // Type-erased conversions from VdfVector to VtValue.
157 //
158 // Inside of execution, there is no distinction between a scalar value and
159 // an array value of length 1. However, systems that interact with
160 // execution may desire single values be returned directly in VtValue or
161 // as a VtValue holding a VtArray depending on the context. The type key
162 // specifies the type held in the resulting VtValue. There are separate
163 // extractors for T and VtArray<T> but they both accepts VdfVectors
164 // holding T.
165 //
166 // Note that this must support the possibility that one thread is querying
167 // extractors at the same time that another thread is registering
168 // additional types.
169 //
170 tbb::concurrent_unordered_map<TfType, Exec_ValueExtractor, TfHash>
171 _extractors;
172};
173
174template <typename ValueType>
175void
176ExecTypeRegistry::_RegisterType(ValueType const &fallback)
177{
178 const TfType type = VdfExecutionTypeRegistry::Define(fallback);
179
180 // CreateVector has internal handling for value types known to Vt so we do
181 // not need to register them here.
182 if constexpr (!VtIsKnownValueType<ValueType>()) {
183 _createVector.RegisterType<ValueType>();
184 }
185
186 _RegisterExtractor(type, *+_MakeExtractorFunction<ValueType>());
187}
188
189template <typename T>
191ExecTypeRegistry::_CreateVector<T>::Create(const T &value)
192{
193 if constexpr (!VtIsArray<T>::value) {
195 v.Set(value);
196 return v;
197 }
198 else {
199 using ElementType = typename T::value_type;
200
201 const size_t size = value.size();
202
203 Vdf_BoxedContainer<ElementType> execValue(size);
204 std::copy_n(value.cdata(), size, execValue.data());
205
207 v.Set(std::move(execValue));
208 return v;
209 }
210}
211
212template <typename T>
213auto
214ExecTypeRegistry::_MakeExtractorFunction()
215{
216 if constexpr (!VtIsArray<T>::value) {
217 return [](const VdfVector &v, const VdfMask::Bits &mask) {
218 const VdfVector::ReadAccessor access =
219 v.GetReadAccessor<T>();
220
221 if (access.IsEmpty()) {
222 TF_VERIFY(mask.GetNumSet() == 0);
223 return VtValue();
224 }
225
226 if (!TF_VERIFY(mask.GetNumSet() == 1)) {
227 return VtValue();
228 }
229
230 const int offset = mask.GetFirstSet();
231 return VtValue(access[offset]);
232 };
233 }
234 else {
235 return [](const VdfVector &v, const VdfMask::Bits &mask) {
236 using ElementType = typename T::value_type;
237
238 if (!TF_VERIFY(mask.AreContiguouslySet())) {
239 return VtValue();
240 }
241
242 const VdfVector::ReadAccessor access =
243 v.GetReadAccessor<ElementType>();
244
245 const int offset = mask.GetFirstSet();
246 const size_t numValues = access.IsBoxed()
247 ? access.GetNumValues()
248 : mask.GetNumSet();
249 return VtValue(v.ExtractAsVtArray<ElementType>(numValues, offset));
250 };
251 }
252}
253
254PXR_NAMESPACE_CLOSE_SCOPE
255
256#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:94
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:75
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:469
bool IsBoxed() const
Returns true if this accessor is providing element-wise access into a boxed container.
Definition: vector.h:487
bool IsEmpty() const
Returns true if the vector is empty.
Definition: vector.h:478
size_t GetNumValues() const
Returns the size of the vector, i.e.
Definition: vector.h:482
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:370
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:514
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