1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 // This file is generated by a script.  Do not edit directly.  Edit the
26 // schema.template.cpp file to make changes.
27 
28 #include "pxr/imaging/hd/primvarSchema.h"
29 #include "pxr/imaging/hd/retainedDataSource.h"
30 
31 #include "pxr/base/trace/trace.h"
32 
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
36 TF_DEFINE_PUBLIC_TOKENS(HdPrimvarSchemaTokens,
37     HDPRIMVAR_SCHEMA_TOKENS);
38 
39 
40 
41 bool
IsIndexed()42 HdPrimvarSchema::IsIndexed()
43 {
44     if (_container) {
45         return (_container->Has(HdPrimvarSchemaTokens->
46                     indexedPrimvarValue) &&
47                 _container->Has(HdPrimvarSchemaTokens->indices));
48     }
49     return false;
50 }
51 
52 namespace {
53 
54 template<typename T>
55 VtValue
_ComputeFlattened(VtValue const & value,VtIntArray const & indices)56 _ComputeFlattened(VtValue const &value, VtIntArray const &indices) {
57     VtArray<T> array = value.Get<VtArray<T>>();
58     VtArray<T> result = VtArray<T>(indices.size());
59 
60     bool invalidIndices = false;
61     for (size_t i = 0; i < indices.size(); ++i) {
62         int index = indices[i];
63         if (index >= 0 && (size_t)index < array.size()) {
64             result[i] = array[index];
65         } else {
66             result[i] = T();
67             invalidIndices = true;
68         }
69     }
70     if (invalidIndices) {
71         TF_WARN("Invalid primvar indices");
72     }
73 
74     return VtValue(result);
75 }
76 
77 VtValue
_ComputeFlattenedValue(VtValue const & value,VtIntArray const & indices)78 _ComputeFlattenedValue(VtValue const &value, VtIntArray const &indices) {
79     if (value.IsHolding<VtVec2fArray>()) {
80         return _ComputeFlattened<GfVec2f>(value, indices);
81     } else if (value.IsHolding<VtVec2dArray>()) {
82         return _ComputeFlattened<GfVec2d>(value, indices);
83     } else if (value.IsHolding<VtVec2iArray>()) {
84         return _ComputeFlattened<GfVec2i>(value, indices);
85     } else if (value.IsHolding<VtVec2hArray>()) {
86         return _ComputeFlattened<GfVec2h>(value, indices);
87     } else if (value.IsHolding<VtVec3fArray>()) {
88         return _ComputeFlattened<GfVec3f>(value, indices);
89     } else if (value.IsHolding<VtVec3dArray>()) {
90         return _ComputeFlattened<GfVec3d>(value, indices);
91      } else if (value.IsHolding<VtVec3iArray>()) {
92         return _ComputeFlattened<GfVec3i>(value, indices);
93     } else if (value.IsHolding<VtVec3hArray>()) {
94         return _ComputeFlattened<GfVec3h>(value, indices);
95     } else if (value.IsHolding<VtVec4fArray>()) {
96         return _ComputeFlattened<GfVec4f>(value, indices);
97     } else if (value.IsHolding<VtVec4dArray>()) {
98         return _ComputeFlattened<GfVec4d>(value, indices);
99     } else if (value.IsHolding<VtVec4iArray>()) {
100         return _ComputeFlattened<GfVec4i>(value, indices);
101     } else if (value.IsHolding<VtVec4hArray>()) {
102         return _ComputeFlattened<GfVec4h>(value, indices);
103     } else if (value.IsHolding<VtMatrix4dArray>()) {
104         return _ComputeFlattened<GfMatrix4d>(value, indices);
105     } else if (value.IsHolding<VtStringArray>()) {
106         return _ComputeFlattened<std::string>(value, indices);
107     } else if (value.IsHolding<VtDoubleArray>()) {
108         return _ComputeFlattened<double>(value, indices);
109     } else if (value.IsHolding<VtIntArray>()) {
110         return _ComputeFlattened<int>(value, indices);
111     } else if (value.IsHolding<VtUIntArray>()) {
112         return _ComputeFlattened<unsigned int>(value, indices);
113     } else if (value.IsHolding<VtFloatArray>()) {
114         return _ComputeFlattened<float>(value, indices);
115     } else if (value.IsHolding<VtHalfArray>()) {
116         return _ComputeFlattened<GfHalf>(value, indices);
117     } else {
118         TF_WARN("Unsupported indexed primvar type");
119     }
120     return value;
121 }
122 
123 class _HdDataSourceFlattenedPrimvarValue : public HdSampledDataSource
124 {
125 public:
126     HD_DECLARE_DATASOURCE(_HdDataSourceFlattenedPrimvarValue);
127 
_HdDataSourceFlattenedPrimvarValue(HdSampledDataSourceHandle indexedValue,HdIntArrayDataSourceHandle indices)128     _HdDataSourceFlattenedPrimvarValue(
129         HdSampledDataSourceHandle indexedValue,
130         HdIntArrayDataSourceHandle indices)
131     : _indexedValue(indexedValue)
132     , _indices(indices)
133     {
134     }
135 
GetValue(Time shutterOffset)136     VtValue GetValue(Time shutterOffset) override
137     {
138         VtValue indexedValue = _indexedValue->GetValue(shutterOffset);
139         VtIntArray indices = _indices->GetTypedValue(shutterOffset);
140         return _ComputeFlattenedValue(indexedValue, indices);
141     }
142 
GetContributingSampleTimesForInterval(Time startTime,Time endTime,std::vector<Time> * outSampleTimes)143     bool GetContributingSampleTimesForInterval(
144             Time startTime, Time endTime,
145             std::vector<Time> * outSampleTimes) override
146     {
147         return _indexedValue->GetContributingSampleTimesForInterval(
148             startTime, endTime, outSampleTimes);
149     }
150 
151 private:
152     HdSampledDataSourceHandle _indexedValue;
153     HdIntArrayDataSourceHandle _indices;
154 };
155 
156 }
157 
158 
159 
160 HdSampledDataSourceHandle
GetPrimvarValue()161 HdPrimvarSchema::GetPrimvarValue()
162 {
163     // overriden definition from primvarSchemaGetValue.template.cpp
164     if (_container) {
165         if (_container->Has(HdPrimvarSchemaTokens->primvarValue)) {
166             return _GetTypedDataSource<HdSampledDataSource>(
167                 HdPrimvarSchemaTokens->primvarValue);
168         } else if (_container->Has(HdPrimvarSchemaTokens->indexedPrimvarValue) &&
169                    _container->Has(HdPrimvarSchemaTokens->indices)) {
170             return _HdDataSourceFlattenedPrimvarValue::New(
171                 _GetTypedDataSource<HdSampledDataSource>(
172                     HdPrimvarSchemaTokens->indexedPrimvarValue),
173                 _GetTypedDataSource<HdTypedSampledDataSource<VtIntArray>>(
174                     HdPrimvarSchemaTokens->indices));
175         }
176     }
177     return nullptr;
178 }
179 
180 HdSampledDataSourceHandle
GetIndexedPrimvarValue()181 HdPrimvarSchema::GetIndexedPrimvarValue()
182 {
183     // overriden definition from primvarSchemaGetIndexedValue.template.cpp
184     if (IsIndexed()) {
185         return _GetTypedDataSource<HdSampledDataSource>(
186                 HdPrimvarSchemaTokens->indexedPrimvarValue);
187     } else {
188         return _GetTypedDataSource<HdSampledDataSource>(
189                 HdPrimvarSchemaTokens->primvarValue);
190     }
191 }
192 
193 HdIntArrayDataSourceHandle
GetIndices()194 HdPrimvarSchema::GetIndices()
195 {
196     return _GetTypedDataSource<HdIntArrayDataSource>(
197         HdPrimvarSchemaTokens->indices);
198 }
199 
200 HdTokenDataSourceHandle
GetInterpolation()201 HdPrimvarSchema::GetInterpolation()
202 {
203     return _GetTypedDataSource<HdTokenDataSource>(
204         HdPrimvarSchemaTokens->interpolation);
205 }
206 
207 HdTokenDataSourceHandle
GetRole()208 HdPrimvarSchema::GetRole()
209 {
210     return _GetTypedDataSource<HdTokenDataSource>(
211         HdPrimvarSchemaTokens->role);
212 }
213 
214 /*static*/
215 HdContainerDataSourceHandle
BuildRetained(const HdSampledDataSourceHandle & primvarValue,const HdSampledDataSourceHandle & indexedPrimvarValue,const HdIntArrayDataSourceHandle & indices,const HdTokenDataSourceHandle & interpolation,const HdTokenDataSourceHandle & role)216 HdPrimvarSchema::BuildRetained(
217         const HdSampledDataSourceHandle &primvarValue,
218         const HdSampledDataSourceHandle &indexedPrimvarValue,
219         const HdIntArrayDataSourceHandle &indices,
220         const HdTokenDataSourceHandle &interpolation,
221         const HdTokenDataSourceHandle &role
222 )
223 {
224     TfToken names[5];
225     HdDataSourceBaseHandle values[5];
226 
227     size_t count = 0;
228     if (primvarValue) {
229         names[count] = HdPrimvarSchemaTokens->primvarValue;
230         values[count++] = primvarValue;
231     }
232 
233     if (indexedPrimvarValue) {
234         names[count] = HdPrimvarSchemaTokens->indexedPrimvarValue;
235         values[count++] = indexedPrimvarValue;
236     }
237 
238     if (indices) {
239         names[count] = HdPrimvarSchemaTokens->indices;
240         values[count++] = indices;
241     }
242 
243     if (interpolation) {
244         names[count] = HdPrimvarSchemaTokens->interpolation;
245         values[count++] = interpolation;
246     }
247 
248     if (role) {
249         names[count] = HdPrimvarSchemaTokens->role;
250         values[count++] = role;
251     }
252 
253     return HdRetainedContainerDataSource::New(count, names, values);
254 }
255 
256 
257 
258 /*static*/
259 HdTokenDataSourceHandle
BuildInterpolationDataSource(const TfToken & interpolation)260 HdPrimvarSchema::BuildInterpolationDataSource(
261     const TfToken &interpolation)
262 {
263     if (interpolation == HdPrimvarSchemaTokens->constant) {
264         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
265             HdRetainedTypedSampledDataSource<TfToken>::New(interpolation);
266         return ds;
267     }
268     if (interpolation == HdPrimvarSchemaTokens->uniform) {
269         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
270             HdRetainedTypedSampledDataSource<TfToken>::New(interpolation);
271         return ds;
272     }
273     if (interpolation == HdPrimvarSchemaTokens->varying) {
274         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
275             HdRetainedTypedSampledDataSource<TfToken>::New(interpolation);
276         return ds;
277     }
278     if (interpolation == HdPrimvarSchemaTokens->vertex) {
279         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
280             HdRetainedTypedSampledDataSource<TfToken>::New(interpolation);
281         return ds;
282     }
283     if (interpolation == HdPrimvarSchemaTokens->faceVarying) {
284         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
285             HdRetainedTypedSampledDataSource<TfToken>::New(interpolation);
286         return ds;
287     }
288     if (interpolation == HdPrimvarSchemaTokens->instance) {
289         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
290             HdRetainedTypedSampledDataSource<TfToken>::New(interpolation);
291         return ds;
292     }
293 
294     // fallback for unknown token
295     return HdRetainedTypedSampledDataSource<TfToken>::New(interpolation);
296 }
297 
298 
299 /*static*/
300 HdTokenDataSourceHandle
BuildRoleDataSource(const TfToken & role)301 HdPrimvarSchema::BuildRoleDataSource(
302     const TfToken &role)
303 {
304     if (role == HdPrimvarSchemaTokens->Point) {
305         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
306             HdRetainedTypedSampledDataSource<TfToken>::New(role);
307         return ds;
308     }
309     if (role == HdPrimvarSchemaTokens->Normal) {
310         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
311             HdRetainedTypedSampledDataSource<TfToken>::New(role);
312         return ds;
313     }
314     if (role == HdPrimvarSchemaTokens->Vector) {
315         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
316             HdRetainedTypedSampledDataSource<TfToken>::New(role);
317         return ds;
318     }
319     if (role == HdPrimvarSchemaTokens->Color) {
320         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
321             HdRetainedTypedSampledDataSource<TfToken>::New(role);
322         return ds;
323     }
324     if (role == HdPrimvarSchemaTokens->PointIndex) {
325         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
326             HdRetainedTypedSampledDataSource<TfToken>::New(role);
327         return ds;
328     }
329     if (role == HdPrimvarSchemaTokens->EdgeIndex) {
330         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
331             HdRetainedTypedSampledDataSource<TfToken>::New(role);
332         return ds;
333     }
334     if (role == HdPrimvarSchemaTokens->FaceIndex) {
335         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
336             HdRetainedTypedSampledDataSource<TfToken>::New(role);
337         return ds;
338     }
339     if (role == HdPrimvarSchemaTokens->TextureCoordinate) {
340         static const HdRetainedTypedSampledDataSource<TfToken>::Handle ds =
341             HdRetainedTypedSampledDataSource<TfToken>::New(role);
342         return ds;
343     }
344 
345     // fallback for unknown token
346     return HdRetainedTypedSampledDataSource<TfToken>::New(role);
347 }
348 
349 HdPrimvarSchema::Builder &
SetPrimvarValue(const HdSampledDataSourceHandle & primvarValue)350 HdPrimvarSchema::Builder::SetPrimvarValue(
351     const HdSampledDataSourceHandle &primvarValue)
352 {
353     _primvarValue = primvarValue;
354     return *this;
355 }
356 
357 HdPrimvarSchema::Builder &
SetIndexedPrimvarValue(const HdSampledDataSourceHandle & indexedPrimvarValue)358 HdPrimvarSchema::Builder::SetIndexedPrimvarValue(
359     const HdSampledDataSourceHandle &indexedPrimvarValue)
360 {
361     _indexedPrimvarValue = indexedPrimvarValue;
362     return *this;
363 }
364 
365 HdPrimvarSchema::Builder &
SetIndices(const HdIntArrayDataSourceHandle & indices)366 HdPrimvarSchema::Builder::SetIndices(
367     const HdIntArrayDataSourceHandle &indices)
368 {
369     _indices = indices;
370     return *this;
371 }
372 
373 HdPrimvarSchema::Builder &
SetInterpolation(const HdTokenDataSourceHandle & interpolation)374 HdPrimvarSchema::Builder::SetInterpolation(
375     const HdTokenDataSourceHandle &interpolation)
376 {
377     _interpolation = interpolation;
378     return *this;
379 }
380 
381 HdPrimvarSchema::Builder &
SetRole(const HdTokenDataSourceHandle & role)382 HdPrimvarSchema::Builder::SetRole(
383     const HdTokenDataSourceHandle &role)
384 {
385     _role = role;
386     return *this;
387 }
388 
389 HdContainerDataSourceHandle
Build()390 HdPrimvarSchema::Builder::Build()
391 {
392     return HdPrimvarSchema::BuildRetained(
393         _primvarValue,
394         _indexedPrimvarValue,
395         _indices,
396         _interpolation,
397         _role
398     );
399 }
400 
401 
402 PXR_NAMESPACE_CLOSE_SCOPE