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