2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-12-02 13:25:25 +01:00
|
|
|
|
2022-06-05 21:05:13 +02:00
|
|
|
#include "BLI_bounds.hh"
|
Geometry Nodes: Make instances real on-demand
This commit makes the geometry output of the collection info usable.
The output is the geometry of a collection instance, but this commit
adds a utility to convert the instances to real geometry, used in the
background whenever it is needed, like copy on write.
The recursive nature of the "realize instances" code is essential,
because collection instances in the `InstancesComponent`, might have no
geometry sets of their own containing even more collection instances,
which might then contain object instances, etc.
Another consideration is that currently, every single instance contains
a reference to its data. This is inefficient since most of the time
there are many locations and only a few sets of unique data. So this
commit adds a `GeometryInstanceGroup` to support this future optimization.
The API for instances returns a vector of `GeometryInstanceGroup`.
This may be less efficient when there are many instances, but it makes
more complicated operations like point distribution that need to iterate
over input geometry multiple times much simpler.
Any code that needs to change data, like most of the attribute nodes,
can simply call `geometry_set_realize_instances(geometry_set)`,
which will move any geometry in the `InstancesComponent` to new "real"
geometry components.
Many nodes can support read-only access to instances in order to avoid
making them real, this will be addressed where needed in the near future.
Instances from the existing "dupli" system are not supported yet.
Differential Revision: https://developer.blender.org/D10327
2021-02-12 11:58:15 -06:00
|
|
|
#include "BLI_map.hh"
|
2021-09-28 10:17:00 +02:00
|
|
|
#include "BLI_task.hh"
|
Geometry Nodes: Make instances real on-demand
This commit makes the geometry output of the collection info usable.
The output is the geometry of a collection instance, but this commit
adds a utility to convert the instances to real geometry, used in the
background whenever it is needed, like copy on write.
The recursive nature of the "realize instances" code is essential,
because collection instances in the `InstancesComponent`, might have no
geometry sets of their own containing even more collection instances,
which might then contain object instances, etc.
Another consideration is that currently, every single instance contains
a reference to its data. This is inefficient since most of the time
there are many locations and only a few sets of unique data. So this
commit adds a `GeometryInstanceGroup` to support this future optimization.
The API for instances returns a vector of `GeometryInstanceGroup`.
This may be less efficient when there are many instances, but it makes
more complicated operations like point distribution that need to iterate
over input geometry multiple times much simpler.
Any code that needs to change data, like most of the attribute nodes,
can simply call `geometry_set_realize_instances(geometry_set)`,
which will move any geometry in the `InstancesComponent` to new "real"
geometry components.
Many nodes can support read-only access to instances in order to avoid
making them real, this will be addressed where needed in the near future.
Instances from the existing "dupli" system are not supported yet.
Differential Revision: https://developer.blender.org/D10327
2021-02-12 11:58:15 -06:00
|
|
|
|
2022-01-10 16:41:05 -06:00
|
|
|
#include "BLT_translation.h"
|
|
|
|
|
|
Geometry Nodes: Make instances real on-demand
This commit makes the geometry output of the collection info usable.
The output is the geometry of a collection instance, but this commit
adds a utility to convert the instances to real geometry, used in the
background whenever it is needed, like copy on write.
The recursive nature of the "realize instances" code is essential,
because collection instances in the `InstancesComponent`, might have no
geometry sets of their own containing even more collection instances,
which might then contain object instances, etc.
Another consideration is that currently, every single instance contains
a reference to its data. This is inefficient since most of the time
there are many locations and only a few sets of unique data. So this
commit adds a `GeometryInstanceGroup` to support this future optimization.
The API for instances returns a vector of `GeometryInstanceGroup`.
This may be less efficient when there are many instances, but it makes
more complicated operations like point distribution that need to iterate
over input geometry multiple times much simpler.
Any code that needs to change data, like most of the attribute nodes,
can simply call `geometry_set_realize_instances(geometry_set)`,
which will move any geometry in the `InstancesComponent` to new "real"
geometry components.
Many nodes can support read-only access to instances in order to avoid
making them real, this will be addressed where needed in the near future.
Instances from the existing "dupli" system are not supported yet.
Differential Revision: https://developer.blender.org/D10327
2021-02-12 11:58:15 -06:00
|
|
|
#include "BKE_attribute.h"
|
2022-02-28 10:46:34 -05:00
|
|
|
#include "BKE_curves.hh"
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "BKE_geometry_set.hh"
|
2022-10-17 11:39:40 +02:00
|
|
|
#include "BKE_instances.hh"
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "BKE_mesh_wrapper.h"
|
Geometry Nodes: Make instances real on-demand
This commit makes the geometry output of the collection info usable.
The output is the geometry of a collection instance, but this commit
adds a utility to convert the instances to real geometry, used in the
background whenever it is needed, like copy on write.
The recursive nature of the "realize instances" code is essential,
because collection instances in the `InstancesComponent`, might have no
geometry sets of their own containing even more collection instances,
which might then contain object instances, etc.
Another consideration is that currently, every single instance contains
a reference to its data. This is inefficient since most of the time
there are many locations and only a few sets of unique data. So this
commit adds a `GeometryInstanceGroup` to support this future optimization.
The API for instances returns a vector of `GeometryInstanceGroup`.
This may be less efficient when there are many instances, but it makes
more complicated operations like point distribution that need to iterate
over input geometry multiple times much simpler.
Any code that needs to change data, like most of the attribute nodes,
can simply call `geometry_set_realize_instances(geometry_set)`,
which will move any geometry in the `InstancesComponent` to new "real"
geometry components.
Many nodes can support read-only access to instances in order to avoid
making them real, this will be addressed where needed in the near future.
Instances from the existing "dupli" system are not supported yet.
Differential Revision: https://developer.blender.org/D10327
2021-02-12 11:58:15 -06:00
|
|
|
#include "BKE_modifier.h"
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "BKE_pointcloud.h"
|
2021-01-21 10:32:42 +01:00
|
|
|
#include "BKE_volume.h"
|
2020-12-02 13:25:25 +01:00
|
|
|
|
Geometry Nodes: Make instances real on-demand
This commit makes the geometry output of the collection info usable.
The output is the geometry of a collection instance, but this commit
adds a utility to convert the instances to real geometry, used in the
background whenever it is needed, like copy on write.
The recursive nature of the "realize instances" code is essential,
because collection instances in the `InstancesComponent`, might have no
geometry sets of their own containing even more collection instances,
which might then contain object instances, etc.
Another consideration is that currently, every single instance contains
a reference to its data. This is inefficient since most of the time
there are many locations and only a few sets of unique data. So this
commit adds a `GeometryInstanceGroup` to support this future optimization.
The API for instances returns a vector of `GeometryInstanceGroup`.
This may be less efficient when there are many instances, but it makes
more complicated operations like point distribution that need to iterate
over input geometry multiple times much simpler.
Any code that needs to change data, like most of the attribute nodes,
can simply call `geometry_set_realize_instances(geometry_set)`,
which will move any geometry in the `InstancesComponent` to new "real"
geometry components.
Many nodes can support read-only access to instances in order to avoid
making them real, this will be addressed where needed in the near future.
Instances from the existing "dupli" system are not supported yet.
Differential Revision: https://developer.blender.org/D10327
2021-02-12 11:58:15 -06:00
|
|
|
#include "DNA_collection_types.h"
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "DNA_object_types.h"
|
2022-09-07 10:43:46 +02:00
|
|
|
#include "DNA_pointcloud_types.h"
|
2020-12-02 13:25:25 +01:00
|
|
|
|
2021-02-12 17:41:28 +01:00
|
|
|
#include "BLI_rand.hh"
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
using blender::float3;
|
2021-01-26 18:21:12 +01:00
|
|
|
using blender::float4x4;
|
Geometry Nodes: Make instances real on-demand
This commit makes the geometry output of the collection info usable.
The output is the geometry of a collection instance, but this commit
adds a utility to convert the instances to real geometry, used in the
background whenever it is needed, like copy on write.
The recursive nature of the "realize instances" code is essential,
because collection instances in the `InstancesComponent`, might have no
geometry sets of their own containing even more collection instances,
which might then contain object instances, etc.
Another consideration is that currently, every single instance contains
a reference to its data. This is inefficient since most of the time
there are many locations and only a few sets of unique data. So this
commit adds a `GeometryInstanceGroup` to support this future optimization.
The API for instances returns a vector of `GeometryInstanceGroup`.
This may be less efficient when there are many instances, but it makes
more complicated operations like point distribution that need to iterate
over input geometry multiple times much simpler.
Any code that needs to change data, like most of the attribute nodes,
can simply call `geometry_set_realize_instances(geometry_set)`,
which will move any geometry in the `InstancesComponent` to new "real"
geometry components.
Many nodes can support read-only access to instances in order to avoid
making them real, this will be addressed where needed in the near future.
Instances from the existing "dupli" system are not supported yet.
Differential Revision: https://developer.blender.org/D10327
2021-02-12 11:58:15 -06:00
|
|
|
using blender::Map;
|
2020-12-02 13:25:25 +01:00
|
|
|
using blender::MutableSpan;
|
|
|
|
|
using blender::Span;
|
|
|
|
|
using blender::StringRef;
|
|
|
|
|
using blender::Vector;
|
2022-10-17 11:39:40 +02:00
|
|
|
using blender::bke::InstanceReference;
|
|
|
|
|
using blender::bke::Instances;
|
2020-12-02 13:25:25 +01:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Geometry Component
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
GeometryComponent::GeometryComponent(GeometryComponentType type) : type_(type)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeometryComponent *GeometryComponent::create(GeometryComponentType component_type)
|
|
|
|
|
{
|
|
|
|
|
switch (component_type) {
|
2021-03-10 11:53:17 +01:00
|
|
|
case GEO_COMPONENT_TYPE_MESH:
|
2020-12-02 13:25:25 +01:00
|
|
|
return new MeshComponent();
|
2021-03-10 11:53:17 +01:00
|
|
|
case GEO_COMPONENT_TYPE_POINT_CLOUD:
|
2020-12-02 13:25:25 +01:00
|
|
|
return new PointCloudComponent();
|
2021-03-10 11:53:17 +01:00
|
|
|
case GEO_COMPONENT_TYPE_INSTANCES:
|
2020-12-02 13:25:25 +01:00
|
|
|
return new InstancesComponent();
|
2021-03-10 11:53:17 +01:00
|
|
|
case GEO_COMPONENT_TYPE_VOLUME:
|
2021-01-21 10:32:42 +01:00
|
|
|
return new VolumeComponent();
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
case GEO_COMPONENT_TYPE_CURVE:
|
|
|
|
|
return new CurveComponent();
|
2022-07-22 15:39:41 +02:00
|
|
|
case GEO_COMPONENT_TYPE_EDIT:
|
|
|
|
|
return new GeometryComponentEditData();
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
2021-03-23 16:49:47 +01:00
|
|
|
BLI_assert_unreachable();
|
2020-12-02 13:25:25 +01:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
Geometry Nodes: new geometry attribute API
Currently, there are two attribute API. The first, defined in `BKE_attribute.h` is
accessible from RNA and C code. The second is implemented with `GeometryComponent`
and is only accessible in C++ code. The second is widely used, but only being
accessible through the `GeometrySet` API makes it awkward to use, and even impossible
for types that don't correspond directly to a geometry component like `CurvesGeometry`.
This patch adds a new attribute API, designed to replace the `GeometryComponent`
attribute API now, and to eventually replace or be the basis of the other one.
The basic idea is that there is an `AttributeAccessor` class that allows code to
interact with a set of attributes owned by some geometry. The accessor itself has
no ownership. `AttributeAccessor` is a simple type that can be passed around by
value. That makes it easy to return it from functions and to store it in containers.
For const-correctness, there is also a `MutableAttributeAccessor` that allows
changing individual and can add or remove attributes.
Currently, `AttributeAccessor` is composed of two pointers. The first is a pointer
to the owner of the attribute data. The second is a pointer to a struct with
function pointers, that is similar to a virtual function table. The functions
know how to access attributes on the owner.
The actual attribute access for geometries is still implemented with the `AttributeProvider`
pattern, which makes it easy to support different sources of attributes on a
geometry and simplifies dealing with built-in attributes.
There are different ways to get an attribute accessor for a geometry:
* `GeometryComponent.attributes()`
* `CurvesGeometry.attributes()`
* `bke::mesh_attributes(const Mesh &)`
* `bke::pointcloud_attributes(const PointCloud &)`
All of these also have a `_for_write` variant that returns a `MutabelAttributeAccessor`.
Differential Revision: https://developer.blender.org/D15280
2022-07-08 16:16:56 +02:00
|
|
|
int GeometryComponent::attribute_domain_size(const eAttrDomain domain) const
|
|
|
|
|
{
|
|
|
|
|
if (this->is_empty()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
const std::optional<blender::bke::AttributeAccessor> attributes = this->attributes();
|
|
|
|
|
if (attributes.has_value()) {
|
|
|
|
|
return attributes->domain_size(domain);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::optional<blender::bke::AttributeAccessor> GeometryComponent::attributes() const
|
|
|
|
|
{
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
};
|
|
|
|
|
std::optional<blender::bke::MutableAttributeAccessor> GeometryComponent::attributes_for_write()
|
|
|
|
|
{
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
void GeometryComponent::user_add() const
|
|
|
|
|
{
|
|
|
|
|
users_.fetch_add(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeometryComponent::user_remove() const
|
|
|
|
|
{
|
|
|
|
|
const int new_users = users_.fetch_sub(1) - 1;
|
|
|
|
|
if (new_users == 0) {
|
|
|
|
|
delete this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeometryComponent::is_mutable() const
|
|
|
|
|
{
|
|
|
|
|
/* If the item is shared, it is read-only. */
|
|
|
|
|
/* The user count can be 0, when this is called from the destructor. */
|
|
|
|
|
return users_ <= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeometryComponentType GeometryComponent::type() const
|
|
|
|
|
{
|
|
|
|
|
return type_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeometryComponent::is_empty() const
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Geometry Set
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2021-10-03 16:58:33 +02:00
|
|
|
GeometrySet::GeometrySet() = default;
|
|
|
|
|
GeometrySet::GeometrySet(const GeometrySet &other) = default;
|
|
|
|
|
GeometrySet::GeometrySet(GeometrySet &&other) = default;
|
|
|
|
|
GeometrySet::~GeometrySet() = default;
|
|
|
|
|
GeometrySet &GeometrySet::operator=(const GeometrySet &other) = default;
|
|
|
|
|
GeometrySet &GeometrySet::operator=(GeometrySet &&other) = default;
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
GeometryComponent &GeometrySet::get_component_for_write(GeometryComponentType component_type)
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
GeometryComponentPtr &component_ptr = components_[component_type];
|
|
|
|
|
if (!component_ptr) {
|
|
|
|
|
/* If the component did not exist before, create a new one. */
|
|
|
|
|
component_ptr = GeometryComponent::create(component_type);
|
|
|
|
|
return *component_ptr;
|
|
|
|
|
}
|
|
|
|
|
if (component_ptr->is_mutable()) {
|
|
|
|
|
/* If the referenced component is already mutable, return it directly. */
|
|
|
|
|
return *component_ptr;
|
|
|
|
|
}
|
|
|
|
|
/* If the referenced component is shared, make a copy. The copy is not shared and is
|
|
|
|
|
* therefore mutable. */
|
|
|
|
|
component_ptr = component_ptr->copy();
|
|
|
|
|
return *component_ptr;
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-14 12:43:36 -05:00
|
|
|
GeometryComponent *GeometrySet::get_component_ptr(GeometryComponentType type)
|
|
|
|
|
{
|
2021-10-15 13:39:11 +02:00
|
|
|
if (this->has(type)) {
|
|
|
|
|
return &this->get_component_for_write(type);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
2021-10-14 12:43:36 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
const GeometryComponent *GeometrySet::get_component_for_read(
|
|
|
|
|
GeometryComponentType component_type) const
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
return components_[component_type].get();
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeometrySet::has(const GeometryComponentType component_type) const
|
|
|
|
|
{
|
2022-11-22 15:40:42 -06:00
|
|
|
const GeometryComponentPtr &component = components_[component_type];
|
|
|
|
|
return component.has_value() && !component->is_empty();
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeometrySet::remove(const GeometryComponentType component_type)
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
components_[component_type].reset();
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 15:52:04 +02:00
|
|
|
void GeometrySet::keep_only(const blender::Span<GeometryComponentType> component_types)
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
for (GeometryComponentPtr &component_ptr : components_) {
|
|
|
|
|
if (component_ptr) {
|
|
|
|
|
if (!component_types.contains(component_ptr->type())) {
|
|
|
|
|
component_ptr.reset();
|
|
|
|
|
}
|
2021-09-28 15:52:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 15:39:41 +02:00
|
|
|
void GeometrySet::keep_only_during_modify(
|
|
|
|
|
const blender::Span<GeometryComponentType> component_types)
|
|
|
|
|
{
|
|
|
|
|
Vector<GeometryComponentType> extended_types = component_types;
|
|
|
|
|
extended_types.append_non_duplicates(GEO_COMPONENT_TYPE_INSTANCES);
|
|
|
|
|
extended_types.append_non_duplicates(GEO_COMPONENT_TYPE_EDIT);
|
|
|
|
|
this->keep_only(extended_types);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeometrySet::remove_geometry_during_modify()
|
|
|
|
|
{
|
|
|
|
|
this->keep_only_during_modify({});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
void GeometrySet::add(const GeometryComponent &component)
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
BLI_assert(!components_[component.type()]);
|
2020-12-02 13:25:25 +01:00
|
|
|
component.user_add();
|
2021-12-05 15:10:11 +01:00
|
|
|
components_[component.type()] = const_cast<GeometryComponent *>(&component);
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-19 12:29:37 +01:00
|
|
|
Vector<const GeometryComponent *> GeometrySet::get_components_for_read() const
|
|
|
|
|
{
|
|
|
|
|
Vector<const GeometryComponent *> components;
|
2021-12-05 15:10:11 +01:00
|
|
|
for (const GeometryComponentPtr &component_ptr : components_) {
|
|
|
|
|
if (component_ptr) {
|
|
|
|
|
components.append(component_ptr.get());
|
|
|
|
|
}
|
2021-02-19 12:29:37 +01:00
|
|
|
}
|
|
|
|
|
return components;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 12:27:24 -03:00
|
|
|
bool GeometrySet::compute_boundbox_without_instances(float3 *r_min, float3 *r_max) const
|
2020-12-02 13:25:25 +01:00
|
|
|
{
|
2022-06-05 21:05:13 +02:00
|
|
|
using namespace blender;
|
2021-12-29 12:27:24 -03:00
|
|
|
bool have_minmax = false;
|
2022-04-05 16:40:28 -05:00
|
|
|
if (const PointCloud *pointcloud = this->get_pointcloud_for_read()) {
|
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
|
|
|
have_minmax |= pointcloud->bounds_min_max(*r_min, *r_max);
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
2022-04-05 16:40:28 -05:00
|
|
|
if (const Mesh *mesh = this->get_mesh_for_read()) {
|
2021-12-29 12:27:24 -03:00
|
|
|
have_minmax |= BKE_mesh_wrapper_minmax(mesh, *r_min, *r_max);
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
2022-04-05 16:40:28 -05:00
|
|
|
if (const Volume *volume = this->get_volume_for_read()) {
|
2021-12-29 12:27:24 -03:00
|
|
|
have_minmax |= BKE_volume_min_max(volume, *r_min, *r_max);
|
2021-04-08 14:32:41 -05:00
|
|
|
}
|
2022-06-05 21:05:13 +02:00
|
|
|
if (const Curves *curves_id = this->get_curves_for_read()) {
|
2023-01-31 18:45:34 +01:00
|
|
|
const bke::CurvesGeometry &curves = curves_id->geometry.wrap();
|
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
|
|
|
have_minmax |= curves.bounds_min_max(*r_min, *r_max);
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
}
|
2021-12-29 12:27:24 -03:00
|
|
|
return have_minmax;
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ostream &operator<<(std::ostream &stream, const GeometrySet &geometry_set)
|
|
|
|
|
{
|
2022-09-07 10:43:46 +02:00
|
|
|
Vector<std::string> parts;
|
|
|
|
|
if (const Mesh *mesh = geometry_set.get_mesh_for_read()) {
|
|
|
|
|
parts.append(std::to_string(mesh->totvert) + " verts");
|
|
|
|
|
parts.append(std::to_string(mesh->totedge) + " edges");
|
|
|
|
|
parts.append(std::to_string(mesh->totpoly) + " polys");
|
|
|
|
|
parts.append(std::to_string(mesh->totloop) + " corners");
|
|
|
|
|
}
|
|
|
|
|
if (const Curves *curves = geometry_set.get_curves_for_read()) {
|
|
|
|
|
parts.append(std::to_string(curves->geometry.point_num) + " control points");
|
|
|
|
|
parts.append(std::to_string(curves->geometry.curve_num) + " curves");
|
|
|
|
|
}
|
|
|
|
|
if (const PointCloud *point_cloud = geometry_set.get_pointcloud_for_read()) {
|
|
|
|
|
parts.append(std::to_string(point_cloud->totpoint) + " points");
|
|
|
|
|
}
|
|
|
|
|
if (const Volume *volume = geometry_set.get_volume_for_read()) {
|
|
|
|
|
parts.append(std::to_string(BKE_volume_num_grids(volume)) + " volume grids");
|
|
|
|
|
}
|
|
|
|
|
if (geometry_set.has_instances()) {
|
2022-10-17 11:39:40 +02:00
|
|
|
parts.append(std::to_string(geometry_set.get_instances_for_read()->instances_num()) +
|
2022-09-07 10:43:46 +02:00
|
|
|
" instances");
|
|
|
|
|
}
|
|
|
|
|
if (geometry_set.get_curve_edit_hints_for_read()) {
|
|
|
|
|
parts.append("curve edit hints");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stream << "<GeometrySet: ";
|
|
|
|
|
for (const int i : parts.index_range()) {
|
|
|
|
|
stream << parts[i];
|
|
|
|
|
if (i < parts.size() - 1) {
|
|
|
|
|
stream << ", ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
stream << ">";
|
2020-12-02 13:25:25 +01:00
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-30 12:34:05 +02:00
|
|
|
void GeometrySet::clear()
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
for (GeometryComponentPtr &component_ptr : components_) {
|
|
|
|
|
component_ptr.reset();
|
|
|
|
|
}
|
2021-03-30 12:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-08 17:35:06 +02:00
|
|
|
void GeometrySet::ensure_owns_direct_data()
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
for (GeometryComponentPtr &component_ptr : components_) {
|
|
|
|
|
if (!component_ptr) {
|
|
|
|
|
continue;
|
2021-04-08 17:35:06 +02:00
|
|
|
}
|
2021-12-05 15:10:11 +01:00
|
|
|
if (component_ptr->owns_direct_data()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
GeometryComponent &component_for_write = this->get_component_for_write(component_ptr->type());
|
|
|
|
|
component_for_write.ensure_owns_direct_data();
|
2021-04-08 17:35:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 18:22:24 +02:00
|
|
|
bool GeometrySet::owns_direct_data() const
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
for (const GeometryComponentPtr &component_ptr : components_) {
|
|
|
|
|
if (component_ptr) {
|
|
|
|
|
if (!component_ptr->owns_direct_data()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-09-06 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
const Mesh *GeometrySet::get_mesh_for_read() const
|
|
|
|
|
{
|
|
|
|
|
const MeshComponent *component = this->get_component_for_read<MeshComponent>();
|
|
|
|
|
return (component == nullptr) ? nullptr : component->get_for_read();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeometrySet::has_mesh() const
|
|
|
|
|
{
|
|
|
|
|
const MeshComponent *component = this->get_component_for_read<MeshComponent>();
|
|
|
|
|
return component != nullptr && component->has_mesh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PointCloud *GeometrySet::get_pointcloud_for_read() const
|
|
|
|
|
{
|
|
|
|
|
const PointCloudComponent *component = this->get_component_for_read<PointCloudComponent>();
|
|
|
|
|
return (component == nullptr) ? nullptr : component->get_for_read();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 10:32:42 +01:00
|
|
|
const Volume *GeometrySet::get_volume_for_read() const
|
|
|
|
|
{
|
|
|
|
|
const VolumeComponent *component = this->get_component_for_read<VolumeComponent>();
|
|
|
|
|
return (component == nullptr) ? nullptr : component->get_for_read();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 10:53:30 -05:00
|
|
|
const Curves *GeometrySet::get_curves_for_read() const
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
{
|
|
|
|
|
const CurveComponent *component = this->get_component_for_read<CurveComponent>();
|
|
|
|
|
return (component == nullptr) ? nullptr : component->get_for_read();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 11:39:40 +02:00
|
|
|
const Instances *GeometrySet::get_instances_for_read() const
|
|
|
|
|
{
|
|
|
|
|
const InstancesComponent *component = this->get_component_for_read<InstancesComponent>();
|
|
|
|
|
return (component == nullptr) ? nullptr : component->get_for_read();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 15:39:41 +02:00
|
|
|
const blender::bke::CurvesEditHints *GeometrySet::get_curve_edit_hints_for_read() const
|
|
|
|
|
{
|
|
|
|
|
const GeometryComponentEditData *component =
|
|
|
|
|
this->get_component_for_read<GeometryComponentEditData>();
|
|
|
|
|
return (component == nullptr) ? nullptr : component->curves_edit_hints_.get();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
bool GeometrySet::has_pointcloud() const
|
|
|
|
|
{
|
|
|
|
|
const PointCloudComponent *component = this->get_component_for_read<PointCloudComponent>();
|
|
|
|
|
return component != nullptr && component->has_pointcloud();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GeometrySet::has_instances() const
|
|
|
|
|
{
|
|
|
|
|
const InstancesComponent *component = this->get_component_for_read<InstancesComponent>();
|
2022-10-17 11:39:40 +02:00
|
|
|
return component != nullptr && component->get_for_read() != nullptr &&
|
|
|
|
|
component->get_for_read()->instances_num() >= 1;
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-21 10:32:42 +01:00
|
|
|
bool GeometrySet::has_volume() const
|
|
|
|
|
{
|
|
|
|
|
const VolumeComponent *component = this->get_component_for_read<VolumeComponent>();
|
|
|
|
|
return component != nullptr && component->has_volume();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 10:53:30 -05:00
|
|
|
bool GeometrySet::has_curves() const
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
{
|
|
|
|
|
const CurveComponent *component = this->get_component_for_read<CurveComponent>();
|
2022-02-28 10:46:34 -05:00
|
|
|
return component != nullptr && component->has_curves();
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
2021-09-27 10:16:38 +02:00
|
|
|
bool GeometrySet::has_realized_data() const
|
|
|
|
|
{
|
2021-12-05 15:10:11 +01:00
|
|
|
for (const GeometryComponentPtr &component_ptr : components_) {
|
|
|
|
|
if (component_ptr) {
|
|
|
|
|
if (component_ptr->type() != GEO_COMPONENT_TYPE_INSTANCES) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-27 10:16:38 +02:00
|
|
|
}
|
2021-12-05 15:10:11 +01:00
|
|
|
return false;
|
2021-09-27 10:16:38 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 11:36:28 -05:00
|
|
|
bool GeometrySet::is_empty() const
|
|
|
|
|
{
|
2022-02-28 10:53:30 -05:00
|
|
|
return !(this->has_mesh() || this->has_curves() || this->has_pointcloud() ||
|
|
|
|
|
this->has_volume() || this->has_instances());
|
2021-09-28 11:36:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
GeometrySet GeometrySet::create_with_mesh(Mesh *mesh, GeometryOwnershipType ownership)
|
|
|
|
|
{
|
|
|
|
|
GeometrySet geometry_set;
|
2021-10-14 12:43:36 -05:00
|
|
|
if (mesh != nullptr) {
|
|
|
|
|
MeshComponent &component = geometry_set.get_component_for_write<MeshComponent>();
|
|
|
|
|
component.replace(mesh, ownership);
|
|
|
|
|
}
|
2020-12-02 13:25:25 +01:00
|
|
|
return geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 10:56:17 -05:00
|
|
|
GeometrySet GeometrySet::create_with_volume(Volume *volume, GeometryOwnershipType ownership)
|
|
|
|
|
{
|
|
|
|
|
GeometrySet geometry_set;
|
|
|
|
|
if (volume != nullptr) {
|
|
|
|
|
VolumeComponent &component = geometry_set.get_component_for_write<VolumeComponent>();
|
|
|
|
|
component.replace(volume, ownership);
|
|
|
|
|
}
|
|
|
|
|
return geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
GeometrySet GeometrySet::create_with_pointcloud(PointCloud *pointcloud,
|
|
|
|
|
GeometryOwnershipType ownership)
|
|
|
|
|
{
|
|
|
|
|
GeometrySet geometry_set;
|
2021-10-14 12:43:36 -05:00
|
|
|
if (pointcloud != nullptr) {
|
|
|
|
|
PointCloudComponent &component = geometry_set.get_component_for_write<PointCloudComponent>();
|
|
|
|
|
component.replace(pointcloud, ownership);
|
|
|
|
|
}
|
2020-12-02 13:25:25 +01:00
|
|
|
return geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-28 10:53:30 -05:00
|
|
|
GeometrySet GeometrySet::create_with_curves(Curves *curves, GeometryOwnershipType ownership)
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
{
|
|
|
|
|
GeometrySet geometry_set;
|
2022-02-28 10:46:34 -05:00
|
|
|
if (curves != nullptr) {
|
2021-10-14 12:43:36 -05:00
|
|
|
CurveComponent &component = geometry_set.get_component_for_write<CurveComponent>();
|
2022-02-28 10:46:34 -05:00
|
|
|
component.replace(curves, ownership);
|
2021-10-14 12:43:36 -05:00
|
|
|
}
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
return geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-17 11:39:40 +02:00
|
|
|
GeometrySet GeometrySet::create_with_instances(Instances *instances,
|
|
|
|
|
GeometryOwnershipType ownership)
|
|
|
|
|
{
|
|
|
|
|
GeometrySet geometry_set;
|
|
|
|
|
geometry_set.replace_instances(instances, ownership);
|
|
|
|
|
return geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
void GeometrySet::replace_mesh(Mesh *mesh, GeometryOwnershipType ownership)
|
|
|
|
|
{
|
2021-10-14 12:43:36 -05:00
|
|
|
if (mesh == nullptr) {
|
|
|
|
|
this->remove<MeshComponent>();
|
2021-12-09 14:34:15 -06:00
|
|
|
return;
|
2021-10-14 12:43:36 -05:00
|
|
|
}
|
2021-12-09 17:43:30 -06:00
|
|
|
if (mesh == this->get_mesh_for_read()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this->remove<MeshComponent>();
|
2021-12-09 14:34:15 -06:00
|
|
|
MeshComponent &component = this->get_component_for_write<MeshComponent>();
|
|
|
|
|
component.replace(mesh, ownership);
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 16:26:55 -06:00
|
|
|
void GeometrySet::replace_curves(Curves *curves, GeometryOwnershipType ownership)
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
{
|
2022-02-28 10:46:34 -05:00
|
|
|
if (curves == nullptr) {
|
2021-10-14 12:43:36 -05:00
|
|
|
this->remove<CurveComponent>();
|
2021-12-09 14:34:15 -06:00
|
|
|
return;
|
2021-10-14 12:43:36 -05:00
|
|
|
}
|
2022-02-28 10:53:30 -05:00
|
|
|
if (curves == this->get_curves_for_read()) {
|
2021-12-09 17:43:30 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this->remove<CurveComponent>();
|
2021-12-09 14:34:15 -06:00
|
|
|
CurveComponent &component = this->get_component_for_write<CurveComponent>();
|
2022-02-28 10:46:34 -05:00
|
|
|
component.replace(curves, ownership);
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
2022-10-17 11:39:40 +02:00
|
|
|
void GeometrySet::replace_instances(Instances *instances, GeometryOwnershipType ownership)
|
|
|
|
|
{
|
|
|
|
|
if (instances == nullptr) {
|
|
|
|
|
this->remove<InstancesComponent>();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (instances == this->get_instances_for_read()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this->remove<InstancesComponent>();
|
|
|
|
|
InstancesComponent &component = this->get_component_for_write<InstancesComponent>();
|
|
|
|
|
component.replace(instances, ownership);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
void GeometrySet::replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership)
|
|
|
|
|
{
|
2021-10-14 12:43:36 -05:00
|
|
|
if (pointcloud == nullptr) {
|
|
|
|
|
this->remove<PointCloudComponent>();
|
2021-12-09 14:34:15 -06:00
|
|
|
return;
|
2021-10-14 12:43:36 -05:00
|
|
|
}
|
2021-12-09 17:43:30 -06:00
|
|
|
if (pointcloud == this->get_pointcloud_for_read()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this->remove<PointCloudComponent>();
|
2021-12-09 14:34:15 -06:00
|
|
|
PointCloudComponent &component = this->get_component_for_write<PointCloudComponent>();
|
|
|
|
|
component.replace(pointcloud, ownership);
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-26 14:42:03 -05:00
|
|
|
void GeometrySet::replace_volume(Volume *volume, GeometryOwnershipType ownership)
|
|
|
|
|
{
|
2021-10-14 12:43:36 -05:00
|
|
|
if (volume == nullptr) {
|
|
|
|
|
this->remove<VolumeComponent>();
|
2021-12-09 14:34:15 -06:00
|
|
|
return;
|
2021-10-14 12:43:36 -05:00
|
|
|
}
|
2021-12-09 17:43:30 -06:00
|
|
|
if (volume == this->get_volume_for_read()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this->remove<VolumeComponent>();
|
2021-12-09 14:34:15 -06:00
|
|
|
VolumeComponent &component = this->get_component_for_write<VolumeComponent>();
|
|
|
|
|
component.replace(volume, ownership);
|
2021-04-26 14:42:03 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
Mesh *GeometrySet::get_mesh_for_write()
|
|
|
|
|
{
|
2021-10-14 12:43:36 -05:00
|
|
|
MeshComponent *component = this->get_component_ptr<MeshComponent>();
|
|
|
|
|
return component == nullptr ? nullptr : component->get_for_write();
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PointCloud *GeometrySet::get_pointcloud_for_write()
|
|
|
|
|
{
|
2021-10-14 12:43:36 -05:00
|
|
|
PointCloudComponent *component = this->get_component_ptr<PointCloudComponent>();
|
|
|
|
|
return component == nullptr ? nullptr : component->get_for_write();
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-21 10:32:42 +01:00
|
|
|
Volume *GeometrySet::get_volume_for_write()
|
|
|
|
|
{
|
2021-10-14 12:43:36 -05:00
|
|
|
VolumeComponent *component = this->get_component_ptr<VolumeComponent>();
|
|
|
|
|
return component == nullptr ? nullptr : component->get_for_write();
|
2021-01-21 10:32:42 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-28 10:53:30 -05:00
|
|
|
Curves *GeometrySet::get_curves_for_write()
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
{
|
2021-10-14 12:43:36 -05:00
|
|
|
CurveComponent *component = this->get_component_ptr<CurveComponent>();
|
|
|
|
|
return component == nullptr ? nullptr : component->get_for_write();
|
Geometry Nodes: Initial basic curve data support
This patch adds initial curve support to geometry nodes. Currently
there is only one node available, the "Curve to Mesh" node, T87428.
However, the aim of the changes here is larger than just supporting
curve data in nodes-- it also uses the opportunity to add better spline
data structures, intended to replace the existing curve evaluation code.
The curve code in Blender is quite old, and it's generally regarded as
some of the messiest, hardest-to-understand code as well. The classes
in `BKE_spline.hh` aim to be faster, more extensible, and much more
easily understandable. Further explanation can be found in comments in
that file.
Initial builtin spline attributes are supported-- reading and writing
from the `cyclic` and `resolution` attributes works with any of the
attribute nodes. Also, only Z-up normal calculation is implemented
at the moment, and tilts do not apply yet.
**Limitations**
- For now, you must bring curves into the node tree with an "Object
Info" node. Changes to the curve modifier stack will come later.
- Converting to a mesh is necessary to visualize the curve data.
Further progress can be tracked in: T87245
Higher level design document: https://wiki.blender.org/wiki/Modules/Physics_Nodes/Projects/EverythingNodes/CurveNodes
Differential Revision: https://developer.blender.org/D11091
2021-05-03 12:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
2022-10-17 11:39:40 +02:00
|
|
|
Instances *GeometrySet::get_instances_for_write()
|
|
|
|
|
{
|
|
|
|
|
InstancesComponent *component = this->get_component_ptr<InstancesComponent>();
|
|
|
|
|
return component == nullptr ? nullptr : component->get_for_write();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 15:39:41 +02:00
|
|
|
blender::bke::CurvesEditHints *GeometrySet::get_curve_edit_hints_for_write()
|
|
|
|
|
{
|
|
|
|
|
if (!this->has<GeometryComponentEditData>()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
GeometryComponentEditData &component =
|
|
|
|
|
this->get_component_for_write<GeometryComponentEditData>();
|
|
|
|
|
return component.curves_edit_hints_.get();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 17:50:33 +02:00
|
|
|
void GeometrySet::attribute_foreach(const Span<GeometryComponentType> component_types,
|
|
|
|
|
const bool include_instances,
|
|
|
|
|
const AttributeForeachCallback callback) const
|
|
|
|
|
{
|
|
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
for (const GeometryComponentType component_type : component_types) {
|
|
|
|
|
if (!this->has(component_type)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const GeometryComponent &component = *this->get_component_for_read(component_type);
|
Geometry Nodes: new geometry attribute API
Currently, there are two attribute API. The first, defined in `BKE_attribute.h` is
accessible from RNA and C code. The second is implemented with `GeometryComponent`
and is only accessible in C++ code. The second is widely used, but only being
accessible through the `GeometrySet` API makes it awkward to use, and even impossible
for types that don't correspond directly to a geometry component like `CurvesGeometry`.
This patch adds a new attribute API, designed to replace the `GeometryComponent`
attribute API now, and to eventually replace or be the basis of the other one.
The basic idea is that there is an `AttributeAccessor` class that allows code to
interact with a set of attributes owned by some geometry. The accessor itself has
no ownership. `AttributeAccessor` is a simple type that can be passed around by
value. That makes it easy to return it from functions and to store it in containers.
For const-correctness, there is also a `MutableAttributeAccessor` that allows
changing individual and can add or remove attributes.
Currently, `AttributeAccessor` is composed of two pointers. The first is a pointer
to the owner of the attribute data. The second is a pointer to a struct with
function pointers, that is similar to a virtual function table. The functions
know how to access attributes on the owner.
The actual attribute access for geometries is still implemented with the `AttributeProvider`
pattern, which makes it easy to support different sources of attributes on a
geometry and simplifies dealing with built-in attributes.
There are different ways to get an attribute accessor for a geometry:
* `GeometryComponent.attributes()`
* `CurvesGeometry.attributes()`
* `bke::mesh_attributes(const Mesh &)`
* `bke::pointcloud_attributes(const PointCloud &)`
All of these also have a `_for_write` variant that returns a `MutabelAttributeAccessor`.
Differential Revision: https://developer.blender.org/D15280
2022-07-08 16:16:56 +02:00
|
|
|
const std::optional<AttributeAccessor> attributes = component.attributes();
|
|
|
|
|
if (attributes.has_value()) {
|
|
|
|
|
attributes->for_all(
|
|
|
|
|
[&](const AttributeIDRef &attribute_id, const AttributeMetaData &meta_data) {
|
|
|
|
|
callback(attribute_id, meta_data, component);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-23 17:50:33 +02:00
|
|
|
}
|
|
|
|
|
if (include_instances && this->has_instances()) {
|
2022-10-17 11:39:40 +02:00
|
|
|
const Instances &instances = *this->get_instances_for_read();
|
2021-09-23 17:50:33 +02:00
|
|
|
instances.foreach_referenced_geometry([&](const GeometrySet &instance_geometry_set) {
|
|
|
|
|
instance_geometry_set.attribute_foreach(component_types, include_instances, callback);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeometrySet::gather_attributes_for_propagation(
|
|
|
|
|
const Span<GeometryComponentType> component_types,
|
|
|
|
|
const GeometryComponentType dst_component_type,
|
|
|
|
|
bool include_instances,
|
2023-01-05 14:05:30 +01:00
|
|
|
const blender::bke::AnonymousAttributePropagationInfo &propagation_info,
|
Geometry Nodes: new geometry attribute API
Currently, there are two attribute API. The first, defined in `BKE_attribute.h` is
accessible from RNA and C code. The second is implemented with `GeometryComponent`
and is only accessible in C++ code. The second is widely used, but only being
accessible through the `GeometrySet` API makes it awkward to use, and even impossible
for types that don't correspond directly to a geometry component like `CurvesGeometry`.
This patch adds a new attribute API, designed to replace the `GeometryComponent`
attribute API now, and to eventually replace or be the basis of the other one.
The basic idea is that there is an `AttributeAccessor` class that allows code to
interact with a set of attributes owned by some geometry. The accessor itself has
no ownership. `AttributeAccessor` is a simple type that can be passed around by
value. That makes it easy to return it from functions and to store it in containers.
For const-correctness, there is also a `MutableAttributeAccessor` that allows
changing individual and can add or remove attributes.
Currently, `AttributeAccessor` is composed of two pointers. The first is a pointer
to the owner of the attribute data. The second is a pointer to a struct with
function pointers, that is similar to a virtual function table. The functions
know how to access attributes on the owner.
The actual attribute access for geometries is still implemented with the `AttributeProvider`
pattern, which makes it easy to support different sources of attributes on a
geometry and simplifies dealing with built-in attributes.
There are different ways to get an attribute accessor for a geometry:
* `GeometryComponent.attributes()`
* `CurvesGeometry.attributes()`
* `bke::mesh_attributes(const Mesh &)`
* `bke::pointcloud_attributes(const PointCloud &)`
All of these also have a `_for_write` variant that returns a `MutabelAttributeAccessor`.
Differential Revision: https://developer.blender.org/D15280
2022-07-08 16:16:56 +02:00
|
|
|
blender::Map<blender::bke::AttributeIDRef, blender::bke::AttributeKind> &r_attributes) const
|
2021-09-23 17:50:33 +02:00
|
|
|
{
|
|
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
/* Only needed right now to check if an attribute is built-in on this component type.
|
|
|
|
|
* TODO: Get rid of the dummy component. */
|
|
|
|
|
const GeometryComponent *dummy_component = GeometryComponent::create(dst_component_type);
|
|
|
|
|
this->attribute_foreach(
|
|
|
|
|
component_types,
|
|
|
|
|
include_instances,
|
|
|
|
|
[&](const AttributeIDRef &attribute_id,
|
|
|
|
|
const AttributeMetaData &meta_data,
|
|
|
|
|
const GeometryComponent &component) {
|
Geometry Nodes: new geometry attribute API
Currently, there are two attribute API. The first, defined in `BKE_attribute.h` is
accessible from RNA and C code. The second is implemented with `GeometryComponent`
and is only accessible in C++ code. The second is widely used, but only being
accessible through the `GeometrySet` API makes it awkward to use, and even impossible
for types that don't correspond directly to a geometry component like `CurvesGeometry`.
This patch adds a new attribute API, designed to replace the `GeometryComponent`
attribute API now, and to eventually replace or be the basis of the other one.
The basic idea is that there is an `AttributeAccessor` class that allows code to
interact with a set of attributes owned by some geometry. The accessor itself has
no ownership. `AttributeAccessor` is a simple type that can be passed around by
value. That makes it easy to return it from functions and to store it in containers.
For const-correctness, there is also a `MutableAttributeAccessor` that allows
changing individual and can add or remove attributes.
Currently, `AttributeAccessor` is composed of two pointers. The first is a pointer
to the owner of the attribute data. The second is a pointer to a struct with
function pointers, that is similar to a virtual function table. The functions
know how to access attributes on the owner.
The actual attribute access for geometries is still implemented with the `AttributeProvider`
pattern, which makes it easy to support different sources of attributes on a
geometry and simplifies dealing with built-in attributes.
There are different ways to get an attribute accessor for a geometry:
* `GeometryComponent.attributes()`
* `CurvesGeometry.attributes()`
* `bke::mesh_attributes(const Mesh &)`
* `bke::pointcloud_attributes(const PointCloud &)`
All of these also have a `_for_write` variant that returns a `MutabelAttributeAccessor`.
Differential Revision: https://developer.blender.org/D15280
2022-07-08 16:16:56 +02:00
|
|
|
if (component.attributes()->is_builtin(attribute_id)) {
|
|
|
|
|
if (!dummy_component->attributes()->is_builtin(attribute_id)) {
|
2021-09-23 17:50:33 +02:00
|
|
|
/* Don't propagate built-in attributes that are not built-in on the destination
|
|
|
|
|
* component. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-18 17:16:51 +02:00
|
|
|
if (meta_data.data_type == CD_PROP_STRING) {
|
|
|
|
|
/* Propagating string attributes is not supported yet. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-01-05 14:05:30 +01:00
|
|
|
if (attribute_id.is_anonymous() &&
|
|
|
|
|
!propagation_info.propagate(attribute_id.anonymous_id())) {
|
2021-10-20 09:57:54 -05:00
|
|
|
return;
|
2021-09-23 17:50:33 +02:00
|
|
|
}
|
2021-10-20 09:57:54 -05:00
|
|
|
|
2022-06-01 14:38:06 +10:00
|
|
|
eAttrDomain domain = meta_data.domain;
|
Geometry Nodes: support instance attributes when realizing instances
This patch refactors the instance-realization code and adds new functionality.
* Named and anonymous attributes are propagated from instances to the
realized geometry. If the same attribute exists on the geometry and on an
instance, the attribute on the geometry has precedence.
* The id attribute has special handling to avoid creating the same id on many
output points. This is necessary to make e.g. the Random Value node work
as expected afterwards.
Realizing instance attributes has an effect on existing files, especially due to the
id attribute. To avoid breaking existing files, the Realize Instances node now has
a legacy option that is enabled for all already existing Realize Instances nodes.
Removing this legacy behavior does affect some existing files (although not many).
We can decide whether it's worth to remove the old behavior as a separate step.
This refactor also improves performance when realizing instances. That is mainly
due to multi-threading. See D13446 to get the file used for benchmarking. The
curve code is not as optimized as it could be yet. That's mainly because the storage
for these attributes might change soonish and it wasn't worth optimizing for the
current storage format right now.
```
1,000,000 x mesh vertex: 530 ms -> 130 ms
1,000,000 x simple cube: 1290 ms -> 190 ms
1,000,000 x point: 1000 ms -> 150 ms
1,000,000 x curve spiral: 1740 ms -> 330 ms
1,000,000 x curve line: 1110 ms -> 210 ms
10,000 x subdivided cylinder: 170 ms -> 40 ms
10 x subdivided spiral: 180 ms -> 180 ms
```
Differential Revision: https://developer.blender.org/D13446
2021-12-14 15:57:58 +01:00
|
|
|
if (dst_component_type != GEO_COMPONENT_TYPE_INSTANCES && domain == ATTR_DOMAIN_INSTANCE) {
|
|
|
|
|
domain = ATTR_DOMAIN_POINT;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 17:50:33 +02:00
|
|
|
auto add_info = [&](AttributeKind *attribute_kind) {
|
Geometry Nodes: support instance attributes when realizing instances
This patch refactors the instance-realization code and adds new functionality.
* Named and anonymous attributes are propagated from instances to the
realized geometry. If the same attribute exists on the geometry and on an
instance, the attribute on the geometry has precedence.
* The id attribute has special handling to avoid creating the same id on many
output points. This is necessary to make e.g. the Random Value node work
as expected afterwards.
Realizing instance attributes has an effect on existing files, especially due to the
id attribute. To avoid breaking existing files, the Realize Instances node now has
a legacy option that is enabled for all already existing Realize Instances nodes.
Removing this legacy behavior does affect some existing files (although not many).
We can decide whether it's worth to remove the old behavior as a separate step.
This refactor also improves performance when realizing instances. That is mainly
due to multi-threading. See D13446 to get the file used for benchmarking. The
curve code is not as optimized as it could be yet. That's mainly because the storage
for these attributes might change soonish and it wasn't worth optimizing for the
current storage format right now.
```
1,000,000 x mesh vertex: 530 ms -> 130 ms
1,000,000 x simple cube: 1290 ms -> 190 ms
1,000,000 x point: 1000 ms -> 150 ms
1,000,000 x curve spiral: 1740 ms -> 330 ms
1,000,000 x curve line: 1110 ms -> 210 ms
10,000 x subdivided cylinder: 170 ms -> 40 ms
10 x subdivided spiral: 180 ms -> 180 ms
```
Differential Revision: https://developer.blender.org/D13446
2021-12-14 15:57:58 +01:00
|
|
|
attribute_kind->domain = domain;
|
2021-09-23 17:50:33 +02:00
|
|
|
attribute_kind->data_type = meta_data.data_type;
|
|
|
|
|
};
|
|
|
|
|
auto modify_info = [&](AttributeKind *attribute_kind) {
|
|
|
|
|
attribute_kind->domain = bke::attribute_domain_highest_priority(
|
Geometry Nodes: support instance attributes when realizing instances
This patch refactors the instance-realization code and adds new functionality.
* Named and anonymous attributes are propagated from instances to the
realized geometry. If the same attribute exists on the geometry and on an
instance, the attribute on the geometry has precedence.
* The id attribute has special handling to avoid creating the same id on many
output points. This is necessary to make e.g. the Random Value node work
as expected afterwards.
Realizing instance attributes has an effect on existing files, especially due to the
id attribute. To avoid breaking existing files, the Realize Instances node now has
a legacy option that is enabled for all already existing Realize Instances nodes.
Removing this legacy behavior does affect some existing files (although not many).
We can decide whether it's worth to remove the old behavior as a separate step.
This refactor also improves performance when realizing instances. That is mainly
due to multi-threading. See D13446 to get the file used for benchmarking. The
curve code is not as optimized as it could be yet. That's mainly because the storage
for these attributes might change soonish and it wasn't worth optimizing for the
current storage format right now.
```
1,000,000 x mesh vertex: 530 ms -> 130 ms
1,000,000 x simple cube: 1290 ms -> 190 ms
1,000,000 x point: 1000 ms -> 150 ms
1,000,000 x curve spiral: 1740 ms -> 330 ms
1,000,000 x curve line: 1110 ms -> 210 ms
10,000 x subdivided cylinder: 170 ms -> 40 ms
10 x subdivided spiral: 180 ms -> 180 ms
```
Differential Revision: https://developer.blender.org/D13446
2021-12-14 15:57:58 +01:00
|
|
|
{attribute_kind->domain, domain});
|
2021-09-23 17:50:33 +02:00
|
|
|
attribute_kind->data_type = bke::attribute_data_type_highest_complexity(
|
|
|
|
|
{attribute_kind->data_type, meta_data.data_type});
|
|
|
|
|
};
|
|
|
|
|
r_attributes.add_or_modify(attribute_id, add_info, modify_info);
|
|
|
|
|
});
|
|
|
|
|
delete dummy_component;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 20:00:03 +02:00
|
|
|
static void gather_component_types_recursive(const GeometrySet &geometry_set,
|
|
|
|
|
const bool include_instances,
|
|
|
|
|
const bool ignore_empty,
|
|
|
|
|
Vector<GeometryComponentType> &r_types)
|
|
|
|
|
{
|
|
|
|
|
for (const GeometryComponent *component : geometry_set.get_components_for_read()) {
|
|
|
|
|
if (ignore_empty) {
|
|
|
|
|
if (component->is_empty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
r_types.append_non_duplicates(component->type());
|
|
|
|
|
}
|
|
|
|
|
if (!include_instances) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-17 11:39:40 +02:00
|
|
|
const blender::bke::Instances *instances = geometry_set.get_instances_for_read();
|
2021-10-26 20:00:03 +02:00
|
|
|
if (instances == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
instances->foreach_referenced_geometry([&](const GeometrySet &instance_geometry_set) {
|
|
|
|
|
gather_component_types_recursive(
|
|
|
|
|
instance_geometry_set, include_instances, ignore_empty, r_types);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blender::Vector<GeometryComponentType> GeometrySet::gather_component_types(
|
|
|
|
|
const bool include_instances, bool ignore_empty) const
|
|
|
|
|
{
|
|
|
|
|
Vector<GeometryComponentType> types;
|
|
|
|
|
gather_component_types_recursive(*this, include_instances, ignore_empty, types);
|
|
|
|
|
return types;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 10:17:00 +02:00
|
|
|
static void gather_mutable_geometry_sets(GeometrySet &geometry_set,
|
|
|
|
|
Vector<GeometrySet *> &r_geometry_sets)
|
2021-09-27 17:35:26 +02:00
|
|
|
{
|
2021-09-28 10:17:00 +02:00
|
|
|
r_geometry_sets.append(&geometry_set);
|
|
|
|
|
if (!geometry_set.has_instances()) {
|
2021-09-27 17:35:26 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/* In the future this can be improved by deduplicating instance references across different
|
|
|
|
|
* instances. */
|
2022-10-17 11:39:40 +02:00
|
|
|
Instances &instances = *geometry_set.get_instances_for_write();
|
|
|
|
|
instances.ensure_geometry_instances();
|
|
|
|
|
for (const int handle : instances.references().index_range()) {
|
|
|
|
|
if (instances.references()[handle].type() == InstanceReference::Type::GeometrySet) {
|
|
|
|
|
GeometrySet &instance_geometry = instances.geometry_set_from_reference(handle);
|
2021-09-28 10:17:00 +02:00
|
|
|
gather_mutable_geometry_sets(instance_geometry, r_geometry_sets);
|
2021-09-27 17:35:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 10:17:00 +02:00
|
|
|
void GeometrySet::modify_geometry_sets(ForeachSubGeometryCallback callback)
|
|
|
|
|
{
|
|
|
|
|
Vector<GeometrySet *> geometry_sets;
|
|
|
|
|
gather_mutable_geometry_sets(*this, geometry_sets);
|
2022-04-26 09:24:18 -05:00
|
|
|
if (geometry_sets.size() == 1) {
|
|
|
|
|
/* Avoid possible overhead and a large call stack when multithreading is pointless. */
|
|
|
|
|
callback(*geometry_sets.first());
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
blender::threading::parallel_for_each(
|
|
|
|
|
geometry_sets, [&](GeometrySet *geometry_set) { callback(*geometry_set); });
|
|
|
|
|
}
|
2021-09-28 10:17:00 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-21 10:32:42 +01:00
|
|
|
/** \} */
|
|
|
|
|
|
2020-12-02 13:25:25 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name C API
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
void BKE_geometry_set_free(GeometrySet *geometry_set)
|
|
|
|
|
{
|
|
|
|
|
delete geometry_set;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 18:22:24 +02:00
|
|
|
bool BKE_object_has_geometry_set_instances(const Object *ob)
|
2020-12-02 13:25:25 +01:00
|
|
|
{
|
2021-09-06 18:22:24 +02:00
|
|
|
const GeometrySet *geometry_set = ob->runtime.geometry_set_eval;
|
|
|
|
|
if (geometry_set == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-11-01 12:00:34 +01:00
|
|
|
for (const GeometryComponent *component : geometry_set->get_components_for_read()) {
|
|
|
|
|
if (component->is_empty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const GeometryComponentType type = component->type();
|
|
|
|
|
bool is_instance = false;
|
|
|
|
|
switch (type) {
|
|
|
|
|
case GEO_COMPONENT_TYPE_MESH:
|
|
|
|
|
is_instance = ob->type != OB_MESH;
|
|
|
|
|
break;
|
|
|
|
|
case GEO_COMPONENT_TYPE_POINT_CLOUD:
|
|
|
|
|
is_instance = ob->type != OB_POINTCLOUD;
|
|
|
|
|
break;
|
|
|
|
|
case GEO_COMPONENT_TYPE_INSTANCES:
|
|
|
|
|
is_instance = true;
|
|
|
|
|
break;
|
|
|
|
|
case GEO_COMPONENT_TYPE_VOLUME:
|
|
|
|
|
is_instance = ob->type != OB_VOLUME;
|
|
|
|
|
break;
|
|
|
|
|
case GEO_COMPONENT_TYPE_CURVE:
|
2022-02-18 09:50:29 -06:00
|
|
|
is_instance = !ELEM(ob->type, OB_CURVES_LEGACY, OB_FONT);
|
2021-11-01 12:00:34 +01:00
|
|
|
break;
|
2022-07-22 15:39:41 +02:00
|
|
|
case GEO_COMPONENT_TYPE_EDIT:
|
|
|
|
|
break;
|
2021-11-01 12:00:34 +01:00
|
|
|
}
|
|
|
|
|
if (is_instance) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-09-06 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
2020-12-02 13:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|