Blender V5.3
blender/pointcloud.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/pointcloud.h"
6#include "scene/attribute.h"
7#include "scene/scene.h"
8
9#include "util/hash.h"
10#include "util/tbb.h"
11
13#include "blender/sync.h"
14#include "blender/util.h"
15
17
18#include "BKE_attribute.hh"
19#include "BKE_attribute_math.hh"
20
22
24 const blender::Span<blender::float3> b_attribute,
25 const float motion_scale)
26{
27 const int num_points = pointcloud->num_points();
28
29 /* Override motion steps to fixed number. */
30 pointcloud->set_motion_steps(3);
31
32 /* Set motion steps on position and radius attributes. */
33 Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
34 Attribute *attr_R = pointcloud->attributes.find(ATTR_STD_RADIUS);
35 attr_P->add_motion(pointcloud);
36 attr_R->add_motion(pointcloud);
37 const packed_float3 *P = pointcloud->get_position();
38 const float *radius = pointcloud->get_radius();
39
40 /* Only export previous and next frame, we don't have any in between data. */
41 const float motion_times[2] = {-1.0f, 1.0f};
42 for (int step = 1; step <= 2; step++) {
43 const float relative_time = motion_times[step - 1] * 0.5f * motion_scale;
45 float *mR = attr_R->data_for_write<float>(step);
46
47 for (int i = 0; i < num_points; i++) {
48 mP[i] = float3(P[i]) +
49 make_float3(b_attribute[i][0], b_attribute[i][1], b_attribute[i][2]) * relative_time;
50 mR[i] = radius[i];
51 }
52 }
53}
54
55static void copy_attributes(PointCloud *pointcloud,
56 const blender::PointCloud &b_pointcloud,
57 const bool need_motion,
58 const float motion_scale)
59{
60 const blender::bke::AttributeAccessor b_attributes = b_pointcloud.attributes();
61 if (b_attributes.domain_size(blender::bke::AttrDomain::Point) == 0) {
62 return;
63 }
64
65 AttributeSet &attributes = pointcloud->attributes;
66 static const ustring u_velocity("velocity");
67 b_attributes.foreach_attribute([&](const blender::bke::AttributeIter &iter) {
68 const ustring name{std::string_view(iter.name)};
69
70 if (need_motion && name == u_velocity) {
71 const blender::VArraySpan b_attr = *iter.get<blender::float3>();
72 attr_create_motion_from_velocity(pointcloud, b_attr, motion_scale);
73 }
74
75 if (attributes.find(name)) {
76 return;
77 }
78
79 const blender::bke::GAttributeReader b_attr = iter.get();
80 blender::bke::attribute_math::to_static_type(b_attr.varray.type(), [&]<typename BlenderT>() {
81 using Converter = typename ccl::AttributeConverter<BlenderT>;
82 using CyclesT = typename Converter::CyclesT;
83 if constexpr (!std::is_void_v<CyclesT>) {
84 const blender::VArray<BlenderT> src_varray = b_attr.varray.typed<BlenderT>();
85 const blender::CommonVArrayInfo info = b_attr.varray.common_info();
86
87 if (info.type == blender::CommonVArrayInfo::Type::Single) {
88 const auto &single_value = *static_cast<const BlenderT *>(info.data);
89 Attribute *attr = attributes.add(name, Converter::type_desc, ATTR_ELEMENT_MESH);
90 CyclesT *data = reinterpret_cast<CyclesT *>(attr->data_for_write());
91 *data = Converter::convert(single_value);
92 return;
93 }
94
95 if constexpr (Converter::layout_compatible) {
96 if (info.type == blender::CommonVArrayInfo::Type::Span && b_attr.sharing_info) {
97 attributes.add_shared(name,
98 Converter::type_desc,
99 ATTR_ELEMENT_VERTEX,
100 info.data,
101 src_varray.size(),
102 b_attr.sharing_info);
103 return;
104 }
105 }
106
107 Attribute *attr = attributes.add(name, Converter::type_desc, ATTR_ELEMENT_VERTEX);
108 CyclesT *data = reinterpret_cast<CyclesT *>(attr->data_for_write());
109
110 const blender::VArraySpan src = src_varray;
111 for (const int i : src.index_range()) {
112 data[i] = Converter::convert(src[i]);
113 }
114 }
115 });
116 });
117}
118
120 PointCloud *pointcloud, const blender::bke::AttributeAccessor &b_attributes)
121{
122 const size_t num_points = pointcloud->num_points();
123
124 /* Base radiance and opacity. */
126 pointcloud->attributes,
128 b_attributes.lookup<blender::float4>("radiance:base", blender::bke::AttrDomain::Point),
129 num_points);
130
131 /* Scale. */
133 pointcloud->attributes,
136 num_points);
137
138 /* Rotation. */
140 pointcloud->attributes,
143 num_points);
144
145 /* Spherical harmonics. */
148 for (int i = 0; i < PackedSphericalHarmonicsRest::MAX_COEFFICIENTS; ++i) {
149 const string sh_attr_name = "radiance:sh_" + std::to_string(i);
151 sh_attr_name, blender::bke::AttrDomain::Point);
152 if (!reader) {
153 break;
154 }
155 sh_attribute_readers.push_back(std::move(reader));
156 sh_attribute_spans.push_back(
157 blender::VArraySpan<blender::float3>(*sh_attribute_readers.back()));
158 }
159 if (!sh_attribute_readers.empty()) {
160 Attribute *attr = pointcloud->attributes.add(
163 parallel_for(blocked_range<size_t>(0, num_points, 32), [&](const blocked_range<size_t> &r) {
164 for (size_t point_index = r.begin(); point_index != r.end(); point_index++) {
165 spherical_harmonics_rest_fill_zero(sh_data[point_index]);
166 for (int sh_index = 0; sh_index < int(sh_attribute_readers.size()); ++sh_index) {
168 sh_data[point_index],
169 sh_index,
170 make_float3(sh_attribute_spans[sh_index][point_index].x,
171 sh_attribute_spans[sh_index][point_index].y,
172 sh_attribute_spans[sh_index][point_index].z));
173 }
174 }
175 });
176 }
177
179}
180
182 PointCloud *pointcloud,
183 Attribute *attr_radiance_base,
184 Attribute *attr_scale,
185 Attribute *attr_rotation,
186 const blender::PointCloud &b_pointcloud,
187 const blender::bke::AttributeAccessor &b_attributes,
188 const int attr_step)
189{
190 const blender::Span<blender::float3> b_positions = b_pointcloud.positions();
191 const int num_points = pointcloud->num_points();
192 const bool size_matches = (b_positions.size() == num_points);
193
194 if (size_matches) {
196 *attr_radiance_base,
197 attr_step,
198 b_attributes.lookup<blender::float4>("radiance:base",
200 {
201 float4 *motion_radiance_base = attr_radiance_base->data_for_write<float4>(attr_step);
202 std::fill_n(motion_radiance_base, num_points, PointCloud::DEFAULT_GSPLAT_RADIANCE_BASE);
203 }
204
206 *attr_scale,
207 attr_step,
209 {
210 packed_float3 *motion_scale = attr_scale->data_for_write<packed_float3>(attr_step);
211 std::fill_n(motion_scale, num_points, PointCloud::DEFAULT_GSPLAT_SCALE);
212 }
213
214 if (!sync_attribute_motion_step_from_blender(*attr_rotation,
215 attr_step,
216 b_attributes.lookup<blender::math::Quaternion>(
218 {
219 Quaternion *motion_rotation = attr_rotation->data_for_write<Quaternion>(attr_step);
220 std::fill_n(motion_rotation, num_points, identity_quaternion());
221 }
222 return;
223 }
224
225 /* Slow path: point count differs, copy what overlaps. */
226 const blender::VArraySpan b_radiance_base = *b_attributes.lookup<blender::float4>(
227 "radiance:base", blender::bke::AttrDomain::Point);
228 const blender::VArraySpan b_scale = *b_attributes.lookup<blender::float3>(
230 const blender::VArraySpan b_rotation = *b_attributes.lookup<blender::math::Quaternion>(
232 float4 *motion_radiance_base = attr_radiance_base->data_for_write<float4>(attr_step);
233 packed_float3 *motion_scale = attr_scale->data_for_write<packed_float3>(attr_step);
234 Quaternion *motion_rotation = attr_rotation->data_for_write<Quaternion>(attr_step);
235
236 parallel_for(blocked_range<size_t>(0, std::min<int>(num_points, b_positions.size()), 32),
237 [&](const blocked_range<size_t> &r) {
238 for (size_t i = r.begin(); i != r.end(); i++) {
239 motion_radiance_base[i] = b_radiance_base.is_empty() ?
240 PointCloud::DEFAULT_GSPLAT_RADIANCE_BASE :
241 make_float4(b_radiance_base[i][0],
242 b_radiance_base[i][1],
243 b_radiance_base[i][2],
244 b_radiance_base[i][3]);
245 motion_scale[i] = b_scale.is_empty() ?
246 PointCloud::DEFAULT_GSPLAT_SCALE :
247 make_float3(b_scale[i][0], b_scale[i][1], b_scale[i][2]);
248 motion_rotation[i] = b_rotation.is_empty() ? identity_quaternion() :
249 make_quaternion(b_rotation[i][0],
250 b_rotation[i][1],
251 b_rotation[i][2],
252 b_rotation[i][3]);
253 }
254 });
255}
256
257static void export_pointcloud(Scene *scene,
258 PointCloud *pointcloud,
259 const blender::PointCloud &b_pointcloud,
260 const bool need_motion,
261 const float motion_scale)
262{
263 const blender::Span<blender::float3> b_positions = b_pointcloud.positions();
264 const blender::bke::AttributeAccessor b_attributes = b_pointcloud.attributes();
265
266 pointcloud->resize(b_positions.size());
267
268 /* Sync positions, sharing with Blender when possible. */
270 pointcloud->attributes,
272 b_attributes.lookup<blender::float3>("position", blender::bke::AttrDomain::Point),
273 b_positions.size());
274 pointcloud->tag_position_modified();
275
276 /* Sync radius, sharing with Blender when possible, or filling default. */
278 pointcloud->attributes,
280 b_attributes.lookup<float>("radius", blender::bke::AttrDomain::Point),
281 b_positions.size()))
282 {
283 pointcloud->tag_radius_modified();
284 }
285 else {
286 float *radius = pointcloud->get_radius_for_write();
287 std::fill(radius, radius + b_positions.size(), 0.01f);
288 }
289
290 int *shader = pointcloud->get_shader().data();
291 std::fill(shader, shader + b_positions.size(), 0);
292
293 if (pointcloud->need_attribute(scene, ATTR_STD_POINT_RANDOM)) {
294 Attribute *attr_random = pointcloud->attributes.add(ATTR_STD_POINT_RANDOM);
295 float *data = attr_random->data_for_write<float>();
296 for (const int i : b_positions.index_range()) {
297 data[i] = hash_uint2_to_float(i, 0);
298 }
299 }
300
301 if (b_pointcloud.type == blender::PointCloudType::GSplat) {
302 export_pointcloud_gsplat_attributes(pointcloud, b_attributes);
303 pointcloud->set_render_as(PointCloud::RENDER_AS_GSPLATS);
304 }
305
306 copy_attributes(pointcloud, b_pointcloud, need_motion, motion_scale);
307}
308
309static void export_pointcloud_motion(PointCloud *pointcloud,
310 const blender::PointCloud &b_pointcloud,
311 const int motion_step)
312{
313 const bool is_gsplat = (b_pointcloud.type == blender::PointCloudType::GSplat);
314
315 /* Set motion steps on position and radius attributes. */
316 Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
317 Attribute *attr_R = pointcloud->attributes.find(ATTR_STD_RADIUS);
318 Attribute *attr_radiance_base = is_gsplat ?
320 nullptr;
321 Attribute *attr_scale = is_gsplat ? pointcloud->attributes.find(ATTR_STD_GSPLAT_SCALE) : nullptr;
322 Attribute *attr_rotation = is_gsplat ? pointcloud->attributes.find(ATTR_STD_GSPLAT_ROTATION) :
323 nullptr;
324 const bool has_gsplat_attributes = (attr_radiance_base && attr_scale && attr_rotation);
325 bool new_attribute = false;
326
327 if (!attr_P->has_motion()) {
328 attr_P->add_motion(pointcloud);
329 attr_R->add_motion(pointcloud);
330 if (is_gsplat && has_gsplat_attributes) {
331 attr_radiance_base->add_motion(pointcloud);
332 attr_scale->add_motion(pointcloud);
333 attr_rotation->add_motion(pointcloud);
334 }
335 new_attribute = true;
336 }
337
338 const int num_points = pointcloud->num_points();
339 const int attr_step = motion_step + 1;
340 const blender::Span<blender::float3> b_positions = b_pointcloud.positions();
341 const blender::bke::AttributeAccessor b_attributes = b_pointcloud.attributes();
342 const bool size_matches = (b_positions.size() == num_points);
343
344 bool have_motion = false;
345
346 if (size_matches) {
347 /* Fast path: point count unchanged, sync the whole step from Blender,
348 * sharing the buffer when possible. */
350 *attr_P,
351 attr_step,
352 b_attributes.lookup<blender::float3>("position", blender::bke::AttrDomain::Point));
354 *attr_R,
355 attr_step,
356 b_attributes.lookup<float>("radius", blender::bke::AttrDomain::Point)))
357 {
358 float *mR = attr_R->data_for_write<float>(attr_step);
359 std::fill(mR, mR + num_points, 0.01f);
360 }
361
362 /* If the buffer is shared from Blender and unchanged across frames, the
363 * pointer matches the center step's, so the memcmp is skipped. */
364 const packed_float3 *motion_P = attr_P->data<packed_float3>(attr_step);
365 const packed_float3 *center_P = pointcloud->get_position();
366 have_motion = motion_P != center_P &&
367 std::memcmp(motion_P, center_P, num_points * sizeof(packed_float3)) != 0;
368 }
369 else {
370 /* Slow path: point count differs, copy what overlaps. Remaining points
371 * keep the center data. */
372 pointcloud->copy_center_to_motion_step(motion_step);
373
374 const blender::VArraySpan b_radius = *b_attributes.lookup<float>(
376 packed_float3 *mP = attr_P->data_for_write<packed_float3>(attr_step);
377 float *mR = attr_R->data_for_write<float>(attr_step);
378 for (int i = 0; i < std::min<int>(num_points, b_positions.size()); i++) {
379 mP[i] = make_float3(b_positions[i][0], b_positions[i][1], b_positions[i][2]);
380 mR[i] = b_radius.is_empty() ? 0.01f : b_radius[i];
381 }
382 }
383
384 if (is_gsplat && has_gsplat_attributes) {
386 attr_radiance_base,
387 attr_scale,
388 attr_rotation,
389 b_pointcloud,
390 b_attributes,
391 attr_step);
392 /* TODO(sergey): Remove constant radiance base, scale, and rotation attributes. */
393 }
394
395 /* In case of new attribute, verify if there really was any motion. */
396 if (new_attribute) {
397 if (!size_matches || !have_motion) {
398 attr_P->remove_motion();
399 attr_R->remove_motion();
400 if (is_gsplat && has_gsplat_attributes) {
401 attr_radiance_base->remove_motion();
402 attr_scale->remove_motion();
403 attr_rotation->remove_motion();
404 }
405 }
406 else if (motion_step > 0) {
407 /* Motion, fill up previous steps that we might have skipped because
408 * they had no motion, but we need them anyway now. */
409 for (int step = 0; step < motion_step; step++) {
410 pointcloud->copy_center_to_motion_step(step);
411 }
412 }
413 }
414
415 /* Export attributes */
416 copy_attributes(pointcloud, b_pointcloud, false, 0.0f);
417}
418
419void BlenderSync::sync_pointcloud(PointCloud *pointcloud, BObjectInfo &b_ob_info)
420{
421 const size_t old_numpoints = pointcloud->num_points();
422
423 array<Node *> used_shaders = pointcloud->get_used_shaders();
424
425 PointCloud new_pointcloud;
426 new_pointcloud.set_used_shaders(used_shaders);
427
428 /* TODO: add option to filter out points in the view layer. */
429 const blender::PointCloud *b_pointcloud = blender::id_cast<blender::PointCloud *>(
430 b_ob_info.object_data);
431 /* Motion blur attribute is relative to seconds, we need it relative to frames. */
432 const bool need_motion = object_need_motion_attribute(b_ob_info, scene);
433 const float motion_scale = (need_motion) ? scene->motion_shutter_time() /
434 (b_scene->r.frs_sec / b_scene->r.frs_sec_base) :
435 0.0f;
436 export_pointcloud(scene, &new_pointcloud, *b_pointcloud, need_motion, motion_scale);
437
438 if (scene->need_motion() == Scene::MOTION_PASS_INTERACTIVE &&
439 pointcloud->num_points() == new_pointcloud.num_points())
440 {
441 new_pointcloud.set_motion_steps(2);
442
443 Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
444 Attribute *new_attr_P = new_pointcloud.attributes.find(ATTR_STD_POSITION);
445 if (attr_P->has_motion()) {
446 new_attr_P->take_motion_from(*attr_P);
447 }
448 else {
449 new_attr_P->add_motion(&new_pointcloud);
450 new_pointcloud.copy_center_to_motion_step(0);
451 }
452 }
453
454 /* Update original sockets. */
455
456 pointcloud->clear_non_sockets();
457
458 for (const SocketType &socket : new_pointcloud.type->inputs) {
459 /* Those sockets are updated in sync_object, so do not modify them. */
460 if (socket.name == "use_motion_blur" || socket.name == "used_shaders") {
461 continue;
462 }
463 pointcloud->set_value(socket, new_pointcloud, socket);
464 }
465
466 pointcloud->attributes.update(std::move(new_pointcloud.attributes));
467
468 /* Tag update. */
469 const bool rebuild = (pointcloud && old_numpoints != pointcloud->num_points());
470 pointcloud->tag_update(scene, rebuild);
471}
472
473void BlenderSync::sync_pointcloud_motion(PointCloud *pointcloud,
474 BObjectInfo &b_ob_info,
475 const int motion_step)
476{
477 /* Skip if nothing exported. */
478 if (pointcloud->num_points() == 0) {
479 return;
480 }
481
482 /* Export deformed coordinates. */
483 if (ccl::BKE_object_is_deform_modified(b_ob_info, *b_scene, preview)) {
484 /* PointCloud object. */
485 const blender::PointCloud *b_pointcloud = blender::id_cast<blender::PointCloud *>(
486 b_ob_info.object_data);
487 export_pointcloud_motion(pointcloud, *b_pointcloud, motion_step);
488 }
489 else {
490 /* No deformation on this frame, copy coordinates if other frames did have it. */
491 pointcloud->copy_center_to_motion_step(motion_step);
492 }
493}
494
bool sync_attribute_from_blender(AttributeSet &attributes, const AttributeStandard std, const blender::bke::AttributeReader< BlenderT > &b_reader, const int size)
bool sync_attribute_motion_step_from_blender(Attribute &attr, const int motion_step, const blender::bke::AttributeReader< BlenderT > &b_reader)
static void attr_create_motion_from_velocity(Hair *hair, const blender::Span< blender::float3 > src, const float motion_scale)
static void export_pointcloud_motion_gsplat_attributes(PointCloud *pointcloud, Attribute *attr_radiance_base, Attribute *attr_scale, Attribute *attr_rotation, const blender::PointCloud &b_pointcloud, const blender::bke::AttributeAccessor &b_attributes, const int attr_step)
static void export_pointcloud_motion(PointCloud *pointcloud, const blender::PointCloud &b_pointcloud, const int motion_step)
static void export_pointcloud_gsplat_attributes(PointCloud *pointcloud, const blender::bke::AttributeAccessor &b_attributes)
static void export_pointcloud(Scene *scene, PointCloud *pointcloud, const blender::PointCloud &b_pointcloud, const bool need_motion, const float motion_scale)
static void copy_attributes(PointCloud *pointcloud, const blender::PointCloud &b_pointcloud, const bool need_motion, const float motion_scale)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void update(AttributeSet &&new_attributes)
Attribute * find(ustring name) const
Attribute * add(ustring name, const TypeDesc type, AttributeElement element)
void take_motion_from(Attribute &other)
const T * data(const int step=0) const
T * data_for_write(const int step=0)
bool has_motion() const
void add_motion(const Geometry *geom)
void remove_motion()
void tag_position_modified()
const packed_float3 * get_position() const
void tag_update(Scene *scene, bool rebuild)
float * get_radius_for_write()
const float * get_radius() const
AttributeSet attributes
bool need_attribute(const Scene *scene, AttributeStandard std)
void tag_radius_modified()
void resize(const int numpoints)
void create_missing_gsplat_attributes()
size_t num_points() const
static const float4 DEFAULT_GSPLAT_RADIANCE_BASE
void copy_center_to_motion_step(const int motion_step)
static const float3 DEFAULT_GSPLAT_SCALE
Definition scene.h:133
@ MOTION_PASS_INTERACTIVE
Definition scene.h:207
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
void foreach_attribute(const FunctionRef< void(const AttributeIter &)> fn) const
GAttributeReader lookup(const StringRef name) const
int domain_size(const AttrDomain domain) const
GAttributeReader get() const
constexpr bool is_empty() const
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
float hash_uint2_to_float(uint kx, uint ky)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
@ ATTR_STD_GSPLAT_SCALE
@ ATTR_STD_POINT_RANDOM
@ ATTR_STD_GSPLAT_RADIANCE_BASE
@ ATTR_STD_GSPLAT_ROTATION
@ ATTR_STD_POSITION
@ ATTR_STD_GSPLAT_RADIANCE_SPHERICAL_HARMONICS_REST
@ ATTR_STD_RADIUS
void to_static_type(const CPPType &cpp_type, Fn &&fn)
QuaternionBase< float > Quaternion
VecBase< float, 4 > float4
VecBase< float, 3 > float3
blender::ID * object_data
vector< SocketType, std::allocator< SocketType > > inputs
Definition node_type.h:131
const NodeType * type
Definition graph/node.h:179
void set_value(const SocketType &socket, const Node &other, const SocketType &other_socket)
ccl_static_constexpr int MAX_COEFFICIENTS
ustring name
Definition node_type.h:84
ccl_device_inline Quaternion identity_quaternion()
ccl_device_forceinline void spherical_harmonics_rest_fill_zero(ccl_global PackedSphericalHarmonicsRest &packed_spherical_harmonics_rest)
ccl_device_forceinline void spherical_harmonics_rest_set_coefficient(ccl_global PackedSphericalHarmonicsRest &packed_spherical_harmonics_rest, const int index, const float3 value)