2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-03-08 11:41:23 -05:00
|
|
|
|
2021-09-27 10:16:38 +02:00
|
|
|
#include <mutex>
|
|
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
#include "BLI_float4x4.hh"
|
2021-12-29 11:31:58 -06:00
|
|
|
#include "BLI_index_mask.hh"
|
2021-03-08 11:41:23 -05:00
|
|
|
#include "BLI_map.hh"
|
|
|
|
|
#include "BLI_rand.hh"
|
|
|
|
|
#include "BLI_set.hh"
|
|
|
|
|
#include "BLI_span.hh"
|
2021-09-27 10:16:38 +02:00
|
|
|
#include "BLI_task.hh"
|
2021-03-08 11:41:23 -05:00
|
|
|
#include "BLI_vector.hh"
|
|
|
|
|
|
|
|
|
|
#include "DNA_collection_types.h"
|
|
|
|
|
|
2021-12-29 11:31:58 -06:00
|
|
|
#include "BKE_attribute_access.hh"
|
|
|
|
|
#include "BKE_attribute_math.hh"
|
2021-03-08 11:41:23 -05:00
|
|
|
#include "BKE_geometry_set.hh"
|
2021-09-21 14:20:54 -05:00
|
|
|
#include "BKE_geometry_set_instances.hh"
|
2021-03-08 11:41:23 -05:00
|
|
|
|
2021-09-20 12:49:11 +02:00
|
|
|
#include "attribute_access_intern.hh"
|
|
|
|
|
|
2022-03-18 10:57:45 +01:00
|
|
|
#include "BLI_cpp_type_make.hh"
|
2021-12-15 09:34:13 -06:00
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
using blender::float4x4;
|
2022-03-19 08:26:29 +01:00
|
|
|
using blender::GSpan;
|
2021-12-29 11:31:58 -06:00
|
|
|
using blender::IndexMask;
|
2021-03-08 11:41:23 -05:00
|
|
|
using blender::Map;
|
|
|
|
|
using blender::MutableSpan;
|
|
|
|
|
using blender::Set;
|
|
|
|
|
using blender::Span;
|
2021-09-21 14:20:54 -05:00
|
|
|
using blender::VectorSet;
|
2021-03-08 11:41:23 -05:00
|
|
|
|
2022-03-18 10:57:45 +01:00
|
|
|
BLI_CPP_TYPE_MAKE(InstanceReference, InstanceReference, CPPTypeFlags::None)
|
2021-12-15 09:34:13 -06:00
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Geometry Component Implementation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2021-03-10 11:53:17 +01:00
|
|
|
InstancesComponent::InstancesComponent() : GeometryComponent(GEO_COMPONENT_TYPE_INSTANCES)
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeometryComponent *InstancesComponent::copy() const
|
|
|
|
|
{
|
|
|
|
|
InstancesComponent *new_component = new InstancesComponent();
|
2021-05-04 10:16:24 +02:00
|
|
|
new_component->instance_reference_handles_ = instance_reference_handles_;
|
|
|
|
|
new_component->instance_transforms_ = instance_transforms_;
|
|
|
|
|
new_component->references_ = references_;
|
2021-12-01 12:36:49 -05:00
|
|
|
new_component->attributes_ = attributes_;
|
2021-03-08 11:41:23 -05:00
|
|
|
return new_component;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 10:16:24 +02:00
|
|
|
void InstancesComponent::reserve(int min_capacity)
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
instance_reference_handles_.reserve(min_capacity);
|
|
|
|
|
instance_transforms_.reserve(min_capacity);
|
2021-11-30 10:59:11 -05:00
|
|
|
attributes_.reallocate(min_capacity);
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-08 23:57:36 -05:00
|
|
|
void InstancesComponent::resize(int capacity)
|
|
|
|
|
{
|
|
|
|
|
instance_reference_handles_.resize(capacity);
|
|
|
|
|
instance_transforms_.resize(capacity);
|
2021-11-30 10:59:11 -05:00
|
|
|
attributes_.reallocate(capacity);
|
2021-05-08 23:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-04 10:16:24 +02:00
|
|
|
void InstancesComponent::clear()
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
instance_reference_handles_.clear();
|
|
|
|
|
instance_transforms_.clear();
|
2021-11-30 10:59:11 -05:00
|
|
|
attributes_.clear();
|
2021-05-04 10:16:24 +02:00
|
|
|
references_.clear();
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-10-26 12:50:39 -05:00
|
|
|
void InstancesComponent::add_instance(const int instance_handle, const float4x4 &transform)
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
BLI_assert(instance_handle >= 0);
|
|
|
|
|
BLI_assert(instance_handle < references_.size());
|
|
|
|
|
instance_reference_handles_.append(instance_handle);
|
|
|
|
|
instance_transforms_.append(transform);
|
2022-05-11 12:59:58 +10:00
|
|
|
attributes_.reallocate(this->instances_num());
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-04 10:16:24 +02:00
|
|
|
blender::Span<int> InstancesComponent::instance_reference_handles() const
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
return instance_reference_handles_;
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-08 23:57:36 -05:00
|
|
|
blender::MutableSpan<int> InstancesComponent::instance_reference_handles()
|
|
|
|
|
{
|
|
|
|
|
return instance_reference_handles_;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-04 10:16:24 +02:00
|
|
|
blender::MutableSpan<blender::float4x4> InstancesComponent::instance_transforms()
|
|
|
|
|
{
|
|
|
|
|
return instance_transforms_;
|
|
|
|
|
}
|
|
|
|
|
blender::Span<blender::float4x4> InstancesComponent::instance_transforms() const
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
return instance_transforms_;
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 14:20:54 -05:00
|
|
|
GeometrySet &InstancesComponent::geometry_set_from_reference(const int reference_index)
|
|
|
|
|
{
|
2021-09-27 17:35:26 +02:00
|
|
|
/* If this assert fails, it means #ensure_geometry_instances must be called first or that the
|
|
|
|
|
* reference can't be converted to a geometry set. */
|
2021-09-21 14:20:54 -05:00
|
|
|
BLI_assert(references_[reference_index].type() == InstanceReference::Type::GeometrySet);
|
|
|
|
|
|
|
|
|
|
/* The const cast is okay because the instance's hash in the set
|
|
|
|
|
* is not changed by adjusting the data inside the geometry set. */
|
|
|
|
|
return const_cast<GeometrySet &>(references_[reference_index].geometry_set());
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 18:22:24 +02:00
|
|
|
int InstancesComponent::add_reference(const InstanceReference &reference)
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
return references_.index_of_or_add_as(reference);
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-04 10:16:24 +02:00
|
|
|
blender::Span<InstanceReference> InstancesComponent::references() const
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
return references_;
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-29 11:31:58 -06:00
|
|
|
template<typename T>
|
|
|
|
|
static void copy_data_based_on_mask(Span<T> src, MutableSpan<T> dst, IndexMask mask)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(src.data() != dst.data());
|
|
|
|
|
using namespace blender;
|
|
|
|
|
threading::parallel_for(mask.index_range(), 1024, [&](IndexRange range) {
|
|
|
|
|
for (const int i : range) {
|
|
|
|
|
dst[i] = src[mask[i]];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 13:19:04 -06:00
|
|
|
void InstancesComponent::remove_instances(const IndexMask mask)
|
2021-12-29 11:31:58 -06:00
|
|
|
{
|
|
|
|
|
using namespace blender;
|
2022-01-03 13:19:04 -06:00
|
|
|
if (mask.is_range() && mask.as_range().start() == 0) {
|
2021-12-29 11:31:58 -06:00
|
|
|
/* Deleting from the end of the array can be much faster since no data has to be shifted. */
|
2022-01-03 13:19:04 -06:00
|
|
|
this->resize(mask.size());
|
2021-12-29 11:31:58 -06:00
|
|
|
this->remove_unused_references();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 13:19:04 -06:00
|
|
|
Vector<int> new_handles(mask.size());
|
|
|
|
|
copy_data_based_on_mask<int>(this->instance_reference_handles(), new_handles, mask);
|
2021-12-29 11:31:58 -06:00
|
|
|
instance_reference_handles_ = std::move(new_handles);
|
2022-01-03 13:19:04 -06:00
|
|
|
Vector<float4x4> new_transforms(mask.size());
|
|
|
|
|
copy_data_based_on_mask<float4x4>(this->instance_transforms(), new_transforms, mask);
|
2021-12-29 11:31:58 -06:00
|
|
|
instance_transforms_ = std::move(new_transforms);
|
|
|
|
|
|
|
|
|
|
const bke::CustomDataAttributes &src_attributes = attributes_;
|
|
|
|
|
|
|
|
|
|
bke::CustomDataAttributes dst_attributes;
|
2022-01-03 13:19:04 -06:00
|
|
|
dst_attributes.reallocate(mask.size());
|
2021-12-29 11:31:58 -06:00
|
|
|
|
|
|
|
|
src_attributes.foreach_attribute(
|
|
|
|
|
[&](const bke::AttributeIDRef &id, const AttributeMetaData &meta_data) {
|
|
|
|
|
if (!id.should_be_kept()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GSpan src = *src_attributes.get_for_read(id);
|
|
|
|
|
dst_attributes.create(id, meta_data.data_type);
|
2022-03-19 08:26:29 +01:00
|
|
|
GMutableSpan dst = *dst_attributes.get_for_write(id);
|
2021-12-29 11:31:58 -06:00
|
|
|
|
|
|
|
|
attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
|
|
|
|
|
using T = decltype(dummy);
|
2022-01-03 13:19:04 -06:00
|
|
|
copy_data_based_on_mask<T>(src.typed<T>(), dst.typed<T>(), mask);
|
2021-12-29 11:31:58 -06:00
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
ATTR_DOMAIN_INSTANCE);
|
|
|
|
|
|
|
|
|
|
attributes_ = std::move(dst_attributes);
|
|
|
|
|
this->remove_unused_references();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 10:16:38 +02:00
|
|
|
void InstancesComponent::remove_unused_references()
|
|
|
|
|
{
|
|
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
|
|
|
|
|
2022-05-11 12:59:58 +10:00
|
|
|
const int tot_instances = this->instances_num();
|
2021-09-27 10:16:38 +02:00
|
|
|
const int tot_references_before = references_.size();
|
|
|
|
|
|
|
|
|
|
if (tot_instances == 0) {
|
|
|
|
|
/* If there are no instances, no reference is needed. */
|
|
|
|
|
references_.clear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (tot_references_before == 1) {
|
|
|
|
|
/* There is only one reference and at least one instance. So the only existing reference is
|
|
|
|
|
* used. Nothing to do here. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array<bool> usage_by_handle(tot_references_before, false);
|
|
|
|
|
std::mutex mutex;
|
|
|
|
|
|
|
|
|
|
/* Loop over all instances to see which references are used. */
|
|
|
|
|
threading::parallel_for(IndexRange(tot_instances), 1000, [&](IndexRange range) {
|
|
|
|
|
/* Use local counter to avoid lock contention. */
|
|
|
|
|
Array<bool> local_usage_by_handle(tot_references_before, false);
|
|
|
|
|
|
|
|
|
|
for (const int i : range) {
|
|
|
|
|
const int handle = instance_reference_handles_[i];
|
|
|
|
|
BLI_assert(handle >= 0 && handle < tot_references_before);
|
|
|
|
|
local_usage_by_handle[handle] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::lock_guard lock{mutex};
|
|
|
|
|
for (const int i : IndexRange(tot_references_before)) {
|
|
|
|
|
usage_by_handle[i] |= local_usage_by_handle[i];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!usage_by_handle.as_span().contains(false)) {
|
|
|
|
|
/* All references are used. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create new references and a mapping for the handles. */
|
|
|
|
|
Vector<int> handle_mapping;
|
|
|
|
|
VectorSet<InstanceReference> new_references;
|
|
|
|
|
int next_new_handle = 0;
|
|
|
|
|
bool handles_have_to_be_updated = false;
|
|
|
|
|
for (const int old_handle : IndexRange(tot_references_before)) {
|
|
|
|
|
if (!usage_by_handle[old_handle]) {
|
|
|
|
|
/* Add some dummy value. It won't be read again. */
|
|
|
|
|
handle_mapping.append(-1);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const InstanceReference &reference = references_[old_handle];
|
|
|
|
|
handle_mapping.append(next_new_handle);
|
|
|
|
|
new_references.add_new(reference);
|
|
|
|
|
if (old_handle != next_new_handle) {
|
|
|
|
|
handles_have_to_be_updated = true;
|
|
|
|
|
}
|
|
|
|
|
next_new_handle++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
references_ = new_references;
|
|
|
|
|
|
|
|
|
|
if (!handles_have_to_be_updated) {
|
|
|
|
|
/* All remaining handles are the same as before, so they don't have to be updated. This happens
|
|
|
|
|
* when unused handles are only at the end. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Update handles of instances. */
|
|
|
|
|
threading::parallel_for(IndexRange(tot_instances), 1000, [&](IndexRange range) {
|
|
|
|
|
for (const int i : range) {
|
|
|
|
|
instance_reference_handles_[i] = handle_mapping[instance_reference_handles_[i]];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-11 12:59:58 +10:00
|
|
|
int InstancesComponent::instances_num() const
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
return instance_transforms_.size();
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-11 12:59:58 +10:00
|
|
|
int InstancesComponent::references_num() const
|
2021-09-21 14:20:54 -05:00
|
|
|
{
|
|
|
|
|
return references_.size();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
bool InstancesComponent::is_empty() const
|
|
|
|
|
{
|
2021-05-04 10:16:24 +02:00
|
|
|
return this->instance_reference_handles_.size() == 0;
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-08 17:35:06 +02:00
|
|
|
bool InstancesComponent::owns_direct_data() const
|
|
|
|
|
{
|
2021-09-06 18:22:24 +02:00
|
|
|
for (const InstanceReference &reference : references_) {
|
|
|
|
|
if (!reference.owns_direct_data()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-08 17:35:06 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void InstancesComponent::ensure_owns_direct_data()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
2021-09-06 18:22:24 +02:00
|
|
|
for (const InstanceReference &const_reference : references_) {
|
|
|
|
|
/* Const cast is fine because we are not changing anything that would change the hash of the
|
|
|
|
|
* reference. */
|
|
|
|
|
InstanceReference &reference = const_cast<InstanceReference &>(const_reference);
|
|
|
|
|
reference.ensure_owns_direct_data();
|
|
|
|
|
}
|
2021-04-08 17:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
static blender::Array<int> generate_unique_instance_ids(Span<int> original_ids)
|
|
|
|
|
{
|
|
|
|
|
using namespace blender;
|
|
|
|
|
Array<int> unique_ids(original_ids.size());
|
|
|
|
|
|
|
|
|
|
Set<int> used_unique_ids;
|
|
|
|
|
used_unique_ids.reserve(original_ids.size());
|
|
|
|
|
Vector<int> instances_with_id_collision;
|
|
|
|
|
for (const int instance_index : original_ids.index_range()) {
|
|
|
|
|
const int original_id = original_ids[instance_index];
|
|
|
|
|
if (used_unique_ids.add(original_id)) {
|
|
|
|
|
/* The original id has not been used by another instance yet. */
|
|
|
|
|
unique_ids[instance_index] = original_id;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* The original id of this instance collided with a previous instance, it needs to be looked
|
|
|
|
|
* at again in a second pass. Don't generate a new random id here, because this might collide
|
|
|
|
|
* with other existing ids. */
|
|
|
|
|
instances_with_id_collision.append(instance_index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<int, RandomNumberGenerator> generator_by_original_id;
|
|
|
|
|
for (const int instance_index : instances_with_id_collision) {
|
|
|
|
|
const int original_id = original_ids[instance_index];
|
|
|
|
|
RandomNumberGenerator &rng = generator_by_original_id.lookup_or_add_cb(original_id, [&]() {
|
|
|
|
|
RandomNumberGenerator rng;
|
|
|
|
|
rng.seed_random(original_id);
|
|
|
|
|
return rng;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const int max_iteration = 100;
|
|
|
|
|
for (int iteration = 0;; iteration++) {
|
|
|
|
|
/* Try generating random numbers until an unused one has been found. */
|
|
|
|
|
const int random_id = rng.get_int32();
|
|
|
|
|
if (used_unique_ids.add(random_id)) {
|
|
|
|
|
/* This random id is not used by another instance. */
|
|
|
|
|
unique_ids[instance_index] = random_id;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (iteration == max_iteration) {
|
|
|
|
|
/* It seems to be very unlikely that we ever run into this case (assuming there are less
|
|
|
|
|
* than 2^30 instances). However, if that happens, it's better to use an id that is not
|
|
|
|
|
* unique than to be stuck in an infinite loop. */
|
|
|
|
|
unique_ids[instance_index] = original_id;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return unique_ids;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blender::Span<int> InstancesComponent::almost_unique_ids() const
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard lock(almost_unique_ids_mutex_);
|
2021-12-01 09:27:27 -05:00
|
|
|
std::optional<GSpan> instance_ids_gspan = attributes_.get_for_read("id");
|
|
|
|
|
if (instance_ids_gspan) {
|
|
|
|
|
Span<int> instance_ids = instance_ids_gspan->typed<int>();
|
|
|
|
|
if (almost_unique_ids_.size() != instance_ids.size()) {
|
|
|
|
|
almost_unique_ids_ = generate_unique_instance_ids(instance_ids);
|
2021-10-26 12:50:39 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2022-05-11 12:59:58 +10:00
|
|
|
almost_unique_ids_.reinitialize(this->instances_num());
|
2021-12-01 09:27:27 -05:00
|
|
|
for (const int i : almost_unique_ids_.index_range()) {
|
|
|
|
|
almost_unique_ids_[i] = i;
|
2021-10-26 12:50:39 -05:00
|
|
|
}
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
return almost_unique_ids_;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 14:38:06 +10:00
|
|
|
int InstancesComponent::attribute_domain_num(const eAttrDomain domain) const
|
2021-09-20 12:49:11 +02:00
|
|
|
{
|
2021-11-19 17:53:48 +01:00
|
|
|
if (domain != ATTR_DOMAIN_INSTANCE) {
|
2021-09-20 12:49:11 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2022-05-11 12:59:58 +10:00
|
|
|
return this->instances_num();
|
2021-09-20 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-19 17:53:48 +01:00
|
|
|
blender::bke::CustomDataAttributes &InstancesComponent::attributes()
|
|
|
|
|
{
|
|
|
|
|
return this->attributes_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const blender::bke::CustomDataAttributes &InstancesComponent::attributes() const
|
|
|
|
|
{
|
|
|
|
|
return this->attributes_;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-20 12:49:11 +02:00
|
|
|
namespace blender::bke {
|
|
|
|
|
|
|
|
|
|
static float3 get_transform_position(const float4x4 &transform)
|
|
|
|
|
{
|
|
|
|
|
return transform.translation();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void set_transform_position(float4x4 &transform, const float3 position)
|
|
|
|
|
{
|
|
|
|
|
copy_v3_v3(transform.values[3], position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class InstancePositionAttributeProvider final : public BuiltinAttributeProvider {
|
|
|
|
|
public:
|
|
|
|
|
InstancePositionAttributeProvider()
|
|
|
|
|
: BuiltinAttributeProvider(
|
2021-11-19 17:53:48 +01:00
|
|
|
"position", ATTR_DOMAIN_INSTANCE, CD_PROP_FLOAT3, NonCreatable, Writable, NonDeletable)
|
2021-09-20 12:49:11 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
Geometry Nodes: refactor virtual array system
Goals of this refactor:
* Simplify creating virtual arrays.
* Simplify passing virtual arrays around.
* Simplify converting between typed and generic virtual arrays.
* Reduce memory allocations.
As a quick reminder, a virtual arrays is a data structure that behaves like an
array (i.e. it can be accessed using an index). However, it may not actually
be stored as array internally. The two most important implementations
of virtual arrays are those that correspond to an actual plain array and those
that have the same value for every index. However, many more
implementations exist for various reasons (interfacing with legacy attributes,
unified iterator over all points in multiple splines, ...).
With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and
`GVMutableArray`) can be used like "normal values". They typically live
on the stack. Before, they were usually inside a `std::unique_ptr`. This makes
passing them around much easier. Creation of new virtual arrays is also
much simpler now due to some constructors. Memory allocations are
reduced by making use of small object optimization inside the core types.
Previously, `VArray` was a class with virtual methods that had to be overridden
to change the behavior of a the virtual array. Now,`VArray` has a fixed size
and has no virtual methods. Instead it contains a `VArrayImpl` that is
similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly,
unless a new virtual array implementation is added.
To support the small object optimization for many `VArrayImpl` classes,
a new `blender::Any` type is added. It is similar to `std::any` with two
additional features. It has an adjustable inline buffer size and alignment.
The inline buffer size of `std::any` can't be relied on and is usually too
small for our use case here. Furthermore, `blender::Any` can store
additional user-defined type information without increasing the
stack size.
Differential Revision: https://developer.blender.org/D12986
2021-11-16 10:15:51 +01:00
|
|
|
GVArray try_get_for_read(const GeometryComponent &component) const final
|
2021-09-20 12:49:11 +02:00
|
|
|
{
|
|
|
|
|
const InstancesComponent &instances_component = static_cast<const InstancesComponent &>(
|
|
|
|
|
component);
|
|
|
|
|
Span<float4x4> transforms = instances_component.instance_transforms();
|
Geometry Nodes: refactor virtual array system
Goals of this refactor:
* Simplify creating virtual arrays.
* Simplify passing virtual arrays around.
* Simplify converting between typed and generic virtual arrays.
* Reduce memory allocations.
As a quick reminder, a virtual arrays is a data structure that behaves like an
array (i.e. it can be accessed using an index). However, it may not actually
be stored as array internally. The two most important implementations
of virtual arrays are those that correspond to an actual plain array and those
that have the same value for every index. However, many more
implementations exist for various reasons (interfacing with legacy attributes,
unified iterator over all points in multiple splines, ...).
With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and
`GVMutableArray`) can be used like "normal values". They typically live
on the stack. Before, they were usually inside a `std::unique_ptr`. This makes
passing them around much easier. Creation of new virtual arrays is also
much simpler now due to some constructors. Memory allocations are
reduced by making use of small object optimization inside the core types.
Previously, `VArray` was a class with virtual methods that had to be overridden
to change the behavior of a the virtual array. Now,`VArray` has a fixed size
and has no virtual methods. Instead it contains a `VArrayImpl` that is
similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly,
unless a new virtual array implementation is added.
To support the small object optimization for many `VArrayImpl` classes,
a new `blender::Any` type is added. It is similar to `std::any` with two
additional features. It has an adjustable inline buffer size and alignment.
The inline buffer size of `std::any` can't be relied on and is usually too
small for our use case here. Furthermore, `blender::Any` can store
additional user-defined type information without increasing the
stack size.
Differential Revision: https://developer.blender.org/D12986
2021-11-16 10:15:51 +01:00
|
|
|
return VArray<float3>::ForDerivedSpan<float4x4, get_transform_position>(transforms);
|
2021-09-20 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-29 09:28:31 +02:00
|
|
|
WriteAttributeLookup try_get_for_write(GeometryComponent &component) const final
|
2021-09-20 12:49:11 +02:00
|
|
|
{
|
|
|
|
|
InstancesComponent &instances_component = static_cast<InstancesComponent &>(component);
|
|
|
|
|
MutableSpan<float4x4> transforms = instances_component.instance_transforms();
|
Geometry Nodes: refactor virtual array system
Goals of this refactor:
* Simplify creating virtual arrays.
* Simplify passing virtual arrays around.
* Simplify converting between typed and generic virtual arrays.
* Reduce memory allocations.
As a quick reminder, a virtual arrays is a data structure that behaves like an
array (i.e. it can be accessed using an index). However, it may not actually
be stored as array internally. The two most important implementations
of virtual arrays are those that correspond to an actual plain array and those
that have the same value for every index. However, many more
implementations exist for various reasons (interfacing with legacy attributes,
unified iterator over all points in multiple splines, ...).
With this refactor the core types (`VArray`, `GVArray`, `VMutableArray` and
`GVMutableArray`) can be used like "normal values". They typically live
on the stack. Before, they were usually inside a `std::unique_ptr`. This makes
passing them around much easier. Creation of new virtual arrays is also
much simpler now due to some constructors. Memory allocations are
reduced by making use of small object optimization inside the core types.
Previously, `VArray` was a class with virtual methods that had to be overridden
to change the behavior of a the virtual array. Now,`VArray` has a fixed size
and has no virtual methods. Instead it contains a `VArrayImpl` that is
similar to the old `VArray`. `VArrayImpl` should rarely ever be used directly,
unless a new virtual array implementation is added.
To support the small object optimization for many `VArrayImpl` classes,
a new `blender::Any` type is added. It is similar to `std::any` with two
additional features. It has an adjustable inline buffer size and alignment.
The inline buffer size of `std::any` can't be relied on and is usually too
small for our use case here. Furthermore, `blender::Any` can store
additional user-defined type information without increasing the
stack size.
Differential Revision: https://developer.blender.org/D12986
2021-11-16 10:15:51 +01:00
|
|
|
return {VMutableArray<float3>::ForDerivedSpan<float4x4,
|
|
|
|
|
get_transform_position,
|
|
|
|
|
set_transform_position>(transforms),
|
|
|
|
|
domain_};
|
2021-09-20 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool try_delete(GeometryComponent &UNUSED(component)) const final
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool try_create(GeometryComponent &UNUSED(component),
|
|
|
|
|
const AttributeInit &UNUSED(initializer)) const final
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool exists(const GeometryComponent &UNUSED(component)) const final
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static ComponentAttributeProviders create_attribute_providers_for_instances()
|
|
|
|
|
{
|
|
|
|
|
static InstancePositionAttributeProvider position;
|
2021-11-19 17:53:48 +01:00
|
|
|
static CustomDataAccessInfo instance_custom_data_access = {
|
|
|
|
|
[](GeometryComponent &component) -> CustomData * {
|
|
|
|
|
InstancesComponent &inst = static_cast<InstancesComponent &>(component);
|
|
|
|
|
return &inst.attributes().data;
|
|
|
|
|
},
|
|
|
|
|
[](const GeometryComponent &component) -> const CustomData * {
|
|
|
|
|
const InstancesComponent &inst = static_cast<const InstancesComponent &>(component);
|
|
|
|
|
return &inst.attributes().data;
|
|
|
|
|
},
|
|
|
|
|
nullptr};
|
|
|
|
|
|
2021-12-01 09:27:27 -05:00
|
|
|
/**
|
|
|
|
|
* IDs of the instances. They are used for consistency over multiple frames for things like
|
|
|
|
|
* motion blur. Proper stable ID data that actually helps when rendering can only be generated
|
|
|
|
|
* in some situations, so this vector is allowed to be empty, in which case the index of each
|
|
|
|
|
* instance will be used for the final ID.
|
|
|
|
|
*/
|
|
|
|
|
static BuiltinCustomDataLayerProvider id("id",
|
|
|
|
|
ATTR_DOMAIN_INSTANCE,
|
|
|
|
|
CD_PROP_INT32,
|
|
|
|
|
CD_PROP_INT32,
|
|
|
|
|
BuiltinAttributeProvider::Creatable,
|
|
|
|
|
BuiltinAttributeProvider::Writable,
|
|
|
|
|
BuiltinAttributeProvider::Deletable,
|
|
|
|
|
instance_custom_data_access,
|
|
|
|
|
make_array_read_attribute<int>,
|
|
|
|
|
make_array_write_attribute<int>,
|
|
|
|
|
nullptr);
|
|
|
|
|
|
2021-11-19 17:53:48 +01:00
|
|
|
static CustomDataAttributeProvider instance_custom_data(ATTR_DOMAIN_INSTANCE,
|
|
|
|
|
instance_custom_data_access);
|
|
|
|
|
|
|
|
|
|
return ComponentAttributeProviders({&position, &id}, {&instance_custom_data});
|
2021-09-20 12:49:11 +02:00
|
|
|
}
|
|
|
|
|
} // namespace blender::bke
|
|
|
|
|
|
|
|
|
|
const blender::bke::ComponentAttributeProviders *InstancesComponent::get_attribute_providers()
|
|
|
|
|
const
|
|
|
|
|
{
|
|
|
|
|
static blender::bke::ComponentAttributeProviders providers =
|
|
|
|
|
blender::bke::create_attribute_providers_for_instances();
|
|
|
|
|
return &providers;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
/** \} */
|