2022-02-11 09:07:11 +11: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"
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "BKE_mesh_wrapper.h"
|
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"
|
|
|
|
|
|
2020-09-11 11:20:57 +02:00
|
|
|
#include "BLO_read_write.h"
|
|
|
|
|
|
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);
|
2020-08-04 12:52:04 +02:00
|
|
|
CustomData_add_layer_named(&pointcloud->pdata,
|
|
|
|
|
CD_PROP_FLOAT3,
|
Attributes: Improve custom data initialization options
When allocating new `CustomData` layers, often we do redundant
initialization of arrays. For example, it's common that values are
allocated, set to their default value, and then set to some other
value. This is wasteful, and it negates the benefits of optimizations
to the allocator like D15082. There are two reasons for this. The
first is array-of-structs storage that makes it annoying to initialize
values manually, and the second is confusing options in the Custom Data
API. This patch addresses the latter.
The `CustomData` "alloc type" options are rearranged. Now, besides
the options that use existing layers, there are two remaining:
* `CD_SET_DEFAULT` sets the default value.
* Usually zeroes, but for colors this is white (how it was before).
* Should be used when you add the layer but don't set all values.
* `CD_CONSTRUCT` refers to the "default construct" C++ term.
* Only necessary or defined for non-trivial types like vertex groups.
* Doesn't do anything for trivial types like `int` or `float3`.
* Should be used every other time, when all values will be set.
The attribute API's `AttributeInit` types are updated as well.
To update code, replace `CD_CALLOC` with `CD_SET_DEFAULT` and
`CD_DEFAULT` with `CD_CONSTRUCT`. This doesn't cause any functional
changes yet. Follow-up commits will change to avoid initializing
new layers where the correctness is clear.
Differential Revision: https://developer.blender.org/D15617
2022-08-30 14:54:53 -05:00
|
|
|
CD_SET_DEFAULT,
|
2020-11-09 15:47:16 +01:00
|
|
|
nullptr,
|
2020-08-04 12:52:04 +02:00
|
|
|
pointcloud->totpoint,
|
|
|
|
|
POINTCLOUD_ATTR_POSITION);
|
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
|
|
|
}
|
|
|
|
|
|
2022-10-03 17:37:25 -05: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
|
|
|
|
|
|
|
|
const eCDAllocType alloc_type = (flag & LIB_ID_COPY_CD_REFERENCE) ? CD_REFERENCE : CD_DUPLICATE;
|
|
|
|
|
CustomData_copy(&pointcloud_src->pdata,
|
|
|
|
|
&pointcloud_dst->pdata,
|
|
|
|
|
CD_MASK_ALL,
|
|
|
|
|
alloc_type,
|
|
|
|
|
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);
|
|
|
|
|
if (pointcloud->adt) {
|
|
|
|
|
BKE_animdata_blend_write(writer, pointcloud->adt);
|
|
|
|
|
}
|
2020-09-11 11:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pointcloud_blend_read_data(BlendDataReader *reader, ID *id)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = (PointCloud *)id;
|
|
|
|
|
BLO_read_data_address(reader, &pointcloud->adt);
|
|
|
|
|
BKE_animdata_blend_read_data(reader, pointcloud->adt);
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pointcloud_blend_read_lib(BlendLibReader *reader, ID *id)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = (PointCloud *)id;
|
|
|
|
|
for (int a = 0; a < pointcloud->totcol; a++) {
|
|
|
|
|
BLO_read_id_address(reader, pointcloud->id.lib, &pointcloud->mat[a]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void pointcloud_blend_read_expand(BlendExpander *expander, ID *id)
|
|
|
|
|
{
|
|
|
|
|
PointCloud *pointcloud = (PointCloud *)id;
|
|
|
|
|
for (int a = 0; a < pointcloud->totcol; a++) {
|
|
|
|
|
BLO_expand(expander, pointcloud->mat[a]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
IDTypeInfo IDType_ID_PT = {
|
2020-11-09 15:47:16 +01: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,
|
2021-09-17 16:22:29 +02:00
|
|
|
/* flags */ IDTYPE_FLAGS_APPEND_IS_REUSABLE,
|
2021-11-24 10:32:28 +01:00
|
|
|
/* asset_type_info */ nullptr,
|
2020-11-09 15:47:16 +01:00
|
|
|
|
|
|
|
|
/* 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,
|
Refactor BKE_bpath module.
The main goal of this refactor is to make BPath module use `IDTypeInfo`,
and move each ID-specific part of the `foreach_path` looper into their
own IDTypeInfo struct, using a new `foreach_path` callback.
Additionally, following improvements/cleanups are included:
* Attempt to get better, more consistent namings.
** In particular, move from `path_visitor` to more standard `foreach_path`.
* Update and extend documentation.
** API doc was moved to header, according to recent discussions on this
topic.
* Remove `BKE_bpath_relocate_visitor` from API, this is specific
callback that belongs in `lib_id.c` user code.
NOTE: This commit is expected to be 100% non-behavioral-change. This
implies that several potential further changes were only noted as
comments (like using a more generic solution for
`lib_id_library_local_paths`, addressing inconsistencies like path of
packed libraries always being skipped, regardless of the
`BKE_BPATH_FOREACH_PATH_SKIP_PACKED` `eBPathForeachFlag` flag value,
etc.).
NOTE: basic unittests were added to master already in
rBdcc500e5a265093bc9cc.
Reviewed By: brecht
Differential Revision: https://developer.blender.org/D13381
2021-11-29 14:20:58 +01:00
|
|
|
/* foreach_path */ nullptr,
|
2022-09-08 16:32:35 +02:00
|
|
|
/* owner_pointer_get */ nullptr,
|
2020-11-09 15:47:16 +01:00
|
|
|
|
|
|
|
|
/* blend_write */ pointcloud_blend_write,
|
|
|
|
|
/* blend_read_data */ pointcloud_blend_read_data,
|
|
|
|
|
/* blend_read_lib */ pointcloud_blend_read_lib,
|
|
|
|
|
/* blend_read_expand */ pointcloud_blend_read_expand,
|
|
|
|
|
|
|
|
|
|
/* blend_read_undo_preserve */ nullptr,
|
2021-01-22 14:52:50 +01:00
|
|
|
|
|
|
|
|
/* 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();
|
2022-07-19 18:06:56 -05:00
|
|
|
blender::bke::SpanAttributeWriter positions =
|
|
|
|
|
attributes.lookup_or_add_for_write_only_span<float3>(POINTCLOUD_ATTR_POSITION,
|
|
|
|
|
ATTR_DOMAIN_POINT);
|
|
|
|
|
blender::bke::SpanAttributeWriter<float> radii =
|
|
|
|
|
attributes.lookup_or_add_for_write_only_span<float>(POINTCLOUD_ATTR_RADIUS,
|
|
|
|
|
ATTR_DOMAIN_POINT);
|
|
|
|
|
|
|
|
|
|
for (const int i : positions.span.index_range()) {
|
|
|
|
|
positions.span[i] =
|
|
|
|
|
float3(BLI_rng_get_float(rng), BLI_rng_get_float(rng), BLI_rng_get_float(rng)) * 2.0f -
|
|
|
|
|
1.0f;
|
|
|
|
|
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
|
|
|
positions.finish();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 15:21:25 +02:00
|
|
|
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src,
|
|
|
|
|
PointCloud *pointcloud_dst,
|
|
|
|
|
bool take_ownership)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(pointcloud_src->id.tag & LIB_TAG_NO_MAIN);
|
|
|
|
|
|
|
|
|
|
eCDAllocType alloctype = CD_DUPLICATE;
|
|
|
|
|
|
|
|
|
|
if (take_ownership) {
|
|
|
|
|
bool has_any_referenced_layers = CustomData_has_referenced(&pointcloud_src->pdata);
|
|
|
|
|
|
|
|
|
|
if (!has_any_referenced_layers) {
|
|
|
|
|
alloctype = CD_ASSIGN;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CustomData_free(&pointcloud_dst->pdata, pointcloud_dst->totpoint);
|
|
|
|
|
|
|
|
|
|
const int totpoint = pointcloud_dst->totpoint = pointcloud_src->totpoint;
|
|
|
|
|
CustomData_copy(
|
|
|
|
|
&pointcloud_src->pdata, &pointcloud_dst->pdata, CD_MASK_ALL, alloctype, totpoint);
|
|
|
|
|
|
|
|
|
|
if (take_ownership) {
|
|
|
|
|
if (alloctype == CD_ASSIGN) {
|
|
|
|
|
/* Free the CustomData but keep the layers. */
|
|
|
|
|
CustomData_free_typemask(&pointcloud_src->pdata, pointcloud_src->totpoint, 0);
|
|
|
|
|
}
|
|
|
|
|
BKE_id_free(nullptr, pointcloud_src);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
bool PointCloud::bounds_min_max(blender::float3 &min, blender::float3 &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) {
|
2021-12-29 12:27:24 -03:00
|
|
|
return false;
|
|
|
|
|
}
|
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();
|
|
|
|
|
const VArraySpan<float3> positions = attributes.lookup<float3>(POINTCLOUD_ATTR_POSITION);
|
|
|
|
|
if (attributes.contains(POINTCLOUD_ATTR_RADIUS)) {
|
|
|
|
|
const VArraySpan<float> radii = attributes.lookup<float>(POINTCLOUD_ATTR_RADIUS);
|
|
|
|
|
r_bounds = *bounds::min_max_with_radii(positions, radii);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
r_bounds = *bounds::min_max(positions);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const Bounds<float3> &bounds = this->runtime->bounds_cache.data();
|
|
|
|
|
min = math::min(bounds.min, min);
|
|
|
|
|
max = math::max(bounds.max, max);
|
2021-12-29 12:27:24 -03:00
|
|
|
return true;
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
BoundBox *BKE_pointcloud_boundbox_get(Object *ob)
|
|
|
|
|
{
|
|
|
|
|
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) {
|
|
|
|
|
ob->runtime.bb = static_cast<BoundBox *>(MEM_callocN(sizeof(BoundBox), "pointcloud boundbox"));
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
2020-03-17 14:41:48 +01:00
|
|
|
|
BLI: Refactor vector types & functions to use templates
This patch implements the vector types (i.e:`float2`) by making heavy
usage of templating. All vector functions are now outside of the vector
classes (inside the `blender::math` namespace) and are not vector size
dependent for the most part.
In the ongoing effort to make shaders less GL centric, we are aiming
to share more code between GLSL and C++ to avoid code duplication.
####Motivations:
- We are aiming to share UBO and SSBO structures between GLSL and C++.
This means we will use many of the existing vector types and others
we currently don't have (uintX, intX). All these variations were
asking for many more code duplication.
- Deduplicate existing code which is duplicated for each vector size.
- We also want to share small functions. Which means that vector
functions should be static and not in the class namespace.
- Reduce friction to use these types in new projects due to their
incompleteness.
- The current state of the `BLI_(float|double|mpq)(2|3|4).hh` is a
bit of a let down. Most clases are incomplete, out of sync with each
others with different codestyles, and some functions that should be
static are not (i.e: `float3::reflect()`).
####Upsides:
- Still support `.x, .y, .z, .w` for readability.
- Compact, readable and easilly extendable.
- All of the vector functions are available for all the vectors types
and can be restricted to certain types. Also template specialization
let us define exception for special class (like mpq).
- With optimization ON, the compiler unroll the loops and performance
is the same.
####Downsides:
- Might impact debugability. Though I would arge that the bugs are
rarelly caused by the vector class itself (since the operations are
quite trivial) but by the type conversions.
- Might impact compile time. I did not saw a significant impact since
the usage is not really widespread.
- Functions needs to be rewritten to support arbitrary vector length.
For instance, one can't call `len_squared_v3v3` in
`math::length_squared()` and call it a day.
- Type cast does not work with the template version of the `math::`
vector functions. Meaning you need to manually cast `float *` and
`(float *)[3]` to `float3` for the function calls.
i.e: `math::distance_squared(float3(nearest.co), positions[i]);`
- Some parts might loose in readability:
`float3::dot(v1.normalized(), v2.normalized())`
becoming
`math::dot(math::normalize(v1), math::normalize(v2))`
But I propose, when appropriate, to use
`using namespace blender::math;` on function local or file scope to
increase readability.
`dot(normalize(v1), normalize(v2))`
####Consideration:
- Include back `.length()` method. It is quite handy and is more C++
oriented.
- I considered the GLM library as a candidate for replacement. It felt
like too much for what we need and would be difficult to extend / modify
to our needs.
- I used Macros to reduce code in operators declaration and potential
copy paste bugs. This could reduce debugability and could be reverted.
- This touches `delaunay_2d.cc` and the intersection code. I would like
to know @howardt opinion on the matter.
- The `noexcept` on the copy constructor of `mpq(2|3)` is being removed.
But according to @JacquesLucke it is not a real problem for now.
I would like to give a huge thanks to @JacquesLucke who helped during this
and pushed me to reduce the duplication further.
Reviewed By: brecht, sergey, JacquesLucke
Differential Revision: https://developer.blender.org/D13791
2022-01-12 12:57:07 +01:00
|
|
|
float3 min, max;
|
2020-12-02 13:25:25 +01:00
|
|
|
INIT_MINMAX(min, max);
|
|
|
|
|
if (ob->runtime.geometry_set_eval != nullptr) {
|
|
|
|
|
ob->runtime.geometry_set_eval->compute_boundbox_without_instances(&min, &max);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const PointCloud *pointcloud = static_cast<PointCloud *>(ob->data);
|
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->bounds_min_max(min, max);
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
2020-12-02 13:25:25 +01:00
|
|
|
BKE_boundbox_init_from_minmax(ob->runtime.bb, min, max);
|
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 */
|
|
|
|
|
|
|
|
|
|
PointCloud *BKE_pointcloud_copy_for_eval(struct PointCloud *pointcloud_src, bool reference)
|
|
|
|
|
{
|
|
|
|
|
int flags = LIB_ID_COPY_LOCALIZE;
|
|
|
|
|
|
|
|
|
|
if (reference) {
|
|
|
|
|
flags |= LIB_ID_COPY_CD_REFERENCE;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 15:47:16 +01:00
|
|
|
PointCloud *result = (PointCloud *)BKE_id_copy_ex(nullptr, &pointcloud_src->id, nullptr, flags);
|
2020-03-17 14:41:48 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
static void pointcloud_evaluate_modifiers(struct Depsgraph *depsgraph,
|
|
|
|
|
struct Scene *scene,
|
|
|
|
|
Object *object,
|
|
|
|
|
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. */
|
|
|
|
|
VirtualModifierData virtualModifierData;
|
2020-05-08 10:14:02 +02:00
|
|
|
ModifierData *md = BKE_modifiers_get_virtual_modifierlist(object, &virtualModifierData);
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-10 14:35:02 +01:00
|
|
|
if (mti->modifyGeometrySet) {
|
|
|
|
|
mti->modifyGeometrySet(md, &mectx, &geometry_set);
|
2020-04-21 13:09:41 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-17 14:41:48 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-15 12:42:10 +01:00
|
|
|
static PointCloud *take_pointcloud_ownership_from_geometry_set(GeometrySet &geometry_set)
|
|
|
|
|
{
|
|
|
|
|
if (!geometry_set.has<PointCloudComponent>()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
PointCloudComponent &pointcloud_component =
|
|
|
|
|
geometry_set.get_component_for_write<PointCloudComponent>();
|
|
|
|
|
PointCloud *pointcloud = pointcloud_component.release();
|
|
|
|
|
if (pointcloud != nullptr) {
|
|
|
|
|
/* Add back, but as read-only non-owning component. */
|
|
|
|
|
pointcloud_component.replace(pointcloud, GeometryOwnershipType::ReadOnly);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* The component was empty, we can also remove it. */
|
|
|
|
|
geometry_set.remove<PointCloudComponent>();
|
|
|
|
|
}
|
|
|
|
|
return pointcloud;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-17 14:41:48 +01:00
|
|
|
void BKE_pointcloud_data_update(struct Depsgraph *depsgraph, struct Scene *scene, Object *object)
|
|
|
|
|
{
|
|
|
|
|
/* 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);
|
2020-12-02 13:25:25 +01:00
|
|
|
GeometrySet geometry_set = GeometrySet::create_with_pointcloud(pointcloud,
|
|
|
|
|
GeometryOwnershipType::ReadOnly);
|
|
|
|
|
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);
|
|
|
|
|
object->runtime.geometry_set_eval = new 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);
|
|
|
|
|
}
|
|
|
|
|
}
|