diff options
author | Jacques Lucke <jacques@blender.org> | 2022-07-08 17:16:56 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-07-08 17:16:56 +0300 |
commit | b876ce2a4a4638142439a7cf265a0780491ae4cc (patch) | |
tree | 871d71eb6d1cf215869fc941c831c81bcacc6433 /source/blender/nodes/geometry/nodes/node_geo_accumulate_field.cc | |
parent | f391e8f316bd29b700cef874a59cf3b64203d70c (diff) |
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
Diffstat (limited to 'source/blender/nodes/geometry/nodes/node_geo_accumulate_field.cc')
-rw-r--r-- | source/blender/nodes/geometry/nodes/node_geo_accumulate_field.cc | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/source/blender/nodes/geometry/nodes/node_geo_accumulate_field.cc b/source/blender/nodes/geometry/nodes/node_geo_accumulate_field.cc index be7b3446125..58fbfb5a000 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_accumulate_field.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_accumulate_field.cc @@ -217,16 +217,20 @@ template<typename T> class AccumulateFieldInput final : public GeometryFieldInpu IndexMask UNUSED(mask)) const final { const GeometryComponentFieldContext field_context{component, source_domain_}; - const int domain_num = component.attribute_domain_num(field_context.domain()); + const int domain_size = component.attribute_domain_size(field_context.domain()); + if (domain_size == 0) { + return {}; + } + const AttributeAccessor attributes = *component.attributes(); - fn::FieldEvaluator evaluator{field_context, domain_num}; + fn::FieldEvaluator evaluator{field_context, domain_size}; evaluator.add(input_); evaluator.add(group_index_); evaluator.evaluate(); const VArray<T> values = evaluator.get_evaluated<T>(0); const VArray<int> group_indices = evaluator.get_evaluated<int>(1); - Array<T> accumulations_out(domain_num); + Array<T> accumulations_out(domain_size); if (group_indices.is_single()) { T accumulation = T(); @@ -261,7 +265,7 @@ template<typename T> class AccumulateFieldInput final : public GeometryFieldInpu } } - return component.attribute_try_adapt_domain<T>( + return attributes.adapt_domain<T>( VArray<T>::ForContainer(std::move(accumulations_out)), source_domain_, domain); } @@ -303,9 +307,13 @@ template<typename T> class TotalFieldInput final : public GeometryFieldInput { IndexMask UNUSED(mask)) const final { const GeometryComponentFieldContext field_context{component, source_domain_}; - const int domain_num = component.attribute_domain_num(field_context.domain()); + const int domain_size = component.attribute_domain_size(field_context.domain()); + if (domain_size == 0) { + return {}; + } + const AttributeAccessor attributes = *component.attributes(); - fn::FieldEvaluator evaluator{field_context, domain_num}; + fn::FieldEvaluator evaluator{field_context, domain_size}; evaluator.add(input_); evaluator.add(group_index_); evaluator.evaluate(); @@ -317,10 +325,10 @@ template<typename T> class TotalFieldInput final : public GeometryFieldInput { for (const int i : values.index_range()) { accumulation = values[i] + accumulation; } - return VArray<T>::ForSingle(accumulation, domain_num); + return VArray<T>::ForSingle(accumulation, domain_size); } - Array<T> accumulations_out(domain_num); + Array<T> accumulations_out(domain_size); Map<int, T> accumulations; for (const int i : values.index_range()) { T &value = accumulations.lookup_or_add_default(group_indices[i]); @@ -330,7 +338,7 @@ template<typename T> class TotalFieldInput final : public GeometryFieldInput { accumulations_out[i] = accumulations.lookup(group_indices[i]); } - return component.attribute_try_adapt_domain<T>( + return attributes.adapt_domain<T>( VArray<T>::ForContainer(std::move(accumulations_out)), source_domain_, domain); } |