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
244 lines
8.2 KiB
C++
244 lines
8.2 KiB
C++
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include "DNA_pointcloud_types.h"
|
|
|
|
#include "BKE_geometry_set.hh"
|
|
#include "BKE_lib_id.h"
|
|
#include "BKE_pointcloud.h"
|
|
|
|
#include "attribute_access_intern.hh"
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/** \name Geometry Component Implementation
|
|
* \{ */
|
|
|
|
PointCloudComponent::PointCloudComponent() : GeometryComponent(GEO_COMPONENT_TYPE_POINT_CLOUD)
|
|
{
|
|
}
|
|
|
|
PointCloudComponent::~PointCloudComponent()
|
|
{
|
|
this->clear();
|
|
}
|
|
|
|
GeometryComponent *PointCloudComponent::copy() const
|
|
{
|
|
PointCloudComponent *new_component = new PointCloudComponent();
|
|
if (pointcloud_ != nullptr) {
|
|
new_component->pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
|
|
new_component->ownership_ = GeometryOwnershipType::Owned;
|
|
}
|
|
return new_component;
|
|
}
|
|
|
|
void PointCloudComponent::clear()
|
|
{
|
|
BLI_assert(this->is_mutable());
|
|
if (pointcloud_ != nullptr) {
|
|
if (ownership_ == GeometryOwnershipType::Owned) {
|
|
BKE_id_free(nullptr, pointcloud_);
|
|
}
|
|
pointcloud_ = nullptr;
|
|
}
|
|
}
|
|
|
|
bool PointCloudComponent::has_pointcloud() const
|
|
{
|
|
return pointcloud_ != nullptr;
|
|
}
|
|
|
|
void PointCloudComponent::replace(PointCloud *pointcloud, GeometryOwnershipType ownership)
|
|
{
|
|
BLI_assert(this->is_mutable());
|
|
this->clear();
|
|
pointcloud_ = pointcloud;
|
|
ownership_ = ownership;
|
|
}
|
|
|
|
PointCloud *PointCloudComponent::release()
|
|
{
|
|
BLI_assert(this->is_mutable());
|
|
PointCloud *pointcloud = pointcloud_;
|
|
pointcloud_ = nullptr;
|
|
return pointcloud;
|
|
}
|
|
|
|
const PointCloud *PointCloudComponent::get_for_read() const
|
|
{
|
|
return pointcloud_;
|
|
}
|
|
|
|
PointCloud *PointCloudComponent::get_for_write()
|
|
{
|
|
BLI_assert(this->is_mutable());
|
|
if (ownership_ == GeometryOwnershipType::ReadOnly) {
|
|
pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
|
|
ownership_ = GeometryOwnershipType::Owned;
|
|
}
|
|
return pointcloud_;
|
|
}
|
|
|
|
bool PointCloudComponent::is_empty() const
|
|
{
|
|
return pointcloud_ == nullptr;
|
|
}
|
|
|
|
bool PointCloudComponent::owns_direct_data() const
|
|
{
|
|
return ownership_ == GeometryOwnershipType::Owned;
|
|
}
|
|
|
|
void PointCloudComponent::ensure_owns_direct_data()
|
|
{
|
|
BLI_assert(this->is_mutable());
|
|
if (ownership_ != GeometryOwnershipType::Owned) {
|
|
pointcloud_ = BKE_pointcloud_copy_for_eval(pointcloud_, false);
|
|
ownership_ = GeometryOwnershipType::Owned;
|
|
}
|
|
}
|
|
|
|
/** \} */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
/** \name Attribute Access
|
|
* \{ */
|
|
|
|
namespace blender::bke {
|
|
|
|
static void tag_component_positions_changed(void *owner)
|
|
{
|
|
PointCloud &points = *static_cast<PointCloud *>(owner);
|
|
points.tag_positions_changed();
|
|
}
|
|
|
|
static void tag_component_radius_changed(void *owner)
|
|
{
|
|
PointCloud &points = *static_cast<PointCloud *>(owner);
|
|
points.tag_radii_changed();
|
|
}
|
|
|
|
/**
|
|
* In this function all the attribute providers for a point cloud component are created. Most data
|
|
* in this function is statically allocated, because it does not change over time.
|
|
*/
|
|
static ComponentAttributeProviders create_attribute_providers_for_point_cloud()
|
|
{
|
|
static CustomDataAccessInfo point_access = {
|
|
[](void *owner) -> CustomData * {
|
|
PointCloud *pointcloud = static_cast<PointCloud *>(owner);
|
|
return &pointcloud->pdata;
|
|
},
|
|
[](const void *owner) -> const CustomData * {
|
|
const PointCloud *pointcloud = static_cast<const PointCloud *>(owner);
|
|
return &pointcloud->pdata;
|
|
},
|
|
[](const void *owner) -> int {
|
|
const PointCloud *pointcloud = static_cast<const PointCloud *>(owner);
|
|
return pointcloud->totpoint;
|
|
}};
|
|
|
|
static BuiltinCustomDataLayerProvider position("position",
|
|
ATTR_DOMAIN_POINT,
|
|
CD_PROP_FLOAT3,
|
|
CD_PROP_FLOAT3,
|
|
BuiltinAttributeProvider::NonCreatable,
|
|
BuiltinAttributeProvider::Writable,
|
|
BuiltinAttributeProvider::NonDeletable,
|
|
point_access,
|
|
make_array_read_attribute<float3>,
|
|
make_array_write_attribute<float3>,
|
|
tag_component_positions_changed);
|
|
static BuiltinCustomDataLayerProvider radius("radius",
|
|
ATTR_DOMAIN_POINT,
|
|
CD_PROP_FLOAT,
|
|
CD_PROP_FLOAT,
|
|
BuiltinAttributeProvider::Creatable,
|
|
BuiltinAttributeProvider::Writable,
|
|
BuiltinAttributeProvider::Deletable,
|
|
point_access,
|
|
make_array_read_attribute<float>,
|
|
make_array_write_attribute<float>,
|
|
tag_component_radius_changed);
|
|
static BuiltinCustomDataLayerProvider id("id",
|
|
ATTR_DOMAIN_POINT,
|
|
CD_PROP_INT32,
|
|
CD_PROP_INT32,
|
|
BuiltinAttributeProvider::Creatable,
|
|
BuiltinAttributeProvider::Writable,
|
|
BuiltinAttributeProvider::Deletable,
|
|
point_access,
|
|
make_array_read_attribute<int>,
|
|
make_array_write_attribute<int>,
|
|
nullptr);
|
|
static CustomDataAttributeProvider point_custom_data(ATTR_DOMAIN_POINT, point_access);
|
|
return ComponentAttributeProviders({&position, &radius, &id}, {&point_custom_data});
|
|
}
|
|
|
|
static AttributeAccessorFunctions get_pointcloud_accessor_functions()
|
|
{
|
|
static const ComponentAttributeProviders providers =
|
|
create_attribute_providers_for_point_cloud();
|
|
AttributeAccessorFunctions fn =
|
|
attribute_accessor_functions::accessor_functions_for_providers<providers>();
|
|
fn.domain_size = [](const void *owner, const eAttrDomain domain) {
|
|
if (owner == nullptr) {
|
|
return 0;
|
|
}
|
|
const PointCloud &pointcloud = *static_cast<const PointCloud *>(owner);
|
|
switch (domain) {
|
|
case ATTR_DOMAIN_POINT:
|
|
return pointcloud.totpoint;
|
|
default:
|
|
return 0;
|
|
}
|
|
};
|
|
fn.domain_supported = [](const void * /*owner*/, const eAttrDomain domain) {
|
|
return domain == ATTR_DOMAIN_POINT;
|
|
};
|
|
fn.adapt_domain = [](const void * /*owner*/,
|
|
const blender::GVArray &varray,
|
|
const eAttrDomain from_domain,
|
|
const eAttrDomain to_domain) {
|
|
if (from_domain == to_domain && from_domain == ATTR_DOMAIN_POINT) {
|
|
return varray;
|
|
}
|
|
return blender::GVArray{};
|
|
};
|
|
return fn;
|
|
}
|
|
|
|
static const AttributeAccessorFunctions &get_pointcloud_accessor_functions_ref()
|
|
{
|
|
static const AttributeAccessorFunctions fn = get_pointcloud_accessor_functions();
|
|
return fn;
|
|
}
|
|
|
|
} // namespace blender::bke
|
|
|
|
blender::bke::AttributeAccessor PointCloud::attributes() const
|
|
{
|
|
return blender::bke::AttributeAccessor(this,
|
|
blender::bke::get_pointcloud_accessor_functions_ref());
|
|
}
|
|
|
|
blender::bke::MutableAttributeAccessor PointCloud::attributes_for_write()
|
|
{
|
|
return blender::bke::MutableAttributeAccessor(
|
|
this, blender::bke::get_pointcloud_accessor_functions_ref());
|
|
}
|
|
|
|
std::optional<blender::bke::AttributeAccessor> PointCloudComponent::attributes() const
|
|
{
|
|
return blender::bke::AttributeAccessor(pointcloud_,
|
|
blender::bke::get_pointcloud_accessor_functions_ref());
|
|
}
|
|
|
|
std::optional<blender::bke::MutableAttributeAccessor> PointCloudComponent::attributes_for_write()
|
|
{
|
|
PointCloud *pointcloud = this->get_for_write();
|
|
return blender::bke::MutableAttributeAccessor(
|
|
pointcloud, blender::bke::get_pointcloud_accessor_functions_ref());
|
|
}
|
|
|
|
/** \} */
|