Commit Graph

84 Commits

Author SHA1 Message Date
Hans Goudey 756dea7ca1 Cleanup: PBVH: Remove redundant storage of face set metadata
These two values are already stored in the mesh, and they have no
relation to the PBVH's goal as an acceleration structure. Similar to
a6a2af5fdd, remove the values from the PBVH and
just access them from the mesh as necessary.

Pair-reviewed in person with Sergey
2023-11-01 18:20:43 +01:00
Hans Goudey 40080f618c Sculpt: Use C++ Set to store PBVH Node BMesh elements
Mainly to simplify code and also add some add type safety, replace
`GSet` with `blender::Set` for the storage of BMesh triangles and
vertices on each PBVH node. Some initial tests point to better
performance too, but the numbers are hard to verify so far.

Because of the larger `PBVHNode`, memory usage slightly increases
(observed a 2% increase with a 1M face grid) for regular Mesh sculpting,
but it seems `Set` is more memory efficient than `GSet`, because I also
observed a 10% decrease in memory usage for dynamic topology.
In the future nodes can be split in a more data-oriented fashion to
reduce memory usage overall.

This also makes it simpler to switch to another type in the future.

Pull Request: https://projects.blender.org/blender/blender/pulls/113907
2023-10-19 14:18:15 +02:00
Hans Goudey 3e81f66998 Cleanup: Use C++ arrays for SculptUndoNode
Significantly reduce the amount of manual memory management by
replacing owning pointers with `blender::Array`. This also simplifies
counting the size of the undo steps and iterating over the array values
in some cases.
2023-10-13 09:44:35 +02:00
Hans Goudey de47c2a8b4 Cleanup: Use const pbvh node pointers for some accessor functions 2023-10-12 14:51:51 +02:00
Hans Goudey 9ab290e1b3 Cleanup: Simplify sculpt faces mask undo loop
This is already separated per PBVH type, we can make use of that
to simplify the loops for the types with the aim of sharing code in
a simpler and more performance-friendly way. Ideally this could
be written as `array_utils::scatter`, but we don't have that utility
function now, so just write the raw loop for now.
2023-10-12 14:51:51 +02:00
Hans Goudey cbb4ae80ba Cleanup: Return PBVH node vertex indices with span instead of pointer 2023-10-12 14:29:34 +02:00
Hans Goudey a6a2af5fdd Fix: Sculpt dynamic topology doesn't draw active/render color attribute
Pass the mesh to the drawing functions so it doesn't have to be
retrieved from the PBVH. It's nice to rely less on the PBVH `me`
pointer, since it's a fairly ugly "back pointer" which isn't necessarily
good design.
2023-10-10 18:22:50 +02:00
Sergey Sharybin 9a44445667 Fix assert in PBVH face set drawing code
The assert was assuming that the attribute request is properly
initialized and that was not the case: the "special" data layers
like coordinates, normals, masks, and face sets did not initialize
domain in the attribute request.

The domain is now properly initialized. As well as there is an
assert added in other PBVH types for the face sets. It is possible
to add asserts in more places, but it is not directly related to
this CL.

Pull Request: https://projects.blender.org/blender/blender/pulls/113354
2023-10-06 18:05:59 +02:00
Hans Goudey b595cd296b Merge branch 'blender-v4.0-release' 2023-09-28 09:00:31 -04:00
Hans Goudey 79d0b71751 Fix #112957: Incorrect PBVH multires grid iteration
The grid was pointer was moved to the next element at every loop, which
made `vi.grid` and `vi.mask` disagree. Instead, iterate the grid before
accessing data, so the pointers agree. To avoid unrolling the first loop
iteration or adding a branch for the first iteration, start the grid at
a -1 offset.

Pull Request: https://projects.blender.org/blender/blender/pulls/113015
2023-09-28 14:56:26 +02:00
Hans Goudey 9e5cf73b4d Cleanup: Make PBVH iter mask not a pointer
After 97f2b01ea9, the iterator's mask data is just read only,
so there's no point in storing it as a pointer. This simplifies the code
using the mask data, since the default only needs to be handled once.
2023-09-27 18:21:36 -04:00
Hans Goudey ca447ef542 Cleanup: Move sculpt mask update to a separate function
There's no reason to use a general "update vertex data" function,
that just confuses things.
2023-09-27 17:11:28 -04:00
Hans Goudey a3698d0577 Cleanup: Move sculpt mask update to a separate function
There's no reason to use a general "update vertex data" function,
that just confuses things.
2023-09-27 17:10:24 -04:00
Hans Goudey 97f2b01ea9 Fix #112351: Sculpt implicit sharing thread safety crash
Currently the iteration over a PBVH node's vertices retrieves mutable
access to the mask custom data layer. This isn't threadsafe, but it is
done in the multithreaded loops over all nodes.

In general, we need to be more careful and conservative about storage
of non-const pointers to mesh data. Ideally we would only have one
mutable reference to a resource at a time. And we should avoid doing
work like looking up custom data layers more than we need to.

To that end, make the pointer to the custom data layer used everywhere
const, and retrieve mutable access before parallel node iteration with
a specific function, and write to the mask data with that in mind.

This pushes us in the direction of sharing less code per PBVH type.
In my opinion that's a good thing, because we can actually optimize for
each type. For example, `write_mask_data` gives a picture of
how each of these hot loops could become much simpler.

Pull Request: https://projects.blender.org/blender/blender/pulls/112690
2023-09-26 14:21:07 +02:00
Campbell Barton 4fc5d287ac Cleanup: doxygen parameters, blank comment lines 2023-09-08 16:53:30 +10:00
Hans Goudey afdbb734cf Cleanup: Avoid mesh positions copy applying sculpt to shape key
`BKE_pbvh_vert_coords_alloc` is unnecessary after 1af62cb3bf
since PBVH vertex positions are stored in a contiguous array.
2023-09-04 22:23:26 -04:00
Hans Goudey 2228006d5b Subdiv: Use index instead of pointer for grid to face map
Switch from face pointers to indices, with the following benefits:
- Halve memory usage required for array (saves 16 MB with 1 million quads).
- Allow better code reuse with index-based utilities.
- Ease future replacement of `SubdivCCGFace` with mesh's face offsets.
- Allow future replacement of array with `Mesh::corner_to_face_map()`
- Potentially lower memory bandwidth required during multires evaluation
- Simplify retrieval of index in `BKE_subdiv_ccg_grid_to_face_index`.

For clarity, `gridfaces` was renamed to `grid_to_face_map`.

Pull Request: https://projects.blender.org/blender/blender/pulls/111078
2023-08-31 19:40:39 +02:00
Hans Goudey 111e586424 Cleanup: Sculpt: Avoid unnecessarily changing vertex normals
Keeping a mutable reference to vertex normals for the entire lifetime
of the PBVH structure makes caching the normals and sharing the cache
harder than it should be. Generally code is safer when we reduce the
number of mutable references to data.

Currently the normals are modified in two places. First is the sculpt
mesh normal recalculation. There we can just retrieve the normals from
the mesh each time. Second is the restore from an undo step. That is
unnecessary because the normals are marked for recalculation anyway.
It doesn't even make much sense to store the normals in an undo step
when we can easily recalculate them based on new positions.

This change helps with #110479. These were also the last place that
kept a mutable reference to normals. I tested undo and redo after
sculpting, and it works well for each PBVH type.

Pull Request: https://projects.blender.org/blender/blender/pulls/111470
2023-08-24 16:47:55 +02:00
Hans Goudey 1857df8d5b Cleanup: Use FunctionRef for PBVH filtering callback
Call functions directly in lambdas rather than passing their
arguments in a separate void * argument. This can be changed
more in the future to move callback arguments out of smaller
structs.
2023-08-22 15:06:42 -04:00
Campbell Barton e955c94ed3 License Headers: Set copyright to "Blender Authors", add AUTHORS
Listing the "Blender Foundation" as copyright holder implied the Blender
Foundation holds copyright to files which may include work from many
developers.

While keeping copyright on headers makes sense for isolated libraries,
Blender's own code may be refactored or moved between files in a way
that makes the per file copyright holders less meaningful.

Copyright references to the "Blender Foundation" have been replaced with
"Blender Authors", with the exception of `./extern/` since these this
contains libraries which are more isolated, any changed to license
headers there can be handled on a case-by-case basis.

Some directories in `./intern/` have also been excluded:

- `./intern/cycles/` it's own `AUTHORS` file is planned.
- `./intern/opensubdiv/`.

An "AUTHORS" file has been added, using the chromium projects authors
file as a template.

Design task: #110784

Ref !110783.
2023-08-16 00:20:26 +10:00
Hans Goudey b04a72866d Revert "Multires: Simplify grids normals update and grid to face map"
This reverts commit c97178fa2e.

This was committed to main by mistake, it was meant for a local branch.
2023-08-12 16:38:39 -04:00
Hans Goudey c97178fa2e Multires: Simplify grids normals update and grid to face map
The goal is to move to more data oriented design, reducing memory
usage and simplifying code by clarifying data access, avoiding
unnecessary levels of abstraction, and reusing code.

- Simplify threading with the C++ threading API
- Pass the faces to update with an IndexMask instead of a pointer array
  - IndexMask uses less memory and simplifies masking and iteration
- Store the grid to face map with indices instead of pointers
  - Now this is exactly the same as `build_loop_to_face_map`
2023-08-12 12:20:10 -04:00
Hans Goudey 38fc111fc9 Cleanup: Clarify string usage in PBVH draw
Use StringRef where possible to avoid copying strings, avoid
redundant string returns, and use std::string for attribute
request names now that all the relevant code is C++.
2023-07-29 23:47:13 -04:00
Hans Goudey a632f1ddfd Sculpt: Remove dynamic topology "Smooth Shading" option
Dynamic topology drawing can now use the smooth status saved in each
edge. Because of that, the "Smooth Shading" draw option is unnecessary
and just adds confusion because of inconsistency between dynamic
topology drawing and other modes.

Fixes #109191

Pull Request: https://projects.blender.org/blender/blender/pulls/110548
2023-07-28 13:44:01 +02:00
Hans Goudey 82f67a1b58 Cleanup: Use const reference for PBVH draw arguments 2023-07-27 23:38:28 -04:00
Hans Goudey 1be70f22cc Cleanup: Use C++ containers and spans for some PBVH data
One thing to point out is that `PBVH::nodes` is now stored in a `Vector`
which replaces the manual amortized growth. That requires explicitly
setting the defaults of PBVHNode fields for default initialization.

Similar to f0b53777c8
2023-07-27 17:47:34 -04:00
Hans Goudey 81e687b49d Cleanup: Use const for PBVH node grid indices 2023-07-27 17:47:34 -04:00
Hans Goudey 5e9ea9243b Mesh: Rename "polys" to "faces"
Implements part of #101689.

The "poly" name was chosen to distinguish the `MLoop` + `MPoly`
combination from the `MFace` struct it replaced. Those two structures
persisted together for a long time, but nowadays `MPoly` is gone, and
`MFace` is only used in some legacy code like the particle system.

To avoid unnecessarily using a different term, increase consistency
with the UI and with BMesh, and generally make code a bit easier to
read, this commit replaces the `poly` term with `poly`. Most variables
that use the term are renamed too. `Mesh.totface` and `Mesh.fdata` now
have a `_legacy` suffix to reduce confusion. In a next step, `pdata`
can be renamed to `face_data` as well.

Pull Request: https://projects.blender.org/blender/blender/pulls/109819
2023-07-24 22:06:55 +02:00
Hans Goudey c728fa0663 Cleanup: Simplify retrieving color attribute, improve const correctness
Make it harder to retrieve a mutable attribute from const a const mesh,
and use the attribute search function to check multiple domains and
colors at once.
2023-07-11 16:50:34 -04:00
Hans Goudey f0b53777c8 Cleanup: Use spans and float3 to store sculpt and PBVH vert positions
Also store the allocated deformed positions in a separate array in PBVH,
so there isn't one pointer that _sometimes_ has ownership of its data.

Pull Request: https://projects.blender.org/blender/blender/pulls/109981
2023-07-11 21:01:53 +02:00
Ray Molenkamp c677f791f0 Cleanup: Make format 2023-07-07 20:41:57 -06:00
Joseph Eagar 0b01b7c1fa Sculpt: Fix #109555: More floating point error fixes
* Renamed BKE_pbvh_raycast_project_ray_root to
          BKE_pbvh_clip_ray_ortho for greater
	  clarity.
* BKE_pbvh_clip_ray_ortho no longer strictly clips
  within the input ray interval.  This is not necassary
  for orthographic views and was too prone to floating
  point error.  The function is only called to clip
  brush rays for orthographic views so this is acceptable.
2023-07-07 18:53:06 -07:00
Joseph Eagar f031d7fbf3 Cleanup: remove duplicate function declaration. 2023-07-03 20:11:07 -07:00
Joseph Eagar 7e2659e4ab Cleanup: Split BKE_pbvh.h into BKE_pbvh_api.hh
Split much of BKE_pbvh.h into BKE_pbvh_api.hh.
BKE_pbvh.h is included by BKE_paint.h, which in
turn is included by large amounts of code including
RNA.

This makes it extremely difficult to change
or clean up the PBVH API, since each modification
of BKE_pbvh.h can take 20-30 minutes to compile,
even on a quad-core system with an SSD. This
commit fixes that by moving most of BKE_pbvh.h
into another file and just having the core,
external-facing interfaces in BKE_pbvh.h.
2023-07-03 20:01:04 -07:00