Blender  V3.3
hydra/attribute.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2022 NVIDIA Corporation
3  * Copyright 2022 Blender Foundation */
4 
5 #include "hydra/attribute.h"
6 #include "scene/attribute.h"
7 #include "scene/geometry.h"
8 #include "scene/scene.h"
9 
10 #include <pxr/base/gf/vec2f.h>
11 #include <pxr/base/gf/vec3f.h>
12 #include <pxr/base/gf/vec4f.h>
13 #include <pxr/base/vt/array.h>
14 #include <pxr/imaging/hd/tokens.h>
15 
17 
18 void ApplyPrimvars(AttributeSet &attributes,
19  const ustring &name,
20  VtValue value,
21  AttributeElement elem,
23 {
24  const void *data = HdGetValueData(value);
25  size_t size = value.GetArraySize();
26 
27  const HdType valueType = HdGetValueTupleType(value).type;
28 
29  TypeDesc attrType = CCL_NS::TypeUnknown;
30  switch (valueType) {
31  case HdTypeFloat:
32  attrType = CCL_NS::TypeFloat;
33  size *= sizeof(float);
34  break;
35  case HdTypeFloatVec2:
36  attrType = CCL_NS::TypeFloat2;
37  size *= sizeof(float2);
38  static_assert(sizeof(GfVec2f) == sizeof(float2));
39  break;
40  case HdTypeFloatVec3: {
41  attrType = CCL_NS::TypeVector;
42  size *= sizeof(float3);
43  // The Cycles "float3" data type is padded to "float4", so need to convert the array
44  const auto &valueData = value.Get<VtVec3fArray>();
45  VtArray<float3> valueConverted;
46  valueConverted.reserve(valueData.size());
47  for (const GfVec3f &vec : valueData) {
48  valueConverted.push_back(make_float3(vec[0], vec[1], vec[2]));
49  }
50  data = valueConverted.data();
51  value = std::move(valueConverted);
52  break;
53  }
54  case HdTypeFloatVec4:
55  attrType = CCL_NS::TypeFloat4;
56  size *= sizeof(float4);
57  static_assert(sizeof(GfVec4f) == sizeof(float4));
58  break;
59  default:
60  TF_WARN("Unsupported attribute type %d", static_cast<int>(valueType));
61  return;
62  }
63 
64  Attribute *const attr = attributes.add(name, attrType, elem);
65  attr->std = std;
66 
67  assert(size == attr->buffer.size());
68  std::memcpy(attr->data(), data, size);
69 }
70 
typedef float(TangentPoint)[2]
float float4[4]
float float2[2]
float float3[3]
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
Attribute * add(ustring name, TypeDesc type, AttributeElement element)
char * data()
vector< char > buffer
AttributeStandard std
HDCYCLES_NAMESPACE_OPEN_SCOPE void ApplyPrimvars(AttributeSet &attributes, const ustring &name, VtValue value, AttributeElement elem, AttributeStandard std)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
Definition: hydra/config.h:17
AttributeStandard
Definition: kernel/types.h:612
AttributeElement
Definition: kernel/types.h:597
#define make_float3(x, y, z)
Definition: metal/compat.h:204
CCL_NAMESPACE_BEGIN static constexpr OIIO_NAMESPACE_USING TypeDesc TypeFloat2(TypeDesc::FLOAT, TypeDesc::VEC2)