Commit Graph

84 Commits

Author SHA1 Message Date
Hans Goudey 7c01355cad Cleanup: Use forward declaration headers in a few places 2024-03-27 22:25:08 -04:00
Hans Goudey 51c738da1b Cleanup: Mesh: Use updated "corner" name for variables 2024-03-12 14:55:58 -04:00
Hans Goudey 9cafccf004 Cleanup: Sculpt: Unify face PBVH node face index access
Remove one function from the API.
2024-03-12 14:55:58 -04:00
Pratik Borhade a6258d6b8b Cleanup: rename BKE_pbvh_node_mark_normals_update
This was discussed in !117192

Pull Request: https://projects.blender.org/blender/blender/pulls/117498
2024-01-26 11:24:55 +01:00
Hans Goudey aaa25bc882 Sculpt: Improve mesh normals update performance
Instead of storing a boolean "update tag" for every vertex, just
recalculate the normals of all the faces and vertices in affected PBVH
nodes. This avoids the overhead of tracking position updates at the
vertex level, and simplifies the normal calculation, since we can
just calculate values for all the normals in a node.

The main way we gain performance is avoiding building a `VectorSet`
for all vertices and faces that need updates in the entire mesh. That
process had to be single threaded, and was a large bottleneck when many
vertices were affected at a time.

That `VectorSet` was necessary for thread safety deduplication of work
though, because neighboring nodes can't calculate the normals of the
same faces or vertices at the same time. (Normals need to be calculated
for all faces connected to moved vertices and all vertices connected to
those faces). In this PR, we only build a set of the *boundary* faces
and vertices. We calculate those in a separate step, which duplicates
work from the non-boundary calculations, but at least it's threadsafe.

I measured normal recalculation timing when sculpting on a 16 million
vertex mesh. The removal of the `vert_bitmap` array also saves 16 MB
of memory.

| Nodes | Affected Vertices | Before (ms) | After (ms) |
| ----- | ------------ | ----------- | ---------- |
| 4     | 15625        | 0.208       | 0.304      |
| 35    | 136281       | 2.98        | 0.988      |
| 117   | 457156       | 15.0        | 3.21       |
| 2455  | 9583782      | 566         | 84.0       |

Pull Request: https://projects.blender.org/blender/blender/pulls/116209
2024-01-08 18:49:26 +01:00
Hans Goudey 517feec7ea Cleanup: Improve some mesh topology map variable naming
Avoid unnecessary abbreviations like `pmap` or `vemap`.
2024-01-05 10:35:39 -05:00
Hans Goudey d6cfd7d1f4 Cleanup: Remove unnecessary keywords from C++ headers
- Remove unnecessary `struct`
- Use `using` instead of `typedef`
- Remove `void` from `(void)` as function arguments
2024-01-04 15:07:48 -05:00
Hans Goudey edf8a776ac Cleanup: Use forward declarations to replace includes of BKE_attribute.hh
Remove most includes of this header inside other headers, to remove unnecessary
indirect includes which can have a impact on compile times. In the future we may
want more dedicated "_fwd.hh" headers, but until then, this sticks with the
solution in existing code.

Unfortunately it isn't yet possible to remove the include from `BKE_geometry_set.hh`.
2023-12-20 13:25:28 -05:00
Hans Goudey 19001c9e6c Cleanup: Move attribute domain enum to C++ header, use enum class
Each value is now out of the global namespace, so they can be shorter
and easier to read. Most of this commit just adds the necessary casting
and namespace specification. `enum class` can be forward declared since
it has a specified size. We will make use of that in the next commit.
2023-12-20 13:25:28 -05:00
Hans Goudey 4ee151e204 Cleanup: Remove unnecessary function and data in PBVH 2023-12-18 15:36:55 -05:00
Hans Goudey 8bf73a7a97 Cleanup: Tweak PBVH grid faces update retrieval
The function really just gives an index mask of all the faces in the
provided nodes. The multires usage of the function didn't need that,
since it just passed all nodes. Also pass the SubdivCCG directly rather
than the PBVH. And rename the function to make this clearer.
2023-12-18 15:29:17 -05:00
Hans Goudey 3ddba82716 Cleanup: Remove unused PBVH node pointer
Removed in 47f46637be.
Also remove a lost comment and unused forward declarations.
2023-12-16 11:16:10 -05:00
Hans Goudey 488de130b3 Cleanup: Use FunctionRef for PBVH callbacks 2023-12-16 11:14:25 -05:00
Hans Goudey 3d82c9c239 Cleanup: Move more PBVH code to C++ namespaces
Also remove redundant parts of function names.
2023-12-15 22:51:10 -05:00
Hans Goudey c8aecac001 Cleanup: Remove unused sculpt code
Remove abstract edge and face types. The design is to not abstract away
the code data structures like this and focus on sharing code more with the
rest of Blender rather than within sculpt mode.
2023-12-14 18:25:20 -05:00
Hans Goudey b3aca5b28f Cleanup: Simplify PBVH build process slightly
Before this happened as two steps: first allocating the PBVH with a type,
then calculating the BVH and filling it with data. This just confused things,
so change to allocating the struct when building it. Also move the functions
to the C++ namespace, and fix some cases of requiring the PBVH to be set
when it wasn't yet.
2023-12-14 18:20:46 -05:00
Hans Goudey 4c1f766d0d Cleanup: Remove unnecessary PBVH threading settings function
There were just two more places using the C threading API in sculpt code.
Switch them to the C++ API and remove the settings function.
2023-12-14 15:39:33 -05:00
Hans Goudey ce4ec6d42b Cleanup: Tweak PBVH node face indices functions, add comments 2023-12-14 15:31:12 -05:00
Hans Goudey f15bca64f2 Cleanup: Remove useless PBVH update function
This `update_vertex_data` only found nodes with the color update
tag and also added redraw tags. But whenever nodes are marked
for a color update, those redraw tags are already set anyway.
It appears this was meant to solve problems switching active
color attributes during undo and redo, but it doesn't make a
difference when this function is removed.
2023-12-14 15:01:27 -05:00
Hans Goudey 262572a6a0 Cleanup: Move PBVH update functions to C++ namespace 2023-12-14 14:56:25 -05:00
Hans Goudey c53e220aef Cleanup: Use C++ attribute API instead of CustomData API
This gives better asserts in debug builds through use of Span, more
safety when name convention attributes happen to have different types
or domains, and simpler code in some cases. But the main reasoning is to
avoid relying on the specifics of CustomData more to allow us to replace
it in the future.
2023-12-12 18:23:59 -05:00
Hans Goudey 0ad65762cc Cleanup: Remove unnecessary PBVH function
This is only used internally. Externally, it's simpler to fetch the spans instead.
2023-12-12 12:04:44 -05:00
Hans Goudey ba6c07f2d9 Cleanup: Simplify vertex index retrieval in sculpt undo nodes 2023-12-12 11:39:02 -05:00
Hans Goudey 854cdd1180 Cleanup: Use consistent "mesh" variable name (replace "me")
"mesh" reads much better than "me" since "me" is a different word.
There's no reason to avoid using two more characters here. Replacing
all of these at once is better than encountering it repeatedly and
doing the same change bit by bit.
2023-12-08 16:40:06 -05:00
Hans Goudey 251069ae5b Sculpt: Reimplement face sets visibility operator
Iterate over faces instead of vertices, and multithread the logic over
all PBVH nodes. The result is about 3 times faster in my tests;
noticeably snappier for large meshes. Add a new sculpt undo type for
face hide storage (as opposed to vertex storage), and use that only
when the data actually changed, which should reduce memory usage too.
Internally some code is shared with the other visibility operators,
since this operation always affects base mesh faces, most isn't.

Similar to 4e66769ec0
2023-12-06 23:33:46 -05:00
Hans Goudey 7a96c4672c Cleanup: Move BMesh headers to C++
Pull Request: https://projects.blender.org/blender/blender/pulls/115817
2023-12-05 23:01:12 +01:00
Hans Goudey 874f83c42e Cleanup: Use FunctionRef for PBVH batch drawing
Also use const arguments, move a null check from the callback to the
PBVH function, and reorganice the PBVH code to be in a consistent
place in the file and to simplify the logic.
2023-12-05 09:36:13 -05:00
Hans Goudey 8a705ffd11 PBVH: Use C++ Bounds type for node bounds
This is just a bit more ergonomic and works a bit better with our C++
math vector types. Also, during PBVH build, don't store the center of
each triangle/grid. That's redundant and can always be recalculated
from the bounds.
2023-12-04 12:45:23 -05:00
Hans Goudey e9f4f8b52a PBVH: Specialize node bounds update, expose functions
Similar to previous commits (7332a1eb90), use three separate
functions for updating the bounds of different PBVH types. This avoids
the use of the vertex iteration macro. Also make the functions reusable,
so they can be called directly after a brush update in the future.
2023-12-04 08:36:02 -05:00
Hans Goudey 869ea79da0 Cleanup: Return PBVH vertex data arrays as spans 2023-12-04 07:55:47 -05:00
Hans Goudey 8171f719fe Subdiv: Remove unnecessary CCG derived flag structs
The copied the material index and its smoothness to every grid,
resulting in 4 bytes of memory per base mesh face corner. That's
wasteful, since it's trivial to loop up the original data from the base
mesh attributes as necessary. This way we can also avoid the
dereference.
2023-12-04 07:55:47 -05:00
Hans Goudey 0b9ae3df4c Subdiv: Use base mesh faces and topology map for SubdivCCG
Instead of storing a redundant array of faces, use the base mesh faces
and corner_to_face map. This saves 8 bytes per base mesh face with
multires sculpting, and avoids recalculating the topology map whenever
reevaluating the object. There is a lot of other duplicate data between
base meshes and faces, particularly in `MeshTopology`. This is just
a first step of untangling that.
2023-12-04 07:55:47 -05:00
Hans Goudey 881ec8b1ed Cleanup: Return multires grid key by value 2023-12-04 07:55:47 -05:00
Hans Goudey 7332a1eb90 PBVH: Refactor hide and mask update to be data oriented and reusable
Specialize the mask update for each PBVH type, simplifying hot loops,
reducing reliance on complex shared state and clarifying which data is
used. Expose functions to update the visibility and masks tags for a
specific node. It can be helpful to call these after modifying the data
to update the flags while the data is more likely to be in CPU caches.
2023-12-04 07:55:46 -05:00
Hans Goudey f3bfbd9d84 Cleanup: Use const arguments to PBVH functions 2023-12-04 07:55:46 -05:00
Hans Goudey ccab01f97f Subdiv: Store multires sculpt grid visibility in BitGroupVector
Instead of allocating a separate bitmap per grid for the hide status, store
all the bits in a recently added C++ data structure that stores all bits in one
contiguous memory chunk. When nothing is hidden, nothing is allocated
(that saves 32 MB for a 16 million vertex multires sculpt). Intuitively it
could have better performance because of the cache benefits of
contiguous memory, but this is hard to measure. It also has a nicer
API than `BLI_bitmap`.

I discussed this with Sergey in person recently. Most of the changes are
just straightforward refactors. The part that isn't is a change to the "show/hide"
operator to structure it similarly to the mesh handling in 4e66769ec0.

Pull Request: https://projects.blender.org/blender/blender/pulls/115687
2023-12-02 20:05:29 +01:00
Hans Goudey a7afc5b1e8 Cleanup: Pass PBVH node grid indices as Span 2023-12-01 14:40:14 -05:00
Hans Goudey 8c8ea2ec47 Refactor: Sculpt: Clarify PBVH attribute requests
Avoid reusing the custom data type enum with additional values. Instead
use std::variant and type names to properly distinguish between custom
and generic attribute requests. Use a Vector to hold the requests.

Also attempt to simplify the string key building process for requests
and groups of requests in batches. Previously for every PBVH node it
would rebuild the key 3 times, now it only does it once. It's hard to
measure, but that process did show up in profiles, so performance is
probably slightly improved when many nodes are handled at once.
2023-11-30 23:24:11 -05:00
Hans Goudey 2864f3ad3f Cleanup: Remove unused argument to PBVH draw functions
The attributes weren't used by BKE_pbvh_draw_cb.
2023-11-30 23:24:11 -05:00
Hans Goudey e83b1b8ae0 Cleanup: Use object arguments in many drawing related functions 2023-11-30 23:24:11 -05:00
Hans Goudey ed7b914bd8 Cleanup: Move draw_pbvh to a C++ namespace 2023-11-30 23:24:11 -05:00
Hans Goudey 49f676e6c2 Cleanup: Reduce usage of mesh data pointers in PBVH
Pointers to hide status layers and custom data are removed,
since they can be accessed from the mesh as necessary.
Usage of other arrays has been reduced, so the pointers
can eventually be removed.

The reasoning is the same as some other commits in this area:
the goal is less duplication of state, and a more focused design
of the responsibilities of the PBVH class.

Some of the changes are fairly noisy, since we need to add
arguments to functions in a few places. On the nicer side of
things, some functions for syncing the state can be removed.

Not retrieving hide layers with write access also has performance
implications in some cases, since it means the original arrays can
be reused without a copy when they're shared.
2023-11-30 23:24:11 -05:00
Hans Goudey 8b28bb9882 Cleanup: Remove redundant subdiv pointers from PBVH
These are just duplicates of the pointers in `SubdivCCG`, which are
already quickly accessible. Keeping track of the state is too complex
and bloats the responsibilities of the PBVH too much.
2023-11-30 16:40:09 -05:00
Hans Goudey 8c59532106 Cleanup: Use C++ arrays to store subdiv ccg data
Decrease the amount of manual memory management
and pointer arithmetic.
2023-11-29 12:39:33 -05:00
Hans Goudey 63af8dbfca Multires: Use IndexMask for Subdiv CCG face updates
These updates are used to recalculate normals and average values between
grid boundaries during multires sculpting. In main the affected faces
are passed as an array of pointers. Using an `IndexMask` instead reduces
memory usage by 4x per affected face (8 byte pointer to 2 byte integer),
simplifies iteration and threading, and can also improve performance.
Finding which faces are affected is now multithreaded, with its runtime
changing from 0.63 ms to 0.12 ms in a simple test sculpting on a portion
of a 1 million face grid.

Also switch to VectorSet instead of GHash for finding affected adjacent
elements. That's a friendlier data structure that probably has better
performance as well.
2023-11-29 11:10:08 -05:00
Hans Goudey aa2cdf283a Cleanup: Use proper type for subdiv CCG faces 2023-11-29 08:47:58 -05:00
Hans Goudey 4d7274b7f4 Cleanup: Use C++ types for crazyspace deform storage
Use float3, float3x3, and Array for data used for mesh crazyspace
calculation. Propagate the change wherever necessary to not add
more casting to the old C types.

Because `ObjectRuntime` (and therefore `DEGObjectIterData`) became
non-trivial structs, the code that swaps iterators for RNA depsgraph
object iteration had to be changed a bit to be more friendly to C++
memory semantics.

Pull Request: https://projects.blender.org/blender/blender/pulls/114998
2023-11-16 18:29:52 +01:00
Hans Goudey 3d57bc4397 Cleanup: Move several blenkernel headers to C++
Mostly focus on areas where we're already using C++ features,
where combining C and C++ APIs is getting in the way.

Pull Request: https://projects.blender.org/blender/blender/pulls/114972
2023-11-16 11:41:55 +01:00
Sergey Sharybin a4f4cb6c4a Cleanup: Sculpt, use C++ types for PBVH proxy node API
- Use float3 for coordinates inside of proxy
- Use Vector for storing coordinates and proxy nodes
- Use MutableSpan in the API to access proxy nodes

Pull Request: https://projects.blender.org/blender/blender/pulls/114638
2023-11-09 11:00:58 +01:00
Hans Goudey 44b79af4fc Cleanup: Sculpt: Simplify face and face set iteration
This commit removes knowledge about face sets from the PBVH,
and changes iteration over faces to not depend on the PBVH face
iterator abstraction. Though this adds slightly more boilerplate to
iteration over faces, it makes the whole process more data oriented
and allows use of index-based utilities like `gather` and `scatter`
in the mesh case, and simpler iteration over BMesh faces for
dynamic topology.

Setting face sets is now specialized per PBVH type in a few places
in a similar way. The general goal is to reduce branching and function
calls at the lowest level of hot loops, and to make code more aware
of the data structures it uses, both for performance and clarity.

Since the remaining uses of the face iterator are removed,
the iterator itself is removed too.

Related commits:
- 97f2b01ea9
- 756dea7ca1
- a6a2af5fdd

Pull Request: https://projects.blender.org/blender/blender/pulls/114417
2023-11-03 10:27:38 +01:00