Commit Graph

123 Commits

Author SHA1 Message Date
Brecht Van Lommel
c7a33a62a2 Cleanup: Directly include DNA_userdef_types.h and BLI_listbase.h
Instead of relying on them being included indirectly.

Pull Request: https://projects.blender.org/blender/blender/pulls/134406
2025-02-12 23:01:08 +01:00
Bastien Montagne
87a4c0d3a8 Refactor: Make Library.runtime an allocated pointer.
Move `Library.runtime` to be a pointer, move the related
`LibraryRuntime` struct to `BKE_library.hh`. Similar to e.g.
Mesh.runtime, that pointer is expected to always be valid, and is
allocated at readtime or when creating a new Library ID.

Related smaller changes:
* Write code now uses standard ID writing codepath for Library IDs too.
  * Runtime pointer is reset to nullptr before writing.
* Looking up a library by its absolute path is now handled through a
  dedicated utils, `search_filepath_abs`, instead of using
  `BLI_findstring`.

Pull Request: https://projects.blender.org/blender/blender/pulls/134188
2025-02-07 17:47:16 +01:00
Brecht Van Lommel
a7e0a9a467 Cleanup: Various clang-tidy warnings in blenloader
Pull Request: https://projects.blender.org/blender/blender/pulls/133734
2025-01-31 17:03:17 +01:00
Jacques Lucke
d7cb705707 BLO: generate human readable .blend file for debugging purposes
This adds a debugging utility for developers that makes it easier to what's
stored in a .blend file. Some possible use cases:
* Figure out if some specific data has been written to the .blend file.
* Check the order of data in the .blend file.
* Work towards having less runtime dependent changes in .blend files (it's
  easier to diff the textual output).

It is **not** a goal to provide a general human and machine readable form of
.blend files (like xml/json files). That would be way more involved.

The way to use this is to set `GENERATE_DEBUG_BLEND_FILE` to `1` at the top of
`writefile.cc`. Then, whenever Blender saves a .blend file, it will generate a
`.debug.txt` file next to it.

There is already `blend2json.py` which serves a similar purpose but is a
separate program that can be executed on the .blend file afterwards. With the
`--full-data` flag it outputs comparable data, but the output is a bit more
verbose and it needs an extra step that can be avoided by generating the
`.debug.txt` file immediately. For certain use cases,
`GENERATE_DEBUG_BLEND_FILE` can be more convenient. It's also much simpler to
add log additional data in that file that is produced during the blend-write
process.

Pull Request: https://projects.blender.org/blender/blender/pulls/133063
2025-01-27 18:24:29 +01:00
Hans Goudey
949b9638f5 BLO: Add read/write utility functions for int16 arrays
Pull Request: https://projects.blender.org/blender/blender/pulls/132306
2024-12-24 17:05:26 +01:00
Bastien Montagne
4be060eee6 Writefile: clear runtime data before writing ID.
Avoids some binary noise in saved files, and will some day allow us to use runtime data on read without having to explicitly clean it up (unless we refactor this into an allocated pointer instead!).

Pull Request: https://projects.blender.org/blender/blender/pulls/132190
2024-12-23 16:18:22 +01:00
Jacques Lucke
0e28920bd8 BLI: change default hash-table clear behavior
Previously, calling `clear()` on `Map`, `Set` or `VectorSet` would remove all
elements but did not free the already allocated capacity. This is fine in most
cases, but has very bad and non-obvious worst-case behavior as can be seen in
#131793. The issue is that having a huge hash table with only very few elements
is inefficient when having to iterate over it (e.g. when clearing).

There used to be a `clear_and_shrink()` method to avoid this worst-case
behavior. However, it's not obvious that this should be used to improve
performance.

This patch changes the behavior of `clear` to what `clear_and_shrink` did before
to avoid accidentally running in worst-case behavior. The old behavior is still
available with the name `clear_and_keep_capacity`. This is more efficient if
it's known that the hash-table is filled with approximately the same number of
elements or more again.

The main annoying aspect from an API perspective is that for `Vector`, the
default behavior of `clear` is and should stay to not free the memory. `Vector`
does not have the same worst-case behavior when there is a lot of unused
capacity (besides taking up memory), because the extra memory is never looked
at. `std::vector::clear` also does not free the memory, so that's the expected
behavior. While this patch introduces an inconsistency between `Vector` and
`Map/Set/VectorSet` with regards to freeing memory, it makes them more
consistent in that `clear` is the better default when reusing the data-structure
repeatedly.

I went over existing uses of `clear` to see if any of them should be changed to
`clear_and_keep_capacity`. None of them seemed to really benefit from that or
showed that it was impossible to get into the worst-case scenario. Therefore,
this patch slightly changes the behavior of these calls (only performance wise,
semantics are exactly the same).

Pull Request: https://projects.blender.org/blender/blender/pulls/131852
2024-12-17 13:35:07 +01:00
Sybren A. Stüvel
efd2e762f7 Refactor: Core, give the IDWALK_CB_… enum an explicit name
Give the `IDWALK_CB_…` enum an explicit name:
`LibraryForeachIDCallbackFlag`. This way the flags are type-safe, and
it's known where values come from. This is much preferred (at least by
me) to just having `int flags`.

Uses of `0` have been replaced with `IDWALK_CB_NOP` as that has the same
value and is of the right type.

One invalid use of `IDWALK_NOP` was detected by this change, and is
replaced by `IDWALK_CB_NOP`.

This change might be incomplete; I gave the enum a name, fixed the
compiler errors, and then also updated assignments like `int cb_flag =
cb_data->cb_flag`. I might have missed some assignments to `int` though.

No functional changes.

Pull Request: https://projects.blender.org/blender/blender/pulls/131865
2024-12-13 17:32:24 +01:00
Bastien Montagne
f2949d387c Fix #131793: Blendfile saving in 4.3 can be a lot slower than in 4.2.
Regression from 536fb53dc6. Turns out `Set::clear()` can be orders
of magnitude slower than `Set::clear_and_shrink()` in some
worst-case scenarii.
2024-12-13 12:25:31 +01:00
Sybren A. Stüvel
7d5143e94c Revert "Refactor: Core, give the IDWALK_… enums an explicit name"
This reverts commit 3ef748789d. It was
landed too hastily, my apologies.

Pull Request: https://projects.blender.org/blender/blender/pulls/131813
2024-12-12 20:03:27 +01:00
Sybren A. Stüvel
3ef748789d Refactor: Core, give the IDWALK_… enums an explicit name
Two commits that basically do the same thing for two `enum`s: give
them a name.

- the `IDWALK_…` enum → `LibraryForeachIDFlag`.
- the `IDWALK_CB_…` enum → `LibraryForeachIDCallbackFlag`.

This way the flags are type-safe, and it's known where values come
from. This is much preferred (at least by me) to just having `int
flags`.

Uses of `0` have been replaced with `IDWALK_NOP` and `IDWALK_CB_NOP`,
as those have the same value and are of the right type.

One invalid use of `IDWALK_NOP` was detected by this change, and is
replaced by `IDWALK_CB_NOP`. And another one in the opposite
direction.

This change might be incomplete; I gave the enum a name, fixed the
compiler errors, and then also updated assignments like `int cb_flag =
cb_data->cb_flag`. I might have missed some assignments to `int`
though.

No functional changes.

----------

I intend to land this PR as its two separate commits. I just put them in the same PR so the buildbot can handle them in one go, and we don't have a stack of highly relatled PRs.

In the future this could also apply to the `IDWALK_RET_…` enum. This one I left out, though, because a proper cleanup there would also have to include their ambiguity on whether they are bitflags (like the enums in this PR) or not. Their values and the code in `BKE_lib_query_foreachid_process()` implies they are bitflags, but in practice they are never or'ed together and just used as discrete values.

Pull Request: https://projects.blender.org/blender/blender/pulls/131803
2024-12-12 17:28:28 +01:00
Jacques Lucke
585cd07899 Refactor: Blenloader: simplify writing libraries
This improves the `write_libraries` function in a couple of ways:
* No need to split the `bmain` which I find somewhat hard to reason about. I
  think it's good if `bmain` is as read-only as possible in this write-context.
  Instead a more explicit C++ data structure (`MultiValueMap<Library *, ID *>`)
  is used. I think this was the only place in write-code that used
  `blo_split_main`.
* Deduplication of the check that determines  whether a placeholder should be
  written for a specific ID.
* General cleanup to avoid unnecessarily deep nesting when iterating over IDs.

No functional changes are expected.

Pull Request: https://projects.blender.org/blender/blender/pulls/131385
2024-12-10 17:43:09 +01:00
Jacques Lucke
fba10a8212 Cleanup: Blenloader: extract function to write blend file header
This simplifies the code a bit.

Pull Request: https://projects.blender.org/blender/blender/pulls/131379
2024-12-04 18:53:09 +01:00
Jacques Lucke
0a1526eeff Blenloader: extract functions to gather and write local IDs
Splitting the write-loop into stages makes it easier to understand, debug and time.

All the complex filtering in `gather_local_ids_to_write` is the same as before.

With this and some previous refactors, it's also much easier to have a clean `write_id`
function that should also work for embedded IDs later on.

Pull Request: https://projects.blender.org/blender/blender/pulls/130983
2024-12-03 18:34:04 +01:00
Jacques Lucke
1d39bb4c80 Core: remove leftover IDOverrideLibrary
This is a leftover from an earlier version of overrides, but is effectively dead
code currently. It was used when there were proportional diffing capabilities.
However, Bastien mentioned that this would be implemented differently nowadays
anyway if it becomes necessary again.

This simplifies the write ID loop quite significantly.

Pull Request: https://projects.blender.org/blender/blender/pulls/130928
2024-11-26 16:09:59 +01:00
Jacques Lucke
3c4e46f067 Refactor: Blenloader: simplify API for BLO_Write_IDBuffer
This replaces the existing C API of `BLO_Write_IDBuffer` with a C++ API. This
helps because:
* No need for explicit freeing.
* Can use more generic `DynamicStackBuffer` utility instead of having a separate
  implementation of that.

Additionally, the API is changed so that a new `BLO_Write_IDBuffer` is created
for each `ID` instead of reusing the same for multiple IDs. This simplifies the
code quite a bit and allows for better use of the RAII pattern.

I expect the performance to be the same as before. In theory, there could be a
small speedup, because the `BLO_Write_IDBuffer` is not allocated separately
anymore, but that should be negligible.

No functional changes are expected.

Pull Request: https://projects.blender.org/blender/blender/pulls/130452
2024-11-25 18:07:16 +01:00
Jacques Lucke
12c17e9579 Blenloader: use int64_t in API more consistently
This replaces various uses of `int` and `uint` with `int64_t` in the blenloader API.
This will be necessary for #129309.

Pull Request: https://projects.blender.org/blender/blender/pulls/130591
2024-11-22 12:35:27 +01:00
Jacques Lucke
251731accc Cleanup: Blenloader: simplify writing BHead
This adds a `write_bhead` utility function and also reduces the scope of
some `BHead` variables. The separate `write_bhead` function was quite
useful for #129751 and will likely be useful in a potential separate
implementation too.

Pull Request: https://projects.blender.org/blender/blender/pulls/130457
2024-11-20 10:39:58 +01:00
Jacques Lucke
9f0d20c056 Cleanup: Blenloader: add missing const
This adds `const` in a few obvious places where it should be used.

Pull Request: https://projects.blender.org/blender/blender/pulls/130461
2024-11-18 17:55:24 +01:00
Jacques Lucke
9d87291c40 Fix #129691: heap buffer overflow when writing unaligned data to .blend file
`writedata` used to align the written buffer size to a multiple of 4. This
causes multiple issues:
* Writes uninitialized data.
* Crash with ASAN due to a heap buffer overflow if the buffer is not any longer
  than what is passed in.
* Modifies the length of the buffer which can't be undone when reading the
  buffer again.

I don't know of any reason for this alignment here. I'd think that it doesn't
matter when writing to a file. If it would matter, then we should probably align
to at least 8 nowadays because that's the alignment of pointers. The original
reason for this alignment seems to be lost to history. It was already part of
the initial commit.

Pull Request: https://projects.blender.org/blender/blender/pulls/129821
2024-11-05 15:51:37 +01:00
Jacques Lucke
4c2718e318 Fix #126821: compress saving option gets removed when recovering from autosave
Before efb511a76d this was not necessary, because the G_FILE_COMPRESS option
was not disabled when writing memfile undo steps. However, compression is
generally disabled when writing quit.blend or autosave files.
2024-09-11 15:11:42 +02:00
Campbell Barton
40f96afa61 Cleanup: various non-functional changes
- Use const arguments.
- Remove redundant cast.
- Use ELEM macro.
- Use boolean & nullptr literals.
2024-08-26 11:50:12 +10:00
Bastien Montagne
9106383a52 Fix #99875: Reserve 0 SDNA struct index for raw data.
This commit essentially adds a fake SDNA struct info at struct index
`0`, ensuring that no actual SDNA struct will use that identifier from
now on.

Pull Request: https://projects.blender.org/blender/blender/pulls/125730
2024-08-08 15:56:33 +02:00
Jacques Lucke
d74d8ceb23 Cleanup: move BKE_packedFile.h to C++
Part of #103343.
2024-08-08 15:13:20 +02:00
Jacques Lucke
5861b078f7 Core: rename ID.flag and ID.tag values
Previously, values for `ID.flag` and `ID.tag` used the prefixes `LIB_` and
`LIB_TAG` respectively. This was somewhat confusing because it's not really
related to libraries in general. This patch changes the prefix to `ID_FLAG_` and
`ID_TAG_`. This makes it more obvious what they correspond to, simplifying code.

Pull Request: https://projects.blender.org/blender/blender/pulls/125811
2024-08-07 12:12:17 +02:00
Bastien Montagne
2372bf1377 Cleanup: DNA code: sanitize naming.
Previous namings in makesdna code was very confusing and inconsistent.

This commit unifies names accross the codebase as such:
- `struct` for the structs definitions data.
- `type` for the types definitions data.
- `member` for the struct members definitions data.

Structs and types definitions are not in synced for two reasons:
- types contains also definitions for basic types (int, float, etc.).
- types can be discovered before their struct definition (as members of
  other structs).

This commit also groups all members of `SDNA` struct more logically (all
'structs' ones together, then all 'types' ones, then all 'struct
members' ones).

This commit should have no behavioral change at all.

Pull Request: https://projects.blender.org/blender/blender/pulls/125605
2024-07-31 16:06:43 +02:00
Campbell Barton
99af19932e Cleanup: spelling in comments 2024-07-30 12:38:16 +10:00
Bastien Montagne
11bdd3da5a Blendfile: Do not write 'DrawData'.
This list is already cleared on read, better to also write nullptr for
it then (reduces the risks of fake-changes detection in undo code
mainly).
2024-07-29 21:26:43 +02:00
Bastien Montagne
97982360de Refactor: DNA: Add util to access size of a truct.
Avoids reafile code having to access 'internals' specifics of DNA data.
2024-07-29 17:31:42 +02:00
Jacques Lucke
26b7ae888b Fix: deduplicate shared data when writing to file
If a data-block referenced the same shared data more than once, it used to be
written multiple times. This worked out so far, because the written data is
always the same. So when reading the file, it doesn't matter which written
buffer Blender decides to read on. However, this makes the .blend file look
corrupt and leads to memory leaks on load (#125001).

Pull Request: https://projects.blender.org/blender/blender/pulls/125489
2024-07-27 23:07:21 +02:00
Bastien Montagne
cd07327584 BLO write: Avoid double lookup in already added blocks addresses. 2024-07-26 15:29:00 +02:00
Bastien Montagne
536fb53dc6 Core: Blendfile Write: Check against multiple writing at same address.
Blendfile uses the 'old (memory) address' of its data as 'uid' in the
blendfile. There should only be one block written for a given address
and a same ID (each ID define its own 'virtual address space').

This commit checks that this condition is met at wrtite time (except for
undo steps, for performances reasons).

Tooling part of the investigations on #125001.
2024-07-26 12:40:25 +02:00
Bastien Montagne
6cf4294c68 Comment about known issue with some old blenfiles triggering a write assert.
File from #124777 (from 2.79.1) cause a temp bScreen to not be properly
tagged as such, which prevents its deletion when related window is
closed, and leaves a zero-user bScreen in Main.

While this should not happen, this does not seem trivial to track down,
and not an important enough issue to spend more time on it.

Also reported as #124857.
2024-07-17 12:57:43 +02:00
Julian Eisel
c5180e0988 Assets: Special .asset.blend files, for storing externally editable assets
Introduces a new kind of blend file that store assets that can savely be edited
from the Blender UI, without having to open the blend-file storing the asset
itself (asset "pushing" workflow).

Only brush assets will use this for now.

Technical Documentation:
https://developer.blender.org/docs/features/asset_system/asset_editing/

User Documentation:
https://docs.blender.org/manual/en/latest/files/asset_libraries/introduction.html#asset-system-files-asset-blend-extention

The API to manage assets by generating these files is added in the following
commit.

Main authors: Bastien Montagne, Brecht Van Lommel, Julian Eisel

Pull Request for the latest design iteration:
https://projects.blender.org/blender/blender/pulls/124246

Part of the brush assets project, see:
- https://projects.blender.org/blender/blender/issues/116337
- https://projects.blender.org/blender/blender/pulls/106303
2024-07-08 17:50:24 +02:00
Campbell Barton
bdf06e6d82 Cleanup: spelling in comments 2024-07-06 14:21:24 +10:00
Bastien Montagne
570eee1717 Debug: Add new userpref option to enforce recompute of ID usercount on filesave.
This is a workaround to allow user to keep working without loss of data
when an issue like #124049 happens.

This commit also expose again the `use_all_linked_data_direct` debug
option, no idea why that one was removed.
2024-07-02 16:46:19 +02:00
Bastien Montagne
b32d026441 Fix (studio-reported) unlinkable IDs made directly linked during blendfile write.
Regression from 435b6743fd, no usage of unlinkable ID (mainly
shapekeys...) should make them directly linked.

Note that this had no serious consequences, it was mainly printing annoying
error messages in release builds, and asserting in debug ones.
2024-05-29 17:38:08 +02:00
Campbell Barton
c110d26c61 Cleanup: use const pointers where possible 2024-05-28 13:47:43 +10:00
Bastien Montagne
435b6743fd Fix #121733: Linked masks/moviclips/images used by editors get lost on file reload.
This was a consequence of the work done in #106321, where this specific
'active in UI' case was not identified and properly handled.

Now, consider most ID usages from UI (editors) as 'weak links', i.e.
keep a reference to these IDs even if they are only indirectly used.

Note that missing weak links will not create placeholders if the source
data is not found in the library anymore on load. they are just silently
dropped.

Pull Request: https://projects.blender.org/blender/blender/pulls/122207
2024-05-27 13:35:53 +02:00
Bastien Montagne
6426de4489 Core: IDManagement: Refactor how 'never unused' IDs are defined.
A few ID types are considered as 'never unused' in Blender (UI related
ones, the Libraries and the Scenes). Local IDs of this type are always
considered as used, even if no other ID links to them.

This was previously fairly weekly defined and implemented (mainly in the
writefile code and the 'tag unused' libquery functions).

This commit formalize this characteristic of ID types by adding a new
`IDTYPE_FLAGS_NEVER_UNUSED` flag, and using this in the few places in
the code that handle unused IDs.
2024-05-24 17:43:22 +02:00
Campbell Barton
08df46d7cd Extensions: support token access for remote repositories
This introduce a new "secret" per-repository property of type password
as described by #121856.

A token or secret may be used by some non blender.org repositories.

This only shows for remote repositories and is shown in the
"Add Remote Repository" popup.

This commit doesn't implement sending to token to the server which will
be implemented separately.

Ref !121886

Co-authored-by: Dalai Felinto <dalai@blender.org>
2024-05-22 20:40:28 +10:00
Julian Eisel
a6ebfb05ad UI: Option to store enabled asset shelf catalogs in preferences
Adds a new asset shelf option (`STORE_ENABLED_CATALOGS_IN_PREFERENCES`
option in RNA) to use the Preferences for storing the enabled catalogs.
This way asset shelf types can decide if for their use-case, they want
to synchronize the enabled catalogs over Blender sessions and files, or
keep the stored locally in the file.

This is important because for example on one hand, it would be annoying
if for brush assets you'd have to enable the visible catalog tabs for
every 3D View and every file, while on the other hand you need that
level of control for the pose library where the catalogs the rigger/
animator cares about varies from project to project, character to
character and shot to shot.

Conceptually this also makes some sense: The new brush assets workflow
synchronizes brush assets and their catalogs across Blender sessions
and files, basically making them globally accessible independent of
the current file/project, so treating the enabled catalogs the same
is consistent.

Previously reviewed in #120264

Pull Request: https://projects.blender.org/blender/blender/pulls/121363
2024-05-03 10:20:01 -04:00
Brecht Van Lommel
15b9ae5436 Refactor: Use typed functions for blend file data reading
This makes the read and write API functions match more closely, and adds
asserts to check that the data size is as expected.

There are still a few places remaining that use BLO_read_data_address
and similar generic functions, these should eventually be replaced as well.

Pull Request: https://projects.blender.org/blender/blender/pulls/120994
2024-04-24 17:01:22 +02:00
Campbell Barton
fd589fdca4 Cleanup: various non functional C++ changes 2024-04-20 13:46:14 +10:00
Hans Goudey
c91449836a Cleanup: Move BKE_workspace.h to C++ 2024-04-12 17:03:18 -04:00
Jacques Lucke
936fb7488e Cleanup: move runtime data to Library.runtime
Having a better separation between runtime and non-runtime data makes
it easier to reason about the code.

Pull Request: https://projects.blender.org/blender/blender/pulls/120271
2024-04-08 13:08:36 +02:00
Hans Goudey
efee753e8f Cleanup: Move BKE_idprop.h to C++ 2024-03-26 13:07:04 -04:00
Jacques Lucke
0e8e219d71 Undo: support implicit-sharing in memfile undo step
This adds implicit sharing support for the `MemFile` undo-step. This decreases memory
usage and increases performance.

Implicit sharing allows the undo system to take (shared) ownership of some data.
Previously, the data would always be serialized and compared to the previous undo-step.
So this turns an O(n) operation into O(1) (in terms of memory usage and time).

Read/write code that wants to make use of this has to use the new `BLO_read_shared`
and `BLO_write_shared` functions respectively. Those either make use of implicit-sharing
internally or do the "full" read/write based on a passed-in function. It seems possible to
use the same API in the future to store shared data to .blend files.

Improvements:
* Much faster undo step creation in many cases by avoiding the majority data copies
  and equality checks. This fixes #98574. I found undo step creation and undo step
  decoding to be 2-5 times faster in some demo files from the blender website and in
  some production files from the Heist project.
* Reduced memory usage when there is large data in `bmain`. For example, when
  loading the same highly subdivided mesh that I used in #106228 the memory usage
  is 1.03 GB now (compared to 1.62 GB in `main` currently). The main remaining copy
  of the data now is done by rendering code.
* Some significant performance improvements were also measured for the new grease
  pencil type (#105540).

There is one main downside of using implicit-sharing as implemented here: `MemFile`
undo steps can't be written as .blend files anymore. This has a few consequences:
* Auto-save becomes slower (up to 3x), because it can't just save the previous undo step
  anymore and does a normal save instead. This has been discussed in more detail here:
  https://devtalk.blender.org/t/remove-support-for-saving-memfile-undo-steps-as-blend-files-proposal/33544
  It would be nice to work towards asynchronous auto-save to alleviate this problem.
  Some previous work has been done to reduce the impact of this change in 41b10424c7
  and f0f304e240. This has been committed separately in efb511a76d.
* Writing `quit.blend` has to do a normal file save now. So it's a bit slower too, but it's
  less of a problem in practice.
* The `USE_WRITE_CRASH_BLEND` functionality does not work anymore. It doesn't seem
  to be used by anyone (removed in e90f5d03c4)

There are also benefits to not writing `MemFile` from undo steps to disk. It allows us to
more safely do undo-specific optimizations without risking corrupted .blend files. This
is especially useful when we want to preserve forward compatibility in some cases.
This requires converting data before writing the .blend files, but this conversion is not
necessary for undo steps. Trying to implement this kind of optimization in the past has
often lead to bugs (e.g. 43b37fbc93).

Another new problem is that it is harder to know the size of each undo step. Currently, a
heuristic is used to approximate the memory usage, but better solutions could be found
if necessary.

Pull Request: https://projects.blender.org/blender/blender/pulls/106903
2024-02-29 17:14:58 +01:00
Bastien Montagne
29fe777445 Cleanup: Make BKE_report.h a full Cpp header. 2024-02-10 18:34:29 +01:00
Bastien Montagne
54618dbae3 Cleanup: Make BKE_global.h a Cpp header. 2024-02-10 18:25:14 +01:00