From 98db4cc6391df8c8b21516bbdbc0af8f493a15b3 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Fri, 12 Feb 2021 17:41:28 +0100 Subject: Fix T84899: instance ids are not unique in common cases Ids stored in the `id` attribute cannot be assumed to be unique. While they might be unique in some cases, this is not something that can be guaranteed in general. For some use cases (e.g. generating "stable randomness" on points) uniqueness is not important. To support features like motion blur, unique ids are important though. This patch implements a simple algorithm that turns non-unique ids into unique ones. It might fail to do so under very unlikely circumstances, in which it returns non-unique ids instead of possibly going into an endless loop. Here are some requirements I set for the algorithm: * Ids that are unique already, must not be changed. * The same input should generate the same output. * Handle cases when all ids are different and when all ids are the same equally well (in expected linear time). * Small changes in the input id array should ideally only have a small impact on the output id array. The reported bug happened because cycles found multiple objects with the same id and thought that it was a single object that moved on every check. Differential Revision: https://developer.blender.org/D10402 --- source/blender/blenkernel/BKE_geometry_set.hh | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'source/blender/blenkernel/BKE_geometry_set.hh') diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh index 31739465afd..6f6269be208 100644 --- a/source/blender/blenkernel/BKE_geometry_set.hh +++ b/source/blender/blenkernel/BKE_geometry_set.hh @@ -429,6 +429,13 @@ class InstancesComponent : public GeometryComponent { blender::Vector ids_; blender::Vector instanced_data_; + /* These almost unique ids are generated based on `ids_`, which might not contain unique ids at + * all. They are *almost* unique, because under certain very unlikely circumstances, they are not + * unique. Code using these ids should not crash when they are not unique but can generally + * expect them to be unique. */ + mutable std::mutex almost_unique_ids_mutex_; + mutable blender::Array almost_unique_ids_; + public: InstancesComponent(); ~InstancesComponent() = default; @@ -445,6 +452,8 @@ class InstancesComponent : public GeometryComponent { blender::MutableSpan transforms(); int instances_amount() const; + blender::Span almost_unique_ids() const; + bool is_empty() const final; static constexpr inline GeometryComponentType static_type = GeometryComponentType::Instances; -- cgit v1.2.3