2023-08-16 00:20:26 +10: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
|
|
|
|
2020-03-20 12:19:09 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2020-03-17 14:41:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_defaults.h"
|
2020-05-20 16:58:56 +02:00
|
|
|
#include "DNA_material_types.h"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_pointcloud_types.h"
|
|
|
|
|
|
2022-02-16 10:53:40 -06:00
|
|
|
#include "BLI_bounds.hh"
|
2021-12-29 18:39:08 -06:00
|
|
|
#include "BLI_index_range.hh"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BLI_listbase.h"
|
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 13:46:55 -06:00
|
|
|
#include "BLI_math_vector.hh"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BLI_rand.h"
|
2021-12-29 18:39:08 -06:00
|
|
|
#include "BLI_span.hh"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BLI_string.h"
|
2021-12-29 18:39:08 -06:00
|
|
|
#include "BLI_task.hh"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2022-05-29 11:02:10 +02:00
|
|
|
#include "BLI_vector.hh"
|
2020-03-17 14:41:48 +01:00
|
|
|
|
2020-04-03 13:07:36 +02:00
|
|
|
#include "BKE_anim_data.h"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BKE_customdata.h"
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "BKE_geometry_set.hh"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BKE_global.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_idtype.h"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BKE_lib_id.h"
|
|
|
|
|
#include "BKE_lib_query.h"
|
|
|
|
|
#include "BKE_lib_remap.h"
|
|
|
|
|
#include "BKE_main.h"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_wrapper.hh"
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BKE_modifier.h"
|
|
|
|
|
#include "BKE_object.h"
|
|
|
|
|
#include "BKE_pointcloud.h"
|
2020-10-09 07:25:43 +02:00
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
#include "BLT_translation.h"
|
|
|
|
|
|
|
|
|
|
#include "DEG_depsgraph_query.h"
|
|
|
|
|
|
2023-08-28 15:01:05 +02:00
|
|
|
#include "BLO_read_write.hh"
|
2020-09-11 11:20:57 +02:00
|
|
|
|
2021-12-29 18:39:08 -06:00
|
|
|
using blender::float3;
|
|
|
|
|
using blender::IndexRange;
|
|
|
|
|
using blender::Span;
|
2022-05-29 11:02:10 +02:00
|
|
|
using blender::Vector;
|
2021-12-29 18:39:08 -06:00
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
/* PointCloud datablock */
|
|
|
|
|
|
2020-05-20 16:45:47 +02:00
|
|
|
static void pointcloud_random(PointCloud *pointcloud);
|
2020-03-17 14:41:48 +01:00
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
const char *POINTCLOUD_ATTR_POSITION = "position";
|
|
|
|
|
const char *POINTCLOUD_ATTR_RADIUS = "radius";
|
2020-08-04 12:52:04 +02:00
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
static void pointcloud_init_data(ID *id)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = (PointCloud *)id;
|
|
|
|
|
BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(pointcloud, id));
|
|
|
|
|
|
|
|
|
|
MEMCPY_STRUCT_AFTER(pointcloud, DNA_struct_default_get(PointCloud), id);
|
|
|
|
|
|
|
|
|
|
CustomData_reset(&pointcloud->pdata);
|
2023-07-09 20:00:17 -04:00
|
|
|
pointcloud->attributes_for_write().add<float3>(
|
|
|
|
|
"position", ATTR_DOMAIN_POINT, blender::bke::AttributeInitConstruct());
|
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 13:46:55 -06:00
|
|
|
|
|
|
|
|
pointcloud->runtime = new blender::bke::PointCloudRuntime();
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-13 14:57:57 +02:00
|
|
|
static void pointcloud_copy_data(Main * /*bmain*/,
|
|
|
|
|
ID *id_dst,
|
|
|
|
|
const ID *id_src,
|
|
|
|
|
const int /*flag*/)
|
2020-03-17 14:41:48 +01:00
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud_dst = (PointCloud *)id_dst;
|
|
|
|
|
const PointCloud *pointcloud_src = (const PointCloud *)id_src;
|
2021-12-01 18:56:01 +01:00
|
|
|
pointcloud_dst->mat = static_cast<Material **>(MEM_dupallocN(pointcloud_src->mat));
|
2020-03-17 14:41:48 +01:00
|
|
|
|
2023-04-13 14:57:57 +02:00
|
|
|
CustomData_copy(
|
|
|
|
|
&pointcloud_src->pdata, &pointcloud_dst->pdata, CD_MASK_ALL, pointcloud_dst->totpoint);
|
2020-12-02 13:25:25 +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 13:46:55 -06:00
|
|
|
pointcloud_dst->runtime = new blender::bke::PointCloudRuntime();
|
|
|
|
|
pointcloud_dst->runtime->bounds_cache = pointcloud_src->runtime->bounds_cache;
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
pointcloud_dst->batch_cache = nullptr;
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pointcloud_free_data(ID *id)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = (PointCloud *)id;
|
|
|
|
|
BKE_animdata_free(&pointcloud->id, false);
|
|
|
|
|
BKE_pointcloud_batch_cache_free(pointcloud);
|
|
|
|
|
CustomData_free(&pointcloud->pdata, pointcloud->totpoint);
|
|
|
|
|
MEM_SAFE_FREE(pointcloud->mat);
|
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 13:46:55 -06:00
|
|
|
delete pointcloud->runtime;
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-20 16:58:56 +02:00
|
|
|
static void pointcloud_foreach_id(ID *id, LibraryForeachIDData *data)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = (PointCloud *)id;
|
|
|
|
|
for (int i = 0; i < pointcloud->totcol; i++) {
|
2021-10-26 10:40:36 +02:00
|
|
|
BKE_LIB_FOREACHID_PROCESS_IDSUPER(data, pointcloud->mat[i], IDWALK_CB_USER);
|
2020-05-20 16:58:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 11:20:57 +02:00
|
|
|
static void pointcloud_blend_write(BlendWriter *writer, ID *id, const void *id_address)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = (PointCloud *)id;
|
2021-08-19 11:13:55 +02:00
|
|
|
|
2022-05-29 11:02:10 +02:00
|
|
|
Vector<CustomDataLayer, 16> point_layers;
|
|
|
|
|
CustomData_blend_write_prepare(pointcloud->pdata, point_layers);
|
2021-08-19 11:13:55 +02:00
|
|
|
|
|
|
|
|
/* Write LibData */
|
|
|
|
|
BLO_write_id_struct(writer, PointCloud, id_address, &pointcloud->id);
|
|
|
|
|
BKE_id_blend_write(writer, &pointcloud->id);
|
|
|
|
|
|
|
|
|
|
/* Direct data */
|
2022-05-29 11:02:10 +02:00
|
|
|
CustomData_blend_write(writer,
|
|
|
|
|
&pointcloud->pdata,
|
|
|
|
|
point_layers,
|
|
|
|
|
pointcloud->totpoint,
|
|
|
|
|
CD_MASK_ALL,
|
|
|
|
|
&pointcloud->id);
|
2021-08-19 11:13:55 +02:00
|
|
|
|
|
|
|
|
BLO_write_pointer_array(writer, pointcloud->totcol, pointcloud->mat);
|
2020-09-11 11:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pointcloud_blend_read_data(BlendDataReader *reader, ID *id)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = (PointCloud *)id;
|
|
|
|
|
|
|
|
|
|
/* Geometry */
|
|
|
|
|
CustomData_blend_read(reader, &pointcloud->pdata, pointcloud->totpoint);
|
|
|
|
|
|
|
|
|
|
/* Materials */
|
|
|
|
|
BLO_read_pointer_array(reader, (void **)&pointcloud->mat);
|
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 13:46:55 -06:00
|
|
|
|
|
|
|
|
pointcloud->runtime = new blender::bke::PointCloudRuntime();
|
2020-09-11 11:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
IDTypeInfo IDType_ID_PT = {
|
2023-01-16 12:41:11 +11:00
|
|
|
/*id_code*/ ID_PT,
|
|
|
|
|
/*id_filter*/ FILTER_ID_PT,
|
|
|
|
|
/*main_listbase_index*/ INDEX_ID_PT,
|
|
|
|
|
/*struct_size*/ sizeof(PointCloud),
|
|
|
|
|
/*name*/ "PointCloud",
|
|
|
|
|
/*name_plural*/ "pointclouds",
|
|
|
|
|
/*translation_context*/ BLT_I18NCONTEXT_ID_POINTCLOUD,
|
|
|
|
|
/*flags*/ IDTYPE_FLAGS_APPEND_IS_REUSABLE,
|
|
|
|
|
/*asset_type_info*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*init_data*/ pointcloud_init_data,
|
|
|
|
|
/*copy_data*/ pointcloud_copy_data,
|
|
|
|
|
/*free_data*/ pointcloud_free_data,
|
|
|
|
|
/*make_local*/ nullptr,
|
|
|
|
|
/*foreach_id*/ pointcloud_foreach_id,
|
|
|
|
|
/*foreach_cache*/ nullptr,
|
|
|
|
|
/*foreach_path*/ nullptr,
|
|
|
|
|
/*owner_pointer_get*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*blend_write*/ pointcloud_blend_write,
|
|
|
|
|
/*blend_read_data*/ pointcloud_blend_read_data,
|
2023-03-11 18:07:59 +01:00
|
|
|
/*blend_read_after_liblink*/ nullptr,
|
2023-01-16 12:41:11 +11:00
|
|
|
|
|
|
|
|
/*blend_read_undo_preserve*/ nullptr,
|
|
|
|
|
|
|
|
|
|
/*lib_override_apply_post*/ nullptr,
|
2020-03-17 14:41:48 +01:00
|
|
|
};
|
|
|
|
|
|
2020-05-20 16:45:47 +02:00
|
|
|
static void pointcloud_random(PointCloud *pointcloud)
|
|
|
|
|
{
|
2022-09-12 11:35:33 -05:00
|
|
|
BLI_assert(pointcloud->totpoint == 0);
|
2020-05-20 16:45:47 +02:00
|
|
|
pointcloud->totpoint = 400;
|
2022-09-12 11:35:33 -05:00
|
|
|
CustomData_realloc(&pointcloud->pdata, 0, pointcloud->totpoint);
|
2020-05-20 16:45:47 +02:00
|
|
|
|
|
|
|
|
RNG *rng = BLI_rng_new(0);
|
|
|
|
|
|
2022-09-07 21:41:39 -05:00
|
|
|
blender::bke::MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
|
2023-04-13 11:55:32 -04:00
|
|
|
blender::MutableSpan<float3> positions = pointcloud->positions_for_write();
|
2022-07-19 18:06:56 -05:00
|
|
|
blender::bke::SpanAttributeWriter<float> radii =
|
|
|
|
|
attributes.lookup_or_add_for_write_only_span<float>(POINTCLOUD_ATTR_RADIUS,
|
|
|
|
|
ATTR_DOMAIN_POINT);
|
|
|
|
|
|
2023-04-13 11:55:32 -04:00
|
|
|
for (const int i : positions.index_range()) {
|
|
|
|
|
positions[i] = float3(BLI_rng_get_float(rng), BLI_rng_get_float(rng), BLI_rng_get_float(rng)) *
|
|
|
|
|
2.0f -
|
|
|
|
|
1.0f;
|
2022-07-19 18:06:56 -05:00
|
|
|
radii.span[i] = 0.05f * BLI_rng_get_float(rng);
|
2020-05-20 16:45:47 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 18:06:56 -05:00
|
|
|
radii.finish();
|
|
|
|
|
|
2020-05-20 16:45:47 +02:00
|
|
|
BLI_rng_free(rng);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *BKE_pointcloud_add(Main *bmain, const char *name)
|
|
|
|
|
{
|
2020-11-09 15:47:16 +01:00
|
|
|
PointCloud *pointcloud = static_cast<PointCloud *>(BKE_id_new(bmain, ID_PT, name));
|
2020-05-20 16:45:47 +02:00
|
|
|
|
|
|
|
|
return pointcloud;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 18:49:40 +02:00
|
|
|
void *BKE_pointcloud_add_default(Main *bmain, const char *name)
|
|
|
|
|
{
|
2020-11-09 15:47:16 +01:00
|
|
|
PointCloud *pointcloud = static_cast<PointCloud *>(BKE_libblock_alloc(bmain, ID_PT, name, 0));
|
2020-04-09 18:49:40 +02:00
|
|
|
|
|
|
|
|
pointcloud_init_data(&pointcloud->id);
|
|
|
|
|
pointcloud_random(pointcloud);
|
|
|
|
|
|
|
|
|
|
return pointcloud;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
PointCloud *BKE_pointcloud_new_nomain(const int totpoint)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = static_cast<PointCloud *>(BKE_libblock_alloc(
|
|
|
|
|
nullptr, ID_PT, BKE_idtype_idcode_to_name(ID_PT), LIB_ID_CREATE_LOCALIZE));
|
|
|
|
|
|
|
|
|
|
pointcloud_init_data(&pointcloud->id);
|
|
|
|
|
|
2022-09-12 11:35:33 -05:00
|
|
|
CustomData_realloc(&pointcloud->pdata, 0, totpoint);
|
2020-12-02 13:25:25 +01:00
|
|
|
pointcloud->totpoint = totpoint;
|
|
|
|
|
|
|
|
|
|
return pointcloud;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 15:52:06 -04:00
|
|
|
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
|
2022-09-15 15:21:25 +02:00
|
|
|
{
|
|
|
|
|
BLI_assert(pointcloud_src->id.tag & LIB_TAG_NO_MAIN);
|
|
|
|
|
|
|
|
|
|
CustomData_free(&pointcloud_dst->pdata, pointcloud_dst->totpoint);
|
|
|
|
|
|
|
|
|
|
const int totpoint = pointcloud_dst->totpoint = pointcloud_src->totpoint;
|
2023-04-13 14:57:57 +02:00
|
|
|
CustomData_copy(&pointcloud_src->pdata, &pointcloud_dst->pdata, CD_MASK_ALL, totpoint);
|
2022-09-15 15:21:25 +02:00
|
|
|
|
2023-04-19 15:52:06 -04:00
|
|
|
BKE_id_free(nullptr, pointcloud_src);
|
2022-09-15 15:21:25 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 08:08:18 -04:00
|
|
|
std::optional<blender::Bounds<blender::float3>> PointCloud::bounds_min_max() const
|
2020-12-02 13:25:25 +01:00
|
|
|
{
|
2022-02-16 10:53:40 -06:00
|
|
|
using namespace blender;
|
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 13:46:55 -06:00
|
|
|
using namespace blender::bke;
|
|
|
|
|
if (this->totpoint == 0) {
|
2023-06-16 08:08:18 -04:00
|
|
|
return std::nullopt;
|
2021-12-29 12:27:24 -03: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 13:46:55 -06:00
|
|
|
this->runtime->bounds_cache.ensure([&](Bounds<float3> &r_bounds) {
|
|
|
|
|
const AttributeAccessor attributes = this->attributes();
|
2023-04-19 11:21:06 +02:00
|
|
|
const Span<float3> positions = this->positions();
|
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 13:46:55 -06:00
|
|
|
if (attributes.contains(POINTCLOUD_ATTR_RADIUS)) {
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArraySpan radii = *attributes.lookup<float>(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 13:46:55 -06:00
|
|
|
r_bounds = *bounds::min_max_with_radii(positions, radii);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
r_bounds = *bounds::min_max(positions);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-06-16 08:08:18 -04:00
|
|
|
return this->runtime->bounds_cache.data();
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
BoundBox *BKE_pointcloud_boundbox_get(Object *ob)
|
|
|
|
|
{
|
2023-06-16 08:08:18 -04:00
|
|
|
using namespace blender;
|
2020-03-17 14:41:48 +01:00
|
|
|
BLI_assert(ob->type == OB_POINTCLOUD);
|
2020-11-09 15:47:16 +01:00
|
|
|
if (ob->runtime.bb != nullptr && (ob->runtime.bb->flag & BOUNDBOX_DIRTY) == 0) {
|
2020-03-17 14:41:48 +01:00
|
|
|
return ob->runtime.bb;
|
|
|
|
|
}
|
2020-11-09 15:47:16 +01:00
|
|
|
if (ob->runtime.bb == nullptr) {
|
2023-06-16 08:08:18 -04:00
|
|
|
ob->runtime.bb = MEM_cnew<BoundBox>(__func__);
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
2020-03-17 14:41:48 +01:00
|
|
|
|
2023-06-16 08:08:18 -04:00
|
|
|
std::optional<Bounds<float3>> bounds;
|
|
|
|
|
if (ob->runtime.geometry_set_eval) {
|
|
|
|
|
bounds = ob->runtime.geometry_set_eval->compute_boundbox_without_instances();
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const PointCloud *pointcloud = static_cast<PointCloud *>(ob->data);
|
2023-06-16 08:08:18 -04:00
|
|
|
bounds = pointcloud->bounds_min_max();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bounds) {
|
|
|
|
|
BKE_boundbox_init_from_minmax(ob->runtime.bb, bounds->min, bounds->max);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BKE_boundbox_init_from_minmax(ob->runtime.bb, float3(-1), float3(1));
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ob->runtime.bb;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05:00
|
|
|
bool BKE_pointcloud_attribute_required(const PointCloud * /*pointcloud*/, const char *name)
|
2020-08-04 12:52:04 +02:00
|
|
|
{
|
2022-06-08 10:42:21 +02:00
|
|
|
return STREQ(name, POINTCLOUD_ATTR_POSITION);
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Dependency Graph */
|
|
|
|
|
|
2023-06-12 14:21:56 -04:00
|
|
|
PointCloud *BKE_pointcloud_copy_for_eval(const PointCloud *pointcloud_src)
|
2020-03-17 14:41:48 +01:00
|
|
|
{
|
2023-04-19 15:49:13 -04:00
|
|
|
return reinterpret_cast<PointCloud *>(
|
|
|
|
|
BKE_id_copy_ex(nullptr, &pointcloud_src->id, nullptr, LIB_ID_COPY_LOCALIZE));
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
static void pointcloud_evaluate_modifiers(Depsgraph *depsgraph,
|
|
|
|
|
Scene *scene,
|
2020-12-02 13:25:25 +01:00
|
|
|
Object *object,
|
2023-06-15 22:18:28 +02:00
|
|
|
blender::bke::GeometrySet &geometry_set)
|
2020-03-17 14:41:48 +01:00
|
|
|
{
|
2020-04-21 13:09:41 +02:00
|
|
|
/* Modifier evaluation modes. */
|
|
|
|
|
const bool use_render = (DEG_get_mode(depsgraph) == DAG_EVAL_RENDER);
|
|
|
|
|
const int required_mode = use_render ? eModifierMode_Render : eModifierMode_Realtime;
|
|
|
|
|
ModifierApplyFlag apply_flag = use_render ? MOD_APPLY_RENDER : MOD_APPLY_USECACHE;
|
|
|
|
|
const ModifierEvalContext mectx = {depsgraph, object, apply_flag};
|
|
|
|
|
|
2022-07-26 13:01:30 +02:00
|
|
|
BKE_modifiers_clear_errors(object);
|
|
|
|
|
|
2020-04-21 13:09:41 +02:00
|
|
|
/* Get effective list of modifiers to execute. Some effects like shape keys
|
|
|
|
|
* are added as virtual modifiers before the user created modifiers. */
|
2023-07-27 12:04:18 +10:00
|
|
|
VirtualModifierData virtual_modifier_data;
|
|
|
|
|
ModifierData *md = BKE_modifiers_get_virtual_modifierlist(object, &virtual_modifier_data);
|
2020-04-21 13:09:41 +02:00
|
|
|
|
|
|
|
|
/* Evaluate modifiers. */
|
|
|
|
|
for (; md; md = md->next) {
|
2020-11-09 15:47:16 +01:00
|
|
|
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)md->type);
|
2020-04-21 13:09:41 +02:00
|
|
|
|
2020-05-08 10:14:02 +02:00
|
|
|
if (!BKE_modifier_is_enabled(scene, md, required_mode)) {
|
2020-04-21 13:09:41 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
Modifiers: measure execution time and provide Python access
The goal is to give technical artists the ability to optimize modifier usage
and/or geometry node groups for performance. In the long term, it
would be useful if Blender could provide its own UI to display profiling
information to users. However, right now, there are too many open
design questions making it infeasible to tackle this in the short term.
This commit uses a simpler approach: Instead of adding new ui for
profiling data, it exposes the execution-time of modifiers in the Python
API. This allows technical artists to access the information and to build
their own UI to display the relevant information. In the long term this
will hopefully also help us to integrate a native ui for this in Blender
by observing how users use this information.
Note: The execution time of a modifier highly depends on what other
things the CPU is doing at the same time. For example, in many more
complex files, many objects and therefore modifiers are evaluated at
the same time by multiple threads which makes the measurement
much less reliable. For best results, make sure that only one object
is evaluated at a time (e.g. by changing it in isolation) and that no
other process on the system keeps the CPU busy.
As shown below, the execution time has to be accessed on the
evaluated object, not the original object.
```lang=python
import bpy
depsgraph = bpy.context.view_layer.depsgraph
ob = bpy.context.active_object
ob_eval = ob.evaluated_get(depsgraph)
modifier_eval = ob_eval.modifiers[0]
print(modifier_eval.execution_time, "s")
```
Differential Revision: https://developer.blender.org/D17185
2023-02-06 15:39:59 +01:00
|
|
|
blender::bke::ScopedModifierTimer modifier_timer{*md};
|
|
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
if (mti->modify_geometry_set) {
|
|
|
|
|
mti->modify_geometry_set(md, &mectx, &geometry_set);
|
2020-04-21 13:09:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-15 22:18:28 +02:00
|
|
|
static PointCloud *take_pointcloud_ownership_from_geometry_set(
|
|
|
|
|
blender::bke::GeometrySet &geometry_set)
|
2020-12-15 12:42:10 +01:00
|
|
|
{
|
2023-06-15 22:18:28 +02:00
|
|
|
if (!geometry_set.has<blender::bke::PointCloudComponent>()) {
|
2020-12-15 12:42:10 +01:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
2023-06-15 22:18:28 +02:00
|
|
|
blender::bke::PointCloudComponent &pointcloud_component =
|
|
|
|
|
geometry_set.get_component_for_write<blender::bke::PointCloudComponent>();
|
2020-12-15 12:42:10 +01:00
|
|
|
PointCloud *pointcloud = pointcloud_component.release();
|
|
|
|
|
if (pointcloud != nullptr) {
|
|
|
|
|
/* Add back, but as read-only non-owning component. */
|
2023-06-15 22:18:28 +02:00
|
|
|
pointcloud_component.replace(pointcloud, blender::bke::GeometryOwnershipType::ReadOnly);
|
2020-12-15 12:42:10 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* The component was empty, we can also remove it. */
|
2023-06-15 22:18:28 +02:00
|
|
|
geometry_set.remove<blender::bke::PointCloudComponent>();
|
2020-12-15 12:42:10 +01:00
|
|
|
}
|
|
|
|
|
return pointcloud;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_pointcloud_data_update(Depsgraph *depsgraph, Scene *scene, Object *object)
|
2020-03-17 14:41:48 +01:00
|
|
|
{
|
|
|
|
|
/* Free any evaluated data and restore original data. */
|
|
|
|
|
BKE_object_free_derived_caches(object);
|
|
|
|
|
|
|
|
|
|
/* Evaluate modifiers. */
|
2020-11-09 15:47:16 +01:00
|
|
|
PointCloud *pointcloud = static_cast<PointCloud *>(object->data);
|
2023-08-03 17:09:18 +02:00
|
|
|
blender::bke::GeometrySet geometry_set = blender::bke::GeometrySet::from_pointcloud(
|
2023-06-15 22:18:28 +02:00
|
|
|
pointcloud, blender::bke::GeometryOwnershipType::ReadOnly);
|
2020-12-02 13:25:25 +01:00
|
|
|
pointcloud_evaluate_modifiers(depsgraph, scene, object, geometry_set);
|
2020-03-17 14:41:48 +01:00
|
|
|
|
2020-12-15 12:42:10 +01:00
|
|
|
PointCloud *pointcloud_eval = take_pointcloud_ownership_from_geometry_set(geometry_set);
|
|
|
|
|
|
|
|
|
|
/* If the geometry set did not contain a point cloud, we still create an empty one. */
|
|
|
|
|
if (pointcloud_eval == nullptr) {
|
|
|
|
|
pointcloud_eval = BKE_pointcloud_new_nomain(0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
/* Assign evaluated object. */
|
2020-12-15 12:42:10 +01:00
|
|
|
const bool eval_is_owned = pointcloud_eval != pointcloud;
|
|
|
|
|
BKE_object_eval_assign_data(object, &pointcloud_eval->id, eval_is_owned);
|
2023-06-15 22:18:28 +02:00
|
|
|
object->runtime.geometry_set_eval = new blender::bke::GeometrySet(std::move(geometry_set));
|
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 13:46:55 -06:00
|
|
|
void PointCloud::tag_positions_changed()
|
|
|
|
|
{
|
|
|
|
|
this->runtime->bounds_cache.tag_dirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PointCloud::tag_radii_changed()
|
|
|
|
|
{
|
|
|
|
|
this->runtime->bounds_cache.tag_dirty();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
/* Draw Cache */
|
2021-12-07 17:19:15 +11:00
|
|
|
|
2020-11-09 15:47:16 +01:00
|
|
|
void (*BKE_pointcloud_batch_cache_dirty_tag_cb)(PointCloud *pointcloud, int mode) = nullptr;
|
|
|
|
|
void (*BKE_pointcloud_batch_cache_free_cb)(PointCloud *pointcloud) = nullptr;
|
2020-03-17 14:41:48 +01:00
|
|
|
|
2020-10-09 07:25:43 +02:00
|
|
|
void BKE_pointcloud_batch_cache_dirty_tag(PointCloud *pointcloud, int mode)
|
2020-03-17 14:41:48 +01:00
|
|
|
{
|
|
|
|
|
if (pointcloud->batch_cache) {
|
|
|
|
|
BKE_pointcloud_batch_cache_dirty_tag_cb(pointcloud, mode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_pointcloud_batch_cache_free(PointCloud *pointcloud)
|
|
|
|
|
{
|
|
|
|
|
if (pointcloud->batch_cache) {
|
|
|
|
|
BKE_pointcloud_batch_cache_free_cb(pointcloud);
|
|
|
|
|
}
|
|
|
|
|
}
|