2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-03-08 11:41:23 -05:00
|
|
|
|
|
|
|
|
#include "BKE_geometry_set.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_volume.hh"
|
2021-03-08 11:41:23 -05:00
|
|
|
|
2023-06-15 22:18:28 +02:00
|
|
|
namespace blender::bke {
|
|
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Geometry Component Implementation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-06-15 22:18:28 +02:00
|
|
|
VolumeComponent::VolumeComponent() : GeometryComponent(GeometryComponent::Type::Volume) {}
|
2021-03-08 11:41:23 -05:00
|
|
|
|
|
|
|
|
VolumeComponent::~VolumeComponent()
|
|
|
|
|
{
|
|
|
|
|
this->clear();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 11:23:00 +01:00
|
|
|
GeometryComponentPtr VolumeComponent::copy() const
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
|
|
|
|
VolumeComponent *new_component = new VolumeComponent();
|
|
|
|
|
if (volume_ != nullptr) {
|
2023-04-19 15:49:13 -04:00
|
|
|
new_component->volume_ = BKE_volume_copy_for_eval(volume_);
|
2021-03-08 11:41:23 -05:00
|
|
|
new_component->ownership_ = GeometryOwnershipType::Owned;
|
|
|
|
|
}
|
2023-12-01 11:23:00 +01:00
|
|
|
return GeometryComponentPtr(new_component);
|
2021-03-08 11:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VolumeComponent::clear()
|
|
|
|
|
{
|
BLI: support weak users and version in implicit sharing info
The main goal of these changes is to support checking if some data has
been changed over time. This is used by the WIP simulation nodes during
baking to detect which attributes have to be stored in every frame because
they have changed.
By using a combination of a weak user count and a version counter, it is
possible to detect that an attribute (or any data controlled by implicit
sharing) has not been changed with O(1) memory and time. It's still
possible that the data has been changed multiple times and is the same
in the end and beginning of course. That wouldn't be detected using this
mechanism.
The `ImplicitSharingInfo` struct has a new weak user count. A weak
reference is one that does not keep the referenced data alive, but makes sure
that the `ImplicitSharingInfo` itself is not deleted. If some piece of
data has one strong and multiple weak users, it is still mutable. If the
strong user count goes down to zero, the referenced data is freed.
Remaining weak users can check for this condition using `is_expired`.
This is a bit similar to `std::weak_ptr` but there is an important difference:
a weak user can not become a strong user while one can create a `shared_ptr`
from a `weak_ptr`. This restriction is necessary, because some code might
be changing the referenced data assuming that it is the only owner. If
another thread suddenly adds a new owner, the data would be shared again
and the first thread would not have been allowed to modify the data in
the first place.
There is also a new integer version counter in `ImplicitSharingInfo`.
It is incremented whenever some code wants to modify the referenced data.
Obviously, this can only be done when the data is not shared because then
it would be immutable. By comparing an old and new version number of the
same sharing info, one can check if the data has been modified. One has
to keep a weak reference to the sharing info together with the old version
number to ensure that the new sharing info is still the same as the old one.
Without this, it can happen that the sharing info was freed and a new
one was allocated at the same pointer address. Using a strong reference
for this purpose does not work, because then the data would never be
modified because it's shared.
2023-04-28 12:03:42 +02:00
|
|
|
BLI_assert(this->is_mutable() || this->is_expired());
|
2021-03-08 11:41:23 -05:00
|
|
|
if (volume_ != nullptr) {
|
|
|
|
|
if (ownership_ == GeometryOwnershipType::Owned) {
|
|
|
|
|
BKE_id_free(nullptr, volume_);
|
|
|
|
|
}
|
|
|
|
|
volume_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool VolumeComponent::has_volume() const
|
|
|
|
|
{
|
|
|
|
|
return volume_ != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VolumeComponent::replace(Volume *volume, GeometryOwnershipType ownership)
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
this->clear();
|
|
|
|
|
volume_ = volume;
|
|
|
|
|
ownership_ = ownership;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Volume *VolumeComponent::release()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
Volume *volume = volume_;
|
|
|
|
|
volume_ = nullptr;
|
|
|
|
|
return volume;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 17:09:18 +02:00
|
|
|
const Volume *VolumeComponent::get() const
|
2021-03-08 11:41:23 -05:00
|
|
|
{
|
|
|
|
|
return volume_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Volume *VolumeComponent::get_for_write()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
if (ownership_ == GeometryOwnershipType::ReadOnly) {
|
2023-04-19 15:49:13 -04:00
|
|
|
volume_ = BKE_volume_copy_for_eval(volume_);
|
2021-03-08 11:41:23 -05:00
|
|
|
ownership_ = GeometryOwnershipType::Owned;
|
|
|
|
|
}
|
|
|
|
|
return volume_;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 17:35:06 +02:00
|
|
|
bool VolumeComponent::owns_direct_data() const
|
|
|
|
|
{
|
|
|
|
|
return ownership_ == GeometryOwnershipType::Owned;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VolumeComponent::ensure_owns_direct_data()
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(this->is_mutable());
|
|
|
|
|
if (ownership_ != GeometryOwnershipType::Owned) {
|
2023-12-19 12:51:37 -05:00
|
|
|
if (volume_) {
|
|
|
|
|
volume_ = BKE_volume_copy_for_eval(volume_);
|
|
|
|
|
}
|
2021-04-08 17:35:06 +02:00
|
|
|
ownership_ = GeometryOwnershipType::Owned;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Core: introduce MemoryCounter API
We often have the situation where it would be good if we could easily estimate
the memory usage of some value (e.g. a mesh, or volume). Examples of where we
ran into this in the past:
* Undo step size.
* Caching of volume grids.
* Caching of loaded geometries for import geometry nodes.
Generally, most caching systems would benefit from the ability to know how much
memory they currently use to make better decisions about which data to free and
when. The goal of this patch is to introduce a simple general API to count the
memory usage that is independent of any specific caching system. I'm doing this
to "fix" the chicken and egg problem that caches need to know the memory usage,
but we don't really need to count the memory usage without using it for caches.
Implementing caching and memory counting at the same time make both harder than
implementing them one after another.
The main difficulty with counting memory usage is that some memory may be shared
using implicit sharing. We want to avoid double counting such memory. How
exactly shared memory is treated depends a bit on the use case, so no specific
assumptions are made about that in the API. The gathered memory usage is not
expected to be exact. It's expected to be a decent approximation. It's neither a
lower nor an upper bound unless specified by some specific type. Cache systems
generally build on top of heuristics to decide when to free what anyway.
There are two sides to this API:
1. Get the amount of memory used by one or more values. This side is used by
caching systems and/or systems that want to present the used memory to the
user.
2. Tell the caller how much memory is used. This side is used by all kinds of
types that can report their memory usage such as meshes.
```cpp
/* Get how much memory is used by two meshes together. */
MemoryCounter memory;
mesh_a->count_memory(memory);
mesh_b->count_memory(memory);
int64_t bytes_used = memory.counted_bytes();
/* Tell the caller how much memory is used. */
void Mesh::count_memory(blender::MemoryCounter &memory) const
{
memory.add_shared(this->runtime->face_offsets_sharing_info,
this->face_offsets().size_in_bytes());
/* Forward memory counting to lower level types. This should be fairly common. */
CustomData_count_memory(this->vert_data, this->verts_num, memory);
}
void CustomData_count_memory(const CustomData &data,
const int totelem,
blender::MemoryCounter &memory)
{
for (const CustomDataLayer &layer : Span{data.layers, data.totlayer}) {
memory.add_shared(layer.sharing_info, [&](blender::MemoryCounter &shared_memory) {
/* Not quite correct for all types, but this is only a rough approximation anyway. */
const int64_t elem_size = CustomData_get_elem_size(&layer);
shared_memory.add(totelem * elem_size);
});
}
}
```
Pull Request: https://projects.blender.org/blender/blender/pulls/126295
2024-08-15 10:54:21 +02:00
|
|
|
void VolumeComponent::count_memory(MemoryCounter &memory) const
|
|
|
|
|
{
|
|
|
|
|
if (volume_) {
|
|
|
|
|
BKE_volume_count_memory(*volume_, memory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:41:23 -05:00
|
|
|
/** \} */
|
2023-06-15 22:18:28 +02:00
|
|
|
|
|
|
|
|
} // namespace blender::bke
|