Commit Graph

27039 Commits

Author SHA1 Message Date
Bastien Montagne
dcadb3301e 'Fix' crash in append code in invalid data cases (linked data using local data).
Issue found here at the studio in some production files using dirty
hacks. Ideally we could fully forbid such assignement in BPY/RNA API
too, but there is no 'proper' way to achieve this without this dirty
hack currenlty.

So instead, check for this case and print a nice error about it for now.
2024-08-20 18:22:30 +02:00
Jacques Lucke
24f7b7a8fb Cleanup: Nodes: reduce coupling between socket shape and field state
Previously, the inferencing result was only stored in the socket shape.
However, that was conflicting with experiments where the socket shape and
the field state was not related.
2024-08-20 16:15:59 +02:00
Bastien Montagne
d6fa48c022 Fix #126507: GP conversion code was not updating link/append data.
Code executed in `BLO_read_do_version_after_setup` would not update in
any ways the info in link/append context data.

NOTE: this could potentially be extended to other code in this 'complex
do version' area. However, other versionning did not cause issues
apparently so far, so would rather until such changes are proven needed.
2024-08-20 12:51:47 +02:00
Bastien Montagne
2853a4c4db Refactor: Blendfile Link/Append: make callback of foreach item a function ref. 2024-08-20 12:51:47 +02:00
Bastien Montagne
382122f172 Cleanup: GPv3: Remove unused public GP object conversion function.
This was used during development for the conversion operator. now that
all data is systematically converted on load, it was not needed anymore
(and already unused).
2024-08-20 12:51:47 +02:00
Campbell Barton
908d004fcc Cleanup: spelling in comments 2024-08-20 14:25:52 +10:00
Sean Kim
c9a69f70d9 Refactor: Sculpt: Consolidate pbvh clearing actions
This commit introduces a number of changes to make PBVH and related
attribute lifecycle more predictable.

A common method, `BKE_sculptsession_free_pbvh` is introduced to manage
freeing related resources that are stored in `SculptSession`

Prior to this commit, the `active_vert_` attribute was only ever
changed when a raycast successfully hit the mesh. This commit changes
the behavior to not store a stale reference in the following cases:

* When dyntopo is enabled or disabled
* When a mesh with the subdivision modifier is subdivided further
* When the sculpt level of a subdiv modifier is changed
* When the subdivision modifier is removed from a mesh

Pull Request: https://projects.blender.org/blender/blender/pulls/126341
2024-08-19 22:15:19 +02:00
Jacques Lucke
625ba5dd82 Cleanup: remove unnecessary std::move 2024-08-19 21:56:34 +02:00
Jacques Lucke
354a097ce0 Volumes: improve file cache and unloading
This changes how the lazy-loading and unloading of volume grids works. With that
it should also fix #124164.

The cache is now moved to a deeper and more global level. This allows reloadable
volume grids to be unloaded automatically when a memory limit is reached. The
previous system for automatically unloading grids only worked in fairly specific
cases and also did not work all that well with caching (parts of) volume
sequences.

At its core, this patch adds a general cache system in `BLI_memory_cache.hh`. It
has a simple interface of the form `get(key, compute_if_not_cached_fn) ->
value`. To avoid growing the cache indefinitly, it uses the new
`BLI_memory_counter.hh` API to detect when the cache size limit is reached. In
this case it can automatically free some cached values. Currently, this uses an
LRU system, where the items that have not been used in a while are removed
first. Other heuristics can be implemented too, but especially for caches for
loading files from disk this works well already.

The new memory cache is internally used by `volume_grid_file_cache.cc` for
loading individual volume grids and their simplified variants. It could
potentially also be used to cache which grids are stored in a file.
Additionally, it can potentially also be used as caching layer in more places
like loading bakes or in import geometry nodes. It's not clear yet whether this
will need an extension to the API which currently is fairly minimal.

To allow different systems to use the same memory cache, it has to support
arbitrary identifiers for the cached data. Therefore, this patch also introduces
`GenericKey`, which is an abstract base class for any kind of key that is
comparable, hashable and copyable.

The implementation of the cache currently relies on a new `ConcurrentMap`
data-structure which is a thin wrapper around `tbb::concurrent_hash_map` with a
fallback implementation for when `tbb` is not available. This data structure
allows concurrent reads and writes to the cache. Note that adding data to the
cache is still serialized because of the memory counting.

The size of the cache depends on the `memory_cache_limit` property that's
already shown in the user preferences. While it has a generic name, it's
currently only used by the VSE which is currently using the `MEM_CacheLimiter`
API which has a similar purpose but seems to be less automatic, thread-safe and
also has no idea of implicit-sharing. It also seems to be designed in a way
where one is expected to create multiple "cache limiters" each of which has its
own limit. Longer term, we should probably strive towards unifying these
systems, which seems feasible but a bit out of scope right now. While it's not
ideal that these cache systems don't use a shared memory limit, it's essentially
what we already have for all cache systems in Blender, so it's nothing new.

Some tests for lazy-loading had to be removed because this behavior is more
implicit now and is not as easily observable from the outside.

Pull Request: https://projects.blender.org/blender/blender/pulls/126411
2024-08-19 20:39:32 +02:00
Iliya Katueshenock
1b67be14c6 Cleanup: BKE: Nodes: Functions renaming
Use snake style naming for all the kernel nodes functions.
Omit kernel prefix in the names since of the using namespace.
Use full forms of the terms
('iter' -> 'iterator', 'ntree' -> 'node_tree', 'rem' -> 'remove', ...).

Pull Request: https://projects.blender.org/blender/blender/pulls/126416
2024-08-19 20:27:37 +02:00
Philipp Oeser
0d4e2ea40d Fix #126434: Boid particle fight rule crash involving non-boid psys
For the boid Fight rule (and when determining friend/enemy health), we
need to check if other `ParticleTarget` psys is actually boids as well
(their health data is accessed -- which is only present on boid particle
systems), otherwise NULL data access crashes.

I know, EOL, but simple crash fixers should still get a chance to get in
I think.
Could also go into LTSes.

Pull Request: https://projects.blender.org/blender/blender/pulls/126494
2024-08-19 17:01:18 +02:00
Aras Pranckevicius
6d93bf6b44 IMB: Speedups, fixes and cleanups to various image scaling functions
API: merged IMB_scalefastImBuf, IMB_scaleImBuf, IMB_scaleImBuf_threaded
into one function IMB_scale with enum IMBScaleFilter {Nearest, Bilinear, Box}
and bool "threaded" param.

Performance:
- Box filtering (nee IMB_scaleImBuf) can be multi-threaded now.
- Nearest filtering (nee IMB_scalefastImBuf) can be multi-threaded now.
  Also fix performance regression on float images caused by fix in #126234
- Bilinear filtering (nee IMB_scaleImBuf_threaded) is several times faster now.

Correctness:
- Nearest and Box filtering: no longer loses half of edge pixels when scaling
  up.
- Box: fixed garbage results (and possible out of bounds reads) for non-4
  channel float images.
- Bilinear: no longer shifts image when scaling up.
- Bilinear: properly filters when scaling down by 2x2.

Test coverage:
- Add gtest coverage for various IMB_scale modes.
- Add a IMB_performance_test performance test, ran manually.

More details, images and performance numbers in PR.

Pull Request: https://projects.blender.org/blender/blender/pulls/126390
2024-08-19 16:50:05 +02:00
Jacques Lucke
20bc346cf0 Fix: remove unnecessary manual user count
`tree_sharing_info_` is automatically user-counted now.
2024-08-17 11:21:55 +02:00
Jacques Lucke
e0b5654655 Cleanup: improve check for whether object has geometry set instances
This speeds up the file from #126391 from 3.2fps to 3.3fps.
2024-08-17 00:58:37 +02:00
Jacques Lucke
f21a25489f Volumes: speedup extracting dense voxel values
Extract dense voxels if a very memory bandwidth limited task. I get a 10-20%
speedup be reducing the number of threads doing it at the same time.
2024-08-17 00:25:36 +02:00
Jacques Lucke
aa3549f590 Fix: correct place where simplify level is taken into account for volumes
Otherwise it did not take the current frame into account properly when
the volume is a sequence.
2024-08-16 23:40:25 +02:00
Harley Acheson
aa78d43a05 UI: Multicolor Autokeying Indicator
Show the autokeying indicator (record button) on the Timeline header in
red when enabled. With a white outline on dark theme, or with a black
outline on light theme. The red color is user changeable.

Pull Request: https://projects.blender.org/blender/blender/pulls/126344
2024-08-16 23:16:40 +02:00
Jacques Lucke
4493842fe7 Cleanup: use ImplicitSharingPtr for volume tree sharing info
This avoids manual user management.
2024-08-16 17:45:25 +02:00
Jacques Lucke
cebffdc060 Cleanup: simplify using ImplicitSharingPtr for raw ImplicitSharingInfo 2024-08-16 17:36:29 +02:00
Jacques Lucke
e40c3e5662 Geometry Nodes: add memory counting for bake state 2024-08-16 17:20:19 +02:00
Hans Goudey
4b71496f56 Fix #120145: mesh_new_from_object crash with evaluated object type change
When the preserve_all_data_layers argument was passed, the object
would be reevaluated, assuming the type of the original object data.
However, the evaluated object type can change compared to the
original, so to reevaluate the original mesh we need to check that
the original object was also a mesh object.

Pull Request: https://projects.blender.org/blender/blender/pulls/126407
2024-08-16 16:56:18 +02:00
Bastien Montagne
5d0894f06a Packedfile reading: report filepath of missing packed file.
Can be useful info for users to rebuild missing data.
2024-08-16 15:04:21 +02:00
Bastien Montagne
7597f494b9 Fix #126147: Crash on reading blendfile with missing packed data.
Over the year, changes in how image packedfiles were handled broke the
fallback case of missing packed data in a few places, this commit fixes
proper cleanup of invalid packed files in the Image's list of
packedfiles.

In addition, also do not create inplicit sharing info when the read data
is `nullptr`.
2024-08-16 15:04:21 +02:00
Sean Kim
a2d6808a7a Revert "Fix: Sculpt: Correct asserts in previous commit"
This reverts commit 1602f82f28.

Original commit was correct, the affected functions are working on the
evaluated object, so we should assert that it is *not* the original
object.

Pull Request: https://projects.blender.org/blender/blender/pulls/126388
2024-08-16 06:50:16 +02:00
Campbell Barton
d71d325692 Cleanup: spelling in comments, strings 2024-08-16 09:33:20 +10:00
Hans Goudey
1602f82f28 Fix: Sculpt: Correct asserts in previous commit 2024-08-15 16:02:13 -04:00
Hans Goudey
f04fd1fad7 Refactor: Sculpt: Require depsgraph to access mesh positions
Part of #118145.
Similar to cfe69e32bc.
The dependency graph is required to access the evaluated object.
2024-08-15 15:43:17 -04:00
Hans Goudey
220cf67172 Cleanup: Improve object argument name
These functions expect an evaluated object which wasn't clear.
2024-08-15 13:09:28 -04:00
Hans Goudey
cfe69e32bc Refactor: Sculpt: Require object for position access
Part of #118145.
In preparation for removing the duplicated position arrays from the
pbvh::Tree structure, change the API functions that access the arrays
to require object arguments. This will allow retrieving data from the
original mesh or the evaluated deform mesh as necessary.

The pbvh code isn't really the right place for this, but neither is the
sculpt or paint code in my opinion, since this concept is not specific
to sculpting or painting. For now keep the API in the same place. It
probably makes sense to move a bunch of these functions at some point.

Pull Request: https://projects.blender.org/blender/blender/pulls/126370
2024-08-15 17:07:36 +02:00
Campbell Barton
b5e0b59736 Cleanup: remove space around identifiers in C-style comments 2024-08-15 20:46:00 +10:00
Jacques Lucke
a8667aa03f 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
Hans Goudey
6ca352a7b6 Refactor: Sculpt: Remove mesh pointer from BVH tree, pass to drawing code
Part of #118145.
Similar in concept to recent commits removing the usage of
this mesh pointer in favor of fetching the data as necessary.
Also see recent discussion in a recent fix for this area:
https://projects.blender.org/blender/blender/pulls/122850.

And also note the comment for `Tree::mesh_` was incorrect.
The mesh was the original mesh, not the evaluated mesh.
2024-08-15 00:24:12 -04:00
Hans Goudey
ae8ecd6e4e Cleanup: Remove unused variables 2024-08-15 00:00:44 -04:00
Hans Goudey
0c0a9d7714 Cleanup: Remove unused variables 2024-08-14 23:58:07 -04:00
Hans Goudey
9a776830a6 Refactor: Sculpt: Reduce usage of BVH tree positions array
In preparation for removing the positions array from the BVH tree
and just retrieving the evaluated or original positions as necessary.
2024-08-14 23:27:20 -04:00
Hans Goudey
bd19212fb8 Refactor: Sculpt: Avoid BVH tree geometry pointers in normals update
Part of #118145.
There is some complexity in this area because the normals need to be
updated on the original geometry only when there is no deformation
or multires modifier. The simplest way to encapsulate that usage of
the original geometry for now was adding a separate function that
contains the lookup with a comment justifying it.
2024-08-14 23:19:39 -04:00
Hans Goudey
561d035da4 Cleanup: Follow class layout style guide for SculptSession 2024-08-14 23:19:39 -04:00
Hans Goudey
6703927a05 Refactor: Sculpt: Retrieve positions instead of using BVH tree reference
Part of #118145.
2024-08-14 21:10:04 -04:00
Hans Goudey
70a24258c6 Sculpt: Remove unused positions array copy with some deform modifiers
Last usage was removed in 0250596f5b.
2024-08-14 14:10:32 -04:00
Hans Goudey
658b32093a Refactor: Sculpt: Replace sculpt attribute system for dynamic topology data
Part of #118145.
Use the BMesh functions instead of the sculpt attribute API which we
intend to remove.
2024-08-14 10:25:52 -04:00
Hans Goudey
e612260f5c Refactor: Sculpt: Replace sculpt attribute system for persistent base data
Part of #118145.
Use the mesh attribute API instead of the sculpt attribute API which we
intend to remove.
2024-08-14 10:13:37 -04:00
Aras Pranckevicius
df00c30fcd Fix #126201: Video artifacts when rendering into ffmpeg with PNG codec
Started happening with 422dd9404f that introduced multi-threaded
conversions of src->dst (usually RGBA->YUV) format before encoding
the frame with ffmpeg. But the issue itself is not related to
multi-threading, but rather with the fact that AVFrame objects
started to be backed by an AVBuffer object (as that is needed for
threaded swscale to work).

Turns out, if a frame is backed by AVBuffer object, said buffer
might become "non writable" because it got shared (non-1 refcount).
And that happens with some ffmpeg video codecs, particularly PNG one.

Make sure to make the AVFrame objects writable inside
generate_video_frame. This follows official ffmpeg example
(doc/examples/encode_video.c) that explains why that is needed:

"the codec may have kept a reference to the frame in its internal
structures, that makes the frame unwritable. av_frame_make_writable()
checks that and allocates a new buffer for the frame only if necessary"

Pull Request: https://projects.blender.org/blender/blender/pulls/126317
2024-08-14 16:07:11 +02:00
Hans Goudey
2af62ba0f6 Refactor: Sculpt: Reduce usage of BVH tree geometry back pointers
Similar to 9eb7e8bfd1
Pass the object instead of retrieving geometry data from the BVH tree.
2024-08-13 19:01:15 -04:00
Hans Goudey
1661f0728f Cleanup: Remove usage of BKE_pbvh_get_mesh
Part of #118145.
This acts as a back pointer, allowing code that looks like it just deals with
the sculpt BVH tree to do basically anything.
2024-08-13 19:01:15 -04:00
Jacques Lucke
4f491aca80 Fix #126286: crash when opening blend file with point cache 2024-08-13 19:53:46 +02:00
Jacques Lucke
e1422530d6 Fix: crash when attempting to unload volume tree twice 2024-08-13 17:47:52 +02:00
Hans Goudey
9eb7e8bfd1 Refactor: Sculpt: Reduce reliance on BVH tree geometry back pointers
Retrieve the relevant data directly from the original object's mesh
(or the evaluated SubdivCCG in that case) rather than using the
BVH tree's geometry points which we'd like to remove.

Pull Request: https://projects.blender.org/blender/blender/pulls/126284
2024-08-13 17:38:09 +02:00
Christoph Lendenfeld
9741750497 Refactor: move BKE_action_get_item_transform_flags out of the kernel
No functional changes expected.

The function `BKE_action_get_item_transform_flags` was only ever used in a single place.
As such it could be a static function within the given file.
This reduction in scope will help with future refactors.

This is in service of fixing #126125 since the function in its current form
does not support layered actions.

Pull Request: https://projects.blender.org/blender/blender/pulls/126287
2024-08-13 17:04:01 +02:00
Anthony Roberts
9df75725be Switch to using blender::BitVector in tile changesets
This avoids an issue in clang-cl on Windows ARM64 where an invalid pointer would be (re)used.

See #124182 for more details, but basically without this, when the vector of tiles inside changeset is resized, a reference to an invalid pointer (ie, the location in the old vector) is kept somewhere, which makes the test fail.

Pull Request: https://projects.blender.org/blender/blender/pulls/126083
2024-08-13 16:52:52 +02:00
Jacques Lucke
05925b404d Fix #125419: crash when loading baked data with empty instances
The problem was the automatic instance deduplication. There were two
instance references before baking, both of which were referenced by instances.
When loading the bake, they were deduplicated, so there was only one geometry,
but the instances still referenced two.

The fix is to not do deduplication when loading instances from a bake.
2024-08-13 14:10:25 +02:00