Blender V5.3
hydra/curves.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/curves.h"
7#include "hydra/geometry.inl"
8#include "hydra/util.h"
9#include "scene/hair.h"
10#include "util/types_float3.h"
11
12#include <pxr/imaging/hd/basisCurvesSchema.h>
13#include <pxr/imaging/hd/basisCurvesTopologySchema.h>
14
16
17HdCyclesCurves::HdCyclesCurves(const SdfPath &rprimId) : HdCyclesGeometry(rprimId) {}
18
20
22{
24 bits |= HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths |
25 HdChangeTracker::DirtyPrimvar | HdChangeTracker::DirtyTopology;
26 return bits;
27}
28
29HdDirtyBits HdCyclesCurves::_PropagateDirtyBits(HdDirtyBits bits) const
30{
31 if (bits & (HdChangeTracker::DirtyTopology)) {
32 // Changing topology clears the geometry, so need to populate everything again
33 bits |= HdChangeTracker::DirtyPoints | HdChangeTracker::DirtyWidths |
34 HdChangeTracker::DirtyPrimvar;
35 }
36
37 return bits;
38}
39
40void HdCyclesCurves::Populate(HdSceneDelegate *sceneDelegate, HdDirtyBits dirtyBits, bool &rebuild)
41{
42 if (HdChangeTracker::IsTopologyDirty(dirtyBits, GetId())) {
43 PopulateTopology(sceneDelegate);
44 }
45
46 if (dirtyBits & HdChangeTracker::DirtyPoints) {
47 PopulatePoints(sceneDelegate);
48 }
49
50 if (dirtyBits & HdChangeTracker::DirtyWidths) {
51 PopulateWidths(sceneDelegate);
52 }
53
54 if (dirtyBits & HdChangeTracker::DirtyPrimvar) {
55 PopulatePrimvars(sceneDelegate);
56 }
57
58 rebuild = (_geom->position_is_modified()) || (_geom->radius_is_modified());
59}
60
61void HdCyclesCurves::PopulatePoints(HdSceneDelegate *sceneDelegate)
62{
63 const HdSceneIndexPrim prim = GetPrim(sceneDelegate, GetId());
64 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
65 const VtValue value = ReadPrimvar(primvars, HdTokens->points);
66
67 if (!value.IsHolding<VtVec3fArray>()) {
68 TF_WARN("Invalid points data for %s", GetId().GetText());
69 return;
70 }
71
72 const auto &points = value.UncheckedGet<VtVec3fArray>();
73
74 TF_VERIFY(points.size() >= _geom->num_keys());
75
76 static_assert(sizeof(GfVec3f) == sizeof(packed_float3));
77
78 std::copy_n(reinterpret_cast<const packed_float3 *>(points.data()),
79 std::min(points.size(), _geom->num_keys()),
80 _geom->get_position_for_write());
81}
82
83void HdCyclesCurves::PopulateWidths(HdSceneDelegate *sceneDelegate)
84{
85 const HdSceneIndexPrim prim = GetPrim(sceneDelegate, GetId());
86 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
87 const VtValue value = ReadPrimvar(primvars, HdTokens->widths);
88 const HdInterpolation interpolation = ReadPrimvarInterpolation(primvars, HdTokens->widths);
89
90 if (!value.IsHolding<VtFloatArray>()) {
91 TF_WARN("Invalid widths data for %s", GetId().GetText());
92 return;
93 }
94
95 const auto &widths = value.UncheckedGet<VtFloatArray>();
96 float *radius = _geom->get_radius_for_write();
97
98 if (interpolation == HdInterpolationConstant) {
99 TF_VERIFY(widths.size() == 1);
100
101 const float constantRadius = widths[0] * 0.5f;
102
103 for (size_t i = 0; i < _geom->num_keys(); ++i) {
104 radius[i] = constantRadius;
105 }
106 }
107 else if (interpolation == HdInterpolationVertex) {
108 TF_VERIFY(widths.size() == _geom->num_keys());
109
110 for (size_t i = 0; i < _geom->num_keys(); ++i) {
111 radius[i] = widths[i] * 0.5f;
112 }
113 }
114}
115
116void HdCyclesCurves::PopulatePrimvars(HdSceneDelegate *sceneDelegate)
117{
118 Scene *const scene = (Scene *)_geom->get_owner();
119
120 const HdSceneIndexPrim prim = GetPrim(sceneDelegate, GetId());
121 const HdPrimvarsSchema primvars = HdPrimvarsSchema::GetFromParent(prim.dataSource);
122
123 const std::pair<HdInterpolation, AttributeElement> interpolations[] = {
124 std::make_pair(HdInterpolationVertex, ATTR_ELEMENT_CURVE_KEY),
125 std::make_pair(HdInterpolationVarying, ATTR_ELEMENT_CURVE_KEY),
126 std::make_pair(HdInterpolationUniform, ATTR_ELEMENT_CURVE),
127 std::make_pair(HdInterpolationConstant, ATTR_ELEMENT_OBJECT),
128 };
129
130 for (const auto &interpolation : interpolations) {
131 for (const TfToken &primvarName : PrimvarNamesAtInterpolation(primvars, interpolation.first)) {
132 // Skip special primvars that are handled separately
133 if (primvarName == HdTokens->points || primvarName == HdTokens->widths) {
134 continue;
135 }
136
137 const VtValue value = ReadPrimvar(primvars, primvarName);
138 if (value.IsEmpty()) {
139 continue;
140 }
141
142 const TfToken role = ReadPrimvarRole(primvars, primvarName);
143 const ustring name(primvarName.GetString());
144
146 if (role == HdPrimvarRoleTokens->textureCoordinate) {
147 std = ATTR_STD_UV;
148 }
149 else if (primvarName == HdTokens->normals && interpolation.first == HdInterpolationVertex) {
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 AttributeElement elem = interpolation.second;
166 if (std == ATTR_STD_VERTEX_NORMAL) {
168 }
169 ApplyPrimvars(_geom->attributes, name, value, elem, std);
170 }
171 }
172 }
173}
174
175void HdCyclesCurves::PopulateTopology(HdSceneDelegate *sceneDelegate)
176{
177 // Clear geometry before populating it again with updated topology
178 _geom->clear(true);
179
180 const HdSceneIndexPrim prim = GetPrim(sceneDelegate, GetId());
181 const HdBasisCurvesTopologySchema topoSchema =
182 HdBasisCurvesSchema::GetFromParent(prim.dataSource).GetTopology();
183
184 TfToken curveType = HdTokens->linear;
185 if (auto ds = topoSchema.GetType()) {
186 curveType = ds->GetTypedValue(0.0f);
187 }
188 TfToken curveBasis = HdTokens->bezier;
189 if (auto ds = topoSchema.GetBasis()) {
190 curveBasis = ds->GetTypedValue(0.0f);
191 }
192 TfToken curveWrap = HdTokens->nonperiodic;
193 if (auto ds = topoSchema.GetWrap()) {
194 curveWrap = ds->GetTypedValue(0.0f);
195 }
196 VtIntArray curveVertexCounts;
197 if (auto ds = topoSchema.GetCurveVertexCounts()) {
198 curveVertexCounts = ds->GetTypedValue(0.0f);
199 }
200 VtIntArray curveIndices;
201 if (auto ds = topoSchema.GetCurveIndices()) {
202 curveIndices = ds->GetTypedValue(0.0f);
203 }
204 const HdBasisCurvesTopology topology(
205 curveType, curveBasis, curveWrap, curveVertexCounts, curveIndices);
206
207 _geom->resize_curves(topology.GetNumCurves(), topology.CalculateNeededNumberOfControlPoints());
208
209 const VtIntArray vertCounts = topology.GetCurveVertexCounts();
210
211 int *curve_first_key = _geom->get_curve_first_key().data();
212
213 for (int curve = 0, key = 0; curve < topology.GetNumCurves(); ++curve) {
214 // Always reference shader at index zero, which is the primitive material
215 curve_first_key[curve] = key;
216
217 key += vertCounts[curve];
218 }
219
220 std::ranges::fill(_geom->get_curve_shader(), 0);
221
222 _geom->tag_curve_first_key_modified();
223 _geom->tag_curve_shader_modified();
224}
225
blender::float3 packed_float3
HdCyclesCurves(const PXR_NS::SdfPath &rprimId)
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
~HdCyclesCurves() override
HdCyclesGeometry(const PXR_NS::SdfPath &rprimId)
Definition geometry.inl:27
PXR_NS::HdDirtyBits GetInitialDirtyBitsMask() const override
Definition geometry.inl:41
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)
AttributeElement
@ ATTR_ELEMENT_CURVE_KEY
@ ATTR_ELEMENT_OBJECT
@ ATTR_ELEMENT_CURVE_KEY_NORMAL
@ ATTR_ELEMENT_CURVE
AttributeStandard
@ ATTR_STD_UV
@ ATTR_STD_VERTEX_NORMAL
@ ATTR_STD_NONE