1 //
2 // Copyright 2018 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 #include "pxr/pxr.h"
25 #include "pxr/imaging/hd/types.h"
26 #include "pxr/base/tf/registryManager.h"
27 #include "pxr/base/vt/array.h"
28 #include "pxr/base/vt/value.h"
29
30 #include "pxr/base/gf/matrix3d.h"
31 #include "pxr/base/gf/matrix3f.h"
32 #include "pxr/base/gf/matrix4d.h"
33 #include "pxr/base/gf/matrix4f.h"
34 #include "pxr/base/gf/vec2d.h"
35 #include "pxr/base/gf/vec2f.h"
36 #include "pxr/base/gf/vec2i.h"
37 #include "pxr/base/gf/vec2h.h"
38 #include "pxr/base/gf/vec3d.h"
39 #include "pxr/base/gf/vec3f.h"
40 #include "pxr/base/gf/vec3i.h"
41 #include "pxr/base/gf/vec3h.h"
42 #include "pxr/base/gf/vec4d.h"
43 #include "pxr/base/gf/vec4f.h"
44 #include "pxr/base/gf/vec4i.h"
45 #include "pxr/base/gf/vec4h.h"
46 #include "pxr/base/gf/quath.h"
47 #include "pxr/base/gf/quatf.h"
48 #include "pxr/base/gf/quatd.h"
49
50 #include <unordered_map>
51 #include <typeinfo>
52 #include <typeindex>
53
54 PXR_NAMESPACE_OPEN_SCOPE
55
TF_REGISTRY_FUNCTION(TfEnum)56 TF_REGISTRY_FUNCTION(TfEnum)
57 {
58 TF_ADD_ENUM_NAME(HdTypeInvalid);
59 TF_ADD_ENUM_NAME(HdTypeBool);
60 TF_ADD_ENUM_NAME(HdTypeUInt8);
61 TF_ADD_ENUM_NAME(HdTypeUInt16);
62 TF_ADD_ENUM_NAME(HdTypeInt8);
63 TF_ADD_ENUM_NAME(HdTypeInt16);
64 TF_ADD_ENUM_NAME(HdTypeInt32);
65 TF_ADD_ENUM_NAME(HdTypeInt32Vec2);
66 TF_ADD_ENUM_NAME(HdTypeInt32Vec3);
67 TF_ADD_ENUM_NAME(HdTypeInt32Vec4);
68 TF_ADD_ENUM_NAME(HdTypeUInt32);
69 TF_ADD_ENUM_NAME(HdTypeUInt32Vec2);
70 TF_ADD_ENUM_NAME(HdTypeUInt32Vec3);
71 TF_ADD_ENUM_NAME(HdTypeUInt32Vec4);
72 TF_ADD_ENUM_NAME(HdTypeFloat);
73 TF_ADD_ENUM_NAME(HdTypeFloatVec2);
74 TF_ADD_ENUM_NAME(HdTypeFloatVec3);
75 TF_ADD_ENUM_NAME(HdTypeFloatVec4);
76 TF_ADD_ENUM_NAME(HdTypeFloatMat3);
77 TF_ADD_ENUM_NAME(HdTypeFloatMat4);
78 TF_ADD_ENUM_NAME(HdTypeHalfFloat);
79 TF_ADD_ENUM_NAME(HdTypeHalfFloatVec2);
80 TF_ADD_ENUM_NAME(HdTypeHalfFloatVec3);
81 TF_ADD_ENUM_NAME(HdTypeHalfFloatVec4);
82 TF_ADD_ENUM_NAME(HdTypeDouble);
83 TF_ADD_ENUM_NAME(HdTypeDoubleVec2);
84 TF_ADD_ENUM_NAME(HdTypeDoubleVec3);
85 TF_ADD_ENUM_NAME(HdTypeDoubleVec4);
86 TF_ADD_ENUM_NAME(HdTypeDoubleMat3);
87 TF_ADD_ENUM_NAME(HdTypeDoubleMat4);
88 TF_ADD_ENUM_NAME(HdTypeInt32_2_10_10_10_REV);
89
90 TF_ADD_ENUM_NAME(HdFormatInvalid);
91 TF_ADD_ENUM_NAME(HdFormatUNorm8);
92 TF_ADD_ENUM_NAME(HdFormatUNorm8Vec2);
93 TF_ADD_ENUM_NAME(HdFormatUNorm8Vec3);
94 TF_ADD_ENUM_NAME(HdFormatUNorm8Vec4);
95 TF_ADD_ENUM_NAME(HdFormatSNorm8);
96 TF_ADD_ENUM_NAME(HdFormatSNorm8Vec2);
97 TF_ADD_ENUM_NAME(HdFormatSNorm8Vec3);
98 TF_ADD_ENUM_NAME(HdFormatSNorm8Vec4);
99 TF_ADD_ENUM_NAME(HdFormatFloat16);
100 TF_ADD_ENUM_NAME(HdFormatFloat16Vec2);
101 TF_ADD_ENUM_NAME(HdFormatFloat16Vec3);
102 TF_ADD_ENUM_NAME(HdFormatFloat16Vec4);
103 TF_ADD_ENUM_NAME(HdFormatFloat32);
104 TF_ADD_ENUM_NAME(HdFormatFloat32Vec2);
105 TF_ADD_ENUM_NAME(HdFormatFloat32Vec3);
106 TF_ADD_ENUM_NAME(HdFormatFloat32Vec4);
107 TF_ADD_ENUM_NAME(HdFormatInt16);
108 TF_ADD_ENUM_NAME(HdFormatInt16Vec2);
109 TF_ADD_ENUM_NAME(HdFormatInt16Vec3);
110 TF_ADD_ENUM_NAME(HdFormatInt16Vec4);
111 TF_ADD_ENUM_NAME(HdFormatUInt16);
112 TF_ADD_ENUM_NAME(HdFormatUInt16Vec2);
113 TF_ADD_ENUM_NAME(HdFormatUInt16Vec3);
114 TF_ADD_ENUM_NAME(HdFormatUInt16Vec4);
115 TF_ADD_ENUM_NAME(HdFormatInt32);
116 TF_ADD_ENUM_NAME(HdFormatInt32Vec2);
117 TF_ADD_ENUM_NAME(HdFormatInt32Vec3);
118 TF_ADD_ENUM_NAME(HdFormatInt32Vec4);
119 TF_ADD_ENUM_NAME(HdFormatFloat32UInt8);
120 }
121
122 bool
operator ==(const HdSamplerParameters & other) const123 HdSamplerParameters::operator==(const HdSamplerParameters &other) const
124 {
125 return
126 (wrapS == other.wrapS) &&
127 (wrapT == other.wrapT) &&
128 (wrapR == other.wrapR) &&
129 (minFilter == other.minFilter) &&
130 (magFilter == other.magFilter);
131 }
132
133 bool
operator !=(const HdSamplerParameters & other) const134 HdSamplerParameters::operator!=(const HdSamplerParameters &other) const
135 {
136 return !(*this == other);
137 }
138
139 template <class T>
_GetArrayData(VtValue const & v)140 static void const *_GetArrayData(VtValue const &v) {
141 return v.UncheckedGet<VtArray<T>>().cdata();
142 }
143 template <class T>
_GetSingleData(VtValue const & v)144 static void const *_GetSingleData(VtValue const &v) {
145 return &v.UncheckedGet<T>();
146 }
147 using GetDataFunc = void const *(*)(VtValue const &);
148 using ValueDataGetterMap = std::unordered_map<std::type_index, GetDataFunc>;
_MakeValueDataGetterMap()149 static inline ValueDataGetterMap _MakeValueDataGetterMap() {
150 #define ELEM(T) \
151 { typeid(T), &_GetSingleData<T> }, \
152 { typeid(VtArray<T>), &_GetArrayData<T> }
153
154 return ValueDataGetterMap {
155 ELEM(GfHalf),
156 ELEM(GfMatrix3d),
157 ELEM(GfMatrix3f),
158 ELEM(GfMatrix4d),
159 ELEM(GfMatrix4f),
160 ELEM(GfVec2d),
161 ELEM(GfVec2f),
162 ELEM(GfVec2h),
163 ELEM(GfVec2i),
164 ELEM(GfVec3d),
165 ELEM(GfVec3f),
166 ELEM(GfVec3h),
167 ELEM(GfVec3i),
168 ELEM(GfVec4d),
169 ELEM(GfVec4f),
170 ELEM(GfVec4h),
171 ELEM(GfVec4i),
172 ELEM(GfQuath),
173 ELEM(GfQuatf),
174 ELEM(GfQuatd),
175 ELEM(HdVec4f_2_10_10_10_REV),
176 ELEM(bool),
177 ELEM(char),
178 ELEM(double),
179 ELEM(float),
180 ELEM(int16_t),
181 ELEM(int32_t),
182 ELEM(uint16_t),
183 ELEM(uint32_t),
184 ELEM(unsigned char),
185 };
186 #undef ELEM
187 }
188
HdGetValueData(const VtValue & value)189 const void* HdGetValueData(const VtValue &value)
190 {
191 static ValueDataGetterMap const getterMap = _MakeValueDataGetterMap();
192 auto const iter = getterMap.find(value.GetTypeid());
193 if (ARCH_UNLIKELY(iter == getterMap.end())) {
194 return nullptr;
195 }
196 return iter->second(value);
197 }
198
199 using TupleTypeMap = std::unordered_map<std::type_index, HdType>;
_MakeTupleTypeMap()200 static inline TupleTypeMap _MakeTupleTypeMap() {
201 return TupleTypeMap {
202 { typeid(GfHalf), HdTypeHalfFloat },
203 { typeid(GfMatrix3d), HdTypeDoubleMat3 },
204 { typeid(GfMatrix3f), HdTypeFloatMat3 },
205 { typeid(GfMatrix4d), HdTypeDoubleMat4 },
206 { typeid(GfMatrix4f), HdTypeFloatMat4 },
207 { typeid(GfVec2d), HdTypeDoubleVec2 },
208 { typeid(GfVec2f), HdTypeFloatVec2 },
209 { typeid(GfVec2h), HdTypeHalfFloatVec2 },
210 { typeid(GfVec2i), HdTypeInt32Vec2 },
211 { typeid(GfVec3d), HdTypeDoubleVec3 },
212 { typeid(GfVec3f), HdTypeFloatVec3 },
213 { typeid(GfVec3h), HdTypeHalfFloatVec3 },
214 { typeid(GfVec3i), HdTypeInt32Vec3 },
215 { typeid(GfVec4d), HdTypeDoubleVec4 },
216 { typeid(GfVec4f), HdTypeFloatVec4 },
217 { typeid(GfVec4h), HdTypeHalfFloatVec4 },
218 { typeid(GfVec4i), HdTypeInt32Vec4 },
219 { typeid(GfQuath), HdTypeHalfFloatVec4 },
220 { typeid(GfQuatf), HdTypeFloatVec4 },
221 { typeid(GfQuatd), HdTypeDoubleVec4 },
222 { typeid(HdVec4f_2_10_10_10_REV), HdTypeInt32_2_10_10_10_REV },
223 { typeid(bool), HdTypeBool },
224 { typeid(char), HdTypeInt8 },
225 { typeid(double), HdTypeDouble },
226 { typeid(float), HdTypeFloat },
227 { typeid(int16_t), HdTypeInt16 },
228 { typeid(int32_t), HdTypeInt32 },
229 { typeid(uint16_t), HdTypeUInt16 },
230 { typeid(uint32_t), HdTypeUInt32 },
231 { typeid(unsigned char), HdTypeUInt8 },
232 };
233 }
234
HdGetValueTupleType(const VtValue & value)235 HdTupleType HdGetValueTupleType(const VtValue &value)
236 {
237 static const TupleTypeMap tupleTypeMap = _MakeTupleTypeMap();
238
239 if (value.IsArrayValued()) {
240 auto const iter = tupleTypeMap.find(value.GetElementTypeid());
241 if (ARCH_UNLIKELY(iter == tupleTypeMap.end())) {
242 return HdTupleType { HdTypeInvalid, 0 };
243 }
244 return HdTupleType { iter->second, value.GetArraySize() };
245 }
246 auto const iter = tupleTypeMap.find(value.GetTypeid());
247 if (ARCH_UNLIKELY(iter == tupleTypeMap.end())) {
248 return HdTupleType { HdTypeInvalid, 0 };
249 }
250 return HdTupleType { iter->second, 1 };
251 }
252
HdGetComponentType(HdType t)253 HdType HdGetComponentType(HdType t)
254 {
255 switch (t) {
256 case HdTypeUInt32Vec2:
257 case HdTypeUInt32Vec3:
258 case HdTypeUInt32Vec4:
259 return HdTypeUInt32;
260 case HdTypeInt32Vec2:
261 case HdTypeInt32Vec3:
262 case HdTypeInt32Vec4:
263 return HdTypeInt32;
264 case HdTypeFloatVec2:
265 case HdTypeFloatVec3:
266 case HdTypeFloatVec4:
267 case HdTypeFloatMat3:
268 case HdTypeFloatMat4:
269 return HdTypeFloat;
270 case HdTypeDoubleVec2:
271 case HdTypeDoubleVec3:
272 case HdTypeDoubleVec4:
273 case HdTypeDoubleMat3:
274 case HdTypeDoubleMat4:
275 return HdTypeDouble;
276 case HdTypeHalfFloatVec2:
277 case HdTypeHalfFloatVec3:
278 case HdTypeHalfFloatVec4:
279 return HdTypeHalfFloat;
280 default:
281 return t;
282 }
283 }
284
HdGetComponentCount(HdType t)285 size_t HdGetComponentCount(HdType t)
286 {
287 switch (t) {
288 case HdTypeInt32Vec2:
289 case HdTypeUInt32Vec2:
290 case HdTypeFloatVec2:
291 case HdTypeDoubleVec2:
292 case HdTypeHalfFloatVec2:
293 return 2;
294 case HdTypeInt32Vec3:
295 case HdTypeUInt32Vec3:
296 case HdTypeFloatVec3:
297 case HdTypeDoubleVec3:
298 case HdTypeHalfFloatVec3:
299 return 3;
300 case HdTypeInt32Vec4:
301 case HdTypeUInt32Vec4:
302 case HdTypeFloatVec4:
303 case HdTypeDoubleVec4:
304 case HdTypeHalfFloatVec4:
305 return 4;
306 case HdTypeFloatMat3:
307 case HdTypeDoubleMat3:
308 return 3*3;
309 case HdTypeFloatMat4:
310 case HdTypeDoubleMat4:
311 return 4*4;
312 default:
313 return 1;
314 }
315 }
316
HdDataSizeOfType(HdType t)317 size_t HdDataSizeOfType(HdType t)
318 {
319 switch (t) {
320 case HdTypeInvalid:
321 default:
322 TF_CODING_ERROR("Cannot query size of invalid HdType");
323 return 0;
324 case HdTypeBool:
325 // XXX: Currently, Hd represents bools as int32 sized values.
326 // See HdVtBufferSource for explanation. This should be moved
327 // to the GL backend!
328 return sizeof(int32_t);
329 case HdTypeInt8:
330 return sizeof(int8_t);
331 case HdTypeUInt8:
332 return sizeof(uint8_t);
333 case HdTypeInt16:
334 return sizeof(int16_t);
335 case HdTypeUInt16:
336 return sizeof(uint16_t);
337 case HdTypeInt32:
338 return sizeof(int32_t);
339 case HdTypeInt32Vec2:
340 return sizeof(int32_t)*2;
341 case HdTypeInt32Vec3:
342 return sizeof(int32_t)*3;
343 case HdTypeInt32Vec4:
344 return sizeof(int32_t)*4;
345 case HdTypeUInt32:
346 return sizeof(uint32_t);
347 case HdTypeUInt32Vec2:
348 return sizeof(uint32_t)*2;
349 case HdTypeUInt32Vec3:
350 return sizeof(uint32_t)*3;
351 case HdTypeUInt32Vec4:
352 return sizeof(uint32_t)*4;
353 case HdTypeFloat:
354 return sizeof(float);
355 case HdTypeFloatVec2:
356 return sizeof(float)*2;
357 case HdTypeFloatVec3:
358 return sizeof(float)*3;
359 case HdTypeFloatVec4:
360 return sizeof(float)*4;
361 case HdTypeFloatMat3:
362 return sizeof(float)*3*3;
363 case HdTypeFloatMat4:
364 return sizeof(float)*4*4;
365 case HdTypeDouble:
366 return sizeof(double);
367 case HdTypeDoubleVec2:
368 return sizeof(double)*2;
369 case HdTypeDoubleVec3:
370 return sizeof(double)*3;
371 case HdTypeDoubleVec4:
372 return sizeof(double)*4;
373 case HdTypeDoubleMat3:
374 return sizeof(double)*3*3;
375 case HdTypeDoubleMat4:
376 return sizeof(double)*4*4;
377 case HdTypeHalfFloat:
378 return sizeof(GfHalf);
379 case HdTypeHalfFloatVec2:
380 return sizeof(GfHalf)*2;
381 case HdTypeHalfFloatVec3:
382 return sizeof(GfHalf)*3;
383 case HdTypeHalfFloatVec4:
384 return sizeof(GfHalf)*4;
385 case HdTypeInt32_2_10_10_10_REV:
386 return sizeof(HdVec4f_2_10_10_10_REV);
387 };
388 }
389
HdDataSizeOfTupleType(HdTupleType tupleType)390 size_t HdDataSizeOfTupleType(HdTupleType tupleType)
391 {
392 return HdDataSizeOfType(tupleType.type) * tupleType.count;
393 }
394
HdGetComponentFormat(HdFormat f)395 HdFormat HdGetComponentFormat(HdFormat f)
396 {
397 switch(f) {
398 case HdFormatUNorm8:
399 case HdFormatUNorm8Vec2:
400 case HdFormatUNorm8Vec3:
401 case HdFormatUNorm8Vec4:
402 return HdFormatUNorm8;
403 case HdFormatSNorm8:
404 case HdFormatSNorm8Vec2:
405 case HdFormatSNorm8Vec3:
406 case HdFormatSNorm8Vec4:
407 return HdFormatSNorm8;
408 case HdFormatFloat16:
409 case HdFormatFloat16Vec2:
410 case HdFormatFloat16Vec3:
411 case HdFormatFloat16Vec4:
412 return HdFormatFloat16;
413 case HdFormatFloat32:
414 case HdFormatFloat32Vec2:
415 case HdFormatFloat32Vec3:
416 case HdFormatFloat32Vec4:
417 return HdFormatFloat32;
418 case HdFormatFloat32UInt8:
419 return HdFormatFloat32UInt8; // treat as a single component
420 case HdFormatInt32:
421 case HdFormatInt32Vec2:
422 case HdFormatInt32Vec3:
423 case HdFormatInt32Vec4:
424 return HdFormatInt32;
425 default:
426 return HdFormatInvalid;
427 }
428 }
429
HdGetComponentCount(HdFormat f)430 size_t HdGetComponentCount(HdFormat f)
431 {
432 switch (f) {
433 case HdFormatUNorm8Vec2:
434 case HdFormatSNorm8Vec2:
435 case HdFormatFloat16Vec2:
436 case HdFormatFloat32Vec2:
437 case HdFormatInt32Vec2:
438 return 2;
439 case HdFormatUNorm8Vec3:
440 case HdFormatSNorm8Vec3:
441 case HdFormatFloat16Vec3:
442 case HdFormatFloat32Vec3:
443 case HdFormatInt32Vec3:
444 return 3;
445 case HdFormatUNorm8Vec4:
446 case HdFormatSNorm8Vec4:
447 case HdFormatFloat16Vec4:
448 case HdFormatFloat32Vec4:
449 case HdFormatInt32Vec4:
450 return 4;
451 default:
452 return 1;
453 }
454 }
455
HdDataSizeOfFormat(HdFormat f)456 size_t HdDataSizeOfFormat(HdFormat f)
457 {
458 switch(f) {
459 case HdFormatUNorm8:
460 case HdFormatSNorm8:
461 return 1;
462 case HdFormatUNorm8Vec2:
463 case HdFormatSNorm8Vec2:
464 return 2;
465 case HdFormatUNorm8Vec3:
466 case HdFormatSNorm8Vec3:
467 return 3;
468 case HdFormatUNorm8Vec4:
469 case HdFormatSNorm8Vec4:
470 return 4;
471 case HdFormatFloat16:
472 return 2;
473 case HdFormatFloat16Vec2:
474 return 4;
475 case HdFormatFloat16Vec3:
476 return 6;
477 case HdFormatFloat16Vec4:
478 return 8;
479 case HdFormatFloat32:
480 case HdFormatInt32:
481 return 4;
482 case HdFormatFloat32Vec2:
483 case HdFormatInt32Vec2:
484 case HdFormatFloat32UInt8: // XXX: implementation dependent
485 return 8;
486 case HdFormatFloat32Vec3:
487 case HdFormatInt32Vec3:
488 return 12;
489 case HdFormatFloat32Vec4:
490 case HdFormatInt32Vec4:
491 return 16;
492 default:
493 return 0;
494 }
495 }
496
497 PXR_NAMESPACE_CLOSE_SCOPE
498