Blender V5.3
hydra/pointcloud.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/pointcloud.h"
7#include "hydra/geometry.inl"
8#include "hydra/util.h"
9#include "scene/pointcloud.h"
10
12
13HdCyclesPoints::HdCyclesPoints(const SdfPath &rprimId) : HdCyclesGeometry(rprimId) {}
14
16
18{
20 bits |= HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths |
21 HdChangeTracker::DirtyPrimvar;
22 return bits;
23}
24
25HdDirtyBits HdCyclesPoints::_PropagateDirtyBits(HdDirtyBits bits) const
26{
27 // Points and widths always have to be updated together
28 if (bits & (HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths)) {
29 bits |= HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths;
30 }
31
32 return bits;
33}
34
35void HdCyclesPoints::Populate(HdSceneDelegate *sceneDelegate, HdDirtyBits dirtyBits, bool &rebuild)
36{
37 if (dirtyBits & (HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths)) {
38 const size_t numPoints = _geom->num_points();
39
40 PopulatePoints(sceneDelegate);
41 PopulateWidths(sceneDelegate);
42
43 rebuild = _geom->num_points() != numPoints;
44
45 array<int> shaders;
46 shaders.reserve(_geom->num_points());
47 for (size_t i = 0; i < _geom->num_points(); ++i) {
48 shaders.push_back_reserved(0);
49 }
50
51 _geom->set_shader(shaders);
52 }
53
54 if (dirtyBits & HdChangeTracker::DirtyPrimvar) {
55 PopulatePrimvars(sceneDelegate);
56 }
57}
58
59void HdCyclesPoints::PopulatePoints(HdSceneDelegate *sceneDelegate)
60{
61 const HdSceneIndexPrim prim = GetPrim(sceneDelegate, GetId());
62 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
63 const VtValue value = ReadPrimvar(primvars, HdTokens->points);
64
65 if (!value.IsHolding<VtVec3fArray>()) {
66 TF_WARN("Invalid points data for %s", GetId().GetText());
67 return;
68 }
69
70 const auto &points = value.UncheckedGet<VtVec3fArray>();
71 static_assert(sizeof(GfVec3f) == sizeof(packed_float3));
72
73 _geom->resize(int(points.size()));
74
75 std::copy_n(reinterpret_cast<const packed_float3 *>(points.data()),
76 points.size(),
77 _geom->get_position_for_write());
78}
79
80void HdCyclesPoints::PopulateWidths(HdSceneDelegate *sceneDelegate)
81{
82 const HdSceneIndexPrim prim = GetPrim(sceneDelegate, GetId());
83 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
84 const VtValue value = ReadPrimvar(primvars, HdTokens->widths);
85 const HdInterpolation interpolation = ReadPrimvarInterpolation(primvars, HdTokens->widths);
86
87 if (!value.IsHolding<VtFloatArray>()) {
88 TF_WARN("Invalid widths data for %s", GetId().GetText());
89 return;
90 }
91
92 const auto &widths = value.UncheckedGet<VtFloatArray>();
93 float *radius = _geom->get_radius_for_write();
94
95 if (interpolation == HdInterpolationConstant) {
96 TF_VERIFY(widths.size() == 1);
97
98 const float constantRadius = widths[0] * 0.5f;
99
100 for (size_t i = 0; i < _geom->num_points(); ++i) {
101 radius[i] = constantRadius;
102 }
103 }
104 else if (interpolation == HdInterpolationVertex) {
105 TF_VERIFY(widths.size() == _geom->num_points());
106
107 for (size_t i = 0; i < _geom->num_points(); ++i) {
108 radius[i] = widths[i] * 0.5f;
109 }
110 }
111}
112
113void HdCyclesPoints::PopulatePrimvars(HdSceneDelegate *sceneDelegate)
114{
115 Scene *const scene = (Scene *)_geom->get_owner();
116
117 const HdSceneIndexPrim prim = GetPrim(sceneDelegate, GetId());
118 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
119
120 const std::pair<HdInterpolation, AttributeElement> interpolations[] = {
121 std::make_pair(HdInterpolationVertex, ATTR_ELEMENT_VERTEX),
122 std::make_pair(HdInterpolationConstant, ATTR_ELEMENT_OBJECT),
123 };
124
125 for (const auto &interpolation : interpolations) {
126 for (const TfToken &primvarName : PrimvarNamesAtInterpolation(primvars, interpolation.first)) {
127 // Skip special primvars that are handled separately
128 if (primvarName == HdTokens->points || primvarName == HdTokens->widths) {
129 continue;
130 }
131
132 const VtValue value = ReadPrimvar(primvars, primvarName);
133 if (value.IsEmpty()) {
134 continue;
135 }
136
137 const TfToken role = ReadPrimvarRole(primvars, primvarName);
138 const ustring name(primvarName.GetString());
139
141 if (role == HdPrimvarRoleTokens->textureCoordinate) {
142 std = ATTR_STD_UV;
143 }
144 else if (interpolation.first == HdInterpolationVertex) {
145 if (primvarName == HdTokens->displayColor || role == HdPrimvarRoleTokens->color) {
147 }
148 else if (primvarName == HdTokens->normals) {
150 }
151 }
152 else if (primvarName == HdTokens->displayColor &&
153 interpolation.first == HdInterpolationConstant)
154 {
155 if (value.IsHolding<VtVec3fArray>() && value.GetArraySize() == 1) {
156 const GfVec3f color = value.UncheckedGet<VtVec3fArray>()[0];
157 _instances[0]->set_color(make_float3(color[0], color[1], color[2]));
158 }
159 }
160
161 // Skip attributes that are not needed
162 if ((std != ATTR_STD_NONE && _geom->need_attribute(scene, std)) ||
163 _geom->need_attribute(scene, name))
164 {
165 ApplyPrimvars(_geom->attributes, name, value, interpolation.second, std);
166 }
167 }
168 }
169}
170
blender::float3 packed_float3
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
Definition geometry.inl:41
HdCyclesPoints(const PXR_NS::SdfPath &rprimId)
~HdCyclesPoints() override
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
void push_back_reserved(const T &t)
void reserve(const size_t newcapacity)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
HdInterpolation ReadPrimvarInterpolation(const HdPrimvarsSchema &primvars, const TfToken &name)
TfToken ReadPrimvarRole(const HdPrimvarsSchema &primvars, const TfToken &name)
HDCYCLES_NAMESPACE_OPEN_SCOPE HdSceneIndexPrim GetPrim(HdSceneDelegate *delegate, const SdfPath &id)
TfTokenVector PrimvarNamesAtInterpolation(const HdPrimvarsSchema &primvars, HdInterpolation interpolation)
VtValue ReadPrimvar(const HdPrimvarsSchema &primvars, const TfToken &name)
void ApplyPrimvars(AttributeSet &attributes, const ustring &name, VtValue value, AttributeElement elem, AttributeStandard std)
@ ATTR_ELEMENT_OBJECT
@ ATTR_ELEMENT_VERTEX
AttributeStandard
@ ATTR_STD_UV
@ ATTR_STD_VERTEX_NORMAL
@ ATTR_STD_NONE
@ ATTR_STD_VERTEX_COLOR