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