Blender V4.3
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#include "scene/geometry.h"
9#include "scene/scene.h"
10
11#include <pxr/base/gf/vec2f.h>
12#include <pxr/base/gf/vec3f.h>
13#include <pxr/base/gf/vec4f.h>
14#include <pxr/base/vt/array.h>
15#include <pxr/imaging/hd/tokens.h>
16
18
19void ApplyPrimvars(AttributeSet &attributes,
20 const ustring &name,
21 VtValue value,
24{
25 const void *data = HdGetValueData(value);
26 size_t size = value.GetArraySize();
27
28 const HdType valueType = HdGetValueTupleType(value).type;
29
30 TypeDesc attrType = CCL_NS::TypeUnknown;
31 switch (valueType) {
32 case HdTypeFloat:
33 attrType = CCL_NS::TypeFloat;
34 size *= sizeof(float);
35 break;
36 case HdTypeFloatVec2:
37 attrType = CCL_NS::TypeFloat2;
38 size *= sizeof(float2);
39 static_assert(sizeof(GfVec2f) == sizeof(float2));
40 break;
41 case HdTypeFloatVec3: {
42 attrType = CCL_NS::TypeVector;
43 size *= sizeof(float3);
44 // The Cycles "float3" data type is padded to "float4", so need to convert the array
45 const auto &valueData = value.Get<VtVec3fArray>();
46 VtArray<float3> valueConverted;
47 valueConverted.reserve(valueData.size());
48 for (const GfVec3f &vec : valueData) {
49 valueConverted.push_back(make_float3(vec[0], vec[1], vec[2]));
50 }
51 data = valueConverted.data();
52 value = std::move(valueConverted);
53 break;
54 }
55 case HdTypeFloatVec4:
56 attrType = CCL_NS::TypeFloat4;
57 size *= sizeof(float4);
58 static_assert(sizeof(GfVec4f) == sizeof(float4));
59 break;
60 default:
61 TF_WARN("Unsupported attribute type %d", static_cast<int>(valueType));
62 return;
63 }
64
65 Attribute *const attr = attributes.add(name, attrType, elem);
66 attr->std = std;
67
68 assert(size == attr->buffer.size());
69 std::memcpy(attr->data(), data, size);
70}
71
char * data()
vector< char > buffer
AttributeStandard std
void add(const float &f)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
draw_view in_light_buf[] float
HDCYCLES_NAMESPACE_OPEN_SCOPE void ApplyPrimvars(AttributeSet &attributes, const ustring &name, VtValue value, AttributeElement elem, AttributeStandard std)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
AttributeStandard
AttributeElement
long long TypeDesc