|
| 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 | +#include "pxr/pxr.h" |
| 8 | + |
| 9 | +#include "pxr/exec/exec/typeRegistry.h" |
| 10 | + |
| 11 | +#include "pxr/usd/sdf/timeCode.h" |
| 12 | +#include "pxr/usd/sdf/types.h" |
| 13 | + |
| 14 | +#include "pxr/base/gf/vec3d.h" |
| 15 | +#include "pxr/base/tf/diagnostic.h" |
| 16 | +#include "pxr/base/tf/stringUtils.h" |
| 17 | +#include "pxr/base/vt/types.h" |
| 18 | + |
| 19 | +PXR_NAMESPACE_USING_DIRECTIVE |
| 20 | + |
| 21 | +#define ASSERT_EQ(expr, expected) \ |
| 22 | + [&] { \ |
| 23 | + auto&& expr_ = expr; \ |
| 24 | + if (expr_ != expected) { \ |
| 25 | + TF_FATAL_ERROR( \ |
| 26 | + "Expected " TF_PP_STRINGIZE(expr) " == '%s'; got '%s'", \ |
| 27 | + TfStringify(expected).c_str(), \ |
| 28 | + TfStringify(expr_).c_str()); \ |
| 29 | + } \ |
| 30 | + }() |
| 31 | + |
| 32 | +// A type that supports the minimum requirements to be an input value from |
| 33 | +// external clients, a computation result type and a result value returned to |
| 34 | +// external clients. |
| 35 | +struct TestExecTypeRegistrationValue |
| 36 | +{ |
| 37 | + bool operator==(const TestExecTypeRegistrationValue &) const { |
| 38 | + return true; |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +static void |
| 43 | +TestBasicRegistration() |
| 44 | +{ |
| 45 | + auto ® = ExecTypeRegistry::GetInstance(); |
| 46 | + |
| 47 | + reg.RegisterType(TestExecTypeRegistrationValue{}); |
| 48 | + reg.CheckForRegistration<TestExecTypeRegistrationValue>(); |
| 49 | + |
| 50 | + reg.RegisterType(VtArray<TestExecTypeRegistrationValue>{}); |
| 51 | + reg.CheckForRegistration<VtArray<TestExecTypeRegistrationValue>>(); |
| 52 | +} |
| 53 | + |
| 54 | +static void |
| 55 | +TestCreateVector() |
| 56 | +{ |
| 57 | + auto ® = ExecTypeRegistry::GetInstance(); |
| 58 | + |
| 59 | + // This tests conversion of VtValue to VdfVector with a variety of types. |
| 60 | + // The are two special categories of types: |
| 61 | + // |
| 62 | + // 1. Vt known value types, for which VtValue has optimizations |
| 63 | + // related to type checking. |
| 64 | + // 2. Sdf value types, which comprise the types of attribute and |
| 65 | + // metadata in Usd. |
| 66 | + // |
| 67 | + // The following test cases include types that cover all combinations of |
| 68 | + // these categories, including a type that does not belong to either. |
| 69 | + // Additionally, for VtArray<T> types, test that CreateVector yields a |
| 70 | + // vectorized VdfVector of T rather than a VdfVector holding a single |
| 71 | + // VtArray<T>. |
| 72 | + |
| 73 | + // GfVec3d is both a Vt known value type and an Sdf value type. |
| 74 | + { |
| 75 | + static_assert(VtIsKnownValueType<GfVec3d>()); |
| 76 | + static_assert(SdfValueTypeTraits<GfVec3d>::IsValueType); |
| 77 | + GfVec3d point(1, 2, 3); |
| 78 | + const VdfVector vec = reg.CreateVector(VtValue(point)); |
| 79 | + TF_AXIOM(vec.Holds<GfVec3d>()); |
| 80 | + const auto accessor = vec.GetReadAccessor<GfVec3d>(); |
| 81 | + ASSERT_EQ(accessor.GetNumValues(), 1); |
| 82 | + ASSERT_EQ(accessor[0], point); |
| 83 | + } |
| 84 | + |
| 85 | + // VtArray<GfVec3d> is both a Vt known value type and an Sdf value type. |
| 86 | + { |
| 87 | + static_assert(VtIsKnownValueType<VtArray<GfVec3d>>()); |
| 88 | + static_assert(SdfValueTypeTraits<VtArray<GfVec3d>>::IsValueType); |
| 89 | + const VtArray<GfVec3d> points = { |
| 90 | + { 0., 0., 0. }, |
| 91 | + { 1., 0., 0. }, |
| 92 | + { 2., 0., 0. }, |
| 93 | + { 3., 0., 0. }, |
| 94 | + { 4., 0., 0. }, |
| 95 | + { 5., 0., 0. }, |
| 96 | + }; |
| 97 | + const VdfVector vec = reg.CreateVector(VtValue(points)); |
| 98 | + TF_AXIOM(vec.Holds<GfVec3d>()); |
| 99 | + const auto accessor = vec.GetReadAccessor<GfVec3d>(); |
| 100 | + ASSERT_EQ(accessor.GetNumValues(), 6); |
| 101 | + ASSERT_EQ(accessor[0], points[0]); |
| 102 | + ASSERT_EQ(accessor[1], points[1]); |
| 103 | + ASSERT_EQ(accessor[2], points[2]); |
| 104 | + ASSERT_EQ(accessor[3], points[3]); |
| 105 | + ASSERT_EQ(accessor[4], points[4]); |
| 106 | + ASSERT_EQ(accessor[5], points[5]); |
| 107 | + } |
| 108 | + |
| 109 | + // short is known to Vt but is not an Sdf value type. |
| 110 | + { |
| 111 | + static_assert(VtIsKnownValueType<short>()); |
| 112 | + static_assert(!SdfValueTypeTraits<short>::IsValueType); |
| 113 | + const short x = 3; |
| 114 | + const VdfVector vec = reg.CreateVector(VtValue(x)); |
| 115 | + TF_AXIOM(vec.Holds<short>()); |
| 116 | + const auto accessor = vec.GetReadAccessor<short>(); |
| 117 | + ASSERT_EQ(accessor.GetNumValues(), 1); |
| 118 | + ASSERT_EQ(accessor[0], x); |
| 119 | + } |
| 120 | + |
| 121 | + // VtArray<short> is known to Vt but is not an Sdf value type. |
| 122 | + { |
| 123 | + static_assert(VtIsKnownValueType<VtArray<short>>()); |
| 124 | + static_assert(!SdfValueTypeTraits<VtArray<short>>::IsValueType); |
| 125 | + const VtArray<short> arr = { 0, 1 }; |
| 126 | + const VdfVector vec = reg.CreateVector(VtValue(arr)); |
| 127 | + TF_AXIOM(vec.Holds<short>()); |
| 128 | + const auto accessor = vec.GetReadAccessor<short>(); |
| 129 | + ASSERT_EQ(accessor.GetNumValues(), 2); |
| 130 | + ASSERT_EQ(accessor[0], arr[0]); |
| 131 | + ASSERT_EQ(accessor[1], arr[1]); |
| 132 | + } |
| 133 | + |
| 134 | + // SdfTimeCode is not known to Vt but is an Sdf value type. |
| 135 | + { |
| 136 | + static_assert(!VtIsKnownValueType<SdfTimeCode>()); |
| 137 | + static_assert(SdfValueTypeTraits<SdfTimeCode>::IsValueType); |
| 138 | + const SdfTimeCode t = 1.0; |
| 139 | + const VdfVector vec = reg.CreateVector(VtValue(t)); |
| 140 | + TF_AXIOM(vec.Holds<SdfTimeCode>()); |
| 141 | + const auto accessor = vec.GetReadAccessor<SdfTimeCode>(); |
| 142 | + ASSERT_EQ(accessor.GetNumValues(), 1); |
| 143 | + ASSERT_EQ(accessor[0], t); |
| 144 | + } |
| 145 | + |
| 146 | + // VtArray<SdfTimeCode> is not known to Vt but is an Sdf value type. |
| 147 | + { |
| 148 | + static_assert(!VtIsKnownValueType<VtArray<SdfTimeCode>>()); |
| 149 | + static_assert(SdfValueTypeTraits<VtArray<SdfTimeCode>>::IsValueType); |
| 150 | + const VtArray<SdfTimeCode> ts = { 0., 1. }; |
| 151 | + const VdfVector vec = reg.CreateVector(VtValue(ts)); |
| 152 | + TF_AXIOM(vec.Holds<SdfTimeCode>()); |
| 153 | + const auto accessor = vec.GetReadAccessor<SdfTimeCode>(); |
| 154 | + ASSERT_EQ(accessor.GetNumValues(), 2); |
| 155 | + ASSERT_EQ(accessor[0], ts[0]); |
| 156 | + ASSERT_EQ(accessor[1], ts[1]); |
| 157 | + } |
| 158 | + |
| 159 | + // TestExecTypeRegistrationValue is not known to Vt and is not an Sdf |
| 160 | + // value type. |
| 161 | + { |
| 162 | + static_assert(!VtIsKnownValueType<TestExecTypeRegistrationValue>()); |
| 163 | + static_assert(!SdfValueTypeTraits< |
| 164 | + TestExecTypeRegistrationValue>::IsValueType); |
| 165 | + TestExecTypeRegistrationValue val{}; |
| 166 | + const VdfVector vec = reg.CreateVector(VtValue(val)); |
| 167 | + TF_AXIOM(vec.Holds<TestExecTypeRegistrationValue>()); |
| 168 | + const auto accessor = vec.GetReadAccessor< |
| 169 | + TestExecTypeRegistrationValue>(); |
| 170 | + ASSERT_EQ(accessor.GetNumValues(), 1); |
| 171 | + // ASSERT_EQ is not used here because it requires that the type have a |
| 172 | + // TfStringify or ostream operator<< overload and this type should |
| 173 | + // define only what is necessary for VtValue & VdfVector. |
| 174 | + TF_AXIOM(accessor[0] == TestExecTypeRegistrationValue{}); |
| 175 | + } |
| 176 | + |
| 177 | + // VtArray<TestExecTypeRegistrationValue> is not known to Vt and is not an |
| 178 | + // Sdf value type. |
| 179 | + { |
| 180 | + static_assert(!VtIsKnownValueType< |
| 181 | + VtArray<TestExecTypeRegistrationValue>>()); |
| 182 | + static_assert(!SdfValueTypeTraits< |
| 183 | + VtArray<TestExecTypeRegistrationValue>>::IsValueType); |
| 184 | + const VtArray<TestExecTypeRegistrationValue> arr = { {}, {} }; |
| 185 | + const VdfVector vec = reg.CreateVector(VtValue(arr)); |
| 186 | + TF_AXIOM(vec.Holds<TestExecTypeRegistrationValue>()); |
| 187 | + const auto accessor = vec.GetReadAccessor< |
| 188 | + TestExecTypeRegistrationValue>(); |
| 189 | + ASSERT_EQ(accessor.GetNumValues(), 2); |
| 190 | + TF_AXIOM(accessor[0] == arr[0]); |
| 191 | + TF_AXIOM(accessor[1] == arr[1]); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +int |
| 196 | +main() |
| 197 | +{ |
| 198 | + TestBasicRegistration(); |
| 199 | + TestCreateVector(); |
| 200 | + |
| 201 | + return 0; |
| 202 | +} |
| 203 | + |
0 commit comments