Commit Graph

6535 Commits

Author SHA1 Message Date
Bastien Montagne
050d48edfc BLI_fileops: Harmonize 'rename' behaviors accross platforms.
This commit aim at making the behaviors of `BLI_rename` and
`BLI_rename_overwrite` more consistent and coherent across all
supported platforms.

* `BLI_rename` now only succeeds in case the target `to` path does not
  exists (similar to Windows `rename` behavior).
* `BLI_rename_overwrite` allows to replace an existing target `to` file
  or (empty) directory (similar to Unix `rename` behavior).

NOTE: In case the target is open by some process on the system, trying
to overwrite it will still fail on Windows, while it should succeed on
Unix-like systems.

The main change for Windows is the usage of `MoveFileExW`
instead of `_wrename`, which allows for 'native support' of file
overwrite (using the `MOVEFILE_REPLACE_EXISTING` flag). Directories
still need to be explicitly removed though.

The main change for *nix systems is the use of `renamex_np` (OSX) or
`renameat2` (most Linux systems) to allow forbidding renaming to an
already existing target in an 'atomic' way.

NOTE: While this commit aims at avoiding the TOC/TOU problem as
much as possible by using available system's primitives for most
common cases, there are some situations where race conditions
(filesystem changes between checks on FS state, and actual rename
operation) remain possible.

Pull Request: https://projects.blender.org/blender/blender/pulls/115096
2023-11-30 22:35:00 +01:00
Hans Goudey
7eb8c4cdda Cleanup: use const variables and arguments 2023-11-30 14:14:02 -05:00
Campbell Barton
15db0cb446 Cleanup: use style for doxygen comment blocks 2023-11-30 14:15:11 +11:00
Campbell Barton
3b5031f1cb Cleanup: use 'r_' prefix for output arguments, order last
Also clarify some naming.
2023-11-30 10:42:18 +11:00
Campbell Barton
a385a98e8e Cleanup: spelling in comments 2023-11-29 15:56:17 +11:00
Campbell Barton
44d6995b4c Cleanup: make hash sizes static, remove references to smallhash
Remove BLI_ghash_hash_sizes declaration it's only user (smallhash)
has been removed.
2023-11-29 15:13:05 +11:00
Hans Goudey
991486c37f Cleanup: Remove/replace SmallHash data structure
Use blender::Set which is similar but offsers better type safety
and likely better performance as well. The only remaining user
was the mesh edit mode knife tool, and replacing that usage
with `Set` and `Map` was straightforward.
2023-11-28 14:03:53 -05:00
Hans Goudey
7020a4ff1a Cleanup: Remove unused voxel sampling functions
Last used in Blender internal renderer.
2023-11-28 13:30:18 -05:00
Hans Goudey
573b4728cb Cleanup: Remove unused BLI voronoi code
Unused after 75c947a467
2023-11-28 13:24:45 -05:00
Hans Goudey
21a2390f4c BLI: Add utility to reverse index mask
This utility was already duplicated in two places and planned to be used
more. While we should usually avoid creating arrays the size of the
indexed array (rather than the size of the mask), sometimes it does seem
to be the best option, and we're helped by the fact that most memory
stays unintialized for a small mask (allocating but not writing to memory
pages at all generally isn't too expensive).

Pull Request: https://projects.blender.org/blender/blender/pulls/115491
2023-11-28 17:32:17 +01:00
Campbell Barton
41739cd3fd PyAPI: remove use of stub script to execute files on WIN32
Workaround potential C-API `FILE` incompatibility by reading the
file data into memory, compiling & running it - matching existing logic
for text buffers text buffers. This replaces the in-lined stub-script
that re-opened the file from Python.

While the down-side of the stub-script was minor, it required some
non-obvious logic and had the disadvantage of requiring 2x scripts to
execute whenever a file was executed on WIN32.

Expose BLI_file_read_data_as_mem_from_handle as a public function
since it's useful to be able to read an existing FILE into memory.
2023-11-28 17:50:28 +11:00
Wannes Malfait
b162281caf Mesh: add index-independent test for mesh equality
This adds a new function, `compare_meshes`,
as a replacement for `BKE_mesh_cmp`.

The main benefits of the new version are the following:
- The code is written in c++, and makes use of the new attributes API.
- It adds an additional check, to see if the meshes only differ by
  their indices. This is useful to verify correctness of new algorithmic
  changes in mesh code, which might produce mesh elements in a different
  order than the original algorithm. The tests will still fail, but the
  error will show that the indices changed.

Some downsides:
- The code is more complex, due to having to be index-independent.
- The code is probably slower due to having to do comparisons "index-
  independently". I have not tested this, as correctness was my priority
  for this patch. A future update could look to improve the speed,
  if that is desired.
- This is technically a breaking API change, since it changes the
  returned values of `rna_Mesh_unit_test_compare`. I don't think that
  there are many people (if any) using this, besides our own unit tests.

All tests that pass with `BKE_mesh_cmp` still pass with the new version.

**NOTE:**
Currently, mesh edge indices are allowed to be different in the
comparison, because `BKE_mesh_cmp` also allowed this. There are some
tests which would fail otherwise. These tests should be updated, and
then the corresponding code as well.

I wrote up a more detailed explanation of the algorithm here:
https://hackmd.io/@bo-JY945TOmvepQ1tAWy6w/SyuaFtay6

Pull Request: https://projects.blender.org/blender/blender/pulls/112794
2023-11-27 16:10:43 +01:00
Lukas Tönne
227a4eae77 Make ImplicitSharingPtr constructor from data pointer explicit
The ImplicitSharingPtr has an implicit constructor for raw pointers.
This has unintended effects when comparing an ImplicitSharingPtr to a
raw pointer: The raw pointer is implicitly converted to the shared
pointer (without change in refcount) and when going out of scope will
decrement user count, eventually freeing the data.

Conversion from raw pointer to shared pointer should not happen
implicitly. The constructor is made explicit now. This requires a little
more boilerplate when constructing a sharing pointer. A special
constructor for the nullptr is added so comparison with nullptr can
still happen without writing out a constructor.

Pull Request: https://projects.blender.org/blender/blender/pulls/115476
2023-11-27 15:53:29 +01:00
Iliya Katueshenock
86b1d6de34 Cleanup: formatting
Pull Request: https://projects.blender.org/blender/blender/pulls/115377
2023-11-26 14:20:41 +01:00
Iliya Katueshenock
86fb922f54 Cleanup: BLI: add assertion for uninitialized boolean in IndexMask.from_predicate
Not properly initialized booleans cause problems due to the branchless increment.

Pull Request: https://projects.blender.org/blender/blender/pulls/115338
2023-11-24 10:30:18 +01:00
Hans Goudey
876b624320 Cleanup: Correct offset indices slice assert
If the slice is empty, there doesn't have to be enough space for i
2023-11-22 15:48:42 -05:00
Hans Goudey
9a28852494 Cleanup: Generalize some attribute propagation functions
Add a function that copies selected values to groups of values in the
result array. Add a runtime-typed version and a version for affecting
all attributes. Also make the "gather_group_to_group" follow the
same pattern.
2023-11-22 15:48:42 -05:00
Hans Goudey
f3ba4115f2 Cleanup: Slightly generalize attribute gather function
Move implementation to array_utils namespace, use function
to gather all attributes at once.
2023-11-22 14:35:53 -05:00
Hans Goudey
ba1c8fe6a5 Mesh: Improve remesh attribute transfer
Currently we have options to transfer the paint mask, face sets, and
color attributes to the new mesh created by voxel remesh. This doesn't
make use of the generic attribute design, where it would be clearer to
transfer all attributes with the same methods. That's reflected in the
code as well-- we do duplicate work for the mask and vertex colors, for
example.

This commit replaces the transfer options with a single checkbox for
all attributes. All attribute types on all domains are transferred with
basically the same method as the "Sample Nearest" node from geometry
nodes-- they take the value from the nearest source element of the same
domain. Face corners are handled differently than before. Instead of
retrieving the mixed value of all the corners from the nearest source
vertex, the value from the nearest corner of the nearest face.

---

Some timing information, showing that when interpolating the same
data, the attribute propagation is significantly faster than before.
Edge and corner attributes would add some cost (edges more than
corners), but might not always be present.

Before
```
voxel_remesh_exec: 3834.63 ms
BKE_shrinkwrap_remesh_target_project: 1141.17 ms
BKE_mesh_remesh_reproject_paint_mask: 689.35 ms
BKE_remesh_reproject_sculpt_face_sets: 257.14 ms
BKE_remesh_reproject_vertex_paint: 54.64 ms
BKE_mesh_smooth_flag_set: 0.15 ms
```

After
```
voxel_remesh_exec: 3339.32 ms
BKE_shrinkwrap_remesh_target_project: 1158.76 ms
mesh_remesh_reproject_attributes: 517.52 ms
```

Pull Request: https://projects.blender.org/blender/blender/pulls/115116
2023-11-22 15:21:58 +01:00
Aras Pranckevicius
8a75b54735 BLI: change timeit to use fmtlib instead of direct cout
Especially on windows, direct output to `cout` via `<<` is very expensive.
Instead, use fmtlib to do all formatting into a no-alloc `fmt::memory_buffer`,
and output that with one call to `cout`.

timeit utilities are not used much by default, but during development or
profiling one often uncomments macros like `DEBUG_TIME` that then enable
`SCOPED_TIMER` or `SCOPED_TIMER_AVERAGED`.

Having one `SCOPED_TIMER_AVERAGED` inside sequencer `draw_channels`, with
empty timeline and all default channels; the overhead in % of `draw_channels`
duration of said scoped timer before and after this change:

- Windows: 29% -> 5%
- Mac: 5.0% -> 4.4%

Pull Request: https://projects.blender.org/blender/blender/pulls/115233
2023-11-21 18:35:42 +01:00
Iliya Katueshenock
fc30a4b6a5 Fix: BLI: Math: Build error for bad overload-lookup
Fix for unreported build error.

Pull Request: https://projects.blender.org/blender/blender/pulls/115095
2023-11-21 15:58:17 +01:00
Hans Goudey
29f3d54e91 Cleanup: Grammar in comments
- it's self vs. itself
- boiler plate vs boilerplate
2023-11-21 09:42:39 -05:00
Jacques Lucke
813078daec BLI: support checking if two maps are the same 2023-11-21 13:38:43 +01:00
Jacques Lucke
a976cf4876 Cleanup: reduce boilerplate for equality operators for structs
Pull Request: https://projects.blender.org/blender/blender/pulls/115088
2023-11-20 09:39:13 +01:00
Hoshinova
0b11c591ec Nodes: Merge Musgrave node into Noise node
This path merges the Musgrave and Noise Texture nodes into a single
combined Noise Texture node. The reasoning is that both nodes
intrinsically do the same thing, which is the layering of Perlin noise
derivatives to produce fractal noise. So the patch de-duplicates code
and unifies the use of fractal noise for the end use.

Since the Noise node had a Distortion input and a Color output, while
the Musgrave node did not, those are now available to the Musgrave types
as new functionalities.

The Dimension input of the Musgrave node is analogous to the Roughness
input of the Noise node, so both inputs were unified to follow the same
behavior of the Roughness input, which is arguable more intuitive to
control. Similarly, the Detail input was slightly different across both
nodes, since the Noise node evaluated one extra layer of noise. This was
also unified to follow the behavior of the Noise node.

The patch, coincidentally fixes an unreported bug causing repeated
output for certain noise types and another floating precision bug
#112180.

The versioning code implemented with this patch ensures backward
compatibility for both the Musgrave and Noise Texture nodes. When
opening older Blender files in Blender 4.1 the output of both nodes are
guaranteed to always be exactly identical to that of Blender files
created before the nodes were merged in all cases.

Forward compatibility with Blender 4.0 is implemented by #114236.
Forward compatibility with Blender 3.6 LTS is implemented by #115015.

Pull Request: #111187
2023-11-18 09:40:44 +01:00
Bastien Montagne
6ce5f87d5b BLI_fileops rename unittests: add tests for directory rename.
Pull Request: https://projects.blender.org/blender/blender/pulls/115073
2023-11-17 22:53:58 +01:00
Bastien Montagne
cb06b30587 Add 'rename' unittest for BLI_fileops' BLI_rename/_overwrite().
Pull Request: https://projects.blender.org/blender/blender/pulls/115067
2023-11-17 20:52:03 +01:00
Harley Acheson
07820b0703 UI: File Browser Display Narrowing
Change the format of columns, and remove columns, as the File Browser
window narrows.

Pull Request: https://projects.blender.org/blender/blender/pulls/112464
2023-11-17 00:11:40 +01:00
Christoph Lendenfeld
f52d026151 BLI: Two digit ms precision for SCOPED_TIMER macro
When using `SCOPED_TIMER` or `SCOPED_TIMER_AVERAGED`
the display would switch from ns to ms
once the value is over 0.1 ms with a precision of 1.
So when the timer value is hovering in the range of 0.1 - 0.2 ms it is
not giving any useful information.

Fix this by adding another digit to the precision of ms.

Pull Request: https://projects.blender.org/blender/blender/pulls/114724
2023-11-16 10:42:35 +01:00
Ray Molenkamp
a277ca3d3d Fix #114906 invalid reuse of shell_link
shell_link was being reused, sometimes returning the previous
data causing links that were not ours to be updated.

Co-authored-by: Harley Acheson <harley.acheson@gmail.com>
Pull Request: https://projects.blender.org/blender/blender/pulls/114924
2023-11-15 18:32:45 +01:00
Hans Goudey
5371f5516c Cleanup: Add const to MutableSpan methods
Since the span is just a reference to data owned elsewhere, changing the
values doesn't change the span itself.
2023-11-13 19:09:09 +01:00
Iliya Katueshenock
4f03d502e6 Cleanup: Remove iostreams include from timeit header
Continuation of #110986

Pull Request: https://projects.blender.org/blender/blender/pulls/111010
2023-11-13 10:42:29 +01:00
Jeroen Bakker
37d04f030d Cleanup: Make format 2023-11-13 08:12:39 +01:00
Ray Molenkamp
3816fcd861 Fix: build error with MSVC
BLI_math_base.h is still required to get defines like M_PI
2023-11-12 08:36:39 -07:00
Hans Goudey
6273205c93 Fix: Debug build error after previous commit
To avoid including the C header in the C++ header, reference
the define by name for now.
2023-11-11 22:15:02 +01:00
Hans Goudey
583bd9662e Cleanup: Remove C header from C++ math header
Remove some redundancy between headers, more fully
separate the C++ headers from the older C headers.

Pull Request: https://projects.blender.org/blender/blender/pulls/114705
2023-11-11 21:09:54 +01:00
Hans Goudey
70074a7d46 Cleanup: Follow style guide in new math function, use std:: namespace 2023-11-10 14:21:43 +01:00
Germano Cavalcante
9bf673a37d Fix #114215: Scanfill sometimes fails to identify holes
The problem is in the way of identifying "fast" intersections through bounds.

In the existing code, before testing the intersections (to identify
holes) the polys are sorted according to the bounds
(in the order x1 < x2 || y1 < y2).

Then a for loop is used on the order returned by sort.

Each time the bound of a polygon intersects with another, it is joined
and the bound is added.

The problem with this solution is that some bounds may not intersect
with the first, but could intersect one that is joined to the first,
which, as it is cleared, makes the intersection undetected.

The solution is to remove this code with `qsort` and create a
"target_map" that identifies a source polygon and a dest polygon.

Pull Request: https://projects.blender.org/blender/blender/pulls/114600
2023-11-10 12:56:20 +01:00
Campbell Barton
a615dcdfa8 Cleanup: add missing header to CMake, sort file lists 2023-11-10 09:40:05 +11:00
Campbell Barton
536e5323f5 Cleanup: suppress UBSAN undefined behavior warnings 2023-11-09 17:28:39 +11:00
Campbell Barton
58ea0e051f Cleanup: spelling in comments 2023-11-09 09:54:28 +11:00
Aras Pranckevicius
a70b42d1cd Cleanup: remove unused spherical harmonics functions
Looks like these were only used for CPU-based viewport shading
in pre-2.80 Blender. Which no longer exists.
2023-11-07 21:16:47 +02:00
Aras Pranckevicius
bdddb3b14e Cleanup: remove unused form_factor_ functions
Looks like these were only used for CPU-based ambient occlusion effect
in pre-2.80 Blender. Which no longer exists.
2023-11-07 21:11:25 +02:00
Aras Pranckevicius
13dbeac13c cleanup: remove "sa" acos/asin/sqrt functions and use "safe_" variants
Cleanup talked about in the previous semi-related PR, #114501

- saacos, saasin, sasqrt have been 100% identical to saacosf,
  saasinf, sasqrtf since 2012.
- For all the above, there exist more intuitively named safe_acosf,
  safe_asinf, safe_sqrtf that do the same thing, so switch all code to those.

Pull Request: https://projects.blender.org/blender/blender/pulls/114593
2023-11-07 19:49:01 +01:00
Aras Pranckevicius
b1c7b573c0 mesh: use faster acos() variant in normals calculation
Function                         Module   Inclusive Time  Exclusive Time
--------------------------------------------------------------------------
  mesh_render_data_update_normals  blender  297.51          0.00

  315 -> 297

acos() usage in all places related to normal calculations shows up in the
profiler. Given that "angle between faces" is only additional heuristic
weight in there (the effect of it at all is very subtle), approximate but
faster version of acos() might be just fine. Especially since some other
parts of Blender (e.g. mikktspace) use approximate acos in a conceptually
the same part.

- Adds safe_acos_approx() to BLI_math_base.hh. Implementation the same
  as already exists in Cycles; max error 0.00258 degrees. Between 2x and 4x
  faster in my tests.
- Changes all normals related calculations to use the function above instead
  of saacos.

Computing normals on a Stanford Lucy (14m verts) mesh:
- Mac (arm64, M1 Max): 247ms -> 229ms
- Win (x64, Ryzen 5950X): 276ms -> 250ms

All places that are about "normal calculation" were changed, including e.g.
Corrective Smooth modifier. Applying that one to the same 14m vertices mesh,
Mac M1 Max: 9.96s -> 9.76s

Tiny changes in several test output expectations w.r.t. normals are
observed, these were reviewed and updated expectations checked in svn.

Pull Request: https://projects.blender.org/blender/blender/pulls/114501
2023-11-07 18:22:17 +01:00
Campbell Barton
611930e5a8 Cleanup: use std::min/max instead of MIN2/MAX2 macros 2023-11-07 16:33:19 +11:00
Campbell Barton
6297bbe931 License headers: attribute copyright to "Blender Authors"
See #110784, it seems that merging functionality reintroduced the old
convention.
2023-11-07 15:42:52 +11:00
Campbell Barton
aaf05c2497 Cleanup: various C++ changes (use nullptr, function style casts) 2023-11-07 11:35:16 +11:00
Campbell Barton
1e66938d7a Cleanup: spelling in comments, format 2023-11-07 11:35:16 +11:00
Aras Pranckevicius
6e4adbe694 Cleanup: use_holes arg to isect_point_poly_v2 has been ignored since 2013
07851dd8df made the use_holes argument be no longer used.
2023-11-06 20:51:21 +02:00