2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-03-17 14:41:48 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-03-20 02:19:09 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup bke
|
2021-12-13 06:00:39 +01:00
|
|
|
* \brief General operations for point clouds.
|
2020-03-17 14:41:48 +01:00
|
|
|
*/
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
|
Refactor: Improve access to object data bounds
Currently object bounds (`object.runtime.bb`) are lazily initialized
when accessed. This access happens from arbitrary threads, and
is unprotected by a mutex. This can cause access to stale data at
best, and crashes at worst. Eager calculation is meant to keep this
working, but it's fragile.
Since e8f4010611e7, geometry bounds are cached in the geometry
itself, which makes this object-level cache redundant. So, it's clearer
to build the `BoundBox` from those cached bounds and return it by
value, without interacting with the object's cached bounding box.
The code change is is mostly a move from `const BoundBox *` to
`std::optional<BoundBox>`. This is only one step of a larger change
described in #96968. Followup steps would include switching to
a simpler and smaller `Bounds` type, removing redundant object-
level access, and eventually removing `object.runtime.bb`.
Access of bounds from the object for mesh, curves, and point cloud
objects should now be thread-safe. Other object types still lazily
initialize the object `BoundBox` cache since they don't have
a data-level cache.
Pull Request: https://projects.blender.org/blender/blender/pulls/113465
2023-10-19 14:18:40 +02:00
|
|
|
#include "DNA_object_types.h" /* #BoundBox. */
|
|
|
|
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
# include <mutex>
|
|
|
|
|
|
|
|
# include "BLI_bounds_types.hh"
|
2023-01-04 00:14:55 +01:00
|
|
|
# include "BLI_math_vector_types.hh"
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
# include "BLI_shared_cache.hh"
|
2023-04-13 17:55:32 +02:00
|
|
|
|
|
|
|
# include "DNA_pointcloud_types.h"
|
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
# include "BKE_customdata.hh"
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
#endif
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct BoundBox;
|
|
|
|
struct Depsgraph;
|
|
|
|
struct Main;
|
|
|
|
struct Object;
|
|
|
|
struct PointCloud;
|
|
|
|
struct Scene;
|
|
|
|
|
2020-04-09 18:49:40 +02:00
|
|
|
/* PointCloud datablock */
|
|
|
|
extern const char *POINTCLOUD_ATTR_POSITION;
|
|
|
|
extern const char *POINTCLOUD_ATTR_RADIUS;
|
|
|
|
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
namespace blender::bke {
|
|
|
|
|
|
|
|
struct PointCloudRuntime {
|
|
|
|
/**
|
|
|
|
* A cache of bounds shared between data-blocks with unchanged positions and radii.
|
|
|
|
* When data changes affect the bounds, the cache is "un-shared" with other geometries.
|
|
|
|
* See #SharedCache comments.
|
|
|
|
*/
|
|
|
|
mutable SharedCache<Bounds<float3>> bounds_cache;
|
|
|
|
|
|
|
|
MEM_CXX_CLASS_ALLOC_FUNCS("PointCloudRuntime");
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace blender::bke
|
2023-04-13 17:55:32 +02:00
|
|
|
|
|
|
|
inline blender::Span<blender::float3> PointCloud::positions() const
|
|
|
|
{
|
|
|
|
return {static_cast<const blender::float3 *>(
|
|
|
|
CustomData_get_layer_named(&this->pdata, CD_PROP_FLOAT3, "position")),
|
|
|
|
this->totpoint};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline blender::MutableSpan<blender::float3> PointCloud::positions_for_write()
|
|
|
|
{
|
|
|
|
return {static_cast<blender::float3 *>(CustomData_get_layer_named_for_write(
|
|
|
|
&this->pdata, CD_PROP_FLOAT3, "position", this->totpoint)),
|
|
|
|
this->totpoint};
|
|
|
|
}
|
|
|
|
|
Geometry: Cache bounds min and max, share between data-blocks
Bounding box calculation can be a large in some situations, especially
instancing. This patch caches the min and max of the bounding box in
runtime data of meshes, point clouds, and curves, implementing part of
T96968.
Bounds are now calculated lazily-- only after they are tagged dirty.
Also, cached bounds are also shared when copying geometry data-blocks
that have equivalent data. When bounds are calculated on an evaluated
data-block, they are also accessible on the original, and the next
evaluated ID will also share them. A geometry will stop sharing bounds
as soon as its positions (or radii) are changed.
Just caching the bounds gave a 2-3x speedup with thousands of mesh
geometry instances in the viewport. Sharing the bounds can eliminate
recalculations entirely in cases like copying meshes in geometry nodes
or the selection paint brush in curves sculpt mode, which causes a
reevaluation but doesn't change the positions.
**Implementation**
The sharing is achieved with a `shared_ptr` that points to a cache mutex
(from D16419) and the cached bounds data. When geometries are copied,
the bounds are shared by default, and only "un-shared" when the bounds
are tagged dirty.
Point clouds have a new runtime struct to store this data. Functions
for tagging the data dirty are improved for added for point clouds
and improved for curves. A missing tag has also been fixed for mesh
sculpt mode.
**Future**
There are further improvements which can be worked on next
- Apply changes to volume objects and other types where it makes sense
- Continue cleanup changes described in T96968
- Apply shared cache design to more expensive data like triangulation
or normals
Differential Revision: https://developer.blender.org/D16204
2022-11-15 20:46:55 +01:00
|
|
|
#endif
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
void *BKE_pointcloud_add(struct Main *bmain, const char *name);
|
2020-04-09 18:49:40 +02:00
|
|
|
void *BKE_pointcloud_add_default(struct Main *bmain, const char *name);
|
2022-01-07 01:38:08 +01:00
|
|
|
struct PointCloud *BKE_pointcloud_new_nomain(int totpoint);
|
2022-09-15 15:21:25 +02:00
|
|
|
void BKE_pointcloud_nomain_to_pointcloud(struct PointCloud *pointcloud_src,
|
2023-04-19 21:52:06 +02:00
|
|
|
struct PointCloud *pointcloud_dst);
|
2020-03-17 14:41:48 +01:00
|
|
|
|
Refactor: Improve access to object data bounds
Currently object bounds (`object.runtime.bb`) are lazily initialized
when accessed. This access happens from arbitrary threads, and
is unprotected by a mutex. This can cause access to stale data at
best, and crashes at worst. Eager calculation is meant to keep this
working, but it's fragile.
Since e8f4010611e7, geometry bounds are cached in the geometry
itself, which makes this object-level cache redundant. So, it's clearer
to build the `BoundBox` from those cached bounds and return it by
value, without interacting with the object's cached bounding box.
The code change is is mostly a move from `const BoundBox *` to
`std::optional<BoundBox>`. This is only one step of a larger change
described in #96968. Followup steps would include switching to
a simpler and smaller `Bounds` type, removing redundant object-
level access, and eventually removing `object.runtime.bb`.
Access of bounds from the object for mesh, curves, and point cloud
objects should now be thread-safe. Other object types still lazily
initialize the object `BoundBox` cache since they don't have
a data-level cache.
Pull Request: https://projects.blender.org/blender/blender/pulls/113465
2023-10-19 14:18:40 +02:00
|
|
|
BoundBox BKE_pointcloud_boundbox_get(struct Object *ob);
|
2020-03-17 14:41:48 +01:00
|
|
|
|
2022-09-15 20:14:31 +02:00
|
|
|
bool BKE_pointcloud_attribute_required(const struct PointCloud *pointcloud, const char *name);
|
2020-03-17 14:41:48 +01:00
|
|
|
|
|
|
|
/* Dependency Graph */
|
|
|
|
|
2023-06-12 20:21:56 +02:00
|
|
|
struct PointCloud *BKE_pointcloud_copy_for_eval(const struct PointCloud *pointcloud_src);
|
2020-03-17 14:41:48 +01:00
|
|
|
|
|
|
|
void BKE_pointcloud_data_update(struct Depsgraph *depsgraph,
|
|
|
|
struct Scene *scene,
|
|
|
|
struct Object *object);
|
|
|
|
|
|
|
|
/* Draw Cache */
|
|
|
|
|
|
|
|
enum {
|
|
|
|
BKE_POINTCLOUD_BATCH_DIRTY_ALL = 0,
|
|
|
|
};
|
|
|
|
|
2020-10-09 07:25:43 +02:00
|
|
|
void BKE_pointcloud_batch_cache_dirty_tag(struct PointCloud *pointcloud, int mode);
|
2020-03-17 14:41:48 +01:00
|
|
|
void BKE_pointcloud_batch_cache_free(struct PointCloud *pointcloud);
|
|
|
|
|
2020-10-09 07:25:43 +02:00
|
|
|
extern void (*BKE_pointcloud_batch_cache_dirty_tag_cb)(struct PointCloud *pointcloud, int mode);
|
2020-03-17 14:41:48 +01:00
|
|
|
extern void (*BKE_pointcloud_batch_cache_free_cb)(struct PointCloud *pointcloud);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|