Page MenuHome

Port SculptVertRef refactor to master
ClosedPublic

Authored by Joseph Eagar (joeedh) on Mar 8 2022, 12:50 AM.

Details

Summary

This is a port of sculpt-dev's SculptVertRef refactor
to master. SculptVertRef is a structure that abstracts
the concept of a vertex in the sculpt code; it's simply
an intptr_t wrapped in a struct.

For PBVH_FACES and PBVH_GRIDS this struct stores a
vertex index, but for BMesh it stores a direct pointer
to a BMVert. The intptr_t is wrapped in a struct to prevent
the accidental usage of it as an index.

There are many reasons to do this:

  • Right now BMesh verts are not logical sculpt verts; to use the sculpt API they must first be converted to indices. This requires a lot of indirect lookups into tables, leading to performance loss. It has also led to greater code complexity and duplication.
  • Having an abstract vertex type makes it feasible to have one unified temporary attribute API for all three PBVH modes, which in turn made it rather trivial to port sculpt brushes to DynTopo in sculpt-dev (e.g. the layer brush, draw sharp, the smooth brushes, the paint brushes, etc). This attribute API will be in a future patch.
  • We need to do this anyway for the eventual move to C++.

Diff Detail

Repository
rB Blender
Branch
temp-sculpt-vert-ref (branched from master)
Build Status
Buildable 23099
Build 23099: arc lint + arc unit

Event Timeline

Joseph Eagar (joeedh) requested review of this revision.Mar 8 2022, 12:50 AM
Joseph Eagar (joeedh) created this revision.
Joseph Eagar (joeedh) edited the summary of this revision. (Show Details)Mar 8 2022, 12:51 AM
Brecht Van Lommel (brecht) requested changes to this revision.Mar 10 2022, 4:39 PM
Brecht Van Lommel (brecht) added inline comments.
source/blender/blenkernel/BKE_paint.h
418–420

I would not start storing things twice like this, doesn't seem like that simplifies the code or would help performance much.

source/blender/blenkernel/BKE_pbvh.h
57–59

This type is unused, can be removed I think.

57–71

Can we change the Sculpt prefix to PBVH?

Otherwise is inconsistent with BKE_pbvh_make_vref functions, and I guess this may find a use in paint modes also.

142

I these function names should be more verbose, consistent and have some comment explaining things.

Something like:

PBVHVertRef BKE_pbvh_ref_from_mesh_vertex_index(const int v);
PBVHVertRef BKE_pbvh_ref_from_bmesh_vertex(BMVert *v);
PBVHVertRef BKE_pbvh_ref_from_pbvh_vertex_index(PBVH *pbvh, const int v);

int BKE_pbvh_ref_to_mesh_vertex_index(PBVHVertRef ref);
BMVert *BKE_pbvh_ref_to_bmesh_vertex(PBVHVertRef ref);
int BKE_pbvh_ref_to_pbvh_vertex_index(PBVH *pbvh, PBVHVertRef ref);
160–161

Can this be an inline function instead of a macro?

164

inex -> index

166

This function is not defined anywhere.

170

This function is not defined anywhere.

source/blender/editors/sculpt_paint/sculpt.c
125

Rename index since it's no longer a PBVH vertex index.

131–141

I don't think there should be raw access to .i like this here and in many other places that follow. It can instead use functions like BKE_pbvh_ref_to_mesh_vertex_index(ref) and BKE_pbvh_ref_to_bmesh_vertex(ref).

This revision now requires changes to proceed.Mar 10 2022, 4:39 PM

Update to latest master.

  • Merge with master.
  • Renamed SculptVertRef to PBVHVertRef
  • Inlined a few functions and macros.
Brecht Van Lommel (brecht) requested changes to this revision.Jul 18 2022, 1:56 PM

Comments were not marked as done and the last one was not addressed, so assuming this is not yet ready for review.

This revision now requires changes to proceed.Jul 18 2022, 1:56 PM
Joseph Eagar (joeedh) marked 9 inline comments as done.

Merge with master.
Removed duplicated data in boundary brush.
Remove PBVHElem struct.

source/blender/blenkernel/BKE_pbvh.h
142

Those are way too long. 35 characters is really pushing it. People are simply not going to use functions that long; it becomes a nightmare.

160–161

They can, yes, I'll do that.

166

That'll be in the edge boundary patch. I guess I should remove it from this one.

source/blender/editors/sculpt_paint/sculpt.c
131–141

It's an internal API function, so I think it's safe.

Brecht Van Lommel (brecht) requested changes to this revision.Jul 20 2022, 5:16 PM
Brecht Van Lommel (brecht) added inline comments.
source/blender/blenkernel/BKE_paint.h
418–420

This comment was not addressed, why store two types of vertex references here and in many other places?

Is it really performance critical in all of them, or could you in many cases just store one and compute the index when needed?

437–444

For example these, it doesn't seem obviously helpful for performance to store both.

In fact the indices are not read anywhere, only set.

572

Remove index from the name.

597–598

Remove index from the name.

source/blender/blenkernel/BKE_pbvh.h
61–67

Follow multiline comment style:

/* These structs represent logical verts/edges/faces.
 * for PBVH_GRIDS and PBVH_FACES they store integer
 * offsets, PBVH_BMESH stores pointers.
 *
 * The idea is to enforce stronger type checking by encapsulating
 * intptr_t's in structs. */
142

People are also going to have trouble understanding what all these different types of indices and references mean.

Expecting the caller to cast a pointer to intptr_t is not a good API. At a minimum this could be use function overloading.

PBVHVertRef BKE_pbvh_vert_ref(int index)
PBVHVertRef BKE_pbvh_vert_ref(BMVert *v)
PBVHVertRef BKE_pbvh_vert_ref_from_pbvh_index(PBVH *pbvh, const int v);

Or even make it a C++ class with methods:

PBVHVertRef::from_mesh(vi)
PBVHVertRef::from_bmesh(v)
ref.to_mesh()
ref.to_bmesh()
if (ref.empty()) ...
161

-1 -> PBVH_REF_NONE

180

-1 -> PBVH_REF_NONE

199

-1 -> PBVH_REF_NONE

source/blender/editors/sculpt_paint/sculpt.c
131–141

It's still really confusing to me to casually cast between integers and pointers in many places.

It would help if this was a C++ class with meshes so you could to vertex.to_mesh(), vertex.to_bmesh(), or perhaps a union so it can be vertex.mesh_index, vertex.bmesh_vertex.

195–203

These two functions I guess only work for PBVH_FACES? Now that they take a generic PBVHVertRef you might expect otherwise.

220

Rename index to vertex here and in various other places..

258

This I guess is wrong, using index.i here should be cast to BMVert*? That's the kind of bug I want to prevent.

351–353

Should be BMVert* instead?

5723–5724

Remove index from the name.

5739

Remove index from the name.

This revision now requires changes to proceed.Jul 20 2022, 5:16 PM
Joseph Eagar (joeedh) marked 13 inline comments as done.
source/blender/blenkernel/BKE_paint.h
418–420

I removed it from the code, I just forgot to remove it from the struct. Sorry about that.

437–444

I'll remove this one as well.

source/blender/blenkernel/BKE_pbvh.h
61–67

Yeesh I had completely missed that.

142

Oh, I see what you mean. This by design. Only the core sculpt API methods are supposed to cast from PBVHVertRef to BMVert. PBVHVertRef is supposed to be an opaque vertex type.

142

I

source/blender/editors/sculpt_paint/sculpt.c
131–141

This is the core API. I could add comments that normal sculpt code shouldn't do this.

195–203

They actually do work with all types, since ss->persistent_base only exists for PBVH_FACES/PBVH_GRIDS while ss->shapekey_active and ss->mvert only exist for PBVH_FACES.

I've cleaned up the code to be clearer.

I'd still prefer to have integrer <-> pointer casting wrapped in a function, but I don't want to hold up the patch over that. Other than that it looks good to me.

This revision is now accepted and ready to land.Jul 25 2022, 8:19 PM

I guess it's best to land this in Blender 3.4 after the master branch gets switched to that on Wednesday.

This revision was automatically updated to reflect the committed changes.